c01f91973a0fd6b64f1a5e95281904dc5130aed9
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 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 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-refer-thread-use-nnir nil
122   "*Use nnir to search an entire server when referring threads. A
123 nil value will only search for thread-related articles in the
124 current group."
125   :group 'gnus-thread
126   :type 'boolean)
127
128 (defcustom gnus-summary-make-false-root 'adopt
129   "*nil means that Gnus won't gather loose threads.
130 If the root of a thread has expired or been read in a previous
131 session, the information necessary to build a complete thread has been
132 lost.  Instead of having many small sub-threads from this original thread
133 scattered all over the summary buffer, Gnus can gather them.
134
135 If non-nil, Gnus will try to gather all loose sub-threads from an
136 original thread into one large thread.
137
138 If this variable is non-nil, it should be one of `none', `adopt',
139 `dummy' or `empty'.
140
141 If this variable is `none', Gnus will not make a false root, but just
142 present the sub-threads after another.
143 If this variable is `dummy', Gnus will create a dummy root that will
144 have all the sub-threads as children.
145 If this variable is `adopt', Gnus will make one of the \"children\"
146 the parent and mark all the step-children as such.
147 If this variable is `empty', the \"children\" are printed with empty
148 subject fields.  (Or rather, they will be printed with a string
149 given by the `gnus-summary-same-subject' variable.)"
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const none)
153                  (const dummy)
154                  (const adopt)
155                  (const empty)))
156
157 (defcustom gnus-summary-make-false-root-always nil
158   "Always make a false dummy root."
159   :version "22.1"
160   :group 'gnus-thread
161   :type 'boolean)
162
163 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
164   "*A regexp to match subjects to be excluded from loose thread gathering.
165 As loose thread gathering is done on subjects only, that means that
166 there can be many false gatherings performed.  By rooting out certain
167 common subjects, gathering might become saner."
168   :group 'gnus-thread
169   :type 'regexp)
170
171 (defcustom gnus-summary-gather-subject-limit nil
172   "*Maximum length of subject comparisons when gathering loose threads.
173 Use nil to compare full subjects.  Setting this variable to a low
174 number will help gather threads that have been corrupted by
175 newsreaders chopping off subject lines, but it might also mean that
176 unrelated articles that have subject that happen to begin with the
177 same few characters will be incorrectly gathered.
178
179 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
180 comparing subjects."
181   :group 'gnus-thread
182   :type '(choice (const :tag "off" nil)
183                  (const fuzzy)
184                  (sexp :menu-tag "on" t)))
185
186 (defcustom gnus-simplify-subject-functions nil
187   "List of functions taking a string argument that simplify subjects.
188 The functions are applied recursively.
189
190 Useful functions to put in this list include:
191 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
192 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
193   :group 'gnus-thread
194   :type '(repeat function))
195
196 (defcustom gnus-simplify-ignored-prefixes nil
197   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
198   :group 'gnus-thread
199   :type '(choice (const :tag "off" nil)
200                  regexp))
201
202 (defcustom gnus-build-sparse-threads nil
203   "*If non-nil, fill in the gaps in threads.
204 If `some', only fill in the gaps that are needed to tie loose threads
205 together.  If `more', fill in all leaf nodes that Gnus can find.  If
206 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
207   :group 'gnus-thread
208   :type '(choice (const :tag "off" nil)
209                  (const some)
210                  (const more)
211                  (sexp :menu-tag "all" t)))
212
213 (defcustom gnus-summary-thread-gathering-function
214   'gnus-gather-threads-by-subject
215   "*Function used for gathering loose threads.
216 There are two pre-defined functions: `gnus-gather-threads-by-subject',
217 which only takes Subjects into consideration; and
218 `gnus-gather-threads-by-references', which compared the References
219 headers of the articles to find matches."
220   :group 'gnus-thread
221   :type '(radio (function-item gnus-gather-threads-by-subject)
222                 (function-item gnus-gather-threads-by-references)
223                 (function :tag "other")))
224
225 (defcustom gnus-summary-same-subject ""
226   "*String indicating that the current article has the same subject as the previous.
227 This variable will only be used if the value of
228 `gnus-summary-make-false-root' is `empty'."
229   :group 'gnus-summary-format
230   :type 'string)
231
232 (defcustom gnus-summary-goto-unread nil
233   "*If t, many commands will go to the next unread article.
234 This applies to marking commands as well as other commands that
235 \"naturally\" select the next article, like, for instance, `SPC' at
236 the end of an article.
237
238 If nil, the marking commands do NOT go to the next unread article
239 \(they go to the next article instead).  If `never', commands that
240 usually go to the next unread article, will go to the next article,
241 whether it is read or not."
242   :version "24.1"
243   :group 'gnus-summary-marks
244   :link '(custom-manual "(gnus)Setting Marks")
245   :type '(choice (const :tag "off" nil)
246                  (const never)
247                  (sexp :menu-tag "on" t)))
248
249 (defcustom gnus-summary-default-score 0
250   "*Default article score level.
251 All scores generated by the score files will be added to this score.
252 If this variable is nil, scoring will be disabled."
253   :group 'gnus-score-default
254   :type '(choice (const :tag "disable")
255                  integer))
256
257 (defcustom gnus-summary-default-high-score 0
258   "*Default threshold for a high scored article.
259 An article will be highlighted as high scored if its score is greater
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-default-low-score 0
266   "*Default threshold for a low scored article.
267 An article will be highlighted as low scored if its score is smaller
268 than this score."
269   :version "22.1"
270   :group 'gnus-score-default
271   :type 'integer)
272
273 (defcustom gnus-summary-zcore-fuzz 0
274   "*Fuzziness factor for the zcore in the summary buffer.
275 Articles with scores closer than this to `gnus-summary-default-score'
276 will not be marked."
277   :group 'gnus-summary-format
278   :type 'integer)
279
280 (defcustom gnus-simplify-subject-fuzzy-regexp nil
281   "*Strings to be removed when doing fuzzy matches.
282 This can either be a regular expression or list of regular expressions
283 that will be removed from subject strings if fuzzy subject
284 simplification is selected."
285   :group 'gnus-thread
286   :type '(repeat regexp))
287
288 (defcustom gnus-show-threads t
289   "*If non-nil, display threads in summary mode."
290   :group 'gnus-thread
291   :type 'boolean)
292
293 (defcustom gnus-thread-hide-subtree nil
294   "*If non-nil, hide all threads initially.
295 This can be a predicate specifier which says which threads to hide.
296 If threads are hidden, you have to run the command
297 `gnus-summary-show-thread' by hand or select an article."
298   :group 'gnus-thread
299   :type '(radio (sexp :format "Non-nil\n"
300                       :match (lambda (widget value)
301                                (not (or (consp value) (functionp value))))
302                       :value t)
303                 (const nil)
304                 (sexp :tag "Predicate specifier")))
305
306 (defcustom gnus-thread-hide-killed t
307   "*If non-nil, hide killed threads automatically."
308   :group 'gnus-thread
309   :type 'boolean)
310
311 (defcustom gnus-thread-ignore-subject t
312   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
313 If nil, articles that have different subjects from their parents will
314 start separate threads."
315   :group 'gnus-thread
316   :type 'boolean)
317
318 (defcustom gnus-thread-operation-ignore-subject t
319   "*If non-nil, subjects will be ignored when doing thread commands.
320 This affects commands like `gnus-summary-kill-thread' and
321 `gnus-summary-lower-thread'.
322
323 If this variable is nil, articles in the same thread with different
324 subjects will not be included in the operation in question.  If this
325 variable is `fuzzy', only articles that have subjects that are fuzzily
326 equal will be included."
327   :group 'gnus-thread
328   :type '(choice (const :tag "off" nil)
329                  (const fuzzy)
330                  (sexp :tag "on" t)))
331
332 (defcustom gnus-thread-indent-level 4
333   "*Number that says how much each sub-thread should be indented."
334   :group 'gnus-thread
335   :type 'integer)
336
337 (defcustom gnus-auto-extend-newsgroup t
338   "*If non-nil, extend newsgroup forward and backward when requested."
339   :group 'gnus-summary-choose
340   :type 'boolean)
341
342 (defcustom gnus-auto-select-first t
343   "If non-nil, select an article on group entry.
344 An article is selected automatically when entering a group
345 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
346 `gnus-summary-catchup-and-goto-next-group'.
347
348 Which article is selected is controlled by the variable
349 `gnus-auto-select-subject'.
350
351 If you want to prevent automatic selection of articles in some
352 newsgroups, set the variable to nil in `gnus-select-group-hook'."
353   ;; Commands include...
354   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
355   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
357   :group 'gnus-group-select
358   :type '(choice (const :tag "none" nil)
359                  (sexp :menu-tag "first" t)))
360
361 (defcustom gnus-auto-select-subject 'unseen-or-unread
362   "*Says what subject to place under point when entering a group.
363
364 This variable can either be the symbols `first' (place point on the
365 first subject), `unread' (place point on the subject line of the first
366 unread article), `best' (place point on the subject line of the
367 higest-scored article), `unseen' (place point on the subject line of
368 the first unseen article), `unseen-or-unread' (place point on the subject
369 line of the first unseen article or, if all articles have been seen, on the
370 subject line of the first unread article), or a function to be called to
371 place point on some subject line."
372   :version "24.1"
373   :group 'gnus-group-select
374   :type '(choice (const best)
375                  (const unread)
376                  (const first)
377                  (const unseen)
378                  (const unseen-or-unread)
379                  (function :tag "Function to call")))
380
381 (defcustom gnus-auto-select-next t
382   "*If non-nil, offer to go to the next group from the end of the previous.
383 If the value is t and the next newsgroup is empty, Gnus will exit
384 summary mode and go back to group mode.  If the value is neither nil
385 nor t, Gnus will select the following unread newsgroup.  In
386 particular, if the value is the symbol `quietly', the next unread
387 newsgroup will be selected without any confirmation, and if it is
388 `almost-quietly', the next group will be selected without any
389 confirmation if you are located on the last article in the group.
390 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
391 will go to the next group without confirmation."
392   :group 'gnus-summary-maneuvering
393   :type '(choice (const :tag "off" nil)
394                  (const quietly)
395                  (const almost-quietly)
396                  (const slightly-quietly)
397                  (sexp :menu-tag "on" t)))
398
399 (defcustom gnus-auto-select-same nil
400   "*If non-nil, select the next article with the same subject.
401 If there are no more articles with the same subject, go to
402 the first unread article."
403   :group 'gnus-summary-maneuvering
404   :type 'boolean)
405
406 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
407   "What article should be selected after exiting an ephemeral group.
408 Valid values include:
409
410 `next'
411   Select the next article.
412 `next-unread'
413   Select the next unread article.
414 `next-noselect'
415   Move the cursor to the next article.  This is the default.
416 `next-unread-noselect'
417   Move the cursor to the next unread article.
418
419 If it has any other value or there is no next (unread) article, the
420 article selected before entering to the ephemeral group will appear."
421   :version "23.1" ;; No Gnus
422   :group 'gnus-summary-maneuvering
423   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
424                  (const next) (const next-unread)
425                  (const next-noselect) (const next-unread-noselect)
426                  (sexp :tag "other" :value nil)))
427
428 (defcustom gnus-auto-goto-ignores 'unfetched
429   "*Says how to handle unfetched articles when maneuvering.
430
431 This variable can either be the symbols nil (maneuver to any
432 article), `undownloaded' (maneuvering while unplugged ignores articles
433 that have not been fetched), `always-undownloaded' (maneuvering always
434 ignores articles that have not been fetched), `unfetched' (maneuvering
435 ignores articles whose headers have not been fetched).
436
437 NOTE: The list of unfetched articles will always be nil when plugged
438 and, when unplugged, a subset of the undownloaded article list."
439   :version "22.1"
440   :group 'gnus-summary-maneuvering
441   :type '(choice (const :tag "None" nil)
442                  (const :tag "Undownloaded when unplugged" undownloaded)
443                  (const :tag "Undownloaded" always-undownloaded)
444                  (const :tag "Unfetched" unfetched)))
445
446 (defcustom gnus-summary-check-current nil
447   "*If non-nil, consider the current article when moving.
448 The \"unread\" movement commands will stay on the same line if the
449 current article is unread."
450   :group 'gnus-summary-maneuvering
451   :type 'boolean)
452
453 (defcustom gnus-auto-center-summary 2
454   "*If non-nil, always center the current summary buffer.
455 In particular, if `vertical' do only vertical recentering.  If non-nil
456 and non-`vertical', do both horizontal and vertical recentering."
457   :group 'gnus-summary-maneuvering
458   :type '(choice (const :tag "none" nil)
459                  (const vertical)
460                  (integer :tag "height")
461                  (sexp :menu-tag "both" t)))
462
463 (defcustom gnus-auto-center-group t
464   "If non-nil, always center the group buffer."
465   :group 'gnus-summary-maneuvering
466   :type 'boolean)
467
468 (defcustom gnus-show-all-headers nil
469   "*If non-nil, don't hide any headers."
470   :group 'gnus-article-hiding
471   :group 'gnus-article-headers
472   :type 'boolean)
473
474 (defcustom gnus-summary-ignore-duplicates nil
475   "*If non-nil, ignore articles with identical Message-ID headers."
476   :group 'gnus-summary
477   :type 'boolean)
478
479 (defcustom gnus-single-article-buffer nil
480   "*If non-nil, display all articles in the same buffer.
481 If nil, each group will get its own article buffer."
482   :version "24.1"
483   :group 'gnus-article-various
484   :type 'boolean)
485
486 (defcustom gnus-widen-article-window nil
487   "If non-nil, selecting the article buffer will display only the article buffer."
488   :version "24.1"
489   :group 'gnus-article-various
490   :type 'boolean)
491
492 (defcustom gnus-break-pages t
493   "*If non-nil, do page breaking on articles.
494 The page delimiter is specified by the `gnus-page-delimiter'
495 variable."
496   :group 'gnus-article-various
497   :type 'boolean)
498
499 (defcustom gnus-move-split-methods nil
500   "*Variable used to suggest where articles are to be moved to.
501 It uses the same syntax as the `gnus-split-methods' variable.
502 However, whereas `gnus-split-methods' specifies file names as targets,
503 this variable specifies group names."
504   :group 'gnus-summary-mail
505   :type '(repeat (choice (list :value (fun) function)
506                          (cons :value ("" "") regexp (repeat string))
507                          (sexp :value nil))))
508
509 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
510   "Function used to compute default prefix for article move/copy/etc prompts.
511 The function should take one argument, a group name, and return a
512 string with the suggested prefix."
513   :group 'gnus-summary-mail
514   :type 'function)
515
516 ;; FIXME: Although the custom type is `character' for the following variables,
517 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
518
519 (defcustom gnus-unread-mark ?           ;Whitespace
520   "*Mark used for unread articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-ticked-mark ?!
525   "*Mark used for ticked articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-dormant-mark ??
530   "*Mark used for dormant articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-del-mark ?r
535   "*Mark used for del'd articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-read-mark ?R
540   "*Mark used for read articles."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-expirable-mark ?E
545   "*Mark used for expirable articles."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-killed-mark ?K
550   "*Mark used for killed articles."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-spam-mark ?$
555   "*Mark used for spam articles."
556   :version "22.1"
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-kill-file-mark ?X
561   "*Mark used for articles killed by kill files."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-low-score-mark ?Y
566   "*Mark used for articles with a low score."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-catchup-mark ?C
571   "*Mark used for articles that are caught up."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-replied-mark ?A
576   "*Mark used for articles that have been replied to."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-forwarded-mark ?F
581   "*Mark used for articles that have been forwarded."
582   :version "22.1"
583   :group 'gnus-summary-marks
584   :type 'character)
585
586 (defcustom gnus-recent-mark ?N
587   "*Mark used for articles that are recent."
588   :version "22.1"
589   :group 'gnus-summary-marks
590   :type 'character)
591
592 (defcustom gnus-cached-mark ?*
593   "*Mark used for articles that are in the cache."
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-saved-mark ?S
598   "*Mark used for articles that have been saved."
599   :group 'gnus-summary-marks
600   :type 'character)
601
602 (defcustom gnus-unseen-mark ?.
603   "*Mark used for articles that haven't been seen."
604   :version "22.1"
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-no-mark ?               ;Whitespace
609   "*Mark used for articles that have no other secondary mark."
610   :version "22.1"
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-ancient-mark ?O
615   "*Mark used for ancient articles."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-sparse-mark ?Q
620   "*Mark used for sparsely reffed articles."
621   :group 'gnus-summary-marks
622   :type 'character)
623
624 (defcustom gnus-canceled-mark ?G
625   "*Mark used for canceled articles."
626   :group 'gnus-summary-marks
627   :type 'character)
628
629 (defcustom gnus-duplicate-mark ?M
630   "*Mark used for duplicate articles."
631   :group 'gnus-summary-marks
632   :type 'character)
633
634 (defcustom gnus-undownloaded-mark ?-
635   "*Mark used for articles that weren't downloaded."
636   :version "22.1"
637   :group 'gnus-summary-marks
638   :type 'character)
639
640 (defcustom gnus-downloaded-mark ?+
641   "*Mark used for articles that were downloaded."
642   :group 'gnus-summary-marks
643   :type 'character)
644
645 (defcustom gnus-downloadable-mark ?%
646   "*Mark used for articles that are to be downloaded."
647   :group 'gnus-summary-marks
648   :type 'character)
649
650 (defcustom gnus-unsendable-mark ?=
651   "*Mark used for articles that won't be sent."
652   :group 'gnus-summary-marks
653   :type 'character)
654
655 (defcustom gnus-score-over-mark ?+
656   "*Score mark used for articles with high scores."
657   :group 'gnus-summary-marks
658   :type 'character)
659
660 (defcustom gnus-score-below-mark ?-
661   "*Score mark used for articles with low scores."
662   :group 'gnus-summary-marks
663   :type 'character)
664
665 (defcustom gnus-empty-thread-mark ?     ;Whitespace
666   "*There is no thread under the article."
667   :group 'gnus-summary-marks
668   :type 'character)
669
670 (defcustom gnus-not-empty-thread-mark ?=
671   "*There is a thread under the article."
672   :group 'gnus-summary-marks
673   :type 'character)
674
675 (defcustom gnus-view-pseudo-asynchronously nil
676   "*If non-nil, Gnus will view pseudo-articles asynchronously."
677   :group 'gnus-extract-view
678   :type 'boolean)
679
680 (defcustom gnus-auto-expirable-marks
681   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
682         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
683         gnus-duplicate-mark)
684   "*The list of marks converted into expiration if a group is auto-expirable."
685   :version "24.1"
686   :group 'gnus-summary
687   :type '(repeat character))
688
689 (defcustom gnus-inhibit-user-auto-expire t
690   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
691   :version "21.1"
692   :group 'gnus-summary
693   :type 'boolean)
694
695 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
696   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
697 If nil, the expirable marks will be unchanged except that the marks
698 will be removed when copying or moving articles to a group that has
699 not turned auto-expire on.  If non-nil, articles that have been read
700 will be marked as expirable when being copied or moved to a group in
701 which auto-expire is turned on."
702   :version "23.2"
703   :type 'boolean
704   :group 'gnus-summary-marks)
705
706 (defcustom gnus-view-pseudos nil
707   "*If `automatic', pseudo-articles will be viewed automatically.
708 If `not-confirm', pseudos will be viewed automatically, and the user
709 will not be asked to confirm the command."
710   :group 'gnus-extract-view
711   :type '(choice (const :tag "off" nil)
712                  (const automatic)
713                  (const not-confirm)))
714
715 (defcustom gnus-view-pseudos-separately t
716   "*If non-nil, one pseudo-article will be created for each file to be viewed.
717 If nil, all files that use the same viewing command will be given as a
718 list of parameters to that command."
719   :group 'gnus-extract-view
720   :type 'boolean)
721
722 (defcustom gnus-insert-pseudo-articles t
723   "*If non-nil, insert pseudo-articles when decoding articles."
724   :group 'gnus-extract-view
725   :type 'boolean)
726
727 (defcustom gnus-summary-dummy-line-format
728   "   %(:                             :%) %S\n"
729   "*The format specification for the dummy roots in the summary buffer.
730 It works along the same lines as a normal formatting string,
731 with some simple extensions.
732
733 %S  The subject
734
735 General format specifiers can also be used.
736 See `(gnus)Formatting Variables'."
737   :link '(custom-manual "(gnus)Formatting Variables")
738   :group 'gnus-threading
739   :type 'string)
740
741 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
742   "*The format specification for the summary mode line.
743 It works along the same lines as a normal formatting string,
744 with some simple extensions:
745
746 %G  Group name
747 %p  Unprefixed group name
748 %A  Current article number
749 %z  Current article score
750 %V  Gnus version
751 %U  Number of unread articles in the group
752 %e  Number of unselected articles in the group
753 %Z  A string with unread/unselected article counts
754 %g  Shortish group name
755 %S  Subject of the current article
756 %u  User-defined spec
757 %s  Current score file name
758 %d  Number of dormant articles
759 %r  Number of articles that have been marked as read in this session
760 %E  Number of articles expunged by the score files"
761   :group 'gnus-summary-format
762   :type 'string)
763
764 (defcustom gnus-list-identifiers nil
765   "Regexp that matches list identifiers to be removed from subject.
766 This can also be a list of regexps."
767   :version "21.1"
768   :group 'gnus-summary-format
769   :group 'gnus-article-hiding
770   :type '(choice (const :tag "none" nil)
771                  (regexp :value ".*")
772                  (repeat :value (".*") regexp)))
773
774 (defcustom gnus-summary-mark-below 0
775   "*Mark all articles with a score below this variable as read.
776 This variable is local to each summary buffer and usually set by the
777 score file."
778   :group 'gnus-score-default
779   :type 'integer)
780
781 (defun gnus-widget-reversible-match (widget value)
782   "Ignoring WIDGET, convert VALUE to internal form.
783 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
784   ;; (debug value)
785   (or (symbolp value)
786       (and (listp value)
787            (eq (length value) 2)
788            (eq (nth 0 value) 'not)
789            (symbolp (nth 1 value)))))
790
791 (defun gnus-widget-reversible-to-internal (widget value)
792   "Ignoring WIDGET, convert VALUE to internal form.
793 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
794 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
795   ;; (debug value)
796   (if (atom value)
797       (list value nil)
798     (list (nth 1 value) t)))
799
800 (defun gnus-widget-reversible-to-external (widget value)
801   "Ignoring WIDGET, convert VALUE to external form.
802 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
803 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
804   ;; (debug value)
805   (if (nth 1 value)
806       (list 'not (nth 0 value))
807     (nth 0 value)))
808
809 (define-widget 'gnus-widget-reversible 'group
810   "A `group' that convert values."
811   :match 'gnus-widget-reversible-match
812   :value-to-internal 'gnus-widget-reversible-to-internal
813   :value-to-external 'gnus-widget-reversible-to-external)
814
815 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
816   "*List of functions used for sorting articles in the summary buffer.
817
818 Each function takes two articles and returns non-nil if the first
819 article should be sorted before the other.  If you use more than one
820 function, the primary sort function should be the last.  You should
821 probably always include `gnus-article-sort-by-number' in the list of
822 sorting functions -- preferably first.  Also note that sorting by date
823 is often much slower than sorting by number, and the sorting order is
824 very similar.  (Sorting by date means sorting by the time the message
825 was sent, sorting by number means sorting by arrival time.)
826
827 Each item can also be a list `(not F)' where F is a function;
828 this reverses the sort order.
829
830 Ready-made functions include `gnus-article-sort-by-number',
831 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
832 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
833 and `gnus-article-sort-by-score'.
834
835 When threading is turned on, the variable `gnus-thread-sort-functions'
836 controls how articles are sorted."
837   :group 'gnus-summary-sort
838   :type '(repeat (gnus-widget-reversible
839                   (choice (function-item gnus-article-sort-by-number)
840                           (function-item gnus-article-sort-by-author)
841                           (function-item gnus-article-sort-by-subject)
842                           (function-item gnus-article-sort-by-date)
843                           (function-item gnus-article-sort-by-score)
844                           (function-item gnus-article-sort-by-random)
845                           (function :tag "other"))
846                   (boolean :tag "Reverse order"))))
847
848
849 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
850   "*List of functions used for sorting threads in the summary buffer.
851 By default, threads are sorted by article number.
852
853 Each function takes two threads and returns non-nil if the first
854 thread should be sorted before the other.  If you use more than one
855 function, the primary sort function should be the last.  You should
856 probably always include `gnus-thread-sort-by-number' in the list of
857 sorting functions -- preferably first.  Also note that sorting by date
858 is often much slower than sorting by number, and the sorting order is
859 very similar.  (Sorting by date means sorting by the time the message
860 was sent, sorting by number means sorting by arrival time.)
861
862 Each list item can also be a list `(not F)' where F is a
863 function; this specifies reversed sort order.
864
865 Ready-made functions include `gnus-thread-sort-by-number',
866 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
867 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
868 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
869 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
870 and `gnus-thread-sort-by-total-score' (see
871 `gnus-thread-score-function').
872
873 When threading is turned off, the variable
874 `gnus-article-sort-functions' controls how articles are sorted."
875   :group 'gnus-summary-sort
876   :type '(repeat
877           (gnus-widget-reversible
878            (choice (function-item gnus-thread-sort-by-number)
879                    (function-item gnus-thread-sort-by-author)
880                    (function-item gnus-thread-sort-by-recipient)
881                    (function-item gnus-thread-sort-by-subject)
882                    (function-item gnus-thread-sort-by-date)
883                    (function-item gnus-thread-sort-by-score)
884                    (function-item gnus-thread-sort-by-most-recent-number)
885                    (function-item gnus-thread-sort-by-most-recent-date)
886                    (function-item gnus-thread-sort-by-random)
887                    (function-item gnus-thread-sort-by-total-score)
888                    (function :tag "other"))
889            (boolean :tag "Reverse order"))))
890
891 (defcustom gnus-thread-score-function '+
892   "*Function used for calculating the total score of a thread.
893
894 The function is called with the scores of the article and each
895 subthread and should then return the score of the thread.
896
897 Some functions you can use are `+', `max', or `min'."
898   :group 'gnus-summary-sort
899   :type 'function)
900
901 (defcustom gnus-summary-expunge-below nil
902   "All articles that have a score less than this variable will be expunged.
903 This variable is local to the summary buffers."
904   :group 'gnus-score-default
905   :type '(choice (const :tag "off" nil)
906                  integer))
907
908 (defcustom gnus-thread-expunge-below nil
909   "All threads that have a total score less than this variable will be expunged.
910 See `gnus-thread-score-function' for en explanation of what a
911 \"thread score\" is.
912
913 This variable is local to the summary buffers."
914   :group 'gnus-threading
915   :group 'gnus-score-default
916   :type '(choice (const :tag "off" nil)
917                  integer))
918
919 (defcustom gnus-summary-mode-hook nil
920   "*A hook for Gnus summary mode.
921 This hook is run before any variables are set in the summary buffer."
922   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
923   :group 'gnus-summary-various
924   :type 'hook)
925
926 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
927 (when (featurep 'xemacs)
928   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
929   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
930   (add-hook 'gnus-summary-mode-hook
931             'gnus-xmas-switch-horizontal-scrollbar-off))
932
933 (defcustom gnus-summary-menu-hook nil
934   "*Hook run after the creation of the summary mode menu."
935   :group 'gnus-summary-visual
936   :type 'hook)
937
938 (defcustom gnus-summary-exit-hook nil
939   "*A hook called on exit from the summary buffer.
940 It will be called with point in the group buffer."
941   :group 'gnus-summary-exit
942   :type 'hook)
943
944 (defcustom gnus-summary-prepare-hook nil
945   "*A hook called after the summary buffer has been generated.
946 If you want to modify the summary buffer, you can use this hook."
947   :group 'gnus-summary-various
948   :type 'hook)
949
950 (defcustom gnus-summary-prepared-hook nil
951   "*A hook called as the last thing after the summary buffer has been generated."
952   :group 'gnus-summary-various
953   :type 'hook)
954
955 (defcustom gnus-summary-generate-hook nil
956   "*A hook run just before generating the summary buffer.
957 This hook is commonly used to customize threading variables and the
958 like."
959   :group 'gnus-summary-various
960   :type 'hook)
961
962 (defcustom gnus-select-group-hook nil
963   "*A hook called when a newsgroup is selected.
964
965 If you'd like to simplify subjects like the
966 `gnus-summary-next-same-subject' command does, you can use the
967 following hook:
968
969  (add-hook gnus-select-group-hook
970            (lambda ()
971              (mapcar (lambda (header)
972                        (mail-header-set-subject
973                         header
974                         (gnus-simplify-subject
975                          (mail-header-subject header) 're-only)))
976                      gnus-newsgroup-headers)))"
977   :group 'gnus-group-select
978   :type 'hook)
979
980 (defcustom gnus-select-article-hook nil
981   "*A hook called when an article is selected."
982   :group 'gnus-summary-choose
983   :options '(gnus-agent-fetch-selected-article)
984   :type 'hook)
985
986 (defcustom gnus-visual-mark-article-hook
987   (list 'gnus-highlight-selected-summary)
988   "*Hook run after selecting an article in the summary buffer.
989 It is meant to be used for highlighting the article in some way.  It
990 is not run if `gnus-visual' is nil."
991   :group 'gnus-summary-visual
992   :type 'hook)
993
994 (defcustom gnus-parse-headers-hook nil
995   "*A hook called before parsing the headers."
996   :group 'gnus-various
997   :type 'hook)
998
999 (defcustom gnus-exit-group-hook nil
1000   "*A hook called when exiting summary mode.
1001 This hook is not called from the non-updating exit commands like `Q'."
1002   :group 'gnus-various
1003   :type 'hook)
1004
1005 (defcustom gnus-summary-update-hook nil
1006   "*A hook called when a summary line is changed.
1007 The hook will not be called if `gnus-visual' is nil.
1008
1009 The default function `gnus-summary-highlight-line' will
1010 highlight the line according to the `gnus-summary-highlight'
1011 variable."
1012   :group 'gnus-summary-visual
1013   :type 'hook)
1014
1015 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1016   "*A hook called when an article is selected for the first time.
1017 The hook is intended to mark an article as read (or unread)
1018 automatically when it is selected."
1019   :group 'gnus-summary-choose
1020   :type 'hook)
1021
1022 (defcustom gnus-group-no-more-groups-hook nil
1023   "*A hook run when returning to group mode having no more (unread) groups."
1024   :group 'gnus-group-select
1025   :type 'hook)
1026
1027 (defcustom gnus-ps-print-hook nil
1028   "*A hook run before ps-printing something from Gnus."
1029   :group 'gnus-summary
1030   :type 'hook)
1031
1032 (defcustom gnus-summary-article-move-hook nil
1033   "*A hook called after an article is moved, copied, respooled, or crossposted."
1034   :version "22.1"
1035   :group 'gnus-summary
1036   :type 'hook)
1037
1038 (defcustom gnus-summary-article-delete-hook nil
1039   "*A hook called after an article is deleted."
1040   :version "22.1"
1041   :group 'gnus-summary
1042   :type 'hook)
1043
1044 (defcustom gnus-summary-article-expire-hook nil
1045   "*A hook called after an article is expired."
1046   :version "22.1"
1047   :group 'gnus-summary
1048   :type 'hook)
1049
1050 (defcustom gnus-summary-display-arrow
1051   (and (fboundp 'display-graphic-p)
1052        (display-graphic-p))
1053   "*If non-nil, display an arrow highlighting the current article."
1054   :version "22.1"
1055   :group 'gnus-summary
1056   :type 'boolean)
1057
1058 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1059   "Face used for highlighting the current article in the summary buffer."
1060   :group 'gnus-summary-visual
1061   :type 'face)
1062
1063 (defvar gnus-tmp-downloaded nil)
1064
1065 (defcustom gnus-summary-highlight
1066   '(((eq mark gnus-canceled-mark)
1067      . gnus-summary-cancelled)
1068     ((and uncached (> score default-high))
1069      . gnus-summary-high-undownloaded)
1070     ((and uncached (< score default-low))
1071      . gnus-summary-low-undownloaded)
1072     (uncached
1073      . gnus-summary-normal-undownloaded)
1074     ((and (> score default-high)
1075           (or (eq mark gnus-dormant-mark)
1076               (eq mark gnus-ticked-mark)))
1077      . gnus-summary-high-ticked)
1078     ((and (< score default-low)
1079           (or (eq mark gnus-dormant-mark)
1080               (eq mark gnus-ticked-mark)))
1081      . gnus-summary-low-ticked)
1082     ((or (eq mark gnus-dormant-mark)
1083          (eq mark gnus-ticked-mark))
1084      . gnus-summary-normal-ticked)
1085     ((and (> score default-high) (eq mark gnus-ancient-mark))
1086      . gnus-summary-high-ancient)
1087     ((and (< score default-low) (eq mark gnus-ancient-mark))
1088      . gnus-summary-low-ancient)
1089     ((eq mark gnus-ancient-mark)
1090      . gnus-summary-normal-ancient)
1091     ((and (> score default-high) (eq mark gnus-unread-mark))
1092      . gnus-summary-high-unread)
1093     ((and (< score default-low) (eq mark gnus-unread-mark))
1094      . gnus-summary-low-unread)
1095     ((eq mark gnus-unread-mark)
1096      . gnus-summary-normal-unread)
1097     ((> score default-high)
1098      . gnus-summary-high-read)
1099     ((< score default-low)
1100      . gnus-summary-low-read)
1101     (t
1102      . gnus-summary-normal-read))
1103   "*Controls the highlighting of summary buffer lines.
1104
1105 A list of (FORM . FACE) pairs.  When deciding how a particular
1106 summary line should be displayed, each form is evaluated.  The content
1107 of the face field after the first true form is used.  You can change
1108 how those summary lines are displayed, by editing the face field.
1109
1110 You can use the following variables in the FORM field.
1111
1112 score:        The article's score.
1113 default:      The default article score.
1114 default-high: The default score for high scored articles.
1115 default-low:  The default score for low scored articles.
1116 below:        The score below which articles are automatically marked as read.
1117 mark:         The article's mark.
1118 uncached:     Non-nil if the article is uncached."
1119   :group 'gnus-summary-visual
1120   :type '(repeat (cons (sexp :tag "Form" nil)
1121                        face)))
1122 (put 'gnus-summary-highlight 'risky-local-variable t)
1123
1124 (defcustom gnus-alter-header-function nil
1125   "Function called to allow alteration of article header structures.
1126 The function is called with one parameter, the article header vector,
1127 which it may alter in any way."
1128   :type '(choice (const :tag "None" nil)
1129                  function)
1130   :group 'gnus-summary)
1131
1132 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1133   "Function used to decode a string with encoded words.")
1134
1135 (defvar gnus-decode-encoded-address-function
1136   'mail-decode-encoded-address-string
1137   "Function used to decode addresses with encoded words.")
1138
1139 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1140   "*Extra headers to parse."
1141   :version "24.1"                       ; added Cc Keywords Gcc
1142   :group 'gnus-summary
1143   :type '(repeat symbol))
1144
1145 (defcustom gnus-ignored-from-addresses
1146   (and user-mail-address
1147        (not (string= user-mail-address ""))
1148        (regexp-quote user-mail-address))
1149   "*From headers that may be suppressed in favor of To headers.
1150 This can be a regexp or a list of regexps."
1151   :version "21.1"
1152   :group 'gnus-summary
1153   :type '(choice regexp
1154                  (repeat :tag "Regexp List" regexp)))
1155
1156 (defsubst gnus-ignored-from-addresses ()
1157   (gmm-regexp-concat gnus-ignored-from-addresses))
1158
1159 (defcustom gnus-summary-to-prefix "-> "
1160   "*String prefixed to the To field in the summary line when
1161 using `gnus-ignored-from-addresses'."
1162   :version "22.1"
1163   :group 'gnus-summary
1164   :type 'string)
1165
1166 (defcustom gnus-summary-newsgroup-prefix "=> "
1167   "*String prefixed to the Newsgroup field in the summary
1168 line when using `gnus-ignored-from-addresses'."
1169   :version "22.1"
1170   :group 'gnus-summary
1171   :type 'string)
1172
1173 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1174   "List of charsets that should be ignored.
1175 When these charsets are used in the \"charset\" parameter, the
1176 default charset will be used instead."
1177   :version "21.1"
1178   :type '(repeat symbol)
1179   :group 'gnus-charset)
1180
1181 (defcustom gnus-newsgroup-maximum-articles nil
1182   "The maximum number of articles a newsgroup.
1183 If this is a number, old articles in a newsgroup exceeding this number
1184 are silently ignored.  If it is nil, no article is ignored.  Note that
1185 setting this variable to a number might prevent you from reading very
1186 old articles."
1187   :group 'gnus-group-select
1188   :version "22.2"
1189   :type '(choice (const :tag "No limit" nil)
1190                  integer))
1191
1192 (gnus-define-group-parameter
1193  ignored-charsets
1194  :type list
1195  :function-document
1196  "Return the ignored charsets of GROUP."
1197  :variable gnus-group-ignored-charsets-alist
1198  :variable-default
1199  '(("alt\\.chinese\\.text" iso-8859-1))
1200  :variable-document
1201  "Alist of regexps (to match group names) and charsets that should be ignored.
1202 When these charsets are used in the \"charset\" parameter, the
1203 default charset will be used instead."
1204  :variable-group gnus-charset
1205  :variable-type '(repeat (cons (regexp :tag "Group")
1206                                (repeat symbol)))
1207  :parameter-type '(choice :tag "Ignored charsets"
1208                           :value nil
1209                           (repeat (symbol)))
1210  :parameter-document       "\
1211 List of charsets that should be ignored.
1212
1213 When these charsets are used in the \"charset\" parameter, the
1214 default charset will be used instead.")
1215
1216 (defcustom gnus-group-highlight-words-alist nil
1217   "Alist of group regexps and highlight regexps.
1218 This variable uses the same syntax as `gnus-emphasis-alist'."
1219   :version "21.1"
1220   :type '(repeat (cons (regexp :tag "Group")
1221                        (repeat (list (regexp :tag "Highlight regexp")
1222                                      (number :tag "Group for entire word" 0)
1223                                      (number :tag "Group for displayed part" 0)
1224                                      (symbol :tag "Face"
1225                                              gnus-emphasis-highlight-words)))))
1226   :group 'gnus-summary-visual)
1227
1228 (defcustom gnus-summary-show-article-charset-alist
1229   nil
1230   "Alist of number and charset.
1231 The article will be shown with the charset corresponding to the
1232 numbered argument.
1233 For example: ((1 . cn-gb-2312) (2 . big5))."
1234   :version "21.1"
1235   :type '(repeat (cons (number :tag "Argument" 1)
1236                        (symbol :tag "Charset")))
1237   :group 'gnus-charset)
1238
1239 (defcustom gnus-preserve-marks t
1240   "Whether marks are preserved when moving, copying and respooling messages."
1241   :version "21.1"
1242   :type 'boolean
1243   :group 'gnus-summary-marks)
1244
1245 (defcustom gnus-propagate-marks nil
1246   "If non-nil, Gnus will store and retrieve marks from the backends.
1247 This means that marks will be stored both in .newsrc.eld and in
1248 the backend, and will slow operation down somewhat."
1249   :type 'boolean
1250   :group 'gnus-summary-marks)
1251
1252 (defcustom gnus-alter-articles-to-read-function nil
1253   "Function to be called to alter the list of articles to be selected."
1254   :type '(choice (const nil) function)
1255   :group 'gnus-summary)
1256
1257 (defcustom gnus-orphan-score nil
1258   "*All orphans get this score added.  Set in the score file."
1259   :group 'gnus-score-default
1260   :type '(choice (const nil)
1261                  integer))
1262
1263 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1264   "*A regexp to match MIME parts when saving multiple parts of a
1265 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1266 This regexp will be used by default when prompting the user for which
1267 type of files to save."
1268   :group 'gnus-summary
1269   :type 'regexp)
1270
1271 (defcustom gnus-read-all-available-headers nil
1272   "Whether Gnus should parse all headers made available to it.
1273 This is mostly relevant for slow back ends where the user may
1274 wish to widen the summary buffer to include all headers
1275 that were fetched."
1276   :version "22.1"
1277   :group 'gnus-summary
1278   :type '(choice boolean regexp))
1279
1280 (defcustom gnus-summary-pipe-output-default-command nil
1281   "Command (and optional arguments) used to pipe article to subprocess.
1282 This will be used as the default command if it is non-nil.  The value
1283 will be updated if you modify it when executing the command
1284 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1285   :version "23.1" ;; No Gnus
1286   :group 'gnus-summary
1287   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1288
1289 (defcustom gnus-summary-muttprint-program "muttprint"
1290   "Command (and optional arguments) used to run Muttprint.
1291 The value will be updated if you modify it when executing the command
1292 `gnus-summary-muttprint'."
1293   :version "22.1"
1294   :group 'gnus-summary
1295   :type 'string)
1296
1297 (defcustom gnus-article-loose-mime t
1298   "If non-nil, don't require MIME-Version header.
1299 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1300 supply the MIME-Version header or deliberately strip it from the mail.
1301 If non-nil (the default), Gnus will treat some articles as MIME
1302 even if the MIME-Version header is missing."
1303   :version "22.1"
1304   :type 'boolean
1305   :group 'gnus-article-mime)
1306
1307 (defcustom gnus-article-emulate-mime t
1308   "If non-nil, use MIME emulation for uuencode and the like.
1309 This means that Gnus will search message bodies for text that look
1310 like uuencoded bits, yEncoded bits, and so on, and present that using
1311 the normal Gnus MIME machinery."
1312   :version "22.1"
1313   :type 'boolean
1314   :group 'gnus-article-mime)
1315
1316 ;;; Internal variables
1317
1318 (defvar gnus-summary-display-cache nil)
1319 (defvar gnus-article-mime-handles nil)
1320 (defvar gnus-article-decoded-p nil)
1321 (defvar gnus-article-charset nil)
1322 (defvar gnus-article-ignored-charsets nil)
1323 (defvar gnus-scores-exclude-files nil)
1324 (defvar gnus-page-broken nil)
1325
1326 (defvar gnus-original-article nil)
1327 (defvar gnus-article-internal-prepare-hook nil)
1328 (defvar gnus-newsgroup-process-stack nil)
1329
1330 (defvar gnus-thread-indent-array nil)
1331 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1332 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1333   "Function called to sort the articles within a thread after it has been gathered together.")
1334
1335 (defvar gnus-summary-save-parts-type-history nil)
1336 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1337
1338 ;; Avoid highlighting in kill files.
1339 (defvar gnus-summary-inhibit-highlight nil)
1340 (defvar gnus-newsgroup-selected-overlay nil)
1341 (defvar gnus-inhibit-limiting nil)
1342 (defvar gnus-newsgroup-adaptive-score-file nil)
1343 (defvar gnus-current-score-file nil)
1344 (defvar gnus-current-move-group nil)
1345 (defvar gnus-current-copy-group nil)
1346 (defvar gnus-current-crosspost-group nil)
1347 (defvar gnus-newsgroup-display nil)
1348
1349 (defvar gnus-newsgroup-dependencies nil)
1350 (defvar gnus-newsgroup-adaptive nil)
1351 (defvar gnus-summary-display-article-function nil)
1352 (defvar gnus-summary-highlight-line-function nil
1353   "Function called after highlighting a summary line.")
1354
1355 (defvar gnus-summary-line-format-alist
1356   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1357     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1358     (?s gnus-tmp-subject-or-nil ?s)
1359     (?n gnus-tmp-name ?s)
1360     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1361         ?s)
1362     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1363             gnus-tmp-from) ?s)
1364     (?F gnus-tmp-from ?s)
1365     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1366     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1367     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1368     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1369     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1370     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1371     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1372     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1373     (?L gnus-tmp-lines ?s)
1374     (?Z (or ,(gnus-macroexpand-all
1375               '(nnir-article-rsv (mail-header-number gnus-tmp-header)))
1376             0) ?d)
1377     (?G (or ,(gnus-macroexpand-all
1378               '(nnir-article-group (mail-header-number gnus-tmp-header)))
1379             "") ?s)
1380     (?g (or ,(gnus-macroexpand-all
1381               '(gnus-group-short-name
1382                 (nnir-article-group (mail-header-number gnus-tmp-header))))
1383             "") ?s)
1384     (?O gnus-tmp-downloaded ?c)
1385     (?I gnus-tmp-indentation ?s)
1386     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1387     (?R gnus-tmp-replied ?c)
1388     (?\[ gnus-tmp-opening-bracket ?c)
1389     (?\] gnus-tmp-closing-bracket ?c)
1390     (?\> (make-string gnus-tmp-level ? ) ?s)
1391     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1392     (?i gnus-tmp-score ?d)
1393     (?z gnus-tmp-score-char ?c)
1394     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1395     (?U gnus-tmp-unread ?c)
1396     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1397         ?s)
1398     (?t (gnus-summary-number-of-articles-in-thread
1399          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1400         ?d)
1401     (?e (gnus-summary-number-of-articles-in-thread
1402          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1403         ?c)
1404     (?u gnus-tmp-user-defined ?s)
1405     (?P (gnus-pick-line-number) ?d)
1406     (?B gnus-tmp-thread-tree-header-string ?s)
1407     (user-date (gnus-user-date
1408                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1409   "An alist of format specifications that can appear in summary lines.
1410 These are paired with what variables they correspond with, along with
1411 the type of the variable (string, integer, character, etc).")
1412
1413 (defvar gnus-summary-dummy-line-format-alist
1414   `((?S gnus-tmp-subject ?s)
1415     (?N gnus-tmp-number ?d)
1416     (?u gnus-tmp-user-defined ?s)))
1417
1418 (defvar gnus-summary-mode-line-format-alist
1419   `((?G gnus-tmp-group-name ?s)
1420     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1421     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1422     (?A gnus-tmp-article-number ?d)
1423     (?Z gnus-tmp-unread-and-unselected ?s)
1424     (?V gnus-version ?s)
1425     (?U gnus-tmp-unread-and-unticked ?d)
1426     (?S gnus-tmp-subject ?s)
1427     (?e gnus-tmp-unselected ?d)
1428     (?u gnus-tmp-user-defined ?s)
1429     (?d (length gnus-newsgroup-dormant) ?d)
1430     (?t (length gnus-newsgroup-marked) ?d)
1431     (?h (length gnus-newsgroup-spam-marked) ?d)
1432     (?r (length gnus-newsgroup-reads) ?d)
1433     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1434     (?E gnus-newsgroup-expunged-tally ?d)
1435     (?s (gnus-current-score-file-nondirectory) ?s)))
1436
1437 ;; This is here rather than in gnus-art for compilation reasons.
1438 (defvar gnus-article-mode-line-format-alist
1439   (nconc '((?w (gnus-article-wash-status) ?s)
1440            (?m (gnus-article-mime-part-status) ?s))
1441          gnus-summary-mode-line-format-alist))
1442
1443 (defvar gnus-last-search-regexp nil
1444   "Default regexp for article search command.")
1445
1446 (defvar gnus-last-shell-command nil
1447   "Default shell command on article.")
1448
1449 (defvar gnus-newsgroup-agentized nil
1450   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1451 (defvar gnus-newsgroup-begin nil)
1452 (defvar gnus-newsgroup-end nil)
1453 (defvar gnus-newsgroup-last-rmail nil)
1454 (defvar gnus-newsgroup-last-mail nil)
1455 (defvar gnus-newsgroup-last-folder nil)
1456 (defvar gnus-newsgroup-last-file nil)
1457 (defvar gnus-newsgroup-last-directory nil)
1458 (defvar gnus-newsgroup-auto-expire nil)
1459 (defvar gnus-newsgroup-active nil)
1460 (defvar gnus-newsgroup-highest nil)
1461
1462 (defvar gnus-newsgroup-data nil)
1463 (defvar gnus-newsgroup-data-reverse nil)
1464 (defvar gnus-newsgroup-limit nil)
1465 (defvar gnus-newsgroup-limits nil)
1466 (defvar gnus-summary-use-undownloaded-faces nil)
1467
1468 (defvar gnus-newsgroup-unreads nil
1469   "Sorted list of unread articles in the current newsgroup.")
1470
1471 (defvar gnus-newsgroup-unselected nil
1472   "Sorted list of unselected unread articles in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-reads nil
1475   "Alist of read articles and article marks in the current newsgroup.")
1476
1477 (defvar gnus-newsgroup-expunged-tally nil)
1478
1479 (defvar gnus-newsgroup-marked nil
1480   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1481
1482 (defvar gnus-newsgroup-spam-marked nil
1483   "List of ranges of articles that have been marked as spam.")
1484
1485 (defvar gnus-newsgroup-killed nil
1486   "List of ranges of articles that have been through the scoring process.")
1487
1488 (defvar gnus-newsgroup-cached nil
1489   "Sorted list of articles that come from the article cache.")
1490
1491 (defvar gnus-newsgroup-saved nil
1492   "List of articles that have been saved.")
1493
1494 (defvar gnus-newsgroup-kill-headers nil)
1495
1496 (defvar gnus-newsgroup-replied nil
1497   "List of articles that have been replied to in the current newsgroup.")
1498
1499 (defvar gnus-newsgroup-forwarded nil
1500   "List of articles that have been forwarded in the current newsgroup.")
1501
1502 (defvar gnus-newsgroup-recent nil
1503   "List of articles that have are recent in the current newsgroup.")
1504
1505 (defvar gnus-newsgroup-expirable nil
1506   "Sorted list of articles in the current newsgroup that can be expired.")
1507
1508 (defvar gnus-newsgroup-processable nil
1509   "List of articles in the current newsgroup that can be processed.")
1510
1511 (defvar gnus-newsgroup-downloadable nil
1512   "Sorted list of articles in the current newsgroup that can be processed.")
1513
1514 (defvar gnus-newsgroup-unfetched nil
1515   "Sorted list of articles in the current newsgroup whose headers have
1516 not been fetched into the agent.
1517
1518 This list will always be a subset of gnus-newsgroup-undownloaded.")
1519
1520 (defvar gnus-newsgroup-undownloaded nil
1521   "List of articles in the current newsgroup that haven't been downloaded.")
1522
1523 (defvar gnus-newsgroup-unsendable nil
1524   "List of articles in the current newsgroup that won't be sent.")
1525
1526 (defvar gnus-newsgroup-bookmarks nil
1527   "List of articles in the current newsgroup that have bookmarks.")
1528
1529 (defvar gnus-newsgroup-dormant nil
1530   "Sorted list of dormant articles in the current newsgroup.")
1531
1532 (defvar gnus-newsgroup-unseen nil
1533   "List of unseen articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-seen nil
1536   "Range of seen articles in the current newsgroup.")
1537
1538 (defvar gnus-newsgroup-articles nil
1539   "List of articles in the current newsgroup.")
1540
1541 (defvar gnus-newsgroup-scored nil
1542   "List of scored articles in the current newsgroup.")
1543
1544 (defvar gnus-newsgroup-headers nil
1545   "List of article headers in the current newsgroup.")
1546
1547 (defvar gnus-newsgroup-threads nil)
1548
1549 (defvar gnus-newsgroup-prepared nil
1550   "Whether the current group has been prepared properly.")
1551
1552 (defvar gnus-newsgroup-ancient nil
1553   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1554
1555 (defvar gnus-newsgroup-sparse nil)
1556
1557 (defvar gnus-current-article nil)
1558 (defvar gnus-article-current nil)
1559 (defvar gnus-current-headers nil)
1560 (defvar gnus-have-all-headers nil)
1561 (defvar gnus-last-article nil)
1562 (defvar gnus-newsgroup-history nil)
1563 (defvar gnus-newsgroup-charset nil)
1564 (defvar gnus-newsgroup-ephemeral-charset nil)
1565 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1566
1567 (defvar gnus-article-before-search nil)
1568
1569 (defvar gnus-summary-local-variables
1570   '(gnus-newsgroup-name
1571
1572     ;; Marks lists
1573     gnus-newsgroup-unreads
1574     gnus-newsgroup-unselected
1575     gnus-newsgroup-marked
1576     gnus-newsgroup-spam-marked
1577     gnus-newsgroup-reads
1578     gnus-newsgroup-saved
1579     gnus-newsgroup-replied
1580     gnus-newsgroup-forwarded
1581     gnus-newsgroup-recent
1582     gnus-newsgroup-expirable
1583     gnus-newsgroup-killed
1584     gnus-newsgroup-unseen
1585     gnus-newsgroup-seen
1586     gnus-newsgroup-cached
1587     gnus-newsgroup-downloadable
1588     gnus-newsgroup-undownloaded
1589     gnus-newsgroup-unsendable
1590
1591     gnus-newsgroup-begin gnus-newsgroup-end
1592     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1593     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1594     gnus-newsgroup-last-directory
1595     gnus-newsgroup-auto-expire
1596     gnus-newsgroup-processable
1597     gnus-newsgroup-unfetched
1598     gnus-newsgroup-articles
1599     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1600     gnus-newsgroup-headers gnus-newsgroup-threads
1601     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1602     gnus-current-article gnus-current-headers gnus-have-all-headers
1603     gnus-last-article gnus-article-internal-prepare-hook
1604     (gnus-summary-article-delete-hook . global)
1605     (gnus-summary-article-move-hook . global)
1606     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1607     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1608     gnus-thread-expunge-below
1609     gnus-score-alist gnus-current-score-file
1610     (gnus-summary-expunge-below . global)
1611     (gnus-summary-mark-below . global)
1612     (gnus-orphan-score . global)
1613     gnus-newsgroup-active gnus-scores-exclude-files
1614     gnus-newsgroup-highest
1615     gnus-newsgroup-history gnus-newsgroup-ancient
1616     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1617     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1618     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1619     (gnus-newsgroup-expunged-tally . 0)
1620     gnus-cache-removable-articles
1621     gnus-newsgroup-data gnus-newsgroup-data-reverse
1622     gnus-newsgroup-limit gnus-newsgroup-limits
1623     gnus-newsgroup-charset gnus-newsgroup-display
1624     gnus-summary-use-undownloaded-faces)
1625   "Variables that are buffer-local to the summary buffers.")
1626
1627 (defvar gnus-newsgroup-variables nil
1628   "A list of variables that have separate values in different newsgroups.
1629 A list of newsgroup (summary buffer) local variables, or cons of
1630 variables and their default expressions to be evalled (when the default
1631 values are not nil), that should be made global while the summary buffer
1632 is active.
1633
1634 Note: The default expressions will be evaluated (using function `eval')
1635 before assignment to the local variable rather than just assigned to it.
1636 If the default expression is the symbol `global', that symbol will not
1637 be evaluated but the global value of the local variable will be used
1638 instead.
1639
1640 These variables can be used to set variables in the group parameters
1641 while still allowing them to affect operations done in other buffers.
1642 For example:
1643
1644 \(setq gnus-newsgroup-variables
1645      '(message-use-followup-to
1646        (gnus-visible-headers .
1647          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1648 ")
1649
1650 (eval-when-compile
1651   ;; Bind features so that require will believe that gnus-sum has
1652   ;; already been loaded (avoids infinite recursion)
1653   (let ((features (cons 'gnus-sum features)))
1654     (require 'gnus-art)))
1655
1656 ;; MIME stuff.
1657
1658 (defvar gnus-decode-encoded-word-methods
1659   '(mail-decode-encoded-word-string)
1660   "List of methods used to decode encoded words.
1661
1662 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1663 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1664 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1665 whose names match REGEXP.
1666
1667 For example:
1668 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1669  mail-decode-encoded-word-string
1670  (\"chinese\" . rfc1843-decode-string))")
1671
1672 (defvar gnus-decode-encoded-word-methods-cache nil)
1673
1674 (defun gnus-multi-decode-encoded-word-string (string)
1675   "Apply the functions from `gnus-encoded-word-methods' that match."
1676   (unless (and gnus-decode-encoded-word-methods-cache
1677                (eq gnus-newsgroup-name
1678                    (car gnus-decode-encoded-word-methods-cache)))
1679     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1680     (dolist (method gnus-decode-encoded-word-methods)
1681       (if (symbolp method)
1682           (nconc gnus-decode-encoded-word-methods-cache (list method))
1683         (if (and gnus-newsgroup-name
1684                  (string-match (car method) gnus-newsgroup-name))
1685             (nconc gnus-decode-encoded-word-methods-cache
1686                    (list (cdr method)))))))
1687   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1688     (setq string (funcall method string))))
1689
1690 ;; Subject simplification.
1691
1692 (defun gnus-simplify-whitespace (str)
1693   "Remove excessive whitespace from STR."
1694   ;; Multiple spaces.
1695   (while (string-match "[ \t][ \t]+" str)
1696     (setq str (concat (substring str 0 (match-beginning 0))
1697                         " "
1698                         (substring str (match-end 0)))))
1699   ;; Leading spaces.
1700   (when (string-match "^[ \t]+" str)
1701     (setq str (substring str (match-end 0))))
1702   ;; Trailing spaces.
1703   (when (string-match "[ \t]+$" str)
1704     (setq str (substring str 0 (match-beginning 0))))
1705   str)
1706
1707 (defun gnus-simplify-all-whitespace (str)
1708   "Remove all whitespace from STR."
1709   (while (string-match "[ \t\n]+" str)
1710     (setq str (replace-match "" nil nil str)))
1711   str)
1712
1713 (defsubst gnus-simplify-subject-re (subject)
1714   "Remove \"Re:\" from subject lines."
1715   (if (string-match message-subject-re-regexp subject)
1716       (substring subject (match-end 0))
1717     subject))
1718
1719 (defun gnus-simplify-subject (subject &optional re-only)
1720   "Remove `Re:' and words in parentheses.
1721 If RE-ONLY is non-nil, strip leading `Re:'s only."
1722   (let ((case-fold-search t))           ;Ignore case.
1723     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1724     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1725       (setq subject (substring subject (match-end 0))))
1726     ;; Remove uninteresting prefixes.
1727     (when (and (not re-only)
1728                gnus-simplify-ignored-prefixes
1729                (string-match gnus-simplify-ignored-prefixes subject))
1730       (setq subject (substring subject (match-end 0))))
1731     ;; Remove words in parentheses from end.
1732     (unless re-only
1733       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1734         (setq subject (substring subject 0 (match-beginning 0)))))
1735     ;; Return subject string.
1736     subject))
1737
1738 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1739 ;; all whitespace.
1740 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1741   (goto-char (point-min))
1742   (while (re-search-forward regexp nil t)
1743     (replace-match (or newtext ""))))
1744
1745 (defun gnus-simplify-buffer-fuzzy (regexp)
1746   "Simplify string in the buffer fuzzily.
1747 The string in the accessible portion of the current buffer is simplified.
1748 It is assumed to be a single-line subject.
1749 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1750 matter is removed.  Additional things can be deleted by setting
1751 `gnus-simplify-subject-fuzzy-regexp'."
1752   (let ((case-fold-search t)
1753         (modified-tick))
1754     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1755
1756     (while (not (eq modified-tick (buffer-modified-tick)))
1757       (setq modified-tick (buffer-modified-tick))
1758       (cond
1759        ((listp regexp)
1760         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1761        (regexp
1762         (gnus-simplify-buffer-fuzzy-step regexp)))
1763       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1764       (gnus-simplify-buffer-fuzzy-step
1765        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1766       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1767
1768     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1769     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1770     (gnus-simplify-buffer-fuzzy-step " $")
1771     (gnus-simplify-buffer-fuzzy-step "^ +")))
1772
1773 (defun gnus-simplify-subject-fuzzy (subject)
1774   "Simplify a subject string fuzzily.
1775 See `gnus-simplify-buffer-fuzzy' for details."
1776   (save-excursion
1777     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1778       (gnus-set-work-buffer)
1779       (let ((case-fold-search t))
1780         ;; Remove uninteresting prefixes.
1781         (when (and gnus-simplify-ignored-prefixes
1782                    (string-match gnus-simplify-ignored-prefixes subject))
1783           (setq subject (substring subject (match-end 0))))
1784         (insert subject)
1785         (inline (gnus-simplify-buffer-fuzzy regexp))
1786         (buffer-string)))))
1787
1788 (defsubst gnus-simplify-subject-fully (subject)
1789   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1790   (cond
1791    (gnus-simplify-subject-functions
1792     (gnus-map-function gnus-simplify-subject-functions subject))
1793    ((null gnus-summary-gather-subject-limit)
1794     (gnus-simplify-subject-re subject))
1795    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1796     (gnus-simplify-subject-fuzzy subject))
1797    ((numberp gnus-summary-gather-subject-limit)
1798     (truncate-string-to-width (gnus-simplify-subject-re subject)
1799                               gnus-summary-gather-subject-limit))
1800    (t
1801     subject)))
1802
1803 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1804   "Check whether two subjects are equal.
1805 If optional argument SIMPLE-FIRST is t, first argument is already
1806 simplified."
1807   (cond
1808    ((null simple-first)
1809     (equal (gnus-simplify-subject-fully s1)
1810            (gnus-simplify-subject-fully s2)))
1811    (t
1812     (equal s1
1813            (gnus-simplify-subject-fully s2)))))
1814
1815 (defun gnus-summary-bubble-group ()
1816   "Increase the score of the current group.
1817 This is a handy function to add to `gnus-summary-exit-hook' to
1818 increase the score of each group you read."
1819   (gnus-group-add-score gnus-newsgroup-name))
1820
1821 \f
1822 ;;;
1823 ;;; Gnus summary mode
1824 ;;;
1825
1826 (put 'gnus-summary-mode 'mode-class 'special)
1827
1828 (defvar gnus-article-commands-menu)
1829
1830 ;; Non-orthogonal keys
1831
1832 (gnus-define-keys gnus-summary-mode-map
1833   " " gnus-summary-next-page
1834   "\177" gnus-summary-prev-page
1835   [delete] gnus-summary-prev-page
1836   [backspace] gnus-summary-prev-page
1837   "\r" gnus-summary-scroll-up
1838   "\M-\r" gnus-summary-scroll-down
1839   "n" gnus-summary-next-unread-article
1840   "p" gnus-summary-prev-unread-article
1841   "N" gnus-summary-next-article
1842   "P" gnus-summary-prev-article
1843   "\M-\C-n" gnus-summary-next-same-subject
1844   "\M-\C-p" gnus-summary-prev-same-subject
1845   "\M-n" gnus-summary-next-unread-subject
1846   "\M-p" gnus-summary-prev-unread-subject
1847   "." gnus-summary-first-unread-article
1848   "," gnus-summary-best-unread-article
1849   "\M-s" gnus-summary-search-article-forward
1850   "\M-r" gnus-summary-search-article-backward
1851   "\M-S" gnus-summary-repeat-search-article-forward
1852   "\M-R" gnus-summary-repeat-search-article-backward
1853   "<" gnus-summary-beginning-of-article
1854   ">" gnus-summary-end-of-article
1855   "j" gnus-summary-goto-article
1856   "^" gnus-summary-refer-parent-article
1857   "\M-^" gnus-summary-refer-article
1858   "u" gnus-summary-tick-article-forward
1859   "!" gnus-summary-tick-article-forward
1860   "U" gnus-summary-tick-article-backward
1861   "d" gnus-summary-mark-as-read-forward
1862   "D" gnus-summary-mark-as-read-backward
1863   "E" gnus-summary-mark-as-expirable
1864   "\M-u" gnus-summary-clear-mark-forward
1865   "\M-U" gnus-summary-clear-mark-backward
1866   "k" gnus-summary-kill-same-subject-and-select
1867   "\C-k" gnus-summary-kill-same-subject
1868   "\M-\C-k" gnus-summary-kill-thread
1869   "\M-\C-l" gnus-summary-lower-thread
1870   "e" gnus-summary-edit-article
1871   "#" gnus-summary-mark-as-processable
1872   "\M-#" gnus-summary-unmark-as-processable
1873   "\M-\C-t" gnus-summary-toggle-threads
1874   "\M-\C-s" gnus-summary-show-thread
1875   "\M-\C-h" gnus-summary-hide-thread
1876   "\M-\C-f" gnus-summary-next-thread
1877   "\M-\C-b" gnus-summary-prev-thread
1878   [(meta down)] gnus-summary-next-thread
1879   [(meta up)] gnus-summary-prev-thread
1880   "\M-\C-u" gnus-summary-up-thread
1881   "\M-\C-d" gnus-summary-down-thread
1882   "&" gnus-summary-execute-command
1883   "c" gnus-summary-catchup-and-exit
1884   "\C-w" gnus-summary-mark-region-as-read
1885   "\C-t" gnus-summary-toggle-truncation
1886   "?" gnus-summary-mark-as-dormant
1887   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1888   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1889   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1890   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1891   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1892   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1893   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1894   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1895   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1896   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1897   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1898   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1899   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1900   "=" gnus-summary-expand-window
1901   "\C-x\C-s" gnus-summary-reselect-current-group
1902   "\M-g" gnus-summary-rescan-group
1903   "\C-c\C-r" gnus-summary-caesar-message
1904   "f" gnus-summary-followup
1905   "F" gnus-summary-followup-with-original
1906   "C" gnus-summary-cancel-article
1907   "r" gnus-summary-reply
1908   "R" gnus-summary-reply-with-original
1909   "\C-c\C-f" gnus-summary-mail-forward
1910   "o" gnus-summary-save-article
1911   "\C-o" gnus-summary-save-article-mail
1912   "|" gnus-summary-pipe-output
1913   "\M-k" gnus-summary-edit-local-kill
1914   "\M-K" gnus-summary-edit-global-kill
1915   ;; "V" gnus-version
1916   "\C-c\C-d" gnus-summary-describe-group
1917   "q" gnus-summary-exit
1918   "Q" gnus-summary-exit-no-update
1919   "\C-c\C-i" gnus-info-find-node
1920   gnus-mouse-2 gnus-mouse-pick-article
1921   [follow-link] mouse-face
1922   "m" gnus-summary-mail-other-window
1923   "a" gnus-summary-post-news
1924   "x" gnus-summary-limit-to-unread
1925   "s" gnus-summary-isearch-article
1926   [tab] gnus-summary-widget-forward
1927   "t" gnus-summary-toggle-header
1928   "g" gnus-summary-show-article
1929   "l" gnus-summary-goto-last-article
1930   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1931   "\C-d" gnus-summary-enter-digest-group
1932   "\M-\C-d" gnus-summary-read-document
1933   "\M-\C-e" gnus-summary-edit-parameters
1934   "\M-\C-a" gnus-summary-customize-parameters
1935   "\C-c\C-b" gnus-bug
1936   "*" gnus-cache-enter-article
1937   "\M-*" gnus-cache-remove-article
1938   "\M-&" gnus-summary-universal-argument
1939   "\C-l" gnus-recenter
1940   "I" gnus-summary-increase-score
1941   "L" gnus-summary-lower-score
1942   "\M-i" gnus-symbolic-argument
1943   "h" gnus-summary-select-article-buffer
1944
1945   "b" gnus-article-view-part
1946   "\M-t" gnus-summary-toggle-display-buttonized
1947
1948   "V" gnus-summary-score-map
1949   "X" gnus-uu-extract-map
1950   "S" gnus-summary-send-map)
1951
1952 ;; Sort of orthogonal keymap
1953 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1954   "t" gnus-summary-tick-article-forward
1955   "!" gnus-summary-tick-article-forward
1956   "d" gnus-summary-mark-as-read-forward
1957   "r" gnus-summary-mark-as-read-forward
1958   "c" gnus-summary-clear-mark-forward
1959   " " gnus-summary-clear-mark-forward
1960   "e" gnus-summary-mark-as-expirable
1961   "x" gnus-summary-mark-as-expirable
1962   "?" gnus-summary-mark-as-dormant
1963   "b" gnus-summary-set-bookmark
1964   "B" gnus-summary-remove-bookmark
1965   "#" gnus-summary-mark-as-processable
1966   "\M-#" gnus-summary-unmark-as-processable
1967   "S" gnus-summary-limit-include-expunged
1968   "C" gnus-summary-catchup
1969   "H" gnus-summary-catchup-to-here
1970   "h" gnus-summary-catchup-from-here
1971   "\C-c" gnus-summary-catchup-all
1972   "k" gnus-summary-kill-same-subject-and-select
1973   "K" gnus-summary-kill-same-subject
1974   "P" gnus-uu-mark-map)
1975
1976 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1977   "c" gnus-summary-clear-above
1978   "u" gnus-summary-tick-above
1979   "m" gnus-summary-mark-above
1980   "k" gnus-summary-kill-below)
1981
1982 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1983   "/" gnus-summary-limit-to-subject
1984   "n" gnus-summary-limit-to-articles
1985   "b" gnus-summary-limit-to-bodies
1986   "h" gnus-summary-limit-to-headers
1987   "w" gnus-summary-pop-limit
1988   "s" gnus-summary-limit-to-subject
1989   "a" gnus-summary-limit-to-author
1990   "u" gnus-summary-limit-to-unread
1991   "m" gnus-summary-limit-to-marks
1992   "M" gnus-summary-limit-exclude-marks
1993   "v" gnus-summary-limit-to-score
1994   "*" gnus-summary-limit-include-cached
1995   "D" gnus-summary-limit-include-dormant
1996   "T" gnus-summary-limit-include-thread
1997   "d" gnus-summary-limit-exclude-dormant
1998   "t" gnus-summary-limit-to-age
1999   "." gnus-summary-limit-to-unseen
2000   "x" gnus-summary-limit-to-extra
2001   "p" gnus-summary-limit-to-display-predicate
2002   "E" gnus-summary-limit-include-expunged
2003   "c" gnus-summary-limit-exclude-childless-dormant
2004   "C" gnus-summary-limit-mark-excluded-as-read
2005   "o" gnus-summary-insert-old-articles
2006   "N" gnus-summary-insert-new-articles
2007   "S" gnus-summary-limit-to-singletons
2008   "r" gnus-summary-limit-to-replied
2009   "R" gnus-summary-limit-to-recipient
2010   "A" gnus-summary-limit-to-address)
2011
2012 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2013   "n" gnus-summary-next-unread-article
2014   "p" gnus-summary-prev-unread-article
2015   "N" gnus-summary-next-article
2016   "P" gnus-summary-prev-article
2017   "\C-n" gnus-summary-next-same-subject
2018   "\C-p" gnus-summary-prev-same-subject
2019   "\M-n" gnus-summary-next-unread-subject
2020   "\M-p" gnus-summary-prev-unread-subject
2021   "f" gnus-summary-first-unread-article
2022   "b" gnus-summary-best-unread-article
2023   "j" gnus-summary-goto-article
2024   "g" gnus-summary-goto-subject
2025   "l" gnus-summary-goto-last-article
2026   "o" gnus-summary-pop-article)
2027
2028 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2029   "k" gnus-summary-kill-thread
2030   "E" gnus-summary-expire-thread
2031   "l" gnus-summary-lower-thread
2032   "i" gnus-summary-raise-thread
2033   "T" gnus-summary-toggle-threads
2034   "t" gnus-summary-rethread-current
2035   "^" gnus-summary-reparent-thread
2036   "\M-^" gnus-summary-reparent-children
2037   "s" gnus-summary-show-thread
2038   "S" gnus-summary-show-all-threads
2039   "h" gnus-summary-hide-thread
2040   "H" gnus-summary-hide-all-threads
2041   "n" gnus-summary-next-thread
2042   "p" gnus-summary-prev-thread
2043   "u" gnus-summary-up-thread
2044   "o" gnus-summary-top-thread
2045   "d" gnus-summary-down-thread
2046   "#" gnus-uu-mark-thread
2047   "\M-#" gnus-uu-unmark-thread)
2048
2049 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2050   "g" gnus-summary-prepare
2051   "c" gnus-summary-insert-cached-articles
2052   "d" gnus-summary-insert-dormant-articles
2053   "t" gnus-summary-insert-ticked-articles)
2054
2055 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2056   "c" gnus-summary-catchup-and-exit
2057   "C" gnus-summary-catchup-all-and-exit
2058   "E" gnus-summary-exit-no-update
2059   "Q" gnus-summary-exit
2060   "Z" gnus-summary-exit
2061   "n" gnus-summary-catchup-and-goto-next-group
2062   "p" gnus-summary-catchup-and-goto-prev-group
2063   "R" gnus-summary-reselect-current-group
2064   "G" gnus-summary-rescan-group
2065   "N" gnus-summary-next-group
2066   "s" gnus-summary-save-newsrc
2067   "P" gnus-summary-prev-group)
2068
2069 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2070   " " gnus-summary-next-page
2071   "n" gnus-summary-next-page
2072   "\177" gnus-summary-prev-page
2073   [delete] gnus-summary-prev-page
2074   "p" gnus-summary-prev-page
2075   "\r" gnus-summary-scroll-up
2076   "\M-\r" gnus-summary-scroll-down
2077   "<" gnus-summary-beginning-of-article
2078   ">" gnus-summary-end-of-article
2079   "b" gnus-summary-beginning-of-article
2080   "e" gnus-summary-end-of-article
2081   "^" gnus-summary-refer-parent-article
2082   "r" gnus-summary-refer-parent-article
2083   "C" gnus-summary-show-complete-article
2084   "D" gnus-summary-enter-digest-group
2085   "R" gnus-summary-refer-references
2086   "T" gnus-summary-refer-thread
2087   "W" gnus-warp-to-article
2088   "g" gnus-summary-show-article
2089   "s" gnus-summary-isearch-article
2090   [tab] gnus-summary-widget-forward
2091   "P" gnus-summary-print-article
2092   "S" gnus-sticky-article
2093   "M" gnus-mailing-list-insinuate
2094   "t" gnus-article-babel)
2095
2096 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2097   "b" gnus-article-add-buttons
2098   "B" gnus-article-add-buttons-to-head
2099   "o" gnus-article-treat-overstrike
2100   "e" gnus-article-emphasize
2101   "w" gnus-article-fill-cited-article
2102   "Q" gnus-article-fill-long-lines
2103   "L" gnus-article-toggle-truncate-lines
2104   "C" gnus-article-capitalize-sentences
2105   "c" gnus-article-remove-cr
2106   "q" gnus-article-de-quoted-unreadable
2107   "6" gnus-article-de-base64-unreadable
2108   "Z" gnus-article-decode-HZ
2109   "A" gnus-article-treat-ansi-sequences
2110   "h" gnus-article-wash-html
2111   "u" gnus-article-unsplit-urls
2112   "s" gnus-summary-force-verify-and-decrypt
2113   "f" gnus-article-display-x-face
2114   "l" gnus-summary-stop-page-breaking
2115   "r" gnus-summary-caesar-message
2116   "m" gnus-summary-morse-message
2117   "t" gnus-summary-toggle-header
2118   "g" gnus-treat-smiley
2119   "v" gnus-summary-verbose-headers
2120   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2121   "p" gnus-article-verify-x-pgp-sig
2122   "d" gnus-article-treat-dumbquotes
2123   "U" gnus-article-treat-non-ascii
2124   "i" gnus-summary-idna-message)
2125
2126 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2127   ;; mnemonic: deuglif*Y*
2128   "u" gnus-article-outlook-unwrap-lines
2129   "a" gnus-article-outlook-repair-attribution
2130   "c" gnus-article-outlook-rearrange-citation
2131   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2132
2133 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2134   "a" gnus-article-hide
2135   "h" gnus-article-hide-headers
2136   "b" gnus-article-hide-boring-headers
2137   "s" gnus-article-hide-signature
2138   "c" gnus-article-hide-citation
2139   "C" gnus-article-hide-citation-in-followups
2140   "l" gnus-article-hide-list-identifiers
2141   "B" gnus-article-strip-banner
2142   "P" gnus-article-hide-pem
2143   "\C-c" gnus-article-hide-citation-maybe)
2144
2145 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2146   "a" gnus-article-highlight
2147   "h" gnus-article-highlight-headers
2148   "c" gnus-article-highlight-citation
2149   "s" gnus-article-highlight-signature)
2150
2151 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2152   "f" gnus-article-treat-fold-headers
2153   "u" gnus-article-treat-unfold-headers
2154   "n" gnus-article-treat-fold-newsgroups)
2155
2156 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2157   "x" gnus-article-display-x-face
2158   "d" gnus-article-display-face
2159   "s" gnus-treat-smiley
2160   "D" gnus-article-remove-images
2161   "W" gnus-article-show-images
2162   "f" gnus-treat-from-picon
2163   "m" gnus-treat-mail-picon
2164   "n" gnus-treat-newsgroups-picon
2165   "g" gnus-treat-from-gravatar
2166   "h" gnus-treat-mail-gravatar)
2167
2168 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2169   "w" gnus-article-decode-mime-words
2170   "c" gnus-article-decode-charset
2171   "v" gnus-mime-view-all-parts
2172   "b" gnus-article-view-part)
2173
2174 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2175   "z" gnus-article-date-ut
2176   "u" gnus-article-date-ut
2177   "l" gnus-article-date-local
2178   "p" gnus-article-date-english
2179   "e" gnus-article-date-lapsed
2180   "o" gnus-article-date-original
2181   "i" gnus-article-date-iso8601
2182   "s" gnus-article-date-user)
2183
2184 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2185   "t" gnus-article-remove-trailing-blank-lines
2186   "l" gnus-article-strip-leading-blank-lines
2187   "m" gnus-article-strip-multiple-blank-lines
2188   "a" gnus-article-strip-blank-lines
2189   "A" gnus-article-strip-all-blank-lines
2190   "s" gnus-article-strip-leading-space
2191   "e" gnus-article-strip-trailing-space
2192   "w" gnus-article-remove-leading-whitespace)
2193
2194 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2195   "v" gnus-version
2196   "d" gnus-summary-describe-group
2197   "h" gnus-summary-describe-briefly
2198   "i" gnus-info-find-node)
2199
2200 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2201   "e" gnus-summary-expire-articles
2202   "\M-\C-e" gnus-summary-expire-articles-now
2203   "\177" gnus-summary-delete-article
2204   [delete] gnus-summary-delete-article
2205   [backspace] gnus-summary-delete-article
2206   "m" gnus-summary-move-article
2207   "r" gnus-summary-respool-article
2208   "w" gnus-summary-edit-article
2209   "c" gnus-summary-copy-article
2210   "B" gnus-summary-crosspost-article
2211   "q" gnus-summary-respool-query
2212   "t" gnus-summary-respool-trace
2213   "i" gnus-summary-import-article
2214   "I" gnus-summary-create-article
2215   "p" gnus-summary-article-posted-p)
2216
2217 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2218   "o" gnus-summary-save-article
2219   "m" gnus-summary-save-article-mail
2220   "F" gnus-summary-write-article-file
2221   "r" gnus-summary-save-article-rmail
2222   "f" gnus-summary-save-article-file
2223   "b" gnus-summary-save-article-body-file
2224   "B" gnus-summary-write-article-body-file
2225   "h" gnus-summary-save-article-folder
2226   "v" gnus-summary-save-article-vm
2227   "p" gnus-summary-pipe-output
2228   "P" gnus-summary-muttprint)
2229
2230 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2231   "b" gnus-summary-display-buttonized
2232   "m" gnus-summary-repair-multipart
2233   "v" gnus-article-view-part
2234   "o" gnus-article-save-part
2235   "O" gnus-article-save-part-and-strip
2236   "r" gnus-article-replace-part
2237   "d" gnus-article-delete-part
2238   "t" gnus-article-view-part-as-type
2239   "j" gnus-article-jump-to-part
2240   "c" gnus-article-copy-part
2241   "C" gnus-article-view-part-as-charset
2242   "e" gnus-article-view-part-externally
2243   "H" gnus-article-browse-html-article
2244   "E" gnus-article-encrypt-body
2245   "i" gnus-article-inline-part
2246   "|" gnus-article-pipe-part)
2247
2248 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2249   "p" gnus-summary-mark-as-processable
2250   "u" gnus-summary-unmark-as-processable
2251   "U" gnus-summary-unmark-all-processable
2252   "v" gnus-uu-mark-over
2253   "s" gnus-uu-mark-series
2254   "r" gnus-uu-mark-region
2255   "g" gnus-uu-unmark-region
2256   "R" gnus-uu-mark-by-regexp
2257   "G" gnus-uu-unmark-by-regexp
2258   "t" gnus-uu-mark-thread
2259   "T" gnus-uu-unmark-thread
2260   "a" gnus-uu-mark-all
2261   "b" gnus-uu-mark-buffer
2262   "S" gnus-uu-mark-sparse
2263   "k" gnus-summary-kill-process-mark
2264   "y" gnus-summary-yank-process-mark
2265   "w" gnus-summary-save-process-mark
2266   "i" gnus-uu-invert-processable)
2267
2268 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2269   ;;"x" gnus-uu-extract-any
2270   "m" gnus-summary-save-parts
2271   "u" gnus-uu-decode-uu
2272   "U" gnus-uu-decode-uu-and-save
2273   "s" gnus-uu-decode-unshar
2274   "S" gnus-uu-decode-unshar-and-save
2275   "o" gnus-uu-decode-save
2276   "O" gnus-uu-decode-save
2277   "b" gnus-uu-decode-binhex
2278   "B" gnus-uu-decode-binhex
2279   "Y" gnus-uu-decode-yenc
2280   "p" gnus-uu-decode-postscript
2281   "P" gnus-uu-decode-postscript-and-save)
2282
2283 (gnus-define-keys
2284     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2285   "u" gnus-uu-decode-uu-view
2286   "U" gnus-uu-decode-uu-and-save-view
2287   "s" gnus-uu-decode-unshar-view
2288   "S" gnus-uu-decode-unshar-and-save-view
2289   "o" gnus-uu-decode-save-view
2290   "O" gnus-uu-decode-save-view
2291   "b" gnus-uu-decode-binhex-view
2292   "B" gnus-uu-decode-binhex-view
2293   "p" gnus-uu-decode-postscript-view
2294   "P" gnus-uu-decode-postscript-and-save-view)
2295
2296 (defvar gnus-article-post-menu nil)
2297
2298 (defconst gnus-summary-menu-maxlen 20)
2299
2300 (defun gnus-summary-menu-split (menu)
2301   ;; If we have lots of elements, divide them into groups of 20
2302   ;; and make a pane (or submenu) for each one.
2303   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2304       (let ((menu menu) sublists next
2305             (i 1))
2306         (while menu
2307           ;; Pull off the next gnus-summary-menu-maxlen elements
2308           ;; and make them the next element of sublist.
2309           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2310           (if next
2311               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2312                       nil))
2313           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2314                                              (aref (car (last menu)) 0)) menu)
2315                                sublists))
2316           (setq i (1+ i))
2317           (setq menu next))
2318         (nreverse sublists))
2319     ;; Few elements--put them all in one pane.
2320     menu))
2321
2322 (defun gnus-summary-make-menu-bar ()
2323   (gnus-turn-off-edit-menu 'summary)
2324
2325   (unless (boundp 'gnus-summary-misc-menu)
2326
2327     (easy-menu-define
2328       gnus-summary-kill-menu gnus-summary-mode-map ""
2329       (cons
2330        "Score"
2331        (nconc
2332         (list
2333          ["Customize" gnus-score-customize t])
2334         (gnus-make-score-map 'increase)
2335         (gnus-make-score-map 'lower)
2336         '(("Mark"
2337            ["Kill below" gnus-summary-kill-below t]
2338            ["Mark above" gnus-summary-mark-above t]
2339            ["Tick above" gnus-summary-tick-above t]
2340            ["Clear above" gnus-summary-clear-above t])
2341           ["Current score" gnus-summary-current-score t]
2342           ["Set score" gnus-summary-set-score t]
2343           ["Switch current score file..." gnus-score-change-score-file t]
2344           ["Set mark below..." gnus-score-set-mark-below t]
2345           ["Set expunge below..." gnus-score-set-expunge-below t]
2346           ["Edit current score file" gnus-score-edit-current-scores t]
2347           ["Edit score file..." gnus-score-edit-file t]
2348           ["Trace score" gnus-score-find-trace t]
2349           ["Find words" gnus-score-find-favourite-words t]
2350           ["Rescore buffer" gnus-summary-rescore t]
2351           ["Increase score..." gnus-summary-increase-score t]
2352           ["Lower score..." gnus-summary-lower-score t]))))
2353
2354     ;; Define both the Article menu in the summary buffer and the
2355     ;; equivalent Commands menu in the article buffer here for
2356     ;; consistency.
2357     (let ((innards
2358            `(("Hide"
2359               ["All" gnus-article-hide t]
2360               ["Headers" gnus-article-hide-headers t]
2361               ["Signature" gnus-article-hide-signature t]
2362               ["Citation" gnus-article-hide-citation t]
2363               ["List identifiers" gnus-article-hide-list-identifiers t]
2364               ["Banner" gnus-article-strip-banner t]
2365               ["Boring headers" gnus-article-hide-boring-headers t])
2366              ("Highlight"
2367               ["All" gnus-article-highlight t]
2368               ["Headers" gnus-article-highlight-headers t]
2369               ["Signature" gnus-article-highlight-signature t]
2370               ["Citation" gnus-article-highlight-citation t])
2371              ("MIME"
2372               ["Words" gnus-article-decode-mime-words t]
2373               ["Charset" gnus-article-decode-charset t]
2374               ["QP" gnus-article-de-quoted-unreadable t]
2375               ["Base64" gnus-article-de-base64-unreadable t]
2376               ["View MIME buttons" gnus-summary-display-buttonized t]
2377               ["View all" gnus-mime-view-all-parts t]
2378               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2379               ["Encrypt body" gnus-article-encrypt-body
2380                :active (not (gnus-group-read-only-p))
2381                ,@(if (featurep 'xemacs) nil
2382                    '(:help "Encrypt the message body on disk"))]
2383               ["Extract all parts..." gnus-summary-save-parts t]
2384               ("Multipart"
2385                ["Repair multipart" gnus-summary-repair-multipart t]
2386                ["Pipe part..." gnus-article-pipe-part t]
2387                ["Inline part" gnus-article-inline-part t]
2388                ["View part as type..." gnus-article-view-part-as-type t]
2389                ["Encrypt body" gnus-article-encrypt-body
2390                 :active (not (gnus-group-read-only-p))
2391                ,@(if (featurep 'xemacs) nil
2392                    '(:help "Encrypt the message body on disk"))]
2393                ["View part externally" gnus-article-view-part-externally t]
2394                ["View HTML parts in browser" gnus-article-browse-html-article t]
2395                ["View part with charset..." gnus-article-view-part-as-charset t]
2396                ["Copy part" gnus-article-copy-part t]
2397                ["Save part..." gnus-article-save-part t]
2398                ["View part" gnus-article-view-part t]))
2399              ("Date"
2400               ["Local" gnus-article-date-local t]
2401               ["ISO8601" gnus-article-date-iso8601 t]
2402               ["UT" gnus-article-date-ut t]
2403               ["Original" gnus-article-date-original t]
2404               ["Lapsed" gnus-article-date-lapsed t]
2405               ["User-defined" gnus-article-date-user t])
2406              ("Display"
2407               ["Remove images" gnus-article-remove-images t]
2408               ["Toggle smiley" gnus-treat-smiley t]
2409               ["Show X-Face" gnus-article-display-x-face t]
2410               ["Show picons in From" gnus-treat-from-picon t]
2411               ["Show picons in mail headers" gnus-treat-mail-picon t]
2412               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2413               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2414               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2415               ("View as different encoding"
2416                ,@(gnus-summary-menu-split
2417                   (mapcar
2418                    (lambda (cs)
2419                      ;; Since easymenu under Emacs doesn't allow
2420                      ;; lambda forms for menu commands, we should
2421                      ;; provide intern'ed function symbols.
2422                      (let ((command (intern (format "\
2423 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2424                        (fset command
2425                              `(lambda ()
2426                                 (interactive)
2427                                 (let ((gnus-summary-show-article-charset-alist
2428                                        '((1 . ,cs))))
2429                                   (gnus-summary-show-article 1))))
2430                        `[,(symbol-name cs) ,command t]))
2431                    (sort (if (fboundp 'coding-system-list)
2432                              (coding-system-list)
2433                            (mapcar 'car mm-mime-mule-charset-alist))
2434                          'string<)))))
2435              ("Washing"
2436               ("Remove Blanks"
2437                ["Leading" gnus-article-strip-leading-blank-lines t]
2438                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2439                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2440                ["All of the above" gnus-article-strip-blank-lines t]
2441                ["All" gnus-article-strip-all-blank-lines t]
2442                ["Leading space" gnus-article-strip-leading-space t]
2443                ["Trailing space" gnus-article-strip-trailing-space t]
2444                ["Leading space in headers"
2445                 gnus-article-remove-leading-whitespace t])
2446               ["Overstrike" gnus-article-treat-overstrike t]
2447               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2448               ["Non-ASCII" gnus-article-treat-non-ascii t]
2449               ["Emphasis" gnus-article-emphasize t]
2450               ["Word wrap" gnus-article-fill-cited-article t]
2451               ["Fill long lines" gnus-article-fill-long-lines t]
2452               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2453               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2454               ["Remove CR" gnus-article-remove-cr t]
2455               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2456               ["Base64" gnus-article-de-base64-unreadable t]
2457               ["Rot 13" gnus-summary-caesar-message
2458                ,@(if (featurep 'xemacs) '(t)
2459                    '(:help "\"Caesar rotate\" article by 13"))]
2460               ["De-IDNA" gnus-summary-idna-message t]
2461               ["Morse decode" gnus-summary-morse-message t]
2462               ["Unix pipe..." gnus-summary-pipe-message t]
2463               ["Add buttons" gnus-article-add-buttons t]
2464               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2465               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2466               ["Verbose header" gnus-summary-verbose-headers t]
2467               ["Toggle header" gnus-summary-toggle-header t]
2468               ["Unfold headers" gnus-article-treat-unfold-headers t]
2469               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2470               ["Html" gnus-article-wash-html t]
2471               ["Unsplit URLs" gnus-article-unsplit-urls t]
2472               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2473               ["Decode HZ" gnus-article-decode-HZ t]
2474               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2475               ("(Outlook) Deuglify"
2476                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2477                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2478                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2479                ["Full (Outlook) deuglify"
2480                 gnus-article-outlook-deuglify-article t])
2481               )
2482              ("Output"
2483               ["Save in default format..." gnus-summary-save-article
2484                ,@(if (featurep 'xemacs) '(t)
2485                    '(:help "Save article using default method"))]
2486               ["Save in file..." gnus-summary-save-article-file
2487                ,@(if (featurep 'xemacs) '(t)
2488                    '(:help "Save article in file"))]
2489               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2490               ["Save in MH folder..." gnus-summary-save-article-folder t]
2491               ["Save in VM folder..." gnus-summary-save-article-vm t]
2492               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2493               ["Save body in file..." gnus-summary-save-article-body-file t]
2494               ["Pipe through a filter..." gnus-summary-pipe-output t]
2495               ["Print with Muttprint..." gnus-summary-muttprint t]
2496               ["Print" gnus-summary-print-article
2497                ,@(if (featurep 'xemacs) '(t)
2498                    '(:help "Generate and print a PostScript image"))])
2499              ("Copy, move,... (Backend)"
2500               ,@(if (featurep 'xemacs) nil
2501                   '(:help "Copying, moving, expiring articles..."))
2502               ["Respool article..." gnus-summary-respool-article t]
2503               ["Move article..." gnus-summary-move-article
2504                (gnus-check-backend-function
2505                 'request-move-article gnus-newsgroup-name)]
2506               ["Copy article..." gnus-summary-copy-article t]
2507               ["Crosspost article..." gnus-summary-crosspost-article
2508                (gnus-check-backend-function
2509                 'request-replace-article gnus-newsgroup-name)]
2510               ["Import file..." gnus-summary-import-article
2511                (gnus-check-backend-function
2512                 'request-accept-article gnus-newsgroup-name)]
2513               ["Create article..." gnus-summary-create-article
2514                (gnus-check-backend-function
2515                 'request-accept-article gnus-newsgroup-name)]
2516               ["Check if posted" gnus-summary-article-posted-p t]
2517               ["Edit article" gnus-summary-edit-article
2518                (not (gnus-group-read-only-p))]
2519               ["Delete article" gnus-summary-delete-article
2520                (gnus-check-backend-function
2521                 'request-expire-articles gnus-newsgroup-name)]
2522               ["Query respool" gnus-summary-respool-query t]
2523               ["Trace respool" gnus-summary-respool-trace t]
2524               ["Delete expirable articles" gnus-summary-expire-articles-now
2525                (gnus-check-backend-function
2526                 'request-expire-articles gnus-newsgroup-name)])
2527              ("Extract"
2528               ["Uudecode" gnus-uu-decode-uu
2529                ,@(if (featurep 'xemacs) '(t)
2530                    '(:help "Decode uuencoded article(s)"))]
2531               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2532               ["Unshar" gnus-uu-decode-unshar t]
2533               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2534               ["Save" gnus-uu-decode-save t]
2535               ["Binhex" gnus-uu-decode-binhex t]
2536               ["Postscript" gnus-uu-decode-postscript t]
2537               ["All MIME parts" gnus-summary-save-parts t])
2538              ("Cache"
2539               ["Enter article" gnus-cache-enter-article t]
2540               ["Remove article" gnus-cache-remove-article t])
2541              ["Translate" gnus-article-babel t]
2542              ["Select article buffer" gnus-summary-select-article-buffer t]
2543              ["Make article buffer sticky" gnus-sticky-article t]
2544              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2545              ["Isearch article..." gnus-summary-isearch-article t]
2546              ["Beginning of the article" gnus-summary-beginning-of-article t]
2547              ["End of the article" gnus-summary-end-of-article t]
2548              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2549              ["Fetch referenced articles" gnus-summary-refer-references t]
2550              ["Fetch current thread" gnus-summary-refer-thread t]
2551              ["Fetch article with id..." gnus-summary-refer-article t]
2552              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2553              ["Redisplay" gnus-summary-show-article t]
2554              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2555       (easy-menu-define
2556         gnus-summary-article-menu gnus-summary-mode-map ""
2557         (cons "Article" innards))
2558
2559       (if (not (keymapp gnus-summary-article-menu))
2560           (easy-menu-define
2561             gnus-article-commands-menu gnus-article-mode-map ""
2562             (cons "Commands" innards))
2563         ;; in Emacs, don't share menu.
2564         (setq gnus-article-commands-menu
2565               (copy-keymap gnus-summary-article-menu))
2566         (define-key gnus-article-mode-map [menu-bar commands]
2567           (cons "Commands" gnus-article-commands-menu))))
2568
2569     (easy-menu-define
2570       gnus-summary-thread-menu gnus-summary-mode-map ""
2571       '("Threads"
2572         ["Find all messages in thread" gnus-summary-refer-thread t]
2573         ["Toggle threading" gnus-summary-toggle-threads t]
2574         ["Hide threads" gnus-summary-hide-all-threads t]
2575         ["Show threads" gnus-summary-show-all-threads t]
2576         ["Hide thread" gnus-summary-hide-thread t]
2577         ["Show thread" gnus-summary-show-thread t]
2578         ["Go to next thread" gnus-summary-next-thread t]
2579         ["Go to previous thread" gnus-summary-prev-thread t]
2580         ["Go down thread" gnus-summary-down-thread t]
2581         ["Go up thread" gnus-summary-up-thread t]
2582         ["Top of thread" gnus-summary-top-thread t]
2583         ["Mark thread as read" gnus-summary-kill-thread t]
2584         ["Mark thread as expired" gnus-summary-expire-thread t]
2585         ["Lower thread score" gnus-summary-lower-thread t]
2586         ["Raise thread score" gnus-summary-raise-thread t]
2587         ["Rethread current" gnus-summary-rethread-current t]))
2588
2589     (easy-menu-define
2590       gnus-summary-post-menu gnus-summary-mode-map ""
2591       `("Post"
2592         ["Send a message (mail or news)" gnus-summary-post-news
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Compose a new message (mail or news)"))]
2595         ["Followup" gnus-summary-followup
2596          ,@(if (featurep 'xemacs) '(t)
2597              '(:help "Post followup to this article"))]
2598         ["Followup and yank" gnus-summary-followup-with-original
2599          ,@(if (featurep 'xemacs) '(t)
2600              '(:help "Post followup to this article, quoting its contents"))]
2601         ["Supersede article" gnus-summary-supersede-article t]
2602         ["Cancel article" gnus-summary-cancel-article
2603          ,@(if (featurep 'xemacs) '(t)
2604              '(:help "Cancel an article you posted"))]
2605         ["Reply" gnus-summary-reply t]
2606         ["Reply and yank" gnus-summary-reply-with-original t]
2607         ["Wide reply" gnus-summary-wide-reply t]
2608         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2609          ,@(if (featurep 'xemacs) '(t)
2610              '(:help "Mail a reply, quoting this article"))]
2611         ["Very wide reply" gnus-summary-very-wide-reply t]
2612         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2613          ,@(if (featurep 'xemacs) '(t)
2614              '(:help "Mail a very wide reply, quoting this article"))]
2615         ["Mail forward" gnus-summary-mail-forward t]
2616         ["Post forward" gnus-summary-post-forward t]
2617         ["Digest and mail" gnus-uu-digest-mail-forward t]
2618         ["Digest and post" gnus-uu-digest-post-forward t]
2619         ["Resend message" gnus-summary-resend-message t]
2620         ["Resend message edit" gnus-summary-resend-message-edit t]
2621         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2622         ["Send a mail" gnus-summary-mail-other-window t]
2623         ["Create a local message" gnus-summary-news-other-window t]
2624         ["Uuencode and post" gnus-uu-post-news
2625          ,@(if (featurep 'xemacs) '(t)
2626              '(:help "Post a uuencoded article"))]
2627         ["Followup via news" gnus-summary-followup-to-mail t]
2628         ["Followup via news and yank"
2629          gnus-summary-followup-to-mail-with-original t]
2630         ["Strip signature on reply"
2631          (lambda ()
2632            (interactive)
2633            (if (not (memq message-cite-function
2634                           '(message-cite-original-without-signature
2635                             message-cite-original)))
2636                ;; Stupid workaround for XEmacs not honoring :visible.
2637                (message "Can't toggle this value of `message-cite-function'")
2638              (setq message-cite-function
2639                    (if (eq message-cite-function
2640                            'message-cite-original-without-signature)
2641                        'message-cite-original
2642                      'message-cite-original-without-signature))))
2643          ;; XEmacs barfs on :visible.
2644          ,@(if (featurep 'xemacs) nil
2645              '(:visible (memq message-cite-function
2646                               '(message-cite-original-without-signature
2647                                 message-cite-original))))
2648          :style toggle
2649          :selected (eq message-cite-function
2650                        'message-cite-original-without-signature)
2651          ,@(if (featurep 'xemacs) nil
2652              '(:help "Strip signature from cited article when replying."))]
2653         ;;("Draft"
2654         ;;["Send" gnus-summary-send-draft t]
2655         ;;["Send bounced" gnus-resend-bounced-mail t])
2656         ))
2657
2658     (cond
2659      ((not (keymapp gnus-summary-post-menu))
2660       (setq gnus-article-post-menu gnus-summary-post-menu))
2661      ((not gnus-article-post-menu)
2662       ;; Don't share post menu.
2663       (setq gnus-article-post-menu
2664             (copy-keymap gnus-summary-post-menu))))
2665     (define-key gnus-article-mode-map [menu-bar post]
2666       (cons "Post" gnus-article-post-menu))
2667
2668     (easy-menu-define
2669       gnus-summary-misc-menu gnus-summary-mode-map ""
2670       `("Gnus"
2671         ("Mark Read"
2672          ["Mark as read" gnus-summary-mark-as-read-forward t]
2673          ["Mark same subject and select"
2674           gnus-summary-kill-same-subject-and-select t]
2675          ["Mark same subject" gnus-summary-kill-same-subject t]
2676          ["Catchup" gnus-summary-catchup
2677           ,@(if (featurep 'xemacs) '(t)
2678               '(:help "Mark unread articles in this group as read"))]
2679          ["Catchup all" gnus-summary-catchup-all t]
2680          ["Catchup to here" gnus-summary-catchup-to-here t]
2681          ["Catchup from here" gnus-summary-catchup-from-here t]
2682          ["Catchup region" gnus-summary-mark-region-as-read
2683           (gnus-mark-active-p)]
2684          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2685         ("Mark Various"
2686          ["Tick" gnus-summary-tick-article-forward t]
2687          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2688          ["Remove marks" gnus-summary-clear-mark-forward t]
2689          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2690          ["Set bookmark" gnus-summary-set-bookmark t]
2691          ["Remove bookmark" gnus-summary-remove-bookmark t])
2692         ("Limit to"
2693          ["Marks..." gnus-summary-limit-to-marks t]
2694          ["Subject..." gnus-summary-limit-to-subject t]
2695          ["Author..." gnus-summary-limit-to-author t]
2696          ["Recipient..." gnus-summary-limit-to-recipient t]
2697          ["Address..." gnus-summary-limit-to-address t]
2698          ["Age..." gnus-summary-limit-to-age t]
2699          ["Extra..." gnus-summary-limit-to-extra t]
2700          ["Score..." gnus-summary-limit-to-score t]
2701          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2702          ["Unread" gnus-summary-limit-to-unread t]
2703          ["Unseen" gnus-summary-limit-to-unseen t]
2704          ["Singletons" gnus-summary-limit-to-singletons t]
2705          ["Replied" gnus-summary-limit-to-replied t]
2706          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2707          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2708          ["Pop limit" gnus-summary-pop-limit t]
2709          ["Show dormant" gnus-summary-limit-include-dormant t]
2710          ["Hide childless dormant"
2711           gnus-summary-limit-exclude-childless-dormant t]
2712          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2713          ["Hide marked" gnus-summary-limit-exclude-marks t]
2714          ["Show expunged" gnus-summary-limit-include-expunged t])
2715         ("Process Mark"
2716          ["Set mark" gnus-summary-mark-as-processable t]
2717          ["Remove mark" gnus-summary-unmark-as-processable t]
2718          ["Remove all marks" gnus-summary-unmark-all-processable t]
2719          ["Invert marks" gnus-uu-invert-processable t]
2720          ["Mark above" gnus-uu-mark-over t]
2721          ["Mark series" gnus-uu-mark-series t]
2722          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2723          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2724          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2725          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2726          ["Mark all" gnus-uu-mark-all t]
2727          ["Mark buffer" gnus-uu-mark-buffer t]
2728          ["Mark sparse" gnus-uu-mark-sparse t]
2729          ["Mark thread" gnus-uu-mark-thread t]
2730          ["Unmark thread" gnus-uu-unmark-thread t]
2731          ("Process Mark Sets"
2732           ["Kill" gnus-summary-kill-process-mark t]
2733           ["Yank" gnus-summary-yank-process-mark
2734            gnus-newsgroup-process-stack]
2735           ["Save" gnus-summary-save-process-mark t]
2736           ["Run command on marked..." gnus-summary-universal-argument t]))
2737         ("Registry Marks")
2738         ("Scroll article"
2739          ["Page forward" gnus-summary-next-page
2740           ,@(if (featurep 'xemacs) '(t)
2741               '(:help "Show next page of article"))]
2742          ["Page backward" gnus-summary-prev-page
2743           ,@(if (featurep 'xemacs) '(t)
2744               '(:help "Show previous page of article"))]
2745          ["Line forward" gnus-summary-scroll-up t])
2746         ("Move"
2747          ["Next unread article" gnus-summary-next-unread-article t]
2748          ["Previous unread article" gnus-summary-prev-unread-article t]
2749          ["Next article" gnus-summary-next-article t]
2750          ["Previous article" gnus-summary-prev-article t]
2751          ["Next unread subject" gnus-summary-next-unread-subject t]
2752          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2753          ["Next article same subject" gnus-summary-next-same-subject t]
2754          ["Previous article same subject" gnus-summary-prev-same-subject t]
2755          ["First unread article" gnus-summary-first-unread-article t]
2756          ["Best unread article" gnus-summary-best-unread-article t]
2757          ["Go to subject number..." gnus-summary-goto-subject t]
2758          ["Go to article number..." gnus-summary-goto-article t]
2759          ["Go to the last article" gnus-summary-goto-last-article t]
2760          ["Pop article off history" gnus-summary-pop-article t])
2761         ("Sort"
2762          ["Sort by number" gnus-summary-sort-by-number t]
2763          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2764          ["Sort by author" gnus-summary-sort-by-author t]
2765          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2766          ["Sort by subject" gnus-summary-sort-by-subject t]
2767          ["Sort by date" gnus-summary-sort-by-date t]
2768          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2769          ["Sort by score" gnus-summary-sort-by-score t]
2770          ["Sort by lines" gnus-summary-sort-by-lines t]
2771          ["Sort by characters" gnus-summary-sort-by-chars t]
2772          ["Randomize" gnus-summary-sort-by-random t]
2773          ["Original sort" gnus-summary-sort-by-original t])
2774         ("Help"
2775          ["Describe group" gnus-summary-describe-group t]
2776          ["Read manual" gnus-info-find-node t])
2777         ("Modes"
2778          ["Pick and read" gnus-pick-mode t]
2779          ["Binary" gnus-binary-mode t])
2780         ("Regeneration"
2781          ["Regenerate" gnus-summary-prepare t]
2782          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2783          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2784          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2785          ["Toggle threading" gnus-summary-toggle-threads t])
2786         ["See old articles" gnus-summary-insert-old-articles t]
2787         ["See new articles" gnus-summary-insert-new-articles t]
2788         ["Filter articles..." gnus-summary-execute-command t]
2789         ["Run command on articles..." gnus-summary-universal-argument t]
2790         ["Search articles forward..." gnus-summary-search-article-forward t]
2791         ["Search articles backward..." gnus-summary-search-article-backward t]
2792         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2793         ["Expand window" gnus-summary-expand-window t]
2794         ["Expire expirable articles" gnus-summary-expire-articles
2795          (gnus-check-backend-function
2796           'request-expire-articles gnus-newsgroup-name)]
2797         ["Edit local kill file" gnus-summary-edit-local-kill t]
2798         ["Edit main kill file" gnus-summary-edit-global-kill t]
2799         ["Edit group parameters" gnus-summary-edit-parameters t]
2800         ["Customize group parameters" gnus-summary-customize-parameters t]
2801         ["Send a bug report" gnus-bug t]
2802         ("Exit"
2803          ["Catchup and exit" gnus-summary-catchup-and-exit
2804           ,@(if (featurep 'xemacs) '(t)
2805               '(:help "Mark unread articles in this group as read, then exit"))]
2806          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2807          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2808          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2809          ["Exit group" gnus-summary-exit
2810           ,@(if (featurep 'xemacs) '(t)
2811               '(:help "Exit current group, return to group selection mode"))]
2812          ["Exit group without updating" gnus-summary-exit-no-update t]
2813          ["Exit and goto next group" gnus-summary-next-group t]
2814          ["Exit and goto prev group" gnus-summary-prev-group t]
2815          ["Reselect group" gnus-summary-reselect-current-group t]
2816          ["Rescan group" gnus-summary-rescan-group t]
2817          ["Update dribble" gnus-summary-save-newsrc t])))
2818
2819     (gnus-run-hooks 'gnus-summary-menu-hook)))
2820
2821 (defvar gnus-summary-tool-bar-map nil)
2822
2823 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2824 ;; affect _new_ message buffers.  We might add a function that walks thru all
2825 ;; summary-mode buffers and force the update.
2826 (defun gnus-summary-tool-bar-update (&optional symbol value)
2827   "Update summary mode toolbar.
2828 Setter function for custom variables."
2829   (setq-default gnus-summary-tool-bar-map nil)
2830   (when symbol
2831     ;; When used as ":set" function:
2832     (set-default symbol value))
2833   (when (gnus-buffer-live-p gnus-summary-buffer)
2834     (with-current-buffer gnus-summary-buffer
2835       (gnus-summary-make-tool-bar))))
2836
2837 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2838                                      'gnus-summary-tool-bar-gnome
2839                                    'gnus-summary-tool-bar-retro)
2840   "Specifies the Gnus summary tool bar.
2841
2842 It can be either a list or a symbol refering to a list.  See
2843 `gmm-tool-bar-from-list' for the format of the list.  The
2844 default key map is `gnus-summary-mode-map'.
2845
2846 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2847 `gnus-summary-tool-bar-retro'."
2848   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2849                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2850                  (repeat :tag "User defined list" gmm-tool-bar-item)
2851                  (symbol))
2852   :version "23.1" ;; No Gnus
2853   :initialize 'custom-initialize-default
2854   :set 'gnus-summary-tool-bar-update
2855   :group 'gnus-summary)
2856
2857 (defcustom gnus-summary-tool-bar-gnome
2858   '((gnus-summary-post-news "mail/compose" nil)
2859     (gnus-summary-insert-new-articles "mail/inbox" nil
2860                                       :visible (or (not gnus-agent)
2861                                                    gnus-plugged))
2862     (gnus-summary-reply-with-original "mail/reply")
2863     (gnus-summary-reply "mail/reply" nil :visible nil)
2864     (gnus-summary-followup-with-original "mail/reply-all")
2865     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2866     (gnus-summary-mail-forward "mail/forward")
2867     (gnus-summary-save-article "mail/save")
2868     (gnus-summary-search-article-forward "search" nil :visible nil)
2869     (gnus-summary-print-article "print")
2870     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2871     ;; Some new commands that may need more suitable icons:
2872     (gnus-summary-save-newsrc "save" nil :visible nil)
2873     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2874     (gnus-summary-prev-article "left-arrow")
2875     (gnus-summary-next-article "right-arrow")
2876     (gnus-summary-next-page "next-page")
2877     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2878     ;;
2879     ;; Maybe some sort-by-... could be added:
2880     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2881     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2882     (gnus-summary-mark-as-expirable
2883      "delete" nil
2884      :visible (gnus-check-backend-function 'request-expire-articles
2885                                            gnus-newsgroup-name))
2886     (gnus-summary-mark-as-spam
2887      "mail/spam" t
2888      :visible (and (fboundp 'spam-group-ham-contents-p)
2889                    (spam-group-ham-contents-p gnus-newsgroup-name))
2890      :help "Mark as spam")
2891     (gnus-summary-mark-as-read-forward
2892      "mail/not-spam" nil
2893      :visible (and (fboundp 'spam-group-spam-contents-p)
2894                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2895     ;;
2896     (gnus-summary-exit "exit")
2897     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2898     (gnus-info-find-node "help"))
2899   "List of functions for the summary tool bar (GNOME style).
2900
2901 See `gmm-tool-bar-from-list' for the format of the list."
2902   :type '(repeat gmm-tool-bar-item)
2903   :version "23.1" ;; No Gnus
2904   :initialize 'custom-initialize-default
2905   :set 'gnus-summary-tool-bar-update
2906   :group 'gnus-summary)
2907
2908 (defcustom gnus-summary-tool-bar-retro
2909   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2910     (gnus-summary-next-unread-article "gnus/next-ur")
2911     (gnus-summary-post-news "gnus/post")
2912     (gnus-summary-followup-with-original "gnus/fuwo")
2913     (gnus-summary-followup "gnus/followup")
2914     (gnus-summary-reply-with-original "gnus/reply-wo")
2915     (gnus-summary-reply "gnus/reply")
2916     (gnus-summary-caesar-message "gnus/rot13")
2917     (gnus-uu-decode-uu "gnus/uu-decode")
2918     (gnus-summary-save-article-file "gnus/save-aif")
2919     (gnus-summary-save-article "gnus/save-art")
2920     (gnus-uu-post-news "gnus/uu-post")
2921     (gnus-summary-catchup "gnus/catchup")
2922     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2923     (gnus-summary-exit "gnus/exit-summ")
2924     ;; Some new command that may need more suitable icons:
2925     (gnus-summary-print-article "gnus/print" nil :visible nil)
2926     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2927     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2928     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2929     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2930     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2931     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2932     ;;
2933     (gnus-info-find-node "gnus/help" nil :visible nil))
2934   "List of functions for the summary tool bar (retro look).
2935
2936 See `gmm-tool-bar-from-list' for the format of the list."
2937   :type '(repeat gmm-tool-bar-item)
2938   :version "23.1" ;; No Gnus
2939   :initialize 'custom-initialize-default
2940   :set 'gnus-summary-tool-bar-update
2941   :group 'gnus-summary)
2942
2943 (defcustom gnus-summary-tool-bar-zap-list t
2944   "List of icon items from the global tool bar.
2945 These items are not displayed in the Gnus summary mode tool bar.
2946
2947 See `gmm-tool-bar-from-list' for the format of the list."
2948   :type 'gmm-tool-bar-zap-list
2949   :version "23.1" ;; No Gnus
2950   :initialize 'custom-initialize-default
2951   :set 'gnus-summary-tool-bar-update
2952   :group 'gnus-summary)
2953
2954 (defvar image-load-path)
2955 (defvar tool-bar-map)
2956
2957 (defun gnus-summary-make-tool-bar (&optional force)
2958   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2959 When FORCE, rebuild the tool bar."
2960   (when (and (not (featurep 'xemacs))
2961              (boundp 'tool-bar-mode)
2962              tool-bar-mode
2963              (or (not gnus-summary-tool-bar-map) force))
2964     (let* ((load-path
2965             (gmm-image-load-path-for-library "gnus"
2966                                              "mail/save.xpm"
2967                                              nil t))
2968            (image-load-path (cons (car load-path)
2969                                   (when (boundp 'image-load-path)
2970                                     image-load-path)))
2971            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2972                                         gnus-summary-tool-bar-zap-list
2973                                         'gnus-summary-mode-map)))
2974       (when map
2975         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2976         ;; uses it's value.
2977         (setq gnus-summary-tool-bar-map map))))
2978   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2979
2980 (defun gnus-score-set-default (var value)
2981   "A version of set that updates the GNU Emacs menu-bar."
2982   (set var value)
2983   ;; It is the message that forces the active status to be updated.
2984   (message ""))
2985
2986 (defun gnus-make-score-map (type)
2987   "Make a summary score map of type TYPE."
2988   (if t
2989       nil
2990     (let ((headers '(("author" "from" string)
2991                      ("subject" "subject" string)
2992                      ("article body" "body" string)
2993                      ("article head" "head" string)
2994                      ("xref" "xref" string)
2995                      ("extra header" "extra" string)
2996                      ("lines" "lines" number)
2997                      ("followups to author" "followup" string)))
2998           (types '((number ("less than" <)
2999                            ("greater than" >)
3000                            ("equal" =))
3001                    (string ("substring" s)
3002                            ("exact string" e)
3003                            ("fuzzy string" f)
3004                            ("regexp" r))))
3005           (perms '(("temporary" (current-time-string))
3006                    ("permanent" nil)
3007                    ("immediate" now)))
3008           header)
3009       (list
3010        (apply
3011         'nconc
3012         (list
3013          (if (eq type 'lower)
3014              "Lower score"
3015            "Increase score"))
3016         (let (outh)
3017           (while headers
3018             (setq header (car headers))
3019             (setq outh
3020                   (cons
3021                    (apply
3022                     'nconc
3023                     (list (car header))
3024                     (let ((ts (cdr (assoc (nth 2 header) types)))
3025                           outt)
3026                       (while ts
3027                         (setq outt
3028                               (cons
3029                                (apply
3030                                 'nconc
3031                                 (list (caar ts))
3032                                 (let ((ps perms)
3033                                       outp)
3034                                   (while ps
3035                                     (setq outp
3036                                           (cons
3037                                            (vector
3038                                             (caar ps)
3039                                             (list
3040                                              'gnus-summary-score-entry
3041                                              (nth 1 header)
3042                                              (if (or (string= (nth 1 header)
3043                                                               "head")
3044                                                      (string= (nth 1 header)
3045                                                               "body"))
3046                                                  ""
3047                                                (list 'gnus-summary-header
3048                                                      (nth 1 header)))
3049                                              (list 'quote (nth 1 (car ts)))
3050                                              (list 'gnus-score-delta-default
3051                                                    nil)
3052                                              (nth 1 (car ps))
3053                                              t)
3054                                             t)
3055                                            outp))
3056                                     (setq ps (cdr ps)))
3057                                   (list (nreverse outp))))
3058                                outt))
3059                         (setq ts (cdr ts)))
3060                       (list (nreverse outt))))
3061                    outh))
3062             (setq headers (cdr headers)))
3063           (list (nreverse outh))))))))
3064
3065
3066 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3067 (defvar bookmark-make-record-function)
3068 \f
3069
3070 (defun gnus-summary-mode (&optional group)
3071   "Major mode for reading articles.
3072
3073 All normal editing commands are switched off.
3074 \\<gnus-summary-mode-map>
3075 Each line in this buffer represents one article.  To read an
3076 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3077 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3078 respectively.
3079
3080 You can also post articles and send mail from this buffer.  To
3081 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3082 of an article, type `\\[gnus-summary-reply]'.
3083
3084 There are approx. one gazillion commands you can execute in this
3085 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3086
3087 The following commands are available:
3088
3089 \\{gnus-summary-mode-map}"
3090   (interactive)
3091   (kill-all-local-variables)
3092   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3093     (gnus-summary-make-local-variables))
3094   (gnus-summary-make-local-variables)
3095   (setq gnus-newsgroup-name group)
3096   (when (gnus-visual-p 'summary-menu 'menu)
3097     (gnus-summary-make-menu-bar)
3098     (gnus-summary-make-tool-bar))
3099   (gnus-make-thread-indent-array)
3100   (gnus-simplify-mode-line)
3101   (setq major-mode 'gnus-summary-mode)
3102   (setq mode-name "Summary")
3103   (use-local-map gnus-summary-mode-map)
3104   (buffer-disable-undo)
3105   (setq buffer-read-only t              ;Disable modification
3106         show-trailing-whitespace nil)
3107   (setq truncate-lines t)
3108   (add-to-invisibility-spec '(gnus-sum . t))
3109   (gnus-summary-set-display-table)
3110   (gnus-set-default-directory)
3111   (make-local-variable 'gnus-summary-line-format)
3112   (make-local-variable 'gnus-summary-line-format-spec)
3113   (make-local-variable 'gnus-summary-dummy-line-format)
3114   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3115   (make-local-variable 'gnus-summary-mark-positions)
3116   (gnus-make-local-hook 'pre-command-hook)
3117   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3118   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3119   (turn-on-gnus-mailing-list-mode)
3120   (mm-enable-multibyte)
3121   (set (make-local-variable 'bookmark-make-record-function)
3122        'gnus-summary-bookmark-make-record)
3123   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3124   (gnus-update-summary-mark-positions))
3125
3126 (defun gnus-summary-make-local-variables ()
3127   "Make all the local summary buffer variables."
3128   (let (global)
3129     (dolist (local gnus-summary-local-variables)
3130       (if (consp local)
3131           (progn
3132             (if (eq (cdr local) 'global)
3133                 ;; Copy the global value of the variable.
3134                 (setq global (symbol-value (car local)))
3135               ;; Use the value from the list.
3136               (setq global (eval (cdr local))))
3137             (set (make-local-variable (car local)) global))
3138         ;; Simple nil-valued local variable.
3139         (set (make-local-variable local) nil)))))
3140
3141 ;; Summary data functions.
3142
3143 (defmacro gnus-data-number (data)
3144   `(car ,data))
3145
3146 (defmacro gnus-data-set-number (data number)
3147   `(setcar ,data ,number))
3148
3149 (defmacro gnus-data-mark (data)
3150   `(nth 1 ,data))
3151
3152 (defmacro gnus-data-set-mark (data mark)
3153   `(setcar (nthcdr 1 ,data) ,mark))
3154
3155 (defmacro gnus-data-pos (data)
3156   `(nth 2 ,data))
3157
3158 (defmacro gnus-data-set-pos (data pos)
3159   `(setcar (nthcdr 2 ,data) ,pos))
3160
3161 (defmacro gnus-data-header (data)
3162   `(nth 3 ,data))
3163
3164 (defmacro gnus-data-set-header (data header)
3165   `(setf (nth 3 ,data) ,header))
3166
3167 (defmacro gnus-data-level (data)
3168   `(nth 4 ,data))
3169
3170 (defmacro gnus-data-unread-p (data)
3171   `(= (nth 1 ,data) gnus-unread-mark))
3172
3173 (defmacro gnus-data-read-p (data)
3174   `(/= (nth 1 ,data) gnus-unread-mark))
3175
3176 (defmacro gnus-data-pseudo-p (data)
3177   `(consp (nth 3 ,data)))
3178
3179 (defmacro gnus-data-find (number)
3180   `(assq ,number gnus-newsgroup-data))
3181
3182 (defmacro gnus-data-find-list (number &optional data)
3183   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3184      (memq (assq ,number bdata)
3185            bdata)))
3186
3187 (defmacro gnus-data-make (number mark pos header level)
3188   `(list ,number ,mark ,pos ,header ,level))
3189
3190 (defun gnus-data-enter (after-article number mark pos header level offset)
3191   (let ((data (gnus-data-find-list after-article)))
3192     (unless data
3193       (error "No such article: %d" after-article))
3194     (setcdr data (cons (gnus-data-make number mark pos header level)
3195                        (cdr data)))
3196     (setq gnus-newsgroup-data-reverse nil)
3197     (gnus-data-update-list (cddr data) offset)))
3198
3199 (defun gnus-data-enter-list (after-article list &optional offset)
3200   (when list
3201     (let ((data (and after-article (gnus-data-find-list after-article)))
3202           (ilist list))
3203       (if (not (or data
3204                    after-article))
3205           (let ((odata gnus-newsgroup-data))
3206             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3207             (when offset
3208               (gnus-data-update-list odata offset)))
3209         ;; Find the last element in the list to be spliced into the main
3210         ;; list.
3211         (setq list (last list))
3212         (if (not data)
3213             (progn
3214               (setcdr list gnus-newsgroup-data)
3215               (setq gnus-newsgroup-data ilist)
3216               (when offset
3217                 (gnus-data-update-list (cdr list) offset)))
3218           (setcdr list (cdr data))
3219           (setcdr data ilist)
3220           (when offset
3221             (gnus-data-update-list (cdr list) offset))))
3222       (setq gnus-newsgroup-data-reverse nil))))
3223
3224 (defun gnus-data-remove (article &optional offset)
3225   (let ((data gnus-newsgroup-data))
3226     (if (= (gnus-data-number (car data)) article)
3227         (progn
3228           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3229                 gnus-newsgroup-data-reverse nil)
3230           (when offset
3231             (gnus-data-update-list gnus-newsgroup-data offset)))
3232       (while (cdr data)
3233         (when (= (gnus-data-number (cadr data)) article)
3234           (setcdr data (cddr data))
3235           (when offset
3236             (gnus-data-update-list (cdr data) offset))
3237           (setq data nil
3238                 gnus-newsgroup-data-reverse nil))
3239         (setq data (cdr data))))))
3240
3241 (defmacro gnus-data-list (backward)
3242   `(if ,backward
3243        (or gnus-newsgroup-data-reverse
3244            (setq gnus-newsgroup-data-reverse
3245                  (reverse gnus-newsgroup-data)))
3246      gnus-newsgroup-data))
3247
3248 (defun gnus-data-update-list (data offset)
3249   "Add OFFSET to the POS of all data entries in DATA."
3250   (setq gnus-newsgroup-data-reverse nil)
3251   (while data
3252     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3253     (setq data (cdr data))))
3254
3255 (defun gnus-summary-article-pseudo-p (article)
3256   "Say whether this article is a pseudo article or not."
3257   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3258
3259 (defmacro gnus-summary-article-sparse-p (article)
3260   "Say whether this article is a sparse article or not."
3261   `(memq ,article gnus-newsgroup-sparse))
3262
3263 (defmacro gnus-summary-article-ancient-p (article)
3264   "Say whether this article is a sparse article or not."
3265   `(memq ,article gnus-newsgroup-ancient))
3266
3267 (defun gnus-article-parent-p (number)
3268   "Say whether this article is a parent or not."
3269   (let ((data (gnus-data-find-list number)))
3270     (and (cdr data)              ; There has to be an article after...
3271          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3272             (gnus-data-level (nth 1 data))))))
3273
3274 (defun gnus-article-children (number)
3275   "Return a list of all children to NUMBER."
3276   (let* ((data (gnus-data-find-list number))
3277          (level (gnus-data-level (car data)))
3278          children)
3279     (setq data (cdr data))
3280     (while (and data
3281                 (= (gnus-data-level (car data)) (1+ level)))
3282       (push (gnus-data-number (car data)) children)
3283       (setq data (cdr data)))
3284     children))
3285
3286 (defmacro gnus-summary-skip-intangible ()
3287   "If the current article is intangible, then jump to a different article."
3288   '(let ((to (get-text-property (point) 'gnus-intangible)))
3289      (and to (gnus-summary-goto-subject to))))
3290
3291 (defmacro gnus-summary-article-intangible-p ()
3292   "Say whether this article is intangible or not."
3293   '(get-text-property (point) 'gnus-intangible))
3294
3295 (defun gnus-article-read-p (article)
3296   "Say whether ARTICLE is read or not."
3297   (not (or (memq article gnus-newsgroup-marked)
3298            (memq article gnus-newsgroup-spam-marked)
3299            (memq article gnus-newsgroup-unreads)
3300            (memq article gnus-newsgroup-unselected)
3301            (memq article gnus-newsgroup-dormant))))
3302
3303 ;; Some summary mode macros.
3304
3305 (defmacro gnus-summary-article-number ()
3306   "The article number of the article on the current line.
3307 If there isn't an article number here, then we return the current
3308 article number."
3309   '(progn
3310      (gnus-summary-skip-intangible)
3311      (or (get-text-property (point) 'gnus-number)
3312          (gnus-summary-last-subject))))
3313
3314 (defmacro gnus-summary-article-header (&optional number)
3315   "Return the header of article NUMBER."
3316   `(gnus-data-header (gnus-data-find
3317                       ,(or number '(gnus-summary-article-number)))))
3318
3319 (defmacro gnus-summary-thread-level (&optional number)
3320   "Return the level of thread that starts with article NUMBER."
3321   `(if (and (eq gnus-summary-make-false-root 'dummy)
3322             (get-text-property (point) 'gnus-intangible))
3323        0
3324      (gnus-data-level (gnus-data-find
3325                        ,(or number '(gnus-summary-article-number))))))
3326
3327 (defmacro gnus-summary-article-mark (&optional number)
3328   "Return the mark of article NUMBER."
3329   `(gnus-data-mark (gnus-data-find
3330                     ,(or number '(gnus-summary-article-number)))))
3331
3332 (defmacro gnus-summary-article-pos (&optional number)
3333   "Return the position of the line of article NUMBER."
3334   `(gnus-data-pos (gnus-data-find
3335                    ,(or number '(gnus-summary-article-number)))))
3336
3337 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3338 (defmacro gnus-summary-article-subject (&optional number)
3339   "Return current subject string or nil if nothing."
3340   `(let ((headers
3341           ,(if number
3342                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3343              '(gnus-data-header (assq (gnus-summary-article-number)
3344                                       gnus-newsgroup-data)))))
3345      (and headers
3346           (vectorp headers)
3347           (mail-header-subject headers))))
3348
3349 (defmacro gnus-summary-article-score (&optional number)
3350   "Return current article score."
3351   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3352                   gnus-newsgroup-scored))
3353        gnus-summary-default-score 0))
3354
3355 (defun gnus-summary-article-children (&optional number)
3356   "Return a list of article numbers that are children of article NUMBER."
3357   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3358          (level (gnus-data-level (car data)))
3359          l children)
3360     (while (and (setq data (cdr data))
3361                 (> (setq l (gnus-data-level (car data))) level))
3362       (and (= (1+ level) l)
3363            (push (gnus-data-number (car data))
3364                  children)))
3365     (nreverse children)))
3366
3367 (defun gnus-summary-article-parent (&optional number)
3368   "Return the article number of the parent of article NUMBER."
3369   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3370                                     (gnus-data-list t)))
3371          (level (gnus-data-level (car data))))
3372     (if (zerop level)
3373         ()                              ; This is a root.
3374       ;; We search until we find an article with a level less than
3375       ;; this one.  That function has to be the parent.
3376       (while (and (setq data (cdr data))
3377                   (not (< (gnus-data-level (car data)) level))))
3378       (and data (gnus-data-number (car data))))))
3379
3380 (defun gnus-unread-mark-p (mark)
3381   "Say whether MARK is the unread mark."
3382   (= mark gnus-unread-mark))
3383
3384 (defun gnus-read-mark-p (mark)
3385   "Say whether MARK is one of the marks that mark as read.
3386 This is all marks except unread, ticked, dormant, and expirable."
3387   (not (or (= mark gnus-unread-mark)
3388            (= mark gnus-ticked-mark)
3389            (= mark gnus-spam-mark)
3390            (= mark gnus-dormant-mark)
3391            (= mark gnus-expirable-mark))))
3392
3393 (defmacro gnus-article-mark (number)
3394   "Return the MARK of article NUMBER.
3395 This macro should only be used when computing the mark the \"first\"
3396 time; i.e., when generating the summary lines.  After that,
3397 `gnus-summary-article-mark' should be used to examine the
3398 marks of articles."
3399   `(cond
3400     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3401     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3402     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3403     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3404     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3405     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3406     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3407     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3408            gnus-ancient-mark))))
3409
3410 ;; Saving hidden threads.
3411
3412 (defmacro gnus-save-hidden-threads (&rest forms)
3413   "Save hidden threads, eval FORMS, and restore the hidden threads."
3414   (let ((config (make-symbol "config")))
3415     `(let ((,config (gnus-hidden-threads-configuration)))
3416        (unwind-protect
3417            (save-excursion
3418              ,@forms)
3419          (gnus-restore-hidden-threads-configuration ,config)))))
3420 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3421 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3422
3423 (defun gnus-data-compute-positions ()
3424   "Compute the positions of all articles."
3425   (setq gnus-newsgroup-data-reverse nil)
3426   (let ((data gnus-newsgroup-data))
3427     (save-excursion
3428       (gnus-save-hidden-threads
3429         (gnus-summary-show-all-threads)
3430         (goto-char (point-min))
3431         (while data
3432           (while (get-text-property (point) 'gnus-intangible)
3433             (forward-line 1))
3434           (gnus-data-set-pos (car data) (+ (point) 3))
3435           (setq data (cdr data))
3436           (forward-line 1))))))
3437
3438 (defun gnus-hidden-threads-configuration ()
3439   "Return the current hidden threads configuration."
3440   (save-excursion
3441     (let (config)
3442       (goto-char (point-min))
3443       (while (not (eobp))
3444         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3445           (push (save-excursion (forward-line 0) (point)) config))
3446         (forward-line 1))
3447       config)))
3448
3449 (defun gnus-restore-hidden-threads-configuration (config)
3450   "Restore hidden threads configuration from CONFIG."
3451   (save-excursion
3452     (let (point (inhibit-read-only t))
3453       (while (setq point (pop config))
3454         (goto-char point)
3455         (gnus-summary-hide-thread)))))
3456
3457 ;; Various summary mode internalish functions.
3458
3459 (defun gnus-mouse-pick-article (e)
3460   (interactive "e")
3461   (mouse-set-point e)
3462   (gnus-summary-next-page nil t))
3463
3464 (defun gnus-summary-set-display-table ()
3465   "Change the display table.
3466 Odd characters have a tendency to mess
3467 up nicely formatted displays - we make all possible glyphs
3468 display only a single character."
3469
3470   ;; We start from the standard display table, if any.
3471   (let ((table (or (copy-sequence standard-display-table)
3472                    (make-display-table)))
3473         (i 32))
3474     ;; Nix out all the control chars...
3475     (while (>= (setq i (1- i)) 0)
3476       (gnus-put-display-table i [??] table))
3477    ;; ... but not newline and cr, of course.  (cr is necessary for the
3478     ;; selective display).
3479     (gnus-put-display-table ?\n nil table)
3480     (gnus-put-display-table ?\r nil table)
3481     ;; We keep TAB as well.
3482     (gnus-put-display-table ?\t nil table)
3483     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3484     ;; Emacs 23 (unicode), that are not set already.
3485     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3486                  160
3487                256)))
3488       (while (>= (setq i (1- i)) 127)
3489         ;; Only modify if the entry is nil.
3490         (unless (gnus-get-display-table i table)
3491           (gnus-put-display-table i [??] table))))
3492     (setq buffer-display-table table)))
3493
3494 (defun gnus-summary-set-article-display-arrow (pos)
3495   "Update the overlay arrow to point to line at position POS."
3496   (when gnus-summary-display-arrow
3497     (make-local-variable 'overlay-arrow-position)
3498     (make-local-variable 'overlay-arrow-string)
3499     (save-excursion
3500       (goto-char pos)
3501       (beginning-of-line)
3502       (unless overlay-arrow-position
3503         (setq overlay-arrow-position (make-marker)))
3504       (setq overlay-arrow-string "=>"
3505             overlay-arrow-position (set-marker overlay-arrow-position
3506                                                (point)
3507                                                (current-buffer))))))
3508
3509 (defun gnus-summary-setup-buffer (group)
3510   "Initialize summary buffer."
3511   (let ((buffer (gnus-summary-buffer-name group))
3512         (dead-name (concat "*Dead Summary "
3513                            (gnus-group-decoded-name group) "*")))
3514     ;; If a dead summary buffer exists, we kill it.
3515     (when (gnus-buffer-live-p dead-name)
3516       (gnus-kill-buffer dead-name))
3517     (if (get-buffer buffer)
3518         (progn
3519           (set-buffer buffer)
3520           (setq gnus-summary-buffer (current-buffer))
3521           (not gnus-newsgroup-prepared))
3522       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3523       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3524       (gnus-summary-mode group)
3525       (when (gnus-group-quit-config group)
3526         (set (make-local-variable 'gnus-single-article-buffer) nil))
3527       (make-local-variable 'gnus-article-buffer)
3528       (make-local-variable 'gnus-article-current)
3529       (make-local-variable 'gnus-original-article-buffer)
3530       (setq gnus-newsgroup-name group)
3531       ;; Set any local variables in the group parameters.
3532       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3533       t)))
3534
3535 (defun gnus-set-global-variables ()
3536   "Set the global equivalents of the buffer-local variables.
3537 They are set to the latest values they had.  These reflect the summary
3538 buffer that was in action when the last article was fetched."
3539   (when (eq major-mode 'gnus-summary-mode)
3540     (setq gnus-summary-buffer (current-buffer))
3541     (let ((name gnus-newsgroup-name)
3542           (marked gnus-newsgroup-marked)
3543           (spam gnus-newsgroup-spam-marked)
3544           (unread gnus-newsgroup-unreads)
3545           (headers gnus-current-headers)
3546           (data gnus-newsgroup-data)
3547           (summary gnus-summary-buffer)
3548           (article-buffer gnus-article-buffer)
3549           (original gnus-original-article-buffer)
3550           (gac gnus-article-current)
3551           (reffed gnus-reffed-article-number)
3552           (score-file gnus-current-score-file)
3553           (default-charset gnus-newsgroup-charset)
3554           vlist)
3555       (let ((locals gnus-newsgroup-variables))
3556         (while locals
3557           (if (consp (car locals))
3558               (push (eval (caar locals)) vlist)
3559             (push (eval (car locals)) vlist))
3560           (setq locals (cdr locals)))
3561         (setq vlist (nreverse vlist)))
3562       (with-current-buffer gnus-group-buffer
3563         (setq gnus-newsgroup-name name
3564               gnus-newsgroup-marked marked
3565               gnus-newsgroup-spam-marked spam
3566               gnus-newsgroup-unreads unread
3567               gnus-current-headers headers
3568               gnus-newsgroup-data data
3569               gnus-article-current gac
3570               gnus-summary-buffer summary
3571               gnus-article-buffer article-buffer
3572               gnus-original-article-buffer original
3573               gnus-reffed-article-number reffed
3574               gnus-current-score-file score-file
3575               gnus-newsgroup-charset default-charset)
3576         (let ((locals gnus-newsgroup-variables))
3577           (while locals
3578             (if (consp (car locals))
3579                 (set (caar locals) (pop vlist))
3580               (set (car locals) (pop vlist)))
3581             (setq locals (cdr locals))))
3582         ;; The article buffer also has local variables.
3583         (when (gnus-buffer-live-p gnus-article-buffer)
3584           (set-buffer gnus-article-buffer)
3585           (setq gnus-summary-buffer summary))))))
3586
3587 (defun gnus-summary-article-unread-p (article)
3588   "Say whether ARTICLE is unread or not."
3589   (memq article gnus-newsgroup-unreads))
3590
3591 (defun gnus-summary-first-article-p (&optional article)
3592   "Return whether ARTICLE is the first article in the buffer."
3593   (if (not (setq article (or article (gnus-summary-article-number))))
3594       nil
3595     (eq article (caar gnus-newsgroup-data))))
3596
3597 (defun gnus-summary-last-article-p (&optional article)
3598   "Return whether ARTICLE is the last article in the buffer."
3599   (if (not (setq article (or article (gnus-summary-article-number))))
3600       ;; All non-existent numbers are the last article.  :-)
3601       t
3602     (not (cdr (gnus-data-find-list article)))))
3603
3604 (defun gnus-make-thread-indent-array (&optional n)
3605   (when (or n
3606             (progn (setq n 200) nil)
3607             (null gnus-thread-indent-array)
3608             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3609     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3610           gnus-thread-indent-array-level gnus-thread-indent-level)
3611     (while (>= n 0)
3612       (aset gnus-thread-indent-array n
3613             (make-string (* n gnus-thread-indent-level) ? ))
3614       (setq n (1- n)))))
3615
3616 (defun gnus-update-summary-mark-positions ()
3617   "Compute where the summary marks are to go."
3618   (save-excursion
3619     (when (gnus-buffer-exists-p gnus-summary-buffer)
3620       (set-buffer gnus-summary-buffer))
3621     (let ((spec gnus-summary-line-format-spec)
3622           pos)
3623       (save-excursion
3624         (gnus-set-work-buffer)
3625         (let ((gnus-tmp-unread ?Z)
3626               (gnus-replied-mark ?Z)
3627               (gnus-score-below-mark ?Z)
3628               (gnus-score-over-mark ?Z)
3629               (gnus-undownloaded-mark ?Z)
3630               (gnus-summary-line-format-spec spec)
3631               (gnus-newsgroup-downloadable '(0))
3632               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3633               case-fold-search ignores)
3634           ;; Here, all marks are bound to Z.
3635           (gnus-summary-insert-line header
3636                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3637           (goto-char (point-min))
3638           ;; Memorize the positions of the same characters as dummy marks.
3639           (while (re-search-forward "[A-D]" nil t)
3640             (push (point) ignores))
3641           (erase-buffer)
3642           ;; We use A-D as dummy marks in order to know column positions
3643           ;; where marks should be inserted.
3644           (setq gnus-tmp-unread ?A
3645                 gnus-replied-mark ?B
3646                 gnus-score-below-mark ?C
3647                 gnus-score-over-mark ?C
3648                 gnus-undownloaded-mark ?D)
3649           (gnus-summary-insert-line header
3650                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3651           ;; Ignore characters which aren't dummy marks.
3652           (dolist (p ignores)
3653             (delete-region (goto-char (1- p)) p)
3654             (insert ?Z))
3655           (goto-char (point-min))
3656           (setq pos (list (cons 'unread
3657                                 (and (search-forward "A" nil t)
3658                                      (- (point) (point-min) 1)))))
3659           (goto-char (point-min))
3660           (push (cons 'replied (and (search-forward "B" nil t)
3661                                     (- (point) (point-min) 1)))
3662                 pos)
3663           (goto-char (point-min))
3664           (push (cons 'score (and (search-forward "C" nil t)
3665                                   (- (point) (point-min) 1)))
3666                 pos)
3667           (goto-char (point-min))
3668           (push (cons 'download (and (search-forward "D" nil t)
3669                                      (- (point) (point-min) 1)))
3670                 pos)))
3671       (setq gnus-summary-mark-positions pos))))
3672
3673 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3674   "Insert a dummy root in the summary buffer."
3675   (beginning-of-line)
3676   (gnus-add-text-properties
3677    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3678    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3679
3680 (defun gnus-summary-extract-address-component (from)
3681   (or (car (funcall gnus-extract-address-components from))
3682       from))
3683
3684 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3685   (let ((mail-parse-charset gnus-newsgroup-charset)
3686         (ignored-from-addresses (gnus-ignored-from-addresses))
3687         ; Is it really necessary to do this next part for each summary line?
3688         ; Luckily, doesn't seem to slow things down much.
3689         (mail-parse-ignored-charsets
3690          (with-current-buffer gnus-summary-buffer
3691            gnus-newsgroup-ignored-charsets)))
3692     (or
3693      (and ignored-from-addresses
3694           (string-match ignored-from-addresses gnus-tmp-from)
3695           (let ((extra-headers (mail-header-extra header))
3696                 to
3697                 newsgroups)
3698             (cond
3699              ((setq to (cdr (assq 'To extra-headers)))
3700               (concat gnus-summary-to-prefix
3701                       (inline
3702                         (gnus-summary-extract-address-component
3703                          (funcall gnus-decode-encoded-address-function to)))))
3704              ((setq newsgroups
3705                     (or
3706                      (cdr (assq 'Newsgroups extra-headers))
3707                      (and
3708                       (memq 'Newsgroups gnus-extra-headers)
3709                       (eq (car (gnus-find-method-for-group
3710                                 gnus-newsgroup-name)) 'nntp)
3711                       (gnus-group-real-name gnus-newsgroup-name))))
3712               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3713      (gnus-string-mark-left-to-right
3714       (inline
3715        (gnus-summary-extract-address-component gnus-tmp-from))))))
3716
3717 (defun gnus-summary-insert-line (gnus-tmp-header
3718                                  gnus-tmp-level gnus-tmp-current
3719                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3720                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3721                                  &optional gnus-tmp-dummy gnus-tmp-score
3722                                  gnus-tmp-process)
3723   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3724       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3725                                           gnus-tmp-level)))
3726   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3727          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3728          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3729          (gnus-tmp-score-char
3730           (if (or (null gnus-summary-default-score)
3731                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3732                       gnus-summary-zcore-fuzz))
3733               ?                         ;Whitespace
3734             (if (< gnus-tmp-score gnus-summary-default-score)
3735                 gnus-score-below-mark gnus-score-over-mark)))
3736          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3737          (gnus-tmp-replied
3738           (cond (gnus-tmp-process gnus-process-mark)
3739                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3740                  gnus-cached-mark)
3741                 (gnus-tmp-replied gnus-replied-mark)
3742                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3743                  gnus-forwarded-mark)
3744                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3745                  gnus-saved-mark)
3746                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3747                  gnus-recent-mark)
3748                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3749                  gnus-unseen-mark)
3750                 (t gnus-no-mark)))
3751          (gnus-tmp-downloaded
3752           (cond (undownloaded
3753                  gnus-undownloaded-mark)
3754                 (gnus-newsgroup-agentized
3755                  gnus-downloaded-mark)
3756                 (t
3757                  gnus-no-mark)))
3758          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3759          (gnus-tmp-name
3760           (cond
3761            ((string-match "<[^>]+> *$" gnus-tmp-from)
3762             (let ((beg (match-beginning 0)))
3763               (or (and (string-match "^\".+\"" gnus-tmp-from)
3764                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3765                   (substring gnus-tmp-from 0 beg))))
3766            ((string-match "(.+)" gnus-tmp-from)
3767             (substring gnus-tmp-from
3768                        (1+ (match-beginning 0)) (1- (match-end 0))))
3769            (t gnus-tmp-from)))
3770          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3771          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3772          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3773          (inhibit-read-only t))
3774     (when (string= gnus-tmp-name "")
3775       (setq gnus-tmp-name gnus-tmp-from))
3776     (unless (numberp gnus-tmp-lines)
3777       (setq gnus-tmp-lines -1))
3778     (if (= gnus-tmp-lines -1)
3779         (setq gnus-tmp-lines "?")
3780       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3781     (condition-case ()
3782         (gnus-put-text-property
3783          (point)
3784          (progn (eval gnus-summary-line-format-spec) (point))
3785          'gnus-number gnus-tmp-number)
3786       (error (gnus-message 5 "Error updating the summary line")))
3787     (when (gnus-visual-p 'summary-highlight 'highlight)
3788       (forward-line -1)
3789       (gnus-summary-highlight-line)
3790       (gnus-run-hooks 'gnus-summary-update-hook)
3791       (forward-line 1))))
3792
3793 (defun gnus-summary-update-line (&optional dont-update)
3794   "Update summary line after change."
3795   (when (and gnus-summary-default-score
3796              (not gnus-summary-inhibit-highlight))
3797     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3798            (article (gnus-summary-article-number))
3799            (score (gnus-summary-article-score article)))
3800       (unless dont-update
3801         (if (and gnus-summary-mark-below
3802                  (< (gnus-summary-article-score)
3803                     gnus-summary-mark-below))
3804             ;; This article has a low score, so we mark it as read.
3805             (when (memq article gnus-newsgroup-unreads)
3806               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3807           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3808             ;; This article was previously marked as read on account
3809             ;; of a low score, but now it has risen, so we mark it as
3810             ;; unread.
3811             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3812         (gnus-summary-update-mark
3813          (if (or (null gnus-summary-default-score)
3814                  (<= (abs (- score gnus-summary-default-score))
3815                      gnus-summary-zcore-fuzz))
3816              ?                          ;Whitespace
3817            (if (< score gnus-summary-default-score)
3818                gnus-score-below-mark gnus-score-over-mark))
3819          'score))
3820       ;; Do visual highlighting.
3821       (when (gnus-visual-p 'summary-highlight 'highlight)
3822         (gnus-summary-highlight-line)
3823         (gnus-run-hooks 'gnus-summary-update-hook)))))
3824
3825 (defvar gnus-tmp-new-adopts nil)
3826
3827 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3828   "Return the number of articles in THREAD.
3829 This may be 0 in some cases -- if none of the articles in
3830 the thread are to be displayed."
3831   (let* ((number
3832          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3833           (cond
3834            ((not (listp thread))
3835             1)
3836            ((and (consp thread) (cdr thread))
3837             (apply
3838              '+ 1 (mapcar
3839                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3840            ((null thread)
3841             1)
3842            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3843             1)
3844            (t 0))))
3845     (when (and level (zerop level) gnus-tmp-new-adopts)
3846       (incf number
3847             (apply '+ (mapcar
3848                        'gnus-summary-number-of-articles-in-thread
3849                        gnus-tmp-new-adopts))))
3850     (if char
3851         (if (> number 1) gnus-not-empty-thread-mark
3852           gnus-empty-thread-mark)
3853       number)))
3854
3855 (defsubst gnus-summary-line-message-size (head)
3856   "Return pretty-printed version of message size.
3857 This function is intended to be used in
3858 `gnus-summary-line-format-alist'."
3859   (let ((c (or (mail-header-chars head) -1)))
3860     (cond ((< c 0) "n/a")               ; chars not available
3861           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3862           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3863           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3864           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3865
3866 (defcustom gnus-user-date-format-alist
3867   '(((gnus-seconds-today) . "Today, %H:%M")
3868     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3869     (604800 . "%A %H:%M")               ; That's one week
3870     ((gnus-seconds-month) . "%A %d")
3871     ((gnus-seconds-year) . "%B %d")
3872     (t . "%b %d %Y"))                   ; This one is used when no other
3873                                         ; does match
3874   "Specifies date format depending on age of article.
3875 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3876 seconds) or a Lisp expression evaluating to a number.  When the age of
3877 the article is less than this number, then use `format-time-string'
3878 with the corresponding FORMAT for displaying the date of the article.
3879 If AGE is not a number or a Lisp expression evaluating to a
3880 non-number, then the corresponding FORMAT is used as a default value.
3881
3882 Note that the list is processed from the beginning, so it should be
3883 sorted by ascending AGE.  Also note that items following the first
3884 non-number AGE will be ignored.
3885
3886 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3887 and `gnus-seconds-year' in the AGE spec.  They return the number of
3888 seconds passed since the start of today, of this month, of this year,
3889 respectively."
3890   :version "24.1"
3891   :group 'gnus-summary-format
3892   :type '(alist :key-type sexp :value-type string))
3893
3894 (defun gnus-user-date (messy-date)
3895   "Format the messy-date according to `gnus-user-date-format-alist'.
3896 Returns \"  ?  \" if there's bad input or if another error occurs.
3897 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3898   (condition-case ()
3899       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3900              (now (gnus-float-time))
3901              ;;If we don't find something suitable we'll use this one
3902              (my-format "%b %d '%y"))
3903         (let* ((difference (- now messy-date))
3904                (templist gnus-user-date-format-alist)
3905                (top (eval (caar templist))))
3906           (while (if (numberp top) (< top difference) (not top))
3907             (progn
3908               (setq templist (cdr templist))
3909               (setq top (eval (caar templist)))))
3910           (if (stringp (cdr (car templist)))
3911               (setq my-format (cdr (car templist)))))
3912         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3913     (error "  ?   ")))
3914
3915 (defun gnus-summary-set-local-parameters (group)
3916   "Go through the local params of GROUP and set all variable specs in that list."
3917   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3918                                         ; really variables.
3919     (dolist (elem (gnus-group-find-parameter group))
3920       (and (consp elem)                 ; Has to be a cons.
3921            (consp (cdr elem))           ; The cdr has to be a list.
3922            (symbolp (car elem))         ; Has to be a symbol in there.
3923            (not (memq (car elem) vars))
3924            (ignore-errors
3925              (push (car elem) vars)
3926              ;; Variables like `gnus-show-threads' that are globally
3927              ;; bound, if used as group parameters, need to get to be
3928              ;; buffer-local, whereas just parameters like `gcc-self',
3929              ;; `timestamp', etc. should not be bound as variables.
3930              (if (boundp (car elem))
3931                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3932                (eval (nth 1 elem))))))))
3933
3934 (defun gnus-summary-read-group (group &optional show-all no-article
3935                                       kill-buffer no-display backward
3936                                       select-articles)
3937   "Start reading news in newsgroup GROUP.
3938 If SHOW-ALL is non-nil, already read articles are also listed.
3939 If NO-ARTICLE is non-nil, no article is selected initially.
3940 If NO-DISPLAY, don't generate a summary buffer."
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 5 "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 (eq major-mode '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 (eq major-mode '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       ;; Save the active value in effect when the group was entered.
4025       (setq gnus-newsgroup-active
4026             (gnus-copy-sequence
4027              (gnus-active gnus-newsgroup-name)))
4028       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4029       ;; You can change the summary buffer in some way with this hook.
4030       (gnus-run-hooks 'gnus-select-group-hook)
4031       (when (memq 'summary (gnus-update-format-specifications
4032                             nil 'summary 'summary-mode 'summary-dummy))
4033         ;; The format specification for the summary line was updated,
4034         ;; so we need to update the mark positions as well.
4035         (gnus-update-summary-mark-positions))
4036       ;; Do score processing.
4037       (when gnus-use-scoring
4038         (gnus-possibly-score-headers))
4039       ;; Check whether to fill in the gaps in the threads.
4040       (when gnus-build-sparse-threads
4041         (gnus-build-sparse-threads))
4042       ;; Find the initial limit.
4043       (if show-all
4044           (let ((gnus-newsgroup-dormant nil))
4045             (gnus-summary-initial-limit show-all))
4046         (gnus-summary-initial-limit show-all))
4047       ;; Generate the summary buffer.
4048       (unless no-display
4049         (gnus-summary-prepare))
4050       (when gnus-use-trees
4051         (gnus-tree-open group)
4052         (setq gnus-summary-highlight-line-function
4053               'gnus-tree-highlight-article))
4054       ;; If the summary buffer is empty, but there are some low-scored
4055       ;; articles or some excluded dormants, we include these in the
4056       ;; buffer.
4057       (when (and (zerop (buffer-size))
4058                  (not no-display))
4059         (cond (gnus-newsgroup-dormant
4060                (gnus-summary-limit-include-dormant))
4061               ((and gnus-newsgroup-scored show-all)
4062                (gnus-summary-limit-include-expunged t))))
4063       ;; Function `gnus-apply-kill-file' must be called in this hook.
4064       (gnus-run-hooks 'gnus-apply-kill-hook)
4065       (if (and (zerop (buffer-size))
4066                (not no-display))
4067           (progn
4068             ;; This newsgroup is empty.
4069             (gnus-summary-catchup-and-exit nil t)
4070             (gnus-message 6 "No unread news")
4071             (when kill-buffer
4072               (gnus-kill-or-deaden-summary kill-buffer))
4073             ;; Return nil from this function.
4074             nil)
4075         ;; Hide conversation thread subtrees.  We cannot do this in
4076         ;; gnus-summary-prepare-hook since kill processing may not
4077         ;; work with hidden articles.
4078         (gnus-summary-maybe-hide-threads)
4079         (gnus-configure-windows 'summary)
4080         (when kill-buffer
4081           (gnus-kill-or-deaden-summary kill-buffer))
4082         (gnus-summary-auto-select-subject)
4083         ;; Show first unread article if requested.
4084         (if (and (not no-article)
4085                  (not no-display)
4086                  gnus-newsgroup-unreads
4087                  gnus-auto-select-first)
4088             (progn
4089               (let ((art (gnus-summary-article-number)))
4090                 (unless (and (not gnus-plugged)
4091                              (or (memq art gnus-newsgroup-undownloaded)
4092                                  (memq art gnus-newsgroup-downloadable)))
4093                   (gnus-summary-goto-article art))))
4094           ;; Don't select any articles.
4095           (gnus-summary-position-point)
4096           (gnus-configure-windows 'summary 'force)
4097           (gnus-set-mode-line 'summary))
4098         (when (and gnus-auto-center-group
4099                    (get-buffer-window gnus-group-buffer t))
4100           ;; Gotta use windows, because recenter does weird stuff if
4101           ;; the current buffer ain't the displayed window.
4102           (let ((owin (selected-window)))
4103             (select-window (get-buffer-window gnus-group-buffer t))
4104             (when (gnus-group-goto-group group)
4105               (recenter))
4106             (select-window owin)))
4107         ;; Mark this buffer as "prepared".
4108         (setq gnus-newsgroup-prepared t)
4109         (gnus-run-hooks 'gnus-summary-prepared-hook)
4110         (unless (gnus-ephemeral-group-p group)
4111           (gnus-group-update-group group nil t))
4112         t)))))
4113
4114 (defun gnus-summary-auto-select-subject ()
4115   "Select the subject line on initial group entry."
4116   (goto-char (point-min))
4117   (cond
4118    ((eq gnus-auto-select-subject 'best)
4119     (gnus-summary-best-unread-subject))
4120    ((eq gnus-auto-select-subject 'unread)
4121     (gnus-summary-first-unread-subject))
4122    ((eq gnus-auto-select-subject 'unseen)
4123     (gnus-summary-first-unseen-subject))
4124    ((eq gnus-auto-select-subject 'unseen-or-unread)
4125     (gnus-summary-first-unseen-or-unread-subject))
4126    ((eq gnus-auto-select-subject 'first)
4127     ;; Do nothing.
4128     )
4129    ((functionp gnus-auto-select-subject)
4130     (funcall gnus-auto-select-subject))))
4131
4132 (defun gnus-summary-prepare ()
4133   "Generate the summary buffer."
4134   (interactive)
4135   (let ((inhibit-read-only t))
4136     (erase-buffer)
4137     (setq gnus-newsgroup-data nil
4138           gnus-newsgroup-data-reverse nil)
4139     (gnus-run-hooks 'gnus-summary-generate-hook)
4140     ;; Generate the buffer, either with threads or without.
4141     (when gnus-newsgroup-headers
4142       (gnus-summary-prepare-threads
4143        (if gnus-show-threads
4144            (gnus-sort-gathered-threads
4145             (funcall gnus-summary-thread-gathering-function
4146                      (gnus-sort-threads
4147                       (gnus-cut-threads (gnus-make-threads)))))
4148          ;; Unthreaded display.
4149          (gnus-sort-articles gnus-newsgroup-headers))))
4150     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4151     ;; Call hooks for modifying summary buffer.
4152     (goto-char (point-min))
4153     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4154
4155 (defsubst gnus-general-simplify-subject (subject)
4156   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4157   (setq subject
4158         (cond
4159          ;; Truncate the subject.
4160          (gnus-simplify-subject-functions
4161           (gnus-map-function gnus-simplify-subject-functions subject))
4162          ((numberp gnus-summary-gather-subject-limit)
4163           (setq subject (gnus-simplify-subject-re subject))
4164           (if (> (length subject) gnus-summary-gather-subject-limit)
4165               (substring subject 0 gnus-summary-gather-subject-limit)
4166             subject))
4167          ;; Fuzzily simplify it.
4168          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4169           (gnus-simplify-subject-fuzzy subject))
4170          ;; Just remove the leading "Re:".
4171          (t
4172           (gnus-simplify-subject-re subject))))
4173
4174   (if (and gnus-summary-gather-exclude-subject
4175            (string-match gnus-summary-gather-exclude-subject subject))
4176       nil                         ; This article shouldn't be gathered
4177     subject))
4178
4179 (defun gnus-summary-simplify-subject-query ()
4180   "Query where the respool algorithm would put this article."
4181   (interactive)
4182   (gnus-summary-select-article)
4183   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4184
4185 (defun gnus-gather-threads-by-subject (threads)
4186   "Gather threads by looking at Subject headers."
4187   (if (not gnus-summary-make-false-root)
4188       threads
4189     (let ((hashtb (gnus-make-hashtable 1024))
4190           (prev threads)
4191           (result threads)
4192           subject hthread whole-subject)
4193       (while threads
4194         (setq subject (gnus-general-simplify-subject
4195                        (setq whole-subject (mail-header-subject
4196                                             (caar threads)))))
4197         (when subject
4198           (if (setq hthread (gnus-gethash subject hashtb))
4199               (progn
4200                 ;; We enter a dummy root into the thread, if we
4201                 ;; haven't done that already.
4202                 (unless (stringp (caar hthread))
4203                   (setcar hthread (list whole-subject (car hthread))))
4204                 ;; We add this new gathered thread to this gathered
4205                 ;; thread.
4206                 (setcdr (car hthread)
4207                         (nconc (cdar hthread) (list (car threads))))
4208                 ;; Remove it from the list of threads.
4209                 (setcdr prev (cdr threads))
4210                 (setq threads prev))
4211             ;; Enter this thread into the hash table.
4212             (gnus-sethash subject
4213                           (if gnus-summary-make-false-root-always
4214                               (progn
4215                                 ;; If you want a dummy root above all
4216                                 ;; threads...
4217                                 (setcar threads (list whole-subject
4218                                                       (car threads)))
4219                                 threads)
4220                             threads)
4221                           hashtb)))
4222         (setq prev threads)
4223         (setq threads (cdr threads)))
4224       result)))
4225
4226 (defun gnus-gather-threads-by-references (threads)
4227   "Gather threads by looking at References headers."
4228   (let ((idhashtb (gnus-make-hashtable 1024))
4229         (thhashtb (gnus-make-hashtable 1024))
4230         (prev threads)
4231         (result threads)
4232         ids references id gthread gid entered ref)
4233     (while threads
4234       (when (setq references (mail-header-references (caar threads)))
4235         (setq id (mail-header-id (caar threads))
4236               ids (inline (gnus-split-references references))
4237               entered nil)
4238         (while (setq ref (pop ids))
4239           (setq ids (delete ref ids))
4240           (if (not (setq gid (gnus-gethash ref idhashtb)))
4241               (progn
4242                 (gnus-sethash ref id idhashtb)
4243                 (gnus-sethash id threads thhashtb))
4244             (setq gthread (gnus-gethash gid thhashtb))
4245             (unless entered
4246               ;; We enter a dummy root into the thread, if we
4247               ;; haven't done that already.
4248               (unless (stringp (caar gthread))
4249                 (setcar gthread (list (mail-header-subject (caar gthread))
4250                                       (car gthread))))
4251               ;; We add this new gathered thread to this gathered
4252               ;; thread.
4253               (setcdr (car gthread)
4254                       (nconc (cdar gthread) (list (car threads)))))
4255             ;; Add it into the thread hash table.
4256             (gnus-sethash id gthread thhashtb)
4257             (setq entered t)
4258             ;; Remove it from the list of threads.
4259             (setcdr prev (cdr threads))
4260             (setq threads prev))))
4261       (setq prev threads)
4262       (setq threads (cdr threads)))
4263     result))
4264
4265 (defun gnus-sort-gathered-threads (threads)
4266   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4267   (let ((result threads))
4268     (while threads
4269       (when (stringp (caar threads))
4270         (setcdr (car threads)
4271                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4272       (setq threads (cdr threads)))
4273     result))
4274
4275 (defun gnus-thread-loop-p (root thread)
4276   "Say whether ROOT is in THREAD."
4277   (let ((stack (list thread))
4278         (infloop 0)
4279         th)
4280     (while (setq thread (pop stack))
4281       (setq th (cdr thread))
4282       (while (and th
4283                   (not (eq (caar th) root)))
4284         (pop th))
4285       (if th
4286           ;; We have found a loop.
4287           (let (ref-dep)
4288             (setcdr thread (delq (car th) (cdr thread)))
4289             (if (boundp (setq ref-dep (intern "none"
4290                                               gnus-newsgroup-dependencies)))
4291                 (setcdr (symbol-value ref-dep)
4292                         (nconc (cdr (symbol-value ref-dep))
4293                                (list (car th))))
4294               (set ref-dep (list nil (car th))))
4295             (setq infloop 1
4296                   stack nil))
4297         ;; Push all the subthreads onto the stack.
4298         (push (cdr thread) stack)))
4299     infloop))
4300
4301 (defun gnus-make-threads ()
4302   "Go through the dependency hashtb and find the roots.  Return all threads."
4303   (let (threads)
4304     (while (catch 'infloop
4305              (mapatoms
4306               (lambda (refs)
4307                 ;; Deal with self-referencing References loops.
4308                 (when (and (car (symbol-value refs))
4309                            (not (zerop
4310                                  (apply
4311                                   '+
4312                                   (mapcar
4313                                    (lambda (thread)
4314                                      (gnus-thread-loop-p
4315                                       (car (symbol-value refs)) thread))
4316                                    (cdr (symbol-value refs)))))))
4317                   (setq threads nil)
4318                   (throw 'infloop t))
4319                 (unless (car (symbol-value refs))
4320                   ;; These threads do not refer back to any other
4321                   ;; articles, so they're roots.
4322                   (setq threads (append (cdr (symbol-value refs)) threads))))
4323               gnus-newsgroup-dependencies)))
4324     threads))
4325
4326 ;; Build the thread tree.
4327 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4328   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4329
4330 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4331 if it was already present.
4332
4333 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4334 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4335 Message-IDs will be renamed to a unique Message-ID before being
4336 entered.
4337
4338 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4339   (let* ((id (mail-header-id header))
4340          (id-dep (and id (intern id dependencies)))
4341          parent-id ref ref-dep ref-header replaced)
4342     ;; Enter this `header' in the `dependencies' table.
4343     (cond
4344      ((not id-dep)
4345       (setq header nil))
4346      ;; The first two cases do the normal part: enter a new `header'
4347      ;; in the `dependencies' table.
4348      ((not (boundp id-dep))
4349       (set id-dep (list header)))
4350      ((null (car (symbol-value id-dep)))
4351       (setcar (symbol-value id-dep) header))
4352
4353      ;; From here the `header' was already present in the
4354      ;; `dependencies' table.
4355      (force-new
4356       ;; Overrides an existing entry;
4357       ;; just set the header part of the entry.
4358       (setcar (symbol-value id-dep) header)
4359       (setq replaced t))
4360
4361      ;; Renames the existing `header' to a unique Message-ID.
4362      ((not gnus-summary-ignore-duplicates)
4363       ;; An article with this Message-ID has already been seen.
4364       ;; We rename the Message-ID.
4365       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4366            (list header))
4367       (mail-header-set-id header id))
4368
4369      ;; The last case ignores an existing entry, except it adds any
4370      ;; additional Xrefs (in case the two articles came from different
4371      ;; servers.
4372      ;; Also sets `header' to `nil' meaning that the `dependencies'
4373      ;; table was *not* modified.
4374      (t
4375       (mail-header-set-xref
4376        (car (symbol-value id-dep))
4377        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4378                    "")
4379                (or (mail-header-xref header) "")))
4380       (setq header nil)))
4381
4382     (when (and header (not replaced))
4383       ;; First check that we are not creating a References loop.
4384       (setq parent-id (gnus-parent-id (mail-header-references header)))
4385       (setq ref parent-id)
4386       (while (and ref
4387                   (setq ref-dep (intern-soft ref dependencies))
4388                   (boundp ref-dep)
4389                   (setq ref-header (car (symbol-value ref-dep))))
4390         (if (string= id ref)
4391             ;; Yuk!  This is a reference loop.  Make the article be a
4392             ;; root article.
4393             (progn
4394               (mail-header-set-references (car (symbol-value id-dep)) "none")
4395               (setq ref nil)
4396               (setq parent-id nil))
4397           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4398       (setq ref-dep (intern (or parent-id "none") dependencies))
4399       (if (boundp ref-dep)
4400           (setcdr (symbol-value ref-dep)
4401                   (nconc (cdr (symbol-value ref-dep))
4402                          (list (symbol-value id-dep))))
4403         (set ref-dep (list nil (symbol-value id-dep)))))
4404     header))
4405
4406 (defun gnus-extract-message-id-from-in-reply-to (string)
4407   (if (string-match "<[^>]+>" string)
4408       (substring string (match-beginning 0) (match-end 0))
4409     nil))
4410
4411 (defun gnus-build-sparse-threads ()
4412   (let ((headers gnus-newsgroup-headers)
4413         (mail-parse-charset gnus-newsgroup-charset)
4414         (gnus-summary-ignore-duplicates t)
4415         header references generation relations
4416         subject child end new-child date)
4417     ;; First we create an alist of generations/relations, where
4418     ;; generations is how much we trust the relation, and the relation
4419     ;; is parent/child.
4420     (gnus-message 7 "Making sparse threads...")
4421     (save-excursion
4422       (nnheader-set-temp-buffer " *gnus sparse threads*")
4423       (while (setq header (pop headers))
4424         (when (and (setq references (mail-header-references header))
4425                    (not (string= references "")))
4426           (insert references)
4427           (setq child (mail-header-id header)
4428                 subject (mail-header-subject header)
4429                 date (mail-header-date header)
4430                 generation 0)
4431           (while (search-backward ">" nil t)
4432             (setq end (1+ (point)))
4433             (when (search-backward "<" nil t)
4434               (setq new-child (buffer-substring (point) end))
4435               (push (list (incf generation)
4436                           child (setq child new-child)
4437                           subject date)
4438                     relations)))
4439           (when child
4440             (push (list (1+ generation) child nil subject) relations))
4441           (erase-buffer)))
4442       (kill-buffer (current-buffer)))
4443     ;; Sort over trustworthiness.
4444     (dolist (relation (sort relations 'car-less-than-car))
4445       (when (gnus-dependencies-add-header
4446              (make-full-mail-header
4447               gnus-reffed-article-number
4448               (nth 3 relation) "" (or (nth 4 relation) "")
4449               (nth 1 relation)
4450               (or (nth 2 relation) "") 0 0 "")
4451              gnus-newsgroup-dependencies nil)
4452         (push gnus-reffed-article-number gnus-newsgroup-limit)
4453         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4454         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4455               gnus-newsgroup-reads)
4456         (decf gnus-reffed-article-number)))
4457     (gnus-message 7 "Making sparse threads...done")))
4458
4459 (defun gnus-build-old-threads ()
4460   ;; Look at all the articles that refer back to old articles, and
4461   ;; fetch the headers for the articles that aren't there.  This will
4462   ;; build complete threads - if the roots haven't been expired by the
4463   ;; server, that is.
4464   (let ((mail-parse-charset gnus-newsgroup-charset)
4465         id heads)
4466     (mapatoms
4467      (lambda (refs)
4468        (when (not (car (symbol-value refs)))
4469          (setq heads (cdr (symbol-value refs)))
4470          (while heads
4471            (if (memq (mail-header-number (caar heads))
4472                      gnus-newsgroup-dormant)
4473                (setq heads (cdr heads))
4474              (setq id (symbol-name refs))
4475              (while (and (setq id (gnus-build-get-header id))
4476                          (not (car (gnus-id-to-thread id)))))
4477              (setq heads nil)))))
4478      gnus-newsgroup-dependencies)))
4479
4480 (defsubst gnus-remove-odd-characters (string)
4481   "Translate STRING into something that doesn't contain weird characters."
4482   (mm-subst-char-in-string
4483    ?\r ?\-
4484    (mm-subst-char-in-string ?\n ?\- string t) t))
4485
4486 ;; This function has to be called with point after the article number
4487 ;; on the beginning of the line.
4488 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4489   (let ((eol (point-at-eol))
4490         (buffer (current-buffer))
4491         header references in-reply-to)
4492
4493     ;; overview: [num subject from date id refs chars lines misc]
4494     (unwind-protect
4495         (let (x)
4496           (narrow-to-region (point) eol)
4497           (unless (eobp)
4498             (forward-char))
4499
4500           (setq header
4501                 (make-full-mail-header
4502                  number                 ; number
4503                  (condition-case ()     ; subject
4504                      (gnus-remove-odd-characters
4505                       (funcall gnus-decode-encoded-word-function
4506                                (setq x (nnheader-nov-field))))
4507                    (error x))
4508                  (condition-case ()     ; from
4509                      (gnus-remove-odd-characters
4510                       (funcall gnus-decode-encoded-address-function
4511                                (setq x (nnheader-nov-field))))
4512                    (error x))
4513                  (nnheader-nov-field)   ; date
4514                  (nnheader-nov-read-message-id number)  ; id
4515                  (setq references (nnheader-nov-field)) ; refs
4516                  (nnheader-nov-read-integer) ; chars
4517                  (nnheader-nov-read-integer) ; lines
4518                  (unless (eobp)
4519                    (if (looking-at "Xref: ")
4520                        (goto-char (match-end 0)))
4521                    (nnheader-nov-field)) ; Xref
4522                  (nnheader-nov-parse-extra)))) ; extra
4523
4524       (widen))
4525
4526     (when (and (string= references "")
4527                (setq in-reply-to (mail-header-extra header))
4528                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4529       (mail-header-set-references
4530        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4531
4532     (when gnus-alter-header-function
4533       (funcall gnus-alter-header-function header))
4534     (gnus-dependencies-add-header header dependencies force-new)))
4535
4536 (defun gnus-build-get-header (id)
4537   "Look through the buffer of NOV lines and find the header to ID.
4538 Enter this line into the dependencies hash table, and return
4539 the id of the parent article (if any)."
4540   (let ((deps gnus-newsgroup-dependencies)
4541         found header)
4542     (prog1
4543         (with-current-buffer nntp-server-buffer
4544           (let ((case-fold-search nil))
4545             (goto-char (point-min))
4546             (while (and (not found)
4547                         (search-forward id nil t))
4548               (beginning-of-line)
4549               (setq found (looking-at
4550                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4551                                    (regexp-quote id))))
4552               (or found (beginning-of-line 2)))
4553             (when found
4554               (beginning-of-line)
4555               (and
4556                (setq header (gnus-nov-parse-line
4557                              (read (current-buffer)) deps))
4558                (gnus-parent-id (mail-header-references header))))))
4559       (when header
4560         (let ((number (mail-header-number header)))
4561           (push number gnus-newsgroup-limit)
4562           (push header gnus-newsgroup-headers)
4563           (if (memq number gnus-newsgroup-unselected)
4564               (progn
4565                 (setq gnus-newsgroup-unreads
4566                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4567                                                number))
4568                 (setq gnus-newsgroup-unselected
4569                       (delq number gnus-newsgroup-unselected)))
4570             (push number gnus-newsgroup-ancient)))))))
4571
4572 (defun gnus-build-all-threads ()
4573   "Read all the headers."
4574   (let ((gnus-summary-ignore-duplicates t)
4575         (mail-parse-charset gnus-newsgroup-charset)
4576         (dependencies gnus-newsgroup-dependencies)
4577         header article)
4578     (with-current-buffer nntp-server-buffer
4579       (let ((case-fold-search nil))
4580         (goto-char (point-min))
4581         (while (not (eobp))
4582           (ignore-errors
4583             (setq article (read (current-buffer))
4584                   header (gnus-nov-parse-line article dependencies t)))
4585           (when header
4586             (with-current-buffer gnus-summary-buffer
4587               (push header gnus-newsgroup-headers)
4588               (if (memq (setq article (mail-header-number header))
4589                         gnus-newsgroup-unselected)
4590                   (progn
4591                     (setq gnus-newsgroup-unreads
4592                           (gnus-add-to-sorted-list
4593                            gnus-newsgroup-unreads article))
4594                     (setq gnus-newsgroup-unselected
4595                           (delq article gnus-newsgroup-unselected)))
4596                 (push article gnus-newsgroup-ancient)))
4597             (forward-line 1)))))))
4598
4599 (defun gnus-summary-update-article-line (article header)
4600   "Update the line for ARTICLE using HEADER."
4601   (let* ((id (mail-header-id header))
4602          (thread (gnus-id-to-thread id)))
4603     (unless thread
4604       (error "Article in no thread"))
4605     ;; Update the thread.
4606     (setcar thread header)
4607     (gnus-summary-goto-subject article)
4608     (let* ((datal (gnus-data-find-list article))
4609            (data (car datal))
4610            (inhibit-read-only t)
4611            (level (gnus-summary-thread-level)))
4612       (gnus-delete-line)
4613       (let ((inserted (- (point)
4614                          (progn
4615                            (gnus-summary-insert-line
4616                             header level nil
4617                             (memq article gnus-newsgroup-undownloaded)
4618                             (gnus-article-mark article)
4619                             (memq article gnus-newsgroup-replied)
4620                             (memq article gnus-newsgroup-expirable)
4621                             ;; Only insert the Subject string when it's different
4622                             ;; from the previous Subject string.
4623                             (if (and
4624                                  gnus-show-threads
4625                                  (gnus-subject-equal
4626                                   (condition-case ()
4627                                       (mail-header-subject
4628                                        (gnus-data-header
4629                                         (cadr
4630                                          (gnus-data-find-list
4631                                           article
4632                                           (gnus-data-list t)))))
4633                                     ;; Error on the side of excessive subjects.
4634                                     (error ""))
4635                                   (mail-header-subject header)))
4636                                 ""
4637                               (mail-header-subject header))
4638                             nil (cdr (assq article gnus-newsgroup-scored))
4639                             (memq article gnus-newsgroup-processable))
4640                            (point)))))
4641         (when (cdr datal)
4642           (gnus-data-update-list
4643            (cdr datal)
4644            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4645
4646 (defun gnus-summary-update-article (article &optional iheader)
4647   "Update ARTICLE in the summary buffer."
4648   (set-buffer gnus-summary-buffer)
4649   (let* ((header (gnus-summary-article-header article))
4650          (id (mail-header-id header))
4651          (data (gnus-data-find article))
4652          (thread (gnus-id-to-thread id))
4653          (references (mail-header-references header))
4654          (parent
4655           (gnus-id-to-thread
4656            (or (gnus-parent-id
4657                 (when (and references
4658                            (not (equal "" references)))
4659                   references))
4660                "none")))
4661          (inhibit-read-only t)
4662          (old (car thread)))
4663     (when thread
4664       (unless iheader
4665         (setcar thread nil)
4666         (when parent
4667           (delq thread parent)))
4668       (if (gnus-summary-insert-subject id header)
4669           ;; Set the (possibly) new article number in the data structure.
4670           (gnus-data-set-number data (gnus-id-to-article id))
4671         (setcar thread old)
4672         nil))))
4673
4674 (defun gnus-rebuild-thread (id &optional line)
4675   "Rebuild the thread containing ID.
4676 If LINE, insert the rebuilt thread starting on line LINE."
4677   (let ((inhibit-read-only t)
4678         old-pos current thread data)
4679     (if (not gnus-show-threads)
4680         (setq thread (list (car (gnus-id-to-thread id))))
4681       ;; Get the thread this article is part of.
4682       (setq thread (gnus-remove-thread id)))
4683     (setq old-pos (point-at-bol))
4684     (setq current (save-excursion
4685                     (and (re-search-backward "[\r\n]" nil t)
4686                          (gnus-summary-article-number))))
4687     ;; If this is a gathered thread, we have to go some re-gathering.
4688     (when (stringp (car thread))
4689       (let ((subject (car thread))
4690             roots thr)
4691         (setq thread (cdr thread))
4692         (while thread
4693           (unless (memq (setq thr (gnus-id-to-thread
4694                                    (gnus-root-id
4695                                     (mail-header-id (caar thread)))))
4696                         roots)
4697             (push thr roots))
4698           (setq thread (cdr thread)))
4699         ;; We now have all (unique) roots.
4700         (if (= (length roots) 1)
4701             ;; All the loose roots are now one solid root.
4702             (setq thread (car roots))
4703           (setq thread (cons subject (gnus-sort-threads roots))))))
4704     (let (threads)
4705       ;; We then insert this thread into the summary buffer.
4706       (when line
4707         (goto-char (point-min))
4708         (forward-line (1- line)))
4709       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4710         (if gnus-show-threads
4711             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4712           (gnus-summary-prepare-unthreaded thread))
4713         (setq data (nreverse gnus-newsgroup-data))
4714         (setq threads gnus-newsgroup-threads))
4715       ;; We splice the new data into the data structure.
4716       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4717       ;;!!! then we want to insert at the beginning of the buffer.
4718       ;;!!! That happens to be true with Gnus now, but that may
4719       ;;!!! change in the future.  Perhaps.
4720       (gnus-data-enter-list
4721        (if line nil current) data (- (point) old-pos))
4722       (setq gnus-newsgroup-threads
4723             (nconc threads gnus-newsgroup-threads))
4724       (gnus-data-compute-positions))))
4725
4726 (defun gnus-number-to-header (number)
4727   "Return the header for article NUMBER."
4728   (let ((headers gnus-newsgroup-headers))
4729     (while (and headers
4730                 (not (= number (mail-header-number (car headers)))))
4731       (pop headers))
4732     (when headers
4733       (car headers))))
4734
4735 (defun gnus-parent-headers (in-headers &optional generation)
4736   "Return the headers of the GENERATIONeth parent of HEADERS."
4737   (unless generation
4738     (setq generation 1))
4739   (let ((parent t)
4740         (headers in-headers)
4741         references)
4742     (while (and parent
4743                 (not (zerop generation))
4744                 (setq references (mail-header-references headers)))
4745       (setq headers (if (and references
4746                              (setq parent (gnus-parent-id references)))
4747                         (car (gnus-id-to-thread parent))
4748                       nil))
4749       (decf generation))
4750     (and (not (eq headers in-headers))
4751          headers)))
4752
4753 (defun gnus-id-to-thread (id)
4754   "Return the (sub-)thread where ID appears."
4755   (gnus-gethash id gnus-newsgroup-dependencies))
4756
4757 (defun gnus-id-to-article (id)
4758   "Return the article number of ID."
4759   (let ((thread (gnus-id-to-thread id)))
4760     (when (and thread
4761                (car thread))
4762       (mail-header-number (car thread)))))
4763
4764 (defun gnus-id-to-header (id)
4765   "Return the article headers of ID."
4766   (car (gnus-id-to-thread id)))
4767
4768 (defun gnus-article-displayed-root-p (article)
4769   "Say whether ARTICLE is a root(ish) article."
4770   (let ((level (gnus-summary-thread-level article))
4771         (refs (mail-header-references  (gnus-summary-article-header article)))
4772         particle)
4773     (cond
4774      ((null level) nil)
4775      ((zerop level) t)
4776      ((null refs) t)
4777      ((null (gnus-parent-id refs)) t)
4778      ((and (= 1 level)
4779            (null (setq particle (gnus-id-to-article
4780                                  (gnus-parent-id refs))))
4781            (null (gnus-summary-thread-level particle)))))))
4782
4783 (defun gnus-root-id (id)
4784   "Return the id of the root of the thread where ID appears."
4785   (let (last-id prev)
4786     (while (and id (setq prev (car (gnus-id-to-thread id))))
4787       (setq last-id id
4788             id (gnus-parent-id (mail-header-references prev))))
4789     last-id))
4790
4791 (defun gnus-articles-in-thread (thread)
4792   "Return the list of articles in THREAD."
4793   (cons (mail-header-number (car thread))
4794         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4795
4796 (defun gnus-remove-thread (id &optional dont-remove)
4797   "Remove the thread that has ID in it."
4798   (let (headers thread last-id)
4799     ;; First go up in this thread until we find the root.
4800     (setq last-id (gnus-root-id id)
4801           headers (message-flatten-list (gnus-id-to-thread last-id)))
4802     ;; We have now found the real root of this thread.  It might have
4803     ;; been gathered into some loose thread, so we have to search
4804     ;; through the threads to find the thread we wanted.
4805     (let ((threads gnus-newsgroup-threads)
4806           sub)
4807       (while threads
4808         (setq sub (car threads))
4809         (if (stringp (car sub))
4810             ;; This is a gathered thread, so we look at the roots
4811             ;; below it to find whether this article is in this
4812             ;; gathered root.
4813             (progn
4814               (setq sub (cdr sub))
4815               (while sub
4816                 (when (member (caar sub) headers)
4817                   (setq thread (car threads)
4818                         threads nil
4819                         sub nil))
4820                 (setq sub (cdr sub))))
4821           ;; It's an ordinary thread, so we check it.
4822           (when (eq (car sub) (car headers))
4823             (setq thread sub
4824                   threads nil)))
4825         (setq threads (cdr threads)))
4826       ;; If this article is in no thread, then it's a root.
4827       (if thread
4828           (unless dont-remove
4829             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4830         (setq thread (gnus-id-to-thread last-id)))
4831       (when thread
4832         (prog1
4833             thread                      ; We return this thread.
4834           (unless dont-remove
4835             (if (stringp (car thread))
4836                 (progn
4837                   ;; If we use dummy roots, then we have to remove the
4838                   ;; dummy root as well.
4839                   (when (eq gnus-summary-make-false-root 'dummy)
4840                     ;; We go to the dummy root by going to
4841                     ;; the first sub-"thread", and then one line up.
4842                     (gnus-summary-goto-article
4843                      (mail-header-number (caadr thread)))
4844                     (forward-line -1)
4845                     (gnus-delete-line)
4846                     (gnus-data-compute-positions))
4847                   (setq thread (cdr thread))
4848                   (while thread
4849                     (gnus-remove-thread-1 (car thread))
4850                     (setq thread (cdr thread))))
4851               (gnus-remove-thread-1 thread))))))))
4852
4853 (defun gnus-remove-thread-1 (thread)
4854   "Remove the thread THREAD recursively."
4855   (let ((number (mail-header-number (pop thread)))
4856         d)
4857     (setq thread (reverse thread))
4858     (while thread
4859       (gnus-remove-thread-1 (pop thread)))
4860     (when (setq d (gnus-data-find number))
4861       (goto-char (gnus-data-pos d))
4862       (gnus-summary-show-thread)
4863       (gnus-data-remove
4864        number
4865        (- (point-at-bol)
4866           (prog1
4867               (1+ (point-at-eol))
4868             (gnus-delete-line)))))))
4869
4870 (defun gnus-sort-threads-recursive (threads func)
4871   (sort (mapcar (lambda (thread)
4872                   (cons (car thread)
4873                         (and (cdr thread)
4874                              (gnus-sort-threads-recursive (cdr thread) func))))
4875                 threads) func))
4876
4877 (defun gnus-sort-threads-loop (threads func)
4878   (let* ((superthread (cons nil threads))
4879          (stack (list (cons superthread threads)))
4880          remaining-threads thread)
4881     (while stack
4882       (setq remaining-threads (cdr (car stack)))
4883       (if remaining-threads
4884           (progn (setq thread (car remaining-threads))
4885                  (setcdr (car stack) (cdr remaining-threads))
4886                  (if (cdr thread)
4887                      (push (cons thread (cdr thread)) stack)))
4888         (setq thread (caar stack))
4889         (setcdr thread (sort (cdr thread) func))
4890         (pop stack)))
4891     (cdr superthread)))
4892
4893 (defun gnus-sort-threads (threads)
4894   "Sort THREADS."
4895   (if (not gnus-thread-sort-functions)
4896       threads
4897     (gnus-message 8 "Sorting threads...")
4898     (prog1
4899         (condition-case nil
4900             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4901               (gnus-sort-threads-recursive
4902                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4903           ;; Even after binding max-lisp-eval-depth, the recursive
4904           ;; sorter might fail for very long threads.  In that case,
4905           ;; try using a (less well-tested) non-recursive sorter.
4906           (error (gnus-message 9 "Sorting threads with loop...")
4907                  (gnus-sort-threads-loop
4908                   threads (gnus-make-sort-function
4909                            gnus-thread-sort-functions))))
4910       (gnus-message 8 "Sorting threads...done"))))
4911
4912 (defun gnus-sort-articles (articles)
4913   "Sort ARTICLES."
4914   (when gnus-article-sort-functions
4915     (gnus-message 7 "Sorting articles...")
4916     (prog1
4917         (setq gnus-newsgroup-headers
4918               (sort articles (gnus-make-sort-function
4919                               gnus-article-sort-functions)))
4920       (gnus-message 7 "Sorting articles...done"))))
4921
4922 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4923 (defmacro gnus-thread-header (thread)
4924   "Return header of first article in THREAD.
4925 Note that THREAD must never, ever be anything else than a variable -
4926 using some other form will lead to serious barfage."
4927   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4928   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4929   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4930         (vector thread) 2))
4931
4932 (defsubst gnus-article-sort-by-number (h1 h2)
4933   "Sort articles by article number."
4934   (< (mail-header-number h1)
4935      (mail-header-number h2)))
4936
4937 (defun gnus-thread-sort-by-number (h1 h2)
4938   "Sort threads by root article number."
4939   (gnus-article-sort-by-number
4940    (gnus-thread-header h1) (gnus-thread-header h2)))
4941
4942 (defsubst gnus-article-sort-by-random (h1 h2)
4943   "Sort articles randomly."
4944   (zerop (random 2)))
4945
4946 (defun gnus-thread-sort-by-random (h1 h2)
4947   "Sort threads randomly."
4948   (gnus-article-sort-by-random
4949    (gnus-thread-header h1) (gnus-thread-header h2)))
4950
4951 (defsubst gnus-article-sort-by-lines (h1 h2)
4952   "Sort articles by article Lines header."
4953   (< (mail-header-lines h1)
4954      (mail-header-lines h2)))
4955
4956 (defun gnus-thread-sort-by-lines (h1 h2)
4957   "Sort threads by root article Lines header."
4958   (gnus-article-sort-by-lines
4959    (gnus-thread-header h1) (gnus-thread-header h2)))
4960
4961 (defsubst gnus-article-sort-by-chars (h1 h2)
4962   "Sort articles by octet length."
4963   (< (mail-header-chars h1)
4964      (mail-header-chars h2)))
4965
4966 (defun gnus-thread-sort-by-chars (h1 h2)
4967   "Sort threads by root article octet length."
4968   (gnus-article-sort-by-chars
4969    (gnus-thread-header h1) (gnus-thread-header h2)))
4970
4971 (defsubst gnus-article-sort-by-author (h1 h2)
4972   "Sort articles by root author."
4973   (gnus-string<
4974    (let ((extract (funcall
4975                    gnus-extract-address-components
4976                    (mail-header-from h1))))
4977      (or (car extract) (cadr extract) ""))
4978    (let ((extract (funcall
4979                    gnus-extract-address-components
4980                    (mail-header-from h2))))
4981      (or (car extract) (cadr extract) ""))))
4982
4983 (defun gnus-thread-sort-by-author (h1 h2)
4984   "Sort threads by root author."
4985   (gnus-article-sort-by-author
4986    (gnus-thread-header h1)  (gnus-thread-header h2)))
4987
4988 (defsubst gnus-article-sort-by-recipient (h1 h2)
4989   "Sort articles by recipient."
4990   (gnus-string<
4991    (let ((extract (funcall
4992                    gnus-extract-address-components
4993                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4994      (or (car extract) (cadr extract)))
4995    (let ((extract (funcall
4996                    gnus-extract-address-components
4997                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4998      (or (car extract) (cadr extract)))))
4999
5000 (defun gnus-thread-sort-by-recipient (h1 h2)
5001   "Sort threads by root recipient."
5002   (gnus-article-sort-by-recipient
5003    (gnus-thread-header h1) (gnus-thread-header h2)))
5004
5005 (defsubst gnus-article-sort-by-subject (h1 h2)
5006   "Sort articles by root subject."
5007   (gnus-string<
5008    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5009    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5010
5011 (defun gnus-thread-sort-by-subject (h1 h2)
5012   "Sort threads by root subject."
5013   (gnus-article-sort-by-subject
5014    (gnus-thread-header h1) (gnus-thread-header h2)))
5015
5016 (defsubst gnus-article-sort-by-date (h1 h2)
5017   "Sort articles by root article date."
5018   (time-less-p
5019    (gnus-date-get-time (mail-header-date h1))
5020    (gnus-date-get-time (mail-header-date h2))))
5021
5022 (defun gnus-thread-sort-by-date (h1 h2)
5023   "Sort threads by root article date."
5024   (gnus-article-sort-by-date
5025    (gnus-thread-header h1) (gnus-thread-header h2)))
5026
5027 (defsubst gnus-article-sort-by-score (h1 h2)
5028   "Sort articles by root article score.
5029 Unscored articles will be counted as having a score of zero."
5030   (> (or (cdr (assq (mail-header-number h1)
5031                     gnus-newsgroup-scored))
5032          gnus-summary-default-score 0)
5033      (or (cdr (assq (mail-header-number h2)
5034                     gnus-newsgroup-scored))
5035          gnus-summary-default-score 0)))
5036
5037 (defun gnus-thread-sort-by-score (h1 h2)
5038   "Sort threads by root article score."
5039   (gnus-article-sort-by-score
5040    (gnus-thread-header h1) (gnus-thread-header h2)))
5041
5042 (defun gnus-thread-sort-by-total-score (h1 h2)
5043   "Sort threads by the sum of all scores in the thread.
5044 Unscored articles will be counted as having a score of zero."
5045   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5046
5047 (defun gnus-thread-total-score (thread)
5048   ;; This function find the total score of THREAD.
5049   (cond
5050    ((null thread)
5051     0)
5052    ((consp thread)
5053     (if (stringp (car thread))
5054         (apply gnus-thread-score-function 0
5055                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5056       (gnus-thread-total-score-1 thread)))
5057    (t
5058     (gnus-thread-total-score-1 (list thread)))))
5059
5060 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5061   "Sort articles by number."
5062   (gnus-article-sort-by-number h1 h2))
5063
5064 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5065   "Sort threads such that the thread with the most recently arrived article comes first."
5066   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5067
5068 (defun gnus-thread-highest-number (thread)
5069   "Return the highest article number in THREAD."
5070   (apply 'max (mapcar (lambda (header)
5071                         (mail-header-number header))
5072                       (message-flatten-list thread))))
5073
5074 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5075   "Sort articles by number."
5076   (gnus-article-sort-by-date h1 h2))
5077
5078 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5079   "Sort threads such that the thread with the most recently dated article comes first."
5080   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5081
5082 ; Since this is called not only to sort the top-level threads, but
5083 ; also in recursive sorts to order the articles within a thread, each
5084 ; article will be processed many times.  Thus it speeds things up
5085 ; quite a bit to use gnus-date-get-time, which caches the time value.
5086 (defun gnus-thread-latest-date (thread)
5087   "Return the highest article date in THREAD."
5088   (apply 'max
5089          (mapcar (lambda (header) (gnus-float-time
5090                                    (gnus-date-get-time
5091                                     (mail-header-date header))))
5092                  (message-flatten-list thread))))
5093
5094 (defun gnus-thread-total-score-1 (root)
5095   ;; This function find the total score of the thread below ROOT.
5096   (setq root (car root))
5097   (apply gnus-thread-score-function
5098          (or (append
5099               (mapcar 'gnus-thread-total-score
5100                       (cdr (gnus-id-to-thread (mail-header-id root))))
5101               (when (> (mail-header-number root) 0)
5102                 (list (or (cdr (assq (mail-header-number root)
5103                                      gnus-newsgroup-scored))
5104                           gnus-summary-default-score 0))))
5105              (list gnus-summary-default-score)
5106              '(0))))
5107
5108 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5109 (defvar gnus-tmp-prev-subject nil)
5110 (defvar gnus-tmp-false-parent nil)
5111 (defvar gnus-tmp-root-expunged nil)
5112 (defvar gnus-tmp-dummy-line nil)
5113
5114 (defun gnus-extra-header (type &optional header)
5115   "Return the extra header of TYPE."
5116   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5117       ""))
5118
5119 (defvar gnus-tmp-thread-tree-header-string "")
5120
5121 (defcustom gnus-sum-thread-tree-root "> "
5122   "With %B spec, used for the root of a thread.
5123 If nil, use subject instead."
5124   :version "22.1"
5125   :type '(radio (const :format "%v  " nil) string)
5126   :group 'gnus-thread)
5127
5128 (defcustom gnus-sum-thread-tree-false-root "> "
5129   "With %B spec, used for a false root of a thread.
5130 If nil, use subject instead."
5131   :version "22.1"
5132   :type '(radio (const :format "%v  " nil) string)
5133   :group 'gnus-thread)
5134
5135 (defcustom gnus-sum-thread-tree-single-indent ""
5136   "With %B spec, used for a thread with just one message.
5137 If nil, use subject instead."
5138   :version "22.1"
5139   :type '(radio (const :format "%v  " nil) string)
5140   :group 'gnus-thread)
5141
5142 (defcustom gnus-sum-thread-tree-vertical "| "
5143   "With %B spec, used for drawing a vertical line."
5144   :version "22.1"
5145   :type 'string
5146   :group 'gnus-thread)
5147
5148 (defcustom gnus-sum-thread-tree-indent "  "
5149   "With %B spec, used for indenting."
5150   :version "22.1"
5151   :type 'string
5152   :group 'gnus-thread)
5153
5154 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5155   "With %B spec, used for a leaf with brothers."
5156   :version "22.1"
5157   :type 'string
5158   :group 'gnus-thread)
5159
5160 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5161   "With %B spec, used for a leaf without brothers."
5162   :version "22.1"
5163   :type 'string
5164   :group 'gnus-thread)
5165
5166 (defcustom gnus-summary-display-while-building nil
5167   "If non-nil, show and update the summary buffer as it's being built.
5168 If the value is t, update the buffer after every line is inserted.  If
5169 the value is an integer (N), update the display every N lines."
5170   :version "22.1"
5171   :group 'gnus-thread
5172   :type '(choice (const :tag "off" nil)
5173                  number
5174                  (const :tag "frequently" t)))
5175
5176 (defun gnus-summary-prepare-threads (threads)
5177   "Prepare summary buffer from THREADS and indentation LEVEL.
5178 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5179 or a straight list of headers."
5180   (gnus-message 7 "Generating summary...")
5181
5182   (setq gnus-newsgroup-threads threads)
5183   (beginning-of-line)
5184
5185   (let ((gnus-tmp-level 0)
5186         (default-score (or gnus-summary-default-score 0))
5187         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5188         (building-line-count gnus-summary-display-while-building)
5189         (building-count (integerp gnus-summary-display-while-building))
5190         thread number subject stack state gnus-tmp-gathered beg-match
5191         new-roots gnus-tmp-new-adopts thread-end simp-subject
5192         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5193         gnus-tmp-replied gnus-tmp-subject-or-nil
5194         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5195         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5196         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5197         tree-stack)
5198
5199     (setq gnus-tmp-prev-subject nil
5200           gnus-tmp-thread-tree-header-string "")
5201
5202     (if (vectorp (car threads))
5203         ;; If this is a straight (sic) list of headers, then a
5204         ;; threaded summary display isn't required, so we just create
5205         ;; an unthreaded one.
5206         (gnus-summary-prepare-unthreaded threads)
5207
5208       ;; Do the threaded display.
5209
5210       (if gnus-summary-display-while-building
5211           (switch-to-buffer (buffer-name)))
5212       (while (or threads stack gnus-tmp-new-adopts new-roots)
5213
5214         (if (and (= gnus-tmp-level 0)
5215                  (or (not stack)
5216                      (= (caar stack) 0))
5217                  (not gnus-tmp-false-parent)
5218                  (or gnus-tmp-new-adopts new-roots))
5219             (if gnus-tmp-new-adopts
5220                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5221                       thread (list (car gnus-tmp-new-adopts))
5222                       gnus-tmp-header (caar thread)
5223                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5224               (when new-roots
5225                 (setq thread (list (car new-roots))
5226                       gnus-tmp-header (caar thread)
5227                       new-roots (cdr new-roots))))
5228
5229           (if threads
5230               ;; If there are some threads, we do them before the
5231               ;; threads on the stack.
5232               (setq thread threads
5233                     gnus-tmp-header (caar thread))
5234             ;; There were no current threads, so we pop something off
5235             ;; the stack.
5236             (setq state (car stack)
5237                   gnus-tmp-level (car state)
5238                   tree-stack (cadr state)
5239                   thread (caddr state)
5240                   stack (cdr stack)
5241                   gnus-tmp-header (caar thread))))
5242
5243         (setq gnus-tmp-false-parent nil)
5244         (setq gnus-tmp-root-expunged nil)
5245         (setq thread-end nil)
5246
5247         (if (stringp gnus-tmp-header)
5248             ;; The header is a dummy root.
5249             (cond
5250              ((eq gnus-summary-make-false-root 'adopt)
5251               ;; We let the first article adopt the rest.
5252               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5253                                                (cddar thread)))
5254               (setq gnus-tmp-gathered
5255                     (nconc (mapcar
5256                             (lambda (h) (mail-header-number (car h)))
5257                             (cddar thread))
5258                            gnus-tmp-gathered))
5259               (setq thread (cons (list (caar thread)
5260                                        (cadar thread))
5261                                  (cdr thread)))
5262               (setq gnus-tmp-level -1
5263                     gnus-tmp-false-parent t))
5264              ((eq gnus-summary-make-false-root 'empty)
5265               ;; We print adopted articles with empty subject fields.
5266               (setq gnus-tmp-gathered
5267                     (nconc (mapcar
5268                             (lambda (h) (mail-header-number (car h)))
5269                             (cddar thread))
5270                            gnus-tmp-gathered))
5271               (setq gnus-tmp-level -1))
5272              ((eq gnus-summary-make-false-root 'dummy)
5273               ;; We remember that we probably want to output a dummy
5274               ;; root.
5275               (setq gnus-tmp-dummy-line gnus-tmp-header)
5276               (setq gnus-tmp-prev-subject gnus-tmp-header))
5277              (t
5278               ;; We do not make a root for the gathered
5279               ;; sub-threads at all.
5280               (setq gnus-tmp-level -1)))
5281
5282           (setq number (mail-header-number gnus-tmp-header)
5283                 subject (mail-header-subject gnus-tmp-header)
5284                 simp-subject (gnus-simplify-subject-fully subject))
5285
5286           (cond
5287            ;; If the thread has changed subject, we might want to make
5288            ;; this subthread into a root.
5289            ((and (null gnus-thread-ignore-subject)
5290                  (not (zerop gnus-tmp-level))
5291                  gnus-tmp-prev-subject
5292                  (not (string= gnus-tmp-prev-subject simp-subject)))
5293             (setq new-roots (nconc new-roots (list (car thread)))
5294                   thread-end t
5295                   gnus-tmp-header nil))
5296            ;; If the article lies outside the current limit,
5297            ;; then we do not display it.
5298            ((not (memq number gnus-newsgroup-limit))
5299             (setq gnus-tmp-gathered
5300                   (nconc (mapcar
5301                           (lambda (h) (mail-header-number (car h)))
5302                           (cdar thread))
5303                          gnus-tmp-gathered))
5304             (setq gnus-tmp-new-adopts (if (cdar thread)
5305                                           (append gnus-tmp-new-adopts
5306                                                   (cdar thread))
5307                                         gnus-tmp-new-adopts)
5308                   thread-end t
5309                   gnus-tmp-header nil)
5310             (when (zerop gnus-tmp-level)
5311               (setq gnus-tmp-root-expunged t)))
5312            ;; Perhaps this article is to be marked as read?
5313            ((and gnus-summary-mark-below
5314                  (< (or (cdr (assq number gnus-newsgroup-scored))
5315                         default-score)
5316                     gnus-summary-mark-below)
5317                  ;; Don't touch sparse articles.
5318                  (not (gnus-summary-article-sparse-p number))
5319                  (not (gnus-summary-article-ancient-p number)))
5320             (setq gnus-newsgroup-unreads
5321                   (delq number gnus-newsgroup-unreads))
5322             (if gnus-newsgroup-auto-expire
5323                 (setq gnus-newsgroup-expirable
5324                       (gnus-add-to-sorted-list
5325                        gnus-newsgroup-expirable number))
5326               (push (cons number gnus-low-score-mark)
5327                     gnus-newsgroup-reads))))
5328
5329           (when gnus-tmp-header
5330             ;; We may have an old dummy line to output before this
5331             ;; article.
5332             (when (and gnus-tmp-dummy-line
5333                        (gnus-subject-equal
5334                         gnus-tmp-dummy-line
5335                         (mail-header-subject gnus-tmp-header)))
5336               (gnus-summary-insert-dummy-line
5337                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5338               (setq gnus-tmp-dummy-line nil))
5339
5340             ;; Compute the mark.
5341             (setq gnus-tmp-unread (gnus-article-mark number))
5342
5343             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5344                                   gnus-tmp-header gnus-tmp-level)
5345                   gnus-newsgroup-data)
5346
5347             ;; Actually insert the line.
5348             (setq
5349              gnus-tmp-subject-or-nil
5350              (cond
5351               ((and gnus-thread-ignore-subject
5352                     gnus-tmp-prev-subject
5353                     (not (string= gnus-tmp-prev-subject simp-subject)))
5354                subject)
5355               ((zerop gnus-tmp-level)
5356                (if (and (eq gnus-summary-make-false-root 'empty)
5357                         (memq number gnus-tmp-gathered)
5358                         gnus-tmp-prev-subject
5359                         (string= gnus-tmp-prev-subject simp-subject))
5360                    gnus-summary-same-subject
5361                  subject))
5362               (t gnus-summary-same-subject)))
5363             (if (and (eq gnus-summary-make-false-root 'adopt)
5364                      (= gnus-tmp-level 1)
5365                      (memq number gnus-tmp-gathered))
5366                 (setq gnus-tmp-opening-bracket ?\<
5367                       gnus-tmp-closing-bracket ?\>)
5368               (setq gnus-tmp-opening-bracket ?\[
5369                     gnus-tmp-closing-bracket ?\]))
5370             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5371                 (gnus-make-thread-indent-array
5372                  (max (* 2 (length gnus-thread-indent-array))
5373                       gnus-tmp-level)))
5374             (setq
5375              gnus-tmp-indentation
5376              (aref gnus-thread-indent-array gnus-tmp-level)
5377              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5378              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5379                                 gnus-summary-default-score 0)
5380              gnus-tmp-score-char
5381              (if (or (null gnus-summary-default-score)
5382                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5383                          gnus-summary-zcore-fuzz))
5384                  ?                      ;Whitespace
5385                (if (< gnus-tmp-score gnus-summary-default-score)
5386                    gnus-score-below-mark gnus-score-over-mark))
5387              gnus-tmp-replied
5388              (cond ((memq number gnus-newsgroup-processable)
5389                     gnus-process-mark)
5390                    ((memq number gnus-newsgroup-cached)
5391                     gnus-cached-mark)
5392                    ((memq number gnus-newsgroup-replied)
5393                     gnus-replied-mark)
5394                    ((memq number gnus-newsgroup-forwarded)
5395                     gnus-forwarded-mark)
5396                    ((memq number gnus-newsgroup-saved)
5397                     gnus-saved-mark)
5398                    ((memq number gnus-newsgroup-recent)
5399                     gnus-recent-mark)
5400                    ((memq number gnus-newsgroup-unseen)
5401                     gnus-unseen-mark)
5402                    (t gnus-no-mark))
5403              gnus-tmp-downloaded
5404              (cond ((memq number gnus-newsgroup-undownloaded)
5405                     gnus-undownloaded-mark)
5406                    (gnus-newsgroup-agentized
5407                     gnus-downloaded-mark)
5408                    (t
5409                     gnus-no-mark))
5410              gnus-tmp-from (mail-header-from gnus-tmp-header)
5411              gnus-tmp-name
5412              (cond
5413               ((string-match "<[^>]+> *$" gnus-tmp-from)
5414                (setq beg-match (match-beginning 0))
5415                (or (and (string-match "^\".+\"" gnus-tmp-from)
5416                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5417                    (substring gnus-tmp-from 0 beg-match)))
5418               ((string-match "(.+)" gnus-tmp-from)
5419                (substring gnus-tmp-from
5420                           (1+ (match-beginning 0)) (1- (match-end 0))))
5421               (t gnus-tmp-from))
5422
5423              ;; Do the %B string
5424              gnus-tmp-thread-tree-header-string
5425              (cond
5426               ((not gnus-show-threads) "")
5427               ((zerop gnus-tmp-level)
5428                (cond ((cdar thread)
5429                       (or gnus-sum-thread-tree-root subject))
5430                      (gnus-tmp-new-adopts
5431                       (or gnus-sum-thread-tree-false-root subject))
5432                      (t
5433                       (or gnus-sum-thread-tree-single-indent subject))))
5434               (t
5435                (concat (apply 'concat
5436                               (mapcar (lambda (item)
5437                                         (if (= item 1)
5438                                             gnus-sum-thread-tree-vertical
5439                                           gnus-sum-thread-tree-indent))
5440                                       (cdr (reverse tree-stack))))
5441                        (if (nth 1 thread)
5442                            gnus-sum-thread-tree-leaf-with-other
5443                          gnus-sum-thread-tree-single-leaf)))))
5444             (when (string= gnus-tmp-name "")
5445               (setq gnus-tmp-name gnus-tmp-from))
5446             (unless (numberp gnus-tmp-lines)
5447               (setq gnus-tmp-lines -1))
5448             (if (= gnus-tmp-lines -1)
5449                 (setq gnus-tmp-lines "?")
5450               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5451             (gnus-put-text-property
5452              (point)
5453              (progn (eval gnus-summary-line-format-spec) (point))
5454              'gnus-number number)
5455             (when gnus-visual-p
5456               (forward-line -1)
5457               (gnus-summary-highlight-line)
5458               (when gnus-summary-update-hook
5459                 (gnus-run-hooks 'gnus-summary-update-hook))
5460               (forward-line 1))
5461
5462             (setq gnus-tmp-prev-subject simp-subject)))
5463
5464         (when (nth 1 thread)
5465           (push (list (max 0 gnus-tmp-level)
5466                       (copy-sequence tree-stack)
5467                       (nthcdr 1 thread))
5468                 stack))
5469         (push (if (nth 1 thread) 1 0) tree-stack)
5470         (incf gnus-tmp-level)
5471         (setq threads (if thread-end nil (cdar thread)))
5472         (if gnus-summary-display-while-building
5473             (if building-count
5474                 (progn
5475                   ;; use a set frequency
5476                   (setq building-line-count (1- building-line-count))
5477                   (when (= building-line-count 0)
5478                     (sit-for 0)
5479                     (setq building-line-count
5480                           gnus-summary-display-while-building)))
5481               ;; always
5482               (sit-for 0)))
5483         (unless threads
5484           (setq gnus-tmp-level 0)))))
5485   (gnus-message 7 "Generating summary...done"))
5486
5487 (defun gnus-summary-prepare-unthreaded (headers)
5488   "Generate an unthreaded summary buffer based on HEADERS."
5489   (let (header number mark)
5490
5491     (beginning-of-line)
5492
5493     (while headers
5494       ;; We may have to root out some bad articles...
5495       (when (memq (setq number (mail-header-number
5496                                 (setq header (pop headers))))
5497                   gnus-newsgroup-limit)
5498         ;; Mark article as read when it has a low score.
5499         (when (and gnus-summary-mark-below
5500                    (< (or (cdr (assq number gnus-newsgroup-scored))
5501                           gnus-summary-default-score 0)
5502                       gnus-summary-mark-below)
5503                    (not (gnus-summary-article-ancient-p number)))
5504           (setq gnus-newsgroup-unreads
5505                 (delq number gnus-newsgroup-unreads))
5506           (if gnus-newsgroup-auto-expire
5507               (push number gnus-newsgroup-expirable)
5508             (push (cons number gnus-low-score-mark)
5509                   gnus-newsgroup-reads)))
5510
5511         (setq mark (gnus-article-mark number))
5512         (push (gnus-data-make number mark (1+ (point)) header 0)
5513               gnus-newsgroup-data)
5514         (gnus-summary-insert-line
5515          header 0 number
5516          (memq number gnus-newsgroup-undownloaded)
5517          mark (memq number gnus-newsgroup-replied)
5518          (memq number gnus-newsgroup-expirable)
5519          (mail-header-subject header) nil
5520          (cdr (assq number gnus-newsgroup-scored))
5521          (memq number gnus-newsgroup-processable))))))
5522
5523 (defun gnus-group-get-list-identifiers (group)
5524   "Get list identifier regexp for GROUP."
5525   (or (gnus-parameter-list-identifier group)
5526       (if (consp gnus-list-identifiers)
5527           (mapconcat 'identity gnus-list-identifiers " *\\|")
5528         gnus-list-identifiers)))
5529
5530 (defun gnus-summary-remove-list-identifiers ()
5531   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5532   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5533         changed subject)
5534     (when regexp
5535       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5536       (dolist (header gnus-newsgroup-headers)
5537         (setq subject (mail-header-subject header)
5538               changed nil)
5539         (while (string-match regexp subject)
5540           (setq subject
5541                 (concat (substring subject 0 (match-beginning 1))
5542                         (substring subject (match-end 0)))
5543                 changed t))
5544         (when changed
5545           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5546             (setq subject
5547                   (concat (substring subject 0 (match-beginning 1))
5548                           (substring subject (match-end 1)))))
5549           (mail-header-set-subject header subject))))))
5550
5551 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5552   "Fetch headers of ARTICLES."
5553   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5554     (gnus-message 5 "Fetching headers for %s..." name)
5555     (prog1
5556         (if (eq 'nov
5557                 (setq gnus-headers-retrieved-by
5558                       (gnus-retrieve-headers
5559                        articles gnus-newsgroup-name
5560                        (or limit
5561                            ;; We might want to fetch old headers, but
5562                            ;; not if there is only 1 article.
5563                            (and (or (and
5564                                      (not (eq gnus-fetch-old-headers 'some))
5565                                      (not (numberp gnus-fetch-old-headers)))
5566                                     (> (length articles) 1))
5567                                 gnus-fetch-old-headers)))))
5568             (gnus-get-newsgroup-headers-xover
5569              articles force-new dependencies gnus-newsgroup-name t)
5570           (gnus-get-newsgroup-headers dependencies force-new))
5571       (gnus-message 5 "Fetching headers for %s...done" name))))
5572
5573 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5574   "Select newsgroup GROUP.
5575 If READ-ALL is non-nil, all articles in the group are selected.
5576 If SELECT-ARTICLES, only select those articles from GROUP."
5577   (let* ((entry (gnus-group-entry group))
5578          ;;!!! Dirty hack; should be removed.
5579          (gnus-summary-ignore-duplicates
5580           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5581               t
5582             gnus-summary-ignore-duplicates))
5583          (info (nth 2 entry))
5584          charset articles fetched-articles cached)
5585
5586     (unless (gnus-check-server
5587              (set (make-local-variable 'gnus-current-select-method)
5588                   (gnus-find-method-for-group group)))
5589       (error "Couldn't open server"))
5590     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5591
5592     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5593         (gnus-activate-group group)     ; Or we can activate it...
5594         (progn                          ; Or we bug out.
5595           (when (equal major-mode 'gnus-summary-mode)
5596             (gnus-kill-buffer (current-buffer)))
5597           (error
5598            "Couldn't activate group %s: %s"
5599            (mm-decode-coding-string group charset)
5600            (mm-decode-coding-string (gnus-status-message group) charset))))
5601
5602     (unless (gnus-request-group group t)
5603       (when (equal major-mode 'gnus-summary-mode)
5604         (gnus-kill-buffer (current-buffer)))
5605       (error "Couldn't request group %s: %s"
5606              (mm-decode-coding-string group charset)
5607              (mm-decode-coding-string (gnus-status-message group) charset)))
5608
5609     (when (and gnus-agent
5610                (gnus-active group))
5611       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5612
5613       (setq gnus-summary-use-undownloaded-faces
5614             (gnus-agent-find-parameter
5615              group
5616              'agent-enable-undownloaded-faces)))
5617
5618     (setq gnus-newsgroup-name group
5619           gnus-newsgroup-unselected nil
5620           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5621
5622     (let ((display (gnus-group-find-parameter group 'display)))
5623       (setq gnus-newsgroup-display
5624             (cond
5625              ((not (zerop (or (car-safe read-all) 0)))
5626               ;; The user entered the group with C-u SPC/RET, let's show
5627               ;; all articles.
5628               'gnus-not-ignore)
5629              ((eq display 'all)
5630               'gnus-not-ignore)
5631              ((arrayp display)
5632               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5633              ((numberp display)
5634               ;; The following is probably the "correct" solution, but
5635               ;; it makes Gnus fetch all headers and then limit the
5636               ;; articles (which is slow), so instead we hack the
5637               ;; select-articles parameter instead. -- Simon Josefsson
5638               ;; <jas@kth.se>
5639               ;;
5640               ;; (gnus-byte-compile
5641               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5642               ;;                         display)))))
5643               (setq select-articles
5644                     (gnus-uncompress-range
5645                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5646                              (if (> tmp 0)
5647                                  tmp
5648                                1))
5649                            (cdr (gnus-active group)))))
5650               nil)
5651              (t
5652               nil))))
5653
5654     (gnus-summary-setup-default-charset)
5655
5656     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5657     (when (gnus-virtual-group-p group)
5658       (setq cached gnus-newsgroup-cached))
5659
5660     (setq gnus-newsgroup-unreads
5661           (gnus-sorted-ndifference
5662            (gnus-sorted-ndifference gnus-newsgroup-unreads
5663                                     gnus-newsgroup-marked)
5664            gnus-newsgroup-dormant))
5665
5666     (setq gnus-newsgroup-processable nil)
5667
5668     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5669
5670     ;; Adjust and set lists of article marks.
5671     (when info
5672       (gnus-adjust-marked-articles info))
5673     (if (setq articles select-articles)
5674         (setq gnus-newsgroup-unselected
5675               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5676       (setq articles (gnus-articles-to-read group read-all)))
5677
5678     (cond
5679      ((null articles)
5680       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5681       'quit)
5682      ((eq articles 0) nil)
5683      (t
5684       ;; Init the dependencies hash table.
5685       (setq gnus-newsgroup-dependencies
5686             (gnus-make-hashtable (length articles)))
5687       (gnus-set-global-variables)
5688       ;; Retrieve the headers and read them in.
5689
5690       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5691
5692       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5693       (when cached
5694         (setq gnus-newsgroup-cached cached))
5695
5696       ;; Suppress duplicates?
5697       (when gnus-suppress-duplicates
5698         (gnus-dup-suppress-articles))
5699
5700       ;; Set the initial limit.
5701       (setq gnus-newsgroup-limit (copy-sequence articles))
5702       ;; Remove canceled articles from the list of unread articles.
5703       (setq fetched-articles
5704             (mapcar (lambda (headers) (mail-header-number headers))
5705                     gnus-newsgroup-headers))
5706       (setq gnus-newsgroup-articles fetched-articles)
5707       (setq gnus-newsgroup-unreads
5708             (gnus-sorted-nintersection
5709              gnus-newsgroup-unreads fetched-articles))
5710       (gnus-compute-unseen-list)
5711
5712       ;; Removed marked articles that do not exist.
5713       (gnus-update-missing-marks
5714        (gnus-sorted-difference articles fetched-articles))
5715       ;; We might want to build some more threads first.
5716       (when (and gnus-fetch-old-headers
5717                  (eq gnus-headers-retrieved-by 'nov))
5718         (if (eq gnus-fetch-old-headers 'invisible)
5719             (gnus-build-all-threads)
5720           (gnus-build-old-threads)))
5721       ;; Let the Gnus agent mark articles as read.
5722       (when gnus-agent
5723         (gnus-agent-get-undownloaded-list))
5724       ;; Remove list identifiers from subject
5725       (gnus-summary-remove-list-identifiers)
5726       ;; Check whether auto-expire is to be done in this group.
5727       (setq gnus-newsgroup-auto-expire
5728             (and (gnus-group-auto-expirable-p group)
5729                  (not (gnus-group-read-only-p group))))
5730       ;; Set up the article buffer now, if necessary.
5731       (unless (and gnus-single-article-buffer
5732                    (equal gnus-article-buffer "*Article*"))
5733         (gnus-article-setup-buffer))
5734       ;; First and last article in this newsgroup.
5735       (when gnus-newsgroup-headers
5736         (setq gnus-newsgroup-begin
5737               (mail-header-number (car gnus-newsgroup-headers))
5738               gnus-newsgroup-end
5739               (mail-header-number
5740                (gnus-last-element gnus-newsgroup-headers))))
5741       ;; GROUP is successfully selected.
5742       (or gnus-newsgroup-headers t)))))
5743
5744 (defun gnus-compute-unseen-list ()
5745   ;; The `seen' marks are treated specially.
5746   (if (not gnus-newsgroup-seen)
5747       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5748     (setq gnus-newsgroup-unseen
5749           (gnus-inverse-list-range-intersection
5750            gnus-newsgroup-articles gnus-newsgroup-seen))))
5751
5752 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5753
5754 (defun gnus-summary-display-make-predicate (display)
5755   (require 'gnus-agent)
5756   (when (= (length display) 1)
5757     (setq display (car display)))
5758   (unless gnus-summary-display-cache
5759     (dolist (elem (append '((unread . unread)
5760                             (read . read)
5761                             (unseen . unseen))
5762                           gnus-article-mark-lists))
5763       (push (cons (cdr elem)
5764                   (gnus-byte-compile    ;Why bother?
5765                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5766             gnus-summary-display-cache)))
5767   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5768         (gnus-category-predicate-cache gnus-summary-display-cache))
5769     (gnus-get-predicate display)))
5770
5771 ;; Uses the dynamically bound `gnus-number' variable.
5772 (defvar gnus-number)
5773 (defun gnus-article-marked-p (type &optional article)
5774   (let ((article (or article gnus-number)))
5775     (cond
5776      ((eq type 'tick)
5777       (memq article gnus-newsgroup-marked))
5778      ((eq type 'spam)
5779       (memq article gnus-newsgroup-spam-marked))
5780      ((eq type 'unsend)
5781       (memq article gnus-newsgroup-unsendable))
5782      ((eq type 'undownload)
5783       (memq article gnus-newsgroup-undownloaded))
5784      ((eq type 'download)
5785       (memq article gnus-newsgroup-downloadable))
5786      ((eq type 'unread)
5787       (memq article gnus-newsgroup-unreads))
5788      ((eq type 'read)
5789       (memq article gnus-newsgroup-reads))
5790      ((eq type 'dormant)
5791       (memq article gnus-newsgroup-dormant) )
5792      ((eq type 'expire)
5793       (memq article gnus-newsgroup-expirable))
5794      ((eq type 'reply)
5795       (memq article gnus-newsgroup-replied))
5796      ((eq type 'killed)
5797       (memq article gnus-newsgroup-killed))
5798      ((eq type 'bookmark)
5799       (assq article gnus-newsgroup-bookmarks))
5800      ((eq type 'score)
5801       (assq article gnus-newsgroup-scored))
5802      ((eq type 'save)
5803       (memq article gnus-newsgroup-saved))
5804      ((eq type 'cache)
5805       (memq article gnus-newsgroup-cached))
5806      ((eq type 'forward)
5807       (memq article gnus-newsgroup-forwarded))
5808      ((eq type 'seen)
5809       (not (memq article gnus-newsgroup-unseen)))
5810      ((eq type 'recent)
5811       (memq article gnus-newsgroup-recent))
5812      (t t))))
5813
5814 (defun gnus-articles-to-read (group &optional read-all)
5815   "Find out what articles the user wants to read."
5816   (let* ((only-read-p t)
5817          (articles
5818           ;; Select all articles if `read-all' is non-nil, or if there
5819           ;; are no unread articles.
5820           (if (or read-all
5821                   (and (zerop (length gnus-newsgroup-marked))
5822                        (zerop (length gnus-newsgroup-unreads)))
5823                   ;; Fetch all if the predicate is non-nil.
5824                   gnus-newsgroup-display)
5825               ;; We want to select the headers for all the articles in
5826               ;; the group, so we select either all the active
5827               ;; articles in the group, or (if that's nil), the
5828               ;; articles in the cache.
5829               (or
5830                (if gnus-newsgroup-maximum-articles
5831                    (let ((active (gnus-active group)))
5832                      (gnus-uncompress-range
5833                       (cons (max (car active)
5834                                  (- (cdr active)
5835                                     gnus-newsgroup-maximum-articles
5836                                     -1))
5837                             (cdr active))))
5838                  (gnus-uncompress-range (gnus-active group)))
5839                (gnus-cache-articles-in-group group))
5840             ;; Select only the "normal" subset of articles.
5841             (setq only-read-p nil)
5842             (gnus-sorted-nunion
5843              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5844              gnus-newsgroup-unreads)))
5845          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5846          (scored (length scored-list))
5847          (number (length articles))
5848          (marked (+ (length gnus-newsgroup-marked)
5849                     (length gnus-newsgroup-dormant)))
5850          (select
5851           (cond
5852            ((numberp read-all)
5853             read-all)
5854            ((numberp gnus-newsgroup-display)
5855             gnus-newsgroup-display)
5856            (t
5857             (condition-case ()
5858                 (cond
5859                  ((and (or (<= scored marked) (= scored number))
5860                        (numberp gnus-large-newsgroup)
5861                        (> number gnus-large-newsgroup))
5862                   (let* ((cursor-in-echo-area nil)
5863                          (initial (gnus-parameter-large-newsgroup-initial
5864                                    gnus-newsgroup-name))
5865                          (default (if only-read-p
5866                                       (or initial gnus-large-newsgroup)
5867                                     number))
5868                          (input
5869                           (read-string
5870                            (if only-read-p
5871                                (format
5872                               "How many articles from %s (available %d, default %d): "
5873                               (gnus-group-decoded-name
5874                                (gnus-group-real-name gnus-newsgroup-name))
5875                               number default)
5876                              (format
5877                                 "How many articles from %s (%d available): "
5878                                 (gnus-group-decoded-name
5879                                  (gnus-group-real-name gnus-newsgroup-name))
5880                                 default))
5881                            nil
5882                            nil
5883                            (number-to-string default))))
5884                     (if (string-match "^[ \t]*$" input) number input)))
5885                  ((and (> scored marked) (< scored number)
5886                        (> (- scored number) 20))
5887                   (let ((input
5888                          (read-string
5889                           (format "%s %s (%d scored, %d total): "
5890                                   "How many articles from"
5891                                   (gnus-group-decoded-name
5892                                    (gnus-group-real-name gnus-newsgroup-name))
5893                                   scored number))))
5894                     (if (string-match "^[ \t]*$" input)
5895                         number input)))
5896                  (t number))
5897               (quit
5898                (message "Quit getting the articles to read")
5899                nil))))))
5900     (setq select (if (stringp select) (string-to-number select) select))
5901     (if (or (null select) (zerop select))
5902         select
5903       (if (and (not (zerop scored)) (<= (abs select) scored))
5904           (progn
5905             (setq articles (sort scored-list '<))
5906             (setq number (length articles)))
5907         (setq articles (copy-sequence articles)))
5908
5909       (when (< (abs select) number)
5910         (if (< select 0)
5911             ;; Select the N oldest articles.
5912             (setcdr (nthcdr (1- (abs select)) articles) nil)
5913           ;; Select the N most recent articles.
5914           (setq articles (nthcdr (- number select) articles))))
5915       (setq gnus-newsgroup-unselected
5916             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5917       (when gnus-alter-articles-to-read-function
5918         (setq articles
5919               (sort
5920                (funcall gnus-alter-articles-to-read-function
5921                         gnus-newsgroup-name articles)
5922                '<)))
5923       articles)))
5924
5925 (defun gnus-killed-articles (killed articles)
5926   (let (out)
5927     (while articles
5928       (when (inline (gnus-member-of-range (car articles) killed))
5929         (push (car articles) out))
5930       (setq articles (cdr articles)))
5931     out))
5932
5933 (defun gnus-uncompress-marks (marks)
5934   "Uncompress the mark ranges in MARKS."
5935   (let ((uncompressed '(score bookmark))
5936         out)
5937     (while marks
5938       (if (memq (caar marks) uncompressed)
5939           (push (car marks) out)
5940         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5941       (setq marks (cdr marks)))
5942     out))
5943
5944 (defun gnus-article-mark-to-type (mark)
5945   "Return the type of MARK."
5946   (or (cadr (assq mark gnus-article-special-mark-lists))
5947       'list))
5948
5949 (defun gnus-article-unpropagatable-p (mark)
5950   "Return whether MARK should be propagated to back end."
5951   (memq mark gnus-article-unpropagated-mark-lists))
5952
5953 (defun gnus-adjust-marked-articles (info)
5954   "Set all article lists and remove all marks that are no longer valid."
5955   (let* ((marked-lists (gnus-info-marks info))
5956          (active (gnus-active (gnus-info-group info)))
5957          (min (car active))
5958          (max (cdr active))
5959          (types gnus-article-mark-lists)
5960          marks var articles article mark mark-type
5961          bgn end)
5962     ;; Hack to avoid adjusting marks for imap.
5963     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5964               'nnimap)
5965       (setq min 1))
5966
5967     (dolist (marks marked-lists)
5968       (setq mark (car marks)
5969             mark-type (gnus-article-mark-to-type mark)
5970             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5971
5972       ;; We set the variable according to the type of the marks list,
5973       ;; and then adjust the marks to a subset of the active articles.
5974       (cond
5975        ;; Adjust "simple" lists - compressed yet unsorted
5976        ((eq mark-type 'list)
5977         ;; Simultaneously uncompress and clip to active range
5978         ;; See gnus-uncompress-range for a description of possible marks
5979         (let (l lh)
5980           (if (not (cadr marks))
5981               (set var nil)
5982             (setq articles (if (numberp (cddr marks))
5983                                (list (cdr marks))
5984                              (cdr marks))
5985                   lh (cons nil nil)
5986                   l lh)
5987
5988             (while (setq article (pop articles))
5989               (cond ((consp article)
5990                      (setq bgn (max (car article) min)
5991                            end (min (cdr article) max))
5992                      (while (<= bgn end)
5993                        (setq l (setcdr l (cons bgn nil))
5994                              bgn (1+ bgn))))
5995                     ((and (<= min article)
5996                           (>= max article))
5997                      (setq l (setcdr l (cons article nil))))))
5998             (set var (cdr lh)))))
5999        ;; Adjust assocs.
6000        ((eq mark-type 'tuple)
6001         (set var (setq articles (cdr marks)))
6002         (when (not (listp (cdr (symbol-value var))))
6003           (set var (list (symbol-value var))))
6004         (when (not (listp (cdr articles)))
6005           (setq articles (list articles)))
6006         (while articles
6007           (when (or (not (consp (setq article (pop articles))))
6008                     (< (car article) min)
6009                     (> (car article) max))
6010             (set var (delq article (symbol-value var))))))
6011        ;; Adjust ranges (sloppily).
6012        ((eq mark-type 'range)
6013         (cond
6014          ((eq mark 'seen)
6015           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6016           ;; It should be (seen (NUM1 . NUM2)).
6017           (when (numberp (cddr marks))
6018             (setcdr marks (list (cdr marks))))
6019           (setq articles (cdr marks))
6020           (while (and articles
6021                       (or (and (consp (car articles))
6022                                (> min (cdar articles)))
6023                           (and (numberp (car articles))
6024                                (> min (car articles)))))
6025             (pop articles))
6026           (set var articles))))))))
6027
6028 (defun gnus-update-missing-marks (missing)
6029   "Go through the list of MISSING articles and remove them from the mark lists."
6030   (when missing
6031     (let (var m)
6032       ;; Go through all types.
6033       (dolist (elem gnus-article-mark-lists)
6034         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6035           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6036           (when (symbol-value var)
6037             ;; This list has articles.  So we delete all missing
6038             ;; articles from it.
6039             (setq m missing)
6040             (while m
6041               (set var (delq (pop m) (symbol-value var))))))))))
6042
6043 (defun gnus-update-marks ()
6044   "Enter the various lists of marked articles into the newsgroup info list."
6045   (let ((types gnus-article-mark-lists)
6046         (info (gnus-get-info gnus-newsgroup-name))
6047         type list newmarked symbol delta-marks)
6048     (when info
6049       ;; Add all marks lists to the list of marks lists.
6050       (while (setq type (pop types))
6051         (setq list (symbol-value
6052                     (setq symbol
6053                           (intern (format "gnus-newsgroup-%s" (car type))))))
6054
6055         (when list
6056           ;; Get rid of the entries of the articles that have the
6057           ;; default score.
6058           (when (and (eq (cdr type) 'score)
6059                      gnus-save-score
6060                      list)
6061             (let* ((arts list)
6062                    (prev (cons nil list))
6063                    (all prev))
6064               (while arts
6065                 (if (or (not (consp (car arts)))
6066                         (= (cdar arts) gnus-summary-default-score))
6067                     (setcdr prev (cdr arts))
6068                   (setq prev arts))
6069                 (setq arts (cdr arts)))
6070               (setq list (cdr all)))))
6071
6072         (when (eq (cdr type) 'seen)
6073           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6074
6075         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6076           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6077
6078         (when (and (gnus-check-backend-function
6079                     'request-set-mark gnus-newsgroup-name)
6080                    (not (gnus-article-unpropagatable-p (cdr type))))
6081           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6082                  ;; Don't do anything about marks for articles we
6083                  ;; didn't actually get any headers for.
6084                  (del
6085                   (gnus-list-range-intersection
6086                    gnus-newsgroup-articles
6087                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6088                  (add
6089                   (gnus-list-range-intersection
6090                    gnus-newsgroup-articles
6091                    (gnus-remove-from-range
6092                     (gnus-copy-sequence list) old))))
6093             (when add
6094               (push (list add 'add (list (cdr type))) delta-marks))
6095             (when del
6096               ;; Don't delete marks from outside the active range.
6097               ;; This shouldn't happen, but is a sanity check.
6098               (setq del (gnus-sorted-range-intersection
6099                          (gnus-active gnus-newsgroup-name) del))
6100               (push (list del 'del (list (cdr type))) delta-marks))))
6101
6102         (when list
6103           (push (cons (cdr type) list) newmarked)))
6104
6105       (when delta-marks
6106         (unless (gnus-check-group gnus-newsgroup-name)
6107           (error "Can't open server for %s" gnus-newsgroup-name))
6108         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6109
6110       ;; Enter these new marks into the info of the group.
6111       (if (nthcdr 3 info)
6112           (setcar (nthcdr 3 info) newmarked)
6113         ;; Add the marks lists to the end of the info.
6114         (when newmarked
6115           (setcdr (nthcdr 2 info) (list newmarked))))
6116
6117       ;; Cut off the end of the info if there's nothing else there.
6118       (let ((i 5))
6119         (while (and (> i 2)
6120                     (not (nth i info)))
6121           (when (nthcdr (decf i) info)
6122             (setcdr (nthcdr i info) nil)))))))
6123
6124 (defun gnus-set-mode-line (where)
6125   "Set the mode line of the article or summary buffers.
6126 If WHERE is `summary', the summary mode line format will be used."
6127   ;; Is this mode line one we keep updated?
6128   (when (and (memq where gnus-updated-mode-lines)
6129              (symbol-value
6130               (intern (format "gnus-%s-mode-line-format-spec" where))))
6131     (let (mode-string)
6132       ;; We evaluate this in the summary buffer since these
6133       ;; variables are buffer-local to that buffer.
6134       (with-current-buffer gnus-summary-buffer
6135         ;; We bind all these variables that are used in the `eval' form
6136         ;; below.
6137         (let* ((mformat (symbol-value
6138                          (intern
6139                           (format "gnus-%s-mode-line-format-spec" where))))
6140                (gnus-tmp-group-name (gnus-mode-string-quote
6141                                      (gnus-group-decoded-name
6142                                       gnus-newsgroup-name)))
6143                (gnus-tmp-article-number (or gnus-current-article 0))
6144                (gnus-tmp-unread gnus-newsgroup-unreads)
6145                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6146                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6147                (gnus-tmp-unread-and-unselected
6148                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6149                             (zerop gnus-tmp-unselected))
6150                        "")
6151                       ((zerop gnus-tmp-unselected)
6152                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6153                       (t (format "{%d(+%d) more}"
6154                                  gnus-tmp-unread-and-unticked
6155                                  gnus-tmp-unselected))))
6156                (gnus-tmp-subject
6157                 (if (and gnus-current-headers
6158                          (vectorp gnus-current-headers))
6159                     (gnus-mode-string-quote
6160                      (mail-header-subject gnus-current-headers))
6161                   ""))
6162                bufname-length max-len
6163                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6164           (setq mode-string (eval mformat))
6165           (setq bufname-length (if (string-match "%b" mode-string)
6166                                    (- (length
6167                                        (buffer-name
6168                                         (if (eq where 'summary)
6169                                             nil
6170                                           (get-buffer gnus-article-buffer))))
6171                                       2)
6172                                  0))
6173           (setq max-len (max 4 (if gnus-mode-non-string-length
6174                                    (- (window-width)
6175                                       gnus-mode-non-string-length
6176                                       bufname-length)
6177                                  (length mode-string))))
6178           ;; We might have to chop a bit of the string off...
6179           (when (> (length mode-string) max-len)
6180             (setq mode-string
6181                   (concat (truncate-string-to-width mode-string (- max-len 3))
6182                           "...")))))
6183       ;; Update the mode line.
6184       (setq mode-line-buffer-identification
6185             (gnus-mode-line-buffer-identification (list mode-string)))
6186       (set-buffer-modified-p t))))
6187
6188 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6189   "Go through the HEADERS list and add all Xrefs to a hash table.
6190 The resulting hash table is returned, or nil if no Xrefs were found."
6191   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6192          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6193          (xref-hashtb (gnus-make-hashtable))
6194          start group entry number xrefs header)
6195     (while headers
6196       (setq header (pop headers))
6197       (when (and (setq xrefs (mail-header-xref header))
6198                  (not (memq (setq number (mail-header-number header))
6199                             unreads)))
6200         (setq start 0)
6201         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6202           (setq start (match-end 0))
6203           (setq group (if prefix
6204                           (concat prefix (substring xrefs (match-beginning 1)
6205                                                     (match-end 1)))
6206                         (substring xrefs (match-beginning 1) (match-end 1))))
6207           (setq number
6208                 (string-to-number (substring xrefs (match-beginning 2)
6209                                           (match-end 2))))
6210           (if (setq entry (gnus-gethash group xref-hashtb))
6211               (setcdr entry (cons number (cdr entry)))
6212             (gnus-sethash group (cons number nil) xref-hashtb)))))
6213     (and start xref-hashtb)))
6214
6215 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6216   "Look through all the headers and mark the Xrefs as read."
6217   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6218         name info xref-hashtb idlist method nth4)
6219     (with-current-buffer gnus-group-buffer
6220       (when (setq xref-hashtb
6221                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6222         (mapatoms
6223          (lambda (group)
6224            (unless (string= from-newsgroup (setq name (symbol-name group)))
6225              (setq idlist (symbol-value group))
6226              ;; Dead groups are not updated.
6227              (and (prog1
6228                       (setq info (gnus-get-info name))
6229                     (when (stringp (setq nth4 (gnus-info-method info)))
6230                       (setq nth4 (gnus-server-to-method nth4))))
6231                   ;; Only do the xrefs if the group has the same
6232                   ;; select method as the group we have just read.
6233                   (or (gnus-methods-equal-p
6234                        nth4 (gnus-find-method-for-group from-newsgroup))
6235                       virtual
6236                       (equal nth4 (setq method (gnus-find-method-for-group
6237                                                 from-newsgroup)))
6238                       (and (equal (car nth4) (car method))
6239                            (equal (nth 1 nth4) (nth 1 method))))
6240                   gnus-use-cross-reference
6241                   (or (not (eq gnus-use-cross-reference t))
6242                       virtual
6243                       ;; Only do cross-references on subscribed
6244                       ;; groups, if that is what is wanted.
6245                       (<= (gnus-info-level info) gnus-level-subscribed))
6246                   (gnus-group-make-articles-read name idlist))))
6247          xref-hashtb)))))
6248
6249 (defun gnus-compute-read-articles (group articles)
6250   (let* ((entry (gnus-group-entry group))
6251          (info (nth 2 entry))
6252          (active (gnus-active group))
6253          ninfo)
6254     (when entry
6255       ;; First peel off all invalid article numbers.
6256       (when active
6257         (let ((ids articles)
6258               id first)
6259           (while (setq id (pop ids))
6260             (when (and first (> id (cdr active)))
6261               ;; We'll end up in this situation in one particular
6262               ;; obscure situation.  If you re-scan a group and get
6263               ;; a new article that is cross-posted to a different
6264               ;; group that has not been re-scanned, you might get
6265               ;; crossposted article that has a higher number than
6266               ;; Gnus believes possible.  So we re-activate this
6267               ;; group as well.  This might mean doing the
6268               ;; crossposting thingy will *increase* the number
6269               ;; of articles in some groups.  Tsk, tsk.
6270               (setq active (or (gnus-activate-group group) active)))
6271             (when (or (> id (cdr active))
6272                       (< id (car active)))
6273               (setq articles (delq id articles))))))
6274       ;; If the read list is nil, we init it.
6275       (if (and active
6276                (null (gnus-info-read info))
6277                (> (car active) 1))
6278           (setq ninfo (cons 1 (1- (car active))))
6279         (setq ninfo (gnus-info-read info)))
6280       ;; Then we add the read articles to the range.
6281       (gnus-add-to-range
6282        ninfo (setq articles (sort articles '<))))))
6283
6284 (defun gnus-group-make-articles-read (group articles)
6285   "Update the info of GROUP to say that ARTICLES are read."
6286   (let* ((num 0)
6287          (entry (gnus-group-entry group))
6288          (info (nth 2 entry))
6289          (active (gnus-active group))
6290          range)
6291     (if (not entry)
6292         ;; Group that Gnus doesn't know exists, but still allow the
6293         ;; backend to set marks.
6294         (gnus-request-set-mark
6295          group (list (list (gnus-compress-sequence (sort articles #'<))
6296                            'add '(read))))
6297       ;; Normal, subscribed groups.
6298       (setq range (gnus-compute-read-articles group articles))
6299       (with-current-buffer gnus-group-buffer
6300         (gnus-undo-register
6301           `(progn
6302              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6303              (gnus-info-set-read ',info ',(gnus-info-read info))
6304              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6305              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6306              (gnus-group-update-group ,group t))))
6307       ;; Add the read articles to the range.
6308       (gnus-info-set-read info range)
6309       (gnus-request-set-mark group (list (list range 'add '(read))))
6310       ;; Then we have to re-compute how many unread
6311       ;; articles there are in this group.
6312       (when active
6313         (cond
6314          ((not range)
6315           (setq num (- (1+ (cdr active)) (car active))))
6316          ((not (listp (cdr range)))
6317           (setq num (- (cdr active) (- (1+ (cdr range))
6318                                        (car range)))))
6319          (t
6320           (while range
6321             (if (numberp (car range))
6322                 (setq num (1+ num))
6323               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6324             (setq range (cdr range)))
6325           (setq num (- (cdr active) num))))
6326         ;; Update the number of unread articles.
6327         (setcar entry num)
6328         ;; Update the group buffer.
6329         (unless (gnus-ephemeral-group-p group)
6330           (gnus-group-update-group group t))))))
6331
6332 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6333   (let ((cur nntp-server-buffer)
6334         (dependencies
6335          (or dependencies
6336              (with-current-buffer gnus-summary-buffer
6337                gnus-newsgroup-dependencies)))
6338         headers id end ref number
6339         (mail-parse-charset gnus-newsgroup-charset)
6340         (mail-parse-ignored-charsets
6341          (save-current-buffer (condition-case nil
6342                                   (set-buffer gnus-summary-buffer)
6343                                 (error))
6344                               gnus-newsgroup-ignored-charsets)))
6345     (with-current-buffer nntp-server-buffer
6346       ;; Translate all TAB characters into SPACE characters.
6347       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6348       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6349       (ietf-drums-unfold-fws)
6350       (gnus-run-hooks 'gnus-parse-headers-hook)
6351       (let ((case-fold-search t)
6352             in-reply-to header p lines chars)
6353         (goto-char (point-min))
6354         ;; Search to the beginning of the next header.  Error messages
6355         ;; do not begin with 2 or 3.
6356         (while (re-search-forward "^[23][0-9]+ " nil t)
6357           (setq id nil
6358                 ref nil)
6359           ;; This implementation of this function, with nine
6360           ;; search-forwards instead of the one re-search-forward and
6361           ;; a case (which basically was the old function) is actually
6362           ;; about twice as fast, even though it looks messier.  You
6363           ;; can't have everything, I guess.  Speed and elegance
6364           ;; doesn't always go hand in hand.
6365           (setq
6366            header
6367            (vector
6368             ;; Number.
6369             (prog1
6370                 (setq number (read cur))
6371               (end-of-line)
6372               (setq p (point))
6373               (narrow-to-region (point)
6374                                 (or (and (search-forward "\n.\n" nil t)
6375                                          (- (point) 2))
6376                                     (point))))
6377             ;; Subject.
6378             (progn
6379               (goto-char p)
6380               (if (search-forward "\nsubject:" nil t)
6381                   (funcall gnus-decode-encoded-word-function
6382                            (nnheader-header-value))
6383                 "(none)"))
6384             ;; From.
6385             (progn
6386               (goto-char p)
6387               (if (search-forward "\nfrom:" nil t)
6388                   (funcall gnus-decode-encoded-address-function
6389                            (nnheader-header-value))
6390                 "(nobody)"))
6391             ;; Date.
6392             (progn
6393               (goto-char p)
6394               (if (search-forward "\ndate:" nil t)
6395                   (nnheader-header-value) ""))
6396             ;; Message-ID.
6397             (progn
6398               (goto-char p)
6399               (setq id (if (re-search-forward
6400                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6401                            ;; We do it this way to make sure the Message-ID
6402                            ;; is (somewhat) syntactically valid.
6403                            (buffer-substring (match-beginning 1)
6404                                              (match-end 1))
6405                          ;; If there was no message-id, we just fake one
6406                          ;; to make subsequent routines simpler.
6407                          (nnheader-generate-fake-message-id number))))
6408             ;; References.
6409             (progn
6410               (goto-char p)
6411               (if (search-forward "\nreferences:" nil t)
6412                   (progn
6413                     (setq end (point))
6414                     (prog1
6415                         (nnheader-header-value)
6416                       (setq ref
6417                             (buffer-substring
6418                              (progn
6419                                (end-of-line)
6420                                (search-backward ">" end t)
6421                                (1+ (point)))
6422                              (progn
6423                                (search-backward "<" end t)
6424                                (point))))))
6425                 ;; Get the references from the in-reply-to header if there
6426                 ;; were no references and the in-reply-to header looks
6427                 ;; promising.
6428                 (if (and (search-forward "\nin-reply-to:" nil t)
6429                          (setq in-reply-to (nnheader-header-value))
6430                          (string-match "<[^>]+>" in-reply-to))
6431                     (let (ref2)
6432                       (setq ref (substring in-reply-to (match-beginning 0)
6433                                            (match-end 0)))
6434                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6435                         (setq ref2 (substring in-reply-to (match-beginning 0)
6436                                               (match-end 0)))
6437                         (when (> (length ref2) (length ref))
6438                           (setq ref ref2)))
6439                       ref)
6440                   (setq ref nil))))
6441             ;; Chars.
6442             (progn
6443               (goto-char p)
6444               (if (search-forward "\nchars: " nil t)
6445                   (if (numberp (setq chars (ignore-errors (read cur))))
6446                       chars -1)
6447                 -1))
6448             ;; Lines.
6449             (progn
6450               (goto-char p)
6451               (if (search-forward "\nlines: " nil t)
6452                   (if (numberp (setq lines (ignore-errors (read cur))))
6453                       lines -1)
6454                 -1))
6455             ;; Xref.
6456             (progn
6457               (goto-char p)
6458               (and (search-forward "\nxref:" nil t)
6459                    (nnheader-header-value)))
6460             ;; Extra.
6461             (when gnus-extra-headers
6462               (let ((extra gnus-extra-headers)
6463                     out)
6464                 (while extra
6465                   (goto-char p)
6466                   (when (search-forward
6467                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6468                     (push (cons (car extra) (nnheader-header-value))
6469                           out))
6470                   (pop extra))
6471                 out))))
6472           (when (equal id ref)
6473             (setq ref nil))
6474
6475           (when gnus-alter-header-function
6476             (funcall gnus-alter-header-function header)
6477             (setq id (mail-header-id header)
6478                   ref (gnus-parent-id (mail-header-references header))))
6479
6480           (when (setq header
6481                       (gnus-dependencies-add-header
6482                        header dependencies force-new))
6483             (push header headers))
6484           (goto-char (point-max))
6485           (widen))
6486         (nreverse headers)))))
6487
6488 ;; Goes through the xover lines and returns a list of vectors
6489 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6490                                                   force-new dependencies
6491                                                   group also-fetch-heads)
6492   "Parse the news overview data in the server buffer.
6493 Return a list of headers that match SEQUENCE (see
6494 `nntp-retrieve-headers')."
6495   ;; Get the Xref when the users reads the articles since most/some
6496   ;; NNTP servers do not include Xrefs when using XOVER.
6497   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6498   (let ((mail-parse-charset gnus-newsgroup-charset)
6499         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6500         (cur nntp-server-buffer)
6501         (dependencies (or dependencies gnus-newsgroup-dependencies))
6502         (allp (cond
6503                ((eq gnus-read-all-available-headers t)
6504                 t)
6505                ((and (stringp gnus-read-all-available-headers)
6506                      group)
6507                 (string-match gnus-read-all-available-headers group))
6508                (t
6509                 nil)))
6510         number headers header)
6511     (with-current-buffer nntp-server-buffer
6512       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6513       ;; Allow the user to mangle the headers before parsing them.
6514       (gnus-run-hooks 'gnus-parse-headers-hook)
6515       (goto-char (point-min))
6516       (gnus-parse-without-error
6517         (while (and (or sequence allp)
6518                     (not (eobp)))
6519           (setq number (read cur))
6520           (when (not allp)
6521             (while (and sequence
6522                         (< (car sequence) number))
6523               (setq sequence (cdr sequence))))
6524           (when (and (or allp
6525                          (and sequence
6526                               (eq number (car sequence))))
6527                      (progn
6528                        (setq sequence (cdr sequence))
6529                        (setq header (inline
6530                                       (gnus-nov-parse-line
6531                                        number dependencies force-new)))))
6532             (push header headers))
6533           (forward-line 1)))
6534       ;; A common bug in inn is that if you have posted an article and
6535       ;; then retrieves the active file, it will answer correctly --
6536       ;; the new article is included.  However, a NOV entry for the
6537       ;; article may not have been generated yet, so this may fail.
6538       ;; We work around this problem by retrieving the last few
6539       ;; headers using HEAD.
6540       (if (or (not also-fetch-heads)
6541               (not sequence))
6542           ;; We (probably) got all the headers.
6543           (nreverse headers)
6544         (let ((gnus-nov-is-evil t))
6545           (nconc
6546            (nreverse headers)
6547            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6548              (gnus-get-newsgroup-headers))))))))
6549
6550 (defun gnus-article-get-xrefs ()
6551   "Fill in the Xref value in `gnus-current-headers', if necessary.
6552 This is meant to be called in `gnus-article-internal-prepare-hook'."
6553   (let ((headers (with-current-buffer gnus-summary-buffer
6554                    gnus-current-headers)))
6555     (or (not gnus-use-cross-reference)
6556         (not headers)
6557         (and (mail-header-xref headers)
6558              (not (string= (mail-header-xref headers) "")))
6559         (let ((case-fold-search t)
6560               xref)
6561           (save-restriction
6562             (nnheader-narrow-to-headers)
6563             (goto-char (point-min))
6564             (when (or (and (not (eobp))
6565                            (eq (downcase (char-after)) ?x)
6566                            (looking-at "Xref:"))
6567                       (search-forward "\nXref:" nil t))
6568               (goto-char (1+ (match-end 0)))
6569               (setq xref (buffer-substring (point) (point-at-eol)))
6570               (mail-header-set-xref headers xref)))))))
6571
6572 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6573   "Find article ID and insert the summary line for that article.
6574 OLD-HEADER can either be a header or a line number to insert
6575 the subject line on.
6576 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6577 and OLD-HEADER will be used when the summary line is inserted,
6578 too, instead of trying to fetch new headers."
6579   (let* ((line (and (numberp old-header) old-header))
6580          (old-header (and (vectorp old-header) old-header))
6581          (header (cond ((and old-header use-old-header)
6582                         old-header)
6583                        ((and (numberp id)
6584                              (gnus-number-to-header id))
6585                         (gnus-number-to-header id))
6586                        (t
6587                         (gnus-read-header id))))
6588          (number (and (numberp id) id))
6589          d)
6590     (when header
6591       ;; Rebuild the thread that this article is part of and go to the
6592       ;; article we have fetched.
6593       (when (and (not gnus-show-threads)
6594                  old-header)
6595         (when (and number
6596                    (setq d (gnus-data-find (mail-header-number old-header))))
6597           (goto-char (gnus-data-pos d))
6598           (gnus-data-remove
6599            number
6600            (- (point-at-bol)
6601               (prog1
6602                   (1+ (point-at-eol))
6603                 (gnus-delete-line))))))
6604       ;; Remove list identifiers from subject.
6605       (let ((gnus-newsgroup-headers (list header)))
6606         (gnus-summary-remove-list-identifiers))
6607       (when old-header
6608         (mail-header-set-number header (mail-header-number old-header)))
6609       (setq gnus-newsgroup-sparse
6610             (delq (setq number (mail-header-number header))
6611                   gnus-newsgroup-sparse))
6612       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6613       (push number gnus-newsgroup-limit)
6614       (gnus-rebuild-thread (mail-header-id header) line)
6615       (gnus-summary-goto-subject number nil t))
6616     (when (and (numberp number)
6617                (> number 0))
6618       ;; We have to update the boundaries even if we can't fetch the
6619       ;; article if ID is a number -- so that the next `P' or `N'
6620       ;; command will fetch the previous (or next) article even
6621       ;; if the one we tried to fetch this time has been canceled.
6622       (when (> number gnus-newsgroup-end)
6623         (setq gnus-newsgroup-end number))
6624       (when (< number gnus-newsgroup-begin)
6625         (setq gnus-newsgroup-begin number))
6626       (setq gnus-newsgroup-unselected
6627             (delq number gnus-newsgroup-unselected)))
6628     ;; Report back a success?
6629     (and header (mail-header-number header))))
6630
6631 ;;; Process/prefix in the summary buffer
6632
6633 (defun gnus-summary-work-articles (n)
6634   "Return a list of articles to be worked upon.
6635 The prefix argument, the list of process marked articles, and the
6636 current article will be taken into consideration."
6637   (with-current-buffer gnus-summary-buffer
6638     (cond
6639      (n
6640       ;; A numerical prefix has been given.
6641       (setq n (prefix-numeric-value n))
6642       (let ((backward (< n 0))
6643             (n (abs (prefix-numeric-value n)))
6644             articles article)
6645         (save-excursion
6646           (while
6647               (and (> n 0)
6648                    (push (setq article (gnus-summary-article-number))
6649                          articles)
6650                    (if backward
6651                        (gnus-summary-find-prev nil article)
6652                      (gnus-summary-find-next nil article)))
6653             (decf n)))
6654         (nreverse articles)))
6655      ((and (gnus-region-active-p) (mark))
6656       (message "region active")
6657       ;; Work on the region between point and mark.
6658       (let ((max (max (point) (mark)))
6659             articles article)
6660         (save-excursion
6661           (goto-char (min (point) (mark)))
6662           (while
6663               (and
6664                (push (setq article (gnus-summary-article-number)) articles)
6665                (gnus-summary-find-next nil article)
6666                (< (point) max)))
6667           (nreverse articles))))
6668      (gnus-newsgroup-processable
6669       ;; There are process-marked articles present.
6670       ;; Save current state.
6671       (gnus-summary-save-process-mark)
6672       ;; Return the list.
6673       (reverse gnus-newsgroup-processable))
6674      (t
6675       ;; Just return the current article.
6676       (list (gnus-summary-article-number))))))
6677
6678 (defmacro gnus-summary-iterate (arg &rest forms)
6679   "Iterate over the process/prefixed articles and do FORMS.
6680 ARG is the interactive prefix given to the command.  FORMS will be
6681 executed with point over the summary line of the articles."
6682   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6683     `(let ((,articles (gnus-summary-work-articles ,arg)))
6684        (while ,articles
6685          (gnus-summary-goto-subject (car ,articles))
6686          ,@forms
6687          (pop ,articles)))))
6688
6689 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6690 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6691
6692 (defun gnus-summary-save-process-mark ()
6693   "Push the current set of process marked articles on the stack."
6694   (interactive)
6695   (push (copy-sequence gnus-newsgroup-processable)
6696         gnus-newsgroup-process-stack))
6697
6698 (defun gnus-summary-kill-process-mark ()
6699   "Push the current set of process marked articles on the stack and unmark."
6700   (interactive)
6701   (gnus-summary-save-process-mark)
6702   (gnus-summary-unmark-all-processable))
6703
6704 (defun gnus-summary-yank-process-mark ()
6705   "Pop the last process mark state off the stack and restore it."
6706   (interactive)
6707   (unless gnus-newsgroup-process-stack
6708     (error "Empty mark stack"))
6709   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6710
6711 (defun gnus-summary-process-mark-set (set)
6712   "Make SET into the current process marked articles."
6713   (gnus-summary-unmark-all-processable)
6714   (mapc 'gnus-summary-set-process-mark set))
6715
6716 ;;; Searching and stuff
6717
6718 (defun gnus-summary-search-group (&optional backward use-level)
6719   "Search for next unread newsgroup.
6720 If optional argument BACKWARD is non-nil, search backward instead."
6721   (with-current-buffer gnus-group-buffer
6722     (when (gnus-group-search-forward
6723            backward nil (if use-level (gnus-group-group-level) nil))
6724       (gnus-group-group-name))))
6725
6726 (defun gnus-summary-best-group (&optional exclude-group)
6727   "Find the name of the best unread group.
6728 If EXCLUDE-GROUP, do not go to this group."
6729   (with-current-buffer gnus-group-buffer
6730     (save-excursion
6731       (gnus-group-best-unread-group exclude-group))))
6732
6733 (defun gnus-summary-find-next (&optional unread article backward)
6734   (if backward
6735       (gnus-summary-find-prev unread article)
6736     (let* ((dummy (gnus-summary-article-intangible-p))
6737            (article (or article (gnus-summary-article-number)))
6738            (data (gnus-data-find-list article))
6739            result)
6740       (when (and (not dummy)
6741                  (or (not gnus-summary-check-current)
6742                      (not unread)
6743                      (not (gnus-data-unread-p (car data)))))
6744         (setq data (cdr data)))
6745       (when (setq result
6746                   (if unread
6747                       (progn
6748                         (while data
6749                           (unless (memq (gnus-data-number (car data))
6750                                         (cond
6751                                          ((eq gnus-auto-goto-ignores
6752                                               'always-undownloaded)
6753                                           gnus-newsgroup-undownloaded)
6754                                          (gnus-plugged
6755                                           nil)
6756                                          ((eq gnus-auto-goto-ignores
6757                                               'unfetched)
6758                                           gnus-newsgroup-unfetched)
6759                                          ((eq gnus-auto-goto-ignores
6760                                               'undownloaded)
6761                                           gnus-newsgroup-undownloaded)))
6762                             (when (gnus-data-unread-p (car data))
6763                               (setq result (car data)
6764                                     data nil)))
6765                           (setq data (cdr data)))
6766                         result)
6767                     (car data)))
6768         (goto-char (gnus-data-pos result))
6769         (gnus-data-number result)))))
6770
6771 (defun gnus-summary-find-prev (&optional unread article)
6772   (let* ((eobp (eobp))
6773          (article (or article (gnus-summary-article-number)))
6774          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6775          result)
6776     (when (and (not eobp)
6777                (or (not gnus-summary-check-current)
6778                    (not unread)
6779                    (not (gnus-data-unread-p (car data)))))
6780       (setq data (cdr data)))
6781     (when (setq result
6782                 (if unread
6783                     (progn
6784                       (while data
6785                         (unless (memq (gnus-data-number (car data))
6786                                       (cond
6787                                        ((eq gnus-auto-goto-ignores
6788                                             'always-undownloaded)
6789                                         gnus-newsgroup-undownloaded)
6790                                        (gnus-plugged
6791                                         nil)
6792                                        ((eq gnus-auto-goto-ignores
6793                                             'unfetched)
6794                                         gnus-newsgroup-unfetched)
6795                                        ((eq gnus-auto-goto-ignores
6796                                             'undownloaded)
6797                                         gnus-newsgroup-undownloaded)))
6798                           (when (gnus-data-unread-p (car data))
6799                             (setq result (car data)
6800                                   data nil)))
6801                         (setq data (cdr data)))
6802                       result)
6803                   (car data)))
6804       (goto-char (gnus-data-pos result))
6805       (gnus-data-number result))))
6806
6807 (defun gnus-summary-find-subject (subject &optional unread backward article)
6808   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6809          (article (or article (gnus-summary-article-number)))
6810          (articles (gnus-data-list backward))
6811          (arts (gnus-data-find-list article articles))
6812          result)
6813     (when (or (not gnus-summary-check-current)
6814               (not unread)
6815               (not (gnus-data-unread-p (car arts))))
6816       (setq arts (cdr arts)))
6817     (while arts
6818       (and (or (not unread)
6819                (gnus-data-unread-p (car arts)))
6820            (vectorp (gnus-data-header (car arts)))
6821            (gnus-subject-equal
6822             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6823            (setq result (car arts)
6824                  arts nil))
6825       (setq arts (cdr arts)))
6826     (and result
6827          (goto-char (gnus-data-pos result))
6828          (gnus-data-number result))))
6829
6830 (defun gnus-summary-search-forward (&optional unread subject backward)
6831   "Search forward for an article.
6832 If UNREAD, look for unread articles.  If SUBJECT, look for
6833 articles with that subject.  If BACKWARD, search backward instead."
6834   (cond (subject (gnus-summary-find-subject subject unread backward))
6835         (backward (gnus-summary-find-prev unread))
6836         (t (gnus-summary-find-next unread))))
6837
6838 (defun gnus-recenter (&optional n)
6839   "Center point in window and redisplay frame.
6840 Also do horizontal recentering."
6841   (interactive "P")
6842   (when (and gnus-auto-center-summary
6843              (not (eq gnus-auto-center-summary 'vertical)))
6844     (gnus-horizontal-recenter))
6845   (if (fboundp 'recenter-top-bottom)
6846       (recenter-top-bottom n)
6847     (recenter n)))
6848
6849 (put 'gnus-recenter 'isearch-scroll t)
6850
6851 (defun gnus-forward-line-ignore-invisible (n)
6852   "Move N lines forward (backward if N is negative).
6853 Like forward-line, but skip over (and don't count) invisible lines."
6854   (let (done)
6855     (while (and (> n 0) (not done))
6856       ;; If the following character is currently invisible,
6857       ;; skip all characters with that same `invisible' property value.
6858       (while (gnus-invisible-p (point))
6859         (goto-char (gnus-next-char-property-change (point))))
6860       (forward-line 1)
6861       (if (eobp)
6862           (setq done t)
6863         (setq n (1- n))))
6864     (while (and (< n 0) (not done))
6865       (forward-line -1)
6866       (if (bobp) (setq done t)
6867         (setq n (1+ n))
6868         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6869           (goto-char (gnus-previous-char-property-change (point))))))))
6870
6871 (defun gnus-summary-recenter ()
6872   "Center point in the summary window.
6873 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6874 displayed, no centering will be performed."
6875   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6876   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6877   (interactive)
6878   ;; The user has to want it.
6879   (when gnus-auto-center-summary
6880     (let* ((top (cond ((< (window-height) 4) 0)
6881                       ((< (window-height) 7) 1)
6882                       (t (if (numberp gnus-auto-center-summary)
6883                              gnus-auto-center-summary
6884                            (/ (1- (window-height)) 2)))))
6885            (height (1- (window-height)))
6886            (bottom (save-excursion
6887                      (goto-char (point-max))
6888                      (gnus-forward-line-ignore-invisible (- height))
6889                      (point)))
6890            (window (get-buffer-window (current-buffer))))
6891       (when (get-buffer-window gnus-article-buffer)
6892         ;; Only do recentering when the article buffer is displayed,
6893         ;; Set the window start to either `bottom', which is the biggest
6894         ;; possible valid number, or the second line from the top,
6895         ;; whichever is the least.
6896         (let ((top-pos (save-excursion
6897                          (gnus-forward-line-ignore-invisible (- top))
6898                          (point))))
6899           (if (> bottom top-pos)
6900               ;; Keep the second line from the top visible
6901               (set-window-start window top-pos)
6902             ;; Try to keep the bottom line visible; if it's partially
6903             ;; obscured, either scroll one more line to make it fully
6904             ;; visible, or revert to using TOP-POS.
6905             (save-excursion
6906               (goto-char (point-max))
6907               (gnus-forward-line-ignore-invisible -1)
6908               (let ((last-line-start (point)))
6909                 (goto-char bottom)
6910                 (set-window-start window (point) t)
6911                 (when (not (pos-visible-in-window-p last-line-start window))
6912                   (gnus-forward-line-ignore-invisible 1)
6913                   (set-window-start window (min (point) top-pos) t)))))))
6914       ;; Do horizontal recentering while we're at it.
6915       (when (and (get-buffer-window (current-buffer) t)
6916                  (not (eq gnus-auto-center-summary 'vertical)))
6917         (let ((selected (selected-window)))
6918           (select-window (get-buffer-window (current-buffer) t))
6919           (gnus-summary-position-point)
6920           (gnus-horizontal-recenter)
6921           (select-window selected))))))
6922
6923 (defun gnus-summary-jump-to-group (newsgroup)
6924   "Move point to NEWSGROUP in group mode buffer."
6925   ;; Keep update point of group mode buffer if visible.
6926   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6927       (save-window-excursion
6928         ;; Take care of tree window mode.
6929         (when (get-buffer-window gnus-group-buffer)
6930           (pop-to-buffer gnus-group-buffer))
6931         (gnus-group-jump-to-group newsgroup))
6932     (save-excursion
6933       ;; Take care of tree window mode.
6934       (if (get-buffer-window gnus-group-buffer 0)
6935           (pop-to-buffer gnus-group-buffer)
6936         (set-buffer gnus-group-buffer))
6937       (gnus-group-jump-to-group newsgroup))))
6938
6939 ;; This function returns a list of article numbers based on the
6940 ;; difference between the ranges of read articles in this group and
6941 ;; the range of active articles.
6942 (defun gnus-list-of-unread-articles (group)
6943   (let* ((read (gnus-info-read (gnus-get-info group)))
6944          (active (or (gnus-active group) (gnus-activate-group group)))
6945          (last (or (cdr active)
6946                    (error "Group %s couldn't be activated " group)))
6947          (bottom (if gnus-newsgroup-maximum-articles
6948                      (max (car active)
6949                           (- last gnus-newsgroup-maximum-articles -1))
6950                    (car active)))
6951          first nlast unread)
6952     ;; If none are read, then all are unread.
6953     (if (not read)
6954         (setq first bottom)
6955       ;; If the range of read articles is a single range, then the
6956       ;; first unread article is the article after the last read
6957       ;; article.  Sounds logical, doesn't it?
6958       (if (and (not (listp (cdr read)))
6959                (or (< (car read) bottom)
6960                    (progn (setq read (list read))
6961                           nil)))
6962           (setq first (max bottom (1+ (cdr read))))
6963         ;; `read' is a list of ranges.
6964         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6965                                   (caar read)))
6966                   1)
6967           (setq first bottom))
6968         (while read
6969           (when first
6970             (while (< first nlast)
6971               (setq unread (cons first unread)
6972                     first (1+ first))))
6973           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6974           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6975           (setq read (cdr read)))))
6976     ;; And add the last unread articles.
6977     (while (<= first last)
6978       (setq unread (cons first unread)
6979             first (1+ first)))
6980     ;; Return the list of unread articles.
6981     (delq 0 (nreverse unread))))
6982
6983 (defun gnus-list-of-read-articles (group)
6984   "Return a list of unread, unticked and non-dormant articles."
6985   (let* ((info (gnus-get-info group))
6986          (marked (gnus-info-marks info))
6987          (active (gnus-active group)))
6988     (and info active
6989          (gnus-list-range-difference
6990           (gnus-list-range-difference
6991            (gnus-sorted-complement
6992             (gnus-uncompress-range
6993              (if gnus-newsgroup-maximum-articles
6994                  (cons (max (car active)
6995                             (- (cdr active)
6996                                gnus-newsgroup-maximum-articles
6997                                -1))
6998                        (cdr active))
6999                active))
7000             (gnus-list-of-unread-articles group))
7001            (cdr (assq 'dormant marked)))
7002           (cdr (assq 'tick marked))))))
7003
7004 ;; This function returns a sequence of article numbers based on the
7005 ;; difference between the ranges of read articles in this group and
7006 ;; the range of active articles.
7007 (defun gnus-sequence-of-unread-articles (group)
7008   (let* ((read (gnus-info-read (gnus-get-info group)))
7009          (active (or (gnus-active group) (gnus-activate-group group)))
7010          (last (cdr active))
7011          (bottom (if gnus-newsgroup-maximum-articles
7012                      (max (car active)
7013                           (- last gnus-newsgroup-maximum-articles -1))
7014                    (car active)))
7015          first nlast unread)
7016     ;; If none are read, then all are unread.
7017     (if (not read)
7018         (setq first bottom)
7019       ;; If the range of read articles is a single range, then the
7020       ;; first unread article is the article after the last read
7021       ;; article.  Sounds logical, doesn't it?
7022       (if (and (not (listp (cdr read)))
7023                (or (< (car read) bottom)
7024                    (progn (setq read (list read))
7025                           nil)))
7026           (setq first (max bottom (1+ (cdr read))))
7027         ;; `read' is a list of ranges.
7028         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7029                                   (caar read)))
7030                   1)
7031           (setq first bottom))
7032         (while read
7033           (when first
7034             (push (cons first nlast) unread))
7035           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7036           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7037           (setq read (cdr read)))))
7038     ;; And add the last unread articles.
7039     (cond ((not (and first last))
7040            nil)
7041           ((< first last)
7042            (push (cons first last) unread))
7043           ((= first last)
7044            (push first unread)))
7045     ;; Return the sequence of unread articles.
7046     (delq 0 (nreverse unread))))
7047
7048 ;; Various summary commands
7049
7050 (defun gnus-summary-select-article-buffer ()
7051   "Reconfigure windows to show the article buffer.
7052 If `gnus-widen-article-window' is set, show only the article
7053 buffer."
7054   (interactive)
7055   (if (not (gnus-buffer-live-p gnus-article-buffer))
7056       (error "There is no article buffer for this summary buffer")
7057     (unless (get-buffer-window gnus-article-buffer)
7058       (gnus-summary-show-article))
7059     (gnus-configure-windows
7060      (if gnus-widen-article-window
7061          'only-article
7062        'article)
7063      t)
7064     (select-window (get-buffer-window gnus-article-buffer))))
7065
7066 (defun gnus-summary-universal-argument (arg)
7067   "Perform any operation on all articles that are process/prefixed."
7068   (interactive "P")
7069   (let ((articles (gnus-summary-work-articles arg))
7070         func article)
7071     (if (eq
7072          (setq
7073           func
7074           (key-binding
7075            (read-key-sequence
7076             (substitute-command-keys
7077              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7078          'undefined)
7079         (gnus-error 1 "Undefined key")
7080       (save-excursion
7081         (while articles
7082           (gnus-summary-goto-subject (setq article (pop articles)))
7083           (let (gnus-newsgroup-processable)
7084             (command-execute func))
7085           (gnus-summary-remove-process-mark article)))))
7086   (gnus-summary-position-point))
7087
7088 (defun gnus-summary-toggle-truncation (&optional arg)
7089   "Toggle truncation of summary lines.
7090 With ARG, turn line truncation on if ARG is positive."
7091   (interactive "P")
7092   (setq truncate-lines
7093         (if (null arg) (not truncate-lines)
7094           (> (prefix-numeric-value arg) 0)))
7095   (redraw-display))
7096
7097 (defun gnus-summary-find-for-reselect ()
7098   "Return the number of an article to stay on across a reselect.
7099 The current article is considered, then following articles, then previous
7100 articles.  An article is sought which is not cancelled and isn't a temporary
7101 insertion from another group.  If there's no such then return a dummy 0."
7102   (let (found)
7103     (dolist (rev '(nil t))
7104       (unless found      ; don't demand the reverse list if we don't need it
7105         (let ((data (gnus-data-find-list
7106                      (gnus-summary-article-number) (gnus-data-list rev))))
7107           (while (and data (not found))
7108             (if (and (< 0 (gnus-data-number (car data)))
7109                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7110                 (setq found (gnus-data-number (car data))))
7111             (setq data (cdr data))))))
7112     (or found 0)))
7113
7114 (defun gnus-summary-reselect-current-group (&optional all rescan)
7115   "Exit and then reselect the current newsgroup.
7116 The prefix argument ALL means to select all articles."
7117   (interactive "P")
7118   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7119     (error "Ephemeral groups can't be reselected"))
7120   (let ((current-subject (gnus-summary-find-for-reselect))
7121         (group gnus-newsgroup-name))
7122     (setq gnus-newsgroup-begin nil)
7123     (gnus-summary-exit nil 'leave-hidden)
7124     ;; We have to adjust the point of group mode buffer because
7125     ;; point was moved to the next unread newsgroup by exiting.
7126     (gnus-summary-jump-to-group group)
7127     (when rescan
7128       (save-excursion
7129         (gnus-group-get-new-news-this-group 1)))
7130     (gnus-group-read-group all t)
7131     (gnus-summary-goto-subject current-subject nil t)))
7132
7133 (defun gnus-summary-rescan-group (&optional all)
7134   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7135   (interactive "P")
7136   (let ((config gnus-current-window-configuration))
7137     (gnus-summary-reselect-current-group all t)
7138     (gnus-configure-windows config)
7139     (when (eq config 'article)
7140       (gnus-summary-select-article))))
7141
7142 (defun gnus-summary-update-info (&optional non-destructive)
7143   (save-excursion
7144     (let ((group gnus-newsgroup-name))
7145       (when group
7146         (when gnus-newsgroup-kill-headers
7147           (setq gnus-newsgroup-killed
7148                 (gnus-compress-sequence
7149                  (gnus-sorted-union
7150                   (gnus-list-range-intersection
7151                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7152                   gnus-newsgroup-unreads)
7153                  t)))
7154         (unless (listp (cdr gnus-newsgroup-killed))
7155           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7156         (let ((headers gnus-newsgroup-headers)
7157               (ephemeral-p (gnus-ephemeral-group-p group))
7158               info)
7159           (unless ephemeral-p
7160             (setq info (copy-sequence (gnus-get-info group))
7161                   info (delq (gnus-info-params info) info)))
7162           ;; Set the new ranges of read articles.
7163           (with-current-buffer gnus-group-buffer
7164             (gnus-undo-force-boundary))
7165           (gnus-update-read-articles
7166            group (gnus-sorted-union
7167                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7168           ;; Set the current article marks.
7169           (let ((gnus-newsgroup-scored
7170                  (if (and (not gnus-save-score)
7171                           (not non-destructive))
7172                      nil
7173                    gnus-newsgroup-scored)))
7174             (save-excursion
7175               (gnus-update-marks)))
7176           ;; Do the cross-ref thing.
7177           (when gnus-use-cross-reference
7178             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7179           ;; Do not switch windows but change the buffer to work.
7180           (set-buffer gnus-group-buffer)
7181           (unless ephemeral-p
7182             (gnus-group-update-group
7183              group nil
7184              (equal info
7185                     (setq info (copy-sequence (gnus-get-info group))
7186                           info (delq (gnus-info-params info) info))))))))))
7187
7188 (defun gnus-summary-save-newsrc (&optional force)
7189   "Save the current number of read/marked articles in the dribble buffer.
7190 The dribble buffer will then be saved.
7191 If FORCE (the prefix), also save the .newsrc file(s)."
7192   (interactive "P")
7193   (gnus-summary-update-info t)
7194   (if force
7195       (gnus-save-newsrc-file)
7196     (gnus-dribble-save)))
7197
7198 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7199
7200 (defun gnus-summary-exit (&optional temporary leave-hidden)
7201   "Exit reading current newsgroup, and then return to group selection mode.
7202 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7203   (interactive)
7204   (gnus-set-global-variables)
7205   (when (gnus-buffer-live-p gnus-article-buffer)
7206     (with-current-buffer gnus-article-buffer
7207       (mm-destroy-parts gnus-article-mime-handles)
7208       ;; Set it to nil for safety reason.
7209       (setq gnus-article-mime-handle-alist nil)
7210       (setq gnus-article-mime-handles nil)))
7211   (gnus-kill-save-kill-buffer)
7212   (gnus-async-halt-prefetch)
7213   (let* ((group gnus-newsgroup-name)
7214          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7215          (gnus-group-is-exiting-p t)
7216          (article-buffer gnus-article-buffer)
7217          (mode major-mode)
7218          (group-point nil)
7219          (buf (current-buffer))
7220          ;; `gnus-single-article-buffer' is nil buffer-locally in
7221          ;; ephemeral group of which summary buffer will be killed,
7222          ;; but the global value may be non-nil.
7223          (single-article-buffer gnus-single-article-buffer))
7224     (unless quit-config
7225       ;; Do adaptive scoring, and possibly save score files.
7226       (when gnus-newsgroup-adaptive
7227         (gnus-score-adaptive))
7228       (when gnus-use-scoring
7229         (gnus-score-save)))
7230     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7231     (when gnus-use-cache
7232       (gnus-cache-possibly-remove-articles)
7233       (gnus-cache-save-buffers))
7234     (gnus-async-prefetch-remove-group group)
7235     (when gnus-suppress-duplicates
7236       (gnus-dup-enter-articles))
7237     (when gnus-use-trees
7238       (gnus-tree-close group))
7239     (when gnus-use-cache
7240       (gnus-cache-write-active))
7241     ;; Remove entries for this group.
7242     (nnmail-purge-split-history (gnus-group-real-name group))
7243     ;; Make all changes in this group permanent.
7244     (unless quit-config
7245       (gnus-run-hooks 'gnus-exit-group-hook)
7246       (gnus-summary-update-info))
7247     (gnus-close-group group)
7248     ;; Make sure where we were, and go to next newsgroup.
7249     (set-buffer gnus-group-buffer)
7250     (unless quit-config
7251       (gnus-group-jump-to-group group))
7252     (gnus-run-hooks 'gnus-summary-exit-hook)
7253     (unless (or quit-config
7254                 (not gnus-summary-next-group-on-exit)
7255                 ;; If this group has disappeared from the summary
7256                 ;; buffer, don't skip forwards.
7257                 (not (string= group (gnus-group-group-name))))
7258       (gnus-group-next-unread-group 1))
7259     (setq group-point (point))
7260     (if temporary
7261         nil                             ;Nothing to do.
7262       (set-buffer buf)
7263       (if (not gnus-kill-summary-on-exit)
7264           (progn
7265             (gnus-deaden-summary)
7266             (setq mode nil))
7267         (when (get-buffer gnus-article-buffer)
7268           (bury-buffer gnus-article-buffer))
7269         ;; Return to group mode buffer.
7270         (when (eq mode 'gnus-summary-mode)
7271           (gnus-kill-buffer buf)))
7272
7273       (setq gnus-current-select-method gnus-select-method)
7274       (set-buffer gnus-group-buffer)
7275       (if quit-config
7276           (gnus-handle-ephemeral-exit quit-config)
7277         (goto-char group-point)
7278         ;; If gnus-group-buffer is already displayed, make sure we also move
7279         ;; the cursor in the window that displays it.
7280         (let ((win (get-buffer-window (current-buffer) 0)))
7281           (if win (set-window-point win (point))))
7282         (unless leave-hidden
7283           (gnus-configure-windows 'group 'force)))
7284
7285       ;; If we have several article buffers, we kill them at exit.
7286       (unless single-article-buffer
7287         (when (gnus-buffer-live-p article-buffer)
7288           (with-current-buffer article-buffer
7289             ;; Don't kill sticky article buffers
7290             (unless (eq major-mode 'gnus-sticky-article-mode)
7291               (gnus-kill-buffer article-buffer)
7292               (setq gnus-article-current nil))))
7293         (gnus-kill-buffer gnus-original-article-buffer))
7294
7295       ;; Clear the current group name.
7296       (unless quit-config
7297         (setq gnus-newsgroup-name nil)))))
7298
7299 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7300 (defun gnus-summary-exit-no-update (&optional no-questions)
7301   "Quit reading current newsgroup without updating read article info."
7302   (interactive)
7303   (let* ((group gnus-newsgroup-name)
7304          (gnus-group-is-exiting-p t)
7305          (gnus-group-is-exiting-without-update-p t)
7306          (quit-config (gnus-group-quit-config group)))
7307     (when (or no-questions
7308               gnus-expert-user
7309               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7310       (gnus-async-halt-prefetch)
7311       (run-hooks 'gnus-summary-prepare-exit-hook)
7312       (when (gnus-buffer-live-p gnus-article-buffer)
7313         (with-current-buffer gnus-article-buffer
7314           (gnus-article-stop-animations)
7315           (mm-destroy-parts gnus-article-mime-handles)
7316           ;; Set it to nil for safety reason.
7317           (setq gnus-article-mime-handle-alist nil)
7318           (setq gnus-article-mime-handles nil)))
7319       ;; If we have several article buffers, we kill them at exit.
7320       (unless gnus-single-article-buffer
7321         (gnus-kill-buffer gnus-article-buffer)
7322         (gnus-kill-buffer gnus-original-article-buffer)
7323         (setq gnus-article-current nil))
7324       ;; Return to the group buffer.
7325       (gnus-configure-windows 'group 'force)
7326       (if (not gnus-kill-summary-on-exit)
7327           (gnus-deaden-summary)
7328         (gnus-close-group group)
7329         (gnus-kill-buffer gnus-summary-buffer))
7330       (unless gnus-single-article-buffer
7331         (setq gnus-article-current nil))
7332       (when gnus-use-trees
7333         (gnus-tree-close group))
7334       (gnus-async-prefetch-remove-group group)
7335       (when (get-buffer gnus-article-buffer)
7336         (bury-buffer gnus-article-buffer))
7337       ;; Clear the current group name.
7338       (setq gnus-newsgroup-name nil)
7339       (unless (gnus-ephemeral-group-p group)
7340         (gnus-group-update-group group nil t))
7341       (when (equal (gnus-group-group-name) group)
7342         (gnus-group-next-unread-group 1))
7343       (when quit-config
7344         (gnus-handle-ephemeral-exit quit-config)))))
7345
7346 (defun gnus-handle-ephemeral-exit (quit-config)
7347   "Handle movement when leaving an ephemeral group.
7348 The state which existed when entering the ephemeral is reset."
7349   (if (not (buffer-name (car quit-config)))
7350       (gnus-configure-windows 'group 'force)
7351     (set-buffer (car quit-config))
7352     (unless (eq (cdr quit-config) 'group)
7353       (setq gnus-current-select-method
7354             (gnus-find-method-for-group gnus-newsgroup-name)))
7355     (cond ((eq major-mode 'gnus-summary-mode)
7356            (gnus-set-global-variables))
7357           ((eq major-mode 'gnus-article-mode)
7358            (save-current-buffer
7359              ;; The `gnus-summary-buffer' variable may point
7360              ;; to the old summary buffer when using a single
7361              ;; article buffer.
7362              (unless (gnus-buffer-live-p gnus-summary-buffer)
7363                (set-buffer gnus-group-buffer))
7364              (set-buffer gnus-summary-buffer)
7365              (gnus-set-global-variables))))
7366     (if (or (eq (cdr quit-config) 'article)
7367             (eq (cdr quit-config) 'pick))
7368         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7369             (gnus-configure-windows 'pick 'force)
7370           (gnus-configure-windows (cdr quit-config) 'force))
7371       (gnus-configure-windows (cdr quit-config) 'force))
7372     (when (eq major-mode 'gnus-summary-mode)
7373       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7374                                                      next-unread-noselect))
7375           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7376                                   'next-noselect)
7377                               (gnus-summary-next-subject 1 nil t))
7378                              ((eq gnus-auto-select-on-ephemeral-exit
7379                                   'next-unread-noselect)
7380                               (gnus-summary-next-subject 1 t t))))
7381             ;; Hide the article buffer which displays the article different
7382             ;; from the one that the cursor points to in the summary buffer.
7383             (gnus-configure-windows 'summary 'force))
7384         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7385                (gnus-summary-next-subject 1))
7386               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7387                (gnus-summary-next-subject 1 t))))
7388       (gnus-summary-recenter)
7389       (gnus-summary-position-point))))
7390
7391 ;;; Dead summaries.
7392
7393 (defvar gnus-dead-summary-mode-map
7394   (let ((map (make-keymap)))
7395     (suppress-keymap map)
7396     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7397     (dolist (key '("\C-d" "\r" "\177" [delete]))
7398       (define-key map key 'gnus-summary-wake-up-the-dead))
7399     (dolist (key '("q" "Q"))
7400       (define-key map key 'bury-buffer))
7401     map))
7402
7403 (define-minor-mode gnus-dead-summary-mode
7404   "Minor mode for Gnus summary buffers."
7405   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7406   (unless (derived-mode-p 'gnus-summary-mode)
7407     (setq gnus-dead-summary-mode nil)))
7408
7409 (defun gnus-deaden-summary ()
7410   "Make the current summary buffer into a dead summary buffer."
7411   ;; Kill any previous dead summary buffer.
7412   (when (and gnus-dead-summary
7413              (buffer-name gnus-dead-summary))
7414     (with-current-buffer gnus-dead-summary
7415       (when gnus-dead-summary-mode
7416         (kill-buffer (current-buffer)))))
7417   ;; Make this the current dead summary.
7418   (setq gnus-dead-summary (current-buffer))
7419   (gnus-dead-summary-mode 1)
7420   (let ((name (buffer-name)))
7421     (when (string-match "Summary" name)
7422       (rename-buffer
7423        (concat (substring name 0 (match-beginning 0)) "Dead "
7424                (substring name (match-beginning 0)))
7425        t)
7426       (bury-buffer))))
7427
7428 (defun gnus-kill-or-deaden-summary (buffer)
7429   "Kill or deaden the summary BUFFER."
7430   (save-excursion
7431     (when (and (buffer-name buffer)
7432                (not gnus-single-article-buffer))
7433       (with-current-buffer buffer
7434         (gnus-kill-buffer gnus-article-buffer)
7435         (gnus-kill-buffer gnus-original-article-buffer)))
7436     (cond
7437      ;; Kill the buffer.
7438      (gnus-kill-summary-on-exit
7439       (when (and gnus-use-trees
7440                  (gnus-buffer-exists-p buffer))
7441         (with-current-buffer buffer
7442           (gnus-tree-close gnus-newsgroup-name)))
7443       (gnus-kill-buffer buffer))
7444      ;; Deaden the buffer.
7445      ((gnus-buffer-exists-p buffer)
7446       (with-current-buffer buffer
7447         (gnus-deaden-summary))))))
7448
7449 (defun gnus-summary-wake-up-the-dead (&rest args)
7450   "Wake up the dead summary buffer."
7451   (interactive)
7452   (gnus-dead-summary-mode -1)
7453   (let ((name (buffer-name)))
7454     (when (string-match "Dead " name)
7455       (rename-buffer
7456        (concat (substring name 0 (match-beginning 0))
7457                (substring name (match-end 0)))
7458        t)))
7459   (gnus-message 3 "This dead summary is now alive again"))
7460
7461 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7462 (defun gnus-summary-describe-group (&optional force)
7463   "Describe the current newsgroup."
7464   (interactive "P")
7465   (gnus-group-describe-group force gnus-newsgroup-name))
7466
7467 (defun gnus-summary-describe-briefly ()
7468   "Describe summary mode commands briefly."
7469   (interactive)
7470   (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")))
7471
7472 ;; Walking around group mode buffer from summary mode.
7473
7474 (defun gnus-summary-next-group (&optional no-article target-group backward)
7475   "Exit current newsgroup and then select next unread newsgroup.
7476 If prefix argument NO-ARTICLE is non-nil, no article is selected
7477 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7478 previous group instead."
7479   (interactive "P")
7480   ;; Stop pre-fetching.
7481   (gnus-async-halt-prefetch)
7482   (let ((current-group gnus-newsgroup-name)
7483         (current-buffer (current-buffer))
7484         entered)
7485     ;; First we semi-exit this group to update Xrefs and all variables.
7486     ;; We can't do a real exit, because the window conf must remain
7487     ;; the same in case the user is prompted for info, and we don't
7488     ;; want the window conf to change before that...
7489     (gnus-summary-exit t)
7490     (while (not entered)
7491       ;; Then we find what group we are supposed to enter.
7492       (set-buffer gnus-group-buffer)
7493       (gnus-group-jump-to-group current-group)
7494       (setq target-group
7495             (or target-group
7496                 (if (eq gnus-keep-same-level 'best)
7497                     (gnus-summary-best-group gnus-newsgroup-name)
7498                   (gnus-summary-search-group backward gnus-keep-same-level))))
7499       (if (not target-group)
7500           ;; There are no further groups, so we return to the group
7501           ;; buffer.
7502           (progn
7503             (gnus-message 5 "Returning to the group buffer")
7504             (setq entered t)
7505             (when (gnus-buffer-live-p current-buffer)
7506               (set-buffer current-buffer)
7507               (gnus-summary-exit))
7508             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7509         ;; We try to enter the target group.
7510         (gnus-group-jump-to-group target-group)
7511         (let ((unreads (gnus-group-group-unread)))
7512           (if (and (or (eq t unreads)
7513                        (and unreads (not (zerop unreads))))
7514                    (gnus-summary-read-group
7515                     target-group nil no-article
7516                     (and (buffer-name current-buffer) current-buffer)
7517                     nil backward))
7518               (setq entered t)
7519             (setq current-group target-group
7520                   target-group nil)))))))
7521
7522 (defun gnus-summary-prev-group (&optional no-article)
7523   "Exit current newsgroup and then select previous unread newsgroup.
7524 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7525   (interactive "P")
7526   (gnus-summary-next-group no-article nil t))
7527
7528 ;; Walking around summary lines.
7529
7530 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7531   "Go to the first subject satisfying any non-nil constraint.
7532 If UNREAD is non-nil, the article should be unread.
7533 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7534 If UNSEEN is non-nil, the article should be unseen as well as unread.
7535 Returns the article selected or nil if there are no matching articles."
7536   (interactive "P")
7537   (cond
7538    ;; Empty summary.
7539    ((null gnus-newsgroup-data)
7540     (gnus-message 3 "No articles in the group")
7541     nil)
7542    ;; Pick the first article.
7543    ((not (or unread undownloaded unseen))
7544     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7545     (gnus-data-number (car gnus-newsgroup-data)))
7546    ;; Find the first unread article.
7547    (t
7548     (let ((data gnus-newsgroup-data))
7549       (while (and data
7550                   (let ((num (gnus-data-number (car data))))
7551                     (or (memq num gnus-newsgroup-unfetched)
7552                         (not (or (and unread
7553                                       (memq num gnus-newsgroup-unreads))
7554                                  (and undownloaded
7555                                       (memq num gnus-newsgroup-undownloaded))
7556                                  (and unseen
7557                                       (memq num gnus-newsgroup-unseen)
7558                                       (memq num gnus-newsgroup-unreads)))))))
7559         (setq data (cdr data)))
7560       (prog1
7561           (if data
7562               (progn
7563                 (goto-char (gnus-data-pos (car data)))
7564                 (gnus-data-number (car data)))
7565             (gnus-message 3 "No more%s articles"
7566                           (let* ((r (when unread " unread"))
7567                                  (d (when undownloaded " undownloaded"))
7568                                  (s (when unseen " unseen"))
7569                                  (l (delq nil (list r d s))))
7570                             (cond ((= 3 (length l))
7571                                    (concat r "," d ", or" s))
7572                                   ((= 2 (length l))
7573                                    (concat (car l) ", or" (cadr l)))
7574                                   ((= 1 (length l))
7575                                    (car l))
7576                                   (t
7577                                    ""))))
7578             nil
7579             )
7580         (gnus-summary-position-point))))))
7581
7582 (defun gnus-summary-next-subject (n &optional unread dont-display)
7583   "Go to next N'th summary line.
7584 If N is negative, go to the previous N'th subject line.
7585 If UNREAD is non-nil, only unread articles are selected.
7586 The difference between N and the actual number of steps taken is
7587 returned."
7588   (interactive "p")
7589   (let ((backward (< n 0))
7590         (n (abs n)))
7591     (while (and (> n 0)
7592                 (if backward
7593                     (gnus-summary-find-prev unread)
7594                   (gnus-summary-find-next unread)))
7595       (unless (zerop (setq n (1- n)))
7596         (gnus-summary-show-thread)))
7597     (when (/= 0 n)
7598       (gnus-message 7 "No more%s articles"
7599                     (if unread " unread" "")))
7600     (unless dont-display
7601       (gnus-summary-recenter)
7602       (gnus-summary-position-point))
7603     n))
7604
7605 (defun gnus-summary-next-unread-subject (n)
7606   "Go to next N'th unread summary line."
7607   (interactive "p")
7608   (gnus-summary-next-subject n t))
7609
7610 (defun gnus-summary-prev-subject (n &optional unread)
7611   "Go to previous N'th summary line.
7612 If optional argument UNREAD is non-nil, only unread article is selected."
7613   (interactive "p")
7614   (gnus-summary-next-subject (- n) unread))
7615
7616 (defun gnus-summary-prev-unread-subject (n)
7617   "Go to previous N'th unread summary line."
7618   (interactive "p")
7619   (gnus-summary-next-subject (- n) t))
7620
7621 (defun gnus-summary-goto-subjects (articles)
7622   "Insert the subject header for ARTICLES in the current buffer."
7623   (save-excursion
7624     (dolist (article articles)
7625       (gnus-summary-goto-subject article t)))
7626   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7627   (gnus-summary-position-point))
7628
7629 (defun gnus-summary-goto-subject (article &optional force silent)
7630   "Go to the subject line of ARTICLE.
7631 If FORCE, also allow jumping to articles not currently shown."
7632   (interactive "nArticle number: ")
7633   (unless (numberp article)
7634     (error "Article %s is not a number" article))
7635   (let ((b (point))
7636         (data (gnus-data-find article)))
7637     ;; We read in the article if we have to.
7638     (and (not data)
7639          force
7640          (gnus-summary-insert-subject
7641           article
7642           (if (or (numberp force) (vectorp force)) force)
7643           t)
7644          (setq data (gnus-data-find article)))
7645     (goto-char b)
7646     (if (not data)
7647         (progn
7648           (unless silent
7649             (gnus-message 3 "Can't find article %d" article))
7650           nil)
7651       (let ((pt (gnus-data-pos data)))
7652         (goto-char pt)
7653         (gnus-summary-set-article-display-arrow pt))
7654       (gnus-summary-position-point)
7655       article)))
7656
7657 ;; Walking around summary lines with displaying articles.
7658
7659 (defun gnus-summary-expand-window (&optional arg)
7660   "Make the summary buffer take up the entire Emacs frame.
7661 Given a prefix, will force an `article' buffer configuration."
7662   (interactive "P")
7663   (if arg
7664       (gnus-configure-windows 'article 'force)
7665     (gnus-configure-windows 'summary 'force)))
7666
7667 (defun gnus-summary-display-article (article &optional all-header)
7668   "Display ARTICLE in article buffer."
7669   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7670                (with-current-buffer gnus-article-buffer
7671                  (eq major-mode 'gnus-article-mode)))
7672     (gnus-article-setup-buffer))
7673   (gnus-set-global-variables)
7674   (with-current-buffer gnus-article-buffer
7675     (setq gnus-article-charset gnus-newsgroup-charset)
7676     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7677     (mm-enable-multibyte))
7678   (if (null article)
7679       nil
7680     (prog1
7681         (if gnus-summary-display-article-function
7682             (funcall gnus-summary-display-article-function article all-header)
7683           (gnus-article-prepare article all-header))
7684       (gnus-run-hooks 'gnus-select-article-hook)
7685       (when (and gnus-current-article
7686                  (not (zerop gnus-current-article)))
7687         (gnus-summary-goto-subject gnus-current-article))
7688       (gnus-summary-recenter)
7689       (when (and gnus-use-trees gnus-show-threads)
7690         (gnus-possibly-generate-tree article)
7691         (gnus-highlight-selected-tree article))
7692       ;; Successfully display article.
7693       (gnus-article-set-window-start
7694        (cdr (assq article gnus-newsgroup-bookmarks))))))
7695
7696 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7697   "Select the current article.
7698 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7699 non-nil, the article will be re-fetched even if it already present in
7700 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7701 be displayed."
7702   ;; Make sure we are in the summary buffer to work around bbdb bug.
7703   (unless (eq major-mode 'gnus-summary-mode)
7704     (set-buffer gnus-summary-buffer))
7705   (let ((article (or article (gnus-summary-article-number)))
7706         (all-headers (not (not all-headers))) ;Must be t or nil.
7707         gnus-summary-display-article-function)
7708     (and (not pseudo)
7709          (gnus-summary-article-pseudo-p article)
7710          (error "This is a pseudo-article"))
7711     (with-current-buffer gnus-summary-buffer
7712       (if (or (and gnus-single-article-buffer
7713                    (or (null gnus-current-article)
7714                        (null gnus-article-current)
7715                        (null (get-buffer gnus-article-buffer))
7716                        (not (eq article (cdr gnus-article-current)))
7717                        (not (equal (car gnus-article-current)
7718                                    gnus-newsgroup-name))
7719                        (not (get-buffer gnus-original-article-buffer))))
7720               (and (not gnus-single-article-buffer)
7721                    (or (null gnus-current-article)
7722                        (not (get-buffer gnus-original-article-buffer))
7723                        (not (eq gnus-current-article article))))
7724               force)
7725           ;; The requested article is different from the current article.
7726           (progn
7727             (gnus-summary-display-article article all-headers)
7728             (when (gnus-buffer-live-p gnus-article-buffer)
7729               (with-current-buffer gnus-article-buffer
7730                 (if (not gnus-article-decoded-p) ;; a local variable
7731                     (mm-disable-multibyte))))
7732             (gnus-article-set-window-start
7733              (cdr (assq article gnus-newsgroup-bookmarks)))
7734             article)
7735         'old))))
7736
7737 (defun gnus-summary-force-verify-and-decrypt ()
7738   "Display buttons for signed/encrypted parts and verify/decrypt them."
7739   (interactive)
7740   (let ((mm-verify-option 'known)
7741         (mm-decrypt-option 'known)
7742         (gnus-article-emulate-mime t)
7743         (gnus-buttonized-mime-types (append (list "multipart/signed"
7744                                                   "multipart/encrypted")
7745                                             gnus-buttonized-mime-types)))
7746     (gnus-summary-select-article nil 'force)))
7747
7748 (defun gnus-summary-set-current-mark (&optional current-mark)
7749   "Obsolete function."
7750   nil)
7751
7752 (defun gnus-summary-next-article (&optional unread subject backward push)
7753   "Select the next article.
7754 If UNREAD, only unread articles are selected.
7755 If SUBJECT, only articles with SUBJECT are selected.
7756 If BACKWARD, the previous article is selected instead of the next."
7757   (interactive "P")
7758   ;; Make sure we are in the summary buffer.
7759   (unless (eq major-mode 'gnus-summary-mode)
7760     (set-buffer gnus-summary-buffer))
7761   (cond
7762    ;; Is there such an article?
7763    ((and (gnus-summary-search-forward unread subject backward)
7764          (or (gnus-summary-display-article (gnus-summary-article-number))
7765              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7766     (gnus-summary-position-point))
7767    ;; If not, we try the first unread, if that is wanted.
7768    ((and subject
7769          gnus-auto-select-same
7770          (gnus-summary-first-unread-article))
7771     (gnus-summary-position-point)
7772     (gnus-message 6 "Wrapped"))
7773    ;; Try to get next/previous article not displayed in this group.
7774    ((and gnus-auto-extend-newsgroup
7775          (not unread) (not subject))
7776     (gnus-summary-goto-article
7777      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7778      nil (count-lines (point-min) (point))))
7779    ;; Go to next/previous group.
7780    (t
7781     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7782       (gnus-summary-jump-to-group gnus-newsgroup-name))
7783     (let ((cmd (if (featurep 'xemacs)
7784                    last-command-char
7785                  last-command-event))
7786           (point
7787            (with-current-buffer gnus-group-buffer
7788              (point)))
7789           (current-summary (current-buffer))
7790           (group
7791            (if (eq gnus-keep-same-level 'best)
7792                (gnus-summary-best-group gnus-newsgroup-name)
7793              (gnus-summary-search-group backward gnus-keep-same-level))))
7794       ;; Select next unread newsgroup automagically.
7795       (cond
7796        ((or (not gnus-auto-select-next)
7797             (not cmd))
7798         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7799        ((or (eq gnus-auto-select-next 'quietly)
7800             (and (eq gnus-auto-select-next 'slightly-quietly)
7801                  push)
7802             (and (eq gnus-auto-select-next 'almost-quietly)
7803                  (gnus-summary-last-article-p)))
7804         ;; Select quietly.
7805         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7806             (gnus-summary-exit)
7807           (gnus-message 7 "No more%s articles (%s)..."
7808                         (if unread " unread" "")
7809                         (if group (concat "selecting " group)
7810                           "exiting"))
7811           (gnus-summary-next-group nil group backward)))
7812        (t
7813         (when (gnus-key-press-event-p last-input-event)
7814           ;; Somehow or other, we may now have selected a different
7815           ;; window.  Make point go back to the summary buffer.
7816           (when (eq current-summary (current-buffer))
7817             ;; FIXME: This burps when get-buffer-window returns nil.
7818             (select-window (get-buffer-window current-summary 0)))
7819           (gnus-summary-walk-group-buffer
7820            gnus-newsgroup-name cmd unread backward point))))))))
7821
7822 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7823   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7824                       (?\C-p (gnus-group-prev-unread-group 1))))
7825         (cursor-in-echo-area t)
7826         keve key group ended prompt)
7827     (with-current-buffer gnus-group-buffer
7828       (goto-char start)
7829       (setq group
7830             (if (eq gnus-keep-same-level 'best)
7831                 (gnus-summary-best-group gnus-newsgroup-name)
7832               (gnus-summary-search-group backward gnus-keep-same-level))))
7833     (while (not ended)
7834       (setq prompt
7835             (format
7836              "No more%s articles%s " (if unread " unread" "")
7837              (if (and group
7838                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7839                  (format " (Type %s for %s [%s])"
7840                          (single-key-description cmd)
7841                          (gnus-group-decoded-name group)
7842                          (gnus-group-unread group))
7843                (format " (Type %s to exit %s)"
7844                        (single-key-description cmd)
7845                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7846       ;; Confirm auto selection.
7847       (setq key (car (setq keve (gnus-read-event-char prompt)))
7848             ended t)
7849       (cond
7850        ((assq key keystrokes)
7851         (let ((obuf (current-buffer)))
7852           (switch-to-buffer gnus-group-buffer)
7853           (when group
7854             (gnus-group-jump-to-group group))
7855           (eval (cadr (assq key keystrokes)))
7856           (setq group (gnus-group-group-name))
7857           (switch-to-buffer obuf))
7858         (setq ended nil))
7859        ((equal key cmd)
7860         (if (or (not group)
7861                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7862             (gnus-summary-exit)
7863           (gnus-summary-next-group nil group backward)))
7864        (t
7865         (push (cdr keve) unread-command-events))))))
7866
7867 (defun gnus-summary-next-unread-article ()
7868   "Select unread article after current one."
7869   (interactive)
7870   (gnus-summary-next-article
7871    (or (not (eq gnus-summary-goto-unread 'never))
7872        (gnus-summary-last-article-p (gnus-summary-article-number)))
7873    (and gnus-auto-select-same
7874         (gnus-summary-article-subject))))
7875
7876 (defun gnus-summary-prev-article (&optional unread subject)
7877   "Select the article before the current one.
7878 If UNREAD is non-nil, only unread articles are selected."
7879   (interactive "P")
7880   (gnus-summary-next-article unread subject t))
7881
7882 (defun gnus-summary-prev-unread-article ()
7883   "Select unread article before current one."
7884   (interactive)
7885   (gnus-summary-prev-article
7886    (or (not (eq gnus-summary-goto-unread 'never))
7887        (gnus-summary-first-article-p (gnus-summary-article-number)))
7888    (and gnus-auto-select-same
7889         (gnus-summary-article-subject))))
7890
7891 (defun gnus-summary-next-page (&optional lines circular stop)
7892   "Show next page of the selected article.
7893 If at the end of the current article, select the next article.
7894 LINES says how many lines should be scrolled up.
7895
7896 If CIRCULAR is non-nil, go to the start of the article instead of
7897 selecting the next article when reaching the end of the current
7898 article.
7899
7900 If STOP is non-nil, just stop when reaching the end of the message.
7901
7902 Also see the variable `gnus-article-skip-boring'."
7903   (interactive "P")
7904   (setq gnus-summary-buffer (current-buffer))
7905   (gnus-set-global-variables)
7906   (let ((article (gnus-summary-article-number))
7907         (article-window (get-buffer-window gnus-article-buffer t))
7908         endp)
7909     ;; If the buffer is empty, we have no article.
7910     (unless article
7911       (error "No article to select"))
7912     (gnus-configure-windows 'article)
7913     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7914         (if (and (eq gnus-summary-goto-unread 'never)
7915                  (not (gnus-summary-last-article-p article)))
7916             (gnus-summary-next-article)
7917           (gnus-summary-next-unread-article))
7918       (if (or (null gnus-current-article)
7919               (null gnus-article-current)
7920               (/= article (cdr gnus-article-current))
7921               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7922           ;; Selected subject is different from current article's.
7923           (gnus-summary-display-article article)
7924         (when article-window
7925           (gnus-eval-in-buffer-window gnus-article-buffer
7926             (setq endp (or (gnus-article-next-page lines)
7927                            (gnus-article-only-boring-p))))
7928           (when endp
7929             (cond ((or stop gnus-summary-stop-at-end-of-message)
7930                    (gnus-message 3 "End of message"))
7931                   (circular
7932                    (gnus-summary-beginning-of-article))
7933                   (lines
7934                    (gnus-message 3 "End of message"))
7935                   ((null lines)
7936                    (if (and (eq gnus-summary-goto-unread 'never)
7937                             (not (gnus-summary-last-article-p article)))
7938                        (gnus-summary-next-article)
7939                      (gnus-summary-next-unread-article))))))))
7940     (gnus-summary-recenter)
7941     (gnus-summary-position-point)))
7942
7943 (defun gnus-summary-prev-page (&optional lines move)
7944   "Show previous page of selected article.
7945 Argument LINES specifies lines to be scrolled down.
7946 If MOVE, move to the previous unread article if point is at
7947 the beginning of the buffer."
7948   (interactive "P")
7949   (let ((article (gnus-summary-article-number))
7950         (article-window (get-buffer-window gnus-article-buffer t))
7951         endp)
7952     (gnus-configure-windows 'article)
7953     (if (or (null gnus-current-article)
7954             (null gnus-article-current)
7955             (/= article (cdr gnus-article-current))
7956             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7957         ;; Selected subject is different from current article's.
7958         (gnus-summary-display-article article)
7959       (gnus-summary-recenter)
7960       (when article-window
7961         (gnus-eval-in-buffer-window gnus-article-buffer
7962           (setq endp (gnus-article-prev-page lines)))
7963         (when (and move endp)
7964           (cond (lines
7965                  (gnus-message 3 "Beginning of message"))
7966                 ((null lines)
7967                  (if (and (eq gnus-summary-goto-unread 'never)
7968                           (not (gnus-summary-first-article-p article)))
7969                      (gnus-summary-prev-article)
7970                    (gnus-summary-prev-unread-article))))))))
7971   (gnus-summary-position-point))
7972
7973 (defun gnus-summary-prev-page-or-article (&optional lines)
7974   "Show previous page of selected article.
7975 Argument LINES specifies lines to be scrolled down.
7976 If at the beginning of the article, go to the next article."
7977   (interactive "P")
7978   (gnus-summary-prev-page lines t))
7979
7980 (defun gnus-summary-scroll-up (lines)
7981   "Scroll up (or down) one line current article.
7982 Argument LINES specifies lines to be scrolled up (or down if negative).
7983 If no article is selected, then the current article will be selected first."
7984   (interactive "p")
7985   (gnus-configure-windows 'article)
7986   (gnus-summary-show-thread)
7987   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7988     (gnus-eval-in-buffer-window gnus-article-buffer
7989       (cond ((> lines 0)
7990              (when (gnus-article-next-page lines)
7991                (gnus-message 3 "End of message")))
7992             ((< lines 0)
7993              (gnus-article-prev-page (- lines))))))
7994   (gnus-summary-recenter)
7995   (gnus-summary-position-point))
7996
7997 (defun gnus-summary-scroll-down (lines)
7998   "Scroll down (or up) one line current article.
7999 Argument LINES specifies lines to be scrolled down (or up if negative).
8000 If no article is selected, then the current article will be selected first."
8001   (interactive "p")
8002   (gnus-summary-scroll-up (- lines)))
8003
8004 (defun gnus-summary-next-same-subject ()
8005   "Select next article which has the same subject as current one."
8006   (interactive)
8007   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8008
8009 (defun gnus-summary-prev-same-subject ()
8010   "Select previous article which has the same subject as current one."
8011   (interactive)
8012   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8013
8014 (defun gnus-summary-next-unread-same-subject ()
8015   "Select next unread article which has the same subject as current one."
8016   (interactive)
8017   (gnus-summary-next-article t (gnus-summary-article-subject)))
8018
8019 (defun gnus-summary-prev-unread-same-subject ()
8020   "Select previous unread article which has the same subject as current one."
8021   (interactive)
8022   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8023
8024 (defun gnus-summary-first-unread-article ()
8025   "Select the first unread article.
8026 Return nil if there are no unread articles."
8027   (interactive)
8028   (prog1
8029       (when (gnus-summary-first-subject t)
8030         (gnus-summary-show-thread)
8031         (gnus-summary-first-subject t)
8032         (gnus-summary-display-article (gnus-summary-article-number)))
8033     (gnus-summary-position-point)))
8034
8035 (defun gnus-summary-first-unread-subject ()
8036   "Place the point on the subject line of the first unread article.
8037 Return nil if there are no unread articles."
8038   (interactive)
8039   (prog1
8040       (when (gnus-summary-first-subject t)
8041         (gnus-summary-show-thread)
8042         (gnus-summary-first-subject t))
8043     (gnus-summary-position-point)))
8044
8045 (defun gnus-summary-first-unseen-subject ()
8046   "Place the point on the subject line of the first unseen article.
8047 Return nil if there are no unseen articles."
8048   (interactive)
8049   (prog1
8050       (when (gnus-summary-first-subject nil nil t)
8051         (gnus-summary-show-thread)
8052         (gnus-summary-first-subject nil nil t))
8053     (gnus-summary-position-point)))
8054
8055 (defun gnus-summary-first-unseen-or-unread-subject ()
8056   "Place the point on the subject line of the first unseen and unread article.
8057 If all article have been seen, on the subject line of the first unread
8058 article."
8059   (interactive)
8060   (prog1
8061       (unless (when (gnus-summary-first-subject nil nil t)
8062                 (gnus-summary-show-thread)
8063                 (gnus-summary-first-subject nil nil t))
8064         (when (gnus-summary-first-subject t)
8065           (gnus-summary-show-thread)
8066           (gnus-summary-first-subject t)))
8067     (gnus-summary-position-point)))
8068
8069 (defun gnus-summary-first-article ()
8070   "Select the first article.
8071 Return nil if there are no articles."
8072   (interactive)
8073   (prog1
8074       (when (gnus-summary-first-subject)
8075         (gnus-summary-show-thread)
8076         (gnus-summary-first-subject)
8077         (gnus-summary-display-article (gnus-summary-article-number)))
8078     (gnus-summary-position-point)))
8079
8080 (defun gnus-summary-best-unread-article (&optional arg)
8081   "Select the unread article with the highest score.
8082 If given a prefix argument, select the next unread article that has a
8083 score higher than the default score."
8084   (interactive "P")
8085   (let ((article (if arg
8086                      (gnus-summary-better-unread-subject)
8087                    (gnus-summary-best-unread-subject))))
8088     (if article
8089         (gnus-summary-goto-article article)
8090       (error "No unread articles"))))
8091
8092 (defun gnus-summary-best-unread-subject ()
8093   "Select the unread subject with the highest score."
8094   (interactive)
8095   (let ((best -1000000)
8096         (data gnus-newsgroup-data)
8097         article score)
8098     (while data
8099       (and (gnus-data-unread-p (car data))
8100            (> (setq score
8101                     (gnus-summary-article-score (gnus-data-number (car data))))
8102               best)
8103            (setq best score
8104                  article (gnus-data-number (car data))))
8105       (setq data (cdr data)))
8106     (when article
8107       (gnus-summary-goto-subject article))
8108     (gnus-summary-position-point)
8109     article))
8110
8111 (defun gnus-summary-better-unread-subject ()
8112   "Select the first unread subject that has a score over the default score."
8113   (interactive)
8114   (let ((data gnus-newsgroup-data)
8115         article score)
8116     (while (and (setq article (gnus-data-number (car data)))
8117                 (or (gnus-data-read-p (car data))
8118                     (not (> (gnus-summary-article-score article)
8119                             gnus-summary-default-score))))
8120       (setq data (cdr data)))
8121     (when article
8122       (gnus-summary-goto-subject article))
8123     (gnus-summary-position-point)
8124     article))
8125
8126 (defun gnus-summary-last-subject ()
8127   "Go to the last displayed subject line in the group."
8128   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8129     (when article
8130       (gnus-summary-goto-subject article))))
8131
8132 (defun gnus-summary-goto-article (article &optional all-headers force)
8133   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8134 If ALL-HEADERS is non-nil, no header lines are hidden.
8135 If FORCE, go to the article even if it isn't displayed.  If FORCE
8136 is a number, it is the line the article is to be displayed on."
8137   (interactive
8138    (list
8139     (gnus-completing-read
8140      "Article number or Message-ID"
8141      (mapcar 'int-to-string gnus-newsgroup-limit))
8142     current-prefix-arg
8143     t))
8144   (prog1
8145       (if (and (stringp article)
8146                (string-match "@\\|%40" article))
8147           (gnus-summary-refer-article article)
8148         (when (stringp article)
8149           (setq article (string-to-number article)))
8150         (if (gnus-summary-goto-subject article force)
8151             (gnus-summary-display-article article all-headers)
8152           (gnus-message 4 "Couldn't go to article %s" article) nil))
8153     (gnus-summary-position-point)))
8154
8155 (defun gnus-summary-goto-last-article ()
8156   "Go to the previously read article."
8157   (interactive)
8158   (prog1
8159       (when gnus-last-article
8160         (gnus-summary-goto-article gnus-last-article nil t))
8161     (gnus-summary-position-point)))
8162
8163 (defun gnus-summary-pop-article (number)
8164   "Pop one article off the history and go to the previous.
8165 NUMBER articles will be popped off."
8166   (interactive "p")
8167   (let (to)
8168     (setq gnus-newsgroup-history
8169           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8170     (if to
8171         (gnus-summary-goto-article (car to) nil t)
8172       (error "Article history empty")))
8173   (gnus-summary-position-point))
8174
8175 ;; Summary commands and functions for limiting the summary buffer.
8176
8177 (defun gnus-summary-limit-to-articles (n)
8178   "Limit the summary buffer to the next N articles.
8179 If not given a prefix, use the process marked articles instead."
8180   (interactive "P")
8181   (prog1
8182       (let ((articles (gnus-summary-work-articles n)))
8183         (setq gnus-newsgroup-processable nil)
8184         (gnus-summary-limit articles))
8185     (gnus-summary-position-point)))
8186
8187 (defun gnus-summary-pop-limit (&optional total)
8188   "Restore the previous limit.
8189 If given a prefix, remove all limits."
8190   (interactive "P")
8191   (when total
8192     (setq gnus-newsgroup-limits
8193           (list (mapcar (lambda (h) (mail-header-number h))
8194                         gnus-newsgroup-headers))))
8195   (unless gnus-newsgroup-limits
8196     (error "No limit to pop"))
8197   (prog1
8198       (gnus-summary-limit nil 'pop)
8199     (gnus-summary-position-point)))
8200
8201 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8202   "Limit the summary buffer to articles that have subjects that match a regexp.
8203 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8204   (interactive
8205    (list (read-string (if current-prefix-arg
8206                           "Exclude subject (regexp): "
8207                         "Limit to subject (regexp): "))
8208          nil current-prefix-arg))
8209   (unless header
8210     (setq header "subject"))
8211   (when (not (equal "" subject))
8212     (prog1
8213         (let ((articles (gnus-summary-find-matching
8214                          (or header "subject") subject 'all nil nil
8215                          not-matching)))
8216           (unless articles
8217             (error "Found no matches for \"%s\"" subject))
8218           (gnus-summary-limit articles))
8219       (gnus-summary-position-point))))
8220
8221 (defun gnus-summary-limit-to-author (from &optional not-matching)
8222   "Limit the summary buffer to articles that have authors that match a regexp.
8223 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8224   (interactive
8225    (list (read-string (if current-prefix-arg
8226                           "Exclude author (regexp): "
8227                         "Limit to author (regexp): "))
8228          current-prefix-arg))
8229   (gnus-summary-limit-to-subject from "from" not-matching))
8230
8231 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8232   "Limit the summary buffer to articles with the given RECIPIENT.
8233
8234 If NOT-MATCHING, exclude RECIPIENT.
8235
8236 To and Cc headers are checked.  You need to include them in
8237 `nnmail-extra-headers'."
8238   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8239   (interactive
8240    (list (read-string (format "%s recipient (regexp): "
8241                               (if current-prefix-arg "Exclude" "Limit to")))
8242          current-prefix-arg))
8243   (when (not (equal "" recipient))
8244     (prog1 (let* ((to
8245                    (if (memq 'To nnmail-extra-headers)
8246                        (gnus-summary-find-matching
8247                         (cons 'extra 'To) recipient 'all nil nil
8248                         not-matching)
8249                      (gnus-message
8250                       1 "`To' isn't present in `nnmail-extra-headers'")
8251                      (sit-for 1)
8252                      nil))
8253                   (cc
8254                    (if (memq 'Cc nnmail-extra-headers)
8255                        (gnus-summary-find-matching
8256                         (cons 'extra 'Cc) recipient 'all nil nil
8257                         not-matching)
8258                      (gnus-message
8259                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8260                      (sit-for 1)
8261                      nil))
8262                   (articles
8263                    (if not-matching
8264                        ;; We need the numbers that are in both lists:
8265                        (mapcar (lambda (a)
8266                                  (and (memq a to) a))
8267                                cc)
8268                      (nconc to cc))))
8269              (unless articles
8270                (error "Found no matches for \"%s\"" recipient))
8271              (gnus-summary-limit articles))
8272       (gnus-summary-position-point))))
8273
8274 (defun gnus-summary-limit-to-address (address &optional not-matching)
8275   "Limit the summary buffer to articles with the given ADDRESS.
8276
8277 If NOT-MATCHING, exclude ADDRESS.
8278
8279 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8280 in `nnmail-extra-headers'."
8281   (interactive
8282    (list (read-string (format "%s address (regexp): "
8283                               (if current-prefix-arg "Exclude" "Limit to")))
8284          current-prefix-arg))
8285   (when (not (equal "" address))
8286     (prog1 (let* ((to
8287                    (if (memq 'To nnmail-extra-headers)
8288                        (gnus-summary-find-matching
8289                         (cons 'extra 'To) address 'all nil nil
8290                         not-matching)
8291                      (gnus-message
8292                       1 "`To' isn't present in `nnmail-extra-headers'")
8293                      (sit-for 1)
8294                      t))
8295                   (cc
8296                    (if (memq 'Cc nnmail-extra-headers)
8297                        (gnus-summary-find-matching
8298                         (cons 'extra 'Cc) address 'all nil nil
8299                         not-matching)
8300                      (gnus-message
8301                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8302                      (sit-for 1)
8303                      t))
8304                   (from
8305                    (gnus-summary-find-matching "from" address
8306                                                'all nil nil not-matching))
8307                   (articles
8308                    (if not-matching
8309                        ;; We need the numbers that are in all lists:
8310                        (if (eq cc t)
8311                            (if (eq to t)
8312                                from
8313                              (mapcar (lambda (a) (car (memq a from))) to))
8314                          (if (eq to t)
8315                              (mapcar (lambda (a) (car (memq a from))) cc)
8316                            (mapcar (lambda (a) (car (memq a from)))
8317                                    (mapcar (lambda (a) (car (memq a to)))
8318                                            cc))))
8319                      (nconc (if (eq to t) nil to)
8320                             (if (eq cc t) nil cc)
8321                             from))))
8322              (unless articles
8323                (error "Found no matches for \"%s\"" address))
8324              (gnus-summary-limit articles))
8325       (gnus-summary-position-point))))
8326
8327 (defun gnus-summary-limit-strange-charsets-predicate (header)
8328   (when (fboundp 'char-charset)
8329     (let ((string (concat (mail-header-subject header)
8330                           (mail-header-from header)))
8331           charset found)
8332       (dotimes (i (1- (length string)))
8333         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8334         (when (string-match "unicode\\|big\\|japanese" charset)
8335           (setq found t)))
8336       found)))
8337
8338 (defun gnus-summary-limit-to-predicate (predicate)
8339   "Limit to articles where PREDICATE returns non-nil.
8340 PREDICATE will be called with the header structures of the
8341 articles."
8342   (let ((articles nil)
8343         (case-fold-search t))
8344     (dolist (header gnus-newsgroup-headers)
8345       (when (funcall predicate header)
8346         (push (mail-header-number header) articles)))
8347     (gnus-summary-limit (nreverse articles))))
8348
8349 (defun gnus-summary-limit-to-age (age &optional younger-p)
8350   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8351 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8352 articles that are younger than AGE days."
8353   (interactive
8354    (let ((younger current-prefix-arg)
8355          (days-got nil)
8356          days)
8357      (while (not days-got)
8358        (setq days (if younger
8359                       (read-string "Limit to articles younger than (in days, older when negative): ")
8360                     (read-string
8361                      "Limit to articles older than (in days, younger when negative): ")))
8362        (when (> (length days) 0)
8363          (setq days (read days)))
8364        (if (numberp days)
8365            (progn
8366              (setq days-got t)
8367              (when (< days 0)
8368                (setq younger (not younger))
8369                (setq days (* days -1))))
8370          (message "Please enter a number.")
8371          (sleep-for 1)))
8372      (list days younger)))
8373   (prog1
8374       (let ((data gnus-newsgroup-data)
8375             (cutoff (days-to-time age))
8376             articles d date is-younger)
8377         (while (setq d (pop data))
8378           (when (and (vectorp (gnus-data-header d))
8379                      (setq date (mail-header-date (gnus-data-header d))))
8380             (setq is-younger (time-less-p
8381                               (time-since (gnus-date-get-time date))
8382                               cutoff))
8383             (when (if younger-p
8384                       is-younger
8385                     (not is-younger))
8386               (push (gnus-data-number d) articles))))
8387         (gnus-summary-limit (nreverse articles)))
8388     (gnus-summary-position-point)))
8389
8390 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8391   "Limit the summary buffer to articles that match an 'extra' header."
8392   (interactive
8393    (let ((header
8394           (intern
8395            (gnus-completing-read
8396             (if current-prefix-arg
8397                 "Exclude extra header"
8398               "Limit extra header")
8399             (mapcar 'symbol-name gnus-extra-headers)
8400             t nil nil
8401             (symbol-name (car gnus-extra-headers))))))
8402      (list header
8403            (read-string (format "%s header %s (regexp): "
8404                                 (if current-prefix-arg "Exclude" "Limit to")
8405                                 header))
8406            current-prefix-arg)))
8407   (when (not (equal "" regexp))
8408     (prog1
8409         (let ((articles (gnus-summary-find-matching
8410                          (cons 'extra header) regexp 'all nil nil
8411                          not-matching)))
8412           (unless articles
8413             (error "Found no matches for \"%s\"" regexp))
8414           (gnus-summary-limit articles))
8415       (gnus-summary-position-point))))
8416
8417 (defun gnus-summary-limit-to-display-predicate ()
8418   "Limit the summary buffer to the predicated in the `display' group parameter."
8419   (interactive)
8420   (unless gnus-newsgroup-display
8421     (error "There is no `display' group parameter"))
8422   (let (articles)
8423     (dolist (gnus-number gnus-newsgroup-articles)
8424       (when (funcall gnus-newsgroup-display)
8425         (push gnus-number articles)))
8426     (gnus-summary-limit articles))
8427   (gnus-summary-position-point))
8428
8429 (defun gnus-summary-limit-to-unread (&optional all)
8430   "Limit the summary buffer to articles that are not marked as read.
8431 If ALL is non-nil, limit strictly to unread articles."
8432   (interactive "P")
8433   (if all
8434       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8435     (gnus-summary-limit-to-marks
8436      ;; Concat all the marks that say that an article is read and have
8437      ;; those removed.
8438      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8439            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8440            gnus-low-score-mark gnus-expirable-mark
8441            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8442            gnus-duplicate-mark)
8443      'reverse)))
8444
8445 (defun gnus-summary-limit-to-headers (match &optional reverse)
8446   "Limit the summary buffer to articles that have headers that match MATCH.
8447 If REVERSE (the prefix), limit to articles that don't match."
8448   (interactive "sMatch headers (regexp): \nP")
8449   (gnus-summary-limit-to-bodies match reverse t))
8450
8451 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8452   "Limit the summary buffer to articles that have bodies that match MATCH.
8453 If REVERSE (the prefix), limit to articles that don't match."
8454   (interactive "sMatch body (regexp): \nP")
8455   (let ((articles nil)
8456         (gnus-select-article-hook nil)  ;Disable hook.
8457         (gnus-article-prepare-hook nil)
8458         (gnus-use-article-prefetch nil)
8459         (gnus-keep-backlog nil)
8460         (gnus-break-pages nil)
8461         (gnus-summary-display-arrow nil)
8462         (gnus-updated-mode-lines nil)
8463         (gnus-auto-center-summary nil)
8464         (gnus-display-mime-function nil))
8465     (dolist (data gnus-newsgroup-data)
8466       (let (gnus-mark-article-hook)
8467         (gnus-summary-select-article t t nil (gnus-data-number data)))
8468       (with-current-buffer gnus-article-buffer
8469         (article-goto-body)
8470         (let* ((case-fold-search t)
8471                (found (if headersp
8472                           (re-search-backward match nil t)
8473                         (re-search-forward match nil t))))
8474           (when (or (and found
8475                          (not reverse))
8476                     (and (not found)
8477                          reverse))
8478             (push (gnus-data-number data) articles)))))
8479     (if (not articles)
8480         (message "No messages matched")
8481       (gnus-summary-limit articles)))
8482   (gnus-summary-position-point))
8483
8484 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8485   "Limit the summary buffer to articles that aren't part on any thread.
8486 If THREADSP (the prefix), limit to articles that are in threads."
8487   (interactive "P")
8488   (let ((articles nil)
8489         thread-articles
8490         threads)
8491     (dolist (thread gnus-newsgroup-threads)
8492       (if (stringp (car thread))
8493           (dolist (thread (cdr thread))
8494             (push thread threads))
8495         (push thread threads)))
8496     (dolist (thread threads)
8497       (setq thread-articles (gnus-articles-in-thread thread))
8498       (when (or (and threadsp
8499                      (> (length thread-articles) 1))
8500                 (and (not threadsp)
8501                      (= (length thread-articles) 1)))
8502         (setq articles (nconc thread-articles articles))))
8503     (if (not articles)
8504         (message "No messages matched")
8505       (gnus-summary-limit articles))
8506     (gnus-summary-position-point)))
8507
8508 (defun gnus-summary-limit-to-replied (&optional unreplied)
8509   "Limit the summary buffer to replied articles.
8510 If UNREPLIED (the prefix), limit to unreplied articles."
8511   (interactive "P")
8512   (if unreplied
8513       (gnus-summary-limit
8514        (gnus-set-difference gnus-newsgroup-articles
8515         gnus-newsgroup-replied))
8516     (gnus-summary-limit gnus-newsgroup-replied))
8517   (gnus-summary-position-point))
8518
8519 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8520   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8521 If REVERSE, limit the summary buffer to articles that are marked
8522 with MARKS.  MARKS can either be a string of marks or a list of marks.
8523 Returns how many articles were removed."
8524   (interactive "sMarks: ")
8525   (gnus-summary-limit-to-marks marks t))
8526
8527 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8528   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8529 If REVERSE (the prefix), limit the summary buffer to articles that are
8530 not marked with MARKS.  MARKS can either be a string of marks or a
8531 list of marks.
8532 Returns how many articles were removed."
8533   (interactive "sMarks: \nP")
8534   (prog1
8535       (let ((data gnus-newsgroup-data)
8536             (marks (if (listp marks) marks
8537                      (append marks nil))) ; Transform to list.
8538             articles)
8539         (while data
8540           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8541                   (memq (gnus-data-mark (car data)) marks))
8542             (push (gnus-data-number (car data)) articles))
8543           (setq data (cdr data)))
8544         (gnus-summary-limit articles))
8545     (gnus-summary-position-point)))
8546
8547 (defun gnus-summary-limit-to-score (score)
8548   "Limit to articles with score at or above SCORE."
8549   (interactive "NLimit to articles with score of at least: ")
8550   (let ((data gnus-newsgroup-data)
8551         articles)
8552     (while data
8553       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8554                 score)
8555         (push (gnus-data-number (car data)) articles))
8556       (setq data (cdr data)))
8557     (prog1
8558         (gnus-summary-limit articles)
8559       (gnus-summary-position-point))))
8560
8561 (defun gnus-summary-limit-to-unseen ()
8562   "Limit to unseen articles."
8563   (interactive)
8564   (prog1
8565       (gnus-summary-limit gnus-newsgroup-unseen)
8566     (gnus-summary-position-point)))
8567
8568 (defun gnus-summary-limit-include-thread (id)
8569   "Display all the hidden articles that is in the thread with ID in it.
8570 When called interactively, ID is the Message-ID of the current
8571 article."
8572   (interactive (list (mail-header-id (gnus-summary-article-header))))
8573   (let ((articles (gnus-articles-in-thread
8574                    (gnus-id-to-thread (gnus-root-id id))))
8575         ;;we REALLY want the whole thread---this prevents cut-threads
8576         ;;from removing the thread we want to include.
8577         (gnus-fetch-old-headers nil)
8578         (gnus-build-sparse-threads nil))
8579     (prog1
8580         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8581       (gnus-summary-limit-include-matching-articles
8582        "subject"
8583        (regexp-quote (gnus-simplify-subject-re
8584                       (mail-header-subject (gnus-id-to-header id)))))
8585       (gnus-summary-position-point))))
8586
8587 (defun gnus-summary-limit-include-matching-articles (header regexp)
8588   "Display all the hidden articles that have HEADERs that match REGEXP."
8589   (interactive (list (read-string "Match on header: ")
8590                      (read-string "Regexp: ")))
8591   (let ((articles (gnus-find-matching-articles header regexp)))
8592     (prog1
8593         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8594       (gnus-summary-position-point))))
8595
8596 (defun gnus-summary-insert-dormant-articles ()
8597   "Insert all the dormant articles for this group into the current buffer."
8598   (interactive)
8599   (let ((gnus-verbose (max 6 gnus-verbose)))
8600     (if (not gnus-newsgroup-dormant)
8601         (gnus-message 3 "No dormant articles for this group")
8602       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8603
8604 (defun gnus-summary-insert-ticked-articles ()
8605   "Insert ticked articles for this group into the current buffer."
8606   (interactive)
8607   (let ((gnus-verbose (max 6 gnus-verbose)))
8608     (if (not gnus-newsgroup-marked)
8609         (gnus-message 3 "No ticked articles for this group")
8610       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8611
8612 (defun gnus-summary-limit-include-dormant ()
8613   "Display all the hidden articles that are marked as dormant.
8614 Note that this command only works on a subset of the articles currently
8615 fetched for this group."
8616   (interactive)
8617   (unless gnus-newsgroup-dormant
8618     (error "There are no dormant articles in this group"))
8619   (prog1
8620       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8621     (gnus-summary-position-point)))
8622
8623 (defun gnus-summary-include-articles (articles)
8624   "Fetch the headers for ARTICLES and then display the summary lines."
8625   (let ((gnus-inhibit-demon t)
8626         (gnus-agent nil)
8627         (gnus-read-all-available-headers t))
8628     (setq gnus-newsgroup-headers
8629           (gnus-merge
8630            'list gnus-newsgroup-headers
8631            (gnus-fetch-headers articles nil t)
8632            'gnus-article-sort-by-number))
8633     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8634
8635 (defun gnus-summary-limit-exclude-dormant ()
8636   "Hide all dormant articles."
8637   (interactive)
8638   (prog1
8639       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8640     (gnus-summary-position-point)))
8641
8642 (defun gnus-summary-limit-exclude-childless-dormant ()
8643   "Hide all dormant articles that have no children."
8644   (interactive)
8645   (let ((data (gnus-data-list t))
8646         articles d children)
8647     ;; Find all articles that are either not dormant or have
8648     ;; children.
8649     (while (setq d (pop data))
8650       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8651                 (and (setq children
8652                            (gnus-article-children (gnus-data-number d)))
8653                      (let (found)
8654                        (while children
8655                          (when (memq (car children) articles)
8656                            (setq children nil
8657                                  found t))
8658                          (pop children))
8659                        found)))
8660         (push (gnus-data-number d) articles)))
8661     ;; Do the limiting.
8662     (prog1
8663         (gnus-summary-limit articles)
8664       (gnus-summary-position-point))))
8665
8666 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8667   "Mark all unread excluded articles as read.
8668 If ALL, mark even excluded ticked and dormants as read."
8669   (interactive "P")
8670   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8671   (let ((articles (gnus-sorted-ndifference
8672                    (sort
8673                     (mapcar (lambda (h) (mail-header-number h))
8674                             gnus-newsgroup-headers)
8675                     '<)
8676                    gnus-newsgroup-limit))
8677         article)
8678     (setq gnus-newsgroup-unreads
8679           (gnus-sorted-intersection gnus-newsgroup-unreads
8680                                     gnus-newsgroup-limit))
8681     (if all
8682         (setq gnus-newsgroup-dormant nil
8683               gnus-newsgroup-marked nil
8684               gnus-newsgroup-reads
8685               (nconc
8686                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8687                gnus-newsgroup-reads))
8688       (while (setq article (pop articles))
8689         (unless (or (memq article gnus-newsgroup-dormant)
8690                     (memq article gnus-newsgroup-marked))
8691           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8692
8693 (defun gnus-summary-limit (articles &optional pop)
8694   (if pop
8695       ;; We pop the previous limit off the stack and use that.
8696       (setq articles (car gnus-newsgroup-limits)
8697             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8698     ;; We use the new limit, so we push the old limit on the stack.
8699     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8700   ;; Set the limit.
8701   (setq gnus-newsgroup-limit articles)
8702   (let ((total (length gnus-newsgroup-data))
8703         (data (gnus-data-find-list (gnus-summary-article-number)))
8704         (gnus-summary-mark-below nil)   ; Inhibit this.
8705         found)
8706     ;; This will do all the work of generating the new summary buffer
8707     ;; according to the new limit.
8708     (gnus-summary-prepare)
8709     ;; Hide any threads, possibly.
8710     (gnus-summary-maybe-hide-threads)
8711     ;; Try to return to the article you were at, or one in the
8712     ;; neighborhood.
8713     (when data
8714       ;; We try to find some article after the current one.
8715       (while data
8716         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8717           (setq data nil
8718                 found t))
8719         (setq data (cdr data))))
8720     (unless found
8721       ;; If there is no data, that means that we were after the last
8722       ;; article.  The same goes when we can't find any articles
8723       ;; after the current one.
8724       (goto-char (point-max))
8725       (gnus-summary-find-prev))
8726     (gnus-set-mode-line 'summary)
8727     ;; We return how many articles were removed from the summary
8728     ;; buffer as a result of the new limit.
8729     (- total (length gnus-newsgroup-data))))
8730
8731 (defsubst gnus-invisible-cut-children (threads)
8732   (let ((num 0))
8733     (while threads
8734       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8735         (incf num))
8736       (pop threads))
8737     (< num 2)))
8738
8739 (defsubst gnus-cut-thread (thread)
8740   "Go forwards in the thread until we find an article that we want to display."
8741   (when (or (eq gnus-fetch-old-headers 'some)
8742             (eq gnus-fetch-old-headers 'invisible)
8743             (numberp gnus-fetch-old-headers)
8744             (eq gnus-build-sparse-threads 'some)
8745             (eq gnus-build-sparse-threads 'more))
8746     ;; Deal with old-fetched headers and sparse threads.
8747     (while (and
8748             thread
8749             (or
8750              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8751              (gnus-summary-article-ancient-p
8752               (mail-header-number (car thread))))
8753             (if (or (<= (length (cdr thread)) 1)
8754                     (eq gnus-fetch-old-headers 'invisible))
8755                 (setq gnus-newsgroup-limit
8756                       (delq (mail-header-number (car thread))
8757                             gnus-newsgroup-limit)
8758                       thread (cadr thread))
8759               (when (gnus-invisible-cut-children (cdr thread))
8760                 (let ((th (cdr thread)))
8761                   (while th
8762                     (if (memq (mail-header-number (caar th))
8763                               gnus-newsgroup-limit)
8764                         (setq thread (car th)
8765                               th nil)
8766                       (setq th (cdr th))))))))))
8767   thread)
8768
8769 (defun gnus-cut-threads (threads)
8770   "Cut off all uninteresting articles from the beginning of THREADS."
8771   (when (or (eq gnus-fetch-old-headers 'some)
8772             (eq gnus-fetch-old-headers 'invisible)
8773             (numberp gnus-fetch-old-headers)
8774             (eq gnus-build-sparse-threads 'some)
8775             (eq gnus-build-sparse-threads 'more))
8776     (let ((th threads))
8777       (while th
8778         (setcar th (gnus-cut-thread (car th)))
8779         (setq th (cdr th)))))
8780   ;; Remove nixed out threads.
8781   (delq nil threads))
8782
8783 (defun gnus-summary-initial-limit (&optional show-if-empty)
8784   "Figure out what the initial limit is supposed to be on group entry.
8785 This entails weeding out unwanted dormants, low-scored articles,
8786 fetch-old-headers verbiage, and so on."
8787   ;; Most groups have nothing to remove.
8788   (unless (or gnus-inhibit-limiting
8789               (and (null gnus-newsgroup-dormant)
8790                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8791                    (not (eq gnus-fetch-old-headers 'some))
8792                    (not (numberp gnus-fetch-old-headers))
8793                    (not (eq gnus-fetch-old-headers 'invisible))
8794                    (null gnus-summary-expunge-below)
8795                    (not (eq gnus-build-sparse-threads 'some))
8796                    (not (eq gnus-build-sparse-threads 'more))
8797                    (null gnus-thread-expunge-below)))
8798     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8799     (setq gnus-newsgroup-limit nil)
8800     (mapatoms
8801      (lambda (node)
8802        (unless (car (symbol-value node))
8803          ;; These threads have no parents -- they are roots.
8804          (let ((nodes (cdr (symbol-value node)))
8805                thread)
8806            (while nodes
8807              (if (and gnus-thread-expunge-below
8808                       (< (gnus-thread-total-score (car nodes))
8809                          gnus-thread-expunge-below))
8810                  (gnus-expunge-thread (pop nodes))
8811                (setq thread (pop nodes))
8812                (gnus-summary-limit-children thread))))))
8813      gnus-newsgroup-dependencies)
8814     ;; If this limitation resulted in an empty group, we might
8815     ;; pop the previous limit and use it instead.
8816     (when (and (not gnus-newsgroup-limit)
8817                show-if-empty)
8818       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8819     gnus-newsgroup-limit))
8820
8821 (defun gnus-summary-limit-children (thread)
8822   "Return 1 if this subthread is visible and 0 if it is not."
8823   ;; First we get the number of visible children to this thread.  This
8824   ;; is done by recursing down the thread using this function, so this
8825   ;; will really go down to a leaf article first, before slowly
8826   ;; working its way up towards the root.
8827   (when thread
8828     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8829            (children
8830            (if (cdr thread)
8831                (apply '+ (mapcar 'gnus-summary-limit-children
8832                                  (cdr thread)))
8833              0))
8834            (number (mail-header-number (car thread)))
8835            score)
8836       (if (and
8837            (not (memq number gnus-newsgroup-marked))
8838            (or
8839             ;; If this article is dormant and has absolutely no visible
8840             ;; children, then this article isn't visible.
8841             (and (memq number gnus-newsgroup-dormant)
8842                  (zerop children))
8843             ;; If this is "fetch-old-headered" and there is no
8844             ;; visible children, then we don't want this article.
8845             (and (or (eq gnus-fetch-old-headers 'some)
8846                      (numberp gnus-fetch-old-headers))
8847                  (gnus-summary-article-ancient-p number)
8848                  (zerop children))
8849             ;; If this is "fetch-old-headered" and `invisible', then
8850             ;; we don't want this article.
8851             (and (eq gnus-fetch-old-headers 'invisible)
8852                  (gnus-summary-article-ancient-p number))
8853             ;; If this is a sparsely inserted article with no children,
8854             ;; we don't want it.
8855             (and (eq gnus-build-sparse-threads 'some)
8856                  (gnus-summary-article-sparse-p number)
8857                  (zerop children))
8858             ;; If we use expunging, and this article is really
8859             ;; low-scored, then we don't want this article.
8860             (when (and gnus-summary-expunge-below
8861                        (< (setq score
8862                                 (or (cdr (assq number gnus-newsgroup-scored))
8863                                     gnus-summary-default-score))
8864                           gnus-summary-expunge-below))
8865               ;; We increase the expunge-tally here, but that has
8866               ;; nothing to do with the limits, really.
8867               (incf gnus-newsgroup-expunged-tally)
8868               ;; We also mark as read here, if that's wanted.
8869               (when (and gnus-summary-mark-below
8870                          (< score gnus-summary-mark-below))
8871                 (setq gnus-newsgroup-unreads
8872                       (delq number gnus-newsgroup-unreads))
8873                 (if gnus-newsgroup-auto-expire
8874                     (push number gnus-newsgroup-expirable)
8875                   (push (cons number gnus-low-score-mark)
8876                         gnus-newsgroup-reads)))
8877               t)
8878             ;; Do the `display' group parameter.
8879             (and gnus-newsgroup-display
8880                  (let ((gnus-number number))
8881                    (not (funcall gnus-newsgroup-display))))))
8882           ;; Nope, invisible article.
8883           0
8884         ;; Ok, this article is to be visible, so we add it to the limit
8885         ;; and return 1.
8886         (push number gnus-newsgroup-limit)
8887         1))))
8888
8889 (defun gnus-expunge-thread (thread)
8890   "Mark all articles in THREAD as read."
8891   (let* ((number (mail-header-number (car thread))))
8892     (incf gnus-newsgroup-expunged-tally)
8893     ;; We also mark as read here, if that's wanted.
8894     (setq gnus-newsgroup-unreads
8895           (delq number gnus-newsgroup-unreads))
8896     (if gnus-newsgroup-auto-expire
8897         (push number gnus-newsgroup-expirable)
8898       (push (cons number gnus-low-score-mark)
8899             gnus-newsgroup-reads)))
8900   ;; Go recursively through all subthreads.
8901   (mapcar 'gnus-expunge-thread (cdr thread)))
8902
8903 ;; Summary article oriented commands
8904
8905 (defun gnus-summary-refer-parent-article (n)
8906   "Refer parent article N times.
8907 If N is negative, go to ancestor -N instead.
8908 The difference between N and the number of articles fetched is returned."
8909   (interactive "p")
8910   (let ((skip 1)
8911         error header ref)
8912     (when (not (natnump n))
8913       (setq skip (abs n)
8914             n 1))
8915     (while (and (> n 0)
8916                 (not error))
8917       (setq header (gnus-summary-article-header))
8918       (if (and (eq (mail-header-number header)
8919                    (cdr gnus-article-current))
8920                (equal gnus-newsgroup-name
8921                       (car gnus-article-current)))
8922           ;; If we try to find the parent of the currently
8923           ;; displayed article, then we take a look at the actual
8924           ;; References header, since this is slightly more
8925           ;; reliable than the References field we got from the
8926           ;; server.
8927           (with-current-buffer gnus-original-article-buffer
8928             (nnheader-narrow-to-headers)
8929             (unless (setq ref (message-fetch-field "references"))
8930               (when (setq ref (message-fetch-field "in-reply-to"))
8931                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8932             (widen))
8933         (setq ref
8934               ;; It's not the current article, so we take a bet on
8935               ;; the value we got from the server.
8936               (mail-header-references header)))
8937       (if (and ref
8938                (not (equal ref "")))
8939           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8940             (gnus-message 1 "Couldn't find parent"))
8941         (gnus-message 1 "No references in article %d"
8942                       (gnus-summary-article-number))
8943         (setq error t))
8944       (decf n))
8945     (gnus-summary-position-point)
8946     n))
8947
8948 (defun gnus-summary-refer-references ()
8949   "Fetch all articles mentioned in the References header.
8950 Return the number of articles fetched."
8951   (interactive)
8952   (let ((ref (mail-header-references (gnus-summary-article-header)))
8953         (current (gnus-summary-article-number))
8954         (n 0))
8955     (if (or (not ref)
8956             (equal ref ""))
8957         (error "No References in the current article")
8958       ;; For each Message-ID in the References header...
8959       (while (string-match "<[^>]*>" ref)
8960         (incf n)
8961         ;; ... fetch that article.
8962         (gnus-summary-refer-article
8963          (prog1 (match-string 0 ref)
8964            (setq ref (substring ref (match-end 0))))))
8965       (gnus-summary-goto-subject current)
8966       (gnus-summary-position-point)
8967       n)))
8968
8969 (defun gnus-delete-duplicate-headers (headers)
8970   ;; First remove leading duplicates.
8971   (while (and (> (length headers) 1)
8972               (= (mail-header-number (car headers))
8973                  (mail-header-number (cadr headers))))
8974     (pop headers))
8975   ;; Then the rest.
8976   (let ((result headers))
8977     (while (> (length headers) 1)
8978       (if (= (mail-header-number (car headers))
8979              (mail-header-number (cadr headers)))
8980           (setcdr headers (cddr headers))
8981         (pop headers)))
8982     result))
8983
8984 (defun gnus-summary-refer-thread (&optional limit)
8985   "Fetch all articles in the current thread. For backends that
8986 know how to search for threads (currently only 'nnimap) a
8987 non-numeric prefix arg will use nnir to search the entire
8988 server; without a prefix arg only the current group is
8989 searched. If the variable `gnus-refer-thread-use-nnir' is
8990 non-nil the prefix arg has the reverse meaning. If no
8991 backend-specific 'request-thread function is available fetch
8992 LIMIT (the numerical prefix) old headers. If LIMIT is
8993 non-numeric or nil fetch the number specified by the
8994 `gnus-refer-thread-limit' variable."
8995   (interactive "P")
8996   (gnus-warp-to-article)
8997   (let* ((header (gnus-summary-article-header))
8998          (id (mail-header-id header))
8999          (gnus-inhibit-demon t)
9000          (gnus-summary-ignore-duplicates t)
9001          (gnus-read-all-available-headers t)
9002          (gnus-refer-thread-use-nnir
9003           (if (and (not (null limit)) (listp limit))
9004               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9005          (new-headers
9006           (if (gnus-check-backend-function
9007                'request-thread gnus-newsgroup-name)
9008               (gnus-request-thread header gnus-newsgroup-name)
9009             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9010                             gnus-refer-thread-limit))
9011                    (last (if (numberp limit)
9012                              (min (+ (mail-header-number header)
9013                                      limit)
9014                                   gnus-newsgroup-highest)
9015                            gnus-newsgroup-highest))
9016                    (subject (gnus-simplify-subject
9017                              (mail-header-subject header)))
9018                    (refs (split-string (or (mail-header-references header)
9019                                            "")))
9020                    (gnus-parse-headers-hook
9021                     `(lambda () (goto-char (point-min))
9022                       (keep-lines
9023                        (regexp-opt ',(append refs (list id subject)))))))
9024               (gnus-fetch-headers (list last) (if (numberp limit)
9025                                                   (* 2 limit) limit) t)))))
9026     (when (listp new-headers)
9027       (dolist (header new-headers)
9028         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9029           (push (mail-header-number header) gnus-newsgroup-unreads)
9030           (setq gnus-newsgroup-unselected
9031                 (delete (mail-header-number header)
9032                         gnus-newsgroup-unselected))))
9033       (setq gnus-newsgroup-headers
9034             (gnus-delete-duplicate-headers
9035              (gnus-merge
9036               'list gnus-newsgroup-headers new-headers
9037               'gnus-article-sort-by-number)))
9038       (gnus-summary-limit-include-thread id))))
9039
9040 (defun gnus-summary-refer-article (message-id)
9041   "Fetch an article specified by MESSAGE-ID."
9042   (interactive "sMessage-ID: ")
9043   (gnus-warp-to-article)
9044   (when (and (stringp message-id)
9045              (not (zerop (length message-id))))
9046     (setq message-id (gnus-replace-in-string message-id " " ""))
9047     ;; Construct the correct Message-ID if necessary.
9048     ;; Suggested by tale@pawl.rpi.edu.
9049     (unless (string-match "^<" message-id)
9050       (setq message-id (concat "<" message-id)))
9051     (unless (string-match ">$" message-id)
9052       (setq message-id (concat message-id ">")))
9053     ;; People often post MIDs from URLs, so unhex it:
9054     (unless (string-match "@" message-id)
9055       (setq message-id (gnus-url-unhex-string message-id)))
9056     (let* ((header (gnus-id-to-header message-id))
9057            (sparse (and header
9058                         (gnus-summary-article-sparse-p
9059                          (mail-header-number header))
9060                         (memq (mail-header-number header)
9061                               gnus-newsgroup-limit)))
9062            number)
9063       (cond
9064        ;; If the article is present in the buffer we just go to it.
9065        ((and header
9066              (or (not (gnus-summary-article-sparse-p
9067                        (mail-header-number header)))
9068                  sparse))
9069         (prog1
9070             (gnus-summary-goto-article
9071              (mail-header-number header) nil t)
9072           (when sparse
9073             (gnus-summary-update-article (mail-header-number header)))))
9074        (t
9075         ;; We fetch the article.
9076         (catch 'found
9077           (dolist (gnus-override-method (gnus-refer-article-methods))
9078             (when (and (gnus-check-server gnus-override-method)
9079                        ;; Fetch the header,
9080                        (setq number (gnus-summary-insert-subject message-id)))
9081               ;; and display the article.
9082               (gnus-summary-select-article nil nil nil number)
9083               (throw 'found t)))
9084           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9085
9086 (defun gnus-refer-article-methods ()
9087   "Return a list of referable methods."
9088   (cond
9089    ;; No method, so we default to current and native.
9090    ((null gnus-refer-article-method)
9091     (list gnus-current-select-method gnus-select-method))
9092    ;; Current.
9093    ((eq 'current gnus-refer-article-method)
9094     (list gnus-current-select-method))
9095    ;; List of select methods.
9096    ((not (and (symbolp (car gnus-refer-article-method))
9097               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9098     (let (out)
9099       (dolist (method gnus-refer-article-method)
9100         (push (if (eq 'current method)
9101                   gnus-current-select-method
9102                 (if (eq 'nnir (car method))
9103                     (list
9104                      'nnir
9105                      (or (cadr method)
9106                          (gnus-method-to-server gnus-current-select-method)))
9107                   method))
9108               out))
9109       (nreverse out)))
9110    ;; One single select method.
9111    (t
9112     (list gnus-refer-article-method))))
9113
9114 (defun gnus-summary-edit-parameters ()
9115   "Edit the group parameters of the current group."
9116   (interactive)
9117   (gnus-group-edit-group gnus-newsgroup-name 'params))
9118
9119 (defun gnus-summary-customize-parameters ()
9120   "Customize the group parameters of the current group."
9121   (interactive)
9122   (gnus-group-customize gnus-newsgroup-name))
9123
9124 (defun gnus-summary-enter-digest-group (&optional force)
9125   "Enter an nndoc group based on the current article.
9126 If FORCE, force a digest interpretation.  If not, try to guess
9127 what the document format is.
9128
9129 To control what happens when you exit the group, see the
9130 `gnus-auto-select-on-ephemeral-exit' variable."
9131   (interactive "P")
9132   (let ((conf gnus-current-window-configuration))
9133     (save-window-excursion
9134       (save-excursion
9135         (let (gnus-article-prepare-hook
9136               gnus-display-mime-function
9137               gnus-break-pages)
9138           (gnus-summary-select-article))))
9139     (setq gnus-current-window-configuration conf)
9140     (let* ((name (format "%s-%d"
9141                          (gnus-group-prefixed-name
9142                           gnus-newsgroup-name (list 'nndoc ""))
9143                          (with-current-buffer gnus-summary-buffer
9144                            gnus-current-article)))
9145            (ogroup gnus-newsgroup-name)
9146            (params (append (gnus-info-params (gnus-get-info ogroup))
9147                            (list (cons 'to-group ogroup))
9148                            (list (cons 'parent-group ogroup))
9149                            (list (cons 'save-article-group ogroup))))
9150            (case-fold-search t)
9151            (buf (current-buffer))
9152            dig to-address)
9153       (with-current-buffer gnus-original-article-buffer
9154         ;; Have the digest group inherit the main mail address of
9155         ;; the parent article.
9156         (when (setq to-address (or (gnus-fetch-field "reply-to")
9157                                    (gnus-fetch-field "from")))
9158           (setq params
9159                 (append
9160                  (list (cons 'to-address
9161                              (funcall gnus-decode-encoded-address-function
9162                                       to-address))))))
9163         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9164         (insert-buffer-substring gnus-original-article-buffer)
9165         ;; Remove lines that may lead nndoc to misinterpret the
9166         ;; document type.
9167         (narrow-to-region
9168          (goto-char (point-min))
9169          (or (search-forward "\n\n" nil t) (point)))
9170         (goto-char (point-min))
9171         (delete-matching-lines "^Path:\\|^From ")
9172         (widen))
9173       (unwind-protect
9174           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9175                     (gnus-newsgroup-ephemeral-ignored-charsets
9176                      gnus-newsgroup-ignored-charsets))
9177                 (gnus-group-read-ephemeral-group
9178                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9179                               (nndoc-article-type
9180                                ,(if force 'mbox 'guess)))
9181                  t nil nil nil
9182                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9183                                                         "ADAPT")))))
9184               ;; Make all postings to this group go to the parent group.
9185               (nconc (gnus-info-params (gnus-get-info name))
9186                      params)
9187             ;; Couldn't select this doc group.
9188             (switch-to-buffer buf)
9189             (gnus-set-global-variables)
9190             (gnus-configure-windows 'summary)
9191             (gnus-message 3 "Article couldn't be entered?"))
9192         (kill-buffer dig)))))
9193
9194 (defun gnus-summary-read-document (n)
9195   "Open a new group based on the current article(s).
9196 This will allow you to read digests and other similar
9197 documents as newsgroups.
9198 Obeys the standard process/prefix convention."
9199   (interactive "P")
9200   (let* ((ogroup gnus-newsgroup-name)
9201          (params (append (gnus-info-params (gnus-get-info ogroup))
9202                          (list (cons 'to-group ogroup))))
9203          group egroup groups vgroup)
9204     (dolist (article (gnus-summary-work-articles n))
9205       (setq group (format "%s-%d" gnus-newsgroup-name article))
9206       (gnus-summary-remove-process-mark article)
9207       (when (gnus-summary-display-article article)
9208         (save-excursion ;;What for?
9209           (with-temp-buffer
9210             (insert-buffer-substring gnus-original-article-buffer)
9211             ;; Remove some headers that may lead nndoc to make
9212             ;; the wrong guess.
9213             (message-narrow-to-head)
9214             (goto-char (point-min))
9215             (delete-matching-lines "^Path:\\|^From ")
9216             (widen)
9217             (if (setq egroup
9218                       (gnus-group-read-ephemeral-group
9219                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9220                                      (nndoc-article-type guess))
9221                        t nil t))
9222                 (progn
9223                   ;; Make all postings to this group go to the parent group.
9224                   (nconc (gnus-info-params (gnus-get-info egroup))
9225                          params)
9226                   (push egroup groups))
9227               ;; Couldn't select this doc group.
9228               (gnus-error 3 "Article couldn't be entered"))))))
9229     ;; Now we have selected all the documents.
9230     (cond
9231      ((not groups)
9232       (error "None of the articles could be interpreted as documents"))
9233      ((gnus-group-read-ephemeral-group
9234        (setq vgroup (format
9235                      "nnvirtual:%s-%s" gnus-newsgroup-name
9236                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9237        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9238        t
9239        (cons (current-buffer) 'summary)))
9240      (t
9241       (error "Couldn't select virtual nndoc group")))))
9242
9243 (defun gnus-summary-widget-forward (arg)
9244   "Move point to the next field or button in the article.
9245 With optional ARG, move across that many fields."
9246   (interactive "p")
9247   (gnus-summary-select-article)
9248   (gnus-configure-windows 'article)
9249   (select-window (gnus-get-buffer-window gnus-article-buffer))
9250   (widget-forward arg))
9251
9252 (defun gnus-summary-isearch-article (&optional regexp-p)
9253   "Do incremental search forward on the current article.
9254 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9255   (interactive "P")
9256   (gnus-summary-select-article)
9257   (gnus-configure-windows 'article)
9258   (gnus-eval-in-buffer-window gnus-article-buffer
9259     (save-restriction
9260       (widen)
9261       (isearch-forward regexp-p))))
9262
9263 (defun gnus-summary-repeat-search-article-forward ()
9264   "Repeat the previous search forwards."
9265   (interactive)
9266   (unless gnus-last-search-regexp
9267     (error "No previous search"))
9268   (gnus-summary-search-article-forward gnus-last-search-regexp))
9269
9270 (defun gnus-summary-repeat-search-article-backward ()
9271   "Repeat the previous search backwards."
9272   (interactive)
9273   (unless gnus-last-search-regexp
9274     (error "No previous search"))
9275   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9276
9277 (defun gnus-summary-search-article-forward (regexp &optional backward)
9278   "Search for an article containing REGEXP forward.
9279 If BACKWARD, search backward instead."
9280   (interactive
9281    (list (read-string
9282           (format "Search article %s (regexp%s): "
9283                   (if current-prefix-arg "backward" "forward")
9284                   (if gnus-last-search-regexp
9285                       (concat ", default " gnus-last-search-regexp)
9286                     "")))
9287          current-prefix-arg))
9288   (if (string-equal regexp "")
9289       (setq regexp (or gnus-last-search-regexp ""))
9290     (setq gnus-last-search-regexp regexp)
9291     (setq gnus-article-before-search gnus-current-article))
9292   ;; Intentionally set gnus-last-article.
9293   (setq gnus-last-article gnus-article-before-search)
9294   (let ((gnus-last-article gnus-last-article))
9295     (if (gnus-summary-search-article regexp backward)
9296         (gnus-summary-show-thread)
9297       (signal 'search-failed (list regexp)))))
9298
9299 (defun gnus-summary-search-article-backward (regexp)
9300   "Search for an article containing REGEXP backward."
9301   (interactive
9302    (list (read-string
9303           (format "Search article backward (regexp%s): "
9304                   (if gnus-last-search-regexp
9305                       (concat ", default " gnus-last-search-regexp)
9306                     "")))))
9307   (gnus-summary-search-article-forward regexp 'backward))
9308
9309 (defun gnus-summary-search-article (regexp &optional backward)
9310   "Search for an article containing REGEXP.
9311 Optional argument BACKWARD means do search for backward.
9312 `gnus-select-article-hook' is not called during the search."
9313   ;; We have to require this here to make sure that the following
9314   ;; dynamic binding isn't shadowed by autoloading.
9315   (require 'gnus-async)
9316   (require 'gnus-art)
9317   (let ((gnus-select-article-hook nil)  ;Disable hook.
9318         (gnus-article-prepare-hook nil)
9319         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9320         (gnus-use-article-prefetch nil)
9321         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9322         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9323         (gnus-visual nil)
9324         (gnus-keep-backlog nil)
9325         (gnus-break-pages nil)
9326         (gnus-summary-display-arrow nil)
9327         (gnus-updated-mode-lines nil)
9328         (gnus-auto-center-summary nil)
9329         (sum (current-buffer))
9330         (gnus-display-mime-function nil)
9331         (found nil)
9332         point)
9333     (gnus-save-hidden-threads
9334       (gnus-summary-select-article)
9335       (set-buffer gnus-article-buffer)
9336       (goto-char (window-point (get-buffer-window (current-buffer))))
9337       (when backward
9338         (forward-line -1))
9339       (while (not found)
9340         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9341         (if (if backward
9342                 (re-search-backward regexp nil t)
9343               (re-search-forward regexp nil t))
9344             ;; We found the regexp.
9345             (progn
9346               (setq found 'found)
9347               (beginning-of-line)
9348               (set-window-start
9349                (get-buffer-window (current-buffer))
9350                (point))
9351               (forward-line 1)
9352               (set-window-point
9353                (get-buffer-window (current-buffer))
9354                (point))
9355               (set-buffer sum)
9356               (setq point (point)))
9357           ;; We didn't find it, so we go to the next article.
9358           (set-buffer sum)
9359           (setq found 'not)
9360           (while (eq found 'not)
9361             (if (not (if backward (gnus-summary-find-prev)
9362                        (gnus-summary-find-next)))
9363                 ;; No more articles.
9364                 (setq found t)
9365               ;; Select the next article and adjust point.
9366               (unless (gnus-summary-article-sparse-p
9367                        (gnus-summary-article-number))
9368                 (setq found nil)
9369                 (gnus-summary-select-article)
9370                 (set-buffer gnus-article-buffer)
9371                 (widen)
9372                 (goto-char (if backward (point-max) (point-min))))))))
9373       (gnus-message 7 ""))
9374     ;; Return whether we found the regexp.
9375     (when (eq found 'found)
9376       (goto-char point)
9377       (gnus-summary-show-thread)
9378       (gnus-summary-goto-subject gnus-current-article)
9379       (gnus-summary-position-point)
9380       t)))
9381
9382 (defun gnus-find-matching-articles (header regexp)
9383   "Return a list of all articles that match REGEXP on HEADER.
9384 This search includes all articles in the current group that Gnus has
9385 fetched headers for, whether they are displayed or not."
9386   (let ((articles nil)
9387         ;; Can't eta-reduce because it's a macro.
9388         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9389         (case-fold-search t))
9390     (dolist (header gnus-newsgroup-headers)
9391       (when (string-match regexp (funcall func header))
9392         (push (mail-header-number header) articles)))
9393     (nreverse articles)))
9394
9395 (defun gnus-summary-find-matching (header regexp &optional backward unread
9396                                           not-case-fold not-matching)
9397   "Return a list of all articles that match REGEXP on HEADER.
9398 The search stars on the current article and goes forwards unless
9399 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9400 If UNREAD is non-nil, only unread articles will
9401 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9402 in the comparisons. If NOT-MATCHING, return a list of all articles that
9403 not match REGEXP on HEADER."
9404   (let ((case-fold-search (not not-case-fold))
9405         articles d func)
9406     (if (consp header)
9407         (if (eq (car header) 'extra)
9408             (setq func
9409                   `(lambda (h)
9410                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9411                          "")))
9412           (error "%s is an invalid header" header))
9413       (unless (fboundp (intern (concat "mail-header-" header)))
9414         (error "%s is not a valid header" header))
9415       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9416     (dolist (d (if (eq backward 'all)
9417                    gnus-newsgroup-data
9418                  (gnus-data-find-list
9419                   (gnus-summary-article-number)
9420                   (gnus-data-list backward))))
9421       (when (and (or (not unread)       ; We want all articles...
9422                      (gnus-data-unread-p d)) ; Or just unreads.
9423                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9424                  (if not-matching
9425                      (not (string-match
9426                            regexp
9427                            (funcall func (gnus-data-header d))))
9428                    (string-match regexp
9429                                  (funcall func (gnus-data-header d)))))
9430         (push (gnus-data-number d) articles))) ; Success!
9431     (nreverse articles)))
9432
9433 (defun gnus-summary-execute-command (header regexp command &optional backward)
9434   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9435 If HEADER is an empty string (or nil), the match is done on the entire
9436 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9437   (interactive
9438    (list (let ((completion-ignore-case t))
9439            (gnus-completing-read
9440             "Header name"
9441             (mapcar 'symbol-name
9442                     (append
9443                      '(Number Subject From Lines Date
9444                        Message-ID Xref References Body)
9445                      gnus-extra-headers))
9446             'require-match))
9447          (read-string "Regexp: ")
9448          (read-key-sequence "Command: ")
9449          current-prefix-arg))
9450   (when (equal header "Body")
9451     (setq header ""))
9452   ;; Hidden thread subtrees must be searched as well.
9453   (gnus-summary-show-all-threads)
9454   ;; We don't want to change current point nor window configuration.
9455   (save-excursion
9456     (save-window-excursion
9457       (let (gnus-visual
9458             gnus-treat-strip-trailing-blank-lines
9459             gnus-treat-strip-leading-blank-lines
9460             gnus-treat-strip-multiple-blank-lines
9461             gnus-treat-hide-boring-headers
9462             gnus-treat-fold-newsgroups
9463             gnus-article-prepare-hook)
9464         (gnus-message 6 "Executing %s..." (key-description command))
9465         ;; We'd like to execute COMMAND interactively so as to give arguments.
9466         (gnus-execute header regexp
9467                       `(call-interactively ',(key-binding command))
9468                       backward)
9469         (gnus-message 6 "Executing %s...done" (key-description command))))))
9470
9471 (defun gnus-summary-beginning-of-article ()
9472   "Scroll the article back to the beginning."
9473   (interactive)
9474   (gnus-summary-select-article)
9475   (gnus-configure-windows 'article)
9476   (gnus-eval-in-buffer-window gnus-article-buffer
9477     (widen)
9478     (goto-char (point-min))
9479     (when gnus-break-pages
9480       (gnus-narrow-to-page))))
9481
9482 (defun gnus-summary-end-of-article ()
9483   "Scroll to the end of the article."
9484   (interactive)
9485   (gnus-summary-select-article)
9486   (gnus-configure-windows 'article)
9487   (gnus-eval-in-buffer-window gnus-article-buffer
9488     (widen)
9489     (goto-char (point-max))
9490     (recenter -3)
9491     (when gnus-break-pages
9492       (gnus-narrow-to-page))))
9493
9494 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9495   "Truncate to LEN and quote all \"(\"'s in STRING."
9496   (gnus-replace-in-string (if (and len (> (length string) len))
9497                               (substring string 0 len)
9498                             string)
9499                           "[()]" "\\\\\\&"))
9500
9501 (defun gnus-summary-print-article (&optional filename n)
9502   "Generate and print a PostScript image of the process-marked (mail) articles.
9503
9504 If used interactively, print the current article if none are
9505 process-marked.  With prefix arg, prompt the user for the name of the
9506 file to save in.
9507
9508 When used from Lisp, accept two optional args FILENAME and N.  N means
9509 to print the next N articles.  If N is negative, print the N previous
9510 articles.  If N is nil and articles have been marked with the process
9511 mark, print these instead.
9512
9513 If the optional first argument FILENAME is nil, send the image to the
9514 printer.  If FILENAME is a string, save the PostScript image in a file with
9515 that name.  If FILENAME is a number, prompt the user for the name of the file
9516 to save in."
9517   (interactive (list (ps-print-preprint current-prefix-arg)))
9518   (dolist (article (gnus-summary-work-articles n))
9519     (gnus-summary-select-article nil nil 'pseudo article)
9520     (gnus-eval-in-buffer-window gnus-article-buffer
9521       (gnus-print-buffer))
9522     (gnus-summary-remove-process-mark article))
9523   (ps-despool filename))
9524
9525 (defun gnus-print-buffer ()
9526   (let ((ps-left-header
9527          (list
9528           (concat "("
9529                   (gnus-summary-print-truncate-and-quote
9530                    (mail-header-subject gnus-current-headers)
9531                    66) ")")
9532           (concat "("
9533                   (gnus-summary-print-truncate-and-quote
9534                    (mail-header-from gnus-current-headers)
9535                    45) ")")))
9536         (ps-right-header
9537          (list
9538           "/pagenumberstring load"
9539           (concat "("
9540                   (mail-header-date gnus-current-headers) ")"))))
9541     (gnus-run-hooks 'gnus-ps-print-hook)
9542     (save-excursion
9543       (if ps-print-color-p
9544           (ps-spool-buffer-with-faces)
9545         (ps-spool-buffer)))))
9546
9547 (defun gnus-summary-show-complete-article ()
9548   "Show a complete version of the current article.
9549 This is only useful if you're looking at a partial version of the
9550 article currently."
9551   (interactive)
9552   (let ((gnus-keep-backlog nil)
9553         (gnus-use-cache nil)
9554         (gnus-agent nil)
9555         (variable (intern
9556                    (format "%s-fetch-partial-articles"
9557                            (car (gnus-find-method-for-group
9558                                  gnus-newsgroup-name)))
9559                    obarray))
9560         old-val)
9561     (unwind-protect
9562         (progn
9563           (setq old-val (symbol-value variable))
9564           (set variable nil)
9565           (gnus-flush-original-article-buffer)
9566           (gnus-summary-show-article))
9567       (set variable old-val))))
9568
9569 (defun gnus-summary-show-article (&optional arg)
9570   "Force redisplaying of the current article.
9571 If ARG (the prefix) is a number, show the article with the charset
9572 defined in `gnus-summary-show-article-charset-alist', or the charset
9573 input.
9574 If ARG (the prefix) is non-nil and not a number, show the article,
9575 but without running any of the article treatment functions
9576 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9577 C-u g', show the raw article."
9578   (interactive "P")
9579   (cond
9580    ((numberp arg)
9581     (gnus-summary-show-article t)
9582     (let ((gnus-newsgroup-charset
9583            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9584                (mm-read-coding-system
9585                 "View as charset: " ;; actually it is coding system.
9586                 (with-current-buffer gnus-article-buffer
9587                   (mm-detect-coding-region (point) (point-max))))))
9588           (gnus-newsgroup-ignored-charsets 'gnus-all))
9589       (gnus-summary-select-article nil 'force)
9590       (let ((deps gnus-newsgroup-dependencies)
9591             head header lines)
9592         (with-current-buffer gnus-original-article-buffer
9593           (save-restriction
9594             (message-narrow-to-head)
9595             (setq head (buffer-string))
9596             (goto-char (point-min))
9597             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9598               (goto-char (point-max))
9599               (widen)
9600               (setq lines (1- (count-lines (point) (point-max))))))
9601           (with-temp-buffer
9602             (insert (format "211 %d Article retrieved.\n"
9603                             (cdr gnus-article-current)))
9604             (insert head)
9605             (if lines (insert (format "Lines: %d\n" lines)))
9606             (insert ".\n")
9607             (let ((nntp-server-buffer (current-buffer)))
9608               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9609         (gnus-data-set-header
9610          (gnus-data-find (cdr gnus-article-current))
9611          header)
9612         (gnus-summary-update-article-line
9613          (cdr gnus-article-current) header)
9614         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9615           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9616    ((not arg)
9617     ;; Select the article the normal way.
9618     (gnus-summary-select-article nil 'force))
9619    ((equal arg '(16))
9620     ;; C-u C-u g
9621     (let ((gnus-inhibit-article-treatments t))
9622       (gnus-summary-select-article nil 'force)))
9623    (t
9624     ;; We have to require this here to make sure that the following
9625     ;; dynamic binding isn't shadowed by autoloading.
9626     (require 'gnus-async)
9627     (require 'gnus-art)
9628     ;; Bind the article treatment functions to nil.
9629     (let ((gnus-have-all-headers t)
9630           gnus-article-prepare-hook
9631           gnus-article-decode-hook
9632           gnus-display-mime-function
9633           gnus-break-pages)
9634       ;; Destroy any MIME parts.
9635       (when (gnus-buffer-live-p gnus-article-buffer)
9636         (with-current-buffer gnus-article-buffer
9637           (gnus-article-stop-animations)
9638           (mm-destroy-parts gnus-article-mime-handles)
9639           ;; Set it to nil for safety reason.
9640           (setq gnus-article-mime-handle-alist nil)
9641           (setq gnus-article-mime-handles nil)))
9642       (gnus-summary-select-article nil 'force))))
9643   (gnus-summary-goto-subject gnus-current-article)
9644   (gnus-summary-position-point))
9645
9646 (defun gnus-summary-show-raw-article ()
9647   "Show the raw article without any article massaging functions being run."
9648   (interactive)
9649   (gnus-summary-show-article t))
9650
9651 (defun gnus-summary-verbose-headers (&optional arg)
9652   "Toggle permanent full header display.
9653 If ARG is a positive number, turn header display on.
9654 If ARG is a negative number, turn header display off."
9655   (interactive "P")
9656   (setq gnus-show-all-headers
9657         (cond ((or (not (numberp arg))
9658                    (zerop arg))
9659                (not gnus-show-all-headers))
9660               ((natnump arg)
9661                t)))
9662   (gnus-summary-show-article))
9663
9664 (defun gnus-summary-toggle-header (&optional arg)
9665   "Show the headers if they are hidden, or hide them if they are shown.
9666 If ARG is a positive number, show the entire header.
9667 If ARG is a negative number, hide the unwanted header lines."
9668   (interactive "P")
9669   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9670                      (get-buffer-window gnus-article-buffer t))))
9671     (with-current-buffer gnus-article-buffer
9672       (widen)
9673       (article-narrow-to-head)
9674       (let* ((inhibit-read-only t)
9675              (inhibit-point-motion-hooks t)
9676              (hidden (if (numberp arg)
9677                          (>= arg 0)
9678                        (or (not (looking-at "[^ \t\n]+:"))
9679                            (gnus-article-hidden-text-p 'headers))))
9680              s e)
9681         (delete-region (point-min) (point-max))
9682         (with-current-buffer gnus-original-article-buffer
9683           (goto-char (setq s (point-min)))
9684           (setq e (if (search-forward "\n\n" nil t)
9685                       (1- (point))
9686                     (point-max))))
9687         (insert-buffer-substring gnus-original-article-buffer s e)
9688         (run-hooks 'gnus-article-decode-hook)
9689         (if hidden
9690             (let ((gnus-treat-hide-headers nil)
9691                   (gnus-treat-hide-boring-headers nil))
9692               (gnus-delete-wash-type 'headers)
9693               (gnus-treat-article 'head))
9694           (gnus-treat-article 'head))
9695         (widen)
9696         (if window
9697             (set-window-start window (goto-char (point-min))))
9698         (if gnus-break-pages
9699             (gnus-narrow-to-page)
9700           (when (gnus-visual-p 'page-marker)
9701             (let ((inhibit-read-only t))
9702               (gnus-remove-text-with-property 'gnus-prev)
9703               (gnus-remove-text-with-property 'gnus-next))))
9704         (gnus-set-mode-line 'article)))))
9705
9706 (defun gnus-summary-show-all-headers ()
9707   "Make all header lines visible."
9708   (interactive)
9709   (gnus-summary-toggle-header 1))
9710
9711 (defun gnus-summary-caesar-message (&optional arg)
9712   "Caesar rotate the current article by 13.
9713 With a non-numerical prefix, also rotate headers.  A numerical
9714 prefix specifies how many places to rotate each letter forward."
9715   (interactive "P")
9716   (gnus-summary-select-article)
9717   (let ((mail-header-separator ""))
9718     (gnus-eval-in-buffer-window gnus-article-buffer
9719       (save-restriction
9720         (widen)
9721         (let ((start (window-start))
9722               (inhibit-read-only t))
9723           (if (equal arg '(4))
9724               (message-caesar-buffer-body nil t)
9725             (message-caesar-buffer-body arg))
9726           (set-window-start (get-buffer-window (current-buffer)) start)))))
9727   ;; Create buttons and stuff...
9728   (gnus-treat-article nil))
9729
9730 (declare-function idna-to-unicode "ext:idna" (str))
9731
9732 (defun gnus-summary-idna-message (&optional arg)
9733   "Decode IDNA encoded domain names in the current articles.
9734 IDNA encoded domain names looks like `xn--bar'.  If a string
9735 remain unencoded after running this function, it is likely an
9736 invalid IDNA string (`xn--bar' is invalid).
9737
9738 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9739 installed for this command to work."
9740   (interactive "P")
9741   (if (not (and (condition-case nil (require 'idna)
9742                   (file-error))
9743                 (mm-coding-system-p 'utf-8)
9744                 (executable-find (symbol-value 'idna-program))))
9745       (gnus-message
9746        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9747     (gnus-summary-select-article)
9748     (let ((mail-header-separator ""))
9749       (gnus-eval-in-buffer-window gnus-article-buffer
9750         (save-restriction
9751           (widen)
9752           (let ((start (window-start))
9753                 buffer-read-only)
9754             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9755               (replace-match (idna-to-unicode (match-string 1))))
9756             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9757
9758 (defun gnus-summary-morse-message (&optional arg)
9759   "Morse decode the current article."
9760   (interactive "P")
9761   (gnus-summary-select-article)
9762   (let ((mail-header-separator ""))
9763     (gnus-eval-in-buffer-window gnus-article-buffer
9764       (save-excursion
9765         (save-restriction
9766           (widen)
9767           (let ((pos (window-start))
9768                 (inhibit-read-only t))
9769             (goto-char (point-min))
9770             (when (message-goto-body)
9771               (gnus-narrow-to-body))
9772             (goto-char (point-min))
9773             (while (search-forward "·" (point-max) t)
9774               (replace-match "."))
9775             (unmorse-region (point-min) (point-max))
9776             (widen)
9777             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9778
9779 (defun gnus-summary-stop-page-breaking ()
9780   "Stop page breaking in the current article."
9781   (interactive)
9782   (gnus-summary-select-article)
9783   (gnus-eval-in-buffer-window gnus-article-buffer
9784     (widen)
9785     (when (gnus-visual-p 'page-marker)
9786       (let ((inhibit-read-only t))
9787         (gnus-remove-text-with-property 'gnus-prev)
9788         (gnus-remove-text-with-property 'gnus-next))
9789       (setq gnus-page-broken nil))))
9790
9791 (defun gnus-summary-move-article (&optional n to-newsgroup
9792                                             select-method action)
9793   "Move the current article to a different newsgroup.
9794 If N is a positive number, move the N next articles.
9795 If N is a negative number, move the N previous articles.
9796 If N is nil and any articles have been marked with the process mark,
9797 move those articles instead.
9798 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9799 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9800 re-spool using this method.
9801
9802 When called interactively with TO-NEWSGROUP being nil, the value of
9803 the variable `gnus-move-split-methods' is used for finding a default
9804 for the target newsgroup.
9805
9806 For this function to work, both the current newsgroup and the
9807 newsgroup that you want to move to have to support the `request-move'
9808 and `request-accept' functions.
9809
9810 ACTION can be either `move' (the default), `crosspost' or `copy'."
9811   (interactive "P")
9812   (unless action
9813     (setq action 'move))
9814   ;; Check whether the source group supports the required functions.
9815   (cond ((and (eq action 'move)
9816               (not (gnus-check-backend-function
9817                     'request-move-article gnus-newsgroup-name)))
9818          (error "The current group does not support article moving"))
9819         ((and (eq action 'crosspost)
9820               (not (gnus-check-backend-function
9821                     'request-replace-article gnus-newsgroup-name)))
9822          (error "The current group does not support article editing")))
9823   (let ((articles (gnus-summary-work-articles n))
9824         (prefix (if (gnus-check-backend-function
9825                      'request-move-article gnus-newsgroup-name)
9826                     (funcall gnus-move-group-prefix-function
9827                              gnus-newsgroup-name)
9828                   ""))
9829         (names '((move "Move" "Moving")
9830                  (copy "Copy" "Copying")
9831                  (crosspost "Crosspost" "Crossposting")))
9832         (copy-buf (save-excursion
9833                     (nnheader-set-temp-buffer " *copy article*")))
9834         art-group to-method new-xref article to-groups
9835         articles-to-update-marks encoded)
9836     (unless (assq action names)
9837       (error "Unknown action %s" action))
9838     ;; Read the newsgroup name.
9839     (when (and (not to-newsgroup)
9840                (not select-method))
9841       (if (and gnus-move-split-methods
9842                (not
9843                 (and (memq gnus-current-article articles)
9844                      (gnus-buffer-live-p gnus-original-article-buffer))))
9845           ;; When `gnus-move-split-methods' is non-nil, we have to
9846           ;; select an article to give `gnus-read-move-group-name' an
9847           ;; opportunity to suggest an appropriate default.  However,
9848           ;; we needn't render or mark the article.
9849           (let ((gnus-display-mime-function nil)
9850                 (gnus-article-prepare-hook nil)
9851                 (gnus-mark-article-hook nil))
9852             (gnus-summary-select-article nil nil nil (car articles))))
9853       (setq to-newsgroup (gnus-read-move-group-name
9854                           (cadr (assq action names))
9855                           (symbol-value
9856                            (intern (format "gnus-current-%s-group" action)))
9857                           articles prefix)
9858             encoded to-newsgroup
9859             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9860       (set (intern (format "gnus-current-%s-group" action))
9861            (mm-decode-coding-string
9862             to-newsgroup
9863             (gnus-group-name-charset to-method to-newsgroup))))
9864     (unless to-method
9865       (setq to-method (or select-method
9866                           (gnus-server-to-method
9867                            (gnus-group-method to-newsgroup)))))
9868     (setq to-newsgroup
9869           (or encoded
9870               (and to-newsgroup
9871                    (mm-encode-coding-string
9872                     to-newsgroup
9873                     (gnus-group-name-charset to-method to-newsgroup)))))
9874     ;; Check the method we are to move this article to...
9875     (unless (gnus-check-backend-function
9876              'request-accept-article (car to-method))
9877       (error "%s does not support article copying" (car to-method)))
9878     (unless (gnus-check-server to-method)
9879       (error "Can't open server %s" (car to-method)))
9880     (gnus-message 6 "%s to %s: %s..."
9881                   (caddr (assq action names))
9882                   (or (car select-method)
9883                       (gnus-group-decoded-name to-newsgroup))
9884                   articles)
9885     (while articles
9886       (setq article (pop articles))
9887       ;; Set any marks that may have changed in the summary buffer.
9888       (when gnus-preserve-marks
9889         (gnus-summary-push-marks-to-backend article))
9890       (setq
9891        art-group
9892        (cond
9893         ;; Move the article.
9894         ((eq action 'move)
9895          ;; Remove this article from future suppression.
9896          (gnus-dup-unsuppress-article article)
9897          (let* ((from-method (gnus-find-method-for-group
9898                               gnus-newsgroup-name))
9899                 (to-method (or select-method
9900                                (gnus-find-method-for-group to-newsgroup)))
9901                 (move-is-internal (gnus-server-equal from-method to-method)))
9902            (gnus-request-move-article
9903             article                     ; Article to move
9904             gnus-newsgroup-name         ; From newsgroup
9905             (nth 1 (gnus-find-method-for-group
9906                     gnus-newsgroup-name)) ; Server
9907             (list 'gnus-request-accept-article
9908                   to-newsgroup (list 'quote select-method)
9909                   (not articles) t)     ; Accept form
9910             (not articles)              ; Only save nov last time
9911             (and move-is-internal
9912                  to-newsgroup           ; Not respooling
9913                                         ; Is this move internal?
9914                  (gnus-group-real-name to-newsgroup)))))
9915         ;; Copy the article.
9916         ((eq action 'copy)
9917          (with-current-buffer copy-buf
9918            (when (gnus-request-article-this-buffer article
9919                                                    gnus-newsgroup-name)
9920              (save-restriction
9921                (nnheader-narrow-to-headers)
9922                (dolist (hdr gnus-copy-article-ignored-headers)
9923                  (message-remove-header hdr t)))
9924              (gnus-request-accept-article
9925               to-newsgroup select-method (not articles) t))))
9926         ;; Crosspost the article.
9927         ((eq action 'crosspost)
9928          (let ((xref (message-tokenize-header
9929                       (mail-header-xref (gnus-summary-article-header
9930                                          article))
9931                       " ")))
9932            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9933                                   ":" (number-to-string article)))
9934            (unless xref
9935              (setq xref (list (system-name))))
9936            (setq new-xref
9937                  (concat
9938                   (mapconcat 'identity
9939                              (delete "Xref:" (delete new-xref xref))
9940                              " ")
9941                   " " new-xref))
9942            (with-current-buffer copy-buf
9943              ;; First put the article in the destination group.
9944              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9945              (when (consp (setq art-group
9946                                 (gnus-request-accept-article
9947                                  to-newsgroup select-method (not articles)
9948                                  t)))
9949                (setq new-xref (concat new-xref " " (car art-group)
9950                                       ":"
9951                                       (number-to-string (cdr art-group))))
9952                ;; Now we have the new Xrefs header, so we insert
9953                ;; it and replace the new article.
9954                (nnheader-replace-header "Xref" new-xref)
9955                (gnus-request-replace-article
9956                 (cdr art-group) to-newsgroup (current-buffer) t)
9957                art-group))))))
9958       (cond
9959        ((not art-group)
9960         (gnus-message 1 "Couldn't %s article %s: %s"
9961                       (cadr (assq action names)) article
9962                       (nnheader-get-report (car to-method))))
9963        ((eq art-group 'junk)
9964         (when (eq action 'move)
9965           (gnus-summary-mark-article article gnus-canceled-mark)
9966           (gnus-message 4 "Deleted article %s" article)
9967           ;; run the delete hook
9968           (run-hook-with-args 'gnus-summary-article-delete-hook
9969                               action
9970                               (gnus-data-header
9971                                (assoc article (gnus-data-list nil)))
9972                               gnus-newsgroup-name nil
9973                               select-method)))
9974        (t
9975         (let* ((pto-group (gnus-group-prefixed-name
9976                            (car art-group) to-method))
9977                (info (gnus-get-info pto-group))
9978                (to-group (gnus-info-group info))
9979                to-marks)
9980           ;; Update the group that has been moved to.
9981           (when (and info
9982                      (memq action '(move copy)))
9983             (unless (member to-group to-groups)
9984               (push to-group to-groups))
9985
9986             (when (and (not (memq article gnus-newsgroup-unreads))
9987                        (cdr art-group))
9988               (push 'read to-marks)
9989               (gnus-info-set-read
9990                info (gnus-add-to-range (gnus-info-read info)
9991                                        (list (cdr art-group)))))
9992
9993             ;; See whether the article is to be put in the cache.
9994             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9995                    (marks (if expirable
9996                               gnus-article-mark-lists
9997                             (delete '(expirable . expire)
9998                                     (copy-sequence
9999                                      gnus-article-mark-lists))))
10000                    (to-article (cdr art-group)))
10001
10002               ;; Enter the article into the cache in the new group,
10003               ;; if that is required.
10004               (when (and to-article
10005                          gnus-use-cache)
10006                 (gnus-cache-possibly-enter-article
10007                  to-group to-article
10008                  (memq article gnus-newsgroup-marked)
10009                  (memq article gnus-newsgroup-dormant)
10010                  (memq article gnus-newsgroup-unreads)))
10011
10012               (when (and gnus-preserve-marks
10013                          to-article)
10014                 ;; Copy any marks over to the new group.
10015                 (when (and (equal to-group gnus-newsgroup-name)
10016                            (not (memq article gnus-newsgroup-unreads)))
10017                   ;; Mark this article as read in this group.
10018                   (push (cons to-article gnus-read-mark)
10019                         gnus-newsgroup-reads)
10020                   ;; Increase the active status of this group.
10021                   (setcdr (gnus-active to-group) to-article)
10022                   (setcdr gnus-newsgroup-active to-article))
10023
10024                 (while marks
10025                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10026                     (when (memq article (symbol-value
10027                                          (intern (format "gnus-newsgroup-%s"
10028                                                          (caar marks)))))
10029                       (push (cdar marks) to-marks)
10030                       ;; If the other group is the same as this group,
10031                       ;; then we have to add the mark to the list.
10032                       (when (equal to-group gnus-newsgroup-name)
10033                         (set (intern (format "gnus-newsgroup-%s"
10034                                              (caar marks)))
10035                              (cons to-article
10036                                    (symbol-value
10037                                     (intern (format "gnus-newsgroup-%s"
10038                                                     (caar marks)))))))
10039                       ;; Copy the marks to other group.
10040                       (gnus-add-marked-articles
10041                        to-group (cdar marks) (list to-article) info)))
10042                   (setq marks (cdr marks)))
10043
10044                 (when (and expirable
10045                            gnus-mark-copied-or-moved-articles-as-expirable
10046                            (not (memq 'expire to-marks)))
10047                   ;; Mark this article as expirable.
10048                   (push 'expire to-marks)
10049                   (when (equal to-group gnus-newsgroup-name)
10050                     (push to-article gnus-newsgroup-expirable))
10051                   ;; Copy the expirable mark to other group.
10052                   (gnus-add-marked-articles
10053                    to-group 'expire (list to-article) info))
10054
10055                 (when to-marks
10056                   (gnus-request-set-mark
10057                    to-group (list (list (list to-article) 'add to-marks)))))
10058
10059               (gnus-dribble-enter
10060                (concat "(gnus-group-set-info '"
10061                        (gnus-prin1-to-string (gnus-get-info to-group))
10062                        ")")
10063                (concat "^(gnus-group-set-info '(\""
10064                        (regexp-quote to-group) "\""))))
10065
10066           ;; Update the Xref header in this article to point to
10067           ;; the new crossposted article we have just created.
10068           (when (eq action 'crosspost)
10069             (with-current-buffer copy-buf
10070               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10071               (nnheader-replace-header "Xref" new-xref)
10072               (gnus-request-replace-article
10073                article gnus-newsgroup-name (current-buffer) t)))
10074
10075           ;; run the move/copy/crosspost/respool hook
10076           (run-hook-with-args 'gnus-summary-article-move-hook
10077                               action
10078                               (gnus-data-header
10079                                (assoc article (gnus-data-list nil)))
10080                               gnus-newsgroup-name
10081                               to-newsgroup
10082                               select-method))
10083
10084         ;;;!!!Why is this necessary?
10085         (set-buffer gnus-summary-buffer)
10086
10087         (when (eq action 'move)
10088           (save-excursion
10089             (gnus-summary-goto-subject article)
10090             (gnus-summary-mark-article article gnus-canceled-mark)))))
10091       (push article articles-to-update-marks))
10092
10093     (save-excursion
10094       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10095     ;; Re-activate all groups that have been moved to.
10096     (with-current-buffer gnus-group-buffer
10097       (let ((gnus-group-marked to-groups))
10098         (gnus-group-get-new-news-this-group nil t)))
10099
10100     (gnus-kill-buffer copy-buf)
10101     (gnus-summary-position-point)
10102     (gnus-set-mode-line 'summary)))
10103
10104 (defun gnus-summary-push-marks-to-backend (article)
10105   (let ((set nil)
10106         (marks gnus-article-mark-lists))
10107     (unless (memq article gnus-newsgroup-unreads)
10108       (push 'read set))
10109     (while marks
10110       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10111                  (memq article (symbol-value
10112                                 (intern (format "gnus-newsgroup-%s"
10113                                                 (caar marks))))))
10114         (push (cdar marks) set))
10115       (pop marks))
10116     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10117
10118 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10119   "Copy the current article to some other group.
10120 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10121 When called interactively, if TO-NEWSGROUP is nil, use the value of
10122 the variable `gnus-move-split-methods' for finding a default target
10123 newsgroup.
10124 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10125 re-spool using this method."
10126   (interactive "P")
10127   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10128
10129 (defun gnus-summary-crosspost-article (&optional n)
10130   "Crosspost the current article to some other group."
10131   (interactive "P")
10132   (gnus-summary-move-article n nil nil 'crosspost))
10133
10134 (defcustom gnus-summary-respool-default-method nil
10135   "Default method type for respooling an article.
10136 If nil, use to the current newsgroup method."
10137   :type 'symbol
10138   :group 'gnus-summary-mail)
10139
10140 (defun gnus-summary-respool-article (&optional n method)
10141   "Respool the current article.
10142 The article will be squeezed through the mail spooling process again,
10143 which means that it will be put in some mail newsgroup or other
10144 depending on `nnmail-split-methods'.
10145 If N is a positive number, respool the N next articles.
10146 If N is a negative number, respool the N previous articles.
10147 If N is nil and any articles have been marked with the process mark,
10148 respool those articles instead.
10149
10150 Respooling can be done both from mail groups and \"real\" newsgroups.
10151 In the former case, the articles in question will be moved from the
10152 current group into whatever groups they are destined to.  In the
10153 latter case, they will be copied into the relevant groups."
10154   (interactive
10155    (list current-prefix-arg
10156          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10157                 (methname
10158                  (symbol-name (or gnus-summary-respool-default-method
10159                                   (car (gnus-find-method-for-group
10160                                         gnus-newsgroup-name)))))
10161                 (method
10162                  (gnus-completing-read
10163                   "Backend to use when respooling"
10164                   methods t nil 'gnus-mail-method-history methname))
10165                 ms)
10166            (cond
10167             ((zerop (length (setq ms (gnus-servers-using-backend
10168                                       (intern method)))))
10169              (list (intern method) ""))
10170             ((= 1 (length ms))
10171              (car ms))
10172             (t
10173              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10174                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10175                            ms-alist))))))))
10176   (unless method
10177     (error "No method given for respooling"))
10178   (if (assoc (symbol-name
10179               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10180              (gnus-methods-using 'respool))
10181       (gnus-summary-move-article n nil method)
10182     (gnus-summary-copy-article n nil method)))
10183
10184 (defun gnus-summary-import-article (file &optional edit)
10185   "Import an arbitrary file into a mail newsgroup."
10186   (interactive "fImport file: \nP")
10187   (let ((group gnus-newsgroup-name)
10188         (now (current-time))
10189         atts lines group-art)
10190     (unless (gnus-check-backend-function 'request-accept-article group)
10191       (error "%s does not support article importing" group))
10192     (or (file-readable-p file)
10193         (not (file-regular-p file))
10194         (error "Can't read %s" file))
10195     (with-current-buffer (gnus-get-buffer-create " *import file*")
10196       (erase-buffer)
10197       (nnheader-insert-file-contents file)
10198       (goto-char (point-min))
10199       (if (nnheader-article-p)
10200           (save-restriction
10201             (goto-char (point-min))
10202             (search-forward "\n\n" nil t)
10203             (narrow-to-region (point-min) (1- (point)))
10204             (goto-char (point-min))
10205             (unless (re-search-forward "^date:" nil t)
10206               (goto-char (point-max))
10207               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10208        ;; This doesn't look like an article, so we fudge some headers.
10209         (setq atts (file-attributes file)
10210               lines (count-lines (point-min) (point-max)))
10211         (insert "From: " (read-string "From: ") "\n"
10212                 "Subject: " (read-string "Subject: ") "\n"
10213                 "Date: " (message-make-date (nth 5 atts)) "\n"
10214                 "Message-ID: " (message-make-message-id) "\n"
10215                 "Lines: " (int-to-string lines) "\n"
10216                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10217       (setq group-art (gnus-request-accept-article group nil t))
10218       (kill-buffer (current-buffer)))
10219     (setq gnus-newsgroup-active (gnus-activate-group group))
10220     (forward-line 1)
10221     (gnus-summary-goto-article (cdr group-art) nil t)
10222     (when edit
10223       (gnus-summary-edit-article))))
10224
10225 (defun gnus-summary-create-article ()
10226   "Create an article in a mail newsgroup."
10227   (interactive)
10228   (let ((group gnus-newsgroup-name)
10229         (now (current-time))
10230         group-art)
10231     (unless (gnus-check-backend-function 'request-accept-article group)
10232       (error "%s does not support article importing" group))
10233     (with-current-buffer (gnus-get-buffer-create " *import file*")
10234       (erase-buffer)
10235       (goto-char (point-min))
10236       ;; This doesn't look like an article, so we fudge some headers.
10237       (insert "From: " (read-string "From: ") "\n"
10238               "Subject: " (read-string "Subject: ") "\n"
10239               "Date: " (message-make-date now) "\n"
10240               "Message-ID: " (message-make-message-id) "\n")
10241       (setq group-art (gnus-request-accept-article group nil t))
10242       (kill-buffer (current-buffer)))
10243     (setq gnus-newsgroup-active (gnus-activate-group group))
10244     (forward-line 1)
10245     (gnus-summary-goto-article (cdr group-art) nil t)
10246     (gnus-summary-edit-article)))
10247
10248 (defun gnus-summary-article-posted-p ()
10249   "Say whether the current (mail) article is available from news as well.
10250 This will be the case if the article has both been mailed and posted."
10251   (interactive)
10252   (let ((id (mail-header-references (gnus-summary-article-header)))
10253         (gnus-override-method (car (gnus-refer-article-methods))))
10254     (if (gnus-request-head id "")
10255         (gnus-message 2 "The current message was found on %s"
10256                       gnus-override-method)
10257       (gnus-message 2 "The current message couldn't be found on %s"
10258                     gnus-override-method)
10259       nil)))
10260
10261 (defun gnus-summary-expire-articles (&optional now)
10262   "Expire all articles that are marked as expirable in the current group."
10263   (interactive)
10264   (when (and (not gnus-group-is-exiting-without-update-p)
10265              (gnus-check-backend-function
10266               'request-expire-articles gnus-newsgroup-name))
10267     ;; This backend supports expiry.
10268     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10269            (expirable (if total
10270                           (progn
10271                             ;; We need to update the info for
10272                             ;; this group for `gnus-list-of-read-articles'
10273                             ;; to give us the right answer.
10274                             (gnus-run-hooks 'gnus-exit-group-hook)
10275                             (gnus-summary-update-info)
10276                             (gnus-list-of-read-articles gnus-newsgroup-name))
10277                         (setq gnus-newsgroup-expirable
10278                               (sort gnus-newsgroup-expirable '<))))
10279            (expiry-wait (if now 'immediate
10280                           (gnus-group-find-parameter
10281                            gnus-newsgroup-name 'expiry-wait)))
10282            (nnmail-expiry-target
10283             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10284                 nnmail-expiry-target))
10285            es)
10286       (when expirable
10287         ;; There are expirable articles in this group, so we run them
10288         ;; through the expiry process.
10289         (gnus-message 6 "Expiring articles...")
10290         (when (gnus-check-group gnus-newsgroup-name)
10291           ;; The list of articles that weren't expired is returned.
10292           (save-excursion
10293             (if expiry-wait
10294                 (let ((nnmail-expiry-wait-function nil)
10295                       (nnmail-expiry-wait expiry-wait))
10296                   (setq es (gnus-request-expire-articles
10297                             expirable gnus-newsgroup-name)))
10298               (setq es (gnus-request-expire-articles
10299                         expirable gnus-newsgroup-name)))
10300             (unless total
10301               (setq gnus-newsgroup-expirable es))
10302             ;; We go through the old list of expirable, and mark all
10303             ;; really expired articles as nonexistent.
10304             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10305               (let ((gnus-use-cache nil))
10306                 (dolist (article expirable)
10307                   (when (and (not (memq article es))
10308                              (gnus-data-find article))
10309                     (gnus-summary-mark-article article gnus-canceled-mark)
10310                     (run-hook-with-args 'gnus-summary-article-expire-hook
10311                                         'delete
10312                                         (gnus-data-header
10313                                          (assoc article (gnus-data-list nil)))
10314                                         gnus-newsgroup-name
10315                                         nil
10316                                         nil)))))))
10317         (gnus-message 6 "Expiring articles...done")))))
10318
10319 (defun gnus-summary-expire-articles-now ()
10320   "Expunge all expirable articles in the current group.
10321 This means that *all* articles that are marked as expirable will be
10322 deleted forever, right now."
10323   (interactive)
10324   (or gnus-expert-user
10325       (gnus-yes-or-no-p
10326        "Are you really, really sure you want to delete all expirable messages? ")
10327       (error "Phew!"))
10328   (gnus-summary-expire-articles t))
10329
10330 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10331 (defun gnus-summary-delete-article (&optional n)
10332   "Delete the N next (mail) articles.
10333 This command actually deletes articles.  This is not a marking
10334 command.  The article will disappear forever from your life, never to
10335 return.
10336
10337 If N is negative, delete backwards.
10338 If N is nil and articles have been marked with the process mark,
10339 delete these instead.
10340
10341 If `gnus-novice-user' is non-nil you will be asked for
10342 confirmation before the articles are deleted."
10343   (interactive "P")
10344   (unless (gnus-check-backend-function 'request-expire-articles
10345                                        gnus-newsgroup-name)
10346     (error "The current newsgroup does not support article deletion"))
10347   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10348     (error "Couldn't open server"))
10349   ;; Compute the list of articles to delete.
10350   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10351         (nnmail-expiry-target 'delete)
10352         not-deleted)
10353     (if (and gnus-novice-user
10354              (not (gnus-yes-or-no-p
10355                    (format "Do you really want to delete %s forever? "
10356                            (if (> (length articles) 1)
10357                                (format "these %s articles" (length articles))
10358                              "this article")))))
10359         ()
10360       ;; Delete the articles.
10361       (setq not-deleted (gnus-request-expire-articles
10362                          articles gnus-newsgroup-name 'force))
10363       (save-excursion
10364         (while articles
10365           (gnus-summary-remove-process-mark (car articles))
10366           ;; The backend might not have been able to delete the article
10367           ;; after all.
10368           (unless (memq (car articles) not-deleted)
10369             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10370             (let* ((article (car articles))
10371                    (ghead  (gnus-data-header
10372                             (assoc article (gnus-data-list nil)))))
10373               (run-hook-with-args 'gnus-summary-article-delete-hook
10374                                   'delete ghead gnus-newsgroup-name nil
10375                                   nil)))
10376           (setq articles (cdr articles))))
10377       (when not-deleted
10378         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10379     (gnus-summary-position-point)
10380     (gnus-set-mode-line 'summary)
10381     not-deleted))
10382
10383 (defun gnus-summary-edit-article (&optional arg)
10384   "Edit the current article.
10385 This will have permanent effect only in mail groups.
10386 If ARG is nil, edit the decoded articles.
10387 If ARG is 1, edit the raw articles.
10388 If ARG is 2, edit the raw articles even in read-only groups.
10389 If ARG is 3, edit the articles with the current handles.
10390 Otherwise, allow editing of articles even in read-only
10391 groups."
10392   (interactive "P")
10393   (let (force raw current-handles)
10394     (cond
10395      ((null arg))
10396      ((eq arg 1)
10397       (setq raw t))
10398      ((eq arg 2)
10399       (setq raw t
10400             force t))
10401      ((eq arg 3)
10402       (setq current-handles
10403             (and (gnus-buffer-live-p gnus-article-buffer)
10404                  (with-current-buffer gnus-article-buffer
10405                    (prog1
10406                        gnus-article-mime-handles
10407                      (setq gnus-article-mime-handles nil))))))
10408      (t
10409       (setq force t)))
10410     (when (and raw (not force)
10411                (member gnus-newsgroup-name '("nndraft:delayed"
10412                                              "nndraft:drafts"
10413                                              "nndraft:queue")))
10414       (error "Can't edit the raw article in group %s"
10415              gnus-newsgroup-name))
10416     (with-current-buffer gnus-summary-buffer
10417       (let ((mail-parse-charset gnus-newsgroup-charset)
10418             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10419         (gnus-set-global-variables)
10420         (when (and (not force)
10421                    (gnus-group-read-only-p))
10422           (error "The current newsgroup does not support article editing"))
10423         (gnus-summary-show-article t)
10424         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10425           (with-current-buffer gnus-article-buffer
10426             (mm-enable-multibyte)))
10427         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10428             (setq raw t))
10429         (gnus-article-edit-article
10430          (if raw 'ignore
10431            `(lambda ()
10432               (let ((mbl mml-buffer-list))
10433                 (setq mml-buffer-list nil)
10434                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10435                   (mime-to-mml ,'current-handles))
10436                 (let ((mbl1 mml-buffer-list))
10437                   (setq mml-buffer-list mbl)
10438                   (set (make-local-variable 'mml-buffer-list) mbl1))
10439                 (gnus-make-local-hook 'kill-buffer-hook)
10440                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10441          `(lambda (no-highlight)
10442             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10443                   (message-options message-options)
10444                   (message-options-set-recipient)
10445                   (mail-parse-ignored-charsets
10446                    ',gnus-newsgroup-ignored-charsets)
10447                   (rfc2047-header-encoding-alist
10448                    ',(let ((charset (gnus-group-name-charset
10449                                      (gnus-find-method-for-group
10450                                       gnus-newsgroup-name)
10451                                      gnus-newsgroup-name)))
10452                        (append (list (cons "Newsgroups" charset)
10453                                      (cons "Followup-To" charset)
10454                                      (cons "Xref" charset))
10455                                rfc2047-header-encoding-alist))))
10456               ,(if (not raw) '(progn
10457                                 (mml-to-mime)
10458                                 (mml-destroy-buffers)
10459                                 (remove-hook 'kill-buffer-hook
10460                                              'mml-destroy-buffers t)
10461                                 (kill-local-variable 'mml-buffer-list)))
10462               (gnus-summary-edit-article-done
10463                ,(or (mail-header-references gnus-current-headers) "")
10464                ,(gnus-group-read-only-p)
10465                ,gnus-summary-buffer no-highlight))))))))
10466
10467 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10468
10469 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10470                                                  no-highlight)
10471   "Make edits to the current article permanent."
10472   (interactive)
10473   (save-excursion
10474     ;; The buffer restriction contains the entire article if it exists.
10475     (when (article-goto-body)
10476       (let ((lines (count-lines (point) (point-max)))
10477             (length (- (point-max) (point)))
10478             (case-fold-search t)
10479             (body (copy-marker (point))))
10480         (goto-char (point-min))
10481         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10482           (delete-region (match-beginning 1) (match-end 1))
10483           (insert (number-to-string length)))
10484         (goto-char (point-min))
10485         (when (re-search-forward
10486                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10487           (delete-region (match-beginning 1) (match-end 1))
10488           (insert (number-to-string length)))
10489         (goto-char (point-min))
10490         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10491           (delete-region (match-beginning 1) (match-end 1))
10492           (insert (number-to-string lines))))))
10493   ;; Replace the article.
10494   (let ((buf (current-buffer))
10495         (article (cdr gnus-article-current))
10496         replace-result)
10497     (with-temp-buffer
10498       (insert-buffer-substring buf)
10499       (if (and (not read-only)
10500                (not (setq replace-result
10501                           (gnus-request-replace-article
10502                            article (car gnus-article-current)
10503                            (current-buffer) t))))
10504           (error "Couldn't replace article")
10505         ;; If we got a number back, then that's the new article number
10506         ;; for this article.  Otherwise, the article number didn't change.
10507         (when (numberp replace-result)
10508           (with-current-buffer gnus-summary-buffer
10509             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10510             (gnus-summary-limit gnus-newsgroup-limit)
10511             (setq article replace-result)
10512             (gnus-summary-goto-subject article t)))
10513         ;; Update the summary buffer.
10514         (if (and references
10515                  (equal (message-tokenize-header references " ")
10516                         (message-tokenize-header
10517                          (or (message-fetch-field "references") "") " ")))
10518             ;; We only have to update this line.
10519             (save-excursion
10520               (save-restriction
10521                 (message-narrow-to-head)
10522                 (let ((head (buffer-substring-no-properties
10523                              (point-min) (point-max)))
10524                       header)
10525                   (with-temp-buffer
10526                     (insert (format "211 %d Article retrieved.\n" article))
10527                     (insert head)
10528                     (insert ".\n")
10529                     (let ((nntp-server-buffer (current-buffer)))
10530                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10531                     (with-current-buffer gnus-summary-buffer
10532                       (gnus-data-set-header (gnus-data-find article) header)
10533                       (gnus-summary-update-article-line article header)
10534                       (if (gnus-summary-goto-subject article nil t)
10535                           (gnus-summary-update-secondary-mark article)))))))
10536           ;; Update threads.
10537           (set-buffer (or buffer gnus-summary-buffer))
10538           (gnus-summary-update-article article)
10539           (if (gnus-summary-goto-subject article nil t)
10540               (gnus-summary-update-secondary-mark article)))
10541         ;; Prettify the article buffer again.
10542         (unless no-highlight
10543           (with-current-buffer gnus-article-buffer
10544             ;;!!! Fix this -- article should be rehighlighted.
10545             ;;(gnus-run-hooks 'gnus-article-display-hook)
10546             (set-buffer gnus-original-article-buffer)
10547             (gnus-request-article
10548              article (car gnus-article-current) (current-buffer))))
10549         ;; Prettify the summary buffer line.
10550         (when (gnus-visual-p 'summary-highlight 'highlight)
10551           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10552
10553 (defun gnus-summary-edit-wash (key)
10554   "Perform editing command KEY in the article buffer."
10555   (interactive
10556    (list
10557     (progn
10558       (message "%s" (concat (this-command-keys) "- "))
10559       (read-char))))
10560   (message "")
10561   (gnus-summary-edit-article)
10562   (execute-kbd-macro (concat (this-command-keys) key))
10563   (gnus-article-edit-done))
10564
10565 ;;; Respooling
10566
10567 (defun gnus-summary-respool-query (&optional silent trace)
10568   "Query where the respool algorithm would put this article."
10569   (interactive)
10570   (let (gnus-mark-article-hook)
10571     (gnus-summary-select-article)
10572     (with-current-buffer gnus-original-article-buffer
10573       (let ((groups (nnmail-article-group 'identity trace)))
10574         (unless silent
10575           (if groups
10576               (message "This message would go to %s"
10577                        (mapconcat 'car groups ", "))
10578             (message "This message would go to no groups"))
10579           groups)))))
10580
10581 (defun gnus-summary-respool-trace ()
10582   "Trace where the respool algorithm would put this article.
10583 Display a buffer showing all fancy splitting patterns which matched."
10584   (interactive)
10585   (gnus-summary-respool-query nil t))
10586
10587 ;; Summary marking commands.
10588
10589 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10590   "Mark articles which has the same subject as read, and then select the next.
10591 If UNMARK is positive, remove any kind of mark.
10592 If UNMARK is negative, tick articles."
10593   (interactive "P")
10594   (when unmark
10595     (setq unmark (prefix-numeric-value unmark)))
10596   (let ((count
10597          (gnus-summary-mark-same-subject
10598           (gnus-summary-article-subject) unmark)))
10599     ;; Select next unread article.  If auto-select-same mode, should
10600     ;; select the first unread article.
10601     (gnus-summary-next-article t (and gnus-auto-select-same
10602                                       (gnus-summary-article-subject)))
10603     (gnus-message 7 "%d article%s marked as %s"
10604                   count (if (= count 1) " is" "s are")
10605                   (if unmark "unread" "read"))))
10606
10607 (defun gnus-summary-kill-same-subject (&optional unmark)
10608   "Mark articles which has the same subject as read.
10609 If UNMARK is positive, remove any kind of mark.
10610 If UNMARK is negative, tick articles."
10611   (interactive "P")
10612   (when unmark
10613     (setq unmark (prefix-numeric-value unmark)))
10614   (let ((count
10615          (gnus-summary-mark-same-subject
10616           (gnus-summary-article-subject) unmark)))
10617     ;; If marked as read, go to next unread subject.
10618     (when (null unmark)
10619       ;; Go to next unread subject.
10620       (gnus-summary-next-subject 1 t))
10621     (gnus-message 7 "%d articles are marked as %s"
10622                   count (if unmark "unread" "read"))))
10623
10624 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10625   "Mark articles with same SUBJECT as read, and return marked number.
10626 If optional argument UNMARK is positive, remove any kinds of marks.
10627 If optional argument UNMARK is negative, mark articles as unread instead."
10628   (let ((count 1))
10629     (save-excursion
10630       (cond
10631        ((null unmark)                   ; Mark as read.
10632         (while (and
10633                 (progn
10634                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10635                   (gnus-summary-show-thread) t)
10636                 (gnus-summary-find-subject subject))
10637           (setq count (1+ count))))
10638        ((> unmark 0)                    ; Tick.
10639         (while (and
10640                 (progn
10641                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10642                   (gnus-summary-show-thread) t)
10643                 (gnus-summary-find-subject subject))
10644           (setq count (1+ count))))
10645        (t                               ; Mark as unread.
10646         (while (and
10647                 (progn
10648                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10649                   (gnus-summary-show-thread) t)
10650                 (gnus-summary-find-subject subject))
10651           (setq count (1+ count)))))
10652       (gnus-set-mode-line 'summary)
10653       ;; Return the number of marked articles.
10654       count)))
10655
10656 (defun gnus-summary-mark-as-processable (n &optional unmark)
10657   "Set the process mark on the next N articles.
10658 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10659 the process mark instead.  The difference between N and the actual
10660 number of articles marked is returned."
10661   (interactive "P")
10662   (if (and (null n) (gnus-region-active-p))
10663       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10664     (setq n (prefix-numeric-value n))
10665     (let ((backward (< n 0))
10666           (n (abs n)))
10667       (while (and
10668               (> n 0)
10669               (if unmark
10670                   (gnus-summary-remove-process-mark
10671                    (gnus-summary-article-number))
10672                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10673               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10674         (setq n (1- n)))
10675       (when (/= 0 n)
10676         (gnus-message 7 "No more articles"))
10677       (gnus-summary-recenter)
10678       (gnus-summary-position-point)
10679       n)))
10680
10681 (defun gnus-summary-unmark-as-processable (n)
10682   "Remove the process mark from the next N articles.
10683 If N is negative, unmark backward instead.  The difference between N and
10684 the actual number of articles unmarked is returned."
10685   (interactive "P")
10686   (gnus-summary-mark-as-processable n t))
10687
10688 (defun gnus-summary-unmark-all-processable ()
10689   "Remove the process mark from all articles."
10690   (interactive)
10691   (save-excursion
10692     (while gnus-newsgroup-processable
10693       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10694   (gnus-summary-position-point))
10695
10696 (defun gnus-summary-add-mark (article type)
10697   "Mark ARTICLE with a mark of TYPE."
10698   (let ((vtype (car (assq type gnus-article-mark-lists)))
10699         var)
10700     (if (not vtype)
10701         (error "No such mark type: %s" type)
10702       (setq var (intern (format "gnus-newsgroup-%s" type)))
10703       (set var (cons article (symbol-value var)))
10704       (if (memq type '(processable cached replied forwarded recent saved))
10705           (gnus-summary-update-secondary-mark article)
10706         ;;; !!! This is bogus.  We should find out what primary
10707         ;;; !!! mark we want to set.
10708         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10709
10710 (defun gnus-summary-mark-as-expirable (n)
10711   "Mark N articles forward as expirable.
10712 If N is negative, mark backward instead.  The difference between N and
10713 the actual number of articles marked is returned."
10714   (interactive "p")
10715   (gnus-summary-mark-forward n gnus-expirable-mark))
10716
10717 (defun gnus-summary-mark-as-spam (n)
10718   "Mark N articles forward as spam.
10719 If N is negative, mark backward instead.  The difference between N and
10720 the actual number of articles marked is returned."
10721   (interactive "p")
10722   (gnus-summary-mark-forward n gnus-spam-mark))
10723
10724 (defun gnus-summary-mark-article-as-replied (article)
10725   "Mark ARTICLE as replied to and update the summary line.
10726 ARTICLE can also be a list of articles."
10727   (interactive (list (gnus-summary-article-number)))
10728   (let ((articles (if (listp article) article (list article))))
10729     (dolist (article articles)
10730       (unless (numberp article)
10731         (error "%s is not a number" article))
10732       (push article gnus-newsgroup-replied)
10733       (let ((inhibit-read-only t))
10734         (when (gnus-summary-goto-subject article nil t)
10735           (gnus-summary-update-secondary-mark article))))))
10736
10737 (defun gnus-summary-mark-article-as-forwarded (article)
10738   "Mark ARTICLE as forwarded and update the summary line.
10739 ARTICLE can also be a list of articles."
10740   (let ((articles (if (listp article) article (list article))))
10741     (dolist (article articles)
10742       (push article gnus-newsgroup-forwarded)
10743       (let ((inhibit-read-only t))
10744         (when (gnus-summary-goto-subject article nil t)
10745           (gnus-summary-update-secondary-mark article))))))
10746
10747 (defun gnus-summary-set-bookmark (article)
10748   "Set a bookmark in current article."
10749   (interactive (list (gnus-summary-article-number)))
10750   (when (or (not (get-buffer gnus-article-buffer))
10751             (not gnus-current-article)
10752             (not gnus-article-current)
10753             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10754     (error "No current article selected"))
10755   ;; Remove old bookmark, if one exists.
10756   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10757   ;; Set the new bookmark, which is on the form
10758   ;; (article-number . line-number-in-body).
10759   (push
10760    (cons article
10761          (with-current-buffer gnus-article-buffer
10762            (count-lines
10763             (min (point)
10764                  (save-excursion
10765                    (article-goto-body)
10766                    (point)))
10767             (point))))
10768    gnus-newsgroup-bookmarks)
10769   (gnus-message 6 "A bookmark has been added to the current article."))
10770
10771 (defun gnus-summary-remove-bookmark (article)
10772   "Remove the bookmark from the current article."
10773   (interactive (list (gnus-summary-article-number)))
10774   ;; Remove old bookmark, if one exists.
10775   (if (not (assq article gnus-newsgroup-bookmarks))
10776       (gnus-message 6 "No bookmark in current article.")
10777     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10778     (gnus-message 6 "Removed bookmark.")))
10779
10780 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10781 (defun gnus-summary-mark-as-dormant (n)
10782   "Mark N articles forward as dormant.
10783 If N is negative, mark backward instead.  The difference between N and
10784 the actual number of articles marked is returned."
10785   (interactive "p")
10786   (gnus-summary-mark-forward n gnus-dormant-mark))
10787
10788 (defun gnus-summary-set-process-mark (article)
10789   "Set the process mark on ARTICLE and update the summary line."
10790   (setq gnus-newsgroup-processable
10791         (cons article
10792               (delq article gnus-newsgroup-processable)))
10793   (when (gnus-summary-goto-subject article)
10794     (gnus-summary-show-thread)
10795     (gnus-summary-goto-subject article)
10796     (gnus-summary-update-secondary-mark article)))
10797
10798 (defun gnus-summary-remove-process-mark (&rest articles)
10799   "Remove the process mark from ARTICLES and update the summary line."
10800   (dolist (article articles)
10801     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10802     (when (gnus-summary-goto-subject article)
10803       (gnus-summary-show-thread)
10804       (gnus-summary-goto-subject article)
10805       (gnus-summary-update-secondary-mark article)))
10806   t)
10807
10808 (defun gnus-summary-set-saved-mark (article)
10809   "Set the process mark on ARTICLE and update the summary line."
10810   (push article gnus-newsgroup-saved)
10811   (when (gnus-summary-goto-subject article)
10812     (gnus-summary-update-secondary-mark article)))
10813
10814 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10815   "Mark N articles as read forwards.
10816 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10817 The difference between N and the actual number of articles marked is
10818 returned.
10819 If NO-EXPIRE, auto-expiry will be inhibited."
10820   (interactive "p")
10821   (gnus-summary-show-thread)
10822   (let ((backward (< n 0))
10823         (gnus-summary-goto-unread
10824          (and gnus-summary-goto-unread
10825               (not (eq gnus-summary-goto-unread 'never))
10826               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10827                                     gnus-ticked-mark gnus-dormant-mark)))))
10828         (n (abs n))
10829         (mark (or mark gnus-del-mark)))
10830     (while (and (> n 0)
10831                 (gnus-summary-mark-article nil mark no-expire)
10832                 (zerop (gnus-summary-next-subject
10833                         (if backward -1 1)
10834                         (and gnus-summary-goto-unread
10835                              (not (eq gnus-summary-goto-unread 'never)))
10836                         t)))
10837       (setq n (1- n)))
10838     (when (/= 0 n)
10839       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10840     (gnus-summary-recenter)
10841     (gnus-summary-position-point)
10842     (gnus-set-mode-line 'summary)
10843     n))
10844
10845 (defun gnus-summary-mark-article-as-read (mark)
10846   "Mark the current article quickly as read with MARK."
10847   (let ((article (gnus-summary-article-number)))
10848     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10849     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10850     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10851     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10852     (push (cons article mark) gnus-newsgroup-reads)
10853     ;; Possibly remove from cache, if that is used.
10854     (when gnus-use-cache
10855       (gnus-cache-enter-remove-article article))
10856     ;; Allow the backend to change the mark.
10857     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10858     ;; Check for auto-expiry.
10859     (when (and gnus-newsgroup-auto-expire
10860                (memq mark gnus-auto-expirable-marks))
10861       (setq mark gnus-expirable-mark)
10862       ;; Let the backend know about the mark change.
10863       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10864       (push article gnus-newsgroup-expirable))
10865     ;; Set the mark in the buffer.
10866     (gnus-summary-update-mark mark 'unread)
10867     t))
10868
10869 (defun gnus-summary-mark-article-as-unread (mark)
10870   "Mark the current article quickly as unread with MARK."
10871   (let* ((article (gnus-summary-article-number))
10872          (old-mark (gnus-summary-article-mark article)))
10873     ;; Allow the backend to change the mark.
10874     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10875     (if (eq mark old-mark)
10876         t
10877       (if (<= article 0)
10878           (progn
10879             (gnus-error 1 "Can't mark negative article numbers")
10880             nil)
10881         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10882         (setq gnus-newsgroup-spam-marked
10883               (delq article gnus-newsgroup-spam-marked))
10884         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10885         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10886         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10887         (cond ((= mark gnus-ticked-mark)
10888                (setq gnus-newsgroup-marked
10889                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10890                                               article)))
10891               ((= mark gnus-spam-mark)
10892                (setq gnus-newsgroup-spam-marked
10893                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10894                                               article)))
10895               ((= mark gnus-dormant-mark)
10896                (setq gnus-newsgroup-dormant
10897                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10898                                               article)))
10899               (t
10900                (setq gnus-newsgroup-unreads
10901                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10902                                               article))))
10903         (gnus-alist-pull article gnus-newsgroup-reads)
10904
10905         ;; See whether the article is to be put in the cache.
10906         (and gnus-use-cache
10907              (vectorp (gnus-summary-article-header article))
10908              (save-excursion
10909                (gnus-cache-possibly-enter-article
10910                 gnus-newsgroup-name article
10911                 (= mark gnus-ticked-mark)
10912                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10913
10914         ;; Fix the mark.
10915         (gnus-summary-update-mark mark 'unread)
10916         t))))
10917
10918 (defun gnus-summary-mark-article (&optional article mark no-expire)
10919   "Mark ARTICLE with MARK.  MARK can be any character.
10920 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10921 `??' (dormant) and `?E' (expirable).
10922 If MARK is nil, then the default character `?r' is used.
10923 If ARTICLE is nil, then the article on the current line will be
10924 marked.
10925 If NO-EXPIRE, auto-expiry will be inhibited."
10926   ;; The mark might be a string.
10927   (when (stringp mark)
10928     (setq mark (aref mark 0)))
10929   ;; If no mark is given, then we check auto-expiring.
10930   (when (null mark)
10931     (setq mark gnus-del-mark))
10932   (when (and (not no-expire)
10933              gnus-newsgroup-auto-expire
10934              (memq mark gnus-auto-expirable-marks))
10935     (setq mark gnus-expirable-mark))
10936   (let ((article (or article (gnus-summary-article-number)))
10937         (old-mark (gnus-summary-article-mark article)))
10938     ;; Allow the backend to change the mark.
10939     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10940     (if (eq mark old-mark)
10941         t
10942       (unless article
10943         (error "No article on current line"))
10944       (if (not (if (or (= mark gnus-unread-mark)
10945                        (= mark gnus-ticked-mark)
10946                        (= mark gnus-spam-mark)
10947                        (= mark gnus-dormant-mark))
10948                    (gnus-mark-article-as-unread article mark)
10949                  (gnus-mark-article-as-read article mark)))
10950           t
10951         ;; See whether the article is to be put in the cache.
10952         (and gnus-use-cache
10953              (not (= mark gnus-canceled-mark))
10954              (vectorp (gnus-summary-article-header article))
10955              (save-excursion
10956                (gnus-cache-possibly-enter-article
10957                 gnus-newsgroup-name article
10958                 (= mark gnus-ticked-mark)
10959                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10960
10961         (when (gnus-summary-goto-subject article nil t)
10962           (let ((inhibit-read-only t))
10963             (gnus-summary-show-thread)
10964             ;; Fix the mark.
10965             (gnus-summary-update-mark mark 'unread)
10966             t))))))
10967
10968 (defun gnus-summary-update-secondary-mark (article)
10969   "Update the secondary (read, process, cache) mark."
10970   (gnus-summary-update-mark
10971    (cond ((memq article gnus-newsgroup-processable)
10972           gnus-process-mark)
10973          ((memq article gnus-newsgroup-cached)
10974           gnus-cached-mark)
10975          ((memq article gnus-newsgroup-replied)
10976           gnus-replied-mark)
10977          ((memq article gnus-newsgroup-forwarded)
10978           gnus-forwarded-mark)
10979          ((memq article gnus-newsgroup-saved)
10980           gnus-saved-mark)
10981          ((memq article gnus-newsgroup-recent)
10982           gnus-recent-mark)
10983          ((memq article gnus-newsgroup-unseen)
10984           gnus-unseen-mark)
10985          (t gnus-no-mark))
10986    'replied)
10987   (when (gnus-visual-p 'summary-highlight 'highlight)
10988     (gnus-summary-highlight-line)
10989     (gnus-run-hooks 'gnus-summary-update-hook))
10990   t)
10991
10992 (defun gnus-summary-update-download-mark (article)
10993   "Update the download mark."
10994   (gnus-summary-update-mark
10995    (cond ((memq article gnus-newsgroup-undownloaded)
10996           gnus-undownloaded-mark)
10997          (gnus-newsgroup-agentized
10998           gnus-downloaded-mark)
10999          (t
11000           gnus-no-mark))
11001    'download)
11002   (gnus-summary-update-line t)
11003   t)
11004
11005 (defun gnus-summary-update-mark (mark type)
11006   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11007         (inhibit-read-only t))
11008     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11009     (when forward
11010       (when (looking-at "\r")
11011         (incf forward))
11012       (when (<= (+ forward (point)) (point-max))
11013         ;; Go to the right position on the line.
11014         (goto-char (+ forward (point)))
11015         ;; Replace the old mark with the new mark.
11016         (let ((to-insert
11017                (mm-subst-char-in-string
11018                 (char-after) mark
11019                 (buffer-substring (point) (1+ (point))))))
11020           (delete-region (point) (1+ (point)))
11021           (insert to-insert))
11022         ;; Optionally update the marks by some user rule.
11023         (when (eq type 'unread)
11024           (gnus-data-set-mark
11025            (gnus-data-find (gnus-summary-article-number)) mark)
11026           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11027
11028 (defun gnus-mark-article-as-read (article &optional mark)
11029   "Enter ARTICLE in the pertinent lists and remove it from others."
11030   ;; Make the article expirable.
11031   (let ((mark (or mark gnus-del-mark)))
11032     (setq gnus-newsgroup-expirable
11033           (if (= mark gnus-expirable-mark)
11034               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11035             (delq article gnus-newsgroup-expirable)))
11036     ;; Remove from unread and marked lists.
11037     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11038     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11039     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11040     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11041     (push (cons article mark) gnus-newsgroup-reads)
11042     ;; Possibly remove from cache, if that is used.
11043     (when gnus-use-cache
11044       (gnus-cache-enter-remove-article article))
11045     t))
11046
11047 (defun gnus-mark-article-as-unread (article &optional mark)
11048   "Enter ARTICLE in the pertinent lists and remove it from others."
11049   (let ((mark (or mark gnus-ticked-mark)))
11050     (if (<= article 0)
11051         (progn
11052           (gnus-error 1 "Can't mark negative article numbers")
11053           nil)
11054       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11055             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11056             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11057             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11058             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11059
11060       ;; Unsuppress duplicates?
11061       (when gnus-suppress-duplicates
11062         (gnus-dup-unsuppress-article article))
11063
11064       (cond ((= mark gnus-ticked-mark)
11065              (setq gnus-newsgroup-marked
11066                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11067             ((= mark gnus-spam-mark)
11068              (setq gnus-newsgroup-spam-marked
11069                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11070                                             article)))
11071             ((= mark gnus-dormant-mark)
11072              (setq gnus-newsgroup-dormant
11073                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11074             (t
11075              (setq gnus-newsgroup-unreads
11076                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11077       (gnus-alist-pull article gnus-newsgroup-reads)
11078       t)))
11079
11080 (defun gnus-summary-tick-article-forward (n)
11081   "Tick N articles forwards.
11082 If N is negative, tick backwards instead.
11083 The difference between N and the number of articles ticked is returned."
11084   (interactive "p")
11085   (gnus-summary-mark-forward n gnus-ticked-mark))
11086
11087 (defun gnus-summary-tick-article-backward (n)
11088   "Tick N articles backwards.
11089 The difference between N and the number of articles ticked is returned."
11090   (interactive "p")
11091   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11092
11093 (defun gnus-summary-tick-article (&optional article clear-mark)
11094   "Mark current article as unread.
11095 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11096 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11097   (interactive)
11098   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11099                                        gnus-ticked-mark)))
11100
11101 (defun gnus-summary-mark-as-read-forward (n)
11102   "Mark N articles as read forwards.
11103 If N is negative, mark backwards instead.
11104 The difference between N and the actual number of articles marked is
11105 returned."
11106   (interactive "p")
11107   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11108
11109 (defun gnus-summary-mark-as-read-backward (n)
11110   "Mark the N articles as read backwards.
11111 The difference between N and the actual number of articles marked is
11112 returned."
11113   (interactive "p")
11114   (gnus-summary-mark-forward
11115    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11116
11117 (defun gnus-summary-mark-as-read (&optional article mark)
11118   "Mark current article as read.
11119 ARTICLE specifies the article to be marked as read.
11120 MARK specifies a string to be inserted at the beginning of the line."
11121   (gnus-summary-mark-article article mark))
11122
11123 (defun gnus-summary-clear-mark-forward (n)
11124   "Clear marks from N articles forward.
11125 If N is negative, clear backward instead.
11126 The difference between N and the number of marks cleared is returned."
11127   (interactive "p")
11128   (gnus-summary-mark-forward n gnus-unread-mark))
11129
11130 (defun gnus-summary-clear-mark-backward (n)
11131   "Clear marks from N articles backward.
11132 The difference between N and the number of marks cleared is returned."
11133   (interactive "p")
11134   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11135
11136 (defun gnus-summary-mark-unread-as-read ()
11137   "Intended to be used by `gnus-mark-article-hook'."
11138   (when (memq gnus-current-article gnus-newsgroup-unreads)
11139     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11140
11141 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11142   "Intended to be used by `gnus-mark-article-hook'."
11143   (let ((mark (gnus-summary-article-mark)))
11144     (when (or (gnus-unread-mark-p mark)
11145               (gnus-read-mark-p mark))
11146       (gnus-summary-mark-article gnus-current-article
11147                                  (or new-mark gnus-read-mark)))))
11148
11149 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11150   "Intended to be used by `gnus-mark-article-hook'."
11151   (let ((mark (gnus-summary-article-mark)))
11152     (when (or (gnus-unread-mark-p mark)
11153               (gnus-read-mark-p mark))
11154       (gnus-summary-mark-article (gnus-summary-article-number)
11155                                  (or new-mark gnus-read-mark)))))
11156
11157 (defun gnus-summary-mark-unread-as-ticked ()
11158   "Intended to be used by `gnus-mark-article-hook'."
11159   (when (memq gnus-current-article gnus-newsgroup-unreads)
11160     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11161
11162 (defun gnus-summary-mark-region-as-read (point mark all)
11163   "Mark all unread articles between point and mark as read.
11164 If given a prefix, mark all articles between point and mark as read,
11165 even ticked and dormant ones."
11166   (interactive "r\nP")
11167   (save-excursion
11168     (let (article)
11169       (goto-char point)
11170       (beginning-of-line)
11171       (while (and
11172               (< (point) mark)
11173               (progn
11174                 (when (or all
11175                           (memq (setq article (gnus-summary-article-number))
11176                                 gnus-newsgroup-unreads))
11177                   (gnus-summary-mark-article article gnus-del-mark))
11178                 t)
11179               (gnus-summary-find-next))))))
11180
11181 (defun gnus-summary-mark-below (score mark)
11182   "Mark articles with score less than SCORE with MARK."
11183   (interactive "P\ncMark: ")
11184   (setq score (if score
11185                   (prefix-numeric-value score)
11186                 (or gnus-summary-default-score 0)))
11187   (with-current-buffer gnus-summary-buffer
11188     (goto-char (point-min))
11189     (while
11190         (progn
11191           (and (< (gnus-summary-article-score) score)
11192                (gnus-summary-mark-article nil mark))
11193           (gnus-summary-find-next)))))
11194
11195 (defun gnus-summary-kill-below (&optional score)
11196   "Mark articles with score below SCORE as read."
11197   (interactive "P")
11198   (gnus-summary-mark-below score gnus-killed-mark))
11199
11200 (defun gnus-summary-clear-above (&optional score)
11201   "Clear all marks from articles with score above SCORE."
11202   (interactive "P")
11203   (gnus-summary-mark-above score gnus-unread-mark))
11204
11205 (defun gnus-summary-tick-above (&optional score)
11206   "Tick all articles with score above SCORE."
11207   (interactive "P")
11208   (gnus-summary-mark-above score gnus-ticked-mark))
11209
11210 (defun gnus-summary-mark-above (score mark)
11211   "Mark articles with score over SCORE with MARK."
11212   (interactive "P\ncMark: ")
11213   (setq score (if score
11214                   (prefix-numeric-value score)
11215                 (or gnus-summary-default-score 0)))
11216   (with-current-buffer gnus-summary-buffer
11217     (goto-char (point-min))
11218     (while (and (progn
11219                   (when (> (gnus-summary-article-score) score)
11220                     (gnus-summary-mark-article nil mark))
11221                   t)
11222                 (gnus-summary-find-next)))))
11223
11224 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11225 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11226 (defun gnus-summary-limit-include-expunged (&optional no-error)
11227   "Display all the hidden articles that were expunged for low scores."
11228   (interactive)
11229   (let ((inhibit-read-only t))
11230     (let ((scored gnus-newsgroup-scored)
11231           headers h)
11232       (while scored
11233         (unless (gnus-summary-article-header (caar scored))
11234           (and (setq h (gnus-number-to-header (caar scored)))
11235                (< (cdar scored) gnus-summary-expunge-below)
11236                (push h headers)))
11237         (setq scored (cdr scored)))
11238       (if (not headers)
11239           (when (not no-error)
11240             (error "No expunged articles hidden"))
11241         (goto-char (point-min))
11242         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11243         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11244         (dolist (x headers)
11245           (push (mail-header-number x) gnus-newsgroup-limit))
11246         (gnus-summary-prepare-unthreaded (nreverse headers))
11247         (goto-char (point-min))
11248         (gnus-summary-position-point)
11249         t))))
11250
11251 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11252   "Mark all unread articles in this newsgroup as read.
11253 If prefix argument ALL is non-nil, ticked and dormant articles will
11254 also be marked as read.
11255 If QUIETLY is non-nil, no questions will be asked.
11256
11257 If TO-HERE is non-nil, it should be a point in the buffer.  All
11258 articles before (after, if REVERSE is set) this point will be marked
11259 as read.
11260
11261 Note that this function will only catch up the unread article
11262 in the current summary buffer limitation.
11263
11264 The number of articles marked as read is returned."
11265   (interactive "P")
11266   (prog1
11267       (save-excursion
11268         (when (or quietly
11269                   (not gnus-interactive-catchup) ;Without confirmation?
11270                   gnus-expert-user
11271                   (gnus-y-or-n-p
11272                    (if all
11273                        "Mark absolutely all articles as read? "
11274                      "Mark all unread articles as read? ")))
11275           (if (and not-mark
11276                    (not gnus-newsgroup-adaptive)
11277                    (not gnus-newsgroup-auto-expire)
11278                    (not gnus-suppress-duplicates)
11279                    (or (not gnus-use-cache)
11280                        (eq gnus-use-cache 'passive)))
11281               (progn
11282                 (when all
11283                   (setq gnus-newsgroup-marked nil
11284                         gnus-newsgroup-spam-marked nil
11285                         gnus-newsgroup-dormant nil))
11286                 (setq gnus-newsgroup-unreads
11287                       (gnus-sorted-nunion
11288                        (gnus-sorted-intersection gnus-newsgroup-unreads
11289                                                  gnus-newsgroup-downloadable)
11290                        (gnus-sorted-difference gnus-newsgroup-unfetched
11291                                                gnus-newsgroup-cached))))
11292             ;; We actually mark all articles as canceled, which we
11293             ;; have to do when using auto-expiry or adaptive scoring.
11294             (gnus-summary-show-all-threads)
11295             (if (and to-here reverse)
11296                 (progn
11297                   (goto-char to-here)
11298                   (gnus-summary-mark-current-read-and-unread-as-read
11299                    gnus-catchup-mark)
11300                   (while (gnus-summary-find-next (not all))
11301                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11302               (when (gnus-summary-first-subject (not all))
11303                 (while (and
11304                         (if to-here (< (point) to-here) t)
11305                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11306                         (gnus-summary-find-next (not all))))))
11307             (gnus-set-mode-line 'summary))
11308           t))
11309     (gnus-summary-position-point)))
11310
11311 (defun gnus-summary-catchup-to-here (&optional all)
11312   "Mark all unticked articles before the current one as read.
11313 If ALL is non-nil, also mark ticked and dormant articles as read."
11314   (interactive "P")
11315   (save-excursion
11316     (gnus-save-hidden-threads
11317       (let ((beg (point)))
11318         ;; We check that there are unread articles.
11319         (when (or all (gnus-summary-find-prev))
11320           (gnus-summary-catchup all t beg)))))
11321   (gnus-summary-position-point))
11322
11323 (defun gnus-summary-catchup-from-here (&optional all)
11324   "Mark all unticked articles after (and including) the current one as read.
11325 If ALL is non-nil, also mark ticked and dormant articles as read."
11326   (interactive "P")
11327   (save-excursion
11328     (gnus-save-hidden-threads
11329       (let ((beg (point)))
11330         ;; We check that there are unread articles.
11331         (when (or all (gnus-summary-find-next))
11332           (gnus-summary-catchup all t beg nil t)))))
11333   (gnus-summary-position-point))
11334
11335 (defun gnus-summary-catchup-all (&optional quietly)
11336   "Mark all articles in this newsgroup as read.
11337 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11338 instead, which marks only unread articles as read."
11339   (interactive "P")
11340   (gnus-summary-catchup t quietly))
11341
11342 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11343   "Mark all unread articles in this group as read, then exit.
11344 If prefix argument ALL is non-nil, all articles are marked as read.
11345 If QUIETLY is non-nil, no questions will be asked."
11346   (interactive "P")
11347   (when (gnus-summary-catchup all quietly nil 'fast)
11348     ;; Select next newsgroup or exit.
11349     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11350              (eq gnus-auto-select-next 'quietly))
11351         (gnus-summary-next-group nil)
11352       (gnus-summary-exit))))
11353
11354 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11355   "Mark all articles in this newsgroup as read, and then exit.
11356 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11357 instead, which marks only unread articles as read."
11358   (interactive "P")
11359   (gnus-summary-catchup-and-exit t quietly))
11360
11361 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11362   "Mark all articles in this group as read and select the next group.
11363 If given a prefix, mark all articles, unread as well as ticked, as
11364 read."
11365   (interactive "P")
11366   (save-excursion
11367     (gnus-summary-catchup all))
11368   (gnus-summary-next-group))
11369
11370 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11371   "Mark all articles in this group as read and select the previous group.
11372 If given a prefix, mark all articles, unread as well as ticked, as
11373 read."
11374   (interactive "P")
11375   (save-excursion
11376     (gnus-summary-catchup all))
11377   (gnus-summary-next-group nil nil t))
11378
11379 ;;;
11380 ;;; with article
11381 ;;;
11382
11383 (defmacro gnus-with-article (article &rest forms)
11384   "Select ARTICLE and perform FORMS in the original article buffer.
11385 Then replace the article with the result."
11386   `(progn
11387      ;; We don't want the article to be marked as read.
11388      (let (gnus-mark-article-hook)
11389        (gnus-summary-select-article t t nil ,article))
11390      (set-buffer gnus-original-article-buffer)
11391      ,@forms
11392      (if (not (gnus-check-backend-function
11393                'request-replace-article (car gnus-article-current)))
11394          (gnus-message 5 "Read-only group; not replacing")
11395        (unless (gnus-request-replace-article
11396                 ,article (car gnus-article-current)
11397                 (current-buffer) t)
11398          (error "Couldn't replace article")))
11399      ;; The cache and backlog have to be flushed somewhat.
11400      (when gnus-keep-backlog
11401        (gnus-backlog-remove-article
11402         (car gnus-article-current) (cdr gnus-article-current)))
11403      (when gnus-use-cache
11404        (gnus-cache-update-article
11405         (car gnus-article-current) (cdr gnus-article-current)))))
11406
11407 (put 'gnus-with-article 'lisp-indent-function 1)
11408 (put 'gnus-with-article 'edebug-form-spec '(form body))
11409
11410 ;; Thread-based commands.
11411
11412 (defun gnus-summary-articles-in-thread (&optional article)
11413   "Return a list of all articles in the current thread.
11414 If ARTICLE is non-nil, return all articles in the thread that starts
11415 with that article."
11416   (let* ((article (or article (gnus-summary-article-number)))
11417          (data (gnus-data-find-list article))
11418          (top-level (gnus-data-level (car data)))
11419          (top-subject
11420           (cond ((null gnus-thread-operation-ignore-subject)
11421                  (gnus-simplify-subject-re
11422                   (mail-header-subject (gnus-data-header (car data)))))
11423                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11424                  (gnus-simplify-subject-fuzzy
11425                   (mail-header-subject (gnus-data-header (car data)))))
11426                 (t nil)))
11427          (end-point (save-excursion
11428                       (goto-char (gnus-data-pos (car data)))
11429                       (if (gnus-summary-go-to-next-thread)
11430                           (point) (point-max))))
11431          articles)
11432     (while (and data
11433                 (< (gnus-data-pos (car data)) end-point))
11434       (when (or (not top-subject)
11435                 (string= top-subject
11436                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11437                              (gnus-simplify-subject-fuzzy
11438                               (mail-header-subject
11439                                (gnus-data-header (car data))))
11440                            (gnus-simplify-subject-re
11441                             (mail-header-subject
11442                              (gnus-data-header (car data)))))))
11443         (push (gnus-data-number (car data)) articles))
11444       (unless (and (setq data (cdr data))
11445                    (> (gnus-data-level (car data)) top-level))
11446         (setq data nil)))
11447     ;; Return the list of articles.
11448     (nreverse articles)))
11449
11450 (defun gnus-summary-rethread-current ()
11451   "Rethread the thread the current article is part of."
11452   (interactive)
11453   (let* ((gnus-show-threads t)
11454          (article (gnus-summary-article-number))
11455          (id (mail-header-id (gnus-summary-article-header)))
11456          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11457     (unless id
11458       (error "No article on the current line"))
11459     (gnus-rebuild-thread id)
11460     (gnus-summary-goto-subject article)))
11461
11462 (defun gnus-summary-reparent-thread ()
11463   "Make the current article child of the marked (or previous) article.
11464
11465 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11466 is non-nil or the Subject: of both articles are the same."
11467   (interactive)
11468   (unless (not (gnus-group-read-only-p))
11469     (error "The current newsgroup does not support article editing"))
11470   (unless (<= (length gnus-newsgroup-processable) 1)
11471     (error "No more than one article may be marked"))
11472   (let ((child (gnus-summary-article-number))
11473         ;; First grab the marked article, otherwise one line up.
11474         (parent (if (not (null gnus-newsgroup-processable))
11475                     (car gnus-newsgroup-processable)
11476                   (save-excursion
11477                     (if (eq (forward-line -1) 0)
11478                         (gnus-summary-article-number)
11479                       (error "Beginning of summary buffer"))))))
11480     (gnus-summary-reparent-children parent (list child))))
11481
11482 (defun gnus-summary-reparent-children (parent children)
11483   "Make PARENT the parent of CHILDREN.
11484 When called interactively, PARENT is the current article and CHILDREN
11485 are the process-marked articles."
11486   (interactive
11487    (list (gnus-summary-article-number)
11488          (gnus-summary-work-articles nil)))
11489   (dolist (child children)
11490     (save-window-excursion
11491       (let ((gnus-article-buffer " *reparent*"))
11492         (unless (not (eq parent child))
11493           (error "An article may not be self-referential"))
11494         (let ((message-id (mail-header-id
11495                            (gnus-summary-article-header parent))))
11496           (unless (and message-id (not (equal message-id "")))
11497             (error "No message-id in desired parent"))
11498           (gnus-with-article child
11499             (save-restriction
11500               (goto-char (point-min))
11501               (message-narrow-to-head)
11502               (if (re-search-forward "^References: " nil t)
11503                   (progn
11504                     (re-search-forward "^[^ \t]" nil t)
11505                     (forward-line -1)
11506                     (end-of-line)
11507                     (insert " " message-id))
11508                 (insert "References: " message-id "\n"))))
11509           (set-buffer gnus-summary-buffer)
11510           (gnus-summary-unmark-all-processable)
11511           (gnus-summary-update-article child)
11512           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11513             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11514           (gnus-summary-rethread-current)
11515           (gnus-message 3 "Article %d is now the child of article %d"
11516                         child parent))))))
11517
11518 (defun gnus-summary-toggle-threads (&optional arg)
11519   "Toggle showing conversation threads.
11520 If ARG is positive number, turn showing conversation threads on."
11521   (interactive "P")
11522   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11523     (setq gnus-show-threads
11524           (if (null arg) (not gnus-show-threads)
11525             (> (prefix-numeric-value arg) 0)))
11526     (gnus-summary-prepare)
11527     (gnus-summary-goto-subject current)
11528     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11529     (gnus-summary-position-point)))
11530
11531 (eval-and-compile
11532   (if (fboundp 'remove-overlays)
11533       (defalias 'gnus-remove-overlays 'remove-overlays)
11534     (defun gnus-remove-overlays (beg end name val)
11535       "Clear BEG and END of overlays whose property NAME has value VAL.
11536 For compatibility with XEmacs."
11537       (dolist (ov (gnus-overlays-in beg end))
11538         (when (eq (gnus-overlay-get ov name) val)
11539           (gnus-delete-overlay ov))))))
11540
11541 (defun gnus-summary-show-all-threads ()
11542   "Show all threads."
11543   (interactive)
11544   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11545   (gnus-summary-position-point))
11546
11547 (defsubst gnus-summary--inv (p)
11548   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11549
11550 (defun gnus-summary-show-thread ()
11551   "Show thread subtrees.
11552 Returns nil if no thread was there to be shown."
11553   (interactive)
11554   (let* ((orig (point))
11555          (end (point-at-eol))
11556          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11557          ;; Leave point at bol
11558          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11559          (eoi (when end
11560                 (if (fboundp 'next-single-char-property-change)
11561                     (or (next-single-char-property-change end 'invisible)
11562                         (point-max))
11563                   (while (progn
11564                            (end-of-line 2)
11565                            (and (not (eobp))
11566                                 (eq (get-char-property (point) 'invisible)
11567                                     'gnus-sum))))
11568                   (point)))))
11569     (when eoi
11570       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11571       (goto-char orig)
11572       (gnus-summary-position-point)
11573       eoi)))
11574
11575 (defun gnus-summary-maybe-hide-threads ()
11576   "If requested, hide the threads that should be hidden."
11577   (when (and gnus-show-threads
11578              gnus-thread-hide-subtree)
11579     (gnus-summary-hide-all-threads
11580      (if (or (consp gnus-thread-hide-subtree)
11581              (functionp gnus-thread-hide-subtree))
11582          (gnus-make-predicate gnus-thread-hide-subtree)
11583        nil))))
11584
11585 ;;; Hiding predicates.
11586
11587 (defun gnus-article-unread-p (header)
11588   (memq (mail-header-number header) gnus-newsgroup-unreads))
11589
11590 (defun gnus-article-unseen-p (header)
11591   (memq (mail-header-number header) gnus-newsgroup-unseen))
11592
11593 (defun gnus-map-articles (predicate articles)
11594   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11595   (apply 'gnus-or (mapcar predicate
11596                           (mapcar (lambda (number)
11597                                     (gnus-summary-article-header number))
11598                                   articles))))
11599
11600 (defun gnus-summary-hide-all-threads (&optional predicate)
11601   "Hide all thread subtrees.
11602 If PREDICATE is supplied, threads that satisfy this predicate
11603 will not be hidden."
11604   (interactive)
11605   (save-excursion
11606     (goto-char (point-min))
11607     (let ((end nil)
11608           (count 0))
11609       (while (not end)
11610         (incf count)
11611         (when (zerop (mod count 1000))
11612           (message "Hiding all threads... %d" count))
11613         (when (or (not predicate)
11614                   (gnus-map-articles
11615                    predicate (gnus-summary-article-children)))
11616             (gnus-summary-hide-thread))
11617         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11618   (gnus-summary-position-point))
11619
11620 (defun gnus-summary-hide-thread ()
11621   "Hide thread subtrees.
11622 If PREDICATE is supplied, threads that satisfy this predicate
11623 will not be hidden.
11624 Returns nil if no threads were there to be hidden."
11625   (interactive)
11626   (let ((start (point))
11627         (starteol (line-end-position))
11628         (article (gnus-summary-article-number)))
11629     (goto-char start)
11630     ;; Go forward until either the buffer ends or the subthread ends.
11631     (when (and (not (eobp))
11632                (or (zerop (gnus-summary-next-thread 1 t))
11633                    (goto-char (point-max))))
11634       (if (and (> (point) start)
11635                ;; FIXME: this should actually search for a non-invisible \n.
11636                (search-backward "\n" start t))
11637           (progn
11638             (when (> (point) starteol)
11639               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11640               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11641                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11642                 (gnus-overlay-put ol 'evaporate t)))
11643             (gnus-summary-goto-subject article)
11644             (when (> start (point))
11645               (message "Hiding the thread moved us backwards, aborting!")
11646               (goto-char (point-max))))
11647         (goto-char start)
11648         nil))))
11649
11650 (defun gnus-summary-go-to-next-thread (&optional previous)
11651   "Go to the same level (or less) next thread.
11652 If PREVIOUS is non-nil, go to previous thread instead.
11653 Return the article number moved to, or nil if moving was impossible."
11654   (let ((level (gnus-summary-thread-level))
11655         (way (if previous -1 1))
11656         (beg (point)))
11657     (forward-line way)
11658     (while (and (not (eobp))
11659                 (< level (gnus-summary-thread-level)))
11660       (forward-line way))
11661     (if (eobp)
11662         (progn
11663           (goto-char beg)
11664           nil)
11665       (setq beg (point))
11666       (prog1
11667           (gnus-summary-article-number)
11668         (goto-char beg)))))
11669
11670 (defun gnus-summary-next-thread (n &optional silent)
11671   "Go to the same level next N'th thread.
11672 If N is negative, search backward instead.
11673 Returns the difference between N and the number of skips actually
11674 done.
11675
11676 If SILENT, don't output messages."
11677   (interactive "p")
11678   (let ((backward (< n 0))
11679         (n (abs n)))
11680     (while (and (> n 0)
11681                 (gnus-summary-go-to-next-thread backward))
11682       (decf n))
11683     (unless silent
11684       (gnus-summary-position-point))
11685     (when (and (not silent) (/= 0 n))
11686       (gnus-message 7 "No more threads"))
11687     n))
11688
11689 (defun gnus-summary-prev-thread (n)
11690   "Go to the same level previous N'th thread.
11691 Returns the difference between N and the number of skips actually
11692 done."
11693   (interactive "p")
11694   (gnus-summary-next-thread (- n)))
11695
11696 (defun gnus-summary-go-down-thread ()
11697   "Go down one level in the current thread."
11698   (let ((children (gnus-summary-article-children)))
11699     (when children
11700       (gnus-summary-goto-subject (car children)))))
11701
11702 (defun gnus-summary-go-up-thread ()
11703   "Go up one level in the current thread."
11704   (let ((parent (gnus-summary-article-parent)))
11705     (when parent
11706       (gnus-summary-goto-subject parent))))
11707
11708 (defun gnus-summary-down-thread (n)
11709   "Go down thread N steps.
11710 If N is negative, go up instead.
11711 Returns the difference between N and how many steps down that were
11712 taken."
11713   (interactive "p")
11714   (let ((up (< n 0))
11715         (n (abs n)))
11716     (while (and (> n 0)
11717                 (if up (gnus-summary-go-up-thread)
11718                   (gnus-summary-go-down-thread)))
11719       (setq n (1- n)))
11720     (gnus-summary-position-point)
11721     (when (/= 0 n)
11722       (gnus-message 7 "Can't go further"))
11723     n))
11724
11725 (defun gnus-summary-up-thread (n)
11726   "Go up thread N steps.
11727 If N is negative, go down instead.
11728 Returns the difference between N and how many steps down that were
11729 taken."
11730   (interactive "p")
11731   (gnus-summary-down-thread (- n)))
11732
11733 (defun gnus-summary-top-thread ()
11734   "Go to the top of the thread."
11735   (interactive)
11736   (while (gnus-summary-go-up-thread))
11737   (gnus-summary-article-number))
11738
11739 (defun gnus-summary-expire-thread ()
11740   "Mark articles under current thread as expired."
11741   (interactive)
11742   (gnus-summary-kill-thread 0))
11743
11744 (defun gnus-summary-kill-thread (&optional unmark)
11745   "Mark articles under current thread as read.
11746 If the prefix argument is positive, remove any kinds of marks.
11747 If the prefix argument is zero, mark thread as expired.
11748 If the prefix argument is negative, tick articles instead."
11749   (interactive "P")
11750   (when unmark
11751     (setq unmark (prefix-numeric-value unmark)))
11752   (let ((articles (gnus-summary-articles-in-thread))
11753         (hide (or (null unmark) (= unmark 0))))
11754     (save-excursion
11755       ;; Expand the thread.
11756       (gnus-summary-show-thread)
11757       ;; Mark all the articles.
11758       (while articles
11759         (gnus-summary-goto-subject (car articles))
11760         (cond ((null unmark)
11761                (gnus-summary-mark-article-as-read gnus-killed-mark))
11762               ((> unmark 0)
11763                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11764               ((= unmark 0)
11765                (gnus-summary-mark-article nil gnus-expirable-mark))
11766               (t
11767                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11768         (setq articles (cdr articles))))
11769     ;; Hide killed subtrees when hide is true.
11770     (and hide
11771          gnus-thread-hide-killed
11772          (gnus-summary-hide-thread))
11773     ;; If hide is t, go to next unread subject.
11774     (when hide
11775       ;; Go to next unread subject.
11776       (gnus-summary-next-subject 1 t)))
11777   (gnus-set-mode-line 'summary))
11778
11779 ;; Summary sorting commands
11780
11781 (defun gnus-summary-sort-by-number (&optional reverse)
11782   "Sort the summary buffer by article number.
11783 Argument REVERSE means reverse order."
11784   (interactive "P")
11785   (gnus-summary-sort 'number reverse))
11786
11787 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11788   "Sort the summary buffer by most recent article number.
11789 Argument REVERSE means reverse order."
11790   (interactive "P")
11791   (gnus-summary-sort 'most-recent-number reverse))
11792
11793 (defun gnus-summary-sort-by-random (&optional reverse)
11794   "Randomize the order in the summary buffer.
11795 Argument REVERSE means to randomize in reverse order."
11796   (interactive "P")
11797   (gnus-summary-sort 'random reverse))
11798
11799 (defun gnus-summary-sort-by-author (&optional reverse)
11800   "Sort the summary buffer by author name alphabetically.
11801 If `case-fold-search' is non-nil, case of letters is ignored.
11802 Argument REVERSE means reverse order."
11803   (interactive "P")
11804   (gnus-summary-sort 'author reverse))
11805
11806 (defun gnus-summary-sort-by-recipient (&optional reverse)
11807   "Sort the summary buffer by recipient name alphabetically.
11808 If `case-fold-search' is non-nil, case of letters is ignored.
11809 Argument REVERSE means reverse order."
11810   (interactive "P")
11811   (gnus-summary-sort 'recipient reverse))
11812
11813 (defun gnus-summary-sort-by-subject (&optional reverse)
11814   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11815 If `case-fold-search' is non-nil, case of letters is ignored.
11816 Argument REVERSE means reverse order."
11817   (interactive "P")
11818   (gnus-summary-sort 'subject reverse))
11819
11820 (defun gnus-summary-sort-by-date (&optional reverse)
11821   "Sort the summary buffer by date.
11822 Argument REVERSE means reverse order."
11823   (interactive "P")
11824   (gnus-summary-sort 'date reverse))
11825
11826 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11827   "Sort the summary buffer by most recent date.
11828 Argument REVERSE means reverse order."
11829   (interactive "P")
11830   (gnus-summary-sort 'most-recent-date reverse))
11831
11832 (defun gnus-summary-sort-by-score (&optional reverse)
11833   "Sort the summary buffer by score.
11834 Argument REVERSE means reverse order."
11835   (interactive "P")
11836   (gnus-summary-sort 'score reverse))
11837
11838 (defun gnus-summary-sort-by-lines (&optional reverse)
11839   "Sort the summary buffer by the number of lines.
11840 Argument REVERSE means reverse order."
11841   (interactive "P")
11842   (gnus-summary-sort 'lines reverse))
11843
11844 (defun gnus-summary-sort-by-chars (&optional reverse)
11845   "Sort the summary buffer by article length.
11846 Argument REVERSE means reverse order."
11847   (interactive "P")
11848   (gnus-summary-sort 'chars reverse))
11849
11850 (defun gnus-summary-sort-by-original (&optional reverse)
11851   "Sort the summary buffer using the default sorting method.
11852 Argument REVERSE means reverse order."
11853   (interactive "P")
11854   (let* ((inhibit-read-only t)
11855          (gnus-summary-prepare-hook nil))
11856     ;; We do the sorting by regenerating the threads.
11857     (gnus-summary-prepare)
11858     ;; Hide subthreads if needed.
11859     (gnus-summary-maybe-hide-threads)))
11860
11861 (defun gnus-summary-sort (predicate reverse)
11862   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11863   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11864          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11865          (gnus-thread-sort-functions
11866           (if (not reverse)
11867               thread
11868             `(lambda (t1 t2)
11869                (,thread t2 t1))))
11870          (gnus-sort-gathered-threads-function
11871           gnus-thread-sort-functions)
11872          (gnus-article-sort-functions
11873           (if (not reverse)
11874               article
11875             `(lambda (t1 t2)
11876                (,article t2 t1))))
11877          (inhibit-read-only t)
11878          (gnus-summary-prepare-hook nil))
11879     ;; We do the sorting by regenerating the threads.
11880     (gnus-summary-prepare)
11881     ;; Hide subthreads if needed.
11882     (gnus-summary-maybe-hide-threads)))
11883
11884 ;; Summary saving commands.
11885
11886 (defun gnus-summary-save-article (&optional n not-saved)
11887   "Save the current article using the default saver function.
11888 If N is a positive number, save the N next articles.
11889 If N is a negative number, save the N previous articles.
11890 If N is nil and any articles have been marked with the process mark,
11891 save those articles instead.
11892 The variable `gnus-default-article-saver' specifies the saver function.
11893
11894 If the optional second argument NOT-SAVED is non-nil, articles saved
11895 will not be marked as saved."
11896   (interactive "P")
11897   (require 'gnus-art)
11898   (let* ((articles (gnus-summary-work-articles n))
11899          (save-buffer (save-excursion
11900                         (nnheader-set-temp-buffer " *Gnus Save*")))
11901          (num (length articles))
11902          ;; Whether to save decoded articles or raw articles.
11903          (decode (when gnus-article-save-coding-system
11904                    (get gnus-default-article-saver :decode)))
11905          ;; When saving many articles in a single file, use the other
11906          ;; function to save articles other than the first one.
11907          (saver2 (get gnus-default-article-saver :function))
11908          (gnus-prompt-before-saving (if saver2
11909                                         t
11910                                       gnus-prompt-before-saving))
11911          (gnus-default-article-saver gnus-default-article-saver)
11912          header file)
11913     (dolist (article articles)
11914       (setq header (gnus-summary-article-header article))
11915       (if (not (vectorp header))
11916           ;; This is a pseudo-article.
11917           (if (assq 'name header)
11918               (gnus-copy-file (cdr (assq 'name header)))
11919             (gnus-message 1 "Article %d is unsaveable" article))
11920         ;; This is a real article.
11921         (save-window-excursion
11922           (gnus-summary-select-article decode decode nil article)
11923           (gnus-summary-goto-subject article))
11924         (with-current-buffer save-buffer
11925           (erase-buffer)
11926           (insert-buffer-substring (if decode
11927                                        gnus-article-buffer
11928                                      gnus-original-article-buffer)))
11929         (setq file (gnus-article-save save-buffer file num))
11930         (gnus-summary-remove-process-mark article)
11931         (unless not-saved
11932           (gnus-summary-set-saved-mark article)))
11933       (when saver2
11934         (setq gnus-default-article-saver saver2
11935               saver2 nil)))
11936     (gnus-kill-buffer save-buffer)
11937     (gnus-summary-position-point)
11938     (gnus-set-mode-line 'summary)
11939     n))
11940
11941 (defun gnus-summary-pipe-output (&optional n sym)
11942   "Pipe the current article to a subprocess.
11943 If N is a positive number, pipe the N next articles.
11944 If N is a negative number, pipe the N previous articles.
11945 If N is nil and any articles have been marked with the process mark,
11946 pipe those articles instead.
11947 The default command to which articles are piped is specified by the
11948 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11949 will be prompted for the command.
11950
11951 The properties `:decode' and `:headers' that are put to the function
11952 symbol `gnus-summary-save-in-pipe' control whether this function
11953 decodes articles and what headers to keep (see the doc string for the
11954 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11955 is neither omitted nor the symbol `r', force including all headers
11956 regardless of the `:headers' property.  If it is the symbol `r',
11957 articles that are not decoded and include all headers will be piped
11958 no matter what the properties `:decode' and `:headers' are."
11959   (interactive (gnus-interactive "P\ny"))
11960   (require 'gnus-art)
11961   (let* ((articles (gnus-summary-work-articles n))
11962          (result-buffer "*Shell Command Output*")
11963          (all-headers (not (memq sym '(nil r))))
11964          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11965          (raw (eq sym 'r))
11966          (headers (get 'gnus-summary-save-in-pipe :headers))
11967          command result)
11968     (unless (numberp (car articles))
11969       (error "No article to pipe"))
11970     (setq command (gnus-read-shell-command
11971                    (concat "Shell command on "
11972                            (if (cdr articles)
11973                                (format "these %d articles" (length articles))
11974                              "this article")
11975                            ": ")
11976                    gnus-summary-pipe-output-default-command))
11977     (when (string-equal command "")
11978       (error "A command is required"))
11979     (when all-headers
11980       (put 'gnus-summary-save-in-pipe :headers nil))
11981     (unwind-protect
11982         (while articles
11983           (gnus-summary-goto-subject (pop articles))
11984           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11985           (when (and (get-buffer result-buffer)
11986                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11987             (setq result (concat result (with-current-buffer result-buffer
11988                                           (buffer-string))))))
11989       (put 'gnus-summary-save-in-pipe :headers headers))
11990     (unless (zerop (length result))
11991       (if (with-current-buffer (get-buffer-create result-buffer)
11992             (erase-buffer)
11993             (insert result)
11994             (prog1
11995                 (and (= (count-lines (point-min) (point)) 1)
11996                      (progn
11997                        (end-of-line 0)
11998                        (<= (current-column)
11999                            (window-width (minibuffer-window)))))
12000               (goto-char (point-min))))
12001           (message "%s" (substring result 0 -1))
12002         (message nil)
12003         (gnus-configure-windows 'pipe)))))
12004
12005 (defun gnus-summary-save-article-mail (&optional arg)
12006   "Append the current article to a Unix mail box file.
12007 If N is a positive number, save the N next articles.
12008 If N is a negative number, save the N previous articles.
12009 If N is nil and any articles have been marked with the process mark,
12010 save those articles instead."
12011   (interactive "P")
12012   (require 'gnus-art)
12013   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12014     (gnus-summary-save-article arg)))
12015
12016 (defun gnus-summary-save-article-rmail (&optional arg)
12017   "Append the current article to an rmail file.
12018 If N is a positive number, save the N next articles.
12019 If N is a negative number, save the N previous articles.
12020 If N is nil and any articles have been marked with the process mark,
12021 save those articles instead."
12022   (interactive "P")
12023   (require 'gnus-art)
12024   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12025     (gnus-summary-save-article arg)))
12026
12027 (defun gnus-summary-save-article-file (&optional arg)
12028   "Append the current article to a file.
12029 If N is a positive number, save the N next articles.
12030 If N is a negative number, save the N previous articles.
12031 If N is nil and any articles have been marked with the process mark,
12032 save those articles instead."
12033   (interactive "P")
12034   (require 'gnus-art)
12035   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12036     (gnus-summary-save-article arg)))
12037
12038 (defun gnus-summary-write-article-file (&optional arg)
12039   "Write the current article to a file, deleting the previous file.
12040 If N is a positive number, save the N next articles.
12041 If N is a negative number, save the N previous articles.
12042 If N is nil and any articles have been marked with the process mark,
12043 save those articles instead."
12044   (interactive "P")
12045   (require 'gnus-art)
12046   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12047     (gnus-summary-save-article arg)))
12048
12049 (defun gnus-summary-save-article-body-file (&optional arg)
12050   "Append the current article body to a file.
12051 If N is a positive number, save the N next articles.
12052 If N is a negative number, save the N previous articles.
12053 If N is nil and any articles have been marked with the process mark,
12054 save those articles instead."
12055   (interactive "P")
12056   (require 'gnus-art)
12057   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12058     (gnus-summary-save-article arg)))
12059
12060 (defun gnus-summary-write-article-body-file (&optional arg)
12061   "Write the current article body to a file, deleting the previous file.
12062 If N is a positive number, save the N next articles.
12063 If N is a negative number, save the N previous articles.
12064 If N is nil and any articles have been marked with the process mark,
12065 save those articles instead."
12066   (interactive "P")
12067   (require 'gnus-art)
12068   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12069     (gnus-summary-save-article arg)))
12070
12071 (defun gnus-summary-muttprint (&optional arg)
12072   "Print the current article using Muttprint.
12073 If N is a positive number, save the N next articles.
12074 If N is a negative number, save the N previous articles.
12075 If N is nil and any articles have been marked with the process mark,
12076 save those articles instead."
12077   (interactive "P")
12078   (require 'gnus-art)
12079   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12080     (gnus-summary-save-article arg t)))
12081
12082 (defun gnus-summary-pipe-message (program)
12083   "Pipe the current article through PROGRAM."
12084   (interactive "sProgram: ")
12085   (gnus-summary-select-article)
12086   (let ((mail-header-separator ""))
12087     (gnus-eval-in-buffer-window gnus-article-buffer
12088       (save-restriction
12089         (widen)
12090         (let ((start (window-start))
12091               (inhibit-read-only t))
12092           (message-pipe-buffer-body program)
12093           (set-window-start (get-buffer-window (current-buffer)) start))))))
12094
12095 (defun gnus-get-split-value (methods)
12096   "Return a value based on the split METHODS."
12097   (let (split-name method result match)
12098     (when methods
12099       (with-current-buffer gnus-original-article-buffer
12100         (save-restriction
12101           (nnheader-narrow-to-headers)
12102           (while (and methods (not split-name))
12103             (goto-char (point-min))
12104             (setq method (pop methods))
12105             (setq match (car method))
12106             (when (cond
12107                    ((stringp match)
12108                     ;; Regular expression.
12109                     (ignore-errors
12110                       (re-search-forward match nil t)))
12111                    ((functionp match)
12112                     ;; Function.
12113                     (save-restriction
12114                       (widen)
12115                       (setq result (funcall match gnus-newsgroup-name))))
12116                    ((consp match)
12117                     ;; Form.
12118                     (save-restriction
12119                       (widen)
12120                       (setq result (eval match)))))
12121               (setq split-name (cdr method))
12122               (cond ((stringp result)
12123                      (push (expand-file-name
12124                             result gnus-article-save-directory)
12125                            split-name))
12126                     ((consp result)
12127                      (setq split-name (append result split-name)))))))))
12128     (nreverse split-name)))
12129
12130 (defun gnus-valid-move-group-p (group)
12131   (and (symbolp group)
12132        (boundp group)
12133        (symbol-name group)
12134        (symbol-value group)
12135        (gnus-get-function (gnus-find-method-for-group
12136                            (symbol-name group)) 'request-accept-article t)))
12137
12138 (defun gnus-read-move-group-name (prompt default articles prefix)
12139   "Read a group name."
12140   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12141          (minibuffer-confirm-incomplete nil) ; XEmacs
12142          (prom
12143           (format "%s %s to"
12144                   prompt
12145                   (if (> (length articles) 1)
12146                       (format "these %d articles" (length articles))
12147                     "this article")))
12148          (to-newsgroup
12149           (cond
12150            ((null split-name)
12151             (gnus-group-completing-read
12152              prom
12153              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12154              nil prefix nil default))
12155            ((= 1 (length split-name))
12156             (gnus-group-completing-read
12157              prom
12158              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12159              nil prefix 'gnus-group-history (car split-name)))
12160            (t
12161             (gnus-completing-read
12162              prom (nreverse split-name) nil nil 'gnus-group-history))))
12163          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12164          encoded)
12165     (when to-newsgroup
12166       (if (or (string= to-newsgroup "")
12167               (string= to-newsgroup prefix))
12168           (setq to-newsgroup default))
12169       (unless to-newsgroup
12170         (error "No group name entered"))
12171       (setq encoded (mm-encode-coding-string
12172                      to-newsgroup
12173                      (gnus-group-name-charset to-method to-newsgroup)))
12174       (or (gnus-active encoded)
12175           (gnus-activate-group encoded nil nil to-method)
12176           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12177                                      to-newsgroup))
12178               (or (and (gnus-request-create-group encoded to-method)
12179                        (gnus-activate-group encoded nil nil to-method)
12180                        (gnus-subscribe-group encoded))
12181                   (error "Couldn't create group %s" to-newsgroup)))
12182           (error "No such group: %s" to-newsgroup))
12183       encoded)))
12184
12185 (defvar gnus-summary-save-parts-counter)
12186 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12187
12188 (defun gnus-summary-save-parts (type dir n &optional reverse)
12189   "Save parts matching TYPE to DIR.
12190 If REVERSE, save parts that do not match TYPE."
12191   (interactive
12192    (list (read-string "Save parts of type: "
12193                       (or (car gnus-summary-save-parts-type-history)
12194                           gnus-summary-save-parts-default-mime)
12195                       'gnus-summary-save-parts-type-history)
12196          (setq gnus-summary-save-parts-last-directory
12197                (read-directory-name "Save to directory: "
12198                                     gnus-summary-save-parts-last-directory
12199                                     nil t))
12200          current-prefix-arg))
12201   (gnus-summary-iterate n
12202     (let ((gnus-display-mime-function nil)
12203           gnus-article-prepare-hook
12204           gnus-article-decode-hook
12205           gnus-display-mime-function
12206           gnus-break-pages
12207           (gnus-inhibit-treatment t))
12208       (gnus-summary-select-article))
12209     (with-current-buffer gnus-article-buffer
12210       (let ((handles (or gnus-article-mime-handles
12211                          (mm-dissect-buffer nil gnus-article-loose-mime)
12212                          (and gnus-article-emulate-mime
12213                               (mm-uu-dissect))))
12214             (gnus-summary-save-parts-counter 1))
12215         (when handles
12216           (gnus-summary-save-parts-1 type dir handles reverse)
12217           (unless gnus-article-mime-handles ;; Don't destroy this case.
12218             (mm-destroy-parts handles)))))))
12219
12220 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12221   (if (stringp (car handle))
12222       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12223               (cdr handle))
12224     (when (if reverse
12225               (not (string-match type (mm-handle-media-type handle)))
12226             (string-match type (mm-handle-media-type handle)))
12227       (let ((file (expand-file-name
12228                    (gnus-map-function
12229                     mm-file-name-rewrite-functions
12230                     (file-name-nondirectory
12231                      (or
12232                       (mm-handle-filename handle)
12233                       (format "%s.%d.%d" gnus-newsgroup-name
12234                               (cdr gnus-article-current)
12235                               gnus-summary-save-parts-counter))))
12236                    dir)))
12237         (incf gnus-summary-save-parts-counter)
12238         (unless (file-exists-p file)
12239           (mm-save-part-to-file handle file))))))
12240
12241 ;; Summary extract commands
12242
12243 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12244   (let ((inhibit-read-only t)
12245         (article (gnus-summary-article-number))
12246         after-article b e)
12247     (unless (gnus-summary-goto-subject article)
12248       (error "No such article: %d" article))
12249     (gnus-summary-position-point)
12250     ;; If all commands are to be bunched up on one line, we collect
12251     ;; them here.
12252     (unless gnus-view-pseudos-separately
12253       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12254             files action)
12255         (while ps
12256           (setq action (cdr (assq 'action (car ps))))
12257           (setq files (list (cdr (assq 'name (car ps)))))
12258           (while (and ps (cdr ps)
12259                       (string= (or action "1")
12260                                (or (cdr (assq 'action (cadr ps))) "2")))
12261             (push (cdr (assq 'name (cadr ps))) files)
12262             (setcdr ps (cddr ps)))
12263           (when files
12264             (when (not (string-match "%s" action))
12265               (push " " files))
12266             (push " " files)
12267             (when (assq 'execute (car ps))
12268               (setcdr (assq 'execute (car ps))
12269                       (funcall (if (string-match "%s" action)
12270                                    'format 'concat)
12271                                action
12272                                (mapconcat
12273                                 (lambda (f)
12274                                   (if (equal f " ")
12275                                       f
12276                                     (shell-quote-argument f)))
12277                                 files " ")))))
12278           (setq ps (cdr ps)))))
12279     (if (and gnus-view-pseudos (not not-view))
12280         (while pslist
12281           (when (assq 'execute (car pslist))
12282             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12283                                   (eq gnus-view-pseudos 'not-confirm)))
12284           (setq pslist (cdr pslist)))
12285       (save-excursion
12286         (while pslist
12287           (setq after-article (or (cdr (assq 'article (car pslist)))
12288                                   (gnus-summary-article-number)))
12289           (gnus-summary-goto-subject after-article)
12290           (forward-line 1)
12291           (setq b (point))
12292           (insert "    " (file-name-nondirectory
12293                           (cdr (assq 'name (car pslist))))
12294                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12295           (setq e (point))
12296           (forward-line -1)             ; back to `b'
12297           (gnus-add-text-properties
12298            b (1- e) (list 'gnus-number gnus-reffed-article-number
12299                           gnus-mouse-face-prop gnus-mouse-face))
12300           (gnus-data-enter
12301            after-article gnus-reffed-article-number
12302            gnus-unread-mark b (car pslist) 0 (- e b))
12303           (setq gnus-newsgroup-unreads
12304                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12305                                          gnus-reffed-article-number))
12306           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12307           (setq pslist (cdr pslist)))))))
12308
12309 (defun gnus-pseudos< (p1 p2)
12310   (let ((c1 (cdr (assq 'action p1)))
12311         (c2 (cdr (assq 'action p2))))
12312     (and c1 c2 (string< c1 c2))))
12313
12314 (defun gnus-request-pseudo-article (props)
12315   (cond ((assq 'execute props)
12316          (gnus-execute-command (cdr (assq 'execute props)))))
12317   (let ((gnus-current-article (gnus-summary-article-number)))
12318     (gnus-run-hooks 'gnus-mark-article-hook)))
12319
12320 (defun gnus-execute-command (command &optional automatic)
12321   (save-excursion
12322     (gnus-article-setup-buffer)
12323     (set-buffer gnus-article-buffer)
12324     (setq buffer-read-only nil)
12325     (let ((command (if automatic command
12326                      (read-string "Command: " (cons command 0)))))
12327       (erase-buffer)
12328       (insert "$ " command "\n\n")
12329       (if gnus-view-pseudo-asynchronously
12330           (start-process "gnus-execute" (current-buffer) shell-file-name
12331                          shell-command-switch command)
12332         (call-process shell-file-name nil t nil
12333                       shell-command-switch command)))))
12334
12335 ;; Summary kill commands.
12336
12337 (defun gnus-summary-edit-global-kill (article)
12338   "Edit the \"global\" kill file."
12339   (interactive (list (gnus-summary-article-number)))
12340   (gnus-group-edit-global-kill article))
12341
12342 (defun gnus-summary-edit-local-kill ()
12343   "Edit a local kill file applied to the current newsgroup."
12344   (interactive)
12345   (setq gnus-current-headers (gnus-summary-article-header))
12346   (gnus-group-edit-local-kill
12347    (gnus-summary-article-number) gnus-newsgroup-name))
12348
12349 ;;; Header reading.
12350
12351 (defun gnus-read-header (id &optional header)
12352   "Read the headers of article ID and enter them into the Gnus system."
12353   (let ((group gnus-newsgroup-name)
12354         (gnus-override-method
12355          (or
12356           gnus-override-method
12357           (and (gnus-news-group-p gnus-newsgroup-name)
12358                (car (gnus-refer-article-methods)))))
12359         where)
12360     ;; First we check to see whether the header in question is already
12361     ;; fetched.
12362     (if (stringp id)
12363         ;; This is a Message-ID.
12364         (setq header (or header (gnus-id-to-header id)))
12365       ;; This is an article number.
12366       (setq header (or header (gnus-summary-article-header id))))
12367     (if (and header
12368              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12369         ;; We have found the header.
12370         header
12371       ;; We have to really fetch the header to this article.
12372       (with-current-buffer nntp-server-buffer
12373         (when (setq where (gnus-request-head id group))
12374           (nnheader-fold-continuation-lines)
12375           (goto-char (point-max))
12376           (insert ".\n")
12377           (goto-char (point-min))
12378           (insert "211 ")
12379           (princ (cond
12380                   ((numberp id) id)
12381                   ((cdr where) (cdr where))
12382                   (header (mail-header-number header))
12383                   (t gnus-reffed-article-number))
12384                  (current-buffer))
12385           (insert " Article retrieved.\n"))
12386         (if (or (not where)
12387                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12388             ()                          ; Malformed head.
12389           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12390             (when (and (stringp id)
12391                        (or
12392                         (not (string= (gnus-group-real-name group)
12393                                       (car where)))
12394                         (not (gnus-server-equal gnus-override-method
12395                                                 (gnus-group-method group)))))
12396               ;; If we fetched by Message-ID and the article came from
12397               ;; a different group (or server), we fudge some bogus
12398               ;; article numbers for this article.
12399               (mail-header-set-number header gnus-reffed-article-number))
12400             (with-current-buffer gnus-summary-buffer
12401               (decf gnus-reffed-article-number)
12402               (gnus-remove-header (mail-header-number header))
12403               (push header gnus-newsgroup-headers)
12404               (setq gnus-current-headers header)
12405               (push (mail-header-number header) gnus-newsgroup-limit)))
12406           header)))))
12407
12408 (defun gnus-remove-header (number)
12409   "Remove header NUMBER from `gnus-newsgroup-headers'."
12410   (if (and gnus-newsgroup-headers
12411            (= number (mail-header-number (car gnus-newsgroup-headers))))
12412       (pop gnus-newsgroup-headers)
12413     (let ((headers gnus-newsgroup-headers))
12414       (while (and (cdr headers)
12415                   (not (= number (mail-header-number (cadr headers)))))
12416         (pop headers))
12417       (when (cdr headers)
12418         (setcdr headers (cddr headers))))))
12419
12420 ;;;
12421 ;;; summary highlights
12422 ;;;
12423
12424 (defun gnus-highlight-selected-summary ()
12425   "Highlight selected article in summary buffer."
12426   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12427   (when gnus-summary-selected-face
12428     (save-excursion
12429       (let* ((beg (point-at-bol))
12430              (end (point-at-eol))
12431              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12432              (from (if (get-text-property beg gnus-mouse-face-prop)
12433                        beg
12434                      (or (next-single-property-change
12435                           beg gnus-mouse-face-prop nil end)
12436                          beg)))
12437              (to
12438               (if (= from end)
12439                   (- from 2)
12440                 (or (next-single-property-change
12441                      from gnus-mouse-face-prop nil end)
12442                     end))))
12443         ;; If no mouse-face prop on line we will have to = from = end,
12444         ;; so we highlight the entire line instead.
12445         (when (= (+ to 2) from)
12446           (setq from beg)
12447           (setq to end))
12448         (if gnus-newsgroup-selected-overlay
12449             ;; Move old overlay.
12450             (gnus-move-overlay
12451              gnus-newsgroup-selected-overlay from to (current-buffer))
12452           ;; Create new overlay.
12453           (gnus-overlay-put
12454            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12455            'face gnus-summary-selected-face))))))
12456
12457 (defvar gnus-summary-highlight-line-cached nil)
12458 (defvar gnus-summary-highlight-line-trigger nil)
12459
12460 (defun gnus-summary-highlight-line-0 ()
12461   (if (and (eq gnus-summary-highlight-line-trigger
12462                gnus-summary-highlight)
12463            gnus-summary-highlight-line-cached)
12464       gnus-summary-highlight-line-cached
12465     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12466           gnus-summary-highlight-line-cached
12467           (let* ((cond (list 'cond))
12468                  (c cond)
12469                  (list gnus-summary-highlight))
12470             (while list
12471               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12472                               nil))
12473               (setq c (cdr c)
12474                     list (cdr list)))
12475             (gnus-byte-compile (list 'lambda nil cond))))))
12476
12477 (defun gnus-summary-highlight-line ()
12478   "Highlight current line according to `gnus-summary-highlight'."
12479   (let* ((beg (point-at-bol))
12480          (article (or (gnus-summary-article-number) gnus-current-article))
12481          (score (or (cdr (assq article
12482                                gnus-newsgroup-scored))
12483                     gnus-summary-default-score 0))
12484          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12485          (inhibit-read-only t)
12486          (default gnus-summary-default-score)
12487          (default-high gnus-summary-default-high-score)
12488          (default-low gnus-summary-default-low-score)
12489          (uncached (and gnus-summary-use-undownloaded-faces
12490                         (memq article gnus-newsgroup-undownloaded)
12491                         (not (memq article gnus-newsgroup-cached)))))
12492     (let ((face (funcall (gnus-summary-highlight-line-0))))
12493       (unless (eq face (get-text-property beg 'face))
12494         (gnus-put-text-property-excluding-characters-with-faces
12495          beg (point-at-eol) 'face
12496          (setq face (if (boundp face) (symbol-value face) face)))
12497         (when gnus-summary-highlight-line-function
12498           (funcall gnus-summary-highlight-line-function article face))))))
12499
12500 (defun gnus-update-read-articles (group unread &optional compute)
12501   "Update the list of read articles in GROUP.
12502 UNREAD is a sorted list."
12503   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12504         (info (gnus-get-info group))
12505         (prev 1)
12506         read)
12507     (if (or (not info) (not active))
12508         ;; There is no info on this group if it was, in fact,
12509         ;; killed.  Gnus stores no information on killed groups, so
12510         ;; there's nothing to be done.
12511         ;; One could store the information somewhere temporarily,
12512         ;; perhaps...  Hmmm...
12513         ()
12514       ;; Remove any negative articles numbers.
12515       (while (and unread (< (car unread) 0))
12516         (setq unread (cdr unread)))
12517       ;; Remove any expired article numbers
12518       (while (and unread (< (car unread) (car active)))
12519         (setq unread (cdr unread)))
12520       ;; Compute the ranges of read articles by looking at the list of
12521       ;; unread articles.
12522       (while unread
12523         (when (/= (car unread) prev)
12524           (push (if (= prev (1- (car unread))) prev
12525                   (cons prev (1- (car unread))))
12526                 read))
12527         (setq prev (1+ (car unread)))
12528         (setq unread (cdr unread)))
12529       (when (<= prev (cdr active))
12530         (push (cons prev (cdr active)) read))
12531       (setq read (if (> (length read) 1) (nreverse read) read))
12532       (if compute
12533           read
12534         (save-excursion
12535           (let (setmarkundo)
12536             ;; Propagate the read marks to the backend.
12537             (when (and (or gnus-propagate-marks
12538                            (gnus-method-option-p
12539                             (gnus-find-method-for-group group)
12540                             'server-marks))
12541                        (gnus-check-backend-function 'request-set-mark group))
12542               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12543                     (add (gnus-remove-from-range read (gnus-info-read info))))
12544                 (when (or add del)
12545                   (unless (gnus-check-group group)
12546                     (error "Can't open server for %s" group))
12547                   (gnus-request-set-mark
12548                    group (delq nil (list (if add (list add 'add '(read)))
12549                                          (if del (list del 'del '(read))))))
12550                   (setq setmarkundo
12551                         `(gnus-request-set-mark
12552                           ,group
12553                           ',(delq nil (list
12554                                        (if del (list del 'add '(read)))
12555                                        (if add (list add 'del '(read))))))))))
12556             (set-buffer gnus-group-buffer)
12557             (gnus-undo-register
12558               `(progn
12559                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12560                  (gnus-info-set-read ',info ',(gnus-info-read info))
12561                  (gnus-get-unread-articles-in-group ',info
12562                                                     (gnus-active ,group))
12563                  (gnus-group-update-group ,group t)
12564                  ,setmarkundo))))
12565         ;; Enter this list into the group info.
12566         (gnus-info-set-read info read)
12567         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12568         (gnus-get-unread-articles-in-group info (gnus-active group))
12569         t))))
12570
12571 (defun gnus-offer-save-summaries ()
12572   "Offer to save all active summary buffers."
12573   (let (buffers)
12574     ;; Go through all buffers and find all summaries.
12575     (dolist (buffer (buffer-list))
12576       (when (and (setq buffer (buffer-name buffer))
12577                  (string-match "Summary" buffer)
12578                  (with-current-buffer buffer
12579                    ;; We check that this is, indeed, a summary buffer.
12580                    (and (eq major-mode 'gnus-summary-mode)
12581                         ;; Also make sure this isn't bogus.
12582                         gnus-newsgroup-prepared
12583                         ;; Also make sure that this isn't a
12584                         ;; dead summary buffer.
12585                         (not gnus-dead-summary-mode))))
12586         (push buffer buffers)))
12587     ;; Go through all these summary buffers and offer to save them.
12588     (when buffers
12589       (save-excursion
12590         (map-y-or-n-p
12591          "Update summary buffer %s? "
12592          (lambda (buf)
12593            (switch-to-buffer buf)
12594            (gnus-summary-exit))
12595          buffers)))))
12596
12597 (defun gnus-summary-setup-default-charset ()
12598   "Setup newsgroup default charset."
12599   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12600       (setq gnus-newsgroup-charset nil)
12601     (let* ((ignored-charsets
12602             (or gnus-newsgroup-ephemeral-ignored-charsets
12603                 (append
12604                  (and gnus-newsgroup-name
12605                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12606                  gnus-newsgroup-ignored-charsets))))
12607       (setq gnus-newsgroup-charset
12608             (or gnus-newsgroup-ephemeral-charset
12609                 (and gnus-newsgroup-name
12610                      (gnus-parameter-charset gnus-newsgroup-name))
12611                 gnus-default-charset))
12612       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12613            ignored-charsets))))
12614
12615 ;;;
12616 ;;; Mime Commands
12617 ;;;
12618
12619 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12620   "Display the current article buffer fully MIME-buttonized.
12621 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12622 treated as multipart/mixed."
12623   (interactive "P")
12624   (require 'gnus-art)
12625   (let ((gnus-unbuttonized-mime-types nil)
12626         (gnus-mime-display-multipart-as-mixed show-all-parts))
12627     (gnus-summary-show-article)))
12628
12629 (defun gnus-summary-repair-multipart (article)
12630   "Add a Content-Type header to a multipart article without one."
12631   (interactive (list (gnus-summary-article-number)))
12632   (gnus-with-article article
12633     (message-narrow-to-head)
12634     (message-remove-header "Mime-Version")
12635     (goto-char (point-max))
12636     (insert "Mime-Version: 1.0\n")
12637     (widen)
12638     (when (search-forward "\n--" nil t)
12639       (let ((separator (buffer-substring (point) (point-at-eol))))
12640         (message-narrow-to-head)
12641         (message-remove-header "Content-Type")
12642         (goto-char (point-max))
12643         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12644                         separator))
12645         (widen))))
12646   (let (gnus-mark-article-hook)
12647     (gnus-summary-select-article t t nil article)))
12648
12649 (defun gnus-summary-toggle-display-buttonized ()
12650   "Toggle the buttonizing of the article buffer."
12651   (interactive)
12652   (require 'gnus-art)
12653   (if (setq gnus-inhibit-mime-unbuttonizing
12654             (not gnus-inhibit-mime-unbuttonizing))
12655       (let ((gnus-unbuttonized-mime-types nil))
12656         (gnus-summary-show-article))
12657     (gnus-summary-show-article)))
12658
12659 ;;;
12660 ;;; Generic summary marking commands
12661 ;;;
12662
12663 (defvar gnus-summary-marking-alist
12664   '((read gnus-del-mark "d")
12665     (unread gnus-unread-mark "u")
12666     (ticked gnus-ticked-mark "!")
12667     (dormant gnus-dormant-mark "?")
12668     (expirable gnus-expirable-mark "e"))
12669   "An alist of names/marks/keystrokes.")
12670
12671 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12672 (defvar gnus-summary-mark-map)
12673
12674 (defun gnus-summary-make-all-marking-commands ()
12675   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12676   (dolist (elem gnus-summary-marking-alist)
12677     (apply 'gnus-summary-make-marking-command elem)))
12678
12679 (defun gnus-summary-make-marking-command (name mark keystroke)
12680   (let ((map (make-sparse-keymap)))
12681     (define-key gnus-summary-generic-mark-map keystroke map)
12682     (dolist (lway `((next "next" next nil "n")
12683                     (next-unread "next unread" next t "N")
12684                     (prev "previous" prev nil "p")
12685                     (prev-unread "previous unread" prev t "P")
12686                     (nomove "" nil nil ,keystroke)))
12687       (let ((func (gnus-summary-make-marking-command-1
12688                    mark (car lway) lway name)))
12689         (setq func (eval func))
12690         (define-key map (nth 4 lway) func)))))
12691
12692 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12693   `(defun ,(intern
12694             (format "gnus-summary-put-mark-as-%s%s"
12695                     name (if (eq way 'nomove)
12696                              ""
12697                            (concat "-" (symbol-name way)))))
12698      (n)
12699      ,(format
12700        "Mark the current article as %s%s.
12701 If N, the prefix, then repeat N times.
12702 If N is negative, move in reverse order.
12703 The difference between N and the actual number of articles marked is
12704 returned."
12705        name (cadr lway))
12706      (interactive "p")
12707      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12708
12709 (defun gnus-summary-generic-mark (n mark move unread)
12710   "Mark N articles with MARK."
12711   (unless (eq major-mode 'gnus-summary-mode)
12712     (error "This command can only be used in the summary buffer"))
12713   (gnus-summary-show-thread)
12714   (let ((nummove
12715          (cond
12716           ((eq move 'next) 1)
12717           ((eq move 'prev) -1)
12718           (t 0))))
12719     (if (zerop nummove)
12720         (setq n 1)
12721       (when (< n 0)
12722         (setq n (abs n)
12723               nummove (* -1 nummove))))
12724     (while (and (> n 0)
12725                 (gnus-summary-mark-article nil mark)
12726                 (zerop (gnus-summary-next-subject nummove unread t)))
12727       (setq n (1- n)))
12728     (when (/= 0 n)
12729       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12730     (gnus-summary-recenter)
12731     (gnus-summary-position-point)
12732     (gnus-set-mode-line 'summary)
12733     n))
12734
12735 (defun gnus-summary-insert-articles (articles)
12736   (when (setq articles
12737               (gnus-sorted-difference articles
12738                                       (mapcar (lambda (h)
12739                                                 (mail-header-number h))
12740                                               gnus-newsgroup-headers)))
12741     (setq gnus-newsgroup-headers
12742           (gnus-merge 'list
12743                       gnus-newsgroup-headers
12744                       (gnus-fetch-headers articles)
12745                       'gnus-article-sort-by-number))
12746     ;; Suppress duplicates?
12747     (when gnus-suppress-duplicates
12748       (gnus-dup-suppress-articles))
12749
12750     (if (and gnus-fetch-old-headers
12751              (eq gnus-headers-retrieved-by 'nov))
12752         ;; We might want to build some more threads first.
12753         (if (eq gnus-fetch-old-headers 'invisible)
12754             (gnus-build-all-threads)
12755           (gnus-build-old-threads))
12756       ;; Mark the inserted articles that are unread as unread.
12757       (setq gnus-newsgroup-unreads
12758             (gnus-sorted-nunion
12759              gnus-newsgroup-unreads
12760              (gnus-sorted-nintersection
12761               (gnus-list-of-unread-articles gnus-newsgroup-name)
12762               articles)))
12763       ;; Mark the inserted articles as selected so that the information
12764       ;; of the marks having been changed by a user may be updated when
12765       ;; exiting this group.  See `gnus-summary-update-info'.
12766       (dolist (art articles)
12767         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12768     ;; Let the Gnus agent mark articles as read.
12769     (when gnus-agent
12770       (gnus-agent-get-undownloaded-list))
12771     ;; Remove list identifiers from subject
12772     (gnus-summary-remove-list-identifiers)
12773     ;; First and last article in this newsgroup.
12774     (when gnus-newsgroup-headers
12775       (setq gnus-newsgroup-begin
12776             (mail-header-number (car gnus-newsgroup-headers))
12777             gnus-newsgroup-end
12778             (mail-header-number
12779              (gnus-last-element gnus-newsgroup-headers))))
12780     (when gnus-use-scoring
12781       (gnus-possibly-score-headers))))
12782
12783 (defun gnus-summary-insert-old-articles (&optional all)
12784   "Insert all old articles in this group.
12785 If ALL is non-nil, already read articles become readable.
12786 If ALL is a number, fetch this number of articles."
12787   (interactive "P")
12788   (prog1
12789       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12790             older len)
12791         (setq older
12792               ;; Some nntp servers lie about their active range.  When
12793               ;; this happens, the active range can be in the millions.
12794               ;; Use a compressed range to avoid creating a huge list.
12795               (gnus-range-difference (list gnus-newsgroup-active) old))
12796         (setq len (gnus-range-length older))
12797         (cond
12798          ((null older) nil)
12799          ((numberp all)
12800           (if (< all len)
12801               (let ((older-range (nreverse older)))
12802                 (setq older nil)
12803
12804                 (while (> all 0)
12805                   (let* ((r (pop older-range))
12806                          (min (if (numberp r) r (car r)))
12807                          (max (if (numberp r) r (cdr r))))
12808                     (while (and (<= min max)
12809                                 (> all 0))
12810                       (push max older)
12811                       (setq all (1- all)
12812                             max (1- max))))))
12813             (setq older (gnus-uncompress-range older))))
12814          (all
12815           (setq older (gnus-uncompress-range older)))
12816          (t
12817           (when (and (numberp gnus-large-newsgroup)
12818                    (> len gnus-large-newsgroup))
12819               (let* ((cursor-in-echo-area nil)
12820                      (initial (gnus-parameter-large-newsgroup-initial
12821                                gnus-newsgroup-name))
12822                      (input
12823                       (read-string
12824                        (format
12825                         "How many articles from %s (%s %d): "
12826                         (gnus-group-decoded-name gnus-newsgroup-name)
12827                         (if initial "max" "default")
12828                         len)
12829                        (if initial
12830                            (cons (number-to-string initial)
12831                                  0)))))
12832                 (unless (string-match "^[ \t]*$" input)
12833                   (setq all (string-to-number input))
12834                   (if (< all len)
12835                       (let ((older-range (nreverse older)))
12836                         (setq older nil)
12837
12838                         (while (> all 0)
12839                           (let* ((r (pop older-range))
12840                                  (min (if (numberp r) r (car r)))
12841                                  (max (if (numberp r) r (cdr r))))
12842                             (while (and (<= min max)
12843                                         (> all 0))
12844                               (push max older)
12845                               (setq all (1- all)
12846                                     max (1- max))))))))))
12847           (setq older (gnus-uncompress-range older))))
12848         (if (not older)
12849             (message "No old news.")
12850           (gnus-summary-insert-articles older)
12851           (gnus-summary-limit (gnus-sorted-nunion old older))))
12852     (gnus-summary-position-point)))
12853
12854 (defun gnus-summary-insert-new-articles ()
12855   "Insert all new articles in this group."
12856   (interactive)
12857   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12858         (old-high gnus-newsgroup-highest)
12859         (nnmail-fetched-sources (list t))
12860         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12861         i new)
12862     (unless new-active
12863       (error "Couldn't fetch new data"))
12864     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12865     (setq i (cdr gnus-newsgroup-active)
12866           gnus-newsgroup-highest i)
12867     (while (> i old-high)
12868       (push i new)
12869       (decf i))
12870     (if (not new)
12871         (message "No gnus is bad news")
12872       (gnus-summary-insert-articles new)
12873       (setq gnus-newsgroup-unreads
12874             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12875       (gnus-summary-limit (gnus-sorted-nunion old new))))
12876   (gnus-summary-position-point))
12877
12878 ;;; Bookmark support for Gnus.
12879 (declare-function bookmark-make-record-default
12880                   "bookmark" (&optional no-file no-context posn))
12881 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12882 (declare-function bookmark-default-handler "bookmark" (bmk))
12883 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12884 (defvar bookmark-yank-point)
12885 (defvar bookmark-current-buffer)
12886
12887 (defun gnus-summary-bookmark-make-record ()
12888   "Make a bookmark entry for a Gnus summary buffer."
12889   (let (pos buf)
12890     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12891       (save-restriction              ; FIXME is it necessary to widen?
12892         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12893       (setq buf "art") ; We are recording bookmark from article buffer.
12894       (setq bookmark-yank-point (point))
12895       (setq bookmark-current-buffer (current-buffer))
12896       (gnus-article-show-summary))      ; Go back in summary buffer.
12897     ;; We are now recording bookmark from summary buffer.
12898     (unless buf (setq buf "sum"))
12899     (let* ((subject (elt (gnus-summary-article-header) 1))
12900            (grp     (car gnus-article-current))
12901            (art     (cdr gnus-article-current))
12902            (head    (gnus-summary-article-header art))
12903            (id      (mail-header-id head)))
12904       `(,subject
12905         ,@(condition-case nil
12906               (bookmark-make-record-default 'no-file 'no-context pos)
12907             (wrong-number-of-arguments
12908              (bookmark-make-record-default 'point-only)))
12909         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12910         (group . ,grp) (article . ,art)
12911         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12912
12913 ;;;###autoload
12914 (defun gnus-summary-bookmark-jump (bookmark)
12915   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12916 BOOKMARK is a bookmark name or a bookmark record."
12917   (let ((group    (bookmark-prop-get bookmark 'group))
12918         (article  (bookmark-prop-get bookmark 'article))
12919         (id       (bookmark-prop-get bookmark 'message-id))
12920         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12921     (gnus-fetch-group group (list article))
12922     (gnus-summary-insert-cached-articles)
12923     (gnus-summary-goto-article id nil 'force)
12924     ;; FIXME we have to wait article buffer is ready (only large buffer)
12925     ;; Is there a better solution to know that?
12926     ;; If we don't wait `bookmark-default-handler' will have no chance
12927     ;; to set position. However there is no error, just wrong pos.
12928     (sit-for 1)
12929     (when (string= buf "Gnus-art")
12930       (other-window 1))
12931     (bookmark-default-handler
12932      `(""
12933        (buffer . ,(current-buffer))
12934        . ,(bookmark-get-bookmark-record bookmark)))))
12935
12936 (gnus-summary-make-all-marking-commands)
12937
12938 (gnus-ems-redefine)
12939
12940 (provide 'gnus-sum)
12941
12942 (run-hooks 'gnus-sum-load-hook)
12943
12944 ;; Local Variables:
12945 ;; coding: iso-8859-1
12946 ;; End:
12947
12948 ;;; gnus-sum.el ends here