Cleanups related to "unexist" tracking
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 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 highest-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 (nnir-article-rsv (mail-header-number gnus-tmp-header))
1375             0) ?d)
1376     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1377             "") ?s)
1378     (?g (or (gnus-group-short-name
1379              (nnir-article-group (mail-header-number gnus-tmp-header)))
1380             "") ?s)
1381     (?O gnus-tmp-downloaded ?c)
1382     (?I gnus-tmp-indentation ?s)
1383     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1384     (?R gnus-tmp-replied ?c)
1385     (?\[ gnus-tmp-opening-bracket ?c)
1386     (?\] gnus-tmp-closing-bracket ?c)
1387     (?\> (make-string gnus-tmp-level ? ) ?s)
1388     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1389     (?i gnus-tmp-score ?d)
1390     (?z gnus-tmp-score-char ?c)
1391     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1392     (?U gnus-tmp-unread ?c)
1393     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1394         ?s)
1395     (?t (gnus-summary-number-of-articles-in-thread
1396          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1397         ?d)
1398     (?e (gnus-summary-number-of-articles-in-thread
1399          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1400         ?c)
1401     (?u gnus-tmp-user-defined ?s)
1402     (?P (gnus-pick-line-number) ?d)
1403     (?B gnus-tmp-thread-tree-header-string ?s)
1404     (user-date (gnus-user-date
1405                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1406   "An alist of format specifications that can appear in summary lines.
1407 These are paired with what variables they correspond with, along with
1408 the type of the variable (string, integer, character, etc).")
1409
1410 (defvar gnus-summary-dummy-line-format-alist
1411   `((?S gnus-tmp-subject ?s)
1412     (?N gnus-tmp-number ?d)
1413     (?u gnus-tmp-user-defined ?s)))
1414
1415 (defvar gnus-summary-mode-line-format-alist
1416   `((?G gnus-tmp-group-name ?s)
1417     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1418     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1419     (?A gnus-tmp-article-number ?d)
1420     (?Z gnus-tmp-unread-and-unselected ?s)
1421     (?V gnus-version ?s)
1422     (?U gnus-tmp-unread-and-unticked ?d)
1423     (?S gnus-tmp-subject ?s)
1424     (?e gnus-tmp-unselected ?d)
1425     (?u gnus-tmp-user-defined ?s)
1426     (?d (length gnus-newsgroup-dormant) ?d)
1427     (?t (length gnus-newsgroup-marked) ?d)
1428     (?h (length gnus-newsgroup-spam-marked) ?d)
1429     (?r (length gnus-newsgroup-reads) ?d)
1430     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1431     (?E gnus-newsgroup-expunged-tally ?d)
1432     (?s (gnus-current-score-file-nondirectory) ?s)))
1433
1434 ;; This is here rather than in gnus-art for compilation reasons.
1435 (defvar gnus-article-mode-line-format-alist
1436   (nconc '((?w (gnus-article-wash-status) ?s)
1437            (?m (gnus-article-mime-part-status) ?s))
1438          gnus-summary-mode-line-format-alist))
1439
1440 (defvar gnus-last-search-regexp nil
1441   "Default regexp for article search command.")
1442
1443 (defvar gnus-last-shell-command nil
1444   "Default shell command on article.")
1445
1446 (defvar gnus-newsgroup-agentized nil
1447   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1448 (defvar gnus-newsgroup-begin nil)
1449 (defvar gnus-newsgroup-end nil)
1450 (defvar gnus-newsgroup-last-rmail nil)
1451 (defvar gnus-newsgroup-last-mail nil)
1452 (defvar gnus-newsgroup-last-folder nil)
1453 (defvar gnus-newsgroup-last-file nil)
1454 (defvar gnus-newsgroup-last-directory nil)
1455 (defvar gnus-newsgroup-auto-expire nil)
1456 (defvar gnus-newsgroup-active nil)
1457 (defvar gnus-newsgroup-highest nil)
1458
1459 (defvar gnus-newsgroup-data nil)
1460 (defvar gnus-newsgroup-data-reverse nil)
1461 (defvar gnus-newsgroup-limit nil)
1462 (defvar gnus-newsgroup-limits nil)
1463 (defvar gnus-summary-use-undownloaded-faces nil)
1464
1465 (defvar gnus-newsgroup-unreads nil
1466   "Sorted list of unread articles in the current newsgroup.")
1467
1468 (defvar gnus-newsgroup-unselected nil
1469   "Sorted list of unselected unread articles in the current newsgroup.")
1470
1471 (defvar gnus-newsgroup-reads nil
1472   "Alist of read articles and article marks in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-expunged-tally nil)
1475
1476 (defvar gnus-newsgroup-marked nil
1477   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1478
1479 (defvar gnus-newsgroup-spam-marked nil
1480   "List of ranges of articles that have been marked as spam.")
1481
1482 (defvar gnus-newsgroup-killed nil
1483   "List of ranges of articles that have been through the scoring process.")
1484
1485 (defvar gnus-newsgroup-cached nil
1486   "Sorted list of articles that come from the article cache.")
1487
1488 (defvar gnus-newsgroup-saved nil
1489   "List of articles that have been saved.")
1490
1491 (defvar gnus-newsgroup-kill-headers nil)
1492
1493 (defvar gnus-newsgroup-replied nil
1494   "List of articles that have been replied to in the current newsgroup.")
1495
1496 (defvar gnus-newsgroup-forwarded nil
1497   "List of articles that have been forwarded in the current newsgroup.")
1498
1499 (defvar gnus-newsgroup-expirable nil
1500   "Sorted list of articles in the current newsgroup that can be expired.")
1501
1502 (defvar gnus-newsgroup-processable nil
1503   "List of articles in the current newsgroup that can be processed.")
1504
1505 (defvar gnus-newsgroup-downloadable nil
1506   "Sorted list of articles in the current newsgroup that can be processed.")
1507
1508 (defvar gnus-newsgroup-unfetched nil
1509   "Sorted list of articles in the current newsgroup whose headers have
1510 not been fetched into the agent.
1511
1512 This list will always be a subset of gnus-newsgroup-undownloaded.")
1513
1514 (defvar gnus-newsgroup-undownloaded nil
1515   "List of articles in the current newsgroup that haven't been downloaded.")
1516
1517 (defvar gnus-newsgroup-unsendable nil
1518   "List of articles in the current newsgroup that won't be sent.")
1519
1520 (defvar gnus-newsgroup-bookmarks nil
1521   "List of articles in the current newsgroup that have bookmarks.")
1522
1523 (defvar gnus-newsgroup-dormant nil
1524   "Sorted list of dormant articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-unseen nil
1527   "List of unseen articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-seen nil
1530   "Range of seen articles in the current newsgroup.")
1531
1532 (defvar gnus-newsgroup-unexist nil
1533   "Range of unexistent 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 applied only to the 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   [backtab] gnus-summary-widget-backward
1924   "t" gnus-summary-toggle-header
1925   "g" gnus-summary-show-article
1926   "l" gnus-summary-goto-last-article
1927   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1928   "\C-d" gnus-summary-enter-digest-group
1929   "\M-\C-d" gnus-summary-read-document
1930   "\M-\C-e" gnus-summary-edit-parameters
1931   "\M-\C-a" gnus-summary-customize-parameters
1932   "\C-c\C-b" gnus-bug
1933   "*" gnus-cache-enter-article
1934   "\M-*" gnus-cache-remove-article
1935   "\M-&" gnus-summary-universal-argument
1936   "\C-l" gnus-recenter
1937   "I" gnus-summary-increase-score
1938   "L" gnus-summary-lower-score
1939   "\M-i" gnus-symbolic-argument
1940   "h" gnus-summary-select-article-buffer
1941
1942   "b" gnus-article-view-part
1943   "\M-t" gnus-summary-toggle-display-buttonized
1944
1945   "V" gnus-summary-score-map
1946   "X" gnus-uu-extract-map
1947   "S" gnus-summary-send-map)
1948
1949 ;; Sort of orthogonal keymap
1950 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1951   "t" gnus-summary-tick-article-forward
1952   "!" gnus-summary-tick-article-forward
1953   "d" gnus-summary-mark-as-read-forward
1954   "r" gnus-summary-mark-as-read-forward
1955   "c" gnus-summary-clear-mark-forward
1956   " " gnus-summary-clear-mark-forward
1957   "e" gnus-summary-mark-as-expirable
1958   "x" gnus-summary-mark-as-expirable
1959   "?" gnus-summary-mark-as-dormant
1960   "b" gnus-summary-set-bookmark
1961   "B" gnus-summary-remove-bookmark
1962   "#" gnus-summary-mark-as-processable
1963   "\M-#" gnus-summary-unmark-as-processable
1964   "S" gnus-summary-limit-include-expunged
1965   "C" gnus-summary-catchup
1966   "H" gnus-summary-catchup-to-here
1967   "h" gnus-summary-catchup-from-here
1968   "\C-c" gnus-summary-catchup-all
1969   "k" gnus-summary-kill-same-subject-and-select
1970   "K" gnus-summary-kill-same-subject
1971   "P" gnus-uu-mark-map)
1972
1973 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1974   "c" gnus-summary-clear-above
1975   "u" gnus-summary-tick-above
1976   "m" gnus-summary-mark-above
1977   "k" gnus-summary-kill-below)
1978
1979 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1980   "/" gnus-summary-limit-to-subject
1981   "n" gnus-summary-limit-to-articles
1982   "b" gnus-summary-limit-to-bodies
1983   "h" gnus-summary-limit-to-headers
1984   "w" gnus-summary-pop-limit
1985   "s" gnus-summary-limit-to-subject
1986   "a" gnus-summary-limit-to-author
1987   "u" gnus-summary-limit-to-unread
1988   "m" gnus-summary-limit-to-marks
1989   "M" gnus-summary-limit-exclude-marks
1990   "v" gnus-summary-limit-to-score
1991   "*" gnus-summary-limit-include-cached
1992   "D" gnus-summary-limit-include-dormant
1993   "T" gnus-summary-limit-include-thread
1994   "d" gnus-summary-limit-exclude-dormant
1995   "t" gnus-summary-limit-to-age
1996   "." gnus-summary-limit-to-unseen
1997   "x" gnus-summary-limit-to-extra
1998   "p" gnus-summary-limit-to-display-predicate
1999   "E" gnus-summary-limit-include-expunged
2000   "c" gnus-summary-limit-exclude-childless-dormant
2001   "C" gnus-summary-limit-mark-excluded-as-read
2002   "o" gnus-summary-insert-old-articles
2003   "N" gnus-summary-insert-new-articles
2004   "S" gnus-summary-limit-to-singletons
2005   "r" gnus-summary-limit-to-replied
2006   "R" gnus-summary-limit-to-recipient
2007   "A" gnus-summary-limit-to-address)
2008
2009 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2010   "n" gnus-summary-next-unread-article
2011   "p" gnus-summary-prev-unread-article
2012   "N" gnus-summary-next-article
2013   "P" gnus-summary-prev-article
2014   "\C-n" gnus-summary-next-same-subject
2015   "\C-p" gnus-summary-prev-same-subject
2016   "\M-n" gnus-summary-next-unread-subject
2017   "\M-p" gnus-summary-prev-unread-subject
2018   "f" gnus-summary-first-unread-article
2019   "b" gnus-summary-best-unread-article
2020   "j" gnus-summary-goto-article
2021   "g" gnus-summary-goto-subject
2022   "l" gnus-summary-goto-last-article
2023   "o" gnus-summary-pop-article)
2024
2025 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2026   "k" gnus-summary-kill-thread
2027   "E" gnus-summary-expire-thread
2028   "l" gnus-summary-lower-thread
2029   "i" gnus-summary-raise-thread
2030   "T" gnus-summary-toggle-threads
2031   "t" gnus-summary-rethread-current
2032   "^" gnus-summary-reparent-thread
2033   "\M-^" gnus-summary-reparent-children
2034   "s" gnus-summary-show-thread
2035   "S" gnus-summary-show-all-threads
2036   "h" gnus-summary-hide-thread
2037   "H" gnus-summary-hide-all-threads
2038   "n" gnus-summary-next-thread
2039   "p" gnus-summary-prev-thread
2040   "u" gnus-summary-up-thread
2041   "o" gnus-summary-top-thread
2042   "d" gnus-summary-down-thread
2043   "#" gnus-uu-mark-thread
2044   "\M-#" gnus-uu-unmark-thread)
2045
2046 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2047   "g" gnus-summary-prepare
2048   "c" gnus-summary-insert-cached-articles
2049   "d" gnus-summary-insert-dormant-articles
2050   "t" gnus-summary-insert-ticked-articles)
2051
2052 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2053   "c" gnus-summary-catchup-and-exit
2054   "C" gnus-summary-catchup-all-and-exit
2055   "E" gnus-summary-exit-no-update
2056   "Q" gnus-summary-exit
2057   "Z" gnus-summary-exit
2058   "n" gnus-summary-catchup-and-goto-next-group
2059   "p" gnus-summary-catchup-and-goto-prev-group
2060   "R" gnus-summary-reselect-current-group
2061   "G" gnus-summary-rescan-group
2062   "N" gnus-summary-next-group
2063   "s" gnus-summary-save-newsrc
2064   "P" gnus-summary-prev-group)
2065
2066 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2067   " " gnus-summary-next-page
2068   "n" gnus-summary-next-page
2069   "\177" gnus-summary-prev-page
2070   [delete] gnus-summary-prev-page
2071   "p" gnus-summary-prev-page
2072   "\r" gnus-summary-scroll-up
2073   "\M-\r" gnus-summary-scroll-down
2074   "<" gnus-summary-beginning-of-article
2075   ">" gnus-summary-end-of-article
2076   "b" gnus-summary-beginning-of-article
2077   "e" gnus-summary-end-of-article
2078   "^" gnus-summary-refer-parent-article
2079   "r" gnus-summary-refer-parent-article
2080   "C" gnus-summary-show-complete-article
2081   "D" gnus-summary-enter-digest-group
2082   "R" gnus-summary-refer-references
2083   "T" gnus-summary-refer-thread
2084   "W" gnus-warp-to-article
2085   "g" gnus-summary-show-article
2086   "s" gnus-summary-isearch-article
2087   [tab] gnus-summary-widget-forward
2088   [backtab] gnus-summary-widget-backward
2089   "P" gnus-summary-print-article
2090   "S" gnus-sticky-article
2091   "M" gnus-mailing-list-insinuate
2092   "t" gnus-article-babel)
2093
2094 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2095   "b" gnus-article-add-buttons
2096   "B" gnus-article-add-buttons-to-head
2097   "o" gnus-article-treat-overstrike
2098   "e" gnus-article-emphasize
2099   "w" gnus-article-fill-cited-article
2100   "Q" gnus-article-fill-long-lines
2101   "L" gnus-article-toggle-truncate-lines
2102   "C" gnus-article-capitalize-sentences
2103   "c" gnus-article-remove-cr
2104   "q" gnus-article-de-quoted-unreadable
2105   "6" gnus-article-de-base64-unreadable
2106   "Z" gnus-article-decode-HZ
2107   "A" gnus-article-treat-ansi-sequences
2108   "h" gnus-article-wash-html
2109   "u" gnus-article-unsplit-urls
2110   "s" gnus-summary-force-verify-and-decrypt
2111   "f" gnus-article-display-x-face
2112   "l" gnus-summary-stop-page-breaking
2113   "r" gnus-summary-caesar-message
2114   "m" gnus-summary-morse-message
2115   "t" gnus-summary-toggle-header
2116   "g" gnus-treat-smiley
2117   "v" gnus-summary-verbose-headers
2118   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2119   "p" gnus-article-verify-x-pgp-sig
2120   "d" gnus-article-treat-dumbquotes
2121   "U" gnus-article-treat-non-ascii
2122   "i" gnus-summary-idna-message)
2123
2124 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2125   ;; mnemonic: deuglif*Y*
2126   "u" gnus-article-outlook-unwrap-lines
2127   "a" gnus-article-outlook-repair-attribution
2128   "c" gnus-article-outlook-rearrange-citation
2129   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2130
2131 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2132   "a" gnus-article-hide
2133   "h" gnus-article-hide-headers
2134   "b" gnus-article-hide-boring-headers
2135   "s" gnus-article-hide-signature
2136   "c" gnus-article-hide-citation
2137   "C" gnus-article-hide-citation-in-followups
2138   "l" gnus-article-hide-list-identifiers
2139   "B" gnus-article-strip-banner
2140   "P" gnus-article-hide-pem
2141   "\C-c" gnus-article-hide-citation-maybe)
2142
2143 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2144   "a" gnus-article-highlight
2145   "h" gnus-article-highlight-headers
2146   "c" gnus-article-highlight-citation
2147   "s" gnus-article-highlight-signature)
2148
2149 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2150   "f" gnus-article-treat-fold-headers
2151   "u" gnus-article-treat-unfold-headers
2152   "n" gnus-article-treat-fold-newsgroups)
2153
2154 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2155   "x" gnus-article-display-x-face
2156   "d" gnus-article-display-face
2157   "s" gnus-treat-smiley
2158   "D" gnus-article-remove-images
2159   "W" gnus-article-show-images
2160   "f" gnus-treat-from-picon
2161   "m" gnus-treat-mail-picon
2162   "n" gnus-treat-newsgroups-picon
2163   "g" gnus-treat-from-gravatar
2164   "h" gnus-treat-mail-gravatar)
2165
2166 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2167   "w" gnus-article-decode-mime-words
2168   "c" gnus-article-decode-charset
2169   "v" gnus-mime-view-all-parts
2170   "b" gnus-article-view-part)
2171
2172 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2173   "z" gnus-article-date-ut
2174   "u" gnus-article-date-ut
2175   "l" gnus-article-date-local
2176   "p" gnus-article-date-english
2177   "e" gnus-article-date-lapsed
2178   "o" gnus-article-date-original
2179   "i" gnus-article-date-iso8601
2180   "s" gnus-article-date-user)
2181
2182 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2183   "t" gnus-article-remove-trailing-blank-lines
2184   "l" gnus-article-strip-leading-blank-lines
2185   "m" gnus-article-strip-multiple-blank-lines
2186   "a" gnus-article-strip-blank-lines
2187   "A" gnus-article-strip-all-blank-lines
2188   "s" gnus-article-strip-leading-space
2189   "e" gnus-article-strip-trailing-space
2190   "w" gnus-article-remove-leading-whitespace)
2191
2192 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2193   "v" gnus-version
2194   "d" gnus-summary-describe-group
2195   "h" gnus-summary-describe-briefly
2196   "i" gnus-info-find-node)
2197
2198 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2199   "e" gnus-summary-expire-articles
2200   "\M-\C-e" gnus-summary-expire-articles-now
2201   "\177" gnus-summary-delete-article
2202   [delete] gnus-summary-delete-article
2203   [backspace] gnus-summary-delete-article
2204   "m" gnus-summary-move-article
2205   "r" gnus-summary-respool-article
2206   "w" gnus-summary-edit-article
2207   "c" gnus-summary-copy-article
2208   "B" gnus-summary-crosspost-article
2209   "q" gnus-summary-respool-query
2210   "t" gnus-summary-respool-trace
2211   "i" gnus-summary-import-article
2212   "I" gnus-summary-create-article
2213   "p" gnus-summary-article-posted-p)
2214
2215 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2216   "o" gnus-summary-save-article
2217   "m" gnus-summary-save-article-mail
2218   "F" gnus-summary-write-article-file
2219   "r" gnus-summary-save-article-rmail
2220   "f" gnus-summary-save-article-file
2221   "b" gnus-summary-save-article-body-file
2222   "B" gnus-summary-write-article-body-file
2223   "h" gnus-summary-save-article-folder
2224   "v" gnus-summary-save-article-vm
2225   "p" gnus-summary-pipe-output
2226   "P" gnus-summary-muttprint)
2227
2228 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2229   "b" gnus-summary-display-buttonized
2230   "m" gnus-summary-repair-multipart
2231   "v" gnus-article-view-part
2232   "o" gnus-article-save-part
2233   "O" gnus-article-save-part-and-strip
2234   "r" gnus-article-replace-part
2235   "d" gnus-article-delete-part
2236   "t" gnus-article-view-part-as-type
2237   "j" gnus-article-jump-to-part
2238   "c" gnus-article-copy-part
2239   "C" gnus-article-view-part-as-charset
2240   "e" gnus-article-view-part-externally
2241   "H" gnus-article-browse-html-article
2242   "E" gnus-article-encrypt-body
2243   "i" gnus-article-inline-part
2244   "|" gnus-article-pipe-part)
2245
2246 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2247   "p" gnus-summary-mark-as-processable
2248   "u" gnus-summary-unmark-as-processable
2249   "U" gnus-summary-unmark-all-processable
2250   "v" gnus-uu-mark-over
2251   "s" gnus-uu-mark-series
2252   "r" gnus-uu-mark-region
2253   "g" gnus-uu-unmark-region
2254   "R" gnus-uu-mark-by-regexp
2255   "G" gnus-uu-unmark-by-regexp
2256   "t" gnus-uu-mark-thread
2257   "T" gnus-uu-unmark-thread
2258   "a" gnus-uu-mark-all
2259   "b" gnus-uu-mark-buffer
2260   "S" gnus-uu-mark-sparse
2261   "k" gnus-summary-kill-process-mark
2262   "y" gnus-summary-yank-process-mark
2263   "w" gnus-summary-save-process-mark
2264   "i" gnus-uu-invert-processable)
2265
2266 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2267   ;;"x" gnus-uu-extract-any
2268   "m" gnus-summary-save-parts
2269   "u" gnus-uu-decode-uu
2270   "U" gnus-uu-decode-uu-and-save
2271   "s" gnus-uu-decode-unshar
2272   "S" gnus-uu-decode-unshar-and-save
2273   "o" gnus-uu-decode-save
2274   "O" gnus-uu-decode-save
2275   "b" gnus-uu-decode-binhex
2276   "B" gnus-uu-decode-binhex
2277   "Y" gnus-uu-decode-yenc
2278   "p" gnus-uu-decode-postscript
2279   "P" gnus-uu-decode-postscript-and-save)
2280
2281 (gnus-define-keys
2282     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2283   "u" gnus-uu-decode-uu-view
2284   "U" gnus-uu-decode-uu-and-save-view
2285   "s" gnus-uu-decode-unshar-view
2286   "S" gnus-uu-decode-unshar-and-save-view
2287   "o" gnus-uu-decode-save-view
2288   "O" gnus-uu-decode-save-view
2289   "b" gnus-uu-decode-binhex-view
2290   "B" gnus-uu-decode-binhex-view
2291   "p" gnus-uu-decode-postscript-view
2292   "P" gnus-uu-decode-postscript-and-save-view)
2293
2294 (defvar gnus-article-post-menu nil)
2295
2296 (defconst gnus-summary-menu-maxlen 20)
2297
2298 (defun gnus-summary-menu-split (menu)
2299   ;; If we have lots of elements, divide them into groups of 20
2300   ;; and make a pane (or submenu) for each one.
2301   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2302       (let ((menu menu) sublists next
2303             (i 1))
2304         (while menu
2305           ;; Pull off the next gnus-summary-menu-maxlen elements
2306           ;; and make them the next element of sublist.
2307           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2308           (if next
2309               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2310                       nil))
2311           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2312                                              (aref (car (last menu)) 0)) menu)
2313                                sublists))
2314           (setq i (1+ i))
2315           (setq menu next))
2316         (nreverse sublists))
2317     ;; Few elements--put them all in one pane.
2318     menu))
2319
2320 (defun gnus-summary-make-menu-bar ()
2321   (gnus-turn-off-edit-menu 'summary)
2322
2323   (unless (boundp 'gnus-summary-misc-menu)
2324
2325     (easy-menu-define
2326       gnus-summary-kill-menu gnus-summary-mode-map ""
2327       (cons
2328        "Score"
2329        (nconc
2330         (list
2331          ["Customize" gnus-score-customize t])
2332         (gnus-make-score-map 'increase)
2333         (gnus-make-score-map 'lower)
2334         '(("Mark"
2335            ["Kill below" gnus-summary-kill-below t]
2336            ["Mark above" gnus-summary-mark-above t]
2337            ["Tick above" gnus-summary-tick-above t]
2338            ["Clear above" gnus-summary-clear-above t])
2339           ["Current score" gnus-summary-current-score t]
2340           ["Set score" gnus-summary-set-score t]
2341           ["Switch current score file..." gnus-score-change-score-file t]
2342           ["Set mark below..." gnus-score-set-mark-below t]
2343           ["Set expunge below..." gnus-score-set-expunge-below t]
2344           ["Edit current score file" gnus-score-edit-current-scores t]
2345           ["Edit score file..." gnus-score-edit-file t]
2346           ["Trace score" gnus-score-find-trace t]
2347           ["Find words" gnus-score-find-favourite-words t]
2348           ["Rescore buffer" gnus-summary-rescore t]
2349           ["Increase score..." gnus-summary-increase-score t]
2350           ["Lower score..." gnus-summary-lower-score t]))))
2351
2352     ;; Define both the Article menu in the summary buffer and the
2353     ;; equivalent Commands menu in the article buffer here for
2354     ;; consistency.
2355     (let ((innards
2356            `(("Hide"
2357               ["All" gnus-article-hide t]
2358               ["Headers" gnus-article-hide-headers t]
2359               ["Signature" gnus-article-hide-signature t]
2360               ["Citation" gnus-article-hide-citation t]
2361               ["List identifiers" gnus-article-hide-list-identifiers t]
2362               ["Banner" gnus-article-strip-banner t]
2363               ["Boring headers" gnus-article-hide-boring-headers t])
2364              ("Highlight"
2365               ["All" gnus-article-highlight t]
2366               ["Headers" gnus-article-highlight-headers t]
2367               ["Signature" gnus-article-highlight-signature t]
2368               ["Citation" gnus-article-highlight-citation t])
2369              ("MIME"
2370               ["Words" gnus-article-decode-mime-words t]
2371               ["Charset" gnus-article-decode-charset t]
2372               ["QP" gnus-article-de-quoted-unreadable t]
2373               ["Base64" gnus-article-de-base64-unreadable t]
2374               ["View MIME buttons" gnus-summary-display-buttonized t]
2375               ["View all" gnus-mime-view-all-parts t]
2376               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2377               ["Encrypt body" gnus-article-encrypt-body
2378                :active (not (gnus-group-read-only-p))
2379                ,@(if (featurep 'xemacs) nil
2380                    '(:help "Encrypt the message body on disk"))]
2381               ["Extract all parts..." gnus-summary-save-parts t]
2382               ("Multipart"
2383                ["Repair multipart" gnus-summary-repair-multipart t]
2384                ["Pipe part..." gnus-article-pipe-part t]
2385                ["Inline part" gnus-article-inline-part t]
2386                ["View part as type..." gnus-article-view-part-as-type t]
2387                ["Encrypt body" gnus-article-encrypt-body
2388                 :active (not (gnus-group-read-only-p))
2389                ,@(if (featurep 'xemacs) nil
2390                    '(:help "Encrypt the message body on disk"))]
2391                ["View part externally" gnus-article-view-part-externally t]
2392                ["View HTML parts in browser" gnus-article-browse-html-article t]
2393                ["View part with charset..." gnus-article-view-part-as-charset t]
2394                ["Copy part" gnus-article-copy-part t]
2395                ["Save part..." gnus-article-save-part t]
2396                ["View part" gnus-article-view-part t]))
2397              ("Date"
2398               ["Local" gnus-article-date-local t]
2399               ["ISO8601" gnus-article-date-iso8601 t]
2400               ["UT" gnus-article-date-ut t]
2401               ["Original" gnus-article-date-original t]
2402               ["Lapsed" gnus-article-date-lapsed t]
2403               ["User-defined" gnus-article-date-user t])
2404              ("Display"
2405               ["Remove images" gnus-article-remove-images t]
2406               ["Toggle smiley" gnus-treat-smiley t]
2407               ["Show X-Face" gnus-article-display-x-face t]
2408               ["Show picons in From" gnus-treat-from-picon t]
2409               ["Show picons in mail headers" gnus-treat-mail-picon t]
2410               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2411               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2412               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2413               ("View as different encoding"
2414                ,@(gnus-summary-menu-split
2415                   (mapcar
2416                    (lambda (cs)
2417                      ;; Since easymenu under Emacs doesn't allow
2418                      ;; lambda forms for menu commands, we should
2419                      ;; provide intern'ed function symbols.
2420                      (let ((command (intern (format "\
2421 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2422                        (fset command
2423                              `(lambda ()
2424                                 (interactive)
2425                                 (let ((gnus-summary-show-article-charset-alist
2426                                        '((1 . ,cs))))
2427                                   (gnus-summary-show-article 1))))
2428                        `[,(symbol-name cs) ,command t]))
2429                    (sort (if (fboundp 'coding-system-list)
2430                              (coding-system-list)
2431                            (mapcar 'car mm-mime-mule-charset-alist))
2432                          'string<)))))
2433              ("Washing"
2434               ("Remove Blanks"
2435                ["Leading" gnus-article-strip-leading-blank-lines t]
2436                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2437                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2438                ["All of the above" gnus-article-strip-blank-lines t]
2439                ["All" gnus-article-strip-all-blank-lines t]
2440                ["Leading space" gnus-article-strip-leading-space t]
2441                ["Trailing space" gnus-article-strip-trailing-space t]
2442                ["Leading space in headers"
2443                 gnus-article-remove-leading-whitespace t])
2444               ["Overstrike" gnus-article-treat-overstrike t]
2445               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2446               ["Non-ASCII" gnus-article-treat-non-ascii t]
2447               ["Emphasis" gnus-article-emphasize t]
2448               ["Word wrap" gnus-article-fill-cited-article t]
2449               ["Fill long lines" gnus-article-fill-long-lines t]
2450               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2451               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2452               ["Remove CR" gnus-article-remove-cr t]
2453               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2454               ["Base64" gnus-article-de-base64-unreadable t]
2455               ["Rot 13" gnus-summary-caesar-message
2456                ,@(if (featurep 'xemacs) '(t)
2457                    '(:help "\"Caesar rotate\" article by 13"))]
2458               ["De-IDNA" gnus-summary-idna-message t]
2459               ["Morse decode" gnus-summary-morse-message t]
2460               ["Unix pipe..." gnus-summary-pipe-message t]
2461               ["Add buttons" gnus-article-add-buttons t]
2462               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2463               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2464               ["Verbose header" gnus-summary-verbose-headers t]
2465               ["Toggle header" gnus-summary-toggle-header t]
2466               ["Unfold headers" gnus-article-treat-unfold-headers t]
2467               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2468               ["Html" gnus-article-wash-html t]
2469               ["Unsplit URLs" gnus-article-unsplit-urls t]
2470               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2471               ["Decode HZ" gnus-article-decode-HZ t]
2472               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2473               ("(Outlook) Deuglify"
2474                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2475                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2476                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2477                ["Full (Outlook) deuglify"
2478                 gnus-article-outlook-deuglify-article t])
2479               )
2480              ("Output"
2481               ["Save in default format..." gnus-summary-save-article
2482                ,@(if (featurep 'xemacs) '(t)
2483                    '(:help "Save article using default method"))]
2484               ["Save in file..." gnus-summary-save-article-file
2485                ,@(if (featurep 'xemacs) '(t)
2486                    '(:help "Save article in file"))]
2487               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2488               ["Save in MH folder..." gnus-summary-save-article-folder t]
2489               ["Save in VM folder..." gnus-summary-save-article-vm t]
2490               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2491               ["Save body in file..." gnus-summary-save-article-body-file t]
2492               ["Pipe through a filter..." gnus-summary-pipe-output t]
2493               ["Print with Muttprint..." gnus-summary-muttprint t]
2494               ["Print" gnus-summary-print-article
2495                ,@(if (featurep 'xemacs) '(t)
2496                    '(:help "Generate and print a PostScript image"))])
2497              ("Copy, move,... (Backend)"
2498               ,@(if (featurep 'xemacs) nil
2499                   '(:help "Copying, moving, expiring articles..."))
2500               ["Respool article..." gnus-summary-respool-article t]
2501               ["Move article..." gnus-summary-move-article
2502                (gnus-check-backend-function
2503                 'request-move-article gnus-newsgroup-name)]
2504               ["Copy article..." gnus-summary-copy-article t]
2505               ["Crosspost article..." gnus-summary-crosspost-article
2506                (gnus-check-backend-function
2507                 'request-replace-article gnus-newsgroup-name)]
2508               ["Import file..." gnus-summary-import-article
2509                (gnus-check-backend-function
2510                 'request-accept-article gnus-newsgroup-name)]
2511               ["Create article..." gnus-summary-create-article
2512                (gnus-check-backend-function
2513                 'request-accept-article gnus-newsgroup-name)]
2514               ["Check if posted" gnus-summary-article-posted-p t]
2515               ["Edit article" gnus-summary-edit-article
2516                (not (gnus-group-read-only-p))]
2517               ["Delete article" gnus-summary-delete-article
2518                (gnus-check-backend-function
2519                 'request-expire-articles gnus-newsgroup-name)]
2520               ["Query respool" gnus-summary-respool-query t]
2521               ["Trace respool" gnus-summary-respool-trace t]
2522               ["Delete expirable articles" gnus-summary-expire-articles-now
2523                (gnus-check-backend-function
2524                 'request-expire-articles gnus-newsgroup-name)])
2525              ("Extract"
2526               ["Uudecode" gnus-uu-decode-uu
2527                ,@(if (featurep 'xemacs) '(t)
2528                    '(:help "Decode uuencoded article(s)"))]
2529               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2530               ["Unshar" gnus-uu-decode-unshar t]
2531               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2532               ["Save" gnus-uu-decode-save t]
2533               ["Binhex" gnus-uu-decode-binhex t]
2534               ["PostScript" gnus-uu-decode-postscript t]
2535               ["All MIME parts" gnus-summary-save-parts t])
2536              ("Cache"
2537               ["Enter article" gnus-cache-enter-article t]
2538               ["Remove article" gnus-cache-remove-article t])
2539              ["Translate" gnus-article-babel t]
2540              ["Select article buffer" gnus-summary-select-article-buffer t]
2541              ["Make article buffer sticky" gnus-sticky-article t]
2542              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2543              ["Isearch article..." gnus-summary-isearch-article t]
2544              ["Beginning of the article" gnus-summary-beginning-of-article t]
2545              ["End of the article" gnus-summary-end-of-article t]
2546              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2547              ["Fetch referenced articles" gnus-summary-refer-references t]
2548              ["Fetch current thread" gnus-summary-refer-thread t]
2549              ["Fetch article with id..." gnus-summary-refer-article t]
2550              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2551              ["Redisplay" gnus-summary-show-article t]
2552              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2553       (easy-menu-define
2554         gnus-summary-article-menu gnus-summary-mode-map ""
2555         (cons "Article" innards))
2556
2557       (if (not (keymapp gnus-summary-article-menu))
2558           (easy-menu-define
2559             gnus-article-commands-menu gnus-article-mode-map ""
2560             (cons "Commands" innards))
2561         ;; in Emacs, don't share menu.
2562         (setq gnus-article-commands-menu
2563               (copy-keymap gnus-summary-article-menu))
2564         (define-key gnus-article-mode-map [menu-bar commands]
2565           (cons "Commands" gnus-article-commands-menu))))
2566
2567     (easy-menu-define
2568       gnus-summary-thread-menu gnus-summary-mode-map ""
2569       '("Threads"
2570         ["Find all messages in thread" gnus-summary-refer-thread t]
2571         ["Toggle threading" gnus-summary-toggle-threads t]
2572         ["Hide threads" gnus-summary-hide-all-threads t]
2573         ["Show threads" gnus-summary-show-all-threads t]
2574         ["Hide thread" gnus-summary-hide-thread t]
2575         ["Show thread" gnus-summary-show-thread t]
2576         ["Go to next thread" gnus-summary-next-thread t]
2577         ["Go to previous thread" gnus-summary-prev-thread t]
2578         ["Go down thread" gnus-summary-down-thread t]
2579         ["Go up thread" gnus-summary-up-thread t]
2580         ["Top of thread" gnus-summary-top-thread t]
2581         ["Mark thread as read" gnus-summary-kill-thread t]
2582         ["Mark thread as expired" gnus-summary-expire-thread t]
2583         ["Lower thread score" gnus-summary-lower-thread t]
2584         ["Raise thread score" gnus-summary-raise-thread t]
2585         ["Rethread current" gnus-summary-rethread-current t]))
2586
2587     (easy-menu-define
2588       gnus-summary-post-menu gnus-summary-mode-map ""
2589       `("Post"
2590         ["Send a message (mail or news)" gnus-summary-post-news
2591          ,@(if (featurep 'xemacs) '(t)
2592              '(:help "Compose a new message (mail or news)"))]
2593         ["Followup" gnus-summary-followup
2594          ,@(if (featurep 'xemacs) '(t)
2595              '(:help "Post followup to this article"))]
2596         ["Followup and yank" gnus-summary-followup-with-original
2597          ,@(if (featurep 'xemacs) '(t)
2598              '(:help "Post followup to this article, quoting its contents"))]
2599         ["Supersede article" gnus-summary-supersede-article t]
2600         ["Cancel article" gnus-summary-cancel-article
2601          ,@(if (featurep 'xemacs) '(t)
2602              '(:help "Cancel an article you posted"))]
2603         ["Reply" gnus-summary-reply t]
2604         ["Reply and yank" gnus-summary-reply-with-original t]
2605         ["Wide reply" gnus-summary-wide-reply t]
2606         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2607          ,@(if (featurep 'xemacs) '(t)
2608              '(:help "Mail a reply, quoting this article"))]
2609         ["Very wide reply" gnus-summary-very-wide-reply t]
2610         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2611          ,@(if (featurep 'xemacs) '(t)
2612              '(:help "Mail a very wide reply, quoting this article"))]
2613         ["Mail forward" gnus-summary-mail-forward t]
2614         ["Post forward" gnus-summary-post-forward t]
2615         ["Digest and mail" gnus-uu-digest-mail-forward t]
2616         ["Digest and post" gnus-uu-digest-post-forward t]
2617         ["Resend message" gnus-summary-resend-message t]
2618         ["Resend message edit" gnus-summary-resend-message-edit t]
2619         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2620         ["Send a mail" gnus-summary-mail-other-window t]
2621         ["Create a local message" gnus-summary-news-other-window t]
2622         ["Uuencode and post" gnus-uu-post-news
2623          ,@(if (featurep 'xemacs) '(t)
2624              '(:help "Post a uuencoded article"))]
2625         ["Followup via news" gnus-summary-followup-to-mail t]
2626         ["Followup via news and yank"
2627          gnus-summary-followup-to-mail-with-original t]
2628         ["Strip signature on reply"
2629          (lambda ()
2630            (interactive)
2631            (if (not (memq message-cite-function
2632                           '(message-cite-original-without-signature
2633                             message-cite-original)))
2634                ;; Stupid workaround for XEmacs not honoring :visible.
2635                (message "Can't toggle this value of `message-cite-function'")
2636              (setq message-cite-function
2637                    (if (eq message-cite-function
2638                            'message-cite-original-without-signature)
2639                        'message-cite-original
2640                      'message-cite-original-without-signature))))
2641          ;; XEmacs barfs on :visible.
2642          ,@(if (featurep 'xemacs) nil
2643              '(:visible (memq message-cite-function
2644                               '(message-cite-original-without-signature
2645                                 message-cite-original))))
2646          :style toggle
2647          :selected (eq message-cite-function
2648                        'message-cite-original-without-signature)
2649          ,@(if (featurep 'xemacs) nil
2650              '(:help "Strip signature from cited article when replying."))]
2651         ;;("Draft"
2652         ;;["Send" gnus-summary-send-draft t]
2653         ;;["Send bounced" gnus-resend-bounced-mail t])
2654         ))
2655
2656     (cond
2657      ((not (keymapp gnus-summary-post-menu))
2658       (setq gnus-article-post-menu gnus-summary-post-menu))
2659      ((not gnus-article-post-menu)
2660       ;; Don't share post menu.
2661       (setq gnus-article-post-menu
2662             (copy-keymap gnus-summary-post-menu))))
2663     (define-key gnus-article-mode-map [menu-bar post]
2664       (cons "Post" gnus-article-post-menu))
2665
2666     (easy-menu-define
2667       gnus-summary-misc-menu gnus-summary-mode-map ""
2668       `("Gnus"
2669         ("Mark Read"
2670          ["Mark as read" gnus-summary-mark-as-read-forward t]
2671          ["Mark same subject and select"
2672           gnus-summary-kill-same-subject-and-select t]
2673          ["Mark same subject" gnus-summary-kill-same-subject t]
2674          ["Catchup" gnus-summary-catchup
2675           ,@(if (featurep 'xemacs) '(t)
2676               '(:help "Mark unread articles in this group as read"))]
2677          ["Catchup all" gnus-summary-catchup-all t]
2678          ["Catchup to here" gnus-summary-catchup-to-here t]
2679          ["Catchup from here" gnus-summary-catchup-from-here t]
2680          ["Catchup region" gnus-summary-mark-region-as-read
2681           (gnus-mark-active-p)]
2682          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2683         ("Mark Various"
2684          ["Tick" gnus-summary-tick-article-forward t]
2685          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2686          ["Remove marks" gnus-summary-clear-mark-forward t]
2687          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2688          ["Set bookmark" gnus-summary-set-bookmark t]
2689          ["Remove bookmark" gnus-summary-remove-bookmark t])
2690         ("Limit to"
2691          ["Marks..." gnus-summary-limit-to-marks t]
2692          ["Subject..." gnus-summary-limit-to-subject t]
2693          ["Author..." gnus-summary-limit-to-author t]
2694          ["Recipient..." gnus-summary-limit-to-recipient t]
2695          ["Address..." gnus-summary-limit-to-address t]
2696          ["Age..." gnus-summary-limit-to-age t]
2697          ["Extra..." gnus-summary-limit-to-extra t]
2698          ["Score..." gnus-summary-limit-to-score t]
2699          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2700          ["Unread" gnus-summary-limit-to-unread t]
2701          ["Unseen" gnus-summary-limit-to-unseen t]
2702          ["Singletons" gnus-summary-limit-to-singletons t]
2703          ["Replied" gnus-summary-limit-to-replied t]
2704          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2705          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2706          ["Pop limit" gnus-summary-pop-limit t]
2707          ["Show dormant" gnus-summary-limit-include-dormant t]
2708          ["Hide childless dormant"
2709           gnus-summary-limit-exclude-childless-dormant t]
2710          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2711          ["Hide marked" gnus-summary-limit-exclude-marks t]
2712          ["Show expunged" gnus-summary-limit-include-expunged t])
2713         ("Process Mark"
2714          ["Set mark" gnus-summary-mark-as-processable t]
2715          ["Remove mark" gnus-summary-unmark-as-processable t]
2716          ["Remove all marks" gnus-summary-unmark-all-processable t]
2717          ["Invert marks" gnus-uu-invert-processable t]
2718          ["Mark above" gnus-uu-mark-over t]
2719          ["Mark series" gnus-uu-mark-series t]
2720          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2721          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2722          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2723          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2724          ["Mark all" gnus-uu-mark-all t]
2725          ["Mark buffer" gnus-uu-mark-buffer t]
2726          ["Mark sparse" gnus-uu-mark-sparse t]
2727          ["Mark thread" gnus-uu-mark-thread t]
2728          ["Unmark thread" gnus-uu-unmark-thread t]
2729          ("Process Mark Sets"
2730           ["Kill" gnus-summary-kill-process-mark t]
2731           ["Yank" gnus-summary-yank-process-mark
2732            gnus-newsgroup-process-stack]
2733           ["Save" gnus-summary-save-process-mark t]
2734           ["Run command on marked..." gnus-summary-universal-argument t]))
2735         ("Registry Marks")
2736         ("Scroll article"
2737          ["Page forward" gnus-summary-next-page
2738           ,@(if (featurep 'xemacs) '(t)
2739               '(:help "Show next page of article"))]
2740          ["Page backward" gnus-summary-prev-page
2741           ,@(if (featurep 'xemacs) '(t)
2742               '(:help "Show previous page of article"))]
2743          ["Line forward" gnus-summary-scroll-up t])
2744         ("Move"
2745          ["Next unread article" gnus-summary-next-unread-article t]
2746          ["Previous unread article" gnus-summary-prev-unread-article t]
2747          ["Next article" gnus-summary-next-article t]
2748          ["Previous article" gnus-summary-prev-article t]
2749          ["Next unread subject" gnus-summary-next-unread-subject t]
2750          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2751          ["Next article same subject" gnus-summary-next-same-subject t]
2752          ["Previous article same subject" gnus-summary-prev-same-subject t]
2753          ["First unread article" gnus-summary-first-unread-article t]
2754          ["Best unread article" gnus-summary-best-unread-article t]
2755          ["Go to subject number..." gnus-summary-goto-subject t]
2756          ["Go to article number..." gnus-summary-goto-article t]
2757          ["Go to the last article" gnus-summary-goto-last-article t]
2758          ["Pop article off history" gnus-summary-pop-article t])
2759         ("Sort"
2760          ["Sort by number" gnus-summary-sort-by-number t]
2761          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2762          ["Sort by author" gnus-summary-sort-by-author t]
2763          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2764          ["Sort by subject" gnus-summary-sort-by-subject t]
2765          ["Sort by date" gnus-summary-sort-by-date t]
2766          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2767          ["Sort by score" gnus-summary-sort-by-score t]
2768          ["Sort by lines" gnus-summary-sort-by-lines t]
2769          ["Sort by characters" gnus-summary-sort-by-chars t]
2770          ["Randomize" gnus-summary-sort-by-random t]
2771          ["Original sort" gnus-summary-sort-by-original t])
2772         ("Help"
2773          ["Describe group" gnus-summary-describe-group t]
2774          ["Read manual" gnus-info-find-node t])
2775         ("Modes"
2776          ["Pick and read" gnus-pick-mode t]
2777          ["Binary" gnus-binary-mode t])
2778         ("Regeneration"
2779          ["Regenerate" gnus-summary-prepare t]
2780          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2781          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2782          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2783          ["Toggle threading" gnus-summary-toggle-threads t])
2784         ["See old articles" gnus-summary-insert-old-articles t]
2785         ["See new articles" gnus-summary-insert-new-articles t]
2786         ["Filter articles..." gnus-summary-execute-command t]
2787         ["Run command on articles..." gnus-summary-universal-argument t]
2788         ["Search articles forward..." gnus-summary-search-article-forward t]
2789         ["Search articles backward..." gnus-summary-search-article-backward t]
2790         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2791         ["Expand window" gnus-summary-expand-window t]
2792         ["Expire expirable articles" gnus-summary-expire-articles
2793          (gnus-check-backend-function
2794           'request-expire-articles gnus-newsgroup-name)]
2795         ["Edit local kill file" gnus-summary-edit-local-kill t]
2796         ["Edit main kill file" gnus-summary-edit-global-kill t]
2797         ["Edit group parameters" gnus-summary-edit-parameters t]
2798         ["Customize group parameters" gnus-summary-customize-parameters t]
2799         ["Send a bug report" gnus-bug t]
2800         ("Exit"
2801          ["Catchup and exit" gnus-summary-catchup-and-exit
2802           ,@(if (featurep 'xemacs) '(t)
2803               '(:help "Mark unread articles in this group as read, then exit"))]
2804          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2805          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2806          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2807          ["Exit group" gnus-summary-exit
2808           ,@(if (featurep 'xemacs) '(t)
2809               '(:help "Exit current group, return to group selection mode"))]
2810          ["Exit group without updating" gnus-summary-exit-no-update t]
2811          ["Exit and goto next group" gnus-summary-next-group t]
2812          ["Exit and goto prev group" gnus-summary-prev-group t]
2813          ["Reselect group" gnus-summary-reselect-current-group t]
2814          ["Rescan group" gnus-summary-rescan-group t]
2815          ["Update dribble" gnus-summary-save-newsrc t])))
2816
2817     (gnus-run-hooks 'gnus-summary-menu-hook)))
2818
2819 (defvar gnus-summary-tool-bar-map nil)
2820
2821 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2822 ;; affect _new_ message buffers.  We might add a function that walks thru all
2823 ;; summary-mode buffers and force the update.
2824 (defun gnus-summary-tool-bar-update (&optional symbol value)
2825   "Update summary mode toolbar.
2826 Setter function for custom variables."
2827   (setq-default gnus-summary-tool-bar-map nil)
2828   (when symbol
2829     ;; When used as ":set" function:
2830     (set-default symbol value))
2831   (when (gnus-buffer-live-p gnus-summary-buffer)
2832     (with-current-buffer gnus-summary-buffer
2833       (gnus-summary-make-tool-bar))))
2834
2835 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2836                                      'gnus-summary-tool-bar-gnome
2837                                    'gnus-summary-tool-bar-retro)
2838   "Specifies the Gnus summary tool bar.
2839
2840 It can be either a list or a symbol referring to a list.  See
2841 `gmm-tool-bar-from-list' for the format of the list.  The
2842 default key map is `gnus-summary-mode-map'.
2843
2844 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2845 `gnus-summary-tool-bar-retro'."
2846   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2847                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2848                  (repeat :tag "User defined list" gmm-tool-bar-item)
2849                  (symbol))
2850   :version "23.1" ;; No Gnus
2851   :initialize 'custom-initialize-default
2852   :set 'gnus-summary-tool-bar-update
2853   :group 'gnus-summary)
2854
2855 (defcustom gnus-summary-tool-bar-gnome
2856   '((gnus-summary-post-news "mail/compose" nil)
2857     (gnus-summary-insert-new-articles "mail/inbox" nil
2858                                       :visible (or (not gnus-agent)
2859                                                    gnus-plugged))
2860     (gnus-summary-reply-with-original "mail/reply")
2861     (gnus-summary-reply "mail/reply" nil :visible nil)
2862     (gnus-summary-followup-with-original "mail/reply-all")
2863     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2864     (gnus-summary-mail-forward "mail/forward")
2865     (gnus-summary-save-article "mail/save")
2866     (gnus-summary-search-article-forward "search" nil :visible nil)
2867     (gnus-summary-print-article "print")
2868     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2869     ;; Some new commands that may need more suitable icons:
2870     (gnus-summary-save-newsrc "save" nil :visible nil)
2871     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2872     (gnus-summary-prev-article "left-arrow")
2873     (gnus-summary-next-article "right-arrow")
2874     (gnus-summary-next-page "next-page")
2875     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2876     ;;
2877     ;; Maybe some sort-by-... could be added:
2878     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2879     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2880     (gnus-summary-mark-as-expirable
2881      "delete" nil
2882      :visible (gnus-check-backend-function 'request-expire-articles
2883                                            gnus-newsgroup-name))
2884     (gnus-summary-mark-as-spam
2885      "mail/spam" t
2886      :visible (and (fboundp 'spam-group-ham-contents-p)
2887                    (spam-group-ham-contents-p gnus-newsgroup-name))
2888      :help "Mark as spam")
2889     (gnus-summary-mark-as-read-forward
2890      "mail/not-spam" nil
2891      :visible (and (fboundp 'spam-group-spam-contents-p)
2892                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2893     ;;
2894     (gnus-summary-exit "exit")
2895     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2896     (gnus-info-find-node "help"))
2897   "List of functions for the summary tool bar (GNOME style).
2898
2899 See `gmm-tool-bar-from-list' for the format of the list."
2900   :type '(repeat gmm-tool-bar-item)
2901   :version "23.1" ;; No Gnus
2902   :initialize 'custom-initialize-default
2903   :set 'gnus-summary-tool-bar-update
2904   :group 'gnus-summary)
2905
2906 (defcustom gnus-summary-tool-bar-retro
2907   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2908     (gnus-summary-next-unread-article "gnus/next-ur")
2909     (gnus-summary-post-news "gnus/post")
2910     (gnus-summary-followup-with-original "gnus/fuwo")
2911     (gnus-summary-followup "gnus/followup")
2912     (gnus-summary-reply-with-original "gnus/reply-wo")
2913     (gnus-summary-reply "gnus/reply")
2914     (gnus-summary-caesar-message "gnus/rot13")
2915     (gnus-uu-decode-uu "gnus/uu-decode")
2916     (gnus-summary-save-article-file "gnus/save-aif")
2917     (gnus-summary-save-article "gnus/save-art")
2918     (gnus-uu-post-news "gnus/uu-post")
2919     (gnus-summary-catchup "gnus/catchup")
2920     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2921     (gnus-summary-exit "gnus/exit-summ")
2922     ;; Some new command that may need more suitable icons:
2923     (gnus-summary-print-article "gnus/print" nil :visible nil)
2924     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2925     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2926     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2927     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2928     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2929     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2930     ;;
2931     (gnus-info-find-node "gnus/help" nil :visible nil))
2932   "List of functions for the summary tool bar (retro look).
2933
2934 See `gmm-tool-bar-from-list' for the format of the list."
2935   :type '(repeat gmm-tool-bar-item)
2936   :version "23.1" ;; No Gnus
2937   :initialize 'custom-initialize-default
2938   :set 'gnus-summary-tool-bar-update
2939   :group 'gnus-summary)
2940
2941 (defcustom gnus-summary-tool-bar-zap-list t
2942   "List of icon items from the global tool bar.
2943 These items are not displayed in the Gnus summary mode tool bar.
2944
2945 See `gmm-tool-bar-from-list' for the format of the list."
2946   :type 'gmm-tool-bar-zap-list
2947   :version "23.1" ;; No Gnus
2948   :initialize 'custom-initialize-default
2949   :set 'gnus-summary-tool-bar-update
2950   :group 'gnus-summary)
2951
2952 (defvar image-load-path)
2953 (defvar tool-bar-map)
2954
2955 (defun gnus-summary-make-tool-bar (&optional force)
2956   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2957 When FORCE, rebuild the tool bar."
2958   (when (and (not (featurep 'xemacs))
2959              (boundp 'tool-bar-mode)
2960              tool-bar-mode
2961              (or (not gnus-summary-tool-bar-map) force))
2962     (let* ((load-path
2963             (gmm-image-load-path-for-library "gnus"
2964                                              "mail/save.xpm"
2965                                              nil t))
2966            (image-load-path (cons (car load-path)
2967                                   (when (boundp 'image-load-path)
2968                                     image-load-path)))
2969            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2970                                         gnus-summary-tool-bar-zap-list
2971                                         'gnus-summary-mode-map)))
2972       (when map
2973         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2974         ;; uses its value.
2975         (setq gnus-summary-tool-bar-map map))))
2976   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2977
2978 (defun gnus-score-set-default (var value)
2979   "A version of set that updates the GNU Emacs menu-bar."
2980   (set var value)
2981   ;; It is the message that forces the active status to be updated.
2982   (message ""))
2983
2984 (defun gnus-make-score-map (type)
2985   "Make a summary score map of type TYPE."
2986   (if t
2987       nil
2988     (let ((headers '(("author" "from" string)
2989                      ("subject" "subject" string)
2990                      ("article body" "body" string)
2991                      ("article head" "head" string)
2992                      ("xref" "xref" string)
2993                      ("extra header" "extra" string)
2994                      ("lines" "lines" number)
2995                      ("followups to author" "followup" string)))
2996           (types '((number ("less than" <)
2997                            ("greater than" >)
2998                            ("equal" =))
2999                    (string ("substring" s)
3000                            ("exact string" e)
3001                            ("fuzzy string" f)
3002                            ("regexp" r))))
3003           (perms '(("temporary" (current-time-string))
3004                    ("permanent" nil)
3005                    ("immediate" now)))
3006           header)
3007       (list
3008        (apply
3009         'nconc
3010         (list
3011          (if (eq type 'lower)
3012              "Lower score"
3013            "Increase score"))
3014         (let (outh)
3015           (while headers
3016             (setq header (car headers))
3017             (setq outh
3018                   (cons
3019                    (apply
3020                     'nconc
3021                     (list (car header))
3022                     (let ((ts (cdr (assoc (nth 2 header) types)))
3023                           outt)
3024                       (while ts
3025                         (setq outt
3026                               (cons
3027                                (apply
3028                                 'nconc
3029                                 (list (caar ts))
3030                                 (let ((ps perms)
3031                                       outp)
3032                                   (while ps
3033                                     (setq outp
3034                                           (cons
3035                                            (vector
3036                                             (caar ps)
3037                                             (list
3038                                              'gnus-summary-score-entry
3039                                              (nth 1 header)
3040                                              (if (or (string= (nth 1 header)
3041                                                               "head")
3042                                                      (string= (nth 1 header)
3043                                                               "body"))
3044                                                  ""
3045                                                (list 'gnus-summary-header
3046                                                      (nth 1 header)))
3047                                              (list 'quote (nth 1 (car ts)))
3048                                              (list 'gnus-score-delta-default
3049                                                    nil)
3050                                              (nth 1 (car ps))
3051                                              t)
3052                                             t)
3053                                            outp))
3054                                     (setq ps (cdr ps)))
3055                                   (list (nreverse outp))))
3056                                outt))
3057                         (setq ts (cdr ts)))
3058                       (list (nreverse outt))))
3059                    outh))
3060             (setq headers (cdr headers)))
3061           (list (nreverse outh))))))))
3062
3063
3064 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3065 (defvar bookmark-make-record-function)
3066 \f
3067 (defvar bidi-paragraph-direction)
3068
3069 (defun gnus-summary-mode (&optional group)
3070   "Major mode for reading articles.
3071
3072 All normal editing commands are switched off.
3073 \\<gnus-summary-mode-map>
3074 Each line in this buffer represents one article.  To read an
3075 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3076 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3077 respectively.
3078
3079 You can also post articles and send mail from this buffer.  To
3080 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3081 of an article, type `\\[gnus-summary-reply]'.
3082
3083 There are approx. one gazillion commands you can execute in this
3084 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3085
3086 The following commands are available:
3087
3088 \\{gnus-summary-mode-map}"
3089   (interactive)
3090   (kill-all-local-variables)
3091   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3092     (gnus-summary-make-local-variables))
3093   (gnus-summary-make-local-variables)
3094   (setq gnus-newsgroup-name group)
3095   (when (gnus-visual-p 'summary-menu 'menu)
3096     (gnus-summary-make-menu-bar)
3097     (gnus-summary-make-tool-bar))
3098   (gnus-make-thread-indent-array)
3099   (gnus-simplify-mode-line)
3100   (setq major-mode 'gnus-summary-mode)
3101   (setq mode-name "Summary")
3102   (use-local-map gnus-summary-mode-map)
3103   (buffer-disable-undo)
3104   (setq buffer-read-only t              ;Disable modification
3105         show-trailing-whitespace nil)
3106   (setq truncate-lines t)
3107   ;; Force paragraph direction to be left-to-right.  Don't make it
3108   ;; bound globally in old Emacsen and XEmacsen.
3109   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3110   (add-to-invisibility-spec '(gnus-sum . t))
3111   (gnus-summary-set-display-table)
3112   (gnus-set-default-directory)
3113   (make-local-variable 'gnus-summary-line-format)
3114   (make-local-variable 'gnus-summary-line-format-spec)
3115   (make-local-variable 'gnus-summary-dummy-line-format)
3116   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3117   (make-local-variable 'gnus-summary-mark-positions)
3118   (gnus-make-local-hook 'pre-command-hook)
3119   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3120   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3121   (turn-on-gnus-mailing-list-mode)
3122   (mm-enable-multibyte)
3123   (set (make-local-variable 'bookmark-make-record-function)
3124        'gnus-summary-bookmark-make-record)
3125   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3126   (gnus-update-summary-mark-positions))
3127
3128 (defun gnus-summary-make-local-variables ()
3129   "Make all the local summary buffer variables."
3130   (let (global)
3131     (dolist (local gnus-summary-local-variables)
3132       (if (consp local)
3133           (progn
3134             (if (eq (cdr local) 'global)
3135                 ;; Copy the global value of the variable.
3136                 (setq global (symbol-value (car local)))
3137               ;; Use the value from the list.
3138               (setq global (eval (cdr local))))
3139             (set (make-local-variable (car local)) global))
3140         ;; Simple nil-valued local variable.
3141         (set (make-local-variable local) nil)))))
3142
3143 ;; Summary data functions.
3144
3145 (defmacro gnus-data-number (data)
3146   `(car ,data))
3147
3148 (defmacro gnus-data-set-number (data number)
3149   `(setcar ,data ,number))
3150
3151 (defmacro gnus-data-mark (data)
3152   `(nth 1 ,data))
3153
3154 (defmacro gnus-data-set-mark (data mark)
3155   `(setcar (nthcdr 1 ,data) ,mark))
3156
3157 (defmacro gnus-data-pos (data)
3158   `(nth 2 ,data))
3159
3160 (defmacro gnus-data-set-pos (data pos)
3161   `(setcar (nthcdr 2 ,data) ,pos))
3162
3163 (defmacro gnus-data-header (data)
3164   `(nth 3 ,data))
3165
3166 (defmacro gnus-data-set-header (data header)
3167   `(setf (nth 3 ,data) ,header))
3168
3169 (defmacro gnus-data-level (data)
3170   `(nth 4 ,data))
3171
3172 (defmacro gnus-data-unread-p (data)
3173   `(= (nth 1 ,data) gnus-unread-mark))
3174
3175 (defmacro gnus-data-read-p (data)
3176   `(/= (nth 1 ,data) gnus-unread-mark))
3177
3178 (defmacro gnus-data-pseudo-p (data)
3179   `(consp (nth 3 ,data)))
3180
3181 (defmacro gnus-data-find (number)
3182   `(assq ,number gnus-newsgroup-data))
3183
3184 (defmacro gnus-data-find-list (number &optional data)
3185   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3186      (memq (assq ,number bdata)
3187            bdata)))
3188
3189 (defmacro gnus-data-make (number mark pos header level)
3190   `(list ,number ,mark ,pos ,header ,level))
3191
3192 (defun gnus-data-enter (after-article number mark pos header level offset)
3193   (let ((data (gnus-data-find-list after-article)))
3194     (unless data
3195       (error "No such article: %d" after-article))
3196     (setcdr data (cons (gnus-data-make number mark pos header level)
3197                        (cdr data)))
3198     (setq gnus-newsgroup-data-reverse nil)
3199     (gnus-data-update-list (cddr data) offset)))
3200
3201 (defun gnus-data-enter-list (after-article list &optional offset)
3202   (when list
3203     (let ((data (and after-article (gnus-data-find-list after-article)))
3204           (ilist list))
3205       (if (not (or data
3206                    after-article))
3207           (let ((odata gnus-newsgroup-data))
3208             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3209             (when offset
3210               (gnus-data-update-list odata offset)))
3211         ;; Find the last element in the list to be spliced into the main
3212         ;; list.
3213         (setq list (last list))
3214         (if (not data)
3215             (progn
3216               (setcdr list gnus-newsgroup-data)
3217               (setq gnus-newsgroup-data ilist)
3218               (when offset
3219                 (gnus-data-update-list (cdr list) offset)))
3220           (setcdr list (cdr data))
3221           (setcdr data ilist)
3222           (when offset
3223             (gnus-data-update-list (cdr list) offset))))
3224       (setq gnus-newsgroup-data-reverse nil))))
3225
3226 (defun gnus-data-remove (article &optional offset)
3227   (let ((data gnus-newsgroup-data))
3228     (if (= (gnus-data-number (car data)) article)
3229         (progn
3230           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3231                 gnus-newsgroup-data-reverse nil)
3232           (when offset
3233             (gnus-data-update-list gnus-newsgroup-data offset)))
3234       (while (cdr data)
3235         (when (= (gnus-data-number (cadr data)) article)
3236           (setcdr data (cddr data))
3237           (when offset
3238             (gnus-data-update-list (cdr data) offset))
3239           (setq data nil
3240                 gnus-newsgroup-data-reverse nil))
3241         (setq data (cdr data))))))
3242
3243 (defmacro gnus-data-list (backward)
3244   `(if ,backward
3245        (or gnus-newsgroup-data-reverse
3246            (setq gnus-newsgroup-data-reverse
3247                  (reverse gnus-newsgroup-data)))
3248      gnus-newsgroup-data))
3249
3250 (defun gnus-data-update-list (data offset)
3251   "Add OFFSET to the POS of all data entries in DATA."
3252   (setq gnus-newsgroup-data-reverse nil)
3253   (while data
3254     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3255     (setq data (cdr data))))
3256
3257 (defun gnus-summary-article-pseudo-p (article)
3258   "Say whether this article is a pseudo article or not."
3259   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3260
3261 (defmacro gnus-summary-article-sparse-p (article)
3262   "Say whether this article is a sparse article or not."
3263   `(memq ,article gnus-newsgroup-sparse))
3264
3265 (defmacro gnus-summary-article-ancient-p (article)
3266   "Say whether this article is a sparse article or not."
3267   `(memq ,article gnus-newsgroup-ancient))
3268
3269 (defun gnus-article-parent-p (number)
3270   "Say whether this article is a parent or not."
3271   (let ((data (gnus-data-find-list number)))
3272     (and (cdr data)              ; There has to be an article after...
3273          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3274             (gnus-data-level (nth 1 data))))))
3275
3276 (defun gnus-article-children (number)
3277   "Return a list of all children to NUMBER."
3278   (let* ((data (gnus-data-find-list number))
3279          (level (gnus-data-level (car data)))
3280          children)
3281     (setq data (cdr data))
3282     (while (and data
3283                 (= (gnus-data-level (car data)) (1+ level)))
3284       (push (gnus-data-number (car data)) children)
3285       (setq data (cdr data)))
3286     children))
3287
3288 (defmacro gnus-summary-skip-intangible ()
3289   "If the current article is intangible, then jump to a different article."
3290   '(let ((to (get-text-property (point) 'gnus-intangible)))
3291      (and to (gnus-summary-goto-subject to))))
3292
3293 (defmacro gnus-summary-article-intangible-p ()
3294   "Say whether this article is intangible or not."
3295   '(get-text-property (point) 'gnus-intangible))
3296
3297 (defun gnus-article-read-p (article)
3298   "Say whether ARTICLE is read or not."
3299   (not (or (memq article gnus-newsgroup-marked)
3300            (memq article gnus-newsgroup-spam-marked)
3301            (memq article gnus-newsgroup-unreads)
3302            (memq article gnus-newsgroup-unselected)
3303            (memq article gnus-newsgroup-dormant))))
3304
3305 ;; Some summary mode macros.
3306
3307 (defmacro gnus-summary-article-number ()
3308   "The article number of the article on the current line.
3309 If there isn't an article number here, then we return the current
3310 article number."
3311   '(progn
3312      (gnus-summary-skip-intangible)
3313      (or (get-text-property (point) 'gnus-number)
3314          (gnus-summary-last-subject))))
3315
3316 (defmacro gnus-summary-article-header (&optional number)
3317   "Return the header of article NUMBER."
3318   `(gnus-data-header (gnus-data-find
3319                       ,(or number '(gnus-summary-article-number)))))
3320
3321 (defmacro gnus-summary-thread-level (&optional number)
3322   "Return the level of thread that starts with article NUMBER."
3323   `(if (and (eq gnus-summary-make-false-root 'dummy)
3324             (get-text-property (point) 'gnus-intangible))
3325        0
3326      (gnus-data-level (gnus-data-find
3327                        ,(or number '(gnus-summary-article-number))))))
3328
3329 (defmacro gnus-summary-article-mark (&optional number)
3330   "Return the mark of article NUMBER."
3331   `(gnus-data-mark (gnus-data-find
3332                     ,(or number '(gnus-summary-article-number)))))
3333
3334 (defmacro gnus-summary-article-pos (&optional number)
3335   "Return the position of the line of article NUMBER."
3336   `(gnus-data-pos (gnus-data-find
3337                    ,(or number '(gnus-summary-article-number)))))
3338
3339 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3340 (defmacro gnus-summary-article-subject (&optional number)
3341   "Return current subject string or nil if nothing."
3342   `(let ((headers
3343           ,(if number
3344                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3345              '(gnus-data-header (assq (gnus-summary-article-number)
3346                                       gnus-newsgroup-data)))))
3347      (and headers
3348           (vectorp headers)
3349           (mail-header-subject headers))))
3350
3351 (defmacro gnus-summary-article-score (&optional number)
3352   "Return current article score."
3353   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3354                   gnus-newsgroup-scored))
3355        gnus-summary-default-score 0))
3356
3357 (defun gnus-summary-article-children (&optional number)
3358   "Return a list of article numbers that are children of article NUMBER."
3359   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3360          (level (gnus-data-level (car data)))
3361          l children)
3362     (while (and (setq data (cdr data))
3363                 (> (setq l (gnus-data-level (car data))) level))
3364       (and (= (1+ level) l)
3365            (push (gnus-data-number (car data))
3366                  children)))
3367     (nreverse children)))
3368
3369 (defun gnus-summary-article-parent (&optional number)
3370   "Return the article number of the parent of article NUMBER."
3371   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3372                                     (gnus-data-list t)))
3373          (level (gnus-data-level (car data))))
3374     (if (zerop level)
3375         ()                              ; This is a root.
3376       ;; We search until we find an article with a level less than
3377       ;; this one.  That function has to be the parent.
3378       (while (and (setq data (cdr data))
3379                   (not (< (gnus-data-level (car data)) level))))
3380       (and data (gnus-data-number (car data))))))
3381
3382 (defun gnus-unread-mark-p (mark)
3383   "Say whether MARK is the unread mark."
3384   (= mark gnus-unread-mark))
3385
3386 (defun gnus-read-mark-p (mark)
3387   "Say whether MARK is one of the marks that mark as read.
3388 This is all marks except unread, ticked, dormant, and expirable."
3389   (not (or (= mark gnus-unread-mark)
3390            (= mark gnus-ticked-mark)
3391            (= mark gnus-spam-mark)
3392            (= mark gnus-dormant-mark)
3393            (= mark gnus-expirable-mark))))
3394
3395 (defmacro gnus-article-mark (number)
3396   "Return the MARK of article NUMBER.
3397 This macro should only be used when computing the mark the \"first\"
3398 time; i.e., when generating the summary lines.  After that,
3399 `gnus-summary-article-mark' should be used to examine the
3400 marks of articles."
3401   `(cond
3402     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3403     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3404     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3405     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3406     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3407     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3408     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3409     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3410            gnus-ancient-mark))))
3411
3412 ;; Saving hidden threads.
3413
3414 (defmacro gnus-save-hidden-threads (&rest forms)
3415   "Save hidden threads, eval FORMS, and restore the hidden threads."
3416   (let ((config (make-symbol "config")))
3417     `(let ((,config (gnus-hidden-threads-configuration)))
3418        (unwind-protect
3419            (save-excursion
3420              ,@forms)
3421          (gnus-restore-hidden-threads-configuration ,config)))))
3422 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3423 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3424
3425 (defun gnus-data-compute-positions ()
3426   "Compute the positions of all articles."
3427   (setq gnus-newsgroup-data-reverse nil)
3428   (let ((data gnus-newsgroup-data))
3429     (save-excursion
3430       (gnus-save-hidden-threads
3431         (gnus-summary-show-all-threads)
3432         (goto-char (point-min))
3433         (while data
3434           (while (get-text-property (point) 'gnus-intangible)
3435             (forward-line 1))
3436           (gnus-data-set-pos (car data) (+ (point) 3))
3437           (setq data (cdr data))
3438           (forward-line 1))))))
3439
3440 (defun gnus-hidden-threads-configuration ()
3441   "Return the current hidden threads configuration."
3442   (save-excursion
3443     (let (config)
3444       (goto-char (point-min))
3445       (while (not (eobp))
3446         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3447           (push (save-excursion (forward-line 0) (point)) config))
3448         (forward-line 1))
3449       config)))
3450
3451 (defun gnus-restore-hidden-threads-configuration (config)
3452   "Restore hidden threads configuration from CONFIG."
3453   (save-excursion
3454     (let (point (inhibit-read-only t))
3455       (while (setq point (pop config))
3456         (goto-char point)
3457         (gnus-summary-hide-thread)))))
3458
3459 ;; Various summary mode internalish functions.
3460
3461 (defun gnus-mouse-pick-article (e)
3462   (interactive "e")
3463   (mouse-set-point e)
3464   (gnus-summary-next-page nil t))
3465
3466 (defun gnus-summary-set-display-table ()
3467   "Change the display table.
3468 Odd characters have a tendency to mess
3469 up nicely formatted displays - we make all possible glyphs
3470 display only a single character."
3471
3472   ;; We start from the standard display table, if any.
3473   (let ((table (or (copy-sequence standard-display-table)
3474                    (make-display-table)))
3475         (i 32))
3476     ;; Nix out all the control chars...
3477     (while (>= (setq i (1- i)) 0)
3478       (gnus-put-display-table i [??] table))
3479    ;; ... but not newline and cr, of course.  (cr is necessary for the
3480     ;; selective display).
3481     (gnus-put-display-table ?\n nil table)
3482     (gnus-put-display-table ?\r nil table)
3483     ;; We keep TAB as well.
3484     (gnus-put-display-table ?\t nil table)
3485     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3486     ;; Emacs 23 (unicode), that are not set already.
3487     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3488                  160
3489                256)))
3490       (while (>= (setq i (1- i)) 127)
3491         ;; Only modify if the entry is nil.
3492         (unless (gnus-get-display-table i table)
3493           (gnus-put-display-table i [??] table))))
3494     (setq buffer-display-table table)))
3495
3496 (defun gnus-summary-set-article-display-arrow (pos)
3497   "Update the overlay arrow to point to line at position POS."
3498   (when gnus-summary-display-arrow
3499     (make-local-variable 'overlay-arrow-position)
3500     (make-local-variable 'overlay-arrow-string)
3501     (save-excursion
3502       (goto-char pos)
3503       (beginning-of-line)
3504       (unless overlay-arrow-position
3505         (setq overlay-arrow-position (make-marker)))
3506       (setq overlay-arrow-string "=>"
3507             overlay-arrow-position (set-marker overlay-arrow-position
3508                                                (point)
3509                                                (current-buffer))))))
3510
3511 (defun gnus-summary-setup-buffer (group)
3512   "Initialize summary buffer.
3513 If the setup was successful, non-nil is returned."
3514   (let ((buffer (gnus-summary-buffer-name group))
3515         (dead-name (concat "*Dead Summary "
3516                            (gnus-group-decoded-name group) "*")))
3517     ;; If a dead summary buffer exists, we kill it.
3518     (when (gnus-buffer-live-p dead-name)
3519       (gnus-kill-buffer dead-name))
3520     (if (get-buffer buffer)
3521         (progn
3522           (set-buffer buffer)
3523           (setq gnus-summary-buffer (current-buffer))
3524           (not gnus-newsgroup-prepared))
3525       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3526       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3527       (gnus-summary-mode group)
3528       (when (gnus-group-quit-config group)
3529         (set (make-local-variable 'gnus-single-article-buffer) nil))
3530       (make-local-variable 'gnus-article-buffer)
3531       (make-local-variable 'gnus-article-current)
3532       (make-local-variable 'gnus-original-article-buffer)
3533       (setq gnus-newsgroup-name group)
3534       ;; Set any local variables in the group parameters.
3535       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3536       t)))
3537
3538 (defun gnus-set-global-variables ()
3539   "Set the global equivalents of the buffer-local variables.
3540 They are set to the latest values they had.  These reflect the summary
3541 buffer that was in action when the last article was fetched."
3542   (when (eq major-mode 'gnus-summary-mode)
3543     (setq gnus-summary-buffer (current-buffer))
3544     (let ((name gnus-newsgroup-name)
3545           (marked gnus-newsgroup-marked)
3546           (spam gnus-newsgroup-spam-marked)
3547           (unread gnus-newsgroup-unreads)
3548           (headers gnus-current-headers)
3549           (data gnus-newsgroup-data)
3550           (summary gnus-summary-buffer)
3551           (article-buffer gnus-article-buffer)
3552           (original gnus-original-article-buffer)
3553           (gac gnus-article-current)
3554           (reffed gnus-reffed-article-number)
3555           (score-file gnus-current-score-file)
3556           (default-charset gnus-newsgroup-charset)
3557           vlist)
3558       (let ((locals gnus-newsgroup-variables))
3559         (while locals
3560           (if (consp (car locals))
3561               (push (eval (caar locals)) vlist)
3562             (push (eval (car locals)) vlist))
3563           (setq locals (cdr locals)))
3564         (setq vlist (nreverse vlist)))
3565       (with-current-buffer gnus-group-buffer
3566         (setq gnus-newsgroup-name name
3567               gnus-newsgroup-marked marked
3568               gnus-newsgroup-spam-marked spam
3569               gnus-newsgroup-unreads unread
3570               gnus-current-headers headers
3571               gnus-newsgroup-data data
3572               gnus-article-current gac
3573               gnus-summary-buffer summary
3574               gnus-article-buffer article-buffer
3575               gnus-original-article-buffer original
3576               gnus-reffed-article-number reffed
3577               gnus-current-score-file score-file
3578               gnus-newsgroup-charset default-charset)
3579         (let ((locals gnus-newsgroup-variables))
3580           (while locals
3581             (if (consp (car locals))
3582                 (set (caar locals) (pop vlist))
3583               (set (car locals) (pop vlist)))
3584             (setq locals (cdr locals))))
3585         ;; The article buffer also has local variables.
3586         (when (gnus-buffer-live-p gnus-article-buffer)
3587           (set-buffer gnus-article-buffer)
3588           (setq gnus-summary-buffer summary))))))
3589
3590 (defun gnus-summary-article-unread-p (article)
3591   "Say whether ARTICLE is unread or not."
3592   (memq article gnus-newsgroup-unreads))
3593
3594 (defun gnus-summary-first-article-p (&optional article)
3595   "Return whether ARTICLE is the first article in the buffer."
3596   (if (not (setq article (or article (gnus-summary-article-number))))
3597       nil
3598     (eq article (caar gnus-newsgroup-data))))
3599
3600 (defun gnus-summary-last-article-p (&optional article)
3601   "Return whether ARTICLE is the last article in the buffer."
3602   (if (not (setq article (or article (gnus-summary-article-number))))
3603       ;; All non-existent numbers are the last article.  :-)
3604       t
3605     (not (cdr (gnus-data-find-list article)))))
3606
3607 (defun gnus-make-thread-indent-array (&optional n)
3608   (when (or n
3609             (progn (setq n 200) nil)
3610             (null gnus-thread-indent-array)
3611             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3612     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3613           gnus-thread-indent-array-level gnus-thread-indent-level)
3614     (while (>= n 0)
3615       (aset gnus-thread-indent-array n
3616             (make-string (* n gnus-thread-indent-level) ? ))
3617       (setq n (1- n)))))
3618
3619 (defun gnus-update-summary-mark-positions ()
3620   "Compute where the summary marks are to go."
3621   (save-excursion
3622     (when (gnus-buffer-exists-p gnus-summary-buffer)
3623       (set-buffer gnus-summary-buffer))
3624     (let ((spec gnus-summary-line-format-spec)
3625           pos)
3626       (save-excursion
3627         (gnus-set-work-buffer)
3628         (let ((gnus-tmp-unread ?Z)
3629               (gnus-replied-mark ?Z)
3630               (gnus-score-below-mark ?Z)
3631               (gnus-score-over-mark ?Z)
3632               (gnus-undownloaded-mark ?Z)
3633               (gnus-summary-line-format-spec spec)
3634               (gnus-newsgroup-downloadable '(0))
3635               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3636               case-fold-search ignores)
3637           ;; Here, all marks are bound to Z.
3638           (gnus-summary-insert-line header
3639                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3640           (goto-char (point-min))
3641           ;; Memorize the positions of the same characters as dummy marks.
3642           (while (re-search-forward "[A-D]" nil t)
3643             (push (point) ignores))
3644           (erase-buffer)
3645           ;; We use A-D as dummy marks in order to know column positions
3646           ;; where marks should be inserted.
3647           (setq gnus-tmp-unread ?A
3648                 gnus-replied-mark ?B
3649                 gnus-score-below-mark ?C
3650                 gnus-score-over-mark ?C
3651                 gnus-undownloaded-mark ?D)
3652           (gnus-summary-insert-line header
3653                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3654           ;; Ignore characters which aren't dummy marks.
3655           (dolist (p ignores)
3656             (delete-region (goto-char (1- p)) p)
3657             (insert ?Z))
3658           (goto-char (point-min))
3659           (setq pos (list (cons 'unread
3660                                 (and (search-forward "A" nil t)
3661                                      (- (point) (point-min) 1)))))
3662           (goto-char (point-min))
3663           (push (cons 'replied (and (search-forward "B" nil t)
3664                                     (- (point) (point-min) 1)))
3665                 pos)
3666           (goto-char (point-min))
3667           (push (cons 'score (and (search-forward "C" nil t)
3668                                   (- (point) (point-min) 1)))
3669                 pos)
3670           (goto-char (point-min))
3671           (push (cons 'download (and (search-forward "D" nil t)
3672                                      (- (point) (point-min) 1)))
3673                 pos)))
3674       (setq gnus-summary-mark-positions pos))))
3675
3676 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3677   "Insert a dummy root in the summary buffer."
3678   (beginning-of-line)
3679   (gnus-add-text-properties
3680    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3681    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3682
3683 (defun gnus-summary-extract-address-component (from)
3684   (or (car (funcall gnus-extract-address-components from))
3685       from))
3686
3687 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3688   (let ((mail-parse-charset gnus-newsgroup-charset)
3689         (ignored-from-addresses (gnus-ignored-from-addresses))
3690         ; Is it really necessary to do this next part for each summary line?
3691         ; Luckily, doesn't seem to slow things down much.
3692         (mail-parse-ignored-charsets
3693          (with-current-buffer gnus-summary-buffer
3694            gnus-newsgroup-ignored-charsets)))
3695     (or
3696      (and ignored-from-addresses
3697           (string-match ignored-from-addresses gnus-tmp-from)
3698           (let ((extra-headers (mail-header-extra header))
3699                 to
3700                 newsgroups)
3701             (cond
3702              ((setq to (cdr (assq 'To extra-headers)))
3703               (concat gnus-summary-to-prefix
3704                       (inline
3705                         (gnus-summary-extract-address-component
3706                          (funcall gnus-decode-encoded-address-function to)))))
3707              ((setq newsgroups
3708                     (or
3709                      (cdr (assq 'Newsgroups extra-headers))
3710                      (and
3711                       (memq 'Newsgroups gnus-extra-headers)
3712                       (eq (car (gnus-find-method-for-group
3713                                 gnus-newsgroup-name)) 'nntp)
3714                       (gnus-group-real-name gnus-newsgroup-name))))
3715               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3716      (gnus-string-mark-left-to-right
3717       (inline
3718        (gnus-summary-extract-address-component gnus-tmp-from))))))
3719
3720 (defun gnus-summary-insert-line (gnus-tmp-header
3721                                  gnus-tmp-level gnus-tmp-current
3722                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3723                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3724                                  &optional gnus-tmp-dummy gnus-tmp-score
3725                                  gnus-tmp-process)
3726   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3727       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3728                                           gnus-tmp-level)))
3729   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3730          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3731          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3732          (gnus-tmp-score-char
3733           (if (or (null gnus-summary-default-score)
3734                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3735                       gnus-summary-zcore-fuzz))
3736               ?                         ;Whitespace
3737             (if (< gnus-tmp-score gnus-summary-default-score)
3738                 gnus-score-below-mark gnus-score-over-mark)))
3739          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3740          (gnus-tmp-replied
3741           (cond (gnus-tmp-process gnus-process-mark)
3742                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3743                  gnus-cached-mark)
3744                 (gnus-tmp-replied gnus-replied-mark)
3745                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3746                  gnus-forwarded-mark)
3747                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3748                  gnus-saved-mark)
3749                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3750                  gnus-unseen-mark)
3751                 (t gnus-no-mark)))
3752          (gnus-tmp-downloaded
3753           (cond (undownloaded
3754                  gnus-undownloaded-mark)
3755                 (gnus-newsgroup-agentized
3756                  gnus-downloaded-mark)
3757                 (t
3758                  gnus-no-mark)))
3759          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3760          (gnus-tmp-name
3761           (cond
3762            ((string-match "<[^>]+> *$" gnus-tmp-from)
3763             (let ((beg (match-beginning 0)))
3764               (or (and (string-match "^\".+\"" gnus-tmp-from)
3765                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3766                   (substring gnus-tmp-from 0 beg))))
3767            ((string-match "(.+)" gnus-tmp-from)
3768             (substring gnus-tmp-from
3769                        (1+ (match-beginning 0)) (1- (match-end 0))))
3770            (t gnus-tmp-from)))
3771          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3772          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3773          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3774          (inhibit-read-only t))
3775     (when (string= gnus-tmp-name "")
3776       (setq gnus-tmp-name gnus-tmp-from))
3777     (unless (numberp gnus-tmp-lines)
3778       (setq gnus-tmp-lines -1))
3779     (if (= gnus-tmp-lines -1)
3780         (setq gnus-tmp-lines "?")
3781       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3782     (condition-case ()
3783         (gnus-put-text-property
3784          (point)
3785          (progn (eval gnus-summary-line-format-spec) (point))
3786          'gnus-number gnus-tmp-number)
3787       (error (gnus-message 5 "Error updating the summary line")))
3788     (when (gnus-visual-p 'summary-highlight 'highlight)
3789       (forward-line -1)
3790       (gnus-summary-highlight-line)
3791       (gnus-run-hooks 'gnus-summary-update-hook)
3792       (forward-line 1))))
3793
3794 (defun gnus-summary-update-line (&optional dont-update)
3795   "Update summary line after change."
3796   (when (and gnus-summary-default-score
3797              (not gnus-summary-inhibit-highlight))
3798     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3799            (article (gnus-summary-article-number))
3800            (score (gnus-summary-article-score article)))
3801       (unless dont-update
3802         (if (and gnus-summary-mark-below
3803                  (< (gnus-summary-article-score)
3804                     gnus-summary-mark-below))
3805             ;; This article has a low score, so we mark it as read.
3806             (when (memq article gnus-newsgroup-unreads)
3807               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3808           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3809             ;; This article was previously marked as read on account
3810             ;; of a low score, but now it has risen, so we mark it as
3811             ;; unread.
3812             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3813         (gnus-summary-update-mark
3814          (if (or (null gnus-summary-default-score)
3815                  (<= (abs (- score gnus-summary-default-score))
3816                      gnus-summary-zcore-fuzz))
3817              ?                          ;Whitespace
3818            (if (< score gnus-summary-default-score)
3819                gnus-score-below-mark gnus-score-over-mark))
3820          'score))
3821       ;; Do visual highlighting.
3822       (when (gnus-visual-p 'summary-highlight 'highlight)
3823         (gnus-summary-highlight-line)
3824         (gnus-run-hooks 'gnus-summary-update-hook)))))
3825
3826 (defvar gnus-tmp-new-adopts nil)
3827
3828 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3829   "Return the number of articles in THREAD.
3830 This may be 0 in some cases -- if none of the articles in
3831 the thread are to be displayed."
3832   (let* ((number
3833          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3834           (cond
3835            ((not (listp thread))
3836             1)
3837            ((and (consp thread) (cdr thread))
3838             (apply
3839              '+ 1 (mapcar
3840                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3841            ((null thread)
3842             1)
3843            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3844             1)
3845            (t 0))))
3846     (when (and level (zerop level) gnus-tmp-new-adopts)
3847       (incf number
3848             (apply '+ (mapcar
3849                        'gnus-summary-number-of-articles-in-thread
3850                        gnus-tmp-new-adopts))))
3851     (if char
3852         (if (> number 1) gnus-not-empty-thread-mark
3853           gnus-empty-thread-mark)
3854       number)))
3855
3856 (defsubst gnus-summary-line-message-size (head)
3857   "Return pretty-printed version of message size.
3858 This function is intended to be used in
3859 `gnus-summary-line-format-alist'."
3860   (let ((c (or (mail-header-chars head) -1)))
3861     (cond ((< c 0) "n/a")               ; chars not available
3862           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3863           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3864           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3865           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3866
3867 (defcustom gnus-user-date-format-alist
3868   '(((gnus-seconds-today) . "Today, %H:%M")
3869     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3870     (604800 . "%A %H:%M")               ; That's one week
3871     ((gnus-seconds-month) . "%A %d")
3872     ((gnus-seconds-year) . "%B %d")
3873     (t . "%b %d %Y"))                   ; This one is used when no other
3874                                         ; does match
3875   "Specifies date format depending on age of article.
3876 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3877 seconds) or a Lisp expression evaluating to a number.  When the age of
3878 the article is less than this number, then use `format-time-string'
3879 with the corresponding FORMAT for displaying the date of the article.
3880 If AGE is not a number or a Lisp expression evaluating to a
3881 non-number, then the corresponding FORMAT is used as a default value.
3882
3883 Note that the list is processed from the beginning, so it should be
3884 sorted by ascending AGE.  Also note that items following the first
3885 non-number AGE will be ignored.
3886
3887 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3888 and `gnus-seconds-year' in the AGE spec.  They return the number of
3889 seconds passed since the start of today, of this month, of this year,
3890 respectively."
3891   :version "24.1"
3892   :group 'gnus-summary-format
3893   :type '(alist :key-type sexp :value-type string))
3894
3895 (defun gnus-user-date (messy-date)
3896   "Format the messy-date according to `gnus-user-date-format-alist'.
3897 Returns \"  ?  \" if there's bad input or if another error occurs.
3898 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3899   (condition-case ()
3900       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3901              (now (gnus-float-time))
3902              ;;If we don't find something suitable we'll use this one
3903              (my-format "%b %d '%y"))
3904         (let* ((difference (- now messy-date))
3905                (templist gnus-user-date-format-alist)
3906                (top (eval (caar templist))))
3907           (while (if (numberp top) (< top difference) (not top))
3908             (progn
3909               (setq templist (cdr templist))
3910               (setq top (eval (caar templist)))))
3911           (if (stringp (cdr (car templist)))
3912               (setq my-format (cdr (car templist)))))
3913         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3914     (error "  ?   ")))
3915
3916 (defun gnus-summary-set-local-parameters (group)
3917   "Go through the local params of GROUP and set all variable specs in that list."
3918   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3919                                         ; really variables.
3920     (dolist (elem (gnus-group-find-parameter group))
3921       (and (consp elem)                 ; Has to be a cons.
3922            (consp (cdr elem))           ; The cdr has to be a list.
3923            (symbolp (car elem))         ; Has to be a symbol in there.
3924            (not (memq (car elem) vars))
3925            (ignore-errors
3926              (push (car elem) vars)
3927              ;; Variables like `gnus-show-threads' that are globally
3928              ;; bound, if used as group parameters, need to get to be
3929              ;; buffer-local, whereas just parameters like `gcc-self',
3930              ;; `timestamp', etc. should not be bound as variables.
3931              (if (boundp (car elem))
3932                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3933                (eval (nth 1 elem))))))))
3934
3935 (defun gnus-summary-read-group (group &optional show-all no-article
3936                                       kill-buffer no-display backward
3937                                       select-articles)
3938   "Start reading news in newsgroup GROUP.
3939 If SHOW-ALL is non-nil, already read articles are also listed.
3940 If NO-ARTICLE is non-nil, no article is selected initially.
3941 If NO-DISPLAY, don't generate the summary buffer contents.
3942 If KILL-BUFFER, it should be a buffer that's killed once the new
3943 summary buffer has been generated.
3944 If BACKWARD, move point to the previous group in the group buffer
3945 If SELECT-ARTICLES, only select those articles from GROUP."
3946   (let (result)
3947     (while (and group
3948                 (null (setq result
3949                             (let ((gnus-auto-select-next nil))
3950                               (or (gnus-summary-read-group-1
3951                                    group show-all no-article
3952                                    kill-buffer no-display
3953                                    select-articles)
3954                                   (setq show-all nil
3955                                         select-articles nil)))))
3956                 (eq gnus-auto-select-next 'quietly))
3957       (set-buffer gnus-group-buffer)
3958       ;; The entry function called above goes to the next
3959       ;; group automatically, so we go two groups back
3960       ;; if we are searching for the previous group.
3961       (when backward
3962         (gnus-group-prev-unread-group 2))
3963       (if (not (equal group (gnus-group-group-name)))
3964           (setq group (gnus-group-group-name))
3965         (setq group nil)))
3966     result))
3967
3968 (defun gnus-summary-read-group-1 (group show-all no-article
3969                                         kill-buffer no-display
3970                                         &optional select-articles)
3971   ;; Killed foreign groups can't be entered.
3972   ;;  (when (and (not (gnus-group-native-p group))
3973   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3974   ;;    (error "Dead non-native groups can't be entered"))
3975   (gnus-message 7 "Retrieving newsgroup: %s..."
3976                 (gnus-group-decoded-name group))
3977   (let* ((new-group (gnus-summary-setup-buffer group))
3978          (quit-config (gnus-group-quit-config group))
3979          (did-select (and new-group (gnus-select-newsgroup
3980                                      group show-all select-articles))))
3981     (cond
3982      ;; This summary buffer exists already, so we just select it.
3983      ((not new-group)
3984       (gnus-set-global-variables)
3985       (when kill-buffer
3986         (gnus-kill-or-deaden-summary kill-buffer))
3987       (gnus-configure-windows 'summary 'force)
3988       (gnus-set-mode-line 'summary)
3989       (gnus-summary-position-point)
3990       (message "")
3991       t)
3992      ;; We couldn't select this group.
3993      ((null did-select)
3994       (when (and (eq major-mode 'gnus-summary-mode)
3995                  (not (equal (current-buffer) kill-buffer)))
3996         (kill-buffer (current-buffer))
3997         (if (not quit-config)
3998             (progn
3999               ;; Update the info -- marks might need to be removed,
4000               ;; for instance.
4001               (gnus-summary-update-info)
4002               (set-buffer gnus-group-buffer)
4003               (gnus-group-jump-to-group group)
4004               (gnus-group-next-unread-group 1))
4005           (gnus-handle-ephemeral-exit quit-config)))
4006       (if (null (gnus-list-of-unread-articles group))
4007           (gnus-message 3 "Group %s contains no messages" group)
4008         (gnus-message 3 "Can't select group"))
4009       nil)
4010      ;; The user did a `C-g' while prompting for number of articles,
4011      ;; so we exit this group.
4012      ((eq did-select 'quit)
4013       (and (eq major-mode 'gnus-summary-mode)
4014            (not (equal (current-buffer) kill-buffer))
4015            (kill-buffer (current-buffer)))
4016       (when kill-buffer
4017         (gnus-kill-or-deaden-summary kill-buffer))
4018       (if (not quit-config)
4019           (progn
4020             (set-buffer gnus-group-buffer)
4021             (gnus-group-jump-to-group group)
4022             (gnus-configure-windows 'group 'force))
4023         (gnus-handle-ephemeral-exit quit-config))
4024       ;; Finally signal the quit.
4025       (signal 'quit nil))
4026      ;; The group was successfully selected.
4027      (t
4028       (gnus-set-global-variables)
4029       ;; Save the active value in effect when the group was entered.
4030       (setq gnus-newsgroup-active
4031             (gnus-copy-sequence
4032              (gnus-active gnus-newsgroup-name)))
4033       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4034       ;; You can change the summary buffer in some way with this hook.
4035       (gnus-run-hooks 'gnus-select-group-hook)
4036       (when (memq 'summary (gnus-update-format-specifications
4037                             nil 'summary 'summary-mode 'summary-dummy))
4038         ;; The format specification for the summary line was updated,
4039         ;; so we need to update the mark positions as well.
4040         (gnus-update-summary-mark-positions))
4041       ;; Do score processing.
4042       (when gnus-use-scoring
4043         (gnus-possibly-score-headers))
4044       ;; Check whether to fill in the gaps in the threads.
4045       (when gnus-build-sparse-threads
4046         (gnus-build-sparse-threads))
4047       ;; Find the initial limit.
4048       (if show-all
4049           (let ((gnus-newsgroup-dormant nil))
4050             (gnus-summary-initial-limit show-all))
4051         (gnus-summary-initial-limit show-all))
4052       ;; Generate the summary buffer.
4053       (unless no-display
4054         (gnus-summary-prepare))
4055       (when gnus-use-trees
4056         (gnus-tree-open group)
4057         (setq gnus-summary-highlight-line-function
4058               'gnus-tree-highlight-article))
4059       ;; If the summary buffer is empty, but there are some low-scored
4060       ;; articles or some excluded dormants, we include these in the
4061       ;; buffer.
4062       (when (and (zerop (buffer-size))
4063                  (not no-display))
4064         (cond (gnus-newsgroup-dormant
4065                (gnus-summary-limit-include-dormant))
4066               ((and gnus-newsgroup-scored show-all)
4067                (gnus-summary-limit-include-expunged t))))
4068       ;; Function `gnus-apply-kill-file' must be called in this hook.
4069       (gnus-run-hooks 'gnus-apply-kill-hook)
4070       (if (and (zerop (buffer-size))
4071                (not no-display))
4072           (progn
4073             ;; This newsgroup is empty.
4074             (gnus-summary-catchup-and-exit nil t)
4075             (gnus-message 6 "No unread news")
4076             (when kill-buffer
4077               (gnus-kill-or-deaden-summary kill-buffer))
4078             ;; Return nil from this function.
4079             nil)
4080         ;; Hide conversation thread subtrees.  We cannot do this in
4081         ;; gnus-summary-prepare-hook since kill processing may not
4082         ;; work with hidden articles.
4083         (gnus-summary-maybe-hide-threads)
4084         (gnus-configure-windows 'summary)
4085         (when kill-buffer
4086           (gnus-kill-or-deaden-summary kill-buffer))
4087         (gnus-summary-auto-select-subject)
4088         ;; Show first unread article if requested.
4089         (if (and (not no-article)
4090                  (not no-display)
4091                  gnus-newsgroup-unreads
4092                  gnus-auto-select-first)
4093             (progn
4094               (let ((art (gnus-summary-article-number)))
4095                 (unless (and (not gnus-plugged)
4096                              (or (memq art gnus-newsgroup-undownloaded)
4097                                  (memq art gnus-newsgroup-downloadable)))
4098                   (gnus-summary-goto-article art))))
4099           ;; Don't select any articles.
4100           (gnus-summary-position-point)
4101           (gnus-configure-windows 'summary 'force)
4102           (gnus-set-mode-line 'summary))
4103         (when (and gnus-auto-center-group
4104                    (get-buffer-window gnus-group-buffer t))
4105           ;; Gotta use windows, because recenter does weird stuff if
4106           ;; the current buffer ain't the displayed window.
4107           (let ((owin (selected-window)))
4108             (select-window (get-buffer-window gnus-group-buffer t))
4109             (when (gnus-group-goto-group group)
4110               (recenter))
4111             (select-window owin)))
4112         ;; Mark this buffer as "prepared".
4113         (setq gnus-newsgroup-prepared t)
4114         (gnus-run-hooks 'gnus-summary-prepared-hook)
4115         (unless (gnus-ephemeral-group-p group)
4116           (gnus-group-update-group group nil t))
4117         t)))))
4118
4119 (defun gnus-summary-auto-select-subject ()
4120   "Select the subject line on initial group entry."
4121   (goto-char (point-min))
4122   (cond
4123    ((eq gnus-auto-select-subject 'best)
4124     (gnus-summary-best-unread-subject))
4125    ((eq gnus-auto-select-subject 'unread)
4126     (gnus-summary-first-unread-subject))
4127    ((eq gnus-auto-select-subject 'unseen)
4128     (gnus-summary-first-unseen-subject))
4129    ((eq gnus-auto-select-subject 'unseen-or-unread)
4130     (gnus-summary-first-unseen-or-unread-subject))
4131    ((eq gnus-auto-select-subject 'first)
4132     ;; Do nothing.
4133     )
4134    ((functionp gnus-auto-select-subject)
4135     (funcall gnus-auto-select-subject))))
4136
4137 (defun gnus-summary-prepare ()
4138   "Generate the summary buffer."
4139   (interactive)
4140   (let ((inhibit-read-only t))
4141     (erase-buffer)
4142     (setq gnus-newsgroup-data nil
4143           gnus-newsgroup-data-reverse nil)
4144     (gnus-run-hooks 'gnus-summary-generate-hook)
4145     ;; Generate the buffer, either with threads or without.
4146     (when gnus-newsgroup-headers
4147       (gnus-summary-prepare-threads
4148        (if gnus-show-threads
4149            (gnus-sort-gathered-threads
4150             (funcall gnus-summary-thread-gathering-function
4151                      (gnus-sort-threads
4152                       (gnus-cut-threads (gnus-make-threads)))))
4153          ;; Unthreaded display.
4154          (gnus-sort-articles gnus-newsgroup-headers))))
4155     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4156     ;; Call hooks for modifying summary buffer.
4157     (goto-char (point-min))
4158     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4159
4160 (defsubst gnus-general-simplify-subject (subject)
4161   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4162   (setq subject
4163         (cond
4164          ;; Truncate the subject.
4165          (gnus-simplify-subject-functions
4166           (gnus-map-function gnus-simplify-subject-functions subject))
4167          ((numberp gnus-summary-gather-subject-limit)
4168           (setq subject (gnus-simplify-subject-re subject))
4169           (if (> (length subject) gnus-summary-gather-subject-limit)
4170               (substring subject 0 gnus-summary-gather-subject-limit)
4171             subject))
4172          ;; Fuzzily simplify it.
4173          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4174           (gnus-simplify-subject-fuzzy subject))
4175          ;; Just remove the leading "Re:".
4176          (t
4177           (gnus-simplify-subject-re subject))))
4178
4179   (if (and gnus-summary-gather-exclude-subject
4180            (string-match gnus-summary-gather-exclude-subject subject))
4181       nil                         ; This article shouldn't be gathered
4182     subject))
4183
4184 (defun gnus-summary-simplify-subject-query ()
4185   "Query where the respool algorithm would put this article."
4186   (interactive)
4187   (gnus-summary-select-article)
4188   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4189
4190 (defun gnus-gather-threads-by-subject (threads)
4191   "Gather threads by looking at Subject headers."
4192   (if (not gnus-summary-make-false-root)
4193       threads
4194     (let ((hashtb (gnus-make-hashtable 1024))
4195           (prev threads)
4196           (result threads)
4197           subject hthread whole-subject)
4198       (while threads
4199         (setq subject (gnus-general-simplify-subject
4200                        (setq whole-subject (mail-header-subject
4201                                             (caar threads)))))
4202         (when subject
4203           (if (setq hthread (gnus-gethash subject hashtb))
4204               (progn
4205                 ;; We enter a dummy root into the thread, if we
4206                 ;; haven't done that already.
4207                 (unless (stringp (caar hthread))
4208                   (setcar hthread (list whole-subject (car hthread))))
4209                 ;; We add this new gathered thread to this gathered
4210                 ;; thread.
4211                 (setcdr (car hthread)
4212                         (nconc (cdar hthread) (list (car threads))))
4213                 ;; Remove it from the list of threads.
4214                 (setcdr prev (cdr threads))
4215                 (setq threads prev))
4216             ;; Enter this thread into the hash table.
4217             (gnus-sethash subject
4218                           (if gnus-summary-make-false-root-always
4219                               (progn
4220                                 ;; If you want a dummy root above all
4221                                 ;; threads...
4222                                 (setcar threads (list whole-subject
4223                                                       (car threads)))
4224                                 threads)
4225                             threads)
4226                           hashtb)))
4227         (setq prev threads)
4228         (setq threads (cdr threads)))
4229       result)))
4230
4231 (defun gnus-gather-threads-by-references (threads)
4232   "Gather threads by looking at References headers."
4233   (let ((idhashtb (gnus-make-hashtable 1024))
4234         (thhashtb (gnus-make-hashtable 1024))
4235         (prev threads)
4236         (result threads)
4237         ids references id gthread gid entered ref)
4238     (while threads
4239       (when (setq references (mail-header-references (caar threads)))
4240         (setq id (mail-header-id (caar threads))
4241               ids (inline (gnus-split-references references))
4242               entered nil)
4243         (while (setq ref (pop ids))
4244           (setq ids (delete ref ids))
4245           (if (not (setq gid (gnus-gethash ref idhashtb)))
4246               (progn
4247                 (gnus-sethash ref id idhashtb)
4248                 (gnus-sethash id threads thhashtb))
4249             (setq gthread (gnus-gethash gid thhashtb))
4250             (unless entered
4251               ;; We enter a dummy root into the thread, if we
4252               ;; haven't done that already.
4253               (unless (stringp (caar gthread))
4254                 (setcar gthread (list (mail-header-subject (caar gthread))
4255                                       (car gthread))))
4256               ;; We add this new gathered thread to this gathered
4257               ;; thread.
4258               (setcdr (car gthread)
4259                       (nconc (cdar gthread) (list (car threads)))))
4260             ;; Add it into the thread hash table.
4261             (gnus-sethash id gthread thhashtb)
4262             (setq entered t)
4263             ;; Remove it from the list of threads.
4264             (setcdr prev (cdr threads))
4265             (setq threads prev))))
4266       (setq prev threads)
4267       (setq threads (cdr threads)))
4268     result))
4269
4270 (defun gnus-sort-gathered-threads (threads)
4271   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4272   (let ((result threads))
4273     (while threads
4274       (when (stringp (caar threads))
4275         (setcdr (car threads)
4276                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4277       (setq threads (cdr threads)))
4278     result))
4279
4280 (defun gnus-thread-loop-p (root thread)
4281   "Say whether ROOT is in THREAD."
4282   (let ((stack (list thread))
4283         (infloop 0)
4284         th)
4285     (while (setq thread (pop stack))
4286       (setq th (cdr thread))
4287       (while (and th
4288                   (not (eq (caar th) root)))
4289         (pop th))
4290       (if th
4291           ;; We have found a loop.
4292           (let (ref-dep)
4293             (setcdr thread (delq (car th) (cdr thread)))
4294             (if (boundp (setq ref-dep (intern "none"
4295                                               gnus-newsgroup-dependencies)))
4296                 (setcdr (symbol-value ref-dep)
4297                         (nconc (cdr (symbol-value ref-dep))
4298                                (list (car th))))
4299               (set ref-dep (list nil (car th))))
4300             (setq infloop 1
4301                   stack nil))
4302         ;; Push all the subthreads onto the stack.
4303         (push (cdr thread) stack)))
4304     infloop))
4305
4306 (defun gnus-make-threads ()
4307   "Go through the dependency hashtb and find the roots.  Return all threads."
4308   (let (threads)
4309     (while (catch 'infloop
4310              (mapatoms
4311               (lambda (refs)
4312                 ;; Deal with self-referencing References loops.
4313                 (when (and (car (symbol-value refs))
4314                            (not (zerop
4315                                  (apply
4316                                   '+
4317                                   (mapcar
4318                                    (lambda (thread)
4319                                      (gnus-thread-loop-p
4320                                       (car (symbol-value refs)) thread))
4321                                    (cdr (symbol-value refs)))))))
4322                   (setq threads nil)
4323                   (throw 'infloop t))
4324                 (unless (car (symbol-value refs))
4325                   ;; These threads do not refer back to any other
4326                   ;; articles, so they're roots.
4327                   (setq threads (append (cdr (symbol-value refs)) threads))))
4328               gnus-newsgroup-dependencies)))
4329     threads))
4330
4331 ;; Build the thread tree.
4332 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4333   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4334
4335 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4336 if it was already present.
4337
4338 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4339 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4340 Message-IDs will be renamed to a unique Message-ID before being
4341 entered.
4342
4343 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4344   (let* ((id (mail-header-id header))
4345          (id-dep (and id (intern id dependencies)))
4346          parent-id ref ref-dep ref-header replaced)
4347     ;; Enter this `header' in the `dependencies' table.
4348     (cond
4349      ((not id-dep)
4350       (setq header nil))
4351      ;; The first two cases do the normal part: enter a new `header'
4352      ;; in the `dependencies' table.
4353      ((not (boundp id-dep))
4354       (set id-dep (list header)))
4355      ((null (car (symbol-value id-dep)))
4356       (setcar (symbol-value id-dep) header))
4357
4358      ;; From here the `header' was already present in the
4359      ;; `dependencies' table.
4360      (force-new
4361       ;; Overrides an existing entry;
4362       ;; just set the header part of the entry.
4363       (setcar (symbol-value id-dep) header)
4364       (setq replaced t))
4365
4366      ;; Renames the existing `header' to a unique Message-ID.
4367      ((not gnus-summary-ignore-duplicates)
4368       ;; An article with this Message-ID has already been seen.
4369       ;; We rename the Message-ID.
4370       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4371            (list header))
4372       (mail-header-set-id header id))
4373
4374      ;; The last case ignores an existing entry, except it adds any
4375      ;; additional Xrefs (in case the two articles came from different
4376      ;; servers.
4377      ;; Also sets `header' to `nil' meaning that the `dependencies'
4378      ;; table was *not* modified.
4379      (t
4380       (mail-header-set-xref
4381        (car (symbol-value id-dep))
4382        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4383                    "")
4384                (or (mail-header-xref header) "")))
4385       (setq header nil)))
4386
4387     (when (and header (not replaced))
4388       ;; First check that we are not creating a References loop.
4389       (setq parent-id (gnus-parent-id (mail-header-references header)))
4390       (setq ref parent-id)
4391       (while (and ref
4392                   (setq ref-dep (intern-soft ref dependencies))
4393                   (boundp ref-dep)
4394                   (setq ref-header (car (symbol-value ref-dep))))
4395         (if (string= id ref)
4396             ;; Yuk!  This is a reference loop.  Make the article be a
4397             ;; root article.
4398             (progn
4399               (mail-header-set-references (car (symbol-value id-dep)) "none")
4400               (setq ref nil)
4401               (setq parent-id nil))
4402           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4403       (setq ref-dep (intern (or parent-id "none") dependencies))
4404       (if (boundp ref-dep)
4405           (setcdr (symbol-value ref-dep)
4406                   (nconc (cdr (symbol-value ref-dep))
4407                          (list (symbol-value id-dep))))
4408         (set ref-dep (list nil (symbol-value id-dep)))))
4409     header))
4410
4411 (defun gnus-extract-message-id-from-in-reply-to (string)
4412   (if (string-match "<[^>]+>" string)
4413       (substring string (match-beginning 0) (match-end 0))
4414     nil))
4415
4416 (defun gnus-build-sparse-threads ()
4417   (let ((headers gnus-newsgroup-headers)
4418         (mail-parse-charset gnus-newsgroup-charset)
4419         (gnus-summary-ignore-duplicates t)
4420         header references generation relations
4421         subject child end new-child date)
4422     ;; First we create an alist of generations/relations, where
4423     ;; generations is how much we trust the relation, and the relation
4424     ;; is parent/child.
4425     (gnus-message 7 "Making sparse threads...")
4426     (save-excursion
4427       (nnheader-set-temp-buffer " *gnus sparse threads*")
4428       (while (setq header (pop headers))
4429         (when (and (setq references (mail-header-references header))
4430                    (not (string= references "")))
4431           (insert references)
4432           (setq child (mail-header-id header)
4433                 subject (mail-header-subject header)
4434                 date (mail-header-date header)
4435                 generation 0)
4436           (while (search-backward ">" nil t)
4437             (setq end (1+ (point)))
4438             (when (search-backward "<" nil t)
4439               (setq new-child (buffer-substring (point) end))
4440               (push (list (incf generation)
4441                           child (setq child new-child)
4442                           subject date)
4443                     relations)))
4444           (when child
4445             (push (list (1+ generation) child nil subject) relations))
4446           (erase-buffer)))
4447       (kill-buffer (current-buffer)))
4448     ;; Sort over trustworthiness.
4449     (dolist (relation (sort relations 'car-less-than-car))
4450       (when (gnus-dependencies-add-header
4451              (make-full-mail-header
4452               gnus-reffed-article-number
4453               (nth 3 relation) "" (or (nth 4 relation) "")
4454               (nth 1 relation)
4455               (or (nth 2 relation) "") 0 0 "")
4456              gnus-newsgroup-dependencies nil)
4457         (push gnus-reffed-article-number gnus-newsgroup-limit)
4458         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4459         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4460               gnus-newsgroup-reads)
4461         (decf gnus-reffed-article-number)))
4462     (gnus-message 7 "Making sparse threads...done")))
4463
4464 (defun gnus-build-old-threads ()
4465   ;; Look at all the articles that refer back to old articles, and
4466   ;; fetch the headers for the articles that aren't there.  This will
4467   ;; build complete threads - if the roots haven't been expired by the
4468   ;; server, that is.
4469   (let ((mail-parse-charset gnus-newsgroup-charset)
4470         id heads)
4471     (mapatoms
4472      (lambda (refs)
4473        (when (not (car (symbol-value refs)))
4474          (setq heads (cdr (symbol-value refs)))
4475          (while heads
4476            (if (memq (mail-header-number (caar heads))
4477                      gnus-newsgroup-dormant)
4478                (setq heads (cdr heads))
4479              (setq id (symbol-name refs))
4480              (while (and (setq id (gnus-build-get-header id))
4481                          (not (car (gnus-id-to-thread id)))))
4482              (setq heads nil)))))
4483      gnus-newsgroup-dependencies)))
4484
4485 (defsubst gnus-remove-odd-characters (string)
4486   "Translate STRING into something that doesn't contain weird characters."
4487   (mm-subst-char-in-string
4488    ?\r ?\-
4489    (mm-subst-char-in-string ?\n ?\- string t) t))
4490
4491 ;; This function has to be called with point after the article number
4492 ;; on the beginning of the line.
4493 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4494   (let ((eol (point-at-eol))
4495         (buffer (current-buffer))
4496         header references in-reply-to)
4497
4498     ;; overview: [num subject from date id refs chars lines misc]
4499     (unwind-protect
4500         (let (x)
4501           (narrow-to-region (point) eol)
4502           (unless (eobp)
4503             (forward-char))
4504
4505           (setq header
4506                 (make-full-mail-header
4507                  number                 ; number
4508                  (condition-case ()     ; subject
4509                      (gnus-remove-odd-characters
4510                       (funcall gnus-decode-encoded-word-function
4511                                (setq x (nnheader-nov-field))))
4512                    (error x))
4513                  (condition-case ()     ; from
4514                      (gnus-remove-odd-characters
4515                       (funcall gnus-decode-encoded-address-function
4516                                (setq x (nnheader-nov-field))))
4517                    (error x))
4518                  (nnheader-nov-field)   ; date
4519                  (nnheader-nov-read-message-id number)  ; id
4520                  (setq references (nnheader-nov-field)) ; refs
4521                  (nnheader-nov-read-integer) ; chars
4522                  (nnheader-nov-read-integer) ; lines
4523                  (unless (eobp)
4524                    (if (looking-at "Xref: ")
4525                        (goto-char (match-end 0)))
4526                    (nnheader-nov-field)) ; Xref
4527                  (nnheader-nov-parse-extra)))) ; extra
4528
4529       (widen))
4530
4531     (when (and (string= references "")
4532                (setq in-reply-to (mail-header-extra header))
4533                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4534       (mail-header-set-references
4535        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4536
4537     (when gnus-alter-header-function
4538       (funcall gnus-alter-header-function header))
4539     (gnus-dependencies-add-header header dependencies force-new)))
4540
4541 (defun gnus-build-get-header (id)
4542   "Look through the buffer of NOV lines and find the header to ID.
4543 Enter this line into the dependencies hash table, and return
4544 the id of the parent article (if any)."
4545   (let ((deps gnus-newsgroup-dependencies)
4546         found header)
4547     (prog1
4548         (with-current-buffer nntp-server-buffer
4549           (let ((case-fold-search nil))
4550             (goto-char (point-min))
4551             (while (and (not found)
4552                         (search-forward id nil t))
4553               (beginning-of-line)
4554               (setq found (looking-at
4555                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4556                                    (regexp-quote id))))
4557               (or found (beginning-of-line 2)))
4558             (when found
4559               (beginning-of-line)
4560               (and
4561                (setq header (gnus-nov-parse-line
4562                              (read (current-buffer)) deps))
4563                (gnus-parent-id (mail-header-references header))))))
4564       (when header
4565         (let ((number (mail-header-number header)))
4566           (push number gnus-newsgroup-limit)
4567           (push header gnus-newsgroup-headers)
4568           (if (memq number gnus-newsgroup-unselected)
4569               (progn
4570                 (setq gnus-newsgroup-unreads
4571                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4572                                                number))
4573                 (setq gnus-newsgroup-unselected
4574                       (delq number gnus-newsgroup-unselected)))
4575             (push number gnus-newsgroup-ancient)))))))
4576
4577 (defun gnus-build-all-threads ()
4578   "Read all the headers."
4579   (let ((gnus-summary-ignore-duplicates t)
4580         (mail-parse-charset gnus-newsgroup-charset)
4581         (dependencies gnus-newsgroup-dependencies)
4582         header article)
4583     (with-current-buffer nntp-server-buffer
4584       (let ((case-fold-search nil))
4585         (goto-char (point-min))
4586         (while (not (eobp))
4587           (ignore-errors
4588             (setq article (read (current-buffer))
4589                   header (gnus-nov-parse-line article dependencies t)))
4590           (when header
4591             (with-current-buffer gnus-summary-buffer
4592               (push header gnus-newsgroup-headers)
4593               (if (memq (setq article (mail-header-number header))
4594                         gnus-newsgroup-unselected)
4595                   (progn
4596                     (setq gnus-newsgroup-unreads
4597                           (gnus-add-to-sorted-list
4598                            gnus-newsgroup-unreads article))
4599                     (setq gnus-newsgroup-unselected
4600                           (delq article gnus-newsgroup-unselected)))
4601                 (push article gnus-newsgroup-ancient)))
4602             (forward-line 1)))))))
4603
4604 (defun gnus-summary-update-article-line (article header)
4605   "Update the line for ARTICLE using HEADER."
4606   (let* ((id (mail-header-id header))
4607          (thread (gnus-id-to-thread id)))
4608     (unless thread
4609       (error "Article in no thread"))
4610     ;; Update the thread.
4611     (setcar thread header)
4612     (gnus-summary-goto-subject article)
4613     (let* ((datal (gnus-data-find-list article))
4614            (data (car datal))
4615            (inhibit-read-only t)
4616            (level (gnus-summary-thread-level)))
4617       (gnus-delete-line)
4618       (let ((inserted (- (point)
4619                          (progn
4620                            (gnus-summary-insert-line
4621                             header level nil
4622                             (memq article gnus-newsgroup-undownloaded)
4623                             (gnus-article-mark article)
4624                             (memq article gnus-newsgroup-replied)
4625                             (memq article gnus-newsgroup-expirable)
4626                             ;; Only insert the Subject string when it's different
4627                             ;; from the previous Subject string.
4628                             (if (and
4629                                  gnus-show-threads
4630                                  (gnus-subject-equal
4631                                   (condition-case ()
4632                                       (mail-header-subject
4633                                        (gnus-data-header
4634                                         (cadr
4635                                          (gnus-data-find-list
4636                                           article
4637                                           (gnus-data-list t)))))
4638                                     ;; Error on the side of excessive subjects.
4639                                     (error ""))
4640                                   (mail-header-subject header)))
4641                                 ""
4642                               (mail-header-subject header))
4643                             nil (cdr (assq article gnus-newsgroup-scored))
4644                             (memq article gnus-newsgroup-processable))
4645                            (point)))))
4646         (when (cdr datal)
4647           (gnus-data-update-list
4648            (cdr datal)
4649            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4650
4651 (defun gnus-summary-update-article (article &optional iheader)
4652   "Update ARTICLE in the summary buffer."
4653   (set-buffer gnus-summary-buffer)
4654   (let* ((header (gnus-summary-article-header article))
4655          (id (mail-header-id header))
4656          (data (gnus-data-find article))
4657          (thread (gnus-id-to-thread id))
4658          (references (mail-header-references header))
4659          (parent
4660           (gnus-id-to-thread
4661            (or (gnus-parent-id
4662                 (when (and references
4663                            (not (equal "" references)))
4664                   references))
4665                "none")))
4666          (inhibit-read-only t)
4667          (old (car thread)))
4668     (when thread
4669       (unless iheader
4670         (setcar thread nil)
4671         (when parent
4672           (delq thread parent)))
4673       (if (gnus-summary-insert-subject id header)
4674           ;; Set the (possibly) new article number in the data structure.
4675           (gnus-data-set-number data (gnus-id-to-article id))
4676         (setcar thread old)
4677         nil))))
4678
4679 (defun gnus-rebuild-thread (id &optional line)
4680   "Rebuild the thread containing ID.
4681 If LINE, insert the rebuilt thread starting on line LINE."
4682   (let ((inhibit-read-only t)
4683         old-pos current thread data)
4684     (if (not gnus-show-threads)
4685         (setq thread (list (car (gnus-id-to-thread id))))
4686       ;; Get the thread this article is part of.
4687       (setq thread (gnus-remove-thread id)))
4688     (setq old-pos (point-at-bol))
4689     (setq current (save-excursion
4690                     (and (re-search-backward "[\r\n]" nil t)
4691                          (gnus-summary-article-number))))
4692     ;; If this is a gathered thread, we have to go some re-gathering.
4693     (when (stringp (car thread))
4694       (let ((subject (car thread))
4695             roots thr)
4696         (setq thread (cdr thread))
4697         (while thread
4698           (unless (memq (setq thr (gnus-id-to-thread
4699                                    (gnus-root-id
4700                                     (mail-header-id (caar thread)))))
4701                         roots)
4702             (push thr roots))
4703           (setq thread (cdr thread)))
4704         ;; We now have all (unique) roots.
4705         (if (= (length roots) 1)
4706             ;; All the loose roots are now one solid root.
4707             (setq thread (car roots))
4708           (setq thread (cons subject (gnus-sort-threads roots))))))
4709     (let (threads)
4710       ;; We then insert this thread into the summary buffer.
4711       (when line
4712         (goto-char (point-min))
4713         (forward-line (1- line)))
4714       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4715         (if gnus-show-threads
4716             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4717           (gnus-summary-prepare-unthreaded thread))
4718         (setq data (nreverse gnus-newsgroup-data))
4719         (setq threads gnus-newsgroup-threads))
4720       ;; We splice the new data into the data structure.
4721       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4722       ;;!!! then we want to insert at the beginning of the buffer.
4723       ;;!!! That happens to be true with Gnus now, but that may
4724       ;;!!! change in the future.  Perhaps.
4725       (gnus-data-enter-list
4726        (if line nil current) data (- (point) old-pos))
4727       (setq gnus-newsgroup-threads
4728             (nconc threads gnus-newsgroup-threads))
4729       (gnus-data-compute-positions))))
4730
4731 (defun gnus-number-to-header (number)
4732   "Return the header for article NUMBER."
4733   (let ((headers gnus-newsgroup-headers))
4734     (while (and headers
4735                 (not (= number (mail-header-number (car headers)))))
4736       (pop headers))
4737     (when headers
4738       (car headers))))
4739
4740 (defun gnus-parent-headers (in-headers &optional generation)
4741   "Return the headers of the GENERATIONth parent of HEADERS."
4742   (unless generation
4743     (setq generation 1))
4744   (let ((parent t)
4745         (headers in-headers)
4746         references)
4747     (while (and parent
4748                 (not (zerop generation))
4749                 (setq references (mail-header-references headers)))
4750       (setq headers (if (and references
4751                              (setq parent (gnus-parent-id references)))
4752                         (car (gnus-id-to-thread parent))
4753                       nil))
4754       (decf generation))
4755     (and (not (eq headers in-headers))
4756          headers)))
4757
4758 (defun gnus-id-to-thread (id)
4759   "Return the (sub-)thread where ID appears."
4760   (gnus-gethash id gnus-newsgroup-dependencies))
4761
4762 (defun gnus-id-to-article (id)
4763   "Return the article number of ID."
4764   (let ((thread (gnus-id-to-thread id)))
4765     (when (and thread
4766                (car thread))
4767       (mail-header-number (car thread)))))
4768
4769 (defun gnus-id-to-header (id)
4770   "Return the article headers of ID."
4771   (car (gnus-id-to-thread id)))
4772
4773 (defun gnus-article-displayed-root-p (article)
4774   "Say whether ARTICLE is a root(ish) article."
4775   (let ((level (gnus-summary-thread-level article))
4776         (refs (mail-header-references  (gnus-summary-article-header article)))
4777         particle)
4778     (cond
4779      ((null level) nil)
4780      ((zerop level) t)
4781      ((null refs) t)
4782      ((null (gnus-parent-id refs)) t)
4783      ((and (= 1 level)
4784            (null (setq particle (gnus-id-to-article
4785                                  (gnus-parent-id refs))))
4786            (null (gnus-summary-thread-level particle)))))))
4787
4788 (defun gnus-root-id (id)
4789   "Return the id of the root of the thread where ID appears."
4790   (let (last-id prev)
4791     (while (and id (setq prev (car (gnus-id-to-thread id))))
4792       (setq last-id id
4793             id (gnus-parent-id (mail-header-references prev))))
4794     last-id))
4795
4796 (defun gnus-articles-in-thread (thread)
4797   "Return the list of articles in THREAD."
4798   (cons (mail-header-number (car thread))
4799         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4800
4801 (defun gnus-remove-thread (id &optional dont-remove)
4802   "Remove the thread that has ID in it."
4803   (let (headers thread last-id)
4804     ;; First go up in this thread until we find the root.
4805     (setq last-id (gnus-root-id id)
4806           headers (message-flatten-list (gnus-id-to-thread last-id)))
4807     ;; We have now found the real root of this thread.  It might have
4808     ;; been gathered into some loose thread, so we have to search
4809     ;; through the threads to find the thread we wanted.
4810     (let ((threads gnus-newsgroup-threads)
4811           sub)
4812       (while threads
4813         (setq sub (car threads))
4814         (if (stringp (car sub))
4815             ;; This is a gathered thread, so we look at the roots
4816             ;; below it to find whether this article is in this
4817             ;; gathered root.
4818             (progn
4819               (setq sub (cdr sub))
4820               (while sub
4821                 (when (member (caar sub) headers)
4822                   (setq thread (car threads)
4823                         threads nil
4824                         sub nil))
4825                 (setq sub (cdr sub))))
4826           ;; It's an ordinary thread, so we check it.
4827           (when (eq (car sub) (car headers))
4828             (setq thread sub
4829                   threads nil)))
4830         (setq threads (cdr threads)))
4831       ;; If this article is in no thread, then it's a root.
4832       (if thread
4833           (unless dont-remove
4834             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4835         (setq thread (gnus-id-to-thread last-id)))
4836       (when thread
4837         (prog1
4838             thread                      ; We return this thread.
4839           (unless dont-remove
4840             (if (stringp (car thread))
4841                 (progn
4842                   ;; If we use dummy roots, then we have to remove the
4843                   ;; dummy root as well.
4844                   (when (eq gnus-summary-make-false-root 'dummy)
4845                     ;; We go to the dummy root by going to
4846                     ;; the first sub-"thread", and then one line up.
4847                     (gnus-summary-goto-article
4848                      (mail-header-number (caadr thread)))
4849                     (forward-line -1)
4850                     (gnus-delete-line)
4851                     (gnus-data-compute-positions))
4852                   (setq thread (cdr thread))
4853                   (while thread
4854                     (gnus-remove-thread-1 (car thread))
4855                     (setq thread (cdr thread))))
4856               (gnus-remove-thread-1 thread))))))))
4857
4858 (defun gnus-remove-thread-1 (thread)
4859   "Remove the thread THREAD recursively."
4860   (let ((number (mail-header-number (pop thread)))
4861         d)
4862     (setq thread (reverse thread))
4863     (while thread
4864       (gnus-remove-thread-1 (pop thread)))
4865     (when (setq d (gnus-data-find number))
4866       (goto-char (gnus-data-pos d))
4867       (gnus-summary-show-thread)
4868       (gnus-data-remove
4869        number
4870        (- (point-at-bol)
4871           (prog1
4872               (1+ (point-at-eol))
4873             (gnus-delete-line)))))))
4874
4875 (defun gnus-sort-threads-recursive (threads func)
4876   (sort (mapcar (lambda (thread)
4877                   (cons (car thread)
4878                         (and (cdr thread)
4879                              (gnus-sort-threads-recursive (cdr thread) func))))
4880                 threads) func))
4881
4882 (defun gnus-sort-threads-loop (threads func)
4883   (let* ((superthread (cons nil threads))
4884          (stack (list (cons superthread threads)))
4885          remaining-threads thread)
4886     (while stack
4887       (setq remaining-threads (cdr (car stack)))
4888       (if remaining-threads
4889           (progn (setq thread (car remaining-threads))
4890                  (setcdr (car stack) (cdr remaining-threads))
4891                  (if (cdr thread)
4892                      (push (cons thread (cdr thread)) stack)))
4893         (setq thread (caar stack))
4894         (setcdr thread (sort (cdr thread) func))
4895         (pop stack)))
4896     (cdr superthread)))
4897
4898 (defun gnus-sort-threads (threads)
4899   "Sort THREADS."
4900   (if (not gnus-thread-sort-functions)
4901       threads
4902     (gnus-message 8 "Sorting threads...")
4903     (prog1
4904         (condition-case nil
4905             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4906               (gnus-sort-threads-recursive
4907                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4908           ;; Even after binding max-lisp-eval-depth, the recursive
4909           ;; sorter might fail for very long threads.  In that case,
4910           ;; try using a (less well-tested) non-recursive sorter.
4911           (error (gnus-message 9 "Sorting threads with loop...")
4912                  (gnus-sort-threads-loop
4913                   threads (gnus-make-sort-function
4914                            gnus-thread-sort-functions))))
4915       (gnus-message 8 "Sorting threads...done"))))
4916
4917 (defun gnus-sort-articles (articles)
4918   "Sort ARTICLES."
4919   (when gnus-article-sort-functions
4920     (gnus-message 7 "Sorting articles...")
4921     (prog1
4922         (setq gnus-newsgroup-headers
4923               (sort articles (gnus-make-sort-function
4924                               gnus-article-sort-functions)))
4925       (gnus-message 7 "Sorting articles...done"))))
4926
4927 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4928 (defmacro gnus-thread-header (thread)
4929   "Return header of first article in THREAD.
4930 Note that THREAD must never, ever be anything else than a variable -
4931 using some other form will lead to serious barfage."
4932   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4933   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4934   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4935         (vector thread) 2))
4936
4937 (defsubst gnus-article-sort-by-number (h1 h2)
4938   "Sort articles by article number."
4939   (< (mail-header-number h1)
4940      (mail-header-number h2)))
4941
4942 (defun gnus-thread-sort-by-number (h1 h2)
4943   "Sort threads by root article number."
4944   (gnus-article-sort-by-number
4945    (gnus-thread-header h1) (gnus-thread-header h2)))
4946
4947 (defsubst gnus-article-sort-by-random (h1 h2)
4948   "Sort articles randomly."
4949   (zerop (random 2)))
4950
4951 (defun gnus-thread-sort-by-random (h1 h2)
4952   "Sort threads randomly."
4953   (gnus-article-sort-by-random
4954    (gnus-thread-header h1) (gnus-thread-header h2)))
4955
4956 (defsubst gnus-article-sort-by-lines (h1 h2)
4957   "Sort articles by article Lines header."
4958   (< (mail-header-lines h1)
4959      (mail-header-lines h2)))
4960
4961 (defun gnus-thread-sort-by-lines (h1 h2)
4962   "Sort threads by root article Lines header."
4963   (gnus-article-sort-by-lines
4964    (gnus-thread-header h1) (gnus-thread-header h2)))
4965
4966 (defsubst gnus-article-sort-by-chars (h1 h2)
4967   "Sort articles by octet length."
4968   (< (mail-header-chars h1)
4969      (mail-header-chars h2)))
4970
4971 (defun gnus-thread-sort-by-chars (h1 h2)
4972   "Sort threads by root article octet length."
4973   (gnus-article-sort-by-chars
4974    (gnus-thread-header h1) (gnus-thread-header h2)))
4975
4976 (defsubst gnus-article-sort-by-author (h1 h2)
4977   "Sort articles by root author."
4978   (gnus-string<
4979    (let ((extract (funcall
4980                    gnus-extract-address-components
4981                    (mail-header-from h1))))
4982      (or (car extract) (cadr extract) ""))
4983    (let ((extract (funcall
4984                    gnus-extract-address-components
4985                    (mail-header-from h2))))
4986      (or (car extract) (cadr extract) ""))))
4987
4988 (defun gnus-thread-sort-by-author (h1 h2)
4989   "Sort threads by root author."
4990   (gnus-article-sort-by-author
4991    (gnus-thread-header h1)  (gnus-thread-header h2)))
4992
4993 (defsubst gnus-article-sort-by-recipient (h1 h2)
4994   "Sort articles by recipient."
4995   (gnus-string<
4996    (let ((extract (funcall
4997                    gnus-extract-address-components
4998                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4999      (or (car extract) (cadr extract)))
5000    (let ((extract (funcall
5001                    gnus-extract-address-components
5002                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
5003      (or (car extract) (cadr extract)))))
5004
5005 (defun gnus-thread-sort-by-recipient (h1 h2)
5006   "Sort threads by root recipient."
5007   (gnus-article-sort-by-recipient
5008    (gnus-thread-header h1) (gnus-thread-header h2)))
5009
5010 (defsubst gnus-article-sort-by-subject (h1 h2)
5011   "Sort articles by root subject."
5012   (gnus-string<
5013    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5014    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5015
5016 (defun gnus-thread-sort-by-subject (h1 h2)
5017   "Sort threads by root subject."
5018   (gnus-article-sort-by-subject
5019    (gnus-thread-header h1) (gnus-thread-header h2)))
5020
5021 (defsubst gnus-article-sort-by-date (h1 h2)
5022   "Sort articles by root article date."
5023   (time-less-p
5024    (gnus-date-get-time (mail-header-date h1))
5025    (gnus-date-get-time (mail-header-date h2))))
5026
5027 (defun gnus-thread-sort-by-date (h1 h2)
5028   "Sort threads by root article date."
5029   (gnus-article-sort-by-date
5030    (gnus-thread-header h1) (gnus-thread-header h2)))
5031
5032 (defsubst gnus-article-sort-by-score (h1 h2)
5033   "Sort articles by root article score.
5034 Unscored articles will be counted as having a score of zero."
5035   (> (or (cdr (assq (mail-header-number h1)
5036                     gnus-newsgroup-scored))
5037          gnus-summary-default-score 0)
5038      (or (cdr (assq (mail-header-number h2)
5039                     gnus-newsgroup-scored))
5040          gnus-summary-default-score 0)))
5041
5042 (defun gnus-thread-sort-by-score (h1 h2)
5043   "Sort threads by root article score."
5044   (gnus-article-sort-by-score
5045    (gnus-thread-header h1) (gnus-thread-header h2)))
5046
5047 (defun gnus-thread-sort-by-total-score (h1 h2)
5048   "Sort threads by the sum of all scores in the thread.
5049 Unscored articles will be counted as having a score of zero."
5050   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5051
5052 (defun gnus-thread-total-score (thread)
5053   ;; This function find the total score of THREAD.
5054   (cond
5055    ((null thread)
5056     0)
5057    ((consp thread)
5058     (if (stringp (car thread))
5059         (apply gnus-thread-score-function 0
5060                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5061       (gnus-thread-total-score-1 thread)))
5062    (t
5063     (gnus-thread-total-score-1 (list thread)))))
5064
5065 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5066   "Sort articles by number."
5067   (gnus-article-sort-by-number h1 h2))
5068
5069 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5070   "Sort threads such that the thread with the most recently arrived article comes first."
5071   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5072
5073 (defun gnus-thread-highest-number (thread)
5074   "Return the highest article number in THREAD."
5075   (apply 'max (mapcar (lambda (header)
5076                         (mail-header-number header))
5077                       (message-flatten-list thread))))
5078
5079 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5080   "Sort articles by number."
5081   (gnus-article-sort-by-date h1 h2))
5082
5083 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5084   "Sort threads such that the thread with the most recently dated article comes first."
5085   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5086
5087 ; Since this is called not only to sort the top-level threads, but
5088 ; also in recursive sorts to order the articles within a thread, each
5089 ; article will be processed many times.  Thus it speeds things up
5090 ; quite a bit to use gnus-date-get-time, which caches the time value.
5091 (defun gnus-thread-latest-date (thread)
5092   "Return the highest article date in THREAD."
5093   (apply 'max
5094          (mapcar (lambda (header) (gnus-float-time
5095                                    (gnus-date-get-time
5096                                     (mail-header-date header))))
5097                  (message-flatten-list thread))))
5098
5099 (defun gnus-thread-total-score-1 (root)
5100   ;; This function find the total score of the thread below ROOT.
5101   (setq root (car root))
5102   (apply gnus-thread-score-function
5103          (or (append
5104               (mapcar 'gnus-thread-total-score
5105                       (cdr (gnus-id-to-thread (mail-header-id root))))
5106               (when (> (mail-header-number root) 0)
5107                 (list (or (cdr (assq (mail-header-number root)
5108                                      gnus-newsgroup-scored))
5109                           gnus-summary-default-score 0))))
5110              (list gnus-summary-default-score)
5111              '(0))))
5112
5113 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5114 (defvar gnus-tmp-prev-subject nil)
5115 (defvar gnus-tmp-false-parent nil)
5116 (defvar gnus-tmp-root-expunged nil)
5117 (defvar gnus-tmp-dummy-line nil)
5118
5119 (defun gnus-extra-header (type &optional header)
5120   "Return the extra header of TYPE."
5121   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5122       ""))
5123
5124 (defvar gnus-tmp-thread-tree-header-string "")
5125
5126 (defcustom gnus-sum-thread-tree-root "> "
5127   "With %B spec, used for the root of a thread.
5128 If nil, use subject instead."
5129   :version "22.1"
5130   :type '(radio (const :format "%v  " nil) string)
5131   :group 'gnus-thread)
5132
5133 (defcustom gnus-sum-thread-tree-false-root "> "
5134   "With %B spec, used for a false root of a thread.
5135 If nil, use subject instead."
5136   :version "22.1"
5137   :type '(radio (const :format "%v  " nil) string)
5138   :group 'gnus-thread)
5139
5140 (defcustom gnus-sum-thread-tree-single-indent ""
5141   "With %B spec, used for a thread with just one message.
5142 If nil, use subject instead."
5143   :version "22.1"
5144   :type '(radio (const :format "%v  " nil) string)
5145   :group 'gnus-thread)
5146
5147 (defcustom gnus-sum-thread-tree-vertical "| "
5148   "With %B spec, used for drawing a vertical line."
5149   :version "22.1"
5150   :type 'string
5151   :group 'gnus-thread)
5152
5153 (defcustom gnus-sum-thread-tree-indent "  "
5154   "With %B spec, used for indenting."
5155   :version "22.1"
5156   :type 'string
5157   :group 'gnus-thread)
5158
5159 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5160   "With %B spec, used for a leaf with brothers."
5161   :version "22.1"
5162   :type 'string
5163   :group 'gnus-thread)
5164
5165 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5166   "With %B spec, used for a leaf without brothers."
5167   :version "22.1"
5168   :type 'string
5169   :group 'gnus-thread)
5170
5171 (defcustom gnus-summary-display-while-building nil
5172   "If non-nil, show and update the summary buffer as it's being built.
5173 If the value is t, update the buffer after every line is inserted.  If
5174 the value is an integer (N), update the display every N lines."
5175   :version "22.1"
5176   :group 'gnus-thread
5177   :type '(choice (const :tag "off" nil)
5178                  number
5179                  (const :tag "frequently" t)))
5180
5181 (defun gnus-summary-prepare-threads (threads)
5182   "Prepare summary buffer from THREADS and indentation LEVEL.
5183 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5184 or a straight list of headers."
5185   (gnus-message 7 "Generating summary...")
5186
5187   (setq gnus-newsgroup-threads threads)
5188   (beginning-of-line)
5189
5190   (let ((gnus-tmp-level 0)
5191         (default-score (or gnus-summary-default-score 0))
5192         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5193         (building-line-count gnus-summary-display-while-building)
5194         (building-count (integerp gnus-summary-display-while-building))
5195         thread number subject stack state gnus-tmp-gathered beg-match
5196         new-roots gnus-tmp-new-adopts thread-end simp-subject
5197         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5198         gnus-tmp-replied gnus-tmp-subject-or-nil
5199         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5200         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5201         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5202         tree-stack)
5203
5204     (setq gnus-tmp-prev-subject nil
5205           gnus-tmp-thread-tree-header-string "")
5206
5207     (if (vectorp (car threads))
5208         ;; If this is a straight (sic) list of headers, then a
5209         ;; threaded summary display isn't required, so we just create
5210         ;; an unthreaded one.
5211         (gnus-summary-prepare-unthreaded threads)
5212
5213       ;; Do the threaded display.
5214
5215       (if gnus-summary-display-while-building
5216           (switch-to-buffer (buffer-name)))
5217       (while (or threads stack gnus-tmp-new-adopts new-roots)
5218
5219         (if (and (= gnus-tmp-level 0)
5220                  (or (not stack)
5221                      (= (caar stack) 0))
5222                  (not gnus-tmp-false-parent)
5223                  (or gnus-tmp-new-adopts new-roots))
5224             (if gnus-tmp-new-adopts
5225                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5226                       thread (list (car gnus-tmp-new-adopts))
5227                       gnus-tmp-header (caar thread)
5228                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5229               (when new-roots
5230                 (setq thread (list (car new-roots))
5231                       gnus-tmp-header (caar thread)
5232                       new-roots (cdr new-roots))))
5233
5234           (if threads
5235               ;; If there are some threads, we do them before the
5236               ;; threads on the stack.
5237               (setq thread threads
5238                     gnus-tmp-header (caar thread))
5239             ;; There were no current threads, so we pop something off
5240             ;; the stack.
5241             (setq state (car stack)
5242                   gnus-tmp-level (car state)
5243                   tree-stack (cadr state)
5244                   thread (caddr state)
5245                   stack (cdr stack)
5246                   gnus-tmp-header (caar thread))))
5247
5248         (setq gnus-tmp-false-parent nil)
5249         (setq gnus-tmp-root-expunged nil)
5250         (setq thread-end nil)
5251
5252         (if (stringp gnus-tmp-header)
5253             ;; The header is a dummy root.
5254             (cond
5255              ((eq gnus-summary-make-false-root 'adopt)
5256               ;; We let the first article adopt the rest.
5257               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5258                                                (cddar thread)))
5259               (setq gnus-tmp-gathered
5260                     (nconc (mapcar
5261                             (lambda (h) (mail-header-number (car h)))
5262                             (cddar thread))
5263                            gnus-tmp-gathered))
5264               (setq thread (cons (list (caar thread)
5265                                        (cadar thread))
5266                                  (cdr thread)))
5267               (setq gnus-tmp-level -1
5268                     gnus-tmp-false-parent t))
5269              ((eq gnus-summary-make-false-root 'empty)
5270               ;; We print adopted articles with empty subject fields.
5271               (setq gnus-tmp-gathered
5272                     (nconc (mapcar
5273                             (lambda (h) (mail-header-number (car h)))
5274                             (cddar thread))
5275                            gnus-tmp-gathered))
5276               (setq gnus-tmp-level -1))
5277              ((eq gnus-summary-make-false-root 'dummy)
5278               ;; We remember that we probably want to output a dummy
5279               ;; root.
5280               (setq gnus-tmp-dummy-line gnus-tmp-header)
5281               (setq gnus-tmp-prev-subject gnus-tmp-header))
5282              (t
5283               ;; We do not make a root for the gathered
5284               ;; sub-threads at all.
5285               (setq gnus-tmp-level -1)))
5286
5287           (setq number (mail-header-number gnus-tmp-header)
5288                 subject (mail-header-subject gnus-tmp-header)
5289                 simp-subject (gnus-simplify-subject-fully subject))
5290
5291           (cond
5292            ;; If the thread has changed subject, we might want to make
5293            ;; this subthread into a root.
5294            ((and (null gnus-thread-ignore-subject)
5295                  (not (zerop gnus-tmp-level))
5296                  gnus-tmp-prev-subject
5297                  (not (string= gnus-tmp-prev-subject simp-subject)))
5298             (setq new-roots (nconc new-roots (list (car thread)))
5299                   thread-end t
5300                   gnus-tmp-header nil))
5301            ;; If the article lies outside the current limit,
5302            ;; then we do not display it.
5303            ((not (memq number gnus-newsgroup-limit))
5304             (setq gnus-tmp-gathered
5305                   (nconc (mapcar
5306                           (lambda (h) (mail-header-number (car h)))
5307                           (cdar thread))
5308                          gnus-tmp-gathered))
5309             (setq gnus-tmp-new-adopts (if (cdar thread)
5310                                           (append gnus-tmp-new-adopts
5311                                                   (cdar thread))
5312                                         gnus-tmp-new-adopts)
5313                   thread-end t
5314                   gnus-tmp-header nil)
5315             (when (zerop gnus-tmp-level)
5316               (setq gnus-tmp-root-expunged t)))
5317            ;; Perhaps this article is to be marked as read?
5318            ((and gnus-summary-mark-below
5319                  (< (or (cdr (assq number gnus-newsgroup-scored))
5320                         default-score)
5321                     gnus-summary-mark-below)
5322                  ;; Don't touch sparse articles.
5323                  (not (gnus-summary-article-sparse-p number))
5324                  (not (gnus-summary-article-ancient-p number)))
5325             (setq gnus-newsgroup-unreads
5326                   (delq number gnus-newsgroup-unreads))
5327             (if gnus-newsgroup-auto-expire
5328                 (setq gnus-newsgroup-expirable
5329                       (gnus-add-to-sorted-list
5330                        gnus-newsgroup-expirable number))
5331               (push (cons number gnus-low-score-mark)
5332                     gnus-newsgroup-reads))))
5333
5334           (when gnus-tmp-header
5335             ;; We may have an old dummy line to output before this
5336             ;; article.
5337             (when (and gnus-tmp-dummy-line
5338                        (gnus-subject-equal
5339                         gnus-tmp-dummy-line
5340                         (mail-header-subject gnus-tmp-header)))
5341               (gnus-summary-insert-dummy-line
5342                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5343               (setq gnus-tmp-dummy-line nil))
5344
5345             ;; Compute the mark.
5346             (setq gnus-tmp-unread (gnus-article-mark number))
5347
5348             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5349                                   gnus-tmp-header gnus-tmp-level)
5350                   gnus-newsgroup-data)
5351
5352             ;; Actually insert the line.
5353             (setq
5354              gnus-tmp-subject-or-nil
5355              (cond
5356               ((and gnus-thread-ignore-subject
5357                     gnus-tmp-prev-subject
5358                     (not (string= gnus-tmp-prev-subject simp-subject)))
5359                subject)
5360               ((zerop gnus-tmp-level)
5361                (if (and (eq gnus-summary-make-false-root 'empty)
5362                         (memq number gnus-tmp-gathered)
5363                         gnus-tmp-prev-subject
5364                         (string= gnus-tmp-prev-subject simp-subject))
5365                    gnus-summary-same-subject
5366                  subject))
5367               (t gnus-summary-same-subject)))
5368             (if (and (eq gnus-summary-make-false-root 'adopt)
5369                      (= gnus-tmp-level 1)
5370                      (memq number gnus-tmp-gathered))
5371                 (setq gnus-tmp-opening-bracket ?\<
5372                       gnus-tmp-closing-bracket ?\>)
5373               (setq gnus-tmp-opening-bracket ?\[
5374                     gnus-tmp-closing-bracket ?\]))
5375             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5376                 (gnus-make-thread-indent-array
5377                  (max (* 2 (length gnus-thread-indent-array))
5378                       gnus-tmp-level)))
5379             (setq
5380              gnus-tmp-indentation
5381              (aref gnus-thread-indent-array gnus-tmp-level)
5382              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5383              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5384                                 gnus-summary-default-score 0)
5385              gnus-tmp-score-char
5386              (if (or (null gnus-summary-default-score)
5387                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5388                          gnus-summary-zcore-fuzz))
5389                  ?                      ;Whitespace
5390                (if (< gnus-tmp-score gnus-summary-default-score)
5391                    gnus-score-below-mark gnus-score-over-mark))
5392              gnus-tmp-replied
5393              (cond ((memq number gnus-newsgroup-processable)
5394                     gnus-process-mark)
5395                    ((memq number gnus-newsgroup-cached)
5396                     gnus-cached-mark)
5397                    ((memq number gnus-newsgroup-replied)
5398                     gnus-replied-mark)
5399                    ((memq number gnus-newsgroup-forwarded)
5400                     gnus-forwarded-mark)
5401                    ((memq number gnus-newsgroup-saved)
5402                     gnus-saved-mark)
5403                    ((memq number gnus-newsgroup-unseen)
5404                     gnus-unseen-mark)
5405                    (t gnus-no-mark))
5406              gnus-tmp-downloaded
5407              (cond ((memq number gnus-newsgroup-undownloaded)
5408                     gnus-undownloaded-mark)
5409                    (gnus-newsgroup-agentized
5410                     gnus-downloaded-mark)
5411                    (t
5412                     gnus-no-mark))
5413              gnus-tmp-from (mail-header-from gnus-tmp-header)
5414              gnus-tmp-name
5415              (cond
5416               ((string-match "<[^>]+> *$" gnus-tmp-from)
5417                (setq beg-match (match-beginning 0))
5418                (or (and (string-match "^\".+\"" gnus-tmp-from)
5419                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5420                    (substring gnus-tmp-from 0 beg-match)))
5421               ((string-match "(.+)" gnus-tmp-from)
5422                (substring gnus-tmp-from
5423                           (1+ (match-beginning 0)) (1- (match-end 0))))
5424               (t gnus-tmp-from))
5425
5426              ;; Do the %B string
5427              gnus-tmp-thread-tree-header-string
5428              (cond
5429               ((not gnus-show-threads) "")
5430               ((zerop gnus-tmp-level)
5431                (cond ((cdar thread)
5432                       (or gnus-sum-thread-tree-root subject))
5433                      (gnus-tmp-new-adopts
5434                       (or gnus-sum-thread-tree-false-root subject))
5435                      (t
5436                       (or gnus-sum-thread-tree-single-indent subject))))
5437               (t
5438                (concat (apply 'concat
5439                               (mapcar (lambda (item)
5440                                         (if (= item 1)
5441                                             gnus-sum-thread-tree-vertical
5442                                           gnus-sum-thread-tree-indent))
5443                                       (cdr (reverse tree-stack))))
5444                        (if (nth 1 thread)
5445                            gnus-sum-thread-tree-leaf-with-other
5446                          gnus-sum-thread-tree-single-leaf)))))
5447             (when (string= gnus-tmp-name "")
5448               (setq gnus-tmp-name gnus-tmp-from))
5449             (unless (numberp gnus-tmp-lines)
5450               (setq gnus-tmp-lines -1))
5451             (if (= gnus-tmp-lines -1)
5452                 (setq gnus-tmp-lines "?")
5453               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5454             (gnus-put-text-property
5455              (point)
5456              (progn (eval gnus-summary-line-format-spec) (point))
5457              'gnus-number number)
5458             (when gnus-visual-p
5459               (forward-line -1)
5460               (gnus-summary-highlight-line)
5461               (when gnus-summary-update-hook
5462                 (gnus-run-hooks 'gnus-summary-update-hook))
5463               (forward-line 1))
5464
5465             (setq gnus-tmp-prev-subject simp-subject)))
5466
5467         (when (nth 1 thread)
5468           (push (list (max 0 gnus-tmp-level)
5469                       (copy-sequence tree-stack)
5470                       (nthcdr 1 thread))
5471                 stack))
5472         (push (if (nth 1 thread) 1 0) tree-stack)
5473         (incf gnus-tmp-level)
5474         (setq threads (if thread-end nil (cdar thread)))
5475         (if gnus-summary-display-while-building
5476             (if building-count
5477                 (progn
5478                   ;; use a set frequency
5479                   (setq building-line-count (1- building-line-count))
5480                   (when (= building-line-count 0)
5481                     (sit-for 0)
5482                     (setq building-line-count
5483                           gnus-summary-display-while-building)))
5484               ;; always
5485               (sit-for 0)))
5486         (unless threads
5487           (setq gnus-tmp-level 0)))))
5488   (gnus-message 7 "Generating summary...done"))
5489
5490 (defun gnus-summary-prepare-unthreaded (headers)
5491   "Generate an unthreaded summary buffer based on HEADERS."
5492   (let (header number mark)
5493
5494     (beginning-of-line)
5495
5496     (while headers
5497       ;; We may have to root out some bad articles...
5498       (when (memq (setq number (mail-header-number
5499                                 (setq header (pop headers))))
5500                   gnus-newsgroup-limit)
5501         ;; Mark article as read when it has a low score.
5502         (when (and gnus-summary-mark-below
5503                    (< (or (cdr (assq number gnus-newsgroup-scored))
5504                           gnus-summary-default-score 0)
5505                       gnus-summary-mark-below)
5506                    (not (gnus-summary-article-ancient-p number)))
5507           (setq gnus-newsgroup-unreads
5508                 (delq number gnus-newsgroup-unreads))
5509           (if gnus-newsgroup-auto-expire
5510               (push number gnus-newsgroup-expirable)
5511             (push (cons number gnus-low-score-mark)
5512                   gnus-newsgroup-reads)))
5513
5514         (setq mark (gnus-article-mark number))
5515         (push (gnus-data-make number mark (1+ (point)) header 0)
5516               gnus-newsgroup-data)
5517         (gnus-summary-insert-line
5518          header 0 number
5519          (memq number gnus-newsgroup-undownloaded)
5520          mark (memq number gnus-newsgroup-replied)
5521          (memq number gnus-newsgroup-expirable)
5522          (mail-header-subject header) nil
5523          (cdr (assq number gnus-newsgroup-scored))
5524          (memq number gnus-newsgroup-processable))))))
5525
5526 (defun gnus-group-get-list-identifiers (group)
5527   "Get list identifier regexp for GROUP."
5528   (or (gnus-parameter-list-identifier group)
5529       (if (consp gnus-list-identifiers)
5530           (mapconcat 'identity gnus-list-identifiers " *\\|")
5531         gnus-list-identifiers)))
5532
5533 (defun gnus-summary-remove-list-identifiers ()
5534   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5535   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5536         changed subject)
5537     (when regexp
5538       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5539       (dolist (header gnus-newsgroup-headers)
5540         (setq subject (mail-header-subject header)
5541               changed nil)
5542         (while (string-match regexp subject)
5543           (setq subject
5544                 (concat (substring subject 0 (match-beginning 1))
5545                         (substring subject (match-end 0)))
5546                 changed t))
5547         (when changed
5548           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5549             (setq subject
5550                   (concat (substring subject 0 (match-beginning 1))
5551                           (substring subject (match-end 1)))))
5552           (mail-header-set-subject header subject))))))
5553
5554 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5555   "Fetch headers of ARTICLES."
5556   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5557     (gnus-message 7 "Fetching headers for %s..." name)
5558     (prog1
5559         (if (eq 'nov
5560                 (setq gnus-headers-retrieved-by
5561                       (gnus-retrieve-headers
5562                        articles gnus-newsgroup-name
5563                        (or limit
5564                            ;; We might want to fetch old headers, but
5565                            ;; not if there is only 1 article.
5566                            (and (or (and
5567                                      (not (eq gnus-fetch-old-headers 'some))
5568                                      (not (numberp gnus-fetch-old-headers)))
5569                                     (> (length articles) 1))
5570                                 gnus-fetch-old-headers)))))
5571             (gnus-get-newsgroup-headers-xover
5572              articles force-new dependencies gnus-newsgroup-name t)
5573           (gnus-get-newsgroup-headers dependencies force-new))
5574       (gnus-message 7 "Fetching headers for %s...done" name))))
5575
5576 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5577   "Select newsgroup GROUP.
5578 If READ-ALL is non-nil, all articles in the group are selected.
5579 If SELECT-ARTICLES, only select those articles from GROUP."
5580   (let* ((entry (gnus-group-entry group))
5581          ;;!!! Dirty hack; should be removed.
5582          (gnus-summary-ignore-duplicates
5583           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5584               t
5585             gnus-summary-ignore-duplicates))
5586          (info (nth 2 entry))
5587          charset articles fetched-articles cached)
5588
5589     (unless (gnus-check-server
5590              (set (make-local-variable 'gnus-current-select-method)
5591                   (gnus-find-method-for-group group)))
5592       (error "Couldn't open server"))
5593     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5594
5595     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5596         (gnus-activate-group group)     ; Or we can activate it...
5597         (progn                          ; Or we bug out.
5598           (when (equal major-mode 'gnus-summary-mode)
5599             (gnus-kill-buffer (current-buffer)))
5600           (error
5601            "Couldn't activate group %s: %s"
5602            (mm-decode-coding-string group charset)
5603            (mm-decode-coding-string (gnus-status-message group) charset))))
5604
5605     (unless (gnus-request-group group t)
5606       (when (equal major-mode 'gnus-summary-mode)
5607         (gnus-kill-buffer (current-buffer)))
5608       (error "Couldn't request group %s: %s"
5609              (mm-decode-coding-string group charset)
5610              (mm-decode-coding-string (gnus-status-message group) charset)))
5611
5612     (when (and gnus-agent
5613                (gnus-active group))
5614       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5615
5616       (setq gnus-summary-use-undownloaded-faces
5617             (gnus-agent-find-parameter
5618              group
5619              'agent-enable-undownloaded-faces)))
5620
5621     (setq gnus-newsgroup-name group
5622           gnus-newsgroup-unselected nil
5623           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5624
5625     (let ((display (gnus-group-find-parameter group 'display)))
5626       (setq gnus-newsgroup-display
5627             (cond
5628              ((not (zerop (or (car-safe read-all) 0)))
5629               ;; The user entered the group with C-u SPC/RET, let's show
5630               ;; all articles.
5631               'gnus-not-ignore)
5632              ((eq display 'all)
5633               'gnus-not-ignore)
5634              ((arrayp display)
5635               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5636              ((numberp display)
5637               ;; The following is probably the "correct" solution, but
5638               ;; it makes Gnus fetch all headers and then limit the
5639               ;; articles (which is slow), so instead we hack the
5640               ;; select-articles parameter instead. -- Simon Josefsson
5641               ;; <jas@kth.se>
5642               ;;
5643               ;; (gnus-byte-compile
5644               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5645               ;;                         display)))))
5646               (setq select-articles
5647                     (gnus-uncompress-range
5648                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5649                              (if (> tmp 0)
5650                                  tmp
5651                                1))
5652                            (cdr (gnus-active group)))))
5653               nil)
5654              (t
5655               nil))))
5656
5657     (gnus-summary-setup-default-charset)
5658
5659     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5660     (when (gnus-virtual-group-p group)
5661       (setq cached gnus-newsgroup-cached))
5662
5663     (setq gnus-newsgroup-unreads
5664           (gnus-sorted-ndifference
5665            (gnus-sorted-ndifference gnus-newsgroup-unreads
5666                                     gnus-newsgroup-marked)
5667            gnus-newsgroup-dormant))
5668
5669     (setq gnus-newsgroup-processable nil)
5670
5671     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5672
5673     ;; Adjust and set lists of article marks.
5674     (when info
5675       (gnus-adjust-marked-articles info))
5676     (if (setq articles select-articles)
5677         (setq gnus-newsgroup-unselected
5678               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5679       (setq articles (gnus-articles-to-read group read-all)))
5680     
5681     (cond
5682      ((null articles)
5683       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5684       'quit)
5685      ((eq articles 0) nil)
5686      (t
5687       ;; Init the dependencies hash table.
5688       (setq gnus-newsgroup-dependencies
5689             (gnus-make-hashtable (length articles)))
5690       (gnus-set-global-variables)
5691       ;; Retrieve the headers and read them in.
5692
5693       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5694
5695       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5696       (when cached
5697         (setq gnus-newsgroup-cached cached))
5698
5699       ;; Suppress duplicates?
5700       (when gnus-suppress-duplicates
5701         (gnus-dup-suppress-articles))
5702
5703       ;; Set the initial limit.
5704       (setq gnus-newsgroup-limit (copy-sequence articles))
5705       ;; Remove canceled articles from the list of unread articles.
5706       (setq fetched-articles
5707             (mapcar (lambda (headers) (mail-header-number headers))
5708                     gnus-newsgroup-headers))
5709       (setq gnus-newsgroup-articles fetched-articles)
5710       (setq gnus-newsgroup-unreads
5711             (gnus-sorted-nintersection
5712              gnus-newsgroup-unreads fetched-articles))
5713       (gnus-compute-unseen-list)
5714
5715       ;; Removed marked articles that do not exist.
5716       (gnus-update-missing-marks
5717        (gnus-sorted-difference articles fetched-articles))
5718       ;; We might want to build some more threads first.
5719       (when (and gnus-fetch-old-headers
5720                  (eq gnus-headers-retrieved-by 'nov))
5721         (if (eq gnus-fetch-old-headers 'invisible)
5722             (gnus-build-all-threads)
5723           (gnus-build-old-threads)))
5724       ;; Let the Gnus agent mark articles as read.
5725       (when gnus-agent
5726         (gnus-agent-get-undownloaded-list))
5727       ;; Remove list identifiers from subject
5728       (gnus-summary-remove-list-identifiers)
5729       ;; Check whether auto-expire is to be done in this group.
5730       (setq gnus-newsgroup-auto-expire
5731             (and (gnus-group-auto-expirable-p group)
5732                  (not (gnus-group-read-only-p group))))
5733       ;; Set up the article buffer now, if necessary.
5734       (unless (and gnus-single-article-buffer
5735                    (equal gnus-article-buffer "*Article*"))
5736         (gnus-article-setup-buffer))
5737       ;; First and last article in this newsgroup.
5738       (when gnus-newsgroup-headers
5739         (setq gnus-newsgroup-begin
5740               (mail-header-number (car gnus-newsgroup-headers))
5741               gnus-newsgroup-end
5742               (mail-header-number
5743                (gnus-last-element gnus-newsgroup-headers))))
5744       ;; GROUP is successfully selected.
5745       (or gnus-newsgroup-headers t)))))
5746
5747 (defun gnus-compute-unseen-list ()
5748   ;; The `seen' marks are treated specially.
5749   (if (not gnus-newsgroup-seen)
5750       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5751     (setq gnus-newsgroup-unseen
5752           (gnus-inverse-list-range-intersection
5753            gnus-newsgroup-articles gnus-newsgroup-seen))))
5754
5755 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5756
5757 (defun gnus-summary-display-make-predicate (display)
5758   (require 'gnus-agent)
5759   (when (= (length display) 1)
5760     (setq display (car display)))
5761   (unless gnus-summary-display-cache
5762     (dolist (elem (append '((unread . unread)
5763                             (read . read)
5764                             (unseen . unseen))
5765                           gnus-article-mark-lists))
5766       (push (cons (cdr elem)
5767                   (gnus-byte-compile    ;Why bother?
5768                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5769             gnus-summary-display-cache)))
5770   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5771         (gnus-category-predicate-cache gnus-summary-display-cache))
5772     (gnus-get-predicate display)))
5773
5774 ;; Uses the dynamically bound `gnus-number' variable.
5775 (defvar gnus-number)
5776 (defun gnus-article-marked-p (type &optional article)
5777   (let ((article (or article gnus-number)))
5778     (cond
5779      ((eq type 'tick)
5780       (memq article gnus-newsgroup-marked))
5781      ((eq type 'spam)
5782       (memq article gnus-newsgroup-spam-marked))
5783      ((eq type 'unsend)
5784       (memq article gnus-newsgroup-unsendable))
5785      ((eq type 'undownload)
5786       (memq article gnus-newsgroup-undownloaded))
5787      ((eq type 'download)
5788       (memq article gnus-newsgroup-downloadable))
5789      ((eq type 'unread)
5790       (memq article gnus-newsgroup-unreads))
5791      ((eq type 'read)
5792       (memq article gnus-newsgroup-reads))
5793      ((eq type 'dormant)
5794       (memq article gnus-newsgroup-dormant) )
5795      ((eq type 'expire)
5796       (memq article gnus-newsgroup-expirable))
5797      ((eq type 'reply)
5798       (memq article gnus-newsgroup-replied))
5799      ((eq type 'killed)
5800       (memq article gnus-newsgroup-killed))
5801      ((eq type 'bookmark)
5802       (assq article gnus-newsgroup-bookmarks))
5803      ((eq type 'score)
5804       (assq article gnus-newsgroup-scored))
5805      ((eq type 'save)
5806       (memq article gnus-newsgroup-saved))
5807      ((eq type 'cache)
5808       (memq article gnus-newsgroup-cached))
5809      ((eq type 'forward)
5810       (memq article gnus-newsgroup-forwarded))
5811      ((eq type 'seen)
5812       (not (memq article gnus-newsgroup-unseen)))
5813      (t t))))
5814
5815 (defun gnus-articles-to-read (group &optional read-all)
5816   "Find out what articles the user wants to read."
5817   (let* ((only-read-p t)
5818          (articles
5819           (gnus-list-range-difference
5820           ;; Select all articles if `read-all' is non-nil, or if there
5821           ;; are no unread articles.
5822           (if (or read-all
5823                   (and (zerop (length gnus-newsgroup-marked))
5824                        (zerop (length gnus-newsgroup-unreads)))
5825                   ;; Fetch all if the predicate is non-nil.
5826                   gnus-newsgroup-display)
5827               ;; We want to select the headers for all the articles in
5828               ;; the group, so we select either all the active
5829               ;; articles in the group, or (if that's nil), the
5830               ;; articles in the cache.
5831               (or
5832                (if gnus-newsgroup-maximum-articles
5833                    (let ((active (gnus-active group)))
5834                      (gnus-uncompress-range
5835                       (cons (max (car active)
5836                                  (- (cdr active)
5837                                     gnus-newsgroup-maximum-articles
5838                                     -1))
5839                             (cdr active))))
5840                  (gnus-uncompress-range (gnus-active group)))
5841                (gnus-cache-articles-in-group group))
5842             ;; Select only the "normal" subset of articles.
5843             (setq only-read-p nil)
5844             (gnus-sorted-nunion
5845              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5846              gnus-newsgroup-unreads))
5847           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5848          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5849          (scored (length scored-list))
5850          (number (length articles))
5851          (marked (+ (length gnus-newsgroup-marked)
5852                     (length gnus-newsgroup-dormant)))
5853          (select
5854           (cond
5855            ((numberp read-all)
5856             read-all)
5857            ((numberp gnus-newsgroup-display)
5858             gnus-newsgroup-display)
5859            (t
5860             (condition-case ()
5861                 (cond
5862                  ((and (or (<= scored marked) (= scored number))
5863                        (numberp gnus-large-newsgroup)
5864                        (> number gnus-large-newsgroup))
5865                   (let* ((cursor-in-echo-area nil)
5866                          (initial (gnus-parameter-large-newsgroup-initial
5867                                    gnus-newsgroup-name))
5868                          (default (if only-read-p
5869                                       (or initial gnus-large-newsgroup)
5870                                     number))
5871                          (input
5872                           (read-string
5873                            (if only-read-p
5874                                (format
5875                                 "How many articles from %s (available %d, default %d): "
5876                                 (gnus-group-decoded-name
5877                                  (gnus-group-real-name gnus-newsgroup-name))
5878                                 number default)
5879                              (format
5880                               "How many articles from %s (%d default): "
5881                               (gnus-group-decoded-name
5882                                (gnus-group-real-name gnus-newsgroup-name))
5883                               default))
5884                            nil
5885                            nil
5886                            (number-to-string default))))
5887                     (if (string-match "^[ \t]*$" input) number input)))
5888                  ((and (> scored marked) (< scored number)
5889                        (> (- scored number) 20))
5890                   (let ((input
5891                          (read-string
5892                           (format "%s %s (%d scored, %d total): "
5893                                   "How many articles from"
5894                                   (gnus-group-decoded-name
5895                                    (gnus-group-real-name gnus-newsgroup-name))
5896                                   scored number))))
5897                     (if (string-match "^[ \t]*$" input)
5898                         number input)))
5899                  (t number))
5900               (quit
5901                (message "Quit getting the articles to read")
5902                nil))))))
5903     (setq select (if (stringp select) (string-to-number select) select))
5904     (if (or (null select) (zerop select))
5905         select
5906       (if (and (not (zerop scored)) (<= (abs select) scored))
5907           (progn
5908             (setq articles (sort scored-list '<))
5909             (setq number (length articles)))
5910         (setq articles (copy-sequence articles)))
5911
5912       (when (< (abs select) number)
5913         (if (< select 0)
5914             ;; Select the N oldest articles.
5915             (setcdr (nthcdr (1- (abs select)) articles) nil)
5916           ;; Select the N most recent articles.
5917           (setq articles (nthcdr (- number select) articles))))
5918       (setq gnus-newsgroup-unselected
5919             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5920       (when gnus-alter-articles-to-read-function
5921         (setq articles
5922               (sort
5923                (funcall gnus-alter-articles-to-read-function
5924                         gnus-newsgroup-name articles)
5925                '<)))
5926       articles)))
5927
5928 (defun gnus-killed-articles (killed articles)
5929   (let (out)
5930     (while articles
5931       (when (inline (gnus-member-of-range (car articles) killed))
5932         (push (car articles) out))
5933       (setq articles (cdr articles)))
5934     out))
5935
5936 (defun gnus-uncompress-marks (marks)
5937   "Uncompress the mark ranges in MARKS."
5938   (let ((uncompressed '(score bookmark))
5939         out)
5940     (while marks
5941       (if (memq (caar marks) uncompressed)
5942           (push (car marks) out)
5943         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5944       (setq marks (cdr marks)))
5945     out))
5946
5947 (defun gnus-article-mark-to-type (mark)
5948   "Return the type of MARK."
5949   (or (cadr (assq mark gnus-article-special-mark-lists))
5950       'list))
5951
5952 (defun gnus-article-unpropagatable-p (mark)
5953   "Return whether MARK should be propagated to back end."
5954   (memq mark gnus-article-unpropagated-mark-lists))
5955
5956 (defun gnus-adjust-marked-articles (info)
5957   "Set all article lists and remove all marks that are no longer valid."
5958   (let* ((marked-lists (gnus-info-marks info))
5959          (active (gnus-active (gnus-info-group info)))
5960          (min (car active))
5961          (max (cdr active))
5962          (types gnus-article-mark-lists)
5963          marks var articles article mark mark-type
5964          bgn end)
5965     ;; Hack to avoid adjusting marks for imap.
5966     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5967               'nnimap)
5968       (setq min 1))
5969
5970     (dolist (marks marked-lists)
5971       (setq mark (car marks)
5972             mark-type (gnus-article-mark-to-type mark)
5973             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5974
5975       ;; We set the variable according to the type of the marks list,
5976       ;; and then adjust the marks to a subset of the active articles.
5977       (cond
5978        ;; Adjust "simple" lists - compressed yet unsorted
5979        ((eq mark-type 'list)
5980         ;; Simultaneously uncompress and clip to active range
5981         ;; See gnus-uncompress-range for a description of possible marks
5982         (let (l lh)
5983           (if (not (cadr marks))
5984               (set var nil)
5985             (setq articles (if (numberp (cddr marks))
5986                                (list (cdr marks))
5987                              (cdr marks))
5988                   lh (cons nil nil)
5989                   l lh)
5990
5991             (while (setq article (pop articles))
5992               (cond ((consp article)
5993                      (setq bgn (max (car article) min)
5994                            end (min (cdr article) max))
5995                      (while (<= bgn end)
5996                        (setq l (setcdr l (cons bgn nil))
5997                              bgn (1+ bgn))))
5998                     ((and (<= min article)
5999                           (>= max article))
6000                      (setq l (setcdr l (cons article nil))))))
6001             (set var (cdr lh)))))
6002        ;; Adjust assocs.
6003        ((eq mark-type 'tuple)
6004         (set var (setq articles (cdr marks)))
6005         (when (not (listp (cdr (symbol-value var))))
6006           (set var (list (symbol-value var))))
6007         (when (not (listp (cdr articles)))
6008           (setq articles (list articles)))
6009         (while articles
6010           (when (or (not (consp (setq article (pop articles))))
6011                     (< (car article) min)
6012                     (> (car article) max))
6013             (set var (delq article (symbol-value var))))))
6014        ;; Adjust ranges (sloppily).
6015        ((eq mark-type 'range)
6016         (cond
6017          ((eq mark 'seen)
6018           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6019           ;; It should be (seen (NUM1 . NUM2)).
6020           (when (numberp (cddr marks))
6021             (setcdr marks (list (cdr marks))))
6022           (setq articles (cdr marks))
6023           (while (and articles
6024                       (or (and (consp (car articles))
6025                                (> min (cdar articles)))
6026                           (and (numberp (car articles))
6027                                (> min (car articles)))))
6028             (pop articles))
6029           (set var articles))
6030          ((eq mark 'unexist)
6031           (set var (cdr marks)))))))))
6032
6033 (defun gnus-update-missing-marks (missing)
6034   "Go through the list of MISSING articles and remove them from the mark lists."
6035   (when missing
6036     (let (var m)
6037       ;; Go through all types.
6038       (dolist (elem gnus-article-mark-lists)
6039         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6040           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6041           (when (symbol-value var)
6042             ;; This list has articles.  So we delete all missing
6043             ;; articles from it.
6044             (setq m missing)
6045             (while m
6046               (set var (delq (pop m) (symbol-value var))))))))))
6047
6048 (defun gnus-update-marks ()
6049   "Enter the various lists of marked articles into the newsgroup info list."
6050   (let ((types gnus-article-mark-lists)
6051         (info (gnus-get-info gnus-newsgroup-name))
6052         type list newmarked symbol delta-marks)
6053     (when info
6054       ;; Add all marks lists to the list of marks lists.
6055       (while (setq type (pop types))
6056         (setq list (symbol-value
6057                     (setq symbol
6058                           (intern (format "gnus-newsgroup-%s" (car type))))))
6059
6060         (when list
6061           ;; Get rid of the entries of the articles that have the
6062           ;; default score.
6063           (when (and (eq (cdr type) 'score)
6064                      gnus-save-score
6065                      list)
6066             (let* ((arts list)
6067                    (prev (cons nil list))
6068                    (all prev))
6069               (while arts
6070                 (if (or (not (consp (car arts)))
6071                         (= (cdar arts) gnus-summary-default-score))
6072                     (setcdr prev (cdr arts))
6073                   (setq prev arts))
6074                 (setq arts (cdr arts)))
6075               (setq list (cdr all)))))
6076
6077         (when (eq (cdr type) 'seen)
6078           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6079
6080         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6081           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6082
6083         (when (and (gnus-check-backend-function
6084                     'request-set-mark gnus-newsgroup-name)
6085                    (not (gnus-article-unpropagatable-p (cdr type))))
6086           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6087                  ;; Don't do anything about marks for articles we
6088                  ;; didn't actually get any headers for.
6089                  (del
6090                   (gnus-list-range-intersection
6091                    gnus-newsgroup-articles
6092                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6093                  (add
6094                   (gnus-list-range-intersection
6095                    gnus-newsgroup-articles
6096                    (gnus-remove-from-range
6097                     (gnus-copy-sequence list) old))))
6098             (when add
6099               (push (list add 'add (list (cdr type))) delta-marks))
6100             (when del
6101               ;; Don't delete marks from outside the active range.
6102               ;; This shouldn't happen, but is a sanity check.
6103               (setq del (gnus-sorted-range-intersection
6104                          (gnus-active gnus-newsgroup-name) del))
6105               (push (list del 'del (list (cdr type))) delta-marks))))
6106
6107         (when list
6108           (push (cons (cdr type) list) newmarked)))
6109
6110       (when delta-marks
6111         (unless (gnus-check-group gnus-newsgroup-name)
6112           (error "Can't open server for %s" gnus-newsgroup-name))
6113         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6114
6115       ;; Enter these new marks into the info of the group.
6116       (if (nthcdr 3 info)
6117           (setcar (nthcdr 3 info) newmarked)
6118         ;; Add the marks lists to the end of the info.
6119         (when newmarked
6120           (setcdr (nthcdr 2 info) (list newmarked))))
6121
6122       ;; Cut off the end of the info if there's nothing else there.
6123       (let ((i 5))
6124         (while (and (> i 2)
6125                     (not (nth i info)))
6126           (when (nthcdr (decf i) info)
6127             (setcdr (nthcdr i info) nil)))))))
6128
6129 (defun gnus-set-mode-line (where)
6130   "Set the mode line of the article or summary buffers.
6131 If WHERE is `summary', the summary mode line format will be used."
6132   ;; Is this mode line one we keep updated?
6133   (when (and (memq where gnus-updated-mode-lines)
6134              (symbol-value
6135               (intern (format "gnus-%s-mode-line-format-spec" where))))
6136     (let (mode-string)
6137       ;; We evaluate this in the summary buffer since these
6138       ;; variables are buffer-local to that buffer.
6139       (with-current-buffer gnus-summary-buffer
6140         ;; We bind all these variables that are used in the `eval' form
6141         ;; below.
6142         (let* ((mformat (symbol-value
6143                          (intern
6144                           (format "gnus-%s-mode-line-format-spec" where))))
6145                (gnus-tmp-group-name (gnus-mode-string-quote
6146                                      (gnus-group-decoded-name
6147                                       gnus-newsgroup-name)))
6148                (gnus-tmp-article-number (or gnus-current-article 0))
6149                (gnus-tmp-unread gnus-newsgroup-unreads)
6150                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6151                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6152                (gnus-tmp-unread-and-unselected
6153                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6154                             (zerop gnus-tmp-unselected))
6155                        "")
6156                       ((zerop gnus-tmp-unselected)
6157                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6158                       (t (format "{%d(+%d) more}"
6159                                  gnus-tmp-unread-and-unticked
6160                                  gnus-tmp-unselected))))
6161                (gnus-tmp-subject
6162                 (if (and gnus-current-headers
6163                          (vectorp gnus-current-headers))
6164                     (gnus-mode-string-quote
6165                      (mail-header-subject gnus-current-headers))
6166                   ""))
6167                bufname-length max-len
6168                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6169           (setq mode-string (eval mformat))
6170           (setq bufname-length (if (string-match "%b" mode-string)
6171                                    (- (length
6172                                        (buffer-name
6173                                         (if (eq where 'summary)
6174                                             nil
6175                                           (get-buffer gnus-article-buffer))))
6176                                       2)
6177                                  0))
6178           (setq max-len (max 4 (if gnus-mode-non-string-length
6179                                    (- (window-width)
6180                                       gnus-mode-non-string-length
6181                                       bufname-length)
6182                                  (length mode-string))))
6183           ;; We might have to chop a bit of the string off...
6184           (when (> (length mode-string) max-len)
6185             (setq mode-string
6186                   (concat (truncate-string-to-width mode-string (- max-len 3))
6187                           "...")))))
6188       ;; Update the mode line.
6189       (setq mode-line-buffer-identification
6190             (gnus-mode-line-buffer-identification (list mode-string)))
6191       (set-buffer-modified-p t))))
6192
6193 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6194   "Go through the HEADERS list and add all Xrefs to a hash table.
6195 The resulting hash table is returned, or nil if no Xrefs were found."
6196   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6197          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6198          (xref-hashtb (gnus-make-hashtable))
6199          start group entry number xrefs header)
6200     (while headers
6201       (setq header (pop headers))
6202       (when (and (setq xrefs (mail-header-xref header))
6203                  (not (memq (setq number (mail-header-number header))
6204                             unreads)))
6205         (setq start 0)
6206         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6207           (setq start (match-end 0))
6208           (setq group (if prefix
6209                           (concat prefix (substring xrefs (match-beginning 1)
6210                                                     (match-end 1)))
6211                         (substring xrefs (match-beginning 1) (match-end 1))))
6212           (setq number
6213                 (string-to-number (substring xrefs (match-beginning 2)
6214                                           (match-end 2))))
6215           (if (setq entry (gnus-gethash group xref-hashtb))
6216               (setcdr entry (cons number (cdr entry)))
6217             (gnus-sethash group (cons number nil) xref-hashtb)))))
6218     (and start xref-hashtb)))
6219
6220 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6221   "Look through all the headers and mark the Xrefs as read."
6222   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6223         name info xref-hashtb idlist method nth4)
6224     (with-current-buffer gnus-group-buffer
6225       (when (setq xref-hashtb
6226                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6227         (mapatoms
6228          (lambda (group)
6229            (unless (string= from-newsgroup (setq name (symbol-name group)))
6230              (setq idlist (symbol-value group))
6231              ;; Dead groups are not updated.
6232              (and (prog1
6233                       (setq info (gnus-get-info name))
6234                     (when (stringp (setq nth4 (gnus-info-method info)))
6235                       (setq nth4 (gnus-server-to-method nth4))))
6236                   ;; Only do the xrefs if the group has the same
6237                   ;; select method as the group we have just read.
6238                   (or (gnus-methods-equal-p
6239                        nth4 (gnus-find-method-for-group from-newsgroup))
6240                       virtual
6241                       (equal nth4 (setq method (gnus-find-method-for-group
6242                                                 from-newsgroup)))
6243                       (and (equal (car nth4) (car method))
6244                            (equal (nth 1 nth4) (nth 1 method))))
6245                   gnus-use-cross-reference
6246                   (or (not (eq gnus-use-cross-reference t))
6247                       virtual
6248                       ;; Only do cross-references on subscribed
6249                       ;; groups, if that is what is wanted.
6250                       (<= (gnus-info-level info) gnus-level-subscribed))
6251                   (gnus-group-make-articles-read name idlist))))
6252          xref-hashtb)))))
6253
6254 (defun gnus-compute-read-articles (group articles)
6255   (let* ((entry (gnus-group-entry group))
6256          (info (nth 2 entry))
6257          (active (gnus-active group))
6258          ninfo)
6259     (when entry
6260       ;; First peel off all invalid article numbers.
6261       (when active
6262         (let ((ids articles)
6263               id first)
6264           (while (setq id (pop ids))
6265             (when (and first (> id (cdr active)))
6266               ;; We'll end up in this situation in one particular
6267               ;; obscure situation.  If you re-scan a group and get
6268               ;; a new article that is cross-posted to a different
6269               ;; group that has not been re-scanned, you might get
6270               ;; crossposted article that has a higher number than
6271               ;; Gnus believes possible.  So we re-activate this
6272               ;; group as well.  This might mean doing the
6273               ;; crossposting thingy will *increase* the number
6274               ;; of articles in some groups.  Tsk, tsk.
6275               (setq active (or (gnus-activate-group group) active)))
6276             (when (or (> id (cdr active))
6277                       (< id (car active)))
6278               (setq articles (delq id articles))))))
6279       ;; If the read list is nil, we init it.
6280       (if (and active
6281                (null (gnus-info-read info))
6282                (> (car active) 1))
6283           (setq ninfo (cons 1 (1- (car active))))
6284         (setq ninfo (gnus-info-read info)))
6285       ;; Then we add the read articles to the range.
6286       (gnus-add-to-range
6287        ninfo (setq articles (sort articles '<))))))
6288
6289 (defun gnus-group-make-articles-read (group articles)
6290   "Update the info of GROUP to say that ARTICLES are read."
6291   (let* ((num 0)
6292          (entry (gnus-group-entry group))
6293          (info (nth 2 entry))
6294          (active (gnus-active group))
6295          (set-marks
6296           (or gnus-propagate-marks
6297               (gnus-method-option-p
6298                (gnus-find-method-for-group group)
6299                'server-marks)))
6300          range)
6301     (if (not entry)
6302         ;; Group that Gnus doesn't know exists, but still allow the
6303         ;; backend to set marks.
6304         (when set-marks
6305           (gnus-request-set-mark
6306            group (list (list (gnus-compress-sequence (sort articles #'<))
6307                              'add '(read)))))
6308       ;; Normal, subscribed groups.
6309       (setq range (gnus-compute-read-articles group articles))
6310       (with-current-buffer gnus-group-buffer
6311         (gnus-undo-register
6312           `(progn
6313              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6314              (gnus-info-set-read ',info ',(gnus-info-read info))
6315              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6316              (when ,set-marks
6317                (gnus-request-set-mark
6318                 ,group (list (list ',range 'del '(read)))))
6319              (gnus-group-update-group ,group t))))
6320       ;; Add the read articles to the range.
6321       (gnus-info-set-read info range)
6322       (when set-marks
6323         (gnus-request-set-mark group (list (list range 'add '(read)))))
6324       ;; Then we have to re-compute how many unread
6325       ;; articles there are in this group.
6326       (when active
6327         (cond
6328          ((not range)
6329           (setq num (- (1+ (cdr active)) (car active))))
6330          ((not (listp (cdr range)))
6331           (setq num (- (cdr active) (- (1+ (cdr range))
6332                                        (car range)))))
6333          (t
6334           (while range
6335             (if (numberp (car range))
6336                 (setq num (1+ num))
6337               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6338             (setq range (cdr range)))
6339           (setq num (- (cdr active) num))))
6340         ;; Update the number of unread articles.
6341         (setcar entry num)
6342         ;; Update the group buffer.
6343         (unless (gnus-ephemeral-group-p group)
6344           (gnus-group-update-group group t))))))
6345
6346 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6347   (let ((cur nntp-server-buffer)
6348         (dependencies
6349          (or dependencies
6350              (with-current-buffer gnus-summary-buffer
6351                gnus-newsgroup-dependencies)))
6352         headers id end ref number
6353         (mail-parse-charset gnus-newsgroup-charset)
6354         (mail-parse-ignored-charsets
6355          (save-current-buffer (condition-case nil
6356                                   (set-buffer gnus-summary-buffer)
6357                                 (error))
6358                               gnus-newsgroup-ignored-charsets)))
6359     (with-current-buffer nntp-server-buffer
6360       ;; Translate all TAB characters into SPACE characters.
6361       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6362       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6363       (ietf-drums-unfold-fws)
6364       (gnus-run-hooks 'gnus-parse-headers-hook)
6365       (let ((case-fold-search t)
6366             in-reply-to header p lines chars)
6367         (goto-char (point-min))
6368         ;; Search to the beginning of the next header.  Error messages
6369         ;; do not begin with 2 or 3.
6370         (while (re-search-forward "^[23][0-9]+ " nil t)
6371           (setq id nil
6372                 ref nil)
6373           ;; This implementation of this function, with nine
6374           ;; search-forwards instead of the one re-search-forward and
6375           ;; a case (which basically was the old function) is actually
6376           ;; about twice as fast, even though it looks messier.  You
6377           ;; can't have everything, I guess.  Speed and elegance
6378           ;; doesn't always go hand in hand.
6379           (setq
6380            header
6381            (vector
6382             ;; Number.
6383             (prog1
6384                 (setq number (read cur))
6385               (end-of-line)
6386               (setq p (point))
6387               (narrow-to-region (point)
6388                                 (or (and (search-forward "\n.\n" nil t)
6389                                          (- (point) 2))
6390                                     (point))))
6391             ;; Subject.
6392             (progn
6393               (goto-char p)
6394               (if (search-forward "\nsubject:" nil t)
6395                   (funcall gnus-decode-encoded-word-function
6396                            (nnheader-header-value))
6397                 "(none)"))
6398             ;; From.
6399             (progn
6400               (goto-char p)
6401               (if (search-forward "\nfrom:" nil t)
6402                   (funcall gnus-decode-encoded-address-function
6403                            (nnheader-header-value))
6404                 "(nobody)"))
6405             ;; Date.
6406             (progn
6407               (goto-char p)
6408               (if (search-forward "\ndate:" nil t)
6409                   (nnheader-header-value) ""))
6410             ;; Message-ID.
6411             (progn
6412               (goto-char p)
6413               (setq id (if (re-search-forward
6414                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6415                            ;; We do it this way to make sure the Message-ID
6416                            ;; is (somewhat) syntactically valid.
6417                            (buffer-substring (match-beginning 1)
6418                                              (match-end 1))
6419                          ;; If there was no message-id, we just fake one
6420                          ;; to make subsequent routines simpler.
6421                          (nnheader-generate-fake-message-id number))))
6422             ;; References.
6423             (progn
6424               (goto-char p)
6425               (if (search-forward "\nreferences:" nil t)
6426                   (progn
6427                     (setq end (point))
6428                     (prog1
6429                         (nnheader-header-value)
6430                       (setq ref
6431                             (buffer-substring
6432                              (progn
6433                                (end-of-line)
6434                                (search-backward ">" end t)
6435                                (1+ (point)))
6436                              (progn
6437                                (search-backward "<" end t)
6438                                (point))))))
6439                 ;; Get the references from the in-reply-to header if there
6440                 ;; were no references and the in-reply-to header looks
6441                 ;; promising.
6442                 (if (and (search-forward "\nin-reply-to:" nil t)
6443                          (setq in-reply-to (nnheader-header-value))
6444                          (string-match "<[^>]+>" in-reply-to))
6445                     (let (ref2)
6446                       (setq ref (substring in-reply-to (match-beginning 0)
6447                                            (match-end 0)))
6448                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6449                         (setq ref2 (substring in-reply-to (match-beginning 0)
6450                                               (match-end 0)))
6451                         (when (> (length ref2) (length ref))
6452                           (setq ref ref2)))
6453                       ref)
6454                   (setq ref nil))))
6455             ;; Chars.
6456             (progn
6457               (goto-char p)
6458               (if (search-forward "\nchars: " nil t)
6459                   (if (numberp (setq chars (ignore-errors (read cur))))
6460                       chars -1)
6461                 -1))
6462             ;; Lines.
6463             (progn
6464               (goto-char p)
6465               (if (search-forward "\nlines: " nil t)
6466                   (if (numberp (setq lines (ignore-errors (read cur))))
6467                       lines -1)
6468                 -1))
6469             ;; Xref.
6470             (progn
6471               (goto-char p)
6472               (and (search-forward "\nxref:" nil t)
6473                    (nnheader-header-value)))
6474             ;; Extra.
6475             (when gnus-extra-headers
6476               (let ((extra gnus-extra-headers)
6477                     out)
6478                 (while extra
6479                   (goto-char p)
6480                   (when (search-forward
6481                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6482                     (push (cons (car extra) (nnheader-header-value))
6483                           out))
6484                   (pop extra))
6485                 out))))
6486           (when (equal id ref)
6487             (setq ref nil))
6488
6489           (when gnus-alter-header-function
6490             (funcall gnus-alter-header-function header)
6491             (setq id (mail-header-id header)
6492                   ref (gnus-parent-id (mail-header-references header))))
6493
6494           (when (setq header
6495                       (gnus-dependencies-add-header
6496                        header dependencies force-new))
6497             (push header headers))
6498           (goto-char (point-max))
6499           (widen))
6500         (nreverse headers)))))
6501
6502 ;; Goes through the xover lines and returns a list of vectors
6503 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6504                                                   force-new dependencies
6505                                                   group also-fetch-heads)
6506   "Parse the news overview data in the server buffer.
6507 Return a list of headers that match SEQUENCE (see
6508 `nntp-retrieve-headers')."
6509   ;; Get the Xref when the users reads the articles since most/some
6510   ;; NNTP servers do not include Xrefs when using XOVER.
6511   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6512   (let ((mail-parse-charset gnus-newsgroup-charset)
6513         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6514         (cur nntp-server-buffer)
6515         (dependencies (or dependencies gnus-newsgroup-dependencies))
6516         (allp (cond
6517                ((eq gnus-read-all-available-headers t)
6518                 t)
6519                ((and (stringp gnus-read-all-available-headers)
6520                      group)
6521                 (string-match gnus-read-all-available-headers group))
6522                (t
6523                 nil)))
6524         number headers header)
6525     (with-current-buffer nntp-server-buffer
6526       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6527       ;; Allow the user to mangle the headers before parsing them.
6528       (gnus-run-hooks 'gnus-parse-headers-hook)
6529       (goto-char (point-min))
6530       (gnus-parse-without-error
6531         (while (and (or sequence allp)
6532                     (not (eobp)))
6533           (setq number (read cur))
6534           (when (not allp)
6535             (while (and sequence
6536                         (< (car sequence) number))
6537               (setq sequence (cdr sequence))))
6538           (when (and (or allp
6539                          (and sequence
6540                               (eq number (car sequence))))
6541                      (progn
6542                        (setq sequence (cdr sequence))
6543                        (setq header (inline
6544                                       (gnus-nov-parse-line
6545                                        number dependencies force-new)))))
6546             (push header headers))
6547           (forward-line 1)))
6548       ;; A common bug in inn is that if you have posted an article and
6549       ;; then retrieves the active file, it will answer correctly --
6550       ;; the new article is included.  However, a NOV entry for the
6551       ;; article may not have been generated yet, so this may fail.
6552       ;; We work around this problem by retrieving the last few
6553       ;; headers using HEAD.
6554       (if (or (not also-fetch-heads)
6555               (not sequence))
6556           ;; We (probably) got all the headers.
6557           (nreverse headers)
6558         (let ((gnus-nov-is-evil t))
6559           (nconc
6560            (nreverse headers)
6561            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6562              (gnus-get-newsgroup-headers))))))))
6563
6564 (defun gnus-article-get-xrefs ()
6565   "Fill in the Xref value in `gnus-current-headers', if necessary.
6566 This is meant to be called in `gnus-article-internal-prepare-hook'."
6567   (let ((headers (with-current-buffer gnus-summary-buffer
6568                    gnus-current-headers)))
6569     (or (not gnus-use-cross-reference)
6570         (not headers)
6571         (and (mail-header-xref headers)
6572              (not (string= (mail-header-xref headers) "")))
6573         (let ((case-fold-search t)
6574               xref)
6575           (save-restriction
6576             (nnheader-narrow-to-headers)
6577             (goto-char (point-min))
6578             (when (or (and (not (eobp))
6579                            (eq (downcase (char-after)) ?x)
6580                            (looking-at "Xref:"))
6581                       (search-forward "\nXref:" nil t))
6582               (goto-char (1+ (match-end 0)))
6583               (setq xref (buffer-substring (point) (point-at-eol)))
6584               (mail-header-set-xref headers xref)))))))
6585
6586 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6587   "Find article ID and insert the summary line for that article.
6588 OLD-HEADER can either be a header or a line number to insert
6589 the subject line on.
6590 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6591 and OLD-HEADER will be used when the summary line is inserted,
6592 too, instead of trying to fetch new headers."
6593   (let* ((line (and (numberp old-header) old-header))
6594          (old-header (and (vectorp old-header) old-header))
6595          (header (cond ((and old-header use-old-header)
6596                         old-header)
6597                        ((and (numberp id)
6598                              (gnus-number-to-header id))
6599                         (gnus-number-to-header id))
6600                        (t
6601                         (gnus-read-header id))))
6602          (number (and (numberp id) id))
6603          d)
6604     (when header
6605       ;; Rebuild the thread that this article is part of and go to the
6606       ;; article we have fetched.
6607       (when (and (not gnus-show-threads)
6608                  old-header)
6609         (when (and number
6610                    (setq d (gnus-data-find (mail-header-number old-header))))
6611           (goto-char (gnus-data-pos d))
6612           (gnus-data-remove
6613            number
6614            (- (point-at-bol)
6615               (prog1
6616                   (1+ (point-at-eol))
6617                 (gnus-delete-line))))))
6618       ;; Remove list identifiers from subject.
6619       (let ((gnus-newsgroup-headers (list header)))
6620         (gnus-summary-remove-list-identifiers))
6621       (when old-header
6622         (mail-header-set-number header (mail-header-number old-header)))
6623       (setq gnus-newsgroup-sparse
6624             (delq (setq number (mail-header-number header))
6625                   gnus-newsgroup-sparse))
6626       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6627       (push number gnus-newsgroup-limit)
6628       (gnus-rebuild-thread (mail-header-id header) line)
6629       (gnus-summary-goto-subject number nil t))
6630     (when (and (numberp number)
6631                (> number 0))
6632       ;; We have to update the boundaries even if we can't fetch the
6633       ;; article if ID is a number -- so that the next `P' or `N'
6634       ;; command will fetch the previous (or next) article even
6635       ;; if the one we tried to fetch this time has been canceled.
6636       (when (> number gnus-newsgroup-end)
6637         (setq gnus-newsgroup-end number))
6638       (when (< number gnus-newsgroup-begin)
6639         (setq gnus-newsgroup-begin number))
6640       (setq gnus-newsgroup-unselected
6641             (delq number gnus-newsgroup-unselected)))
6642     ;; Report back a success?
6643     (and header (mail-header-number header))))
6644
6645 ;;; Process/prefix in the summary buffer
6646
6647 (defun gnus-summary-work-articles (n)
6648   "Return a list of articles to be worked upon.
6649 The prefix argument, the list of process marked articles, and the
6650 current article will be taken into consideration."
6651   (with-current-buffer gnus-summary-buffer
6652     (cond
6653      (n
6654       ;; A numerical prefix has been given.
6655       (setq n (prefix-numeric-value n))
6656       (let ((backward (< n 0))
6657             (n (abs (prefix-numeric-value n)))
6658             articles article)
6659         (save-excursion
6660           (while
6661               (and (> n 0)
6662                    (push (setq article (gnus-summary-article-number))
6663                          articles)
6664                    (if backward
6665                        (gnus-summary-find-prev nil article)
6666                      (gnus-summary-find-next nil article)))
6667             (decf n)))
6668         (nreverse articles)))
6669      ((and (gnus-region-active-p) (mark))
6670       (message "region active")
6671       ;; Work on the region between point and mark.
6672       (let ((max (max (point) (mark)))
6673             articles article)
6674         (save-excursion
6675           (goto-char (min (point) (mark)))
6676           (while
6677               (and
6678                (push (setq article (gnus-summary-article-number)) articles)
6679                (gnus-summary-find-next nil article)
6680                (< (point) max)))
6681           (nreverse articles))))
6682      (gnus-newsgroup-processable
6683       ;; There are process-marked articles present.
6684       ;; Save current state.
6685       (gnus-summary-save-process-mark)
6686       ;; Return the list.
6687       (reverse gnus-newsgroup-processable))
6688      (t
6689       ;; Just return the current article.
6690       (list (gnus-summary-article-number))))))
6691
6692 (defmacro gnus-summary-iterate (arg &rest forms)
6693   "Iterate over the process/prefixed articles and do FORMS.
6694 ARG is the interactive prefix given to the command.  FORMS will be
6695 executed with point over the summary line of the articles."
6696   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6697     `(let ((,articles (gnus-summary-work-articles ,arg)))
6698        (while ,articles
6699          (gnus-summary-goto-subject (car ,articles))
6700          ,@forms
6701          (pop ,articles)))))
6702
6703 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6704 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6705
6706 (defun gnus-summary-save-process-mark ()
6707   "Push the current set of process marked articles on the stack."
6708   (interactive)
6709   (push (copy-sequence gnus-newsgroup-processable)
6710         gnus-newsgroup-process-stack))
6711
6712 (defun gnus-summary-kill-process-mark ()
6713   "Push the current set of process marked articles on the stack and unmark."
6714   (interactive)
6715   (gnus-summary-save-process-mark)
6716   (gnus-summary-unmark-all-processable))
6717
6718 (defun gnus-summary-yank-process-mark ()
6719   "Pop the last process mark state off the stack and restore it."
6720   (interactive)
6721   (unless gnus-newsgroup-process-stack
6722     (error "Empty mark stack"))
6723   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6724
6725 (defun gnus-summary-process-mark-set (set)
6726   "Make SET into the current process marked articles."
6727   (gnus-summary-unmark-all-processable)
6728   (mapc 'gnus-summary-set-process-mark set))
6729
6730 ;;; Searching and stuff
6731
6732 (defun gnus-summary-search-group (&optional backward use-level)
6733   "Search for next unread newsgroup.
6734 If optional argument BACKWARD is non-nil, search backward instead."
6735   (with-current-buffer gnus-group-buffer
6736     (when (gnus-group-search-forward
6737            backward nil (if use-level (gnus-group-group-level) nil))
6738       (gnus-group-group-name))))
6739
6740 (defun gnus-summary-best-group (&optional exclude-group)
6741   "Find the name of the best unread group.
6742 If EXCLUDE-GROUP, do not go to this group."
6743   (with-current-buffer gnus-group-buffer
6744     (save-excursion
6745       (gnus-group-best-unread-group exclude-group))))
6746
6747 (defun gnus-summary-find-next (&optional unread article backward)
6748   (if backward
6749       (gnus-summary-find-prev unread article)
6750     (let* ((dummy (gnus-summary-article-intangible-p))
6751            (article (or article (gnus-summary-article-number)))
6752            (data (gnus-data-find-list article))
6753            result)
6754       (when (and (not dummy)
6755                  (or (not gnus-summary-check-current)
6756                      (not unread)
6757                      (not (gnus-data-unread-p (car data)))))
6758         (setq data (cdr data)))
6759       (when (setq result
6760                   (if unread
6761                       (progn
6762                         (while data
6763                           (unless (memq (gnus-data-number (car data))
6764                                         (cond
6765                                          ((eq gnus-auto-goto-ignores
6766                                               'always-undownloaded)
6767                                           gnus-newsgroup-undownloaded)
6768                                          (gnus-plugged
6769                                           nil)
6770                                          ((eq gnus-auto-goto-ignores
6771                                               'unfetched)
6772                                           gnus-newsgroup-unfetched)
6773                                          ((eq gnus-auto-goto-ignores
6774                                               'undownloaded)
6775                                           gnus-newsgroup-undownloaded)))
6776                             (when (gnus-data-unread-p (car data))
6777                               (setq result (car data)
6778                                     data nil)))
6779                           (setq data (cdr data)))
6780                         result)
6781                     (car data)))
6782         (goto-char (gnus-data-pos result))
6783         (gnus-data-number result)))))
6784
6785 (defun gnus-summary-find-prev (&optional unread article)
6786   (let* ((eobp (eobp))
6787          (article (or article (gnus-summary-article-number)))
6788          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6789          result)
6790     (when (and (not eobp)
6791                (or (not gnus-summary-check-current)
6792                    (not unread)
6793                    (not (gnus-data-unread-p (car data)))))
6794       (setq data (cdr data)))
6795     (when (setq result
6796                 (if unread
6797                     (progn
6798                       (while data
6799                         (unless (memq (gnus-data-number (car data))
6800                                       (cond
6801                                        ((eq gnus-auto-goto-ignores
6802                                             'always-undownloaded)
6803                                         gnus-newsgroup-undownloaded)
6804                                        (gnus-plugged
6805                                         nil)
6806                                        ((eq gnus-auto-goto-ignores
6807                                             'unfetched)
6808                                         gnus-newsgroup-unfetched)
6809                                        ((eq gnus-auto-goto-ignores
6810                                             'undownloaded)
6811                                         gnus-newsgroup-undownloaded)))
6812                           (when (gnus-data-unread-p (car data))
6813                             (setq result (car data)
6814                                   data nil)))
6815                         (setq data (cdr data)))
6816                       result)
6817                   (car data)))
6818       (goto-char (gnus-data-pos result))
6819       (gnus-data-number result))))
6820
6821 (defun gnus-summary-find-subject (subject &optional unread backward article)
6822   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6823          (article (or article (gnus-summary-article-number)))
6824          (articles (gnus-data-list backward))
6825          (arts (gnus-data-find-list article articles))
6826          result)
6827     (when (or (not gnus-summary-check-current)
6828               (not unread)
6829               (not (gnus-data-unread-p (car arts))))
6830       (setq arts (cdr arts)))
6831     (while arts
6832       (and (or (not unread)
6833                (gnus-data-unread-p (car arts)))
6834            (vectorp (gnus-data-header (car arts)))
6835            (gnus-subject-equal
6836             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6837            (setq result (car arts)
6838                  arts nil))
6839       (setq arts (cdr arts)))
6840     (and result
6841          (goto-char (gnus-data-pos result))
6842          (gnus-data-number result))))
6843
6844 (defun gnus-summary-search-forward (&optional unread subject backward)
6845   "Search forward for an article.
6846 If UNREAD, look for unread articles.  If SUBJECT, look for
6847 articles with that subject.  If BACKWARD, search backward instead."
6848   (cond (subject (gnus-summary-find-subject subject unread backward))
6849         (backward (gnus-summary-find-prev unread))
6850         (t (gnus-summary-find-next unread))))
6851
6852 (defun gnus-recenter (&optional n)
6853   "Center point in window and redisplay frame.
6854 Also do horizontal recentering."
6855   (interactive "P")
6856   (when (and gnus-auto-center-summary
6857              (not (eq gnus-auto-center-summary 'vertical)))
6858     (gnus-horizontal-recenter))
6859   (if (fboundp 'recenter-top-bottom)
6860       (recenter-top-bottom n)
6861     (recenter n)))
6862
6863 (put 'gnus-recenter 'isearch-scroll t)
6864
6865 (defun gnus-forward-line-ignore-invisible (n)
6866   "Move N lines forward (backward if N is negative).
6867 Like forward-line, but skip over (and don't count) invisible lines."
6868   (let (done)
6869     (while (and (> n 0) (not done))
6870       ;; If the following character is currently invisible,
6871       ;; skip all characters with that same `invisible' property value.
6872       (while (gnus-invisible-p (point))
6873         (goto-char (gnus-next-char-property-change (point))))
6874       (forward-line 1)
6875       (if (eobp)
6876           (setq done t)
6877         (setq n (1- n))))
6878     (while (and (< n 0) (not done))
6879       (forward-line -1)
6880       (if (bobp) (setq done t)
6881         (setq n (1+ n))
6882         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6883           (goto-char (gnus-previous-char-property-change (point))))))))
6884
6885 (defun gnus-summary-recenter ()
6886   "Center point in the summary window.
6887 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6888 displayed, no centering will be performed."
6889   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6890   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6891   (interactive)
6892   ;; The user has to want it.
6893   (when gnus-auto-center-summary
6894     (let* ((top (cond ((< (window-height) 4) 0)
6895                       ((< (window-height) 7) 1)
6896                       (t (if (numberp gnus-auto-center-summary)
6897                              gnus-auto-center-summary
6898                            (/ (1- (window-height)) 2)))))
6899            (height (1- (window-height)))
6900            (bottom (save-excursion
6901                      (goto-char (point-max))
6902                      (gnus-forward-line-ignore-invisible (- height))
6903                      (point)))
6904            (window (get-buffer-window (current-buffer))))
6905       (when (get-buffer-window gnus-article-buffer)
6906         ;; Only do recentering when the article buffer is displayed,
6907         ;; Set the window start to either `bottom', which is the biggest
6908         ;; possible valid number, or the second line from the top,
6909         ;; whichever is the least.
6910         (let ((top-pos (save-excursion
6911                          (gnus-forward-line-ignore-invisible (- top))
6912                          (point))))
6913           (if (> bottom top-pos)
6914               ;; Keep the second line from the top visible
6915               (set-window-start window top-pos)
6916             ;; Try to keep the bottom line visible; if it's partially
6917             ;; obscured, either scroll one more line to make it fully
6918             ;; visible, or revert to using TOP-POS.
6919             (save-excursion
6920               (goto-char (point-max))
6921               (gnus-forward-line-ignore-invisible -1)
6922               (let ((last-line-start (point)))
6923                 (goto-char bottom)
6924                 (set-window-start window (point) t)
6925                 (when (not (pos-visible-in-window-p last-line-start window))
6926                   (gnus-forward-line-ignore-invisible 1)
6927                   (set-window-start window (min (point) top-pos) t)))))))
6928       ;; Do horizontal recentering while we're at it.
6929       (when (and (get-buffer-window (current-buffer) t)
6930                  (not (eq gnus-auto-center-summary 'vertical)))
6931         (let ((selected (selected-window)))
6932           (select-window (get-buffer-window (current-buffer) t))
6933           (gnus-summary-position-point)
6934           (gnus-horizontal-recenter)
6935           (select-window selected))))))
6936
6937 (defun gnus-summary-jump-to-group (newsgroup)
6938   "Move point to NEWSGROUP in group mode buffer."
6939   ;; Keep update point of group mode buffer if visible.
6940   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6941       (save-window-excursion
6942         ;; Take care of tree window mode.
6943         (when (get-buffer-window gnus-group-buffer)
6944           (pop-to-buffer gnus-group-buffer))
6945         (gnus-group-jump-to-group newsgroup))
6946     (save-excursion
6947       ;; Take care of tree window mode.
6948       (if (get-buffer-window gnus-group-buffer 0)
6949           (pop-to-buffer gnus-group-buffer)
6950         (set-buffer gnus-group-buffer))
6951       (gnus-group-jump-to-group newsgroup))))
6952
6953 ;; This function returns a list of article numbers based on the
6954 ;; difference between the ranges of read articles in this group and
6955 ;; the range of active articles.
6956 (defun gnus-list-of-unread-articles (group)
6957   (let* ((read (gnus-info-read (gnus-get-info group)))
6958          (active (or (gnus-active group) (gnus-activate-group group)))
6959          (last (or (cdr active)
6960                    (error "Group %s couldn't be activated " group)))
6961          (bottom (if gnus-newsgroup-maximum-articles
6962                      (max (car active)
6963                           (- last gnus-newsgroup-maximum-articles -1))
6964                    (car active)))
6965          first nlast unread)
6966     ;; If none are read, then all are unread.
6967     (if (not read)
6968         (setq first bottom)
6969       ;; If the range of read articles is a single range, then the
6970       ;; first unread article is the article after the last read
6971       ;; article.  Sounds logical, doesn't it?
6972       (if (and (not (listp (cdr read)))
6973                (or (< (car read) bottom)
6974                    (progn (setq read (list read))
6975                           nil)))
6976           (setq first (max bottom (1+ (cdr read))))
6977         ;; `read' is a list of ranges.
6978         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6979                                   (caar read)))
6980                   1)
6981           (setq first bottom))
6982         (while read
6983           (when first
6984             (while (< first nlast)
6985               (setq unread (cons first unread)
6986                     first (1+ first))))
6987           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6988           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6989           (setq read (cdr read)))))
6990     ;; And add the last unread articles.
6991     (while (<= first last)
6992       (setq unread (cons first unread)
6993             first (1+ first)))
6994     ;; Return the list of unread articles.
6995     (delq 0 (nreverse unread))))
6996
6997 (defun gnus-list-of-read-articles (group)
6998   "Return a list of unread, unticked and non-dormant articles."
6999   (let* ((info (gnus-get-info group))
7000          (marked (gnus-info-marks info))
7001          (active (gnus-active group)))
7002     (and info active
7003          (gnus-list-range-difference
7004           (gnus-list-range-difference
7005            (gnus-sorted-complement
7006             (gnus-uncompress-range
7007              (if gnus-newsgroup-maximum-articles
7008                  (cons (max (car active)
7009                             (- (cdr active)
7010                                gnus-newsgroup-maximum-articles
7011                                -1))
7012                        (cdr active))
7013                active))
7014             (gnus-list-of-unread-articles group))
7015            (cdr (assq 'dormant marked)))
7016           (cdr (assq 'tick marked))))))
7017
7018 ;; This function returns a sequence of article numbers based on the
7019 ;; difference between the ranges of read articles in this group and
7020 ;; the range of active articles.
7021 (defun gnus-sequence-of-unread-articles (group)
7022   (let* ((read (gnus-info-read (gnus-get-info group)))
7023          (active (or (gnus-active group) (gnus-activate-group group)))
7024          (last (cdr active))
7025          (bottom (if gnus-newsgroup-maximum-articles
7026                      (max (car active)
7027                           (- last gnus-newsgroup-maximum-articles -1))
7028                    (car active)))
7029          first nlast unread)
7030     ;; If none are read, then all are unread.
7031     (if (not read)
7032         (setq first bottom)
7033       ;; If the range of read articles is a single range, then the
7034       ;; first unread article is the article after the last read
7035       ;; article.  Sounds logical, doesn't it?
7036       (if (and (not (listp (cdr read)))
7037                (or (< (car read) bottom)
7038                    (progn (setq read (list read))
7039                           nil)))
7040           (setq first (max bottom (1+ (cdr read))))
7041         ;; `read' is a list of ranges.
7042         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7043                                   (caar read)))
7044                   1)
7045           (setq first bottom))
7046         (while read
7047           (when first
7048             (push (cons first nlast) unread))
7049           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7050           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7051           (setq read (cdr read)))))
7052     ;; And add the last unread articles.
7053     (cond ((not (and first last))
7054            nil)
7055           ((< first last)
7056            (push (cons first last) unread))
7057           ((= first last)
7058            (push first unread)))
7059     ;; Return the sequence of unread articles.
7060     (delq 0 (nreverse unread))))
7061
7062 ;; Various summary commands
7063
7064 (defun gnus-summary-select-article-buffer ()
7065   "Reconfigure windows to show the article buffer.
7066 If `gnus-widen-article-window' is set, show only the article
7067 buffer."
7068   (interactive)
7069   (if (not (gnus-buffer-live-p gnus-article-buffer))
7070       (error "There is no article buffer for this summary buffer")
7071     (unless (get-buffer-window gnus-article-buffer)
7072       (gnus-summary-show-article))
7073     (gnus-configure-windows
7074      (if gnus-widen-article-window
7075          'only-article
7076        'article)
7077      t)
7078     (select-window (get-buffer-window gnus-article-buffer))))
7079
7080 (defun gnus-summary-universal-argument (arg)
7081   "Perform any operation on all articles that are process/prefixed."
7082   (interactive "P")
7083   (let ((articles (gnus-summary-work-articles arg))
7084         func article)
7085     (if (eq
7086          (setq
7087           func
7088           (key-binding
7089            (read-key-sequence
7090             (substitute-command-keys
7091              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7092          'undefined)
7093         (gnus-error 1 "Undefined key")
7094       (save-excursion
7095         (while articles
7096           (gnus-summary-goto-subject (setq article (pop articles)))
7097           (let (gnus-newsgroup-processable)
7098             (command-execute func))
7099           (gnus-summary-remove-process-mark article)))))
7100   (gnus-summary-position-point))
7101
7102 (defun gnus-summary-toggle-truncation (&optional arg)
7103   "Toggle truncation of summary lines.
7104 With ARG, turn line truncation on if ARG is positive."
7105   (interactive "P")
7106   (setq truncate-lines
7107         (if (null arg) (not truncate-lines)
7108           (> (prefix-numeric-value arg) 0)))
7109   (redraw-display))
7110
7111 (defun gnus-summary-find-for-reselect ()
7112   "Return the number of an article to stay on across a reselect.
7113 The current article is considered, then following articles, then previous
7114 articles.  An article is sought which is not canceled and isn't a temporary
7115 insertion from another group.  If there's no such then return a dummy 0."
7116   (let (found)
7117     (dolist (rev '(nil t))
7118       (unless found      ; don't demand the reverse list if we don't need it
7119         (let ((data (gnus-data-find-list
7120                      (gnus-summary-article-number) (gnus-data-list rev))))
7121           (while (and data (not found))
7122             (if (and (< 0 (gnus-data-number (car data)))
7123                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7124                 (setq found (gnus-data-number (car data))))
7125             (setq data (cdr data))))))
7126     (or found 0)))
7127
7128 (defun gnus-summary-reselect-current-group (&optional all rescan)
7129   "Exit and then reselect the current newsgroup.
7130 The prefix argument ALL means to select all articles."
7131   (interactive "P")
7132   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7133     (error "Ephemeral groups can't be reselected"))
7134   (let ((current-subject (gnus-summary-find-for-reselect))
7135         (group gnus-newsgroup-name))
7136     (setq gnus-newsgroup-begin nil)
7137     (gnus-summary-exit nil 'leave-hidden)
7138     ;; We have to adjust the point of group mode buffer because
7139     ;; point was moved to the next unread newsgroup by exiting.
7140     (gnus-summary-jump-to-group group)
7141     (when rescan
7142       (save-excursion
7143         (gnus-group-get-new-news-this-group 1)))
7144     (gnus-group-read-group all t)
7145     (gnus-summary-goto-subject current-subject nil t)))
7146
7147 (defun gnus-summary-rescan-group (&optional all)
7148   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7149   (interactive "P")
7150   (let ((config gnus-current-window-configuration))
7151     (gnus-summary-reselect-current-group all t)
7152     (gnus-configure-windows config)
7153     (when (eq config 'article)
7154       (gnus-summary-select-article))))
7155
7156 (defun gnus-summary-update-info (&optional non-destructive)
7157   (save-excursion
7158     (let ((group gnus-newsgroup-name))
7159       (when group
7160         (when gnus-newsgroup-kill-headers
7161           (setq gnus-newsgroup-killed
7162                 (gnus-compress-sequence
7163                  (gnus-sorted-union
7164                   (gnus-list-range-intersection
7165                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7166                   gnus-newsgroup-unreads)
7167                  t)))
7168         (unless (listp (cdr gnus-newsgroup-killed))
7169           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7170         (let ((headers gnus-newsgroup-headers)
7171               (ephemeral-p (gnus-ephemeral-group-p group))
7172               info)
7173           (unless ephemeral-p
7174             (setq info (copy-sequence (gnus-get-info group))
7175                   info (delq (gnus-info-params info) info)))
7176           ;; Set the new ranges of read articles.
7177           (with-current-buffer gnus-group-buffer
7178             (gnus-undo-force-boundary))
7179           (gnus-update-read-articles
7180            group (gnus-sorted-union
7181                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7182           ;; Set the current article marks.
7183           (let ((gnus-newsgroup-scored
7184                  (if (and (not gnus-save-score)
7185                           (not non-destructive))
7186                      nil
7187                    gnus-newsgroup-scored)))
7188             (save-excursion
7189               (gnus-update-marks)))
7190           ;; Do the cross-ref thing.
7191           (when gnus-use-cross-reference
7192             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7193           ;; Do not switch windows but change the buffer to work.
7194           (set-buffer gnus-group-buffer)
7195           (unless ephemeral-p
7196             (gnus-group-update-group
7197              group nil
7198              (equal info
7199                     (setq info (copy-sequence (gnus-get-info group))
7200                           info (delq (gnus-info-params info) info))))))))))
7201
7202 (defun gnus-summary-save-newsrc (&optional force)
7203   "Save the current number of read/marked articles in the dribble buffer.
7204 The dribble buffer will then be saved.
7205 If FORCE (the prefix), also save the .newsrc file(s)."
7206   (interactive "P")
7207   (gnus-summary-update-info t)
7208   (if force
7209       (gnus-save-newsrc-file)
7210     (gnus-dribble-save)))
7211
7212 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7213
7214 (defun gnus-summary-exit (&optional temporary leave-hidden)
7215   "Exit reading current newsgroup, and then return to group selection mode.
7216 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7217   (interactive)
7218   (gnus-set-global-variables)
7219   (when (gnus-buffer-live-p gnus-article-buffer)
7220     (with-current-buffer gnus-article-buffer
7221       (mm-destroy-parts gnus-article-mime-handles)
7222       ;; Set it to nil for safety reason.
7223       (setq gnus-article-mime-handle-alist nil)
7224       (setq gnus-article-mime-handles nil)))
7225   (gnus-kill-save-kill-buffer)
7226   (gnus-async-halt-prefetch)
7227   (let* ((group gnus-newsgroup-name)
7228          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7229          (gnus-group-is-exiting-p t)
7230          (article-buffer gnus-article-buffer)
7231          (original-article-buffer gnus-original-article-buffer)
7232          (mode major-mode)
7233          (group-point nil)
7234          (buf (current-buffer))
7235          ;; `gnus-single-article-buffer' is nil buffer-locally in
7236          ;; ephemeral group of which summary buffer will be killed,
7237          ;; but the global value may be non-nil.
7238          (single-article-buffer gnus-single-article-buffer))
7239     (unless quit-config
7240       ;; Do adaptive scoring, and possibly save score files.
7241       (when gnus-newsgroup-adaptive
7242         (gnus-score-adaptive))
7243       (when gnus-use-scoring
7244         (gnus-score-save)))
7245     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7246     (when gnus-use-cache
7247       (gnus-cache-possibly-remove-articles)
7248       (gnus-cache-save-buffers))
7249     (gnus-async-prefetch-remove-group group)
7250     (when gnus-suppress-duplicates
7251       (gnus-dup-enter-articles))
7252     (when gnus-use-trees
7253       (gnus-tree-close group))
7254     (when gnus-use-cache
7255       (gnus-cache-write-active))
7256     ;; Remove entries for this group.
7257     (nnmail-purge-split-history (gnus-group-real-name group))
7258     ;; Make all changes in this group permanent.
7259     (unless quit-config
7260       (gnus-run-hooks 'gnus-exit-group-hook)
7261       (gnus-summary-update-info))
7262     (gnus-close-group group)
7263     ;; Make sure where we were, and go to next newsgroup.
7264     (set-buffer gnus-group-buffer)
7265     (unless quit-config
7266       (gnus-group-jump-to-group group))
7267     (gnus-run-hooks 'gnus-summary-exit-hook)
7268     (unless (or quit-config
7269                 (not gnus-summary-next-group-on-exit)
7270                 ;; If this group has disappeared from the summary
7271                 ;; buffer, don't skip forwards.
7272                 (not (string= group (gnus-group-group-name))))
7273       (gnus-group-next-unread-group 1))
7274     (setq group-point (point))
7275     (if temporary
7276         nil                             ;Nothing to do.
7277       (set-buffer buf)
7278       (if (not gnus-kill-summary-on-exit)
7279           (progn
7280             (gnus-deaden-summary)
7281             (setq mode nil))
7282         (when (get-buffer gnus-article-buffer)
7283           (bury-buffer gnus-article-buffer))
7284         ;; Return to group mode buffer.
7285         (when (eq mode 'gnus-summary-mode)
7286           (gnus-kill-buffer buf)))
7287
7288       (setq gnus-current-select-method gnus-select-method)
7289       (set-buffer gnus-group-buffer)
7290       (if quit-config
7291           (gnus-handle-ephemeral-exit quit-config)
7292         (goto-char group-point)
7293         ;; If gnus-group-buffer is already displayed, make sure we also move
7294         ;; the cursor in the window that displays it.
7295         (let ((win (get-buffer-window (current-buffer) 0)))
7296           (if win (set-window-point win (point))))
7297         (unless leave-hidden
7298           (gnus-configure-windows 'group 'force)))
7299
7300       ;; If we have several article buffers, we kill them at exit.
7301       (unless single-article-buffer
7302         (when (gnus-buffer-live-p article-buffer)
7303           (with-current-buffer article-buffer
7304             ;; Don't kill sticky article buffers
7305             (unless (eq major-mode 'gnus-sticky-article-mode)
7306               (gnus-kill-buffer article-buffer)
7307               (setq gnus-article-current nil))))
7308         (gnus-kill-buffer original-article-buffer))
7309
7310       ;; Clear the current group name.
7311       (unless quit-config
7312         (setq gnus-newsgroup-name nil)))))
7313
7314 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7315 (defun gnus-summary-exit-no-update (&optional no-questions)
7316   "Quit reading current newsgroup without updating read article info."
7317   (interactive)
7318   (let* ((group gnus-newsgroup-name)
7319          (gnus-group-is-exiting-p t)
7320          (gnus-group-is-exiting-without-update-p t)
7321          (quit-config (gnus-group-quit-config group)))
7322     (when (or no-questions
7323               gnus-expert-user
7324               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7325       (gnus-async-halt-prefetch)
7326       (run-hooks 'gnus-summary-prepare-exit-hook)
7327       (when (gnus-buffer-live-p gnus-article-buffer)
7328         (with-current-buffer gnus-article-buffer
7329           (gnus-article-stop-animations)
7330           (mm-destroy-parts gnus-article-mime-handles)
7331           ;; Set it to nil for safety reason.
7332           (setq gnus-article-mime-handle-alist nil)
7333           (setq gnus-article-mime-handles nil)))
7334       ;; If we have several article buffers, we kill them at exit.
7335       (unless gnus-single-article-buffer
7336         (gnus-kill-buffer gnus-article-buffer)
7337         (gnus-kill-buffer gnus-original-article-buffer)
7338         (setq gnus-article-current nil))
7339       ;; Return to the group buffer.
7340       (if (not gnus-kill-summary-on-exit)
7341           (progn
7342             (gnus-deaden-summary)
7343             (gnus-configure-windows 'group 'force))
7344         (gnus-configure-windows 'group 'force)
7345         (gnus-close-group group)
7346         (gnus-kill-buffer gnus-summary-buffer))
7347       (unless gnus-single-article-buffer
7348         (setq gnus-article-current nil))
7349       (when gnus-use-trees
7350         (gnus-tree-close group))
7351       (gnus-async-prefetch-remove-group group)
7352       (when (get-buffer gnus-article-buffer)
7353         (bury-buffer gnus-article-buffer))
7354       ;; Clear the current group name.
7355       (setq gnus-newsgroup-name nil)
7356       (unless (gnus-ephemeral-group-p group)
7357         (gnus-group-update-group group nil t))
7358       (when (equal (gnus-group-group-name) group)
7359         (gnus-group-next-unread-group 1))
7360       (when quit-config
7361         (gnus-handle-ephemeral-exit quit-config)))))
7362
7363 (defun gnus-handle-ephemeral-exit (quit-config)
7364   "Handle movement when leaving an ephemeral group.
7365 The state which existed when entering the ephemeral is reset."
7366   (if (not (buffer-live-p (car quit-config)))
7367       (gnus-configure-windows 'group 'force)
7368     (set-buffer (car quit-config))
7369     (unless (eq (cdr quit-config) 'group)
7370       (setq gnus-current-select-method
7371             (gnus-find-method-for-group gnus-newsgroup-name)))
7372     (cond ((eq major-mode 'gnus-summary-mode)
7373            (gnus-set-global-variables))
7374           ((eq major-mode 'gnus-article-mode)
7375            (save-current-buffer
7376              ;; The `gnus-summary-buffer' variable may point
7377              ;; to the old summary buffer when using a single
7378              ;; article buffer.
7379              (unless (gnus-buffer-live-p gnus-summary-buffer)
7380                (set-buffer gnus-group-buffer))
7381              (set-buffer gnus-summary-buffer)
7382              (gnus-set-global-variables))))
7383     (if (or (eq (cdr quit-config) 'article)
7384             (eq (cdr quit-config) 'pick))
7385         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7386             (gnus-configure-windows 'pick 'force)
7387           (gnus-configure-windows (cdr quit-config) 'force))
7388       (gnus-configure-windows (cdr quit-config) 'force))
7389     (when (eq major-mode 'gnus-summary-mode)
7390       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7391                                                      next-unread-noselect))
7392           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7393                                   'next-noselect)
7394                               (gnus-summary-next-subject 1 nil t))
7395                              ((eq gnus-auto-select-on-ephemeral-exit
7396                                   'next-unread-noselect)
7397                               (gnus-summary-next-subject 1 t t))))
7398             ;; Hide the article buffer which displays the article different
7399             ;; from the one that the cursor points to in the summary buffer.
7400             (gnus-configure-windows 'summary 'force))
7401         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7402                (gnus-summary-next-subject 1))
7403               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7404                (gnus-summary-next-subject 1 t))))
7405       (gnus-summary-recenter)
7406       (gnus-summary-position-point))))
7407
7408 ;;; Dead summaries.
7409
7410 (defvar gnus-dead-summary-mode-map
7411   (let ((map (make-keymap)))
7412     (suppress-keymap map)
7413     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7414     (dolist (key '("\C-d" "\r" "\177" [delete]))
7415       (define-key map key 'gnus-summary-wake-up-the-dead))
7416     (dolist (key '("q" "Q"))
7417       (define-key map key 'bury-buffer))
7418     map))
7419
7420 (define-minor-mode gnus-dead-summary-mode
7421   "Minor mode for Gnus summary buffers."
7422   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7423   (unless (derived-mode-p 'gnus-summary-mode)
7424     (setq gnus-dead-summary-mode nil)))
7425
7426 (defun gnus-deaden-summary ()
7427   "Make the current summary buffer into a dead summary buffer."
7428   ;; Kill any previous dead summary buffer.
7429   (when (and gnus-dead-summary
7430              (buffer-name gnus-dead-summary))
7431     (with-current-buffer gnus-dead-summary
7432       (when gnus-dead-summary-mode
7433         (kill-buffer (current-buffer)))))
7434   ;; Make this the current dead summary.
7435   (setq gnus-dead-summary (current-buffer))
7436   (gnus-dead-summary-mode 1)
7437   (let ((name (buffer-name)))
7438     (when (string-match "Summary" name)
7439       (rename-buffer
7440        (concat (substring name 0 (match-beginning 0)) "Dead "
7441                (substring name (match-beginning 0)))
7442        t)
7443       (bury-buffer))))
7444
7445 (defun gnus-kill-or-deaden-summary (buffer)
7446   "Kill or deaden the summary BUFFER."
7447   (save-excursion
7448     (when (and (buffer-name buffer)
7449                (not gnus-single-article-buffer))
7450       (with-current-buffer buffer
7451         (gnus-kill-buffer gnus-article-buffer)
7452         (gnus-kill-buffer gnus-original-article-buffer)))
7453     (cond
7454      ;; Kill the buffer.
7455      (gnus-kill-summary-on-exit
7456       (when (and gnus-use-trees
7457                  (gnus-buffer-exists-p buffer))
7458         (with-current-buffer buffer
7459           (gnus-tree-close gnus-newsgroup-name)))
7460       (gnus-kill-buffer buffer))
7461      ;; Deaden the buffer.
7462      ((gnus-buffer-exists-p buffer)
7463       (with-current-buffer buffer
7464         (gnus-deaden-summary))))))
7465
7466 (defun gnus-summary-wake-up-the-dead (&rest args)
7467   "Wake up the dead summary buffer."
7468   (interactive)
7469   (gnus-dead-summary-mode -1)
7470   (let ((name (buffer-name)))
7471     (when (string-match "Dead " name)
7472       (rename-buffer
7473        (concat (substring name 0 (match-beginning 0))
7474                (substring name (match-end 0)))
7475        t)))
7476   (gnus-message 3 "This dead summary is now alive again"))
7477
7478 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7479 (defun gnus-summary-describe-group (&optional force)
7480   "Describe the current newsgroup."
7481   (interactive "P")
7482   (gnus-group-describe-group force gnus-newsgroup-name))
7483
7484 (defun gnus-summary-describe-briefly ()
7485   "Describe summary mode commands briefly."
7486   (interactive)
7487   (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")))
7488
7489 ;; Walking around group mode buffer from summary mode.
7490
7491 (defun gnus-summary-next-group (&optional no-article target-group backward)
7492   "Exit current newsgroup and then select next unread newsgroup.
7493 If prefix argument NO-ARTICLE is non-nil, no article is selected
7494 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7495 previous group instead."
7496   (interactive "P")
7497   ;; Stop pre-fetching.
7498   (gnus-async-halt-prefetch)
7499   (let ((current-group gnus-newsgroup-name)
7500         (current-buffer (current-buffer))
7501         entered)
7502     ;; First we semi-exit this group to update Xrefs and all variables.
7503     ;; We can't do a real exit, because the window conf must remain
7504     ;; the same in case the user is prompted for info, and we don't
7505     ;; want the window conf to change before that...
7506     (gnus-summary-exit t)
7507     (while (not entered)
7508       ;; Then we find what group we are supposed to enter.
7509       (set-buffer gnus-group-buffer)
7510       (gnus-group-jump-to-group current-group)
7511       (setq target-group
7512             (or target-group
7513                 (if (eq gnus-keep-same-level 'best)
7514                     (gnus-summary-best-group gnus-newsgroup-name)
7515                   (gnus-summary-search-group backward gnus-keep-same-level))))
7516       (if (not target-group)
7517           ;; There are no further groups, so we return to the group
7518           ;; buffer.
7519           (progn
7520             (gnus-message 5 "Returning to the group buffer")
7521             (setq entered t)
7522             (when (gnus-buffer-live-p current-buffer)
7523               (set-buffer current-buffer)
7524               (gnus-summary-exit))
7525             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7526         ;; We try to enter the target group.
7527         (gnus-group-jump-to-group target-group)
7528         (let ((unreads (gnus-group-group-unread)))
7529           (if (and (or (eq t unreads)
7530                        (and unreads (not (zerop unreads))))
7531                    (gnus-summary-read-group
7532                     target-group nil no-article
7533                     (and (buffer-name current-buffer) current-buffer)
7534                     nil backward))
7535               (setq entered t)
7536             (setq current-group target-group
7537                   target-group nil)))))))
7538
7539 (defun gnus-summary-prev-group (&optional no-article)
7540   "Exit current newsgroup and then select previous unread newsgroup.
7541 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7542   (interactive "P")
7543   (gnus-summary-next-group no-article nil t))
7544
7545 ;; Walking around summary lines.
7546
7547 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7548   "Go to the first subject satisfying any non-nil constraint.
7549 If UNREAD is non-nil, the article should be unread.
7550 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7551 If UNSEEN is non-nil, the article should be unseen as well as unread.
7552 Returns the article selected or nil if there are no matching articles."
7553   (interactive "P")
7554   (cond
7555    ;; Empty summary.
7556    ((null gnus-newsgroup-data)
7557     (gnus-message 3 "No articles in the group")
7558     nil)
7559    ;; Pick the first article.
7560    ((not (or unread undownloaded unseen))
7561     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7562     (gnus-data-number (car gnus-newsgroup-data)))
7563    ;; Find the first unread article.
7564    (t
7565     (let ((data gnus-newsgroup-data))
7566       (while (and data
7567                   (let ((num (gnus-data-number (car data))))
7568                     (or (memq num gnus-newsgroup-unfetched)
7569                         (not (or (and unread
7570                                       (memq num gnus-newsgroup-unreads))
7571                                  (and undownloaded
7572                                       (memq num gnus-newsgroup-undownloaded))
7573                                  (and unseen
7574                                       (memq num gnus-newsgroup-unseen)
7575                                       (memq num gnus-newsgroup-unreads)))))))
7576         (setq data (cdr data)))
7577       (prog1
7578           (if data
7579               (progn
7580                 (goto-char (gnus-data-pos (car data)))
7581                 (gnus-data-number (car data)))
7582             (gnus-message 3 "No more%s articles"
7583                           (let* ((r (when unread " unread"))
7584                                  (d (when undownloaded " undownloaded"))
7585                                  (s (when unseen " unseen"))
7586                                  (l (delq nil (list r d s))))
7587                             (cond ((= 3 (length l))
7588                                    (concat r "," d ", or" s))
7589                                   ((= 2 (length l))
7590                                    (concat (car l) ", or" (cadr l)))
7591                                   ((= 1 (length l))
7592                                    (car l))
7593                                   (t
7594                                    ""))))
7595             nil
7596             )
7597         (gnus-summary-position-point))))))
7598
7599 (defun gnus-summary-next-subject (n &optional unread dont-display)
7600   "Go to next N'th summary line.
7601 If N is negative, go to the previous N'th subject line.
7602 If UNREAD is non-nil, only unread articles are selected.
7603 The difference between N and the actual number of steps taken is
7604 returned."
7605   (interactive "p")
7606   (let ((backward (< n 0))
7607         (n (abs n)))
7608     (while (and (> n 0)
7609                 (if backward
7610                     (gnus-summary-find-prev unread)
7611                   (gnus-summary-find-next unread)))
7612       (unless (zerop (setq n (1- n)))
7613         (gnus-summary-show-thread)))
7614     (when (/= 0 n)
7615       (gnus-message 7 "No more%s articles"
7616                     (if unread " unread" "")))
7617     (unless dont-display
7618       (gnus-summary-recenter)
7619       (gnus-summary-position-point))
7620     n))
7621
7622 (defun gnus-summary-next-unread-subject (n)
7623   "Go to next N'th unread summary line."
7624   (interactive "p")
7625   (gnus-summary-next-subject n t))
7626
7627 (defun gnus-summary-prev-subject (n &optional unread)
7628   "Go to previous N'th summary line.
7629 If optional argument UNREAD is non-nil, only unread article is selected."
7630   (interactive "p")
7631   (gnus-summary-next-subject (- n) unread))
7632
7633 (defun gnus-summary-prev-unread-subject (n)
7634   "Go to previous N'th unread summary line."
7635   (interactive "p")
7636   (gnus-summary-next-subject (- n) t))
7637
7638 (defun gnus-summary-goto-subjects (articles)
7639   "Insert the subject header for ARTICLES in the current buffer."
7640   (save-excursion
7641     (dolist (article articles)
7642       (gnus-summary-goto-subject article t)))
7643   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7644   (gnus-summary-position-point))
7645
7646 (defun gnus-summary-goto-subject (article &optional force silent)
7647   "Go to the subject line of ARTICLE.
7648 If FORCE, also allow jumping to articles not currently shown."
7649   (interactive "nArticle number: ")
7650   (unless (numberp article)
7651     (error "Article %s is not a number" article))
7652   (let ((b (point))
7653         (data (gnus-data-find article)))
7654     ;; We read in the article if we have to.
7655     (and (not data)
7656          force
7657          (gnus-summary-insert-subject
7658           article
7659           (if (or (numberp force) (vectorp force)) force)
7660           t)
7661          (setq data (gnus-data-find article)))
7662     (goto-char b)
7663     (if (not data)
7664         (progn
7665           (unless silent
7666             (gnus-message 3 "Can't find article %d" article))
7667           nil)
7668       (let ((pt (gnus-data-pos data)))
7669         (goto-char pt)
7670         (gnus-summary-set-article-display-arrow pt))
7671       (gnus-summary-position-point)
7672       article)))
7673
7674 ;; Walking around summary lines with displaying articles.
7675
7676 (defun gnus-summary-expand-window (&optional arg)
7677   "Make the summary buffer take up the entire Emacs frame.
7678 Given a prefix, will force an `article' buffer configuration."
7679   (interactive "P")
7680   (if arg
7681       (gnus-configure-windows 'article 'force)
7682     (gnus-configure-windows 'summary 'force)))
7683
7684 (defun gnus-summary-display-article (article &optional all-header)
7685   "Display ARTICLE in article buffer."
7686   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7687                (with-current-buffer gnus-article-buffer
7688                  (eq major-mode 'gnus-article-mode)))
7689     (gnus-article-setup-buffer))
7690   (gnus-set-global-variables)
7691   (with-current-buffer gnus-article-buffer
7692     (setq gnus-article-charset gnus-newsgroup-charset)
7693     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7694     (mm-enable-multibyte))
7695   (if (null article)
7696       nil
7697     (prog1
7698         (if gnus-summary-display-article-function
7699             (funcall gnus-summary-display-article-function article all-header)
7700           (gnus-article-prepare article all-header))
7701       (gnus-run-hooks 'gnus-select-article-hook)
7702       (when (and gnus-current-article
7703                  (not (zerop gnus-current-article)))
7704         (gnus-summary-goto-subject gnus-current-article))
7705       (gnus-summary-recenter)
7706       (when (and gnus-use-trees gnus-show-threads)
7707         (gnus-possibly-generate-tree article)
7708         (gnus-highlight-selected-tree article))
7709       ;; Successfully display article.
7710       (gnus-article-set-window-start
7711        (cdr (assq article gnus-newsgroup-bookmarks))))))
7712
7713 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7714   "Select the current article.
7715 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7716 non-nil, the article will be re-fetched even if it already present in
7717 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7718 be displayed."
7719   ;; Make sure we are in the summary buffer to work around bbdb bug.
7720   (unless (eq major-mode 'gnus-summary-mode)
7721     (set-buffer gnus-summary-buffer))
7722   (let ((article (or article (gnus-summary-article-number)))
7723         (all-headers (not (not all-headers))) ;Must be t or nil.
7724         gnus-summary-display-article-function)
7725     (and (not pseudo)
7726          (gnus-summary-article-pseudo-p article)
7727          (error "This is a pseudo-article"))
7728     (with-current-buffer gnus-summary-buffer
7729       (if (or (and gnus-single-article-buffer
7730                    (or (null gnus-current-article)
7731                        (null gnus-article-current)
7732                        (null (get-buffer gnus-article-buffer))
7733                        (not (eq article (cdr gnus-article-current)))
7734                        (not (equal (car gnus-article-current)
7735                                    gnus-newsgroup-name))
7736                        (not (get-buffer gnus-original-article-buffer))))
7737               (and (not gnus-single-article-buffer)
7738                    (or (null gnus-current-article)
7739                        (not (get-buffer gnus-original-article-buffer))
7740                        (not (eq gnus-current-article article))))
7741               force)
7742           ;; The requested article is different from the current article.
7743           (progn
7744             (gnus-summary-display-article article all-headers)
7745             (when (gnus-buffer-live-p gnus-article-buffer)
7746               (with-current-buffer gnus-article-buffer
7747                 (if (not gnus-article-decoded-p) ;; a local variable
7748                     (mm-disable-multibyte))))
7749             (gnus-article-set-window-start
7750              (cdr (assq article gnus-newsgroup-bookmarks)))
7751             article)
7752         'old))))
7753
7754 (defun gnus-summary-force-verify-and-decrypt ()
7755   "Display buttons for signed/encrypted parts and verify/decrypt them."
7756   (interactive)
7757   (let ((mm-verify-option 'known)
7758         (mm-decrypt-option 'known)
7759         (gnus-article-emulate-mime t)
7760         (gnus-buttonized-mime-types (append (list "multipart/signed"
7761                                                   "multipart/encrypted")
7762                                             gnus-buttonized-mime-types)))
7763     (gnus-summary-select-article nil 'force)))
7764
7765 (defun gnus-summary-set-current-mark (&optional current-mark)
7766   "Obsolete function."
7767   nil)
7768
7769 (defun gnus-summary-next-article (&optional unread subject backward push)
7770   "Select the next article.
7771 If UNREAD, only unread articles are selected.
7772 If SUBJECT, only articles with SUBJECT are selected.
7773 If BACKWARD, the previous article is selected instead of the next."
7774   (interactive "P")
7775   ;; Make sure we are in the summary buffer.
7776   (unless (eq major-mode 'gnus-summary-mode)
7777     (set-buffer gnus-summary-buffer))
7778   (cond
7779    ;; Is there such an article?
7780    ((and (gnus-summary-search-forward unread subject backward)
7781          (or (gnus-summary-display-article (gnus-summary-article-number))
7782              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7783     (gnus-summary-position-point))
7784    ;; If not, we try the first unread, if that is wanted.
7785    ((and subject
7786          gnus-auto-select-same
7787          (gnus-summary-first-unread-article))
7788     (gnus-summary-position-point)
7789     (gnus-message 6 "Wrapped"))
7790    ;; Try to get next/previous article not displayed in this group.
7791    ((and gnus-auto-extend-newsgroup
7792          (not unread) (not subject))
7793     (gnus-summary-goto-article
7794      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7795      nil (count-lines (point-min) (point))))
7796    ;; Go to next/previous group.
7797    (t
7798     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7799       (gnus-summary-jump-to-group gnus-newsgroup-name))
7800     (let ((cmd (if (featurep 'xemacs)
7801                    last-command-char
7802                  last-command-event))
7803           (point
7804            (with-current-buffer gnus-group-buffer
7805              (point)))
7806           (current-summary (current-buffer))
7807           (group
7808            (if (eq gnus-keep-same-level 'best)
7809                (gnus-summary-best-group gnus-newsgroup-name)
7810              (gnus-summary-search-group backward gnus-keep-same-level))))
7811       ;; Select next unread newsgroup automagically.
7812       (cond
7813        ((or (not gnus-auto-select-next)
7814             (not cmd))
7815         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7816        ((or (eq gnus-auto-select-next 'quietly)
7817             (and (eq gnus-auto-select-next 'slightly-quietly)
7818                  push)
7819             (and (eq gnus-auto-select-next 'almost-quietly)
7820                  (gnus-summary-last-article-p)))
7821         ;; Select quietly.
7822         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7823             (gnus-summary-exit)
7824           (gnus-message 7 "No more%s articles (%s)..."
7825                         (if unread " unread" "")
7826                         (if group (concat "selecting " group)
7827                           "exiting"))
7828           (gnus-summary-next-group nil group backward)))
7829        (t
7830         (when (gnus-key-press-event-p last-input-event)
7831           ;; Somehow or other, we may now have selected a different
7832           ;; window.  Make point go back to the summary buffer.
7833           (when (eq current-summary (current-buffer))
7834             ;; FIXME: This burps when get-buffer-window returns nil.
7835             (select-window (get-buffer-window current-summary 0)))
7836           (gnus-summary-walk-group-buffer
7837            gnus-newsgroup-name cmd unread backward point))))))))
7838
7839 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7840   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7841                       (?\C-p (gnus-group-prev-unread-group 1))))
7842         (cursor-in-echo-area t)
7843         keve key group ended prompt)
7844     (with-current-buffer gnus-group-buffer
7845       (goto-char start)
7846       (setq group
7847             (if (eq gnus-keep-same-level 'best)
7848                 (gnus-summary-best-group gnus-newsgroup-name)
7849               (gnus-summary-search-group backward gnus-keep-same-level))))
7850     (while (not ended)
7851       (setq prompt
7852             (format
7853              "No more%s articles%s " (if unread " unread" "")
7854              (if (and group
7855                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7856                  (format " (Type %s for %s [%s])"
7857                          (single-key-description cmd)
7858                          (gnus-group-decoded-name group)
7859                          (gnus-group-unread group))
7860                (format " (Type %s to exit %s)"
7861                        (single-key-description cmd)
7862                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7863       ;; Confirm auto selection.
7864       (setq key (car (setq keve (gnus-read-event-char prompt)))
7865             ended t)
7866       (cond
7867        ((assq key keystrokes)
7868         (let ((obuf (current-buffer)))
7869           (switch-to-buffer gnus-group-buffer)
7870           (when group
7871             (gnus-group-jump-to-group group))
7872           (eval (cadr (assq key keystrokes)))
7873           (setq group (gnus-group-group-name))
7874           (switch-to-buffer obuf))
7875         (setq ended nil))
7876        ((equal key cmd)
7877         (if (or (not group)
7878                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7879             (gnus-summary-exit)
7880           (gnus-summary-next-group nil group backward)))
7881        (t
7882         (push (cdr keve) unread-command-events))))))
7883
7884 (defun gnus-summary-next-unread-article ()
7885   "Select unread article after current one."
7886   (interactive)
7887   (gnus-summary-next-article
7888    (or (not (eq gnus-summary-goto-unread 'never))
7889        (gnus-summary-last-article-p (gnus-summary-article-number)))
7890    (and gnus-auto-select-same
7891         (gnus-summary-article-subject))))
7892
7893 (defun gnus-summary-prev-article (&optional unread subject)
7894   "Select the article before the current one.
7895 If UNREAD is non-nil, only unread articles are selected."
7896   (interactive "P")
7897   (gnus-summary-next-article unread subject t))
7898
7899 (defun gnus-summary-prev-unread-article ()
7900   "Select unread article before current one."
7901   (interactive)
7902   (gnus-summary-prev-article
7903    (or (not (eq gnus-summary-goto-unread 'never))
7904        (gnus-summary-first-article-p (gnus-summary-article-number)))
7905    (and gnus-auto-select-same
7906         (gnus-summary-article-subject))))
7907
7908 (defun gnus-summary-next-page (&optional lines circular stop)
7909   "Show next page of the selected article.
7910 If at the end of the current article, select the next article.
7911 LINES says how many lines should be scrolled up.
7912
7913 If CIRCULAR is non-nil, go to the start of the article instead of
7914 selecting the next article when reaching the end of the current
7915 article.
7916
7917 If STOP is non-nil, just stop when reaching the end of the message.
7918
7919 Also see the variable `gnus-article-skip-boring'."
7920   (interactive "P")
7921   (setq gnus-summary-buffer (current-buffer))
7922   (gnus-set-global-variables)
7923   (let ((article (gnus-summary-article-number))
7924         (article-window (get-buffer-window gnus-article-buffer t))
7925         endp)
7926     ;; If the buffer is empty, we have no article.
7927     (unless article
7928       (error "No article to select"))
7929     (gnus-configure-windows 'article)
7930     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7931         (if (and (eq gnus-summary-goto-unread 'never)
7932                  (not (gnus-summary-last-article-p article)))
7933             (gnus-summary-next-article)
7934           (gnus-summary-next-unread-article))
7935       (if (or (null gnus-current-article)
7936               (null gnus-article-current)
7937               (/= article (cdr gnus-article-current))
7938               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7939           ;; Selected subject is different from current article's.
7940           (gnus-summary-display-article article)
7941         (when article-window
7942           (gnus-eval-in-buffer-window gnus-article-buffer
7943             (setq endp (or (gnus-article-next-page lines)
7944                            (gnus-article-only-boring-p))))
7945           (when endp
7946             (cond ((or stop gnus-summary-stop-at-end-of-message)
7947                    (gnus-message 3 "End of message"))
7948                   (circular
7949                    (gnus-summary-beginning-of-article))
7950                   (lines
7951                    (gnus-message 3 "End of message"))
7952                   ((null lines)
7953                    (if (and (eq gnus-summary-goto-unread 'never)
7954                             (not (gnus-summary-last-article-p article)))
7955                        (gnus-summary-next-article)
7956                      (gnus-summary-next-unread-article))))))))
7957     (gnus-summary-recenter)
7958     (gnus-summary-position-point)))
7959
7960 (defun gnus-summary-prev-page (&optional lines move)
7961   "Show previous page of selected article.
7962 Argument LINES specifies lines to be scrolled down.
7963 If MOVE, move to the previous unread article if point is at
7964 the beginning of the buffer."
7965   (interactive "P")
7966   (let ((article (gnus-summary-article-number))
7967         (article-window (get-buffer-window gnus-article-buffer t))
7968         endp)
7969     (gnus-configure-windows 'article)
7970     (if (or (null gnus-current-article)
7971             (null gnus-article-current)
7972             (/= article (cdr gnus-article-current))
7973             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7974         ;; Selected subject is different from current article's.
7975         (gnus-summary-display-article article)
7976       (gnus-summary-recenter)
7977       (when article-window
7978         (gnus-eval-in-buffer-window gnus-article-buffer
7979           (setq endp (gnus-article-prev-page lines)))
7980         (when (and move endp)
7981           (cond (lines
7982                  (gnus-message 3 "Beginning of message"))
7983                 ((null lines)
7984                  (if (and (eq gnus-summary-goto-unread 'never)
7985                           (not (gnus-summary-first-article-p article)))
7986                      (gnus-summary-prev-article)
7987                    (gnus-summary-prev-unread-article))))))))
7988   (gnus-summary-position-point))
7989
7990 (defun gnus-summary-prev-page-or-article (&optional lines)
7991   "Show previous page of selected article.
7992 Argument LINES specifies lines to be scrolled down.
7993 If at the beginning of the article, go to the next article."
7994   (interactive "P")
7995   (gnus-summary-prev-page lines t))
7996
7997 (defun gnus-summary-scroll-up (lines)
7998   "Scroll up (or down) one line current article.
7999 Argument LINES specifies lines to be scrolled up (or down if negative).
8000 If no article is selected, then the current article will be selected first."
8001   (interactive "p")
8002   (gnus-configure-windows 'article)
8003   (gnus-summary-show-thread)
8004   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
8005     (gnus-eval-in-buffer-window gnus-article-buffer
8006       (cond ((> lines 0)
8007              (when (gnus-article-next-page lines)
8008                (gnus-message 3 "End of message")))
8009             ((< lines 0)
8010              (gnus-article-prev-page (- lines))))))
8011   (gnus-summary-recenter)
8012   (gnus-summary-position-point))
8013
8014 (defun gnus-summary-scroll-down (lines)
8015   "Scroll down (or up) one line current article.
8016 Argument LINES specifies lines to be scrolled down (or up if negative).
8017 If no article is selected, then the current article will be selected first."
8018   (interactive "p")
8019   (gnus-summary-scroll-up (- lines)))
8020
8021 (defun gnus-summary-next-same-subject ()
8022   "Select next article which has the same subject as current one."
8023   (interactive)
8024   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8025
8026 (defun gnus-summary-prev-same-subject ()
8027   "Select previous article which has the same subject as current one."
8028   (interactive)
8029   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8030
8031 (defun gnus-summary-next-unread-same-subject ()
8032   "Select next unread article which has the same subject as current one."
8033   (interactive)
8034   (gnus-summary-next-article t (gnus-summary-article-subject)))
8035
8036 (defun gnus-summary-prev-unread-same-subject ()
8037   "Select previous unread article which has the same subject as current one."
8038   (interactive)
8039   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8040
8041 (defun gnus-summary-first-unread-article ()
8042   "Select the first unread article.
8043 Return nil if there are no unread articles."
8044   (interactive)
8045   (prog1
8046       (when (gnus-summary-first-subject t)
8047         (gnus-summary-show-thread)
8048         (gnus-summary-first-subject t)
8049         (gnus-summary-display-article (gnus-summary-article-number)))
8050     (gnus-summary-position-point)))
8051
8052 (defun gnus-summary-first-unread-subject ()
8053   "Place the point on the subject line of the first unread article.
8054 Return nil if there are no unread articles."
8055   (interactive)
8056   (prog1
8057       (when (gnus-summary-first-subject t)
8058         (gnus-summary-show-thread)
8059         (gnus-summary-first-subject t))
8060     (gnus-summary-position-point)))
8061
8062 (defun gnus-summary-first-unseen-subject ()
8063   "Place the point on the subject line of the first unseen article.
8064 Return nil if there are no unseen articles."
8065   (interactive)
8066   (prog1
8067       (when (gnus-summary-first-subject nil nil t)
8068         (gnus-summary-show-thread)
8069         (gnus-summary-first-subject nil nil t))
8070     (gnus-summary-position-point)))
8071
8072 (defun gnus-summary-first-unseen-or-unread-subject ()
8073   "Place the point on the subject line of the first unseen and unread article.
8074 If all article have been seen, on the subject line of the first unread
8075 article."
8076   (interactive)
8077   (prog1
8078       (unless (when (gnus-summary-first-subject nil nil t)
8079                 (gnus-summary-show-thread)
8080                 (gnus-summary-first-subject nil nil t))
8081         (when (gnus-summary-first-subject t)
8082           (gnus-summary-show-thread)
8083           (gnus-summary-first-subject t)))
8084     (gnus-summary-position-point)))
8085
8086 (defun gnus-summary-first-article ()
8087   "Select the first article.
8088 Return nil if there are no articles."
8089   (interactive)
8090   (prog1
8091       (when (gnus-summary-first-subject)
8092         (gnus-summary-show-thread)
8093         (gnus-summary-first-subject)
8094         (gnus-summary-display-article (gnus-summary-article-number)))
8095     (gnus-summary-position-point)))
8096
8097 (defun gnus-summary-best-unread-article (&optional arg)
8098   "Select the unread article with the highest score.
8099 If given a prefix argument, select the next unread article that has a
8100 score higher than the default score."
8101   (interactive "P")
8102   (let ((article (if arg
8103                      (gnus-summary-better-unread-subject)
8104                    (gnus-summary-best-unread-subject))))
8105     (if article
8106         (gnus-summary-goto-article article)
8107       (error "No unread articles"))))
8108
8109 (defun gnus-summary-best-unread-subject ()
8110   "Select the unread subject with the highest score."
8111   (interactive)
8112   (let ((best -1000000)
8113         (data gnus-newsgroup-data)
8114         article score)
8115     (while data
8116       (and (gnus-data-unread-p (car data))
8117            (> (setq score
8118                     (gnus-summary-article-score (gnus-data-number (car data))))
8119               best)
8120            (setq best score
8121                  article (gnus-data-number (car data))))
8122       (setq data (cdr data)))
8123     (when article
8124       (gnus-summary-goto-subject article))
8125     (gnus-summary-position-point)
8126     article))
8127
8128 (defun gnus-summary-better-unread-subject ()
8129   "Select the first unread subject that has a score over the default score."
8130   (interactive)
8131   (let ((data gnus-newsgroup-data)
8132         article score)
8133     (while (and (setq article (gnus-data-number (car data)))
8134                 (or (gnus-data-read-p (car data))
8135                     (not (> (gnus-summary-article-score article)
8136                             gnus-summary-default-score))))
8137       (setq data (cdr data)))
8138     (when article
8139       (gnus-summary-goto-subject article))
8140     (gnus-summary-position-point)
8141     article))
8142
8143 (defun gnus-summary-last-subject ()
8144   "Go to the last displayed subject line in the group."
8145   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8146     (when article
8147       (gnus-summary-goto-subject article))))
8148
8149 (defun gnus-summary-goto-article (article &optional all-headers force)
8150   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8151 If ALL-HEADERS is non-nil, no header lines are hidden.
8152 If FORCE, go to the article even if it isn't displayed.  If FORCE
8153 is a number, it is the line the article is to be displayed on."
8154   (interactive
8155    (list
8156     (gnus-completing-read
8157      "Article number or Message-ID"
8158      (mapcar 'int-to-string gnus-newsgroup-limit))
8159     current-prefix-arg
8160     t))
8161   (prog1
8162       (if (and (stringp article)
8163                (string-match "@\\|%40" article))
8164           (gnus-summary-refer-article article)
8165         (when (stringp article)
8166           (setq article (string-to-number article)))
8167         (if (gnus-summary-goto-subject article force)
8168             (gnus-summary-display-article article all-headers)
8169           (gnus-message 4 "Couldn't go to article %s" article) nil))
8170     (gnus-summary-position-point)))
8171
8172 (defun gnus-summary-goto-last-article ()
8173   "Go to the previously read article."
8174   (interactive)
8175   (prog1
8176       (when gnus-last-article
8177         (gnus-summary-goto-article gnus-last-article nil t))
8178     (gnus-summary-position-point)))
8179
8180 (defun gnus-summary-pop-article (number)
8181   "Pop one article off the history and go to the previous.
8182 NUMBER articles will be popped off."
8183   (interactive "p")
8184   (let (to)
8185     (setq gnus-newsgroup-history
8186           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8187     (if to
8188         (gnus-summary-goto-article (car to) nil t)
8189       (error "Article history empty")))
8190   (gnus-summary-position-point))
8191
8192 ;; Summary commands and functions for limiting the summary buffer.
8193
8194 (defun gnus-summary-limit-to-articles (n)
8195   "Limit the summary buffer to the next N articles.
8196 If not given a prefix, use the process marked articles instead."
8197   (interactive "P")
8198   (prog1
8199       (let ((articles (gnus-summary-work-articles n)))
8200         (setq gnus-newsgroup-processable nil)
8201         (gnus-summary-limit articles))
8202     (gnus-summary-position-point)))
8203
8204 (defun gnus-summary-pop-limit (&optional total)
8205   "Restore the previous limit.
8206 If given a prefix, remove all limits."
8207   (interactive "P")
8208   (when total
8209     (setq gnus-newsgroup-limits
8210           (list (mapcar (lambda (h) (mail-header-number h))
8211                         gnus-newsgroup-headers))))
8212   (unless gnus-newsgroup-limits
8213     (error "No limit to pop"))
8214   (prog1
8215       (gnus-summary-limit nil 'pop)
8216     (gnus-summary-position-point)))
8217
8218 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8219   "Limit the summary buffer to articles that have subjects that match a regexp.
8220 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8221   (interactive
8222    (list (read-string (if current-prefix-arg
8223                           "Exclude subject (regexp): "
8224                         "Limit to subject (regexp): "))
8225          nil current-prefix-arg))
8226   (unless header
8227     (setq header "subject"))
8228   (when (not (equal "" subject))
8229     (prog1
8230         (let ((articles (gnus-summary-find-matching
8231                          (or header "subject") subject 'all nil nil
8232                          not-matching)))
8233           (unless articles
8234             (error "Found no matches for \"%s\"" subject))
8235           (gnus-summary-limit articles))
8236       (gnus-summary-position-point))))
8237
8238 (defun gnus-summary-limit-to-author (from &optional not-matching)
8239   "Limit the summary buffer to articles that have authors that match a regexp.
8240 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8241   (interactive
8242    (list (read-string (if current-prefix-arg
8243                           "Exclude author (regexp): "
8244                         "Limit to author (regexp): "))
8245          current-prefix-arg))
8246   (gnus-summary-limit-to-subject from "from" not-matching))
8247
8248 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8249   "Limit the summary buffer to articles with the given RECIPIENT.
8250
8251 If NOT-MATCHING, exclude RECIPIENT.
8252
8253 To and Cc headers are checked.  You need to include them in
8254 `nnmail-extra-headers'."
8255   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8256   (interactive
8257    (list (read-string (format "%s recipient (regexp): "
8258                               (if current-prefix-arg "Exclude" "Limit to")))
8259          current-prefix-arg))
8260   (when (not (equal "" recipient))
8261     (prog1 (let* ((to
8262                    (if (memq 'To nnmail-extra-headers)
8263                        (gnus-summary-find-matching
8264                         (cons 'extra 'To) recipient 'all nil nil
8265                         not-matching)
8266                      (gnus-message
8267                       1 "`To' isn't present in `nnmail-extra-headers'")
8268                      (sit-for 1)
8269                      nil))
8270                   (cc
8271                    (if (memq 'Cc nnmail-extra-headers)
8272                        (gnus-summary-find-matching
8273                         (cons 'extra 'Cc) recipient 'all nil nil
8274                         not-matching)
8275                      (gnus-message
8276                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8277                      (sit-for 1)
8278                      nil))
8279                   (articles
8280                    (if not-matching
8281                        ;; We need the numbers that are in both lists:
8282                        (mapcar (lambda (a)
8283                                  (and (memq a to) a))
8284                                cc)
8285                      (nconc to cc))))
8286              (unless articles
8287                (error "Found no matches for \"%s\"" recipient))
8288              (gnus-summary-limit articles))
8289       (gnus-summary-position-point))))
8290
8291 (defun gnus-summary-limit-to-address (address &optional not-matching)
8292   "Limit the summary buffer to articles with the given ADDRESS.
8293
8294 If NOT-MATCHING, exclude ADDRESS.
8295
8296 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8297 in `nnmail-extra-headers'."
8298   (interactive
8299    (list (read-string (format "%s address (regexp): "
8300                               (if current-prefix-arg "Exclude" "Limit to")))
8301          current-prefix-arg))
8302   (when (not (equal "" address))
8303     (prog1 (let* ((to
8304                    (if (memq 'To nnmail-extra-headers)
8305                        (gnus-summary-find-matching
8306                         (cons 'extra 'To) address 'all nil nil
8307                         not-matching)
8308                      (gnus-message
8309                       1 "`To' isn't present in `nnmail-extra-headers'")
8310                      (sit-for 1)
8311                      t))
8312                   (cc
8313                    (if (memq 'Cc nnmail-extra-headers)
8314                        (gnus-summary-find-matching
8315                         (cons 'extra 'Cc) address 'all nil nil
8316                         not-matching)
8317                      (gnus-message
8318                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8319                      (sit-for 1)
8320                      t))
8321                   (from
8322                    (gnus-summary-find-matching "from" address
8323                                                'all nil nil not-matching))
8324                   (articles
8325                    (if not-matching
8326                        ;; We need the numbers that are in all lists:
8327                        (if (eq cc t)
8328                            (if (eq to t)
8329                                from
8330                              (mapcar (lambda (a) (car (memq a from))) to))
8331                          (if (eq to t)
8332                              (mapcar (lambda (a) (car (memq a from))) cc)
8333                            (mapcar (lambda (a) (car (memq a from)))
8334                                    (mapcar (lambda (a) (car (memq a to)))
8335                                            cc))))
8336                      (nconc (if (eq to t) nil to)
8337                             (if (eq cc t) nil cc)
8338                             from))))
8339              (unless articles
8340                (error "Found no matches for \"%s\"" address))
8341              (gnus-summary-limit articles))
8342       (gnus-summary-position-point))))
8343
8344 (defun gnus-summary-limit-strange-charsets-predicate (header)
8345   (when (fboundp 'char-charset)
8346     (let ((string (concat (mail-header-subject header)
8347                           (mail-header-from header)))
8348           charset found)
8349       (dotimes (i (1- (length string)))
8350         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8351         (when (string-match "unicode\\|big\\|japanese" charset)
8352           (setq found t)))
8353       found)))
8354
8355 (defun gnus-summary-limit-to-predicate (predicate)
8356   "Limit to articles where PREDICATE returns non-nil.
8357 PREDICATE will be called with the header structures of the
8358 articles."
8359   (let ((articles nil)
8360         (case-fold-search t))
8361     (dolist (header gnus-newsgroup-headers)
8362       (when (funcall predicate header)
8363         (push (mail-header-number header) articles)))
8364     (gnus-summary-limit (nreverse articles))))
8365
8366 (defun gnus-summary-limit-to-age (age &optional younger-p)
8367   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8368 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8369 articles that are younger than AGE days."
8370   (interactive
8371    (let ((younger current-prefix-arg)
8372          (days-got nil)
8373          days)
8374      (while (not days-got)
8375        (setq days (if younger
8376                       (read-string "Limit to articles younger than (in days, older when negative): ")
8377                     (read-string
8378                      "Limit to articles older than (in days, younger when negative): ")))
8379        (when (> (length days) 0)
8380          (setq days (read days)))
8381        (if (numberp days)
8382            (progn
8383              (setq days-got t)
8384              (when (< days 0)
8385                (setq younger (not younger))
8386                (setq days (* days -1))))
8387          (message "Please enter a number.")
8388          (sleep-for 1)))
8389      (list days younger)))
8390   (prog1
8391       (let ((data gnus-newsgroup-data)
8392             (cutoff (days-to-time age))
8393             articles d date is-younger)
8394         (while (setq d (pop data))
8395           (when (and (vectorp (gnus-data-header d))
8396                      (setq date (mail-header-date (gnus-data-header d))))
8397             (setq is-younger (time-less-p
8398                               (time-since (gnus-date-get-time date))
8399                               cutoff))
8400             (when (if younger-p
8401                       is-younger
8402                     (not is-younger))
8403               (push (gnus-data-number d) articles))))
8404         (gnus-summary-limit (nreverse articles)))
8405     (gnus-summary-position-point)))
8406
8407 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8408   "Limit the summary buffer to articles that match an 'extra' header."
8409   (interactive
8410    (let ((header
8411           (intern
8412            (gnus-completing-read
8413             (if current-prefix-arg
8414                 "Exclude extra header"
8415               "Limit extra header")
8416             (mapcar 'symbol-name gnus-extra-headers)
8417             t nil nil
8418             (symbol-name (car gnus-extra-headers))))))
8419      (list header
8420            (read-string (format "%s header %s (regexp): "
8421                                 (if current-prefix-arg "Exclude" "Limit to")
8422                                 header))
8423            current-prefix-arg)))
8424   (when (not (equal "" regexp))
8425     (prog1
8426         (let ((articles (gnus-summary-find-matching
8427                          (cons 'extra header) regexp 'all nil nil
8428                          not-matching)))
8429           (unless articles
8430             (error "Found no matches for \"%s\"" regexp))
8431           (gnus-summary-limit articles))
8432       (gnus-summary-position-point))))
8433
8434 (defun gnus-summary-limit-to-display-predicate ()
8435   "Limit the summary buffer to the predicated in the `display' group parameter."
8436   (interactive)
8437   (unless gnus-newsgroup-display
8438     (error "There is no `display' group parameter"))
8439   (let (articles)
8440     (dolist (gnus-number gnus-newsgroup-articles)
8441       (when (funcall gnus-newsgroup-display)
8442         (push gnus-number articles)))
8443     (gnus-summary-limit articles))
8444   (gnus-summary-position-point))
8445
8446 (defun gnus-summary-limit-to-unread (&optional all)
8447   "Limit the summary buffer to articles that are not marked as read.
8448 If ALL is non-nil, limit strictly to unread articles."
8449   (interactive "P")
8450   (if all
8451       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8452     (gnus-summary-limit-to-marks
8453      ;; Concat all the marks that say that an article is read and have
8454      ;; those removed.
8455      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8456            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8457            gnus-low-score-mark gnus-expirable-mark
8458            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8459            gnus-duplicate-mark)
8460      'reverse)))
8461
8462 (defun gnus-summary-limit-to-headers (match &optional reverse)
8463   "Limit the summary buffer to articles that have headers that match MATCH.
8464 If REVERSE (the prefix), limit to articles that don't match."
8465   (interactive "sMatch headers (regexp): \nP")
8466   (gnus-summary-limit-to-bodies match reverse t))
8467
8468 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8469   "Limit the summary buffer to articles that have bodies that match MATCH.
8470 If REVERSE (the prefix), limit to articles that don't match."
8471   (interactive "sMatch body (regexp): \nP")
8472   (let ((articles nil)
8473         (gnus-select-article-hook nil)  ;Disable hook.
8474         (gnus-article-prepare-hook nil)
8475         (gnus-use-article-prefetch nil)
8476         (gnus-keep-backlog nil)
8477         (gnus-break-pages nil)
8478         (gnus-summary-display-arrow nil)
8479         (gnus-updated-mode-lines nil)
8480         (gnus-auto-center-summary nil)
8481         (gnus-display-mime-function nil))
8482     (dolist (data gnus-newsgroup-data)
8483       (let (gnus-mark-article-hook)
8484         (gnus-summary-select-article t t nil (gnus-data-number data)))
8485       (with-current-buffer gnus-article-buffer
8486         (article-goto-body)
8487         (let* ((case-fold-search t)
8488                (found (if headersp
8489                           (re-search-backward match nil t)
8490                         (re-search-forward match nil t))))
8491           (when (or (and found
8492                          (not reverse))
8493                     (and (not found)
8494                          reverse))
8495             (push (gnus-data-number data) articles)))))
8496     (if (not articles)
8497         (message "No messages matched")
8498       (gnus-summary-limit articles)))
8499   (gnus-summary-position-point))
8500
8501 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8502   "Limit the summary buffer to articles that aren't part on any thread.
8503 If THREADSP (the prefix), limit to articles that are in threads."
8504   (interactive "P")
8505   (let ((articles nil)
8506         thread-articles
8507         threads)
8508     (dolist (thread gnus-newsgroup-threads)
8509       (if (stringp (car thread))
8510           (dolist (thread (cdr thread))
8511             (push thread threads))
8512         (push thread threads)))
8513     (dolist (thread threads)
8514       (setq thread-articles (gnus-articles-in-thread thread))
8515       (when (or (and threadsp
8516                      (> (length thread-articles) 1))
8517                 (and (not threadsp)
8518                      (= (length thread-articles) 1)))
8519         (setq articles (nconc thread-articles articles))))
8520     (if (not articles)
8521         (message "No messages matched")
8522       (gnus-summary-limit articles))
8523     (gnus-summary-position-point)))
8524
8525 (defun gnus-summary-limit-to-replied (&optional unreplied)
8526   "Limit the summary buffer to replied articles.
8527 If UNREPLIED (the prefix), limit to unreplied articles."
8528   (interactive "P")
8529   (if unreplied
8530       (gnus-summary-limit
8531        (gnus-set-difference gnus-newsgroup-articles
8532         gnus-newsgroup-replied))
8533     (gnus-summary-limit gnus-newsgroup-replied))
8534   (gnus-summary-position-point))
8535
8536 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8537   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8538 If REVERSE, limit the summary buffer to articles that are marked
8539 with MARKS.  MARKS can either be a string of marks or a list of marks.
8540 Returns how many articles were removed."
8541   (interactive "sMarks: ")
8542   (gnus-summary-limit-to-marks marks t))
8543
8544 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8545   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8546 If REVERSE (the prefix), limit the summary buffer to articles that are
8547 not marked with MARKS.  MARKS can either be a string of marks or a
8548 list of marks.
8549 Returns how many articles were removed."
8550   (interactive "sMarks: \nP")
8551   (prog1
8552       (let ((data gnus-newsgroup-data)
8553             (marks (if (listp marks) marks
8554                      (append marks nil))) ; Transform to list.
8555             articles)
8556         (while data
8557           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8558                   (memq (gnus-data-mark (car data)) marks))
8559             (push (gnus-data-number (car data)) articles))
8560           (setq data (cdr data)))
8561         (gnus-summary-limit articles))
8562     (gnus-summary-position-point)))
8563
8564 (defun gnus-summary-limit-to-score (score)
8565   "Limit to articles with score at or above SCORE."
8566   (interactive "NLimit to articles with score of at least: ")
8567   (let ((data gnus-newsgroup-data)
8568         articles)
8569     (while data
8570       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8571                 score)
8572         (push (gnus-data-number (car data)) articles))
8573       (setq data (cdr data)))
8574     (prog1
8575         (gnus-summary-limit articles)
8576       (gnus-summary-position-point))))
8577
8578 (defun gnus-summary-limit-to-unseen ()
8579   "Limit to unseen articles."
8580   (interactive)
8581   (prog1
8582       (gnus-summary-limit gnus-newsgroup-unseen)
8583     (gnus-summary-position-point)))
8584
8585 (defun gnus-summary-limit-include-thread (id)
8586   "Display all the hidden articles that is in the thread with ID in it.
8587 When called interactively, ID is the Message-ID of the current
8588 article."
8589   (interactive (list (mail-header-id (gnus-summary-article-header))))
8590   (let ((articles (gnus-articles-in-thread
8591                    (gnus-id-to-thread (gnus-root-id id))))
8592         ;;we REALLY want the whole thread---this prevents cut-threads
8593         ;;from removing the thread we want to include.
8594         (gnus-fetch-old-headers nil)
8595         (gnus-build-sparse-threads nil))
8596     (prog1
8597         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8598       (gnus-summary-limit-include-matching-articles
8599        "subject"
8600        (regexp-quote (gnus-simplify-subject-re
8601                       (mail-header-subject (gnus-id-to-header id)))))
8602       (gnus-summary-position-point))))
8603
8604 (defun gnus-summary-limit-include-matching-articles (header regexp)
8605   "Display all the hidden articles that have HEADERs that match REGEXP."
8606   (interactive (list (read-string "Match on header: ")
8607                      (read-string "Regexp: ")))
8608   (let ((articles (gnus-find-matching-articles header regexp)))
8609     (prog1
8610         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8611       (gnus-summary-position-point))))
8612
8613 (defun gnus-summary-insert-dormant-articles ()
8614   "Insert all the dormant articles for this group into the current buffer."
8615   (interactive)
8616   (let ((gnus-verbose (max 6 gnus-verbose)))
8617     (if (not gnus-newsgroup-dormant)
8618         (gnus-message 3 "No dormant articles for this group")
8619       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8620
8621 (defun gnus-summary-insert-ticked-articles ()
8622   "Insert ticked articles for this group into the current buffer."
8623   (interactive)
8624   (let ((gnus-verbose (max 6 gnus-verbose)))
8625     (if (not gnus-newsgroup-marked)
8626         (gnus-message 3 "No ticked articles for this group")
8627       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8628
8629 (defun gnus-summary-limit-include-dormant ()
8630   "Display all the hidden articles that are marked as dormant.
8631 Note that this command only works on a subset of the articles currently
8632 fetched for this group."
8633   (interactive)
8634   (unless gnus-newsgroup-dormant
8635     (error "There are no dormant articles in this group"))
8636   (prog1
8637       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8638     (gnus-summary-position-point)))
8639
8640 (defun gnus-summary-include-articles (articles)
8641   "Fetch the headers for ARTICLES and then display the summary lines."
8642   (let ((gnus-inhibit-demon t)
8643         (gnus-agent nil)
8644         (gnus-read-all-available-headers t))
8645     (setq gnus-newsgroup-headers
8646           (gnus-merge
8647            'list gnus-newsgroup-headers
8648            (gnus-fetch-headers articles nil t)
8649            'gnus-article-sort-by-number))
8650     (setq gnus-newsgroup-articles
8651           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8652     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8653
8654 (defun gnus-summary-limit-exclude-dormant ()
8655   "Hide all dormant articles."
8656   (interactive)
8657   (prog1
8658       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8659     (gnus-summary-position-point)))
8660
8661 (defun gnus-summary-limit-exclude-childless-dormant ()
8662   "Hide all dormant articles that have no children."
8663   (interactive)
8664   (let ((data (gnus-data-list t))
8665         articles d children)
8666     ;; Find all articles that are either not dormant or have
8667     ;; children.
8668     (while (setq d (pop data))
8669       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8670                 (and (setq children
8671                            (gnus-article-children (gnus-data-number d)))
8672                      (let (found)
8673                        (while children
8674                          (when (memq (car children) articles)
8675                            (setq children nil
8676                                  found t))
8677                          (pop children))
8678                        found)))
8679         (push (gnus-data-number d) articles)))
8680     ;; Do the limiting.
8681     (prog1
8682         (gnus-summary-limit articles)
8683       (gnus-summary-position-point))))
8684
8685 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8686   "Mark all unread excluded articles as read.
8687 If ALL, mark even excluded ticked and dormants as read."
8688   (interactive "P")
8689   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8690   (let ((articles (gnus-sorted-ndifference
8691                    (sort
8692                     (mapcar (lambda (h) (mail-header-number h))
8693                             gnus-newsgroup-headers)
8694                     '<)
8695                    gnus-newsgroup-limit))
8696         article)
8697     (setq gnus-newsgroup-unreads
8698           (gnus-sorted-intersection gnus-newsgroup-unreads
8699                                     gnus-newsgroup-limit))
8700     (if all
8701         (setq gnus-newsgroup-dormant nil
8702               gnus-newsgroup-marked nil
8703               gnus-newsgroup-reads
8704               (nconc
8705                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8706                gnus-newsgroup-reads))
8707       (while (setq article (pop articles))
8708         (unless (or (memq article gnus-newsgroup-dormant)
8709                     (memq article gnus-newsgroup-marked))
8710           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8711
8712 (defun gnus-summary-limit (articles &optional pop)
8713   (if pop
8714       ;; We pop the previous limit off the stack and use that.
8715       (setq articles (car gnus-newsgroup-limits)
8716             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8717     ;; We use the new limit, so we push the old limit on the stack.
8718     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8719   ;; Set the limit.
8720   (setq gnus-newsgroup-limit articles)
8721   (let ((total (length gnus-newsgroup-data))
8722         (data (gnus-data-find-list (gnus-summary-article-number)))
8723         (gnus-summary-mark-below nil)   ; Inhibit this.
8724         found)
8725     ;; This will do all the work of generating the new summary buffer
8726     ;; according to the new limit.
8727     (gnus-summary-prepare)
8728     ;; Hide any threads, possibly.
8729     (gnus-summary-maybe-hide-threads)
8730     ;; Try to return to the article you were at, or one in the
8731     ;; neighborhood.
8732     (when data
8733       ;; We try to find some article after the current one.
8734       (while data
8735         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8736           (setq data nil
8737                 found t))
8738         (setq data (cdr data))))
8739     (unless found
8740       ;; If there is no data, that means that we were after the last
8741       ;; article.  The same goes when we can't find any articles
8742       ;; after the current one.
8743       (goto-char (point-max))
8744       (gnus-summary-find-prev))
8745     (gnus-set-mode-line 'summary)
8746     ;; We return how many articles were removed from the summary
8747     ;; buffer as a result of the new limit.
8748     (- total (length gnus-newsgroup-data))))
8749
8750 (defsubst gnus-invisible-cut-children (threads)
8751   (let ((num 0))
8752     (while threads
8753       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8754         (incf num))
8755       (pop threads))
8756     (< num 2)))
8757
8758 (defsubst gnus-cut-thread (thread)
8759   "Go forwards in the thread until we find an article that we want to display."
8760   (when (or (eq gnus-fetch-old-headers 'some)
8761             (eq gnus-fetch-old-headers 'invisible)
8762             (numberp gnus-fetch-old-headers)
8763             (eq gnus-build-sparse-threads 'some)
8764             (eq gnus-build-sparse-threads 'more))
8765     ;; Deal with old-fetched headers and sparse threads.
8766     (while (and
8767             thread
8768             (or
8769              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8770              (gnus-summary-article-ancient-p
8771               (mail-header-number (car thread))))
8772             (if (or (<= (length (cdr thread)) 1)
8773                     (eq gnus-fetch-old-headers 'invisible))
8774                 (setq gnus-newsgroup-limit
8775                       (delq (mail-header-number (car thread))
8776                             gnus-newsgroup-limit)
8777                       thread (cadr thread))
8778               (when (gnus-invisible-cut-children (cdr thread))
8779                 (let ((th (cdr thread)))
8780                   (while th
8781                     (if (memq (mail-header-number (caar th))
8782                               gnus-newsgroup-limit)
8783                         (setq thread (car th)
8784                               th nil)
8785                       (setq th (cdr th))))))))))
8786   thread)
8787
8788 (defun gnus-cut-threads (threads)
8789   "Cut off all uninteresting articles from the beginning of THREADS."
8790   (when (or (eq gnus-fetch-old-headers 'some)
8791             (eq gnus-fetch-old-headers 'invisible)
8792             (numberp gnus-fetch-old-headers)
8793             (eq gnus-build-sparse-threads 'some)
8794             (eq gnus-build-sparse-threads 'more))
8795     (let ((th threads))
8796       (while th
8797         (setcar th (gnus-cut-thread (car th)))
8798         (setq th (cdr th)))))
8799   ;; Remove nixed out threads.
8800   (delq nil threads))
8801
8802 (defun gnus-summary-initial-limit (&optional show-if-empty)
8803   "Figure out what the initial limit is supposed to be on group entry.
8804 This entails weeding out unwanted dormants, low-scored articles,
8805 fetch-old-headers verbiage, and so on."
8806   ;; Most groups have nothing to remove.
8807   (unless (or gnus-inhibit-limiting
8808               (and (null gnus-newsgroup-dormant)
8809                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8810                    (not (eq gnus-fetch-old-headers 'some))
8811                    (not (numberp gnus-fetch-old-headers))
8812                    (not (eq gnus-fetch-old-headers 'invisible))
8813                    (null gnus-summary-expunge-below)
8814                    (not (eq gnus-build-sparse-threads 'some))
8815                    (not (eq gnus-build-sparse-threads 'more))
8816                    (null gnus-thread-expunge-below)))
8817     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8818     (setq gnus-newsgroup-limit nil)
8819     (mapatoms
8820      (lambda (node)
8821        (unless (car (symbol-value node))
8822          ;; These threads have no parents -- they are roots.
8823          (let ((nodes (cdr (symbol-value node)))
8824                thread)
8825            (while nodes
8826              (if (and gnus-thread-expunge-below
8827                       (< (gnus-thread-total-score (car nodes))
8828                          gnus-thread-expunge-below))
8829                  (gnus-expunge-thread (pop nodes))
8830                (setq thread (pop nodes))
8831                (gnus-summary-limit-children thread))))))
8832      gnus-newsgroup-dependencies)
8833     ;; If this limitation resulted in an empty group, we might
8834     ;; pop the previous limit and use it instead.
8835     (when (and (not gnus-newsgroup-limit)
8836                show-if-empty)
8837       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8838     gnus-newsgroup-limit))
8839
8840 (defun gnus-summary-limit-children (thread)
8841   "Return 1 if this subthread is visible and 0 if it is not."
8842   ;; First we get the number of visible children to this thread.  This
8843   ;; is done by recursing down the thread using this function, so this
8844   ;; will really go down to a leaf article first, before slowly
8845   ;; working its way up towards the root.
8846   (when thread
8847     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8848            (children
8849            (if (cdr thread)
8850                (apply '+ (mapcar 'gnus-summary-limit-children
8851                                  (cdr thread)))
8852              0))
8853            (number (mail-header-number (car thread)))
8854            score)
8855       (if (and
8856            (not (memq number gnus-newsgroup-marked))
8857            (or
8858             ;; If this article is dormant and has absolutely no visible
8859             ;; children, then this article isn't visible.
8860             (and (memq number gnus-newsgroup-dormant)
8861                  (zerop children))
8862             ;; If this is "fetch-old-headered" and there is no
8863             ;; visible children, then we don't want this article.
8864             (and (or (eq gnus-fetch-old-headers 'some)
8865                      (numberp gnus-fetch-old-headers))
8866                  (gnus-summary-article-ancient-p number)
8867                  (zerop children))
8868             ;; If this is "fetch-old-headered" and `invisible', then
8869             ;; we don't want this article.
8870             (and (eq gnus-fetch-old-headers 'invisible)
8871                  (gnus-summary-article-ancient-p number))
8872             ;; If this is a sparsely inserted article with no children,
8873             ;; we don't want it.
8874             (and (eq gnus-build-sparse-threads 'some)
8875                  (gnus-summary-article-sparse-p number)
8876                  (zerop children))
8877             ;; If we use expunging, and this article is really
8878             ;; low-scored, then we don't want this article.
8879             (when (and gnus-summary-expunge-below
8880                        (< (setq score
8881                                 (or (cdr (assq number gnus-newsgroup-scored))
8882                                     gnus-summary-default-score))
8883                           gnus-summary-expunge-below))
8884               ;; We increase the expunge-tally here, but that has
8885               ;; nothing to do with the limits, really.
8886               (incf gnus-newsgroup-expunged-tally)
8887               ;; We also mark as read here, if that's wanted.
8888               (when (and gnus-summary-mark-below
8889                          (< score gnus-summary-mark-below))
8890                 (setq gnus-newsgroup-unreads
8891                       (delq number gnus-newsgroup-unreads))
8892                 (if gnus-newsgroup-auto-expire
8893                     (push number gnus-newsgroup-expirable)
8894                   (push (cons number gnus-low-score-mark)
8895                         gnus-newsgroup-reads)))
8896               t)
8897             ;; Do the `display' group parameter.
8898             (and gnus-newsgroup-display
8899                  (let ((gnus-number number))
8900                    (not (funcall gnus-newsgroup-display))))))
8901           ;; Nope, invisible article.
8902           0
8903         ;; Ok, this article is to be visible, so we add it to the limit
8904         ;; and return 1.
8905         (push number gnus-newsgroup-limit)
8906         1))))
8907
8908 (defun gnus-expunge-thread (thread)
8909   "Mark all articles in THREAD as read."
8910   (let* ((number (mail-header-number (car thread))))
8911     (incf gnus-newsgroup-expunged-tally)
8912     ;; We also mark as read here, if that's wanted.
8913     (setq gnus-newsgroup-unreads
8914           (delq number gnus-newsgroup-unreads))
8915     (if gnus-newsgroup-auto-expire
8916         (push number gnus-newsgroup-expirable)
8917       (push (cons number gnus-low-score-mark)
8918             gnus-newsgroup-reads)))
8919   ;; Go recursively through all subthreads.
8920   (mapcar 'gnus-expunge-thread (cdr thread)))
8921
8922 ;; Summary article oriented commands
8923
8924 (defun gnus-summary-refer-parent-article (n)
8925   "Refer parent article N times.
8926 If N is negative, go to ancestor -N instead.
8927 The difference between N and the number of articles fetched is returned."
8928   (interactive "p")
8929   (let ((skip 1)
8930         error header ref)
8931     (when (not (natnump n))
8932       (setq skip (abs n)
8933             n 1))
8934     (while (and (> n 0)
8935                 (not error))
8936       (setq header (gnus-summary-article-header))
8937       (if (and (eq (mail-header-number header)
8938                    (cdr gnus-article-current))
8939                (equal gnus-newsgroup-name
8940                       (car gnus-article-current)))
8941           ;; If we try to find the parent of the currently
8942           ;; displayed article, then we take a look at the actual
8943           ;; References header, since this is slightly more
8944           ;; reliable than the References field we got from the
8945           ;; server.
8946           (with-current-buffer gnus-original-article-buffer
8947             (nnheader-narrow-to-headers)
8948             (unless (setq ref (message-fetch-field "references"))
8949               (when (setq ref (message-fetch-field "in-reply-to"))
8950                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8951             (widen))
8952         (setq ref
8953               ;; It's not the current article, so we take a bet on
8954               ;; the value we got from the server.
8955               (mail-header-references header)))
8956       (if (and ref
8957                (not (equal ref "")))
8958           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8959             (gnus-message 1 "Couldn't find parent"))
8960         (gnus-message 1 "No references in article %d"
8961                       (gnus-summary-article-number))
8962         (setq error t))
8963       (decf n))
8964     (gnus-summary-position-point)
8965     n))
8966
8967 (defun gnus-summary-refer-references ()
8968   "Fetch all articles mentioned in the References header.
8969 Return the number of articles fetched."
8970   (interactive)
8971   (let ((ref (mail-header-references (gnus-summary-article-header)))
8972         (current (gnus-summary-article-number))
8973         (n 0))
8974     (if (or (not ref)
8975             (equal ref ""))
8976         (error "No References in the current article")
8977       ;; For each Message-ID in the References header...
8978       (while (string-match "<[^>]*>" ref)
8979         (incf n)
8980         ;; ... fetch that article.
8981         (gnus-summary-refer-article
8982          (prog1 (match-string 0 ref)
8983            (setq ref (substring ref (match-end 0))))))
8984       (gnus-summary-goto-subject current)
8985       (gnus-summary-position-point)
8986       n)))
8987
8988 (defun gnus-delete-duplicate-headers (headers)
8989   ;; First remove leading duplicates.
8990   (while (and (> (length headers) 1)
8991               (= (mail-header-number (car headers))
8992                  (mail-header-number (cadr headers))))
8993     (pop headers))
8994   ;; Then the rest.
8995   (let ((result headers))
8996     (while (> (length headers) 1)
8997       (if (= (mail-header-number (car headers))
8998              (mail-header-number (cadr headers)))
8999           (setcdr headers (cddr headers))
9000         (pop headers)))
9001     result))
9002
9003 (defun gnus-summary-refer-thread (&optional limit)
9004   "Fetch all articles in the current thread. For backends that
9005 know how to search for threads (currently only 'nnimap) a
9006 non-numeric prefix arg will use nnir to search the entire
9007 server; without a prefix arg only the current group is
9008 searched. If the variable `gnus-refer-thread-use-nnir' is
9009 non-nil the prefix arg has the reverse meaning. If no
9010 backend-specific 'request-thread function is available fetch
9011 LIMIT (the numerical prefix) old headers. If LIMIT is
9012 non-numeric or nil fetch the number specified by the
9013 `gnus-refer-thread-limit' variable."
9014   (interactive "P")
9015   (gnus-warp-to-article)
9016   (let* ((header (gnus-summary-article-header))
9017          (id (mail-header-id header))
9018          (gnus-inhibit-demon t)
9019          (gnus-summary-ignore-duplicates t)
9020          (gnus-read-all-available-headers t)
9021          (gnus-refer-thread-use-nnir
9022           (if (and (not (null limit)) (listp limit))
9023               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9024          (new-headers
9025           (if (gnus-check-backend-function
9026                'request-thread gnus-newsgroup-name)
9027               (gnus-request-thread header gnus-newsgroup-name)
9028             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9029                             gnus-refer-thread-limit))
9030                    (last (if (numberp limit)
9031                              (min (+ (mail-header-number header)
9032                                      limit)
9033                                   gnus-newsgroup-highest)
9034                            gnus-newsgroup-highest))
9035                    (subject (gnus-simplify-subject
9036                              (mail-header-subject header)))
9037                    (refs (split-string (or (mail-header-references header)
9038                                            "")))
9039                    (gnus-parse-headers-hook
9040                     `(lambda () (goto-char (point-min))
9041                       (keep-lines
9042                        (regexp-opt ',(append refs (list id subject)))))))
9043               (gnus-fetch-headers (list last) (if (numberp limit)
9044                                                   (* 2 limit) limit) t))))
9045          article-ids)
9046     (when (listp new-headers)
9047       (dolist (header new-headers)
9048         (push (mail-header-number header) article-ids)
9049         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9050           (push (mail-header-number header) gnus-newsgroup-unreads)
9051           (setq gnus-newsgroup-unselected
9052                 (delete (mail-header-number header)
9053                         gnus-newsgroup-unselected))))
9054       (setq gnus-newsgroup-headers
9055             (gnus-delete-duplicate-headers
9056              (gnus-merge
9057               'list gnus-newsgroup-headers new-headers
9058               'gnus-article-sort-by-number)))
9059       (setq gnus-newsgroup-articles
9060             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9061       (gnus-summary-limit-include-thread id)))
9062   (gnus-summary-show-thread))
9063
9064 (defun gnus-summary-refer-article (message-id)
9065   "Fetch an article specified by MESSAGE-ID."
9066   (interactive "sMessage-ID: ")
9067   (gnus-warp-to-article)
9068   (when (and (stringp message-id)
9069              (not (zerop (length message-id))))
9070     (setq message-id (gnus-replace-in-string message-id " " ""))
9071     ;; Construct the correct Message-ID if necessary.
9072     ;; Suggested by tale@pawl.rpi.edu.
9073     (unless (string-match "^<" message-id)
9074       (setq message-id (concat "<" message-id)))
9075     (unless (string-match ">$" message-id)
9076       (setq message-id (concat message-id ">")))
9077     ;; People often post MIDs from URLs, so unhex it:
9078     (unless (string-match "@" message-id)
9079       (setq message-id (gnus-url-unhex-string message-id)))
9080     (let* ((header (gnus-id-to-header message-id))
9081            (sparse (and header
9082                         (gnus-summary-article-sparse-p
9083                          (mail-header-number header))
9084                         (memq (mail-header-number header)
9085                               gnus-newsgroup-limit)))
9086            number)
9087       (cond
9088        ;; If the article is present in the buffer we just go to it.
9089        ((and header
9090              (or (not (gnus-summary-article-sparse-p
9091                        (mail-header-number header)))
9092                  sparse))
9093         (prog1
9094             (gnus-summary-goto-article
9095              (mail-header-number header) nil t)
9096           (when sparse
9097             (gnus-summary-update-article (mail-header-number header)))))
9098        (t
9099         ;; We fetch the article.
9100         (catch 'found
9101           (dolist (gnus-override-method (gnus-refer-article-methods))
9102             (when (and (gnus-check-server gnus-override-method)
9103                        ;; Fetch the header,
9104                        (setq number (gnus-summary-insert-subject message-id)))
9105               ;; and display the article.
9106               (gnus-summary-select-article nil nil nil number)
9107               (throw 'found t)))
9108           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9109
9110 (defun gnus-refer-article-methods ()
9111   "Return a list of referable methods."
9112   (cond
9113    ;; No method, so we default to current and native.
9114    ((null gnus-refer-article-method)
9115     (list gnus-current-select-method gnus-select-method))
9116    ;; Current.
9117    ((eq 'current gnus-refer-article-method)
9118     (list gnus-current-select-method))
9119    ;; List of select methods.
9120    ((not (and (symbolp (car gnus-refer-article-method))
9121               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9122     (let (out)
9123       (dolist (method gnus-refer-article-method)
9124         (push (if (eq 'current method)
9125                   gnus-current-select-method
9126                 (if (eq 'nnir (car method))
9127                     (list
9128                      'nnir
9129                      (or (cadr method)
9130                          (gnus-method-to-server gnus-current-select-method)))
9131                   method))
9132               out))
9133       (nreverse out)))
9134    ;; One single select method.
9135    (t
9136     (list gnus-refer-article-method))))
9137
9138 (defun gnus-summary-edit-parameters ()
9139   "Edit the group parameters of the current group."
9140   (interactive)
9141   (gnus-group-edit-group gnus-newsgroup-name 'params))
9142
9143 (defun gnus-summary-customize-parameters ()
9144   "Customize the group parameters of the current group."
9145   (interactive)
9146   (gnus-group-customize gnus-newsgroup-name))
9147
9148 (defun gnus-summary-enter-digest-group (&optional force)
9149   "Enter an nndoc group based on the current article.
9150 If FORCE, force a digest interpretation.  If not, try to guess
9151 what the document format is.
9152
9153 To control what happens when you exit the group, see the
9154 `gnus-auto-select-on-ephemeral-exit' variable."
9155   (interactive "P")
9156   (let ((conf gnus-current-window-configuration))
9157     (save-window-excursion
9158       (save-excursion
9159         (let (gnus-article-prepare-hook
9160               gnus-display-mime-function
9161               gnus-break-pages)
9162           (gnus-summary-select-article))))
9163     (setq gnus-current-window-configuration conf)
9164     (let* ((name (format "%s-%d"
9165                          (gnus-group-prefixed-name
9166                           gnus-newsgroup-name (list 'nndoc ""))
9167                          (with-current-buffer gnus-summary-buffer
9168                            gnus-current-article)))
9169            (ogroup gnus-newsgroup-name)
9170            (params (append (gnus-info-params (gnus-get-info ogroup))
9171                            (list (cons 'to-group ogroup))
9172                            (list (cons 'parent-group ogroup))
9173                            (list (cons 'save-article-group ogroup))))
9174            (case-fold-search t)
9175            (buf (current-buffer))
9176            dig to-address)
9177       (with-current-buffer gnus-original-article-buffer
9178         ;; Have the digest group inherit the main mail address of
9179         ;; the parent article.
9180         (when (setq to-address (or (gnus-fetch-field "reply-to")
9181                                    (gnus-fetch-field "from")))
9182           (setq params
9183                 (append
9184                  (list (cons 'to-address
9185                              (funcall gnus-decode-encoded-address-function
9186                                       to-address))))))
9187         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9188         (insert-buffer-substring gnus-original-article-buffer)
9189         ;; Remove lines that may lead nndoc to misinterpret the
9190         ;; document type.
9191         (narrow-to-region
9192          (goto-char (point-min))
9193          (or (search-forward "\n\n" nil t) (point)))
9194         (goto-char (point-min))
9195         (delete-matching-lines "^Path:\\|^From ")
9196         (widen))
9197       (unwind-protect
9198           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9199                     (gnus-newsgroup-ephemeral-ignored-charsets
9200                      gnus-newsgroup-ignored-charsets))
9201                 (gnus-group-read-ephemeral-group
9202                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9203                               (nndoc-article-type
9204                                ,(if force 'mbox 'guess)))
9205                  t nil nil nil
9206                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9207                                                         "ADAPT")))))
9208               ;; Make all postings to this group go to the parent group.
9209               (nconc (gnus-info-params (gnus-get-info name))
9210                      params)
9211             ;; Couldn't select this doc group.
9212             (switch-to-buffer buf)
9213             (gnus-set-global-variables)
9214             (gnus-configure-windows 'summary)
9215             (gnus-message 3 "Article couldn't be entered?"))
9216         (kill-buffer dig)))))
9217
9218 (defun gnus-summary-read-document (n)
9219   "Open a new group based on the current article(s).
9220 This will allow you to read digests and other similar
9221 documents as newsgroups.
9222 Obeys the standard process/prefix convention."
9223   (interactive "P")
9224   (let* ((ogroup gnus-newsgroup-name)
9225          (params (append (gnus-info-params (gnus-get-info ogroup))
9226                          (list (cons 'to-group ogroup))))
9227          group egroup groups vgroup)
9228     (dolist (article (gnus-summary-work-articles n))
9229       (setq group (format "%s-%d" gnus-newsgroup-name article))
9230       (gnus-summary-remove-process-mark article)
9231       (when (gnus-summary-display-article article)
9232         (save-excursion ;;What for?
9233           (with-temp-buffer
9234             (insert-buffer-substring gnus-original-article-buffer)
9235             ;; Remove some headers that may lead nndoc to make
9236             ;; the wrong guess.
9237             (message-narrow-to-head)
9238             (goto-char (point-min))
9239             (delete-matching-lines "^Path:\\|^From ")
9240             (widen)
9241             (if (setq egroup
9242                       (gnus-group-read-ephemeral-group
9243                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9244                                      (nndoc-article-type guess))
9245                        t nil t))
9246                 (progn
9247                   ;; Make all postings to this group go to the parent group.
9248                   (nconc (gnus-info-params (gnus-get-info egroup))
9249                          params)
9250                   (push egroup groups))
9251               ;; Couldn't select this doc group.
9252               (gnus-error 3 "Article couldn't be entered"))))))
9253     ;; Now we have selected all the documents.
9254     (cond
9255      ((not groups)
9256       (error "None of the articles could be interpreted as documents"))
9257      ((gnus-group-read-ephemeral-group
9258        (setq vgroup (format
9259                      "nnvirtual:%s-%s" gnus-newsgroup-name
9260                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9261        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9262        t
9263        (cons (current-buffer) 'summary)))
9264      (t
9265       (error "Couldn't select virtual nndoc group")))))
9266
9267 (defun gnus-summary-widget-forward (arg)
9268   "Move point to the next field or button in the article.
9269 With optional ARG, move across that many fields."
9270   (interactive "p")
9271   (gnus-summary-select-article)
9272   (gnus-configure-windows 'article)
9273   (select-window (gnus-get-buffer-window gnus-article-buffer))
9274   (widget-forward arg))
9275
9276 (defun gnus-summary-widget-backward (arg)
9277   "Move point to the previous field or button in the article.
9278 With optional ARG, move across that many fields."
9279   (interactive "p")
9280   (gnus-summary-select-article)
9281   (gnus-configure-windows 'article)
9282   (select-window (gnus-get-buffer-window gnus-article-buffer))
9283   (unless (widget-at (point))
9284     (goto-char (point-max)))
9285   (widget-backward arg))
9286
9287 (defun gnus-summary-isearch-article (&optional regexp-p)
9288   "Do incremental search forward on the current article.
9289 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9290   (interactive "P")
9291   (gnus-summary-select-article)
9292   (gnus-configure-windows 'article)
9293   (gnus-eval-in-buffer-window gnus-article-buffer
9294     (save-restriction
9295       (widen)
9296       (isearch-forward regexp-p))))
9297
9298 (defun gnus-summary-repeat-search-article-forward ()
9299   "Repeat the previous search forwards."
9300   (interactive)
9301   (unless gnus-last-search-regexp
9302     (error "No previous search"))
9303   (gnus-summary-search-article-forward gnus-last-search-regexp))
9304
9305 (defun gnus-summary-repeat-search-article-backward ()
9306   "Repeat the previous search backwards."
9307   (interactive)
9308   (unless gnus-last-search-regexp
9309     (error "No previous search"))
9310   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9311
9312 (defun gnus-summary-search-article-forward (regexp &optional backward)
9313   "Search for an article containing REGEXP forward.
9314 If BACKWARD, search backward instead."
9315   (interactive
9316    (list (read-string
9317           (format "Search article %s (regexp%s): "
9318                   (if current-prefix-arg "backward" "forward")
9319                   (if gnus-last-search-regexp
9320                       (concat ", default " gnus-last-search-regexp)
9321                     "")))
9322          current-prefix-arg))
9323   (if (string-equal regexp "")
9324       (setq regexp (or gnus-last-search-regexp ""))
9325     (setq gnus-last-search-regexp regexp)
9326     (setq gnus-article-before-search gnus-current-article))
9327   ;; Intentionally set gnus-last-article.
9328   (setq gnus-last-article gnus-article-before-search)
9329   (let ((gnus-last-article gnus-last-article))
9330     (if (gnus-summary-search-article regexp backward)
9331         (gnus-summary-show-thread)
9332       (signal 'search-failed (list regexp)))))
9333
9334 (defun gnus-summary-search-article-backward (regexp)
9335   "Search for an article containing REGEXP backward."
9336   (interactive
9337    (list (read-string
9338           (format "Search article backward (regexp%s): "
9339                   (if gnus-last-search-regexp
9340                       (concat ", default " gnus-last-search-regexp)
9341                     "")))))
9342   (gnus-summary-search-article-forward regexp 'backward))
9343
9344 (defun gnus-summary-search-article (regexp &optional backward)
9345   "Search for an article containing REGEXP.
9346 Optional argument BACKWARD means do search for backward.
9347 `gnus-select-article-hook' is not called during the search."
9348   ;; We have to require this here to make sure that the following
9349   ;; dynamic binding isn't shadowed by autoloading.
9350   (require 'gnus-async)
9351   (require 'gnus-art)
9352   (let ((gnus-select-article-hook nil)  ;Disable hook.
9353         (gnus-article-prepare-hook nil)
9354         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9355         (gnus-use-article-prefetch nil)
9356         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9357         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9358         (gnus-visual nil)
9359         (gnus-keep-backlog nil)
9360         (gnus-break-pages nil)
9361         (gnus-summary-display-arrow nil)
9362         (gnus-updated-mode-lines nil)
9363         (gnus-auto-center-summary nil)
9364         (sum (current-buffer))
9365         (gnus-display-mime-function nil)
9366         (found nil)
9367         point)
9368     (gnus-save-hidden-threads
9369       (gnus-summary-select-article)
9370       (set-buffer gnus-article-buffer)
9371       (goto-char (window-point (get-buffer-window (current-buffer))))
9372       (when backward
9373         (forward-line -1))
9374       (while (not found)
9375         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9376         (if (if backward
9377                 (re-search-backward regexp nil t)
9378               (re-search-forward regexp nil t))
9379             ;; We found the regexp.
9380             (progn
9381               (setq found 'found)
9382               (beginning-of-line)
9383               (set-window-start
9384                (get-buffer-window (current-buffer))
9385                (point))
9386               (forward-line 1)
9387               (set-window-point
9388                (get-buffer-window (current-buffer))
9389                (point))
9390               (set-buffer sum)
9391               (setq point (point)))
9392           ;; We didn't find it, so we go to the next article.
9393           (set-buffer sum)
9394           (setq found 'not)
9395           (while (eq found 'not)
9396             (if (not (if backward (gnus-summary-find-prev)
9397                        (gnus-summary-find-next)))
9398                 ;; No more articles.
9399                 (setq found t)
9400               ;; Select the next article and adjust point.
9401               (unless (gnus-summary-article-sparse-p
9402                        (gnus-summary-article-number))
9403                 (setq found nil)
9404                 (gnus-summary-select-article)
9405                 (set-buffer gnus-article-buffer)
9406                 (widen)
9407                 (goto-char (if backward (point-max) (point-min))))))))
9408       (gnus-message 7 ""))
9409     ;; Return whether we found the regexp.
9410     (when (eq found 'found)
9411       (goto-char point)
9412       (gnus-summary-show-thread)
9413       (gnus-summary-goto-subject gnus-current-article)
9414       (gnus-summary-position-point)
9415       t)))
9416
9417 (defun gnus-find-matching-articles (header regexp)
9418   "Return a list of all articles that match REGEXP on HEADER.
9419 This search includes all articles in the current group that Gnus has
9420 fetched headers for, whether they are displayed or not."
9421   (let ((articles nil)
9422         ;; Can't eta-reduce because it's a macro.
9423         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9424         (case-fold-search t))
9425     (dolist (header gnus-newsgroup-headers)
9426       (when (string-match regexp (funcall func header))
9427         (push (mail-header-number header) articles)))
9428     (nreverse articles)))
9429
9430 (defun gnus-summary-find-matching (header regexp &optional backward unread
9431                                           not-case-fold not-matching)
9432   "Return a list of all articles that match REGEXP on HEADER.
9433 The search stars on the current article and goes forwards unless
9434 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9435 If UNREAD is non-nil, only unread articles will
9436 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9437 in the comparisons. If NOT-MATCHING, return a list of all articles that
9438 not match REGEXP on HEADER."
9439   (let ((case-fold-search (not not-case-fold))
9440         articles d func)
9441     (if (consp header)
9442         (if (eq (car header) 'extra)
9443             (setq func
9444                   `(lambda (h)
9445                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9446                          "")))
9447           (error "%s is an invalid header" header))
9448       (unless (fboundp (intern (concat "mail-header-" header)))
9449         (error "%s is not a valid header" header))
9450       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9451     (dolist (d (if (eq backward 'all)
9452                    gnus-newsgroup-data
9453                  (gnus-data-find-list
9454                   (gnus-summary-article-number)
9455                   (gnus-data-list backward))))
9456       (when (and (or (not unread)       ; We want all articles...
9457                      (gnus-data-unread-p d)) ; Or just unreads.
9458                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9459                  (if not-matching
9460                      (not (string-match
9461                            regexp
9462                            (funcall func (gnus-data-header d))))
9463                    (string-match regexp
9464                                  (funcall func (gnus-data-header d)))))
9465         (push (gnus-data-number d) articles))) ; Success!
9466     (nreverse articles)))
9467
9468 (defun gnus-summary-execute-command (header regexp command &optional backward)
9469   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9470 If HEADER is an empty string (or nil), the match is done on the entire
9471 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9472   (interactive
9473    (list (let ((completion-ignore-case t))
9474            (gnus-completing-read
9475             "Header name"
9476             (mapcar 'symbol-name
9477                     (append
9478                      '(Number Subject From Lines Date
9479                        Message-ID Xref References Body)
9480                      gnus-extra-headers))
9481             'require-match))
9482          (read-string "Regexp: ")
9483          (read-key-sequence "Command: ")
9484          current-prefix-arg))
9485   (when (equal header "Body")
9486     (setq header ""))
9487   ;; Hidden thread subtrees must be searched as well.
9488   (gnus-summary-show-all-threads)
9489   ;; We don't want to change current point nor window configuration.
9490   (save-excursion
9491     (save-window-excursion
9492       (let (gnus-visual
9493             gnus-treat-strip-trailing-blank-lines
9494             gnus-treat-strip-leading-blank-lines
9495             gnus-treat-strip-multiple-blank-lines
9496             gnus-treat-hide-boring-headers
9497             gnus-treat-fold-newsgroups
9498             gnus-article-prepare-hook)
9499         (gnus-message 6 "Executing %s..." (key-description command))
9500         ;; We'd like to execute COMMAND interactively so as to give arguments.
9501         (gnus-execute header regexp
9502                       `(call-interactively ',(key-binding command))
9503                       backward)
9504         (gnus-message 6 "Executing %s...done" (key-description command))))))
9505
9506 (defun gnus-summary-beginning-of-article ()
9507   "Scroll the article back to the beginning."
9508   (interactive)
9509   (gnus-summary-select-article)
9510   (gnus-configure-windows 'article)
9511   (gnus-eval-in-buffer-window gnus-article-buffer
9512     (widen)
9513     (goto-char (point-min))
9514     (when gnus-break-pages
9515       (gnus-narrow-to-page))))
9516
9517 (defun gnus-summary-end-of-article ()
9518   "Scroll to the end of the article."
9519   (interactive)
9520   (gnus-summary-select-article)
9521   (gnus-configure-windows 'article)
9522   (gnus-eval-in-buffer-window gnus-article-buffer
9523     (widen)
9524     (goto-char (point-max))
9525     (recenter -3)
9526     (when gnus-break-pages
9527       (gnus-narrow-to-page))))
9528
9529 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9530   "Truncate to LEN and quote all \"(\"'s in STRING."
9531   (gnus-replace-in-string (if (and len (> (length string) len))
9532                               (substring string 0 len)
9533                             string)
9534                           "[()]" "\\\\\\&"))
9535
9536 (defun gnus-summary-print-article (&optional filename n)
9537   "Generate and print a PostScript image of the process-marked (mail) articles.
9538
9539 If used interactively, print the current article if none are
9540 process-marked.  With prefix arg, prompt the user for the name of the
9541 file to save in.
9542
9543 When used from Lisp, accept two optional args FILENAME and N.  N means
9544 to print the next N articles.  If N is negative, print the N previous
9545 articles.  If N is nil and articles have been marked with the process
9546 mark, print these instead.
9547
9548 If the optional first argument FILENAME is nil, send the image to the
9549 printer.  If FILENAME is a string, save the PostScript image in a file with
9550 that name.  If FILENAME is a number, prompt the user for the name of the file
9551 to save in."
9552   (interactive (list (ps-print-preprint current-prefix-arg)))
9553   (dolist (article (gnus-summary-work-articles n))
9554     (gnus-summary-select-article nil nil 'pseudo article)
9555     (gnus-eval-in-buffer-window gnus-article-buffer
9556       (gnus-print-buffer))
9557     (gnus-summary-remove-process-mark article))
9558   (ps-despool filename))
9559
9560 (defun gnus-print-buffer ()
9561   (let ((ps-left-header
9562          (list
9563           (concat "("
9564                   (gnus-summary-print-truncate-and-quote
9565                    (mail-header-subject gnus-current-headers)
9566                    66) ")")
9567           (concat "("
9568                   (gnus-summary-print-truncate-and-quote
9569                    (mail-header-from gnus-current-headers)
9570                    45) ")")))
9571         (ps-right-header
9572          (list
9573           "/pagenumberstring load"
9574           (concat "("
9575                   (mail-header-date gnus-current-headers) ")"))))
9576     (gnus-run-hooks 'gnus-ps-print-hook)
9577     (save-excursion
9578       (if ps-print-color-p
9579           (ps-spool-buffer-with-faces)
9580         (ps-spool-buffer)))))
9581
9582 (defun gnus-summary-show-complete-article ()
9583   "Show a complete version of the current article.
9584 This is only useful if you're looking at a partial version of the
9585 article currently."
9586   (interactive)
9587   (let ((gnus-keep-backlog nil)
9588         (gnus-use-cache nil)
9589         (gnus-agent nil)
9590         (variable (intern
9591                    (format "%s-fetch-partial-articles"
9592                            (car (gnus-find-method-for-group
9593                                  gnus-newsgroup-name)))
9594                    obarray))
9595         old-val)
9596     (unwind-protect
9597         (progn
9598           (setq old-val (symbol-value variable))
9599           (set variable nil)
9600           (gnus-flush-original-article-buffer)
9601           (gnus-summary-show-article))
9602       (set variable old-val))))
9603
9604 (defun gnus-summary-show-article (&optional arg)
9605   "Force redisplaying of the current article.
9606 If ARG (the prefix) is a number, show the article with the charset
9607 defined in `gnus-summary-show-article-charset-alist', or the charset
9608 input.
9609 If ARG (the prefix) is non-nil and not a number, show the article,
9610 but without running any of the article treatment functions
9611 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9612 C-u g', show the raw article."
9613   (interactive "P")
9614   (cond
9615    ((numberp arg)
9616     (gnus-summary-show-article t)
9617     (let ((gnus-newsgroup-charset
9618            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9619                (mm-read-coding-system
9620                 "View as charset: " ;; actually it is coding system.
9621                 (with-current-buffer gnus-article-buffer
9622                   (mm-detect-coding-region (point) (point-max))))))
9623           (gnus-newsgroup-ignored-charsets 'gnus-all))
9624       (gnus-summary-select-article nil 'force)
9625       (let ((deps gnus-newsgroup-dependencies)
9626             head header lines)
9627         (with-current-buffer gnus-original-article-buffer
9628           (save-restriction
9629             (message-narrow-to-head)
9630             (setq head (buffer-string))
9631             (goto-char (point-min))
9632             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9633               (goto-char (point-max))
9634               (widen)
9635               (setq lines (1- (count-lines (point) (point-max))))))
9636           (with-temp-buffer
9637             (insert (format "211 %d Article retrieved.\n"
9638                             (cdr gnus-article-current)))
9639             (insert head)
9640             (if lines (insert (format "Lines: %d\n" lines)))
9641             (insert ".\n")
9642             (let ((nntp-server-buffer (current-buffer)))
9643               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9644         (gnus-data-set-header
9645          (gnus-data-find (cdr gnus-article-current))
9646          header)
9647         (gnus-summary-update-article-line
9648          (cdr gnus-article-current) header)
9649         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9650           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9651    ((not arg)
9652     ;; Select the article the normal way.
9653     (if (eq mm-text-html-renderer 'shr)
9654         (progn
9655           (require 'shr)
9656           (let ((shr-ignore-cache t))
9657             (gnus-summary-select-article nil 'force)))
9658       (gnus-summary-select-article nil 'force)))
9659    ((equal arg '(16))
9660     ;; C-u C-u g
9661     (let ((gnus-inhibit-article-treatments t))
9662       (gnus-summary-select-article nil 'force)))
9663    (t
9664     ;; We have to require this here to make sure that the following
9665     ;; dynamic binding isn't shadowed by autoloading.
9666     (require 'gnus-async)
9667     (require 'gnus-art)
9668     ;; Bind the article treatment functions to nil.
9669     (let ((gnus-have-all-headers t)
9670           gnus-article-prepare-hook
9671           gnus-article-decode-hook
9672           gnus-display-mime-function
9673           gnus-break-pages)
9674       ;; Destroy any MIME parts.
9675       (when (gnus-buffer-live-p gnus-article-buffer)
9676         (with-current-buffer gnus-article-buffer
9677           (gnus-article-stop-animations)
9678           (mm-destroy-parts gnus-article-mime-handles)
9679           ;; Set it to nil for safety reason.
9680           (setq gnus-article-mime-handle-alist nil)
9681           (setq gnus-article-mime-handles nil)))
9682       (gnus-summary-select-article nil 'force))))
9683   (gnus-summary-goto-subject gnus-current-article)
9684   (gnus-summary-position-point))
9685
9686 (defun gnus-summary-show-raw-article ()
9687   "Show the raw article without any article massaging functions being run."
9688   (interactive)
9689   (gnus-summary-show-article t))
9690
9691 (defun gnus-summary-verbose-headers (&optional arg)
9692   "Toggle permanent full header display.
9693 If ARG is a positive number, turn header display on.
9694 If ARG is a negative number, turn header display off."
9695   (interactive "P")
9696   (setq gnus-show-all-headers
9697         (cond ((or (not (numberp arg))
9698                    (zerop arg))
9699                (not gnus-show-all-headers))
9700               ((natnump arg)
9701                t)))
9702   (gnus-summary-show-article))
9703
9704 (defun gnus-summary-toggle-header (&optional arg)
9705   "Show the headers if they are hidden, or hide them if they are shown.
9706 If ARG is a positive number, show the entire header.
9707 If ARG is a negative number, hide the unwanted header lines."
9708   (interactive "P")
9709   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9710                      (get-buffer-window gnus-article-buffer t))))
9711     (with-current-buffer gnus-article-buffer
9712       (widen)
9713       (article-narrow-to-head)
9714       (let* ((inhibit-read-only t)
9715              (inhibit-point-motion-hooks t)
9716              (hidden (if (numberp arg)
9717                          (>= arg 0)
9718                        (or (not (looking-at "[^ \t\n]+:"))
9719                            (gnus-article-hidden-text-p 'headers))))
9720              s e)
9721         (delete-region (point-min) (point-max))
9722         (with-current-buffer gnus-original-article-buffer
9723           (goto-char (setq s (point-min)))
9724           (setq e (if (search-forward "\n\n" nil t)
9725                       (1- (point))
9726                     (point-max))))
9727         (insert-buffer-substring gnus-original-article-buffer s e)
9728         (run-hooks 'gnus-article-decode-hook)
9729         (if hidden
9730             (let ((gnus-treat-hide-headers nil)
9731                   (gnus-treat-hide-boring-headers nil))
9732               (gnus-delete-wash-type 'headers)
9733               (gnus-treat-article 'head))
9734           (gnus-treat-article 'head))
9735         (widen)
9736         (if window
9737             (set-window-start window (goto-char (point-min))))
9738         (if gnus-break-pages
9739             (gnus-narrow-to-page)
9740           (when (gnus-visual-p 'page-marker)
9741             (let ((inhibit-read-only t))
9742               (gnus-remove-text-with-property 'gnus-prev)
9743               (gnus-remove-text-with-property 'gnus-next))))
9744         (gnus-set-mode-line 'article)))))
9745
9746 (defun gnus-summary-show-all-headers ()
9747   "Make all header lines visible."
9748   (interactive)
9749   (gnus-summary-toggle-header 1))
9750
9751 (defun gnus-summary-caesar-message (&optional arg)
9752   "Caesar rotate the current article by 13.
9753 With a non-numerical prefix, also rotate headers.  A numerical
9754 prefix specifies how many places to rotate each letter forward."
9755   (interactive "P")
9756   (gnus-summary-select-article)
9757   (let ((mail-header-separator ""))
9758     (gnus-eval-in-buffer-window gnus-article-buffer
9759       (save-restriction
9760         (widen)
9761         (let ((start (window-start))
9762               (inhibit-read-only t))
9763           (if (equal arg '(4))
9764               (message-caesar-buffer-body nil t)
9765             (message-caesar-buffer-body arg))
9766           (set-window-start (get-buffer-window (current-buffer)) start)))))
9767   ;; Create buttons and stuff...
9768   (gnus-treat-article nil))
9769
9770 (declare-function idna-to-unicode "ext:idna" (str))
9771
9772 (defun gnus-summary-idna-message (&optional arg)
9773   "Decode IDNA encoded domain names in the current articles.
9774 IDNA encoded domain names looks like `xn--bar'.  If a string
9775 remain unencoded after running this function, it is likely an
9776 invalid IDNA string (`xn--bar' is invalid).
9777
9778 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9779 installed for this command to work."
9780   (interactive "P")
9781   (if (not (and (condition-case nil (require 'idna)
9782                   (file-error))
9783                 (mm-coding-system-p 'utf-8)
9784                 (executable-find (symbol-value 'idna-program))))
9785       (gnus-message
9786        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9787     (gnus-summary-select-article)
9788     (let ((mail-header-separator ""))
9789       (gnus-eval-in-buffer-window gnus-article-buffer
9790         (save-restriction
9791           (widen)
9792           (let ((start (window-start))
9793                 buffer-read-only)
9794             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9795               (replace-match (idna-to-unicode (match-string 1))))
9796             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9797
9798 (defun gnus-summary-morse-message (&optional arg)
9799   "Morse decode the current article."
9800   (interactive "P")
9801   (gnus-summary-select-article)
9802   (let ((mail-header-separator ""))
9803     (gnus-eval-in-buffer-window gnus-article-buffer
9804       (save-excursion
9805         (save-restriction
9806           (widen)
9807           (let ((pos (window-start))
9808                 (inhibit-read-only t))
9809             (goto-char (point-min))
9810             (when (message-goto-body)
9811               (gnus-narrow-to-body))
9812             (goto-char (point-min))
9813             (while (search-forward "·" (point-max) t)
9814               (replace-match "."))
9815             (unmorse-region (point-min) (point-max))
9816             (widen)
9817             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9818
9819 (defun gnus-summary-stop-page-breaking ()
9820   "Stop page breaking in the current article."
9821   (interactive)
9822   (gnus-summary-select-article)
9823   (gnus-eval-in-buffer-window gnus-article-buffer
9824     (widen)
9825     (when (gnus-visual-p 'page-marker)
9826       (let ((inhibit-read-only t))
9827         (gnus-remove-text-with-property 'gnus-prev)
9828         (gnus-remove-text-with-property 'gnus-next))
9829       (setq gnus-page-broken nil))))
9830
9831 (defun gnus-summary-move-article (&optional n to-newsgroup
9832                                             select-method action)
9833   "Move the current article to a different newsgroup.
9834 If N is a positive number, move the N next articles.
9835 If N is a negative number, move the N previous articles.
9836 If N is nil and any articles have been marked with the process mark,
9837 move those articles instead.
9838 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9839 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9840 re-spool using this method.
9841
9842 When called interactively with TO-NEWSGROUP being nil, the value of
9843 the variable `gnus-move-split-methods' is used for finding a default
9844 for the target newsgroup.
9845
9846 For this function to work, both the current newsgroup and the
9847 newsgroup that you want to move to have to support the `request-move'
9848 and `request-accept' functions.
9849
9850 ACTION can be either `move' (the default), `crosspost' or `copy'."
9851   (interactive "P")
9852   (unless action
9853     (setq action 'move))
9854   ;; Check whether the source group supports the required functions.
9855   (cond ((and (eq action 'move)
9856               (not (gnus-check-backend-function
9857                     'request-move-article gnus-newsgroup-name)))
9858          (error "The current group does not support article moving"))
9859         ((and (eq action 'crosspost)
9860               (not (gnus-check-backend-function
9861                     'request-replace-article gnus-newsgroup-name)))
9862          (error "The current group does not support article editing")))
9863   (let ((articles (gnus-summary-work-articles n))
9864         (prefix (if (gnus-check-backend-function
9865                      'request-move-article gnus-newsgroup-name)
9866                     (funcall gnus-move-group-prefix-function
9867                              gnus-newsgroup-name)
9868                   ""))
9869         (names '((move "Move" "Moving")
9870                  (copy "Copy" "Copying")
9871                  (crosspost "Crosspost" "Crossposting")))
9872         (copy-buf (save-excursion
9873                     (nnheader-set-temp-buffer " *copy article*")))
9874         art-group to-method new-xref article to-groups
9875         articles-to-update-marks encoded)
9876     (unless (assq action names)
9877       (error "Unknown action %s" action))
9878     ;; Read the newsgroup name.
9879     (when (and (not to-newsgroup)
9880                (not select-method))
9881       (if (and gnus-move-split-methods
9882                (not
9883                 (and (memq gnus-current-article articles)
9884                      (gnus-buffer-live-p gnus-original-article-buffer))))
9885           ;; When `gnus-move-split-methods' is non-nil, we have to
9886           ;; select an article to give `gnus-read-move-group-name' an
9887           ;; opportunity to suggest an appropriate default.  However,
9888           ;; we needn't render or mark the article.
9889           (let ((gnus-display-mime-function nil)
9890                 (gnus-article-prepare-hook nil)
9891                 (gnus-mark-article-hook nil))
9892             (gnus-summary-select-article nil nil nil (car articles))))
9893       (setq to-newsgroup (gnus-read-move-group-name
9894                           (cadr (assq action names))
9895                           (symbol-value
9896                            (intern (format "gnus-current-%s-group" action)))
9897                           articles prefix)
9898             encoded to-newsgroup
9899             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9900       (set (intern (format "gnus-current-%s-group" action))
9901            (mm-decode-coding-string
9902             to-newsgroup
9903             (gnus-group-name-charset to-method to-newsgroup))))
9904     (unless to-method
9905       (setq to-method (or select-method
9906                           (gnus-server-to-method
9907                            (gnus-group-method to-newsgroup)))))
9908     (setq to-newsgroup
9909           (or encoded
9910               (and to-newsgroup
9911                    (mm-encode-coding-string
9912                     to-newsgroup
9913                     (gnus-group-name-charset to-method to-newsgroup)))))
9914     ;; Check the method we are to move this article to...
9915     (unless (gnus-check-backend-function
9916              'request-accept-article (car to-method))
9917       (error "%s does not support article copying" (car to-method)))
9918     (unless (gnus-check-server to-method)
9919       (error "Can't open server %s" (car to-method)))
9920     (gnus-message 6 "%s to %s: %s..."
9921                   (caddr (assq action names))
9922                   (or (car select-method)
9923                       (gnus-group-decoded-name to-newsgroup))
9924                   articles)
9925     (while articles
9926       (setq article (pop articles))
9927       ;; Set any marks that may have changed in the summary buffer.
9928       (when gnus-preserve-marks
9929         (gnus-summary-push-marks-to-backend article))
9930       (setq
9931        art-group
9932        (cond
9933         ;; Move the article.
9934         ((eq action 'move)
9935          ;; Remove this article from future suppression.
9936          (gnus-dup-unsuppress-article article)
9937          (let* ((from-method (gnus-find-method-for-group
9938                               gnus-newsgroup-name))
9939                 (to-method (or select-method
9940                                (gnus-find-method-for-group to-newsgroup)))
9941                 (move-is-internal (gnus-server-equal from-method to-method)))
9942            (gnus-request-move-article
9943             article                     ; Article to move
9944             gnus-newsgroup-name         ; From newsgroup
9945             (nth 1 (gnus-find-method-for-group
9946                     gnus-newsgroup-name)) ; Server
9947             (list 'gnus-request-accept-article
9948                   to-newsgroup (list 'quote select-method)
9949                   (not articles) t)     ; Accept form
9950             (not articles)              ; Only save nov last time
9951             (and move-is-internal
9952                  to-newsgroup           ; Not respooling
9953                                         ; Is this move internal?
9954                  (gnus-group-real-name to-newsgroup)))))
9955         ;; Copy the article.
9956         ((eq action 'copy)
9957          (with-current-buffer copy-buf
9958            (when (gnus-request-article-this-buffer article
9959                                                    gnus-newsgroup-name)
9960              (save-restriction
9961                (nnheader-narrow-to-headers)
9962                (dolist (hdr gnus-copy-article-ignored-headers)
9963                  (message-remove-header hdr t)))
9964              (gnus-request-accept-article
9965               to-newsgroup select-method (not articles) t))))
9966         ;; Crosspost the article.
9967         ((eq action 'crosspost)
9968          (let ((xref (message-tokenize-header
9969                       (mail-header-xref (gnus-summary-article-header
9970                                          article))
9971                       " ")))
9972            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9973                                   ":" (number-to-string article)))
9974            (unless xref
9975              (setq xref (list (system-name))))
9976            (setq new-xref
9977                  (concat
9978                   (mapconcat 'identity
9979                              (delete "Xref:" (delete new-xref xref))
9980                              " ")
9981                   " " new-xref))
9982            (with-current-buffer copy-buf
9983              ;; First put the article in the destination group.
9984              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9985              (when (consp (setq art-group
9986                                 (gnus-request-accept-article
9987                                  to-newsgroup select-method (not articles)
9988                                  t)))
9989                (setq new-xref (concat new-xref " " (car art-group)
9990                                       ":"
9991                                       (number-to-string (cdr art-group))))
9992                ;; Now we have the new Xrefs header, so we insert
9993                ;; it and replace the new article.
9994                (nnheader-replace-header "Xref" new-xref)
9995                (gnus-request-replace-article
9996                 (cdr art-group) to-newsgroup (current-buffer) t)
9997                art-group))))))
9998       (cond
9999        ((not art-group)
10000         (gnus-message 1 "Couldn't %s article %s: %s"
10001                       (cadr (assq action names)) article
10002                       (nnheader-get-report (car to-method))))
10003        ((eq art-group 'junk)
10004         (when (eq action 'move)
10005           (gnus-summary-mark-article article gnus-canceled-mark)
10006           (gnus-message 4 "Deleted article %s" article)
10007           ;; run the delete hook
10008           (run-hook-with-args 'gnus-summary-article-delete-hook
10009                               action
10010                               (gnus-data-header
10011                                (assoc article (gnus-data-list nil)))
10012                               gnus-newsgroup-name nil
10013                               select-method)))
10014        (t
10015         (let* ((pto-group (gnus-group-prefixed-name
10016                            (car art-group) to-method))
10017                (info (gnus-get-info pto-group))
10018                (to-group (gnus-info-group info))
10019                to-marks)
10020           ;; Update the group that has been moved to.
10021           (when (and info
10022                      (memq action '(move copy)))
10023             (unless (member to-group to-groups)
10024               (push to-group to-groups))
10025
10026             (when (and (not (memq article gnus-newsgroup-unreads))
10027                        (cdr art-group))
10028               (push 'read to-marks)
10029               (gnus-info-set-read
10030                info (gnus-add-to-range (gnus-info-read info)
10031                                        (list (cdr art-group)))))
10032
10033             ;; See whether the article is to be put in the cache.
10034             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10035                    (marks (if expirable
10036                               gnus-article-mark-lists
10037                             (delete '(expirable . expire)
10038                                     (copy-sequence
10039                                      gnus-article-mark-lists))))
10040                    (to-article (cdr art-group)))
10041
10042               ;; Enter the article into the cache in the new group,
10043               ;; if that is required.
10044               (when (and to-article
10045                          gnus-use-cache)
10046                 (gnus-cache-possibly-enter-article
10047                  to-group to-article
10048                  (memq article gnus-newsgroup-marked)
10049                  (memq article gnus-newsgroup-dormant)
10050                  (memq article gnus-newsgroup-unreads)))
10051
10052               (when (and gnus-preserve-marks
10053                          to-article)
10054                 ;; Copy any marks over to the new group.
10055                 (when (and (equal to-group gnus-newsgroup-name)
10056                            (not (memq article gnus-newsgroup-unreads)))
10057                   ;; Mark this article as read in this group.
10058                   (push (cons to-article gnus-read-mark)
10059                         gnus-newsgroup-reads)
10060                   ;; Increase the active status of this group.
10061                   (setcdr (gnus-active to-group) to-article)
10062                   (setcdr gnus-newsgroup-active to-article))
10063
10064                 (while marks
10065                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10066                     (when (memq article (symbol-value
10067                                          (intern (format "gnus-newsgroup-%s"
10068                                                          (caar marks)))))
10069                       (push (cdar marks) to-marks)
10070                       ;; If the other group is the same as this group,
10071                       ;; then we have to add the mark to the list.
10072                       (when (equal to-group gnus-newsgroup-name)
10073                         (set (intern (format "gnus-newsgroup-%s"
10074                                              (caar marks)))
10075                              (cons to-article
10076                                    (symbol-value
10077                                     (intern (format "gnus-newsgroup-%s"
10078                                                     (caar marks)))))))
10079                       ;; Copy the marks to other group.
10080                       (gnus-add-marked-articles
10081                        to-group (cdar marks) (list to-article) info)))
10082                   (setq marks (cdr marks)))
10083
10084                 (when (and expirable
10085                            gnus-mark-copied-or-moved-articles-as-expirable
10086                            (not (memq 'expire to-marks)))
10087                   ;; Mark this article as expirable.
10088                   (push 'expire to-marks)
10089                   (when (equal to-group gnus-newsgroup-name)
10090                     (push to-article gnus-newsgroup-expirable))
10091                   ;; Copy the expirable mark to other group.
10092                   (gnus-add-marked-articles
10093                    to-group 'expire (list to-article) info))
10094
10095                 (when (and to-marks
10096                            (or gnus-propagate-marks
10097                                (gnus-method-option-p
10098                                 (gnus-find-method-for-group to-group)
10099                                 'server-marks)))
10100                   (gnus-request-set-mark
10101                    to-group (list (list (list to-article) 'add to-marks)))))
10102
10103               (gnus-dribble-enter
10104                (concat "(gnus-group-set-info '"
10105                        (gnus-prin1-to-string (gnus-get-info to-group))
10106                        ")")
10107                (concat "^(gnus-group-set-info '(\""
10108                        (regexp-quote to-group) "\""))))
10109
10110           ;; Update the Xref header in this article to point to
10111           ;; the new crossposted article we have just created.
10112           (when (eq action 'crosspost)
10113             (with-current-buffer copy-buf
10114               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10115               (nnheader-replace-header "Xref" new-xref)
10116               (gnus-request-replace-article
10117                article gnus-newsgroup-name (current-buffer) t)))
10118
10119           ;; run the move/copy/crosspost/respool hook
10120           (run-hook-with-args 'gnus-summary-article-move-hook
10121                               action
10122                               (gnus-data-header
10123                                (assoc article (gnus-data-list nil)))
10124                               gnus-newsgroup-name
10125                               to-newsgroup
10126                               select-method))
10127
10128         ;;;!!!Why is this necessary?
10129         (set-buffer gnus-summary-buffer)
10130
10131         (when (eq action 'move)
10132           (save-excursion
10133             (gnus-summary-goto-subject article)
10134             (gnus-summary-mark-article article gnus-canceled-mark)))))
10135       (push article articles-to-update-marks))
10136
10137     (save-excursion
10138       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10139     ;; Re-activate all groups that have been moved to.
10140     (with-current-buffer gnus-group-buffer
10141       (let ((gnus-group-marked to-groups))
10142         (gnus-group-get-new-news-this-group nil t)))
10143
10144     (gnus-kill-buffer copy-buf)
10145     (gnus-summary-position-point)
10146     (gnus-set-mode-line 'summary)))
10147
10148 (defun gnus-summary-push-marks-to-backend (article)
10149   (let ((set nil)
10150         (marks gnus-article-mark-lists))
10151     (unless (memq article gnus-newsgroup-unreads)
10152       (push 'read set))
10153     (while marks
10154       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10155                  (memq article (symbol-value
10156                                 (intern (format "gnus-newsgroup-%s"
10157                                                 (caar marks))))))
10158         (push (cdar marks) set))
10159       (pop marks))
10160     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10161
10162 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10163   "Copy the current article to some other group.
10164 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10165 When called interactively, if TO-NEWSGROUP is nil, use the value of
10166 the variable `gnus-move-split-methods' for finding a default target
10167 newsgroup.
10168 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10169 re-spool using this method."
10170   (interactive "P")
10171   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10172
10173 (defun gnus-summary-crosspost-article (&optional n)
10174   "Crosspost the current article to some other group."
10175   (interactive "P")
10176   (gnus-summary-move-article n nil nil 'crosspost))
10177
10178 (defcustom gnus-summary-respool-default-method nil
10179   "Default method type for respooling an article.
10180 If nil, use to the current newsgroup method."
10181   :type 'symbol
10182   :group 'gnus-summary-mail)
10183
10184 (defun gnus-summary-respool-article (&optional n method)
10185   "Respool the current article.
10186 The article will be squeezed through the mail spooling process again,
10187 which means that it will be put in some mail newsgroup or other
10188 depending on `nnmail-split-methods'.
10189 If N is a positive number, respool the N next articles.
10190 If N is a negative number, respool the N previous articles.
10191 If N is nil and any articles have been marked with the process mark,
10192 respool those articles instead.
10193
10194 Respooling can be done both from mail groups and \"real\" newsgroups.
10195 In the former case, the articles in question will be moved from the
10196 current group into whatever groups they are destined to.  In the
10197 latter case, they will be copied into the relevant groups."
10198   (interactive
10199    (list current-prefix-arg
10200          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10201                 (methname
10202                  (symbol-name (or gnus-summary-respool-default-method
10203                                   (car (gnus-find-method-for-group
10204                                         gnus-newsgroup-name)))))
10205                 (method
10206                  (gnus-completing-read
10207                   "Backend to use when respooling"
10208                   methods t nil 'gnus-mail-method-history methname))
10209                 ms)
10210            (cond
10211             ((zerop (length (setq ms (gnus-servers-using-backend
10212                                       (intern method)))))
10213              (list (intern method) ""))
10214             ((= 1 (length ms))
10215              (car ms))
10216             (t
10217              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10218                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10219                            ms-alist))))))))
10220   (unless method
10221     (error "No method given for respooling"))
10222   (if (assoc (symbol-name
10223               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10224              (gnus-methods-using 'respool))
10225       (gnus-summary-move-article n nil method)
10226     (gnus-summary-copy-article n nil method)))
10227
10228 (defun gnus-summary-import-article (file &optional edit)
10229   "Import an arbitrary file into a mail newsgroup."
10230   (interactive "fImport file: \nP")
10231   (let ((group gnus-newsgroup-name)
10232         (now (current-time))
10233         atts lines group-art)
10234     (unless (gnus-check-backend-function 'request-accept-article group)
10235       (error "%s does not support article importing" group))
10236     (or (file-readable-p file)
10237         (not (file-regular-p file))
10238         (error "Can't read %s" file))
10239     (with-current-buffer (gnus-get-buffer-create " *import file*")
10240       (erase-buffer)
10241       (nnheader-insert-file-contents file)
10242       (goto-char (point-min))
10243       (if (nnheader-article-p)
10244           (save-restriction
10245             (goto-char (point-min))
10246             (search-forward "\n\n" nil t)
10247             (narrow-to-region (point-min) (1- (point)))
10248             (goto-char (point-min))
10249             (unless (re-search-forward "^date:" nil t)
10250               (goto-char (point-max))
10251               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10252        ;; This doesn't look like an article, so we fudge some headers.
10253         (setq atts (file-attributes file)
10254               lines (count-lines (point-min) (point-max)))
10255         (insert "From: " (read-string "From: ") "\n"
10256                 "Subject: " (read-string "Subject: ") "\n"
10257                 "Date: " (message-make-date (nth 5 atts)) "\n"
10258                 "Message-ID: " (message-make-message-id) "\n"
10259                 "Lines: " (int-to-string lines) "\n"
10260                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10261       (setq group-art (gnus-request-accept-article group nil t))
10262       (kill-buffer (current-buffer)))
10263     (setq gnus-newsgroup-active (gnus-activate-group group))
10264     (forward-line 1)
10265     (gnus-summary-goto-article (cdr group-art) nil t)
10266     (when edit
10267       (gnus-summary-edit-article))))
10268
10269 (defun gnus-summary-create-article ()
10270   "Create an article in a mail newsgroup."
10271   (interactive)
10272   (let ((group gnus-newsgroup-name)
10273         (now (current-time))
10274         group-art)
10275     (unless (gnus-check-backend-function 'request-accept-article group)
10276       (error "%s does not support article importing" group))
10277     (with-current-buffer (gnus-get-buffer-create " *import file*")
10278       (erase-buffer)
10279       (goto-char (point-min))
10280       ;; This doesn't look like an article, so we fudge some headers.
10281       (insert "From: " (read-string "From: ") "\n"
10282               "Subject: " (read-string "Subject: ") "\n"
10283               "Date: " (message-make-date now) "\n"
10284               "Message-ID: " (message-make-message-id) "\n")
10285       (setq group-art (gnus-request-accept-article group nil t))
10286       (kill-buffer (current-buffer)))
10287     (setq gnus-newsgroup-active (gnus-activate-group group))
10288     (forward-line 1)
10289     (gnus-summary-goto-article (cdr group-art) nil t)
10290     (gnus-summary-edit-article)))
10291
10292 (defun gnus-summary-article-posted-p ()
10293   "Say whether the current (mail) article is available from news as well.
10294 This will be the case if the article has both been mailed and posted."
10295   (interactive)
10296   (let ((id (mail-header-references (gnus-summary-article-header)))
10297         (gnus-override-method (car (gnus-refer-article-methods))))
10298     (if (gnus-request-head id "")
10299         (gnus-message 2 "The current message was found on %s"
10300                       gnus-override-method)
10301       (gnus-message 2 "The current message couldn't be found on %s"
10302                     gnus-override-method)
10303       nil)))
10304
10305 (defun gnus-summary-expire-articles (&optional now)
10306   "Expire all articles that are marked as expirable in the current group."
10307   (interactive)
10308   (when (and (not gnus-group-is-exiting-without-update-p)
10309              (gnus-check-backend-function
10310               'request-expire-articles gnus-newsgroup-name))
10311     ;; This backend supports expiry.
10312     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10313            (expirable (if total
10314                           (progn
10315                             ;; We need to update the info for
10316                             ;; this group for `gnus-list-of-read-articles'
10317                             ;; to give us the right answer.
10318                             (gnus-run-hooks 'gnus-exit-group-hook)
10319                             (gnus-summary-update-info)
10320                             (gnus-list-of-read-articles gnus-newsgroup-name))
10321                         (setq gnus-newsgroup-expirable
10322                               (sort gnus-newsgroup-expirable '<))))
10323            (expiry-wait (if now 'immediate
10324                           (gnus-group-find-parameter
10325                            gnus-newsgroup-name 'expiry-wait)))
10326            (nnmail-expiry-target
10327             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10328                 nnmail-expiry-target))
10329            es)
10330       (when expirable
10331         ;; There are expirable articles in this group, so we run them
10332         ;; through the expiry process.
10333         (gnus-message 6 "Expiring articles...")
10334         (when (gnus-check-group gnus-newsgroup-name)
10335           ;; The list of articles that weren't expired is returned.
10336           (save-excursion
10337             (if expiry-wait
10338                 (let ((nnmail-expiry-wait-function nil)
10339                       (nnmail-expiry-wait expiry-wait))
10340                   (setq es (gnus-request-expire-articles
10341                             expirable gnus-newsgroup-name)))
10342               (setq es (gnus-request-expire-articles
10343                         expirable gnus-newsgroup-name)))
10344             (unless total
10345               (setq gnus-newsgroup-expirable es))
10346             ;; We go through the old list of expirable, and mark all
10347             ;; really expired articles as nonexistent.
10348             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10349               (let ((gnus-use-cache nil))
10350                 (dolist (article expirable)
10351                   (when (and (not (memq article es))
10352                              (gnus-data-find article))
10353                     (gnus-summary-mark-article article gnus-canceled-mark)
10354                     (run-hook-with-args 'gnus-summary-article-expire-hook
10355                                         'delete
10356                                         (gnus-data-header
10357                                          (assoc article (gnus-data-list nil)))
10358                                         gnus-newsgroup-name
10359                                         nil
10360                                         nil)))))))
10361         (gnus-message 6 "Expiring articles...done")))))
10362
10363 (defun gnus-summary-expire-articles-now ()
10364   "Expunge all expirable articles in the current group.
10365 This means that *all* articles that are marked as expirable will be
10366 deleted forever, right now."
10367   (interactive)
10368   (or gnus-expert-user
10369       (gnus-yes-or-no-p
10370        "Are you really, really sure you want to delete all expirable messages? ")
10371       (error "Phew!"))
10372   (gnus-summary-expire-articles t))
10373
10374 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10375 (defun gnus-summary-delete-article (&optional n)
10376   "Delete the N next (mail) articles.
10377 This command actually deletes articles.  This is not a marking
10378 command.  The article will disappear forever from your life, never to
10379 return.
10380
10381 If N is negative, delete backwards.
10382 If N is nil and articles have been marked with the process mark,
10383 delete these instead.
10384
10385 If `gnus-novice-user' is non-nil you will be asked for
10386 confirmation before the articles are deleted."
10387   (interactive "P")
10388   (unless (gnus-check-backend-function 'request-expire-articles
10389                                        gnus-newsgroup-name)
10390     (error "The current newsgroup does not support article deletion"))
10391   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10392     (error "Couldn't open server"))
10393   ;; Compute the list of articles to delete.
10394   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10395         (nnmail-expiry-target 'delete)
10396         not-deleted)
10397     (if (and gnus-novice-user
10398              (not (gnus-yes-or-no-p
10399                    (format "Do you really want to delete %s forever? "
10400                            (if (> (length articles) 1)
10401                                (format "these %s articles" (length articles))
10402                              "this article")))))
10403         ()
10404       ;; Delete the articles.
10405       (setq not-deleted (gnus-request-expire-articles
10406                          articles gnus-newsgroup-name 'force))
10407       (save-excursion
10408         (while articles
10409           (gnus-summary-remove-process-mark (car articles))
10410           ;; The backend might not have been able to delete the article
10411           ;; after all.
10412           (unless (memq (car articles) not-deleted)
10413             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10414             (let* ((article (car articles))
10415                    (ghead  (gnus-data-header
10416                             (assoc article (gnus-data-list nil)))))
10417               (run-hook-with-args 'gnus-summary-article-delete-hook
10418                                   'delete ghead gnus-newsgroup-name nil
10419                                   nil)))
10420           (setq articles (cdr articles))))
10421       (when not-deleted
10422         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10423     (gnus-summary-position-point)
10424     (gnus-set-mode-line 'summary)
10425     not-deleted))
10426
10427 (defun gnus-summary-edit-article (&optional arg)
10428   "Edit the current article.
10429 This will have permanent effect only in mail groups.
10430 If ARG is nil, edit the decoded articles.
10431 If ARG is 1, edit the raw articles.
10432 If ARG is 2, edit the raw articles even in read-only groups.
10433 If ARG is 3, edit the articles with the current handles.
10434 Otherwise, allow editing of articles even in read-only
10435 groups."
10436   (interactive "P")
10437   (let (force raw current-handles)
10438     (cond
10439      ((null arg))
10440      ((eq arg 1)
10441       (setq raw t))
10442      ((eq arg 2)
10443       (setq raw t
10444             force t))
10445      ((eq arg 3)
10446       (setq current-handles
10447             (and (gnus-buffer-live-p gnus-article-buffer)
10448                  (with-current-buffer gnus-article-buffer
10449                    (prog1
10450                        gnus-article-mime-handles
10451                      (setq gnus-article-mime-handles nil))))))
10452      (t
10453       (setq force t)))
10454     (when (and raw (not force)
10455                (member gnus-newsgroup-name '("nndraft:delayed"
10456                                              "nndraft:drafts"
10457                                              "nndraft:queue")))
10458       (error "Can't edit the raw article in group %s"
10459              gnus-newsgroup-name))
10460     (with-current-buffer gnus-summary-buffer
10461       (let ((mail-parse-charset gnus-newsgroup-charset)
10462             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10463         (gnus-set-global-variables)
10464         (when (and (not force)
10465                    (gnus-group-read-only-p))
10466           (error "The current newsgroup does not support article editing"))
10467         (gnus-summary-show-article t)
10468         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10469           (with-current-buffer gnus-article-buffer
10470             (mm-enable-multibyte)))
10471         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10472             (setq raw t))
10473         (gnus-article-edit-article
10474          (if raw 'ignore
10475            `(lambda ()
10476               (let ((mbl mml-buffer-list))
10477                 (setq mml-buffer-list nil)
10478                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10479                   (mime-to-mml ,'current-handles))
10480                 (let ((mbl1 mml-buffer-list))
10481                   (setq mml-buffer-list mbl)
10482                   (set (make-local-variable 'mml-buffer-list) mbl1))
10483                 (gnus-make-local-hook 'kill-buffer-hook)
10484                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10485          `(lambda (no-highlight)
10486             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10487                   (message-options message-options)
10488                   (message-options-set-recipient)
10489                   (mail-parse-ignored-charsets
10490                    ',gnus-newsgroup-ignored-charsets)
10491                   (rfc2047-header-encoding-alist
10492                    ',(let ((charset (gnus-group-name-charset
10493                                      (gnus-find-method-for-group
10494                                       gnus-newsgroup-name)
10495                                      gnus-newsgroup-name)))
10496                        (append (list (cons "Newsgroups" charset)
10497                                      (cons "Followup-To" charset)
10498                                      (cons "Xref" charset))
10499                                rfc2047-header-encoding-alist))))
10500               ,(if (not raw) '(progn
10501                                 (mml-to-mime)
10502                                 (mml-destroy-buffers)
10503                                 (remove-hook 'kill-buffer-hook
10504                                              'mml-destroy-buffers t)
10505                                 (kill-local-variable 'mml-buffer-list)))
10506               (gnus-summary-edit-article-done
10507                ,(or (mail-header-references gnus-current-headers) "")
10508                ,(gnus-group-read-only-p)
10509                ,gnus-summary-buffer no-highlight))))))))
10510
10511 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10512
10513 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10514                                                  no-highlight)
10515   "Make edits to the current article permanent."
10516   (interactive)
10517   (save-excursion
10518     ;; The buffer restriction contains the entire article if it exists.
10519     (when (article-goto-body)
10520       (let ((lines (count-lines (point) (point-max)))
10521             (length (- (point-max) (point)))
10522             (case-fold-search t)
10523             (body (copy-marker (point))))
10524         (goto-char (point-min))
10525         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10526           (delete-region (match-beginning 1) (match-end 1))
10527           (insert (number-to-string length)))
10528         (goto-char (point-min))
10529         (when (re-search-forward
10530                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10531           (delete-region (match-beginning 1) (match-end 1))
10532           (insert (number-to-string length)))
10533         (goto-char (point-min))
10534         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10535           (delete-region (match-beginning 1) (match-end 1))
10536           (insert (number-to-string lines))))))
10537   ;; Replace the article.
10538   (let ((buf (current-buffer))
10539         (article (cdr gnus-article-current))
10540         replace-result)
10541     (with-temp-buffer
10542       (insert-buffer-substring buf)
10543       (if (and (not read-only)
10544                (not (setq replace-result
10545                           (gnus-request-replace-article
10546                            article (car gnus-article-current)
10547                            (current-buffer) t))))
10548           (error "Couldn't replace article")
10549         ;; If we got a number back, then that's the new article number
10550         ;; for this article.  Otherwise, the article number didn't change.
10551         (when (numberp replace-result)
10552           (with-current-buffer gnus-summary-buffer
10553             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10554             (gnus-summary-limit gnus-newsgroup-limit)
10555             (setq article replace-result)
10556             (gnus-summary-goto-subject article t)))
10557         ;; Update the summary buffer.
10558         (if (and references
10559                  (equal (message-tokenize-header references " ")
10560                         (message-tokenize-header
10561                          (or (message-fetch-field "references") "") " ")))
10562             ;; We only have to update this line.
10563             (save-excursion
10564               (save-restriction
10565                 (message-narrow-to-head)
10566                 (let ((head (buffer-substring-no-properties
10567                              (point-min) (point-max)))
10568                       header)
10569                   (with-temp-buffer
10570                     (insert (format "211 %d Article retrieved.\n" article))
10571                     (insert head)
10572                     (insert ".\n")
10573                     (let ((nntp-server-buffer (current-buffer)))
10574                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10575                     (with-current-buffer gnus-summary-buffer
10576                       (gnus-data-set-header (gnus-data-find article) header)
10577                       (gnus-summary-update-article-line article header)
10578                       (if (gnus-summary-goto-subject article nil t)
10579                           (gnus-summary-update-secondary-mark article)))))))
10580           ;; Update threads.
10581           (set-buffer (or buffer gnus-summary-buffer))
10582           (gnus-summary-update-article article)
10583           (if (gnus-summary-goto-subject article nil t)
10584               (gnus-summary-update-secondary-mark article)))
10585         ;; Prettify the article buffer again.
10586         (unless no-highlight
10587           (with-current-buffer gnus-article-buffer
10588             ;;!!! Fix this -- article should be rehighlighted.
10589             ;;(gnus-run-hooks 'gnus-article-display-hook)
10590             (set-buffer gnus-original-article-buffer)
10591             (gnus-request-article
10592              article (car gnus-article-current) (current-buffer))))
10593         ;; Prettify the summary buffer line.
10594         (when (gnus-visual-p 'summary-highlight 'highlight)
10595           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10596
10597 (defun gnus-summary-edit-wash (key)
10598   "Perform editing command KEY in the article buffer."
10599   (interactive
10600    (list
10601     (progn
10602       (message "%s" (concat (this-command-keys) "- "))
10603       (read-char))))
10604   (message "")
10605   (gnus-summary-edit-article)
10606   (execute-kbd-macro (concat (this-command-keys) key))
10607   (gnus-article-edit-done))
10608
10609 ;;; Respooling
10610
10611 (defun gnus-summary-respool-query (&optional silent trace)
10612   "Query where the respool algorithm would put this article."
10613   (interactive)
10614   (let (gnus-mark-article-hook)
10615     (gnus-summary-select-article)
10616     (with-current-buffer gnus-original-article-buffer
10617       (let ((groups (nnmail-article-group 'identity trace)))
10618         (unless silent
10619           (if groups
10620               (message "This message would go to %s"
10621                        (mapconcat 'car groups ", "))
10622             (message "This message would go to no groups"))
10623           groups)))))
10624
10625 (defun gnus-summary-respool-trace ()
10626   "Trace where the respool algorithm would put this article.
10627 Display a buffer showing all fancy splitting patterns which matched."
10628   (interactive)
10629   (gnus-summary-respool-query nil t))
10630
10631 ;; Summary marking commands.
10632
10633 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10634   "Mark articles which has the same subject as read, and then select the next.
10635 If UNMARK is positive, remove any kind of mark.
10636 If UNMARK is negative, tick articles."
10637   (interactive "P")
10638   (when unmark
10639     (setq unmark (prefix-numeric-value unmark)))
10640   (let ((count
10641          (gnus-summary-mark-same-subject
10642           (gnus-summary-article-subject) unmark)))
10643     ;; Select next unread article.  If auto-select-same mode, should
10644     ;; select the first unread article.
10645     (gnus-summary-next-article t (and gnus-auto-select-same
10646                                       (gnus-summary-article-subject)))
10647     (gnus-message 7 "%d article%s marked as %s"
10648                   count (if (= count 1) " is" "s are")
10649                   (if unmark "unread" "read"))))
10650
10651 (defun gnus-summary-kill-same-subject (&optional unmark)
10652   "Mark articles which has the same subject as read.
10653 If UNMARK is positive, remove any kind of mark.
10654 If UNMARK is negative, tick articles."
10655   (interactive "P")
10656   (when unmark
10657     (setq unmark (prefix-numeric-value unmark)))
10658   (let ((count
10659          (gnus-summary-mark-same-subject
10660           (gnus-summary-article-subject) unmark)))
10661     ;; If marked as read, go to next unread subject.
10662     (when (null unmark)
10663       ;; Go to next unread subject.
10664       (gnus-summary-next-subject 1 t))
10665     (gnus-message 7 "%d articles are marked as %s"
10666                   count (if unmark "unread" "read"))))
10667
10668 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10669   "Mark articles with same SUBJECT as read, and return marked number.
10670 If optional argument UNMARK is positive, remove any kinds of marks.
10671 If optional argument UNMARK is negative, mark articles as unread instead."
10672   (let ((count 1))
10673     (save-excursion
10674       (cond
10675        ((null unmark)                   ; Mark as read.
10676         (while (and
10677                 (progn
10678                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10679                   (gnus-summary-show-thread) t)
10680                 (gnus-summary-find-subject subject))
10681           (setq count (1+ count))))
10682        ((> unmark 0)                    ; Tick.
10683         (while (and
10684                 (progn
10685                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10686                   (gnus-summary-show-thread) t)
10687                 (gnus-summary-find-subject subject))
10688           (setq count (1+ count))))
10689        (t                               ; Mark as unread.
10690         (while (and
10691                 (progn
10692                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10693                   (gnus-summary-show-thread) t)
10694                 (gnus-summary-find-subject subject))
10695           (setq count (1+ count)))))
10696       (gnus-set-mode-line 'summary)
10697       ;; Return the number of marked articles.
10698       count)))
10699
10700 (defun gnus-summary-mark-as-processable (n &optional unmark)
10701   "Set the process mark on the next N articles.
10702 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10703 the process mark instead.  The difference between N and the actual
10704 number of articles marked is returned."
10705   (interactive "P")
10706   (if (and (null n) (gnus-region-active-p))
10707       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10708     (setq n (prefix-numeric-value n))
10709     (let ((backward (< n 0))
10710           (n (abs n)))
10711       (while (and
10712               (> n 0)
10713               (if unmark
10714                   (gnus-summary-remove-process-mark
10715                    (gnus-summary-article-number))
10716                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10717               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10718         (setq n (1- n)))
10719       (when (/= 0 n)
10720         (gnus-message 7 "No more articles"))
10721       (gnus-summary-recenter)
10722       (gnus-summary-position-point)
10723       n)))
10724
10725 (defun gnus-summary-unmark-as-processable (n)
10726   "Remove the process mark from the next N articles.
10727 If N is negative, unmark backward instead.  The difference between N and
10728 the actual number of articles unmarked is returned."
10729   (interactive "P")
10730   (gnus-summary-mark-as-processable n t))
10731
10732 (defun gnus-summary-unmark-all-processable ()
10733   "Remove the process mark from all articles."
10734   (interactive)
10735   (save-excursion
10736     (while gnus-newsgroup-processable
10737       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10738   (gnus-summary-position-point))
10739
10740 (defun gnus-summary-add-mark (article type)
10741   "Mark ARTICLE with a mark of TYPE."
10742   (let ((vtype (car (assq type gnus-article-mark-lists)))
10743         var)
10744     (if (not vtype)
10745         (error "No such mark type: %s" type)
10746       (setq var (intern (format "gnus-newsgroup-%s" type)))
10747       (set var (cons article (symbol-value var)))
10748       (if (memq type '(processable cached replied forwarded recent saved))
10749           (gnus-summary-update-secondary-mark article)
10750         ;;; !!! This is bogus.  We should find out what primary
10751         ;;; !!! mark we want to set.
10752         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10753
10754 (defun gnus-summary-mark-as-expirable (n)
10755   "Mark N articles forward as expirable.
10756 If N is negative, mark backward instead.  The difference between N and
10757 the actual number of articles marked is returned."
10758   (interactive "p")
10759   (gnus-summary-mark-forward n gnus-expirable-mark))
10760
10761 (defun gnus-summary-mark-as-spam (n)
10762   "Mark N articles forward as spam.
10763 If N is negative, mark backward instead.  The difference between N and
10764 the actual number of articles marked is returned."
10765   (interactive "p")
10766   (gnus-summary-mark-forward n gnus-spam-mark))
10767
10768 (defun gnus-summary-mark-article-as-replied (article)
10769   "Mark ARTICLE as replied to and update the summary line.
10770 ARTICLE can also be a list of articles."
10771   (interactive (list (gnus-summary-article-number)))
10772   (let ((articles (if (listp article) article (list article))))
10773     (dolist (article articles)
10774       (unless (numberp article)
10775         (error "%s is not a number" article))
10776       (push article gnus-newsgroup-replied)
10777       (let ((inhibit-read-only t))
10778         (when (gnus-summary-goto-subject article nil t)
10779           (gnus-summary-update-secondary-mark article))))))
10780
10781 (defun gnus-summary-mark-article-as-forwarded (article)
10782   "Mark ARTICLE as forwarded and update the summary line.
10783 ARTICLE can also be a list of articles."
10784   (let ((articles (if (listp article) article (list article))))
10785     (dolist (article articles)
10786       (push article gnus-newsgroup-forwarded)
10787       (let ((inhibit-read-only t))
10788         (when (gnus-summary-goto-subject article nil t)
10789           (gnus-summary-update-secondary-mark article))))))
10790
10791 (defun gnus-summary-set-bookmark (article)
10792   "Set a bookmark in current article."
10793   (interactive (list (gnus-summary-article-number)))
10794   (when (or (not (get-buffer gnus-article-buffer))
10795             (not gnus-current-article)
10796             (not gnus-article-current)
10797             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10798     (error "No current article selected"))
10799   ;; Remove old bookmark, if one exists.
10800   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10801   ;; Set the new bookmark, which is on the form
10802   ;; (article-number . line-number-in-body).
10803   (push
10804    (cons article
10805          (with-current-buffer gnus-article-buffer
10806            (count-lines
10807             (min (point)
10808                  (save-excursion
10809                    (article-goto-body)
10810                    (point)))
10811             (point))))
10812    gnus-newsgroup-bookmarks)
10813   (gnus-message 6 "A bookmark has been added to the current article."))
10814
10815 (defun gnus-summary-remove-bookmark (article)
10816   "Remove the bookmark from the current article."
10817   (interactive (list (gnus-summary-article-number)))
10818   ;; Remove old bookmark, if one exists.
10819   (if (not (assq article gnus-newsgroup-bookmarks))
10820       (gnus-message 6 "No bookmark in current article.")
10821     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10822     (gnus-message 6 "Removed bookmark.")))
10823
10824 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10825 (defun gnus-summary-mark-as-dormant (n)
10826   "Mark N articles forward as dormant.
10827 If N is negative, mark backward instead.  The difference between N and
10828 the actual number of articles marked is returned."
10829   (interactive "p")
10830   (gnus-summary-mark-forward n gnus-dormant-mark))
10831
10832 (defun gnus-summary-set-process-mark (article)
10833   "Set the process mark on ARTICLE and update the summary line."
10834   (setq gnus-newsgroup-processable
10835         (cons article
10836               (delq article gnus-newsgroup-processable)))
10837   (when (gnus-summary-goto-subject article)
10838     (gnus-summary-show-thread)
10839     (gnus-summary-goto-subject article)
10840     (gnus-summary-update-secondary-mark article)))
10841
10842 (defun gnus-summary-remove-process-mark (&rest articles)
10843   "Remove the process mark from ARTICLES and update the summary line."
10844   (dolist (article articles)
10845     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10846     (when (gnus-summary-goto-subject article)
10847       (gnus-summary-show-thread)
10848       (gnus-summary-goto-subject article)
10849       (gnus-summary-update-secondary-mark article)))
10850   t)
10851
10852 (defun gnus-summary-set-saved-mark (article)
10853   "Set the process mark on ARTICLE and update the summary line."
10854   (push article gnus-newsgroup-saved)
10855   (when (gnus-summary-goto-subject article)
10856     (gnus-summary-update-secondary-mark article)))
10857
10858 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10859   "Mark N articles as read forwards.
10860 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10861 The difference between N and the actual number of articles marked is
10862 returned.
10863 If NO-EXPIRE, auto-expiry will be inhibited."
10864   (interactive "p")
10865   (gnus-summary-show-thread)
10866   (let ((backward (< n 0))
10867         (gnus-summary-goto-unread
10868          (and gnus-summary-goto-unread
10869               (not (eq gnus-summary-goto-unread 'never))
10870               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10871                                     gnus-ticked-mark gnus-dormant-mark)))))
10872         (n (abs n))
10873         (mark (or mark gnus-del-mark)))
10874     (while (and (> n 0)
10875                 (gnus-summary-mark-article nil mark no-expire)
10876                 (zerop (gnus-summary-next-subject
10877                         (if backward -1 1)
10878                         (and gnus-summary-goto-unread
10879                              (not (eq gnus-summary-goto-unread 'never)))
10880                         t)))
10881       (setq n (1- n)))
10882     (when (/= 0 n)
10883       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10884     (gnus-summary-recenter)
10885     (gnus-summary-position-point)
10886     (gnus-set-mode-line 'summary)
10887     n))
10888
10889 (defun gnus-summary-mark-article-as-read (mark)
10890   "Mark the current article quickly as read with MARK."
10891   (let ((article (gnus-summary-article-number)))
10892     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10893     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10894     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10895     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10896     (push (cons article mark) gnus-newsgroup-reads)
10897     ;; Possibly remove from cache, if that is used.
10898     (when gnus-use-cache
10899       (gnus-cache-enter-remove-article article))
10900     ;; Allow the backend to change the mark.
10901     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10902     ;; Check for auto-expiry.
10903     (when (and gnus-newsgroup-auto-expire
10904                (memq mark gnus-auto-expirable-marks))
10905       (setq mark gnus-expirable-mark)
10906       ;; Let the backend know about the mark change.
10907       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10908       (push article gnus-newsgroup-expirable))
10909     ;; Set the mark in the buffer.
10910     (gnus-summary-update-mark mark 'unread)
10911     t))
10912
10913 (defun gnus-summary-mark-article-as-unread (mark)
10914   "Mark the current article quickly as unread with MARK."
10915   (let* ((article (gnus-summary-article-number))
10916          (old-mark (gnus-summary-article-mark article)))
10917     ;; Allow the backend to change the mark.
10918     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10919     (if (eq mark old-mark)
10920         t
10921       (if (<= article 0)
10922           (progn
10923             (gnus-error 1 "Can't mark negative article numbers")
10924             nil)
10925         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10926         (setq gnus-newsgroup-spam-marked
10927               (delq article gnus-newsgroup-spam-marked))
10928         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10929         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10930         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10931         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10932         (cond ((= mark gnus-ticked-mark)
10933                (setq gnus-newsgroup-marked
10934                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10935                                               article)))
10936               ((= mark gnus-spam-mark)
10937                (setq gnus-newsgroup-spam-marked
10938                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10939                                               article)))
10940               ((= mark gnus-dormant-mark)
10941                (setq gnus-newsgroup-dormant
10942                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10943                                               article)))
10944               (t
10945                (setq gnus-newsgroup-unreads
10946                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10947                                               article))))
10948         (gnus-alist-pull article gnus-newsgroup-reads)
10949
10950         ;; See whether the article is to be put in the cache.
10951         (and gnus-use-cache
10952              (vectorp (gnus-summary-article-header article))
10953              (save-excursion
10954                (gnus-cache-possibly-enter-article
10955                 gnus-newsgroup-name article
10956                 (= mark gnus-ticked-mark)
10957                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10958
10959         ;; Fix the mark.
10960         (gnus-summary-update-mark mark 'unread)
10961         t))))
10962
10963 (defun gnus-summary-mark-article (&optional article mark no-expire)
10964   "Mark ARTICLE with MARK.  MARK can be any character.
10965 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10966 `??' (dormant) and `?E' (expirable).
10967 If MARK is nil, then the default character `?r' is used.
10968 If ARTICLE is nil, then the article on the current line will be
10969 marked.
10970 If NO-EXPIRE, auto-expiry will be inhibited."
10971   ;; The mark might be a string.
10972   (when (stringp mark)
10973     (setq mark (aref mark 0)))
10974   ;; If no mark is given, then we check auto-expiring.
10975   (when (null mark)
10976     (setq mark gnus-del-mark))
10977   (when (and (not no-expire)
10978              gnus-newsgroup-auto-expire
10979              (memq mark gnus-auto-expirable-marks))
10980     (setq mark gnus-expirable-mark))
10981   (let ((article (or article (gnus-summary-article-number)))
10982         (old-mark (gnus-summary-article-mark article)))
10983     ;; Allow the backend to change the mark.
10984     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10985     (if (eq mark old-mark)
10986         t
10987       (unless article
10988         (error "No article on current line"))
10989       (if (not (if (or (= mark gnus-unread-mark)
10990                        (= mark gnus-ticked-mark)
10991                        (= mark gnus-spam-mark)
10992                        (= mark gnus-dormant-mark))
10993                    (gnus-mark-article-as-unread article mark)
10994                  (gnus-mark-article-as-read article mark)))
10995           t
10996         ;; See whether the article is to be put in the cache.
10997         (and gnus-use-cache
10998              (not (= mark gnus-canceled-mark))
10999              (vectorp (gnus-summary-article-header article))
11000              (save-excursion
11001                (gnus-cache-possibly-enter-article
11002                 gnus-newsgroup-name article
11003                 (= mark gnus-ticked-mark)
11004                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11005
11006         (when (gnus-summary-goto-subject article nil t)
11007           (let ((inhibit-read-only t))
11008             (gnus-summary-show-thread)
11009             ;; Fix the mark.
11010             (gnus-summary-update-mark mark 'unread)
11011             t))))))
11012
11013 (defun gnus-summary-update-secondary-mark (article)
11014   "Update the secondary (read, process, cache) mark."
11015   (gnus-summary-update-mark
11016    (cond ((memq article gnus-newsgroup-processable)
11017           gnus-process-mark)
11018          ((memq article gnus-newsgroup-cached)
11019           gnus-cached-mark)
11020          ((memq article gnus-newsgroup-replied)
11021           gnus-replied-mark)
11022          ((memq article gnus-newsgroup-forwarded)
11023           gnus-forwarded-mark)
11024          ((memq article gnus-newsgroup-saved)
11025           gnus-saved-mark)
11026          ((memq article gnus-newsgroup-unseen)
11027           gnus-unseen-mark)
11028          (t gnus-no-mark))
11029    'replied)
11030   (when (gnus-visual-p 'summary-highlight 'highlight)
11031     (gnus-summary-highlight-line)
11032     (gnus-run-hooks 'gnus-summary-update-hook))
11033   t)
11034
11035 (defun gnus-summary-update-download-mark (article)
11036   "Update the download mark."
11037   (gnus-summary-update-mark
11038    (cond ((memq article gnus-newsgroup-undownloaded)
11039           gnus-undownloaded-mark)
11040          (gnus-newsgroup-agentized
11041           gnus-downloaded-mark)
11042          (t
11043           gnus-no-mark))
11044    'download)
11045   (gnus-summary-update-line t)
11046   t)
11047
11048 (defun gnus-summary-update-mark (mark type)
11049   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11050         (inhibit-read-only t))
11051     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11052     (when forward
11053       (when (looking-at "\r")
11054         (incf forward))
11055       (when (<= (+ forward (point)) (point-max))
11056         ;; Go to the right position on the line.
11057         (goto-char (+ forward (point)))
11058         ;; Replace the old mark with the new mark.
11059         (let ((to-insert
11060                (mm-subst-char-in-string
11061                 (char-after) mark
11062                 (buffer-substring (point) (1+ (point))))))
11063           (delete-region (point) (1+ (point)))
11064           (insert to-insert))
11065         ;; Optionally update the marks by some user rule.
11066         (when (eq type 'unread)
11067           (gnus-data-set-mark
11068            (gnus-data-find (gnus-summary-article-number)) mark)
11069           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11070
11071 (defun gnus-mark-article-as-read (article &optional mark)
11072   "Enter ARTICLE in the pertinent lists and remove it from others."
11073   ;; Make the article expirable.
11074   (let ((mark (or mark gnus-del-mark)))
11075     (setq gnus-newsgroup-expirable
11076           (if (= mark gnus-expirable-mark)
11077               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11078             (delq article gnus-newsgroup-expirable)))
11079     ;; Remove from unread and marked lists.
11080     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11081     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11082     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11083     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11084     (push (cons article mark) gnus-newsgroup-reads)
11085     ;; Possibly remove from cache, if that is used.
11086     (when gnus-use-cache
11087       (gnus-cache-enter-remove-article article))
11088     t))
11089
11090 (defun gnus-mark-article-as-unread (article &optional mark)
11091   "Enter ARTICLE in the pertinent lists and remove it from others."
11092   (let ((mark (or mark gnus-ticked-mark)))
11093     (if (<= article 0)
11094         (progn
11095           (gnus-error 1 "Can't mark negative article numbers")
11096           nil)
11097       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11098             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11099             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11100             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11101             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11102
11103       ;; Unsuppress duplicates?
11104       (when gnus-suppress-duplicates
11105         (gnus-dup-unsuppress-article article))
11106
11107       (cond ((= mark gnus-ticked-mark)
11108              (setq gnus-newsgroup-marked
11109                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11110             ((= mark gnus-spam-mark)
11111              (setq gnus-newsgroup-spam-marked
11112                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11113                                             article)))
11114             ((= mark gnus-dormant-mark)
11115              (setq gnus-newsgroup-dormant
11116                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11117             (t
11118              (setq gnus-newsgroup-unreads
11119                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11120       (gnus-alist-pull article gnus-newsgroup-reads)
11121       t)))
11122
11123 (defun gnus-summary-tick-article-forward (n)
11124   "Tick N articles forwards.
11125 If N is negative, tick backwards instead.
11126 The difference between N and the number of articles ticked is returned."
11127   (interactive "p")
11128   (gnus-summary-mark-forward n gnus-ticked-mark))
11129
11130 (defun gnus-summary-tick-article-backward (n)
11131   "Tick N articles backwards.
11132 The difference between N and the number of articles ticked is returned."
11133   (interactive "p")
11134   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11135
11136 (defun gnus-summary-tick-article (&optional article clear-mark)
11137   "Mark current article as unread.
11138 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11139 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11140   (interactive)
11141   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11142                                        gnus-ticked-mark)))
11143
11144 (defun gnus-summary-mark-as-read-forward (n)
11145   "Mark N articles as read forwards.
11146 If N is negative, mark backwards instead.
11147 The difference between N and the actual number of articles marked is
11148 returned."
11149   (interactive "p")
11150   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11151
11152 (defun gnus-summary-mark-as-read-backward (n)
11153   "Mark the N articles as read backwards.
11154 The difference between N and the actual number of articles marked is
11155 returned."
11156   (interactive "p")
11157   (gnus-summary-mark-forward
11158    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11159
11160 (defun gnus-summary-mark-as-read (&optional article mark)
11161   "Mark current article as read.
11162 ARTICLE specifies the article to be marked as read.
11163 MARK specifies a string to be inserted at the beginning of the line."
11164   (gnus-summary-mark-article article mark))
11165
11166 (defun gnus-summary-clear-mark-forward (n)
11167   "Clear marks from N articles forward.
11168 If N is negative, clear backward instead.
11169 The difference between N and the number of marks cleared is returned."
11170   (interactive "p")
11171   (gnus-summary-mark-forward n gnus-unread-mark))
11172
11173 (defun gnus-summary-clear-mark-backward (n)
11174   "Clear marks from N articles backward.
11175 The difference between N and the number of marks cleared is returned."
11176   (interactive "p")
11177   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11178
11179 (defun gnus-summary-mark-unread-as-read ()
11180   "Intended to be used by `gnus-mark-article-hook'."
11181   (when (memq gnus-current-article gnus-newsgroup-unreads)
11182     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11183
11184 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11185   "Intended to be used by `gnus-mark-article-hook'."
11186   (let ((mark (gnus-summary-article-mark)))
11187     (when (or (gnus-unread-mark-p mark)
11188               (gnus-read-mark-p mark))
11189       (gnus-summary-mark-article gnus-current-article
11190                                  (or new-mark gnus-read-mark)))))
11191
11192 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11193   "Intended to be used by `gnus-mark-article-hook'."
11194   (let ((mark (gnus-summary-article-mark)))
11195     (when (or (gnus-unread-mark-p mark)
11196               (gnus-read-mark-p mark))
11197       (gnus-summary-mark-article (gnus-summary-article-number)
11198                                  (or new-mark gnus-read-mark)))))
11199
11200 (defun gnus-summary-mark-unread-as-ticked ()
11201   "Intended to be used by `gnus-mark-article-hook'."
11202   (when (memq gnus-current-article gnus-newsgroup-unreads)
11203     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11204
11205 (defun gnus-summary-mark-region-as-read (point mark all)
11206   "Mark all unread articles between point and mark as read.
11207 If given a prefix, mark all articles between point and mark as read,
11208 even ticked and dormant ones."
11209   (interactive "r\nP")
11210   (save-excursion
11211     (let (article)
11212       (goto-char point)
11213       (beginning-of-line)
11214       (while (and
11215               (< (point) mark)
11216               (progn
11217                 (when (or all
11218                           (memq (setq article (gnus-summary-article-number))
11219                                 gnus-newsgroup-unreads))
11220                   (gnus-summary-mark-article article gnus-del-mark))
11221                 t)
11222               (gnus-summary-find-next))))))
11223
11224 (defun gnus-summary-mark-below (score mark)
11225   "Mark articles with score less than SCORE with MARK."
11226   (interactive "P\ncMark: ")
11227   (setq score (if score
11228                   (prefix-numeric-value score)
11229                 (or gnus-summary-default-score 0)))
11230   (with-current-buffer gnus-summary-buffer
11231     (goto-char (point-min))
11232     (while
11233         (progn
11234           (and (< (gnus-summary-article-score) score)
11235                (gnus-summary-mark-article nil mark))
11236           (gnus-summary-find-next)))))
11237
11238 (defun gnus-summary-kill-below (&optional score)
11239   "Mark articles with score below SCORE as read."
11240   (interactive "P")
11241   (gnus-summary-mark-below score gnus-killed-mark))
11242
11243 (defun gnus-summary-clear-above (&optional score)
11244   "Clear all marks from articles with score above SCORE."
11245   (interactive "P")
11246   (gnus-summary-mark-above score gnus-unread-mark))
11247
11248 (defun gnus-summary-tick-above (&optional score)
11249   "Tick all articles with score above SCORE."
11250   (interactive "P")
11251   (gnus-summary-mark-above score gnus-ticked-mark))
11252
11253 (defun gnus-summary-mark-above (score mark)
11254   "Mark articles with score over SCORE with MARK."
11255   (interactive "P\ncMark: ")
11256   (setq score (if score
11257                   (prefix-numeric-value score)
11258                 (or gnus-summary-default-score 0)))
11259   (with-current-buffer gnus-summary-buffer
11260     (goto-char (point-min))
11261     (while (and (progn
11262                   (when (> (gnus-summary-article-score) score)
11263                     (gnus-summary-mark-article nil mark))
11264                   t)
11265                 (gnus-summary-find-next)))))
11266
11267 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11268 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11269 (defun gnus-summary-limit-include-expunged (&optional no-error)
11270   "Display all the hidden articles that were expunged for low scores."
11271   (interactive)
11272   (let ((inhibit-read-only t))
11273     (let ((scored gnus-newsgroup-scored)
11274           headers h)
11275       (while scored
11276         (unless (gnus-summary-article-header (caar scored))
11277           (and (setq h (gnus-number-to-header (caar scored)))
11278                (< (cdar scored) gnus-summary-expunge-below)
11279                (push h headers)))
11280         (setq scored (cdr scored)))
11281       (if (not headers)
11282           (when (not no-error)
11283             (error "No expunged articles hidden"))
11284         (goto-char (point-min))
11285         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11286         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11287         (dolist (x headers)
11288           (push (mail-header-number x) gnus-newsgroup-limit))
11289         (gnus-summary-prepare-unthreaded (nreverse headers))
11290         (goto-char (point-min))
11291         (gnus-summary-position-point)
11292         t))))
11293
11294 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11295   "Mark all unread articles in this newsgroup as read.
11296 If prefix argument ALL is non-nil, ticked and dormant articles will
11297 also be marked as read.
11298 If QUIETLY is non-nil, no questions will be asked.
11299
11300 If TO-HERE is non-nil, it should be a point in the buffer.  All
11301 articles before (after, if REVERSE is set) this point will be marked
11302 as read.
11303
11304 Note that this function will only catch up the unread article
11305 in the current summary buffer limitation.
11306
11307 The number of articles marked as read is returned."
11308   (interactive "P")
11309   (prog1
11310       (save-excursion
11311         (when (or quietly
11312                   (not gnus-interactive-catchup) ;Without confirmation?
11313                   gnus-expert-user
11314                   (gnus-y-or-n-p
11315                    (if all
11316                        "Mark absolutely all articles as read? "
11317                      "Mark all unread articles as read? ")))
11318           (if (and not-mark
11319                    (not gnus-newsgroup-adaptive)
11320                    (not gnus-newsgroup-auto-expire)
11321                    (not gnus-suppress-duplicates)
11322                    (or (not gnus-use-cache)
11323                        (eq gnus-use-cache 'passive)))
11324               (progn
11325                 (when all
11326                   (setq gnus-newsgroup-marked nil
11327                         gnus-newsgroup-spam-marked nil
11328                         gnus-newsgroup-dormant nil))
11329                 (setq gnus-newsgroup-unreads
11330                       (gnus-sorted-nunion
11331                        (gnus-sorted-intersection gnus-newsgroup-unreads
11332                                                  gnus-newsgroup-downloadable)
11333                        (gnus-sorted-difference gnus-newsgroup-unfetched
11334                                                gnus-newsgroup-cached))))
11335             ;; We actually mark all articles as canceled, which we
11336             ;; have to do when using auto-expiry or adaptive scoring.
11337             (gnus-summary-show-all-threads)
11338             (if (and to-here reverse)
11339                 (progn
11340                   (goto-char to-here)
11341                   (gnus-summary-mark-current-read-and-unread-as-read
11342                    gnus-catchup-mark)
11343                   (while (gnus-summary-find-next (not all))
11344                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11345               (when (gnus-summary-first-subject (not all))
11346                 (while (and
11347                         (if to-here (< (point) to-here) t)
11348                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11349                         (gnus-summary-find-next (not all))))))
11350             (gnus-set-mode-line 'summary))
11351           t))
11352     (gnus-summary-position-point)))
11353
11354 (defun gnus-summary-catchup-to-here (&optional all)
11355   "Mark all unticked articles before the current one as read.
11356 If ALL is non-nil, also mark ticked and dormant articles as read."
11357   (interactive "P")
11358   (save-excursion
11359     (gnus-save-hidden-threads
11360       (let ((beg (point)))
11361         ;; We check that there are unread articles.
11362         (when (or all (gnus-summary-find-prev))
11363           (gnus-summary-catchup all t beg)))))
11364   (gnus-summary-position-point))
11365
11366 (defun gnus-summary-catchup-from-here (&optional all)
11367   "Mark all unticked articles after (and including) the current one as read.
11368 If ALL is non-nil, also mark ticked and dormant articles as read."
11369   (interactive "P")
11370   (save-excursion
11371     (gnus-save-hidden-threads
11372       (let ((beg (point)))
11373         ;; We check that there are unread articles.
11374         (when (or all (gnus-summary-find-next))
11375           (gnus-summary-catchup all t beg nil t)))))
11376   (gnus-summary-position-point))
11377
11378 (defun gnus-summary-catchup-all (&optional quietly)
11379   "Mark all articles in this newsgroup as read.
11380 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11381 instead, which marks only unread articles as read."
11382   (interactive "P")
11383   (gnus-summary-catchup t quietly))
11384
11385 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11386   "Mark all unread articles in this group as read, then exit.
11387 If prefix argument ALL is non-nil, all articles are marked as read.
11388 If QUIETLY is non-nil, no questions will be asked."
11389   (interactive "P")
11390   (when (gnus-summary-catchup all quietly nil 'fast)
11391     ;; Select next newsgroup or exit.
11392     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11393              (eq gnus-auto-select-next 'quietly))
11394         (gnus-summary-next-group nil)
11395       (gnus-summary-exit))))
11396
11397 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11398   "Mark all articles in this newsgroup as read, and then exit.
11399 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11400 instead, which marks only unread articles as read."
11401   (interactive "P")
11402   (gnus-summary-catchup-and-exit t quietly))
11403
11404 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11405   "Mark all articles in this group as read and select the next group.
11406 If given a prefix, mark all articles, unread as well as ticked, as
11407 read."
11408   (interactive "P")
11409   (save-excursion
11410     (gnus-summary-catchup all))
11411   (gnus-summary-next-group))
11412
11413 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11414   "Mark all articles in this group as read and select the previous group.
11415 If given a prefix, mark all articles, unread as well as ticked, as
11416 read."
11417   (interactive "P")
11418   (save-excursion
11419     (gnus-summary-catchup all))
11420   (gnus-summary-next-group nil nil t))
11421
11422 ;;;
11423 ;;; with article
11424 ;;;
11425
11426 (defmacro gnus-with-article (article &rest forms)
11427   "Select ARTICLE and perform FORMS in the original article buffer.
11428 Then replace the article with the result."
11429   `(progn
11430      ;; We don't want the article to be marked as read.
11431      (let (gnus-mark-article-hook)
11432        (gnus-summary-select-article t t nil ,article))
11433      (set-buffer gnus-original-article-buffer)
11434      ,@forms
11435      (if (not (gnus-check-backend-function
11436                'request-replace-article (car gnus-article-current)))
11437          (gnus-message 5 "Read-only group; not replacing")
11438        (unless (gnus-request-replace-article
11439                 ,article (car gnus-article-current)
11440                 (current-buffer) t)
11441          (error "Couldn't replace article")))
11442      ;; The cache and backlog have to be flushed somewhat.
11443      (when gnus-keep-backlog
11444        (gnus-backlog-remove-article
11445         (car gnus-article-current) (cdr gnus-article-current)))
11446      (when gnus-use-cache
11447        (gnus-cache-update-article
11448         (car gnus-article-current) (cdr gnus-article-current)))))
11449
11450 (put 'gnus-with-article 'lisp-indent-function 1)
11451 (put 'gnus-with-article 'edebug-form-spec '(form body))
11452
11453 ;; Thread-based commands.
11454
11455 (defun gnus-summary-articles-in-thread (&optional article)
11456   "Return a list of all articles in the current thread.
11457 If ARTICLE is non-nil, return all articles in the thread that starts
11458 with that article."
11459   (let* ((article (or article (gnus-summary-article-number)))
11460          (data (gnus-data-find-list article))
11461          (top-level (gnus-data-level (car data)))
11462          (top-subject
11463           (cond ((null gnus-thread-operation-ignore-subject)
11464                  (gnus-simplify-subject-re
11465                   (mail-header-subject (gnus-data-header (car data)))))
11466                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11467                  (gnus-simplify-subject-fuzzy
11468                   (mail-header-subject (gnus-data-header (car data)))))
11469                 (t nil)))
11470          (end-point (save-excursion
11471                       (goto-char (gnus-data-pos (car data)))
11472                       (if (gnus-summary-go-to-next-thread)
11473                           (point) (point-max))))
11474          articles)
11475     (while (and data
11476                 (< (gnus-data-pos (car data)) end-point))
11477       (when (or (not top-subject)
11478                 (string= top-subject
11479                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11480                              (gnus-simplify-subject-fuzzy
11481                               (mail-header-subject
11482                                (gnus-data-header (car data))))
11483                            (gnus-simplify-subject-re
11484                             (mail-header-subject
11485                              (gnus-data-header (car data)))))))
11486         (push (gnus-data-number (car data)) articles))
11487       (unless (and (setq data (cdr data))
11488                    (> (gnus-data-level (car data)) top-level))
11489         (setq data nil)))
11490     ;; Return the list of articles.
11491     (nreverse articles)))
11492
11493 (defun gnus-summary-rethread-current ()
11494   "Rethread the thread the current article is part of."
11495   (interactive)
11496   (let* ((gnus-show-threads t)
11497          (article (gnus-summary-article-number))
11498          (id (mail-header-id (gnus-summary-article-header)))
11499          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11500     (unless id
11501       (error "No article on the current line"))
11502     (gnus-rebuild-thread id)
11503     (gnus-summary-goto-subject article)))
11504
11505 (defun gnus-summary-reparent-thread ()
11506   "Make the current article child of the marked (or previous) article.
11507
11508 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11509 is non-nil or the Subject: of both articles are the same."
11510   (interactive)
11511   (unless (not (gnus-group-read-only-p))
11512     (error "The current newsgroup does not support article editing"))
11513   (unless (<= (length gnus-newsgroup-processable) 1)
11514     (error "No more than one article may be marked"))
11515   (let ((child (gnus-summary-article-number))
11516         ;; First grab the marked article, otherwise one line up.
11517         (parent (if (not (null gnus-newsgroup-processable))
11518                     (car gnus-newsgroup-processable)
11519                   (save-excursion
11520                     (if (eq (forward-line -1) 0)
11521                         (gnus-summary-article-number)
11522                       (error "Beginning of summary buffer"))))))
11523     (gnus-summary-reparent-children parent (list child))))
11524
11525 (defun gnus-summary-reparent-children (parent children)
11526   "Make PARENT the parent of CHILDREN.
11527 When called interactively, PARENT is the current article and CHILDREN
11528 are the process-marked articles."
11529   (interactive
11530    (list (gnus-summary-article-number)
11531          (gnus-summary-work-articles nil)))
11532   (dolist (child children)
11533     (save-window-excursion
11534       (let ((gnus-article-buffer " *reparent*"))
11535         (unless (not (eq parent child))
11536           (error "An article may not be self-referential"))
11537         (let ((message-id (mail-header-id
11538                            (gnus-summary-article-header parent))))
11539           (unless (and message-id (not (equal message-id "")))
11540             (error "No message-id in desired parent"))
11541           (gnus-with-article child
11542             (save-restriction
11543               (goto-char (point-min))
11544               (message-narrow-to-head)
11545               (if (re-search-forward "^References: " nil t)
11546                   (progn
11547                     (re-search-forward "^[^ \t]" nil t)
11548                     (forward-line -1)
11549                     (end-of-line)
11550                     (insert " " message-id))
11551                 (insert "References: " message-id "\n"))))
11552           (set-buffer gnus-summary-buffer)
11553           (gnus-summary-unmark-all-processable)
11554           (gnus-summary-update-article child)
11555           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11556             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11557           (gnus-summary-rethread-current)
11558           (gnus-message 3 "Article %d is now the child of article %d"
11559                         child parent))))))
11560
11561 (defun gnus-summary-toggle-threads (&optional arg)
11562   "Toggle showing conversation threads.
11563 If ARG is positive number, turn showing conversation threads on."
11564   (interactive "P")
11565   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11566     (setq gnus-show-threads
11567           (if (null arg) (not gnus-show-threads)
11568             (> (prefix-numeric-value arg) 0)))
11569     (gnus-summary-prepare)
11570     (gnus-summary-goto-subject current)
11571     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11572     (gnus-summary-position-point)))
11573
11574 (eval-and-compile
11575   (if (fboundp 'remove-overlays)
11576       (defalias 'gnus-remove-overlays 'remove-overlays)
11577     (defun gnus-remove-overlays (beg end name val)
11578       "Clear BEG and END of overlays whose property NAME has value VAL.
11579 For compatibility with XEmacs."
11580       (dolist (ov (gnus-overlays-in beg end))
11581         (when (eq (gnus-overlay-get ov name) val)
11582           (gnus-delete-overlay ov))))))
11583
11584 (defun gnus-summary-show-all-threads ()
11585   "Show all threads."
11586   (interactive)
11587   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11588   (gnus-summary-position-point))
11589
11590 (defsubst gnus-summary--inv (p)
11591   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11592
11593 (defun gnus-summary-show-thread ()
11594   "Show thread subtrees.
11595 Returns nil if no thread was there to be shown."
11596   (interactive)
11597   (let* ((orig (point))
11598          (end (point-at-eol))
11599          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11600          ;; Leave point at bol
11601          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11602          (eoi (when end
11603                 (if (fboundp 'next-single-char-property-change)
11604                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11605                     (or (next-single-char-property-change end 'invisible)
11606                         (point-max))
11607                   (while (progn
11608                            (end-of-line 2)
11609                            (and (not (eobp))
11610                                 (eq (get-char-property (point) 'invisible)
11611                                     'gnus-sum))))
11612                   (point)))))
11613     (when eoi
11614       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11615       (goto-char orig)
11616       (gnus-summary-position-point)
11617       eoi)))
11618
11619 (defun gnus-summary-maybe-hide-threads ()
11620   "If requested, hide the threads that should be hidden."
11621   (when (and gnus-show-threads
11622              gnus-thread-hide-subtree)
11623     (gnus-summary-hide-all-threads
11624      (if (or (consp gnus-thread-hide-subtree)
11625              (functionp gnus-thread-hide-subtree))
11626          (gnus-make-predicate gnus-thread-hide-subtree)
11627        nil))))
11628
11629 ;;; Hiding predicates.
11630
11631 (defun gnus-article-unread-p (header)
11632   (memq (mail-header-number header) gnus-newsgroup-unreads))
11633
11634 (defun gnus-article-unseen-p (header)
11635   (memq (mail-header-number header) gnus-newsgroup-unseen))
11636
11637 (defun gnus-map-articles (predicate articles)
11638   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11639   (apply 'gnus-or (mapcar predicate
11640                           (mapcar (lambda (number)
11641                                     (gnus-summary-article-header number))
11642                                   articles))))
11643
11644 (defun gnus-summary-hide-all-threads (&optional predicate)
11645   "Hide all thread subtrees.
11646 If PREDICATE is supplied, threads that satisfy this predicate
11647 will not be hidden."
11648   (interactive)
11649   (save-excursion
11650     (goto-char (point-min))
11651     (let ((end nil)
11652           (count 0))
11653       (while (not end)
11654         (incf count)
11655         (when (zerop (mod count 1000))
11656           (message "Hiding all threads... %d" count))
11657         (when (or (not predicate)
11658                   (gnus-map-articles
11659                    predicate (gnus-summary-article-children)))
11660             (gnus-summary-hide-thread))
11661         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11662   (gnus-summary-position-point))
11663
11664 (defun gnus-summary-hide-thread ()
11665   "Hide thread subtrees.
11666 If PREDICATE is supplied, threads that satisfy this predicate
11667 will not be hidden.
11668 Returns nil if no threads were there to be hidden."
11669   (interactive)
11670   (let ((start (point))
11671         (starteol (line-end-position))
11672         (article (gnus-summary-article-number)))
11673     (goto-char start)
11674     ;; Go forward until either the buffer ends or the subthread ends.
11675     (when (and (not (eobp))
11676                (or (zerop (gnus-summary-next-thread 1 t))
11677                    (goto-char (point-max))))
11678       (if (and (> (point) start)
11679                ;; FIXME: this should actually search for a non-invisible \n.
11680                (search-backward "\n" start t))
11681           (progn
11682             (when (> (point) starteol)
11683               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11684               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11685                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11686                 (gnus-overlay-put ol 'evaporate t)))
11687             (gnus-summary-goto-subject article)
11688             (when (> start (point))
11689               (message "Hiding the thread moved us backwards, aborting!")
11690               (goto-char (point-max))))
11691         (goto-char start)
11692         nil))))
11693
11694 (defun gnus-summary-go-to-next-thread (&optional previous)
11695   "Go to the same level (or less) next thread.
11696 If PREVIOUS is non-nil, go to previous thread instead.
11697 Return the article number moved to, or nil if moving was impossible."
11698   (let ((level (gnus-summary-thread-level))
11699         (way (if previous -1 1))
11700         (beg (point)))
11701     (forward-line way)
11702     (while (and (not (eobp))
11703                 (< level (gnus-summary-thread-level)))
11704       (forward-line way))
11705     (if (eobp)
11706         (progn
11707           (goto-char beg)
11708           nil)
11709       (setq beg (point))
11710       (prog1
11711           (gnus-summary-article-number)
11712         (goto-char beg)))))
11713
11714 (defun gnus-summary-next-thread (n &optional silent)
11715   "Go to the same level next N'th thread.
11716 If N is negative, search backward instead.
11717 Returns the difference between N and the number of skips actually
11718 done.
11719
11720 If SILENT, don't output messages."
11721   (interactive "p")
11722   (let ((backward (< n 0))
11723         (n (abs n)))
11724     (while (and (> n 0)
11725                 (gnus-summary-go-to-next-thread backward))
11726       (decf n))
11727     (unless silent
11728       (gnus-summary-position-point))
11729     (when (and (not silent) (/= 0 n))
11730       (gnus-message 7 "No more threads"))
11731     n))
11732
11733 (defun gnus-summary-prev-thread (n)
11734   "Go to the same level previous N'th thread.
11735 Returns the difference between N and the number of skips actually
11736 done."
11737   (interactive "p")
11738   (gnus-summary-next-thread (- n)))
11739
11740 (defun gnus-summary-go-down-thread ()
11741   "Go down one level in the current thread."
11742   (let ((children (gnus-summary-article-children)))
11743     (when children
11744       (gnus-summary-goto-subject (car children)))))
11745
11746 (defun gnus-summary-go-up-thread ()
11747   "Go up one level in the current thread."
11748   (let ((parent (gnus-summary-article-parent)))
11749     (when parent
11750       (gnus-summary-goto-subject parent))))
11751
11752 (defun gnus-summary-down-thread (n)
11753   "Go down thread N steps.
11754 If N is negative, go up instead.
11755 Returns the difference between N and how many steps down that were
11756 taken."
11757   (interactive "p")
11758   (let ((up (< n 0))
11759         (n (abs n)))
11760     (while (and (> n 0)
11761                 (if up (gnus-summary-go-up-thread)
11762                   (gnus-summary-go-down-thread)))
11763       (setq n (1- n)))
11764     (gnus-summary-position-point)
11765     (when (/= 0 n)
11766       (gnus-message 7 "Can't go further"))
11767     n))
11768
11769 (defun gnus-summary-up-thread (n)
11770   "Go up thread N steps.
11771 If N is negative, go down instead.
11772 Returns the difference between N and how many steps down that were
11773 taken."
11774   (interactive "p")
11775   (gnus-summary-down-thread (- n)))
11776
11777 (defun gnus-summary-top-thread ()
11778   "Go to the top of the thread."
11779   (interactive)
11780   (while (gnus-summary-go-up-thread))
11781   (gnus-summary-article-number))
11782
11783 (defun gnus-summary-expire-thread ()
11784   "Mark articles under current thread as expired."
11785   (interactive)
11786   (gnus-summary-kill-thread 0))
11787
11788 (defun gnus-summary-kill-thread (&optional unmark)
11789   "Mark articles under current thread as read.
11790 If the prefix argument is positive, remove any kinds of marks.
11791 If the prefix argument is zero, mark thread as expired.
11792 If the prefix argument is negative, tick articles instead."
11793   (interactive "P")
11794   (when unmark
11795     (setq unmark (prefix-numeric-value unmark)))
11796   (let ((articles (gnus-summary-articles-in-thread))
11797         (hide (or (null unmark) (= unmark 0))))
11798     (save-excursion
11799       ;; Expand the thread.
11800       (gnus-summary-show-thread)
11801       ;; Mark all the articles.
11802       (while articles
11803         (gnus-summary-goto-subject (car articles))
11804         (cond ((null unmark)
11805                (gnus-summary-mark-article-as-read gnus-killed-mark))
11806               ((> unmark 0)
11807                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11808               ((= unmark 0)
11809                (gnus-summary-mark-article nil gnus-expirable-mark))
11810               (t
11811                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11812         (setq articles (cdr articles))))
11813     ;; Hide killed subtrees when hide is true.
11814     (and hide
11815          gnus-thread-hide-killed
11816          (gnus-summary-hide-thread))
11817     ;; If hide is t, go to next unread subject.
11818     (when hide
11819       ;; Go to next unread subject.
11820       (gnus-summary-next-subject 1 t)))
11821   (gnus-set-mode-line 'summary))
11822
11823 ;; Summary sorting commands
11824
11825 (defun gnus-summary-sort-by-number (&optional reverse)
11826   "Sort the summary buffer by article number.
11827 Argument REVERSE means reverse order."
11828   (interactive "P")
11829   (gnus-summary-sort 'number reverse))
11830
11831 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11832   "Sort the summary buffer by most recent article number.
11833 Argument REVERSE means reverse order."
11834   (interactive "P")
11835   (gnus-summary-sort 'most-recent-number reverse))
11836
11837 (defun gnus-summary-sort-by-random (&optional reverse)
11838   "Randomize the order in the summary buffer.
11839 Argument REVERSE means to randomize in reverse order."
11840   (interactive "P")
11841   (gnus-summary-sort 'random reverse))
11842
11843 (defun gnus-summary-sort-by-author (&optional reverse)
11844   "Sort the summary buffer by author name alphabetically.
11845 If `case-fold-search' is non-nil, case of letters is ignored.
11846 Argument REVERSE means reverse order."
11847   (interactive "P")
11848   (gnus-summary-sort 'author reverse))
11849
11850 (defun gnus-summary-sort-by-recipient (&optional reverse)
11851   "Sort the summary buffer by recipient name alphabetically.
11852 If `case-fold-search' is non-nil, case of letters is ignored.
11853 Argument REVERSE means reverse order."
11854   (interactive "P")
11855   (gnus-summary-sort 'recipient reverse))
11856
11857 (defun gnus-summary-sort-by-subject (&optional reverse)
11858   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11859 If `case-fold-search' is non-nil, case of letters is ignored.
11860 Argument REVERSE means reverse order."
11861   (interactive "P")
11862   (gnus-summary-sort 'subject reverse))
11863
11864 (defun gnus-summary-sort-by-date (&optional reverse)
11865   "Sort the summary buffer by date.
11866 Argument REVERSE means reverse order."
11867   (interactive "P")
11868   (gnus-summary-sort 'date reverse))
11869
11870 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11871   "Sort the summary buffer by most recent date.
11872 Argument REVERSE means reverse order."
11873   (interactive "P")
11874   (gnus-summary-sort 'most-recent-date reverse))
11875
11876 (defun gnus-summary-sort-by-score (&optional reverse)
11877   "Sort the summary buffer by score.
11878 Argument REVERSE means reverse order."
11879   (interactive "P")
11880   (gnus-summary-sort 'score reverse))
11881
11882 (defun gnus-summary-sort-by-lines (&optional reverse)
11883   "Sort the summary buffer by the number of lines.
11884 Argument REVERSE means reverse order."
11885   (interactive "P")
11886   (gnus-summary-sort 'lines reverse))
11887
11888 (defun gnus-summary-sort-by-chars (&optional reverse)
11889   "Sort the summary buffer by article length.
11890 Argument REVERSE means reverse order."
11891   (interactive "P")
11892   (gnus-summary-sort 'chars reverse))
11893
11894 (defun gnus-summary-sort-by-original (&optional reverse)
11895   "Sort the summary buffer using the default sorting method.
11896 Argument REVERSE means reverse order."
11897   (interactive "P")
11898   (let* ((inhibit-read-only t)
11899          (gnus-summary-prepare-hook nil))
11900     ;; We do the sorting by regenerating the threads.
11901     (gnus-summary-prepare)
11902     ;; Hide subthreads if needed.
11903     (gnus-summary-maybe-hide-threads)))
11904
11905 (defun gnus-summary-sort (predicate reverse)
11906   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11907   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11908          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11909          (gnus-thread-sort-functions
11910           (if (not reverse)
11911               thread
11912             `(lambda (t1 t2)
11913                (,thread t2 t1))))
11914          (gnus-sort-gathered-threads-function
11915           gnus-thread-sort-functions)
11916          (gnus-article-sort-functions
11917           (if (not reverse)
11918               article
11919             `(lambda (t1 t2)
11920                (,article t2 t1))))
11921          (inhibit-read-only t)
11922          (gnus-summary-prepare-hook nil))
11923     ;; We do the sorting by regenerating the threads.
11924     (gnus-summary-prepare)
11925     ;; Hide subthreads if needed.
11926     (gnus-summary-maybe-hide-threads)))
11927
11928 ;; Summary saving commands.
11929
11930 (defun gnus-summary-save-article (&optional n not-saved)
11931   "Save the current article using the default saver function.
11932 If N is a positive number, save the N next articles.
11933 If N is a negative number, save the N previous articles.
11934 If N is nil and any articles have been marked with the process mark,
11935 save those articles instead.
11936 The variable `gnus-default-article-saver' specifies the saver function.
11937
11938 If the optional second argument NOT-SAVED is non-nil, articles saved
11939 will not be marked as saved."
11940   (interactive "P")
11941   (require 'gnus-art)
11942   (let* ((articles (gnus-summary-work-articles n))
11943          (save-buffer (save-excursion
11944                         (nnheader-set-temp-buffer " *Gnus Save*")))
11945          (num (length articles))
11946          ;; Whether to save decoded articles or raw articles.
11947          (decode (when gnus-article-save-coding-system
11948                    (get gnus-default-article-saver :decode)))
11949          ;; When saving many articles in a single file, use the other
11950          ;; function to save articles other than the first one.
11951          (saver2 (get gnus-default-article-saver :function))
11952          (gnus-prompt-before-saving (if saver2
11953                                         t
11954                                       gnus-prompt-before-saving))
11955          (gnus-default-article-saver gnus-default-article-saver)
11956          header file)
11957     (dolist (article articles)
11958       (setq header (gnus-summary-article-header article))
11959       (if (not (vectorp header))
11960           ;; This is a pseudo-article.
11961           (if (assq 'name header)
11962               (gnus-copy-file (cdr (assq 'name header)))
11963             (gnus-message 1 "Article %d is unsavable" article))
11964         ;; This is a real article.
11965         (save-window-excursion
11966           (gnus-summary-select-article decode decode nil article)
11967           (gnus-summary-goto-subject article))
11968         (with-current-buffer save-buffer
11969           (erase-buffer)
11970           (insert-buffer-substring (if decode
11971                                        gnus-article-buffer
11972                                      gnus-original-article-buffer)))
11973         (setq file (gnus-article-save save-buffer file num))
11974         (gnus-summary-remove-process-mark article)
11975         (unless not-saved
11976           (gnus-summary-set-saved-mark article)))
11977       (when saver2
11978         (setq gnus-default-article-saver saver2
11979               saver2 nil)))
11980     (gnus-kill-buffer save-buffer)
11981     (gnus-summary-position-point)
11982     (gnus-set-mode-line 'summary)
11983     n))
11984
11985 (defun gnus-summary-pipe-output (&optional n sym)
11986   "Pipe the current article to a subprocess.
11987 If N is a positive number, pipe the N next articles.
11988 If N is a negative number, pipe the N previous articles.
11989 If N is nil and any articles have been marked with the process mark,
11990 pipe those articles instead.
11991 The default command to which articles are piped is specified by the
11992 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11993 will be prompted for the command.
11994
11995 The properties `:decode' and `:headers' that are put to the function
11996 symbol `gnus-summary-save-in-pipe' control whether this function
11997 decodes articles and what headers to keep (see the doc string for the
11998 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11999 is neither omitted nor the symbol `r', force including all headers
12000 regardless of the `:headers' property.  If it is the symbol `r',
12001 articles that are not decoded and include all headers will be piped
12002 no matter what the properties `:decode' and `:headers' are."
12003   (interactive (gnus-interactive "P\ny"))
12004   (require 'gnus-art)
12005   (let* ((articles (gnus-summary-work-articles n))
12006          (result-buffer "*Shell Command Output*")
12007          (all-headers (not (memq sym '(nil r))))
12008          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12009          (raw (eq sym 'r))
12010          (headers (get 'gnus-summary-save-in-pipe :headers))
12011          command result)
12012     (unless (numberp (car articles))
12013       (error "No article to pipe"))
12014     (setq command (gnus-read-shell-command
12015                    (concat "Shell command on "
12016                            (if (cdr articles)
12017                                (format "these %d articles" (length articles))
12018                              "this article")
12019                            ": ")
12020                    gnus-summary-pipe-output-default-command))
12021     (when (string-equal command "")
12022       (error "A command is required"))
12023     (when all-headers
12024       (put 'gnus-summary-save-in-pipe :headers nil))
12025     (unwind-protect
12026         (while articles
12027           (gnus-summary-goto-subject (pop articles))
12028           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12029           (when (and (get-buffer result-buffer)
12030                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12031             (setq result (concat result (with-current-buffer result-buffer
12032                                           (buffer-string))))))
12033       (put 'gnus-summary-save-in-pipe :headers headers))
12034     (unless (zerop (length result))
12035       (if (with-current-buffer (get-buffer-create result-buffer)
12036             (erase-buffer)
12037             (insert result)
12038             (prog1
12039                 (and (= (count-lines (point-min) (point)) 1)
12040                      (progn
12041                        (end-of-line 0)
12042                        (<= (current-column)
12043                            (window-width (minibuffer-window)))))
12044               (goto-char (point-min))))
12045           (message "%s" (substring result 0 -1))
12046         (message nil)
12047         (gnus-configure-windows 'pipe)))))
12048
12049 (defun gnus-summary-save-article-mail (&optional arg)
12050   "Append the current article to a Unix mail box file.
12051 If N is a positive number, save the N next articles.
12052 If N is a negative number, save the N previous articles.
12053 If N is nil and any articles have been marked with the process mark,
12054 save those articles instead."
12055   (interactive "P")
12056   (require 'gnus-art)
12057   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12058     (gnus-summary-save-article arg)))
12059
12060 (defun gnus-summary-save-article-rmail (&optional arg)
12061   "Append the current article to an rmail file.
12062 If N is a positive number, save the N next articles.
12063 If N is a negative number, save the N previous articles.
12064 If N is nil and any articles have been marked with the process mark,
12065 save those articles instead."
12066   (interactive "P")
12067   (require 'gnus-art)
12068   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12069     (gnus-summary-save-article arg)))
12070
12071 (defun gnus-summary-save-article-file (&optional arg)
12072   "Append the current article to a file.
12073 If N is a positive number, save the N next articles.
12074 If N is a negative number, save the N previous articles.
12075 If N is nil and any articles have been marked with the process mark,
12076 save those articles instead."
12077   (interactive "P")
12078   (require 'gnus-art)
12079   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12080     (gnus-summary-save-article arg)))
12081
12082 (defun gnus-summary-write-article-file (&optional arg)
12083   "Write the current article to a file, deleting the previous file.
12084 If N is a positive number, save the N next articles.
12085 If N is a negative number, save the N previous articles.
12086 If N is nil and any articles have been marked with the process mark,
12087 save those articles instead."
12088   (interactive "P")
12089   (require 'gnus-art)
12090   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12091     (gnus-summary-save-article arg)))
12092
12093 (defun gnus-summary-save-article-body-file (&optional arg)
12094   "Append the current article body to a file.
12095 If N is a positive number, save the N next articles.
12096 If N is a negative number, save the N previous articles.
12097 If N is nil and any articles have been marked with the process mark,
12098 save those articles instead."
12099   (interactive "P")
12100   (require 'gnus-art)
12101   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12102     (gnus-summary-save-article arg)))
12103
12104 (defun gnus-summary-write-article-body-file (&optional arg)
12105   "Write the current article body to a file, deleting the previous file.
12106 If N is a positive number, save the N next articles.
12107 If N is a negative number, save the N previous articles.
12108 If N is nil and any articles have been marked with the process mark,
12109 save those articles instead."
12110   (interactive "P")
12111   (require 'gnus-art)
12112   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12113     (gnus-summary-save-article arg)))
12114
12115 (defun gnus-summary-muttprint (&optional arg)
12116   "Print the current article using Muttprint.
12117 If N is a positive number, save the N next articles.
12118 If N is a negative number, save the N previous articles.
12119 If N is nil and any articles have been marked with the process mark,
12120 save those articles instead."
12121   (interactive "P")
12122   (require 'gnus-art)
12123   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12124     (gnus-summary-save-article arg t)))
12125
12126 (defun gnus-summary-pipe-message (program)
12127   "Pipe the current article through PROGRAM."
12128   (interactive "sProgram: ")
12129   (gnus-summary-select-article)
12130   (let ((mail-header-separator ""))
12131     (gnus-eval-in-buffer-window gnus-article-buffer
12132       (save-restriction
12133         (widen)
12134         (let ((start (window-start))
12135               (inhibit-read-only t))
12136           (message-pipe-buffer-body program)
12137           (set-window-start (get-buffer-window (current-buffer)) start))))))
12138
12139 (defun gnus-get-split-value (methods)
12140   "Return a value based on the split METHODS."
12141   (let (split-name method result match)
12142     (when methods
12143       (with-current-buffer gnus-original-article-buffer
12144         (save-restriction
12145           (nnheader-narrow-to-headers)
12146           (while (and methods (not split-name))
12147             (goto-char (point-min))
12148             (setq method (pop methods))
12149             (setq match (car method))
12150             (when (cond
12151                    ((stringp match)
12152                     ;; Regular expression.
12153                     (ignore-errors
12154                       (re-search-forward match nil t)))
12155                    ((functionp match)
12156                     ;; Function.
12157                     (save-restriction
12158                       (widen)
12159                       (setq result (funcall match gnus-newsgroup-name))))
12160                    ((consp match)
12161                     ;; Form.
12162                     (save-restriction
12163                       (widen)
12164                       (setq result (eval match)))))
12165               (setq split-name (cdr method))
12166               (cond ((stringp result)
12167                      (push (expand-file-name
12168                             result gnus-article-save-directory)
12169                            split-name))
12170                     ((consp result)
12171                      (setq split-name (append result split-name)))))))))
12172     (nreverse split-name)))
12173
12174 (defun gnus-valid-move-group-p (group)
12175   (and (symbolp group)
12176        (boundp group)
12177        (symbol-name group)
12178        (symbol-value group)
12179        (gnus-get-function (gnus-find-method-for-group
12180                            (symbol-name group)) 'request-accept-article t)))
12181
12182 (defun gnus-read-move-group-name (prompt default articles prefix)
12183   "Read a group name."
12184   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12185          (minibuffer-confirm-incomplete nil) ; XEmacs
12186          (prom
12187           (format "%s %s to"
12188                   prompt
12189                   (if (> (length articles) 1)
12190                       (format "these %d articles" (length articles))
12191                     "this article")))
12192          (to-newsgroup
12193           (cond
12194            ((null split-name)
12195             (gnus-group-completing-read
12196              prom
12197              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12198              nil prefix nil default))
12199            ((= 1 (length split-name))
12200             (gnus-group-completing-read
12201              prom
12202              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12203              nil prefix 'gnus-group-history (car split-name)))
12204            (t
12205             (gnus-completing-read
12206              prom (nreverse split-name) nil nil 'gnus-group-history))))
12207          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12208          encoded)
12209     (when to-newsgroup
12210       (if (or (string= to-newsgroup "")
12211               (string= to-newsgroup prefix))
12212           (setq to-newsgroup default))
12213       (unless to-newsgroup
12214         (error "No group name entered"))
12215       (setq encoded (mm-encode-coding-string
12216                      to-newsgroup
12217                      (gnus-group-name-charset to-method to-newsgroup)))
12218       (or (gnus-active encoded)
12219           (gnus-activate-group encoded nil nil to-method)
12220           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12221                                      to-newsgroup))
12222               (or (and (gnus-request-create-group encoded to-method)
12223                        (gnus-activate-group encoded nil nil to-method)
12224                        (gnus-subscribe-group encoded))
12225                   (error "Couldn't create group %s" to-newsgroup)))
12226           (error "No such group: %s" to-newsgroup))
12227       encoded)))
12228
12229 (defvar gnus-summary-save-parts-counter)
12230 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12231
12232 (defun gnus-summary-save-parts (type dir n &optional reverse)
12233   "Save parts matching TYPE to DIR.
12234 If REVERSE, save parts that do not match TYPE."
12235   (interactive
12236    (list (read-string "Save parts of type: "
12237                       (or (car gnus-summary-save-parts-type-history)
12238                           gnus-summary-save-parts-default-mime)
12239                       'gnus-summary-save-parts-type-history)
12240          (setq gnus-summary-save-parts-last-directory
12241                (read-directory-name "Save to directory: "
12242                                     gnus-summary-save-parts-last-directory
12243                                     nil t))
12244          current-prefix-arg))
12245   (gnus-summary-iterate n
12246     (let ((gnus-display-mime-function nil)
12247           gnus-article-prepare-hook
12248           gnus-article-decode-hook
12249           gnus-display-mime-function
12250           gnus-break-pages
12251           (gnus-inhibit-treatment t))
12252       (gnus-summary-select-article))
12253     (with-current-buffer gnus-article-buffer
12254       (let ((handles (or gnus-article-mime-handles
12255                          (mm-dissect-buffer nil gnus-article-loose-mime)
12256                          (and gnus-article-emulate-mime
12257                               (mm-uu-dissect))))
12258             (gnus-summary-save-parts-counter 1))
12259         (when handles
12260           (gnus-summary-save-parts-1 type dir handles reverse)
12261           (unless gnus-article-mime-handles ;; Don't destroy this case.
12262             (mm-destroy-parts handles)))))))
12263
12264 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12265   (if (stringp (car handle))
12266       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12267               (cdr handle))
12268     (when (if reverse
12269               (not (string-match type (mm-handle-media-type handle)))
12270             (string-match type (mm-handle-media-type handle)))
12271       (let ((file (expand-file-name
12272                    (gnus-map-function
12273                     mm-file-name-rewrite-functions
12274                     (file-name-nondirectory
12275                      (or
12276                       (mm-handle-filename handle)
12277                       (format "%s.%d.%d" gnus-newsgroup-name
12278                               (cdr gnus-article-current)
12279                               gnus-summary-save-parts-counter))))
12280                    dir)))
12281         (incf gnus-summary-save-parts-counter)
12282         (unless (file-exists-p file)
12283           (mm-save-part-to-file handle file))))))
12284
12285 ;; Summary extract commands
12286
12287 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12288   (let ((inhibit-read-only t)
12289         (article (gnus-summary-article-number))
12290         after-article b e)
12291     (unless (gnus-summary-goto-subject article)
12292       (error "No such article: %d" article))
12293     (gnus-summary-position-point)
12294     ;; If all commands are to be bunched up on one line, we collect
12295     ;; them here.
12296     (unless gnus-view-pseudos-separately
12297       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12298             files action)
12299         (while ps
12300           (setq action (cdr (assq 'action (car ps))))
12301           (setq files (list (cdr (assq 'name (car ps)))))
12302           (while (and ps (cdr ps)
12303                       (string= (or action "1")
12304                                (or (cdr (assq 'action (cadr ps))) "2")))
12305             (push (cdr (assq 'name (cadr ps))) files)
12306             (setcdr ps (cddr ps)))
12307           (when files
12308             (when (not (string-match "%s" action))
12309               (push " " files))
12310             (push " " files)
12311             (when (assq 'execute (car ps))
12312               (setcdr (assq 'execute (car ps))
12313                       (funcall (if (string-match "%s" action)
12314                                    'format 'concat)
12315                                action
12316                                (mapconcat
12317                                 (lambda (f)
12318                                   (if (equal f " ")
12319                                       f
12320                                     (shell-quote-argument f)))
12321                                 files " ")))))
12322           (setq ps (cdr ps)))))
12323     (if (and gnus-view-pseudos (not not-view))
12324         (while pslist
12325           (when (assq 'execute (car pslist))
12326             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12327                                   (eq gnus-view-pseudos 'not-confirm)))
12328           (setq pslist (cdr pslist)))
12329       (save-excursion
12330         (while pslist
12331           (setq after-article (or (cdr (assq 'article (car pslist)))
12332                                   (gnus-summary-article-number)))
12333           (gnus-summary-goto-subject after-article)
12334           (forward-line 1)
12335           (setq b (point))
12336           (insert "    " (file-name-nondirectory
12337                           (cdr (assq 'name (car pslist))))
12338                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12339           (setq e (point))
12340           (forward-line -1)             ; back to `b'
12341           (gnus-add-text-properties
12342            b (1- e) (list 'gnus-number gnus-reffed-article-number
12343                           gnus-mouse-face-prop gnus-mouse-face))
12344           (gnus-data-enter
12345            after-article gnus-reffed-article-number
12346            gnus-unread-mark b (car pslist) 0 (- e b))
12347           (setq gnus-newsgroup-unreads
12348                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12349                                          gnus-reffed-article-number))
12350           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12351           (setq pslist (cdr pslist)))))))
12352
12353 (defun gnus-pseudos< (p1 p2)
12354   (let ((c1 (cdr (assq 'action p1)))
12355         (c2 (cdr (assq 'action p2))))
12356     (and c1 c2 (string< c1 c2))))
12357
12358 (defun gnus-request-pseudo-article (props)
12359   (cond ((assq 'execute props)
12360          (gnus-execute-command (cdr (assq 'execute props)))))
12361   (let ((gnus-current-article (gnus-summary-article-number)))
12362     (gnus-run-hooks 'gnus-mark-article-hook)))
12363
12364 (defun gnus-execute-command (command &optional automatic)
12365   (save-excursion
12366     (gnus-article-setup-buffer)
12367     (set-buffer gnus-article-buffer)
12368     (setq buffer-read-only nil)
12369     (let ((command (if automatic command
12370                      (read-string "Command: " (cons command 0)))))
12371       (erase-buffer)
12372       (insert "$ " command "\n\n")
12373       (if gnus-view-pseudo-asynchronously
12374           (start-process "gnus-execute" (current-buffer) shell-file-name
12375                          shell-command-switch command)
12376         (call-process shell-file-name nil t nil
12377                       shell-command-switch command)))))
12378
12379 ;; Summary kill commands.
12380
12381 (defun gnus-summary-edit-global-kill (article)
12382   "Edit the \"global\" kill file."
12383   (interactive (list (gnus-summary-article-number)))
12384   (gnus-group-edit-global-kill article))
12385
12386 (defun gnus-summary-edit-local-kill ()
12387   "Edit a local kill file applied to the current newsgroup."
12388   (interactive)
12389   (setq gnus-current-headers (gnus-summary-article-header))
12390   (gnus-group-edit-local-kill
12391    (gnus-summary-article-number) gnus-newsgroup-name))
12392
12393 ;;; Header reading.
12394
12395 (defun gnus-read-header (id &optional header)
12396   "Read the headers of article ID and enter them into the Gnus system."
12397   (let ((group gnus-newsgroup-name)
12398         (gnus-override-method
12399          (or
12400           gnus-override-method
12401           (and (gnus-news-group-p gnus-newsgroup-name)
12402                (car (gnus-refer-article-methods)))))
12403         where)
12404     ;; First we check to see whether the header in question is already
12405     ;; fetched.
12406     (if (stringp id)
12407         ;; This is a Message-ID.
12408         (setq header (or header (gnus-id-to-header id)))
12409       ;; This is an article number.
12410       (setq header (or header (gnus-summary-article-header id))))
12411     (if (and header
12412              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12413         ;; We have found the header.
12414         header
12415       ;; We have to really fetch the header to this article.
12416       (with-current-buffer nntp-server-buffer
12417         (when (setq where (gnus-request-head id group))
12418           (nnheader-fold-continuation-lines)
12419           (goto-char (point-max))
12420           (insert ".\n")
12421           (goto-char (point-min))
12422           (insert "211 ")
12423           (princ (cond
12424                   ((numberp id) id)
12425                   ((cdr where) (cdr where))
12426                   (header (mail-header-number header))
12427                   (t gnus-reffed-article-number))
12428                  (current-buffer))
12429           (insert " Article retrieved.\n"))
12430         (if (or (not where)
12431                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12432             ()                          ; Malformed head.
12433           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12434             (when (and (stringp id)
12435                        (or
12436                         (not (string= (gnus-group-real-name group)
12437                                       (car where)))
12438                         (not (gnus-server-equal gnus-override-method
12439                                                 (gnus-group-method group)))))
12440               ;; If we fetched by Message-ID and the article came from
12441               ;; a different group (or server), we fudge some bogus
12442               ;; article numbers for this article.
12443               (mail-header-set-number header gnus-reffed-article-number))
12444             (with-current-buffer gnus-summary-buffer
12445               (decf gnus-reffed-article-number)
12446               (gnus-remove-header (mail-header-number header))
12447               (push header gnus-newsgroup-headers)
12448               (setq gnus-current-headers header)
12449               (push (mail-header-number header) gnus-newsgroup-limit)))
12450           header)))))
12451
12452 (defun gnus-remove-header (number)
12453   "Remove header NUMBER from `gnus-newsgroup-headers'."
12454   (if (and gnus-newsgroup-headers
12455            (= number (mail-header-number (car gnus-newsgroup-headers))))
12456       (pop gnus-newsgroup-headers)
12457     (let ((headers gnus-newsgroup-headers))
12458       (while (and (cdr headers)
12459                   (not (= number (mail-header-number (cadr headers)))))
12460         (pop headers))
12461       (when (cdr headers)
12462         (setcdr headers (cddr headers))))))
12463
12464 ;;;
12465 ;;; summary highlights
12466 ;;;
12467
12468 (defun gnus-highlight-selected-summary ()
12469   "Highlight selected article in summary buffer."
12470   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12471   (when gnus-summary-selected-face
12472     (save-excursion
12473       (let* ((beg (point-at-bol))
12474              (end (point-at-eol))
12475              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12476              (from (if (get-text-property beg gnus-mouse-face-prop)
12477                        beg
12478                      (or (next-single-property-change
12479                           beg gnus-mouse-face-prop nil end)
12480                          beg)))
12481              (to
12482               (if (= from end)
12483                   (- from 2)
12484                 (or (next-single-property-change
12485                      from gnus-mouse-face-prop nil end)
12486                     end))))
12487         ;; If no mouse-face prop on line we will have to = from = end,
12488         ;; so we highlight the entire line instead.
12489         (when (= (+ to 2) from)
12490           (setq from beg)
12491           (setq to end))
12492         (if gnus-newsgroup-selected-overlay
12493             ;; Move old overlay.
12494             (gnus-move-overlay
12495              gnus-newsgroup-selected-overlay from to (current-buffer))
12496           ;; Create new overlay.
12497           (gnus-overlay-put
12498            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12499            'face gnus-summary-selected-face))))))
12500
12501 (defvar gnus-summary-highlight-line-cached nil)
12502 (defvar gnus-summary-highlight-line-trigger nil)
12503
12504 (defun gnus-summary-highlight-line-0 ()
12505   (if (and (eq gnus-summary-highlight-line-trigger
12506                gnus-summary-highlight)
12507            gnus-summary-highlight-line-cached)
12508       gnus-summary-highlight-line-cached
12509     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12510           gnus-summary-highlight-line-cached
12511           (let* ((cond (list 'cond))
12512                  (c cond)
12513                  (list gnus-summary-highlight))
12514             (while list
12515               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12516                               nil))
12517               (setq c (cdr c)
12518                     list (cdr list)))
12519             (gnus-byte-compile (list 'lambda nil cond))))))
12520
12521 (defun gnus-summary-highlight-line ()
12522   "Highlight current line according to `gnus-summary-highlight'."
12523   (let* ((beg (point-at-bol))
12524          (article (or (gnus-summary-article-number) gnus-current-article))
12525          (score (or (cdr (assq article
12526                                gnus-newsgroup-scored))
12527                     gnus-summary-default-score 0))
12528          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12529          (inhibit-read-only t)
12530          (default gnus-summary-default-score)
12531          (default-high gnus-summary-default-high-score)
12532          (default-low gnus-summary-default-low-score)
12533          (uncached (and gnus-summary-use-undownloaded-faces
12534                         (memq article gnus-newsgroup-undownloaded)
12535                         (not (memq article gnus-newsgroup-cached)))))
12536     (let ((face (funcall (gnus-summary-highlight-line-0))))
12537       (unless (eq face (get-text-property beg 'face))
12538         (gnus-put-text-property-excluding-characters-with-faces
12539          beg (point-at-eol) 'face
12540          (setq face (if (boundp face) (symbol-value face) face)))
12541         (when gnus-summary-highlight-line-function
12542           (funcall gnus-summary-highlight-line-function article face))))))
12543
12544 (defun gnus-update-read-articles (group unread &optional compute)
12545   "Update the list of read articles in GROUP.
12546 UNREAD is a sorted list."
12547   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12548         (info (gnus-get-info group))
12549         (prev 1)
12550         read)
12551     (if (or (not info) (not active))
12552         ;; There is no info on this group if it was, in fact,
12553         ;; killed.  Gnus stores no information on killed groups, so
12554         ;; there's nothing to be done.
12555         ;; One could store the information somewhere temporarily,
12556         ;; perhaps...  Hmmm...
12557         ()
12558       ;; Remove any negative articles numbers.
12559       (while (and unread (< (car unread) 0))
12560         (setq unread (cdr unread)))
12561       ;; Remove any expired article numbers
12562       (while (and unread (< (car unread) (car active)))
12563         (setq unread (cdr unread)))
12564       ;; Compute the ranges of read articles by looking at the list of
12565       ;; unread articles.
12566       (while unread
12567         (when (/= (car unread) prev)
12568           (push (if (= prev (1- (car unread))) prev
12569                   (cons prev (1- (car unread))))
12570                 read))
12571         (setq prev (1+ (car unread)))
12572         (setq unread (cdr unread)))
12573       (when (<= prev (cdr active))
12574         (push (cons prev (cdr active)) read))
12575       (setq read (if (> (length read) 1) (nreverse read) read))
12576       (if compute
12577           read
12578         (save-excursion
12579           (let (setmarkundo)
12580             ;; Propagate the read marks to the backend.
12581             (when (and (or gnus-propagate-marks
12582                            (gnus-method-option-p
12583                             (gnus-find-method-for-group group)
12584                             'server-marks))
12585                        (gnus-check-backend-function 'request-set-mark group))
12586               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12587                     (add (gnus-remove-from-range read (gnus-info-read info))))
12588                 (when (or add del)
12589                   (unless (gnus-check-group group)
12590                     (error "Can't open server for %s" group))
12591                   (gnus-request-set-mark
12592                    group (delq nil (list (if add (list add 'add '(read)))
12593                                          (if del (list del 'del '(read))))))
12594                   (setq setmarkundo
12595                         `(gnus-request-set-mark
12596                           ,group
12597                           ',(delq nil (list
12598                                        (if del (list del 'add '(read)))
12599                                        (if add (list add 'del '(read))))))))))
12600             (set-buffer gnus-group-buffer)
12601             (gnus-undo-register
12602               `(progn
12603                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12604                  (gnus-info-set-read ',info ',(gnus-info-read info))
12605                  (gnus-get-unread-articles-in-group ',info
12606                                                     (gnus-active ,group))
12607                  (gnus-group-update-group ,group t)
12608                  ,setmarkundo))))
12609         ;; Enter this list into the group info.
12610         (gnus-info-set-read info read)
12611         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12612         (gnus-get-unread-articles-in-group info (gnus-active group))
12613         t))))
12614
12615 (defun gnus-offer-save-summaries ()
12616   "Offer to save all active summary buffers."
12617   (let (buffers)
12618     ;; Go through all buffers and find all summaries.
12619     (dolist (buffer (buffer-list))
12620       (when (and (setq buffer (buffer-name buffer))
12621                  (string-match "Summary" buffer)
12622                  (with-current-buffer buffer
12623                    ;; We check that this is, indeed, a summary buffer.
12624                    (and (eq major-mode 'gnus-summary-mode)
12625                         ;; Also make sure this isn't bogus.
12626                         gnus-newsgroup-prepared
12627                         ;; Also make sure that this isn't a
12628                         ;; dead summary buffer.
12629                         (not gnus-dead-summary-mode))))
12630         (push buffer buffers)))
12631     ;; Go through all these summary buffers and offer to save them.
12632     (when buffers
12633       (save-excursion
12634         (if (eq gnus-interactive-exit 'quiet)
12635             (dolist (buffer buffers)
12636               (switch-to-buffer buffer)
12637               (gnus-summary-exit))
12638           (map-y-or-n-p
12639            "Update summary buffer %s? "
12640            (lambda (buf)
12641              (switch-to-buffer buf)
12642              (gnus-summary-exit))
12643            buffers))))))
12644
12645 (defun gnus-summary-setup-default-charset ()
12646   "Setup newsgroup default charset."
12647   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12648       (setq gnus-newsgroup-charset nil)
12649     (let* ((ignored-charsets
12650             (or gnus-newsgroup-ephemeral-ignored-charsets
12651                 (append
12652                  (and gnus-newsgroup-name
12653                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12654                  gnus-newsgroup-ignored-charsets))))
12655       (setq gnus-newsgroup-charset
12656             (or gnus-newsgroup-ephemeral-charset
12657                 (and gnus-newsgroup-name
12658                      (gnus-parameter-charset gnus-newsgroup-name))
12659                 gnus-default-charset))
12660       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12661            ignored-charsets))))
12662
12663 ;;;
12664 ;;; Mime Commands
12665 ;;;
12666
12667 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12668   "Display the current article buffer fully MIME-buttonized.
12669 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12670 treated as multipart/mixed."
12671   (interactive "P")
12672   (require 'gnus-art)
12673   (let ((gnus-unbuttonized-mime-types nil)
12674         (gnus-mime-display-multipart-as-mixed show-all-parts))
12675     (gnus-summary-show-article)))
12676
12677 (defun gnus-summary-repair-multipart (article)
12678   "Add a Content-Type header to a multipart article without one."
12679   (interactive (list (gnus-summary-article-number)))
12680   (gnus-with-article article
12681     (message-narrow-to-head)
12682     (message-remove-header "Mime-Version")
12683     (goto-char (point-max))
12684     (insert "Mime-Version: 1.0\n")
12685     (widen)
12686     (when (search-forward "\n--" nil t)
12687       (let ((separator (buffer-substring (point) (point-at-eol))))
12688         (message-narrow-to-head)
12689         (message-remove-header "Content-Type")
12690         (goto-char (point-max))
12691         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12692                         separator))
12693         (widen))))
12694   (let (gnus-mark-article-hook)
12695     (gnus-summary-select-article t t nil article)))
12696
12697 (defun gnus-summary-toggle-display-buttonized ()
12698   "Toggle the buttonizing of the article buffer."
12699   (interactive)
12700   (require 'gnus-art)
12701   (if (setq gnus-inhibit-mime-unbuttonizing
12702             (not gnus-inhibit-mime-unbuttonizing))
12703       (let ((gnus-unbuttonized-mime-types nil))
12704         (gnus-summary-show-article))
12705     (gnus-summary-show-article)))
12706
12707 ;;;
12708 ;;; Generic summary marking commands
12709 ;;;
12710
12711 (defvar gnus-summary-marking-alist
12712   '((read gnus-del-mark "d")
12713     (unread gnus-unread-mark "u")
12714     (ticked gnus-ticked-mark "!")
12715     (dormant gnus-dormant-mark "?")
12716     (expirable gnus-expirable-mark "e"))
12717   "An alist of names/marks/keystrokes.")
12718
12719 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12720 (defvar gnus-summary-mark-map)
12721
12722 (defun gnus-summary-make-all-marking-commands ()
12723   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12724   (dolist (elem gnus-summary-marking-alist)
12725     (apply 'gnus-summary-make-marking-command elem)))
12726
12727 (defun gnus-summary-make-marking-command (name mark keystroke)
12728   (let ((map (make-sparse-keymap)))
12729     (define-key gnus-summary-generic-mark-map keystroke map)
12730     (dolist (lway `((next "next" next nil "n")
12731                     (next-unread "next unread" next t "N")
12732                     (prev "previous" prev nil "p")
12733                     (prev-unread "previous unread" prev t "P")
12734                     (nomove "" nil nil ,keystroke)))
12735       (let ((func (gnus-summary-make-marking-command-1
12736                    mark (car lway) lway name)))
12737         (setq func (eval func))
12738         (define-key map (nth 4 lway) func)))))
12739
12740 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12741   `(defun ,(intern
12742             (format "gnus-summary-put-mark-as-%s%s"
12743                     name (if (eq way 'nomove)
12744                              ""
12745                            (concat "-" (symbol-name way)))))
12746      (n)
12747      ,(format
12748        "Mark the current article as %s%s.
12749 If N, the prefix, then repeat N times.
12750 If N is negative, move in reverse order.
12751 The difference between N and the actual number of articles marked is
12752 returned."
12753        name (cadr lway))
12754      (interactive "p")
12755      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12756
12757 (defun gnus-summary-generic-mark (n mark move unread)
12758   "Mark N articles with MARK."
12759   (unless (eq major-mode 'gnus-summary-mode)
12760     (error "This command can only be used in the summary buffer"))
12761   (gnus-summary-show-thread)
12762   (let ((nummove
12763          (cond
12764           ((eq move 'next) 1)
12765           ((eq move 'prev) -1)
12766           (t 0))))
12767     (if (zerop nummove)
12768         (setq n 1)
12769       (when (< n 0)
12770         (setq n (abs n)
12771               nummove (* -1 nummove))))
12772     (while (and (> n 0)
12773                 (gnus-summary-mark-article nil mark)
12774                 (zerop (gnus-summary-next-subject nummove unread t)))
12775       (setq n (1- n)))
12776     (when (/= 0 n)
12777       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12778     (gnus-summary-recenter)
12779     (gnus-summary-position-point)
12780     (gnus-set-mode-line 'summary)
12781     n))
12782
12783 (defun gnus-summary-insert-articles (articles)
12784   (when (setq articles
12785               (gnus-sorted-difference articles
12786                                       (mapcar (lambda (h)
12787                                                 (mail-header-number h))
12788                                               gnus-newsgroup-headers)))
12789     (setq gnus-newsgroup-headers
12790           (gnus-merge 'list
12791                       gnus-newsgroup-headers
12792                       (gnus-fetch-headers articles)
12793                       'gnus-article-sort-by-number))
12794     (setq gnus-newsgroup-articles
12795           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12796     ;; Suppress duplicates?
12797     (when gnus-suppress-duplicates
12798       (gnus-dup-suppress-articles))
12799
12800     (if (and gnus-fetch-old-headers
12801              (eq gnus-headers-retrieved-by 'nov))
12802         ;; We might want to build some more threads first.
12803         (if (eq gnus-fetch-old-headers 'invisible)
12804             (gnus-build-all-threads)
12805           (gnus-build-old-threads))
12806       ;; Mark the inserted articles that are unread as unread.
12807       (setq gnus-newsgroup-unreads
12808             (gnus-sorted-nunion
12809              gnus-newsgroup-unreads
12810              (gnus-sorted-nintersection
12811               (gnus-list-of-unread-articles gnus-newsgroup-name)
12812               articles)))
12813       ;; Mark the inserted articles as selected so that the information
12814       ;; of the marks having been changed by a user may be updated when
12815       ;; exiting this group.  See `gnus-summary-update-info'.
12816       (dolist (art articles)
12817         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12818     ;; Let the Gnus agent mark articles as read.
12819     (when gnus-agent
12820       (gnus-agent-get-undownloaded-list))
12821     ;; Remove list identifiers from subject
12822     (gnus-summary-remove-list-identifiers)
12823     ;; First and last article in this newsgroup.
12824     (when gnus-newsgroup-headers
12825       (setq gnus-newsgroup-begin
12826             (mail-header-number (car gnus-newsgroup-headers))
12827             gnus-newsgroup-end
12828             (mail-header-number
12829              (gnus-last-element gnus-newsgroup-headers))))
12830     (when gnus-use-scoring
12831       (gnus-possibly-score-headers))))
12832
12833 (defun gnus-summary-insert-old-articles (&optional all)
12834   "Insert all old articles in this group.
12835 If ALL is non-nil, already read articles become readable.
12836 If ALL is a number, fetch this number of articles."
12837   (interactive "P")
12838   (prog1
12839       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12840             older len)
12841         (setq older
12842               ;; Some nntp servers lie about their active range.  When
12843               ;; this happens, the active range can be in the millions.
12844               ;; Use a compressed range to avoid creating a huge list.
12845               (gnus-range-difference (list gnus-newsgroup-active) old))
12846         (setq len (gnus-range-length older))
12847         (cond
12848          ((null older) nil)
12849          ((numberp all)
12850           (if (< all len)
12851               (let ((older-range (nreverse older)))
12852                 (setq older nil)
12853
12854                 (while (> all 0)
12855                   (let* ((r (pop older-range))
12856                          (min (if (numberp r) r (car r)))
12857                          (max (if (numberp r) r (cdr r))))
12858                     (while (and (<= min max)
12859                                 (> all 0))
12860                       (push max older)
12861                       (setq all (1- all)
12862                             max (1- max))))))
12863             (setq older (gnus-uncompress-range older))))
12864          (all
12865           (setq older (gnus-uncompress-range older)))
12866          (t
12867           (when (and (numberp gnus-large-newsgroup)
12868                    (> len gnus-large-newsgroup))
12869               (let* ((cursor-in-echo-area nil)
12870                      (initial (gnus-parameter-large-newsgroup-initial
12871                                gnus-newsgroup-name))
12872                      (input
12873                       (read-string
12874                        (format
12875                         "How many articles from %s (%s %d): "
12876                         (gnus-group-decoded-name gnus-newsgroup-name)
12877                         (if initial "max" "default")
12878                         len)
12879                        nil nil
12880                        (if initial
12881                            (cons (number-to-string initial)
12882                                  0)))))
12883                 (unless (string-match "^[ \t]*$" input)
12884                   (setq all (string-to-number input))
12885                   (if (< all len)
12886                       (let ((older-range (nreverse older)))
12887                         (setq older nil)
12888
12889                         (while (> all 0)
12890                           (let* ((r (pop older-range))
12891                                  (min (if (numberp r) r (car r)))
12892                                  (max (if (numberp r) r (cdr r))))
12893                             (while (and (<= min max)
12894                                         (> all 0))
12895                               (push max older)
12896                               (setq all (1- all)
12897                                     max (1- max))))))))))
12898           (setq older (gnus-uncompress-range older))))
12899         (if (not older)
12900             (message "No old news.")
12901           (gnus-summary-insert-articles older)
12902           (gnus-summary-limit (gnus-sorted-nunion old older))))
12903     (gnus-summary-position-point)))
12904
12905 (defun gnus-summary-insert-new-articles ()
12906   "Insert all new articles in this group."
12907   (interactive)
12908   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12909         (old-high gnus-newsgroup-highest)
12910         (nnmail-fetched-sources (list t))
12911         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12912         i new)
12913     (unless new-active
12914       (error "Couldn't fetch new data"))
12915     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12916     (setq i (cdr gnus-newsgroup-active)
12917           gnus-newsgroup-highest i)
12918     (while (> i old-high)
12919       (push i new)
12920       (decf i))
12921     (if (not new)
12922         (message "No gnus is bad news")
12923       (gnus-summary-insert-articles new)
12924       (setq gnus-newsgroup-unreads
12925             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12926       (gnus-summary-limit (gnus-sorted-nunion old new))))
12927   (gnus-summary-position-point))
12928
12929 ;;; Bookmark support for Gnus.
12930 (declare-function bookmark-make-record-default
12931                   "bookmark" (&optional no-file no-context posn))
12932 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12933 (declare-function bookmark-default-handler "bookmark" (bmk))
12934 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12935 (defvar bookmark-yank-point)
12936 (defvar bookmark-current-buffer)
12937
12938 (defun gnus-summary-bookmark-make-record ()
12939   "Make a bookmark entry for a Gnus summary buffer."
12940   (let (pos buf)
12941     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12942       (save-restriction              ; FIXME is it necessary to widen?
12943         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12944       (setq buf "art") ; We are recording bookmark from article buffer.
12945       (setq bookmark-yank-point (point))
12946       (setq bookmark-current-buffer (current-buffer))
12947       (gnus-article-show-summary))      ; Go back in summary buffer.
12948     ;; We are now recording bookmark from summary buffer.
12949     (unless buf (setq buf "sum"))
12950     (let* ((subject (elt (gnus-summary-article-header) 1))
12951            (grp     (car gnus-article-current))
12952            (art     (cdr gnus-article-current))
12953            (head    (gnus-summary-article-header art))
12954            (id      (mail-header-id head)))
12955       `(,subject
12956         ,@(condition-case nil
12957               (bookmark-make-record-default 'no-file 'no-context pos)
12958             (wrong-number-of-arguments
12959              (bookmark-make-record-default 'point-only)))
12960         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12961         (group . ,grp) (article . ,art)
12962         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12963
12964 ;;;###autoload
12965 (defun gnus-summary-bookmark-jump (bookmark)
12966   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12967 BOOKMARK is a bookmark name or a bookmark record."
12968   (let ((group    (bookmark-prop-get bookmark 'group))
12969         (article  (bookmark-prop-get bookmark 'article))
12970         (id       (bookmark-prop-get bookmark 'message-id))
12971         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12972     (gnus-fetch-group group (list article))
12973     (gnus-summary-insert-cached-articles)
12974     (gnus-summary-goto-article id nil 'force)
12975     ;; FIXME we have to wait article buffer is ready (only large buffer)
12976     ;; Is there a better solution to know that?
12977     ;; If we don't wait `bookmark-default-handler' will have no chance
12978     ;; to set position. However there is no error, just wrong pos.
12979     (sit-for 1)
12980     (when (string= buf "Gnus-art")
12981       (other-window 1))
12982     (bookmark-default-handler
12983      `(""
12984        (buffer . ,(current-buffer))
12985        . ,(bookmark-get-bookmark-record bookmark)))))
12986
12987 (gnus-summary-make-all-marking-commands)
12988
12989 (gnus-ems-redefine)
12990
12991 (provide 'gnus-sum)
12992
12993 (run-hooks 'gnus-sum-load-hook)
12994
12995 ;; Local Variables:
12996 ;; coding: iso-8859-1
12997 ;; End:
12998
12999 ;;; gnus-sum.el ends here