Extend `gnus-interactive-exit' to `quiet'.
[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-expirable nil
1503   "Sorted list of articles in the current newsgroup that can be expired.")
1504
1505 (defvar gnus-newsgroup-processable nil
1506   "List of articles in the current newsgroup that can be processed.")
1507
1508 (defvar gnus-newsgroup-downloadable nil
1509   "Sorted list of articles in the current newsgroup that can be processed.")
1510
1511 (defvar gnus-newsgroup-unfetched nil
1512   "Sorted list of articles in the current newsgroup whose headers have
1513 not been fetched into the agent.
1514
1515 This list will always be a subset of gnus-newsgroup-undownloaded.")
1516
1517 (defvar gnus-newsgroup-undownloaded nil
1518   "List of articles in the current newsgroup that haven't been downloaded.")
1519
1520 (defvar gnus-newsgroup-unsendable nil
1521   "List of articles in the current newsgroup that won't be sent.")
1522
1523 (defvar gnus-newsgroup-bookmarks nil
1524   "List of articles in the current newsgroup that have bookmarks.")
1525
1526 (defvar gnus-newsgroup-dormant nil
1527   "Sorted list of dormant articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-unseen nil
1530   "List of unseen articles in the current newsgroup.")
1531
1532 (defvar gnus-newsgroup-seen nil
1533   "Range of seen articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-articles nil
1536   "List of articles in the current newsgroup.")
1537
1538 (defvar gnus-newsgroup-scored nil
1539   "List of scored articles in the current newsgroup.")
1540
1541 (defvar gnus-newsgroup-headers nil
1542   "List of article headers in the current newsgroup.")
1543
1544 (defvar gnus-newsgroup-threads nil)
1545
1546 (defvar gnus-newsgroup-prepared nil
1547   "Whether the current group has been prepared properly.")
1548
1549 (defvar gnus-newsgroup-ancient nil
1550   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1551
1552 (defvar gnus-newsgroup-sparse nil)
1553
1554 (defvar gnus-current-article nil)
1555 (defvar gnus-article-current nil)
1556 (defvar gnus-current-headers nil)
1557 (defvar gnus-have-all-headers nil)
1558 (defvar gnus-last-article nil)
1559 (defvar gnus-newsgroup-history nil)
1560 (defvar gnus-newsgroup-charset nil)
1561 (defvar gnus-newsgroup-ephemeral-charset nil)
1562 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1563
1564 (defvar gnus-article-before-search nil)
1565
1566 (defvar gnus-summary-local-variables
1567   '(gnus-newsgroup-name
1568
1569     ;; Marks lists
1570     gnus-newsgroup-unreads
1571     gnus-newsgroup-unselected
1572     gnus-newsgroup-marked
1573     gnus-newsgroup-spam-marked
1574     gnus-newsgroup-reads
1575     gnus-newsgroup-saved
1576     gnus-newsgroup-replied
1577     gnus-newsgroup-forwarded
1578     gnus-newsgroup-expirable
1579     gnus-newsgroup-killed
1580     gnus-newsgroup-unseen
1581     gnus-newsgroup-seen
1582     gnus-newsgroup-cached
1583     gnus-newsgroup-downloadable
1584     gnus-newsgroup-undownloaded
1585     gnus-newsgroup-unsendable
1586
1587     gnus-newsgroup-begin gnus-newsgroup-end
1588     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1589     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1590     gnus-newsgroup-last-directory
1591     gnus-newsgroup-auto-expire
1592     gnus-newsgroup-processable
1593     gnus-newsgroup-unfetched
1594     gnus-newsgroup-articles
1595     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1596     gnus-newsgroup-headers gnus-newsgroup-threads
1597     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1598     gnus-current-article gnus-current-headers gnus-have-all-headers
1599     gnus-last-article gnus-article-internal-prepare-hook
1600     (gnus-summary-article-delete-hook . global)
1601     (gnus-summary-article-move-hook . global)
1602     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1603     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1604     gnus-thread-expunge-below
1605     gnus-score-alist gnus-current-score-file
1606     (gnus-summary-expunge-below . global)
1607     (gnus-summary-mark-below . global)
1608     (gnus-orphan-score . global)
1609     gnus-newsgroup-active gnus-scores-exclude-files
1610     gnus-newsgroup-highest
1611     gnus-newsgroup-history gnus-newsgroup-ancient
1612     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1613     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1614     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1615     (gnus-newsgroup-expunged-tally . 0)
1616     gnus-cache-removable-articles
1617     gnus-newsgroup-data gnus-newsgroup-data-reverse
1618     gnus-newsgroup-limit gnus-newsgroup-limits
1619     gnus-newsgroup-charset gnus-newsgroup-display
1620     gnus-summary-use-undownloaded-faces)
1621   "Variables that are buffer-local to the summary buffers.")
1622
1623 (defvar gnus-newsgroup-variables nil
1624   "A list of variables that have separate values in different newsgroups.
1625 A list of newsgroup (summary buffer) local variables, or cons of
1626 variables and their default expressions to be evalled (when the default
1627 values are not nil), that should be made global while the summary buffer
1628 is active.
1629
1630 Note: The default expressions will be evaluated (using function `eval')
1631 before assignment to the local variable rather than just assigned to it.
1632 If the default expression is the symbol `global', that symbol will not
1633 be evaluated but the global value of the local variable will be used
1634 instead.
1635
1636 These variables can be used to set variables in the group parameters
1637 while still allowing them to affect operations done in other buffers.
1638 For example:
1639
1640 \(setq gnus-newsgroup-variables
1641      '(message-use-followup-to
1642        (gnus-visible-headers .
1643          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1644 ")
1645
1646 (eval-when-compile
1647   ;; Bind features so that require will believe that gnus-sum has
1648   ;; already been loaded (avoids infinite recursion)
1649   (let ((features (cons 'gnus-sum features)))
1650     (require 'gnus-art)))
1651
1652 ;; MIME stuff.
1653
1654 (defvar gnus-decode-encoded-word-methods
1655   '(mail-decode-encoded-word-string)
1656   "List of methods used to decode encoded words.
1657
1658 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1659 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1660 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1661 whose names match REGEXP.
1662
1663 For example:
1664 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1665  mail-decode-encoded-word-string
1666  (\"chinese\" . rfc1843-decode-string))")
1667
1668 (defvar gnus-decode-encoded-word-methods-cache nil)
1669
1670 (defun gnus-multi-decode-encoded-word-string (string)
1671   "Apply the functions from `gnus-encoded-word-methods' that match."
1672   (unless (and gnus-decode-encoded-word-methods-cache
1673                (eq gnus-newsgroup-name
1674                    (car gnus-decode-encoded-word-methods-cache)))
1675     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1676     (dolist (method gnus-decode-encoded-word-methods)
1677       (if (symbolp method)
1678           (nconc gnus-decode-encoded-word-methods-cache (list method))
1679         (if (and gnus-newsgroup-name
1680                  (string-match (car method) gnus-newsgroup-name))
1681             (nconc gnus-decode-encoded-word-methods-cache
1682                    (list (cdr method)))))))
1683   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1684     (setq string (funcall method string))))
1685
1686 ;; Subject simplification.
1687
1688 (defun gnus-simplify-whitespace (str)
1689   "Remove excessive whitespace from STR."
1690   ;; Multiple spaces.
1691   (while (string-match "[ \t][ \t]+" str)
1692     (setq str (concat (substring str 0 (match-beginning 0))
1693                         " "
1694                         (substring str (match-end 0)))))
1695   ;; Leading spaces.
1696   (when (string-match "^[ \t]+" str)
1697     (setq str (substring str (match-end 0))))
1698   ;; Trailing spaces.
1699   (when (string-match "[ \t]+$" str)
1700     (setq str (substring str 0 (match-beginning 0))))
1701   str)
1702
1703 (defun gnus-simplify-all-whitespace (str)
1704   "Remove all whitespace from STR."
1705   (while (string-match "[ \t\n]+" str)
1706     (setq str (replace-match "" nil nil str)))
1707   str)
1708
1709 (defsubst gnus-simplify-subject-re (subject)
1710   "Remove \"Re:\" from subject lines."
1711   (if (string-match message-subject-re-regexp subject)
1712       (substring subject (match-end 0))
1713     subject))
1714
1715 (defun gnus-simplify-subject (subject &optional re-only)
1716   "Remove `Re:' and words in parentheses.
1717 If RE-ONLY is non-nil, strip leading `Re:'s only."
1718   (let ((case-fold-search t))           ;Ignore case.
1719     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1720     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1721       (setq subject (substring subject (match-end 0))))
1722     ;; Remove uninteresting prefixes.
1723     (when (and (not re-only)
1724                gnus-simplify-ignored-prefixes
1725                (string-match gnus-simplify-ignored-prefixes subject))
1726       (setq subject (substring subject (match-end 0))))
1727     ;; Remove words in parentheses from end.
1728     (unless re-only
1729       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1730         (setq subject (substring subject 0 (match-beginning 0)))))
1731     ;; Return subject string.
1732     subject))
1733
1734 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1735 ;; all whitespace.
1736 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1737   (goto-char (point-min))
1738   (while (re-search-forward regexp nil t)
1739     (replace-match (or newtext ""))))
1740
1741 (defun gnus-simplify-buffer-fuzzy (regexp)
1742   "Simplify string in the buffer fuzzily.
1743 The string in the accessible portion of the current buffer is simplified.
1744 It is assumed to be a single-line subject.
1745 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1746 matter is removed.  Additional things can be deleted by setting
1747 `gnus-simplify-subject-fuzzy-regexp'."
1748   (let ((case-fold-search t)
1749         (modified-tick))
1750     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1751
1752     (while (not (eq modified-tick (buffer-modified-tick)))
1753       (setq modified-tick (buffer-modified-tick))
1754       (cond
1755        ((listp regexp)
1756         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1757        (regexp
1758         (gnus-simplify-buffer-fuzzy-step regexp)))
1759       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1760       (gnus-simplify-buffer-fuzzy-step
1761        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1762       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1763
1764     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1765     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1766     (gnus-simplify-buffer-fuzzy-step " $")
1767     (gnus-simplify-buffer-fuzzy-step "^ +")))
1768
1769 (defun gnus-simplify-subject-fuzzy (subject)
1770   "Simplify a subject string fuzzily.
1771 See `gnus-simplify-buffer-fuzzy' for details."
1772   (save-excursion
1773     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1774       (gnus-set-work-buffer)
1775       (let ((case-fold-search t))
1776         ;; Remove uninteresting prefixes.
1777         (when (and gnus-simplify-ignored-prefixes
1778                    (string-match gnus-simplify-ignored-prefixes subject))
1779           (setq subject (substring subject (match-end 0))))
1780         (insert subject)
1781         (inline (gnus-simplify-buffer-fuzzy regexp))
1782         (buffer-string)))))
1783
1784 (defsubst gnus-simplify-subject-fully (subject)
1785   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1786   (cond
1787    (gnus-simplify-subject-functions
1788     (gnus-map-function gnus-simplify-subject-functions subject))
1789    ((null gnus-summary-gather-subject-limit)
1790     (gnus-simplify-subject-re subject))
1791    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1792     (gnus-simplify-subject-fuzzy subject))
1793    ((numberp gnus-summary-gather-subject-limit)
1794     (truncate-string-to-width (gnus-simplify-subject-re subject)
1795                               gnus-summary-gather-subject-limit))
1796    (t
1797     subject)))
1798
1799 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1800   "Check whether two subjects are equal.
1801 If optional argument SIMPLE-FIRST is t, first argument is already
1802 simplified."
1803   (cond
1804    ((null simple-first)
1805     (equal (gnus-simplify-subject-fully s1)
1806            (gnus-simplify-subject-fully s2)))
1807    (t
1808     (equal s1
1809            (gnus-simplify-subject-fully s2)))))
1810
1811 (defun gnus-summary-bubble-group ()
1812   "Increase the score of the current group.
1813 This is a handy function to add to `gnus-summary-exit-hook' to
1814 increase the score of each group you read."
1815   (gnus-group-add-score gnus-newsgroup-name))
1816
1817 \f
1818 ;;;
1819 ;;; Gnus summary mode
1820 ;;;
1821
1822 (put 'gnus-summary-mode 'mode-class 'special)
1823
1824 (defvar gnus-article-commands-menu)
1825
1826 ;; Non-orthogonal keys
1827
1828 (gnus-define-keys gnus-summary-mode-map
1829   " " gnus-summary-next-page
1830   "\177" gnus-summary-prev-page
1831   [delete] gnus-summary-prev-page
1832   [backspace] gnus-summary-prev-page
1833   "\r" gnus-summary-scroll-up
1834   "\M-\r" gnus-summary-scroll-down
1835   "n" gnus-summary-next-unread-article
1836   "p" gnus-summary-prev-unread-article
1837   "N" gnus-summary-next-article
1838   "P" gnus-summary-prev-article
1839   "\M-\C-n" gnus-summary-next-same-subject
1840   "\M-\C-p" gnus-summary-prev-same-subject
1841   "\M-n" gnus-summary-next-unread-subject
1842   "\M-p" gnus-summary-prev-unread-subject
1843   "." gnus-summary-first-unread-article
1844   "," gnus-summary-best-unread-article
1845   "\M-s" gnus-summary-search-article-forward
1846   "\M-r" gnus-summary-search-article-backward
1847   "\M-S" gnus-summary-repeat-search-article-forward
1848   "\M-R" gnus-summary-repeat-search-article-backward
1849   "<" gnus-summary-beginning-of-article
1850   ">" gnus-summary-end-of-article
1851   "j" gnus-summary-goto-article
1852   "^" gnus-summary-refer-parent-article
1853   "\M-^" gnus-summary-refer-article
1854   "u" gnus-summary-tick-article-forward
1855   "!" gnus-summary-tick-article-forward
1856   "U" gnus-summary-tick-article-backward
1857   "d" gnus-summary-mark-as-read-forward
1858   "D" gnus-summary-mark-as-read-backward
1859   "E" gnus-summary-mark-as-expirable
1860   "\M-u" gnus-summary-clear-mark-forward
1861   "\M-U" gnus-summary-clear-mark-backward
1862   "k" gnus-summary-kill-same-subject-and-select
1863   "\C-k" gnus-summary-kill-same-subject
1864   "\M-\C-k" gnus-summary-kill-thread
1865   "\M-\C-l" gnus-summary-lower-thread
1866   "e" gnus-summary-edit-article
1867   "#" gnus-summary-mark-as-processable
1868   "\M-#" gnus-summary-unmark-as-processable
1869   "\M-\C-t" gnus-summary-toggle-threads
1870   "\M-\C-s" gnus-summary-show-thread
1871   "\M-\C-h" gnus-summary-hide-thread
1872   "\M-\C-f" gnus-summary-next-thread
1873   "\M-\C-b" gnus-summary-prev-thread
1874   [(meta down)] gnus-summary-next-thread
1875   [(meta up)] gnus-summary-prev-thread
1876   "\M-\C-u" gnus-summary-up-thread
1877   "\M-\C-d" gnus-summary-down-thread
1878   "&" gnus-summary-execute-command
1879   "c" gnus-summary-catchup-and-exit
1880   "\C-w" gnus-summary-mark-region-as-read
1881   "\C-t" gnus-summary-toggle-truncation
1882   "?" gnus-summary-mark-as-dormant
1883   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1884   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1885   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1886   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1887   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1888   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1889   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1890   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1891   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1892   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1893   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1894   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1895   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1896   "=" gnus-summary-expand-window
1897   "\C-x\C-s" gnus-summary-reselect-current-group
1898   "\M-g" gnus-summary-rescan-group
1899   "\C-c\C-r" gnus-summary-caesar-message
1900   "f" gnus-summary-followup
1901   "F" gnus-summary-followup-with-original
1902   "C" gnus-summary-cancel-article
1903   "r" gnus-summary-reply
1904   "R" gnus-summary-reply-with-original
1905   "\C-c\C-f" gnus-summary-mail-forward
1906   "o" gnus-summary-save-article
1907   "\C-o" gnus-summary-save-article-mail
1908   "|" gnus-summary-pipe-output
1909   "\M-k" gnus-summary-edit-local-kill
1910   "\M-K" gnus-summary-edit-global-kill
1911   ;; "V" gnus-version
1912   "\C-c\C-d" gnus-summary-describe-group
1913   "q" gnus-summary-exit
1914   "Q" gnus-summary-exit-no-update
1915   "\C-c\C-i" gnus-info-find-node
1916   gnus-mouse-2 gnus-mouse-pick-article
1917   [follow-link] mouse-face
1918   "m" gnus-summary-mail-other-window
1919   "a" gnus-summary-post-news
1920   "x" gnus-summary-limit-to-unread
1921   "s" gnus-summary-isearch-article
1922   [tab] gnus-summary-widget-forward
1923   "t" gnus-summary-toggle-header
1924   "g" gnus-summary-show-article
1925   "l" gnus-summary-goto-last-article
1926   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1927   "\C-d" gnus-summary-enter-digest-group
1928   "\M-\C-d" gnus-summary-read-document
1929   "\M-\C-e" gnus-summary-edit-parameters
1930   "\M-\C-a" gnus-summary-customize-parameters
1931   "\C-c\C-b" gnus-bug
1932   "*" gnus-cache-enter-article
1933   "\M-*" gnus-cache-remove-article
1934   "\M-&" gnus-summary-universal-argument
1935   "\C-l" gnus-recenter
1936   "I" gnus-summary-increase-score
1937   "L" gnus-summary-lower-score
1938   "\M-i" gnus-symbolic-argument
1939   "h" gnus-summary-select-article-buffer
1940
1941   "b" gnus-article-view-part
1942   "\M-t" gnus-summary-toggle-display-buttonized
1943
1944   "V" gnus-summary-score-map
1945   "X" gnus-uu-extract-map
1946   "S" gnus-summary-send-map)
1947
1948 ;; Sort of orthogonal keymap
1949 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1950   "t" gnus-summary-tick-article-forward
1951   "!" gnus-summary-tick-article-forward
1952   "d" gnus-summary-mark-as-read-forward
1953   "r" gnus-summary-mark-as-read-forward
1954   "c" gnus-summary-clear-mark-forward
1955   " " gnus-summary-clear-mark-forward
1956   "e" gnus-summary-mark-as-expirable
1957   "x" gnus-summary-mark-as-expirable
1958   "?" gnus-summary-mark-as-dormant
1959   "b" gnus-summary-set-bookmark
1960   "B" gnus-summary-remove-bookmark
1961   "#" gnus-summary-mark-as-processable
1962   "\M-#" gnus-summary-unmark-as-processable
1963   "S" gnus-summary-limit-include-expunged
1964   "C" gnus-summary-catchup
1965   "H" gnus-summary-catchup-to-here
1966   "h" gnus-summary-catchup-from-here
1967   "\C-c" gnus-summary-catchup-all
1968   "k" gnus-summary-kill-same-subject-and-select
1969   "K" gnus-summary-kill-same-subject
1970   "P" gnus-uu-mark-map)
1971
1972 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1973   "c" gnus-summary-clear-above
1974   "u" gnus-summary-tick-above
1975   "m" gnus-summary-mark-above
1976   "k" gnus-summary-kill-below)
1977
1978 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1979   "/" gnus-summary-limit-to-subject
1980   "n" gnus-summary-limit-to-articles
1981   "b" gnus-summary-limit-to-bodies
1982   "h" gnus-summary-limit-to-headers
1983   "w" gnus-summary-pop-limit
1984   "s" gnus-summary-limit-to-subject
1985   "a" gnus-summary-limit-to-author
1986   "u" gnus-summary-limit-to-unread
1987   "m" gnus-summary-limit-to-marks
1988   "M" gnus-summary-limit-exclude-marks
1989   "v" gnus-summary-limit-to-score
1990   "*" gnus-summary-limit-include-cached
1991   "D" gnus-summary-limit-include-dormant
1992   "T" gnus-summary-limit-include-thread
1993   "d" gnus-summary-limit-exclude-dormant
1994   "t" gnus-summary-limit-to-age
1995   "." gnus-summary-limit-to-unseen
1996   "x" gnus-summary-limit-to-extra
1997   "p" gnus-summary-limit-to-display-predicate
1998   "E" gnus-summary-limit-include-expunged
1999   "c" gnus-summary-limit-exclude-childless-dormant
2000   "C" gnus-summary-limit-mark-excluded-as-read
2001   "o" gnus-summary-insert-old-articles
2002   "N" gnus-summary-insert-new-articles
2003   "S" gnus-summary-limit-to-singletons
2004   "r" gnus-summary-limit-to-replied
2005   "R" gnus-summary-limit-to-recipient
2006   "A" gnus-summary-limit-to-address)
2007
2008 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2009   "n" gnus-summary-next-unread-article
2010   "p" gnus-summary-prev-unread-article
2011   "N" gnus-summary-next-article
2012   "P" gnus-summary-prev-article
2013   "\C-n" gnus-summary-next-same-subject
2014   "\C-p" gnus-summary-prev-same-subject
2015   "\M-n" gnus-summary-next-unread-subject
2016   "\M-p" gnus-summary-prev-unread-subject
2017   "f" gnus-summary-first-unread-article
2018   "b" gnus-summary-best-unread-article
2019   "j" gnus-summary-goto-article
2020   "g" gnus-summary-goto-subject
2021   "l" gnus-summary-goto-last-article
2022   "o" gnus-summary-pop-article)
2023
2024 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2025   "k" gnus-summary-kill-thread
2026   "E" gnus-summary-expire-thread
2027   "l" gnus-summary-lower-thread
2028   "i" gnus-summary-raise-thread
2029   "T" gnus-summary-toggle-threads
2030   "t" gnus-summary-rethread-current
2031   "^" gnus-summary-reparent-thread
2032   "\M-^" gnus-summary-reparent-children
2033   "s" gnus-summary-show-thread
2034   "S" gnus-summary-show-all-threads
2035   "h" gnus-summary-hide-thread
2036   "H" gnus-summary-hide-all-threads
2037   "n" gnus-summary-next-thread
2038   "p" gnus-summary-prev-thread
2039   "u" gnus-summary-up-thread
2040   "o" gnus-summary-top-thread
2041   "d" gnus-summary-down-thread
2042   "#" gnus-uu-mark-thread
2043   "\M-#" gnus-uu-unmark-thread)
2044
2045 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2046   "g" gnus-summary-prepare
2047   "c" gnus-summary-insert-cached-articles
2048   "d" gnus-summary-insert-dormant-articles
2049   "t" gnus-summary-insert-ticked-articles)
2050
2051 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2052   "c" gnus-summary-catchup-and-exit
2053   "C" gnus-summary-catchup-all-and-exit
2054   "E" gnus-summary-exit-no-update
2055   "Q" gnus-summary-exit
2056   "Z" gnus-summary-exit
2057   "n" gnus-summary-catchup-and-goto-next-group
2058   "p" gnus-summary-catchup-and-goto-prev-group
2059   "R" gnus-summary-reselect-current-group
2060   "G" gnus-summary-rescan-group
2061   "N" gnus-summary-next-group
2062   "s" gnus-summary-save-newsrc
2063   "P" gnus-summary-prev-group)
2064
2065 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2066   " " gnus-summary-next-page
2067   "n" gnus-summary-next-page
2068   "\177" gnus-summary-prev-page
2069   [delete] gnus-summary-prev-page
2070   "p" gnus-summary-prev-page
2071   "\r" gnus-summary-scroll-up
2072   "\M-\r" gnus-summary-scroll-down
2073   "<" gnus-summary-beginning-of-article
2074   ">" gnus-summary-end-of-article
2075   "b" gnus-summary-beginning-of-article
2076   "e" gnus-summary-end-of-article
2077   "^" gnus-summary-refer-parent-article
2078   "r" gnus-summary-refer-parent-article
2079   "C" gnus-summary-show-complete-article
2080   "D" gnus-summary-enter-digest-group
2081   "R" gnus-summary-refer-references
2082   "T" gnus-summary-refer-thread
2083   "W" gnus-warp-to-article
2084   "g" gnus-summary-show-article
2085   "s" gnus-summary-isearch-article
2086   [tab] gnus-summary-widget-forward
2087   "P" gnus-summary-print-article
2088   "S" gnus-sticky-article
2089   "M" gnus-mailing-list-insinuate
2090   "t" gnus-article-babel)
2091
2092 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2093   "b" gnus-article-add-buttons
2094   "B" gnus-article-add-buttons-to-head
2095   "o" gnus-article-treat-overstrike
2096   "e" gnus-article-emphasize
2097   "w" gnus-article-fill-cited-article
2098   "Q" gnus-article-fill-long-lines
2099   "L" gnus-article-toggle-truncate-lines
2100   "C" gnus-article-capitalize-sentences
2101   "c" gnus-article-remove-cr
2102   "q" gnus-article-de-quoted-unreadable
2103   "6" gnus-article-de-base64-unreadable
2104   "Z" gnus-article-decode-HZ
2105   "A" gnus-article-treat-ansi-sequences
2106   "h" gnus-article-wash-html
2107   "u" gnus-article-unsplit-urls
2108   "s" gnus-summary-force-verify-and-decrypt
2109   "f" gnus-article-display-x-face
2110   "l" gnus-summary-stop-page-breaking
2111   "r" gnus-summary-caesar-message
2112   "m" gnus-summary-morse-message
2113   "t" gnus-summary-toggle-header
2114   "g" gnus-treat-smiley
2115   "v" gnus-summary-verbose-headers
2116   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2117   "p" gnus-article-verify-x-pgp-sig
2118   "d" gnus-article-treat-dumbquotes
2119   "U" gnus-article-treat-non-ascii
2120   "i" gnus-summary-idna-message)
2121
2122 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2123   ;; mnemonic: deuglif*Y*
2124   "u" gnus-article-outlook-unwrap-lines
2125   "a" gnus-article-outlook-repair-attribution
2126   "c" gnus-article-outlook-rearrange-citation
2127   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2128
2129 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2130   "a" gnus-article-hide
2131   "h" gnus-article-hide-headers
2132   "b" gnus-article-hide-boring-headers
2133   "s" gnus-article-hide-signature
2134   "c" gnus-article-hide-citation
2135   "C" gnus-article-hide-citation-in-followups
2136   "l" gnus-article-hide-list-identifiers
2137   "B" gnus-article-strip-banner
2138   "P" gnus-article-hide-pem
2139   "\C-c" gnus-article-hide-citation-maybe)
2140
2141 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2142   "a" gnus-article-highlight
2143   "h" gnus-article-highlight-headers
2144   "c" gnus-article-highlight-citation
2145   "s" gnus-article-highlight-signature)
2146
2147 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2148   "f" gnus-article-treat-fold-headers
2149   "u" gnus-article-treat-unfold-headers
2150   "n" gnus-article-treat-fold-newsgroups)
2151
2152 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2153   "x" gnus-article-display-x-face
2154   "d" gnus-article-display-face
2155   "s" gnus-treat-smiley
2156   "D" gnus-article-remove-images
2157   "W" gnus-article-show-images
2158   "f" gnus-treat-from-picon
2159   "m" gnus-treat-mail-picon
2160   "n" gnus-treat-newsgroups-picon
2161   "g" gnus-treat-from-gravatar
2162   "h" gnus-treat-mail-gravatar)
2163
2164 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2165   "w" gnus-article-decode-mime-words
2166   "c" gnus-article-decode-charset
2167   "v" gnus-mime-view-all-parts
2168   "b" gnus-article-view-part)
2169
2170 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2171   "z" gnus-article-date-ut
2172   "u" gnus-article-date-ut
2173   "l" gnus-article-date-local
2174   "p" gnus-article-date-english
2175   "e" gnus-article-date-lapsed
2176   "o" gnus-article-date-original
2177   "i" gnus-article-date-iso8601
2178   "s" gnus-article-date-user)
2179
2180 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2181   "t" gnus-article-remove-trailing-blank-lines
2182   "l" gnus-article-strip-leading-blank-lines
2183   "m" gnus-article-strip-multiple-blank-lines
2184   "a" gnus-article-strip-blank-lines
2185   "A" gnus-article-strip-all-blank-lines
2186   "s" gnus-article-strip-leading-space
2187   "e" gnus-article-strip-trailing-space
2188   "w" gnus-article-remove-leading-whitespace)
2189
2190 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2191   "v" gnus-version
2192   "d" gnus-summary-describe-group
2193   "h" gnus-summary-describe-briefly
2194   "i" gnus-info-find-node)
2195
2196 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2197   "e" gnus-summary-expire-articles
2198   "\M-\C-e" gnus-summary-expire-articles-now
2199   "\177" gnus-summary-delete-article
2200   [delete] gnus-summary-delete-article
2201   [backspace] gnus-summary-delete-article
2202   "m" gnus-summary-move-article
2203   "r" gnus-summary-respool-article
2204   "w" gnus-summary-edit-article
2205   "c" gnus-summary-copy-article
2206   "B" gnus-summary-crosspost-article
2207   "q" gnus-summary-respool-query
2208   "t" gnus-summary-respool-trace
2209   "i" gnus-summary-import-article
2210   "I" gnus-summary-create-article
2211   "p" gnus-summary-article-posted-p)
2212
2213 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2214   "o" gnus-summary-save-article
2215   "m" gnus-summary-save-article-mail
2216   "F" gnus-summary-write-article-file
2217   "r" gnus-summary-save-article-rmail
2218   "f" gnus-summary-save-article-file
2219   "b" gnus-summary-save-article-body-file
2220   "B" gnus-summary-write-article-body-file
2221   "h" gnus-summary-save-article-folder
2222   "v" gnus-summary-save-article-vm
2223   "p" gnus-summary-pipe-output
2224   "P" gnus-summary-muttprint)
2225
2226 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2227   "b" gnus-summary-display-buttonized
2228   "m" gnus-summary-repair-multipart
2229   "v" gnus-article-view-part
2230   "o" gnus-article-save-part
2231   "O" gnus-article-save-part-and-strip
2232   "r" gnus-article-replace-part
2233   "d" gnus-article-delete-part
2234   "t" gnus-article-view-part-as-type
2235   "j" gnus-article-jump-to-part
2236   "c" gnus-article-copy-part
2237   "C" gnus-article-view-part-as-charset
2238   "e" gnus-article-view-part-externally
2239   "H" gnus-article-browse-html-article
2240   "E" gnus-article-encrypt-body
2241   "i" gnus-article-inline-part
2242   "|" gnus-article-pipe-part)
2243
2244 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2245   "p" gnus-summary-mark-as-processable
2246   "u" gnus-summary-unmark-as-processable
2247   "U" gnus-summary-unmark-all-processable
2248   "v" gnus-uu-mark-over
2249   "s" gnus-uu-mark-series
2250   "r" gnus-uu-mark-region
2251   "g" gnus-uu-unmark-region
2252   "R" gnus-uu-mark-by-regexp
2253   "G" gnus-uu-unmark-by-regexp
2254   "t" gnus-uu-mark-thread
2255   "T" gnus-uu-unmark-thread
2256   "a" gnus-uu-mark-all
2257   "b" gnus-uu-mark-buffer
2258   "S" gnus-uu-mark-sparse
2259   "k" gnus-summary-kill-process-mark
2260   "y" gnus-summary-yank-process-mark
2261   "w" gnus-summary-save-process-mark
2262   "i" gnus-uu-invert-processable)
2263
2264 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2265   ;;"x" gnus-uu-extract-any
2266   "m" gnus-summary-save-parts
2267   "u" gnus-uu-decode-uu
2268   "U" gnus-uu-decode-uu-and-save
2269   "s" gnus-uu-decode-unshar
2270   "S" gnus-uu-decode-unshar-and-save
2271   "o" gnus-uu-decode-save
2272   "O" gnus-uu-decode-save
2273   "b" gnus-uu-decode-binhex
2274   "B" gnus-uu-decode-binhex
2275   "Y" gnus-uu-decode-yenc
2276   "p" gnus-uu-decode-postscript
2277   "P" gnus-uu-decode-postscript-and-save)
2278
2279 (gnus-define-keys
2280     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2281   "u" gnus-uu-decode-uu-view
2282   "U" gnus-uu-decode-uu-and-save-view
2283   "s" gnus-uu-decode-unshar-view
2284   "S" gnus-uu-decode-unshar-and-save-view
2285   "o" gnus-uu-decode-save-view
2286   "O" gnus-uu-decode-save-view
2287   "b" gnus-uu-decode-binhex-view
2288   "B" gnus-uu-decode-binhex-view
2289   "p" gnus-uu-decode-postscript-view
2290   "P" gnus-uu-decode-postscript-and-save-view)
2291
2292 (defvar gnus-article-post-menu nil)
2293
2294 (defconst gnus-summary-menu-maxlen 20)
2295
2296 (defun gnus-summary-menu-split (menu)
2297   ;; If we have lots of elements, divide them into groups of 20
2298   ;; and make a pane (or submenu) for each one.
2299   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2300       (let ((menu menu) sublists next
2301             (i 1))
2302         (while menu
2303           ;; Pull off the next gnus-summary-menu-maxlen elements
2304           ;; and make them the next element of sublist.
2305           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2306           (if next
2307               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2308                       nil))
2309           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2310                                              (aref (car (last menu)) 0)) menu)
2311                                sublists))
2312           (setq i (1+ i))
2313           (setq menu next))
2314         (nreverse sublists))
2315     ;; Few elements--put them all in one pane.
2316     menu))
2317
2318 (defun gnus-summary-make-menu-bar ()
2319   (gnus-turn-off-edit-menu 'summary)
2320
2321   (unless (boundp 'gnus-summary-misc-menu)
2322
2323     (easy-menu-define
2324       gnus-summary-kill-menu gnus-summary-mode-map ""
2325       (cons
2326        "Score"
2327        (nconc
2328         (list
2329          ["Customize" gnus-score-customize t])
2330         (gnus-make-score-map 'increase)
2331         (gnus-make-score-map 'lower)
2332         '(("Mark"
2333            ["Kill below" gnus-summary-kill-below t]
2334            ["Mark above" gnus-summary-mark-above t]
2335            ["Tick above" gnus-summary-tick-above t]
2336            ["Clear above" gnus-summary-clear-above t])
2337           ["Current score" gnus-summary-current-score t]
2338           ["Set score" gnus-summary-set-score t]
2339           ["Switch current score file..." gnus-score-change-score-file t]
2340           ["Set mark below..." gnus-score-set-mark-below t]
2341           ["Set expunge below..." gnus-score-set-expunge-below t]
2342           ["Edit current score file" gnus-score-edit-current-scores t]
2343           ["Edit score file..." gnus-score-edit-file t]
2344           ["Trace score" gnus-score-find-trace t]
2345           ["Find words" gnus-score-find-favourite-words t]
2346           ["Rescore buffer" gnus-summary-rescore t]
2347           ["Increase score..." gnus-summary-increase-score t]
2348           ["Lower score..." gnus-summary-lower-score t]))))
2349
2350     ;; Define both the Article menu in the summary buffer and the
2351     ;; equivalent Commands menu in the article buffer here for
2352     ;; consistency.
2353     (let ((innards
2354            `(("Hide"
2355               ["All" gnus-article-hide t]
2356               ["Headers" gnus-article-hide-headers t]
2357               ["Signature" gnus-article-hide-signature t]
2358               ["Citation" gnus-article-hide-citation t]
2359               ["List identifiers" gnus-article-hide-list-identifiers t]
2360               ["Banner" gnus-article-strip-banner t]
2361               ["Boring headers" gnus-article-hide-boring-headers t])
2362              ("Highlight"
2363               ["All" gnus-article-highlight t]
2364               ["Headers" gnus-article-highlight-headers t]
2365               ["Signature" gnus-article-highlight-signature t]
2366               ["Citation" gnus-article-highlight-citation t])
2367              ("MIME"
2368               ["Words" gnus-article-decode-mime-words t]
2369               ["Charset" gnus-article-decode-charset t]
2370               ["QP" gnus-article-de-quoted-unreadable t]
2371               ["Base64" gnus-article-de-base64-unreadable t]
2372               ["View MIME buttons" gnus-summary-display-buttonized t]
2373               ["View all" gnus-mime-view-all-parts t]
2374               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2375               ["Encrypt body" gnus-article-encrypt-body
2376                :active (not (gnus-group-read-only-p))
2377                ,@(if (featurep 'xemacs) nil
2378                    '(:help "Encrypt the message body on disk"))]
2379               ["Extract all parts..." gnus-summary-save-parts t]
2380               ("Multipart"
2381                ["Repair multipart" gnus-summary-repair-multipart t]
2382                ["Pipe part..." gnus-article-pipe-part t]
2383                ["Inline part" gnus-article-inline-part t]
2384                ["View part as type..." gnus-article-view-part-as-type t]
2385                ["Encrypt body" gnus-article-encrypt-body
2386                 :active (not (gnus-group-read-only-p))
2387                ,@(if (featurep 'xemacs) nil
2388                    '(:help "Encrypt the message body on disk"))]
2389                ["View part externally" gnus-article-view-part-externally t]
2390                ["View HTML parts in browser" gnus-article-browse-html-article t]
2391                ["View part with charset..." gnus-article-view-part-as-charset t]
2392                ["Copy part" gnus-article-copy-part t]
2393                ["Save part..." gnus-article-save-part t]
2394                ["View part" gnus-article-view-part t]))
2395              ("Date"
2396               ["Local" gnus-article-date-local t]
2397               ["ISO8601" gnus-article-date-iso8601 t]
2398               ["UT" gnus-article-date-ut t]
2399               ["Original" gnus-article-date-original t]
2400               ["Lapsed" gnus-article-date-lapsed t]
2401               ["User-defined" gnus-article-date-user t])
2402              ("Display"
2403               ["Remove images" gnus-article-remove-images t]
2404               ["Toggle smiley" gnus-treat-smiley t]
2405               ["Show X-Face" gnus-article-display-x-face t]
2406               ["Show picons in From" gnus-treat-from-picon t]
2407               ["Show picons in mail headers" gnus-treat-mail-picon t]
2408               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2409               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2410               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2411               ("View as different encoding"
2412                ,@(gnus-summary-menu-split
2413                   (mapcar
2414                    (lambda (cs)
2415                      ;; Since easymenu under Emacs doesn't allow
2416                      ;; lambda forms for menu commands, we should
2417                      ;; provide intern'ed function symbols.
2418                      (let ((command (intern (format "\
2419 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2420                        (fset command
2421                              `(lambda ()
2422                                 (interactive)
2423                                 (let ((gnus-summary-show-article-charset-alist
2424                                        '((1 . ,cs))))
2425                                   (gnus-summary-show-article 1))))
2426                        `[,(symbol-name cs) ,command t]))
2427                    (sort (if (fboundp 'coding-system-list)
2428                              (coding-system-list)
2429                            (mapcar 'car mm-mime-mule-charset-alist))
2430                          'string<)))))
2431              ("Washing"
2432               ("Remove Blanks"
2433                ["Leading" gnus-article-strip-leading-blank-lines t]
2434                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2435                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2436                ["All of the above" gnus-article-strip-blank-lines t]
2437                ["All" gnus-article-strip-all-blank-lines t]
2438                ["Leading space" gnus-article-strip-leading-space t]
2439                ["Trailing space" gnus-article-strip-trailing-space t]
2440                ["Leading space in headers"
2441                 gnus-article-remove-leading-whitespace t])
2442               ["Overstrike" gnus-article-treat-overstrike t]
2443               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2444               ["Non-ASCII" gnus-article-treat-non-ascii t]
2445               ["Emphasis" gnus-article-emphasize t]
2446               ["Word wrap" gnus-article-fill-cited-article t]
2447               ["Fill long lines" gnus-article-fill-long-lines t]
2448               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2449               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2450               ["Remove CR" gnus-article-remove-cr t]
2451               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2452               ["Base64" gnus-article-de-base64-unreadable t]
2453               ["Rot 13" gnus-summary-caesar-message
2454                ,@(if (featurep 'xemacs) '(t)
2455                    '(:help "\"Caesar rotate\" article by 13"))]
2456               ["De-IDNA" gnus-summary-idna-message t]
2457               ["Morse decode" gnus-summary-morse-message t]
2458               ["Unix pipe..." gnus-summary-pipe-message t]
2459               ["Add buttons" gnus-article-add-buttons t]
2460               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2461               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2462               ["Verbose header" gnus-summary-verbose-headers t]
2463               ["Toggle header" gnus-summary-toggle-header t]
2464               ["Unfold headers" gnus-article-treat-unfold-headers t]
2465               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2466               ["Html" gnus-article-wash-html t]
2467               ["Unsplit URLs" gnus-article-unsplit-urls t]
2468               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2469               ["Decode HZ" gnus-article-decode-HZ t]
2470               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2471               ("(Outlook) Deuglify"
2472                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2473                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2474                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2475                ["Full (Outlook) deuglify"
2476                 gnus-article-outlook-deuglify-article t])
2477               )
2478              ("Output"
2479               ["Save in default format..." gnus-summary-save-article
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article using default method"))]
2482               ["Save in file..." gnus-summary-save-article-file
2483                ,@(if (featurep 'xemacs) '(t)
2484                    '(:help "Save article in file"))]
2485               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2486               ["Save in MH folder..." gnus-summary-save-article-folder t]
2487               ["Save in VM folder..." gnus-summary-save-article-vm t]
2488               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2489               ["Save body in file..." gnus-summary-save-article-body-file t]
2490               ["Pipe through a filter..." gnus-summary-pipe-output t]
2491               ["Print with Muttprint..." gnus-summary-muttprint t]
2492               ["Print" gnus-summary-print-article
2493                ,@(if (featurep 'xemacs) '(t)
2494                    '(:help "Generate and print a PostScript image"))])
2495              ("Copy, move,... (Backend)"
2496               ,@(if (featurep 'xemacs) nil
2497                   '(:help "Copying, moving, expiring articles..."))
2498               ["Respool article..." gnus-summary-respool-article t]
2499               ["Move article..." gnus-summary-move-article
2500                (gnus-check-backend-function
2501                 'request-move-article gnus-newsgroup-name)]
2502               ["Copy article..." gnus-summary-copy-article t]
2503               ["Crosspost article..." gnus-summary-crosspost-article
2504                (gnus-check-backend-function
2505                 'request-replace-article gnus-newsgroup-name)]
2506               ["Import file..." gnus-summary-import-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Create article..." gnus-summary-create-article
2510                (gnus-check-backend-function
2511                 'request-accept-article gnus-newsgroup-name)]
2512               ["Check if posted" gnus-summary-article-posted-p t]
2513               ["Edit article" gnus-summary-edit-article
2514                (not (gnus-group-read-only-p))]
2515               ["Delete article" gnus-summary-delete-article
2516                (gnus-check-backend-function
2517                 'request-expire-articles gnus-newsgroup-name)]
2518               ["Query respool" gnus-summary-respool-query t]
2519               ["Trace respool" gnus-summary-respool-trace t]
2520               ["Delete expirable articles" gnus-summary-expire-articles-now
2521                (gnus-check-backend-function
2522                 'request-expire-articles gnus-newsgroup-name)])
2523              ("Extract"
2524               ["Uudecode" gnus-uu-decode-uu
2525                ,@(if (featurep 'xemacs) '(t)
2526                    '(:help "Decode uuencoded article(s)"))]
2527               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2528               ["Unshar" gnus-uu-decode-unshar t]
2529               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2530               ["Save" gnus-uu-decode-save t]
2531               ["Binhex" gnus-uu-decode-binhex t]
2532               ["Postscript" gnus-uu-decode-postscript t]
2533               ["All MIME parts" gnus-summary-save-parts t])
2534              ("Cache"
2535               ["Enter article" gnus-cache-enter-article t]
2536               ["Remove article" gnus-cache-remove-article t])
2537              ["Translate" gnus-article-babel t]
2538              ["Select article buffer" gnus-summary-select-article-buffer t]
2539              ["Make article buffer sticky" gnus-sticky-article t]
2540              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2541              ["Isearch article..." gnus-summary-isearch-article t]
2542              ["Beginning of the article" gnus-summary-beginning-of-article t]
2543              ["End of the article" gnus-summary-end-of-article t]
2544              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2545              ["Fetch referenced articles" gnus-summary-refer-references t]
2546              ["Fetch current thread" gnus-summary-refer-thread t]
2547              ["Fetch article with id..." gnus-summary-refer-article t]
2548              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2549              ["Redisplay" gnus-summary-show-article t]
2550              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2551       (easy-menu-define
2552         gnus-summary-article-menu gnus-summary-mode-map ""
2553         (cons "Article" innards))
2554
2555       (if (not (keymapp gnus-summary-article-menu))
2556           (easy-menu-define
2557             gnus-article-commands-menu gnus-article-mode-map ""
2558             (cons "Commands" innards))
2559         ;; in Emacs, don't share menu.
2560         (setq gnus-article-commands-menu
2561               (copy-keymap gnus-summary-article-menu))
2562         (define-key gnus-article-mode-map [menu-bar commands]
2563           (cons "Commands" gnus-article-commands-menu))))
2564
2565     (easy-menu-define
2566       gnus-summary-thread-menu gnus-summary-mode-map ""
2567       '("Threads"
2568         ["Find all messages in thread" gnus-summary-refer-thread t]
2569         ["Toggle threading" gnus-summary-toggle-threads t]
2570         ["Hide threads" gnus-summary-hide-all-threads t]
2571         ["Show threads" gnus-summary-show-all-threads t]
2572         ["Hide thread" gnus-summary-hide-thread t]
2573         ["Show thread" gnus-summary-show-thread t]
2574         ["Go to next thread" gnus-summary-next-thread t]
2575         ["Go to previous thread" gnus-summary-prev-thread t]
2576         ["Go down thread" gnus-summary-down-thread t]
2577         ["Go up thread" gnus-summary-up-thread t]
2578         ["Top of thread" gnus-summary-top-thread t]
2579         ["Mark thread as read" gnus-summary-kill-thread t]
2580         ["Mark thread as expired" gnus-summary-expire-thread t]
2581         ["Lower thread score" gnus-summary-lower-thread t]
2582         ["Raise thread score" gnus-summary-raise-thread t]
2583         ["Rethread current" gnus-summary-rethread-current t]))
2584
2585     (easy-menu-define
2586       gnus-summary-post-menu gnus-summary-mode-map ""
2587       `("Post"
2588         ["Send a message (mail or news)" gnus-summary-post-news
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Compose a new message (mail or news)"))]
2591         ["Followup" gnus-summary-followup
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article"))]
2594         ["Followup and yank" gnus-summary-followup-with-original
2595          ,@(if (featurep 'xemacs) '(t)
2596              '(:help "Post followup to this article, quoting its contents"))]
2597         ["Supersede article" gnus-summary-supersede-article t]
2598         ["Cancel article" gnus-summary-cancel-article
2599          ,@(if (featurep 'xemacs) '(t)
2600              '(:help "Cancel an article you posted"))]
2601         ["Reply" gnus-summary-reply t]
2602         ["Reply and yank" gnus-summary-reply-with-original t]
2603         ["Wide reply" gnus-summary-wide-reply t]
2604         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2605          ,@(if (featurep 'xemacs) '(t)
2606              '(:help "Mail a reply, quoting this article"))]
2607         ["Very wide reply" gnus-summary-very-wide-reply t]
2608         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2609          ,@(if (featurep 'xemacs) '(t)
2610              '(:help "Mail a very wide reply, quoting this article"))]
2611         ["Mail forward" gnus-summary-mail-forward t]
2612         ["Post forward" gnus-summary-post-forward t]
2613         ["Digest and mail" gnus-uu-digest-mail-forward t]
2614         ["Digest and post" gnus-uu-digest-post-forward t]
2615         ["Resend message" gnus-summary-resend-message t]
2616         ["Resend message edit" gnus-summary-resend-message-edit t]
2617         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2618         ["Send a mail" gnus-summary-mail-other-window t]
2619         ["Create a local message" gnus-summary-news-other-window t]
2620         ["Uuencode and post" gnus-uu-post-news
2621          ,@(if (featurep 'xemacs) '(t)
2622              '(:help "Post a uuencoded article"))]
2623         ["Followup via news" gnus-summary-followup-to-mail t]
2624         ["Followup via news and yank"
2625          gnus-summary-followup-to-mail-with-original t]
2626         ["Strip signature on reply"
2627          (lambda ()
2628            (interactive)
2629            (if (not (memq message-cite-function
2630                           '(message-cite-original-without-signature
2631                             message-cite-original)))
2632                ;; Stupid workaround for XEmacs not honoring :visible.
2633                (message "Can't toggle this value of `message-cite-function'")
2634              (setq message-cite-function
2635                    (if (eq message-cite-function
2636                            'message-cite-original-without-signature)
2637                        'message-cite-original
2638                      'message-cite-original-without-signature))))
2639          ;; XEmacs barfs on :visible.
2640          ,@(if (featurep 'xemacs) nil
2641              '(:visible (memq message-cite-function
2642                               '(message-cite-original-without-signature
2643                                 message-cite-original))))
2644          :style toggle
2645          :selected (eq message-cite-function
2646                        'message-cite-original-without-signature)
2647          ,@(if (featurep 'xemacs) nil
2648              '(:help "Strip signature from cited article when replying."))]
2649         ;;("Draft"
2650         ;;["Send" gnus-summary-send-draft t]
2651         ;;["Send bounced" gnus-resend-bounced-mail t])
2652         ))
2653
2654     (cond
2655      ((not (keymapp gnus-summary-post-menu))
2656       (setq gnus-article-post-menu gnus-summary-post-menu))
2657      ((not gnus-article-post-menu)
2658       ;; Don't share post menu.
2659       (setq gnus-article-post-menu
2660             (copy-keymap gnus-summary-post-menu))))
2661     (define-key gnus-article-mode-map [menu-bar post]
2662       (cons "Post" gnus-article-post-menu))
2663
2664     (easy-menu-define
2665       gnus-summary-misc-menu gnus-summary-mode-map ""
2666       `("Gnus"
2667         ("Mark Read"
2668          ["Mark as read" gnus-summary-mark-as-read-forward t]
2669          ["Mark same subject and select"
2670           gnus-summary-kill-same-subject-and-select t]
2671          ["Mark same subject" gnus-summary-kill-same-subject t]
2672          ["Catchup" gnus-summary-catchup
2673           ,@(if (featurep 'xemacs) '(t)
2674               '(:help "Mark unread articles in this group as read"))]
2675          ["Catchup all" gnus-summary-catchup-all t]
2676          ["Catchup to here" gnus-summary-catchup-to-here t]
2677          ["Catchup from here" gnus-summary-catchup-from-here t]
2678          ["Catchup region" gnus-summary-mark-region-as-read
2679           (gnus-mark-active-p)]
2680          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2681         ("Mark Various"
2682          ["Tick" gnus-summary-tick-article-forward t]
2683          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2684          ["Remove marks" gnus-summary-clear-mark-forward t]
2685          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2686          ["Set bookmark" gnus-summary-set-bookmark t]
2687          ["Remove bookmark" gnus-summary-remove-bookmark t])
2688         ("Limit to"
2689          ["Marks..." gnus-summary-limit-to-marks t]
2690          ["Subject..." gnus-summary-limit-to-subject t]
2691          ["Author..." gnus-summary-limit-to-author t]
2692          ["Recipient..." gnus-summary-limit-to-recipient t]
2693          ["Address..." gnus-summary-limit-to-address t]
2694          ["Age..." gnus-summary-limit-to-age t]
2695          ["Extra..." gnus-summary-limit-to-extra t]
2696          ["Score..." gnus-summary-limit-to-score t]
2697          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2698          ["Unread" gnus-summary-limit-to-unread t]
2699          ["Unseen" gnus-summary-limit-to-unseen t]
2700          ["Singletons" gnus-summary-limit-to-singletons t]
2701          ["Replied" gnus-summary-limit-to-replied t]
2702          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2703          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2704          ["Pop limit" gnus-summary-pop-limit t]
2705          ["Show dormant" gnus-summary-limit-include-dormant t]
2706          ["Hide childless dormant"
2707           gnus-summary-limit-exclude-childless-dormant t]
2708          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2709          ["Hide marked" gnus-summary-limit-exclude-marks t]
2710          ["Show expunged" gnus-summary-limit-include-expunged t])
2711         ("Process Mark"
2712          ["Set mark" gnus-summary-mark-as-processable t]
2713          ["Remove mark" gnus-summary-unmark-as-processable t]
2714          ["Remove all marks" gnus-summary-unmark-all-processable t]
2715          ["Invert marks" gnus-uu-invert-processable t]
2716          ["Mark above" gnus-uu-mark-over t]
2717          ["Mark series" gnus-uu-mark-series t]
2718          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2719          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2720          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2721          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2722          ["Mark all" gnus-uu-mark-all t]
2723          ["Mark buffer" gnus-uu-mark-buffer t]
2724          ["Mark sparse" gnus-uu-mark-sparse t]
2725          ["Mark thread" gnus-uu-mark-thread t]
2726          ["Unmark thread" gnus-uu-unmark-thread t]
2727          ("Process Mark Sets"
2728           ["Kill" gnus-summary-kill-process-mark t]
2729           ["Yank" gnus-summary-yank-process-mark
2730            gnus-newsgroup-process-stack]
2731           ["Save" gnus-summary-save-process-mark t]
2732           ["Run command on marked..." gnus-summary-universal-argument t]))
2733         ("Registry Marks")
2734         ("Scroll article"
2735          ["Page forward" gnus-summary-next-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show next page of article"))]
2738          ["Page backward" gnus-summary-prev-page
2739           ,@(if (featurep 'xemacs) '(t)
2740               '(:help "Show previous page of article"))]
2741          ["Line forward" gnus-summary-scroll-up t])
2742         ("Move"
2743          ["Next unread article" gnus-summary-next-unread-article t]
2744          ["Previous unread article" gnus-summary-prev-unread-article t]
2745          ["Next article" gnus-summary-next-article t]
2746          ["Previous article" gnus-summary-prev-article t]
2747          ["Next unread subject" gnus-summary-next-unread-subject t]
2748          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2749          ["Next article same subject" gnus-summary-next-same-subject t]
2750          ["Previous article same subject" gnus-summary-prev-same-subject t]
2751          ["First unread article" gnus-summary-first-unread-article t]
2752          ["Best unread article" gnus-summary-best-unread-article t]
2753          ["Go to subject number..." gnus-summary-goto-subject t]
2754          ["Go to article number..." gnus-summary-goto-article t]
2755          ["Go to the last article" gnus-summary-goto-last-article t]
2756          ["Pop article off history" gnus-summary-pop-article t])
2757         ("Sort"
2758          ["Sort by number" gnus-summary-sort-by-number t]
2759          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2760          ["Sort by author" gnus-summary-sort-by-author t]
2761          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2762          ["Sort by subject" gnus-summary-sort-by-subject t]
2763          ["Sort by date" gnus-summary-sort-by-date t]
2764          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2765          ["Sort by score" gnus-summary-sort-by-score t]
2766          ["Sort by lines" gnus-summary-sort-by-lines t]
2767          ["Sort by characters" gnus-summary-sort-by-chars t]
2768          ["Randomize" gnus-summary-sort-by-random t]
2769          ["Original sort" gnus-summary-sort-by-original t])
2770         ("Help"
2771          ["Describe group" gnus-summary-describe-group t]
2772          ["Read manual" gnus-info-find-node t])
2773         ("Modes"
2774          ["Pick and read" gnus-pick-mode t]
2775          ["Binary" gnus-binary-mode t])
2776         ("Regeneration"
2777          ["Regenerate" gnus-summary-prepare t]
2778          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2779          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2780          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2781          ["Toggle threading" gnus-summary-toggle-threads t])
2782         ["See old articles" gnus-summary-insert-old-articles t]
2783         ["See new articles" gnus-summary-insert-new-articles t]
2784         ["Filter articles..." gnus-summary-execute-command t]
2785         ["Run command on articles..." gnus-summary-universal-argument t]
2786         ["Search articles forward..." gnus-summary-search-article-forward t]
2787         ["Search articles backward..." gnus-summary-search-article-backward t]
2788         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2789         ["Expand window" gnus-summary-expand-window t]
2790         ["Expire expirable articles" gnus-summary-expire-articles
2791          (gnus-check-backend-function
2792           'request-expire-articles gnus-newsgroup-name)]
2793         ["Edit local kill file" gnus-summary-edit-local-kill t]
2794         ["Edit main kill file" gnus-summary-edit-global-kill t]
2795         ["Edit group parameters" gnus-summary-edit-parameters t]
2796         ["Customize group parameters" gnus-summary-customize-parameters t]
2797         ["Send a bug report" gnus-bug t]
2798         ("Exit"
2799          ["Catchup and exit" gnus-summary-catchup-and-exit
2800           ,@(if (featurep 'xemacs) '(t)
2801               '(:help "Mark unread articles in this group as read, then exit"))]
2802          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2803          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2804          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2805          ["Exit group" gnus-summary-exit
2806           ,@(if (featurep 'xemacs) '(t)
2807               '(:help "Exit current group, return to group selection mode"))]
2808          ["Exit group without updating" gnus-summary-exit-no-update t]
2809          ["Exit and goto next group" gnus-summary-next-group t]
2810          ["Exit and goto prev group" gnus-summary-prev-group t]
2811          ["Reselect group" gnus-summary-reselect-current-group t]
2812          ["Rescan group" gnus-summary-rescan-group t]
2813          ["Update dribble" gnus-summary-save-newsrc t])))
2814
2815     (gnus-run-hooks 'gnus-summary-menu-hook)))
2816
2817 (defvar gnus-summary-tool-bar-map nil)
2818
2819 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2820 ;; affect _new_ message buffers.  We might add a function that walks thru all
2821 ;; summary-mode buffers and force the update.
2822 (defun gnus-summary-tool-bar-update (&optional symbol value)
2823   "Update summary mode toolbar.
2824 Setter function for custom variables."
2825   (setq-default gnus-summary-tool-bar-map nil)
2826   (when symbol
2827     ;; When used as ":set" function:
2828     (set-default symbol value))
2829   (when (gnus-buffer-live-p gnus-summary-buffer)
2830     (with-current-buffer gnus-summary-buffer
2831       (gnus-summary-make-tool-bar))))
2832
2833 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2834                                      'gnus-summary-tool-bar-gnome
2835                                    'gnus-summary-tool-bar-retro)
2836   "Specifies the Gnus summary tool bar.
2837
2838 It can be either a list or a symbol refering to a list.  See
2839 `gmm-tool-bar-from-list' for the format of the list.  The
2840 default key map is `gnus-summary-mode-map'.
2841
2842 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2843 `gnus-summary-tool-bar-retro'."
2844   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2845                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2846                  (repeat :tag "User defined list" gmm-tool-bar-item)
2847                  (symbol))
2848   :version "23.1" ;; No Gnus
2849   :initialize 'custom-initialize-default
2850   :set 'gnus-summary-tool-bar-update
2851   :group 'gnus-summary)
2852
2853 (defcustom gnus-summary-tool-bar-gnome
2854   '((gnus-summary-post-news "mail/compose" nil)
2855     (gnus-summary-insert-new-articles "mail/inbox" nil
2856                                       :visible (or (not gnus-agent)
2857                                                    gnus-plugged))
2858     (gnus-summary-reply-with-original "mail/reply")
2859     (gnus-summary-reply "mail/reply" nil :visible nil)
2860     (gnus-summary-followup-with-original "mail/reply-all")
2861     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2862     (gnus-summary-mail-forward "mail/forward")
2863     (gnus-summary-save-article "mail/save")
2864     (gnus-summary-search-article-forward "search" nil :visible nil)
2865     (gnus-summary-print-article "print")
2866     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2867     ;; Some new commands that may need more suitable icons:
2868     (gnus-summary-save-newsrc "save" nil :visible nil)
2869     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2870     (gnus-summary-prev-article "left-arrow")
2871     (gnus-summary-next-article "right-arrow")
2872     (gnus-summary-next-page "next-page")
2873     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2874     ;;
2875     ;; Maybe some sort-by-... could be added:
2876     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2877     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2878     (gnus-summary-mark-as-expirable
2879      "delete" nil
2880      :visible (gnus-check-backend-function 'request-expire-articles
2881                                            gnus-newsgroup-name))
2882     (gnus-summary-mark-as-spam
2883      "mail/spam" t
2884      :visible (and (fboundp 'spam-group-ham-contents-p)
2885                    (spam-group-ham-contents-p gnus-newsgroup-name))
2886      :help "Mark as spam")
2887     (gnus-summary-mark-as-read-forward
2888      "mail/not-spam" nil
2889      :visible (and (fboundp 'spam-group-spam-contents-p)
2890                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2891     ;;
2892     (gnus-summary-exit "exit")
2893     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2894     (gnus-info-find-node "help"))
2895   "List of functions for the summary tool bar (GNOME style).
2896
2897 See `gmm-tool-bar-from-list' for the format of the list."
2898   :type '(repeat gmm-tool-bar-item)
2899   :version "23.1" ;; No Gnus
2900   :initialize 'custom-initialize-default
2901   :set 'gnus-summary-tool-bar-update
2902   :group 'gnus-summary)
2903
2904 (defcustom gnus-summary-tool-bar-retro
2905   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2906     (gnus-summary-next-unread-article "gnus/next-ur")
2907     (gnus-summary-post-news "gnus/post")
2908     (gnus-summary-followup-with-original "gnus/fuwo")
2909     (gnus-summary-followup "gnus/followup")
2910     (gnus-summary-reply-with-original "gnus/reply-wo")
2911     (gnus-summary-reply "gnus/reply")
2912     (gnus-summary-caesar-message "gnus/rot13")
2913     (gnus-uu-decode-uu "gnus/uu-decode")
2914     (gnus-summary-save-article-file "gnus/save-aif")
2915     (gnus-summary-save-article "gnus/save-art")
2916     (gnus-uu-post-news "gnus/uu-post")
2917     (gnus-summary-catchup "gnus/catchup")
2918     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2919     (gnus-summary-exit "gnus/exit-summ")
2920     ;; Some new command that may need more suitable icons:
2921     (gnus-summary-print-article "gnus/print" nil :visible nil)
2922     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2923     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2924     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2925     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2926     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2927     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2928     ;;
2929     (gnus-info-find-node "gnus/help" nil :visible nil))
2930   "List of functions for the summary tool bar (retro look).
2931
2932 See `gmm-tool-bar-from-list' for the format of the list."
2933   :type '(repeat gmm-tool-bar-item)
2934   :version "23.1" ;; No Gnus
2935   :initialize 'custom-initialize-default
2936   :set 'gnus-summary-tool-bar-update
2937   :group 'gnus-summary)
2938
2939 (defcustom gnus-summary-tool-bar-zap-list t
2940   "List of icon items from the global tool bar.
2941 These items are not displayed in the Gnus summary mode tool bar.
2942
2943 See `gmm-tool-bar-from-list' for the format of the list."
2944   :type 'gmm-tool-bar-zap-list
2945   :version "23.1" ;; No Gnus
2946   :initialize 'custom-initialize-default
2947   :set 'gnus-summary-tool-bar-update
2948   :group 'gnus-summary)
2949
2950 (defvar image-load-path)
2951 (defvar tool-bar-map)
2952
2953 (defun gnus-summary-make-tool-bar (&optional force)
2954   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2955 When FORCE, rebuild the tool bar."
2956   (when (and (not (featurep 'xemacs))
2957              (boundp 'tool-bar-mode)
2958              tool-bar-mode
2959              (or (not gnus-summary-tool-bar-map) force))
2960     (let* ((load-path
2961             (gmm-image-load-path-for-library "gnus"
2962                                              "mail/save.xpm"
2963                                              nil t))
2964            (image-load-path (cons (car load-path)
2965                                   (when (boundp 'image-load-path)
2966                                     image-load-path)))
2967            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2968                                         gnus-summary-tool-bar-zap-list
2969                                         'gnus-summary-mode-map)))
2970       (when map
2971         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2972         ;; uses it's value.
2973         (setq gnus-summary-tool-bar-map map))))
2974   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2975
2976 (defun gnus-score-set-default (var value)
2977   "A version of set that updates the GNU Emacs menu-bar."
2978   (set var value)
2979   ;; It is the message that forces the active status to be updated.
2980   (message ""))
2981
2982 (defun gnus-make-score-map (type)
2983   "Make a summary score map of type TYPE."
2984   (if t
2985       nil
2986     (let ((headers '(("author" "from" string)
2987                      ("subject" "subject" string)
2988                      ("article body" "body" string)
2989                      ("article head" "head" string)
2990                      ("xref" "xref" string)
2991                      ("extra header" "extra" string)
2992                      ("lines" "lines" number)
2993                      ("followups to author" "followup" string)))
2994           (types '((number ("less than" <)
2995                            ("greater than" >)
2996                            ("equal" =))
2997                    (string ("substring" s)
2998                            ("exact string" e)
2999                            ("fuzzy string" f)
3000                            ("regexp" r))))
3001           (perms '(("temporary" (current-time-string))
3002                    ("permanent" nil)
3003                    ("immediate" now)))
3004           header)
3005       (list
3006        (apply
3007         'nconc
3008         (list
3009          (if (eq type 'lower)
3010              "Lower score"
3011            "Increase score"))
3012         (let (outh)
3013           (while headers
3014             (setq header (car headers))
3015             (setq outh
3016                   (cons
3017                    (apply
3018                     'nconc
3019                     (list (car header))
3020                     (let ((ts (cdr (assoc (nth 2 header) types)))
3021                           outt)
3022                       (while ts
3023                         (setq outt
3024                               (cons
3025                                (apply
3026                                 'nconc
3027                                 (list (caar ts))
3028                                 (let ((ps perms)
3029                                       outp)
3030                                   (while ps
3031                                     (setq outp
3032                                           (cons
3033                                            (vector
3034                                             (caar ps)
3035                                             (list
3036                                              'gnus-summary-score-entry
3037                                              (nth 1 header)
3038                                              (if (or (string= (nth 1 header)
3039                                                               "head")
3040                                                      (string= (nth 1 header)
3041                                                               "body"))
3042                                                  ""
3043                                                (list 'gnus-summary-header
3044                                                      (nth 1 header)))
3045                                              (list 'quote (nth 1 (car ts)))
3046                                              (list 'gnus-score-delta-default
3047                                                    nil)
3048                                              (nth 1 (car ps))
3049                                              t)
3050                                             t)
3051                                            outp))
3052                                     (setq ps (cdr ps)))
3053                                   (list (nreverse outp))))
3054                                outt))
3055                         (setq ts (cdr ts)))
3056                       (list (nreverse outt))))
3057                    outh))
3058             (setq headers (cdr headers)))
3059           (list (nreverse outh))))))))
3060
3061
3062 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3063 (defvar bookmark-make-record-function)
3064 \f
3065
3066 (defun gnus-summary-mode (&optional group)
3067   "Major mode for reading articles.
3068
3069 All normal editing commands are switched off.
3070 \\<gnus-summary-mode-map>
3071 Each line in this buffer represents one article.  To read an
3072 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3073 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3074 respectively.
3075
3076 You can also post articles and send mail from this buffer.  To
3077 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3078 of an article, type `\\[gnus-summary-reply]'.
3079
3080 There are approx. one gazillion commands you can execute in this
3081 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3082
3083 The following commands are available:
3084
3085 \\{gnus-summary-mode-map}"
3086   (interactive)
3087   (kill-all-local-variables)
3088   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3089     (gnus-summary-make-local-variables))
3090   (gnus-summary-make-local-variables)
3091   (setq gnus-newsgroup-name group)
3092   (when (gnus-visual-p 'summary-menu 'menu)
3093     (gnus-summary-make-menu-bar)
3094     (gnus-summary-make-tool-bar))
3095   (gnus-make-thread-indent-array)
3096   (gnus-simplify-mode-line)
3097   (setq major-mode 'gnus-summary-mode)
3098   (setq mode-name "Summary")
3099   (use-local-map gnus-summary-mode-map)
3100   (buffer-disable-undo)
3101   (setq buffer-read-only t              ;Disable modification
3102         show-trailing-whitespace nil)
3103   (setq truncate-lines t)
3104   (add-to-invisibility-spec '(gnus-sum . t))
3105   (gnus-summary-set-display-table)
3106   (gnus-set-default-directory)
3107   (make-local-variable 'gnus-summary-line-format)
3108   (make-local-variable 'gnus-summary-line-format-spec)
3109   (make-local-variable 'gnus-summary-dummy-line-format)
3110   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3111   (make-local-variable 'gnus-summary-mark-positions)
3112   (gnus-make-local-hook 'pre-command-hook)
3113   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3114   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3115   (turn-on-gnus-mailing-list-mode)
3116   (mm-enable-multibyte)
3117   (set (make-local-variable 'bookmark-make-record-function)
3118        'gnus-summary-bookmark-make-record)
3119   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3120   (gnus-update-summary-mark-positions))
3121
3122 (defun gnus-summary-make-local-variables ()
3123   "Make all the local summary buffer variables."
3124   (let (global)
3125     (dolist (local gnus-summary-local-variables)
3126       (if (consp local)
3127           (progn
3128             (if (eq (cdr local) 'global)
3129                 ;; Copy the global value of the variable.
3130                 (setq global (symbol-value (car local)))
3131               ;; Use the value from the list.
3132               (setq global (eval (cdr local))))
3133             (set (make-local-variable (car local)) global))
3134         ;; Simple nil-valued local variable.
3135         (set (make-local-variable local) nil)))))
3136
3137 ;; Summary data functions.
3138
3139 (defmacro gnus-data-number (data)
3140   `(car ,data))
3141
3142 (defmacro gnus-data-set-number (data number)
3143   `(setcar ,data ,number))
3144
3145 (defmacro gnus-data-mark (data)
3146   `(nth 1 ,data))
3147
3148 (defmacro gnus-data-set-mark (data mark)
3149   `(setcar (nthcdr 1 ,data) ,mark))
3150
3151 (defmacro gnus-data-pos (data)
3152   `(nth 2 ,data))
3153
3154 (defmacro gnus-data-set-pos (data pos)
3155   `(setcar (nthcdr 2 ,data) ,pos))
3156
3157 (defmacro gnus-data-header (data)
3158   `(nth 3 ,data))
3159
3160 (defmacro gnus-data-set-header (data header)
3161   `(setf (nth 3 ,data) ,header))
3162
3163 (defmacro gnus-data-level (data)
3164   `(nth 4 ,data))
3165
3166 (defmacro gnus-data-unread-p (data)
3167   `(= (nth 1 ,data) gnus-unread-mark))
3168
3169 (defmacro gnus-data-read-p (data)
3170   `(/= (nth 1 ,data) gnus-unread-mark))
3171
3172 (defmacro gnus-data-pseudo-p (data)
3173   `(consp (nth 3 ,data)))
3174
3175 (defmacro gnus-data-find (number)
3176   `(assq ,number gnus-newsgroup-data))
3177
3178 (defmacro gnus-data-find-list (number &optional data)
3179   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3180      (memq (assq ,number bdata)
3181            bdata)))
3182
3183 (defmacro gnus-data-make (number mark pos header level)
3184   `(list ,number ,mark ,pos ,header ,level))
3185
3186 (defun gnus-data-enter (after-article number mark pos header level offset)
3187   (let ((data (gnus-data-find-list after-article)))
3188     (unless data
3189       (error "No such article: %d" after-article))
3190     (setcdr data (cons (gnus-data-make number mark pos header level)
3191                        (cdr data)))
3192     (setq gnus-newsgroup-data-reverse nil)
3193     (gnus-data-update-list (cddr data) offset)))
3194
3195 (defun gnus-data-enter-list (after-article list &optional offset)
3196   (when list
3197     (let ((data (and after-article (gnus-data-find-list after-article)))
3198           (ilist list))
3199       (if (not (or data
3200                    after-article))
3201           (let ((odata gnus-newsgroup-data))
3202             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3203             (when offset
3204               (gnus-data-update-list odata offset)))
3205         ;; Find the last element in the list to be spliced into the main
3206         ;; list.
3207         (setq list (last list))
3208         (if (not data)
3209             (progn
3210               (setcdr list gnus-newsgroup-data)
3211               (setq gnus-newsgroup-data ilist)
3212               (when offset
3213                 (gnus-data-update-list (cdr list) offset)))
3214           (setcdr list (cdr data))
3215           (setcdr data ilist)
3216           (when offset
3217             (gnus-data-update-list (cdr list) offset))))
3218       (setq gnus-newsgroup-data-reverse nil))))
3219
3220 (defun gnus-data-remove (article &optional offset)
3221   (let ((data gnus-newsgroup-data))
3222     (if (= (gnus-data-number (car data)) article)
3223         (progn
3224           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3225                 gnus-newsgroup-data-reverse nil)
3226           (when offset
3227             (gnus-data-update-list gnus-newsgroup-data offset)))
3228       (while (cdr data)
3229         (when (= (gnus-data-number (cadr data)) article)
3230           (setcdr data (cddr data))
3231           (when offset
3232             (gnus-data-update-list (cdr data) offset))
3233           (setq data nil
3234                 gnus-newsgroup-data-reverse nil))
3235         (setq data (cdr data))))))
3236
3237 (defmacro gnus-data-list (backward)
3238   `(if ,backward
3239        (or gnus-newsgroup-data-reverse
3240            (setq gnus-newsgroup-data-reverse
3241                  (reverse gnus-newsgroup-data)))
3242      gnus-newsgroup-data))
3243
3244 (defun gnus-data-update-list (data offset)
3245   "Add OFFSET to the POS of all data entries in DATA."
3246   (setq gnus-newsgroup-data-reverse nil)
3247   (while data
3248     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3249     (setq data (cdr data))))
3250
3251 (defun gnus-summary-article-pseudo-p (article)
3252   "Say whether this article is a pseudo article or not."
3253   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3254
3255 (defmacro gnus-summary-article-sparse-p (article)
3256   "Say whether this article is a sparse article or not."
3257   `(memq ,article gnus-newsgroup-sparse))
3258
3259 (defmacro gnus-summary-article-ancient-p (article)
3260   "Say whether this article is a sparse article or not."
3261   `(memq ,article gnus-newsgroup-ancient))
3262
3263 (defun gnus-article-parent-p (number)
3264   "Say whether this article is a parent or not."
3265   (let ((data (gnus-data-find-list number)))
3266     (and (cdr data)              ; There has to be an article after...
3267          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3268             (gnus-data-level (nth 1 data))))))
3269
3270 (defun gnus-article-children (number)
3271   "Return a list of all children to NUMBER."
3272   (let* ((data (gnus-data-find-list number))
3273          (level (gnus-data-level (car data)))
3274          children)
3275     (setq data (cdr data))
3276     (while (and data
3277                 (= (gnus-data-level (car data)) (1+ level)))
3278       (push (gnus-data-number (car data)) children)
3279       (setq data (cdr data)))
3280     children))
3281
3282 (defmacro gnus-summary-skip-intangible ()
3283   "If the current article is intangible, then jump to a different article."
3284   '(let ((to (get-text-property (point) 'gnus-intangible)))
3285      (and to (gnus-summary-goto-subject to))))
3286
3287 (defmacro gnus-summary-article-intangible-p ()
3288   "Say whether this article is intangible or not."
3289   '(get-text-property (point) 'gnus-intangible))
3290
3291 (defun gnus-article-read-p (article)
3292   "Say whether ARTICLE is read or not."
3293   (not (or (memq article gnus-newsgroup-marked)
3294            (memq article gnus-newsgroup-spam-marked)
3295            (memq article gnus-newsgroup-unreads)
3296            (memq article gnus-newsgroup-unselected)
3297            (memq article gnus-newsgroup-dormant))))
3298
3299 ;; Some summary mode macros.
3300
3301 (defmacro gnus-summary-article-number ()
3302   "The article number of the article on the current line.
3303 If there isn't an article number here, then we return the current
3304 article number."
3305   '(progn
3306      (gnus-summary-skip-intangible)
3307      (or (get-text-property (point) 'gnus-number)
3308          (gnus-summary-last-subject))))
3309
3310 (defmacro gnus-summary-article-header (&optional number)
3311   "Return the header of article NUMBER."
3312   `(gnus-data-header (gnus-data-find
3313                       ,(or number '(gnus-summary-article-number)))))
3314
3315 (defmacro gnus-summary-thread-level (&optional number)
3316   "Return the level of thread that starts with article NUMBER."
3317   `(if (and (eq gnus-summary-make-false-root 'dummy)
3318             (get-text-property (point) 'gnus-intangible))
3319        0
3320      (gnus-data-level (gnus-data-find
3321                        ,(or number '(gnus-summary-article-number))))))
3322
3323 (defmacro gnus-summary-article-mark (&optional number)
3324   "Return the mark of article NUMBER."
3325   `(gnus-data-mark (gnus-data-find
3326                     ,(or number '(gnus-summary-article-number)))))
3327
3328 (defmacro gnus-summary-article-pos (&optional number)
3329   "Return the position of the line of article NUMBER."
3330   `(gnus-data-pos (gnus-data-find
3331                    ,(or number '(gnus-summary-article-number)))))
3332
3333 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3334 (defmacro gnus-summary-article-subject (&optional number)
3335   "Return current subject string or nil if nothing."
3336   `(let ((headers
3337           ,(if number
3338                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3339              '(gnus-data-header (assq (gnus-summary-article-number)
3340                                       gnus-newsgroup-data)))))
3341      (and headers
3342           (vectorp headers)
3343           (mail-header-subject headers))))
3344
3345 (defmacro gnus-summary-article-score (&optional number)
3346   "Return current article score."
3347   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3348                   gnus-newsgroup-scored))
3349        gnus-summary-default-score 0))
3350
3351 (defun gnus-summary-article-children (&optional number)
3352   "Return a list of article numbers that are children of article NUMBER."
3353   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3354          (level (gnus-data-level (car data)))
3355          l children)
3356     (while (and (setq data (cdr data))
3357                 (> (setq l (gnus-data-level (car data))) level))
3358       (and (= (1+ level) l)
3359            (push (gnus-data-number (car data))
3360                  children)))
3361     (nreverse children)))
3362
3363 (defun gnus-summary-article-parent (&optional number)
3364   "Return the article number of the parent of article NUMBER."
3365   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3366                                     (gnus-data-list t)))
3367          (level (gnus-data-level (car data))))
3368     (if (zerop level)
3369         ()                              ; This is a root.
3370       ;; We search until we find an article with a level less than
3371       ;; this one.  That function has to be the parent.
3372       (while (and (setq data (cdr data))
3373                   (not (< (gnus-data-level (car data)) level))))
3374       (and data (gnus-data-number (car data))))))
3375
3376 (defun gnus-unread-mark-p (mark)
3377   "Say whether MARK is the unread mark."
3378   (= mark gnus-unread-mark))
3379
3380 (defun gnus-read-mark-p (mark)
3381   "Say whether MARK is one of the marks that mark as read.
3382 This is all marks except unread, ticked, dormant, and expirable."
3383   (not (or (= mark gnus-unread-mark)
3384            (= mark gnus-ticked-mark)
3385            (= mark gnus-spam-mark)
3386            (= mark gnus-dormant-mark)
3387            (= mark gnus-expirable-mark))))
3388
3389 (defmacro gnus-article-mark (number)
3390   "Return the MARK of article NUMBER.
3391 This macro should only be used when computing the mark the \"first\"
3392 time; i.e., when generating the summary lines.  After that,
3393 `gnus-summary-article-mark' should be used to examine the
3394 marks of articles."
3395   `(cond
3396     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3397     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3398     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3399     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3400     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3401     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3402     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3403     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3404            gnus-ancient-mark))))
3405
3406 ;; Saving hidden threads.
3407
3408 (defmacro gnus-save-hidden-threads (&rest forms)
3409   "Save hidden threads, eval FORMS, and restore the hidden threads."
3410   (let ((config (make-symbol "config")))
3411     `(let ((,config (gnus-hidden-threads-configuration)))
3412        (unwind-protect
3413            (save-excursion
3414              ,@forms)
3415          (gnus-restore-hidden-threads-configuration ,config)))))
3416 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3417 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3418
3419 (defun gnus-data-compute-positions ()
3420   "Compute the positions of all articles."
3421   (setq gnus-newsgroup-data-reverse nil)
3422   (let ((data gnus-newsgroup-data))
3423     (save-excursion
3424       (gnus-save-hidden-threads
3425         (gnus-summary-show-all-threads)
3426         (goto-char (point-min))
3427         (while data
3428           (while (get-text-property (point) 'gnus-intangible)
3429             (forward-line 1))
3430           (gnus-data-set-pos (car data) (+ (point) 3))
3431           (setq data (cdr data))
3432           (forward-line 1))))))
3433
3434 (defun gnus-hidden-threads-configuration ()
3435   "Return the current hidden threads configuration."
3436   (save-excursion
3437     (let (config)
3438       (goto-char (point-min))
3439       (while (not (eobp))
3440         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3441           (push (save-excursion (forward-line 0) (point)) config))
3442         (forward-line 1))
3443       config)))
3444
3445 (defun gnus-restore-hidden-threads-configuration (config)
3446   "Restore hidden threads configuration from CONFIG."
3447   (save-excursion
3448     (let (point (inhibit-read-only t))
3449       (while (setq point (pop config))
3450         (goto-char point)
3451         (gnus-summary-hide-thread)))))
3452
3453 ;; Various summary mode internalish functions.
3454
3455 (defun gnus-mouse-pick-article (e)
3456   (interactive "e")
3457   (mouse-set-point e)
3458   (gnus-summary-next-page nil t))
3459
3460 (defun gnus-summary-set-display-table ()
3461   "Change the display table.
3462 Odd characters have a tendency to mess
3463 up nicely formatted displays - we make all possible glyphs
3464 display only a single character."
3465
3466   ;; We start from the standard display table, if any.
3467   (let ((table (or (copy-sequence standard-display-table)
3468                    (make-display-table)))
3469         (i 32))
3470     ;; Nix out all the control chars...
3471     (while (>= (setq i (1- i)) 0)
3472       (gnus-put-display-table i [??] table))
3473    ;; ... but not newline and cr, of course.  (cr is necessary for the
3474     ;; selective display).
3475     (gnus-put-display-table ?\n nil table)
3476     (gnus-put-display-table ?\r nil table)
3477     ;; We keep TAB as well.
3478     (gnus-put-display-table ?\t nil table)
3479     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3480     ;; Emacs 23 (unicode), that are not set already.
3481     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3482                  160
3483                256)))
3484       (while (>= (setq i (1- i)) 127)
3485         ;; Only modify if the entry is nil.
3486         (unless (gnus-get-display-table i table)
3487           (gnus-put-display-table i [??] table))))
3488     (setq buffer-display-table table)))
3489
3490 (defun gnus-summary-set-article-display-arrow (pos)
3491   "Update the overlay arrow to point to line at position POS."
3492   (when gnus-summary-display-arrow
3493     (make-local-variable 'overlay-arrow-position)
3494     (make-local-variable 'overlay-arrow-string)
3495     (save-excursion
3496       (goto-char pos)
3497       (beginning-of-line)
3498       (unless overlay-arrow-position
3499         (setq overlay-arrow-position (make-marker)))
3500       (setq overlay-arrow-string "=>"
3501             overlay-arrow-position (set-marker overlay-arrow-position
3502                                                (point)
3503                                                (current-buffer))))))
3504
3505 (defun gnus-summary-setup-buffer (group)
3506   "Initialize summary buffer."
3507   (let ((buffer (gnus-summary-buffer-name group))
3508         (dead-name (concat "*Dead Summary "
3509                            (gnus-group-decoded-name group) "*")))
3510     ;; If a dead summary buffer exists, we kill it.
3511     (when (gnus-buffer-live-p dead-name)
3512       (gnus-kill-buffer dead-name))
3513     (if (get-buffer buffer)
3514         (progn
3515           (set-buffer buffer)
3516           (setq gnus-summary-buffer (current-buffer))
3517           (not gnus-newsgroup-prepared))
3518       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3519       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3520       (gnus-summary-mode group)
3521       (when (gnus-group-quit-config group)
3522         (set (make-local-variable 'gnus-single-article-buffer) nil))
3523       (make-local-variable 'gnus-article-buffer)
3524       (make-local-variable 'gnus-article-current)
3525       (make-local-variable 'gnus-original-article-buffer)
3526       (setq gnus-newsgroup-name group)
3527       ;; Set any local variables in the group parameters.
3528       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3529       t)))
3530
3531 (defun gnus-set-global-variables ()
3532   "Set the global equivalents of the buffer-local variables.
3533 They are set to the latest values they had.  These reflect the summary
3534 buffer that was in action when the last article was fetched."
3535   (when (eq major-mode 'gnus-summary-mode)
3536     (setq gnus-summary-buffer (current-buffer))
3537     (let ((name gnus-newsgroup-name)
3538           (marked gnus-newsgroup-marked)
3539           (spam gnus-newsgroup-spam-marked)
3540           (unread gnus-newsgroup-unreads)
3541           (headers gnus-current-headers)
3542           (data gnus-newsgroup-data)
3543           (summary gnus-summary-buffer)
3544           (article-buffer gnus-article-buffer)
3545           (original gnus-original-article-buffer)
3546           (gac gnus-article-current)
3547           (reffed gnus-reffed-article-number)
3548           (score-file gnus-current-score-file)
3549           (default-charset gnus-newsgroup-charset)
3550           vlist)
3551       (let ((locals gnus-newsgroup-variables))
3552         (while locals
3553           (if (consp (car locals))
3554               (push (eval (caar locals)) vlist)
3555             (push (eval (car locals)) vlist))
3556           (setq locals (cdr locals)))
3557         (setq vlist (nreverse vlist)))
3558       (with-current-buffer gnus-group-buffer
3559         (setq gnus-newsgroup-name name
3560               gnus-newsgroup-marked marked
3561               gnus-newsgroup-spam-marked spam
3562               gnus-newsgroup-unreads unread
3563               gnus-current-headers headers
3564               gnus-newsgroup-data data
3565               gnus-article-current gac
3566               gnus-summary-buffer summary
3567               gnus-article-buffer article-buffer
3568               gnus-original-article-buffer original
3569               gnus-reffed-article-number reffed
3570               gnus-current-score-file score-file
3571               gnus-newsgroup-charset default-charset)
3572         (let ((locals gnus-newsgroup-variables))
3573           (while locals
3574             (if (consp (car locals))
3575                 (set (caar locals) (pop vlist))
3576               (set (car locals) (pop vlist)))
3577             (setq locals (cdr locals))))
3578         ;; The article buffer also has local variables.
3579         (when (gnus-buffer-live-p gnus-article-buffer)
3580           (set-buffer gnus-article-buffer)
3581           (setq gnus-summary-buffer summary))))))
3582
3583 (defun gnus-summary-article-unread-p (article)
3584   "Say whether ARTICLE is unread or not."
3585   (memq article gnus-newsgroup-unreads))
3586
3587 (defun gnus-summary-first-article-p (&optional article)
3588   "Return whether ARTICLE is the first article in the buffer."
3589   (if (not (setq article (or article (gnus-summary-article-number))))
3590       nil
3591     (eq article (caar gnus-newsgroup-data))))
3592
3593 (defun gnus-summary-last-article-p (&optional article)
3594   "Return whether ARTICLE is the last article in the buffer."
3595   (if (not (setq article (or article (gnus-summary-article-number))))
3596       ;; All non-existent numbers are the last article.  :-)
3597       t
3598     (not (cdr (gnus-data-find-list article)))))
3599
3600 (defun gnus-make-thread-indent-array (&optional n)
3601   (when (or n
3602             (progn (setq n 200) nil)
3603             (null gnus-thread-indent-array)
3604             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3605     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3606           gnus-thread-indent-array-level gnus-thread-indent-level)
3607     (while (>= n 0)
3608       (aset gnus-thread-indent-array n
3609             (make-string (* n gnus-thread-indent-level) ? ))
3610       (setq n (1- n)))))
3611
3612 (defun gnus-update-summary-mark-positions ()
3613   "Compute where the summary marks are to go."
3614   (save-excursion
3615     (when (gnus-buffer-exists-p gnus-summary-buffer)
3616       (set-buffer gnus-summary-buffer))
3617     (let ((spec gnus-summary-line-format-spec)
3618           pos)
3619       (save-excursion
3620         (gnus-set-work-buffer)
3621         (let ((gnus-tmp-unread ?Z)
3622               (gnus-replied-mark ?Z)
3623               (gnus-score-below-mark ?Z)
3624               (gnus-score-over-mark ?Z)
3625               (gnus-undownloaded-mark ?Z)
3626               (gnus-summary-line-format-spec spec)
3627               (gnus-newsgroup-downloadable '(0))
3628               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3629               case-fold-search ignores)
3630           ;; Here, all marks are bound to Z.
3631           (gnus-summary-insert-line header
3632                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3633           (goto-char (point-min))
3634           ;; Memorize the positions of the same characters as dummy marks.
3635           (while (re-search-forward "[A-D]" nil t)
3636             (push (point) ignores))
3637           (erase-buffer)
3638           ;; We use A-D as dummy marks in order to know column positions
3639           ;; where marks should be inserted.
3640           (setq gnus-tmp-unread ?A
3641                 gnus-replied-mark ?B
3642                 gnus-score-below-mark ?C
3643                 gnus-score-over-mark ?C
3644                 gnus-undownloaded-mark ?D)
3645           (gnus-summary-insert-line header
3646                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3647           ;; Ignore characters which aren't dummy marks.
3648           (dolist (p ignores)
3649             (delete-region (goto-char (1- p)) p)
3650             (insert ?Z))
3651           (goto-char (point-min))
3652           (setq pos (list (cons 'unread
3653                                 (and (search-forward "A" nil t)
3654                                      (- (point) (point-min) 1)))))
3655           (goto-char (point-min))
3656           (push (cons 'replied (and (search-forward "B" nil t)
3657                                     (- (point) (point-min) 1)))
3658                 pos)
3659           (goto-char (point-min))
3660           (push (cons 'score (and (search-forward "C" nil t)
3661                                   (- (point) (point-min) 1)))
3662                 pos)
3663           (goto-char (point-min))
3664           (push (cons 'download (and (search-forward "D" nil t)
3665                                      (- (point) (point-min) 1)))
3666                 pos)))
3667       (setq gnus-summary-mark-positions pos))))
3668
3669 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3670   "Insert a dummy root in the summary buffer."
3671   (beginning-of-line)
3672   (gnus-add-text-properties
3673    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3674    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3675
3676 (defun gnus-summary-extract-address-component (from)
3677   (or (car (funcall gnus-extract-address-components from))
3678       from))
3679
3680 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3681   (let ((mail-parse-charset gnus-newsgroup-charset)
3682         (ignored-from-addresses (gnus-ignored-from-addresses))
3683         ; Is it really necessary to do this next part for each summary line?
3684         ; Luckily, doesn't seem to slow things down much.
3685         (mail-parse-ignored-charsets
3686          (with-current-buffer gnus-summary-buffer
3687            gnus-newsgroup-ignored-charsets)))
3688     (or
3689      (and ignored-from-addresses
3690           (string-match ignored-from-addresses gnus-tmp-from)
3691           (let ((extra-headers (mail-header-extra header))
3692                 to
3693                 newsgroups)
3694             (cond
3695              ((setq to (cdr (assq 'To extra-headers)))
3696               (concat gnus-summary-to-prefix
3697                       (inline
3698                         (gnus-summary-extract-address-component
3699                          (funcall gnus-decode-encoded-address-function to)))))
3700              ((setq newsgroups
3701                     (or
3702                      (cdr (assq 'Newsgroups extra-headers))
3703                      (and
3704                       (memq 'Newsgroups gnus-extra-headers)
3705                       (eq (car (gnus-find-method-for-group
3706                                 gnus-newsgroup-name)) 'nntp)
3707                       (gnus-group-real-name gnus-newsgroup-name))))
3708               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3709      (gnus-string-mark-left-to-right
3710       (inline
3711        (gnus-summary-extract-address-component gnus-tmp-from))))))
3712
3713 (defun gnus-summary-insert-line (gnus-tmp-header
3714                                  gnus-tmp-level gnus-tmp-current
3715                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3716                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3717                                  &optional gnus-tmp-dummy gnus-tmp-score
3718                                  gnus-tmp-process)
3719   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3720       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3721                                           gnus-tmp-level)))
3722   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3723          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3724          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3725          (gnus-tmp-score-char
3726           (if (or (null gnus-summary-default-score)
3727                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3728                       gnus-summary-zcore-fuzz))
3729               ?                         ;Whitespace
3730             (if (< gnus-tmp-score gnus-summary-default-score)
3731                 gnus-score-below-mark gnus-score-over-mark)))
3732          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3733          (gnus-tmp-replied
3734           (cond (gnus-tmp-process gnus-process-mark)
3735                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3736                  gnus-cached-mark)
3737                 (gnus-tmp-replied gnus-replied-mark)
3738                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3739                  gnus-forwarded-mark)
3740                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3741                  gnus-saved-mark)
3742                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3743                  gnus-unseen-mark)
3744                 (t gnus-no-mark)))
3745          (gnus-tmp-downloaded
3746           (cond (undownloaded
3747                  gnus-undownloaded-mark)
3748                 (gnus-newsgroup-agentized
3749                  gnus-downloaded-mark)
3750                 (t
3751                  gnus-no-mark)))
3752          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3753          (gnus-tmp-name
3754           (cond
3755            ((string-match "<[^>]+> *$" gnus-tmp-from)
3756             (let ((beg (match-beginning 0)))
3757               (or (and (string-match "^\".+\"" gnus-tmp-from)
3758                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3759                   (substring gnus-tmp-from 0 beg))))
3760            ((string-match "(.+)" gnus-tmp-from)
3761             (substring gnus-tmp-from
3762                        (1+ (match-beginning 0)) (1- (match-end 0))))
3763            (t gnus-tmp-from)))
3764          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3765          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3766          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3767          (inhibit-read-only t))
3768     (when (string= gnus-tmp-name "")
3769       (setq gnus-tmp-name gnus-tmp-from))
3770     (unless (numberp gnus-tmp-lines)
3771       (setq gnus-tmp-lines -1))
3772     (if (= gnus-tmp-lines -1)
3773         (setq gnus-tmp-lines "?")
3774       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3775     (condition-case ()
3776         (gnus-put-text-property
3777          (point)
3778          (progn (eval gnus-summary-line-format-spec) (point))
3779          'gnus-number gnus-tmp-number)
3780       (error (gnus-message 5 "Error updating the summary line")))
3781     (when (gnus-visual-p 'summary-highlight 'highlight)
3782       (forward-line -1)
3783       (gnus-summary-highlight-line)
3784       (gnus-run-hooks 'gnus-summary-update-hook)
3785       (forward-line 1))))
3786
3787 (defun gnus-summary-update-line (&optional dont-update)
3788   "Update summary line after change."
3789   (when (and gnus-summary-default-score
3790              (not gnus-summary-inhibit-highlight))
3791     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3792            (article (gnus-summary-article-number))
3793            (score (gnus-summary-article-score article)))
3794       (unless dont-update
3795         (if (and gnus-summary-mark-below
3796                  (< (gnus-summary-article-score)
3797                     gnus-summary-mark-below))
3798             ;; This article has a low score, so we mark it as read.
3799             (when (memq article gnus-newsgroup-unreads)
3800               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3801           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3802             ;; This article was previously marked as read on account
3803             ;; of a low score, but now it has risen, so we mark it as
3804             ;; unread.
3805             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3806         (gnus-summary-update-mark
3807          (if (or (null gnus-summary-default-score)
3808                  (<= (abs (- score gnus-summary-default-score))
3809                      gnus-summary-zcore-fuzz))
3810              ?                          ;Whitespace
3811            (if (< score gnus-summary-default-score)
3812                gnus-score-below-mark gnus-score-over-mark))
3813          'score))
3814       ;; Do visual highlighting.
3815       (when (gnus-visual-p 'summary-highlight 'highlight)
3816         (gnus-summary-highlight-line)
3817         (gnus-run-hooks 'gnus-summary-update-hook)))))
3818
3819 (defvar gnus-tmp-new-adopts nil)
3820
3821 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3822   "Return the number of articles in THREAD.
3823 This may be 0 in some cases -- if none of the articles in
3824 the thread are to be displayed."
3825   (let* ((number
3826          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3827           (cond
3828            ((not (listp thread))
3829             1)
3830            ((and (consp thread) (cdr thread))
3831             (apply
3832              '+ 1 (mapcar
3833                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3834            ((null thread)
3835             1)
3836            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3837             1)
3838            (t 0))))
3839     (when (and level (zerop level) gnus-tmp-new-adopts)
3840       (incf number
3841             (apply '+ (mapcar
3842                        'gnus-summary-number-of-articles-in-thread
3843                        gnus-tmp-new-adopts))))
3844     (if char
3845         (if (> number 1) gnus-not-empty-thread-mark
3846           gnus-empty-thread-mark)
3847       number)))
3848
3849 (defsubst gnus-summary-line-message-size (head)
3850   "Return pretty-printed version of message size.
3851 This function is intended to be used in
3852 `gnus-summary-line-format-alist'."
3853   (let ((c (or (mail-header-chars head) -1)))
3854     (cond ((< c 0) "n/a")               ; chars not available
3855           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3856           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3857           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3858           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3859
3860 (defcustom gnus-user-date-format-alist
3861   '(((gnus-seconds-today) . "Today, %H:%M")
3862     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3863     (604800 . "%A %H:%M")               ; That's one week
3864     ((gnus-seconds-month) . "%A %d")
3865     ((gnus-seconds-year) . "%B %d")
3866     (t . "%b %d %Y"))                   ; This one is used when no other
3867                                         ; does match
3868   "Specifies date format depending on age of article.
3869 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3870 seconds) or a Lisp expression evaluating to a number.  When the age of
3871 the article is less than this number, then use `format-time-string'
3872 with the corresponding FORMAT for displaying the date of the article.
3873 If AGE is not a number or a Lisp expression evaluating to a
3874 non-number, then the corresponding FORMAT is used as a default value.
3875
3876 Note that the list is processed from the beginning, so it should be
3877 sorted by ascending AGE.  Also note that items following the first
3878 non-number AGE will be ignored.
3879
3880 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3881 and `gnus-seconds-year' in the AGE spec.  They return the number of
3882 seconds passed since the start of today, of this month, of this year,
3883 respectively."
3884   :version "24.1"
3885   :group 'gnus-summary-format
3886   :type '(alist :key-type sexp :value-type string))
3887
3888 (defun gnus-user-date (messy-date)
3889   "Format the messy-date according to `gnus-user-date-format-alist'.
3890 Returns \"  ?  \" if there's bad input or if another error occurs.
3891 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3892   (condition-case ()
3893       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3894              (now (gnus-float-time))
3895              ;;If we don't find something suitable we'll use this one
3896              (my-format "%b %d '%y"))
3897         (let* ((difference (- now messy-date))
3898                (templist gnus-user-date-format-alist)
3899                (top (eval (caar templist))))
3900           (while (if (numberp top) (< top difference) (not top))
3901             (progn
3902               (setq templist (cdr templist))
3903               (setq top (eval (caar templist)))))
3904           (if (stringp (cdr (car templist)))
3905               (setq my-format (cdr (car templist)))))
3906         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3907     (error "  ?   ")))
3908
3909 (defun gnus-summary-set-local-parameters (group)
3910   "Go through the local params of GROUP and set all variable specs in that list."
3911   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3912                                         ; really variables.
3913     (dolist (elem (gnus-group-find-parameter group))
3914       (and (consp elem)                 ; Has to be a cons.
3915            (consp (cdr elem))           ; The cdr has to be a list.
3916            (symbolp (car elem))         ; Has to be a symbol in there.
3917            (not (memq (car elem) vars))
3918            (ignore-errors
3919              (push (car elem) vars)
3920              ;; Variables like `gnus-show-threads' that are globally
3921              ;; bound, if used as group parameters, need to get to be
3922              ;; buffer-local, whereas just parameters like `gcc-self',
3923              ;; `timestamp', etc. should not be bound as variables.
3924              (if (boundp (car elem))
3925                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3926                (eval (nth 1 elem))))))))
3927
3928 (defun gnus-summary-read-group (group &optional show-all no-article
3929                                       kill-buffer no-display backward
3930                                       select-articles)
3931   "Start reading news in newsgroup GROUP.
3932 If SHOW-ALL is non-nil, already read articles are also listed.
3933 If NO-ARTICLE is non-nil, no article is selected initially.
3934 If NO-DISPLAY, don't generate a summary buffer."
3935   (let (result)
3936     (while (and group
3937                 (null (setq result
3938                             (let ((gnus-auto-select-next nil))
3939                               (or (gnus-summary-read-group-1
3940                                    group show-all no-article
3941                                    kill-buffer no-display
3942                                    select-articles)
3943                                   (setq show-all nil
3944                                         select-articles nil)))))
3945                 (eq gnus-auto-select-next 'quietly))
3946       (set-buffer gnus-group-buffer)
3947       ;; The entry function called above goes to the next
3948       ;; group automatically, so we go two groups back
3949       ;; if we are searching for the previous group.
3950       (when backward
3951         (gnus-group-prev-unread-group 2))
3952       (if (not (equal group (gnus-group-group-name)))
3953           (setq group (gnus-group-group-name))
3954         (setq group nil)))
3955     result))
3956
3957 (defun gnus-summary-read-group-1 (group show-all no-article
3958                                         kill-buffer no-display
3959                                         &optional select-articles)
3960   ;; Killed foreign groups can't be entered.
3961   ;;  (when (and (not (gnus-group-native-p group))
3962   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3963   ;;    (error "Dead non-native groups can't be entered"))
3964   (gnus-message 5 "Retrieving newsgroup: %s..."
3965                 (gnus-group-decoded-name group))
3966   (let* ((new-group (gnus-summary-setup-buffer group))
3967          (quit-config (gnus-group-quit-config group))
3968          (did-select (and new-group (gnus-select-newsgroup
3969                                      group show-all select-articles))))
3970     (cond
3971      ;; This summary buffer exists already, so we just select it.
3972      ((not new-group)
3973       (gnus-set-global-variables)
3974       (when kill-buffer
3975         (gnus-kill-or-deaden-summary kill-buffer))
3976       (gnus-configure-windows 'summary 'force)
3977       (gnus-set-mode-line 'summary)
3978       (gnus-summary-position-point)
3979       (message "")
3980       t)
3981      ;; We couldn't select this group.
3982      ((null did-select)
3983       (when (and (eq major-mode 'gnus-summary-mode)
3984                  (not (equal (current-buffer) kill-buffer)))
3985         (kill-buffer (current-buffer))
3986         (if (not quit-config)
3987             (progn
3988               ;; Update the info -- marks might need to be removed,
3989               ;; for instance.
3990               (gnus-summary-update-info)
3991               (set-buffer gnus-group-buffer)
3992               (gnus-group-jump-to-group group)
3993               (gnus-group-next-unread-group 1))
3994           (gnus-handle-ephemeral-exit quit-config)))
3995       (if (null (gnus-list-of-unread-articles group))
3996           (gnus-message 3 "Group %s contains no messages" group)
3997         (gnus-message 3 "Can't select group"))
3998       nil)
3999      ;; The user did a `C-g' while prompting for number of articles,
4000      ;; so we exit this group.
4001      ((eq did-select 'quit)
4002       (and (eq major-mode 'gnus-summary-mode)
4003            (not (equal (current-buffer) kill-buffer))
4004            (kill-buffer (current-buffer)))
4005       (when kill-buffer
4006         (gnus-kill-or-deaden-summary kill-buffer))
4007       (if (not quit-config)
4008           (progn
4009             (set-buffer gnus-group-buffer)
4010             (gnus-group-jump-to-group group)
4011             (gnus-configure-windows 'group 'force))
4012         (gnus-handle-ephemeral-exit quit-config))
4013       ;; Finally signal the quit.
4014       (signal 'quit nil))
4015      ;; The group was successfully selected.
4016      (t
4017       (gnus-set-global-variables)
4018       ;; Save the active value in effect when the group was entered.
4019       (setq gnus-newsgroup-active
4020             (gnus-copy-sequence
4021              (gnus-active gnus-newsgroup-name)))
4022       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4023       ;; You can change the summary buffer in some way with this hook.
4024       (gnus-run-hooks 'gnus-select-group-hook)
4025       (when (memq 'summary (gnus-update-format-specifications
4026                             nil 'summary 'summary-mode 'summary-dummy))
4027         ;; The format specification for the summary line was updated,
4028         ;; so we need to update the mark positions as well.
4029         (gnus-update-summary-mark-positions))
4030       ;; Do score processing.
4031       (when gnus-use-scoring
4032         (gnus-possibly-score-headers))
4033       ;; Check whether to fill in the gaps in the threads.
4034       (when gnus-build-sparse-threads
4035         (gnus-build-sparse-threads))
4036       ;; Find the initial limit.
4037       (if show-all
4038           (let ((gnus-newsgroup-dormant nil))
4039             (gnus-summary-initial-limit show-all))
4040         (gnus-summary-initial-limit show-all))
4041       ;; Generate the summary buffer.
4042       (unless no-display
4043         (gnus-summary-prepare))
4044       (when gnus-use-trees
4045         (gnus-tree-open group)
4046         (setq gnus-summary-highlight-line-function
4047               'gnus-tree-highlight-article))
4048       ;; If the summary buffer is empty, but there are some low-scored
4049       ;; articles or some excluded dormants, we include these in the
4050       ;; buffer.
4051       (when (and (zerop (buffer-size))
4052                  (not no-display))
4053         (cond (gnus-newsgroup-dormant
4054                (gnus-summary-limit-include-dormant))
4055               ((and gnus-newsgroup-scored show-all)
4056                (gnus-summary-limit-include-expunged t))))
4057       ;; Function `gnus-apply-kill-file' must be called in this hook.
4058       (gnus-run-hooks 'gnus-apply-kill-hook)
4059       (if (and (zerop (buffer-size))
4060                (not no-display))
4061           (progn
4062             ;; This newsgroup is empty.
4063             (gnus-summary-catchup-and-exit nil t)
4064             (gnus-message 6 "No unread news")
4065             (when kill-buffer
4066               (gnus-kill-or-deaden-summary kill-buffer))
4067             ;; Return nil from this function.
4068             nil)
4069         ;; Hide conversation thread subtrees.  We cannot do this in
4070         ;; gnus-summary-prepare-hook since kill processing may not
4071         ;; work with hidden articles.
4072         (gnus-summary-maybe-hide-threads)
4073         (gnus-configure-windows 'summary)
4074         (when kill-buffer
4075           (gnus-kill-or-deaden-summary kill-buffer))
4076         (gnus-summary-auto-select-subject)
4077         ;; Show first unread article if requested.
4078         (if (and (not no-article)
4079                  (not no-display)
4080                  gnus-newsgroup-unreads
4081                  gnus-auto-select-first)
4082             (progn
4083               (let ((art (gnus-summary-article-number)))
4084                 (unless (and (not gnus-plugged)
4085                              (or (memq art gnus-newsgroup-undownloaded)
4086                                  (memq art gnus-newsgroup-downloadable)))
4087                   (gnus-summary-goto-article art))))
4088           ;; Don't select any articles.
4089           (gnus-summary-position-point)
4090           (gnus-configure-windows 'summary 'force)
4091           (gnus-set-mode-line 'summary))
4092         (when (and gnus-auto-center-group
4093                    (get-buffer-window gnus-group-buffer t))
4094           ;; Gotta use windows, because recenter does weird stuff if
4095           ;; the current buffer ain't the displayed window.
4096           (let ((owin (selected-window)))
4097             (select-window (get-buffer-window gnus-group-buffer t))
4098             (when (gnus-group-goto-group group)
4099               (recenter))
4100             (select-window owin)))
4101         ;; Mark this buffer as "prepared".
4102         (setq gnus-newsgroup-prepared t)
4103         (gnus-run-hooks 'gnus-summary-prepared-hook)
4104         (unless (gnus-ephemeral-group-p group)
4105           (gnus-group-update-group group nil t))
4106         t)))))
4107
4108 (defun gnus-summary-auto-select-subject ()
4109   "Select the subject line on initial group entry."
4110   (goto-char (point-min))
4111   (cond
4112    ((eq gnus-auto-select-subject 'best)
4113     (gnus-summary-best-unread-subject))
4114    ((eq gnus-auto-select-subject 'unread)
4115     (gnus-summary-first-unread-subject))
4116    ((eq gnus-auto-select-subject 'unseen)
4117     (gnus-summary-first-unseen-subject))
4118    ((eq gnus-auto-select-subject 'unseen-or-unread)
4119     (gnus-summary-first-unseen-or-unread-subject))
4120    ((eq gnus-auto-select-subject 'first)
4121     ;; Do nothing.
4122     )
4123    ((functionp gnus-auto-select-subject)
4124     (funcall gnus-auto-select-subject))))
4125
4126 (defun gnus-summary-prepare ()
4127   "Generate the summary buffer."
4128   (interactive)
4129   (let ((inhibit-read-only t))
4130     (erase-buffer)
4131     (setq gnus-newsgroup-data nil
4132           gnus-newsgroup-data-reverse nil)
4133     (gnus-run-hooks 'gnus-summary-generate-hook)
4134     ;; Generate the buffer, either with threads or without.
4135     (when gnus-newsgroup-headers
4136       (gnus-summary-prepare-threads
4137        (if gnus-show-threads
4138            (gnus-sort-gathered-threads
4139             (funcall gnus-summary-thread-gathering-function
4140                      (gnus-sort-threads
4141                       (gnus-cut-threads (gnus-make-threads)))))
4142          ;; Unthreaded display.
4143          (gnus-sort-articles gnus-newsgroup-headers))))
4144     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4145     ;; Call hooks for modifying summary buffer.
4146     (goto-char (point-min))
4147     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4148
4149 (defsubst gnus-general-simplify-subject (subject)
4150   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4151   (setq subject
4152         (cond
4153          ;; Truncate the subject.
4154          (gnus-simplify-subject-functions
4155           (gnus-map-function gnus-simplify-subject-functions subject))
4156          ((numberp gnus-summary-gather-subject-limit)
4157           (setq subject (gnus-simplify-subject-re subject))
4158           (if (> (length subject) gnus-summary-gather-subject-limit)
4159               (substring subject 0 gnus-summary-gather-subject-limit)
4160             subject))
4161          ;; Fuzzily simplify it.
4162          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4163           (gnus-simplify-subject-fuzzy subject))
4164          ;; Just remove the leading "Re:".
4165          (t
4166           (gnus-simplify-subject-re subject))))
4167
4168   (if (and gnus-summary-gather-exclude-subject
4169            (string-match gnus-summary-gather-exclude-subject subject))
4170       nil                         ; This article shouldn't be gathered
4171     subject))
4172
4173 (defun gnus-summary-simplify-subject-query ()
4174   "Query where the respool algorithm would put this article."
4175   (interactive)
4176   (gnus-summary-select-article)
4177   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4178
4179 (defun gnus-gather-threads-by-subject (threads)
4180   "Gather threads by looking at Subject headers."
4181   (if (not gnus-summary-make-false-root)
4182       threads
4183     (let ((hashtb (gnus-make-hashtable 1024))
4184           (prev threads)
4185           (result threads)
4186           subject hthread whole-subject)
4187       (while threads
4188         (setq subject (gnus-general-simplify-subject
4189                        (setq whole-subject (mail-header-subject
4190                                             (caar threads)))))
4191         (when subject
4192           (if (setq hthread (gnus-gethash subject hashtb))
4193               (progn
4194                 ;; We enter a dummy root into the thread, if we
4195                 ;; haven't done that already.
4196                 (unless (stringp (caar hthread))
4197                   (setcar hthread (list whole-subject (car hthread))))
4198                 ;; We add this new gathered thread to this gathered
4199                 ;; thread.
4200                 (setcdr (car hthread)
4201                         (nconc (cdar hthread) (list (car threads))))
4202                 ;; Remove it from the list of threads.
4203                 (setcdr prev (cdr threads))
4204                 (setq threads prev))
4205             ;; Enter this thread into the hash table.
4206             (gnus-sethash subject
4207                           (if gnus-summary-make-false-root-always
4208                               (progn
4209                                 ;; If you want a dummy root above all
4210                                 ;; threads...
4211                                 (setcar threads (list whole-subject
4212                                                       (car threads)))
4213                                 threads)
4214                             threads)
4215                           hashtb)))
4216         (setq prev threads)
4217         (setq threads (cdr threads)))
4218       result)))
4219
4220 (defun gnus-gather-threads-by-references (threads)
4221   "Gather threads by looking at References headers."
4222   (let ((idhashtb (gnus-make-hashtable 1024))
4223         (thhashtb (gnus-make-hashtable 1024))
4224         (prev threads)
4225         (result threads)
4226         ids references id gthread gid entered ref)
4227     (while threads
4228       (when (setq references (mail-header-references (caar threads)))
4229         (setq id (mail-header-id (caar threads))
4230               ids (inline (gnus-split-references references))
4231               entered nil)
4232         (while (setq ref (pop ids))
4233           (setq ids (delete ref ids))
4234           (if (not (setq gid (gnus-gethash ref idhashtb)))
4235               (progn
4236                 (gnus-sethash ref id idhashtb)
4237                 (gnus-sethash id threads thhashtb))
4238             (setq gthread (gnus-gethash gid thhashtb))
4239             (unless entered
4240               ;; We enter a dummy root into the thread, if we
4241               ;; haven't done that already.
4242               (unless (stringp (caar gthread))
4243                 (setcar gthread (list (mail-header-subject (caar gthread))
4244                                       (car gthread))))
4245               ;; We add this new gathered thread to this gathered
4246               ;; thread.
4247               (setcdr (car gthread)
4248                       (nconc (cdar gthread) (list (car threads)))))
4249             ;; Add it into the thread hash table.
4250             (gnus-sethash id gthread thhashtb)
4251             (setq entered t)
4252             ;; Remove it from the list of threads.
4253             (setcdr prev (cdr threads))
4254             (setq threads prev))))
4255       (setq prev threads)
4256       (setq threads (cdr threads)))
4257     result))
4258
4259 (defun gnus-sort-gathered-threads (threads)
4260   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4261   (let ((result threads))
4262     (while threads
4263       (when (stringp (caar threads))
4264         (setcdr (car threads)
4265                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4266       (setq threads (cdr threads)))
4267     result))
4268
4269 (defun gnus-thread-loop-p (root thread)
4270   "Say whether ROOT is in THREAD."
4271   (let ((stack (list thread))
4272         (infloop 0)
4273         th)
4274     (while (setq thread (pop stack))
4275       (setq th (cdr thread))
4276       (while (and th
4277                   (not (eq (caar th) root)))
4278         (pop th))
4279       (if th
4280           ;; We have found a loop.
4281           (let (ref-dep)
4282             (setcdr thread (delq (car th) (cdr thread)))
4283             (if (boundp (setq ref-dep (intern "none"
4284                                               gnus-newsgroup-dependencies)))
4285                 (setcdr (symbol-value ref-dep)
4286                         (nconc (cdr (symbol-value ref-dep))
4287                                (list (car th))))
4288               (set ref-dep (list nil (car th))))
4289             (setq infloop 1
4290                   stack nil))
4291         ;; Push all the subthreads onto the stack.
4292         (push (cdr thread) stack)))
4293     infloop))
4294
4295 (defun gnus-make-threads ()
4296   "Go through the dependency hashtb and find the roots.  Return all threads."
4297   (let (threads)
4298     (while (catch 'infloop
4299              (mapatoms
4300               (lambda (refs)
4301                 ;; Deal with self-referencing References loops.
4302                 (when (and (car (symbol-value refs))
4303                            (not (zerop
4304                                  (apply
4305                                   '+
4306                                   (mapcar
4307                                    (lambda (thread)
4308                                      (gnus-thread-loop-p
4309                                       (car (symbol-value refs)) thread))
4310                                    (cdr (symbol-value refs)))))))
4311                   (setq threads nil)
4312                   (throw 'infloop t))
4313                 (unless (car (symbol-value refs))
4314                   ;; These threads do not refer back to any other
4315                   ;; articles, so they're roots.
4316                   (setq threads (append (cdr (symbol-value refs)) threads))))
4317               gnus-newsgroup-dependencies)))
4318     threads))
4319
4320 ;; Build the thread tree.
4321 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4322   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4323
4324 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4325 if it was already present.
4326
4327 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4328 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4329 Message-IDs will be renamed to a unique Message-ID before being
4330 entered.
4331
4332 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4333   (let* ((id (mail-header-id header))
4334          (id-dep (and id (intern id dependencies)))
4335          parent-id ref ref-dep ref-header replaced)
4336     ;; Enter this `header' in the `dependencies' table.
4337     (cond
4338      ((not id-dep)
4339       (setq header nil))
4340      ;; The first two cases do the normal part: enter a new `header'
4341      ;; in the `dependencies' table.
4342      ((not (boundp id-dep))
4343       (set id-dep (list header)))
4344      ((null (car (symbol-value id-dep)))
4345       (setcar (symbol-value id-dep) header))
4346
4347      ;; From here the `header' was already present in the
4348      ;; `dependencies' table.
4349      (force-new
4350       ;; Overrides an existing entry;
4351       ;; just set the header part of the entry.
4352       (setcar (symbol-value id-dep) header)
4353       (setq replaced t))
4354
4355      ;; Renames the existing `header' to a unique Message-ID.
4356      ((not gnus-summary-ignore-duplicates)
4357       ;; An article with this Message-ID has already been seen.
4358       ;; We rename the Message-ID.
4359       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4360            (list header))
4361       (mail-header-set-id header id))
4362
4363      ;; The last case ignores an existing entry, except it adds any
4364      ;; additional Xrefs (in case the two articles came from different
4365      ;; servers.
4366      ;; Also sets `header' to `nil' meaning that the `dependencies'
4367      ;; table was *not* modified.
4368      (t
4369       (mail-header-set-xref
4370        (car (symbol-value id-dep))
4371        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4372                    "")
4373                (or (mail-header-xref header) "")))
4374       (setq header nil)))
4375
4376     (when (and header (not replaced))
4377       ;; First check that we are not creating a References loop.
4378       (setq parent-id (gnus-parent-id (mail-header-references header)))
4379       (setq ref parent-id)
4380       (while (and ref
4381                   (setq ref-dep (intern-soft ref dependencies))
4382                   (boundp ref-dep)
4383                   (setq ref-header (car (symbol-value ref-dep))))
4384         (if (string= id ref)
4385             ;; Yuk!  This is a reference loop.  Make the article be a
4386             ;; root article.
4387             (progn
4388               (mail-header-set-references (car (symbol-value id-dep)) "none")
4389               (setq ref nil)
4390               (setq parent-id nil))
4391           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4392       (setq ref-dep (intern (or parent-id "none") dependencies))
4393       (if (boundp ref-dep)
4394           (setcdr (symbol-value ref-dep)
4395                   (nconc (cdr (symbol-value ref-dep))
4396                          (list (symbol-value id-dep))))
4397         (set ref-dep (list nil (symbol-value id-dep)))))
4398     header))
4399
4400 (defun gnus-extract-message-id-from-in-reply-to (string)
4401   (if (string-match "<[^>]+>" string)
4402       (substring string (match-beginning 0) (match-end 0))
4403     nil))
4404
4405 (defun gnus-build-sparse-threads ()
4406   (let ((headers gnus-newsgroup-headers)
4407         (mail-parse-charset gnus-newsgroup-charset)
4408         (gnus-summary-ignore-duplicates t)
4409         header references generation relations
4410         subject child end new-child date)
4411     ;; First we create an alist of generations/relations, where
4412     ;; generations is how much we trust the relation, and the relation
4413     ;; is parent/child.
4414     (gnus-message 7 "Making sparse threads...")
4415     (save-excursion
4416       (nnheader-set-temp-buffer " *gnus sparse threads*")
4417       (while (setq header (pop headers))
4418         (when (and (setq references (mail-header-references header))
4419                    (not (string= references "")))
4420           (insert references)
4421           (setq child (mail-header-id header)
4422                 subject (mail-header-subject header)
4423                 date (mail-header-date header)
4424                 generation 0)
4425           (while (search-backward ">" nil t)
4426             (setq end (1+ (point)))
4427             (when (search-backward "<" nil t)
4428               (setq new-child (buffer-substring (point) end))
4429               (push (list (incf generation)
4430                           child (setq child new-child)
4431                           subject date)
4432                     relations)))
4433           (when child
4434             (push (list (1+ generation) child nil subject) relations))
4435           (erase-buffer)))
4436       (kill-buffer (current-buffer)))
4437     ;; Sort over trustworthiness.
4438     (dolist (relation (sort relations 'car-less-than-car))
4439       (when (gnus-dependencies-add-header
4440              (make-full-mail-header
4441               gnus-reffed-article-number
4442               (nth 3 relation) "" (or (nth 4 relation) "")
4443               (nth 1 relation)
4444               (or (nth 2 relation) "") 0 0 "")
4445              gnus-newsgroup-dependencies nil)
4446         (push gnus-reffed-article-number gnus-newsgroup-limit)
4447         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4448         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4449               gnus-newsgroup-reads)
4450         (decf gnus-reffed-article-number)))
4451     (gnus-message 7 "Making sparse threads...done")))
4452
4453 (defun gnus-build-old-threads ()
4454   ;; Look at all the articles that refer back to old articles, and
4455   ;; fetch the headers for the articles that aren't there.  This will
4456   ;; build complete threads - if the roots haven't been expired by the
4457   ;; server, that is.
4458   (let ((mail-parse-charset gnus-newsgroup-charset)
4459         id heads)
4460     (mapatoms
4461      (lambda (refs)
4462        (when (not (car (symbol-value refs)))
4463          (setq heads (cdr (symbol-value refs)))
4464          (while heads
4465            (if (memq (mail-header-number (caar heads))
4466                      gnus-newsgroup-dormant)
4467                (setq heads (cdr heads))
4468              (setq id (symbol-name refs))
4469              (while (and (setq id (gnus-build-get-header id))
4470                          (not (car (gnus-id-to-thread id)))))
4471              (setq heads nil)))))
4472      gnus-newsgroup-dependencies)))
4473
4474 (defsubst gnus-remove-odd-characters (string)
4475   "Translate STRING into something that doesn't contain weird characters."
4476   (mm-subst-char-in-string
4477    ?\r ?\-
4478    (mm-subst-char-in-string ?\n ?\- string t) t))
4479
4480 ;; This function has to be called with point after the article number
4481 ;; on the beginning of the line.
4482 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4483   (let ((eol (point-at-eol))
4484         (buffer (current-buffer))
4485         header references in-reply-to)
4486
4487     ;; overview: [num subject from date id refs chars lines misc]
4488     (unwind-protect
4489         (let (x)
4490           (narrow-to-region (point) eol)
4491           (unless (eobp)
4492             (forward-char))
4493
4494           (setq header
4495                 (make-full-mail-header
4496                  number                 ; number
4497                  (condition-case ()     ; subject
4498                      (gnus-remove-odd-characters
4499                       (funcall gnus-decode-encoded-word-function
4500                                (setq x (nnheader-nov-field))))
4501                    (error x))
4502                  (condition-case ()     ; from
4503                      (gnus-remove-odd-characters
4504                       (funcall gnus-decode-encoded-address-function
4505                                (setq x (nnheader-nov-field))))
4506                    (error x))
4507                  (nnheader-nov-field)   ; date
4508                  (nnheader-nov-read-message-id number)  ; id
4509                  (setq references (nnheader-nov-field)) ; refs
4510                  (nnheader-nov-read-integer) ; chars
4511                  (nnheader-nov-read-integer) ; lines
4512                  (unless (eobp)
4513                    (if (looking-at "Xref: ")
4514                        (goto-char (match-end 0)))
4515                    (nnheader-nov-field)) ; Xref
4516                  (nnheader-nov-parse-extra)))) ; extra
4517
4518       (widen))
4519
4520     (when (and (string= references "")
4521                (setq in-reply-to (mail-header-extra header))
4522                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4523       (mail-header-set-references
4524        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4525
4526     (when gnus-alter-header-function
4527       (funcall gnus-alter-header-function header))
4528     (gnus-dependencies-add-header header dependencies force-new)))
4529
4530 (defun gnus-build-get-header (id)
4531   "Look through the buffer of NOV lines and find the header to ID.
4532 Enter this line into the dependencies hash table, and return
4533 the id of the parent article (if any)."
4534   (let ((deps gnus-newsgroup-dependencies)
4535         found header)
4536     (prog1
4537         (with-current-buffer nntp-server-buffer
4538           (let ((case-fold-search nil))
4539             (goto-char (point-min))
4540             (while (and (not found)
4541                         (search-forward id nil t))
4542               (beginning-of-line)
4543               (setq found (looking-at
4544                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4545                                    (regexp-quote id))))
4546               (or found (beginning-of-line 2)))
4547             (when found
4548               (beginning-of-line)
4549               (and
4550                (setq header (gnus-nov-parse-line
4551                              (read (current-buffer)) deps))
4552                (gnus-parent-id (mail-header-references header))))))
4553       (when header
4554         (let ((number (mail-header-number header)))
4555           (push number gnus-newsgroup-limit)
4556           (push header gnus-newsgroup-headers)
4557           (if (memq number gnus-newsgroup-unselected)
4558               (progn
4559                 (setq gnus-newsgroup-unreads
4560                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4561                                                number))
4562                 (setq gnus-newsgroup-unselected
4563                       (delq number gnus-newsgroup-unselected)))
4564             (push number gnus-newsgroup-ancient)))))))
4565
4566 (defun gnus-build-all-threads ()
4567   "Read all the headers."
4568   (let ((gnus-summary-ignore-duplicates t)
4569         (mail-parse-charset gnus-newsgroup-charset)
4570         (dependencies gnus-newsgroup-dependencies)
4571         header article)
4572     (with-current-buffer nntp-server-buffer
4573       (let ((case-fold-search nil))
4574         (goto-char (point-min))
4575         (while (not (eobp))
4576           (ignore-errors
4577             (setq article (read (current-buffer))
4578                   header (gnus-nov-parse-line article dependencies t)))
4579           (when header
4580             (with-current-buffer gnus-summary-buffer
4581               (push header gnus-newsgroup-headers)
4582               (if (memq (setq article (mail-header-number header))
4583                         gnus-newsgroup-unselected)
4584                   (progn
4585                     (setq gnus-newsgroup-unreads
4586                           (gnus-add-to-sorted-list
4587                            gnus-newsgroup-unreads article))
4588                     (setq gnus-newsgroup-unselected
4589                           (delq article gnus-newsgroup-unselected)))
4590                 (push article gnus-newsgroup-ancient)))
4591             (forward-line 1)))))))
4592
4593 (defun gnus-summary-update-article-line (article header)
4594   "Update the line for ARTICLE using HEADER."
4595   (let* ((id (mail-header-id header))
4596          (thread (gnus-id-to-thread id)))
4597     (unless thread
4598       (error "Article in no thread"))
4599     ;; Update the thread.
4600     (setcar thread header)
4601     (gnus-summary-goto-subject article)
4602     (let* ((datal (gnus-data-find-list article))
4603            (data (car datal))
4604            (inhibit-read-only t)
4605            (level (gnus-summary-thread-level)))
4606       (gnus-delete-line)
4607       (let ((inserted (- (point)
4608                          (progn
4609                            (gnus-summary-insert-line
4610                             header level nil
4611                             (memq article gnus-newsgroup-undownloaded)
4612                             (gnus-article-mark article)
4613                             (memq article gnus-newsgroup-replied)
4614                             (memq article gnus-newsgroup-expirable)
4615                             ;; Only insert the Subject string when it's different
4616                             ;; from the previous Subject string.
4617                             (if (and
4618                                  gnus-show-threads
4619                                  (gnus-subject-equal
4620                                   (condition-case ()
4621                                       (mail-header-subject
4622                                        (gnus-data-header
4623                                         (cadr
4624                                          (gnus-data-find-list
4625                                           article
4626                                           (gnus-data-list t)))))
4627                                     ;; Error on the side of excessive subjects.
4628                                     (error ""))
4629                                   (mail-header-subject header)))
4630                                 ""
4631                               (mail-header-subject header))
4632                             nil (cdr (assq article gnus-newsgroup-scored))
4633                             (memq article gnus-newsgroup-processable))
4634                            (point)))))
4635         (when (cdr datal)
4636           (gnus-data-update-list
4637            (cdr datal)
4638            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4639
4640 (defun gnus-summary-update-article (article &optional iheader)
4641   "Update ARTICLE in the summary buffer."
4642   (set-buffer gnus-summary-buffer)
4643   (let* ((header (gnus-summary-article-header article))
4644          (id (mail-header-id header))
4645          (data (gnus-data-find article))
4646          (thread (gnus-id-to-thread id))
4647          (references (mail-header-references header))
4648          (parent
4649           (gnus-id-to-thread
4650            (or (gnus-parent-id
4651                 (when (and references
4652                            (not (equal "" references)))
4653                   references))
4654                "none")))
4655          (inhibit-read-only t)
4656          (old (car thread)))
4657     (when thread
4658       (unless iheader
4659         (setcar thread nil)
4660         (when parent
4661           (delq thread parent)))
4662       (if (gnus-summary-insert-subject id header)
4663           ;; Set the (possibly) new article number in the data structure.
4664           (gnus-data-set-number data (gnus-id-to-article id))
4665         (setcar thread old)
4666         nil))))
4667
4668 (defun gnus-rebuild-thread (id &optional line)
4669   "Rebuild the thread containing ID.
4670 If LINE, insert the rebuilt thread starting on line LINE."
4671   (let ((inhibit-read-only t)
4672         old-pos current thread data)
4673     (if (not gnus-show-threads)
4674         (setq thread (list (car (gnus-id-to-thread id))))
4675       ;; Get the thread this article is part of.
4676       (setq thread (gnus-remove-thread id)))
4677     (setq old-pos (point-at-bol))
4678     (setq current (save-excursion
4679                     (and (re-search-backward "[\r\n]" nil t)
4680                          (gnus-summary-article-number))))
4681     ;; If this is a gathered thread, we have to go some re-gathering.
4682     (when (stringp (car thread))
4683       (let ((subject (car thread))
4684             roots thr)
4685         (setq thread (cdr thread))
4686         (while thread
4687           (unless (memq (setq thr (gnus-id-to-thread
4688                                    (gnus-root-id
4689                                     (mail-header-id (caar thread)))))
4690                         roots)
4691             (push thr roots))
4692           (setq thread (cdr thread)))
4693         ;; We now have all (unique) roots.
4694         (if (= (length roots) 1)
4695             ;; All the loose roots are now one solid root.
4696             (setq thread (car roots))
4697           (setq thread (cons subject (gnus-sort-threads roots))))))
4698     (let (threads)
4699       ;; We then insert this thread into the summary buffer.
4700       (when line
4701         (goto-char (point-min))
4702         (forward-line (1- line)))
4703       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4704         (if gnus-show-threads
4705             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4706           (gnus-summary-prepare-unthreaded thread))
4707         (setq data (nreverse gnus-newsgroup-data))
4708         (setq threads gnus-newsgroup-threads))
4709       ;; We splice the new data into the data structure.
4710       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4711       ;;!!! then we want to insert at the beginning of the buffer.
4712       ;;!!! That happens to be true with Gnus now, but that may
4713       ;;!!! change in the future.  Perhaps.
4714       (gnus-data-enter-list
4715        (if line nil current) data (- (point) old-pos))
4716       (setq gnus-newsgroup-threads
4717             (nconc threads gnus-newsgroup-threads))
4718       (gnus-data-compute-positions))))
4719
4720 (defun gnus-number-to-header (number)
4721   "Return the header for article NUMBER."
4722   (let ((headers gnus-newsgroup-headers))
4723     (while (and headers
4724                 (not (= number (mail-header-number (car headers)))))
4725       (pop headers))
4726     (when headers
4727       (car headers))))
4728
4729 (defun gnus-parent-headers (in-headers &optional generation)
4730   "Return the headers of the GENERATIONeth parent of HEADERS."
4731   (unless generation
4732     (setq generation 1))
4733   (let ((parent t)
4734         (headers in-headers)
4735         references)
4736     (while (and parent
4737                 (not (zerop generation))
4738                 (setq references (mail-header-references headers)))
4739       (setq headers (if (and references
4740                              (setq parent (gnus-parent-id references)))
4741                         (car (gnus-id-to-thread parent))
4742                       nil))
4743       (decf generation))
4744     (and (not (eq headers in-headers))
4745          headers)))
4746
4747 (defun gnus-id-to-thread (id)
4748   "Return the (sub-)thread where ID appears."
4749   (gnus-gethash id gnus-newsgroup-dependencies))
4750
4751 (defun gnus-id-to-article (id)
4752   "Return the article number of ID."
4753   (let ((thread (gnus-id-to-thread id)))
4754     (when (and thread
4755                (car thread))
4756       (mail-header-number (car thread)))))
4757
4758 (defun gnus-id-to-header (id)
4759   "Return the article headers of ID."
4760   (car (gnus-id-to-thread id)))
4761
4762 (defun gnus-article-displayed-root-p (article)
4763   "Say whether ARTICLE is a root(ish) article."
4764   (let ((level (gnus-summary-thread-level article))
4765         (refs (mail-header-references  (gnus-summary-article-header article)))
4766         particle)
4767     (cond
4768      ((null level) nil)
4769      ((zerop level) t)
4770      ((null refs) t)
4771      ((null (gnus-parent-id refs)) t)
4772      ((and (= 1 level)
4773            (null (setq particle (gnus-id-to-article
4774                                  (gnus-parent-id refs))))
4775            (null (gnus-summary-thread-level particle)))))))
4776
4777 (defun gnus-root-id (id)
4778   "Return the id of the root of the thread where ID appears."
4779   (let (last-id prev)
4780     (while (and id (setq prev (car (gnus-id-to-thread id))))
4781       (setq last-id id
4782             id (gnus-parent-id (mail-header-references prev))))
4783     last-id))
4784
4785 (defun gnus-articles-in-thread (thread)
4786   "Return the list of articles in THREAD."
4787   (cons (mail-header-number (car thread))
4788         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4789
4790 (defun gnus-remove-thread (id &optional dont-remove)
4791   "Remove the thread that has ID in it."
4792   (let (headers thread last-id)
4793     ;; First go up in this thread until we find the root.
4794     (setq last-id (gnus-root-id id)
4795           headers (message-flatten-list (gnus-id-to-thread last-id)))
4796     ;; We have now found the real root of this thread.  It might have
4797     ;; been gathered into some loose thread, so we have to search
4798     ;; through the threads to find the thread we wanted.
4799     (let ((threads gnus-newsgroup-threads)
4800           sub)
4801       (while threads
4802         (setq sub (car threads))
4803         (if (stringp (car sub))
4804             ;; This is a gathered thread, so we look at the roots
4805             ;; below it to find whether this article is in this
4806             ;; gathered root.
4807             (progn
4808               (setq sub (cdr sub))
4809               (while sub
4810                 (when (member (caar sub) headers)
4811                   (setq thread (car threads)
4812                         threads nil
4813                         sub nil))
4814                 (setq sub (cdr sub))))
4815           ;; It's an ordinary thread, so we check it.
4816           (when (eq (car sub) (car headers))
4817             (setq thread sub
4818                   threads nil)))
4819         (setq threads (cdr threads)))
4820       ;; If this article is in no thread, then it's a root.
4821       (if thread
4822           (unless dont-remove
4823             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4824         (setq thread (gnus-id-to-thread last-id)))
4825       (when thread
4826         (prog1
4827             thread                      ; We return this thread.
4828           (unless dont-remove
4829             (if (stringp (car thread))
4830                 (progn
4831                   ;; If we use dummy roots, then we have to remove the
4832                   ;; dummy root as well.
4833                   (when (eq gnus-summary-make-false-root 'dummy)
4834                     ;; We go to the dummy root by going to
4835                     ;; the first sub-"thread", and then one line up.
4836                     (gnus-summary-goto-article
4837                      (mail-header-number (caadr thread)))
4838                     (forward-line -1)
4839                     (gnus-delete-line)
4840                     (gnus-data-compute-positions))
4841                   (setq thread (cdr thread))
4842                   (while thread
4843                     (gnus-remove-thread-1 (car thread))
4844                     (setq thread (cdr thread))))
4845               (gnus-remove-thread-1 thread))))))))
4846
4847 (defun gnus-remove-thread-1 (thread)
4848   "Remove the thread THREAD recursively."
4849   (let ((number (mail-header-number (pop thread)))
4850         d)
4851     (setq thread (reverse thread))
4852     (while thread
4853       (gnus-remove-thread-1 (pop thread)))
4854     (when (setq d (gnus-data-find number))
4855       (goto-char (gnus-data-pos d))
4856       (gnus-summary-show-thread)
4857       (gnus-data-remove
4858        number
4859        (- (point-at-bol)
4860           (prog1
4861               (1+ (point-at-eol))
4862             (gnus-delete-line)))))))
4863
4864 (defun gnus-sort-threads-recursive (threads func)
4865   (sort (mapcar (lambda (thread)
4866                   (cons (car thread)
4867                         (and (cdr thread)
4868                              (gnus-sort-threads-recursive (cdr thread) func))))
4869                 threads) func))
4870
4871 (defun gnus-sort-threads-loop (threads func)
4872   (let* ((superthread (cons nil threads))
4873          (stack (list (cons superthread threads)))
4874          remaining-threads thread)
4875     (while stack
4876       (setq remaining-threads (cdr (car stack)))
4877       (if remaining-threads
4878           (progn (setq thread (car remaining-threads))
4879                  (setcdr (car stack) (cdr remaining-threads))
4880                  (if (cdr thread)
4881                      (push (cons thread (cdr thread)) stack)))
4882         (setq thread (caar stack))
4883         (setcdr thread (sort (cdr thread) func))
4884         (pop stack)))
4885     (cdr superthread)))
4886
4887 (defun gnus-sort-threads (threads)
4888   "Sort THREADS."
4889   (if (not gnus-thread-sort-functions)
4890       threads
4891     (gnus-message 8 "Sorting threads...")
4892     (prog1
4893         (condition-case nil
4894             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4895               (gnus-sort-threads-recursive
4896                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4897           ;; Even after binding max-lisp-eval-depth, the recursive
4898           ;; sorter might fail for very long threads.  In that case,
4899           ;; try using a (less well-tested) non-recursive sorter.
4900           (error (gnus-message 9 "Sorting threads with loop...")
4901                  (gnus-sort-threads-loop
4902                   threads (gnus-make-sort-function
4903                            gnus-thread-sort-functions))))
4904       (gnus-message 8 "Sorting threads...done"))))
4905
4906 (defun gnus-sort-articles (articles)
4907   "Sort ARTICLES."
4908   (when gnus-article-sort-functions
4909     (gnus-message 7 "Sorting articles...")
4910     (prog1
4911         (setq gnus-newsgroup-headers
4912               (sort articles (gnus-make-sort-function
4913                               gnus-article-sort-functions)))
4914       (gnus-message 7 "Sorting articles...done"))))
4915
4916 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4917 (defmacro gnus-thread-header (thread)
4918   "Return header of first article in THREAD.
4919 Note that THREAD must never, ever be anything else than a variable -
4920 using some other form will lead to serious barfage."
4921   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4922   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4923   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4924         (vector thread) 2))
4925
4926 (defsubst gnus-article-sort-by-number (h1 h2)
4927   "Sort articles by article number."
4928   (< (mail-header-number h1)
4929      (mail-header-number h2)))
4930
4931 (defun gnus-thread-sort-by-number (h1 h2)
4932   "Sort threads by root article number."
4933   (gnus-article-sort-by-number
4934    (gnus-thread-header h1) (gnus-thread-header h2)))
4935
4936 (defsubst gnus-article-sort-by-random (h1 h2)
4937   "Sort articles randomly."
4938   (zerop (random 2)))
4939
4940 (defun gnus-thread-sort-by-random (h1 h2)
4941   "Sort threads randomly."
4942   (gnus-article-sort-by-random
4943    (gnus-thread-header h1) (gnus-thread-header h2)))
4944
4945 (defsubst gnus-article-sort-by-lines (h1 h2)
4946   "Sort articles by article Lines header."
4947   (< (mail-header-lines h1)
4948      (mail-header-lines h2)))
4949
4950 (defun gnus-thread-sort-by-lines (h1 h2)
4951   "Sort threads by root article Lines header."
4952   (gnus-article-sort-by-lines
4953    (gnus-thread-header h1) (gnus-thread-header h2)))
4954
4955 (defsubst gnus-article-sort-by-chars (h1 h2)
4956   "Sort articles by octet length."
4957   (< (mail-header-chars h1)
4958      (mail-header-chars h2)))
4959
4960 (defun gnus-thread-sort-by-chars (h1 h2)
4961   "Sort threads by root article octet length."
4962   (gnus-article-sort-by-chars
4963    (gnus-thread-header h1) (gnus-thread-header h2)))
4964
4965 (defsubst gnus-article-sort-by-author (h1 h2)
4966   "Sort articles by root author."
4967   (gnus-string<
4968    (let ((extract (funcall
4969                    gnus-extract-address-components
4970                    (mail-header-from h1))))
4971      (or (car extract) (cadr extract) ""))
4972    (let ((extract (funcall
4973                    gnus-extract-address-components
4974                    (mail-header-from h2))))
4975      (or (car extract) (cadr extract) ""))))
4976
4977 (defun gnus-thread-sort-by-author (h1 h2)
4978   "Sort threads by root author."
4979   (gnus-article-sort-by-author
4980    (gnus-thread-header h1)  (gnus-thread-header h2)))
4981
4982 (defsubst gnus-article-sort-by-recipient (h1 h2)
4983   "Sort articles by recipient."
4984   (gnus-string<
4985    (let ((extract (funcall
4986                    gnus-extract-address-components
4987                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4988      (or (car extract) (cadr extract)))
4989    (let ((extract (funcall
4990                    gnus-extract-address-components
4991                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4992      (or (car extract) (cadr extract)))))
4993
4994 (defun gnus-thread-sort-by-recipient (h1 h2)
4995   "Sort threads by root recipient."
4996   (gnus-article-sort-by-recipient
4997    (gnus-thread-header h1) (gnus-thread-header h2)))
4998
4999 (defsubst gnus-article-sort-by-subject (h1 h2)
5000   "Sort articles by root subject."
5001   (gnus-string<
5002    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5003    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5004
5005 (defun gnus-thread-sort-by-subject (h1 h2)
5006   "Sort threads by root subject."
5007   (gnus-article-sort-by-subject
5008    (gnus-thread-header h1) (gnus-thread-header h2)))
5009
5010 (defsubst gnus-article-sort-by-date (h1 h2)
5011   "Sort articles by root article date."
5012   (time-less-p
5013    (gnus-date-get-time (mail-header-date h1))
5014    (gnus-date-get-time (mail-header-date h2))))
5015
5016 (defun gnus-thread-sort-by-date (h1 h2)
5017   "Sort threads by root article date."
5018   (gnus-article-sort-by-date
5019    (gnus-thread-header h1) (gnus-thread-header h2)))
5020
5021 (defsubst gnus-article-sort-by-score (h1 h2)
5022   "Sort articles by root article score.
5023 Unscored articles will be counted as having a score of zero."
5024   (> (or (cdr (assq (mail-header-number h1)
5025                     gnus-newsgroup-scored))
5026          gnus-summary-default-score 0)
5027      (or (cdr (assq (mail-header-number h2)
5028                     gnus-newsgroup-scored))
5029          gnus-summary-default-score 0)))
5030
5031 (defun gnus-thread-sort-by-score (h1 h2)
5032   "Sort threads by root article score."
5033   (gnus-article-sort-by-score
5034    (gnus-thread-header h1) (gnus-thread-header h2)))
5035
5036 (defun gnus-thread-sort-by-total-score (h1 h2)
5037   "Sort threads by the sum of all scores in the thread.
5038 Unscored articles will be counted as having a score of zero."
5039   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5040
5041 (defun gnus-thread-total-score (thread)
5042   ;; This function find the total score of THREAD.
5043   (cond
5044    ((null thread)
5045     0)
5046    ((consp thread)
5047     (if (stringp (car thread))
5048         (apply gnus-thread-score-function 0
5049                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5050       (gnus-thread-total-score-1 thread)))
5051    (t
5052     (gnus-thread-total-score-1 (list thread)))))
5053
5054 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5055   "Sort articles by number."
5056   (gnus-article-sort-by-number h1 h2))
5057
5058 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5059   "Sort threads such that the thread with the most recently arrived article comes first."
5060   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5061
5062 (defun gnus-thread-highest-number (thread)
5063   "Return the highest article number in THREAD."
5064   (apply 'max (mapcar (lambda (header)
5065                         (mail-header-number header))
5066                       (message-flatten-list thread))))
5067
5068 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5069   "Sort articles by number."
5070   (gnus-article-sort-by-date h1 h2))
5071
5072 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5073   "Sort threads such that the thread with the most recently dated article comes first."
5074   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5075
5076 ; Since this is called not only to sort the top-level threads, but
5077 ; also in recursive sorts to order the articles within a thread, each
5078 ; article will be processed many times.  Thus it speeds things up
5079 ; quite a bit to use gnus-date-get-time, which caches the time value.
5080 (defun gnus-thread-latest-date (thread)
5081   "Return the highest article date in THREAD."
5082   (apply 'max
5083          (mapcar (lambda (header) (gnus-float-time
5084                                    (gnus-date-get-time
5085                                     (mail-header-date header))))
5086                  (message-flatten-list thread))))
5087
5088 (defun gnus-thread-total-score-1 (root)
5089   ;; This function find the total score of the thread below ROOT.
5090   (setq root (car root))
5091   (apply gnus-thread-score-function
5092          (or (append
5093               (mapcar 'gnus-thread-total-score
5094                       (cdr (gnus-id-to-thread (mail-header-id root))))
5095               (when (> (mail-header-number root) 0)
5096                 (list (or (cdr (assq (mail-header-number root)
5097                                      gnus-newsgroup-scored))
5098                           gnus-summary-default-score 0))))
5099              (list gnus-summary-default-score)
5100              '(0))))
5101
5102 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5103 (defvar gnus-tmp-prev-subject nil)
5104 (defvar gnus-tmp-false-parent nil)
5105 (defvar gnus-tmp-root-expunged nil)
5106 (defvar gnus-tmp-dummy-line nil)
5107
5108 (defun gnus-extra-header (type &optional header)
5109   "Return the extra header of TYPE."
5110   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5111       ""))
5112
5113 (defvar gnus-tmp-thread-tree-header-string "")
5114
5115 (defcustom gnus-sum-thread-tree-root "> "
5116   "With %B spec, used for the root of a thread.
5117 If nil, use subject instead."
5118   :version "22.1"
5119   :type '(radio (const :format "%v  " nil) string)
5120   :group 'gnus-thread)
5121
5122 (defcustom gnus-sum-thread-tree-false-root "> "
5123   "With %B spec, used for a false root of a thread.
5124 If nil, use subject instead."
5125   :version "22.1"
5126   :type '(radio (const :format "%v  " nil) string)
5127   :group 'gnus-thread)
5128
5129 (defcustom gnus-sum-thread-tree-single-indent ""
5130   "With %B spec, used for a thread with just one message.
5131 If nil, use subject instead."
5132   :version "22.1"
5133   :type '(radio (const :format "%v  " nil) string)
5134   :group 'gnus-thread)
5135
5136 (defcustom gnus-sum-thread-tree-vertical "| "
5137   "With %B spec, used for drawing a vertical line."
5138   :version "22.1"
5139   :type 'string
5140   :group 'gnus-thread)
5141
5142 (defcustom gnus-sum-thread-tree-indent "  "
5143   "With %B spec, used for indenting."
5144   :version "22.1"
5145   :type 'string
5146   :group 'gnus-thread)
5147
5148 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5149   "With %B spec, used for a leaf with brothers."
5150   :version "22.1"
5151   :type 'string
5152   :group 'gnus-thread)
5153
5154 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5155   "With %B spec, used for a leaf without brothers."
5156   :version "22.1"
5157   :type 'string
5158   :group 'gnus-thread)
5159
5160 (defcustom gnus-summary-display-while-building nil
5161   "If non-nil, show and update the summary buffer as it's being built.
5162 If the value is t, update the buffer after every line is inserted.  If
5163 the value is an integer (N), update the display every N lines."
5164   :version "22.1"
5165   :group 'gnus-thread
5166   :type '(choice (const :tag "off" nil)
5167                  number
5168                  (const :tag "frequently" t)))
5169
5170 (defun gnus-summary-prepare-threads (threads)
5171   "Prepare summary buffer from THREADS and indentation LEVEL.
5172 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5173 or a straight list of headers."
5174   (gnus-message 7 "Generating summary...")
5175
5176   (setq gnus-newsgroup-threads threads)
5177   (beginning-of-line)
5178
5179   (let ((gnus-tmp-level 0)
5180         (default-score (or gnus-summary-default-score 0))
5181         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5182         (building-line-count gnus-summary-display-while-building)
5183         (building-count (integerp gnus-summary-display-while-building))
5184         thread number subject stack state gnus-tmp-gathered beg-match
5185         new-roots gnus-tmp-new-adopts thread-end simp-subject
5186         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5187         gnus-tmp-replied gnus-tmp-subject-or-nil
5188         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5189         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5190         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5191         tree-stack)
5192
5193     (setq gnus-tmp-prev-subject nil
5194           gnus-tmp-thread-tree-header-string "")
5195
5196     (if (vectorp (car threads))
5197         ;; If this is a straight (sic) list of headers, then a
5198         ;; threaded summary display isn't required, so we just create
5199         ;; an unthreaded one.
5200         (gnus-summary-prepare-unthreaded threads)
5201
5202       ;; Do the threaded display.
5203
5204       (if gnus-summary-display-while-building
5205           (switch-to-buffer (buffer-name)))
5206       (while (or threads stack gnus-tmp-new-adopts new-roots)
5207
5208         (if (and (= gnus-tmp-level 0)
5209                  (or (not stack)
5210                      (= (caar stack) 0))
5211                  (not gnus-tmp-false-parent)
5212                  (or gnus-tmp-new-adopts new-roots))
5213             (if gnus-tmp-new-adopts
5214                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5215                       thread (list (car gnus-tmp-new-adopts))
5216                       gnus-tmp-header (caar thread)
5217                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5218               (when new-roots
5219                 (setq thread (list (car new-roots))
5220                       gnus-tmp-header (caar thread)
5221                       new-roots (cdr new-roots))))
5222
5223           (if threads
5224               ;; If there are some threads, we do them before the
5225               ;; threads on the stack.
5226               (setq thread threads
5227                     gnus-tmp-header (caar thread))
5228             ;; There were no current threads, so we pop something off
5229             ;; the stack.
5230             (setq state (car stack)
5231                   gnus-tmp-level (car state)
5232                   tree-stack (cadr state)
5233                   thread (caddr state)
5234                   stack (cdr stack)
5235                   gnus-tmp-header (caar thread))))
5236
5237         (setq gnus-tmp-false-parent nil)
5238         (setq gnus-tmp-root-expunged nil)
5239         (setq thread-end nil)
5240
5241         (if (stringp gnus-tmp-header)
5242             ;; The header is a dummy root.
5243             (cond
5244              ((eq gnus-summary-make-false-root 'adopt)
5245               ;; We let the first article adopt the rest.
5246               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5247                                                (cddar thread)))
5248               (setq gnus-tmp-gathered
5249                     (nconc (mapcar
5250                             (lambda (h) (mail-header-number (car h)))
5251                             (cddar thread))
5252                            gnus-tmp-gathered))
5253               (setq thread (cons (list (caar thread)
5254                                        (cadar thread))
5255                                  (cdr thread)))
5256               (setq gnus-tmp-level -1
5257                     gnus-tmp-false-parent t))
5258              ((eq gnus-summary-make-false-root 'empty)
5259               ;; We print adopted articles with empty subject fields.
5260               (setq gnus-tmp-gathered
5261                     (nconc (mapcar
5262                             (lambda (h) (mail-header-number (car h)))
5263                             (cddar thread))
5264                            gnus-tmp-gathered))
5265               (setq gnus-tmp-level -1))
5266              ((eq gnus-summary-make-false-root 'dummy)
5267               ;; We remember that we probably want to output a dummy
5268               ;; root.
5269               (setq gnus-tmp-dummy-line gnus-tmp-header)
5270               (setq gnus-tmp-prev-subject gnus-tmp-header))
5271              (t
5272               ;; We do not make a root for the gathered
5273               ;; sub-threads at all.
5274               (setq gnus-tmp-level -1)))
5275
5276           (setq number (mail-header-number gnus-tmp-header)
5277                 subject (mail-header-subject gnus-tmp-header)
5278                 simp-subject (gnus-simplify-subject-fully subject))
5279
5280           (cond
5281            ;; If the thread has changed subject, we might want to make
5282            ;; this subthread into a root.
5283            ((and (null gnus-thread-ignore-subject)
5284                  (not (zerop gnus-tmp-level))
5285                  gnus-tmp-prev-subject
5286                  (not (string= gnus-tmp-prev-subject simp-subject)))
5287             (setq new-roots (nconc new-roots (list (car thread)))
5288                   thread-end t
5289                   gnus-tmp-header nil))
5290            ;; If the article lies outside the current limit,
5291            ;; then we do not display it.
5292            ((not (memq number gnus-newsgroup-limit))
5293             (setq gnus-tmp-gathered
5294                   (nconc (mapcar
5295                           (lambda (h) (mail-header-number (car h)))
5296                           (cdar thread))
5297                          gnus-tmp-gathered))
5298             (setq gnus-tmp-new-adopts (if (cdar thread)
5299                                           (append gnus-tmp-new-adopts
5300                                                   (cdar thread))
5301                                         gnus-tmp-new-adopts)
5302                   thread-end t
5303                   gnus-tmp-header nil)
5304             (when (zerop gnus-tmp-level)
5305               (setq gnus-tmp-root-expunged t)))
5306            ;; Perhaps this article is to be marked as read?
5307            ((and gnus-summary-mark-below
5308                  (< (or (cdr (assq number gnus-newsgroup-scored))
5309                         default-score)
5310                     gnus-summary-mark-below)
5311                  ;; Don't touch sparse articles.
5312                  (not (gnus-summary-article-sparse-p number))
5313                  (not (gnus-summary-article-ancient-p number)))
5314             (setq gnus-newsgroup-unreads
5315                   (delq number gnus-newsgroup-unreads))
5316             (if gnus-newsgroup-auto-expire
5317                 (setq gnus-newsgroup-expirable
5318                       (gnus-add-to-sorted-list
5319                        gnus-newsgroup-expirable number))
5320               (push (cons number gnus-low-score-mark)
5321                     gnus-newsgroup-reads))))
5322
5323           (when gnus-tmp-header
5324             ;; We may have an old dummy line to output before this
5325             ;; article.
5326             (when (and gnus-tmp-dummy-line
5327                        (gnus-subject-equal
5328                         gnus-tmp-dummy-line
5329                         (mail-header-subject gnus-tmp-header)))
5330               (gnus-summary-insert-dummy-line
5331                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5332               (setq gnus-tmp-dummy-line nil))
5333
5334             ;; Compute the mark.
5335             (setq gnus-tmp-unread (gnus-article-mark number))
5336
5337             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5338                                   gnus-tmp-header gnus-tmp-level)
5339                   gnus-newsgroup-data)
5340
5341             ;; Actually insert the line.
5342             (setq
5343              gnus-tmp-subject-or-nil
5344              (cond
5345               ((and gnus-thread-ignore-subject
5346                     gnus-tmp-prev-subject
5347                     (not (string= gnus-tmp-prev-subject simp-subject)))
5348                subject)
5349               ((zerop gnus-tmp-level)
5350                (if (and (eq gnus-summary-make-false-root 'empty)
5351                         (memq number gnus-tmp-gathered)
5352                         gnus-tmp-prev-subject
5353                         (string= gnus-tmp-prev-subject simp-subject))
5354                    gnus-summary-same-subject
5355                  subject))
5356               (t gnus-summary-same-subject)))
5357             (if (and (eq gnus-summary-make-false-root 'adopt)
5358                      (= gnus-tmp-level 1)
5359                      (memq number gnus-tmp-gathered))
5360                 (setq gnus-tmp-opening-bracket ?\<
5361                       gnus-tmp-closing-bracket ?\>)
5362               (setq gnus-tmp-opening-bracket ?\[
5363                     gnus-tmp-closing-bracket ?\]))
5364             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5365                 (gnus-make-thread-indent-array
5366                  (max (* 2 (length gnus-thread-indent-array))
5367                       gnus-tmp-level)))
5368             (setq
5369              gnus-tmp-indentation
5370              (aref gnus-thread-indent-array gnus-tmp-level)
5371              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5372              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5373                                 gnus-summary-default-score 0)
5374              gnus-tmp-score-char
5375              (if (or (null gnus-summary-default-score)
5376                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5377                          gnus-summary-zcore-fuzz))
5378                  ?                      ;Whitespace
5379                (if (< gnus-tmp-score gnus-summary-default-score)
5380                    gnus-score-below-mark gnus-score-over-mark))
5381              gnus-tmp-replied
5382              (cond ((memq number gnus-newsgroup-processable)
5383                     gnus-process-mark)
5384                    ((memq number gnus-newsgroup-cached)
5385                     gnus-cached-mark)
5386                    ((memq number gnus-newsgroup-replied)
5387                     gnus-replied-mark)
5388                    ((memq number gnus-newsgroup-forwarded)
5389                     gnus-forwarded-mark)
5390                    ((memq number gnus-newsgroup-saved)
5391                     gnus-saved-mark)
5392                    ((memq number gnus-newsgroup-unseen)
5393                     gnus-unseen-mark)
5394                    (t gnus-no-mark))
5395              gnus-tmp-downloaded
5396              (cond ((memq number gnus-newsgroup-undownloaded)
5397                     gnus-undownloaded-mark)
5398                    (gnus-newsgroup-agentized
5399                     gnus-downloaded-mark)
5400                    (t
5401                     gnus-no-mark))
5402              gnus-tmp-from (mail-header-from gnus-tmp-header)
5403              gnus-tmp-name
5404              (cond
5405               ((string-match "<[^>]+> *$" gnus-tmp-from)
5406                (setq beg-match (match-beginning 0))
5407                (or (and (string-match "^\".+\"" gnus-tmp-from)
5408                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5409                    (substring gnus-tmp-from 0 beg-match)))
5410               ((string-match "(.+)" gnus-tmp-from)
5411                (substring gnus-tmp-from
5412                           (1+ (match-beginning 0)) (1- (match-end 0))))
5413               (t gnus-tmp-from))
5414
5415              ;; Do the %B string
5416              gnus-tmp-thread-tree-header-string
5417              (cond
5418               ((not gnus-show-threads) "")
5419               ((zerop gnus-tmp-level)
5420                (cond ((cdar thread)
5421                       (or gnus-sum-thread-tree-root subject))
5422                      (gnus-tmp-new-adopts
5423                       (or gnus-sum-thread-tree-false-root subject))
5424                      (t
5425                       (or gnus-sum-thread-tree-single-indent subject))))
5426               (t
5427                (concat (apply 'concat
5428                               (mapcar (lambda (item)
5429                                         (if (= item 1)
5430                                             gnus-sum-thread-tree-vertical
5431                                           gnus-sum-thread-tree-indent))
5432                                       (cdr (reverse tree-stack))))
5433                        (if (nth 1 thread)
5434                            gnus-sum-thread-tree-leaf-with-other
5435                          gnus-sum-thread-tree-single-leaf)))))
5436             (when (string= gnus-tmp-name "")
5437               (setq gnus-tmp-name gnus-tmp-from))
5438             (unless (numberp gnus-tmp-lines)
5439               (setq gnus-tmp-lines -1))
5440             (if (= gnus-tmp-lines -1)
5441                 (setq gnus-tmp-lines "?")
5442               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5443             (gnus-put-text-property
5444              (point)
5445              (progn (eval gnus-summary-line-format-spec) (point))
5446              'gnus-number number)
5447             (when gnus-visual-p
5448               (forward-line -1)
5449               (gnus-summary-highlight-line)
5450               (when gnus-summary-update-hook
5451                 (gnus-run-hooks 'gnus-summary-update-hook))
5452               (forward-line 1))
5453
5454             (setq gnus-tmp-prev-subject simp-subject)))
5455
5456         (when (nth 1 thread)
5457           (push (list (max 0 gnus-tmp-level)
5458                       (copy-sequence tree-stack)
5459                       (nthcdr 1 thread))
5460                 stack))
5461         (push (if (nth 1 thread) 1 0) tree-stack)
5462         (incf gnus-tmp-level)
5463         (setq threads (if thread-end nil (cdar thread)))
5464         (if gnus-summary-display-while-building
5465             (if building-count
5466                 (progn
5467                   ;; use a set frequency
5468                   (setq building-line-count (1- building-line-count))
5469                   (when (= building-line-count 0)
5470                     (sit-for 0)
5471                     (setq building-line-count
5472                           gnus-summary-display-while-building)))
5473               ;; always
5474               (sit-for 0)))
5475         (unless threads
5476           (setq gnus-tmp-level 0)))))
5477   (gnus-message 7 "Generating summary...done"))
5478
5479 (defun gnus-summary-prepare-unthreaded (headers)
5480   "Generate an unthreaded summary buffer based on HEADERS."
5481   (let (header number mark)
5482
5483     (beginning-of-line)
5484
5485     (while headers
5486       ;; We may have to root out some bad articles...
5487       (when (memq (setq number (mail-header-number
5488                                 (setq header (pop headers))))
5489                   gnus-newsgroup-limit)
5490         ;; Mark article as read when it has a low score.
5491         (when (and gnus-summary-mark-below
5492                    (< (or (cdr (assq number gnus-newsgroup-scored))
5493                           gnus-summary-default-score 0)
5494                       gnus-summary-mark-below)
5495                    (not (gnus-summary-article-ancient-p number)))
5496           (setq gnus-newsgroup-unreads
5497                 (delq number gnus-newsgroup-unreads))
5498           (if gnus-newsgroup-auto-expire
5499               (push number gnus-newsgroup-expirable)
5500             (push (cons number gnus-low-score-mark)
5501                   gnus-newsgroup-reads)))
5502
5503         (setq mark (gnus-article-mark number))
5504         (push (gnus-data-make number mark (1+ (point)) header 0)
5505               gnus-newsgroup-data)
5506         (gnus-summary-insert-line
5507          header 0 number
5508          (memq number gnus-newsgroup-undownloaded)
5509          mark (memq number gnus-newsgroup-replied)
5510          (memq number gnus-newsgroup-expirable)
5511          (mail-header-subject header) nil
5512          (cdr (assq number gnus-newsgroup-scored))
5513          (memq number gnus-newsgroup-processable))))))
5514
5515 (defun gnus-group-get-list-identifiers (group)
5516   "Get list identifier regexp for GROUP."
5517   (or (gnus-parameter-list-identifier group)
5518       (if (consp gnus-list-identifiers)
5519           (mapconcat 'identity gnus-list-identifiers " *\\|")
5520         gnus-list-identifiers)))
5521
5522 (defun gnus-summary-remove-list-identifiers ()
5523   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5524   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5525         changed subject)
5526     (when regexp
5527       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5528       (dolist (header gnus-newsgroup-headers)
5529         (setq subject (mail-header-subject header)
5530               changed nil)
5531         (while (string-match regexp subject)
5532           (setq subject
5533                 (concat (substring subject 0 (match-beginning 1))
5534                         (substring subject (match-end 0)))
5535                 changed t))
5536         (when changed
5537           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5538             (setq subject
5539                   (concat (substring subject 0 (match-beginning 1))
5540                           (substring subject (match-end 1)))))
5541           (mail-header-set-subject header subject))))))
5542
5543 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5544   "Fetch headers of ARTICLES."
5545   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5546     (gnus-message 5 "Fetching headers for %s..." name)
5547     (prog1
5548         (if (eq 'nov
5549                 (setq gnus-headers-retrieved-by
5550                       (gnus-retrieve-headers
5551                        articles gnus-newsgroup-name
5552                        (or limit
5553                            ;; We might want to fetch old headers, but
5554                            ;; not if there is only 1 article.
5555                            (and (or (and
5556                                      (not (eq gnus-fetch-old-headers 'some))
5557                                      (not (numberp gnus-fetch-old-headers)))
5558                                     (> (length articles) 1))
5559                                 gnus-fetch-old-headers)))))
5560             (gnus-get-newsgroup-headers-xover
5561              articles force-new dependencies gnus-newsgroup-name t)
5562           (gnus-get-newsgroup-headers dependencies force-new))
5563       (gnus-message 5 "Fetching headers for %s...done" name))))
5564
5565 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5566   "Select newsgroup GROUP.
5567 If READ-ALL is non-nil, all articles in the group are selected.
5568 If SELECT-ARTICLES, only select those articles from GROUP."
5569   (let* ((entry (gnus-group-entry group))
5570          ;;!!! Dirty hack; should be removed.
5571          (gnus-summary-ignore-duplicates
5572           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5573               t
5574             gnus-summary-ignore-duplicates))
5575          (info (nth 2 entry))
5576          charset articles fetched-articles cached)
5577
5578     (unless (gnus-check-server
5579              (set (make-local-variable 'gnus-current-select-method)
5580                   (gnus-find-method-for-group group)))
5581       (error "Couldn't open server"))
5582     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5583
5584     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5585         (gnus-activate-group group)     ; Or we can activate it...
5586         (progn                          ; Or we bug out.
5587           (when (equal major-mode 'gnus-summary-mode)
5588             (gnus-kill-buffer (current-buffer)))
5589           (error
5590            "Couldn't activate group %s: %s"
5591            (mm-decode-coding-string group charset)
5592            (mm-decode-coding-string (gnus-status-message group) charset))))
5593
5594     (unless (gnus-request-group group t)
5595       (when (equal major-mode 'gnus-summary-mode)
5596         (gnus-kill-buffer (current-buffer)))
5597       (error "Couldn't request group %s: %s"
5598              (mm-decode-coding-string group charset)
5599              (mm-decode-coding-string (gnus-status-message group) charset)))
5600
5601     (when (and gnus-agent
5602                (gnus-active group))
5603       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5604
5605       (setq gnus-summary-use-undownloaded-faces
5606             (gnus-agent-find-parameter
5607              group
5608              'agent-enable-undownloaded-faces)))
5609
5610     (setq gnus-newsgroup-name group
5611           gnus-newsgroup-unselected nil
5612           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5613
5614     (let ((display (gnus-group-find-parameter group 'display)))
5615       (setq gnus-newsgroup-display
5616             (cond
5617              ((not (zerop (or (car-safe read-all) 0)))
5618               ;; The user entered the group with C-u SPC/RET, let's show
5619               ;; all articles.
5620               'gnus-not-ignore)
5621              ((eq display 'all)
5622               'gnus-not-ignore)
5623              ((arrayp display)
5624               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5625              ((numberp display)
5626               ;; The following is probably the "correct" solution, but
5627               ;; it makes Gnus fetch all headers and then limit the
5628               ;; articles (which is slow), so instead we hack the
5629               ;; select-articles parameter instead. -- Simon Josefsson
5630               ;; <jas@kth.se>
5631               ;;
5632               ;; (gnus-byte-compile
5633               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5634               ;;                         display)))))
5635               (setq select-articles
5636                     (gnus-uncompress-range
5637                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5638                              (if (> tmp 0)
5639                                  tmp
5640                                1))
5641                            (cdr (gnus-active group)))))
5642               nil)
5643              (t
5644               nil))))
5645
5646     (gnus-summary-setup-default-charset)
5647
5648     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5649     (when (gnus-virtual-group-p group)
5650       (setq cached gnus-newsgroup-cached))
5651
5652     (setq gnus-newsgroup-unreads
5653           (gnus-sorted-ndifference
5654            (gnus-sorted-ndifference gnus-newsgroup-unreads
5655                                     gnus-newsgroup-marked)
5656            gnus-newsgroup-dormant))
5657
5658     (setq gnus-newsgroup-processable nil)
5659
5660     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5661
5662     ;; Adjust and set lists of article marks.
5663     (when info
5664       (gnus-adjust-marked-articles info))
5665     (if (setq articles select-articles)
5666         (setq gnus-newsgroup-unselected
5667               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5668       (setq articles (gnus-articles-to-read group read-all)))
5669
5670     (cond
5671      ((null articles)
5672       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5673       'quit)
5674      ((eq articles 0) nil)
5675      (t
5676       ;; Init the dependencies hash table.
5677       (setq gnus-newsgroup-dependencies
5678             (gnus-make-hashtable (length articles)))
5679       (gnus-set-global-variables)
5680       ;; Retrieve the headers and read them in.
5681
5682       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5683
5684       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5685       (when cached
5686         (setq gnus-newsgroup-cached cached))
5687
5688       ;; Suppress duplicates?
5689       (when gnus-suppress-duplicates
5690         (gnus-dup-suppress-articles))
5691
5692       ;; Set the initial limit.
5693       (setq gnus-newsgroup-limit (copy-sequence articles))
5694       ;; Remove canceled articles from the list of unread articles.
5695       (setq fetched-articles
5696             (mapcar (lambda (headers) (mail-header-number headers))
5697                     gnus-newsgroup-headers))
5698       (setq gnus-newsgroup-articles fetched-articles)
5699       (setq gnus-newsgroup-unreads
5700             (gnus-sorted-nintersection
5701              gnus-newsgroup-unreads fetched-articles))
5702       (gnus-compute-unseen-list)
5703
5704       ;; Removed marked articles that do not exist.
5705       (gnus-update-missing-marks
5706        (gnus-sorted-difference articles fetched-articles))
5707       ;; We might want to build some more threads first.
5708       (when (and gnus-fetch-old-headers
5709                  (eq gnus-headers-retrieved-by 'nov))
5710         (if (eq gnus-fetch-old-headers 'invisible)
5711             (gnus-build-all-threads)
5712           (gnus-build-old-threads)))
5713       ;; Let the Gnus agent mark articles as read.
5714       (when gnus-agent
5715         (gnus-agent-get-undownloaded-list))
5716       ;; Remove list identifiers from subject
5717       (gnus-summary-remove-list-identifiers)
5718       ;; Check whether auto-expire is to be done in this group.
5719       (setq gnus-newsgroup-auto-expire
5720             (and (gnus-group-auto-expirable-p group)
5721                  (not (gnus-group-read-only-p group))))
5722       ;; Set up the article buffer now, if necessary.
5723       (unless (and gnus-single-article-buffer
5724                    (equal gnus-article-buffer "*Article*"))
5725         (gnus-article-setup-buffer))
5726       ;; First and last article in this newsgroup.
5727       (when gnus-newsgroup-headers
5728         (setq gnus-newsgroup-begin
5729               (mail-header-number (car gnus-newsgroup-headers))
5730               gnus-newsgroup-end
5731               (mail-header-number
5732                (gnus-last-element gnus-newsgroup-headers))))
5733       ;; GROUP is successfully selected.
5734       (or gnus-newsgroup-headers t)))))
5735
5736 (defun gnus-compute-unseen-list ()
5737   ;; The `seen' marks are treated specially.
5738   (if (not gnus-newsgroup-seen)
5739       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5740     (setq gnus-newsgroup-unseen
5741           (gnus-inverse-list-range-intersection
5742            gnus-newsgroup-articles gnus-newsgroup-seen))))
5743
5744 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5745
5746 (defun gnus-summary-display-make-predicate (display)
5747   (require 'gnus-agent)
5748   (when (= (length display) 1)
5749     (setq display (car display)))
5750   (unless gnus-summary-display-cache
5751     (dolist (elem (append '((unread . unread)
5752                             (read . read)
5753                             (unseen . unseen))
5754                           gnus-article-mark-lists))
5755       (push (cons (cdr elem)
5756                   (gnus-byte-compile    ;Why bother?
5757                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5758             gnus-summary-display-cache)))
5759   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5760         (gnus-category-predicate-cache gnus-summary-display-cache))
5761     (gnus-get-predicate display)))
5762
5763 ;; Uses the dynamically bound `gnus-number' variable.
5764 (defvar gnus-number)
5765 (defun gnus-article-marked-p (type &optional article)
5766   (let ((article (or article gnus-number)))
5767     (cond
5768      ((eq type 'tick)
5769       (memq article gnus-newsgroup-marked))
5770      ((eq type 'spam)
5771       (memq article gnus-newsgroup-spam-marked))
5772      ((eq type 'unsend)
5773       (memq article gnus-newsgroup-unsendable))
5774      ((eq type 'undownload)
5775       (memq article gnus-newsgroup-undownloaded))
5776      ((eq type 'download)
5777       (memq article gnus-newsgroup-downloadable))
5778      ((eq type 'unread)
5779       (memq article gnus-newsgroup-unreads))
5780      ((eq type 'read)
5781       (memq article gnus-newsgroup-reads))
5782      ((eq type 'dormant)
5783       (memq article gnus-newsgroup-dormant) )
5784      ((eq type 'expire)
5785       (memq article gnus-newsgroup-expirable))
5786      ((eq type 'reply)
5787       (memq article gnus-newsgroup-replied))
5788      ((eq type 'killed)
5789       (memq article gnus-newsgroup-killed))
5790      ((eq type 'bookmark)
5791       (assq article gnus-newsgroup-bookmarks))
5792      ((eq type 'score)
5793       (assq article gnus-newsgroup-scored))
5794      ((eq type 'save)
5795       (memq article gnus-newsgroup-saved))
5796      ((eq type 'cache)
5797       (memq article gnus-newsgroup-cached))
5798      ((eq type 'forward)
5799       (memq article gnus-newsgroup-forwarded))
5800      ((eq type 'seen)
5801       (not (memq article gnus-newsgroup-unseen)))
5802      (t t))))
5803
5804 (defun gnus-articles-to-read (group &optional read-all)
5805   "Find out what articles the user wants to read."
5806   (let* ((only-read-p t)
5807          (articles
5808           ;; Select all articles if `read-all' is non-nil, or if there
5809           ;; are no unread articles.
5810           (if (or read-all
5811                   (and (zerop (length gnus-newsgroup-marked))
5812                        (zerop (length gnus-newsgroup-unreads)))
5813                   ;; Fetch all if the predicate is non-nil.
5814                   gnus-newsgroup-display)
5815               ;; We want to select the headers for all the articles in
5816               ;; the group, so we select either all the active
5817               ;; articles in the group, or (if that's nil), the
5818               ;; articles in the cache.
5819               (or
5820                (if gnus-newsgroup-maximum-articles
5821                    (let ((active (gnus-active group)))
5822                      (gnus-uncompress-range
5823                       (cons (max (car active)
5824                                  (- (cdr active)
5825                                     gnus-newsgroup-maximum-articles
5826                                     -1))
5827                             (cdr active))))
5828                  (gnus-uncompress-range (gnus-active group)))
5829                (gnus-cache-articles-in-group group))
5830             ;; Select only the "normal" subset of articles.
5831             (setq only-read-p nil)
5832             (gnus-sorted-nunion
5833              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5834              gnus-newsgroup-unreads)))
5835          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5836          (scored (length scored-list))
5837          (number (length articles))
5838          (marked (+ (length gnus-newsgroup-marked)
5839                     (length gnus-newsgroup-dormant)))
5840          (select
5841           (cond
5842            ((numberp read-all)
5843             read-all)
5844            ((numberp gnus-newsgroup-display)
5845             gnus-newsgroup-display)
5846            (t
5847             (condition-case ()
5848                 (cond
5849                  ((and (or (<= scored marked) (= scored number))
5850                        (numberp gnus-large-newsgroup)
5851                        (> number gnus-large-newsgroup))
5852                   (let* ((cursor-in-echo-area nil)
5853                          (initial (gnus-parameter-large-newsgroup-initial
5854                                    gnus-newsgroup-name))
5855                          (default (if only-read-p
5856                                       (or initial gnus-large-newsgroup)
5857                                     number))
5858                          (input
5859                           (read-string
5860                            (if only-read-p
5861                                (format
5862                               "How many articles from %s (available %d, default %d): "
5863                               (gnus-group-decoded-name
5864                                (gnus-group-real-name gnus-newsgroup-name))
5865                               number default)
5866                              (format
5867                                 "How many articles from %s (%d available): "
5868                                 (gnus-group-decoded-name
5869                                  (gnus-group-real-name gnus-newsgroup-name))
5870                                 default))
5871                            nil
5872                            nil
5873                            (number-to-string default))))
5874                     (if (string-match "^[ \t]*$" input) number input)))
5875                  ((and (> scored marked) (< scored number)
5876                        (> (- scored number) 20))
5877                   (let ((input
5878                          (read-string
5879                           (format "%s %s (%d scored, %d total): "
5880                                   "How many articles from"
5881                                   (gnus-group-decoded-name
5882                                    (gnus-group-real-name gnus-newsgroup-name))
5883                                   scored number))))
5884                     (if (string-match "^[ \t]*$" input)
5885                         number input)))
5886                  (t number))
5887               (quit
5888                (message "Quit getting the articles to read")
5889                nil))))))
5890     (setq select (if (stringp select) (string-to-number select) select))
5891     (if (or (null select) (zerop select))
5892         select
5893       (if (and (not (zerop scored)) (<= (abs select) scored))
5894           (progn
5895             (setq articles (sort scored-list '<))
5896             (setq number (length articles)))
5897         (setq articles (copy-sequence articles)))
5898
5899       (when (< (abs select) number)
5900         (if (< select 0)
5901             ;; Select the N oldest articles.
5902             (setcdr (nthcdr (1- (abs select)) articles) nil)
5903           ;; Select the N most recent articles.
5904           (setq articles (nthcdr (- number select) articles))))
5905       (setq gnus-newsgroup-unselected
5906             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5907       (when gnus-alter-articles-to-read-function
5908         (setq articles
5909               (sort
5910                (funcall gnus-alter-articles-to-read-function
5911                         gnus-newsgroup-name articles)
5912                '<)))
5913       articles)))
5914
5915 (defun gnus-killed-articles (killed articles)
5916   (let (out)
5917     (while articles
5918       (when (inline (gnus-member-of-range (car articles) killed))
5919         (push (car articles) out))
5920       (setq articles (cdr articles)))
5921     out))
5922
5923 (defun gnus-uncompress-marks (marks)
5924   "Uncompress the mark ranges in MARKS."
5925   (let ((uncompressed '(score bookmark))
5926         out)
5927     (while marks
5928       (if (memq (caar marks) uncompressed)
5929           (push (car marks) out)
5930         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5931       (setq marks (cdr marks)))
5932     out))
5933
5934 (defun gnus-article-mark-to-type (mark)
5935   "Return the type of MARK."
5936   (or (cadr (assq mark gnus-article-special-mark-lists))
5937       'list))
5938
5939 (defun gnus-article-unpropagatable-p (mark)
5940   "Return whether MARK should be propagated to back end."
5941   (memq mark gnus-article-unpropagated-mark-lists))
5942
5943 (defun gnus-adjust-marked-articles (info)
5944   "Set all article lists and remove all marks that are no longer valid."
5945   (let* ((marked-lists (gnus-info-marks info))
5946          (active (gnus-active (gnus-info-group info)))
5947          (min (car active))
5948          (max (cdr active))
5949          (types gnus-article-mark-lists)
5950          marks var articles article mark mark-type
5951          bgn end)
5952     ;; Hack to avoid adjusting marks for imap.
5953     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5954               'nnimap)
5955       (setq min 1))
5956
5957     (dolist (marks marked-lists)
5958       (setq mark (car marks)
5959             mark-type (gnus-article-mark-to-type mark)
5960             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5961
5962       ;; We set the variable according to the type of the marks list,
5963       ;; and then adjust the marks to a subset of the active articles.
5964       (cond
5965        ;; Adjust "simple" lists - compressed yet unsorted
5966        ((eq mark-type 'list)
5967         ;; Simultaneously uncompress and clip to active range
5968         ;; See gnus-uncompress-range for a description of possible marks
5969         (let (l lh)
5970           (if (not (cadr marks))
5971               (set var nil)
5972             (setq articles (if (numberp (cddr marks))
5973                                (list (cdr marks))
5974                              (cdr marks))
5975                   lh (cons nil nil)
5976                   l lh)
5977
5978             (while (setq article (pop articles))
5979               (cond ((consp article)
5980                      (setq bgn (max (car article) min)
5981                            end (min (cdr article) max))
5982                      (while (<= bgn end)
5983                        (setq l (setcdr l (cons bgn nil))
5984                              bgn (1+ bgn))))
5985                     ((and (<= min article)
5986                           (>= max article))
5987                      (setq l (setcdr l (cons article nil))))))
5988             (set var (cdr lh)))))
5989        ;; Adjust assocs.
5990        ((eq mark-type 'tuple)
5991         (set var (setq articles (cdr marks)))
5992         (when (not (listp (cdr (symbol-value var))))
5993           (set var (list (symbol-value var))))
5994         (when (not (listp (cdr articles)))
5995           (setq articles (list articles)))
5996         (while articles
5997           (when (or (not (consp (setq article (pop articles))))
5998                     (< (car article) min)
5999                     (> (car article) max))
6000             (set var (delq article (symbol-value var))))))
6001        ;; Adjust ranges (sloppily).
6002        ((eq mark-type 'range)
6003         (cond
6004          ((eq mark 'seen)
6005           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6006           ;; It should be (seen (NUM1 . NUM2)).
6007           (when (numberp (cddr marks))
6008             (setcdr marks (list (cdr marks))))
6009           (setq articles (cdr marks))
6010           (while (and articles
6011                       (or (and (consp (car articles))
6012                                (> min (cdar articles)))
6013                           (and (numberp (car articles))
6014                                (> min (car articles)))))
6015             (pop articles))
6016           (set var articles))))))))
6017
6018 (defun gnus-update-missing-marks (missing)
6019   "Go through the list of MISSING articles and remove them from the mark lists."
6020   (when missing
6021     (let (var m)
6022       ;; Go through all types.
6023       (dolist (elem gnus-article-mark-lists)
6024         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6025           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6026           (when (symbol-value var)
6027             ;; This list has articles.  So we delete all missing
6028             ;; articles from it.
6029             (setq m missing)
6030             (while m
6031               (set var (delq (pop m) (symbol-value var))))))))))
6032
6033 (defun gnus-update-marks ()
6034   "Enter the various lists of marked articles into the newsgroup info list."
6035   (let ((types gnus-article-mark-lists)
6036         (info (gnus-get-info gnus-newsgroup-name))
6037         type list newmarked symbol delta-marks)
6038     (when info
6039       ;; Add all marks lists to the list of marks lists.
6040       (while (setq type (pop types))
6041         (setq list (symbol-value
6042                     (setq symbol
6043                           (intern (format "gnus-newsgroup-%s" (car type))))))
6044
6045         (when list
6046           ;; Get rid of the entries of the articles that have the
6047           ;; default score.
6048           (when (and (eq (cdr type) 'score)
6049                      gnus-save-score
6050                      list)
6051             (let* ((arts list)
6052                    (prev (cons nil list))
6053                    (all prev))
6054               (while arts
6055                 (if (or (not (consp (car arts)))
6056                         (= (cdar arts) gnus-summary-default-score))
6057                     (setcdr prev (cdr arts))
6058                   (setq prev arts))
6059                 (setq arts (cdr arts)))
6060               (setq list (cdr all)))))
6061
6062         (when (eq (cdr type) 'seen)
6063           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6064
6065         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6066           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6067
6068         (when (and (gnus-check-backend-function
6069                     'request-set-mark gnus-newsgroup-name)
6070                    (not (gnus-article-unpropagatable-p (cdr type))))
6071           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6072                  ;; Don't do anything about marks for articles we
6073                  ;; didn't actually get any headers for.
6074                  (del
6075                   (gnus-list-range-intersection
6076                    gnus-newsgroup-articles
6077                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6078                  (add
6079                   (gnus-list-range-intersection
6080                    gnus-newsgroup-articles
6081                    (gnus-remove-from-range
6082                     (gnus-copy-sequence list) old))))
6083             (when add
6084               (push (list add 'add (list (cdr type))) delta-marks))
6085             (when del
6086               ;; Don't delete marks from outside the active range.
6087               ;; This shouldn't happen, but is a sanity check.
6088               (setq del (gnus-sorted-range-intersection
6089                          (gnus-active gnus-newsgroup-name) del))
6090               (push (list del 'del (list (cdr type))) delta-marks))))
6091
6092         (when list
6093           (push (cons (cdr type) list) newmarked)))
6094
6095       (when delta-marks
6096         (unless (gnus-check-group gnus-newsgroup-name)
6097           (error "Can't open server for %s" gnus-newsgroup-name))
6098         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6099
6100       ;; Enter these new marks into the info of the group.
6101       (if (nthcdr 3 info)
6102           (setcar (nthcdr 3 info) newmarked)
6103         ;; Add the marks lists to the end of the info.
6104         (when newmarked
6105           (setcdr (nthcdr 2 info) (list newmarked))))
6106
6107       ;; Cut off the end of the info if there's nothing else there.
6108       (let ((i 5))
6109         (while (and (> i 2)
6110                     (not (nth i info)))
6111           (when (nthcdr (decf i) info)
6112             (setcdr (nthcdr i info) nil)))))))
6113
6114 (defun gnus-set-mode-line (where)
6115   "Set the mode line of the article or summary buffers.
6116 If WHERE is `summary', the summary mode line format will be used."
6117   ;; Is this mode line one we keep updated?
6118   (when (and (memq where gnus-updated-mode-lines)
6119              (symbol-value
6120               (intern (format "gnus-%s-mode-line-format-spec" where))))
6121     (let (mode-string)
6122       ;; We evaluate this in the summary buffer since these
6123       ;; variables are buffer-local to that buffer.
6124       (with-current-buffer gnus-summary-buffer
6125         ;; We bind all these variables that are used in the `eval' form
6126         ;; below.
6127         (let* ((mformat (symbol-value
6128                          (intern
6129                           (format "gnus-%s-mode-line-format-spec" where))))
6130                (gnus-tmp-group-name (gnus-mode-string-quote
6131                                      (gnus-group-decoded-name
6132                                       gnus-newsgroup-name)))
6133                (gnus-tmp-article-number (or gnus-current-article 0))
6134                (gnus-tmp-unread gnus-newsgroup-unreads)
6135                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6136                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6137                (gnus-tmp-unread-and-unselected
6138                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6139                             (zerop gnus-tmp-unselected))
6140                        "")
6141                       ((zerop gnus-tmp-unselected)
6142                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6143                       (t (format "{%d(+%d) more}"
6144                                  gnus-tmp-unread-and-unticked
6145                                  gnus-tmp-unselected))))
6146                (gnus-tmp-subject
6147                 (if (and gnus-current-headers
6148                          (vectorp gnus-current-headers))
6149                     (gnus-mode-string-quote
6150                      (mail-header-subject gnus-current-headers))
6151                   ""))
6152                bufname-length max-len
6153                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6154           (setq mode-string (eval mformat))
6155           (setq bufname-length (if (string-match "%b" mode-string)
6156                                    (- (length
6157                                        (buffer-name
6158                                         (if (eq where 'summary)
6159                                             nil
6160                                           (get-buffer gnus-article-buffer))))
6161                                       2)
6162                                  0))
6163           (setq max-len (max 4 (if gnus-mode-non-string-length
6164                                    (- (window-width)
6165                                       gnus-mode-non-string-length
6166                                       bufname-length)
6167                                  (length mode-string))))
6168           ;; We might have to chop a bit of the string off...
6169           (when (> (length mode-string) max-len)
6170             (setq mode-string
6171                   (concat (truncate-string-to-width mode-string (- max-len 3))
6172                           "...")))))
6173       ;; Update the mode line.
6174       (setq mode-line-buffer-identification
6175             (gnus-mode-line-buffer-identification (list mode-string)))
6176       (set-buffer-modified-p t))))
6177
6178 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6179   "Go through the HEADERS list and add all Xrefs to a hash table.
6180 The resulting hash table is returned, or nil if no Xrefs were found."
6181   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6182          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6183          (xref-hashtb (gnus-make-hashtable))
6184          start group entry number xrefs header)
6185     (while headers
6186       (setq header (pop headers))
6187       (when (and (setq xrefs (mail-header-xref header))
6188                  (not (memq (setq number (mail-header-number header))
6189                             unreads)))
6190         (setq start 0)
6191         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6192           (setq start (match-end 0))
6193           (setq group (if prefix
6194                           (concat prefix (substring xrefs (match-beginning 1)
6195                                                     (match-end 1)))
6196                         (substring xrefs (match-beginning 1) (match-end 1))))
6197           (setq number
6198                 (string-to-number (substring xrefs (match-beginning 2)
6199                                           (match-end 2))))
6200           (if (setq entry (gnus-gethash group xref-hashtb))
6201               (setcdr entry (cons number (cdr entry)))
6202             (gnus-sethash group (cons number nil) xref-hashtb)))))
6203     (and start xref-hashtb)))
6204
6205 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6206   "Look through all the headers and mark the Xrefs as read."
6207   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6208         name info xref-hashtb idlist method nth4)
6209     (with-current-buffer gnus-group-buffer
6210       (when (setq xref-hashtb
6211                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6212         (mapatoms
6213          (lambda (group)
6214            (unless (string= from-newsgroup (setq name (symbol-name group)))
6215              (setq idlist (symbol-value group))
6216              ;; Dead groups are not updated.
6217              (and (prog1
6218                       (setq info (gnus-get-info name))
6219                     (when (stringp (setq nth4 (gnus-info-method info)))
6220                       (setq nth4 (gnus-server-to-method nth4))))
6221                   ;; Only do the xrefs if the group has the same
6222                   ;; select method as the group we have just read.
6223                   (or (gnus-methods-equal-p
6224                        nth4 (gnus-find-method-for-group from-newsgroup))
6225                       virtual
6226                       (equal nth4 (setq method (gnus-find-method-for-group
6227                                                 from-newsgroup)))
6228                       (and (equal (car nth4) (car method))
6229                            (equal (nth 1 nth4) (nth 1 method))))
6230                   gnus-use-cross-reference
6231                   (or (not (eq gnus-use-cross-reference t))
6232                       virtual
6233                       ;; Only do cross-references on subscribed
6234                       ;; groups, if that is what is wanted.
6235                       (<= (gnus-info-level info) gnus-level-subscribed))
6236                   (gnus-group-make-articles-read name idlist))))
6237          xref-hashtb)))))
6238
6239 (defun gnus-compute-read-articles (group articles)
6240   (let* ((entry (gnus-group-entry group))
6241          (info (nth 2 entry))
6242          (active (gnus-active group))
6243          ninfo)
6244     (when entry
6245       ;; First peel off all invalid article numbers.
6246       (when active
6247         (let ((ids articles)
6248               id first)
6249           (while (setq id (pop ids))
6250             (when (and first (> id (cdr active)))
6251               ;; We'll end up in this situation in one particular
6252               ;; obscure situation.  If you re-scan a group and get
6253               ;; a new article that is cross-posted to a different
6254               ;; group that has not been re-scanned, you might get
6255               ;; crossposted article that has a higher number than
6256               ;; Gnus believes possible.  So we re-activate this
6257               ;; group as well.  This might mean doing the
6258               ;; crossposting thingy will *increase* the number
6259               ;; of articles in some groups.  Tsk, tsk.
6260               (setq active (or (gnus-activate-group group) active)))
6261             (when (or (> id (cdr active))
6262                       (< id (car active)))
6263               (setq articles (delq id articles))))))
6264       ;; If the read list is nil, we init it.
6265       (if (and active
6266                (null (gnus-info-read info))
6267                (> (car active) 1))
6268           (setq ninfo (cons 1 (1- (car active))))
6269         (setq ninfo (gnus-info-read info)))
6270       ;; Then we add the read articles to the range.
6271       (gnus-add-to-range
6272        ninfo (setq articles (sort articles '<))))))
6273
6274 (defun gnus-group-make-articles-read (group articles)
6275   "Update the info of GROUP to say that ARTICLES are read."
6276   (let* ((num 0)
6277          (entry (gnus-group-entry group))
6278          (info (nth 2 entry))
6279          (active (gnus-active group))
6280          range)
6281     (if (not entry)
6282         ;; Group that Gnus doesn't know exists, but still allow the
6283         ;; backend to set marks.
6284         (gnus-request-set-mark
6285          group (list (list (gnus-compress-sequence (sort articles #'<))
6286                            'add '(read))))
6287       ;; Normal, subscribed groups.
6288       (setq range (gnus-compute-read-articles group articles))
6289       (with-current-buffer gnus-group-buffer
6290         (gnus-undo-register
6291           `(progn
6292              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6293              (gnus-info-set-read ',info ',(gnus-info-read info))
6294              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6295              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6296              (gnus-group-update-group ,group t))))
6297       ;; Add the read articles to the range.
6298       (gnus-info-set-read info range)
6299       (gnus-request-set-mark group (list (list range 'add '(read))))
6300       ;; Then we have to re-compute how many unread
6301       ;; articles there are in this group.
6302       (when active
6303         (cond
6304          ((not range)
6305           (setq num (- (1+ (cdr active)) (car active))))
6306          ((not (listp (cdr range)))
6307           (setq num (- (cdr active) (- (1+ (cdr range))
6308                                        (car range)))))
6309          (t
6310           (while range
6311             (if (numberp (car range))
6312                 (setq num (1+ num))
6313               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6314             (setq range (cdr range)))
6315           (setq num (- (cdr active) num))))
6316         ;; Update the number of unread articles.
6317         (setcar entry num)
6318         ;; Update the group buffer.
6319         (unless (gnus-ephemeral-group-p group)
6320           (gnus-group-update-group group t))))))
6321
6322 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6323   (let ((cur nntp-server-buffer)
6324         (dependencies
6325          (or dependencies
6326              (with-current-buffer gnus-summary-buffer
6327                gnus-newsgroup-dependencies)))
6328         headers id end ref number
6329         (mail-parse-charset gnus-newsgroup-charset)
6330         (mail-parse-ignored-charsets
6331          (save-current-buffer (condition-case nil
6332                                   (set-buffer gnus-summary-buffer)
6333                                 (error))
6334                               gnus-newsgroup-ignored-charsets)))
6335     (with-current-buffer nntp-server-buffer
6336       ;; Translate all TAB characters into SPACE characters.
6337       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6338       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6339       (ietf-drums-unfold-fws)
6340       (gnus-run-hooks 'gnus-parse-headers-hook)
6341       (let ((case-fold-search t)
6342             in-reply-to header p lines chars)
6343         (goto-char (point-min))
6344         ;; Search to the beginning of the next header.  Error messages
6345         ;; do not begin with 2 or 3.
6346         (while (re-search-forward "^[23][0-9]+ " nil t)
6347           (setq id nil
6348                 ref nil)
6349           ;; This implementation of this function, with nine
6350           ;; search-forwards instead of the one re-search-forward and
6351           ;; a case (which basically was the old function) is actually
6352           ;; about twice as fast, even though it looks messier.  You
6353           ;; can't have everything, I guess.  Speed and elegance
6354           ;; doesn't always go hand in hand.
6355           (setq
6356            header
6357            (vector
6358             ;; Number.
6359             (prog1
6360                 (setq number (read cur))
6361               (end-of-line)
6362               (setq p (point))
6363               (narrow-to-region (point)
6364                                 (or (and (search-forward "\n.\n" nil t)
6365                                          (- (point) 2))
6366                                     (point))))
6367             ;; Subject.
6368             (progn
6369               (goto-char p)
6370               (if (search-forward "\nsubject:" nil t)
6371                   (funcall gnus-decode-encoded-word-function
6372                            (nnheader-header-value))
6373                 "(none)"))
6374             ;; From.
6375             (progn
6376               (goto-char p)
6377               (if (search-forward "\nfrom:" nil t)
6378                   (funcall gnus-decode-encoded-address-function
6379                            (nnheader-header-value))
6380                 "(nobody)"))
6381             ;; Date.
6382             (progn
6383               (goto-char p)
6384               (if (search-forward "\ndate:" nil t)
6385                   (nnheader-header-value) ""))
6386             ;; Message-ID.
6387             (progn
6388               (goto-char p)
6389               (setq id (if (re-search-forward
6390                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6391                            ;; We do it this way to make sure the Message-ID
6392                            ;; is (somewhat) syntactically valid.
6393                            (buffer-substring (match-beginning 1)
6394                                              (match-end 1))
6395                          ;; If there was no message-id, we just fake one
6396                          ;; to make subsequent routines simpler.
6397                          (nnheader-generate-fake-message-id number))))
6398             ;; References.
6399             (progn
6400               (goto-char p)
6401               (if (search-forward "\nreferences:" nil t)
6402                   (progn
6403                     (setq end (point))
6404                     (prog1
6405                         (nnheader-header-value)
6406                       (setq ref
6407                             (buffer-substring
6408                              (progn
6409                                (end-of-line)
6410                                (search-backward ">" end t)
6411                                (1+ (point)))
6412                              (progn
6413                                (search-backward "<" end t)
6414                                (point))))))
6415                 ;; Get the references from the in-reply-to header if there
6416                 ;; were no references and the in-reply-to header looks
6417                 ;; promising.
6418                 (if (and (search-forward "\nin-reply-to:" nil t)
6419                          (setq in-reply-to (nnheader-header-value))
6420                          (string-match "<[^>]+>" in-reply-to))
6421                     (let (ref2)
6422                       (setq ref (substring in-reply-to (match-beginning 0)
6423                                            (match-end 0)))
6424                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6425                         (setq ref2 (substring in-reply-to (match-beginning 0)
6426                                               (match-end 0)))
6427                         (when (> (length ref2) (length ref))
6428                           (setq ref ref2)))
6429                       ref)
6430                   (setq ref nil))))
6431             ;; Chars.
6432             (progn
6433               (goto-char p)
6434               (if (search-forward "\nchars: " nil t)
6435                   (if (numberp (setq chars (ignore-errors (read cur))))
6436                       chars -1)
6437                 -1))
6438             ;; Lines.
6439             (progn
6440               (goto-char p)
6441               (if (search-forward "\nlines: " nil t)
6442                   (if (numberp (setq lines (ignore-errors (read cur))))
6443                       lines -1)
6444                 -1))
6445             ;; Xref.
6446             (progn
6447               (goto-char p)
6448               (and (search-forward "\nxref:" nil t)
6449                    (nnheader-header-value)))
6450             ;; Extra.
6451             (when gnus-extra-headers
6452               (let ((extra gnus-extra-headers)
6453                     out)
6454                 (while extra
6455                   (goto-char p)
6456                   (when (search-forward
6457                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6458                     (push (cons (car extra) (nnheader-header-value))
6459                           out))
6460                   (pop extra))
6461                 out))))
6462           (when (equal id ref)
6463             (setq ref nil))
6464
6465           (when gnus-alter-header-function
6466             (funcall gnus-alter-header-function header)
6467             (setq id (mail-header-id header)
6468                   ref (gnus-parent-id (mail-header-references header))))
6469
6470           (when (setq header
6471                       (gnus-dependencies-add-header
6472                        header dependencies force-new))
6473             (push header headers))
6474           (goto-char (point-max))
6475           (widen))
6476         (nreverse headers)))))
6477
6478 ;; Goes through the xover lines and returns a list of vectors
6479 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6480                                                   force-new dependencies
6481                                                   group also-fetch-heads)
6482   "Parse the news overview data in the server buffer.
6483 Return a list of headers that match SEQUENCE (see
6484 `nntp-retrieve-headers')."
6485   ;; Get the Xref when the users reads the articles since most/some
6486   ;; NNTP servers do not include Xrefs when using XOVER.
6487   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6488   (let ((mail-parse-charset gnus-newsgroup-charset)
6489         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6490         (cur nntp-server-buffer)
6491         (dependencies (or dependencies gnus-newsgroup-dependencies))
6492         (allp (cond
6493                ((eq gnus-read-all-available-headers t)
6494                 t)
6495                ((and (stringp gnus-read-all-available-headers)
6496                      group)
6497                 (string-match gnus-read-all-available-headers group))
6498                (t
6499                 nil)))
6500         number headers header)
6501     (with-current-buffer nntp-server-buffer
6502       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6503       ;; Allow the user to mangle the headers before parsing them.
6504       (gnus-run-hooks 'gnus-parse-headers-hook)
6505       (goto-char (point-min))
6506       (gnus-parse-without-error
6507         (while (and (or sequence allp)
6508                     (not (eobp)))
6509           (setq number (read cur))
6510           (when (not allp)
6511             (while (and sequence
6512                         (< (car sequence) number))
6513               (setq sequence (cdr sequence))))
6514           (when (and (or allp
6515                          (and sequence
6516                               (eq number (car sequence))))
6517                      (progn
6518                        (setq sequence (cdr sequence))
6519                        (setq header (inline
6520                                       (gnus-nov-parse-line
6521                                        number dependencies force-new)))))
6522             (push header headers))
6523           (forward-line 1)))
6524       ;; A common bug in inn is that if you have posted an article and
6525       ;; then retrieves the active file, it will answer correctly --
6526       ;; the new article is included.  However, a NOV entry for the
6527       ;; article may not have been generated yet, so this may fail.
6528       ;; We work around this problem by retrieving the last few
6529       ;; headers using HEAD.
6530       (if (or (not also-fetch-heads)
6531               (not sequence))
6532           ;; We (probably) got all the headers.
6533           (nreverse headers)
6534         (let ((gnus-nov-is-evil t))
6535           (nconc
6536            (nreverse headers)
6537            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6538              (gnus-get-newsgroup-headers))))))))
6539
6540 (defun gnus-article-get-xrefs ()
6541   "Fill in the Xref value in `gnus-current-headers', if necessary.
6542 This is meant to be called in `gnus-article-internal-prepare-hook'."
6543   (let ((headers (with-current-buffer gnus-summary-buffer
6544                    gnus-current-headers)))
6545     (or (not gnus-use-cross-reference)
6546         (not headers)
6547         (and (mail-header-xref headers)
6548              (not (string= (mail-header-xref headers) "")))
6549         (let ((case-fold-search t)
6550               xref)
6551           (save-restriction
6552             (nnheader-narrow-to-headers)
6553             (goto-char (point-min))
6554             (when (or (and (not (eobp))
6555                            (eq (downcase (char-after)) ?x)
6556                            (looking-at "Xref:"))
6557                       (search-forward "\nXref:" nil t))
6558               (goto-char (1+ (match-end 0)))
6559               (setq xref (buffer-substring (point) (point-at-eol)))
6560               (mail-header-set-xref headers xref)))))))
6561
6562 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6563   "Find article ID and insert the summary line for that article.
6564 OLD-HEADER can either be a header or a line number to insert
6565 the subject line on.
6566 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6567 and OLD-HEADER will be used when the summary line is inserted,
6568 too, instead of trying to fetch new headers."
6569   (let* ((line (and (numberp old-header) old-header))
6570          (old-header (and (vectorp old-header) old-header))
6571          (header (cond ((and old-header use-old-header)
6572                         old-header)
6573                        ((and (numberp id)
6574                              (gnus-number-to-header id))
6575                         (gnus-number-to-header id))
6576                        (t
6577                         (gnus-read-header id))))
6578          (number (and (numberp id) id))
6579          d)
6580     (when header
6581       ;; Rebuild the thread that this article is part of and go to the
6582       ;; article we have fetched.
6583       (when (and (not gnus-show-threads)
6584                  old-header)
6585         (when (and number
6586                    (setq d (gnus-data-find (mail-header-number old-header))))
6587           (goto-char (gnus-data-pos d))
6588           (gnus-data-remove
6589            number
6590            (- (point-at-bol)
6591               (prog1
6592                   (1+ (point-at-eol))
6593                 (gnus-delete-line))))))
6594       ;; Remove list identifiers from subject.
6595       (let ((gnus-newsgroup-headers (list header)))
6596         (gnus-summary-remove-list-identifiers))
6597       (when old-header
6598         (mail-header-set-number header (mail-header-number old-header)))
6599       (setq gnus-newsgroup-sparse
6600             (delq (setq number (mail-header-number header))
6601                   gnus-newsgroup-sparse))
6602       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6603       (push number gnus-newsgroup-limit)
6604       (gnus-rebuild-thread (mail-header-id header) line)
6605       (gnus-summary-goto-subject number nil t))
6606     (when (and (numberp number)
6607                (> number 0))
6608       ;; We have to update the boundaries even if we can't fetch the
6609       ;; article if ID is a number -- so that the next `P' or `N'
6610       ;; command will fetch the previous (or next) article even
6611       ;; if the one we tried to fetch this time has been canceled.
6612       (when (> number gnus-newsgroup-end)
6613         (setq gnus-newsgroup-end number))
6614       (when (< number gnus-newsgroup-begin)
6615         (setq gnus-newsgroup-begin number))
6616       (setq gnus-newsgroup-unselected
6617             (delq number gnus-newsgroup-unselected)))
6618     ;; Report back a success?
6619     (and header (mail-header-number header))))
6620
6621 ;;; Process/prefix in the summary buffer
6622
6623 (defun gnus-summary-work-articles (n)
6624   "Return a list of articles to be worked upon.
6625 The prefix argument, the list of process marked articles, and the
6626 current article will be taken into consideration."
6627   (with-current-buffer gnus-summary-buffer
6628     (cond
6629      (n
6630       ;; A numerical prefix has been given.
6631       (setq n (prefix-numeric-value n))
6632       (let ((backward (< n 0))
6633             (n (abs (prefix-numeric-value n)))
6634             articles article)
6635         (save-excursion
6636           (while
6637               (and (> n 0)
6638                    (push (setq article (gnus-summary-article-number))
6639                          articles)
6640                    (if backward
6641                        (gnus-summary-find-prev nil article)
6642                      (gnus-summary-find-next nil article)))
6643             (decf n)))
6644         (nreverse articles)))
6645      ((and (gnus-region-active-p) (mark))
6646       (message "region active")
6647       ;; Work on the region between point and mark.
6648       (let ((max (max (point) (mark)))
6649             articles article)
6650         (save-excursion
6651           (goto-char (min (point) (mark)))
6652           (while
6653               (and
6654                (push (setq article (gnus-summary-article-number)) articles)
6655                (gnus-summary-find-next nil article)
6656                (< (point) max)))
6657           (nreverse articles))))
6658      (gnus-newsgroup-processable
6659       ;; There are process-marked articles present.
6660       ;; Save current state.
6661       (gnus-summary-save-process-mark)
6662       ;; Return the list.
6663       (reverse gnus-newsgroup-processable))
6664      (t
6665       ;; Just return the current article.
6666       (list (gnus-summary-article-number))))))
6667
6668 (defmacro gnus-summary-iterate (arg &rest forms)
6669   "Iterate over the process/prefixed articles and do FORMS.
6670 ARG is the interactive prefix given to the command.  FORMS will be
6671 executed with point over the summary line of the articles."
6672   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6673     `(let ((,articles (gnus-summary-work-articles ,arg)))
6674        (while ,articles
6675          (gnus-summary-goto-subject (car ,articles))
6676          ,@forms
6677          (pop ,articles)))))
6678
6679 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6680 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6681
6682 (defun gnus-summary-save-process-mark ()
6683   "Push the current set of process marked articles on the stack."
6684   (interactive)
6685   (push (copy-sequence gnus-newsgroup-processable)
6686         gnus-newsgroup-process-stack))
6687
6688 (defun gnus-summary-kill-process-mark ()
6689   "Push the current set of process marked articles on the stack and unmark."
6690   (interactive)
6691   (gnus-summary-save-process-mark)
6692   (gnus-summary-unmark-all-processable))
6693
6694 (defun gnus-summary-yank-process-mark ()
6695   "Pop the last process mark state off the stack and restore it."
6696   (interactive)
6697   (unless gnus-newsgroup-process-stack
6698     (error "Empty mark stack"))
6699   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6700
6701 (defun gnus-summary-process-mark-set (set)
6702   "Make SET into the current process marked articles."
6703   (gnus-summary-unmark-all-processable)
6704   (mapc 'gnus-summary-set-process-mark set))
6705
6706 ;;; Searching and stuff
6707
6708 (defun gnus-summary-search-group (&optional backward use-level)
6709   "Search for next unread newsgroup.
6710 If optional argument BACKWARD is non-nil, search backward instead."
6711   (with-current-buffer gnus-group-buffer
6712     (when (gnus-group-search-forward
6713            backward nil (if use-level (gnus-group-group-level) nil))
6714       (gnus-group-group-name))))
6715
6716 (defun gnus-summary-best-group (&optional exclude-group)
6717   "Find the name of the best unread group.
6718 If EXCLUDE-GROUP, do not go to this group."
6719   (with-current-buffer gnus-group-buffer
6720     (save-excursion
6721       (gnus-group-best-unread-group exclude-group))))
6722
6723 (defun gnus-summary-find-next (&optional unread article backward)
6724   (if backward
6725       (gnus-summary-find-prev unread article)
6726     (let* ((dummy (gnus-summary-article-intangible-p))
6727            (article (or article (gnus-summary-article-number)))
6728            (data (gnus-data-find-list article))
6729            result)
6730       (when (and (not dummy)
6731                  (or (not gnus-summary-check-current)
6732                      (not unread)
6733                      (not (gnus-data-unread-p (car data)))))
6734         (setq data (cdr data)))
6735       (when (setq result
6736                   (if unread
6737                       (progn
6738                         (while data
6739                           (unless (memq (gnus-data-number (car data))
6740                                         (cond
6741                                          ((eq gnus-auto-goto-ignores
6742                                               'always-undownloaded)
6743                                           gnus-newsgroup-undownloaded)
6744                                          (gnus-plugged
6745                                           nil)
6746                                          ((eq gnus-auto-goto-ignores
6747                                               'unfetched)
6748                                           gnus-newsgroup-unfetched)
6749                                          ((eq gnus-auto-goto-ignores
6750                                               'undownloaded)
6751                                           gnus-newsgroup-undownloaded)))
6752                             (when (gnus-data-unread-p (car data))
6753                               (setq result (car data)
6754                                     data nil)))
6755                           (setq data (cdr data)))
6756                         result)
6757                     (car data)))
6758         (goto-char (gnus-data-pos result))
6759         (gnus-data-number result)))))
6760
6761 (defun gnus-summary-find-prev (&optional unread article)
6762   (let* ((eobp (eobp))
6763          (article (or article (gnus-summary-article-number)))
6764          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6765          result)
6766     (when (and (not eobp)
6767                (or (not gnus-summary-check-current)
6768                    (not unread)
6769                    (not (gnus-data-unread-p (car data)))))
6770       (setq data (cdr data)))
6771     (when (setq result
6772                 (if unread
6773                     (progn
6774                       (while data
6775                         (unless (memq (gnus-data-number (car data))
6776                                       (cond
6777                                        ((eq gnus-auto-goto-ignores
6778                                             'always-undownloaded)
6779                                         gnus-newsgroup-undownloaded)
6780                                        (gnus-plugged
6781                                         nil)
6782                                        ((eq gnus-auto-goto-ignores
6783                                             'unfetched)
6784                                         gnus-newsgroup-unfetched)
6785                                        ((eq gnus-auto-goto-ignores
6786                                             'undownloaded)
6787                                         gnus-newsgroup-undownloaded)))
6788                           (when (gnus-data-unread-p (car data))
6789                             (setq result (car data)
6790                                   data nil)))
6791                         (setq data (cdr data)))
6792                       result)
6793                   (car data)))
6794       (goto-char (gnus-data-pos result))
6795       (gnus-data-number result))))
6796
6797 (defun gnus-summary-find-subject (subject &optional unread backward article)
6798   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6799          (article (or article (gnus-summary-article-number)))
6800          (articles (gnus-data-list backward))
6801          (arts (gnus-data-find-list article articles))
6802          result)
6803     (when (or (not gnus-summary-check-current)
6804               (not unread)
6805               (not (gnus-data-unread-p (car arts))))
6806       (setq arts (cdr arts)))
6807     (while arts
6808       (and (or (not unread)
6809                (gnus-data-unread-p (car arts)))
6810            (vectorp (gnus-data-header (car arts)))
6811            (gnus-subject-equal
6812             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6813            (setq result (car arts)
6814                  arts nil))
6815       (setq arts (cdr arts)))
6816     (and result
6817          (goto-char (gnus-data-pos result))
6818          (gnus-data-number result))))
6819
6820 (defun gnus-summary-search-forward (&optional unread subject backward)
6821   "Search forward for an article.
6822 If UNREAD, look for unread articles.  If SUBJECT, look for
6823 articles with that subject.  If BACKWARD, search backward instead."
6824   (cond (subject (gnus-summary-find-subject subject unread backward))
6825         (backward (gnus-summary-find-prev unread))
6826         (t (gnus-summary-find-next unread))))
6827
6828 (defun gnus-recenter (&optional n)
6829   "Center point in window and redisplay frame.
6830 Also do horizontal recentering."
6831   (interactive "P")
6832   (when (and gnus-auto-center-summary
6833              (not (eq gnus-auto-center-summary 'vertical)))
6834     (gnus-horizontal-recenter))
6835   (if (fboundp 'recenter-top-bottom)
6836       (recenter-top-bottom n)
6837     (recenter n)))
6838
6839 (put 'gnus-recenter 'isearch-scroll t)
6840
6841 (defun gnus-forward-line-ignore-invisible (n)
6842   "Move N lines forward (backward if N is negative).
6843 Like forward-line, but skip over (and don't count) invisible lines."
6844   (let (done)
6845     (while (and (> n 0) (not done))
6846       ;; If the following character is currently invisible,
6847       ;; skip all characters with that same `invisible' property value.
6848       (while (gnus-invisible-p (point))
6849         (goto-char (gnus-next-char-property-change (point))))
6850       (forward-line 1)
6851       (if (eobp)
6852           (setq done t)
6853         (setq n (1- n))))
6854     (while (and (< n 0) (not done))
6855       (forward-line -1)
6856       (if (bobp) (setq done t)
6857         (setq n (1+ n))
6858         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6859           (goto-char (gnus-previous-char-property-change (point))))))))
6860
6861 (defun gnus-summary-recenter ()
6862   "Center point in the summary window.
6863 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6864 displayed, no centering will be performed."
6865   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6866   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6867   (interactive)
6868   ;; The user has to want it.
6869   (when gnus-auto-center-summary
6870     (let* ((top (cond ((< (window-height) 4) 0)
6871                       ((< (window-height) 7) 1)
6872                       (t (if (numberp gnus-auto-center-summary)
6873                              gnus-auto-center-summary
6874                            (/ (1- (window-height)) 2)))))
6875            (height (1- (window-height)))
6876            (bottom (save-excursion
6877                      (goto-char (point-max))
6878                      (gnus-forward-line-ignore-invisible (- height))
6879                      (point)))
6880            (window (get-buffer-window (current-buffer))))
6881       (when (get-buffer-window gnus-article-buffer)
6882         ;; Only do recentering when the article buffer is displayed,
6883         ;; Set the window start to either `bottom', which is the biggest
6884         ;; possible valid number, or the second line from the top,
6885         ;; whichever is the least.
6886         (let ((top-pos (save-excursion
6887                          (gnus-forward-line-ignore-invisible (- top))
6888                          (point))))
6889           (if (> bottom top-pos)
6890               ;; Keep the second line from the top visible
6891               (set-window-start window top-pos)
6892             ;; Try to keep the bottom line visible; if it's partially
6893             ;; obscured, either scroll one more line to make it fully
6894             ;; visible, or revert to using TOP-POS.
6895             (save-excursion
6896               (goto-char (point-max))
6897               (gnus-forward-line-ignore-invisible -1)
6898               (let ((last-line-start (point)))
6899                 (goto-char bottom)
6900                 (set-window-start window (point) t)
6901                 (when (not (pos-visible-in-window-p last-line-start window))
6902                   (gnus-forward-line-ignore-invisible 1)
6903                   (set-window-start window (min (point) top-pos) t)))))))
6904       ;; Do horizontal recentering while we're at it.
6905       (when (and (get-buffer-window (current-buffer) t)
6906                  (not (eq gnus-auto-center-summary 'vertical)))
6907         (let ((selected (selected-window)))
6908           (select-window (get-buffer-window (current-buffer) t))
6909           (gnus-summary-position-point)
6910           (gnus-horizontal-recenter)
6911           (select-window selected))))))
6912
6913 (defun gnus-summary-jump-to-group (newsgroup)
6914   "Move point to NEWSGROUP in group mode buffer."
6915   ;; Keep update point of group mode buffer if visible.
6916   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6917       (save-window-excursion
6918         ;; Take care of tree window mode.
6919         (when (get-buffer-window gnus-group-buffer)
6920           (pop-to-buffer gnus-group-buffer))
6921         (gnus-group-jump-to-group newsgroup))
6922     (save-excursion
6923       ;; Take care of tree window mode.
6924       (if (get-buffer-window gnus-group-buffer 0)
6925           (pop-to-buffer gnus-group-buffer)
6926         (set-buffer gnus-group-buffer))
6927       (gnus-group-jump-to-group newsgroup))))
6928
6929 ;; This function returns a list of article numbers based on the
6930 ;; difference between the ranges of read articles in this group and
6931 ;; the range of active articles.
6932 (defun gnus-list-of-unread-articles (group)
6933   (let* ((read (gnus-info-read (gnus-get-info group)))
6934          (active (or (gnus-active group) (gnus-activate-group group)))
6935          (last (or (cdr active)
6936                    (error "Group %s couldn't be activated " group)))
6937          (bottom (if gnus-newsgroup-maximum-articles
6938                      (max (car active)
6939                           (- last gnus-newsgroup-maximum-articles -1))
6940                    (car active)))
6941          first nlast unread)
6942     ;; If none are read, then all are unread.
6943     (if (not read)
6944         (setq first bottom)
6945       ;; If the range of read articles is a single range, then the
6946       ;; first unread article is the article after the last read
6947       ;; article.  Sounds logical, doesn't it?
6948       (if (and (not (listp (cdr read)))
6949                (or (< (car read) bottom)
6950                    (progn (setq read (list read))
6951                           nil)))
6952           (setq first (max bottom (1+ (cdr read))))
6953         ;; `read' is a list of ranges.
6954         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6955                                   (caar read)))
6956                   1)
6957           (setq first bottom))
6958         (while read
6959           (when first
6960             (while (< first nlast)
6961               (setq unread (cons first unread)
6962                     first (1+ first))))
6963           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6964           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6965           (setq read (cdr read)))))
6966     ;; And add the last unread articles.
6967     (while (<= first last)
6968       (setq unread (cons first unread)
6969             first (1+ first)))
6970     ;; Return the list of unread articles.
6971     (delq 0 (nreverse unread))))
6972
6973 (defun gnus-list-of-read-articles (group)
6974   "Return a list of unread, unticked and non-dormant articles."
6975   (let* ((info (gnus-get-info group))
6976          (marked (gnus-info-marks info))
6977          (active (gnus-active group)))
6978     (and info active
6979          (gnus-list-range-difference
6980           (gnus-list-range-difference
6981            (gnus-sorted-complement
6982             (gnus-uncompress-range
6983              (if gnus-newsgroup-maximum-articles
6984                  (cons (max (car active)
6985                             (- (cdr active)
6986                                gnus-newsgroup-maximum-articles
6987                                -1))
6988                        (cdr active))
6989                active))
6990             (gnus-list-of-unread-articles group))
6991            (cdr (assq 'dormant marked)))
6992           (cdr (assq 'tick marked))))))
6993
6994 ;; This function returns a sequence of article numbers based on the
6995 ;; difference between the ranges of read articles in this group and
6996 ;; the range of active articles.
6997 (defun gnus-sequence-of-unread-articles (group)
6998   (let* ((read (gnus-info-read (gnus-get-info group)))
6999          (active (or (gnus-active group) (gnus-activate-group group)))
7000          (last (cdr active))
7001          (bottom (if gnus-newsgroup-maximum-articles
7002                      (max (car active)
7003                           (- last gnus-newsgroup-maximum-articles -1))
7004                    (car active)))
7005          first nlast unread)
7006     ;; If none are read, then all are unread.
7007     (if (not read)
7008         (setq first bottom)
7009       ;; If the range of read articles is a single range, then the
7010       ;; first unread article is the article after the last read
7011       ;; article.  Sounds logical, doesn't it?
7012       (if (and (not (listp (cdr read)))
7013                (or (< (car read) bottom)
7014                    (progn (setq read (list read))
7015                           nil)))
7016           (setq first (max bottom (1+ (cdr read))))
7017         ;; `read' is a list of ranges.
7018         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7019                                   (caar read)))
7020                   1)
7021           (setq first bottom))
7022         (while read
7023           (when first
7024             (push (cons first nlast) unread))
7025           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7026           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7027           (setq read (cdr read)))))
7028     ;; And add the last unread articles.
7029     (cond ((not (and first last))
7030            nil)
7031           ((< first last)
7032            (push (cons first last) unread))
7033           ((= first last)
7034            (push first unread)))
7035     ;; Return the sequence of unread articles.
7036     (delq 0 (nreverse unread))))
7037
7038 ;; Various summary commands
7039
7040 (defun gnus-summary-select-article-buffer ()
7041   "Reconfigure windows to show the article buffer.
7042 If `gnus-widen-article-window' is set, show only the article
7043 buffer."
7044   (interactive)
7045   (if (not (gnus-buffer-live-p gnus-article-buffer))
7046       (error "There is no article buffer for this summary buffer")
7047     (unless (get-buffer-window gnus-article-buffer)
7048       (gnus-summary-show-article))
7049     (gnus-configure-windows
7050      (if gnus-widen-article-window
7051          'only-article
7052        'article)
7053      t)
7054     (select-window (get-buffer-window gnus-article-buffer))))
7055
7056 (defun gnus-summary-universal-argument (arg)
7057   "Perform any operation on all articles that are process/prefixed."
7058   (interactive "P")
7059   (let ((articles (gnus-summary-work-articles arg))
7060         func article)
7061     (if (eq
7062          (setq
7063           func
7064           (key-binding
7065            (read-key-sequence
7066             (substitute-command-keys
7067              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7068          'undefined)
7069         (gnus-error 1 "Undefined key")
7070       (save-excursion
7071         (while articles
7072           (gnus-summary-goto-subject (setq article (pop articles)))
7073           (let (gnus-newsgroup-processable)
7074             (command-execute func))
7075           (gnus-summary-remove-process-mark article)))))
7076   (gnus-summary-position-point))
7077
7078 (defun gnus-summary-toggle-truncation (&optional arg)
7079   "Toggle truncation of summary lines.
7080 With ARG, turn line truncation on if ARG is positive."
7081   (interactive "P")
7082   (setq truncate-lines
7083         (if (null arg) (not truncate-lines)
7084           (> (prefix-numeric-value arg) 0)))
7085   (redraw-display))
7086
7087 (defun gnus-summary-find-for-reselect ()
7088   "Return the number of an article to stay on across a reselect.
7089 The current article is considered, then following articles, then previous
7090 articles.  An article is sought which is not cancelled and isn't a temporary
7091 insertion from another group.  If there's no such then return a dummy 0."
7092   (let (found)
7093     (dolist (rev '(nil t))
7094       (unless found      ; don't demand the reverse list if we don't need it
7095         (let ((data (gnus-data-find-list
7096                      (gnus-summary-article-number) (gnus-data-list rev))))
7097           (while (and data (not found))
7098             (if (and (< 0 (gnus-data-number (car data)))
7099                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7100                 (setq found (gnus-data-number (car data))))
7101             (setq data (cdr data))))))
7102     (or found 0)))
7103
7104 (defun gnus-summary-reselect-current-group (&optional all rescan)
7105   "Exit and then reselect the current newsgroup.
7106 The prefix argument ALL means to select all articles."
7107   (interactive "P")
7108   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7109     (error "Ephemeral groups can't be reselected"))
7110   (let ((current-subject (gnus-summary-find-for-reselect))
7111         (group gnus-newsgroup-name))
7112     (setq gnus-newsgroup-begin nil)
7113     (gnus-summary-exit nil 'leave-hidden)
7114     ;; We have to adjust the point of group mode buffer because
7115     ;; point was moved to the next unread newsgroup by exiting.
7116     (gnus-summary-jump-to-group group)
7117     (when rescan
7118       (save-excursion
7119         (gnus-group-get-new-news-this-group 1)))
7120     (gnus-group-read-group all t)
7121     (gnus-summary-goto-subject current-subject nil t)))
7122
7123 (defun gnus-summary-rescan-group (&optional all)
7124   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7125   (interactive "P")
7126   (let ((config gnus-current-window-configuration))
7127     (gnus-summary-reselect-current-group all t)
7128     (gnus-configure-windows config)
7129     (when (eq config 'article)
7130       (gnus-summary-select-article))))
7131
7132 (defun gnus-summary-update-info (&optional non-destructive)
7133   (save-excursion
7134     (let ((group gnus-newsgroup-name))
7135       (when group
7136         (when gnus-newsgroup-kill-headers
7137           (setq gnus-newsgroup-killed
7138                 (gnus-compress-sequence
7139                  (gnus-sorted-union
7140                   (gnus-list-range-intersection
7141                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7142                   gnus-newsgroup-unreads)
7143                  t)))
7144         (unless (listp (cdr gnus-newsgroup-killed))
7145           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7146         (let ((headers gnus-newsgroup-headers)
7147               (ephemeral-p (gnus-ephemeral-group-p group))
7148               info)
7149           (unless ephemeral-p
7150             (setq info (copy-sequence (gnus-get-info group))
7151                   info (delq (gnus-info-params info) info)))
7152           ;; Set the new ranges of read articles.
7153           (with-current-buffer gnus-group-buffer
7154             (gnus-undo-force-boundary))
7155           (gnus-update-read-articles
7156            group (gnus-sorted-union
7157                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7158           ;; Set the current article marks.
7159           (let ((gnus-newsgroup-scored
7160                  (if (and (not gnus-save-score)
7161                           (not non-destructive))
7162                      nil
7163                    gnus-newsgroup-scored)))
7164             (save-excursion
7165               (gnus-update-marks)))
7166           ;; Do the cross-ref thing.
7167           (when gnus-use-cross-reference
7168             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7169           ;; Do not switch windows but change the buffer to work.
7170           (set-buffer gnus-group-buffer)
7171           (unless ephemeral-p
7172             (gnus-group-update-group
7173              group nil
7174              (equal info
7175                     (setq info (copy-sequence (gnus-get-info group))
7176                           info (delq (gnus-info-params info) info))))))))))
7177
7178 (defun gnus-summary-save-newsrc (&optional force)
7179   "Save the current number of read/marked articles in the dribble buffer.
7180 The dribble buffer will then be saved.
7181 If FORCE (the prefix), also save the .newsrc file(s)."
7182   (interactive "P")
7183   (gnus-summary-update-info t)
7184   (if force
7185       (gnus-save-newsrc-file)
7186     (gnus-dribble-save)))
7187
7188 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7189
7190 (defun gnus-summary-exit (&optional temporary leave-hidden)
7191   "Exit reading current newsgroup, and then return to group selection mode.
7192 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7193   (interactive)
7194   (gnus-set-global-variables)
7195   (when (gnus-buffer-live-p gnus-article-buffer)
7196     (with-current-buffer gnus-article-buffer
7197       (mm-destroy-parts gnus-article-mime-handles)
7198       ;; Set it to nil for safety reason.
7199       (setq gnus-article-mime-handle-alist nil)
7200       (setq gnus-article-mime-handles nil)))
7201   (gnus-kill-save-kill-buffer)
7202   (gnus-async-halt-prefetch)
7203   (let* ((group gnus-newsgroup-name)
7204          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7205          (gnus-group-is-exiting-p t)
7206          (article-buffer gnus-article-buffer)
7207          (original-article-buffer gnus-original-article-buffer)
7208          (mode major-mode)
7209          (group-point nil)
7210          (buf (current-buffer))
7211          ;; `gnus-single-article-buffer' is nil buffer-locally in
7212          ;; ephemeral group of which summary buffer will be killed,
7213          ;; but the global value may be non-nil.
7214          (single-article-buffer gnus-single-article-buffer))
7215     (unless quit-config
7216       ;; Do adaptive scoring, and possibly save score files.
7217       (when gnus-newsgroup-adaptive
7218         (gnus-score-adaptive))
7219       (when gnus-use-scoring
7220         (gnus-score-save)))
7221     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7222     (when gnus-use-cache
7223       (gnus-cache-possibly-remove-articles)
7224       (gnus-cache-save-buffers))
7225     (gnus-async-prefetch-remove-group group)
7226     (when gnus-suppress-duplicates
7227       (gnus-dup-enter-articles))
7228     (when gnus-use-trees
7229       (gnus-tree-close group))
7230     (when gnus-use-cache
7231       (gnus-cache-write-active))
7232     ;; Remove entries for this group.
7233     (nnmail-purge-split-history (gnus-group-real-name group))
7234     ;; Make all changes in this group permanent.
7235     (unless quit-config
7236       (gnus-run-hooks 'gnus-exit-group-hook)
7237       (gnus-summary-update-info))
7238     (gnus-close-group group)
7239     ;; Make sure where we were, and go to next newsgroup.
7240     (set-buffer gnus-group-buffer)
7241     (unless quit-config
7242       (gnus-group-jump-to-group group))
7243     (gnus-run-hooks 'gnus-summary-exit-hook)
7244     (unless (or quit-config
7245                 (not gnus-summary-next-group-on-exit)
7246                 ;; If this group has disappeared from the summary
7247                 ;; buffer, don't skip forwards.
7248                 (not (string= group (gnus-group-group-name))))
7249       (gnus-group-next-unread-group 1))
7250     (setq group-point (point))
7251     (if temporary
7252         nil                             ;Nothing to do.
7253       (set-buffer buf)
7254       (if (not gnus-kill-summary-on-exit)
7255           (progn
7256             (gnus-deaden-summary)
7257             (setq mode nil))
7258         (when (get-buffer gnus-article-buffer)
7259           (bury-buffer gnus-article-buffer))
7260         ;; Return to group mode buffer.
7261         (when (eq mode 'gnus-summary-mode)
7262           (gnus-kill-buffer buf)))
7263
7264       (setq gnus-current-select-method gnus-select-method)
7265       (set-buffer gnus-group-buffer)
7266       (if quit-config
7267           (gnus-handle-ephemeral-exit quit-config)
7268         (goto-char group-point)
7269         ;; If gnus-group-buffer is already displayed, make sure we also move
7270         ;; the cursor in the window that displays it.
7271         (let ((win (get-buffer-window (current-buffer) 0)))
7272           (if win (set-window-point win (point))))
7273         (unless leave-hidden
7274           (gnus-configure-windows 'group 'force)))
7275
7276       ;; If we have several article buffers, we kill them at exit.
7277       (unless single-article-buffer
7278         (when (gnus-buffer-live-p article-buffer)
7279           (with-current-buffer article-buffer
7280             ;; Don't kill sticky article buffers
7281             (unless (eq major-mode 'gnus-sticky-article-mode)
7282               (gnus-kill-buffer article-buffer)
7283               (setq gnus-article-current nil))))
7284         (gnus-kill-buffer original-article-buffer))
7285
7286       ;; Clear the current group name.
7287       (unless quit-config
7288         (setq gnus-newsgroup-name nil)))))
7289
7290 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7291 (defun gnus-summary-exit-no-update (&optional no-questions)
7292   "Quit reading current newsgroup without updating read article info."
7293   (interactive)
7294   (let* ((group gnus-newsgroup-name)
7295          (gnus-group-is-exiting-p t)
7296          (gnus-group-is-exiting-without-update-p t)
7297          (quit-config (gnus-group-quit-config group)))
7298     (when (or no-questions
7299               gnus-expert-user
7300               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7301       (gnus-async-halt-prefetch)
7302       (run-hooks 'gnus-summary-prepare-exit-hook)
7303       (when (gnus-buffer-live-p gnus-article-buffer)
7304         (with-current-buffer gnus-article-buffer
7305           (gnus-article-stop-animations)
7306           (mm-destroy-parts gnus-article-mime-handles)
7307           ;; Set it to nil for safety reason.
7308           (setq gnus-article-mime-handle-alist nil)
7309           (setq gnus-article-mime-handles nil)))
7310       ;; If we have several article buffers, we kill them at exit.
7311       (unless gnus-single-article-buffer
7312         (gnus-kill-buffer gnus-article-buffer)
7313         (gnus-kill-buffer gnus-original-article-buffer)
7314         (setq gnus-article-current nil))
7315       ;; Return to the group buffer.
7316       (gnus-configure-windows 'group 'force)
7317       (if (not gnus-kill-summary-on-exit)
7318           (gnus-deaden-summary)
7319         (gnus-close-group group)
7320         (gnus-kill-buffer gnus-summary-buffer))
7321       (unless gnus-single-article-buffer
7322         (setq gnus-article-current nil))
7323       (when gnus-use-trees
7324         (gnus-tree-close group))
7325       (gnus-async-prefetch-remove-group group)
7326       (when (get-buffer gnus-article-buffer)
7327         (bury-buffer gnus-article-buffer))
7328       ;; Clear the current group name.
7329       (setq gnus-newsgroup-name nil)
7330       (unless (gnus-ephemeral-group-p group)
7331         (gnus-group-update-group group nil t))
7332       (when (equal (gnus-group-group-name) group)
7333         (gnus-group-next-unread-group 1))
7334       (when quit-config
7335         (gnus-handle-ephemeral-exit quit-config)))))
7336
7337 (defun gnus-handle-ephemeral-exit (quit-config)
7338   "Handle movement when leaving an ephemeral group.
7339 The state which existed when entering the ephemeral is reset."
7340   (if (not (buffer-name (car quit-config)))
7341       (gnus-configure-windows 'group 'force)
7342     (set-buffer (car quit-config))
7343     (unless (eq (cdr quit-config) 'group)
7344       (setq gnus-current-select-method
7345             (gnus-find-method-for-group gnus-newsgroup-name)))
7346     (cond ((eq major-mode 'gnus-summary-mode)
7347            (gnus-set-global-variables))
7348           ((eq major-mode 'gnus-article-mode)
7349            (save-current-buffer
7350              ;; The `gnus-summary-buffer' variable may point
7351              ;; to the old summary buffer when using a single
7352              ;; article buffer.
7353              (unless (gnus-buffer-live-p gnus-summary-buffer)
7354                (set-buffer gnus-group-buffer))
7355              (set-buffer gnus-summary-buffer)
7356              (gnus-set-global-variables))))
7357     (if (or (eq (cdr quit-config) 'article)
7358             (eq (cdr quit-config) 'pick))
7359         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7360             (gnus-configure-windows 'pick 'force)
7361           (gnus-configure-windows (cdr quit-config) 'force))
7362       (gnus-configure-windows (cdr quit-config) 'force))
7363     (when (eq major-mode 'gnus-summary-mode)
7364       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7365                                                      next-unread-noselect))
7366           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7367                                   'next-noselect)
7368                               (gnus-summary-next-subject 1 nil t))
7369                              ((eq gnus-auto-select-on-ephemeral-exit
7370                                   'next-unread-noselect)
7371                               (gnus-summary-next-subject 1 t t))))
7372             ;; Hide the article buffer which displays the article different
7373             ;; from the one that the cursor points to in the summary buffer.
7374             (gnus-configure-windows 'summary 'force))
7375         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7376                (gnus-summary-next-subject 1))
7377               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7378                (gnus-summary-next-subject 1 t))))
7379       (gnus-summary-recenter)
7380       (gnus-summary-position-point))))
7381
7382 ;;; Dead summaries.
7383
7384 (defvar gnus-dead-summary-mode-map
7385   (let ((map (make-keymap)))
7386     (suppress-keymap map)
7387     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7388     (dolist (key '("\C-d" "\r" "\177" [delete]))
7389       (define-key map key 'gnus-summary-wake-up-the-dead))
7390     (dolist (key '("q" "Q"))
7391       (define-key map key 'bury-buffer))
7392     map))
7393
7394 (define-minor-mode gnus-dead-summary-mode
7395   "Minor mode for Gnus summary buffers."
7396   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7397   (unless (derived-mode-p 'gnus-summary-mode)
7398     (setq gnus-dead-summary-mode nil)))
7399
7400 (defun gnus-deaden-summary ()
7401   "Make the current summary buffer into a dead summary buffer."
7402   ;; Kill any previous dead summary buffer.
7403   (when (and gnus-dead-summary
7404              (buffer-name gnus-dead-summary))
7405     (with-current-buffer gnus-dead-summary
7406       (when gnus-dead-summary-mode
7407         (kill-buffer (current-buffer)))))
7408   ;; Make this the current dead summary.
7409   (setq gnus-dead-summary (current-buffer))
7410   (gnus-dead-summary-mode 1)
7411   (let ((name (buffer-name)))
7412     (when (string-match "Summary" name)
7413       (rename-buffer
7414        (concat (substring name 0 (match-beginning 0)) "Dead "
7415                (substring name (match-beginning 0)))
7416        t)
7417       (bury-buffer))))
7418
7419 (defun gnus-kill-or-deaden-summary (buffer)
7420   "Kill or deaden the summary BUFFER."
7421   (save-excursion
7422     (when (and (buffer-name buffer)
7423                (not gnus-single-article-buffer))
7424       (with-current-buffer buffer
7425         (gnus-kill-buffer gnus-article-buffer)
7426         (gnus-kill-buffer gnus-original-article-buffer)))
7427     (cond
7428      ;; Kill the buffer.
7429      (gnus-kill-summary-on-exit
7430       (when (and gnus-use-trees
7431                  (gnus-buffer-exists-p buffer))
7432         (with-current-buffer buffer
7433           (gnus-tree-close gnus-newsgroup-name)))
7434       (gnus-kill-buffer buffer))
7435      ;; Deaden the buffer.
7436      ((gnus-buffer-exists-p buffer)
7437       (with-current-buffer buffer
7438         (gnus-deaden-summary))))))
7439
7440 (defun gnus-summary-wake-up-the-dead (&rest args)
7441   "Wake up the dead summary buffer."
7442   (interactive)
7443   (gnus-dead-summary-mode -1)
7444   (let ((name (buffer-name)))
7445     (when (string-match "Dead " name)
7446       (rename-buffer
7447        (concat (substring name 0 (match-beginning 0))
7448                (substring name (match-end 0)))
7449        t)))
7450   (gnus-message 3 "This dead summary is now alive again"))
7451
7452 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7453 (defun gnus-summary-describe-group (&optional force)
7454   "Describe the current newsgroup."
7455   (interactive "P")
7456   (gnus-group-describe-group force gnus-newsgroup-name))
7457
7458 (defun gnus-summary-describe-briefly ()
7459   "Describe summary mode commands briefly."
7460   (interactive)
7461   (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")))
7462
7463 ;; Walking around group mode buffer from summary mode.
7464
7465 (defun gnus-summary-next-group (&optional no-article target-group backward)
7466   "Exit current newsgroup and then select next unread newsgroup.
7467 If prefix argument NO-ARTICLE is non-nil, no article is selected
7468 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7469 previous group instead."
7470   (interactive "P")
7471   ;; Stop pre-fetching.
7472   (gnus-async-halt-prefetch)
7473   (let ((current-group gnus-newsgroup-name)
7474         (current-buffer (current-buffer))
7475         entered)
7476     ;; First we semi-exit this group to update Xrefs and all variables.
7477     ;; We can't do a real exit, because the window conf must remain
7478     ;; the same in case the user is prompted for info, and we don't
7479     ;; want the window conf to change before that...
7480     (gnus-summary-exit t)
7481     (while (not entered)
7482       ;; Then we find what group we are supposed to enter.
7483       (set-buffer gnus-group-buffer)
7484       (gnus-group-jump-to-group current-group)
7485       (setq target-group
7486             (or target-group
7487                 (if (eq gnus-keep-same-level 'best)
7488                     (gnus-summary-best-group gnus-newsgroup-name)
7489                   (gnus-summary-search-group backward gnus-keep-same-level))))
7490       (if (not target-group)
7491           ;; There are no further groups, so we return to the group
7492           ;; buffer.
7493           (progn
7494             (gnus-message 5 "Returning to the group buffer")
7495             (setq entered t)
7496             (when (gnus-buffer-live-p current-buffer)
7497               (set-buffer current-buffer)
7498               (gnus-summary-exit))
7499             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7500         ;; We try to enter the target group.
7501         (gnus-group-jump-to-group target-group)
7502         (let ((unreads (gnus-group-group-unread)))
7503           (if (and (or (eq t unreads)
7504                        (and unreads (not (zerop unreads))))
7505                    (gnus-summary-read-group
7506                     target-group nil no-article
7507                     (and (buffer-name current-buffer) current-buffer)
7508                     nil backward))
7509               (setq entered t)
7510             (setq current-group target-group
7511                   target-group nil)))))))
7512
7513 (defun gnus-summary-prev-group (&optional no-article)
7514   "Exit current newsgroup and then select previous unread newsgroup.
7515 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7516   (interactive "P")
7517   (gnus-summary-next-group no-article nil t))
7518
7519 ;; Walking around summary lines.
7520
7521 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7522   "Go to the first subject satisfying any non-nil constraint.
7523 If UNREAD is non-nil, the article should be unread.
7524 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7525 If UNSEEN is non-nil, the article should be unseen as well as unread.
7526 Returns the article selected or nil if there are no matching articles."
7527   (interactive "P")
7528   (cond
7529    ;; Empty summary.
7530    ((null gnus-newsgroup-data)
7531     (gnus-message 3 "No articles in the group")
7532     nil)
7533    ;; Pick the first article.
7534    ((not (or unread undownloaded unseen))
7535     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7536     (gnus-data-number (car gnus-newsgroup-data)))
7537    ;; Find the first unread article.
7538    (t
7539     (let ((data gnus-newsgroup-data))
7540       (while (and data
7541                   (let ((num (gnus-data-number (car data))))
7542                     (or (memq num gnus-newsgroup-unfetched)
7543                         (not (or (and unread
7544                                       (memq num gnus-newsgroup-unreads))
7545                                  (and undownloaded
7546                                       (memq num gnus-newsgroup-undownloaded))
7547                                  (and unseen
7548                                       (memq num gnus-newsgroup-unseen)
7549                                       (memq num gnus-newsgroup-unreads)))))))
7550         (setq data (cdr data)))
7551       (prog1
7552           (if data
7553               (progn
7554                 (goto-char (gnus-data-pos (car data)))
7555                 (gnus-data-number (car data)))
7556             (gnus-message 3 "No more%s articles"
7557                           (let* ((r (when unread " unread"))
7558                                  (d (when undownloaded " undownloaded"))
7559                                  (s (when unseen " unseen"))
7560                                  (l (delq nil (list r d s))))
7561                             (cond ((= 3 (length l))
7562                                    (concat r "," d ", or" s))
7563                                   ((= 2 (length l))
7564                                    (concat (car l) ", or" (cadr l)))
7565                                   ((= 1 (length l))
7566                                    (car l))
7567                                   (t
7568                                    ""))))
7569             nil
7570             )
7571         (gnus-summary-position-point))))))
7572
7573 (defun gnus-summary-next-subject (n &optional unread dont-display)
7574   "Go to next N'th summary line.
7575 If N is negative, go to the previous N'th subject line.
7576 If UNREAD is non-nil, only unread articles are selected.
7577 The difference between N and the actual number of steps taken is
7578 returned."
7579   (interactive "p")
7580   (let ((backward (< n 0))
7581         (n (abs n)))
7582     (while (and (> n 0)
7583                 (if backward
7584                     (gnus-summary-find-prev unread)
7585                   (gnus-summary-find-next unread)))
7586       (unless (zerop (setq n (1- n)))
7587         (gnus-summary-show-thread)))
7588     (when (/= 0 n)
7589       (gnus-message 7 "No more%s articles"
7590                     (if unread " unread" "")))
7591     (unless dont-display
7592       (gnus-summary-recenter)
7593       (gnus-summary-position-point))
7594     n))
7595
7596 (defun gnus-summary-next-unread-subject (n)
7597   "Go to next N'th unread summary line."
7598   (interactive "p")
7599   (gnus-summary-next-subject n t))
7600
7601 (defun gnus-summary-prev-subject (n &optional unread)
7602   "Go to previous N'th summary line.
7603 If optional argument UNREAD is non-nil, only unread article is selected."
7604   (interactive "p")
7605   (gnus-summary-next-subject (- n) unread))
7606
7607 (defun gnus-summary-prev-unread-subject (n)
7608   "Go to previous N'th unread summary line."
7609   (interactive "p")
7610   (gnus-summary-next-subject (- n) t))
7611
7612 (defun gnus-summary-goto-subjects (articles)
7613   "Insert the subject header for ARTICLES in the current buffer."
7614   (save-excursion
7615     (dolist (article articles)
7616       (gnus-summary-goto-subject article t)))
7617   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7618   (gnus-summary-position-point))
7619
7620 (defun gnus-summary-goto-subject (article &optional force silent)
7621   "Go to the subject line of ARTICLE.
7622 If FORCE, also allow jumping to articles not currently shown."
7623   (interactive "nArticle number: ")
7624   (unless (numberp article)
7625     (error "Article %s is not a number" article))
7626   (let ((b (point))
7627         (data (gnus-data-find article)))
7628     ;; We read in the article if we have to.
7629     (and (not data)
7630          force
7631          (gnus-summary-insert-subject
7632           article
7633           (if (or (numberp force) (vectorp force)) force)
7634           t)
7635          (setq data (gnus-data-find article)))
7636     (goto-char b)
7637     (if (not data)
7638         (progn
7639           (unless silent
7640             (gnus-message 3 "Can't find article %d" article))
7641           nil)
7642       (let ((pt (gnus-data-pos data)))
7643         (goto-char pt)
7644         (gnus-summary-set-article-display-arrow pt))
7645       (gnus-summary-position-point)
7646       article)))
7647
7648 ;; Walking around summary lines with displaying articles.
7649
7650 (defun gnus-summary-expand-window (&optional arg)
7651   "Make the summary buffer take up the entire Emacs frame.
7652 Given a prefix, will force an `article' buffer configuration."
7653   (interactive "P")
7654   (if arg
7655       (gnus-configure-windows 'article 'force)
7656     (gnus-configure-windows 'summary 'force)))
7657
7658 (defun gnus-summary-display-article (article &optional all-header)
7659   "Display ARTICLE in article buffer."
7660   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7661                (with-current-buffer gnus-article-buffer
7662                  (eq major-mode 'gnus-article-mode)))
7663     (gnus-article-setup-buffer))
7664   (gnus-set-global-variables)
7665   (with-current-buffer gnus-article-buffer
7666     (setq gnus-article-charset gnus-newsgroup-charset)
7667     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7668     (mm-enable-multibyte))
7669   (if (null article)
7670       nil
7671     (prog1
7672         (if gnus-summary-display-article-function
7673             (funcall gnus-summary-display-article-function article all-header)
7674           (gnus-article-prepare article all-header))
7675       (gnus-run-hooks 'gnus-select-article-hook)
7676       (when (and gnus-current-article
7677                  (not (zerop gnus-current-article)))
7678         (gnus-summary-goto-subject gnus-current-article))
7679       (gnus-summary-recenter)
7680       (when (and gnus-use-trees gnus-show-threads)
7681         (gnus-possibly-generate-tree article)
7682         (gnus-highlight-selected-tree article))
7683       ;; Successfully display article.
7684       (gnus-article-set-window-start
7685        (cdr (assq article gnus-newsgroup-bookmarks))))))
7686
7687 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7688   "Select the current article.
7689 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7690 non-nil, the article will be re-fetched even if it already present in
7691 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7692 be displayed."
7693   ;; Make sure we are in the summary buffer to work around bbdb bug.
7694   (unless (eq major-mode 'gnus-summary-mode)
7695     (set-buffer gnus-summary-buffer))
7696   (let ((article (or article (gnus-summary-article-number)))
7697         (all-headers (not (not all-headers))) ;Must be t or nil.
7698         gnus-summary-display-article-function)
7699     (and (not pseudo)
7700          (gnus-summary-article-pseudo-p article)
7701          (error "This is a pseudo-article"))
7702     (with-current-buffer gnus-summary-buffer
7703       (if (or (and gnus-single-article-buffer
7704                    (or (null gnus-current-article)
7705                        (null gnus-article-current)
7706                        (null (get-buffer gnus-article-buffer))
7707                        (not (eq article (cdr gnus-article-current)))
7708                        (not (equal (car gnus-article-current)
7709                                    gnus-newsgroup-name))
7710                        (not (get-buffer gnus-original-article-buffer))))
7711               (and (not gnus-single-article-buffer)
7712                    (or (null gnus-current-article)
7713                        (not (get-buffer gnus-original-article-buffer))
7714                        (not (eq gnus-current-article article))))
7715               force)
7716           ;; The requested article is different from the current article.
7717           (progn
7718             (gnus-summary-display-article article all-headers)
7719             (when (gnus-buffer-live-p gnus-article-buffer)
7720               (with-current-buffer gnus-article-buffer
7721                 (if (not gnus-article-decoded-p) ;; a local variable
7722                     (mm-disable-multibyte))))
7723             (gnus-article-set-window-start
7724              (cdr (assq article gnus-newsgroup-bookmarks)))
7725             article)
7726         'old))))
7727
7728 (defun gnus-summary-force-verify-and-decrypt ()
7729   "Display buttons for signed/encrypted parts and verify/decrypt them."
7730   (interactive)
7731   (let ((mm-verify-option 'known)
7732         (mm-decrypt-option 'known)
7733         (gnus-article-emulate-mime t)
7734         (gnus-buttonized-mime-types (append (list "multipart/signed"
7735                                                   "multipart/encrypted")
7736                                             gnus-buttonized-mime-types)))
7737     (gnus-summary-select-article nil 'force)))
7738
7739 (defun gnus-summary-set-current-mark (&optional current-mark)
7740   "Obsolete function."
7741   nil)
7742
7743 (defun gnus-summary-next-article (&optional unread subject backward push)
7744   "Select the next article.
7745 If UNREAD, only unread articles are selected.
7746 If SUBJECT, only articles with SUBJECT are selected.
7747 If BACKWARD, the previous article is selected instead of the next."
7748   (interactive "P")
7749   ;; Make sure we are in the summary buffer.
7750   (unless (eq major-mode 'gnus-summary-mode)
7751     (set-buffer gnus-summary-buffer))
7752   (cond
7753    ;; Is there such an article?
7754    ((and (gnus-summary-search-forward unread subject backward)
7755          (or (gnus-summary-display-article (gnus-summary-article-number))
7756              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7757     (gnus-summary-position-point))
7758    ;; If not, we try the first unread, if that is wanted.
7759    ((and subject
7760          gnus-auto-select-same
7761          (gnus-summary-first-unread-article))
7762     (gnus-summary-position-point)
7763     (gnus-message 6 "Wrapped"))
7764    ;; Try to get next/previous article not displayed in this group.
7765    ((and gnus-auto-extend-newsgroup
7766          (not unread) (not subject))
7767     (gnus-summary-goto-article
7768      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7769      nil (count-lines (point-min) (point))))
7770    ;; Go to next/previous group.
7771    (t
7772     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7773       (gnus-summary-jump-to-group gnus-newsgroup-name))
7774     (let ((cmd (if (featurep 'xemacs)
7775                    last-command-char
7776                  last-command-event))
7777           (point
7778            (with-current-buffer gnus-group-buffer
7779              (point)))
7780           (current-summary (current-buffer))
7781           (group
7782            (if (eq gnus-keep-same-level 'best)
7783                (gnus-summary-best-group gnus-newsgroup-name)
7784              (gnus-summary-search-group backward gnus-keep-same-level))))
7785       ;; Select next unread newsgroup automagically.
7786       (cond
7787        ((or (not gnus-auto-select-next)
7788             (not cmd))
7789         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7790        ((or (eq gnus-auto-select-next 'quietly)
7791             (and (eq gnus-auto-select-next 'slightly-quietly)
7792                  push)
7793             (and (eq gnus-auto-select-next 'almost-quietly)
7794                  (gnus-summary-last-article-p)))
7795         ;; Select quietly.
7796         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7797             (gnus-summary-exit)
7798           (gnus-message 7 "No more%s articles (%s)..."
7799                         (if unread " unread" "")
7800                         (if group (concat "selecting " group)
7801                           "exiting"))
7802           (gnus-summary-next-group nil group backward)))
7803        (t
7804         (when (gnus-key-press-event-p last-input-event)
7805           ;; Somehow or other, we may now have selected a different
7806           ;; window.  Make point go back to the summary buffer.
7807           (when (eq current-summary (current-buffer))
7808             ;; FIXME: This burps when get-buffer-window returns nil.
7809             (select-window (get-buffer-window current-summary 0)))
7810           (gnus-summary-walk-group-buffer
7811            gnus-newsgroup-name cmd unread backward point))))))))
7812
7813 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7814   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7815                       (?\C-p (gnus-group-prev-unread-group 1))))
7816         (cursor-in-echo-area t)
7817         keve key group ended prompt)
7818     (with-current-buffer gnus-group-buffer
7819       (goto-char start)
7820       (setq group
7821             (if (eq gnus-keep-same-level 'best)
7822                 (gnus-summary-best-group gnus-newsgroup-name)
7823               (gnus-summary-search-group backward gnus-keep-same-level))))
7824     (while (not ended)
7825       (setq prompt
7826             (format
7827              "No more%s articles%s " (if unread " unread" "")
7828              (if (and group
7829                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7830                  (format " (Type %s for %s [%s])"
7831                          (single-key-description cmd)
7832                          (gnus-group-decoded-name group)
7833                          (gnus-group-unread group))
7834                (format " (Type %s to exit %s)"
7835                        (single-key-description cmd)
7836                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7837       ;; Confirm auto selection.
7838       (setq key (car (setq keve (gnus-read-event-char prompt)))
7839             ended t)
7840       (cond
7841        ((assq key keystrokes)
7842         (let ((obuf (current-buffer)))
7843           (switch-to-buffer gnus-group-buffer)
7844           (when group
7845             (gnus-group-jump-to-group group))
7846           (eval (cadr (assq key keystrokes)))
7847           (setq group (gnus-group-group-name))
7848           (switch-to-buffer obuf))
7849         (setq ended nil))
7850        ((equal key cmd)
7851         (if (or (not group)
7852                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7853             (gnus-summary-exit)
7854           (gnus-summary-next-group nil group backward)))
7855        (t
7856         (push (cdr keve) unread-command-events))))))
7857
7858 (defun gnus-summary-next-unread-article ()
7859   "Select unread article after current one."
7860   (interactive)
7861   (gnus-summary-next-article
7862    (or (not (eq gnus-summary-goto-unread 'never))
7863        (gnus-summary-last-article-p (gnus-summary-article-number)))
7864    (and gnus-auto-select-same
7865         (gnus-summary-article-subject))))
7866
7867 (defun gnus-summary-prev-article (&optional unread subject)
7868   "Select the article before the current one.
7869 If UNREAD is non-nil, only unread articles are selected."
7870   (interactive "P")
7871   (gnus-summary-next-article unread subject t))
7872
7873 (defun gnus-summary-prev-unread-article ()
7874   "Select unread article before current one."
7875   (interactive)
7876   (gnus-summary-prev-article
7877    (or (not (eq gnus-summary-goto-unread 'never))
7878        (gnus-summary-first-article-p (gnus-summary-article-number)))
7879    (and gnus-auto-select-same
7880         (gnus-summary-article-subject))))
7881
7882 (defun gnus-summary-next-page (&optional lines circular stop)
7883   "Show next page of the selected article.
7884 If at the end of the current article, select the next article.
7885 LINES says how many lines should be scrolled up.
7886
7887 If CIRCULAR is non-nil, go to the start of the article instead of
7888 selecting the next article when reaching the end of the current
7889 article.
7890
7891 If STOP is non-nil, just stop when reaching the end of the message.
7892
7893 Also see the variable `gnus-article-skip-boring'."
7894   (interactive "P")
7895   (setq gnus-summary-buffer (current-buffer))
7896   (gnus-set-global-variables)
7897   (let ((article (gnus-summary-article-number))
7898         (article-window (get-buffer-window gnus-article-buffer t))
7899         endp)
7900     ;; If the buffer is empty, we have no article.
7901     (unless article
7902       (error "No article to select"))
7903     (gnus-configure-windows 'article)
7904     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7905         (if (and (eq gnus-summary-goto-unread 'never)
7906                  (not (gnus-summary-last-article-p article)))
7907             (gnus-summary-next-article)
7908           (gnus-summary-next-unread-article))
7909       (if (or (null gnus-current-article)
7910               (null gnus-article-current)
7911               (/= article (cdr gnus-article-current))
7912               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7913           ;; Selected subject is different from current article's.
7914           (gnus-summary-display-article article)
7915         (when article-window
7916           (gnus-eval-in-buffer-window gnus-article-buffer
7917             (setq endp (or (gnus-article-next-page lines)
7918                            (gnus-article-only-boring-p))))
7919           (when endp
7920             (cond ((or stop gnus-summary-stop-at-end-of-message)
7921                    (gnus-message 3 "End of message"))
7922                   (circular
7923                    (gnus-summary-beginning-of-article))
7924                   (lines
7925                    (gnus-message 3 "End of message"))
7926                   ((null lines)
7927                    (if (and (eq gnus-summary-goto-unread 'never)
7928                             (not (gnus-summary-last-article-p article)))
7929                        (gnus-summary-next-article)
7930                      (gnus-summary-next-unread-article))))))))
7931     (gnus-summary-recenter)
7932     (gnus-summary-position-point)))
7933
7934 (defun gnus-summary-prev-page (&optional lines move)
7935   "Show previous page of selected article.
7936 Argument LINES specifies lines to be scrolled down.
7937 If MOVE, move to the previous unread article if point is at
7938 the beginning of the buffer."
7939   (interactive "P")
7940   (let ((article (gnus-summary-article-number))
7941         (article-window (get-buffer-window gnus-article-buffer t))
7942         endp)
7943     (gnus-configure-windows 'article)
7944     (if (or (null gnus-current-article)
7945             (null gnus-article-current)
7946             (/= article (cdr gnus-article-current))
7947             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7948         ;; Selected subject is different from current article's.
7949         (gnus-summary-display-article article)
7950       (gnus-summary-recenter)
7951       (when article-window
7952         (gnus-eval-in-buffer-window gnus-article-buffer
7953           (setq endp (gnus-article-prev-page lines)))
7954         (when (and move endp)
7955           (cond (lines
7956                  (gnus-message 3 "Beginning of message"))
7957                 ((null lines)
7958                  (if (and (eq gnus-summary-goto-unread 'never)
7959                           (not (gnus-summary-first-article-p article)))
7960                      (gnus-summary-prev-article)
7961                    (gnus-summary-prev-unread-article))))))))
7962   (gnus-summary-position-point))
7963
7964 (defun gnus-summary-prev-page-or-article (&optional lines)
7965   "Show previous page of selected article.
7966 Argument LINES specifies lines to be scrolled down.
7967 If at the beginning of the article, go to the next article."
7968   (interactive "P")
7969   (gnus-summary-prev-page lines t))
7970
7971 (defun gnus-summary-scroll-up (lines)
7972   "Scroll up (or down) one line current article.
7973 Argument LINES specifies lines to be scrolled up (or down if negative).
7974 If no article is selected, then the current article will be selected first."
7975   (interactive "p")
7976   (gnus-configure-windows 'article)
7977   (gnus-summary-show-thread)
7978   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7979     (gnus-eval-in-buffer-window gnus-article-buffer
7980       (cond ((> lines 0)
7981              (when (gnus-article-next-page lines)
7982                (gnus-message 3 "End of message")))
7983             ((< lines 0)
7984              (gnus-article-prev-page (- lines))))))
7985   (gnus-summary-recenter)
7986   (gnus-summary-position-point))
7987
7988 (defun gnus-summary-scroll-down (lines)
7989   "Scroll down (or up) one line current article.
7990 Argument LINES specifies lines to be scrolled down (or up if negative).
7991 If no article is selected, then the current article will be selected first."
7992   (interactive "p")
7993   (gnus-summary-scroll-up (- lines)))
7994
7995 (defun gnus-summary-next-same-subject ()
7996   "Select next article which has the same subject as current one."
7997   (interactive)
7998   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7999
8000 (defun gnus-summary-prev-same-subject ()
8001   "Select previous article which has the same subject as current one."
8002   (interactive)
8003   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8004
8005 (defun gnus-summary-next-unread-same-subject ()
8006   "Select next unread article which has the same subject as current one."
8007   (interactive)
8008   (gnus-summary-next-article t (gnus-summary-article-subject)))
8009
8010 (defun gnus-summary-prev-unread-same-subject ()
8011   "Select previous unread article which has the same subject as current one."
8012   (interactive)
8013   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8014
8015 (defun gnus-summary-first-unread-article ()
8016   "Select the first unread article.
8017 Return nil if there are no unread articles."
8018   (interactive)
8019   (prog1
8020       (when (gnus-summary-first-subject t)
8021         (gnus-summary-show-thread)
8022         (gnus-summary-first-subject t)
8023         (gnus-summary-display-article (gnus-summary-article-number)))
8024     (gnus-summary-position-point)))
8025
8026 (defun gnus-summary-first-unread-subject ()
8027   "Place the point on the subject line of the first unread article.
8028 Return nil if there are no unread articles."
8029   (interactive)
8030   (prog1
8031       (when (gnus-summary-first-subject t)
8032         (gnus-summary-show-thread)
8033         (gnus-summary-first-subject t))
8034     (gnus-summary-position-point)))
8035
8036 (defun gnus-summary-first-unseen-subject ()
8037   "Place the point on the subject line of the first unseen article.
8038 Return nil if there are no unseen articles."
8039   (interactive)
8040   (prog1
8041       (when (gnus-summary-first-subject nil nil t)
8042         (gnus-summary-show-thread)
8043         (gnus-summary-first-subject nil nil t))
8044     (gnus-summary-position-point)))
8045
8046 (defun gnus-summary-first-unseen-or-unread-subject ()
8047   "Place the point on the subject line of the first unseen and unread article.
8048 If all article have been seen, on the subject line of the first unread
8049 article."
8050   (interactive)
8051   (prog1
8052       (unless (when (gnus-summary-first-subject nil nil t)
8053                 (gnus-summary-show-thread)
8054                 (gnus-summary-first-subject nil nil t))
8055         (when (gnus-summary-first-subject t)
8056           (gnus-summary-show-thread)
8057           (gnus-summary-first-subject t)))
8058     (gnus-summary-position-point)))
8059
8060 (defun gnus-summary-first-article ()
8061   "Select the first article.
8062 Return nil if there are no articles."
8063   (interactive)
8064   (prog1
8065       (when (gnus-summary-first-subject)
8066         (gnus-summary-show-thread)
8067         (gnus-summary-first-subject)
8068         (gnus-summary-display-article (gnus-summary-article-number)))
8069     (gnus-summary-position-point)))
8070
8071 (defun gnus-summary-best-unread-article (&optional arg)
8072   "Select the unread article with the highest score.
8073 If given a prefix argument, select the next unread article that has a
8074 score higher than the default score."
8075   (interactive "P")
8076   (let ((article (if arg
8077                      (gnus-summary-better-unread-subject)
8078                    (gnus-summary-best-unread-subject))))
8079     (if article
8080         (gnus-summary-goto-article article)
8081       (error "No unread articles"))))
8082
8083 (defun gnus-summary-best-unread-subject ()
8084   "Select the unread subject with the highest score."
8085   (interactive)
8086   (let ((best -1000000)
8087         (data gnus-newsgroup-data)
8088         article score)
8089     (while data
8090       (and (gnus-data-unread-p (car data))
8091            (> (setq score
8092                     (gnus-summary-article-score (gnus-data-number (car data))))
8093               best)
8094            (setq best score
8095                  article (gnus-data-number (car data))))
8096       (setq data (cdr data)))
8097     (when article
8098       (gnus-summary-goto-subject article))
8099     (gnus-summary-position-point)
8100     article))
8101
8102 (defun gnus-summary-better-unread-subject ()
8103   "Select the first unread subject that has a score over the default score."
8104   (interactive)
8105   (let ((data gnus-newsgroup-data)
8106         article score)
8107     (while (and (setq article (gnus-data-number (car data)))
8108                 (or (gnus-data-read-p (car data))
8109                     (not (> (gnus-summary-article-score article)
8110                             gnus-summary-default-score))))
8111       (setq data (cdr data)))
8112     (when article
8113       (gnus-summary-goto-subject article))
8114     (gnus-summary-position-point)
8115     article))
8116
8117 (defun gnus-summary-last-subject ()
8118   "Go to the last displayed subject line in the group."
8119   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8120     (when article
8121       (gnus-summary-goto-subject article))))
8122
8123 (defun gnus-summary-goto-article (article &optional all-headers force)
8124   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8125 If ALL-HEADERS is non-nil, no header lines are hidden.
8126 If FORCE, go to the article even if it isn't displayed.  If FORCE
8127 is a number, it is the line the article is to be displayed on."
8128   (interactive
8129    (list
8130     (gnus-completing-read
8131      "Article number or Message-ID"
8132      (mapcar 'int-to-string gnus-newsgroup-limit))
8133     current-prefix-arg
8134     t))
8135   (prog1
8136       (if (and (stringp article)
8137                (string-match "@\\|%40" article))
8138           (gnus-summary-refer-article article)
8139         (when (stringp article)
8140           (setq article (string-to-number article)))
8141         (if (gnus-summary-goto-subject article force)
8142             (gnus-summary-display-article article all-headers)
8143           (gnus-message 4 "Couldn't go to article %s" article) nil))
8144     (gnus-summary-position-point)))
8145
8146 (defun gnus-summary-goto-last-article ()
8147   "Go to the previously read article."
8148   (interactive)
8149   (prog1
8150       (when gnus-last-article
8151         (gnus-summary-goto-article gnus-last-article nil t))
8152     (gnus-summary-position-point)))
8153
8154 (defun gnus-summary-pop-article (number)
8155   "Pop one article off the history and go to the previous.
8156 NUMBER articles will be popped off."
8157   (interactive "p")
8158   (let (to)
8159     (setq gnus-newsgroup-history
8160           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8161     (if to
8162         (gnus-summary-goto-article (car to) nil t)
8163       (error "Article history empty")))
8164   (gnus-summary-position-point))
8165
8166 ;; Summary commands and functions for limiting the summary buffer.
8167
8168 (defun gnus-summary-limit-to-articles (n)
8169   "Limit the summary buffer to the next N articles.
8170 If not given a prefix, use the process marked articles instead."
8171   (interactive "P")
8172   (prog1
8173       (let ((articles (gnus-summary-work-articles n)))
8174         (setq gnus-newsgroup-processable nil)
8175         (gnus-summary-limit articles))
8176     (gnus-summary-position-point)))
8177
8178 (defun gnus-summary-pop-limit (&optional total)
8179   "Restore the previous limit.
8180 If given a prefix, remove all limits."
8181   (interactive "P")
8182   (when total
8183     (setq gnus-newsgroup-limits
8184           (list (mapcar (lambda (h) (mail-header-number h))
8185                         gnus-newsgroup-headers))))
8186   (unless gnus-newsgroup-limits
8187     (error "No limit to pop"))
8188   (prog1
8189       (gnus-summary-limit nil 'pop)
8190     (gnus-summary-position-point)))
8191
8192 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8193   "Limit the summary buffer to articles that have subjects that match a regexp.
8194 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8195   (interactive
8196    (list (read-string (if current-prefix-arg
8197                           "Exclude subject (regexp): "
8198                         "Limit to subject (regexp): "))
8199          nil current-prefix-arg))
8200   (unless header
8201     (setq header "subject"))
8202   (when (not (equal "" subject))
8203     (prog1
8204         (let ((articles (gnus-summary-find-matching
8205                          (or header "subject") subject 'all nil nil
8206                          not-matching)))
8207           (unless articles
8208             (error "Found no matches for \"%s\"" subject))
8209           (gnus-summary-limit articles))
8210       (gnus-summary-position-point))))
8211
8212 (defun gnus-summary-limit-to-author (from &optional not-matching)
8213   "Limit the summary buffer to articles that have authors that match a regexp.
8214 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8215   (interactive
8216    (list (read-string (if current-prefix-arg
8217                           "Exclude author (regexp): "
8218                         "Limit to author (regexp): "))
8219          current-prefix-arg))
8220   (gnus-summary-limit-to-subject from "from" not-matching))
8221
8222 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8223   "Limit the summary buffer to articles with the given RECIPIENT.
8224
8225 If NOT-MATCHING, exclude RECIPIENT.
8226
8227 To and Cc headers are checked.  You need to include them in
8228 `nnmail-extra-headers'."
8229   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8230   (interactive
8231    (list (read-string (format "%s recipient (regexp): "
8232                               (if current-prefix-arg "Exclude" "Limit to")))
8233          current-prefix-arg))
8234   (when (not (equal "" recipient))
8235     (prog1 (let* ((to
8236                    (if (memq 'To nnmail-extra-headers)
8237                        (gnus-summary-find-matching
8238                         (cons 'extra 'To) recipient 'all nil nil
8239                         not-matching)
8240                      (gnus-message
8241                       1 "`To' isn't present in `nnmail-extra-headers'")
8242                      (sit-for 1)
8243                      nil))
8244                   (cc
8245                    (if (memq 'Cc nnmail-extra-headers)
8246                        (gnus-summary-find-matching
8247                         (cons 'extra 'Cc) recipient 'all nil nil
8248                         not-matching)
8249                      (gnus-message
8250                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8251                      (sit-for 1)
8252                      nil))
8253                   (articles
8254                    (if not-matching
8255                        ;; We need the numbers that are in both lists:
8256                        (mapcar (lambda (a)
8257                                  (and (memq a to) a))
8258                                cc)
8259                      (nconc to cc))))
8260              (unless articles
8261                (error "Found no matches for \"%s\"" recipient))
8262              (gnus-summary-limit articles))
8263       (gnus-summary-position-point))))
8264
8265 (defun gnus-summary-limit-to-address (address &optional not-matching)
8266   "Limit the summary buffer to articles with the given ADDRESS.
8267
8268 If NOT-MATCHING, exclude ADDRESS.
8269
8270 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8271 in `nnmail-extra-headers'."
8272   (interactive
8273    (list (read-string (format "%s address (regexp): "
8274                               (if current-prefix-arg "Exclude" "Limit to")))
8275          current-prefix-arg))
8276   (when (not (equal "" address))
8277     (prog1 (let* ((to
8278                    (if (memq 'To nnmail-extra-headers)
8279                        (gnus-summary-find-matching
8280                         (cons 'extra 'To) address 'all nil nil
8281                         not-matching)
8282                      (gnus-message
8283                       1 "`To' isn't present in `nnmail-extra-headers'")
8284                      (sit-for 1)
8285                      t))
8286                   (cc
8287                    (if (memq 'Cc nnmail-extra-headers)
8288                        (gnus-summary-find-matching
8289                         (cons 'extra 'Cc) address 'all nil nil
8290                         not-matching)
8291                      (gnus-message
8292                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8293                      (sit-for 1)
8294                      t))
8295                   (from
8296                    (gnus-summary-find-matching "from" address
8297                                                'all nil nil not-matching))
8298                   (articles
8299                    (if not-matching
8300                        ;; We need the numbers that are in all lists:
8301                        (if (eq cc t)
8302                            (if (eq to t)
8303                                from
8304                              (mapcar (lambda (a) (car (memq a from))) to))
8305                          (if (eq to t)
8306                              (mapcar (lambda (a) (car (memq a from))) cc)
8307                            (mapcar (lambda (a) (car (memq a from)))
8308                                    (mapcar (lambda (a) (car (memq a to)))
8309                                            cc))))
8310                      (nconc (if (eq to t) nil to)
8311                             (if (eq cc t) nil cc)
8312                             from))))
8313              (unless articles
8314                (error "Found no matches for \"%s\"" address))
8315              (gnus-summary-limit articles))
8316       (gnus-summary-position-point))))
8317
8318 (defun gnus-summary-limit-strange-charsets-predicate (header)
8319   (when (fboundp 'char-charset)
8320     (let ((string (concat (mail-header-subject header)
8321                           (mail-header-from header)))
8322           charset found)
8323       (dotimes (i (1- (length string)))
8324         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8325         (when (string-match "unicode\\|big\\|japanese" charset)
8326           (setq found t)))
8327       found)))
8328
8329 (defun gnus-summary-limit-to-predicate (predicate)
8330   "Limit to articles where PREDICATE returns non-nil.
8331 PREDICATE will be called with the header structures of the
8332 articles."
8333   (let ((articles nil)
8334         (case-fold-search t))
8335     (dolist (header gnus-newsgroup-headers)
8336       (when (funcall predicate header)
8337         (push (mail-header-number header) articles)))
8338     (gnus-summary-limit (nreverse articles))))
8339
8340 (defun gnus-summary-limit-to-age (age &optional younger-p)
8341   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8342 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8343 articles that are younger than AGE days."
8344   (interactive
8345    (let ((younger current-prefix-arg)
8346          (days-got nil)
8347          days)
8348      (while (not days-got)
8349        (setq days (if younger
8350                       (read-string "Limit to articles younger than (in days, older when negative): ")
8351                     (read-string
8352                      "Limit to articles older than (in days, younger when negative): ")))
8353        (when (> (length days) 0)
8354          (setq days (read days)))
8355        (if (numberp days)
8356            (progn
8357              (setq days-got t)
8358              (when (< days 0)
8359                (setq younger (not younger))
8360                (setq days (* days -1))))
8361          (message "Please enter a number.")
8362          (sleep-for 1)))
8363      (list days younger)))
8364   (prog1
8365       (let ((data gnus-newsgroup-data)
8366             (cutoff (days-to-time age))
8367             articles d date is-younger)
8368         (while (setq d (pop data))
8369           (when (and (vectorp (gnus-data-header d))
8370                      (setq date (mail-header-date (gnus-data-header d))))
8371             (setq is-younger (time-less-p
8372                               (time-since (gnus-date-get-time date))
8373                               cutoff))
8374             (when (if younger-p
8375                       is-younger
8376                     (not is-younger))
8377               (push (gnus-data-number d) articles))))
8378         (gnus-summary-limit (nreverse articles)))
8379     (gnus-summary-position-point)))
8380
8381 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8382   "Limit the summary buffer to articles that match an 'extra' header."
8383   (interactive
8384    (let ((header
8385           (intern
8386            (gnus-completing-read
8387             (if current-prefix-arg
8388                 "Exclude extra header"
8389               "Limit extra header")
8390             (mapcar 'symbol-name gnus-extra-headers)
8391             t nil nil
8392             (symbol-name (car gnus-extra-headers))))))
8393      (list header
8394            (read-string (format "%s header %s (regexp): "
8395                                 (if current-prefix-arg "Exclude" "Limit to")
8396                                 header))
8397            current-prefix-arg)))
8398   (when (not (equal "" regexp))
8399     (prog1
8400         (let ((articles (gnus-summary-find-matching
8401                          (cons 'extra header) regexp 'all nil nil
8402                          not-matching)))
8403           (unless articles
8404             (error "Found no matches for \"%s\"" regexp))
8405           (gnus-summary-limit articles))
8406       (gnus-summary-position-point))))
8407
8408 (defun gnus-summary-limit-to-display-predicate ()
8409   "Limit the summary buffer to the predicated in the `display' group parameter."
8410   (interactive)
8411   (unless gnus-newsgroup-display
8412     (error "There is no `display' group parameter"))
8413   (let (articles)
8414     (dolist (gnus-number gnus-newsgroup-articles)
8415       (when (funcall gnus-newsgroup-display)
8416         (push gnus-number articles)))
8417     (gnus-summary-limit articles))
8418   (gnus-summary-position-point))
8419
8420 (defun gnus-summary-limit-to-unread (&optional all)
8421   "Limit the summary buffer to articles that are not marked as read.
8422 If ALL is non-nil, limit strictly to unread articles."
8423   (interactive "P")
8424   (if all
8425       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8426     (gnus-summary-limit-to-marks
8427      ;; Concat all the marks that say that an article is read and have
8428      ;; those removed.
8429      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8430            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8431            gnus-low-score-mark gnus-expirable-mark
8432            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8433            gnus-duplicate-mark)
8434      'reverse)))
8435
8436 (defun gnus-summary-limit-to-headers (match &optional reverse)
8437   "Limit the summary buffer to articles that have headers that match MATCH.
8438 If REVERSE (the prefix), limit to articles that don't match."
8439   (interactive "sMatch headers (regexp): \nP")
8440   (gnus-summary-limit-to-bodies match reverse t))
8441
8442 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8443   "Limit the summary buffer to articles that have bodies that match MATCH.
8444 If REVERSE (the prefix), limit to articles that don't match."
8445   (interactive "sMatch body (regexp): \nP")
8446   (let ((articles nil)
8447         (gnus-select-article-hook nil)  ;Disable hook.
8448         (gnus-article-prepare-hook nil)
8449         (gnus-use-article-prefetch nil)
8450         (gnus-keep-backlog nil)
8451         (gnus-break-pages nil)
8452         (gnus-summary-display-arrow nil)
8453         (gnus-updated-mode-lines nil)
8454         (gnus-auto-center-summary nil)
8455         (gnus-display-mime-function nil))
8456     (dolist (data gnus-newsgroup-data)
8457       (let (gnus-mark-article-hook)
8458         (gnus-summary-select-article t t nil (gnus-data-number data)))
8459       (with-current-buffer gnus-article-buffer
8460         (article-goto-body)
8461         (let* ((case-fold-search t)
8462                (found (if headersp
8463                           (re-search-backward match nil t)
8464                         (re-search-forward match nil t))))
8465           (when (or (and found
8466                          (not reverse))
8467                     (and (not found)
8468                          reverse))
8469             (push (gnus-data-number data) articles)))))
8470     (if (not articles)
8471         (message "No messages matched")
8472       (gnus-summary-limit articles)))
8473   (gnus-summary-position-point))
8474
8475 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8476   "Limit the summary buffer to articles that aren't part on any thread.
8477 If THREADSP (the prefix), limit to articles that are in threads."
8478   (interactive "P")
8479   (let ((articles nil)
8480         thread-articles
8481         threads)
8482     (dolist (thread gnus-newsgroup-threads)
8483       (if (stringp (car thread))
8484           (dolist (thread (cdr thread))
8485             (push thread threads))
8486         (push thread threads)))
8487     (dolist (thread threads)
8488       (setq thread-articles (gnus-articles-in-thread thread))
8489       (when (or (and threadsp
8490                      (> (length thread-articles) 1))
8491                 (and (not threadsp)
8492                      (= (length thread-articles) 1)))
8493         (setq articles (nconc thread-articles articles))))
8494     (if (not articles)
8495         (message "No messages matched")
8496       (gnus-summary-limit articles))
8497     (gnus-summary-position-point)))
8498
8499 (defun gnus-summary-limit-to-replied (&optional unreplied)
8500   "Limit the summary buffer to replied articles.
8501 If UNREPLIED (the prefix), limit to unreplied articles."
8502   (interactive "P")
8503   (if unreplied
8504       (gnus-summary-limit
8505        (gnus-set-difference gnus-newsgroup-articles
8506         gnus-newsgroup-replied))
8507     (gnus-summary-limit gnus-newsgroup-replied))
8508   (gnus-summary-position-point))
8509
8510 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8511   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8512 If REVERSE, limit the summary buffer to articles that are marked
8513 with MARKS.  MARKS can either be a string of marks or a list of marks.
8514 Returns how many articles were removed."
8515   (interactive "sMarks: ")
8516   (gnus-summary-limit-to-marks marks t))
8517
8518 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8519   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8520 If REVERSE (the prefix), limit the summary buffer to articles that are
8521 not marked with MARKS.  MARKS can either be a string of marks or a
8522 list of marks.
8523 Returns how many articles were removed."
8524   (interactive "sMarks: \nP")
8525   (prog1
8526       (let ((data gnus-newsgroup-data)
8527             (marks (if (listp marks) marks
8528                      (append marks nil))) ; Transform to list.
8529             articles)
8530         (while data
8531           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8532                   (memq (gnus-data-mark (car data)) marks))
8533             (push (gnus-data-number (car data)) articles))
8534           (setq data (cdr data)))
8535         (gnus-summary-limit articles))
8536     (gnus-summary-position-point)))
8537
8538 (defun gnus-summary-limit-to-score (score)
8539   "Limit to articles with score at or above SCORE."
8540   (interactive "NLimit to articles with score of at least: ")
8541   (let ((data gnus-newsgroup-data)
8542         articles)
8543     (while data
8544       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8545                 score)
8546         (push (gnus-data-number (car data)) articles))
8547       (setq data (cdr data)))
8548     (prog1
8549         (gnus-summary-limit articles)
8550       (gnus-summary-position-point))))
8551
8552 (defun gnus-summary-limit-to-unseen ()
8553   "Limit to unseen articles."
8554   (interactive)
8555   (prog1
8556       (gnus-summary-limit gnus-newsgroup-unseen)
8557     (gnus-summary-position-point)))
8558
8559 (defun gnus-summary-limit-include-thread (id)
8560   "Display all the hidden articles that is in the thread with ID in it.
8561 When called interactively, ID is the Message-ID of the current
8562 article."
8563   (interactive (list (mail-header-id (gnus-summary-article-header))))
8564   (let ((articles (gnus-articles-in-thread
8565                    (gnus-id-to-thread (gnus-root-id id))))
8566         ;;we REALLY want the whole thread---this prevents cut-threads
8567         ;;from removing the thread we want to include.
8568         (gnus-fetch-old-headers nil)
8569         (gnus-build-sparse-threads nil))
8570     (prog1
8571         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8572       (gnus-summary-limit-include-matching-articles
8573        "subject"
8574        (regexp-quote (gnus-simplify-subject-re
8575                       (mail-header-subject (gnus-id-to-header id)))))
8576       (gnus-summary-position-point))))
8577
8578 (defun gnus-summary-limit-include-matching-articles (header regexp)
8579   "Display all the hidden articles that have HEADERs that match REGEXP."
8580   (interactive (list (read-string "Match on header: ")
8581                      (read-string "Regexp: ")))
8582   (let ((articles (gnus-find-matching-articles header regexp)))
8583     (prog1
8584         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8585       (gnus-summary-position-point))))
8586
8587 (defun gnus-summary-insert-dormant-articles ()
8588   "Insert all the dormant articles for this group into the current buffer."
8589   (interactive)
8590   (let ((gnus-verbose (max 6 gnus-verbose)))
8591     (if (not gnus-newsgroup-dormant)
8592         (gnus-message 3 "No dormant articles for this group")
8593       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8594
8595 (defun gnus-summary-insert-ticked-articles ()
8596   "Insert ticked articles for this group into the current buffer."
8597   (interactive)
8598   (let ((gnus-verbose (max 6 gnus-verbose)))
8599     (if (not gnus-newsgroup-marked)
8600         (gnus-message 3 "No ticked articles for this group")
8601       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8602
8603 (defun gnus-summary-limit-include-dormant ()
8604   "Display all the hidden articles that are marked as dormant.
8605 Note that this command only works on a subset of the articles currently
8606 fetched for this group."
8607   (interactive)
8608   (unless gnus-newsgroup-dormant
8609     (error "There are no dormant articles in this group"))
8610   (prog1
8611       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8612     (gnus-summary-position-point)))
8613
8614 (defun gnus-summary-include-articles (articles)
8615   "Fetch the headers for ARTICLES and then display the summary lines."
8616   (let ((gnus-inhibit-demon t)
8617         (gnus-agent nil)
8618         (gnus-read-all-available-headers t))
8619     (setq gnus-newsgroup-headers
8620           (gnus-merge
8621            'list gnus-newsgroup-headers
8622            (gnus-fetch-headers articles nil t)
8623            'gnus-article-sort-by-number))
8624     (setq gnus-newsgroup-articles
8625           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8626     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8627
8628 (defun gnus-summary-limit-exclude-dormant ()
8629   "Hide all dormant articles."
8630   (interactive)
8631   (prog1
8632       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8633     (gnus-summary-position-point)))
8634
8635 (defun gnus-summary-limit-exclude-childless-dormant ()
8636   "Hide all dormant articles that have no children."
8637   (interactive)
8638   (let ((data (gnus-data-list t))
8639         articles d children)
8640     ;; Find all articles that are either not dormant or have
8641     ;; children.
8642     (while (setq d (pop data))
8643       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8644                 (and (setq children
8645                            (gnus-article-children (gnus-data-number d)))
8646                      (let (found)
8647                        (while children
8648                          (when (memq (car children) articles)
8649                            (setq children nil
8650                                  found t))
8651                          (pop children))
8652                        found)))
8653         (push (gnus-data-number d) articles)))
8654     ;; Do the limiting.
8655     (prog1
8656         (gnus-summary-limit articles)
8657       (gnus-summary-position-point))))
8658
8659 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8660   "Mark all unread excluded articles as read.
8661 If ALL, mark even excluded ticked and dormants as read."
8662   (interactive "P")
8663   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8664   (let ((articles (gnus-sorted-ndifference
8665                    (sort
8666                     (mapcar (lambda (h) (mail-header-number h))
8667                             gnus-newsgroup-headers)
8668                     '<)
8669                    gnus-newsgroup-limit))
8670         article)
8671     (setq gnus-newsgroup-unreads
8672           (gnus-sorted-intersection gnus-newsgroup-unreads
8673                                     gnus-newsgroup-limit))
8674     (if all
8675         (setq gnus-newsgroup-dormant nil
8676               gnus-newsgroup-marked nil
8677               gnus-newsgroup-reads
8678               (nconc
8679                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8680                gnus-newsgroup-reads))
8681       (while (setq article (pop articles))
8682         (unless (or (memq article gnus-newsgroup-dormant)
8683                     (memq article gnus-newsgroup-marked))
8684           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8685
8686 (defun gnus-summary-limit (articles &optional pop)
8687   (if pop
8688       ;; We pop the previous limit off the stack and use that.
8689       (setq articles (car gnus-newsgroup-limits)
8690             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8691     ;; We use the new limit, so we push the old limit on the stack.
8692     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8693   ;; Set the limit.
8694   (setq gnus-newsgroup-limit articles)
8695   (let ((total (length gnus-newsgroup-data))
8696         (data (gnus-data-find-list (gnus-summary-article-number)))
8697         (gnus-summary-mark-below nil)   ; Inhibit this.
8698         found)
8699     ;; This will do all the work of generating the new summary buffer
8700     ;; according to the new limit.
8701     (gnus-summary-prepare)
8702     ;; Hide any threads, possibly.
8703     (gnus-summary-maybe-hide-threads)
8704     ;; Try to return to the article you were at, or one in the
8705     ;; neighborhood.
8706     (when data
8707       ;; We try to find some article after the current one.
8708       (while data
8709         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8710           (setq data nil
8711                 found t))
8712         (setq data (cdr data))))
8713     (unless found
8714       ;; If there is no data, that means that we were after the last
8715       ;; article.  The same goes when we can't find any articles
8716       ;; after the current one.
8717       (goto-char (point-max))
8718       (gnus-summary-find-prev))
8719     (gnus-set-mode-line 'summary)
8720     ;; We return how many articles were removed from the summary
8721     ;; buffer as a result of the new limit.
8722     (- total (length gnus-newsgroup-data))))
8723
8724 (defsubst gnus-invisible-cut-children (threads)
8725   (let ((num 0))
8726     (while threads
8727       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8728         (incf num))
8729       (pop threads))
8730     (< num 2)))
8731
8732 (defsubst gnus-cut-thread (thread)
8733   "Go forwards in the thread until we find an article that we want to display."
8734   (when (or (eq gnus-fetch-old-headers 'some)
8735             (eq gnus-fetch-old-headers 'invisible)
8736             (numberp gnus-fetch-old-headers)
8737             (eq gnus-build-sparse-threads 'some)
8738             (eq gnus-build-sparse-threads 'more))
8739     ;; Deal with old-fetched headers and sparse threads.
8740     (while (and
8741             thread
8742             (or
8743              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8744              (gnus-summary-article-ancient-p
8745               (mail-header-number (car thread))))
8746             (if (or (<= (length (cdr thread)) 1)
8747                     (eq gnus-fetch-old-headers 'invisible))
8748                 (setq gnus-newsgroup-limit
8749                       (delq (mail-header-number (car thread))
8750                             gnus-newsgroup-limit)
8751                       thread (cadr thread))
8752               (when (gnus-invisible-cut-children (cdr thread))
8753                 (let ((th (cdr thread)))
8754                   (while th
8755                     (if (memq (mail-header-number (caar th))
8756                               gnus-newsgroup-limit)
8757                         (setq thread (car th)
8758                               th nil)
8759                       (setq th (cdr th))))))))))
8760   thread)
8761
8762 (defun gnus-cut-threads (threads)
8763   "Cut off all uninteresting articles from the beginning of THREADS."
8764   (when (or (eq gnus-fetch-old-headers 'some)
8765             (eq gnus-fetch-old-headers 'invisible)
8766             (numberp gnus-fetch-old-headers)
8767             (eq gnus-build-sparse-threads 'some)
8768             (eq gnus-build-sparse-threads 'more))
8769     (let ((th threads))
8770       (while th
8771         (setcar th (gnus-cut-thread (car th)))
8772         (setq th (cdr th)))))
8773   ;; Remove nixed out threads.
8774   (delq nil threads))
8775
8776 (defun gnus-summary-initial-limit (&optional show-if-empty)
8777   "Figure out what the initial limit is supposed to be on group entry.
8778 This entails weeding out unwanted dormants, low-scored articles,
8779 fetch-old-headers verbiage, and so on."
8780   ;; Most groups have nothing to remove.
8781   (unless (or gnus-inhibit-limiting
8782               (and (null gnus-newsgroup-dormant)
8783                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8784                    (not (eq gnus-fetch-old-headers 'some))
8785                    (not (numberp gnus-fetch-old-headers))
8786                    (not (eq gnus-fetch-old-headers 'invisible))
8787                    (null gnus-summary-expunge-below)
8788                    (not (eq gnus-build-sparse-threads 'some))
8789                    (not (eq gnus-build-sparse-threads 'more))
8790                    (null gnus-thread-expunge-below)))
8791     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8792     (setq gnus-newsgroup-limit nil)
8793     (mapatoms
8794      (lambda (node)
8795        (unless (car (symbol-value node))
8796          ;; These threads have no parents -- they are roots.
8797          (let ((nodes (cdr (symbol-value node)))
8798                thread)
8799            (while nodes
8800              (if (and gnus-thread-expunge-below
8801                       (< (gnus-thread-total-score (car nodes))
8802                          gnus-thread-expunge-below))
8803                  (gnus-expunge-thread (pop nodes))
8804                (setq thread (pop nodes))
8805                (gnus-summary-limit-children thread))))))
8806      gnus-newsgroup-dependencies)
8807     ;; If this limitation resulted in an empty group, we might
8808     ;; pop the previous limit and use it instead.
8809     (when (and (not gnus-newsgroup-limit)
8810                show-if-empty)
8811       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8812     gnus-newsgroup-limit))
8813
8814 (defun gnus-summary-limit-children (thread)
8815   "Return 1 if this subthread is visible and 0 if it is not."
8816   ;; First we get the number of visible children to this thread.  This
8817   ;; is done by recursing down the thread using this function, so this
8818   ;; will really go down to a leaf article first, before slowly
8819   ;; working its way up towards the root.
8820   (when thread
8821     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8822            (children
8823            (if (cdr thread)
8824                (apply '+ (mapcar 'gnus-summary-limit-children
8825                                  (cdr thread)))
8826              0))
8827            (number (mail-header-number (car thread)))
8828            score)
8829       (if (and
8830            (not (memq number gnus-newsgroup-marked))
8831            (or
8832             ;; If this article is dormant and has absolutely no visible
8833             ;; children, then this article isn't visible.
8834             (and (memq number gnus-newsgroup-dormant)
8835                  (zerop children))
8836             ;; If this is "fetch-old-headered" and there is no
8837             ;; visible children, then we don't want this article.
8838             (and (or (eq gnus-fetch-old-headers 'some)
8839                      (numberp gnus-fetch-old-headers))
8840                  (gnus-summary-article-ancient-p number)
8841                  (zerop children))
8842             ;; If this is "fetch-old-headered" and `invisible', then
8843             ;; we don't want this article.
8844             (and (eq gnus-fetch-old-headers 'invisible)
8845                  (gnus-summary-article-ancient-p number))
8846             ;; If this is a sparsely inserted article with no children,
8847             ;; we don't want it.
8848             (and (eq gnus-build-sparse-threads 'some)
8849                  (gnus-summary-article-sparse-p number)
8850                  (zerop children))
8851             ;; If we use expunging, and this article is really
8852             ;; low-scored, then we don't want this article.
8853             (when (and gnus-summary-expunge-below
8854                        (< (setq score
8855                                 (or (cdr (assq number gnus-newsgroup-scored))
8856                                     gnus-summary-default-score))
8857                           gnus-summary-expunge-below))
8858               ;; We increase the expunge-tally here, but that has
8859               ;; nothing to do with the limits, really.
8860               (incf gnus-newsgroup-expunged-tally)
8861               ;; We also mark as read here, if that's wanted.
8862               (when (and gnus-summary-mark-below
8863                          (< score gnus-summary-mark-below))
8864                 (setq gnus-newsgroup-unreads
8865                       (delq number gnus-newsgroup-unreads))
8866                 (if gnus-newsgroup-auto-expire
8867                     (push number gnus-newsgroup-expirable)
8868                   (push (cons number gnus-low-score-mark)
8869                         gnus-newsgroup-reads)))
8870               t)
8871             ;; Do the `display' group parameter.
8872             (and gnus-newsgroup-display
8873                  (let ((gnus-number number))
8874                    (not (funcall gnus-newsgroup-display))))))
8875           ;; Nope, invisible article.
8876           0
8877         ;; Ok, this article is to be visible, so we add it to the limit
8878         ;; and return 1.
8879         (push number gnus-newsgroup-limit)
8880         1))))
8881
8882 (defun gnus-expunge-thread (thread)
8883   "Mark all articles in THREAD as read."
8884   (let* ((number (mail-header-number (car thread))))
8885     (incf gnus-newsgroup-expunged-tally)
8886     ;; We also mark as read here, if that's wanted.
8887     (setq gnus-newsgroup-unreads
8888           (delq number gnus-newsgroup-unreads))
8889     (if gnus-newsgroup-auto-expire
8890         (push number gnus-newsgroup-expirable)
8891       (push (cons number gnus-low-score-mark)
8892             gnus-newsgroup-reads)))
8893   ;; Go recursively through all subthreads.
8894   (mapcar 'gnus-expunge-thread (cdr thread)))
8895
8896 ;; Summary article oriented commands
8897
8898 (defun gnus-summary-refer-parent-article (n)
8899   "Refer parent article N times.
8900 If N is negative, go to ancestor -N instead.
8901 The difference between N and the number of articles fetched is returned."
8902   (interactive "p")
8903   (let ((skip 1)
8904         error header ref)
8905     (when (not (natnump n))
8906       (setq skip (abs n)
8907             n 1))
8908     (while (and (> n 0)
8909                 (not error))
8910       (setq header (gnus-summary-article-header))
8911       (if (and (eq (mail-header-number header)
8912                    (cdr gnus-article-current))
8913                (equal gnus-newsgroup-name
8914                       (car gnus-article-current)))
8915           ;; If we try to find the parent of the currently
8916           ;; displayed article, then we take a look at the actual
8917           ;; References header, since this is slightly more
8918           ;; reliable than the References field we got from the
8919           ;; server.
8920           (with-current-buffer gnus-original-article-buffer
8921             (nnheader-narrow-to-headers)
8922             (unless (setq ref (message-fetch-field "references"))
8923               (when (setq ref (message-fetch-field "in-reply-to"))
8924                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8925             (widen))
8926         (setq ref
8927               ;; It's not the current article, so we take a bet on
8928               ;; the value we got from the server.
8929               (mail-header-references header)))
8930       (if (and ref
8931                (not (equal ref "")))
8932           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8933             (gnus-message 1 "Couldn't find parent"))
8934         (gnus-message 1 "No references in article %d"
8935                       (gnus-summary-article-number))
8936         (setq error t))
8937       (decf n))
8938     (gnus-summary-position-point)
8939     n))
8940
8941 (defun gnus-summary-refer-references ()
8942   "Fetch all articles mentioned in the References header.
8943 Return the number of articles fetched."
8944   (interactive)
8945   (let ((ref (mail-header-references (gnus-summary-article-header)))
8946         (current (gnus-summary-article-number))
8947         (n 0))
8948     (if (or (not ref)
8949             (equal ref ""))
8950         (error "No References in the current article")
8951       ;; For each Message-ID in the References header...
8952       (while (string-match "<[^>]*>" ref)
8953         (incf n)
8954         ;; ... fetch that article.
8955         (gnus-summary-refer-article
8956          (prog1 (match-string 0 ref)
8957            (setq ref (substring ref (match-end 0))))))
8958       (gnus-summary-goto-subject current)
8959       (gnus-summary-position-point)
8960       n)))
8961
8962 (defun gnus-delete-duplicate-headers (headers)
8963   ;; First remove leading duplicates.
8964   (while (and (> (length headers) 1)
8965               (= (mail-header-number (car headers))
8966                  (mail-header-number (cadr headers))))
8967     (pop headers))
8968   ;; Then the rest.
8969   (let ((result headers))
8970     (while (> (length headers) 1)
8971       (if (= (mail-header-number (car headers))
8972              (mail-header-number (cadr headers)))
8973           (setcdr headers (cddr headers))
8974         (pop headers)))
8975     result))
8976
8977 (defun gnus-summary-refer-thread (&optional limit)
8978   "Fetch all articles in the current thread. For backends that
8979 know how to search for threads (currently only 'nnimap) a
8980 non-numeric prefix arg will use nnir to search the entire
8981 server; without a prefix arg only the current group is
8982 searched. If the variable `gnus-refer-thread-use-nnir' is
8983 non-nil the prefix arg has the reverse meaning. If no
8984 backend-specific 'request-thread function is available fetch
8985 LIMIT (the numerical prefix) old headers. If LIMIT is
8986 non-numeric or nil fetch the number specified by the
8987 `gnus-refer-thread-limit' variable."
8988   (interactive "P")
8989   (gnus-warp-to-article)
8990   (let* ((header (gnus-summary-article-header))
8991          (id (mail-header-id header))
8992          (gnus-inhibit-demon t)
8993          (gnus-summary-ignore-duplicates t)
8994          (gnus-read-all-available-headers t)
8995          (gnus-refer-thread-use-nnir
8996           (if (and (not (null limit)) (listp limit))
8997               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
8998          (new-headers
8999           (if (gnus-check-backend-function
9000                'request-thread gnus-newsgroup-name)
9001               (gnus-request-thread header gnus-newsgroup-name)
9002             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9003                             gnus-refer-thread-limit))
9004                    (last (if (numberp limit)
9005                              (min (+ (mail-header-number header)
9006                                      limit)
9007                                   gnus-newsgroup-highest)
9008                            gnus-newsgroup-highest))
9009                    (subject (gnus-simplify-subject
9010                              (mail-header-subject header)))
9011                    (refs (split-string (or (mail-header-references header)
9012                                            "")))
9013                    (gnus-parse-headers-hook
9014                     `(lambda () (goto-char (point-min))
9015                       (keep-lines
9016                        (regexp-opt ',(append refs (list id subject)))))))
9017               (gnus-fetch-headers (list last) (if (numberp limit)
9018                                                   (* 2 limit) limit) t))))
9019          article-ids)
9020     (when (listp new-headers)
9021       (dolist (header new-headers)
9022         (push (mail-header-number header) article-ids)
9023         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9024           (push (mail-header-number header) gnus-newsgroup-unreads)
9025           (setq gnus-newsgroup-unselected
9026                 (delete (mail-header-number header)
9027                         gnus-newsgroup-unselected))))
9028       (setq gnus-newsgroup-headers
9029             (gnus-delete-duplicate-headers
9030              (gnus-merge
9031               'list gnus-newsgroup-headers new-headers
9032               'gnus-article-sort-by-number)))
9033       (setq gnus-newsgroup-articles
9034             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9035       (gnus-summary-limit-include-thread id))))
9036
9037 (defun gnus-summary-refer-article (message-id)
9038   "Fetch an article specified by MESSAGE-ID."
9039   (interactive "sMessage-ID: ")
9040   (gnus-warp-to-article)
9041   (when (and (stringp message-id)
9042              (not (zerop (length message-id))))
9043     (setq message-id (gnus-replace-in-string message-id " " ""))
9044     ;; Construct the correct Message-ID if necessary.
9045     ;; Suggested by tale@pawl.rpi.edu.
9046     (unless (string-match "^<" message-id)
9047       (setq message-id (concat "<" message-id)))
9048     (unless (string-match ">$" message-id)
9049       (setq message-id (concat message-id ">")))
9050     ;; People often post MIDs from URLs, so unhex it:
9051     (unless (string-match "@" message-id)
9052       (setq message-id (gnus-url-unhex-string message-id)))
9053     (let* ((header (gnus-id-to-header message-id))
9054            (sparse (and header
9055                         (gnus-summary-article-sparse-p
9056                          (mail-header-number header))
9057                         (memq (mail-header-number header)
9058                               gnus-newsgroup-limit)))
9059            number)
9060       (cond
9061        ;; If the article is present in the buffer we just go to it.
9062        ((and header
9063              (or (not (gnus-summary-article-sparse-p
9064                        (mail-header-number header)))
9065                  sparse))
9066         (prog1
9067             (gnus-summary-goto-article
9068              (mail-header-number header) nil t)
9069           (when sparse
9070             (gnus-summary-update-article (mail-header-number header)))))
9071        (t
9072         ;; We fetch the article.
9073         (catch 'found
9074           (dolist (gnus-override-method (gnus-refer-article-methods))
9075             (when (and (gnus-check-server gnus-override-method)
9076                        ;; Fetch the header,
9077                        (setq number (gnus-summary-insert-subject message-id)))
9078               ;; and display the article.
9079               (gnus-summary-select-article nil nil nil number)
9080               (throw 'found t)))
9081           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9082
9083 (defun gnus-refer-article-methods ()
9084   "Return a list of referable methods."
9085   (cond
9086    ;; No method, so we default to current and native.
9087    ((null gnus-refer-article-method)
9088     (list gnus-current-select-method gnus-select-method))
9089    ;; Current.
9090    ((eq 'current gnus-refer-article-method)
9091     (list gnus-current-select-method))
9092    ;; List of select methods.
9093    ((not (and (symbolp (car gnus-refer-article-method))
9094               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9095     (let (out)
9096       (dolist (method gnus-refer-article-method)
9097         (push (if (eq 'current method)
9098                   gnus-current-select-method
9099                 (if (eq 'nnir (car method))
9100                     (list
9101                      'nnir
9102                      (or (cadr method)
9103                          (gnus-method-to-server gnus-current-select-method)))
9104                   method))
9105               out))
9106       (nreverse out)))
9107    ;; One single select method.
9108    (t
9109     (list gnus-refer-article-method))))
9110
9111 (defun gnus-summary-edit-parameters ()
9112   "Edit the group parameters of the current group."
9113   (interactive)
9114   (gnus-group-edit-group gnus-newsgroup-name 'params))
9115
9116 (defun gnus-summary-customize-parameters ()
9117   "Customize the group parameters of the current group."
9118   (interactive)
9119   (gnus-group-customize gnus-newsgroup-name))
9120
9121 (defun gnus-summary-enter-digest-group (&optional force)
9122   "Enter an nndoc group based on the current article.
9123 If FORCE, force a digest interpretation.  If not, try to guess
9124 what the document format is.
9125
9126 To control what happens when you exit the group, see the
9127 `gnus-auto-select-on-ephemeral-exit' variable."
9128   (interactive "P")
9129   (let ((conf gnus-current-window-configuration))
9130     (save-window-excursion
9131       (save-excursion
9132         (let (gnus-article-prepare-hook
9133               gnus-display-mime-function
9134               gnus-break-pages)
9135           (gnus-summary-select-article))))
9136     (setq gnus-current-window-configuration conf)
9137     (let* ((name (format "%s-%d"
9138                          (gnus-group-prefixed-name
9139                           gnus-newsgroup-name (list 'nndoc ""))
9140                          (with-current-buffer gnus-summary-buffer
9141                            gnus-current-article)))
9142            (ogroup gnus-newsgroup-name)
9143            (params (append (gnus-info-params (gnus-get-info ogroup))
9144                            (list (cons 'to-group ogroup))
9145                            (list (cons 'parent-group ogroup))
9146                            (list (cons 'save-article-group ogroup))))
9147            (case-fold-search t)
9148            (buf (current-buffer))
9149            dig to-address)
9150       (with-current-buffer gnus-original-article-buffer
9151         ;; Have the digest group inherit the main mail address of
9152         ;; the parent article.
9153         (when (setq to-address (or (gnus-fetch-field "reply-to")
9154                                    (gnus-fetch-field "from")))
9155           (setq params
9156                 (append
9157                  (list (cons 'to-address
9158                              (funcall gnus-decode-encoded-address-function
9159                                       to-address))))))
9160         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9161         (insert-buffer-substring gnus-original-article-buffer)
9162         ;; Remove lines that may lead nndoc to misinterpret the
9163         ;; document type.
9164         (narrow-to-region
9165          (goto-char (point-min))
9166          (or (search-forward "\n\n" nil t) (point)))
9167         (goto-char (point-min))
9168         (delete-matching-lines "^Path:\\|^From ")
9169         (widen))
9170       (unwind-protect
9171           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9172                     (gnus-newsgroup-ephemeral-ignored-charsets
9173                      gnus-newsgroup-ignored-charsets))
9174                 (gnus-group-read-ephemeral-group
9175                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9176                               (nndoc-article-type
9177                                ,(if force 'mbox 'guess)))
9178                  t nil nil nil
9179                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9180                                                         "ADAPT")))))
9181               ;; Make all postings to this group go to the parent group.
9182               (nconc (gnus-info-params (gnus-get-info name))
9183                      params)
9184             ;; Couldn't select this doc group.
9185             (switch-to-buffer buf)
9186             (gnus-set-global-variables)
9187             (gnus-configure-windows 'summary)
9188             (gnus-message 3 "Article couldn't be entered?"))
9189         (kill-buffer dig)))))
9190
9191 (defun gnus-summary-read-document (n)
9192   "Open a new group based on the current article(s).
9193 This will allow you to read digests and other similar
9194 documents as newsgroups.
9195 Obeys the standard process/prefix convention."
9196   (interactive "P")
9197   (let* ((ogroup gnus-newsgroup-name)
9198          (params (append (gnus-info-params (gnus-get-info ogroup))
9199                          (list (cons 'to-group ogroup))))
9200          group egroup groups vgroup)
9201     (dolist (article (gnus-summary-work-articles n))
9202       (setq group (format "%s-%d" gnus-newsgroup-name article))
9203       (gnus-summary-remove-process-mark article)
9204       (when (gnus-summary-display-article article)
9205         (save-excursion ;;What for?
9206           (with-temp-buffer
9207             (insert-buffer-substring gnus-original-article-buffer)
9208             ;; Remove some headers that may lead nndoc to make
9209             ;; the wrong guess.
9210             (message-narrow-to-head)
9211             (goto-char (point-min))
9212             (delete-matching-lines "^Path:\\|^From ")
9213             (widen)
9214             (if (setq egroup
9215                       (gnus-group-read-ephemeral-group
9216                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9217                                      (nndoc-article-type guess))
9218                        t nil t))
9219                 (progn
9220                   ;; Make all postings to this group go to the parent group.
9221                   (nconc (gnus-info-params (gnus-get-info egroup))
9222                          params)
9223                   (push egroup groups))
9224               ;; Couldn't select this doc group.
9225               (gnus-error 3 "Article couldn't be entered"))))))
9226     ;; Now we have selected all the documents.
9227     (cond
9228      ((not groups)
9229       (error "None of the articles could be interpreted as documents"))
9230      ((gnus-group-read-ephemeral-group
9231        (setq vgroup (format
9232                      "nnvirtual:%s-%s" gnus-newsgroup-name
9233                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9234        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9235        t
9236        (cons (current-buffer) 'summary)))
9237      (t
9238       (error "Couldn't select virtual nndoc group")))))
9239
9240 (defun gnus-summary-widget-forward (arg)
9241   "Move point to the next field or button in the article.
9242 With optional ARG, move across that many fields."
9243   (interactive "p")
9244   (gnus-summary-select-article)
9245   (gnus-configure-windows 'article)
9246   (select-window (gnus-get-buffer-window gnus-article-buffer))
9247   (widget-forward arg))
9248
9249 (defun gnus-summary-isearch-article (&optional regexp-p)
9250   "Do incremental search forward on the current article.
9251 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9252   (interactive "P")
9253   (gnus-summary-select-article)
9254   (gnus-configure-windows 'article)
9255   (gnus-eval-in-buffer-window gnus-article-buffer
9256     (save-restriction
9257       (widen)
9258       (isearch-forward regexp-p))))
9259
9260 (defun gnus-summary-repeat-search-article-forward ()
9261   "Repeat the previous search forwards."
9262   (interactive)
9263   (unless gnus-last-search-regexp
9264     (error "No previous search"))
9265   (gnus-summary-search-article-forward gnus-last-search-regexp))
9266
9267 (defun gnus-summary-repeat-search-article-backward ()
9268   "Repeat the previous search backwards."
9269   (interactive)
9270   (unless gnus-last-search-regexp
9271     (error "No previous search"))
9272   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9273
9274 (defun gnus-summary-search-article-forward (regexp &optional backward)
9275   "Search for an article containing REGEXP forward.
9276 If BACKWARD, search backward instead."
9277   (interactive
9278    (list (read-string
9279           (format "Search article %s (regexp%s): "
9280                   (if current-prefix-arg "backward" "forward")
9281                   (if gnus-last-search-regexp
9282                       (concat ", default " gnus-last-search-regexp)
9283                     "")))
9284          current-prefix-arg))
9285   (if (string-equal regexp "")
9286       (setq regexp (or gnus-last-search-regexp ""))
9287     (setq gnus-last-search-regexp regexp)
9288     (setq gnus-article-before-search gnus-current-article))
9289   ;; Intentionally set gnus-last-article.
9290   (setq gnus-last-article gnus-article-before-search)
9291   (let ((gnus-last-article gnus-last-article))
9292     (if (gnus-summary-search-article regexp backward)
9293         (gnus-summary-show-thread)
9294       (signal 'search-failed (list regexp)))))
9295
9296 (defun gnus-summary-search-article-backward (regexp)
9297   "Search for an article containing REGEXP backward."
9298   (interactive
9299    (list (read-string
9300           (format "Search article backward (regexp%s): "
9301                   (if gnus-last-search-regexp
9302                       (concat ", default " gnus-last-search-regexp)
9303                     "")))))
9304   (gnus-summary-search-article-forward regexp 'backward))
9305
9306 (defun gnus-summary-search-article (regexp &optional backward)
9307   "Search for an article containing REGEXP.
9308 Optional argument BACKWARD means do search for backward.
9309 `gnus-select-article-hook' is not called during the search."
9310   ;; We have to require this here to make sure that the following
9311   ;; dynamic binding isn't shadowed by autoloading.
9312   (require 'gnus-async)
9313   (require 'gnus-art)
9314   (let ((gnus-select-article-hook nil)  ;Disable hook.
9315         (gnus-article-prepare-hook nil)
9316         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9317         (gnus-use-article-prefetch nil)
9318         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9319         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9320         (gnus-visual nil)
9321         (gnus-keep-backlog nil)
9322         (gnus-break-pages nil)
9323         (gnus-summary-display-arrow nil)
9324         (gnus-updated-mode-lines nil)
9325         (gnus-auto-center-summary nil)
9326         (sum (current-buffer))
9327         (gnus-display-mime-function nil)
9328         (found nil)
9329         point)
9330     (gnus-save-hidden-threads
9331       (gnus-summary-select-article)
9332       (set-buffer gnus-article-buffer)
9333       (goto-char (window-point (get-buffer-window (current-buffer))))
9334       (when backward
9335         (forward-line -1))
9336       (while (not found)
9337         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9338         (if (if backward
9339                 (re-search-backward regexp nil t)
9340               (re-search-forward regexp nil t))
9341             ;; We found the regexp.
9342             (progn
9343               (setq found 'found)
9344               (beginning-of-line)
9345               (set-window-start
9346                (get-buffer-window (current-buffer))
9347                (point))
9348               (forward-line 1)
9349               (set-window-point
9350                (get-buffer-window (current-buffer))
9351                (point))
9352               (set-buffer sum)
9353               (setq point (point)))
9354           ;; We didn't find it, so we go to the next article.
9355           (set-buffer sum)
9356           (setq found 'not)
9357           (while (eq found 'not)
9358             (if (not (if backward (gnus-summary-find-prev)
9359                        (gnus-summary-find-next)))
9360                 ;; No more articles.
9361                 (setq found t)
9362               ;; Select the next article and adjust point.
9363               (unless (gnus-summary-article-sparse-p
9364                        (gnus-summary-article-number))
9365                 (setq found nil)
9366                 (gnus-summary-select-article)
9367                 (set-buffer gnus-article-buffer)
9368                 (widen)
9369                 (goto-char (if backward (point-max) (point-min))))))))
9370       (gnus-message 7 ""))
9371     ;; Return whether we found the regexp.
9372     (when (eq found 'found)
9373       (goto-char point)
9374       (gnus-summary-show-thread)
9375       (gnus-summary-goto-subject gnus-current-article)
9376       (gnus-summary-position-point)
9377       t)))
9378
9379 (defun gnus-find-matching-articles (header regexp)
9380   "Return a list of all articles that match REGEXP on HEADER.
9381 This search includes all articles in the current group that Gnus has
9382 fetched headers for, whether they are displayed or not."
9383   (let ((articles nil)
9384         ;; Can't eta-reduce because it's a macro.
9385         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9386         (case-fold-search t))
9387     (dolist (header gnus-newsgroup-headers)
9388       (when (string-match regexp (funcall func header))
9389         (push (mail-header-number header) articles)))
9390     (nreverse articles)))
9391
9392 (defun gnus-summary-find-matching (header regexp &optional backward unread
9393                                           not-case-fold not-matching)
9394   "Return a list of all articles that match REGEXP on HEADER.
9395 The search stars on the current article and goes forwards unless
9396 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9397 If UNREAD is non-nil, only unread articles will
9398 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9399 in the comparisons. If NOT-MATCHING, return a list of all articles that
9400 not match REGEXP on HEADER."
9401   (let ((case-fold-search (not not-case-fold))
9402         articles d func)
9403     (if (consp header)
9404         (if (eq (car header) 'extra)
9405             (setq func
9406                   `(lambda (h)
9407                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9408                          "")))
9409           (error "%s is an invalid header" header))
9410       (unless (fboundp (intern (concat "mail-header-" header)))
9411         (error "%s is not a valid header" header))
9412       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9413     (dolist (d (if (eq backward 'all)
9414                    gnus-newsgroup-data
9415                  (gnus-data-find-list
9416                   (gnus-summary-article-number)
9417                   (gnus-data-list backward))))
9418       (when (and (or (not unread)       ; We want all articles...
9419                      (gnus-data-unread-p d)) ; Or just unreads.
9420                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9421                  (if not-matching
9422                      (not (string-match
9423                            regexp
9424                            (funcall func (gnus-data-header d))))
9425                    (string-match regexp
9426                                  (funcall func (gnus-data-header d)))))
9427         (push (gnus-data-number d) articles))) ; Success!
9428     (nreverse articles)))
9429
9430 (defun gnus-summary-execute-command (header regexp command &optional backward)
9431   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9432 If HEADER is an empty string (or nil), the match is done on the entire
9433 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9434   (interactive
9435    (list (let ((completion-ignore-case t))
9436            (gnus-completing-read
9437             "Header name"
9438             (mapcar 'symbol-name
9439                     (append
9440                      '(Number Subject From Lines Date
9441                        Message-ID Xref References Body)
9442                      gnus-extra-headers))
9443             'require-match))
9444          (read-string "Regexp: ")
9445          (read-key-sequence "Command: ")
9446          current-prefix-arg))
9447   (when (equal header "Body")
9448     (setq header ""))
9449   ;; Hidden thread subtrees must be searched as well.
9450   (gnus-summary-show-all-threads)
9451   ;; We don't want to change current point nor window configuration.
9452   (save-excursion
9453     (save-window-excursion
9454       (let (gnus-visual
9455             gnus-treat-strip-trailing-blank-lines
9456             gnus-treat-strip-leading-blank-lines
9457             gnus-treat-strip-multiple-blank-lines
9458             gnus-treat-hide-boring-headers
9459             gnus-treat-fold-newsgroups
9460             gnus-article-prepare-hook)
9461         (gnus-message 6 "Executing %s..." (key-description command))
9462         ;; We'd like to execute COMMAND interactively so as to give arguments.
9463         (gnus-execute header regexp
9464                       `(call-interactively ',(key-binding command))
9465                       backward)
9466         (gnus-message 6 "Executing %s...done" (key-description command))))))
9467
9468 (defun gnus-summary-beginning-of-article ()
9469   "Scroll the article back to the beginning."
9470   (interactive)
9471   (gnus-summary-select-article)
9472   (gnus-configure-windows 'article)
9473   (gnus-eval-in-buffer-window gnus-article-buffer
9474     (widen)
9475     (goto-char (point-min))
9476     (when gnus-break-pages
9477       (gnus-narrow-to-page))))
9478
9479 (defun gnus-summary-end-of-article ()
9480   "Scroll to the end of the article."
9481   (interactive)
9482   (gnus-summary-select-article)
9483   (gnus-configure-windows 'article)
9484   (gnus-eval-in-buffer-window gnus-article-buffer
9485     (widen)
9486     (goto-char (point-max))
9487     (recenter -3)
9488     (when gnus-break-pages
9489       (gnus-narrow-to-page))))
9490
9491 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9492   "Truncate to LEN and quote all \"(\"'s in STRING."
9493   (gnus-replace-in-string (if (and len (> (length string) len))
9494                               (substring string 0 len)
9495                             string)
9496                           "[()]" "\\\\\\&"))
9497
9498 (defun gnus-summary-print-article (&optional filename n)
9499   "Generate and print a PostScript image of the process-marked (mail) articles.
9500
9501 If used interactively, print the current article if none are
9502 process-marked.  With prefix arg, prompt the user for the name of the
9503 file to save in.
9504
9505 When used from Lisp, accept two optional args FILENAME and N.  N means
9506 to print the next N articles.  If N is negative, print the N previous
9507 articles.  If N is nil and articles have been marked with the process
9508 mark, print these instead.
9509
9510 If the optional first argument FILENAME is nil, send the image to the
9511 printer.  If FILENAME is a string, save the PostScript image in a file with
9512 that name.  If FILENAME is a number, prompt the user for the name of the file
9513 to save in."
9514   (interactive (list (ps-print-preprint current-prefix-arg)))
9515   (dolist (article (gnus-summary-work-articles n))
9516     (gnus-summary-select-article nil nil 'pseudo article)
9517     (gnus-eval-in-buffer-window gnus-article-buffer
9518       (gnus-print-buffer))
9519     (gnus-summary-remove-process-mark article))
9520   (ps-despool filename))
9521
9522 (defun gnus-print-buffer ()
9523   (let ((ps-left-header
9524          (list
9525           (concat "("
9526                   (gnus-summary-print-truncate-and-quote
9527                    (mail-header-subject gnus-current-headers)
9528                    66) ")")
9529           (concat "("
9530                   (gnus-summary-print-truncate-and-quote
9531                    (mail-header-from gnus-current-headers)
9532                    45) ")")))
9533         (ps-right-header
9534          (list
9535           "/pagenumberstring load"
9536           (concat "("
9537                   (mail-header-date gnus-current-headers) ")"))))
9538     (gnus-run-hooks 'gnus-ps-print-hook)
9539     (save-excursion
9540       (if ps-print-color-p
9541           (ps-spool-buffer-with-faces)
9542         (ps-spool-buffer)))))
9543
9544 (defun gnus-summary-show-complete-article ()
9545   "Show a complete version of the current article.
9546 This is only useful if you're looking at a partial version of the
9547 article currently."
9548   (interactive)
9549   (let ((gnus-keep-backlog nil)
9550         (gnus-use-cache nil)
9551         (gnus-agent nil)
9552         (variable (intern
9553                    (format "%s-fetch-partial-articles"
9554                            (car (gnus-find-method-for-group
9555                                  gnus-newsgroup-name)))
9556                    obarray))
9557         old-val)
9558     (unwind-protect
9559         (progn
9560           (setq old-val (symbol-value variable))
9561           (set variable nil)
9562           (gnus-flush-original-article-buffer)
9563           (gnus-summary-show-article))
9564       (set variable old-val))))
9565
9566 (defun gnus-summary-show-article (&optional arg)
9567   "Force redisplaying of the current article.
9568 If ARG (the prefix) is a number, show the article with the charset
9569 defined in `gnus-summary-show-article-charset-alist', or the charset
9570 input.
9571 If ARG (the prefix) is non-nil and not a number, show the article,
9572 but without running any of the article treatment functions
9573 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9574 C-u g', show the raw article."
9575   (interactive "P")
9576   (cond
9577    ((numberp arg)
9578     (gnus-summary-show-article t)
9579     (let ((gnus-newsgroup-charset
9580            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9581                (mm-read-coding-system
9582                 "View as charset: " ;; actually it is coding system.
9583                 (with-current-buffer gnus-article-buffer
9584                   (mm-detect-coding-region (point) (point-max))))))
9585           (gnus-newsgroup-ignored-charsets 'gnus-all))
9586       (gnus-summary-select-article nil 'force)
9587       (let ((deps gnus-newsgroup-dependencies)
9588             head header lines)
9589         (with-current-buffer gnus-original-article-buffer
9590           (save-restriction
9591             (message-narrow-to-head)
9592             (setq head (buffer-string))
9593             (goto-char (point-min))
9594             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9595               (goto-char (point-max))
9596               (widen)
9597               (setq lines (1- (count-lines (point) (point-max))))))
9598           (with-temp-buffer
9599             (insert (format "211 %d Article retrieved.\n"
9600                             (cdr gnus-article-current)))
9601             (insert head)
9602             (if lines (insert (format "Lines: %d\n" lines)))
9603             (insert ".\n")
9604             (let ((nntp-server-buffer (current-buffer)))
9605               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9606         (gnus-data-set-header
9607          (gnus-data-find (cdr gnus-article-current))
9608          header)
9609         (gnus-summary-update-article-line
9610          (cdr gnus-article-current) header)
9611         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9612           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9613    ((not arg)
9614     ;; Select the article the normal way.
9615     (gnus-summary-select-article nil 'force))
9616    ((equal arg '(16))
9617     ;; C-u C-u g
9618     (let ((gnus-inhibit-article-treatments t))
9619       (gnus-summary-select-article nil 'force)))
9620    (t
9621     ;; We have to require this here to make sure that the following
9622     ;; dynamic binding isn't shadowed by autoloading.
9623     (require 'gnus-async)
9624     (require 'gnus-art)
9625     ;; Bind the article treatment functions to nil.
9626     (let ((gnus-have-all-headers t)
9627           gnus-article-prepare-hook
9628           gnus-article-decode-hook
9629           gnus-display-mime-function
9630           gnus-break-pages)
9631       ;; Destroy any MIME parts.
9632       (when (gnus-buffer-live-p gnus-article-buffer)
9633         (with-current-buffer gnus-article-buffer
9634           (gnus-article-stop-animations)
9635           (mm-destroy-parts gnus-article-mime-handles)
9636           ;; Set it to nil for safety reason.
9637           (setq gnus-article-mime-handle-alist nil)
9638           (setq gnus-article-mime-handles nil)))
9639       (gnus-summary-select-article nil 'force))))
9640   (gnus-summary-goto-subject gnus-current-article)
9641   (gnus-summary-position-point))
9642
9643 (defun gnus-summary-show-raw-article ()
9644   "Show the raw article without any article massaging functions being run."
9645   (interactive)
9646   (gnus-summary-show-article t))
9647
9648 (defun gnus-summary-verbose-headers (&optional arg)
9649   "Toggle permanent full header display.
9650 If ARG is a positive number, turn header display on.
9651 If ARG is a negative number, turn header display off."
9652   (interactive "P")
9653   (setq gnus-show-all-headers
9654         (cond ((or (not (numberp arg))
9655                    (zerop arg))
9656                (not gnus-show-all-headers))
9657               ((natnump arg)
9658                t)))
9659   (gnus-summary-show-article))
9660
9661 (defun gnus-summary-toggle-header (&optional arg)
9662   "Show the headers if they are hidden, or hide them if they are shown.
9663 If ARG is a positive number, show the entire header.
9664 If ARG is a negative number, hide the unwanted header lines."
9665   (interactive "P")
9666   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9667                      (get-buffer-window gnus-article-buffer t))))
9668     (with-current-buffer gnus-article-buffer
9669       (widen)
9670       (article-narrow-to-head)
9671       (let* ((inhibit-read-only t)
9672              (inhibit-point-motion-hooks t)
9673              (hidden (if (numberp arg)
9674                          (>= arg 0)
9675                        (or (not (looking-at "[^ \t\n]+:"))
9676                            (gnus-article-hidden-text-p 'headers))))
9677              s e)
9678         (delete-region (point-min) (point-max))
9679         (with-current-buffer gnus-original-article-buffer
9680           (goto-char (setq s (point-min)))
9681           (setq e (if (search-forward "\n\n" nil t)
9682                       (1- (point))
9683                     (point-max))))
9684         (insert-buffer-substring gnus-original-article-buffer s e)
9685         (run-hooks 'gnus-article-decode-hook)
9686         (if hidden
9687             (let ((gnus-treat-hide-headers nil)
9688                   (gnus-treat-hide-boring-headers nil))
9689               (gnus-delete-wash-type 'headers)
9690               (gnus-treat-article 'head))
9691           (gnus-treat-article 'head))
9692         (widen)
9693         (if window
9694             (set-window-start window (goto-char (point-min))))
9695         (if gnus-break-pages
9696             (gnus-narrow-to-page)
9697           (when (gnus-visual-p 'page-marker)
9698             (let ((inhibit-read-only t))
9699               (gnus-remove-text-with-property 'gnus-prev)
9700               (gnus-remove-text-with-property 'gnus-next))))
9701         (gnus-set-mode-line 'article)))))
9702
9703 (defun gnus-summary-show-all-headers ()
9704   "Make all header lines visible."
9705   (interactive)
9706   (gnus-summary-toggle-header 1))
9707
9708 (defun gnus-summary-caesar-message (&optional arg)
9709   "Caesar rotate the current article by 13.
9710 With a non-numerical prefix, also rotate headers.  A numerical
9711 prefix specifies how many places to rotate each letter forward."
9712   (interactive "P")
9713   (gnus-summary-select-article)
9714   (let ((mail-header-separator ""))
9715     (gnus-eval-in-buffer-window gnus-article-buffer
9716       (save-restriction
9717         (widen)
9718         (let ((start (window-start))
9719               (inhibit-read-only t))
9720           (if (equal arg '(4))
9721               (message-caesar-buffer-body nil t)
9722             (message-caesar-buffer-body arg))
9723           (set-window-start (get-buffer-window (current-buffer)) start)))))
9724   ;; Create buttons and stuff...
9725   (gnus-treat-article nil))
9726
9727 (declare-function idna-to-unicode "ext:idna" (str))
9728
9729 (defun gnus-summary-idna-message (&optional arg)
9730   "Decode IDNA encoded domain names in the current articles.
9731 IDNA encoded domain names looks like `xn--bar'.  If a string
9732 remain unencoded after running this function, it is likely an
9733 invalid IDNA string (`xn--bar' is invalid).
9734
9735 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9736 installed for this command to work."
9737   (interactive "P")
9738   (if (not (and (condition-case nil (require 'idna)
9739                   (file-error))
9740                 (mm-coding-system-p 'utf-8)
9741                 (executable-find (symbol-value 'idna-program))))
9742       (gnus-message
9743        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9744     (gnus-summary-select-article)
9745     (let ((mail-header-separator ""))
9746       (gnus-eval-in-buffer-window gnus-article-buffer
9747         (save-restriction
9748           (widen)
9749           (let ((start (window-start))
9750                 buffer-read-only)
9751             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9752               (replace-match (idna-to-unicode (match-string 1))))
9753             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9754
9755 (defun gnus-summary-morse-message (&optional arg)
9756   "Morse decode the current article."
9757   (interactive "P")
9758   (gnus-summary-select-article)
9759   (let ((mail-header-separator ""))
9760     (gnus-eval-in-buffer-window gnus-article-buffer
9761       (save-excursion
9762         (save-restriction
9763           (widen)
9764           (let ((pos (window-start))
9765                 (inhibit-read-only t))
9766             (goto-char (point-min))
9767             (when (message-goto-body)
9768               (gnus-narrow-to-body))
9769             (goto-char (point-min))
9770             (while (search-forward "·" (point-max) t)
9771               (replace-match "."))
9772             (unmorse-region (point-min) (point-max))
9773             (widen)
9774             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9775
9776 (defun gnus-summary-stop-page-breaking ()
9777   "Stop page breaking in the current article."
9778   (interactive)
9779   (gnus-summary-select-article)
9780   (gnus-eval-in-buffer-window gnus-article-buffer
9781     (widen)
9782     (when (gnus-visual-p 'page-marker)
9783       (let ((inhibit-read-only t))
9784         (gnus-remove-text-with-property 'gnus-prev)
9785         (gnus-remove-text-with-property 'gnus-next))
9786       (setq gnus-page-broken nil))))
9787
9788 (defun gnus-summary-move-article (&optional n to-newsgroup
9789                                             select-method action)
9790   "Move the current article to a different newsgroup.
9791 If N is a positive number, move the N next articles.
9792 If N is a negative number, move the N previous articles.
9793 If N is nil and any articles have been marked with the process mark,
9794 move those articles instead.
9795 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9796 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9797 re-spool using this method.
9798
9799 When called interactively with TO-NEWSGROUP being nil, the value of
9800 the variable `gnus-move-split-methods' is used for finding a default
9801 for the target newsgroup.
9802
9803 For this function to work, both the current newsgroup and the
9804 newsgroup that you want to move to have to support the `request-move'
9805 and `request-accept' functions.
9806
9807 ACTION can be either `move' (the default), `crosspost' or `copy'."
9808   (interactive "P")
9809   (unless action
9810     (setq action 'move))
9811   ;; Check whether the source group supports the required functions.
9812   (cond ((and (eq action 'move)
9813               (not (gnus-check-backend-function
9814                     'request-move-article gnus-newsgroup-name)))
9815          (error "The current group does not support article moving"))
9816         ((and (eq action 'crosspost)
9817               (not (gnus-check-backend-function
9818                     'request-replace-article gnus-newsgroup-name)))
9819          (error "The current group does not support article editing")))
9820   (let ((articles (gnus-summary-work-articles n))
9821         (prefix (if (gnus-check-backend-function
9822                      'request-move-article gnus-newsgroup-name)
9823                     (funcall gnus-move-group-prefix-function
9824                              gnus-newsgroup-name)
9825                   ""))
9826         (names '((move "Move" "Moving")
9827                  (copy "Copy" "Copying")
9828                  (crosspost "Crosspost" "Crossposting")))
9829         (copy-buf (save-excursion
9830                     (nnheader-set-temp-buffer " *copy article*")))
9831         art-group to-method new-xref article to-groups
9832         articles-to-update-marks encoded)
9833     (unless (assq action names)
9834       (error "Unknown action %s" action))
9835     ;; Read the newsgroup name.
9836     (when (and (not to-newsgroup)
9837                (not select-method))
9838       (if (and gnus-move-split-methods
9839                (not
9840                 (and (memq gnus-current-article articles)
9841                      (gnus-buffer-live-p gnus-original-article-buffer))))
9842           ;; When `gnus-move-split-methods' is non-nil, we have to
9843           ;; select an article to give `gnus-read-move-group-name' an
9844           ;; opportunity to suggest an appropriate default.  However,
9845           ;; we needn't render or mark the article.
9846           (let ((gnus-display-mime-function nil)
9847                 (gnus-article-prepare-hook nil)
9848                 (gnus-mark-article-hook nil))
9849             (gnus-summary-select-article nil nil nil (car articles))))
9850       (setq to-newsgroup (gnus-read-move-group-name
9851                           (cadr (assq action names))
9852                           (symbol-value
9853                            (intern (format "gnus-current-%s-group" action)))
9854                           articles prefix)
9855             encoded to-newsgroup
9856             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9857       (set (intern (format "gnus-current-%s-group" action))
9858            (mm-decode-coding-string
9859             to-newsgroup
9860             (gnus-group-name-charset to-method to-newsgroup))))
9861     (unless to-method
9862       (setq to-method (or select-method
9863                           (gnus-server-to-method
9864                            (gnus-group-method to-newsgroup)))))
9865     (setq to-newsgroup
9866           (or encoded
9867               (and to-newsgroup
9868                    (mm-encode-coding-string
9869                     to-newsgroup
9870                     (gnus-group-name-charset to-method to-newsgroup)))))
9871     ;; Check the method we are to move this article to...
9872     (unless (gnus-check-backend-function
9873              'request-accept-article (car to-method))
9874       (error "%s does not support article copying" (car to-method)))
9875     (unless (gnus-check-server to-method)
9876       (error "Can't open server %s" (car to-method)))
9877     (gnus-message 6 "%s to %s: %s..."
9878                   (caddr (assq action names))
9879                   (or (car select-method)
9880                       (gnus-group-decoded-name to-newsgroup))
9881                   articles)
9882     (while articles
9883       (setq article (pop articles))
9884       ;; Set any marks that may have changed in the summary buffer.
9885       (when gnus-preserve-marks
9886         (gnus-summary-push-marks-to-backend article))
9887       (setq
9888        art-group
9889        (cond
9890         ;; Move the article.
9891         ((eq action 'move)
9892          ;; Remove this article from future suppression.
9893          (gnus-dup-unsuppress-article article)
9894          (let* ((from-method (gnus-find-method-for-group
9895                               gnus-newsgroup-name))
9896                 (to-method (or select-method
9897                                (gnus-find-method-for-group to-newsgroup)))
9898                 (move-is-internal (gnus-server-equal from-method to-method)))
9899            (gnus-request-move-article
9900             article                     ; Article to move
9901             gnus-newsgroup-name         ; From newsgroup
9902             (nth 1 (gnus-find-method-for-group
9903                     gnus-newsgroup-name)) ; Server
9904             (list 'gnus-request-accept-article
9905                   to-newsgroup (list 'quote select-method)
9906                   (not articles) t)     ; Accept form
9907             (not articles)              ; Only save nov last time
9908             (and move-is-internal
9909                  to-newsgroup           ; Not respooling
9910                                         ; Is this move internal?
9911                  (gnus-group-real-name to-newsgroup)))))
9912         ;; Copy the article.
9913         ((eq action 'copy)
9914          (with-current-buffer copy-buf
9915            (when (gnus-request-article-this-buffer article
9916                                                    gnus-newsgroup-name)
9917              (save-restriction
9918                (nnheader-narrow-to-headers)
9919                (dolist (hdr gnus-copy-article-ignored-headers)
9920                  (message-remove-header hdr t)))
9921              (gnus-request-accept-article
9922               to-newsgroup select-method (not articles) t))))
9923         ;; Crosspost the article.
9924         ((eq action 'crosspost)
9925          (let ((xref (message-tokenize-header
9926                       (mail-header-xref (gnus-summary-article-header
9927                                          article))
9928                       " ")))
9929            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9930                                   ":" (number-to-string article)))
9931            (unless xref
9932              (setq xref (list (system-name))))
9933            (setq new-xref
9934                  (concat
9935                   (mapconcat 'identity
9936                              (delete "Xref:" (delete new-xref xref))
9937                              " ")
9938                   " " new-xref))
9939            (with-current-buffer copy-buf
9940              ;; First put the article in the destination group.
9941              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9942              (when (consp (setq art-group
9943                                 (gnus-request-accept-article
9944                                  to-newsgroup select-method (not articles)
9945                                  t)))
9946                (setq new-xref (concat new-xref " " (car art-group)
9947                                       ":"
9948                                       (number-to-string (cdr art-group))))
9949                ;; Now we have the new Xrefs header, so we insert
9950                ;; it and replace the new article.
9951                (nnheader-replace-header "Xref" new-xref)
9952                (gnus-request-replace-article
9953                 (cdr art-group) to-newsgroup (current-buffer) t)
9954                art-group))))))
9955       (cond
9956        ((not art-group)
9957         (gnus-message 1 "Couldn't %s article %s: %s"
9958                       (cadr (assq action names)) article
9959                       (nnheader-get-report (car to-method))))
9960        ((eq art-group 'junk)
9961         (when (eq action 'move)
9962           (gnus-summary-mark-article article gnus-canceled-mark)
9963           (gnus-message 4 "Deleted article %s" article)
9964           ;; run the delete hook
9965           (run-hook-with-args 'gnus-summary-article-delete-hook
9966                               action
9967                               (gnus-data-header
9968                                (assoc article (gnus-data-list nil)))
9969                               gnus-newsgroup-name nil
9970                               select-method)))
9971        (t
9972         (let* ((pto-group (gnus-group-prefixed-name
9973                            (car art-group) to-method))
9974                (info (gnus-get-info pto-group))
9975                (to-group (gnus-info-group info))
9976                to-marks)
9977           ;; Update the group that has been moved to.
9978           (when (and info
9979                      (memq action '(move copy)))
9980             (unless (member to-group to-groups)
9981               (push to-group to-groups))
9982
9983             (when (and (not (memq article gnus-newsgroup-unreads))
9984                        (cdr art-group))
9985               (push 'read to-marks)
9986               (gnus-info-set-read
9987                info (gnus-add-to-range (gnus-info-read info)
9988                                        (list (cdr art-group)))))
9989
9990             ;; See whether the article is to be put in the cache.
9991             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9992                    (marks (if expirable
9993                               gnus-article-mark-lists
9994                             (delete '(expirable . expire)
9995                                     (copy-sequence
9996                                      gnus-article-mark-lists))))
9997                    (to-article (cdr art-group)))
9998
9999               ;; Enter the article into the cache in the new group,
10000               ;; if that is required.
10001               (when (and to-article
10002                          gnus-use-cache)
10003                 (gnus-cache-possibly-enter-article
10004                  to-group to-article
10005                  (memq article gnus-newsgroup-marked)
10006                  (memq article gnus-newsgroup-dormant)
10007                  (memq article gnus-newsgroup-unreads)))
10008
10009               (when (and gnus-preserve-marks
10010                          to-article)
10011                 ;; Copy any marks over to the new group.
10012                 (when (and (equal to-group gnus-newsgroup-name)
10013                            (not (memq article gnus-newsgroup-unreads)))
10014                   ;; Mark this article as read in this group.
10015                   (push (cons to-article gnus-read-mark)
10016                         gnus-newsgroup-reads)
10017                   ;; Increase the active status of this group.
10018                   (setcdr (gnus-active to-group) to-article)
10019                   (setcdr gnus-newsgroup-active to-article))
10020
10021                 (while marks
10022                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10023                     (when (memq article (symbol-value
10024                                          (intern (format "gnus-newsgroup-%s"
10025                                                          (caar marks)))))
10026                       (push (cdar marks) to-marks)
10027                       ;; If the other group is the same as this group,
10028                       ;; then we have to add the mark to the list.
10029                       (when (equal to-group gnus-newsgroup-name)
10030                         (set (intern (format "gnus-newsgroup-%s"
10031                                              (caar marks)))
10032                              (cons to-article
10033                                    (symbol-value
10034                                     (intern (format "gnus-newsgroup-%s"
10035                                                     (caar marks)))))))
10036                       ;; Copy the marks to other group.
10037                       (gnus-add-marked-articles
10038                        to-group (cdar marks) (list to-article) info)))
10039                   (setq marks (cdr marks)))
10040
10041                 (when (and expirable
10042                            gnus-mark-copied-or-moved-articles-as-expirable
10043                            (not (memq 'expire to-marks)))
10044                   ;; Mark this article as expirable.
10045                   (push 'expire to-marks)
10046                   (when (equal to-group gnus-newsgroup-name)
10047                     (push to-article gnus-newsgroup-expirable))
10048                   ;; Copy the expirable mark to other group.
10049                   (gnus-add-marked-articles
10050                    to-group 'expire (list to-article) info))
10051
10052                 (when to-marks
10053                   (gnus-request-set-mark
10054                    to-group (list (list (list to-article) 'add to-marks)))))
10055
10056               (gnus-dribble-enter
10057                (concat "(gnus-group-set-info '"
10058                        (gnus-prin1-to-string (gnus-get-info to-group))
10059                        ")")
10060                (concat "^(gnus-group-set-info '(\""
10061                        (regexp-quote to-group) "\""))))
10062
10063           ;; Update the Xref header in this article to point to
10064           ;; the new crossposted article we have just created.
10065           (when (eq action 'crosspost)
10066             (with-current-buffer copy-buf
10067               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10068               (nnheader-replace-header "Xref" new-xref)
10069               (gnus-request-replace-article
10070                article gnus-newsgroup-name (current-buffer) t)))
10071
10072           ;; run the move/copy/crosspost/respool hook
10073           (run-hook-with-args 'gnus-summary-article-move-hook
10074                               action
10075                               (gnus-data-header
10076                                (assoc article (gnus-data-list nil)))
10077                               gnus-newsgroup-name
10078                               to-newsgroup
10079                               select-method))
10080
10081         ;;;!!!Why is this necessary?
10082         (set-buffer gnus-summary-buffer)
10083
10084         (when (eq action 'move)
10085           (save-excursion
10086             (gnus-summary-goto-subject article)
10087             (gnus-summary-mark-article article gnus-canceled-mark)))))
10088       (push article articles-to-update-marks))
10089
10090     (save-excursion
10091       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10092     ;; Re-activate all groups that have been moved to.
10093     (with-current-buffer gnus-group-buffer
10094       (let ((gnus-group-marked to-groups))
10095         (gnus-group-get-new-news-this-group nil t)))
10096
10097     (gnus-kill-buffer copy-buf)
10098     (gnus-summary-position-point)
10099     (gnus-set-mode-line 'summary)))
10100
10101 (defun gnus-summary-push-marks-to-backend (article)
10102   (let ((set nil)
10103         (marks gnus-article-mark-lists))
10104     (unless (memq article gnus-newsgroup-unreads)
10105       (push 'read set))
10106     (while marks
10107       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10108                  (memq article (symbol-value
10109                                 (intern (format "gnus-newsgroup-%s"
10110                                                 (caar marks))))))
10111         (push (cdar marks) set))
10112       (pop marks))
10113     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10114
10115 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10116   "Copy the current article to some other group.
10117 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10118 When called interactively, if TO-NEWSGROUP is nil, use the value of
10119 the variable `gnus-move-split-methods' for finding a default target
10120 newsgroup.
10121 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10122 re-spool using this method."
10123   (interactive "P")
10124   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10125
10126 (defun gnus-summary-crosspost-article (&optional n)
10127   "Crosspost the current article to some other group."
10128   (interactive "P")
10129   (gnus-summary-move-article n nil nil 'crosspost))
10130
10131 (defcustom gnus-summary-respool-default-method nil
10132   "Default method type for respooling an article.
10133 If nil, use to the current newsgroup method."
10134   :type 'symbol
10135   :group 'gnus-summary-mail)
10136
10137 (defun gnus-summary-respool-article (&optional n method)
10138   "Respool the current article.
10139 The article will be squeezed through the mail spooling process again,
10140 which means that it will be put in some mail newsgroup or other
10141 depending on `nnmail-split-methods'.
10142 If N is a positive number, respool the N next articles.
10143 If N is a negative number, respool the N previous articles.
10144 If N is nil and any articles have been marked with the process mark,
10145 respool those articles instead.
10146
10147 Respooling can be done both from mail groups and \"real\" newsgroups.
10148 In the former case, the articles in question will be moved from the
10149 current group into whatever groups they are destined to.  In the
10150 latter case, they will be copied into the relevant groups."
10151   (interactive
10152    (list current-prefix-arg
10153          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10154                 (methname
10155                  (symbol-name (or gnus-summary-respool-default-method
10156                                   (car (gnus-find-method-for-group
10157                                         gnus-newsgroup-name)))))
10158                 (method
10159                  (gnus-completing-read
10160                   "Backend to use when respooling"
10161                   methods t nil 'gnus-mail-method-history methname))
10162                 ms)
10163            (cond
10164             ((zerop (length (setq ms (gnus-servers-using-backend
10165                                       (intern method)))))
10166              (list (intern method) ""))
10167             ((= 1 (length ms))
10168              (car ms))
10169             (t
10170              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10171                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10172                            ms-alist))))))))
10173   (unless method
10174     (error "No method given for respooling"))
10175   (if (assoc (symbol-name
10176               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10177              (gnus-methods-using 'respool))
10178       (gnus-summary-move-article n nil method)
10179     (gnus-summary-copy-article n nil method)))
10180
10181 (defun gnus-summary-import-article (file &optional edit)
10182   "Import an arbitrary file into a mail newsgroup."
10183   (interactive "fImport file: \nP")
10184   (let ((group gnus-newsgroup-name)
10185         (now (current-time))
10186         atts lines group-art)
10187     (unless (gnus-check-backend-function 'request-accept-article group)
10188       (error "%s does not support article importing" group))
10189     (or (file-readable-p file)
10190         (not (file-regular-p file))
10191         (error "Can't read %s" file))
10192     (with-current-buffer (gnus-get-buffer-create " *import file*")
10193       (erase-buffer)
10194       (nnheader-insert-file-contents file)
10195       (goto-char (point-min))
10196       (if (nnheader-article-p)
10197           (save-restriction
10198             (goto-char (point-min))
10199             (search-forward "\n\n" nil t)
10200             (narrow-to-region (point-min) (1- (point)))
10201             (goto-char (point-min))
10202             (unless (re-search-forward "^date:" nil t)
10203               (goto-char (point-max))
10204               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10205        ;; This doesn't look like an article, so we fudge some headers.
10206         (setq atts (file-attributes file)
10207               lines (count-lines (point-min) (point-max)))
10208         (insert "From: " (read-string "From: ") "\n"
10209                 "Subject: " (read-string "Subject: ") "\n"
10210                 "Date: " (message-make-date (nth 5 atts)) "\n"
10211                 "Message-ID: " (message-make-message-id) "\n"
10212                 "Lines: " (int-to-string lines) "\n"
10213                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10214       (setq group-art (gnus-request-accept-article group nil t))
10215       (kill-buffer (current-buffer)))
10216     (setq gnus-newsgroup-active (gnus-activate-group group))
10217     (forward-line 1)
10218     (gnus-summary-goto-article (cdr group-art) nil t)
10219     (when edit
10220       (gnus-summary-edit-article))))
10221
10222 (defun gnus-summary-create-article ()
10223   "Create an article in a mail newsgroup."
10224   (interactive)
10225   (let ((group gnus-newsgroup-name)
10226         (now (current-time))
10227         group-art)
10228     (unless (gnus-check-backend-function 'request-accept-article group)
10229       (error "%s does not support article importing" group))
10230     (with-current-buffer (gnus-get-buffer-create " *import file*")
10231       (erase-buffer)
10232       (goto-char (point-min))
10233       ;; This doesn't look like an article, so we fudge some headers.
10234       (insert "From: " (read-string "From: ") "\n"
10235               "Subject: " (read-string "Subject: ") "\n"
10236               "Date: " (message-make-date now) "\n"
10237               "Message-ID: " (message-make-message-id) "\n")
10238       (setq group-art (gnus-request-accept-article group nil t))
10239       (kill-buffer (current-buffer)))
10240     (setq gnus-newsgroup-active (gnus-activate-group group))
10241     (forward-line 1)
10242     (gnus-summary-goto-article (cdr group-art) nil t)
10243     (gnus-summary-edit-article)))
10244
10245 (defun gnus-summary-article-posted-p ()
10246   "Say whether the current (mail) article is available from news as well.
10247 This will be the case if the article has both been mailed and posted."
10248   (interactive)
10249   (let ((id (mail-header-references (gnus-summary-article-header)))
10250         (gnus-override-method (car (gnus-refer-article-methods))))
10251     (if (gnus-request-head id "")
10252         (gnus-message 2 "The current message was found on %s"
10253                       gnus-override-method)
10254       (gnus-message 2 "The current message couldn't be found on %s"
10255                     gnus-override-method)
10256       nil)))
10257
10258 (defun gnus-summary-expire-articles (&optional now)
10259   "Expire all articles that are marked as expirable in the current group."
10260   (interactive)
10261   (when (and (not gnus-group-is-exiting-without-update-p)
10262              (gnus-check-backend-function
10263               'request-expire-articles gnus-newsgroup-name))
10264     ;; This backend supports expiry.
10265     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10266            (expirable (if total
10267                           (progn
10268                             ;; We need to update the info for
10269                             ;; this group for `gnus-list-of-read-articles'
10270                             ;; to give us the right answer.
10271                             (gnus-run-hooks 'gnus-exit-group-hook)
10272                             (gnus-summary-update-info)
10273                             (gnus-list-of-read-articles gnus-newsgroup-name))
10274                         (setq gnus-newsgroup-expirable
10275                               (sort gnus-newsgroup-expirable '<))))
10276            (expiry-wait (if now 'immediate
10277                           (gnus-group-find-parameter
10278                            gnus-newsgroup-name 'expiry-wait)))
10279            (nnmail-expiry-target
10280             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10281                 nnmail-expiry-target))
10282            es)
10283       (when expirable
10284         ;; There are expirable articles in this group, so we run them
10285         ;; through the expiry process.
10286         (gnus-message 6 "Expiring articles...")
10287         (when (gnus-check-group gnus-newsgroup-name)
10288           ;; The list of articles that weren't expired is returned.
10289           (save-excursion
10290             (if expiry-wait
10291                 (let ((nnmail-expiry-wait-function nil)
10292                       (nnmail-expiry-wait expiry-wait))
10293                   (setq es (gnus-request-expire-articles
10294                             expirable gnus-newsgroup-name)))
10295               (setq es (gnus-request-expire-articles
10296                         expirable gnus-newsgroup-name)))
10297             (unless total
10298               (setq gnus-newsgroup-expirable es))
10299             ;; We go through the old list of expirable, and mark all
10300             ;; really expired articles as nonexistent.
10301             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10302               (let ((gnus-use-cache nil))
10303                 (dolist (article expirable)
10304                   (when (and (not (memq article es))
10305                              (gnus-data-find article))
10306                     (gnus-summary-mark-article article gnus-canceled-mark)
10307                     (run-hook-with-args 'gnus-summary-article-expire-hook
10308                                         'delete
10309                                         (gnus-data-header
10310                                          (assoc article (gnus-data-list nil)))
10311                                         gnus-newsgroup-name
10312                                         nil
10313                                         nil)))))))
10314         (gnus-message 6 "Expiring articles...done")))))
10315
10316 (defun gnus-summary-expire-articles-now ()
10317   "Expunge all expirable articles in the current group.
10318 This means that *all* articles that are marked as expirable will be
10319 deleted forever, right now."
10320   (interactive)
10321   (or gnus-expert-user
10322       (gnus-yes-or-no-p
10323        "Are you really, really sure you want to delete all expirable messages? ")
10324       (error "Phew!"))
10325   (gnus-summary-expire-articles t))
10326
10327 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10328 (defun gnus-summary-delete-article (&optional n)
10329   "Delete the N next (mail) articles.
10330 This command actually deletes articles.  This is not a marking
10331 command.  The article will disappear forever from your life, never to
10332 return.
10333
10334 If N is negative, delete backwards.
10335 If N is nil and articles have been marked with the process mark,
10336 delete these instead.
10337
10338 If `gnus-novice-user' is non-nil you will be asked for
10339 confirmation before the articles are deleted."
10340   (interactive "P")
10341   (unless (gnus-check-backend-function 'request-expire-articles
10342                                        gnus-newsgroup-name)
10343     (error "The current newsgroup does not support article deletion"))
10344   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10345     (error "Couldn't open server"))
10346   ;; Compute the list of articles to delete.
10347   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10348         (nnmail-expiry-target 'delete)
10349         not-deleted)
10350     (if (and gnus-novice-user
10351              (not (gnus-yes-or-no-p
10352                    (format "Do you really want to delete %s forever? "
10353                            (if (> (length articles) 1)
10354                                (format "these %s articles" (length articles))
10355                              "this article")))))
10356         ()
10357       ;; Delete the articles.
10358       (setq not-deleted (gnus-request-expire-articles
10359                          articles gnus-newsgroup-name 'force))
10360       (save-excursion
10361         (while articles
10362           (gnus-summary-remove-process-mark (car articles))
10363           ;; The backend might not have been able to delete the article
10364           ;; after all.
10365           (unless (memq (car articles) not-deleted)
10366             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10367             (let* ((article (car articles))
10368                    (ghead  (gnus-data-header
10369                             (assoc article (gnus-data-list nil)))))
10370               (run-hook-with-args 'gnus-summary-article-delete-hook
10371                                   'delete ghead gnus-newsgroup-name nil
10372                                   nil)))
10373           (setq articles (cdr articles))))
10374       (when not-deleted
10375         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10376     (gnus-summary-position-point)
10377     (gnus-set-mode-line 'summary)
10378     not-deleted))
10379
10380 (defun gnus-summary-edit-article (&optional arg)
10381   "Edit the current article.
10382 This will have permanent effect only in mail groups.
10383 If ARG is nil, edit the decoded articles.
10384 If ARG is 1, edit the raw articles.
10385 If ARG is 2, edit the raw articles even in read-only groups.
10386 If ARG is 3, edit the articles with the current handles.
10387 Otherwise, allow editing of articles even in read-only
10388 groups."
10389   (interactive "P")
10390   (let (force raw current-handles)
10391     (cond
10392      ((null arg))
10393      ((eq arg 1)
10394       (setq raw t))
10395      ((eq arg 2)
10396       (setq raw t
10397             force t))
10398      ((eq arg 3)
10399       (setq current-handles
10400             (and (gnus-buffer-live-p gnus-article-buffer)
10401                  (with-current-buffer gnus-article-buffer
10402                    (prog1
10403                        gnus-article-mime-handles
10404                      (setq gnus-article-mime-handles nil))))))
10405      (t
10406       (setq force t)))
10407     (when (and raw (not force)
10408                (member gnus-newsgroup-name '("nndraft:delayed"
10409                                              "nndraft:drafts"
10410                                              "nndraft:queue")))
10411       (error "Can't edit the raw article in group %s"
10412              gnus-newsgroup-name))
10413     (with-current-buffer gnus-summary-buffer
10414       (let ((mail-parse-charset gnus-newsgroup-charset)
10415             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10416         (gnus-set-global-variables)
10417         (when (and (not force)
10418                    (gnus-group-read-only-p))
10419           (error "The current newsgroup does not support article editing"))
10420         (gnus-summary-show-article t)
10421         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10422           (with-current-buffer gnus-article-buffer
10423             (mm-enable-multibyte)))
10424         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10425             (setq raw t))
10426         (gnus-article-edit-article
10427          (if raw 'ignore
10428            `(lambda ()
10429               (let ((mbl mml-buffer-list))
10430                 (setq mml-buffer-list nil)
10431                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10432                   (mime-to-mml ,'current-handles))
10433                 (let ((mbl1 mml-buffer-list))
10434                   (setq mml-buffer-list mbl)
10435                   (set (make-local-variable 'mml-buffer-list) mbl1))
10436                 (gnus-make-local-hook 'kill-buffer-hook)
10437                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10438          `(lambda (no-highlight)
10439             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10440                   (message-options message-options)
10441                   (message-options-set-recipient)
10442                   (mail-parse-ignored-charsets
10443                    ',gnus-newsgroup-ignored-charsets)
10444                   (rfc2047-header-encoding-alist
10445                    ',(let ((charset (gnus-group-name-charset
10446                                      (gnus-find-method-for-group
10447                                       gnus-newsgroup-name)
10448                                      gnus-newsgroup-name)))
10449                        (append (list (cons "Newsgroups" charset)
10450                                      (cons "Followup-To" charset)
10451                                      (cons "Xref" charset))
10452                                rfc2047-header-encoding-alist))))
10453               ,(if (not raw) '(progn
10454                                 (mml-to-mime)
10455                                 (mml-destroy-buffers)
10456                                 (remove-hook 'kill-buffer-hook
10457                                              'mml-destroy-buffers t)
10458                                 (kill-local-variable 'mml-buffer-list)))
10459               (gnus-summary-edit-article-done
10460                ,(or (mail-header-references gnus-current-headers) "")
10461                ,(gnus-group-read-only-p)
10462                ,gnus-summary-buffer no-highlight))))))))
10463
10464 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10465
10466 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10467                                                  no-highlight)
10468   "Make edits to the current article permanent."
10469   (interactive)
10470   (save-excursion
10471     ;; The buffer restriction contains the entire article if it exists.
10472     (when (article-goto-body)
10473       (let ((lines (count-lines (point) (point-max)))
10474             (length (- (point-max) (point)))
10475             (case-fold-search t)
10476             (body (copy-marker (point))))
10477         (goto-char (point-min))
10478         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10479           (delete-region (match-beginning 1) (match-end 1))
10480           (insert (number-to-string length)))
10481         (goto-char (point-min))
10482         (when (re-search-forward
10483                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10484           (delete-region (match-beginning 1) (match-end 1))
10485           (insert (number-to-string length)))
10486         (goto-char (point-min))
10487         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10488           (delete-region (match-beginning 1) (match-end 1))
10489           (insert (number-to-string lines))))))
10490   ;; Replace the article.
10491   (let ((buf (current-buffer))
10492         (article (cdr gnus-article-current))
10493         replace-result)
10494     (with-temp-buffer
10495       (insert-buffer-substring buf)
10496       (if (and (not read-only)
10497                (not (setq replace-result
10498                           (gnus-request-replace-article
10499                            article (car gnus-article-current)
10500                            (current-buffer) t))))
10501           (error "Couldn't replace article")
10502         ;; If we got a number back, then that's the new article number
10503         ;; for this article.  Otherwise, the article number didn't change.
10504         (when (numberp replace-result)
10505           (with-current-buffer gnus-summary-buffer
10506             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10507             (gnus-summary-limit gnus-newsgroup-limit)
10508             (setq article replace-result)
10509             (gnus-summary-goto-subject article t)))
10510         ;; Update the summary buffer.
10511         (if (and references
10512                  (equal (message-tokenize-header references " ")
10513                         (message-tokenize-header
10514                          (or (message-fetch-field "references") "") " ")))
10515             ;; We only have to update this line.
10516             (save-excursion
10517               (save-restriction
10518                 (message-narrow-to-head)
10519                 (let ((head (buffer-substring-no-properties
10520                              (point-min) (point-max)))
10521                       header)
10522                   (with-temp-buffer
10523                     (insert (format "211 %d Article retrieved.\n" article))
10524                     (insert head)
10525                     (insert ".\n")
10526                     (let ((nntp-server-buffer (current-buffer)))
10527                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10528                     (with-current-buffer gnus-summary-buffer
10529                       (gnus-data-set-header (gnus-data-find article) header)
10530                       (gnus-summary-update-article-line article header)
10531                       (if (gnus-summary-goto-subject article nil t)
10532                           (gnus-summary-update-secondary-mark article)))))))
10533           ;; Update threads.
10534           (set-buffer (or buffer gnus-summary-buffer))
10535           (gnus-summary-update-article article)
10536           (if (gnus-summary-goto-subject article nil t)
10537               (gnus-summary-update-secondary-mark article)))
10538         ;; Prettify the article buffer again.
10539         (unless no-highlight
10540           (with-current-buffer gnus-article-buffer
10541             ;;!!! Fix this -- article should be rehighlighted.
10542             ;;(gnus-run-hooks 'gnus-article-display-hook)
10543             (set-buffer gnus-original-article-buffer)
10544             (gnus-request-article
10545              article (car gnus-article-current) (current-buffer))))
10546         ;; Prettify the summary buffer line.
10547         (when (gnus-visual-p 'summary-highlight 'highlight)
10548           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10549
10550 (defun gnus-summary-edit-wash (key)
10551   "Perform editing command KEY in the article buffer."
10552   (interactive
10553    (list
10554     (progn
10555       (message "%s" (concat (this-command-keys) "- "))
10556       (read-char))))
10557   (message "")
10558   (gnus-summary-edit-article)
10559   (execute-kbd-macro (concat (this-command-keys) key))
10560   (gnus-article-edit-done))
10561
10562 ;;; Respooling
10563
10564 (defun gnus-summary-respool-query (&optional silent trace)
10565   "Query where the respool algorithm would put this article."
10566   (interactive)
10567   (let (gnus-mark-article-hook)
10568     (gnus-summary-select-article)
10569     (with-current-buffer gnus-original-article-buffer
10570       (let ((groups (nnmail-article-group 'identity trace)))
10571         (unless silent
10572           (if groups
10573               (message "This message would go to %s"
10574                        (mapconcat 'car groups ", "))
10575             (message "This message would go to no groups"))
10576           groups)))))
10577
10578 (defun gnus-summary-respool-trace ()
10579   "Trace where the respool algorithm would put this article.
10580 Display a buffer showing all fancy splitting patterns which matched."
10581   (interactive)
10582   (gnus-summary-respool-query nil t))
10583
10584 ;; Summary marking commands.
10585
10586 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10587   "Mark articles which has the same subject as read, and then select the next.
10588 If UNMARK is positive, remove any kind of mark.
10589 If UNMARK is negative, tick articles."
10590   (interactive "P")
10591   (when unmark
10592     (setq unmark (prefix-numeric-value unmark)))
10593   (let ((count
10594          (gnus-summary-mark-same-subject
10595           (gnus-summary-article-subject) unmark)))
10596     ;; Select next unread article.  If auto-select-same mode, should
10597     ;; select the first unread article.
10598     (gnus-summary-next-article t (and gnus-auto-select-same
10599                                       (gnus-summary-article-subject)))
10600     (gnus-message 7 "%d article%s marked as %s"
10601                   count (if (= count 1) " is" "s are")
10602                   (if unmark "unread" "read"))))
10603
10604 (defun gnus-summary-kill-same-subject (&optional unmark)
10605   "Mark articles which has the same subject as read.
10606 If UNMARK is positive, remove any kind of mark.
10607 If UNMARK is negative, tick articles."
10608   (interactive "P")
10609   (when unmark
10610     (setq unmark (prefix-numeric-value unmark)))
10611   (let ((count
10612          (gnus-summary-mark-same-subject
10613           (gnus-summary-article-subject) unmark)))
10614     ;; If marked as read, go to next unread subject.
10615     (when (null unmark)
10616       ;; Go to next unread subject.
10617       (gnus-summary-next-subject 1 t))
10618     (gnus-message 7 "%d articles are marked as %s"
10619                   count (if unmark "unread" "read"))))
10620
10621 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10622   "Mark articles with same SUBJECT as read, and return marked number.
10623 If optional argument UNMARK is positive, remove any kinds of marks.
10624 If optional argument UNMARK is negative, mark articles as unread instead."
10625   (let ((count 1))
10626     (save-excursion
10627       (cond
10628        ((null unmark)                   ; Mark as read.
10629         (while (and
10630                 (progn
10631                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10632                   (gnus-summary-show-thread) t)
10633                 (gnus-summary-find-subject subject))
10634           (setq count (1+ count))))
10635        ((> unmark 0)                    ; Tick.
10636         (while (and
10637                 (progn
10638                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10639                   (gnus-summary-show-thread) t)
10640                 (gnus-summary-find-subject subject))
10641           (setq count (1+ count))))
10642        (t                               ; Mark as unread.
10643         (while (and
10644                 (progn
10645                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10646                   (gnus-summary-show-thread) t)
10647                 (gnus-summary-find-subject subject))
10648           (setq count (1+ count)))))
10649       (gnus-set-mode-line 'summary)
10650       ;; Return the number of marked articles.
10651       count)))
10652
10653 (defun gnus-summary-mark-as-processable (n &optional unmark)
10654   "Set the process mark on the next N articles.
10655 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10656 the process mark instead.  The difference between N and the actual
10657 number of articles marked is returned."
10658   (interactive "P")
10659   (if (and (null n) (gnus-region-active-p))
10660       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10661     (setq n (prefix-numeric-value n))
10662     (let ((backward (< n 0))
10663           (n (abs n)))
10664       (while (and
10665               (> n 0)
10666               (if unmark
10667                   (gnus-summary-remove-process-mark
10668                    (gnus-summary-article-number))
10669                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10670               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10671         (setq n (1- n)))
10672       (when (/= 0 n)
10673         (gnus-message 7 "No more articles"))
10674       (gnus-summary-recenter)
10675       (gnus-summary-position-point)
10676       n)))
10677
10678 (defun gnus-summary-unmark-as-processable (n)
10679   "Remove the process mark from the next N articles.
10680 If N is negative, unmark backward instead.  The difference between N and
10681 the actual number of articles unmarked is returned."
10682   (interactive "P")
10683   (gnus-summary-mark-as-processable n t))
10684
10685 (defun gnus-summary-unmark-all-processable ()
10686   "Remove the process mark from all articles."
10687   (interactive)
10688   (save-excursion
10689     (while gnus-newsgroup-processable
10690       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10691   (gnus-summary-position-point))
10692
10693 (defun gnus-summary-add-mark (article type)
10694   "Mark ARTICLE with a mark of TYPE."
10695   (let ((vtype (car (assq type gnus-article-mark-lists)))
10696         var)
10697     (if (not vtype)
10698         (error "No such mark type: %s" type)
10699       (setq var (intern (format "gnus-newsgroup-%s" type)))
10700       (set var (cons article (symbol-value var)))
10701       (if (memq type '(processable cached replied forwarded recent saved))
10702           (gnus-summary-update-secondary-mark article)
10703         ;;; !!! This is bogus.  We should find out what primary
10704         ;;; !!! mark we want to set.
10705         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10706
10707 (defun gnus-summary-mark-as-expirable (n)
10708   "Mark N articles forward as expirable.
10709 If N is negative, mark backward instead.  The difference between N and
10710 the actual number of articles marked is returned."
10711   (interactive "p")
10712   (gnus-summary-mark-forward n gnus-expirable-mark))
10713
10714 (defun gnus-summary-mark-as-spam (n)
10715   "Mark N articles forward as spam.
10716 If N is negative, mark backward instead.  The difference between N and
10717 the actual number of articles marked is returned."
10718   (interactive "p")
10719   (gnus-summary-mark-forward n gnus-spam-mark))
10720
10721 (defun gnus-summary-mark-article-as-replied (article)
10722   "Mark ARTICLE as replied to and update the summary line.
10723 ARTICLE can also be a list of articles."
10724   (interactive (list (gnus-summary-article-number)))
10725   (let ((articles (if (listp article) article (list article))))
10726     (dolist (article articles)
10727       (unless (numberp article)
10728         (error "%s is not a number" article))
10729       (push article gnus-newsgroup-replied)
10730       (let ((inhibit-read-only t))
10731         (when (gnus-summary-goto-subject article nil t)
10732           (gnus-summary-update-secondary-mark article))))))
10733
10734 (defun gnus-summary-mark-article-as-forwarded (article)
10735   "Mark ARTICLE as forwarded and update the summary line.
10736 ARTICLE can also be a list of articles."
10737   (let ((articles (if (listp article) article (list article))))
10738     (dolist (article articles)
10739       (push article gnus-newsgroup-forwarded)
10740       (let ((inhibit-read-only t))
10741         (when (gnus-summary-goto-subject article nil t)
10742           (gnus-summary-update-secondary-mark article))))))
10743
10744 (defun gnus-summary-set-bookmark (article)
10745   "Set a bookmark in current article."
10746   (interactive (list (gnus-summary-article-number)))
10747   (when (or (not (get-buffer gnus-article-buffer))
10748             (not gnus-current-article)
10749             (not gnus-article-current)
10750             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10751     (error "No current article selected"))
10752   ;; Remove old bookmark, if one exists.
10753   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10754   ;; Set the new bookmark, which is on the form
10755   ;; (article-number . line-number-in-body).
10756   (push
10757    (cons article
10758          (with-current-buffer gnus-article-buffer
10759            (count-lines
10760             (min (point)
10761                  (save-excursion
10762                    (article-goto-body)
10763                    (point)))
10764             (point))))
10765    gnus-newsgroup-bookmarks)
10766   (gnus-message 6 "A bookmark has been added to the current article."))
10767
10768 (defun gnus-summary-remove-bookmark (article)
10769   "Remove the bookmark from the current article."
10770   (interactive (list (gnus-summary-article-number)))
10771   ;; Remove old bookmark, if one exists.
10772   (if (not (assq article gnus-newsgroup-bookmarks))
10773       (gnus-message 6 "No bookmark in current article.")
10774     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10775     (gnus-message 6 "Removed bookmark.")))
10776
10777 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10778 (defun gnus-summary-mark-as-dormant (n)
10779   "Mark N articles forward as dormant.
10780 If N is negative, mark backward instead.  The difference between N and
10781 the actual number of articles marked is returned."
10782   (interactive "p")
10783   (gnus-summary-mark-forward n gnus-dormant-mark))
10784
10785 (defun gnus-summary-set-process-mark (article)
10786   "Set the process mark on ARTICLE and update the summary line."
10787   (setq gnus-newsgroup-processable
10788         (cons article
10789               (delq article gnus-newsgroup-processable)))
10790   (when (gnus-summary-goto-subject article)
10791     (gnus-summary-show-thread)
10792     (gnus-summary-goto-subject article)
10793     (gnus-summary-update-secondary-mark article)))
10794
10795 (defun gnus-summary-remove-process-mark (&rest articles)
10796   "Remove the process mark from ARTICLES and update the summary line."
10797   (dolist (article articles)
10798     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10799     (when (gnus-summary-goto-subject article)
10800       (gnus-summary-show-thread)
10801       (gnus-summary-goto-subject article)
10802       (gnus-summary-update-secondary-mark article)))
10803   t)
10804
10805 (defun gnus-summary-set-saved-mark (article)
10806   "Set the process mark on ARTICLE and update the summary line."
10807   (push article gnus-newsgroup-saved)
10808   (when (gnus-summary-goto-subject article)
10809     (gnus-summary-update-secondary-mark article)))
10810
10811 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10812   "Mark N articles as read forwards.
10813 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10814 The difference between N and the actual number of articles marked is
10815 returned.
10816 If NO-EXPIRE, auto-expiry will be inhibited."
10817   (interactive "p")
10818   (gnus-summary-show-thread)
10819   (let ((backward (< n 0))
10820         (gnus-summary-goto-unread
10821          (and gnus-summary-goto-unread
10822               (not (eq gnus-summary-goto-unread 'never))
10823               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10824                                     gnus-ticked-mark gnus-dormant-mark)))))
10825         (n (abs n))
10826         (mark (or mark gnus-del-mark)))
10827     (while (and (> n 0)
10828                 (gnus-summary-mark-article nil mark no-expire)
10829                 (zerop (gnus-summary-next-subject
10830                         (if backward -1 1)
10831                         (and gnus-summary-goto-unread
10832                              (not (eq gnus-summary-goto-unread 'never)))
10833                         t)))
10834       (setq n (1- n)))
10835     (when (/= 0 n)
10836       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10837     (gnus-summary-recenter)
10838     (gnus-summary-position-point)
10839     (gnus-set-mode-line 'summary)
10840     n))
10841
10842 (defun gnus-summary-mark-article-as-read (mark)
10843   "Mark the current article quickly as read with MARK."
10844   (let ((article (gnus-summary-article-number)))
10845     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10846     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10847     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10848     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10849     (push (cons article mark) gnus-newsgroup-reads)
10850     ;; Possibly remove from cache, if that is used.
10851     (when gnus-use-cache
10852       (gnus-cache-enter-remove-article article))
10853     ;; Allow the backend to change the mark.
10854     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10855     ;; Check for auto-expiry.
10856     (when (and gnus-newsgroup-auto-expire
10857                (memq mark gnus-auto-expirable-marks))
10858       (setq mark gnus-expirable-mark)
10859       ;; Let the backend know about the mark change.
10860       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10861       (push article gnus-newsgroup-expirable))
10862     ;; Set the mark in the buffer.
10863     (gnus-summary-update-mark mark 'unread)
10864     t))
10865
10866 (defun gnus-summary-mark-article-as-unread (mark)
10867   "Mark the current article quickly as unread with MARK."
10868   (let* ((article (gnus-summary-article-number))
10869          (old-mark (gnus-summary-article-mark article)))
10870     ;; Allow the backend to change the mark.
10871     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10872     (if (eq mark old-mark)
10873         t
10874       (if (<= article 0)
10875           (progn
10876             (gnus-error 1 "Can't mark negative article numbers")
10877             nil)
10878         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10879         (setq gnus-newsgroup-spam-marked
10880               (delq article gnus-newsgroup-spam-marked))
10881         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10882         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10883         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10884         (cond ((= mark gnus-ticked-mark)
10885                (setq gnus-newsgroup-marked
10886                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10887                                               article)))
10888               ((= mark gnus-spam-mark)
10889                (setq gnus-newsgroup-spam-marked
10890                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10891                                               article)))
10892               ((= mark gnus-dormant-mark)
10893                (setq gnus-newsgroup-dormant
10894                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10895                                               article)))
10896               (t
10897                (setq gnus-newsgroup-unreads
10898                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10899                                               article))))
10900         (gnus-alist-pull article gnus-newsgroup-reads)
10901
10902         ;; See whether the article is to be put in the cache.
10903         (and gnus-use-cache
10904              (vectorp (gnus-summary-article-header article))
10905              (save-excursion
10906                (gnus-cache-possibly-enter-article
10907                 gnus-newsgroup-name article
10908                 (= mark gnus-ticked-mark)
10909                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10910
10911         ;; Fix the mark.
10912         (gnus-summary-update-mark mark 'unread)
10913         t))))
10914
10915 (defun gnus-summary-mark-article (&optional article mark no-expire)
10916   "Mark ARTICLE with MARK.  MARK can be any character.
10917 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10918 `??' (dormant) and `?E' (expirable).
10919 If MARK is nil, then the default character `?r' is used.
10920 If ARTICLE is nil, then the article on the current line will be
10921 marked.
10922 If NO-EXPIRE, auto-expiry will be inhibited."
10923   ;; The mark might be a string.
10924   (when (stringp mark)
10925     (setq mark (aref mark 0)))
10926   ;; If no mark is given, then we check auto-expiring.
10927   (when (null mark)
10928     (setq mark gnus-del-mark))
10929   (when (and (not no-expire)
10930              gnus-newsgroup-auto-expire
10931              (memq mark gnus-auto-expirable-marks))
10932     (setq mark gnus-expirable-mark))
10933   (let ((article (or article (gnus-summary-article-number)))
10934         (old-mark (gnus-summary-article-mark article)))
10935     ;; Allow the backend to change the mark.
10936     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10937     (if (eq mark old-mark)
10938         t
10939       (unless article
10940         (error "No article on current line"))
10941       (if (not (if (or (= mark gnus-unread-mark)
10942                        (= mark gnus-ticked-mark)
10943                        (= mark gnus-spam-mark)
10944                        (= mark gnus-dormant-mark))
10945                    (gnus-mark-article-as-unread article mark)
10946                  (gnus-mark-article-as-read article mark)))
10947           t
10948         ;; See whether the article is to be put in the cache.
10949         (and gnus-use-cache
10950              (not (= mark gnus-canceled-mark))
10951              (vectorp (gnus-summary-article-header article))
10952              (save-excursion
10953                (gnus-cache-possibly-enter-article
10954                 gnus-newsgroup-name article
10955                 (= mark gnus-ticked-mark)
10956                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10957
10958         (when (gnus-summary-goto-subject article nil t)
10959           (let ((inhibit-read-only t))
10960             (gnus-summary-show-thread)
10961             ;; Fix the mark.
10962             (gnus-summary-update-mark mark 'unread)
10963             t))))))
10964
10965 (defun gnus-summary-update-secondary-mark (article)
10966   "Update the secondary (read, process, cache) mark."
10967   (gnus-summary-update-mark
10968    (cond ((memq article gnus-newsgroup-processable)
10969           gnus-process-mark)
10970          ((memq article gnus-newsgroup-cached)
10971           gnus-cached-mark)
10972          ((memq article gnus-newsgroup-replied)
10973           gnus-replied-mark)
10974          ((memq article gnus-newsgroup-forwarded)
10975           gnus-forwarded-mark)
10976          ((memq article gnus-newsgroup-saved)
10977           gnus-saved-mark)
10978          ((memq article gnus-newsgroup-unseen)
10979           gnus-unseen-mark)
10980          (t gnus-no-mark))
10981    'replied)
10982   (when (gnus-visual-p 'summary-highlight 'highlight)
10983     (gnus-summary-highlight-line)
10984     (gnus-run-hooks 'gnus-summary-update-hook))
10985   t)
10986
10987 (defun gnus-summary-update-download-mark (article)
10988   "Update the download mark."
10989   (gnus-summary-update-mark
10990    (cond ((memq article gnus-newsgroup-undownloaded)
10991           gnus-undownloaded-mark)
10992          (gnus-newsgroup-agentized
10993           gnus-downloaded-mark)
10994          (t
10995           gnus-no-mark))
10996    'download)
10997   (gnus-summary-update-line t)
10998   t)
10999
11000 (defun gnus-summary-update-mark (mark type)
11001   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11002         (inhibit-read-only t))
11003     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11004     (when forward
11005       (when (looking-at "\r")
11006         (incf forward))
11007       (when (<= (+ forward (point)) (point-max))
11008         ;; Go to the right position on the line.
11009         (goto-char (+ forward (point)))
11010         ;; Replace the old mark with the new mark.
11011         (let ((to-insert
11012                (mm-subst-char-in-string
11013                 (char-after) mark
11014                 (buffer-substring (point) (1+ (point))))))
11015           (delete-region (point) (1+ (point)))
11016           (insert to-insert))
11017         ;; Optionally update the marks by some user rule.
11018         (when (eq type 'unread)
11019           (gnus-data-set-mark
11020            (gnus-data-find (gnus-summary-article-number)) mark)
11021           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11022
11023 (defun gnus-mark-article-as-read (article &optional mark)
11024   "Enter ARTICLE in the pertinent lists and remove it from others."
11025   ;; Make the article expirable.
11026   (let ((mark (or mark gnus-del-mark)))
11027     (setq gnus-newsgroup-expirable
11028           (if (= mark gnus-expirable-mark)
11029               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11030             (delq article gnus-newsgroup-expirable)))
11031     ;; Remove from unread and marked lists.
11032     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11033     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11034     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11035     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11036     (push (cons article mark) gnus-newsgroup-reads)
11037     ;; Possibly remove from cache, if that is used.
11038     (when gnus-use-cache
11039       (gnus-cache-enter-remove-article article))
11040     t))
11041
11042 (defun gnus-mark-article-as-unread (article &optional mark)
11043   "Enter ARTICLE in the pertinent lists and remove it from others."
11044   (let ((mark (or mark gnus-ticked-mark)))
11045     (if (<= article 0)
11046         (progn
11047           (gnus-error 1 "Can't mark negative article numbers")
11048           nil)
11049       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11050             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11051             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11052             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11053             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11054
11055       ;; Unsuppress duplicates?
11056       (when gnus-suppress-duplicates
11057         (gnus-dup-unsuppress-article article))
11058
11059       (cond ((= mark gnus-ticked-mark)
11060              (setq gnus-newsgroup-marked
11061                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11062             ((= mark gnus-spam-mark)
11063              (setq gnus-newsgroup-spam-marked
11064                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11065                                             article)))
11066             ((= mark gnus-dormant-mark)
11067              (setq gnus-newsgroup-dormant
11068                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11069             (t
11070              (setq gnus-newsgroup-unreads
11071                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11072       (gnus-alist-pull article gnus-newsgroup-reads)
11073       t)))
11074
11075 (defun gnus-summary-tick-article-forward (n)
11076   "Tick N articles forwards.
11077 If N is negative, tick backwards instead.
11078 The difference between N and the number of articles ticked is returned."
11079   (interactive "p")
11080   (gnus-summary-mark-forward n gnus-ticked-mark))
11081
11082 (defun gnus-summary-tick-article-backward (n)
11083   "Tick N articles backwards.
11084 The difference between N and the number of articles ticked is returned."
11085   (interactive "p")
11086   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11087
11088 (defun gnus-summary-tick-article (&optional article clear-mark)
11089   "Mark current article as unread.
11090 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11091 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11092   (interactive)
11093   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11094                                        gnus-ticked-mark)))
11095
11096 (defun gnus-summary-mark-as-read-forward (n)
11097   "Mark N articles as read forwards.
11098 If N is negative, mark backwards instead.
11099 The difference between N and the actual number of articles marked is
11100 returned."
11101   (interactive "p")
11102   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11103
11104 (defun gnus-summary-mark-as-read-backward (n)
11105   "Mark the N articles as read backwards.
11106 The difference between N and the actual number of articles marked is
11107 returned."
11108   (interactive "p")
11109   (gnus-summary-mark-forward
11110    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11111
11112 (defun gnus-summary-mark-as-read (&optional article mark)
11113   "Mark current article as read.
11114 ARTICLE specifies the article to be marked as read.
11115 MARK specifies a string to be inserted at the beginning of the line."
11116   (gnus-summary-mark-article article mark))
11117
11118 (defun gnus-summary-clear-mark-forward (n)
11119   "Clear marks from N articles forward.
11120 If N is negative, clear backward instead.
11121 The difference between N and the number of marks cleared is returned."
11122   (interactive "p")
11123   (gnus-summary-mark-forward n gnus-unread-mark))
11124
11125 (defun gnus-summary-clear-mark-backward (n)
11126   "Clear marks from N articles backward.
11127 The difference between N and the number of marks cleared is returned."
11128   (interactive "p")
11129   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11130
11131 (defun gnus-summary-mark-unread-as-read ()
11132   "Intended to be used by `gnus-mark-article-hook'."
11133   (when (memq gnus-current-article gnus-newsgroup-unreads)
11134     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11135
11136 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11137   "Intended to be used by `gnus-mark-article-hook'."
11138   (let ((mark (gnus-summary-article-mark)))
11139     (when (or (gnus-unread-mark-p mark)
11140               (gnus-read-mark-p mark))
11141       (gnus-summary-mark-article gnus-current-article
11142                                  (or new-mark gnus-read-mark)))))
11143
11144 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11145   "Intended to be used by `gnus-mark-article-hook'."
11146   (let ((mark (gnus-summary-article-mark)))
11147     (when (or (gnus-unread-mark-p mark)
11148               (gnus-read-mark-p mark))
11149       (gnus-summary-mark-article (gnus-summary-article-number)
11150                                  (or new-mark gnus-read-mark)))))
11151
11152 (defun gnus-summary-mark-unread-as-ticked ()
11153   "Intended to be used by `gnus-mark-article-hook'."
11154   (when (memq gnus-current-article gnus-newsgroup-unreads)
11155     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11156
11157 (defun gnus-summary-mark-region-as-read (point mark all)
11158   "Mark all unread articles between point and mark as read.
11159 If given a prefix, mark all articles between point and mark as read,
11160 even ticked and dormant ones."
11161   (interactive "r\nP")
11162   (save-excursion
11163     (let (article)
11164       (goto-char point)
11165       (beginning-of-line)
11166       (while (and
11167               (< (point) mark)
11168               (progn
11169                 (when (or all
11170                           (memq (setq article (gnus-summary-article-number))
11171                                 gnus-newsgroup-unreads))
11172                   (gnus-summary-mark-article article gnus-del-mark))
11173                 t)
11174               (gnus-summary-find-next))))))
11175
11176 (defun gnus-summary-mark-below (score mark)
11177   "Mark articles with score less than SCORE with MARK."
11178   (interactive "P\ncMark: ")
11179   (setq score (if score
11180                   (prefix-numeric-value score)
11181                 (or gnus-summary-default-score 0)))
11182   (with-current-buffer gnus-summary-buffer
11183     (goto-char (point-min))
11184     (while
11185         (progn
11186           (and (< (gnus-summary-article-score) score)
11187                (gnus-summary-mark-article nil mark))
11188           (gnus-summary-find-next)))))
11189
11190 (defun gnus-summary-kill-below (&optional score)
11191   "Mark articles with score below SCORE as read."
11192   (interactive "P")
11193   (gnus-summary-mark-below score gnus-killed-mark))
11194
11195 (defun gnus-summary-clear-above (&optional score)
11196   "Clear all marks from articles with score above SCORE."
11197   (interactive "P")
11198   (gnus-summary-mark-above score gnus-unread-mark))
11199
11200 (defun gnus-summary-tick-above (&optional score)
11201   "Tick all articles with score above SCORE."
11202   (interactive "P")
11203   (gnus-summary-mark-above score gnus-ticked-mark))
11204
11205 (defun gnus-summary-mark-above (score mark)
11206   "Mark articles with score over SCORE with MARK."
11207   (interactive "P\ncMark: ")
11208   (setq score (if score
11209                   (prefix-numeric-value score)
11210                 (or gnus-summary-default-score 0)))
11211   (with-current-buffer gnus-summary-buffer
11212     (goto-char (point-min))
11213     (while (and (progn
11214                   (when (> (gnus-summary-article-score) score)
11215                     (gnus-summary-mark-article nil mark))
11216                   t)
11217                 (gnus-summary-find-next)))))
11218
11219 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11220 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11221 (defun gnus-summary-limit-include-expunged (&optional no-error)
11222   "Display all the hidden articles that were expunged for low scores."
11223   (interactive)
11224   (let ((inhibit-read-only t))
11225     (let ((scored gnus-newsgroup-scored)
11226           headers h)
11227       (while scored
11228         (unless (gnus-summary-article-header (caar scored))
11229           (and (setq h (gnus-number-to-header (caar scored)))
11230                (< (cdar scored) gnus-summary-expunge-below)
11231                (push h headers)))
11232         (setq scored (cdr scored)))
11233       (if (not headers)
11234           (when (not no-error)
11235             (error "No expunged articles hidden"))
11236         (goto-char (point-min))
11237         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11238         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11239         (dolist (x headers)
11240           (push (mail-header-number x) gnus-newsgroup-limit))
11241         (gnus-summary-prepare-unthreaded (nreverse headers))
11242         (goto-char (point-min))
11243         (gnus-summary-position-point)
11244         t))))
11245
11246 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11247   "Mark all unread articles in this newsgroup as read.
11248 If prefix argument ALL is non-nil, ticked and dormant articles will
11249 also be marked as read.
11250 If QUIETLY is non-nil, no questions will be asked.
11251
11252 If TO-HERE is non-nil, it should be a point in the buffer.  All
11253 articles before (after, if REVERSE is set) this point will be marked
11254 as read.
11255
11256 Note that this function will only catch up the unread article
11257 in the current summary buffer limitation.
11258
11259 The number of articles marked as read is returned."
11260   (interactive "P")
11261   (prog1
11262       (save-excursion
11263         (when (or quietly
11264                   (not gnus-interactive-catchup) ;Without confirmation?
11265                   gnus-expert-user
11266                   (gnus-y-or-n-p
11267                    (if all
11268                        "Mark absolutely all articles as read? "
11269                      "Mark all unread articles as read? ")))
11270           (if (and not-mark
11271                    (not gnus-newsgroup-adaptive)
11272                    (not gnus-newsgroup-auto-expire)
11273                    (not gnus-suppress-duplicates)
11274                    (or (not gnus-use-cache)
11275                        (eq gnus-use-cache 'passive)))
11276               (progn
11277                 (when all
11278                   (setq gnus-newsgroup-marked nil
11279                         gnus-newsgroup-spam-marked nil
11280                         gnus-newsgroup-dormant nil))
11281                 (setq gnus-newsgroup-unreads
11282                       (gnus-sorted-nunion
11283                        (gnus-sorted-intersection gnus-newsgroup-unreads
11284                                                  gnus-newsgroup-downloadable)
11285                        (gnus-sorted-difference gnus-newsgroup-unfetched
11286                                                gnus-newsgroup-cached))))
11287             ;; We actually mark all articles as canceled, which we
11288             ;; have to do when using auto-expiry or adaptive scoring.
11289             (gnus-summary-show-all-threads)
11290             (if (and to-here reverse)
11291                 (progn
11292                   (goto-char to-here)
11293                   (gnus-summary-mark-current-read-and-unread-as-read
11294                    gnus-catchup-mark)
11295                   (while (gnus-summary-find-next (not all))
11296                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11297               (when (gnus-summary-first-subject (not all))
11298                 (while (and
11299                         (if to-here (< (point) to-here) t)
11300                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11301                         (gnus-summary-find-next (not all))))))
11302             (gnus-set-mode-line 'summary))
11303           t))
11304     (gnus-summary-position-point)))
11305
11306 (defun gnus-summary-catchup-to-here (&optional all)
11307   "Mark all unticked articles before the current one as read.
11308 If ALL is non-nil, also mark ticked and dormant articles as read."
11309   (interactive "P")
11310   (save-excursion
11311     (gnus-save-hidden-threads
11312       (let ((beg (point)))
11313         ;; We check that there are unread articles.
11314         (when (or all (gnus-summary-find-prev))
11315           (gnus-summary-catchup all t beg)))))
11316   (gnus-summary-position-point))
11317
11318 (defun gnus-summary-catchup-from-here (&optional all)
11319   "Mark all unticked articles after (and including) the current one as read.
11320 If ALL is non-nil, also mark ticked and dormant articles as read."
11321   (interactive "P")
11322   (save-excursion
11323     (gnus-save-hidden-threads
11324       (let ((beg (point)))
11325         ;; We check that there are unread articles.
11326         (when (or all (gnus-summary-find-next))
11327           (gnus-summary-catchup all t beg nil t)))))
11328   (gnus-summary-position-point))
11329
11330 (defun gnus-summary-catchup-all (&optional quietly)
11331   "Mark all articles in this newsgroup as read.
11332 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11333 instead, which marks only unread articles as read."
11334   (interactive "P")
11335   (gnus-summary-catchup t quietly))
11336
11337 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11338   "Mark all unread articles in this group as read, then exit.
11339 If prefix argument ALL is non-nil, all articles are marked as read.
11340 If QUIETLY is non-nil, no questions will be asked."
11341   (interactive "P")
11342   (when (gnus-summary-catchup all quietly nil 'fast)
11343     ;; Select next newsgroup or exit.
11344     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11345              (eq gnus-auto-select-next 'quietly))
11346         (gnus-summary-next-group nil)
11347       (gnus-summary-exit))))
11348
11349 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11350   "Mark all articles in this newsgroup as read, and then exit.
11351 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11352 instead, which marks only unread articles as read."
11353   (interactive "P")
11354   (gnus-summary-catchup-and-exit t quietly))
11355
11356 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11357   "Mark all articles in this group as read and select the next group.
11358 If given a prefix, mark all articles, unread as well as ticked, as
11359 read."
11360   (interactive "P")
11361   (save-excursion
11362     (gnus-summary-catchup all))
11363   (gnus-summary-next-group))
11364
11365 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11366   "Mark all articles in this group as read and select the previous group.
11367 If given a prefix, mark all articles, unread as well as ticked, as
11368 read."
11369   (interactive "P")
11370   (save-excursion
11371     (gnus-summary-catchup all))
11372   (gnus-summary-next-group nil nil t))
11373
11374 ;;;
11375 ;;; with article
11376 ;;;
11377
11378 (defmacro gnus-with-article (article &rest forms)
11379   "Select ARTICLE and perform FORMS in the original article buffer.
11380 Then replace the article with the result."
11381   `(progn
11382      ;; We don't want the article to be marked as read.
11383      (let (gnus-mark-article-hook)
11384        (gnus-summary-select-article t t nil ,article))
11385      (set-buffer gnus-original-article-buffer)
11386      ,@forms
11387      (if (not (gnus-check-backend-function
11388                'request-replace-article (car gnus-article-current)))
11389          (gnus-message 5 "Read-only group; not replacing")
11390        (unless (gnus-request-replace-article
11391                 ,article (car gnus-article-current)
11392                 (current-buffer) t)
11393          (error "Couldn't replace article")))
11394      ;; The cache and backlog have to be flushed somewhat.
11395      (when gnus-keep-backlog
11396        (gnus-backlog-remove-article
11397         (car gnus-article-current) (cdr gnus-article-current)))
11398      (when gnus-use-cache
11399        (gnus-cache-update-article
11400         (car gnus-article-current) (cdr gnus-article-current)))))
11401
11402 (put 'gnus-with-article 'lisp-indent-function 1)
11403 (put 'gnus-with-article 'edebug-form-spec '(form body))
11404
11405 ;; Thread-based commands.
11406
11407 (defun gnus-summary-articles-in-thread (&optional article)
11408   "Return a list of all articles in the current thread.
11409 If ARTICLE is non-nil, return all articles in the thread that starts
11410 with that article."
11411   (let* ((article (or article (gnus-summary-article-number)))
11412          (data (gnus-data-find-list article))
11413          (top-level (gnus-data-level (car data)))
11414          (top-subject
11415           (cond ((null gnus-thread-operation-ignore-subject)
11416                  (gnus-simplify-subject-re
11417                   (mail-header-subject (gnus-data-header (car data)))))
11418                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11419                  (gnus-simplify-subject-fuzzy
11420                   (mail-header-subject (gnus-data-header (car data)))))
11421                 (t nil)))
11422          (end-point (save-excursion
11423                       (goto-char (gnus-data-pos (car data)))
11424                       (if (gnus-summary-go-to-next-thread)
11425                           (point) (point-max))))
11426          articles)
11427     (while (and data
11428                 (< (gnus-data-pos (car data)) end-point))
11429       (when (or (not top-subject)
11430                 (string= top-subject
11431                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11432                              (gnus-simplify-subject-fuzzy
11433                               (mail-header-subject
11434                                (gnus-data-header (car data))))
11435                            (gnus-simplify-subject-re
11436                             (mail-header-subject
11437                              (gnus-data-header (car data)))))))
11438         (push (gnus-data-number (car data)) articles))
11439       (unless (and (setq data (cdr data))
11440                    (> (gnus-data-level (car data)) top-level))
11441         (setq data nil)))
11442     ;; Return the list of articles.
11443     (nreverse articles)))
11444
11445 (defun gnus-summary-rethread-current ()
11446   "Rethread the thread the current article is part of."
11447   (interactive)
11448   (let* ((gnus-show-threads t)
11449          (article (gnus-summary-article-number))
11450          (id (mail-header-id (gnus-summary-article-header)))
11451          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11452     (unless id
11453       (error "No article on the current line"))
11454     (gnus-rebuild-thread id)
11455     (gnus-summary-goto-subject article)))
11456
11457 (defun gnus-summary-reparent-thread ()
11458   "Make the current article child of the marked (or previous) article.
11459
11460 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11461 is non-nil or the Subject: of both articles are the same."
11462   (interactive)
11463   (unless (not (gnus-group-read-only-p))
11464     (error "The current newsgroup does not support article editing"))
11465   (unless (<= (length gnus-newsgroup-processable) 1)
11466     (error "No more than one article may be marked"))
11467   (let ((child (gnus-summary-article-number))
11468         ;; First grab the marked article, otherwise one line up.
11469         (parent (if (not (null gnus-newsgroup-processable))
11470                     (car gnus-newsgroup-processable)
11471                   (save-excursion
11472                     (if (eq (forward-line -1) 0)
11473                         (gnus-summary-article-number)
11474                       (error "Beginning of summary buffer"))))))
11475     (gnus-summary-reparent-children parent (list child))))
11476
11477 (defun gnus-summary-reparent-children (parent children)
11478   "Make PARENT the parent of CHILDREN.
11479 When called interactively, PARENT is the current article and CHILDREN
11480 are the process-marked articles."
11481   (interactive
11482    (list (gnus-summary-article-number)
11483          (gnus-summary-work-articles nil)))
11484   (dolist (child children)
11485     (save-window-excursion
11486       (let ((gnus-article-buffer " *reparent*"))
11487         (unless (not (eq parent child))
11488           (error "An article may not be self-referential"))
11489         (let ((message-id (mail-header-id
11490                            (gnus-summary-article-header parent))))
11491           (unless (and message-id (not (equal message-id "")))
11492             (error "No message-id in desired parent"))
11493           (gnus-with-article child
11494             (save-restriction
11495               (goto-char (point-min))
11496               (message-narrow-to-head)
11497               (if (re-search-forward "^References: " nil t)
11498                   (progn
11499                     (re-search-forward "^[^ \t]" nil t)
11500                     (forward-line -1)
11501                     (end-of-line)
11502                     (insert " " message-id))
11503                 (insert "References: " message-id "\n"))))
11504           (set-buffer gnus-summary-buffer)
11505           (gnus-summary-unmark-all-processable)
11506           (gnus-summary-update-article child)
11507           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11508             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11509           (gnus-summary-rethread-current)
11510           (gnus-message 3 "Article %d is now the child of article %d"
11511                         child parent))))))
11512
11513 (defun gnus-summary-toggle-threads (&optional arg)
11514   "Toggle showing conversation threads.
11515 If ARG is positive number, turn showing conversation threads on."
11516   (interactive "P")
11517   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11518     (setq gnus-show-threads
11519           (if (null arg) (not gnus-show-threads)
11520             (> (prefix-numeric-value arg) 0)))
11521     (gnus-summary-prepare)
11522     (gnus-summary-goto-subject current)
11523     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11524     (gnus-summary-position-point)))
11525
11526 (eval-and-compile
11527   (if (fboundp 'remove-overlays)
11528       (defalias 'gnus-remove-overlays 'remove-overlays)
11529     (defun gnus-remove-overlays (beg end name val)
11530       "Clear BEG and END of overlays whose property NAME has value VAL.
11531 For compatibility with XEmacs."
11532       (dolist (ov (gnus-overlays-in beg end))
11533         (when (eq (gnus-overlay-get ov name) val)
11534           (gnus-delete-overlay ov))))))
11535
11536 (defun gnus-summary-show-all-threads ()
11537   "Show all threads."
11538   (interactive)
11539   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11540   (gnus-summary-position-point))
11541
11542 (defsubst gnus-summary--inv (p)
11543   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11544
11545 (defun gnus-summary-show-thread ()
11546   "Show thread subtrees.
11547 Returns nil if no thread was there to be shown."
11548   (interactive)
11549   (let* ((orig (point))
11550          (end (point-at-eol))
11551          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11552          ;; Leave point at bol
11553          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11554          (eoi (when end
11555                 (if (fboundp 'next-single-char-property-change)
11556                     (or (next-single-char-property-change end 'invisible)
11557                         (point-max))
11558                   (while (progn
11559                            (end-of-line 2)
11560                            (and (not (eobp))
11561                                 (eq (get-char-property (point) 'invisible)
11562                                     'gnus-sum))))
11563                   (point)))))
11564     (when eoi
11565       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11566       (goto-char orig)
11567       (gnus-summary-position-point)
11568       eoi)))
11569
11570 (defun gnus-summary-maybe-hide-threads ()
11571   "If requested, hide the threads that should be hidden."
11572   (when (and gnus-show-threads
11573              gnus-thread-hide-subtree)
11574     (gnus-summary-hide-all-threads
11575      (if (or (consp gnus-thread-hide-subtree)
11576              (functionp gnus-thread-hide-subtree))
11577          (gnus-make-predicate gnus-thread-hide-subtree)
11578        nil))))
11579
11580 ;;; Hiding predicates.
11581
11582 (defun gnus-article-unread-p (header)
11583   (memq (mail-header-number header) gnus-newsgroup-unreads))
11584
11585 (defun gnus-article-unseen-p (header)
11586   (memq (mail-header-number header) gnus-newsgroup-unseen))
11587
11588 (defun gnus-map-articles (predicate articles)
11589   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11590   (apply 'gnus-or (mapcar predicate
11591                           (mapcar (lambda (number)
11592                                     (gnus-summary-article-header number))
11593                                   articles))))
11594
11595 (defun gnus-summary-hide-all-threads (&optional predicate)
11596   "Hide all thread subtrees.
11597 If PREDICATE is supplied, threads that satisfy this predicate
11598 will not be hidden."
11599   (interactive)
11600   (save-excursion
11601     (goto-char (point-min))
11602     (let ((end nil)
11603           (count 0))
11604       (while (not end)
11605         (incf count)
11606         (when (zerop (mod count 1000))
11607           (message "Hiding all threads... %d" count))
11608         (when (or (not predicate)
11609                   (gnus-map-articles
11610                    predicate (gnus-summary-article-children)))
11611             (gnus-summary-hide-thread))
11612         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11613   (gnus-summary-position-point))
11614
11615 (defun gnus-summary-hide-thread ()
11616   "Hide thread subtrees.
11617 If PREDICATE is supplied, threads that satisfy this predicate
11618 will not be hidden.
11619 Returns nil if no threads were there to be hidden."
11620   (interactive)
11621   (let ((start (point))
11622         (starteol (line-end-position))
11623         (article (gnus-summary-article-number)))
11624     (goto-char start)
11625     ;; Go forward until either the buffer ends or the subthread ends.
11626     (when (and (not (eobp))
11627                (or (zerop (gnus-summary-next-thread 1 t))
11628                    (goto-char (point-max))))
11629       (if (and (> (point) start)
11630                ;; FIXME: this should actually search for a non-invisible \n.
11631                (search-backward "\n" start t))
11632           (progn
11633             (when (> (point) starteol)
11634               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11635               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11636                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11637                 (gnus-overlay-put ol 'evaporate t)))
11638             (gnus-summary-goto-subject article)
11639             (when (> start (point))
11640               (message "Hiding the thread moved us backwards, aborting!")
11641               (goto-char (point-max))))
11642         (goto-char start)
11643         nil))))
11644
11645 (defun gnus-summary-go-to-next-thread (&optional previous)
11646   "Go to the same level (or less) next thread.
11647 If PREVIOUS is non-nil, go to previous thread instead.
11648 Return the article number moved to, or nil if moving was impossible."
11649   (let ((level (gnus-summary-thread-level))
11650         (way (if previous -1 1))
11651         (beg (point)))
11652     (forward-line way)
11653     (while (and (not (eobp))
11654                 (< level (gnus-summary-thread-level)))
11655       (forward-line way))
11656     (if (eobp)
11657         (progn
11658           (goto-char beg)
11659           nil)
11660       (setq beg (point))
11661       (prog1
11662           (gnus-summary-article-number)
11663         (goto-char beg)))))
11664
11665 (defun gnus-summary-next-thread (n &optional silent)
11666   "Go to the same level next N'th thread.
11667 If N is negative, search backward instead.
11668 Returns the difference between N and the number of skips actually
11669 done.
11670
11671 If SILENT, don't output messages."
11672   (interactive "p")
11673   (let ((backward (< n 0))
11674         (n (abs n)))
11675     (while (and (> n 0)
11676                 (gnus-summary-go-to-next-thread backward))
11677       (decf n))
11678     (unless silent
11679       (gnus-summary-position-point))
11680     (when (and (not silent) (/= 0 n))
11681       (gnus-message 7 "No more threads"))
11682     n))
11683
11684 (defun gnus-summary-prev-thread (n)
11685   "Go to the same level previous N'th thread.
11686 Returns the difference between N and the number of skips actually
11687 done."
11688   (interactive "p")
11689   (gnus-summary-next-thread (- n)))
11690
11691 (defun gnus-summary-go-down-thread ()
11692   "Go down one level in the current thread."
11693   (let ((children (gnus-summary-article-children)))
11694     (when children
11695       (gnus-summary-goto-subject (car children)))))
11696
11697 (defun gnus-summary-go-up-thread ()
11698   "Go up one level in the current thread."
11699   (let ((parent (gnus-summary-article-parent)))
11700     (when parent
11701       (gnus-summary-goto-subject parent))))
11702
11703 (defun gnus-summary-down-thread (n)
11704   "Go down thread N steps.
11705 If N is negative, go up instead.
11706 Returns the difference between N and how many steps down that were
11707 taken."
11708   (interactive "p")
11709   (let ((up (< n 0))
11710         (n (abs n)))
11711     (while (and (> n 0)
11712                 (if up (gnus-summary-go-up-thread)
11713                   (gnus-summary-go-down-thread)))
11714       (setq n (1- n)))
11715     (gnus-summary-position-point)
11716     (when (/= 0 n)
11717       (gnus-message 7 "Can't go further"))
11718     n))
11719
11720 (defun gnus-summary-up-thread (n)
11721   "Go up thread N steps.
11722 If N is negative, go down instead.
11723 Returns the difference between N and how many steps down that were
11724 taken."
11725   (interactive "p")
11726   (gnus-summary-down-thread (- n)))
11727
11728 (defun gnus-summary-top-thread ()
11729   "Go to the top of the thread."
11730   (interactive)
11731   (while (gnus-summary-go-up-thread))
11732   (gnus-summary-article-number))
11733
11734 (defun gnus-summary-expire-thread ()
11735   "Mark articles under current thread as expired."
11736   (interactive)
11737   (gnus-summary-kill-thread 0))
11738
11739 (defun gnus-summary-kill-thread (&optional unmark)
11740   "Mark articles under current thread as read.
11741 If the prefix argument is positive, remove any kinds of marks.
11742 If the prefix argument is zero, mark thread as expired.
11743 If the prefix argument is negative, tick articles instead."
11744   (interactive "P")
11745   (when unmark
11746     (setq unmark (prefix-numeric-value unmark)))
11747   (let ((articles (gnus-summary-articles-in-thread))
11748         (hide (or (null unmark) (= unmark 0))))
11749     (save-excursion
11750       ;; Expand the thread.
11751       (gnus-summary-show-thread)
11752       ;; Mark all the articles.
11753       (while articles
11754         (gnus-summary-goto-subject (car articles))
11755         (cond ((null unmark)
11756                (gnus-summary-mark-article-as-read gnus-killed-mark))
11757               ((> unmark 0)
11758                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11759               ((= unmark 0)
11760                (gnus-summary-mark-article nil gnus-expirable-mark))
11761               (t
11762                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11763         (setq articles (cdr articles))))
11764     ;; Hide killed subtrees when hide is true.
11765     (and hide
11766          gnus-thread-hide-killed
11767          (gnus-summary-hide-thread))
11768     ;; If hide is t, go to next unread subject.
11769     (when hide
11770       ;; Go to next unread subject.
11771       (gnus-summary-next-subject 1 t)))
11772   (gnus-set-mode-line 'summary))
11773
11774 ;; Summary sorting commands
11775
11776 (defun gnus-summary-sort-by-number (&optional reverse)
11777   "Sort the summary buffer by article number.
11778 Argument REVERSE means reverse order."
11779   (interactive "P")
11780   (gnus-summary-sort 'number reverse))
11781
11782 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11783   "Sort the summary buffer by most recent article number.
11784 Argument REVERSE means reverse order."
11785   (interactive "P")
11786   (gnus-summary-sort 'most-recent-number reverse))
11787
11788 (defun gnus-summary-sort-by-random (&optional reverse)
11789   "Randomize the order in the summary buffer.
11790 Argument REVERSE means to randomize in reverse order."
11791   (interactive "P")
11792   (gnus-summary-sort 'random reverse))
11793
11794 (defun gnus-summary-sort-by-author (&optional reverse)
11795   "Sort the summary buffer by author name alphabetically.
11796 If `case-fold-search' is non-nil, case of letters is ignored.
11797 Argument REVERSE means reverse order."
11798   (interactive "P")
11799   (gnus-summary-sort 'author reverse))
11800
11801 (defun gnus-summary-sort-by-recipient (&optional reverse)
11802   "Sort the summary buffer by recipient name alphabetically.
11803 If `case-fold-search' is non-nil, case of letters is ignored.
11804 Argument REVERSE means reverse order."
11805   (interactive "P")
11806   (gnus-summary-sort 'recipient reverse))
11807
11808 (defun gnus-summary-sort-by-subject (&optional reverse)
11809   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11810 If `case-fold-search' is non-nil, case of letters is ignored.
11811 Argument REVERSE means reverse order."
11812   (interactive "P")
11813   (gnus-summary-sort 'subject reverse))
11814
11815 (defun gnus-summary-sort-by-date (&optional reverse)
11816   "Sort the summary buffer by date.
11817 Argument REVERSE means reverse order."
11818   (interactive "P")
11819   (gnus-summary-sort 'date reverse))
11820
11821 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11822   "Sort the summary buffer by most recent date.
11823 Argument REVERSE means reverse order."
11824   (interactive "P")
11825   (gnus-summary-sort 'most-recent-date reverse))
11826
11827 (defun gnus-summary-sort-by-score (&optional reverse)
11828   "Sort the summary buffer by score.
11829 Argument REVERSE means reverse order."
11830   (interactive "P")
11831   (gnus-summary-sort 'score reverse))
11832
11833 (defun gnus-summary-sort-by-lines (&optional reverse)
11834   "Sort the summary buffer by the number of lines.
11835 Argument REVERSE means reverse order."
11836   (interactive "P")
11837   (gnus-summary-sort 'lines reverse))
11838
11839 (defun gnus-summary-sort-by-chars (&optional reverse)
11840   "Sort the summary buffer by article length.
11841 Argument REVERSE means reverse order."
11842   (interactive "P")
11843   (gnus-summary-sort 'chars reverse))
11844
11845 (defun gnus-summary-sort-by-original (&optional reverse)
11846   "Sort the summary buffer using the default sorting method.
11847 Argument REVERSE means reverse order."
11848   (interactive "P")
11849   (let* ((inhibit-read-only t)
11850          (gnus-summary-prepare-hook nil))
11851     ;; We do the sorting by regenerating the threads.
11852     (gnus-summary-prepare)
11853     ;; Hide subthreads if needed.
11854     (gnus-summary-maybe-hide-threads)))
11855
11856 (defun gnus-summary-sort (predicate reverse)
11857   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11858   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11859          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11860          (gnus-thread-sort-functions
11861           (if (not reverse)
11862               thread
11863             `(lambda (t1 t2)
11864                (,thread t2 t1))))
11865          (gnus-sort-gathered-threads-function
11866           gnus-thread-sort-functions)
11867          (gnus-article-sort-functions
11868           (if (not reverse)
11869               article
11870             `(lambda (t1 t2)
11871                (,article t2 t1))))
11872          (inhibit-read-only t)
11873          (gnus-summary-prepare-hook nil))
11874     ;; We do the sorting by regenerating the threads.
11875     (gnus-summary-prepare)
11876     ;; Hide subthreads if needed.
11877     (gnus-summary-maybe-hide-threads)))
11878
11879 ;; Summary saving commands.
11880
11881 (defun gnus-summary-save-article (&optional n not-saved)
11882   "Save the current article using the default saver function.
11883 If N is a positive number, save the N next articles.
11884 If N is a negative number, save the N previous articles.
11885 If N is nil and any articles have been marked with the process mark,
11886 save those articles instead.
11887 The variable `gnus-default-article-saver' specifies the saver function.
11888
11889 If the optional second argument NOT-SAVED is non-nil, articles saved
11890 will not be marked as saved."
11891   (interactive "P")
11892   (require 'gnus-art)
11893   (let* ((articles (gnus-summary-work-articles n))
11894          (save-buffer (save-excursion
11895                         (nnheader-set-temp-buffer " *Gnus Save*")))
11896          (num (length articles))
11897          ;; Whether to save decoded articles or raw articles.
11898          (decode (when gnus-article-save-coding-system
11899                    (get gnus-default-article-saver :decode)))
11900          ;; When saving many articles in a single file, use the other
11901          ;; function to save articles other than the first one.
11902          (saver2 (get gnus-default-article-saver :function))
11903          (gnus-prompt-before-saving (if saver2
11904                                         t
11905                                       gnus-prompt-before-saving))
11906          (gnus-default-article-saver gnus-default-article-saver)
11907          header file)
11908     (dolist (article articles)
11909       (setq header (gnus-summary-article-header article))
11910       (if (not (vectorp header))
11911           ;; This is a pseudo-article.
11912           (if (assq 'name header)
11913               (gnus-copy-file (cdr (assq 'name header)))
11914             (gnus-message 1 "Article %d is unsaveable" article))
11915         ;; This is a real article.
11916         (save-window-excursion
11917           (gnus-summary-select-article decode decode nil article)
11918           (gnus-summary-goto-subject article))
11919         (with-current-buffer save-buffer
11920           (erase-buffer)
11921           (insert-buffer-substring (if decode
11922                                        gnus-article-buffer
11923                                      gnus-original-article-buffer)))
11924         (setq file (gnus-article-save save-buffer file num))
11925         (gnus-summary-remove-process-mark article)
11926         (unless not-saved
11927           (gnus-summary-set-saved-mark article)))
11928       (when saver2
11929         (setq gnus-default-article-saver saver2
11930               saver2 nil)))
11931     (gnus-kill-buffer save-buffer)
11932     (gnus-summary-position-point)
11933     (gnus-set-mode-line 'summary)
11934     n))
11935
11936 (defun gnus-summary-pipe-output (&optional n sym)
11937   "Pipe the current article to a subprocess.
11938 If N is a positive number, pipe the N next articles.
11939 If N is a negative number, pipe the N previous articles.
11940 If N is nil and any articles have been marked with the process mark,
11941 pipe those articles instead.
11942 The default command to which articles are piped is specified by the
11943 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11944 will be prompted for the command.
11945
11946 The properties `:decode' and `:headers' that are put to the function
11947 symbol `gnus-summary-save-in-pipe' control whether this function
11948 decodes articles and what headers to keep (see the doc string for the
11949 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11950 is neither omitted nor the symbol `r', force including all headers
11951 regardless of the `:headers' property.  If it is the symbol `r',
11952 articles that are not decoded and include all headers will be piped
11953 no matter what the properties `:decode' and `:headers' are."
11954   (interactive (gnus-interactive "P\ny"))
11955   (require 'gnus-art)
11956   (let* ((articles (gnus-summary-work-articles n))
11957          (result-buffer "*Shell Command Output*")
11958          (all-headers (not (memq sym '(nil r))))
11959          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11960          (raw (eq sym 'r))
11961          (headers (get 'gnus-summary-save-in-pipe :headers))
11962          command result)
11963     (unless (numberp (car articles))
11964       (error "No article to pipe"))
11965     (setq command (gnus-read-shell-command
11966                    (concat "Shell command on "
11967                            (if (cdr articles)
11968                                (format "these %d articles" (length articles))
11969                              "this article")
11970                            ": ")
11971                    gnus-summary-pipe-output-default-command))
11972     (when (string-equal command "")
11973       (error "A command is required"))
11974     (when all-headers
11975       (put 'gnus-summary-save-in-pipe :headers nil))
11976     (unwind-protect
11977         (while articles
11978           (gnus-summary-goto-subject (pop articles))
11979           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11980           (when (and (get-buffer result-buffer)
11981                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11982             (setq result (concat result (with-current-buffer result-buffer
11983                                           (buffer-string))))))
11984       (put 'gnus-summary-save-in-pipe :headers headers))
11985     (unless (zerop (length result))
11986       (if (with-current-buffer (get-buffer-create result-buffer)
11987             (erase-buffer)
11988             (insert result)
11989             (prog1
11990                 (and (= (count-lines (point-min) (point)) 1)
11991                      (progn
11992                        (end-of-line 0)
11993                        (<= (current-column)
11994                            (window-width (minibuffer-window)))))
11995               (goto-char (point-min))))
11996           (message "%s" (substring result 0 -1))
11997         (message nil)
11998         (gnus-configure-windows 'pipe)))))
11999
12000 (defun gnus-summary-save-article-mail (&optional arg)
12001   "Append the current article to a Unix mail box file.
12002 If N is a positive number, save the N next articles.
12003 If N is a negative number, save the N previous articles.
12004 If N is nil and any articles have been marked with the process mark,
12005 save those articles instead."
12006   (interactive "P")
12007   (require 'gnus-art)
12008   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12009     (gnus-summary-save-article arg)))
12010
12011 (defun gnus-summary-save-article-rmail (&optional arg)
12012   "Append the current article to an rmail file.
12013 If N is a positive number, save the N next articles.
12014 If N is a negative number, save the N previous articles.
12015 If N is nil and any articles have been marked with the process mark,
12016 save those articles instead."
12017   (interactive "P")
12018   (require 'gnus-art)
12019   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12020     (gnus-summary-save-article arg)))
12021
12022 (defun gnus-summary-save-article-file (&optional arg)
12023   "Append the current article to a file.
12024 If N is a positive number, save the N next articles.
12025 If N is a negative number, save the N previous articles.
12026 If N is nil and any articles have been marked with the process mark,
12027 save those articles instead."
12028   (interactive "P")
12029   (require 'gnus-art)
12030   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12031     (gnus-summary-save-article arg)))
12032
12033 (defun gnus-summary-write-article-file (&optional arg)
12034   "Write the current article to a file, deleting the previous file.
12035 If N is a positive number, save the N next articles.
12036 If N is a negative number, save the N previous articles.
12037 If N is nil and any articles have been marked with the process mark,
12038 save those articles instead."
12039   (interactive "P")
12040   (require 'gnus-art)
12041   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12042     (gnus-summary-save-article arg)))
12043
12044 (defun gnus-summary-save-article-body-file (&optional arg)
12045   "Append the current article body to a file.
12046 If N is a positive number, save the N next articles.
12047 If N is a negative number, save the N previous articles.
12048 If N is nil and any articles have been marked with the process mark,
12049 save those articles instead."
12050   (interactive "P")
12051   (require 'gnus-art)
12052   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12053     (gnus-summary-save-article arg)))
12054
12055 (defun gnus-summary-write-article-body-file (&optional arg)
12056   "Write the current article body to a file, deleting the previous file.
12057 If N is a positive number, save the N next articles.
12058 If N is a negative number, save the N previous articles.
12059 If N is nil and any articles have been marked with the process mark,
12060 save those articles instead."
12061   (interactive "P")
12062   (require 'gnus-art)
12063   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12064     (gnus-summary-save-article arg)))
12065
12066 (defun gnus-summary-muttprint (&optional arg)
12067   "Print the current article using Muttprint.
12068 If N is a positive number, save the N next articles.
12069 If N is a negative number, save the N previous articles.
12070 If N is nil and any articles have been marked with the process mark,
12071 save those articles instead."
12072   (interactive "P")
12073   (require 'gnus-art)
12074   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12075     (gnus-summary-save-article arg t)))
12076
12077 (defun gnus-summary-pipe-message (program)
12078   "Pipe the current article through PROGRAM."
12079   (interactive "sProgram: ")
12080   (gnus-summary-select-article)
12081   (let ((mail-header-separator ""))
12082     (gnus-eval-in-buffer-window gnus-article-buffer
12083       (save-restriction
12084         (widen)
12085         (let ((start (window-start))
12086               (inhibit-read-only t))
12087           (message-pipe-buffer-body program)
12088           (set-window-start (get-buffer-window (current-buffer)) start))))))
12089
12090 (defun gnus-get-split-value (methods)
12091   "Return a value based on the split METHODS."
12092   (let (split-name method result match)
12093     (when methods
12094       (with-current-buffer gnus-original-article-buffer
12095         (save-restriction
12096           (nnheader-narrow-to-headers)
12097           (while (and methods (not split-name))
12098             (goto-char (point-min))
12099             (setq method (pop methods))
12100             (setq match (car method))
12101             (when (cond
12102                    ((stringp match)
12103                     ;; Regular expression.
12104                     (ignore-errors
12105                       (re-search-forward match nil t)))
12106                    ((functionp match)
12107                     ;; Function.
12108                     (save-restriction
12109                       (widen)
12110                       (setq result (funcall match gnus-newsgroup-name))))
12111                    ((consp match)
12112                     ;; Form.
12113                     (save-restriction
12114                       (widen)
12115                       (setq result (eval match)))))
12116               (setq split-name (cdr method))
12117               (cond ((stringp result)
12118                      (push (expand-file-name
12119                             result gnus-article-save-directory)
12120                            split-name))
12121                     ((consp result)
12122                      (setq split-name (append result split-name)))))))))
12123     (nreverse split-name)))
12124
12125 (defun gnus-valid-move-group-p (group)
12126   (and (symbolp group)
12127        (boundp group)
12128        (symbol-name group)
12129        (symbol-value group)
12130        (gnus-get-function (gnus-find-method-for-group
12131                            (symbol-name group)) 'request-accept-article t)))
12132
12133 (defun gnus-read-move-group-name (prompt default articles prefix)
12134   "Read a group name."
12135   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12136          (minibuffer-confirm-incomplete nil) ; XEmacs
12137          (prom
12138           (format "%s %s to"
12139                   prompt
12140                   (if (> (length articles) 1)
12141                       (format "these %d articles" (length articles))
12142                     "this article")))
12143          (to-newsgroup
12144           (cond
12145            ((null split-name)
12146             (gnus-group-completing-read
12147              prom
12148              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12149              nil prefix nil default))
12150            ((= 1 (length 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 'gnus-group-history (car split-name)))
12155            (t
12156             (gnus-completing-read
12157              prom (nreverse split-name) nil nil 'gnus-group-history))))
12158          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12159          encoded)
12160     (when to-newsgroup
12161       (if (or (string= to-newsgroup "")
12162               (string= to-newsgroup prefix))
12163           (setq to-newsgroup default))
12164       (unless to-newsgroup
12165         (error "No group name entered"))
12166       (setq encoded (mm-encode-coding-string
12167                      to-newsgroup
12168                      (gnus-group-name-charset to-method to-newsgroup)))
12169       (or (gnus-active encoded)
12170           (gnus-activate-group encoded nil nil to-method)
12171           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12172                                      to-newsgroup))
12173               (or (and (gnus-request-create-group encoded to-method)
12174                        (gnus-activate-group encoded nil nil to-method)
12175                        (gnus-subscribe-group encoded))
12176                   (error "Couldn't create group %s" to-newsgroup)))
12177           (error "No such group: %s" to-newsgroup))
12178       encoded)))
12179
12180 (defvar gnus-summary-save-parts-counter)
12181 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12182
12183 (defun gnus-summary-save-parts (type dir n &optional reverse)
12184   "Save parts matching TYPE to DIR.
12185 If REVERSE, save parts that do not match TYPE."
12186   (interactive
12187    (list (read-string "Save parts of type: "
12188                       (or (car gnus-summary-save-parts-type-history)
12189                           gnus-summary-save-parts-default-mime)
12190                       'gnus-summary-save-parts-type-history)
12191          (setq gnus-summary-save-parts-last-directory
12192                (read-directory-name "Save to directory: "
12193                                     gnus-summary-save-parts-last-directory
12194                                     nil t))
12195          current-prefix-arg))
12196   (gnus-summary-iterate n
12197     (let ((gnus-display-mime-function nil)
12198           gnus-article-prepare-hook
12199           gnus-article-decode-hook
12200           gnus-display-mime-function
12201           gnus-break-pages
12202           (gnus-inhibit-treatment t))
12203       (gnus-summary-select-article))
12204     (with-current-buffer gnus-article-buffer
12205       (let ((handles (or gnus-article-mime-handles
12206                          (mm-dissect-buffer nil gnus-article-loose-mime)
12207                          (and gnus-article-emulate-mime
12208                               (mm-uu-dissect))))
12209             (gnus-summary-save-parts-counter 1))
12210         (when handles
12211           (gnus-summary-save-parts-1 type dir handles reverse)
12212           (unless gnus-article-mime-handles ;; Don't destroy this case.
12213             (mm-destroy-parts handles)))))))
12214
12215 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12216   (if (stringp (car handle))
12217       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12218               (cdr handle))
12219     (when (if reverse
12220               (not (string-match type (mm-handle-media-type handle)))
12221             (string-match type (mm-handle-media-type handle)))
12222       (let ((file (expand-file-name
12223                    (gnus-map-function
12224                     mm-file-name-rewrite-functions
12225                     (file-name-nondirectory
12226                      (or
12227                       (mm-handle-filename handle)
12228                       (format "%s.%d.%d" gnus-newsgroup-name
12229                               (cdr gnus-article-current)
12230                               gnus-summary-save-parts-counter))))
12231                    dir)))
12232         (incf gnus-summary-save-parts-counter)
12233         (unless (file-exists-p file)
12234           (mm-save-part-to-file handle file))))))
12235
12236 ;; Summary extract commands
12237
12238 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12239   (let ((inhibit-read-only t)
12240         (article (gnus-summary-article-number))
12241         after-article b e)
12242     (unless (gnus-summary-goto-subject article)
12243       (error "No such article: %d" article))
12244     (gnus-summary-position-point)
12245     ;; If all commands are to be bunched up on one line, we collect
12246     ;; them here.
12247     (unless gnus-view-pseudos-separately
12248       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12249             files action)
12250         (while ps
12251           (setq action (cdr (assq 'action (car ps))))
12252           (setq files (list (cdr (assq 'name (car ps)))))
12253           (while (and ps (cdr ps)
12254                       (string= (or action "1")
12255                                (or (cdr (assq 'action (cadr ps))) "2")))
12256             (push (cdr (assq 'name (cadr ps))) files)
12257             (setcdr ps (cddr ps)))
12258           (when files
12259             (when (not (string-match "%s" action))
12260               (push " " files))
12261             (push " " files)
12262             (when (assq 'execute (car ps))
12263               (setcdr (assq 'execute (car ps))
12264                       (funcall (if (string-match "%s" action)
12265                                    'format 'concat)
12266                                action
12267                                (mapconcat
12268                                 (lambda (f)
12269                                   (if (equal f " ")
12270                                       f
12271                                     (shell-quote-argument f)))
12272                                 files " ")))))
12273           (setq ps (cdr ps)))))
12274     (if (and gnus-view-pseudos (not not-view))
12275         (while pslist
12276           (when (assq 'execute (car pslist))
12277             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12278                                   (eq gnus-view-pseudos 'not-confirm)))
12279           (setq pslist (cdr pslist)))
12280       (save-excursion
12281         (while pslist
12282           (setq after-article (or (cdr (assq 'article (car pslist)))
12283                                   (gnus-summary-article-number)))
12284           (gnus-summary-goto-subject after-article)
12285           (forward-line 1)
12286           (setq b (point))
12287           (insert "    " (file-name-nondirectory
12288                           (cdr (assq 'name (car pslist))))
12289                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12290           (setq e (point))
12291           (forward-line -1)             ; back to `b'
12292           (gnus-add-text-properties
12293            b (1- e) (list 'gnus-number gnus-reffed-article-number
12294                           gnus-mouse-face-prop gnus-mouse-face))
12295           (gnus-data-enter
12296            after-article gnus-reffed-article-number
12297            gnus-unread-mark b (car pslist) 0 (- e b))
12298           (setq gnus-newsgroup-unreads
12299                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12300                                          gnus-reffed-article-number))
12301           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12302           (setq pslist (cdr pslist)))))))
12303
12304 (defun gnus-pseudos< (p1 p2)
12305   (let ((c1 (cdr (assq 'action p1)))
12306         (c2 (cdr (assq 'action p2))))
12307     (and c1 c2 (string< c1 c2))))
12308
12309 (defun gnus-request-pseudo-article (props)
12310   (cond ((assq 'execute props)
12311          (gnus-execute-command (cdr (assq 'execute props)))))
12312   (let ((gnus-current-article (gnus-summary-article-number)))
12313     (gnus-run-hooks 'gnus-mark-article-hook)))
12314
12315 (defun gnus-execute-command (command &optional automatic)
12316   (save-excursion
12317     (gnus-article-setup-buffer)
12318     (set-buffer gnus-article-buffer)
12319     (setq buffer-read-only nil)
12320     (let ((command (if automatic command
12321                      (read-string "Command: " (cons command 0)))))
12322       (erase-buffer)
12323       (insert "$ " command "\n\n")
12324       (if gnus-view-pseudo-asynchronously
12325           (start-process "gnus-execute" (current-buffer) shell-file-name
12326                          shell-command-switch command)
12327         (call-process shell-file-name nil t nil
12328                       shell-command-switch command)))))
12329
12330 ;; Summary kill commands.
12331
12332 (defun gnus-summary-edit-global-kill (article)
12333   "Edit the \"global\" kill file."
12334   (interactive (list (gnus-summary-article-number)))
12335   (gnus-group-edit-global-kill article))
12336
12337 (defun gnus-summary-edit-local-kill ()
12338   "Edit a local kill file applied to the current newsgroup."
12339   (interactive)
12340   (setq gnus-current-headers (gnus-summary-article-header))
12341   (gnus-group-edit-local-kill
12342    (gnus-summary-article-number) gnus-newsgroup-name))
12343
12344 ;;; Header reading.
12345
12346 (defun gnus-read-header (id &optional header)
12347   "Read the headers of article ID and enter them into the Gnus system."
12348   (let ((group gnus-newsgroup-name)
12349         (gnus-override-method
12350          (or
12351           gnus-override-method
12352           (and (gnus-news-group-p gnus-newsgroup-name)
12353                (car (gnus-refer-article-methods)))))
12354         where)
12355     ;; First we check to see whether the header in question is already
12356     ;; fetched.
12357     (if (stringp id)
12358         ;; This is a Message-ID.
12359         (setq header (or header (gnus-id-to-header id)))
12360       ;; This is an article number.
12361       (setq header (or header (gnus-summary-article-header id))))
12362     (if (and header
12363              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12364         ;; We have found the header.
12365         header
12366       ;; We have to really fetch the header to this article.
12367       (with-current-buffer nntp-server-buffer
12368         (when (setq where (gnus-request-head id group))
12369           (nnheader-fold-continuation-lines)
12370           (goto-char (point-max))
12371           (insert ".\n")
12372           (goto-char (point-min))
12373           (insert "211 ")
12374           (princ (cond
12375                   ((numberp id) id)
12376                   ((cdr where) (cdr where))
12377                   (header (mail-header-number header))
12378                   (t gnus-reffed-article-number))
12379                  (current-buffer))
12380           (insert " Article retrieved.\n"))
12381         (if (or (not where)
12382                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12383             ()                          ; Malformed head.
12384           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12385             (when (and (stringp id)
12386                        (or
12387                         (not (string= (gnus-group-real-name group)
12388                                       (car where)))
12389                         (not (gnus-server-equal gnus-override-method
12390                                                 (gnus-group-method group)))))
12391               ;; If we fetched by Message-ID and the article came from
12392               ;; a different group (or server), we fudge some bogus
12393               ;; article numbers for this article.
12394               (mail-header-set-number header gnus-reffed-article-number))
12395             (with-current-buffer gnus-summary-buffer
12396               (decf gnus-reffed-article-number)
12397               (gnus-remove-header (mail-header-number header))
12398               (push header gnus-newsgroup-headers)
12399               (setq gnus-current-headers header)
12400               (push (mail-header-number header) gnus-newsgroup-limit)))
12401           header)))))
12402
12403 (defun gnus-remove-header (number)
12404   "Remove header NUMBER from `gnus-newsgroup-headers'."
12405   (if (and gnus-newsgroup-headers
12406            (= number (mail-header-number (car gnus-newsgroup-headers))))
12407       (pop gnus-newsgroup-headers)
12408     (let ((headers gnus-newsgroup-headers))
12409       (while (and (cdr headers)
12410                   (not (= number (mail-header-number (cadr headers)))))
12411         (pop headers))
12412       (when (cdr headers)
12413         (setcdr headers (cddr headers))))))
12414
12415 ;;;
12416 ;;; summary highlights
12417 ;;;
12418
12419 (defun gnus-highlight-selected-summary ()
12420   "Highlight selected article in summary buffer."
12421   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12422   (when gnus-summary-selected-face
12423     (save-excursion
12424       (let* ((beg (point-at-bol))
12425              (end (point-at-eol))
12426              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12427              (from (if (get-text-property beg gnus-mouse-face-prop)
12428                        beg
12429                      (or (next-single-property-change
12430                           beg gnus-mouse-face-prop nil end)
12431                          beg)))
12432              (to
12433               (if (= from end)
12434                   (- from 2)
12435                 (or (next-single-property-change
12436                      from gnus-mouse-face-prop nil end)
12437                     end))))
12438         ;; If no mouse-face prop on line we will have to = from = end,
12439         ;; so we highlight the entire line instead.
12440         (when (= (+ to 2) from)
12441           (setq from beg)
12442           (setq to end))
12443         (if gnus-newsgroup-selected-overlay
12444             ;; Move old overlay.
12445             (gnus-move-overlay
12446              gnus-newsgroup-selected-overlay from to (current-buffer))
12447           ;; Create new overlay.
12448           (gnus-overlay-put
12449            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12450            'face gnus-summary-selected-face))))))
12451
12452 (defvar gnus-summary-highlight-line-cached nil)
12453 (defvar gnus-summary-highlight-line-trigger nil)
12454
12455 (defun gnus-summary-highlight-line-0 ()
12456   (if (and (eq gnus-summary-highlight-line-trigger
12457                gnus-summary-highlight)
12458            gnus-summary-highlight-line-cached)
12459       gnus-summary-highlight-line-cached
12460     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12461           gnus-summary-highlight-line-cached
12462           (let* ((cond (list 'cond))
12463                  (c cond)
12464                  (list gnus-summary-highlight))
12465             (while list
12466               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12467                               nil))
12468               (setq c (cdr c)
12469                     list (cdr list)))
12470             (gnus-byte-compile (list 'lambda nil cond))))))
12471
12472 (defun gnus-summary-highlight-line ()
12473   "Highlight current line according to `gnus-summary-highlight'."
12474   (let* ((beg (point-at-bol))
12475          (article (or (gnus-summary-article-number) gnus-current-article))
12476          (score (or (cdr (assq article
12477                                gnus-newsgroup-scored))
12478                     gnus-summary-default-score 0))
12479          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12480          (inhibit-read-only t)
12481          (default gnus-summary-default-score)
12482          (default-high gnus-summary-default-high-score)
12483          (default-low gnus-summary-default-low-score)
12484          (uncached (and gnus-summary-use-undownloaded-faces
12485                         (memq article gnus-newsgroup-undownloaded)
12486                         (not (memq article gnus-newsgroup-cached)))))
12487     (let ((face (funcall (gnus-summary-highlight-line-0))))
12488       (unless (eq face (get-text-property beg 'face))
12489         (gnus-put-text-property-excluding-characters-with-faces
12490          beg (point-at-eol) 'face
12491          (setq face (if (boundp face) (symbol-value face) face)))
12492         (when gnus-summary-highlight-line-function
12493           (funcall gnus-summary-highlight-line-function article face))))))
12494
12495 (defun gnus-update-read-articles (group unread &optional compute)
12496   "Update the list of read articles in GROUP.
12497 UNREAD is a sorted list."
12498   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12499         (info (gnus-get-info group))
12500         (prev 1)
12501         read)
12502     (if (or (not info) (not active))
12503         ;; There is no info on this group if it was, in fact,
12504         ;; killed.  Gnus stores no information on killed groups, so
12505         ;; there's nothing to be done.
12506         ;; One could store the information somewhere temporarily,
12507         ;; perhaps...  Hmmm...
12508         ()
12509       ;; Remove any negative articles numbers.
12510       (while (and unread (< (car unread) 0))
12511         (setq unread (cdr unread)))
12512       ;; Remove any expired article numbers
12513       (while (and unread (< (car unread) (car active)))
12514         (setq unread (cdr unread)))
12515       ;; Compute the ranges of read articles by looking at the list of
12516       ;; unread articles.
12517       (while unread
12518         (when (/= (car unread) prev)
12519           (push (if (= prev (1- (car unread))) prev
12520                   (cons prev (1- (car unread))))
12521                 read))
12522         (setq prev (1+ (car unread)))
12523         (setq unread (cdr unread)))
12524       (when (<= prev (cdr active))
12525         (push (cons prev (cdr active)) read))
12526       (setq read (if (> (length read) 1) (nreverse read) read))
12527       (if compute
12528           read
12529         (save-excursion
12530           (let (setmarkundo)
12531             ;; Propagate the read marks to the backend.
12532             (when (and (or gnus-propagate-marks
12533                            (gnus-method-option-p
12534                             (gnus-find-method-for-group group)
12535                             'server-marks))
12536                        (gnus-check-backend-function 'request-set-mark group))
12537               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12538                     (add (gnus-remove-from-range read (gnus-info-read info))))
12539                 (when (or add del)
12540                   (unless (gnus-check-group group)
12541                     (error "Can't open server for %s" group))
12542                   (gnus-request-set-mark
12543                    group (delq nil (list (if add (list add 'add '(read)))
12544                                          (if del (list del 'del '(read))))))
12545                   (setq setmarkundo
12546                         `(gnus-request-set-mark
12547                           ,group
12548                           ',(delq nil (list
12549                                        (if del (list del 'add '(read)))
12550                                        (if add (list add 'del '(read))))))))))
12551             (set-buffer gnus-group-buffer)
12552             (gnus-undo-register
12553               `(progn
12554                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12555                  (gnus-info-set-read ',info ',(gnus-info-read info))
12556                  (gnus-get-unread-articles-in-group ',info
12557                                                     (gnus-active ,group))
12558                  (gnus-group-update-group ,group t)
12559                  ,setmarkundo))))
12560         ;; Enter this list into the group info.
12561         (gnus-info-set-read info read)
12562         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12563         (gnus-get-unread-articles-in-group info (gnus-active group))
12564         t))))
12565
12566 (defun gnus-offer-save-summaries ()
12567   "Offer to save all active summary buffers."
12568   (let (buffers)
12569     ;; Go through all buffers and find all summaries.
12570     (dolist (buffer (buffer-list))
12571       (when (and (setq buffer (buffer-name buffer))
12572                  (string-match "Summary" buffer)
12573                  (with-current-buffer buffer
12574                    ;; We check that this is, indeed, a summary buffer.
12575                    (and (eq major-mode 'gnus-summary-mode)
12576                         ;; Also make sure this isn't bogus.
12577                         gnus-newsgroup-prepared
12578                         ;; Also make sure that this isn't a
12579                         ;; dead summary buffer.
12580                         (not gnus-dead-summary-mode))))
12581         (push buffer buffers)))
12582     ;; Go through all these summary buffers and offer to save them.
12583     (when buffers
12584       (save-excursion
12585         (if (eq gnus-interactive-exit 'quiet)
12586             (dolist (buffer buffers)
12587               (switch-to-buffer buf)
12588               (gnus-summary-exit))
12589           (map-y-or-n-p
12590            "Update summary buffer %s? "
12591            (lambda (buf)
12592              (switch-to-buffer buf)
12593              (gnus-summary-exit))
12594            buffers))))))
12595
12596 (defun gnus-summary-setup-default-charset ()
12597   "Setup newsgroup default charset."
12598   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12599       (setq gnus-newsgroup-charset nil)
12600     (let* ((ignored-charsets
12601             (or gnus-newsgroup-ephemeral-ignored-charsets
12602                 (append
12603                  (and gnus-newsgroup-name
12604                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12605                  gnus-newsgroup-ignored-charsets))))
12606       (setq gnus-newsgroup-charset
12607             (or gnus-newsgroup-ephemeral-charset
12608                 (and gnus-newsgroup-name
12609                      (gnus-parameter-charset gnus-newsgroup-name))
12610                 gnus-default-charset))
12611       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12612            ignored-charsets))))
12613
12614 ;;;
12615 ;;; Mime Commands
12616 ;;;
12617
12618 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12619   "Display the current article buffer fully MIME-buttonized.
12620 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12621 treated as multipart/mixed."
12622   (interactive "P")
12623   (require 'gnus-art)
12624   (let ((gnus-unbuttonized-mime-types nil)
12625         (gnus-mime-display-multipart-as-mixed show-all-parts))
12626     (gnus-summary-show-article)))
12627
12628 (defun gnus-summary-repair-multipart (article)
12629   "Add a Content-Type header to a multipart article without one."
12630   (interactive (list (gnus-summary-article-number)))
12631   (gnus-with-article article
12632     (message-narrow-to-head)
12633     (message-remove-header "Mime-Version")
12634     (goto-char (point-max))
12635     (insert "Mime-Version: 1.0\n")
12636     (widen)
12637     (when (search-forward "\n--" nil t)
12638       (let ((separator (buffer-substring (point) (point-at-eol))))
12639         (message-narrow-to-head)
12640         (message-remove-header "Content-Type")
12641         (goto-char (point-max))
12642         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12643                         separator))
12644         (widen))))
12645   (let (gnus-mark-article-hook)
12646     (gnus-summary-select-article t t nil article)))
12647
12648 (defun gnus-summary-toggle-display-buttonized ()
12649   "Toggle the buttonizing of the article buffer."
12650   (interactive)
12651   (require 'gnus-art)
12652   (if (setq gnus-inhibit-mime-unbuttonizing
12653             (not gnus-inhibit-mime-unbuttonizing))
12654       (let ((gnus-unbuttonized-mime-types nil))
12655         (gnus-summary-show-article))
12656     (gnus-summary-show-article)))
12657
12658 ;;;
12659 ;;; Generic summary marking commands
12660 ;;;
12661
12662 (defvar gnus-summary-marking-alist
12663   '((read gnus-del-mark "d")
12664     (unread gnus-unread-mark "u")
12665     (ticked gnus-ticked-mark "!")
12666     (dormant gnus-dormant-mark "?")
12667     (expirable gnus-expirable-mark "e"))
12668   "An alist of names/marks/keystrokes.")
12669
12670 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12671 (defvar gnus-summary-mark-map)
12672
12673 (defun gnus-summary-make-all-marking-commands ()
12674   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12675   (dolist (elem gnus-summary-marking-alist)
12676     (apply 'gnus-summary-make-marking-command elem)))
12677
12678 (defun gnus-summary-make-marking-command (name mark keystroke)
12679   (let ((map (make-sparse-keymap)))
12680     (define-key gnus-summary-generic-mark-map keystroke map)
12681     (dolist (lway `((next "next" next nil "n")
12682                     (next-unread "next unread" next t "N")
12683                     (prev "previous" prev nil "p")
12684                     (prev-unread "previous unread" prev t "P")
12685                     (nomove "" nil nil ,keystroke)))
12686       (let ((func (gnus-summary-make-marking-command-1
12687                    mark (car lway) lway name)))
12688         (setq func (eval func))
12689         (define-key map (nth 4 lway) func)))))
12690
12691 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12692   `(defun ,(intern
12693             (format "gnus-summary-put-mark-as-%s%s"
12694                     name (if (eq way 'nomove)
12695                              ""
12696                            (concat "-" (symbol-name way)))))
12697      (n)
12698      ,(format
12699        "Mark the current article as %s%s.
12700 If N, the prefix, then repeat N times.
12701 If N is negative, move in reverse order.
12702 The difference between N and the actual number of articles marked is
12703 returned."
12704        name (cadr lway))
12705      (interactive "p")
12706      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12707
12708 (defun gnus-summary-generic-mark (n mark move unread)
12709   "Mark N articles with MARK."
12710   (unless (eq major-mode 'gnus-summary-mode)
12711     (error "This command can only be used in the summary buffer"))
12712   (gnus-summary-show-thread)
12713   (let ((nummove
12714          (cond
12715           ((eq move 'next) 1)
12716           ((eq move 'prev) -1)
12717           (t 0))))
12718     (if (zerop nummove)
12719         (setq n 1)
12720       (when (< n 0)
12721         (setq n (abs n)
12722               nummove (* -1 nummove))))
12723     (while (and (> n 0)
12724                 (gnus-summary-mark-article nil mark)
12725                 (zerop (gnus-summary-next-subject nummove unread t)))
12726       (setq n (1- n)))
12727     (when (/= 0 n)
12728       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12729     (gnus-summary-recenter)
12730     (gnus-summary-position-point)
12731     (gnus-set-mode-line 'summary)
12732     n))
12733
12734 (defun gnus-summary-insert-articles (articles)
12735   (when (setq articles
12736               (gnus-sorted-difference articles
12737                                       (mapcar (lambda (h)
12738                                                 (mail-header-number h))
12739                                               gnus-newsgroup-headers)))
12740     (setq gnus-newsgroup-headers
12741           (gnus-merge 'list
12742                       gnus-newsgroup-headers
12743                       (gnus-fetch-headers articles)
12744                       'gnus-article-sort-by-number))
12745     (setq gnus-newsgroup-articles
12746           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12747     ;; Suppress duplicates?
12748     (when gnus-suppress-duplicates
12749       (gnus-dup-suppress-articles))
12750
12751     (if (and gnus-fetch-old-headers
12752              (eq gnus-headers-retrieved-by 'nov))
12753         ;; We might want to build some more threads first.
12754         (if (eq gnus-fetch-old-headers 'invisible)
12755             (gnus-build-all-threads)
12756           (gnus-build-old-threads))
12757       ;; Mark the inserted articles that are unread as unread.
12758       (setq gnus-newsgroup-unreads
12759             (gnus-sorted-nunion
12760              gnus-newsgroup-unreads
12761              (gnus-sorted-nintersection
12762               (gnus-list-of-unread-articles gnus-newsgroup-name)
12763               articles)))
12764       ;; Mark the inserted articles as selected so that the information
12765       ;; of the marks having been changed by a user may be updated when
12766       ;; exiting this group.  See `gnus-summary-update-info'.
12767       (dolist (art articles)
12768         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12769     ;; Let the Gnus agent mark articles as read.
12770     (when gnus-agent
12771       (gnus-agent-get-undownloaded-list))
12772     ;; Remove list identifiers from subject
12773     (gnus-summary-remove-list-identifiers)
12774     ;; First and last article in this newsgroup.
12775     (when gnus-newsgroup-headers
12776       (setq gnus-newsgroup-begin
12777             (mail-header-number (car gnus-newsgroup-headers))
12778             gnus-newsgroup-end
12779             (mail-header-number
12780              (gnus-last-element gnus-newsgroup-headers))))
12781     (when gnus-use-scoring
12782       (gnus-possibly-score-headers))))
12783
12784 (defun gnus-summary-insert-old-articles (&optional all)
12785   "Insert all old articles in this group.
12786 If ALL is non-nil, already read articles become readable.
12787 If ALL is a number, fetch this number of articles."
12788   (interactive "P")
12789   (prog1
12790       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12791             older len)
12792         (setq older
12793               ;; Some nntp servers lie about their active range.  When
12794               ;; this happens, the active range can be in the millions.
12795               ;; Use a compressed range to avoid creating a huge list.
12796               (gnus-range-difference (list gnus-newsgroup-active) old))
12797         (setq len (gnus-range-length older))
12798         (cond
12799          ((null older) nil)
12800          ((numberp all)
12801           (if (< all len)
12802               (let ((older-range (nreverse older)))
12803                 (setq older nil)
12804
12805                 (while (> all 0)
12806                   (let* ((r (pop older-range))
12807                          (min (if (numberp r) r (car r)))
12808                          (max (if (numberp r) r (cdr r))))
12809                     (while (and (<= min max)
12810                                 (> all 0))
12811                       (push max older)
12812                       (setq all (1- all)
12813                             max (1- max))))))
12814             (setq older (gnus-uncompress-range older))))
12815          (all
12816           (setq older (gnus-uncompress-range older)))
12817          (t
12818           (when (and (numberp gnus-large-newsgroup)
12819                    (> len gnus-large-newsgroup))
12820               (let* ((cursor-in-echo-area nil)
12821                      (initial (gnus-parameter-large-newsgroup-initial
12822                                gnus-newsgroup-name))
12823                      (input
12824                       (read-string
12825                        (format
12826                         "How many articles from %s (%s %d): "
12827                         (gnus-group-decoded-name gnus-newsgroup-name)
12828                         (if initial "max" "default")
12829                         len)
12830                        (if initial
12831                            (cons (number-to-string initial)
12832                                  0)))))
12833                 (unless (string-match "^[ \t]*$" input)
12834                   (setq all (string-to-number input))
12835                   (if (< all len)
12836                       (let ((older-range (nreverse older)))
12837                         (setq older nil)
12838
12839                         (while (> all 0)
12840                           (let* ((r (pop older-range))
12841                                  (min (if (numberp r) r (car r)))
12842                                  (max (if (numberp r) r (cdr r))))
12843                             (while (and (<= min max)
12844                                         (> all 0))
12845                               (push max older)
12846                               (setq all (1- all)
12847                                     max (1- max))))))))))
12848           (setq older (gnus-uncompress-range older))))
12849         (if (not older)
12850             (message "No old news.")
12851           (gnus-summary-insert-articles older)
12852           (gnus-summary-limit (gnus-sorted-nunion old older))))
12853     (gnus-summary-position-point)))
12854
12855 (defun gnus-summary-insert-new-articles ()
12856   "Insert all new articles in this group."
12857   (interactive)
12858   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12859         (old-high gnus-newsgroup-highest)
12860         (nnmail-fetched-sources (list t))
12861         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12862         i new)
12863     (unless new-active
12864       (error "Couldn't fetch new data"))
12865     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12866     (setq i (cdr gnus-newsgroup-active)
12867           gnus-newsgroup-highest i)
12868     (while (> i old-high)
12869       (push i new)
12870       (decf i))
12871     (if (not new)
12872         (message "No gnus is bad news")
12873       (gnus-summary-insert-articles new)
12874       (setq gnus-newsgroup-unreads
12875             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12876       (gnus-summary-limit (gnus-sorted-nunion old new))))
12877   (gnus-summary-position-point))
12878
12879 ;;; Bookmark support for Gnus.
12880 (declare-function bookmark-make-record-default
12881                   "bookmark" (&optional no-file no-context posn))
12882 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12883 (declare-function bookmark-default-handler "bookmark" (bmk))
12884 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12885 (defvar bookmark-yank-point)
12886 (defvar bookmark-current-buffer)
12887
12888 (defun gnus-summary-bookmark-make-record ()
12889   "Make a bookmark entry for a Gnus summary buffer."
12890   (let (pos buf)
12891     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12892       (save-restriction              ; FIXME is it necessary to widen?
12893         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12894       (setq buf "art") ; We are recording bookmark from article buffer.
12895       (setq bookmark-yank-point (point))
12896       (setq bookmark-current-buffer (current-buffer))
12897       (gnus-article-show-summary))      ; Go back in summary buffer.
12898     ;; We are now recording bookmark from summary buffer.
12899     (unless buf (setq buf "sum"))
12900     (let* ((subject (elt (gnus-summary-article-header) 1))
12901            (grp     (car gnus-article-current))
12902            (art     (cdr gnus-article-current))
12903            (head    (gnus-summary-article-header art))
12904            (id      (mail-header-id head)))
12905       `(,subject
12906         ,@(condition-case nil
12907               (bookmark-make-record-default 'no-file 'no-context pos)
12908             (wrong-number-of-arguments
12909              (bookmark-make-record-default 'point-only)))
12910         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12911         (group . ,grp) (article . ,art)
12912         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12913
12914 ;;;###autoload
12915 (defun gnus-summary-bookmark-jump (bookmark)
12916   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12917 BOOKMARK is a bookmark name or a bookmark record."
12918   (let ((group    (bookmark-prop-get bookmark 'group))
12919         (article  (bookmark-prop-get bookmark 'article))
12920         (id       (bookmark-prop-get bookmark 'message-id))
12921         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12922     (gnus-fetch-group group (list article))
12923     (gnus-summary-insert-cached-articles)
12924     (gnus-summary-goto-article id nil 'force)
12925     ;; FIXME we have to wait article buffer is ready (only large buffer)
12926     ;; Is there a better solution to know that?
12927     ;; If we don't wait `bookmark-default-handler' will have no chance
12928     ;; to set position. However there is no error, just wrong pos.
12929     (sit-for 1)
12930     (when (string= buf "Gnus-art")
12931       (other-window 1))
12932     (bookmark-default-handler
12933      `(""
12934        (buffer . ,(current-buffer))
12935        . ,(bookmark-get-bookmark-record bookmark)))))
12936
12937 (gnus-summary-make-all-marking-commands)
12938
12939 (gnus-ems-redefine)
12940
12941 (provide 'gnus-sum)
12942
12943 (run-hooks 'gnus-sum-load-hook)
12944
12945 ;; Local Variables:
12946 ;; coding: iso-8859-1
12947 ;; End:
12948
12949 ;;; gnus-sum.el ends here