Fix nnir/gnus-sum mutually recursive requirements
[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-articles nil
1533   "List of articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-scored nil
1536   "List of scored articles in the current newsgroup.")
1537
1538 (defvar gnus-newsgroup-headers nil
1539   "List of article headers in the current newsgroup.")
1540
1541 (defvar gnus-newsgroup-threads nil)
1542
1543 (defvar gnus-newsgroup-prepared nil
1544   "Whether the current group has been prepared properly.")
1545
1546 (defvar gnus-newsgroup-ancient nil
1547   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1548
1549 (defvar gnus-newsgroup-sparse nil)
1550
1551 (defvar gnus-current-article nil)
1552 (defvar gnus-article-current nil)
1553 (defvar gnus-current-headers nil)
1554 (defvar gnus-have-all-headers nil)
1555 (defvar gnus-last-article nil)
1556 (defvar gnus-newsgroup-history nil)
1557 (defvar gnus-newsgroup-charset nil)
1558 (defvar gnus-newsgroup-ephemeral-charset nil)
1559 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1560
1561 (defvar gnus-article-before-search nil)
1562
1563 (defvar gnus-summary-local-variables
1564   '(gnus-newsgroup-name
1565
1566     ;; Marks lists
1567     gnus-newsgroup-unreads
1568     gnus-newsgroup-unselected
1569     gnus-newsgroup-marked
1570     gnus-newsgroup-spam-marked
1571     gnus-newsgroup-reads
1572     gnus-newsgroup-saved
1573     gnus-newsgroup-replied
1574     gnus-newsgroup-forwarded
1575     gnus-newsgroup-expirable
1576     gnus-newsgroup-killed
1577     gnus-newsgroup-unseen
1578     gnus-newsgroup-seen
1579     gnus-newsgroup-cached
1580     gnus-newsgroup-downloadable
1581     gnus-newsgroup-undownloaded
1582     gnus-newsgroup-unsendable
1583
1584     gnus-newsgroup-begin gnus-newsgroup-end
1585     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1586     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1587     gnus-newsgroup-last-directory
1588     gnus-newsgroup-auto-expire
1589     gnus-newsgroup-processable
1590     gnus-newsgroup-unfetched
1591     gnus-newsgroup-articles
1592     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1593     gnus-newsgroup-headers gnus-newsgroup-threads
1594     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1595     gnus-current-article gnus-current-headers gnus-have-all-headers
1596     gnus-last-article gnus-article-internal-prepare-hook
1597     (gnus-summary-article-delete-hook . global)
1598     (gnus-summary-article-move-hook . global)
1599     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1600     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1601     gnus-thread-expunge-below
1602     gnus-score-alist gnus-current-score-file
1603     (gnus-summary-expunge-below . global)
1604     (gnus-summary-mark-below . global)
1605     (gnus-orphan-score . global)
1606     gnus-newsgroup-active gnus-scores-exclude-files
1607     gnus-newsgroup-highest
1608     gnus-newsgroup-history gnus-newsgroup-ancient
1609     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1610     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1611     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1612     (gnus-newsgroup-expunged-tally . 0)
1613     gnus-cache-removable-articles
1614     gnus-newsgroup-data gnus-newsgroup-data-reverse
1615     gnus-newsgroup-limit gnus-newsgroup-limits
1616     gnus-newsgroup-charset gnus-newsgroup-display
1617     gnus-summary-use-undownloaded-faces)
1618   "Variables that are buffer-local to the summary buffers.")
1619
1620 (defvar gnus-newsgroup-variables nil
1621   "A list of variables that have separate values in different newsgroups.
1622 A list of newsgroup (summary buffer) local variables, or cons of
1623 variables and their default expressions to be evalled (when the default
1624 values are not nil), that should be made global while the summary buffer
1625 is active.
1626
1627 Note: The default expressions will be evaluated (using function `eval')
1628 before assignment to the local variable rather than just assigned to it.
1629 If the default expression is the symbol `global', that symbol will not
1630 be evaluated but the global value of the local variable will be used
1631 instead.
1632
1633 These variables can be used to set variables in the group parameters
1634 while still allowing them to affect operations done in other buffers.
1635 For example:
1636
1637 \(setq gnus-newsgroup-variables
1638      '(message-use-followup-to
1639        (gnus-visible-headers .
1640          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1641 ")
1642
1643 (eval-when-compile
1644   ;; Bind features so that require will believe that gnus-sum has
1645   ;; already been loaded (avoids infinite recursion)
1646   (let ((features (cons 'gnus-sum features)))
1647     (require 'gnus-art)))
1648
1649 ;; MIME stuff.
1650
1651 (defvar gnus-decode-encoded-word-methods
1652   '(mail-decode-encoded-word-string)
1653   "List of methods used to decode encoded words.
1654
1655 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1656 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1657 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1658 whose names match REGEXP.
1659
1660 For example:
1661 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1662  mail-decode-encoded-word-string
1663  (\"chinese\" . rfc1843-decode-string))")
1664
1665 (defvar gnus-decode-encoded-word-methods-cache nil)
1666
1667 (defun gnus-multi-decode-encoded-word-string (string)
1668   "Apply the functions from `gnus-encoded-word-methods' that match."
1669   (unless (and gnus-decode-encoded-word-methods-cache
1670                (eq gnus-newsgroup-name
1671                    (car gnus-decode-encoded-word-methods-cache)))
1672     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1673     (dolist (method gnus-decode-encoded-word-methods)
1674       (if (symbolp method)
1675           (nconc gnus-decode-encoded-word-methods-cache (list method))
1676         (if (and gnus-newsgroup-name
1677                  (string-match (car method) gnus-newsgroup-name))
1678             (nconc gnus-decode-encoded-word-methods-cache
1679                    (list (cdr method)))))))
1680   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1681     (setq string (funcall method string))))
1682
1683 ;; Subject simplification.
1684
1685 (defun gnus-simplify-whitespace (str)
1686   "Remove excessive whitespace from STR."
1687   ;; Multiple spaces.
1688   (while (string-match "[ \t][ \t]+" str)
1689     (setq str (concat (substring str 0 (match-beginning 0))
1690                         " "
1691                         (substring str (match-end 0)))))
1692   ;; Leading spaces.
1693   (when (string-match "^[ \t]+" str)
1694     (setq str (substring str (match-end 0))))
1695   ;; Trailing spaces.
1696   (when (string-match "[ \t]+$" str)
1697     (setq str (substring str 0 (match-beginning 0))))
1698   str)
1699
1700 (defun gnus-simplify-all-whitespace (str)
1701   "Remove all whitespace from STR."
1702   (while (string-match "[ \t\n]+" str)
1703     (setq str (replace-match "" nil nil str)))
1704   str)
1705
1706 (defsubst gnus-simplify-subject-re (subject)
1707   "Remove \"Re:\" from subject lines."
1708   (if (string-match message-subject-re-regexp subject)
1709       (substring subject (match-end 0))
1710     subject))
1711
1712 (defun gnus-simplify-subject (subject &optional re-only)
1713   "Remove `Re:' and words in parentheses.
1714 If RE-ONLY is non-nil, strip leading `Re:'s only."
1715   (let ((case-fold-search t))           ;Ignore case.
1716     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1717     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1718       (setq subject (substring subject (match-end 0))))
1719     ;; Remove uninteresting prefixes.
1720     (when (and (not re-only)
1721                gnus-simplify-ignored-prefixes
1722                (string-match gnus-simplify-ignored-prefixes subject))
1723       (setq subject (substring subject (match-end 0))))
1724     ;; Remove words in parentheses from end.
1725     (unless re-only
1726       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1727         (setq subject (substring subject 0 (match-beginning 0)))))
1728     ;; Return subject string.
1729     subject))
1730
1731 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1732 ;; all whitespace.
1733 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1734   (goto-char (point-min))
1735   (while (re-search-forward regexp nil t)
1736     (replace-match (or newtext ""))))
1737
1738 (defun gnus-simplify-buffer-fuzzy (regexp)
1739   "Simplify string in the buffer fuzzily.
1740 The string in the accessible portion of the current buffer is simplified.
1741 It is assumed to be a single-line subject.
1742 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1743 matter is removed.  Additional things can be deleted by setting
1744 `gnus-simplify-subject-fuzzy-regexp'."
1745   (let ((case-fold-search t)
1746         (modified-tick))
1747     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1748
1749     (while (not (eq modified-tick (buffer-modified-tick)))
1750       (setq modified-tick (buffer-modified-tick))
1751       (cond
1752        ((listp regexp)
1753         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1754        (regexp
1755         (gnus-simplify-buffer-fuzzy-step regexp)))
1756       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1757       (gnus-simplify-buffer-fuzzy-step
1758        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1759       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1760
1761     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1762     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1763     (gnus-simplify-buffer-fuzzy-step " $")
1764     (gnus-simplify-buffer-fuzzy-step "^ +")))
1765
1766 (defun gnus-simplify-subject-fuzzy (subject)
1767   "Simplify a subject string fuzzily.
1768 See `gnus-simplify-buffer-fuzzy' for details."
1769   (save-excursion
1770     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1771       (gnus-set-work-buffer)
1772       (let ((case-fold-search t))
1773         ;; Remove uninteresting prefixes.
1774         (when (and gnus-simplify-ignored-prefixes
1775                    (string-match gnus-simplify-ignored-prefixes subject))
1776           (setq subject (substring subject (match-end 0))))
1777         (insert subject)
1778         (inline (gnus-simplify-buffer-fuzzy regexp))
1779         (buffer-string)))))
1780
1781 (defsubst gnus-simplify-subject-fully (subject)
1782   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1783   (cond
1784    (gnus-simplify-subject-functions
1785     (gnus-map-function gnus-simplify-subject-functions subject))
1786    ((null gnus-summary-gather-subject-limit)
1787     (gnus-simplify-subject-re subject))
1788    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1789     (gnus-simplify-subject-fuzzy subject))
1790    ((numberp gnus-summary-gather-subject-limit)
1791     (truncate-string-to-width (gnus-simplify-subject-re subject)
1792                               gnus-summary-gather-subject-limit))
1793    (t
1794     subject)))
1795
1796 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1797   "Check whether two subjects are equal.
1798 If optional argument SIMPLE-FIRST is t, first argument is already
1799 simplified."
1800   (cond
1801    ((null simple-first)
1802     (equal (gnus-simplify-subject-fully s1)
1803            (gnus-simplify-subject-fully s2)))
1804    (t
1805     (equal s1
1806            (gnus-simplify-subject-fully s2)))))
1807
1808 (defun gnus-summary-bubble-group ()
1809   "Increase the score of the current group.
1810 This is a handy function to add to `gnus-summary-exit-hook' to
1811 increase the score of each group you read."
1812   (gnus-group-add-score gnus-newsgroup-name))
1813
1814 \f
1815 ;;;
1816 ;;; Gnus summary mode
1817 ;;;
1818
1819 (put 'gnus-summary-mode 'mode-class 'special)
1820
1821 (defvar gnus-article-commands-menu)
1822
1823 ;; Non-orthogonal keys
1824
1825 (gnus-define-keys gnus-summary-mode-map
1826   " " gnus-summary-next-page
1827   "\177" gnus-summary-prev-page
1828   [delete] gnus-summary-prev-page
1829   [backspace] gnus-summary-prev-page
1830   "\r" gnus-summary-scroll-up
1831   "\M-\r" gnus-summary-scroll-down
1832   "n" gnus-summary-next-unread-article
1833   "p" gnus-summary-prev-unread-article
1834   "N" gnus-summary-next-article
1835   "P" gnus-summary-prev-article
1836   "\M-\C-n" gnus-summary-next-same-subject
1837   "\M-\C-p" gnus-summary-prev-same-subject
1838   "\M-n" gnus-summary-next-unread-subject
1839   "\M-p" gnus-summary-prev-unread-subject
1840   "." gnus-summary-first-unread-article
1841   "," gnus-summary-best-unread-article
1842   "\M-s" gnus-summary-search-article-forward
1843   "\M-r" gnus-summary-search-article-backward
1844   "\M-S" gnus-summary-repeat-search-article-forward
1845   "\M-R" gnus-summary-repeat-search-article-backward
1846   "<" gnus-summary-beginning-of-article
1847   ">" gnus-summary-end-of-article
1848   "j" gnus-summary-goto-article
1849   "^" gnus-summary-refer-parent-article
1850   "\M-^" gnus-summary-refer-article
1851   "u" gnus-summary-tick-article-forward
1852   "!" gnus-summary-tick-article-forward
1853   "U" gnus-summary-tick-article-backward
1854   "d" gnus-summary-mark-as-read-forward
1855   "D" gnus-summary-mark-as-read-backward
1856   "E" gnus-summary-mark-as-expirable
1857   "\M-u" gnus-summary-clear-mark-forward
1858   "\M-U" gnus-summary-clear-mark-backward
1859   "k" gnus-summary-kill-same-subject-and-select
1860   "\C-k" gnus-summary-kill-same-subject
1861   "\M-\C-k" gnus-summary-kill-thread
1862   "\M-\C-l" gnus-summary-lower-thread
1863   "e" gnus-summary-edit-article
1864   "#" gnus-summary-mark-as-processable
1865   "\M-#" gnus-summary-unmark-as-processable
1866   "\M-\C-t" gnus-summary-toggle-threads
1867   "\M-\C-s" gnus-summary-show-thread
1868   "\M-\C-h" gnus-summary-hide-thread
1869   "\M-\C-f" gnus-summary-next-thread
1870   "\M-\C-b" gnus-summary-prev-thread
1871   [(meta down)] gnus-summary-next-thread
1872   [(meta up)] gnus-summary-prev-thread
1873   "\M-\C-u" gnus-summary-up-thread
1874   "\M-\C-d" gnus-summary-down-thread
1875   "&" gnus-summary-execute-command
1876   "c" gnus-summary-catchup-and-exit
1877   "\C-w" gnus-summary-mark-region-as-read
1878   "\C-t" gnus-summary-toggle-truncation
1879   "?" gnus-summary-mark-as-dormant
1880   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1881   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1882   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1883   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1884   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1885   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1886   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1887   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1888   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1889   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1890   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1891   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1892   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1893   "=" gnus-summary-expand-window
1894   "\C-x\C-s" gnus-summary-reselect-current-group
1895   "\M-g" gnus-summary-rescan-group
1896   "\C-c\C-r" gnus-summary-caesar-message
1897   "f" gnus-summary-followup
1898   "F" gnus-summary-followup-with-original
1899   "C" gnus-summary-cancel-article
1900   "r" gnus-summary-reply
1901   "R" gnus-summary-reply-with-original
1902   "\C-c\C-f" gnus-summary-mail-forward
1903   "o" gnus-summary-save-article
1904   "\C-o" gnus-summary-save-article-mail
1905   "|" gnus-summary-pipe-output
1906   "\M-k" gnus-summary-edit-local-kill
1907   "\M-K" gnus-summary-edit-global-kill
1908   ;; "V" gnus-version
1909   "\C-c\C-d" gnus-summary-describe-group
1910   "q" gnus-summary-exit
1911   "Q" gnus-summary-exit-no-update
1912   "\C-c\C-i" gnus-info-find-node
1913   gnus-mouse-2 gnus-mouse-pick-article
1914   [follow-link] mouse-face
1915   "m" gnus-summary-mail-other-window
1916   "a" gnus-summary-post-news
1917   "x" gnus-summary-limit-to-unread
1918   "s" gnus-summary-isearch-article
1919   [tab] gnus-summary-widget-forward
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-summary-show-article
1922   "l" gnus-summary-goto-last-article
1923   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1924   "\C-d" gnus-summary-enter-digest-group
1925   "\M-\C-d" gnus-summary-read-document
1926   "\M-\C-e" gnus-summary-edit-parameters
1927   "\M-\C-a" gnus-summary-customize-parameters
1928   "\C-c\C-b" gnus-bug
1929   "*" gnus-cache-enter-article
1930   "\M-*" gnus-cache-remove-article
1931   "\M-&" gnus-summary-universal-argument
1932   "\C-l" gnus-recenter
1933   "I" gnus-summary-increase-score
1934   "L" gnus-summary-lower-score
1935   "\M-i" gnus-symbolic-argument
1936   "h" gnus-summary-select-article-buffer
1937
1938   "b" gnus-article-view-part
1939   "\M-t" gnus-summary-toggle-display-buttonized
1940
1941   "V" gnus-summary-score-map
1942   "X" gnus-uu-extract-map
1943   "S" gnus-summary-send-map)
1944
1945 ;; Sort of orthogonal keymap
1946 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1947   "t" gnus-summary-tick-article-forward
1948   "!" gnus-summary-tick-article-forward
1949   "d" gnus-summary-mark-as-read-forward
1950   "r" gnus-summary-mark-as-read-forward
1951   "c" gnus-summary-clear-mark-forward
1952   " " gnus-summary-clear-mark-forward
1953   "e" gnus-summary-mark-as-expirable
1954   "x" gnus-summary-mark-as-expirable
1955   "?" gnus-summary-mark-as-dormant
1956   "b" gnus-summary-set-bookmark
1957   "B" gnus-summary-remove-bookmark
1958   "#" gnus-summary-mark-as-processable
1959   "\M-#" gnus-summary-unmark-as-processable
1960   "S" gnus-summary-limit-include-expunged
1961   "C" gnus-summary-catchup
1962   "H" gnus-summary-catchup-to-here
1963   "h" gnus-summary-catchup-from-here
1964   "\C-c" gnus-summary-catchup-all
1965   "k" gnus-summary-kill-same-subject-and-select
1966   "K" gnus-summary-kill-same-subject
1967   "P" gnus-uu-mark-map)
1968
1969 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1970   "c" gnus-summary-clear-above
1971   "u" gnus-summary-tick-above
1972   "m" gnus-summary-mark-above
1973   "k" gnus-summary-kill-below)
1974
1975 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1976   "/" gnus-summary-limit-to-subject
1977   "n" gnus-summary-limit-to-articles
1978   "b" gnus-summary-limit-to-bodies
1979   "h" gnus-summary-limit-to-headers
1980   "w" gnus-summary-pop-limit
1981   "s" gnus-summary-limit-to-subject
1982   "a" gnus-summary-limit-to-author
1983   "u" gnus-summary-limit-to-unread
1984   "m" gnus-summary-limit-to-marks
1985   "M" gnus-summary-limit-exclude-marks
1986   "v" gnus-summary-limit-to-score
1987   "*" gnus-summary-limit-include-cached
1988   "D" gnus-summary-limit-include-dormant
1989   "T" gnus-summary-limit-include-thread
1990   "d" gnus-summary-limit-exclude-dormant
1991   "t" gnus-summary-limit-to-age
1992   "." gnus-summary-limit-to-unseen
1993   "x" gnus-summary-limit-to-extra
1994   "p" gnus-summary-limit-to-display-predicate
1995   "E" gnus-summary-limit-include-expunged
1996   "c" gnus-summary-limit-exclude-childless-dormant
1997   "C" gnus-summary-limit-mark-excluded-as-read
1998   "o" gnus-summary-insert-old-articles
1999   "N" gnus-summary-insert-new-articles
2000   "S" gnus-summary-limit-to-singletons
2001   "r" gnus-summary-limit-to-replied
2002   "R" gnus-summary-limit-to-recipient
2003   "A" gnus-summary-limit-to-address)
2004
2005 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2006   "n" gnus-summary-next-unread-article
2007   "p" gnus-summary-prev-unread-article
2008   "N" gnus-summary-next-article
2009   "P" gnus-summary-prev-article
2010   "\C-n" gnus-summary-next-same-subject
2011   "\C-p" gnus-summary-prev-same-subject
2012   "\M-n" gnus-summary-next-unread-subject
2013   "\M-p" gnus-summary-prev-unread-subject
2014   "f" gnus-summary-first-unread-article
2015   "b" gnus-summary-best-unread-article
2016   "j" gnus-summary-goto-article
2017   "g" gnus-summary-goto-subject
2018   "l" gnus-summary-goto-last-article
2019   "o" gnus-summary-pop-article)
2020
2021 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2022   "k" gnus-summary-kill-thread
2023   "E" gnus-summary-expire-thread
2024   "l" gnus-summary-lower-thread
2025   "i" gnus-summary-raise-thread
2026   "T" gnus-summary-toggle-threads
2027   "t" gnus-summary-rethread-current
2028   "^" gnus-summary-reparent-thread
2029   "\M-^" gnus-summary-reparent-children
2030   "s" gnus-summary-show-thread
2031   "S" gnus-summary-show-all-threads
2032   "h" gnus-summary-hide-thread
2033   "H" gnus-summary-hide-all-threads
2034   "n" gnus-summary-next-thread
2035   "p" gnus-summary-prev-thread
2036   "u" gnus-summary-up-thread
2037   "o" gnus-summary-top-thread
2038   "d" gnus-summary-down-thread
2039   "#" gnus-uu-mark-thread
2040   "\M-#" gnus-uu-unmark-thread)
2041
2042 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2043   "g" gnus-summary-prepare
2044   "c" gnus-summary-insert-cached-articles
2045   "d" gnus-summary-insert-dormant-articles
2046   "t" gnus-summary-insert-ticked-articles)
2047
2048 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2049   "c" gnus-summary-catchup-and-exit
2050   "C" gnus-summary-catchup-all-and-exit
2051   "E" gnus-summary-exit-no-update
2052   "Q" gnus-summary-exit
2053   "Z" gnus-summary-exit
2054   "n" gnus-summary-catchup-and-goto-next-group
2055   "p" gnus-summary-catchup-and-goto-prev-group
2056   "R" gnus-summary-reselect-current-group
2057   "G" gnus-summary-rescan-group
2058   "N" gnus-summary-next-group
2059   "s" gnus-summary-save-newsrc
2060   "P" gnus-summary-prev-group)
2061
2062 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2063   " " gnus-summary-next-page
2064   "n" gnus-summary-next-page
2065   "\177" gnus-summary-prev-page
2066   [delete] gnus-summary-prev-page
2067   "p" gnus-summary-prev-page
2068   "\r" gnus-summary-scroll-up
2069   "\M-\r" gnus-summary-scroll-down
2070   "<" gnus-summary-beginning-of-article
2071   ">" gnus-summary-end-of-article
2072   "b" gnus-summary-beginning-of-article
2073   "e" gnus-summary-end-of-article
2074   "^" gnus-summary-refer-parent-article
2075   "r" gnus-summary-refer-parent-article
2076   "C" gnus-summary-show-complete-article
2077   "D" gnus-summary-enter-digest-group
2078   "R" gnus-summary-refer-references
2079   "T" gnus-summary-refer-thread
2080   "W" gnus-warp-to-article
2081   "g" gnus-summary-show-article
2082   "s" gnus-summary-isearch-article
2083   [tab] gnus-summary-widget-forward
2084   "P" gnus-summary-print-article
2085   "S" gnus-sticky-article
2086   "M" gnus-mailing-list-insinuate
2087   "t" gnus-article-babel)
2088
2089 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2090   "b" gnus-article-add-buttons
2091   "B" gnus-article-add-buttons-to-head
2092   "o" gnus-article-treat-overstrike
2093   "e" gnus-article-emphasize
2094   "w" gnus-article-fill-cited-article
2095   "Q" gnus-article-fill-long-lines
2096   "L" gnus-article-toggle-truncate-lines
2097   "C" gnus-article-capitalize-sentences
2098   "c" gnus-article-remove-cr
2099   "q" gnus-article-de-quoted-unreadable
2100   "6" gnus-article-de-base64-unreadable
2101   "Z" gnus-article-decode-HZ
2102   "A" gnus-article-treat-ansi-sequences
2103   "h" gnus-article-wash-html
2104   "u" gnus-article-unsplit-urls
2105   "s" gnus-summary-force-verify-and-decrypt
2106   "f" gnus-article-display-x-face
2107   "l" gnus-summary-stop-page-breaking
2108   "r" gnus-summary-caesar-message
2109   "m" gnus-summary-morse-message
2110   "t" gnus-summary-toggle-header
2111   "g" gnus-treat-smiley
2112   "v" gnus-summary-verbose-headers
2113   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2114   "p" gnus-article-verify-x-pgp-sig
2115   "d" gnus-article-treat-dumbquotes
2116   "U" gnus-article-treat-non-ascii
2117   "i" gnus-summary-idna-message)
2118
2119 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2120   ;; mnemonic: deuglif*Y*
2121   "u" gnus-article-outlook-unwrap-lines
2122   "a" gnus-article-outlook-repair-attribution
2123   "c" gnus-article-outlook-rearrange-citation
2124   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2125
2126 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2127   "a" gnus-article-hide
2128   "h" gnus-article-hide-headers
2129   "b" gnus-article-hide-boring-headers
2130   "s" gnus-article-hide-signature
2131   "c" gnus-article-hide-citation
2132   "C" gnus-article-hide-citation-in-followups
2133   "l" gnus-article-hide-list-identifiers
2134   "B" gnus-article-strip-banner
2135   "P" gnus-article-hide-pem
2136   "\C-c" gnus-article-hide-citation-maybe)
2137
2138 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2139   "a" gnus-article-highlight
2140   "h" gnus-article-highlight-headers
2141   "c" gnus-article-highlight-citation
2142   "s" gnus-article-highlight-signature)
2143
2144 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2145   "f" gnus-article-treat-fold-headers
2146   "u" gnus-article-treat-unfold-headers
2147   "n" gnus-article-treat-fold-newsgroups)
2148
2149 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2150   "x" gnus-article-display-x-face
2151   "d" gnus-article-display-face
2152   "s" gnus-treat-smiley
2153   "D" gnus-article-remove-images
2154   "W" gnus-article-show-images
2155   "f" gnus-treat-from-picon
2156   "m" gnus-treat-mail-picon
2157   "n" gnus-treat-newsgroups-picon
2158   "g" gnus-treat-from-gravatar
2159   "h" gnus-treat-mail-gravatar)
2160
2161 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2162   "w" gnus-article-decode-mime-words
2163   "c" gnus-article-decode-charset
2164   "v" gnus-mime-view-all-parts
2165   "b" gnus-article-view-part)
2166
2167 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2168   "z" gnus-article-date-ut
2169   "u" gnus-article-date-ut
2170   "l" gnus-article-date-local
2171   "p" gnus-article-date-english
2172   "e" gnus-article-date-lapsed
2173   "o" gnus-article-date-original
2174   "i" gnus-article-date-iso8601
2175   "s" gnus-article-date-user)
2176
2177 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2178   "t" gnus-article-remove-trailing-blank-lines
2179   "l" gnus-article-strip-leading-blank-lines
2180   "m" gnus-article-strip-multiple-blank-lines
2181   "a" gnus-article-strip-blank-lines
2182   "A" gnus-article-strip-all-blank-lines
2183   "s" gnus-article-strip-leading-space
2184   "e" gnus-article-strip-trailing-space
2185   "w" gnus-article-remove-leading-whitespace)
2186
2187 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2188   "v" gnus-version
2189   "d" gnus-summary-describe-group
2190   "h" gnus-summary-describe-briefly
2191   "i" gnus-info-find-node)
2192
2193 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2194   "e" gnus-summary-expire-articles
2195   "\M-\C-e" gnus-summary-expire-articles-now
2196   "\177" gnus-summary-delete-article
2197   [delete] gnus-summary-delete-article
2198   [backspace] gnus-summary-delete-article
2199   "m" gnus-summary-move-article
2200   "r" gnus-summary-respool-article
2201   "w" gnus-summary-edit-article
2202   "c" gnus-summary-copy-article
2203   "B" gnus-summary-crosspost-article
2204   "q" gnus-summary-respool-query
2205   "t" gnus-summary-respool-trace
2206   "i" gnus-summary-import-article
2207   "I" gnus-summary-create-article
2208   "p" gnus-summary-article-posted-p)
2209
2210 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2211   "o" gnus-summary-save-article
2212   "m" gnus-summary-save-article-mail
2213   "F" gnus-summary-write-article-file
2214   "r" gnus-summary-save-article-rmail
2215   "f" gnus-summary-save-article-file
2216   "b" gnus-summary-save-article-body-file
2217   "B" gnus-summary-write-article-body-file
2218   "h" gnus-summary-save-article-folder
2219   "v" gnus-summary-save-article-vm
2220   "p" gnus-summary-pipe-output
2221   "P" gnus-summary-muttprint)
2222
2223 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2224   "b" gnus-summary-display-buttonized
2225   "m" gnus-summary-repair-multipart
2226   "v" gnus-article-view-part
2227   "o" gnus-article-save-part
2228   "O" gnus-article-save-part-and-strip
2229   "r" gnus-article-replace-part
2230   "d" gnus-article-delete-part
2231   "t" gnus-article-view-part-as-type
2232   "j" gnus-article-jump-to-part
2233   "c" gnus-article-copy-part
2234   "C" gnus-article-view-part-as-charset
2235   "e" gnus-article-view-part-externally
2236   "H" gnus-article-browse-html-article
2237   "E" gnus-article-encrypt-body
2238   "i" gnus-article-inline-part
2239   "|" gnus-article-pipe-part)
2240
2241 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2242   "p" gnus-summary-mark-as-processable
2243   "u" gnus-summary-unmark-as-processable
2244   "U" gnus-summary-unmark-all-processable
2245   "v" gnus-uu-mark-over
2246   "s" gnus-uu-mark-series
2247   "r" gnus-uu-mark-region
2248   "g" gnus-uu-unmark-region
2249   "R" gnus-uu-mark-by-regexp
2250   "G" gnus-uu-unmark-by-regexp
2251   "t" gnus-uu-mark-thread
2252   "T" gnus-uu-unmark-thread
2253   "a" gnus-uu-mark-all
2254   "b" gnus-uu-mark-buffer
2255   "S" gnus-uu-mark-sparse
2256   "k" gnus-summary-kill-process-mark
2257   "y" gnus-summary-yank-process-mark
2258   "w" gnus-summary-save-process-mark
2259   "i" gnus-uu-invert-processable)
2260
2261 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2262   ;;"x" gnus-uu-extract-any
2263   "m" gnus-summary-save-parts
2264   "u" gnus-uu-decode-uu
2265   "U" gnus-uu-decode-uu-and-save
2266   "s" gnus-uu-decode-unshar
2267   "S" gnus-uu-decode-unshar-and-save
2268   "o" gnus-uu-decode-save
2269   "O" gnus-uu-decode-save
2270   "b" gnus-uu-decode-binhex
2271   "B" gnus-uu-decode-binhex
2272   "Y" gnus-uu-decode-yenc
2273   "p" gnus-uu-decode-postscript
2274   "P" gnus-uu-decode-postscript-and-save)
2275
2276 (gnus-define-keys
2277     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2278   "u" gnus-uu-decode-uu-view
2279   "U" gnus-uu-decode-uu-and-save-view
2280   "s" gnus-uu-decode-unshar-view
2281   "S" gnus-uu-decode-unshar-and-save-view
2282   "o" gnus-uu-decode-save-view
2283   "O" gnus-uu-decode-save-view
2284   "b" gnus-uu-decode-binhex-view
2285   "B" gnus-uu-decode-binhex-view
2286   "p" gnus-uu-decode-postscript-view
2287   "P" gnus-uu-decode-postscript-and-save-view)
2288
2289 (defvar gnus-article-post-menu nil)
2290
2291 (defconst gnus-summary-menu-maxlen 20)
2292
2293 (defun gnus-summary-menu-split (menu)
2294   ;; If we have lots of elements, divide them into groups of 20
2295   ;; and make a pane (or submenu) for each one.
2296   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2297       (let ((menu menu) sublists next
2298             (i 1))
2299         (while menu
2300           ;; Pull off the next gnus-summary-menu-maxlen elements
2301           ;; and make them the next element of sublist.
2302           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2303           (if next
2304               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2305                       nil))
2306           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2307                                              (aref (car (last menu)) 0)) menu)
2308                                sublists))
2309           (setq i (1+ i))
2310           (setq menu next))
2311         (nreverse sublists))
2312     ;; Few elements--put them all in one pane.
2313     menu))
2314
2315 (defun gnus-summary-make-menu-bar ()
2316   (gnus-turn-off-edit-menu 'summary)
2317
2318   (unless (boundp 'gnus-summary-misc-menu)
2319
2320     (easy-menu-define
2321       gnus-summary-kill-menu gnus-summary-mode-map ""
2322       (cons
2323        "Score"
2324        (nconc
2325         (list
2326          ["Customize" gnus-score-customize t])
2327         (gnus-make-score-map 'increase)
2328         (gnus-make-score-map 'lower)
2329         '(("Mark"
2330            ["Kill below" gnus-summary-kill-below t]
2331            ["Mark above" gnus-summary-mark-above t]
2332            ["Tick above" gnus-summary-tick-above t]
2333            ["Clear above" gnus-summary-clear-above t])
2334           ["Current score" gnus-summary-current-score t]
2335           ["Set score" gnus-summary-set-score t]
2336           ["Switch current score file..." gnus-score-change-score-file t]
2337           ["Set mark below..." gnus-score-set-mark-below t]
2338           ["Set expunge below..." gnus-score-set-expunge-below t]
2339           ["Edit current score file" gnus-score-edit-current-scores t]
2340           ["Edit score file..." gnus-score-edit-file t]
2341           ["Trace score" gnus-score-find-trace t]
2342           ["Find words" gnus-score-find-favourite-words t]
2343           ["Rescore buffer" gnus-summary-rescore t]
2344           ["Increase score..." gnus-summary-increase-score t]
2345           ["Lower score..." gnus-summary-lower-score t]))))
2346
2347     ;; Define both the Article menu in the summary buffer and the
2348     ;; equivalent Commands menu in the article buffer here for
2349     ;; consistency.
2350     (let ((innards
2351            `(("Hide"
2352               ["All" gnus-article-hide t]
2353               ["Headers" gnus-article-hide-headers t]
2354               ["Signature" gnus-article-hide-signature t]
2355               ["Citation" gnus-article-hide-citation t]
2356               ["List identifiers" gnus-article-hide-list-identifiers t]
2357               ["Banner" gnus-article-strip-banner t]
2358               ["Boring headers" gnus-article-hide-boring-headers t])
2359              ("Highlight"
2360               ["All" gnus-article-highlight t]
2361               ["Headers" gnus-article-highlight-headers t]
2362               ["Signature" gnus-article-highlight-signature t]
2363               ["Citation" gnus-article-highlight-citation t])
2364              ("MIME"
2365               ["Words" gnus-article-decode-mime-words t]
2366               ["Charset" gnus-article-decode-charset t]
2367               ["QP" gnus-article-de-quoted-unreadable t]
2368               ["Base64" gnus-article-de-base64-unreadable t]
2369               ["View MIME buttons" gnus-summary-display-buttonized t]
2370               ["View all" gnus-mime-view-all-parts t]
2371               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2372               ["Encrypt body" gnus-article-encrypt-body
2373                :active (not (gnus-group-read-only-p))
2374                ,@(if (featurep 'xemacs) nil
2375                    '(:help "Encrypt the message body on disk"))]
2376               ["Extract all parts..." gnus-summary-save-parts t]
2377               ("Multipart"
2378                ["Repair multipart" gnus-summary-repair-multipart t]
2379                ["Pipe part..." gnus-article-pipe-part t]
2380                ["Inline part" gnus-article-inline-part t]
2381                ["View part as type..." gnus-article-view-part-as-type t]
2382                ["Encrypt body" gnus-article-encrypt-body
2383                 :active (not (gnus-group-read-only-p))
2384                ,@(if (featurep 'xemacs) nil
2385                    '(:help "Encrypt the message body on disk"))]
2386                ["View part externally" gnus-article-view-part-externally t]
2387                ["View HTML parts in browser" gnus-article-browse-html-article t]
2388                ["View part with charset..." gnus-article-view-part-as-charset t]
2389                ["Copy part" gnus-article-copy-part t]
2390                ["Save part..." gnus-article-save-part t]
2391                ["View part" gnus-article-view-part t]))
2392              ("Date"
2393               ["Local" gnus-article-date-local t]
2394               ["ISO8601" gnus-article-date-iso8601 t]
2395               ["UT" gnus-article-date-ut t]
2396               ["Original" gnus-article-date-original t]
2397               ["Lapsed" gnus-article-date-lapsed t]
2398               ["User-defined" gnus-article-date-user t])
2399              ("Display"
2400               ["Remove images" gnus-article-remove-images t]
2401               ["Toggle smiley" gnus-treat-smiley t]
2402               ["Show X-Face" gnus-article-display-x-face t]
2403               ["Show picons in From" gnus-treat-from-picon t]
2404               ["Show picons in mail headers" gnus-treat-mail-picon t]
2405               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2406               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2407               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2408               ("View as different encoding"
2409                ,@(gnus-summary-menu-split
2410                   (mapcar
2411                    (lambda (cs)
2412                      ;; Since easymenu under Emacs doesn't allow
2413                      ;; lambda forms for menu commands, we should
2414                      ;; provide intern'ed function symbols.
2415                      (let ((command (intern (format "\
2416 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2417                        (fset command
2418                              `(lambda ()
2419                                 (interactive)
2420                                 (let ((gnus-summary-show-article-charset-alist
2421                                        '((1 . ,cs))))
2422                                   (gnus-summary-show-article 1))))
2423                        `[,(symbol-name cs) ,command t]))
2424                    (sort (if (fboundp 'coding-system-list)
2425                              (coding-system-list)
2426                            (mapcar 'car mm-mime-mule-charset-alist))
2427                          'string<)))))
2428              ("Washing"
2429               ("Remove Blanks"
2430                ["Leading" gnus-article-strip-leading-blank-lines t]
2431                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2432                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2433                ["All of the above" gnus-article-strip-blank-lines t]
2434                ["All" gnus-article-strip-all-blank-lines t]
2435                ["Leading space" gnus-article-strip-leading-space t]
2436                ["Trailing space" gnus-article-strip-trailing-space t]
2437                ["Leading space in headers"
2438                 gnus-article-remove-leading-whitespace t])
2439               ["Overstrike" gnus-article-treat-overstrike t]
2440               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2441               ["Non-ASCII" gnus-article-treat-non-ascii t]
2442               ["Emphasis" gnus-article-emphasize t]
2443               ["Word wrap" gnus-article-fill-cited-article t]
2444               ["Fill long lines" gnus-article-fill-long-lines t]
2445               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2446               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2447               ["Remove CR" gnus-article-remove-cr t]
2448               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2449               ["Base64" gnus-article-de-base64-unreadable t]
2450               ["Rot 13" gnus-summary-caesar-message
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "\"Caesar rotate\" article by 13"))]
2453               ["De-IDNA" gnus-summary-idna-message t]
2454               ["Morse decode" gnus-summary-morse-message t]
2455               ["Unix pipe..." gnus-summary-pipe-message t]
2456               ["Add buttons" gnus-article-add-buttons t]
2457               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2458               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2459               ["Verbose header" gnus-summary-verbose-headers t]
2460               ["Toggle header" gnus-summary-toggle-header t]
2461               ["Unfold headers" gnus-article-treat-unfold-headers t]
2462               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2463               ["Html" gnus-article-wash-html t]
2464               ["Unsplit URLs" gnus-article-unsplit-urls t]
2465               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2466               ["Decode HZ" gnus-article-decode-HZ t]
2467               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2468               ("(Outlook) Deuglify"
2469                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2470                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2471                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2472                ["Full (Outlook) deuglify"
2473                 gnus-article-outlook-deuglify-article t])
2474               )
2475              ("Output"
2476               ["Save in default format..." gnus-summary-save-article
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "Save article using default method"))]
2479               ["Save in file..." gnus-summary-save-article-file
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article in file"))]
2482               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2483               ["Save in MH folder..." gnus-summary-save-article-folder t]
2484               ["Save in VM folder..." gnus-summary-save-article-vm t]
2485               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2486               ["Save body in file..." gnus-summary-save-article-body-file t]
2487               ["Pipe through a filter..." gnus-summary-pipe-output t]
2488               ["Print with Muttprint..." gnus-summary-muttprint t]
2489               ["Print" gnus-summary-print-article
2490                ,@(if (featurep 'xemacs) '(t)
2491                    '(:help "Generate and print a PostScript image"))])
2492              ("Copy, move,... (Backend)"
2493               ,@(if (featurep 'xemacs) nil
2494                   '(:help "Copying, moving, expiring articles..."))
2495               ["Respool article..." gnus-summary-respool-article t]
2496               ["Move article..." gnus-summary-move-article
2497                (gnus-check-backend-function
2498                 'request-move-article gnus-newsgroup-name)]
2499               ["Copy article..." gnus-summary-copy-article t]
2500               ["Crosspost article..." gnus-summary-crosspost-article
2501                (gnus-check-backend-function
2502                 'request-replace-article gnus-newsgroup-name)]
2503               ["Import file..." gnus-summary-import-article
2504                (gnus-check-backend-function
2505                 'request-accept-article gnus-newsgroup-name)]
2506               ["Create article..." gnus-summary-create-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Check if posted" gnus-summary-article-posted-p t]
2510               ["Edit article" gnus-summary-edit-article
2511                (not (gnus-group-read-only-p))]
2512               ["Delete article" gnus-summary-delete-article
2513                (gnus-check-backend-function
2514                 'request-expire-articles gnus-newsgroup-name)]
2515               ["Query respool" gnus-summary-respool-query t]
2516               ["Trace respool" gnus-summary-respool-trace t]
2517               ["Delete expirable articles" gnus-summary-expire-articles-now
2518                (gnus-check-backend-function
2519                 'request-expire-articles gnus-newsgroup-name)])
2520              ("Extract"
2521               ["Uudecode" gnus-uu-decode-uu
2522                ,@(if (featurep 'xemacs) '(t)
2523                    '(:help "Decode uuencoded article(s)"))]
2524               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2525               ["Unshar" gnus-uu-decode-unshar t]
2526               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2527               ["Save" gnus-uu-decode-save t]
2528               ["Binhex" gnus-uu-decode-binhex t]
2529               ["PostScript" gnus-uu-decode-postscript t]
2530               ["All MIME parts" gnus-summary-save-parts t])
2531              ("Cache"
2532               ["Enter article" gnus-cache-enter-article t]
2533               ["Remove article" gnus-cache-remove-article t])
2534              ["Translate" gnus-article-babel t]
2535              ["Select article buffer" gnus-summary-select-article-buffer t]
2536              ["Make article buffer sticky" gnus-sticky-article t]
2537              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2538              ["Isearch article..." gnus-summary-isearch-article t]
2539              ["Beginning of the article" gnus-summary-beginning-of-article t]
2540              ["End of the article" gnus-summary-end-of-article t]
2541              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2542              ["Fetch referenced articles" gnus-summary-refer-references t]
2543              ["Fetch current thread" gnus-summary-refer-thread t]
2544              ["Fetch article with id..." gnus-summary-refer-article t]
2545              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2546              ["Redisplay" gnus-summary-show-article t]
2547              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2548       (easy-menu-define
2549         gnus-summary-article-menu gnus-summary-mode-map ""
2550         (cons "Article" innards))
2551
2552       (if (not (keymapp gnus-summary-article-menu))
2553           (easy-menu-define
2554             gnus-article-commands-menu gnus-article-mode-map ""
2555             (cons "Commands" innards))
2556         ;; in Emacs, don't share menu.
2557         (setq gnus-article-commands-menu
2558               (copy-keymap gnus-summary-article-menu))
2559         (define-key gnus-article-mode-map [menu-bar commands]
2560           (cons "Commands" gnus-article-commands-menu))))
2561
2562     (easy-menu-define
2563       gnus-summary-thread-menu gnus-summary-mode-map ""
2564       '("Threads"
2565         ["Find all messages in thread" gnus-summary-refer-thread t]
2566         ["Toggle threading" gnus-summary-toggle-threads t]
2567         ["Hide threads" gnus-summary-hide-all-threads t]
2568         ["Show threads" gnus-summary-show-all-threads t]
2569         ["Hide thread" gnus-summary-hide-thread t]
2570         ["Show thread" gnus-summary-show-thread t]
2571         ["Go to next thread" gnus-summary-next-thread t]
2572         ["Go to previous thread" gnus-summary-prev-thread t]
2573         ["Go down thread" gnus-summary-down-thread t]
2574         ["Go up thread" gnus-summary-up-thread t]
2575         ["Top of thread" gnus-summary-top-thread t]
2576         ["Mark thread as read" gnus-summary-kill-thread t]
2577         ["Mark thread as expired" gnus-summary-expire-thread t]
2578         ["Lower thread score" gnus-summary-lower-thread t]
2579         ["Raise thread score" gnus-summary-raise-thread t]
2580         ["Rethread current" gnus-summary-rethread-current t]))
2581
2582     (easy-menu-define
2583       gnus-summary-post-menu gnus-summary-mode-map ""
2584       `("Post"
2585         ["Send a message (mail or news)" gnus-summary-post-news
2586          ,@(if (featurep 'xemacs) '(t)
2587              '(:help "Compose a new message (mail or news)"))]
2588         ["Followup" gnus-summary-followup
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Post followup to this article"))]
2591         ["Followup and yank" gnus-summary-followup-with-original
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article, quoting its contents"))]
2594         ["Supersede article" gnus-summary-supersede-article t]
2595         ["Cancel article" gnus-summary-cancel-article
2596          ,@(if (featurep 'xemacs) '(t)
2597              '(:help "Cancel an article you posted"))]
2598         ["Reply" gnus-summary-reply t]
2599         ["Reply and yank" gnus-summary-reply-with-original t]
2600         ["Wide reply" gnus-summary-wide-reply t]
2601         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2602          ,@(if (featurep 'xemacs) '(t)
2603              '(:help "Mail a reply, quoting this article"))]
2604         ["Very wide reply" gnus-summary-very-wide-reply t]
2605         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2606          ,@(if (featurep 'xemacs) '(t)
2607              '(:help "Mail a very wide reply, quoting this article"))]
2608         ["Mail forward" gnus-summary-mail-forward t]
2609         ["Post forward" gnus-summary-post-forward t]
2610         ["Digest and mail" gnus-uu-digest-mail-forward t]
2611         ["Digest and post" gnus-uu-digest-post-forward t]
2612         ["Resend message" gnus-summary-resend-message t]
2613         ["Resend message edit" gnus-summary-resend-message-edit t]
2614         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2615         ["Send a mail" gnus-summary-mail-other-window t]
2616         ["Create a local message" gnus-summary-news-other-window t]
2617         ["Uuencode and post" gnus-uu-post-news
2618          ,@(if (featurep 'xemacs) '(t)
2619              '(:help "Post a uuencoded article"))]
2620         ["Followup via news" gnus-summary-followup-to-mail t]
2621         ["Followup via news and yank"
2622          gnus-summary-followup-to-mail-with-original t]
2623         ["Strip signature on reply"
2624          (lambda ()
2625            (interactive)
2626            (if (not (memq message-cite-function
2627                           '(message-cite-original-without-signature
2628                             message-cite-original)))
2629                ;; Stupid workaround for XEmacs not honoring :visible.
2630                (message "Can't toggle this value of `message-cite-function'")
2631              (setq message-cite-function
2632                    (if (eq message-cite-function
2633                            'message-cite-original-without-signature)
2634                        'message-cite-original
2635                      'message-cite-original-without-signature))))
2636          ;; XEmacs barfs on :visible.
2637          ,@(if (featurep 'xemacs) nil
2638              '(:visible (memq message-cite-function
2639                               '(message-cite-original-without-signature
2640                                 message-cite-original))))
2641          :style toggle
2642          :selected (eq message-cite-function
2643                        'message-cite-original-without-signature)
2644          ,@(if (featurep 'xemacs) nil
2645              '(:help "Strip signature from cited article when replying."))]
2646         ;;("Draft"
2647         ;;["Send" gnus-summary-send-draft t]
2648         ;;["Send bounced" gnus-resend-bounced-mail t])
2649         ))
2650
2651     (cond
2652      ((not (keymapp gnus-summary-post-menu))
2653       (setq gnus-article-post-menu gnus-summary-post-menu))
2654      ((not gnus-article-post-menu)
2655       ;; Don't share post menu.
2656       (setq gnus-article-post-menu
2657             (copy-keymap gnus-summary-post-menu))))
2658     (define-key gnus-article-mode-map [menu-bar post]
2659       (cons "Post" gnus-article-post-menu))
2660
2661     (easy-menu-define
2662       gnus-summary-misc-menu gnus-summary-mode-map ""
2663       `("Gnus"
2664         ("Mark Read"
2665          ["Mark as read" gnus-summary-mark-as-read-forward t]
2666          ["Mark same subject and select"
2667           gnus-summary-kill-same-subject-and-select t]
2668          ["Mark same subject" gnus-summary-kill-same-subject t]
2669          ["Catchup" gnus-summary-catchup
2670           ,@(if (featurep 'xemacs) '(t)
2671               '(:help "Mark unread articles in this group as read"))]
2672          ["Catchup all" gnus-summary-catchup-all t]
2673          ["Catchup to here" gnus-summary-catchup-to-here t]
2674          ["Catchup from here" gnus-summary-catchup-from-here t]
2675          ["Catchup region" gnus-summary-mark-region-as-read
2676           (gnus-mark-active-p)]
2677          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2678         ("Mark Various"
2679          ["Tick" gnus-summary-tick-article-forward t]
2680          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2681          ["Remove marks" gnus-summary-clear-mark-forward t]
2682          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2683          ["Set bookmark" gnus-summary-set-bookmark t]
2684          ["Remove bookmark" gnus-summary-remove-bookmark t])
2685         ("Limit to"
2686          ["Marks..." gnus-summary-limit-to-marks t]
2687          ["Subject..." gnus-summary-limit-to-subject t]
2688          ["Author..." gnus-summary-limit-to-author t]
2689          ["Recipient..." gnus-summary-limit-to-recipient t]
2690          ["Address..." gnus-summary-limit-to-address t]
2691          ["Age..." gnus-summary-limit-to-age t]
2692          ["Extra..." gnus-summary-limit-to-extra t]
2693          ["Score..." gnus-summary-limit-to-score t]
2694          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2695          ["Unread" gnus-summary-limit-to-unread t]
2696          ["Unseen" gnus-summary-limit-to-unseen t]
2697          ["Singletons" gnus-summary-limit-to-singletons t]
2698          ["Replied" gnus-summary-limit-to-replied t]
2699          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2700          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2701          ["Pop limit" gnus-summary-pop-limit t]
2702          ["Show dormant" gnus-summary-limit-include-dormant t]
2703          ["Hide childless dormant"
2704           gnus-summary-limit-exclude-childless-dormant t]
2705          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2706          ["Hide marked" gnus-summary-limit-exclude-marks t]
2707          ["Show expunged" gnus-summary-limit-include-expunged t])
2708         ("Process Mark"
2709          ["Set mark" gnus-summary-mark-as-processable t]
2710          ["Remove mark" gnus-summary-unmark-as-processable t]
2711          ["Remove all marks" gnus-summary-unmark-all-processable t]
2712          ["Invert marks" gnus-uu-invert-processable t]
2713          ["Mark above" gnus-uu-mark-over t]
2714          ["Mark series" gnus-uu-mark-series t]
2715          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2716          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2717          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2718          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2719          ["Mark all" gnus-uu-mark-all t]
2720          ["Mark buffer" gnus-uu-mark-buffer t]
2721          ["Mark sparse" gnus-uu-mark-sparse t]
2722          ["Mark thread" gnus-uu-mark-thread t]
2723          ["Unmark thread" gnus-uu-unmark-thread t]
2724          ("Process Mark Sets"
2725           ["Kill" gnus-summary-kill-process-mark t]
2726           ["Yank" gnus-summary-yank-process-mark
2727            gnus-newsgroup-process-stack]
2728           ["Save" gnus-summary-save-process-mark t]
2729           ["Run command on marked..." gnus-summary-universal-argument t]))
2730         ("Registry Marks")
2731         ("Scroll article"
2732          ["Page forward" gnus-summary-next-page
2733           ,@(if (featurep 'xemacs) '(t)
2734               '(:help "Show next page of article"))]
2735          ["Page backward" gnus-summary-prev-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show previous page of article"))]
2738          ["Line forward" gnus-summary-scroll-up t])
2739         ("Move"
2740          ["Next unread article" gnus-summary-next-unread-article t]
2741          ["Previous unread article" gnus-summary-prev-unread-article t]
2742          ["Next article" gnus-summary-next-article t]
2743          ["Previous article" gnus-summary-prev-article t]
2744          ["Next unread subject" gnus-summary-next-unread-subject t]
2745          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2746          ["Next article same subject" gnus-summary-next-same-subject t]
2747          ["Previous article same subject" gnus-summary-prev-same-subject t]
2748          ["First unread article" gnus-summary-first-unread-article t]
2749          ["Best unread article" gnus-summary-best-unread-article t]
2750          ["Go to subject number..." gnus-summary-goto-subject t]
2751          ["Go to article number..." gnus-summary-goto-article t]
2752          ["Go to the last article" gnus-summary-goto-last-article t]
2753          ["Pop article off history" gnus-summary-pop-article t])
2754         ("Sort"
2755          ["Sort by number" gnus-summary-sort-by-number t]
2756          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2757          ["Sort by author" gnus-summary-sort-by-author t]
2758          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2759          ["Sort by subject" gnus-summary-sort-by-subject t]
2760          ["Sort by date" gnus-summary-sort-by-date t]
2761          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2762          ["Sort by score" gnus-summary-sort-by-score t]
2763          ["Sort by lines" gnus-summary-sort-by-lines t]
2764          ["Sort by characters" gnus-summary-sort-by-chars t]
2765          ["Randomize" gnus-summary-sort-by-random t]
2766          ["Original sort" gnus-summary-sort-by-original t])
2767         ("Help"
2768          ["Describe group" gnus-summary-describe-group t]
2769          ["Read manual" gnus-info-find-node t])
2770         ("Modes"
2771          ["Pick and read" gnus-pick-mode t]
2772          ["Binary" gnus-binary-mode t])
2773         ("Regeneration"
2774          ["Regenerate" gnus-summary-prepare t]
2775          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2776          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2777          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2778          ["Toggle threading" gnus-summary-toggle-threads t])
2779         ["See old articles" gnus-summary-insert-old-articles t]
2780         ["See new articles" gnus-summary-insert-new-articles t]
2781         ["Filter articles..." gnus-summary-execute-command t]
2782         ["Run command on articles..." gnus-summary-universal-argument t]
2783         ["Search articles forward..." gnus-summary-search-article-forward t]
2784         ["Search articles backward..." gnus-summary-search-article-backward t]
2785         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2786         ["Expand window" gnus-summary-expand-window t]
2787         ["Expire expirable articles" gnus-summary-expire-articles
2788          (gnus-check-backend-function
2789           'request-expire-articles gnus-newsgroup-name)]
2790         ["Edit local kill file" gnus-summary-edit-local-kill t]
2791         ["Edit main kill file" gnus-summary-edit-global-kill t]
2792         ["Edit group parameters" gnus-summary-edit-parameters t]
2793         ["Customize group parameters" gnus-summary-customize-parameters t]
2794         ["Send a bug report" gnus-bug t]
2795         ("Exit"
2796          ["Catchup and exit" gnus-summary-catchup-and-exit
2797           ,@(if (featurep 'xemacs) '(t)
2798               '(:help "Mark unread articles in this group as read, then exit"))]
2799          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2800          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2801          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2802          ["Exit group" gnus-summary-exit
2803           ,@(if (featurep 'xemacs) '(t)
2804               '(:help "Exit current group, return to group selection mode"))]
2805          ["Exit group without updating" gnus-summary-exit-no-update t]
2806          ["Exit and goto next group" gnus-summary-next-group t]
2807          ["Exit and goto prev group" gnus-summary-prev-group t]
2808          ["Reselect group" gnus-summary-reselect-current-group t]
2809          ["Rescan group" gnus-summary-rescan-group t]
2810          ["Update dribble" gnus-summary-save-newsrc t])))
2811
2812     (gnus-run-hooks 'gnus-summary-menu-hook)))
2813
2814 (defvar gnus-summary-tool-bar-map nil)
2815
2816 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2817 ;; affect _new_ message buffers.  We might add a function that walks thru all
2818 ;; summary-mode buffers and force the update.
2819 (defun gnus-summary-tool-bar-update (&optional symbol value)
2820   "Update summary mode toolbar.
2821 Setter function for custom variables."
2822   (setq-default gnus-summary-tool-bar-map nil)
2823   (when symbol
2824     ;; When used as ":set" function:
2825     (set-default symbol value))
2826   (when (gnus-buffer-live-p gnus-summary-buffer)
2827     (with-current-buffer gnus-summary-buffer
2828       (gnus-summary-make-tool-bar))))
2829
2830 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2831                                      'gnus-summary-tool-bar-gnome
2832                                    'gnus-summary-tool-bar-retro)
2833   "Specifies the Gnus summary tool bar.
2834
2835 It can be either a list or a symbol referring to a list.  See
2836 `gmm-tool-bar-from-list' for the format of the list.  The
2837 default key map is `gnus-summary-mode-map'.
2838
2839 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2840 `gnus-summary-tool-bar-retro'."
2841   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2842                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2843                  (repeat :tag "User defined list" gmm-tool-bar-item)
2844                  (symbol))
2845   :version "23.1" ;; No Gnus
2846   :initialize 'custom-initialize-default
2847   :set 'gnus-summary-tool-bar-update
2848   :group 'gnus-summary)
2849
2850 (defcustom gnus-summary-tool-bar-gnome
2851   '((gnus-summary-post-news "mail/compose" nil)
2852     (gnus-summary-insert-new-articles "mail/inbox" nil
2853                                       :visible (or (not gnus-agent)
2854                                                    gnus-plugged))
2855     (gnus-summary-reply-with-original "mail/reply")
2856     (gnus-summary-reply "mail/reply" nil :visible nil)
2857     (gnus-summary-followup-with-original "mail/reply-all")
2858     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2859     (gnus-summary-mail-forward "mail/forward")
2860     (gnus-summary-save-article "mail/save")
2861     (gnus-summary-search-article-forward "search" nil :visible nil)
2862     (gnus-summary-print-article "print")
2863     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2864     ;; Some new commands that may need more suitable icons:
2865     (gnus-summary-save-newsrc "save" nil :visible nil)
2866     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2867     (gnus-summary-prev-article "left-arrow")
2868     (gnus-summary-next-article "right-arrow")
2869     (gnus-summary-next-page "next-page")
2870     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2871     ;;
2872     ;; Maybe some sort-by-... could be added:
2873     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2874     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2875     (gnus-summary-mark-as-expirable
2876      "delete" nil
2877      :visible (gnus-check-backend-function 'request-expire-articles
2878                                            gnus-newsgroup-name))
2879     (gnus-summary-mark-as-spam
2880      "mail/spam" t
2881      :visible (and (fboundp 'spam-group-ham-contents-p)
2882                    (spam-group-ham-contents-p gnus-newsgroup-name))
2883      :help "Mark as spam")
2884     (gnus-summary-mark-as-read-forward
2885      "mail/not-spam" nil
2886      :visible (and (fboundp 'spam-group-spam-contents-p)
2887                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2888     ;;
2889     (gnus-summary-exit "exit")
2890     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2891     (gnus-info-find-node "help"))
2892   "List of functions for the summary tool bar (GNOME style).
2893
2894 See `gmm-tool-bar-from-list' for the format of the list."
2895   :type '(repeat gmm-tool-bar-item)
2896   :version "23.1" ;; No Gnus
2897   :initialize 'custom-initialize-default
2898   :set 'gnus-summary-tool-bar-update
2899   :group 'gnus-summary)
2900
2901 (defcustom gnus-summary-tool-bar-retro
2902   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2903     (gnus-summary-next-unread-article "gnus/next-ur")
2904     (gnus-summary-post-news "gnus/post")
2905     (gnus-summary-followup-with-original "gnus/fuwo")
2906     (gnus-summary-followup "gnus/followup")
2907     (gnus-summary-reply-with-original "gnus/reply-wo")
2908     (gnus-summary-reply "gnus/reply")
2909     (gnus-summary-caesar-message "gnus/rot13")
2910     (gnus-uu-decode-uu "gnus/uu-decode")
2911     (gnus-summary-save-article-file "gnus/save-aif")
2912     (gnus-summary-save-article "gnus/save-art")
2913     (gnus-uu-post-news "gnus/uu-post")
2914     (gnus-summary-catchup "gnus/catchup")
2915     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2916     (gnus-summary-exit "gnus/exit-summ")
2917     ;; Some new command that may need more suitable icons:
2918     (gnus-summary-print-article "gnus/print" nil :visible nil)
2919     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2920     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2921     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2922     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2923     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2924     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2925     ;;
2926     (gnus-info-find-node "gnus/help" nil :visible nil))
2927   "List of functions for the summary tool bar (retro look).
2928
2929 See `gmm-tool-bar-from-list' for the format of the list."
2930   :type '(repeat gmm-tool-bar-item)
2931   :version "23.1" ;; No Gnus
2932   :initialize 'custom-initialize-default
2933   :set 'gnus-summary-tool-bar-update
2934   :group 'gnus-summary)
2935
2936 (defcustom gnus-summary-tool-bar-zap-list t
2937   "List of icon items from the global tool bar.
2938 These items are not displayed in the Gnus summary mode tool bar.
2939
2940 See `gmm-tool-bar-from-list' for the format of the list."
2941   :type 'gmm-tool-bar-zap-list
2942   :version "23.1" ;; No Gnus
2943   :initialize 'custom-initialize-default
2944   :set 'gnus-summary-tool-bar-update
2945   :group 'gnus-summary)
2946
2947 (defvar image-load-path)
2948 (defvar tool-bar-map)
2949
2950 (defun gnus-summary-make-tool-bar (&optional force)
2951   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2952 When FORCE, rebuild the tool bar."
2953   (when (and (not (featurep 'xemacs))
2954              (boundp 'tool-bar-mode)
2955              tool-bar-mode
2956              (or (not gnus-summary-tool-bar-map) force))
2957     (let* ((load-path
2958             (gmm-image-load-path-for-library "gnus"
2959                                              "mail/save.xpm"
2960                                              nil t))
2961            (image-load-path (cons (car load-path)
2962                                   (when (boundp 'image-load-path)
2963                                     image-load-path)))
2964            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2965                                         gnus-summary-tool-bar-zap-list
2966                                         'gnus-summary-mode-map)))
2967       (when map
2968         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2969         ;; uses its value.
2970         (setq gnus-summary-tool-bar-map map))))
2971   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2972
2973 (defun gnus-score-set-default (var value)
2974   "A version of set that updates the GNU Emacs menu-bar."
2975   (set var value)
2976   ;; It is the message that forces the active status to be updated.
2977   (message ""))
2978
2979 (defun gnus-make-score-map (type)
2980   "Make a summary score map of type TYPE."
2981   (if t
2982       nil
2983     (let ((headers '(("author" "from" string)
2984                      ("subject" "subject" string)
2985                      ("article body" "body" string)
2986                      ("article head" "head" string)
2987                      ("xref" "xref" string)
2988                      ("extra header" "extra" string)
2989                      ("lines" "lines" number)
2990                      ("followups to author" "followup" string)))
2991           (types '((number ("less than" <)
2992                            ("greater than" >)
2993                            ("equal" =))
2994                    (string ("substring" s)
2995                            ("exact string" e)
2996                            ("fuzzy string" f)
2997                            ("regexp" r))))
2998           (perms '(("temporary" (current-time-string))
2999                    ("permanent" nil)
3000                    ("immediate" now)))
3001           header)
3002       (list
3003        (apply
3004         'nconc
3005         (list
3006          (if (eq type 'lower)
3007              "Lower score"
3008            "Increase score"))
3009         (let (outh)
3010           (while headers
3011             (setq header (car headers))
3012             (setq outh
3013                   (cons
3014                    (apply
3015                     'nconc
3016                     (list (car header))
3017                     (let ((ts (cdr (assoc (nth 2 header) types)))
3018                           outt)
3019                       (while ts
3020                         (setq outt
3021                               (cons
3022                                (apply
3023                                 'nconc
3024                                 (list (caar ts))
3025                                 (let ((ps perms)
3026                                       outp)
3027                                   (while ps
3028                                     (setq outp
3029                                           (cons
3030                                            (vector
3031                                             (caar ps)
3032                                             (list
3033                                              'gnus-summary-score-entry
3034                                              (nth 1 header)
3035                                              (if (or (string= (nth 1 header)
3036                                                               "head")
3037                                                      (string= (nth 1 header)
3038                                                               "body"))
3039                                                  ""
3040                                                (list 'gnus-summary-header
3041                                                      (nth 1 header)))
3042                                              (list 'quote (nth 1 (car ts)))
3043                                              (list 'gnus-score-delta-default
3044                                                    nil)
3045                                              (nth 1 (car ps))
3046                                              t)
3047                                             t)
3048                                            outp))
3049                                     (setq ps (cdr ps)))
3050                                   (list (nreverse outp))))
3051                                outt))
3052                         (setq ts (cdr ts)))
3053                       (list (nreverse outt))))
3054                    outh))
3055             (setq headers (cdr headers)))
3056           (list (nreverse outh))))))))
3057
3058
3059 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3060 (defvar bookmark-make-record-function)
3061 \f
3062
3063 (defun gnus-summary-mode (&optional group)
3064   "Major mode for reading articles.
3065
3066 All normal editing commands are switched off.
3067 \\<gnus-summary-mode-map>
3068 Each line in this buffer represents one article.  To read an
3069 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3070 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3071 respectively.
3072
3073 You can also post articles and send mail from this buffer.  To
3074 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3075 of an article, type `\\[gnus-summary-reply]'.
3076
3077 There are approx. one gazillion commands you can execute in this
3078 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3079
3080 The following commands are available:
3081
3082 \\{gnus-summary-mode-map}"
3083   (interactive)
3084   (kill-all-local-variables)
3085   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3086     (gnus-summary-make-local-variables))
3087   (gnus-summary-make-local-variables)
3088   (setq gnus-newsgroup-name group)
3089   (when (gnus-visual-p 'summary-menu 'menu)
3090     (gnus-summary-make-menu-bar)
3091     (gnus-summary-make-tool-bar))
3092   (gnus-make-thread-indent-array)
3093   (gnus-simplify-mode-line)
3094   (setq major-mode 'gnus-summary-mode)
3095   (setq mode-name "Summary")
3096   (use-local-map gnus-summary-mode-map)
3097   (buffer-disable-undo)
3098   (setq buffer-read-only t              ;Disable modification
3099         show-trailing-whitespace nil)
3100   (setq truncate-lines t)
3101   (add-to-invisibility-spec '(gnus-sum . t))
3102   (gnus-summary-set-display-table)
3103   (gnus-set-default-directory)
3104   (make-local-variable 'gnus-summary-line-format)
3105   (make-local-variable 'gnus-summary-line-format-spec)
3106   (make-local-variable 'gnus-summary-dummy-line-format)
3107   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3108   (make-local-variable 'gnus-summary-mark-positions)
3109   (gnus-make-local-hook 'pre-command-hook)
3110   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3111   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3112   (turn-on-gnus-mailing-list-mode)
3113   (mm-enable-multibyte)
3114   (set (make-local-variable 'bookmark-make-record-function)
3115        'gnus-summary-bookmark-make-record)
3116   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3117   (gnus-update-summary-mark-positions))
3118
3119 (defun gnus-summary-make-local-variables ()
3120   "Make all the local summary buffer variables."
3121   (let (global)
3122     (dolist (local gnus-summary-local-variables)
3123       (if (consp local)
3124           (progn
3125             (if (eq (cdr local) 'global)
3126                 ;; Copy the global value of the variable.
3127                 (setq global (symbol-value (car local)))
3128               ;; Use the value from the list.
3129               (setq global (eval (cdr local))))
3130             (set (make-local-variable (car local)) global))
3131         ;; Simple nil-valued local variable.
3132         (set (make-local-variable local) nil)))))
3133
3134 ;; Summary data functions.
3135
3136 (defmacro gnus-data-number (data)
3137   `(car ,data))
3138
3139 (defmacro gnus-data-set-number (data number)
3140   `(setcar ,data ,number))
3141
3142 (defmacro gnus-data-mark (data)
3143   `(nth 1 ,data))
3144
3145 (defmacro gnus-data-set-mark (data mark)
3146   `(setcar (nthcdr 1 ,data) ,mark))
3147
3148 (defmacro gnus-data-pos (data)
3149   `(nth 2 ,data))
3150
3151 (defmacro gnus-data-set-pos (data pos)
3152   `(setcar (nthcdr 2 ,data) ,pos))
3153
3154 (defmacro gnus-data-header (data)
3155   `(nth 3 ,data))
3156
3157 (defmacro gnus-data-set-header (data header)
3158   `(setf (nth 3 ,data) ,header))
3159
3160 (defmacro gnus-data-level (data)
3161   `(nth 4 ,data))
3162
3163 (defmacro gnus-data-unread-p (data)
3164   `(= (nth 1 ,data) gnus-unread-mark))
3165
3166 (defmacro gnus-data-read-p (data)
3167   `(/= (nth 1 ,data) gnus-unread-mark))
3168
3169 (defmacro gnus-data-pseudo-p (data)
3170   `(consp (nth 3 ,data)))
3171
3172 (defmacro gnus-data-find (number)
3173   `(assq ,number gnus-newsgroup-data))
3174
3175 (defmacro gnus-data-find-list (number &optional data)
3176   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3177      (memq (assq ,number bdata)
3178            bdata)))
3179
3180 (defmacro gnus-data-make (number mark pos header level)
3181   `(list ,number ,mark ,pos ,header ,level))
3182
3183 (defun gnus-data-enter (after-article number mark pos header level offset)
3184   (let ((data (gnus-data-find-list after-article)))
3185     (unless data
3186       (error "No such article: %d" after-article))
3187     (setcdr data (cons (gnus-data-make number mark pos header level)
3188                        (cdr data)))
3189     (setq gnus-newsgroup-data-reverse nil)
3190     (gnus-data-update-list (cddr data) offset)))
3191
3192 (defun gnus-data-enter-list (after-article list &optional offset)
3193   (when list
3194     (let ((data (and after-article (gnus-data-find-list after-article)))
3195           (ilist list))
3196       (if (not (or data
3197                    after-article))
3198           (let ((odata gnus-newsgroup-data))
3199             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3200             (when offset
3201               (gnus-data-update-list odata offset)))
3202         ;; Find the last element in the list to be spliced into the main
3203         ;; list.
3204         (setq list (last list))
3205         (if (not data)
3206             (progn
3207               (setcdr list gnus-newsgroup-data)
3208               (setq gnus-newsgroup-data ilist)
3209               (when offset
3210                 (gnus-data-update-list (cdr list) offset)))
3211           (setcdr list (cdr data))
3212           (setcdr data ilist)
3213           (when offset
3214             (gnus-data-update-list (cdr list) offset))))
3215       (setq gnus-newsgroup-data-reverse nil))))
3216
3217 (defun gnus-data-remove (article &optional offset)
3218   (let ((data gnus-newsgroup-data))
3219     (if (= (gnus-data-number (car data)) article)
3220         (progn
3221           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3222                 gnus-newsgroup-data-reverse nil)
3223           (when offset
3224             (gnus-data-update-list gnus-newsgroup-data offset)))
3225       (while (cdr data)
3226         (when (= (gnus-data-number (cadr data)) article)
3227           (setcdr data (cddr data))
3228           (when offset
3229             (gnus-data-update-list (cdr data) offset))
3230           (setq data nil
3231                 gnus-newsgroup-data-reverse nil))
3232         (setq data (cdr data))))))
3233
3234 (defmacro gnus-data-list (backward)
3235   `(if ,backward
3236        (or gnus-newsgroup-data-reverse
3237            (setq gnus-newsgroup-data-reverse
3238                  (reverse gnus-newsgroup-data)))
3239      gnus-newsgroup-data))
3240
3241 (defun gnus-data-update-list (data offset)
3242   "Add OFFSET to the POS of all data entries in DATA."
3243   (setq gnus-newsgroup-data-reverse nil)
3244   (while data
3245     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3246     (setq data (cdr data))))
3247
3248 (defun gnus-summary-article-pseudo-p (article)
3249   "Say whether this article is a pseudo article or not."
3250   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3251
3252 (defmacro gnus-summary-article-sparse-p (article)
3253   "Say whether this article is a sparse article or not."
3254   `(memq ,article gnus-newsgroup-sparse))
3255
3256 (defmacro gnus-summary-article-ancient-p (article)
3257   "Say whether this article is a sparse article or not."
3258   `(memq ,article gnus-newsgroup-ancient))
3259
3260 (defun gnus-article-parent-p (number)
3261   "Say whether this article is a parent or not."
3262   (let ((data (gnus-data-find-list number)))
3263     (and (cdr data)              ; There has to be an article after...
3264          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3265             (gnus-data-level (nth 1 data))))))
3266
3267 (defun gnus-article-children (number)
3268   "Return a list of all children to NUMBER."
3269   (let* ((data (gnus-data-find-list number))
3270          (level (gnus-data-level (car data)))
3271          children)
3272     (setq data (cdr data))
3273     (while (and data
3274                 (= (gnus-data-level (car data)) (1+ level)))
3275       (push (gnus-data-number (car data)) children)
3276       (setq data (cdr data)))
3277     children))
3278
3279 (defmacro gnus-summary-skip-intangible ()
3280   "If the current article is intangible, then jump to a different article."
3281   '(let ((to (get-text-property (point) 'gnus-intangible)))
3282      (and to (gnus-summary-goto-subject to))))
3283
3284 (defmacro gnus-summary-article-intangible-p ()
3285   "Say whether this article is intangible or not."
3286   '(get-text-property (point) 'gnus-intangible))
3287
3288 (defun gnus-article-read-p (article)
3289   "Say whether ARTICLE is read or not."
3290   (not (or (memq article gnus-newsgroup-marked)
3291            (memq article gnus-newsgroup-spam-marked)
3292            (memq article gnus-newsgroup-unreads)
3293            (memq article gnus-newsgroup-unselected)
3294            (memq article gnus-newsgroup-dormant))))
3295
3296 ;; Some summary mode macros.
3297
3298 (defmacro gnus-summary-article-number ()
3299   "The article number of the article on the current line.
3300 If there isn't an article number here, then we return the current
3301 article number."
3302   '(progn
3303      (gnus-summary-skip-intangible)
3304      (or (get-text-property (point) 'gnus-number)
3305          (gnus-summary-last-subject))))
3306
3307 (defmacro gnus-summary-article-header (&optional number)
3308   "Return the header of article NUMBER."
3309   `(gnus-data-header (gnus-data-find
3310                       ,(or number '(gnus-summary-article-number)))))
3311
3312 (defmacro gnus-summary-thread-level (&optional number)
3313   "Return the level of thread that starts with article NUMBER."
3314   `(if (and (eq gnus-summary-make-false-root 'dummy)
3315             (get-text-property (point) 'gnus-intangible))
3316        0
3317      (gnus-data-level (gnus-data-find
3318                        ,(or number '(gnus-summary-article-number))))))
3319
3320 (defmacro gnus-summary-article-mark (&optional number)
3321   "Return the mark of article NUMBER."
3322   `(gnus-data-mark (gnus-data-find
3323                     ,(or number '(gnus-summary-article-number)))))
3324
3325 (defmacro gnus-summary-article-pos (&optional number)
3326   "Return the position of the line of article NUMBER."
3327   `(gnus-data-pos (gnus-data-find
3328                    ,(or number '(gnus-summary-article-number)))))
3329
3330 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3331 (defmacro gnus-summary-article-subject (&optional number)
3332   "Return current subject string or nil if nothing."
3333   `(let ((headers
3334           ,(if number
3335                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3336              '(gnus-data-header (assq (gnus-summary-article-number)
3337                                       gnus-newsgroup-data)))))
3338      (and headers
3339           (vectorp headers)
3340           (mail-header-subject headers))))
3341
3342 (defmacro gnus-summary-article-score (&optional number)
3343   "Return current article score."
3344   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3345                   gnus-newsgroup-scored))
3346        gnus-summary-default-score 0))
3347
3348 (defun gnus-summary-article-children (&optional number)
3349   "Return a list of article numbers that are children of article NUMBER."
3350   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3351          (level (gnus-data-level (car data)))
3352          l children)
3353     (while (and (setq data (cdr data))
3354                 (> (setq l (gnus-data-level (car data))) level))
3355       (and (= (1+ level) l)
3356            (push (gnus-data-number (car data))
3357                  children)))
3358     (nreverse children)))
3359
3360 (defun gnus-summary-article-parent (&optional number)
3361   "Return the article number of the parent of article NUMBER."
3362   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3363                                     (gnus-data-list t)))
3364          (level (gnus-data-level (car data))))
3365     (if (zerop level)
3366         ()                              ; This is a root.
3367       ;; We search until we find an article with a level less than
3368       ;; this one.  That function has to be the parent.
3369       (while (and (setq data (cdr data))
3370                   (not (< (gnus-data-level (car data)) level))))
3371       (and data (gnus-data-number (car data))))))
3372
3373 (defun gnus-unread-mark-p (mark)
3374   "Say whether MARK is the unread mark."
3375   (= mark gnus-unread-mark))
3376
3377 (defun gnus-read-mark-p (mark)
3378   "Say whether MARK is one of the marks that mark as read.
3379 This is all marks except unread, ticked, dormant, and expirable."
3380   (not (or (= mark gnus-unread-mark)
3381            (= mark gnus-ticked-mark)
3382            (= mark gnus-spam-mark)
3383            (= mark gnus-dormant-mark)
3384            (= mark gnus-expirable-mark))))
3385
3386 (defmacro gnus-article-mark (number)
3387   "Return the MARK of article NUMBER.
3388 This macro should only be used when computing the mark the \"first\"
3389 time; i.e., when generating the summary lines.  After that,
3390 `gnus-summary-article-mark' should be used to examine the
3391 marks of articles."
3392   `(cond
3393     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3394     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3395     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3396     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3397     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3398     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3399     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3400     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3401            gnus-ancient-mark))))
3402
3403 ;; Saving hidden threads.
3404
3405 (defmacro gnus-save-hidden-threads (&rest forms)
3406   "Save hidden threads, eval FORMS, and restore the hidden threads."
3407   (let ((config (make-symbol "config")))
3408     `(let ((,config (gnus-hidden-threads-configuration)))
3409        (unwind-protect
3410            (save-excursion
3411              ,@forms)
3412          (gnus-restore-hidden-threads-configuration ,config)))))
3413 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3414 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3415
3416 (defun gnus-data-compute-positions ()
3417   "Compute the positions of all articles."
3418   (setq gnus-newsgroup-data-reverse nil)
3419   (let ((data gnus-newsgroup-data))
3420     (save-excursion
3421       (gnus-save-hidden-threads
3422         (gnus-summary-show-all-threads)
3423         (goto-char (point-min))
3424         (while data
3425           (while (get-text-property (point) 'gnus-intangible)
3426             (forward-line 1))
3427           (gnus-data-set-pos (car data) (+ (point) 3))
3428           (setq data (cdr data))
3429           (forward-line 1))))))
3430
3431 (defun gnus-hidden-threads-configuration ()
3432   "Return the current hidden threads configuration."
3433   (save-excursion
3434     (let (config)
3435       (goto-char (point-min))
3436       (while (not (eobp))
3437         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3438           (push (save-excursion (forward-line 0) (point)) config))
3439         (forward-line 1))
3440       config)))
3441
3442 (defun gnus-restore-hidden-threads-configuration (config)
3443   "Restore hidden threads configuration from CONFIG."
3444   (save-excursion
3445     (let (point (inhibit-read-only t))
3446       (while (setq point (pop config))
3447         (goto-char point)
3448         (gnus-summary-hide-thread)))))
3449
3450 ;; Various summary mode internalish functions.
3451
3452 (defun gnus-mouse-pick-article (e)
3453   (interactive "e")
3454   (mouse-set-point e)
3455   (gnus-summary-next-page nil t))
3456
3457 (defun gnus-summary-set-display-table ()
3458   "Change the display table.
3459 Odd characters have a tendency to mess
3460 up nicely formatted displays - we make all possible glyphs
3461 display only a single character."
3462
3463   ;; We start from the standard display table, if any.
3464   (let ((table (or (copy-sequence standard-display-table)
3465                    (make-display-table)))
3466         (i 32))
3467     ;; Nix out all the control chars...
3468     (while (>= (setq i (1- i)) 0)
3469       (gnus-put-display-table i [??] table))
3470    ;; ... but not newline and cr, of course.  (cr is necessary for the
3471     ;; selective display).
3472     (gnus-put-display-table ?\n nil table)
3473     (gnus-put-display-table ?\r nil table)
3474     ;; We keep TAB as well.
3475     (gnus-put-display-table ?\t nil table)
3476     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3477     ;; Emacs 23 (unicode), that are not set already.
3478     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3479                  160
3480                256)))
3481       (while (>= (setq i (1- i)) 127)
3482         ;; Only modify if the entry is nil.
3483         (unless (gnus-get-display-table i table)
3484           (gnus-put-display-table i [??] table))))
3485     (setq buffer-display-table table)))
3486
3487 (defun gnus-summary-set-article-display-arrow (pos)
3488   "Update the overlay arrow to point to line at position POS."
3489   (when gnus-summary-display-arrow
3490     (make-local-variable 'overlay-arrow-position)
3491     (make-local-variable 'overlay-arrow-string)
3492     (save-excursion
3493       (goto-char pos)
3494       (beginning-of-line)
3495       (unless overlay-arrow-position
3496         (setq overlay-arrow-position (make-marker)))
3497       (setq overlay-arrow-string "=>"
3498             overlay-arrow-position (set-marker overlay-arrow-position
3499                                                (point)
3500                                                (current-buffer))))))
3501
3502 (defun gnus-summary-setup-buffer (group)
3503   "Initialize summary buffer.
3504 If the setup was successful, non-nil is returned."
3505   (let ((buffer (gnus-summary-buffer-name group))
3506         (dead-name (concat "*Dead Summary "
3507                            (gnus-group-decoded-name group) "*")))
3508     ;; If a dead summary buffer exists, we kill it.
3509     (when (gnus-buffer-live-p dead-name)
3510       (gnus-kill-buffer dead-name))
3511     (if (get-buffer buffer)
3512         (progn
3513           (set-buffer buffer)
3514           (setq gnus-summary-buffer (current-buffer))
3515           (not gnus-newsgroup-prepared))
3516       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3517       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3518       (gnus-summary-mode group)
3519       (when (gnus-group-quit-config group)
3520         (set (make-local-variable 'gnus-single-article-buffer) nil))
3521       (make-local-variable 'gnus-article-buffer)
3522       (make-local-variable 'gnus-article-current)
3523       (make-local-variable 'gnus-original-article-buffer)
3524       (setq gnus-newsgroup-name group)
3525       ;; Set any local variables in the group parameters.
3526       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3527       t)))
3528
3529 (defun gnus-set-global-variables ()
3530   "Set the global equivalents of the buffer-local variables.
3531 They are set to the latest values they had.  These reflect the summary
3532 buffer that was in action when the last article was fetched."
3533   (when (eq major-mode 'gnus-summary-mode)
3534     (setq gnus-summary-buffer (current-buffer))
3535     (let ((name gnus-newsgroup-name)
3536           (marked gnus-newsgroup-marked)
3537           (spam gnus-newsgroup-spam-marked)
3538           (unread gnus-newsgroup-unreads)
3539           (headers gnus-current-headers)
3540           (data gnus-newsgroup-data)
3541           (summary gnus-summary-buffer)
3542           (article-buffer gnus-article-buffer)
3543           (original gnus-original-article-buffer)
3544           (gac gnus-article-current)
3545           (reffed gnus-reffed-article-number)
3546           (score-file gnus-current-score-file)
3547           (default-charset gnus-newsgroup-charset)
3548           vlist)
3549       (let ((locals gnus-newsgroup-variables))
3550         (while locals
3551           (if (consp (car locals))
3552               (push (eval (caar locals)) vlist)
3553             (push (eval (car locals)) vlist))
3554           (setq locals (cdr locals)))
3555         (setq vlist (nreverse vlist)))
3556       (with-current-buffer gnus-group-buffer
3557         (setq gnus-newsgroup-name name
3558               gnus-newsgroup-marked marked
3559               gnus-newsgroup-spam-marked spam
3560               gnus-newsgroup-unreads unread
3561               gnus-current-headers headers
3562               gnus-newsgroup-data data
3563               gnus-article-current gac
3564               gnus-summary-buffer summary
3565               gnus-article-buffer article-buffer
3566               gnus-original-article-buffer original
3567               gnus-reffed-article-number reffed
3568               gnus-current-score-file score-file
3569               gnus-newsgroup-charset default-charset)
3570         (let ((locals gnus-newsgroup-variables))
3571           (while locals
3572             (if (consp (car locals))
3573                 (set (caar locals) (pop vlist))
3574               (set (car locals) (pop vlist)))
3575             (setq locals (cdr locals))))
3576         ;; The article buffer also has local variables.
3577         (when (gnus-buffer-live-p gnus-article-buffer)
3578           (set-buffer gnus-article-buffer)
3579           (setq gnus-summary-buffer summary))))))
3580
3581 (defun gnus-summary-article-unread-p (article)
3582   "Say whether ARTICLE is unread or not."
3583   (memq article gnus-newsgroup-unreads))
3584
3585 (defun gnus-summary-first-article-p (&optional article)
3586   "Return whether ARTICLE is the first article in the buffer."
3587   (if (not (setq article (or article (gnus-summary-article-number))))
3588       nil
3589     (eq article (caar gnus-newsgroup-data))))
3590
3591 (defun gnus-summary-last-article-p (&optional article)
3592   "Return whether ARTICLE is the last article in the buffer."
3593   (if (not (setq article (or article (gnus-summary-article-number))))
3594       ;; All non-existent numbers are the last article.  :-)
3595       t
3596     (not (cdr (gnus-data-find-list article)))))
3597
3598 (defun gnus-make-thread-indent-array (&optional n)
3599   (when (or n
3600             (progn (setq n 200) nil)
3601             (null gnus-thread-indent-array)
3602             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3603     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3604           gnus-thread-indent-array-level gnus-thread-indent-level)
3605     (while (>= n 0)
3606       (aset gnus-thread-indent-array n
3607             (make-string (* n gnus-thread-indent-level) ? ))
3608       (setq n (1- n)))))
3609
3610 (defun gnus-update-summary-mark-positions ()
3611   "Compute where the summary marks are to go."
3612   (save-excursion
3613     (when (gnus-buffer-exists-p gnus-summary-buffer)
3614       (set-buffer gnus-summary-buffer))
3615     (let ((spec gnus-summary-line-format-spec)
3616           pos)
3617       (save-excursion
3618         (gnus-set-work-buffer)
3619         (let ((gnus-tmp-unread ?Z)
3620               (gnus-replied-mark ?Z)
3621               (gnus-score-below-mark ?Z)
3622               (gnus-score-over-mark ?Z)
3623               (gnus-undownloaded-mark ?Z)
3624               (gnus-summary-line-format-spec spec)
3625               (gnus-newsgroup-downloadable '(0))
3626               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3627               case-fold-search ignores)
3628           ;; Here, all marks are bound to Z.
3629           (gnus-summary-insert-line header
3630                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3631           (goto-char (point-min))
3632           ;; Memorize the positions of the same characters as dummy marks.
3633           (while (re-search-forward "[A-D]" nil t)
3634             (push (point) ignores))
3635           (erase-buffer)
3636           ;; We use A-D as dummy marks in order to know column positions
3637           ;; where marks should be inserted.
3638           (setq gnus-tmp-unread ?A
3639                 gnus-replied-mark ?B
3640                 gnus-score-below-mark ?C
3641                 gnus-score-over-mark ?C
3642                 gnus-undownloaded-mark ?D)
3643           (gnus-summary-insert-line header
3644                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3645           ;; Ignore characters which aren't dummy marks.
3646           (dolist (p ignores)
3647             (delete-region (goto-char (1- p)) p)
3648             (insert ?Z))
3649           (goto-char (point-min))
3650           (setq pos (list (cons 'unread
3651                                 (and (search-forward "A" nil t)
3652                                      (- (point) (point-min) 1)))))
3653           (goto-char (point-min))
3654           (push (cons 'replied (and (search-forward "B" nil t)
3655                                     (- (point) (point-min) 1)))
3656                 pos)
3657           (goto-char (point-min))
3658           (push (cons 'score (and (search-forward "C" nil t)
3659                                   (- (point) (point-min) 1)))
3660                 pos)
3661           (goto-char (point-min))
3662           (push (cons 'download (and (search-forward "D" nil t)
3663                                      (- (point) (point-min) 1)))
3664                 pos)))
3665       (setq gnus-summary-mark-positions pos))))
3666
3667 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3668   "Insert a dummy root in the summary buffer."
3669   (beginning-of-line)
3670   (gnus-add-text-properties
3671    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3672    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3673
3674 (defun gnus-summary-extract-address-component (from)
3675   (or (car (funcall gnus-extract-address-components from))
3676       from))
3677
3678 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3679   (let ((mail-parse-charset gnus-newsgroup-charset)
3680         (ignored-from-addresses (gnus-ignored-from-addresses))
3681         ; Is it really necessary to do this next part for each summary line?
3682         ; Luckily, doesn't seem to slow things down much.
3683         (mail-parse-ignored-charsets
3684          (with-current-buffer gnus-summary-buffer
3685            gnus-newsgroup-ignored-charsets)))
3686     (or
3687      (and ignored-from-addresses
3688           (string-match ignored-from-addresses gnus-tmp-from)
3689           (let ((extra-headers (mail-header-extra header))
3690                 to
3691                 newsgroups)
3692             (cond
3693              ((setq to (cdr (assq 'To extra-headers)))
3694               (concat gnus-summary-to-prefix
3695                       (inline
3696                         (gnus-summary-extract-address-component
3697                          (funcall gnus-decode-encoded-address-function to)))))
3698              ((setq newsgroups
3699                     (or
3700                      (cdr (assq 'Newsgroups extra-headers))
3701                      (and
3702                       (memq 'Newsgroups gnus-extra-headers)
3703                       (eq (car (gnus-find-method-for-group
3704                                 gnus-newsgroup-name)) 'nntp)
3705                       (gnus-group-real-name gnus-newsgroup-name))))
3706               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3707      (gnus-string-mark-left-to-right
3708       (inline
3709        (gnus-summary-extract-address-component gnus-tmp-from))))))
3710
3711 (defun gnus-summary-insert-line (gnus-tmp-header
3712                                  gnus-tmp-level gnus-tmp-current
3713                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3714                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3715                                  &optional gnus-tmp-dummy gnus-tmp-score
3716                                  gnus-tmp-process)
3717   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3718       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3719                                           gnus-tmp-level)))
3720   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3721          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3722          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3723          (gnus-tmp-score-char
3724           (if (or (null gnus-summary-default-score)
3725                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3726                       gnus-summary-zcore-fuzz))
3727               ?                         ;Whitespace
3728             (if (< gnus-tmp-score gnus-summary-default-score)
3729                 gnus-score-below-mark gnus-score-over-mark)))
3730          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3731          (gnus-tmp-replied
3732           (cond (gnus-tmp-process gnus-process-mark)
3733                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3734                  gnus-cached-mark)
3735                 (gnus-tmp-replied gnus-replied-mark)
3736                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3737                  gnus-forwarded-mark)
3738                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3739                  gnus-saved-mark)
3740                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3741                  gnus-unseen-mark)
3742                 (t gnus-no-mark)))
3743          (gnus-tmp-downloaded
3744           (cond (undownloaded
3745                  gnus-undownloaded-mark)
3746                 (gnus-newsgroup-agentized
3747                  gnus-downloaded-mark)
3748                 (t
3749                  gnus-no-mark)))
3750          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3751          (gnus-tmp-name
3752           (cond
3753            ((string-match "<[^>]+> *$" gnus-tmp-from)
3754             (let ((beg (match-beginning 0)))
3755               (or (and (string-match "^\".+\"" gnus-tmp-from)
3756                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3757                   (substring gnus-tmp-from 0 beg))))
3758            ((string-match "(.+)" gnus-tmp-from)
3759             (substring gnus-tmp-from
3760                        (1+ (match-beginning 0)) (1- (match-end 0))))
3761            (t gnus-tmp-from)))
3762          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3763          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3764          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3765          (inhibit-read-only t))
3766     (when (string= gnus-tmp-name "")
3767       (setq gnus-tmp-name gnus-tmp-from))
3768     (unless (numberp gnus-tmp-lines)
3769       (setq gnus-tmp-lines -1))
3770     (if (= gnus-tmp-lines -1)
3771         (setq gnus-tmp-lines "?")
3772       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3773     (condition-case ()
3774         (gnus-put-text-property
3775          (point)
3776          (progn (eval gnus-summary-line-format-spec) (point))
3777          'gnus-number gnus-tmp-number)
3778       (error (gnus-message 5 "Error updating the summary line")))
3779     (when (gnus-visual-p 'summary-highlight 'highlight)
3780       (forward-line -1)
3781       (gnus-summary-highlight-line)
3782       (gnus-run-hooks 'gnus-summary-update-hook)
3783       (forward-line 1))))
3784
3785 (defun gnus-summary-update-line (&optional dont-update)
3786   "Update summary line after change."
3787   (when (and gnus-summary-default-score
3788              (not gnus-summary-inhibit-highlight))
3789     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3790            (article (gnus-summary-article-number))
3791            (score (gnus-summary-article-score article)))
3792       (unless dont-update
3793         (if (and gnus-summary-mark-below
3794                  (< (gnus-summary-article-score)
3795                     gnus-summary-mark-below))
3796             ;; This article has a low score, so we mark it as read.
3797             (when (memq article gnus-newsgroup-unreads)
3798               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3799           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3800             ;; This article was previously marked as read on account
3801             ;; of a low score, but now it has risen, so we mark it as
3802             ;; unread.
3803             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3804         (gnus-summary-update-mark
3805          (if (or (null gnus-summary-default-score)
3806                  (<= (abs (- score gnus-summary-default-score))
3807                      gnus-summary-zcore-fuzz))
3808              ?                          ;Whitespace
3809            (if (< score gnus-summary-default-score)
3810                gnus-score-below-mark gnus-score-over-mark))
3811          'score))
3812       ;; Do visual highlighting.
3813       (when (gnus-visual-p 'summary-highlight 'highlight)
3814         (gnus-summary-highlight-line)
3815         (gnus-run-hooks 'gnus-summary-update-hook)))))
3816
3817 (defvar gnus-tmp-new-adopts nil)
3818
3819 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3820   "Return the number of articles in THREAD.
3821 This may be 0 in some cases -- if none of the articles in
3822 the thread are to be displayed."
3823   (let* ((number
3824          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3825           (cond
3826            ((not (listp thread))
3827             1)
3828            ((and (consp thread) (cdr thread))
3829             (apply
3830              '+ 1 (mapcar
3831                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3832            ((null thread)
3833             1)
3834            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3835             1)
3836            (t 0))))
3837     (when (and level (zerop level) gnus-tmp-new-adopts)
3838       (incf number
3839             (apply '+ (mapcar
3840                        'gnus-summary-number-of-articles-in-thread
3841                        gnus-tmp-new-adopts))))
3842     (if char
3843         (if (> number 1) gnus-not-empty-thread-mark
3844           gnus-empty-thread-mark)
3845       number)))
3846
3847 (defsubst gnus-summary-line-message-size (head)
3848   "Return pretty-printed version of message size.
3849 This function is intended to be used in
3850 `gnus-summary-line-format-alist'."
3851   (let ((c (or (mail-header-chars head) -1)))
3852     (cond ((< c 0) "n/a")               ; chars not available
3853           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3854           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3855           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3856           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3857
3858 (defcustom gnus-user-date-format-alist
3859   '(((gnus-seconds-today) . "Today, %H:%M")
3860     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3861     (604800 . "%A %H:%M")               ; That's one week
3862     ((gnus-seconds-month) . "%A %d")
3863     ((gnus-seconds-year) . "%B %d")
3864     (t . "%b %d %Y"))                   ; This one is used when no other
3865                                         ; does match
3866   "Specifies date format depending on age of article.
3867 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3868 seconds) or a Lisp expression evaluating to a number.  When the age of
3869 the article is less than this number, then use `format-time-string'
3870 with the corresponding FORMAT for displaying the date of the article.
3871 If AGE is not a number or a Lisp expression evaluating to a
3872 non-number, then the corresponding FORMAT is used as a default value.
3873
3874 Note that the list is processed from the beginning, so it should be
3875 sorted by ascending AGE.  Also note that items following the first
3876 non-number AGE will be ignored.
3877
3878 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3879 and `gnus-seconds-year' in the AGE spec.  They return the number of
3880 seconds passed since the start of today, of this month, of this year,
3881 respectively."
3882   :version "24.1"
3883   :group 'gnus-summary-format
3884   :type '(alist :key-type sexp :value-type string))
3885
3886 (defun gnus-user-date (messy-date)
3887   "Format the messy-date according to `gnus-user-date-format-alist'.
3888 Returns \"  ?  \" if there's bad input or if another error occurs.
3889 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3890   (condition-case ()
3891       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3892              (now (gnus-float-time))
3893              ;;If we don't find something suitable we'll use this one
3894              (my-format "%b %d '%y"))
3895         (let* ((difference (- now messy-date))
3896                (templist gnus-user-date-format-alist)
3897                (top (eval (caar templist))))
3898           (while (if (numberp top) (< top difference) (not top))
3899             (progn
3900               (setq templist (cdr templist))
3901               (setq top (eval (caar templist)))))
3902           (if (stringp (cdr (car templist)))
3903               (setq my-format (cdr (car templist)))))
3904         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3905     (error "  ?   ")))
3906
3907 (defun gnus-summary-set-local-parameters (group)
3908   "Go through the local params of GROUP and set all variable specs in that list."
3909   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3910                                         ; really variables.
3911     (dolist (elem (gnus-group-find-parameter group))
3912       (and (consp elem)                 ; Has to be a cons.
3913            (consp (cdr elem))           ; The cdr has to be a list.
3914            (symbolp (car elem))         ; Has to be a symbol in there.
3915            (not (memq (car elem) vars))
3916            (ignore-errors
3917              (push (car elem) vars)
3918              ;; Variables like `gnus-show-threads' that are globally
3919              ;; bound, if used as group parameters, need to get to be
3920              ;; buffer-local, whereas just parameters like `gcc-self',
3921              ;; `timestamp', etc. should not be bound as variables.
3922              (if (boundp (car elem))
3923                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3924                (eval (nth 1 elem))))))))
3925
3926 (defun gnus-summary-read-group (group &optional show-all no-article
3927                                       kill-buffer no-display backward
3928                                       select-articles)
3929   "Start reading news in newsgroup GROUP.
3930 If SHOW-ALL is non-nil, already read articles are also listed.
3931 If NO-ARTICLE is non-nil, no article is selected initially.
3932 If NO-DISPLAY, don't generate the summary buffer contents.
3933 If KILL-BUFFER, it should be a buffer that's killed once the new
3934 summary buffer has been generated.
3935 If BACKWARD, move point to the previous group in the group buffer
3936 If SELECT-ARTICLES, only select those articles from GROUP."
3937   (let (result)
3938     (while (and group
3939                 (null (setq result
3940                             (let ((gnus-auto-select-next nil))
3941                               (or (gnus-summary-read-group-1
3942                                    group show-all no-article
3943                                    kill-buffer no-display
3944                                    select-articles)
3945                                   (setq show-all nil
3946                                         select-articles nil)))))
3947                 (eq gnus-auto-select-next 'quietly))
3948       (set-buffer gnus-group-buffer)
3949       ;; The entry function called above goes to the next
3950       ;; group automatically, so we go two groups back
3951       ;; if we are searching for the previous group.
3952       (when backward
3953         (gnus-group-prev-unread-group 2))
3954       (if (not (equal group (gnus-group-group-name)))
3955           (setq group (gnus-group-group-name))
3956         (setq group nil)))
3957     result))
3958
3959 (defun gnus-summary-read-group-1 (group show-all no-article
3960                                         kill-buffer no-display
3961                                         &optional select-articles)
3962   ;; Killed foreign groups can't be entered.
3963   ;;  (when (and (not (gnus-group-native-p group))
3964   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3965   ;;    (error "Dead non-native groups can't be entered"))
3966   (gnus-message 7 "Retrieving newsgroup: %s..."
3967                 (gnus-group-decoded-name group))
3968   (let* ((new-group (gnus-summary-setup-buffer group))
3969          (quit-config (gnus-group-quit-config group))
3970          (did-select (and new-group (gnus-select-newsgroup
3971                                      group show-all select-articles))))
3972     (cond
3973      ;; This summary buffer exists already, so we just select it.
3974      ((not new-group)
3975       (gnus-set-global-variables)
3976       (when kill-buffer
3977         (gnus-kill-or-deaden-summary kill-buffer))
3978       (gnus-configure-windows 'summary 'force)
3979       (gnus-set-mode-line 'summary)
3980       (gnus-summary-position-point)
3981       (message "")
3982       t)
3983      ;; We couldn't select this group.
3984      ((null did-select)
3985       (when (and (eq major-mode 'gnus-summary-mode)
3986                  (not (equal (current-buffer) kill-buffer)))
3987         (kill-buffer (current-buffer))
3988         (if (not quit-config)
3989             (progn
3990               ;; Update the info -- marks might need to be removed,
3991               ;; for instance.
3992               (gnus-summary-update-info)
3993               (set-buffer gnus-group-buffer)
3994               (gnus-group-jump-to-group group)
3995               (gnus-group-next-unread-group 1))
3996           (gnus-handle-ephemeral-exit quit-config)))
3997       (if (null (gnus-list-of-unread-articles group))
3998           (gnus-message 3 "Group %s contains no messages" group)
3999         (gnus-message 3 "Can't select group"))
4000       nil)
4001      ;; The user did a `C-g' while prompting for number of articles,
4002      ;; so we exit this group.
4003      ((eq did-select 'quit)
4004       (and (eq major-mode 'gnus-summary-mode)
4005            (not (equal (current-buffer) kill-buffer))
4006            (kill-buffer (current-buffer)))
4007       (when kill-buffer
4008         (gnus-kill-or-deaden-summary kill-buffer))
4009       (if (not quit-config)
4010           (progn
4011             (set-buffer gnus-group-buffer)
4012             (gnus-group-jump-to-group group)
4013             (gnus-configure-windows 'group 'force))
4014         (gnus-handle-ephemeral-exit quit-config))
4015       ;; Finally signal the quit.
4016       (signal 'quit nil))
4017      ;; The group was successfully selected.
4018      (t
4019       (gnus-set-global-variables)
4020       ;; Save the active value in effect when the group was entered.
4021       (setq gnus-newsgroup-active
4022             (gnus-copy-sequence
4023              (gnus-active gnus-newsgroup-name)))
4024       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4025       ;; You can change the summary buffer in some way with this hook.
4026       (gnus-run-hooks 'gnus-select-group-hook)
4027       (when (memq 'summary (gnus-update-format-specifications
4028                             nil 'summary 'summary-mode 'summary-dummy))
4029         ;; The format specification for the summary line was updated,
4030         ;; so we need to update the mark positions as well.
4031         (gnus-update-summary-mark-positions))
4032       ;; Do score processing.
4033       (when gnus-use-scoring
4034         (gnus-possibly-score-headers))
4035       ;; Check whether to fill in the gaps in the threads.
4036       (when gnus-build-sparse-threads
4037         (gnus-build-sparse-threads))
4038       ;; Find the initial limit.
4039       (if show-all
4040           (let ((gnus-newsgroup-dormant nil))
4041             (gnus-summary-initial-limit show-all))
4042         (gnus-summary-initial-limit show-all))
4043       ;; Generate the summary buffer.
4044       (unless no-display
4045         (gnus-summary-prepare))
4046       (when gnus-use-trees
4047         (gnus-tree-open group)
4048         (setq gnus-summary-highlight-line-function
4049               'gnus-tree-highlight-article))
4050       ;; If the summary buffer is empty, but there are some low-scored
4051       ;; articles or some excluded dormants, we include these in the
4052       ;; buffer.
4053       (when (and (zerop (buffer-size))
4054                  (not no-display))
4055         (cond (gnus-newsgroup-dormant
4056                (gnus-summary-limit-include-dormant))
4057               ((and gnus-newsgroup-scored show-all)
4058                (gnus-summary-limit-include-expunged t))))
4059       ;; Function `gnus-apply-kill-file' must be called in this hook.
4060       (gnus-run-hooks 'gnus-apply-kill-hook)
4061       (if (and (zerop (buffer-size))
4062                (not no-display))
4063           (progn
4064             ;; This newsgroup is empty.
4065             (gnus-summary-catchup-and-exit nil t)
4066             (gnus-message 6 "No unread news")
4067             (when kill-buffer
4068               (gnus-kill-or-deaden-summary kill-buffer))
4069             ;; Return nil from this function.
4070             nil)
4071         ;; Hide conversation thread subtrees.  We cannot do this in
4072         ;; gnus-summary-prepare-hook since kill processing may not
4073         ;; work with hidden articles.
4074         (gnus-summary-maybe-hide-threads)
4075         (gnus-configure-windows 'summary)
4076         (when kill-buffer
4077           (gnus-kill-or-deaden-summary kill-buffer))
4078         (gnus-summary-auto-select-subject)
4079         ;; Show first unread article if requested.
4080         (if (and (not no-article)
4081                  (not no-display)
4082                  gnus-newsgroup-unreads
4083                  gnus-auto-select-first)
4084             (progn
4085               (let ((art (gnus-summary-article-number)))
4086                 (unless (and (not gnus-plugged)
4087                              (or (memq art gnus-newsgroup-undownloaded)
4088                                  (memq art gnus-newsgroup-downloadable)))
4089                   (gnus-summary-goto-article art))))
4090           ;; Don't select any articles.
4091           (gnus-summary-position-point)
4092           (gnus-configure-windows 'summary 'force)
4093           (gnus-set-mode-line 'summary))
4094         (when (and gnus-auto-center-group
4095                    (get-buffer-window gnus-group-buffer t))
4096           ;; Gotta use windows, because recenter does weird stuff if
4097           ;; the current buffer ain't the displayed window.
4098           (let ((owin (selected-window)))
4099             (select-window (get-buffer-window gnus-group-buffer t))
4100             (when (gnus-group-goto-group group)
4101               (recenter))
4102             (select-window owin)))
4103         ;; Mark this buffer as "prepared".
4104         (setq gnus-newsgroup-prepared t)
4105         (gnus-run-hooks 'gnus-summary-prepared-hook)
4106         (unless (gnus-ephemeral-group-p group)
4107           (gnus-group-update-group group nil t))
4108         t)))))
4109
4110 (defun gnus-summary-auto-select-subject ()
4111   "Select the subject line on initial group entry."
4112   (goto-char (point-min))
4113   (cond
4114    ((eq gnus-auto-select-subject 'best)
4115     (gnus-summary-best-unread-subject))
4116    ((eq gnus-auto-select-subject 'unread)
4117     (gnus-summary-first-unread-subject))
4118    ((eq gnus-auto-select-subject 'unseen)
4119     (gnus-summary-first-unseen-subject))
4120    ((eq gnus-auto-select-subject 'unseen-or-unread)
4121     (gnus-summary-first-unseen-or-unread-subject))
4122    ((eq gnus-auto-select-subject 'first)
4123     ;; Do nothing.
4124     )
4125    ((functionp gnus-auto-select-subject)
4126     (funcall gnus-auto-select-subject))))
4127
4128 (defun gnus-summary-prepare ()
4129   "Generate the summary buffer."
4130   (interactive)
4131   (let ((inhibit-read-only t))
4132     (erase-buffer)
4133     (setq gnus-newsgroup-data nil
4134           gnus-newsgroup-data-reverse nil)
4135     (gnus-run-hooks 'gnus-summary-generate-hook)
4136     ;; Generate the buffer, either with threads or without.
4137     (when gnus-newsgroup-headers
4138       (gnus-summary-prepare-threads
4139        (if gnus-show-threads
4140            (gnus-sort-gathered-threads
4141             (funcall gnus-summary-thread-gathering-function
4142                      (gnus-sort-threads
4143                       (gnus-cut-threads (gnus-make-threads)))))
4144          ;; Unthreaded display.
4145          (gnus-sort-articles gnus-newsgroup-headers))))
4146     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4147     ;; Call hooks for modifying summary buffer.
4148     (goto-char (point-min))
4149     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4150
4151 (defsubst gnus-general-simplify-subject (subject)
4152   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4153   (setq subject
4154         (cond
4155          ;; Truncate the subject.
4156          (gnus-simplify-subject-functions
4157           (gnus-map-function gnus-simplify-subject-functions subject))
4158          ((numberp gnus-summary-gather-subject-limit)
4159           (setq subject (gnus-simplify-subject-re subject))
4160           (if (> (length subject) gnus-summary-gather-subject-limit)
4161               (substring subject 0 gnus-summary-gather-subject-limit)
4162             subject))
4163          ;; Fuzzily simplify it.
4164          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4165           (gnus-simplify-subject-fuzzy subject))
4166          ;; Just remove the leading "Re:".
4167          (t
4168           (gnus-simplify-subject-re subject))))
4169
4170   (if (and gnus-summary-gather-exclude-subject
4171            (string-match gnus-summary-gather-exclude-subject subject))
4172       nil                         ; This article shouldn't be gathered
4173     subject))
4174
4175 (defun gnus-summary-simplify-subject-query ()
4176   "Query where the respool algorithm would put this article."
4177   (interactive)
4178   (gnus-summary-select-article)
4179   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4180
4181 (defun gnus-gather-threads-by-subject (threads)
4182   "Gather threads by looking at Subject headers."
4183   (if (not gnus-summary-make-false-root)
4184       threads
4185     (let ((hashtb (gnus-make-hashtable 1024))
4186           (prev threads)
4187           (result threads)
4188           subject hthread whole-subject)
4189       (while threads
4190         (setq subject (gnus-general-simplify-subject
4191                        (setq whole-subject (mail-header-subject
4192                                             (caar threads)))))
4193         (when subject
4194           (if (setq hthread (gnus-gethash subject hashtb))
4195               (progn
4196                 ;; We enter a dummy root into the thread, if we
4197                 ;; haven't done that already.
4198                 (unless (stringp (caar hthread))
4199                   (setcar hthread (list whole-subject (car hthread))))
4200                 ;; We add this new gathered thread to this gathered
4201                 ;; thread.
4202                 (setcdr (car hthread)
4203                         (nconc (cdar hthread) (list (car threads))))
4204                 ;; Remove it from the list of threads.
4205                 (setcdr prev (cdr threads))
4206                 (setq threads prev))
4207             ;; Enter this thread into the hash table.
4208             (gnus-sethash subject
4209                           (if gnus-summary-make-false-root-always
4210                               (progn
4211                                 ;; If you want a dummy root above all
4212                                 ;; threads...
4213                                 (setcar threads (list whole-subject
4214                                                       (car threads)))
4215                                 threads)
4216                             threads)
4217                           hashtb)))
4218         (setq prev threads)
4219         (setq threads (cdr threads)))
4220       result)))
4221
4222 (defun gnus-gather-threads-by-references (threads)
4223   "Gather threads by looking at References headers."
4224   (let ((idhashtb (gnus-make-hashtable 1024))
4225         (thhashtb (gnus-make-hashtable 1024))
4226         (prev threads)
4227         (result threads)
4228         ids references id gthread gid entered ref)
4229     (while threads
4230       (when (setq references (mail-header-references (caar threads)))
4231         (setq id (mail-header-id (caar threads))
4232               ids (inline (gnus-split-references references))
4233               entered nil)
4234         (while (setq ref (pop ids))
4235           (setq ids (delete ref ids))
4236           (if (not (setq gid (gnus-gethash ref idhashtb)))
4237               (progn
4238                 (gnus-sethash ref id idhashtb)
4239                 (gnus-sethash id threads thhashtb))
4240             (setq gthread (gnus-gethash gid thhashtb))
4241             (unless entered
4242               ;; We enter a dummy root into the thread, if we
4243               ;; haven't done that already.
4244               (unless (stringp (caar gthread))
4245                 (setcar gthread (list (mail-header-subject (caar gthread))
4246                                       (car gthread))))
4247               ;; We add this new gathered thread to this gathered
4248               ;; thread.
4249               (setcdr (car gthread)
4250                       (nconc (cdar gthread) (list (car threads)))))
4251             ;; Add it into the thread hash table.
4252             (gnus-sethash id gthread thhashtb)
4253             (setq entered t)
4254             ;; Remove it from the list of threads.
4255             (setcdr prev (cdr threads))
4256             (setq threads prev))))
4257       (setq prev threads)
4258       (setq threads (cdr threads)))
4259     result))
4260
4261 (defun gnus-sort-gathered-threads (threads)
4262   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4263   (let ((result threads))
4264     (while threads
4265       (when (stringp (caar threads))
4266         (setcdr (car threads)
4267                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4268       (setq threads (cdr threads)))
4269     result))
4270
4271 (defun gnus-thread-loop-p (root thread)
4272   "Say whether ROOT is in THREAD."
4273   (let ((stack (list thread))
4274         (infloop 0)
4275         th)
4276     (while (setq thread (pop stack))
4277       (setq th (cdr thread))
4278       (while (and th
4279                   (not (eq (caar th) root)))
4280         (pop th))
4281       (if th
4282           ;; We have found a loop.
4283           (let (ref-dep)
4284             (setcdr thread (delq (car th) (cdr thread)))
4285             (if (boundp (setq ref-dep (intern "none"
4286                                               gnus-newsgroup-dependencies)))
4287                 (setcdr (symbol-value ref-dep)
4288                         (nconc (cdr (symbol-value ref-dep))
4289                                (list (car th))))
4290               (set ref-dep (list nil (car th))))
4291             (setq infloop 1
4292                   stack nil))
4293         ;; Push all the subthreads onto the stack.
4294         (push (cdr thread) stack)))
4295     infloop))
4296
4297 (defun gnus-make-threads ()
4298   "Go through the dependency hashtb and find the roots.  Return all threads."
4299   (let (threads)
4300     (while (catch 'infloop
4301              (mapatoms
4302               (lambda (refs)
4303                 ;; Deal with self-referencing References loops.
4304                 (when (and (car (symbol-value refs))
4305                            (not (zerop
4306                                  (apply
4307                                   '+
4308                                   (mapcar
4309                                    (lambda (thread)
4310                                      (gnus-thread-loop-p
4311                                       (car (symbol-value refs)) thread))
4312                                    (cdr (symbol-value refs)))))))
4313                   (setq threads nil)
4314                   (throw 'infloop t))
4315                 (unless (car (symbol-value refs))
4316                   ;; These threads do not refer back to any other
4317                   ;; articles, so they're roots.
4318                   (setq threads (append (cdr (symbol-value refs)) threads))))
4319               gnus-newsgroup-dependencies)))
4320     threads))
4321
4322 ;; Build the thread tree.
4323 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4324   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4325
4326 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4327 if it was already present.
4328
4329 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4330 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4331 Message-IDs will be renamed to a unique Message-ID before being
4332 entered.
4333
4334 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4335   (let* ((id (mail-header-id header))
4336          (id-dep (and id (intern id dependencies)))
4337          parent-id ref ref-dep ref-header replaced)
4338     ;; Enter this `header' in the `dependencies' table.
4339     (cond
4340      ((not id-dep)
4341       (setq header nil))
4342      ;; The first two cases do the normal part: enter a new `header'
4343      ;; in the `dependencies' table.
4344      ((not (boundp id-dep))
4345       (set id-dep (list header)))
4346      ((null (car (symbol-value id-dep)))
4347       (setcar (symbol-value id-dep) header))
4348
4349      ;; From here the `header' was already present in the
4350      ;; `dependencies' table.
4351      (force-new
4352       ;; Overrides an existing entry;
4353       ;; just set the header part of the entry.
4354       (setcar (symbol-value id-dep) header)
4355       (setq replaced t))
4356
4357      ;; Renames the existing `header' to a unique Message-ID.
4358      ((not gnus-summary-ignore-duplicates)
4359       ;; An article with this Message-ID has already been seen.
4360       ;; We rename the Message-ID.
4361       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4362            (list header))
4363       (mail-header-set-id header id))
4364
4365      ;; The last case ignores an existing entry, except it adds any
4366      ;; additional Xrefs (in case the two articles came from different
4367      ;; servers.
4368      ;; Also sets `header' to `nil' meaning that the `dependencies'
4369      ;; table was *not* modified.
4370      (t
4371       (mail-header-set-xref
4372        (car (symbol-value id-dep))
4373        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4374                    "")
4375                (or (mail-header-xref header) "")))
4376       (setq header nil)))
4377
4378     (when (and header (not replaced))
4379       ;; First check that we are not creating a References loop.
4380       (setq parent-id (gnus-parent-id (mail-header-references header)))
4381       (setq ref parent-id)
4382       (while (and ref
4383                   (setq ref-dep (intern-soft ref dependencies))
4384                   (boundp ref-dep)
4385                   (setq ref-header (car (symbol-value ref-dep))))
4386         (if (string= id ref)
4387             ;; Yuk!  This is a reference loop.  Make the article be a
4388             ;; root article.
4389             (progn
4390               (mail-header-set-references (car (symbol-value id-dep)) "none")
4391               (setq ref nil)
4392               (setq parent-id nil))
4393           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4394       (setq ref-dep (intern (or parent-id "none") dependencies))
4395       (if (boundp ref-dep)
4396           (setcdr (symbol-value ref-dep)
4397                   (nconc (cdr (symbol-value ref-dep))
4398                          (list (symbol-value id-dep))))
4399         (set ref-dep (list nil (symbol-value id-dep)))))
4400     header))
4401
4402 (defun gnus-extract-message-id-from-in-reply-to (string)
4403   (if (string-match "<[^>]+>" string)
4404       (substring string (match-beginning 0) (match-end 0))
4405     nil))
4406
4407 (defun gnus-build-sparse-threads ()
4408   (let ((headers gnus-newsgroup-headers)
4409         (mail-parse-charset gnus-newsgroup-charset)
4410         (gnus-summary-ignore-duplicates t)
4411         header references generation relations
4412         subject child end new-child date)
4413     ;; First we create an alist of generations/relations, where
4414     ;; generations is how much we trust the relation, and the relation
4415     ;; is parent/child.
4416     (gnus-message 7 "Making sparse threads...")
4417     (save-excursion
4418       (nnheader-set-temp-buffer " *gnus sparse threads*")
4419       (while (setq header (pop headers))
4420         (when (and (setq references (mail-header-references header))
4421                    (not (string= references "")))
4422           (insert references)
4423           (setq child (mail-header-id header)
4424                 subject (mail-header-subject header)
4425                 date (mail-header-date header)
4426                 generation 0)
4427           (while (search-backward ">" nil t)
4428             (setq end (1+ (point)))
4429             (when (search-backward "<" nil t)
4430               (setq new-child (buffer-substring (point) end))
4431               (push (list (incf generation)
4432                           child (setq child new-child)
4433                           subject date)
4434                     relations)))
4435           (when child
4436             (push (list (1+ generation) child nil subject) relations))
4437           (erase-buffer)))
4438       (kill-buffer (current-buffer)))
4439     ;; Sort over trustworthiness.
4440     (dolist (relation (sort relations 'car-less-than-car))
4441       (when (gnus-dependencies-add-header
4442              (make-full-mail-header
4443               gnus-reffed-article-number
4444               (nth 3 relation) "" (or (nth 4 relation) "")
4445               (nth 1 relation)
4446               (or (nth 2 relation) "") 0 0 "")
4447              gnus-newsgroup-dependencies nil)
4448         (push gnus-reffed-article-number gnus-newsgroup-limit)
4449         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4450         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4451               gnus-newsgroup-reads)
4452         (decf gnus-reffed-article-number)))
4453     (gnus-message 7 "Making sparse threads...done")))
4454
4455 (defun gnus-build-old-threads ()
4456   ;; Look at all the articles that refer back to old articles, and
4457   ;; fetch the headers for the articles that aren't there.  This will
4458   ;; build complete threads - if the roots haven't been expired by the
4459   ;; server, that is.
4460   (let ((mail-parse-charset gnus-newsgroup-charset)
4461         id heads)
4462     (mapatoms
4463      (lambda (refs)
4464        (when (not (car (symbol-value refs)))
4465          (setq heads (cdr (symbol-value refs)))
4466          (while heads
4467            (if (memq (mail-header-number (caar heads))
4468                      gnus-newsgroup-dormant)
4469                (setq heads (cdr heads))
4470              (setq id (symbol-name refs))
4471              (while (and (setq id (gnus-build-get-header id))
4472                          (not (car (gnus-id-to-thread id)))))
4473              (setq heads nil)))))
4474      gnus-newsgroup-dependencies)))
4475
4476 (defsubst gnus-remove-odd-characters (string)
4477   "Translate STRING into something that doesn't contain weird characters."
4478   (mm-subst-char-in-string
4479    ?\r ?\-
4480    (mm-subst-char-in-string ?\n ?\- string t) t))
4481
4482 ;; This function has to be called with point after the article number
4483 ;; on the beginning of the line.
4484 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4485   (let ((eol (point-at-eol))
4486         (buffer (current-buffer))
4487         header references in-reply-to)
4488
4489     ;; overview: [num subject from date id refs chars lines misc]
4490     (unwind-protect
4491         (let (x)
4492           (narrow-to-region (point) eol)
4493           (unless (eobp)
4494             (forward-char))
4495
4496           (setq header
4497                 (make-full-mail-header
4498                  number                 ; number
4499                  (condition-case ()     ; subject
4500                      (gnus-remove-odd-characters
4501                       (funcall gnus-decode-encoded-word-function
4502                                (setq x (nnheader-nov-field))))
4503                    (error x))
4504                  (condition-case ()     ; from
4505                      (gnus-remove-odd-characters
4506                       (funcall gnus-decode-encoded-address-function
4507                                (setq x (nnheader-nov-field))))
4508                    (error x))
4509                  (nnheader-nov-field)   ; date
4510                  (nnheader-nov-read-message-id number)  ; id
4511                  (setq references (nnheader-nov-field)) ; refs
4512                  (nnheader-nov-read-integer) ; chars
4513                  (nnheader-nov-read-integer) ; lines
4514                  (unless (eobp)
4515                    (if (looking-at "Xref: ")
4516                        (goto-char (match-end 0)))
4517                    (nnheader-nov-field)) ; Xref
4518                  (nnheader-nov-parse-extra)))) ; extra
4519
4520       (widen))
4521
4522     (when (and (string= references "")
4523                (setq in-reply-to (mail-header-extra header))
4524                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4525       (mail-header-set-references
4526        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4527
4528     (when gnus-alter-header-function
4529       (funcall gnus-alter-header-function header))
4530     (gnus-dependencies-add-header header dependencies force-new)))
4531
4532 (defun gnus-build-get-header (id)
4533   "Look through the buffer of NOV lines and find the header to ID.
4534 Enter this line into the dependencies hash table, and return
4535 the id of the parent article (if any)."
4536   (let ((deps gnus-newsgroup-dependencies)
4537         found header)
4538     (prog1
4539         (with-current-buffer nntp-server-buffer
4540           (let ((case-fold-search nil))
4541             (goto-char (point-min))
4542             (while (and (not found)
4543                         (search-forward id nil t))
4544               (beginning-of-line)
4545               (setq found (looking-at
4546                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4547                                    (regexp-quote id))))
4548               (or found (beginning-of-line 2)))
4549             (when found
4550               (beginning-of-line)
4551               (and
4552                (setq header (gnus-nov-parse-line
4553                              (read (current-buffer)) deps))
4554                (gnus-parent-id (mail-header-references header))))))
4555       (when header
4556         (let ((number (mail-header-number header)))
4557           (push number gnus-newsgroup-limit)
4558           (push header gnus-newsgroup-headers)
4559           (if (memq number gnus-newsgroup-unselected)
4560               (progn
4561                 (setq gnus-newsgroup-unreads
4562                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4563                                                number))
4564                 (setq gnus-newsgroup-unselected
4565                       (delq number gnus-newsgroup-unselected)))
4566             (push number gnus-newsgroup-ancient)))))))
4567
4568 (defun gnus-build-all-threads ()
4569   "Read all the headers."
4570   (let ((gnus-summary-ignore-duplicates t)
4571         (mail-parse-charset gnus-newsgroup-charset)
4572         (dependencies gnus-newsgroup-dependencies)
4573         header article)
4574     (with-current-buffer nntp-server-buffer
4575       (let ((case-fold-search nil))
4576         (goto-char (point-min))
4577         (while (not (eobp))
4578           (ignore-errors
4579             (setq article (read (current-buffer))
4580                   header (gnus-nov-parse-line article dependencies t)))
4581           (when header
4582             (with-current-buffer gnus-summary-buffer
4583               (push header gnus-newsgroup-headers)
4584               (if (memq (setq article (mail-header-number header))
4585                         gnus-newsgroup-unselected)
4586                   (progn
4587                     (setq gnus-newsgroup-unreads
4588                           (gnus-add-to-sorted-list
4589                            gnus-newsgroup-unreads article))
4590                     (setq gnus-newsgroup-unselected
4591                           (delq article gnus-newsgroup-unselected)))
4592                 (push article gnus-newsgroup-ancient)))
4593             (forward-line 1)))))))
4594
4595 (defun gnus-summary-update-article-line (article header)
4596   "Update the line for ARTICLE using HEADER."
4597   (let* ((id (mail-header-id header))
4598          (thread (gnus-id-to-thread id)))
4599     (unless thread
4600       (error "Article in no thread"))
4601     ;; Update the thread.
4602     (setcar thread header)
4603     (gnus-summary-goto-subject article)
4604     (let* ((datal (gnus-data-find-list article))
4605            (data (car datal))
4606            (inhibit-read-only t)
4607            (level (gnus-summary-thread-level)))
4608       (gnus-delete-line)
4609       (let ((inserted (- (point)
4610                          (progn
4611                            (gnus-summary-insert-line
4612                             header level nil
4613                             (memq article gnus-newsgroup-undownloaded)
4614                             (gnus-article-mark article)
4615                             (memq article gnus-newsgroup-replied)
4616                             (memq article gnus-newsgroup-expirable)
4617                             ;; Only insert the Subject string when it's different
4618                             ;; from the previous Subject string.
4619                             (if (and
4620                                  gnus-show-threads
4621                                  (gnus-subject-equal
4622                                   (condition-case ()
4623                                       (mail-header-subject
4624                                        (gnus-data-header
4625                                         (cadr
4626                                          (gnus-data-find-list
4627                                           article
4628                                           (gnus-data-list t)))))
4629                                     ;; Error on the side of excessive subjects.
4630                                     (error ""))
4631                                   (mail-header-subject header)))
4632                                 ""
4633                               (mail-header-subject header))
4634                             nil (cdr (assq article gnus-newsgroup-scored))
4635                             (memq article gnus-newsgroup-processable))
4636                            (point)))))
4637         (when (cdr datal)
4638           (gnus-data-update-list
4639            (cdr datal)
4640            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4641
4642 (defun gnus-summary-update-article (article &optional iheader)
4643   "Update ARTICLE in the summary buffer."
4644   (set-buffer gnus-summary-buffer)
4645   (let* ((header (gnus-summary-article-header article))
4646          (id (mail-header-id header))
4647          (data (gnus-data-find article))
4648          (thread (gnus-id-to-thread id))
4649          (references (mail-header-references header))
4650          (parent
4651           (gnus-id-to-thread
4652            (or (gnus-parent-id
4653                 (when (and references
4654                            (not (equal "" references)))
4655                   references))
4656                "none")))
4657          (inhibit-read-only t)
4658          (old (car thread)))
4659     (when thread
4660       (unless iheader
4661         (setcar thread nil)
4662         (when parent
4663           (delq thread parent)))
4664       (if (gnus-summary-insert-subject id header)
4665           ;; Set the (possibly) new article number in the data structure.
4666           (gnus-data-set-number data (gnus-id-to-article id))
4667         (setcar thread old)
4668         nil))))
4669
4670 (defun gnus-rebuild-thread (id &optional line)
4671   "Rebuild the thread containing ID.
4672 If LINE, insert the rebuilt thread starting on line LINE."
4673   (let ((inhibit-read-only t)
4674         old-pos current thread data)
4675     (if (not gnus-show-threads)
4676         (setq thread (list (car (gnus-id-to-thread id))))
4677       ;; Get the thread this article is part of.
4678       (setq thread (gnus-remove-thread id)))
4679     (setq old-pos (point-at-bol))
4680     (setq current (save-excursion
4681                     (and (re-search-backward "[\r\n]" nil t)
4682                          (gnus-summary-article-number))))
4683     ;; If this is a gathered thread, we have to go some re-gathering.
4684     (when (stringp (car thread))
4685       (let ((subject (car thread))
4686             roots thr)
4687         (setq thread (cdr thread))
4688         (while thread
4689           (unless (memq (setq thr (gnus-id-to-thread
4690                                    (gnus-root-id
4691                                     (mail-header-id (caar thread)))))
4692                         roots)
4693             (push thr roots))
4694           (setq thread (cdr thread)))
4695         ;; We now have all (unique) roots.
4696         (if (= (length roots) 1)
4697             ;; All the loose roots are now one solid root.
4698             (setq thread (car roots))
4699           (setq thread (cons subject (gnus-sort-threads roots))))))
4700     (let (threads)
4701       ;; We then insert this thread into the summary buffer.
4702       (when line
4703         (goto-char (point-min))
4704         (forward-line (1- line)))
4705       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4706         (if gnus-show-threads
4707             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4708           (gnus-summary-prepare-unthreaded thread))
4709         (setq data (nreverse gnus-newsgroup-data))
4710         (setq threads gnus-newsgroup-threads))
4711       ;; We splice the new data into the data structure.
4712       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4713       ;;!!! then we want to insert at the beginning of the buffer.
4714       ;;!!! That happens to be true with Gnus now, but that may
4715       ;;!!! change in the future.  Perhaps.
4716       (gnus-data-enter-list
4717        (if line nil current) data (- (point) old-pos))
4718       (setq gnus-newsgroup-threads
4719             (nconc threads gnus-newsgroup-threads))
4720       (gnus-data-compute-positions))))
4721
4722 (defun gnus-number-to-header (number)
4723   "Return the header for article NUMBER."
4724   (let ((headers gnus-newsgroup-headers))
4725     (while (and headers
4726                 (not (= number (mail-header-number (car headers)))))
4727       (pop headers))
4728     (when headers
4729       (car headers))))
4730
4731 (defun gnus-parent-headers (in-headers &optional generation)
4732   "Return the headers of the GENERATIONth parent of HEADERS."
4733   (unless generation
4734     (setq generation 1))
4735   (let ((parent t)
4736         (headers in-headers)
4737         references)
4738     (while (and parent
4739                 (not (zerop generation))
4740                 (setq references (mail-header-references headers)))
4741       (setq headers (if (and references
4742                              (setq parent (gnus-parent-id references)))
4743                         (car (gnus-id-to-thread parent))
4744                       nil))
4745       (decf generation))
4746     (and (not (eq headers in-headers))
4747          headers)))
4748
4749 (defun gnus-id-to-thread (id)
4750   "Return the (sub-)thread where ID appears."
4751   (gnus-gethash id gnus-newsgroup-dependencies))
4752
4753 (defun gnus-id-to-article (id)
4754   "Return the article number of ID."
4755   (let ((thread (gnus-id-to-thread id)))
4756     (when (and thread
4757                (car thread))
4758       (mail-header-number (car thread)))))
4759
4760 (defun gnus-id-to-header (id)
4761   "Return the article headers of ID."
4762   (car (gnus-id-to-thread id)))
4763
4764 (defun gnus-article-displayed-root-p (article)
4765   "Say whether ARTICLE is a root(ish) article."
4766   (let ((level (gnus-summary-thread-level article))
4767         (refs (mail-header-references  (gnus-summary-article-header article)))
4768         particle)
4769     (cond
4770      ((null level) nil)
4771      ((zerop level) t)
4772      ((null refs) t)
4773      ((null (gnus-parent-id refs)) t)
4774      ((and (= 1 level)
4775            (null (setq particle (gnus-id-to-article
4776                                  (gnus-parent-id refs))))
4777            (null (gnus-summary-thread-level particle)))))))
4778
4779 (defun gnus-root-id (id)
4780   "Return the id of the root of the thread where ID appears."
4781   (let (last-id prev)
4782     (while (and id (setq prev (car (gnus-id-to-thread id))))
4783       (setq last-id id
4784             id (gnus-parent-id (mail-header-references prev))))
4785     last-id))
4786
4787 (defun gnus-articles-in-thread (thread)
4788   "Return the list of articles in THREAD."
4789   (cons (mail-header-number (car thread))
4790         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4791
4792 (defun gnus-remove-thread (id &optional dont-remove)
4793   "Remove the thread that has ID in it."
4794   (let (headers thread last-id)
4795     ;; First go up in this thread until we find the root.
4796     (setq last-id (gnus-root-id id)
4797           headers (message-flatten-list (gnus-id-to-thread last-id)))
4798     ;; We have now found the real root of this thread.  It might have
4799     ;; been gathered into some loose thread, so we have to search
4800     ;; through the threads to find the thread we wanted.
4801     (let ((threads gnus-newsgroup-threads)
4802           sub)
4803       (while threads
4804         (setq sub (car threads))
4805         (if (stringp (car sub))
4806             ;; This is a gathered thread, so we look at the roots
4807             ;; below it to find whether this article is in this
4808             ;; gathered root.
4809             (progn
4810               (setq sub (cdr sub))
4811               (while sub
4812                 (when (member (caar sub) headers)
4813                   (setq thread (car threads)
4814                         threads nil
4815                         sub nil))
4816                 (setq sub (cdr sub))))
4817           ;; It's an ordinary thread, so we check it.
4818           (when (eq (car sub) (car headers))
4819             (setq thread sub
4820                   threads nil)))
4821         (setq threads (cdr threads)))
4822       ;; If this article is in no thread, then it's a root.
4823       (if thread
4824           (unless dont-remove
4825             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4826         (setq thread (gnus-id-to-thread last-id)))
4827       (when thread
4828         (prog1
4829             thread                      ; We return this thread.
4830           (unless dont-remove
4831             (if (stringp (car thread))
4832                 (progn
4833                   ;; If we use dummy roots, then we have to remove the
4834                   ;; dummy root as well.
4835                   (when (eq gnus-summary-make-false-root 'dummy)
4836                     ;; We go to the dummy root by going to
4837                     ;; the first sub-"thread", and then one line up.
4838                     (gnus-summary-goto-article
4839                      (mail-header-number (caadr thread)))
4840                     (forward-line -1)
4841                     (gnus-delete-line)
4842                     (gnus-data-compute-positions))
4843                   (setq thread (cdr thread))
4844                   (while thread
4845                     (gnus-remove-thread-1 (car thread))
4846                     (setq thread (cdr thread))))
4847               (gnus-remove-thread-1 thread))))))))
4848
4849 (defun gnus-remove-thread-1 (thread)
4850   "Remove the thread THREAD recursively."
4851   (let ((number (mail-header-number (pop thread)))
4852         d)
4853     (setq thread (reverse thread))
4854     (while thread
4855       (gnus-remove-thread-1 (pop thread)))
4856     (when (setq d (gnus-data-find number))
4857       (goto-char (gnus-data-pos d))
4858       (gnus-summary-show-thread)
4859       (gnus-data-remove
4860        number
4861        (- (point-at-bol)
4862           (prog1
4863               (1+ (point-at-eol))
4864             (gnus-delete-line)))))))
4865
4866 (defun gnus-sort-threads-recursive (threads func)
4867   (sort (mapcar (lambda (thread)
4868                   (cons (car thread)
4869                         (and (cdr thread)
4870                              (gnus-sort-threads-recursive (cdr thread) func))))
4871                 threads) func))
4872
4873 (defun gnus-sort-threads-loop (threads func)
4874   (let* ((superthread (cons nil threads))
4875          (stack (list (cons superthread threads)))
4876          remaining-threads thread)
4877     (while stack
4878       (setq remaining-threads (cdr (car stack)))
4879       (if remaining-threads
4880           (progn (setq thread (car remaining-threads))
4881                  (setcdr (car stack) (cdr remaining-threads))
4882                  (if (cdr thread)
4883                      (push (cons thread (cdr thread)) stack)))
4884         (setq thread (caar stack))
4885         (setcdr thread (sort (cdr thread) func))
4886         (pop stack)))
4887     (cdr superthread)))
4888
4889 (defun gnus-sort-threads (threads)
4890   "Sort THREADS."
4891   (if (not gnus-thread-sort-functions)
4892       threads
4893     (gnus-message 8 "Sorting threads...")
4894     (prog1
4895         (condition-case nil
4896             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4897               (gnus-sort-threads-recursive
4898                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4899           ;; Even after binding max-lisp-eval-depth, the recursive
4900           ;; sorter might fail for very long threads.  In that case,
4901           ;; try using a (less well-tested) non-recursive sorter.
4902           (error (gnus-message 9 "Sorting threads with loop...")
4903                  (gnus-sort-threads-loop
4904                   threads (gnus-make-sort-function
4905                            gnus-thread-sort-functions))))
4906       (gnus-message 8 "Sorting threads...done"))))
4907
4908 (defun gnus-sort-articles (articles)
4909   "Sort ARTICLES."
4910   (when gnus-article-sort-functions
4911     (gnus-message 7 "Sorting articles...")
4912     (prog1
4913         (setq gnus-newsgroup-headers
4914               (sort articles (gnus-make-sort-function
4915                               gnus-article-sort-functions)))
4916       (gnus-message 7 "Sorting articles...done"))))
4917
4918 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4919 (defmacro gnus-thread-header (thread)
4920   "Return header of first article in THREAD.
4921 Note that THREAD must never, ever be anything else than a variable -
4922 using some other form will lead to serious barfage."
4923   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4924   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4925   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4926         (vector thread) 2))
4927
4928 (defsubst gnus-article-sort-by-number (h1 h2)
4929   "Sort articles by article number."
4930   (< (mail-header-number h1)
4931      (mail-header-number h2)))
4932
4933 (defun gnus-thread-sort-by-number (h1 h2)
4934   "Sort threads by root article number."
4935   (gnus-article-sort-by-number
4936    (gnus-thread-header h1) (gnus-thread-header h2)))
4937
4938 (defsubst gnus-article-sort-by-random (h1 h2)
4939   "Sort articles randomly."
4940   (zerop (random 2)))
4941
4942 (defun gnus-thread-sort-by-random (h1 h2)
4943   "Sort threads randomly."
4944   (gnus-article-sort-by-random
4945    (gnus-thread-header h1) (gnus-thread-header h2)))
4946
4947 (defsubst gnus-article-sort-by-lines (h1 h2)
4948   "Sort articles by article Lines header."
4949   (< (mail-header-lines h1)
4950      (mail-header-lines h2)))
4951
4952 (defun gnus-thread-sort-by-lines (h1 h2)
4953   "Sort threads by root article Lines header."
4954   (gnus-article-sort-by-lines
4955    (gnus-thread-header h1) (gnus-thread-header h2)))
4956
4957 (defsubst gnus-article-sort-by-chars (h1 h2)
4958   "Sort articles by octet length."
4959   (< (mail-header-chars h1)
4960      (mail-header-chars h2)))
4961
4962 (defun gnus-thread-sort-by-chars (h1 h2)
4963   "Sort threads by root article octet length."
4964   (gnus-article-sort-by-chars
4965    (gnus-thread-header h1) (gnus-thread-header h2)))
4966
4967 (defsubst gnus-article-sort-by-author (h1 h2)
4968   "Sort articles by root author."
4969   (gnus-string<
4970    (let ((extract (funcall
4971                    gnus-extract-address-components
4972                    (mail-header-from h1))))
4973      (or (car extract) (cadr extract) ""))
4974    (let ((extract (funcall
4975                    gnus-extract-address-components
4976                    (mail-header-from h2))))
4977      (or (car extract) (cadr extract) ""))))
4978
4979 (defun gnus-thread-sort-by-author (h1 h2)
4980   "Sort threads by root author."
4981   (gnus-article-sort-by-author
4982    (gnus-thread-header h1)  (gnus-thread-header h2)))
4983
4984 (defsubst gnus-article-sort-by-recipient (h1 h2)
4985   "Sort articles by recipient."
4986   (gnus-string<
4987    (let ((extract (funcall
4988                    gnus-extract-address-components
4989                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4990      (or (car extract) (cadr extract)))
4991    (let ((extract (funcall
4992                    gnus-extract-address-components
4993                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4994      (or (car extract) (cadr extract)))))
4995
4996 (defun gnus-thread-sort-by-recipient (h1 h2)
4997   "Sort threads by root recipient."
4998   (gnus-article-sort-by-recipient
4999    (gnus-thread-header h1) (gnus-thread-header h2)))
5000
5001 (defsubst gnus-article-sort-by-subject (h1 h2)
5002   "Sort articles by root subject."
5003   (gnus-string<
5004    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5005    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5006
5007 (defun gnus-thread-sort-by-subject (h1 h2)
5008   "Sort threads by root subject."
5009   (gnus-article-sort-by-subject
5010    (gnus-thread-header h1) (gnus-thread-header h2)))
5011
5012 (defsubst gnus-article-sort-by-date (h1 h2)
5013   "Sort articles by root article date."
5014   (time-less-p
5015    (gnus-date-get-time (mail-header-date h1))
5016    (gnus-date-get-time (mail-header-date h2))))
5017
5018 (defun gnus-thread-sort-by-date (h1 h2)
5019   "Sort threads by root article date."
5020   (gnus-article-sort-by-date
5021    (gnus-thread-header h1) (gnus-thread-header h2)))
5022
5023 (defsubst gnus-article-sort-by-score (h1 h2)
5024   "Sort articles by root article score.
5025 Unscored articles will be counted as having a score of zero."
5026   (> (or (cdr (assq (mail-header-number h1)
5027                     gnus-newsgroup-scored))
5028          gnus-summary-default-score 0)
5029      (or (cdr (assq (mail-header-number h2)
5030                     gnus-newsgroup-scored))
5031          gnus-summary-default-score 0)))
5032
5033 (defun gnus-thread-sort-by-score (h1 h2)
5034   "Sort threads by root article score."
5035   (gnus-article-sort-by-score
5036    (gnus-thread-header h1) (gnus-thread-header h2)))
5037
5038 (defun gnus-thread-sort-by-total-score (h1 h2)
5039   "Sort threads by the sum of all scores in the thread.
5040 Unscored articles will be counted as having a score of zero."
5041   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5042
5043 (defun gnus-thread-total-score (thread)
5044   ;; This function find the total score of THREAD.
5045   (cond
5046    ((null thread)
5047     0)
5048    ((consp thread)
5049     (if (stringp (car thread))
5050         (apply gnus-thread-score-function 0
5051                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5052       (gnus-thread-total-score-1 thread)))
5053    (t
5054     (gnus-thread-total-score-1 (list thread)))))
5055
5056 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5057   "Sort articles by number."
5058   (gnus-article-sort-by-number h1 h2))
5059
5060 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5061   "Sort threads such that the thread with the most recently arrived article comes first."
5062   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5063
5064 (defun gnus-thread-highest-number (thread)
5065   "Return the highest article number in THREAD."
5066   (apply 'max (mapcar (lambda (header)
5067                         (mail-header-number header))
5068                       (message-flatten-list thread))))
5069
5070 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5071   "Sort articles by number."
5072   (gnus-article-sort-by-date h1 h2))
5073
5074 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5075   "Sort threads such that the thread with the most recently dated article comes first."
5076   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5077
5078 ; Since this is called not only to sort the top-level threads, but
5079 ; also in recursive sorts to order the articles within a thread, each
5080 ; article will be processed many times.  Thus it speeds things up
5081 ; quite a bit to use gnus-date-get-time, which caches the time value.
5082 (defun gnus-thread-latest-date (thread)
5083   "Return the highest article date in THREAD."
5084   (apply 'max
5085          (mapcar (lambda (header) (gnus-float-time
5086                                    (gnus-date-get-time
5087                                     (mail-header-date header))))
5088                  (message-flatten-list thread))))
5089
5090 (defun gnus-thread-total-score-1 (root)
5091   ;; This function find the total score of the thread below ROOT.
5092   (setq root (car root))
5093   (apply gnus-thread-score-function
5094          (or (append
5095               (mapcar 'gnus-thread-total-score
5096                       (cdr (gnus-id-to-thread (mail-header-id root))))
5097               (when (> (mail-header-number root) 0)
5098                 (list (or (cdr (assq (mail-header-number root)
5099                                      gnus-newsgroup-scored))
5100                           gnus-summary-default-score 0))))
5101              (list gnus-summary-default-score)
5102              '(0))))
5103
5104 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5105 (defvar gnus-tmp-prev-subject nil)
5106 (defvar gnus-tmp-false-parent nil)
5107 (defvar gnus-tmp-root-expunged nil)
5108 (defvar gnus-tmp-dummy-line nil)
5109
5110 (defun gnus-extra-header (type &optional header)
5111   "Return the extra header of TYPE."
5112   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5113       ""))
5114
5115 (defvar gnus-tmp-thread-tree-header-string "")
5116
5117 (defcustom gnus-sum-thread-tree-root "> "
5118   "With %B spec, used for the root of a thread.
5119 If nil, use subject instead."
5120   :version "22.1"
5121   :type '(radio (const :format "%v  " nil) string)
5122   :group 'gnus-thread)
5123
5124 (defcustom gnus-sum-thread-tree-false-root "> "
5125   "With %B spec, used for a false root of a thread.
5126 If nil, use subject instead."
5127   :version "22.1"
5128   :type '(radio (const :format "%v  " nil) string)
5129   :group 'gnus-thread)
5130
5131 (defcustom gnus-sum-thread-tree-single-indent ""
5132   "With %B spec, used for a thread with just one message.
5133 If nil, use subject instead."
5134   :version "22.1"
5135   :type '(radio (const :format "%v  " nil) string)
5136   :group 'gnus-thread)
5137
5138 (defcustom gnus-sum-thread-tree-vertical "| "
5139   "With %B spec, used for drawing a vertical line."
5140   :version "22.1"
5141   :type 'string
5142   :group 'gnus-thread)
5143
5144 (defcustom gnus-sum-thread-tree-indent "  "
5145   "With %B spec, used for indenting."
5146   :version "22.1"
5147   :type 'string
5148   :group 'gnus-thread)
5149
5150 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5151   "With %B spec, used for a leaf with brothers."
5152   :version "22.1"
5153   :type 'string
5154   :group 'gnus-thread)
5155
5156 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5157   "With %B spec, used for a leaf without brothers."
5158   :version "22.1"
5159   :type 'string
5160   :group 'gnus-thread)
5161
5162 (defcustom gnus-summary-display-while-building nil
5163   "If non-nil, show and update the summary buffer as it's being built.
5164 If the value is t, update the buffer after every line is inserted.  If
5165 the value is an integer (N), update the display every N lines."
5166   :version "22.1"
5167   :group 'gnus-thread
5168   :type '(choice (const :tag "off" nil)
5169                  number
5170                  (const :tag "frequently" t)))
5171
5172 (defun gnus-summary-prepare-threads (threads)
5173   "Prepare summary buffer from THREADS and indentation LEVEL.
5174 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5175 or a straight list of headers."
5176   (gnus-message 7 "Generating summary...")
5177
5178   (setq gnus-newsgroup-threads threads)
5179   (beginning-of-line)
5180
5181   (let ((gnus-tmp-level 0)
5182         (default-score (or gnus-summary-default-score 0))
5183         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5184         (building-line-count gnus-summary-display-while-building)
5185         (building-count (integerp gnus-summary-display-while-building))
5186         thread number subject stack state gnus-tmp-gathered beg-match
5187         new-roots gnus-tmp-new-adopts thread-end simp-subject
5188         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5189         gnus-tmp-replied gnus-tmp-subject-or-nil
5190         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5191         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5192         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5193         tree-stack)
5194
5195     (setq gnus-tmp-prev-subject nil
5196           gnus-tmp-thread-tree-header-string "")
5197
5198     (if (vectorp (car threads))
5199         ;; If this is a straight (sic) list of headers, then a
5200         ;; threaded summary display isn't required, so we just create
5201         ;; an unthreaded one.
5202         (gnus-summary-prepare-unthreaded threads)
5203
5204       ;; Do the threaded display.
5205
5206       (if gnus-summary-display-while-building
5207           (switch-to-buffer (buffer-name)))
5208       (while (or threads stack gnus-tmp-new-adopts new-roots)
5209
5210         (if (and (= gnus-tmp-level 0)
5211                  (or (not stack)
5212                      (= (caar stack) 0))
5213                  (not gnus-tmp-false-parent)
5214                  (or gnus-tmp-new-adopts new-roots))
5215             (if gnus-tmp-new-adopts
5216                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5217                       thread (list (car gnus-tmp-new-adopts))
5218                       gnus-tmp-header (caar thread)
5219                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5220               (when new-roots
5221                 (setq thread (list (car new-roots))
5222                       gnus-tmp-header (caar thread)
5223                       new-roots (cdr new-roots))))
5224
5225           (if threads
5226               ;; If there are some threads, we do them before the
5227               ;; threads on the stack.
5228               (setq thread threads
5229                     gnus-tmp-header (caar thread))
5230             ;; There were no current threads, so we pop something off
5231             ;; the stack.
5232             (setq state (car stack)
5233                   gnus-tmp-level (car state)
5234                   tree-stack (cadr state)
5235                   thread (caddr state)
5236                   stack (cdr stack)
5237                   gnus-tmp-header (caar thread))))
5238
5239         (setq gnus-tmp-false-parent nil)
5240         (setq gnus-tmp-root-expunged nil)
5241         (setq thread-end nil)
5242
5243         (if (stringp gnus-tmp-header)
5244             ;; The header is a dummy root.
5245             (cond
5246              ((eq gnus-summary-make-false-root 'adopt)
5247               ;; We let the first article adopt the rest.
5248               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5249                                                (cddar thread)))
5250               (setq gnus-tmp-gathered
5251                     (nconc (mapcar
5252                             (lambda (h) (mail-header-number (car h)))
5253                             (cddar thread))
5254                            gnus-tmp-gathered))
5255               (setq thread (cons (list (caar thread)
5256                                        (cadar thread))
5257                                  (cdr thread)))
5258               (setq gnus-tmp-level -1
5259                     gnus-tmp-false-parent t))
5260              ((eq gnus-summary-make-false-root 'empty)
5261               ;; We print adopted articles with empty subject fields.
5262               (setq gnus-tmp-gathered
5263                     (nconc (mapcar
5264                             (lambda (h) (mail-header-number (car h)))
5265                             (cddar thread))
5266                            gnus-tmp-gathered))
5267               (setq gnus-tmp-level -1))
5268              ((eq gnus-summary-make-false-root 'dummy)
5269               ;; We remember that we probably want to output a dummy
5270               ;; root.
5271               (setq gnus-tmp-dummy-line gnus-tmp-header)
5272               (setq gnus-tmp-prev-subject gnus-tmp-header))
5273              (t
5274               ;; We do not make a root for the gathered
5275               ;; sub-threads at all.
5276               (setq gnus-tmp-level -1)))
5277
5278           (setq number (mail-header-number gnus-tmp-header)
5279                 subject (mail-header-subject gnus-tmp-header)
5280                 simp-subject (gnus-simplify-subject-fully subject))
5281
5282           (cond
5283            ;; If the thread has changed subject, we might want to make
5284            ;; this subthread into a root.
5285            ((and (null gnus-thread-ignore-subject)
5286                  (not (zerop gnus-tmp-level))
5287                  gnus-tmp-prev-subject
5288                  (not (string= gnus-tmp-prev-subject simp-subject)))
5289             (setq new-roots (nconc new-roots (list (car thread)))
5290                   thread-end t
5291                   gnus-tmp-header nil))
5292            ;; If the article lies outside the current limit,
5293            ;; then we do not display it.
5294            ((not (memq number gnus-newsgroup-limit))
5295             (setq gnus-tmp-gathered
5296                   (nconc (mapcar
5297                           (lambda (h) (mail-header-number (car h)))
5298                           (cdar thread))
5299                          gnus-tmp-gathered))
5300             (setq gnus-tmp-new-adopts (if (cdar thread)
5301                                           (append gnus-tmp-new-adopts
5302                                                   (cdar thread))
5303                                         gnus-tmp-new-adopts)
5304                   thread-end t
5305                   gnus-tmp-header nil)
5306             (when (zerop gnus-tmp-level)
5307               (setq gnus-tmp-root-expunged t)))
5308            ;; Perhaps this article is to be marked as read?
5309            ((and gnus-summary-mark-below
5310                  (< (or (cdr (assq number gnus-newsgroup-scored))
5311                         default-score)
5312                     gnus-summary-mark-below)
5313                  ;; Don't touch sparse articles.
5314                  (not (gnus-summary-article-sparse-p number))
5315                  (not (gnus-summary-article-ancient-p number)))
5316             (setq gnus-newsgroup-unreads
5317                   (delq number gnus-newsgroup-unreads))
5318             (if gnus-newsgroup-auto-expire
5319                 (setq gnus-newsgroup-expirable
5320                       (gnus-add-to-sorted-list
5321                        gnus-newsgroup-expirable number))
5322               (push (cons number gnus-low-score-mark)
5323                     gnus-newsgroup-reads))))
5324
5325           (when gnus-tmp-header
5326             ;; We may have an old dummy line to output before this
5327             ;; article.
5328             (when (and gnus-tmp-dummy-line
5329                        (gnus-subject-equal
5330                         gnus-tmp-dummy-line
5331                         (mail-header-subject gnus-tmp-header)))
5332               (gnus-summary-insert-dummy-line
5333                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5334               (setq gnus-tmp-dummy-line nil))
5335
5336             ;; Compute the mark.
5337             (setq gnus-tmp-unread (gnus-article-mark number))
5338
5339             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5340                                   gnus-tmp-header gnus-tmp-level)
5341                   gnus-newsgroup-data)
5342
5343             ;; Actually insert the line.
5344             (setq
5345              gnus-tmp-subject-or-nil
5346              (cond
5347               ((and gnus-thread-ignore-subject
5348                     gnus-tmp-prev-subject
5349                     (not (string= gnus-tmp-prev-subject simp-subject)))
5350                subject)
5351               ((zerop gnus-tmp-level)
5352                (if (and (eq gnus-summary-make-false-root 'empty)
5353                         (memq number gnus-tmp-gathered)
5354                         gnus-tmp-prev-subject
5355                         (string= gnus-tmp-prev-subject simp-subject))
5356                    gnus-summary-same-subject
5357                  subject))
5358               (t gnus-summary-same-subject)))
5359             (if (and (eq gnus-summary-make-false-root 'adopt)
5360                      (= gnus-tmp-level 1)
5361                      (memq number gnus-tmp-gathered))
5362                 (setq gnus-tmp-opening-bracket ?\<
5363                       gnus-tmp-closing-bracket ?\>)
5364               (setq gnus-tmp-opening-bracket ?\[
5365                     gnus-tmp-closing-bracket ?\]))
5366             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5367                 (gnus-make-thread-indent-array
5368                  (max (* 2 (length gnus-thread-indent-array))
5369                       gnus-tmp-level)))
5370             (setq
5371              gnus-tmp-indentation
5372              (aref gnus-thread-indent-array gnus-tmp-level)
5373              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5374              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5375                                 gnus-summary-default-score 0)
5376              gnus-tmp-score-char
5377              (if (or (null gnus-summary-default-score)
5378                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5379                          gnus-summary-zcore-fuzz))
5380                  ?                      ;Whitespace
5381                (if (< gnus-tmp-score gnus-summary-default-score)
5382                    gnus-score-below-mark gnus-score-over-mark))
5383              gnus-tmp-replied
5384              (cond ((memq number gnus-newsgroup-processable)
5385                     gnus-process-mark)
5386                    ((memq number gnus-newsgroup-cached)
5387                     gnus-cached-mark)
5388                    ((memq number gnus-newsgroup-replied)
5389                     gnus-replied-mark)
5390                    ((memq number gnus-newsgroup-forwarded)
5391                     gnus-forwarded-mark)
5392                    ((memq number gnus-newsgroup-saved)
5393                     gnus-saved-mark)
5394                    ((memq number gnus-newsgroup-unseen)
5395                     gnus-unseen-mark)
5396                    (t gnus-no-mark))
5397              gnus-tmp-downloaded
5398              (cond ((memq number gnus-newsgroup-undownloaded)
5399                     gnus-undownloaded-mark)
5400                    (gnus-newsgroup-agentized
5401                     gnus-downloaded-mark)
5402                    (t
5403                     gnus-no-mark))
5404              gnus-tmp-from (mail-header-from gnus-tmp-header)
5405              gnus-tmp-name
5406              (cond
5407               ((string-match "<[^>]+> *$" gnus-tmp-from)
5408                (setq beg-match (match-beginning 0))
5409                (or (and (string-match "^\".+\"" gnus-tmp-from)
5410                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5411                    (substring gnus-tmp-from 0 beg-match)))
5412               ((string-match "(.+)" gnus-tmp-from)
5413                (substring gnus-tmp-from
5414                           (1+ (match-beginning 0)) (1- (match-end 0))))
5415               (t gnus-tmp-from))
5416
5417              ;; Do the %B string
5418              gnus-tmp-thread-tree-header-string
5419              (cond
5420               ((not gnus-show-threads) "")
5421               ((zerop gnus-tmp-level)
5422                (cond ((cdar thread)
5423                       (or gnus-sum-thread-tree-root subject))
5424                      (gnus-tmp-new-adopts
5425                       (or gnus-sum-thread-tree-false-root subject))
5426                      (t
5427                       (or gnus-sum-thread-tree-single-indent subject))))
5428               (t
5429                (concat (apply 'concat
5430                               (mapcar (lambda (item)
5431                                         (if (= item 1)
5432                                             gnus-sum-thread-tree-vertical
5433                                           gnus-sum-thread-tree-indent))
5434                                       (cdr (reverse tree-stack))))
5435                        (if (nth 1 thread)
5436                            gnus-sum-thread-tree-leaf-with-other
5437                          gnus-sum-thread-tree-single-leaf)))))
5438             (when (string= gnus-tmp-name "")
5439               (setq gnus-tmp-name gnus-tmp-from))
5440             (unless (numberp gnus-tmp-lines)
5441               (setq gnus-tmp-lines -1))
5442             (if (= gnus-tmp-lines -1)
5443                 (setq gnus-tmp-lines "?")
5444               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5445             (gnus-put-text-property
5446              (point)
5447              (progn (eval gnus-summary-line-format-spec) (point))
5448              'gnus-number number)
5449             (when gnus-visual-p
5450               (forward-line -1)
5451               (gnus-summary-highlight-line)
5452               (when gnus-summary-update-hook
5453                 (gnus-run-hooks 'gnus-summary-update-hook))
5454               (forward-line 1))
5455
5456             (setq gnus-tmp-prev-subject simp-subject)))
5457
5458         (when (nth 1 thread)
5459           (push (list (max 0 gnus-tmp-level)
5460                       (copy-sequence tree-stack)
5461                       (nthcdr 1 thread))
5462                 stack))
5463         (push (if (nth 1 thread) 1 0) tree-stack)
5464         (incf gnus-tmp-level)
5465         (setq threads (if thread-end nil (cdar thread)))
5466         (if gnus-summary-display-while-building
5467             (if building-count
5468                 (progn
5469                   ;; use a set frequency
5470                   (setq building-line-count (1- building-line-count))
5471                   (when (= building-line-count 0)
5472                     (sit-for 0)
5473                     (setq building-line-count
5474                           gnus-summary-display-while-building)))
5475               ;; always
5476               (sit-for 0)))
5477         (unless threads
5478           (setq gnus-tmp-level 0)))))
5479   (gnus-message 7 "Generating summary...done"))
5480
5481 (defun gnus-summary-prepare-unthreaded (headers)
5482   "Generate an unthreaded summary buffer based on HEADERS."
5483   (let (header number mark)
5484
5485     (beginning-of-line)
5486
5487     (while headers
5488       ;; We may have to root out some bad articles...
5489       (when (memq (setq number (mail-header-number
5490                                 (setq header (pop headers))))
5491                   gnus-newsgroup-limit)
5492         ;; Mark article as read when it has a low score.
5493         (when (and gnus-summary-mark-below
5494                    (< (or (cdr (assq number gnus-newsgroup-scored))
5495                           gnus-summary-default-score 0)
5496                       gnus-summary-mark-below)
5497                    (not (gnus-summary-article-ancient-p number)))
5498           (setq gnus-newsgroup-unreads
5499                 (delq number gnus-newsgroup-unreads))
5500           (if gnus-newsgroup-auto-expire
5501               (push number gnus-newsgroup-expirable)
5502             (push (cons number gnus-low-score-mark)
5503                   gnus-newsgroup-reads)))
5504
5505         (setq mark (gnus-article-mark number))
5506         (push (gnus-data-make number mark (1+ (point)) header 0)
5507               gnus-newsgroup-data)
5508         (gnus-summary-insert-line
5509          header 0 number
5510          (memq number gnus-newsgroup-undownloaded)
5511          mark (memq number gnus-newsgroup-replied)
5512          (memq number gnus-newsgroup-expirable)
5513          (mail-header-subject header) nil
5514          (cdr (assq number gnus-newsgroup-scored))
5515          (memq number gnus-newsgroup-processable))))))
5516
5517 (defun gnus-group-get-list-identifiers (group)
5518   "Get list identifier regexp for GROUP."
5519   (or (gnus-parameter-list-identifier group)
5520       (if (consp gnus-list-identifiers)
5521           (mapconcat 'identity gnus-list-identifiers " *\\|")
5522         gnus-list-identifiers)))
5523
5524 (defun gnus-summary-remove-list-identifiers ()
5525   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5526   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5527         changed subject)
5528     (when regexp
5529       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5530       (dolist (header gnus-newsgroup-headers)
5531         (setq subject (mail-header-subject header)
5532               changed nil)
5533         (while (string-match regexp subject)
5534           (setq subject
5535                 (concat (substring subject 0 (match-beginning 1))
5536                         (substring subject (match-end 0)))
5537                 changed t))
5538         (when changed
5539           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5540             (setq subject
5541                   (concat (substring subject 0 (match-beginning 1))
5542                           (substring subject (match-end 1)))))
5543           (mail-header-set-subject header subject))))))
5544
5545 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5546   "Fetch headers of ARTICLES."
5547   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5548     (gnus-message 7 "Fetching headers for %s..." name)
5549     (prog1
5550         (if (eq 'nov
5551                 (setq gnus-headers-retrieved-by
5552                       (gnus-retrieve-headers
5553                        articles gnus-newsgroup-name
5554                        (or limit
5555                            ;; We might want to fetch old headers, but
5556                            ;; not if there is only 1 article.
5557                            (and (or (and
5558                                      (not (eq gnus-fetch-old-headers 'some))
5559                                      (not (numberp gnus-fetch-old-headers)))
5560                                     (> (length articles) 1))
5561                                 gnus-fetch-old-headers)))))
5562             (gnus-get-newsgroup-headers-xover
5563              articles force-new dependencies gnus-newsgroup-name t)
5564           (gnus-get-newsgroup-headers dependencies force-new))
5565       (gnus-message 7 "Fetching headers for %s...done" name))))
5566
5567 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5568   "Select newsgroup GROUP.
5569 If READ-ALL is non-nil, all articles in the group are selected.
5570 If SELECT-ARTICLES, only select those articles from GROUP."
5571   (let* ((entry (gnus-group-entry group))
5572          ;;!!! Dirty hack; should be removed.
5573          (gnus-summary-ignore-duplicates
5574           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5575               t
5576             gnus-summary-ignore-duplicates))
5577          (info (nth 2 entry))
5578          charset articles fetched-articles cached)
5579
5580     (unless (gnus-check-server
5581              (set (make-local-variable 'gnus-current-select-method)
5582                   (gnus-find-method-for-group group)))
5583       (error "Couldn't open server"))
5584     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5585
5586     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5587         (gnus-activate-group group)     ; Or we can activate it...
5588         (progn                          ; Or we bug out.
5589           (when (equal major-mode 'gnus-summary-mode)
5590             (gnus-kill-buffer (current-buffer)))
5591           (error
5592            "Couldn't activate group %s: %s"
5593            (mm-decode-coding-string group charset)
5594            (mm-decode-coding-string (gnus-status-message group) charset))))
5595
5596     (unless (gnus-request-group group t)
5597       (when (equal major-mode 'gnus-summary-mode)
5598         (gnus-kill-buffer (current-buffer)))
5599       (error "Couldn't request group %s: %s"
5600              (mm-decode-coding-string group charset)
5601              (mm-decode-coding-string (gnus-status-message group) charset)))
5602
5603     (when (and gnus-agent
5604                (gnus-active group))
5605       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5606
5607       (setq gnus-summary-use-undownloaded-faces
5608             (gnus-agent-find-parameter
5609              group
5610              'agent-enable-undownloaded-faces)))
5611
5612     (setq gnus-newsgroup-name group
5613           gnus-newsgroup-unselected nil
5614           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5615
5616     (let ((display (gnus-group-find-parameter group 'display)))
5617       (setq gnus-newsgroup-display
5618             (cond
5619              ((not (zerop (or (car-safe read-all) 0)))
5620               ;; The user entered the group with C-u SPC/RET, let's show
5621               ;; all articles.
5622               'gnus-not-ignore)
5623              ((eq display 'all)
5624               'gnus-not-ignore)
5625              ((arrayp display)
5626               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5627              ((numberp display)
5628               ;; The following is probably the "correct" solution, but
5629               ;; it makes Gnus fetch all headers and then limit the
5630               ;; articles (which is slow), so instead we hack the
5631               ;; select-articles parameter instead. -- Simon Josefsson
5632               ;; <jas@kth.se>
5633               ;;
5634               ;; (gnus-byte-compile
5635               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5636               ;;                         display)))))
5637               (setq select-articles
5638                     (gnus-uncompress-range
5639                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5640                              (if (> tmp 0)
5641                                  tmp
5642                                1))
5643                            (cdr (gnus-active group)))))
5644               nil)
5645              (t
5646               nil))))
5647
5648     (gnus-summary-setup-default-charset)
5649
5650     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5651     (when (gnus-virtual-group-p group)
5652       (setq cached gnus-newsgroup-cached))
5653
5654     (setq gnus-newsgroup-unreads
5655           (gnus-sorted-ndifference
5656            (gnus-sorted-ndifference gnus-newsgroup-unreads
5657                                     gnus-newsgroup-marked)
5658            gnus-newsgroup-dormant))
5659
5660     (setq gnus-newsgroup-processable nil)
5661
5662     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5663
5664     ;; Adjust and set lists of article marks.
5665     (when info
5666       (gnus-adjust-marked-articles info))
5667     (if (setq articles select-articles)
5668         (setq gnus-newsgroup-unselected
5669               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5670       (setq articles (gnus-articles-to-read group read-all)))
5671
5672     (cond
5673      ((null articles)
5674       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5675       'quit)
5676      ((eq articles 0) nil)
5677      (t
5678       ;; Init the dependencies hash table.
5679       (setq gnus-newsgroup-dependencies
5680             (gnus-make-hashtable (length articles)))
5681       (gnus-set-global-variables)
5682       ;; Retrieve the headers and read them in.
5683
5684       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5685
5686       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5687       (when cached
5688         (setq gnus-newsgroup-cached cached))
5689
5690       ;; Suppress duplicates?
5691       (when gnus-suppress-duplicates
5692         (gnus-dup-suppress-articles))
5693
5694       ;; Set the initial limit.
5695       (setq gnus-newsgroup-limit (copy-sequence articles))
5696       ;; Remove canceled articles from the list of unread articles.
5697       (setq fetched-articles
5698             (mapcar (lambda (headers) (mail-header-number headers))
5699                     gnus-newsgroup-headers))
5700       (setq gnus-newsgroup-articles fetched-articles)
5701       (setq gnus-newsgroup-unreads
5702             (gnus-sorted-nintersection
5703              gnus-newsgroup-unreads fetched-articles))
5704       (gnus-compute-unseen-list)
5705
5706       ;; Removed marked articles that do not exist.
5707       (gnus-update-missing-marks
5708        (gnus-sorted-difference articles fetched-articles))
5709       ;; We might want to build some more threads first.
5710       (when (and gnus-fetch-old-headers
5711                  (eq gnus-headers-retrieved-by 'nov))
5712         (if (eq gnus-fetch-old-headers 'invisible)
5713             (gnus-build-all-threads)
5714           (gnus-build-old-threads)))
5715       ;; Let the Gnus agent mark articles as read.
5716       (when gnus-agent
5717         (gnus-agent-get-undownloaded-list))
5718       ;; Remove list identifiers from subject
5719       (gnus-summary-remove-list-identifiers)
5720       ;; Check whether auto-expire is to be done in this group.
5721       (setq gnus-newsgroup-auto-expire
5722             (and (gnus-group-auto-expirable-p group)
5723                  (not (gnus-group-read-only-p group))))
5724       ;; Set up the article buffer now, if necessary.
5725       (unless (and gnus-single-article-buffer
5726                    (equal gnus-article-buffer "*Article*"))
5727         (gnus-article-setup-buffer))
5728       ;; First and last article in this newsgroup.
5729       (when gnus-newsgroup-headers
5730         (setq gnus-newsgroup-begin
5731               (mail-header-number (car gnus-newsgroup-headers))
5732               gnus-newsgroup-end
5733               (mail-header-number
5734                (gnus-last-element gnus-newsgroup-headers))))
5735       ;; GROUP is successfully selected.
5736       (or gnus-newsgroup-headers t)))))
5737
5738 (defun gnus-compute-unseen-list ()
5739   ;; The `seen' marks are treated specially.
5740   (if (not gnus-newsgroup-seen)
5741       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5742     (setq gnus-newsgroup-unseen
5743           (gnus-inverse-list-range-intersection
5744            gnus-newsgroup-articles gnus-newsgroup-seen))))
5745
5746 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5747
5748 (defun gnus-summary-display-make-predicate (display)
5749   (require 'gnus-agent)
5750   (when (= (length display) 1)
5751     (setq display (car display)))
5752   (unless gnus-summary-display-cache
5753     (dolist (elem (append '((unread . unread)
5754                             (read . read)
5755                             (unseen . unseen))
5756                           gnus-article-mark-lists))
5757       (push (cons (cdr elem)
5758                   (gnus-byte-compile    ;Why bother?
5759                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5760             gnus-summary-display-cache)))
5761   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5762         (gnus-category-predicate-cache gnus-summary-display-cache))
5763     (gnus-get-predicate display)))
5764
5765 ;; Uses the dynamically bound `gnus-number' variable.
5766 (defvar gnus-number)
5767 (defun gnus-article-marked-p (type &optional article)
5768   (let ((article (or article gnus-number)))
5769     (cond
5770      ((eq type 'tick)
5771       (memq article gnus-newsgroup-marked))
5772      ((eq type 'spam)
5773       (memq article gnus-newsgroup-spam-marked))
5774      ((eq type 'unsend)
5775       (memq article gnus-newsgroup-unsendable))
5776      ((eq type 'undownload)
5777       (memq article gnus-newsgroup-undownloaded))
5778      ((eq type 'download)
5779       (memq article gnus-newsgroup-downloadable))
5780      ((eq type 'unread)
5781       (memq article gnus-newsgroup-unreads))
5782      ((eq type 'read)
5783       (memq article gnus-newsgroup-reads))
5784      ((eq type 'dormant)
5785       (memq article gnus-newsgroup-dormant) )
5786      ((eq type 'expire)
5787       (memq article gnus-newsgroup-expirable))
5788      ((eq type 'reply)
5789       (memq article gnus-newsgroup-replied))
5790      ((eq type 'killed)
5791       (memq article gnus-newsgroup-killed))
5792      ((eq type 'bookmark)
5793       (assq article gnus-newsgroup-bookmarks))
5794      ((eq type 'score)
5795       (assq article gnus-newsgroup-scored))
5796      ((eq type 'save)
5797       (memq article gnus-newsgroup-saved))
5798      ((eq type 'cache)
5799       (memq article gnus-newsgroup-cached))
5800      ((eq type 'forward)
5801       (memq article gnus-newsgroup-forwarded))
5802      ((eq type 'seen)
5803       (not (memq article gnus-newsgroup-unseen)))
5804      (t t))))
5805
5806 (defun gnus-articles-to-read (group &optional read-all)
5807   "Find out what articles the user wants to read."
5808   (let* ((only-read-p t)
5809          (articles
5810           ;; Select all articles if `read-all' is non-nil, or if there
5811           ;; are no unread articles.
5812           (if (or read-all
5813                   (and (zerop (length gnus-newsgroup-marked))
5814                        (zerop (length gnus-newsgroup-unreads)))
5815                   ;; Fetch all if the predicate is non-nil.
5816                   gnus-newsgroup-display)
5817               ;; We want to select the headers for all the articles in
5818               ;; the group, so we select either all the active
5819               ;; articles in the group, or (if that's nil), the
5820               ;; articles in the cache.
5821               (or
5822                (if gnus-newsgroup-maximum-articles
5823                    (let ((active (gnus-active group)))
5824                      (gnus-uncompress-range
5825                       (cons (max (car active)
5826                                  (- (cdr active)
5827                                     gnus-newsgroup-maximum-articles
5828                                     -1))
5829                             (cdr active))))
5830                  (gnus-uncompress-range (gnus-active group)))
5831                (gnus-cache-articles-in-group group))
5832             ;; Select only the "normal" subset of articles.
5833             (setq only-read-p nil)
5834             (gnus-sorted-nunion
5835              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5836              gnus-newsgroup-unreads)))
5837          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5838          (scored (length scored-list))
5839          (number (length articles))
5840          (marked (+ (length gnus-newsgroup-marked)
5841                     (length gnus-newsgroup-dormant)))
5842          (select
5843           (cond
5844            ((numberp read-all)
5845             read-all)
5846            ((numberp gnus-newsgroup-display)
5847             gnus-newsgroup-display)
5848            (t
5849             (condition-case ()
5850                 (cond
5851                  ((and (or (<= scored marked) (= scored number))
5852                        (numberp gnus-large-newsgroup)
5853                        (> number gnus-large-newsgroup))
5854                   (let* ((cursor-in-echo-area nil)
5855                          (initial (gnus-parameter-large-newsgroup-initial
5856                                    gnus-newsgroup-name))
5857                          (default (if only-read-p
5858                                       (or initial gnus-large-newsgroup)
5859                                     number))
5860                          (input
5861                           (read-string
5862                            (if only-read-p
5863                                (format
5864                                 "How many articles from %s (available %d, default %d): "
5865                                 (gnus-group-decoded-name
5866                                  (gnus-group-real-name gnus-newsgroup-name))
5867                                 number default)
5868                              (format
5869                               "How many articles from %s (%d default): "
5870                               (gnus-group-decoded-name
5871                                (gnus-group-real-name gnus-newsgroup-name))
5872                               default))
5873                            nil
5874                            nil
5875                            (number-to-string default))))
5876                     (if (string-match "^[ \t]*$" input) number input)))
5877                  ((and (> scored marked) (< scored number)
5878                        (> (- scored number) 20))
5879                   (let ((input
5880                          (read-string
5881                           (format "%s %s (%d scored, %d total): "
5882                                   "How many articles from"
5883                                   (gnus-group-decoded-name
5884                                    (gnus-group-real-name gnus-newsgroup-name))
5885                                   scored number))))
5886                     (if (string-match "^[ \t]*$" input)
5887                         number input)))
5888                  (t number))
5889               (quit
5890                (message "Quit getting the articles to read")
5891                nil))))))
5892     (setq select (if (stringp select) (string-to-number select) select))
5893     (if (or (null select) (zerop select))
5894         select
5895       (if (and (not (zerop scored)) (<= (abs select) scored))
5896           (progn
5897             (setq articles (sort scored-list '<))
5898             (setq number (length articles)))
5899         (setq articles (copy-sequence articles)))
5900
5901       (when (< (abs select) number)
5902         (if (< select 0)
5903             ;; Select the N oldest articles.
5904             (setcdr (nthcdr (1- (abs select)) articles) nil)
5905           ;; Select the N most recent articles.
5906           (setq articles (nthcdr (- number select) articles))))
5907       (setq gnus-newsgroup-unselected
5908             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5909       (when gnus-alter-articles-to-read-function
5910         (setq articles
5911               (sort
5912                (funcall gnus-alter-articles-to-read-function
5913                         gnus-newsgroup-name articles)
5914                '<)))
5915       articles)))
5916
5917 (defun gnus-killed-articles (killed articles)
5918   (let (out)
5919     (while articles
5920       (when (inline (gnus-member-of-range (car articles) killed))
5921         (push (car articles) out))
5922       (setq articles (cdr articles)))
5923     out))
5924
5925 (defun gnus-uncompress-marks (marks)
5926   "Uncompress the mark ranges in MARKS."
5927   (let ((uncompressed '(score bookmark))
5928         out)
5929     (while marks
5930       (if (memq (caar marks) uncompressed)
5931           (push (car marks) out)
5932         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5933       (setq marks (cdr marks)))
5934     out))
5935
5936 (defun gnus-article-mark-to-type (mark)
5937   "Return the type of MARK."
5938   (or (cadr (assq mark gnus-article-special-mark-lists))
5939       'list))
5940
5941 (defun gnus-article-unpropagatable-p (mark)
5942   "Return whether MARK should be propagated to back end."
5943   (memq mark gnus-article-unpropagated-mark-lists))
5944
5945 (defun gnus-adjust-marked-articles (info)
5946   "Set all article lists and remove all marks that are no longer valid."
5947   (let* ((marked-lists (gnus-info-marks info))
5948          (active (gnus-active (gnus-info-group info)))
5949          (min (car active))
5950          (max (cdr active))
5951          (types gnus-article-mark-lists)
5952          marks var articles article mark mark-type
5953          bgn end)
5954     ;; Hack to avoid adjusting marks for imap.
5955     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5956               'nnimap)
5957       (setq min 1))
5958
5959     (dolist (marks marked-lists)
5960       (setq mark (car marks)
5961             mark-type (gnus-article-mark-to-type mark)
5962             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5963
5964       ;; We set the variable according to the type of the marks list,
5965       ;; and then adjust the marks to a subset of the active articles.
5966       (cond
5967        ;; Adjust "simple" lists - compressed yet unsorted
5968        ((eq mark-type 'list)
5969         ;; Simultaneously uncompress and clip to active range
5970         ;; See gnus-uncompress-range for a description of possible marks
5971         (let (l lh)
5972           (if (not (cadr marks))
5973               (set var nil)
5974             (setq articles (if (numberp (cddr marks))
5975                                (list (cdr marks))
5976                              (cdr marks))
5977                   lh (cons nil nil)
5978                   l lh)
5979
5980             (while (setq article (pop articles))
5981               (cond ((consp article)
5982                      (setq bgn (max (car article) min)
5983                            end (min (cdr article) max))
5984                      (while (<= bgn end)
5985                        (setq l (setcdr l (cons bgn nil))
5986                              bgn (1+ bgn))))
5987                     ((and (<= min article)
5988                           (>= max article))
5989                      (setq l (setcdr l (cons article nil))))))
5990             (set var (cdr lh)))))
5991        ;; Adjust assocs.
5992        ((eq mark-type 'tuple)
5993         (set var (setq articles (cdr marks)))
5994         (when (not (listp (cdr (symbol-value var))))
5995           (set var (list (symbol-value var))))
5996         (when (not (listp (cdr articles)))
5997           (setq articles (list articles)))
5998         (while articles
5999           (when (or (not (consp (setq article (pop articles))))
6000                     (< (car article) min)
6001                     (> (car article) max))
6002             (set var (delq article (symbol-value var))))))
6003        ;; Adjust ranges (sloppily).
6004        ((eq mark-type 'range)
6005         (cond
6006          ((eq mark 'seen)
6007           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6008           ;; It should be (seen (NUM1 . NUM2)).
6009           (when (numberp (cddr marks))
6010             (setcdr marks (list (cdr marks))))
6011           (setq articles (cdr marks))
6012           (while (and articles
6013                       (or (and (consp (car articles))
6014                                (> min (cdar articles)))
6015                           (and (numberp (car articles))
6016                                (> min (car articles)))))
6017             (pop articles))
6018           (set var articles))))))))
6019
6020 (defun gnus-update-missing-marks (missing)
6021   "Go through the list of MISSING articles and remove them from the mark lists."
6022   (when missing
6023     (let (var m)
6024       ;; Go through all types.
6025       (dolist (elem gnus-article-mark-lists)
6026         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6027           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6028           (when (symbol-value var)
6029             ;; This list has articles.  So we delete all missing
6030             ;; articles from it.
6031             (setq m missing)
6032             (while m
6033               (set var (delq (pop m) (symbol-value var))))))))))
6034
6035 (defun gnus-update-marks ()
6036   "Enter the various lists of marked articles into the newsgroup info list."
6037   (let ((types gnus-article-mark-lists)
6038         (info (gnus-get-info gnus-newsgroup-name))
6039         type list newmarked symbol delta-marks)
6040     (when info
6041       ;; Add all marks lists to the list of marks lists.
6042       (while (setq type (pop types))
6043         (setq list (symbol-value
6044                     (setq symbol
6045                           (intern (format "gnus-newsgroup-%s" (car type))))))
6046
6047         (when list
6048           ;; Get rid of the entries of the articles that have the
6049           ;; default score.
6050           (when (and (eq (cdr type) 'score)
6051                      gnus-save-score
6052                      list)
6053             (let* ((arts list)
6054                    (prev (cons nil list))
6055                    (all prev))
6056               (while arts
6057                 (if (or (not (consp (car arts)))
6058                         (= (cdar arts) gnus-summary-default-score))
6059                     (setcdr prev (cdr arts))
6060                   (setq prev arts))
6061                 (setq arts (cdr arts)))
6062               (setq list (cdr all)))))
6063
6064         (when (eq (cdr type) 'seen)
6065           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6066
6067         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6068           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6069
6070         (when (and (gnus-check-backend-function
6071                     'request-set-mark gnus-newsgroup-name)
6072                    (not (gnus-article-unpropagatable-p (cdr type))))
6073           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6074                  ;; Don't do anything about marks for articles we
6075                  ;; didn't actually get any headers for.
6076                  (del
6077                   (gnus-list-range-intersection
6078                    gnus-newsgroup-articles
6079                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6080                  (add
6081                   (gnus-list-range-intersection
6082                    gnus-newsgroup-articles
6083                    (gnus-remove-from-range
6084                     (gnus-copy-sequence list) old))))
6085             (when add
6086               (push (list add 'add (list (cdr type))) delta-marks))
6087             (when del
6088               ;; Don't delete marks from outside the active range.
6089               ;; This shouldn't happen, but is a sanity check.
6090               (setq del (gnus-sorted-range-intersection
6091                          (gnus-active gnus-newsgroup-name) del))
6092               (push (list del 'del (list (cdr type))) delta-marks))))
6093
6094         (when list
6095           (push (cons (cdr type) list) newmarked)))
6096
6097       (when delta-marks
6098         (unless (gnus-check-group gnus-newsgroup-name)
6099           (error "Can't open server for %s" gnus-newsgroup-name))
6100         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6101
6102       ;; Enter these new marks into the info of the group.
6103       (if (nthcdr 3 info)
6104           (setcar (nthcdr 3 info) newmarked)
6105         ;; Add the marks lists to the end of the info.
6106         (when newmarked
6107           (setcdr (nthcdr 2 info) (list newmarked))))
6108
6109       ;; Cut off the end of the info if there's nothing else there.
6110       (let ((i 5))
6111         (while (and (> i 2)
6112                     (not (nth i info)))
6113           (when (nthcdr (decf i) info)
6114             (setcdr (nthcdr i info) nil)))))))
6115
6116 (defun gnus-set-mode-line (where)
6117   "Set the mode line of the article or summary buffers.
6118 If WHERE is `summary', the summary mode line format will be used."
6119   ;; Is this mode line one we keep updated?
6120   (when (and (memq where gnus-updated-mode-lines)
6121              (symbol-value
6122               (intern (format "gnus-%s-mode-line-format-spec" where))))
6123     (let (mode-string)
6124       ;; We evaluate this in the summary buffer since these
6125       ;; variables are buffer-local to that buffer.
6126       (with-current-buffer gnus-summary-buffer
6127         ;; We bind all these variables that are used in the `eval' form
6128         ;; below.
6129         (let* ((mformat (symbol-value
6130                          (intern
6131                           (format "gnus-%s-mode-line-format-spec" where))))
6132                (gnus-tmp-group-name (gnus-mode-string-quote
6133                                      (gnus-group-decoded-name
6134                                       gnus-newsgroup-name)))
6135                (gnus-tmp-article-number (or gnus-current-article 0))
6136                (gnus-tmp-unread gnus-newsgroup-unreads)
6137                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6138                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6139                (gnus-tmp-unread-and-unselected
6140                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6141                             (zerop gnus-tmp-unselected))
6142                        "")
6143                       ((zerop gnus-tmp-unselected)
6144                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6145                       (t (format "{%d(+%d) more}"
6146                                  gnus-tmp-unread-and-unticked
6147                                  gnus-tmp-unselected))))
6148                (gnus-tmp-subject
6149                 (if (and gnus-current-headers
6150                          (vectorp gnus-current-headers))
6151                     (gnus-mode-string-quote
6152                      (mail-header-subject gnus-current-headers))
6153                   ""))
6154                bufname-length max-len
6155                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6156           (setq mode-string (eval mformat))
6157           (setq bufname-length (if (string-match "%b" mode-string)
6158                                    (- (length
6159                                        (buffer-name
6160                                         (if (eq where 'summary)
6161                                             nil
6162                                           (get-buffer gnus-article-buffer))))
6163                                       2)
6164                                  0))
6165           (setq max-len (max 4 (if gnus-mode-non-string-length
6166                                    (- (window-width)
6167                                       gnus-mode-non-string-length
6168                                       bufname-length)
6169                                  (length mode-string))))
6170           ;; We might have to chop a bit of the string off...
6171           (when (> (length mode-string) max-len)
6172             (setq mode-string
6173                   (concat (truncate-string-to-width mode-string (- max-len 3))
6174                           "...")))))
6175       ;; Update the mode line.
6176       (setq mode-line-buffer-identification
6177             (gnus-mode-line-buffer-identification (list mode-string)))
6178       (set-buffer-modified-p t))))
6179
6180 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6181   "Go through the HEADERS list and add all Xrefs to a hash table.
6182 The resulting hash table is returned, or nil if no Xrefs were found."
6183   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6184          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6185          (xref-hashtb (gnus-make-hashtable))
6186          start group entry number xrefs header)
6187     (while headers
6188       (setq header (pop headers))
6189       (when (and (setq xrefs (mail-header-xref header))
6190                  (not (memq (setq number (mail-header-number header))
6191                             unreads)))
6192         (setq start 0)
6193         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6194           (setq start (match-end 0))
6195           (setq group (if prefix
6196                           (concat prefix (substring xrefs (match-beginning 1)
6197                                                     (match-end 1)))
6198                         (substring xrefs (match-beginning 1) (match-end 1))))
6199           (setq number
6200                 (string-to-number (substring xrefs (match-beginning 2)
6201                                           (match-end 2))))
6202           (if (setq entry (gnus-gethash group xref-hashtb))
6203               (setcdr entry (cons number (cdr entry)))
6204             (gnus-sethash group (cons number nil) xref-hashtb)))))
6205     (and start xref-hashtb)))
6206
6207 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6208   "Look through all the headers and mark the Xrefs as read."
6209   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6210         name info xref-hashtb idlist method nth4)
6211     (with-current-buffer gnus-group-buffer
6212       (when (setq xref-hashtb
6213                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6214         (mapatoms
6215          (lambda (group)
6216            (unless (string= from-newsgroup (setq name (symbol-name group)))
6217              (setq idlist (symbol-value group))
6218              ;; Dead groups are not updated.
6219              (and (prog1
6220                       (setq info (gnus-get-info name))
6221                     (when (stringp (setq nth4 (gnus-info-method info)))
6222                       (setq nth4 (gnus-server-to-method nth4))))
6223                   ;; Only do the xrefs if the group has the same
6224                   ;; select method as the group we have just read.
6225                   (or (gnus-methods-equal-p
6226                        nth4 (gnus-find-method-for-group from-newsgroup))
6227                       virtual
6228                       (equal nth4 (setq method (gnus-find-method-for-group
6229                                                 from-newsgroup)))
6230                       (and (equal (car nth4) (car method))
6231                            (equal (nth 1 nth4) (nth 1 method))))
6232                   gnus-use-cross-reference
6233                   (or (not (eq gnus-use-cross-reference t))
6234                       virtual
6235                       ;; Only do cross-references on subscribed
6236                       ;; groups, if that is what is wanted.
6237                       (<= (gnus-info-level info) gnus-level-subscribed))
6238                   (gnus-group-make-articles-read name idlist))))
6239          xref-hashtb)))))
6240
6241 (defun gnus-compute-read-articles (group articles)
6242   (let* ((entry (gnus-group-entry group))
6243          (info (nth 2 entry))
6244          (active (gnus-active group))
6245          ninfo)
6246     (when entry
6247       ;; First peel off all invalid article numbers.
6248       (when active
6249         (let ((ids articles)
6250               id first)
6251           (while (setq id (pop ids))
6252             (when (and first (> id (cdr active)))
6253               ;; We'll end up in this situation in one particular
6254               ;; obscure situation.  If you re-scan a group and get
6255               ;; a new article that is cross-posted to a different
6256               ;; group that has not been re-scanned, you might get
6257               ;; crossposted article that has a higher number than
6258               ;; Gnus believes possible.  So we re-activate this
6259               ;; group as well.  This might mean doing the
6260               ;; crossposting thingy will *increase* the number
6261               ;; of articles in some groups.  Tsk, tsk.
6262               (setq active (or (gnus-activate-group group) active)))
6263             (when (or (> id (cdr active))
6264                       (< id (car active)))
6265               (setq articles (delq id articles))))))
6266       ;; If the read list is nil, we init it.
6267       (if (and active
6268                (null (gnus-info-read info))
6269                (> (car active) 1))
6270           (setq ninfo (cons 1 (1- (car active))))
6271         (setq ninfo (gnus-info-read info)))
6272       ;; Then we add the read articles to the range.
6273       (gnus-add-to-range
6274        ninfo (setq articles (sort articles '<))))))
6275
6276 (defun gnus-group-make-articles-read (group articles)
6277   "Update the info of GROUP to say that ARTICLES are read."
6278   (let* ((num 0)
6279          (entry (gnus-group-entry group))
6280          (info (nth 2 entry))
6281          (active (gnus-active group))
6282          range)
6283     (if (not entry)
6284         ;; Group that Gnus doesn't know exists, but still allow the
6285         ;; backend to set marks.
6286         (gnus-request-set-mark
6287          group (list (list (gnus-compress-sequence (sort articles #'<))
6288                            'add '(read))))
6289       ;; Normal, subscribed groups.
6290       (setq range (gnus-compute-read-articles group articles))
6291       (with-current-buffer gnus-group-buffer
6292         (gnus-undo-register
6293           `(progn
6294              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6295              (gnus-info-set-read ',info ',(gnus-info-read info))
6296              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6297              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6298              (gnus-group-update-group ,group t))))
6299       ;; Add the read articles to the range.
6300       (gnus-info-set-read info range)
6301       (gnus-request-set-mark group (list (list range 'add '(read))))
6302       ;; Then we have to re-compute how many unread
6303       ;; articles there are in this group.
6304       (when active
6305         (cond
6306          ((not range)
6307           (setq num (- (1+ (cdr active)) (car active))))
6308          ((not (listp (cdr range)))
6309           (setq num (- (cdr active) (- (1+ (cdr range))
6310                                        (car range)))))
6311          (t
6312           (while range
6313             (if (numberp (car range))
6314                 (setq num (1+ num))
6315               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6316             (setq range (cdr range)))
6317           (setq num (- (cdr active) num))))
6318         ;; Update the number of unread articles.
6319         (setcar entry num)
6320         ;; Update the group buffer.
6321         (unless (gnus-ephemeral-group-p group)
6322           (gnus-group-update-group group t))))))
6323
6324 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6325   (let ((cur nntp-server-buffer)
6326         (dependencies
6327          (or dependencies
6328              (with-current-buffer gnus-summary-buffer
6329                gnus-newsgroup-dependencies)))
6330         headers id end ref number
6331         (mail-parse-charset gnus-newsgroup-charset)
6332         (mail-parse-ignored-charsets
6333          (save-current-buffer (condition-case nil
6334                                   (set-buffer gnus-summary-buffer)
6335                                 (error))
6336                               gnus-newsgroup-ignored-charsets)))
6337     (with-current-buffer nntp-server-buffer
6338       ;; Translate all TAB characters into SPACE characters.
6339       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6340       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6341       (ietf-drums-unfold-fws)
6342       (gnus-run-hooks 'gnus-parse-headers-hook)
6343       (let ((case-fold-search t)
6344             in-reply-to header p lines chars)
6345         (goto-char (point-min))
6346         ;; Search to the beginning of the next header.  Error messages
6347         ;; do not begin with 2 or 3.
6348         (while (re-search-forward "^[23][0-9]+ " nil t)
6349           (setq id nil
6350                 ref nil)
6351           ;; This implementation of this function, with nine
6352           ;; search-forwards instead of the one re-search-forward and
6353           ;; a case (which basically was the old function) is actually
6354           ;; about twice as fast, even though it looks messier.  You
6355           ;; can't have everything, I guess.  Speed and elegance
6356           ;; doesn't always go hand in hand.
6357           (setq
6358            header
6359            (vector
6360             ;; Number.
6361             (prog1
6362                 (setq number (read cur))
6363               (end-of-line)
6364               (setq p (point))
6365               (narrow-to-region (point)
6366                                 (or (and (search-forward "\n.\n" nil t)
6367                                          (- (point) 2))
6368                                     (point))))
6369             ;; Subject.
6370             (progn
6371               (goto-char p)
6372               (if (search-forward "\nsubject:" nil t)
6373                   (funcall gnus-decode-encoded-word-function
6374                            (nnheader-header-value))
6375                 "(none)"))
6376             ;; From.
6377             (progn
6378               (goto-char p)
6379               (if (search-forward "\nfrom:" nil t)
6380                   (funcall gnus-decode-encoded-address-function
6381                            (nnheader-header-value))
6382                 "(nobody)"))
6383             ;; Date.
6384             (progn
6385               (goto-char p)
6386               (if (search-forward "\ndate:" nil t)
6387                   (nnheader-header-value) ""))
6388             ;; Message-ID.
6389             (progn
6390               (goto-char p)
6391               (setq id (if (re-search-forward
6392                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6393                            ;; We do it this way to make sure the Message-ID
6394                            ;; is (somewhat) syntactically valid.
6395                            (buffer-substring (match-beginning 1)
6396                                              (match-end 1))
6397                          ;; If there was no message-id, we just fake one
6398                          ;; to make subsequent routines simpler.
6399                          (nnheader-generate-fake-message-id number))))
6400             ;; References.
6401             (progn
6402               (goto-char p)
6403               (if (search-forward "\nreferences:" nil t)
6404                   (progn
6405                     (setq end (point))
6406                     (prog1
6407                         (nnheader-header-value)
6408                       (setq ref
6409                             (buffer-substring
6410                              (progn
6411                                (end-of-line)
6412                                (search-backward ">" end t)
6413                                (1+ (point)))
6414                              (progn
6415                                (search-backward "<" end t)
6416                                (point))))))
6417                 ;; Get the references from the in-reply-to header if there
6418                 ;; were no references and the in-reply-to header looks
6419                 ;; promising.
6420                 (if (and (search-forward "\nin-reply-to:" nil t)
6421                          (setq in-reply-to (nnheader-header-value))
6422                          (string-match "<[^>]+>" in-reply-to))
6423                     (let (ref2)
6424                       (setq ref (substring in-reply-to (match-beginning 0)
6425                                            (match-end 0)))
6426                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6427                         (setq ref2 (substring in-reply-to (match-beginning 0)
6428                                               (match-end 0)))
6429                         (when (> (length ref2) (length ref))
6430                           (setq ref ref2)))
6431                       ref)
6432                   (setq ref nil))))
6433             ;; Chars.
6434             (progn
6435               (goto-char p)
6436               (if (search-forward "\nchars: " nil t)
6437                   (if (numberp (setq chars (ignore-errors (read cur))))
6438                       chars -1)
6439                 -1))
6440             ;; Lines.
6441             (progn
6442               (goto-char p)
6443               (if (search-forward "\nlines: " nil t)
6444                   (if (numberp (setq lines (ignore-errors (read cur))))
6445                       lines -1)
6446                 -1))
6447             ;; Xref.
6448             (progn
6449               (goto-char p)
6450               (and (search-forward "\nxref:" nil t)
6451                    (nnheader-header-value)))
6452             ;; Extra.
6453             (when gnus-extra-headers
6454               (let ((extra gnus-extra-headers)
6455                     out)
6456                 (while extra
6457                   (goto-char p)
6458                   (when (search-forward
6459                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6460                     (push (cons (car extra) (nnheader-header-value))
6461                           out))
6462                   (pop extra))
6463                 out))))
6464           (when (equal id ref)
6465             (setq ref nil))
6466
6467           (when gnus-alter-header-function
6468             (funcall gnus-alter-header-function header)
6469             (setq id (mail-header-id header)
6470                   ref (gnus-parent-id (mail-header-references header))))
6471
6472           (when (setq header
6473                       (gnus-dependencies-add-header
6474                        header dependencies force-new))
6475             (push header headers))
6476           (goto-char (point-max))
6477           (widen))
6478         (nreverse headers)))))
6479
6480 ;; Goes through the xover lines and returns a list of vectors
6481 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6482                                                   force-new dependencies
6483                                                   group also-fetch-heads)
6484   "Parse the news overview data in the server buffer.
6485 Return a list of headers that match SEQUENCE (see
6486 `nntp-retrieve-headers')."
6487   ;; Get the Xref when the users reads the articles since most/some
6488   ;; NNTP servers do not include Xrefs when using XOVER.
6489   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6490   (let ((mail-parse-charset gnus-newsgroup-charset)
6491         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6492         (cur nntp-server-buffer)
6493         (dependencies (or dependencies gnus-newsgroup-dependencies))
6494         (allp (cond
6495                ((eq gnus-read-all-available-headers t)
6496                 t)
6497                ((and (stringp gnus-read-all-available-headers)
6498                      group)
6499                 (string-match gnus-read-all-available-headers group))
6500                (t
6501                 nil)))
6502         number headers header)
6503     (with-current-buffer nntp-server-buffer
6504       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6505       ;; Allow the user to mangle the headers before parsing them.
6506       (gnus-run-hooks 'gnus-parse-headers-hook)
6507       (goto-char (point-min))
6508       (gnus-parse-without-error
6509         (while (and (or sequence allp)
6510                     (not (eobp)))
6511           (setq number (read cur))
6512           (when (not allp)
6513             (while (and sequence
6514                         (< (car sequence) number))
6515               (setq sequence (cdr sequence))))
6516           (when (and (or allp
6517                          (and sequence
6518                               (eq number (car sequence))))
6519                      (progn
6520                        (setq sequence (cdr sequence))
6521                        (setq header (inline
6522                                       (gnus-nov-parse-line
6523                                        number dependencies force-new)))))
6524             (push header headers))
6525           (forward-line 1)))
6526       ;; A common bug in inn is that if you have posted an article and
6527       ;; then retrieves the active file, it will answer correctly --
6528       ;; the new article is included.  However, a NOV entry for the
6529       ;; article may not have been generated yet, so this may fail.
6530       ;; We work around this problem by retrieving the last few
6531       ;; headers using HEAD.
6532       (if (or (not also-fetch-heads)
6533               (not sequence))
6534           ;; We (probably) got all the headers.
6535           (nreverse headers)
6536         (let ((gnus-nov-is-evil t))
6537           (nconc
6538            (nreverse headers)
6539            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6540              (gnus-get-newsgroup-headers))))))))
6541
6542 (defun gnus-article-get-xrefs ()
6543   "Fill in the Xref value in `gnus-current-headers', if necessary.
6544 This is meant to be called in `gnus-article-internal-prepare-hook'."
6545   (let ((headers (with-current-buffer gnus-summary-buffer
6546                    gnus-current-headers)))
6547     (or (not gnus-use-cross-reference)
6548         (not headers)
6549         (and (mail-header-xref headers)
6550              (not (string= (mail-header-xref headers) "")))
6551         (let ((case-fold-search t)
6552               xref)
6553           (save-restriction
6554             (nnheader-narrow-to-headers)
6555             (goto-char (point-min))
6556             (when (or (and (not (eobp))
6557                            (eq (downcase (char-after)) ?x)
6558                            (looking-at "Xref:"))
6559                       (search-forward "\nXref:" nil t))
6560               (goto-char (1+ (match-end 0)))
6561               (setq xref (buffer-substring (point) (point-at-eol)))
6562               (mail-header-set-xref headers xref)))))))
6563
6564 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6565   "Find article ID and insert the summary line for that article.
6566 OLD-HEADER can either be a header or a line number to insert
6567 the subject line on.
6568 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6569 and OLD-HEADER will be used when the summary line is inserted,
6570 too, instead of trying to fetch new headers."
6571   (let* ((line (and (numberp old-header) old-header))
6572          (old-header (and (vectorp old-header) old-header))
6573          (header (cond ((and old-header use-old-header)
6574                         old-header)
6575                        ((and (numberp id)
6576                              (gnus-number-to-header id))
6577                         (gnus-number-to-header id))
6578                        (t
6579                         (gnus-read-header id))))
6580          (number (and (numberp id) id))
6581          d)
6582     (when header
6583       ;; Rebuild the thread that this article is part of and go to the
6584       ;; article we have fetched.
6585       (when (and (not gnus-show-threads)
6586                  old-header)
6587         (when (and number
6588                    (setq d (gnus-data-find (mail-header-number old-header))))
6589           (goto-char (gnus-data-pos d))
6590           (gnus-data-remove
6591            number
6592            (- (point-at-bol)
6593               (prog1
6594                   (1+ (point-at-eol))
6595                 (gnus-delete-line))))))
6596       ;; Remove list identifiers from subject.
6597       (let ((gnus-newsgroup-headers (list header)))
6598         (gnus-summary-remove-list-identifiers))
6599       (when old-header
6600         (mail-header-set-number header (mail-header-number old-header)))
6601       (setq gnus-newsgroup-sparse
6602             (delq (setq number (mail-header-number header))
6603                   gnus-newsgroup-sparse))
6604       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6605       (push number gnus-newsgroup-limit)
6606       (gnus-rebuild-thread (mail-header-id header) line)
6607       (gnus-summary-goto-subject number nil t))
6608     (when (and (numberp number)
6609                (> number 0))
6610       ;; We have to update the boundaries even if we can't fetch the
6611       ;; article if ID is a number -- so that the next `P' or `N'
6612       ;; command will fetch the previous (or next) article even
6613       ;; if the one we tried to fetch this time has been canceled.
6614       (when (> number gnus-newsgroup-end)
6615         (setq gnus-newsgroup-end number))
6616       (when (< number gnus-newsgroup-begin)
6617         (setq gnus-newsgroup-begin number))
6618       (setq gnus-newsgroup-unselected
6619             (delq number gnus-newsgroup-unselected)))
6620     ;; Report back a success?
6621     (and header (mail-header-number header))))
6622
6623 ;;; Process/prefix in the summary buffer
6624
6625 (defun gnus-summary-work-articles (n)
6626   "Return a list of articles to be worked upon.
6627 The prefix argument, the list of process marked articles, and the
6628 current article will be taken into consideration."
6629   (with-current-buffer gnus-summary-buffer
6630     (cond
6631      (n
6632       ;; A numerical prefix has been given.
6633       (setq n (prefix-numeric-value n))
6634       (let ((backward (< n 0))
6635             (n (abs (prefix-numeric-value n)))
6636             articles article)
6637         (save-excursion
6638           (while
6639               (and (> n 0)
6640                    (push (setq article (gnus-summary-article-number))
6641                          articles)
6642                    (if backward
6643                        (gnus-summary-find-prev nil article)
6644                      (gnus-summary-find-next nil article)))
6645             (decf n)))
6646         (nreverse articles)))
6647      ((and (gnus-region-active-p) (mark))
6648       (message "region active")
6649       ;; Work on the region between point and mark.
6650       (let ((max (max (point) (mark)))
6651             articles article)
6652         (save-excursion
6653           (goto-char (min (point) (mark)))
6654           (while
6655               (and
6656                (push (setq article (gnus-summary-article-number)) articles)
6657                (gnus-summary-find-next nil article)
6658                (< (point) max)))
6659           (nreverse articles))))
6660      (gnus-newsgroup-processable
6661       ;; There are process-marked articles present.
6662       ;; Save current state.
6663       (gnus-summary-save-process-mark)
6664       ;; Return the list.
6665       (reverse gnus-newsgroup-processable))
6666      (t
6667       ;; Just return the current article.
6668       (list (gnus-summary-article-number))))))
6669
6670 (defmacro gnus-summary-iterate (arg &rest forms)
6671   "Iterate over the process/prefixed articles and do FORMS.
6672 ARG is the interactive prefix given to the command.  FORMS will be
6673 executed with point over the summary line of the articles."
6674   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6675     `(let ((,articles (gnus-summary-work-articles ,arg)))
6676        (while ,articles
6677          (gnus-summary-goto-subject (car ,articles))
6678          ,@forms
6679          (pop ,articles)))))
6680
6681 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6682 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6683
6684 (defun gnus-summary-save-process-mark ()
6685   "Push the current set of process marked articles on the stack."
6686   (interactive)
6687   (push (copy-sequence gnus-newsgroup-processable)
6688         gnus-newsgroup-process-stack))
6689
6690 (defun gnus-summary-kill-process-mark ()
6691   "Push the current set of process marked articles on the stack and unmark."
6692   (interactive)
6693   (gnus-summary-save-process-mark)
6694   (gnus-summary-unmark-all-processable))
6695
6696 (defun gnus-summary-yank-process-mark ()
6697   "Pop the last process mark state off the stack and restore it."
6698   (interactive)
6699   (unless gnus-newsgroup-process-stack
6700     (error "Empty mark stack"))
6701   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6702
6703 (defun gnus-summary-process-mark-set (set)
6704   "Make SET into the current process marked articles."
6705   (gnus-summary-unmark-all-processable)
6706   (mapc 'gnus-summary-set-process-mark set))
6707
6708 ;;; Searching and stuff
6709
6710 (defun gnus-summary-search-group (&optional backward use-level)
6711   "Search for next unread newsgroup.
6712 If optional argument BACKWARD is non-nil, search backward instead."
6713   (with-current-buffer gnus-group-buffer
6714     (when (gnus-group-search-forward
6715            backward nil (if use-level (gnus-group-group-level) nil))
6716       (gnus-group-group-name))))
6717
6718 (defun gnus-summary-best-group (&optional exclude-group)
6719   "Find the name of the best unread group.
6720 If EXCLUDE-GROUP, do not go to this group."
6721   (with-current-buffer gnus-group-buffer
6722     (save-excursion
6723       (gnus-group-best-unread-group exclude-group))))
6724
6725 (defun gnus-summary-find-next (&optional unread article backward)
6726   (if backward
6727       (gnus-summary-find-prev unread article)
6728     (let* ((dummy (gnus-summary-article-intangible-p))
6729            (article (or article (gnus-summary-article-number)))
6730            (data (gnus-data-find-list article))
6731            result)
6732       (when (and (not dummy)
6733                  (or (not gnus-summary-check-current)
6734                      (not unread)
6735                      (not (gnus-data-unread-p (car data)))))
6736         (setq data (cdr data)))
6737       (when (setq result
6738                   (if unread
6739                       (progn
6740                         (while data
6741                           (unless (memq (gnus-data-number (car data))
6742                                         (cond
6743                                          ((eq gnus-auto-goto-ignores
6744                                               'always-undownloaded)
6745                                           gnus-newsgroup-undownloaded)
6746                                          (gnus-plugged
6747                                           nil)
6748                                          ((eq gnus-auto-goto-ignores
6749                                               'unfetched)
6750                                           gnus-newsgroup-unfetched)
6751                                          ((eq gnus-auto-goto-ignores
6752                                               'undownloaded)
6753                                           gnus-newsgroup-undownloaded)))
6754                             (when (gnus-data-unread-p (car data))
6755                               (setq result (car data)
6756                                     data nil)))
6757                           (setq data (cdr data)))
6758                         result)
6759                     (car data)))
6760         (goto-char (gnus-data-pos result))
6761         (gnus-data-number result)))))
6762
6763 (defun gnus-summary-find-prev (&optional unread article)
6764   (let* ((eobp (eobp))
6765          (article (or article (gnus-summary-article-number)))
6766          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6767          result)
6768     (when (and (not eobp)
6769                (or (not gnus-summary-check-current)
6770                    (not unread)
6771                    (not (gnus-data-unread-p (car data)))))
6772       (setq data (cdr data)))
6773     (when (setq result
6774                 (if unread
6775                     (progn
6776                       (while data
6777                         (unless (memq (gnus-data-number (car data))
6778                                       (cond
6779                                        ((eq gnus-auto-goto-ignores
6780                                             'always-undownloaded)
6781                                         gnus-newsgroup-undownloaded)
6782                                        (gnus-plugged
6783                                         nil)
6784                                        ((eq gnus-auto-goto-ignores
6785                                             'unfetched)
6786                                         gnus-newsgroup-unfetched)
6787                                        ((eq gnus-auto-goto-ignores
6788                                             'undownloaded)
6789                                         gnus-newsgroup-undownloaded)))
6790                           (when (gnus-data-unread-p (car data))
6791                             (setq result (car data)
6792                                   data nil)))
6793                         (setq data (cdr data)))
6794                       result)
6795                   (car data)))
6796       (goto-char (gnus-data-pos result))
6797       (gnus-data-number result))))
6798
6799 (defun gnus-summary-find-subject (subject &optional unread backward article)
6800   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6801          (article (or article (gnus-summary-article-number)))
6802          (articles (gnus-data-list backward))
6803          (arts (gnus-data-find-list article articles))
6804          result)
6805     (when (or (not gnus-summary-check-current)
6806               (not unread)
6807               (not (gnus-data-unread-p (car arts))))
6808       (setq arts (cdr arts)))
6809     (while arts
6810       (and (or (not unread)
6811                (gnus-data-unread-p (car arts)))
6812            (vectorp (gnus-data-header (car arts)))
6813            (gnus-subject-equal
6814             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6815            (setq result (car arts)
6816                  arts nil))
6817       (setq arts (cdr arts)))
6818     (and result
6819          (goto-char (gnus-data-pos result))
6820          (gnus-data-number result))))
6821
6822 (defun gnus-summary-search-forward (&optional unread subject backward)
6823   "Search forward for an article.
6824 If UNREAD, look for unread articles.  If SUBJECT, look for
6825 articles with that subject.  If BACKWARD, search backward instead."
6826   (cond (subject (gnus-summary-find-subject subject unread backward))
6827         (backward (gnus-summary-find-prev unread))
6828         (t (gnus-summary-find-next unread))))
6829
6830 (defun gnus-recenter (&optional n)
6831   "Center point in window and redisplay frame.
6832 Also do horizontal recentering."
6833   (interactive "P")
6834   (when (and gnus-auto-center-summary
6835              (not (eq gnus-auto-center-summary 'vertical)))
6836     (gnus-horizontal-recenter))
6837   (if (fboundp 'recenter-top-bottom)
6838       (recenter-top-bottom n)
6839     (recenter n)))
6840
6841 (put 'gnus-recenter 'isearch-scroll t)
6842
6843 (defun gnus-forward-line-ignore-invisible (n)
6844   "Move N lines forward (backward if N is negative).
6845 Like forward-line, but skip over (and don't count) invisible lines."
6846   (let (done)
6847     (while (and (> n 0) (not done))
6848       ;; If the following character is currently invisible,
6849       ;; skip all characters with that same `invisible' property value.
6850       (while (gnus-invisible-p (point))
6851         (goto-char (gnus-next-char-property-change (point))))
6852       (forward-line 1)
6853       (if (eobp)
6854           (setq done t)
6855         (setq n (1- n))))
6856     (while (and (< n 0) (not done))
6857       (forward-line -1)
6858       (if (bobp) (setq done t)
6859         (setq n (1+ n))
6860         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6861           (goto-char (gnus-previous-char-property-change (point))))))))
6862
6863 (defun gnus-summary-recenter ()
6864   "Center point in the summary window.
6865 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6866 displayed, no centering will be performed."
6867   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6868   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6869   (interactive)
6870   ;; The user has to want it.
6871   (when gnus-auto-center-summary
6872     (let* ((top (cond ((< (window-height) 4) 0)
6873                       ((< (window-height) 7) 1)
6874                       (t (if (numberp gnus-auto-center-summary)
6875                              gnus-auto-center-summary
6876                            (/ (1- (window-height)) 2)))))
6877            (height (1- (window-height)))
6878            (bottom (save-excursion
6879                      (goto-char (point-max))
6880                      (gnus-forward-line-ignore-invisible (- height))
6881                      (point)))
6882            (window (get-buffer-window (current-buffer))))
6883       (when (get-buffer-window gnus-article-buffer)
6884         ;; Only do recentering when the article buffer is displayed,
6885         ;; Set the window start to either `bottom', which is the biggest
6886         ;; possible valid number, or the second line from the top,
6887         ;; whichever is the least.
6888         (let ((top-pos (save-excursion
6889                          (gnus-forward-line-ignore-invisible (- top))
6890                          (point))))
6891           (if (> bottom top-pos)
6892               ;; Keep the second line from the top visible
6893               (set-window-start window top-pos)
6894             ;; Try to keep the bottom line visible; if it's partially
6895             ;; obscured, either scroll one more line to make it fully
6896             ;; visible, or revert to using TOP-POS.
6897             (save-excursion
6898               (goto-char (point-max))
6899               (gnus-forward-line-ignore-invisible -1)
6900               (let ((last-line-start (point)))
6901                 (goto-char bottom)
6902                 (set-window-start window (point) t)
6903                 (when (not (pos-visible-in-window-p last-line-start window))
6904                   (gnus-forward-line-ignore-invisible 1)
6905                   (set-window-start window (min (point) top-pos) t)))))))
6906       ;; Do horizontal recentering while we're at it.
6907       (when (and (get-buffer-window (current-buffer) t)
6908                  (not (eq gnus-auto-center-summary 'vertical)))
6909         (let ((selected (selected-window)))
6910           (select-window (get-buffer-window (current-buffer) t))
6911           (gnus-summary-position-point)
6912           (gnus-horizontal-recenter)
6913           (select-window selected))))))
6914
6915 (defun gnus-summary-jump-to-group (newsgroup)
6916   "Move point to NEWSGROUP in group mode buffer."
6917   ;; Keep update point of group mode buffer if visible.
6918   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6919       (save-window-excursion
6920         ;; Take care of tree window mode.
6921         (when (get-buffer-window gnus-group-buffer)
6922           (pop-to-buffer gnus-group-buffer))
6923         (gnus-group-jump-to-group newsgroup))
6924     (save-excursion
6925       ;; Take care of tree window mode.
6926       (if (get-buffer-window gnus-group-buffer 0)
6927           (pop-to-buffer gnus-group-buffer)
6928         (set-buffer gnus-group-buffer))
6929       (gnus-group-jump-to-group newsgroup))))
6930
6931 ;; This function returns a list of article numbers based on the
6932 ;; difference between the ranges of read articles in this group and
6933 ;; the range of active articles.
6934 (defun gnus-list-of-unread-articles (group)
6935   (let* ((read (gnus-info-read (gnus-get-info group)))
6936          (active (or (gnus-active group) (gnus-activate-group group)))
6937          (last (or (cdr active)
6938                    (error "Group %s couldn't be activated " group)))
6939          (bottom (if gnus-newsgroup-maximum-articles
6940                      (max (car active)
6941                           (- last gnus-newsgroup-maximum-articles -1))
6942                    (car active)))
6943          first nlast unread)
6944     ;; If none are read, then all are unread.
6945     (if (not read)
6946         (setq first bottom)
6947       ;; If the range of read articles is a single range, then the
6948       ;; first unread article is the article after the last read
6949       ;; article.  Sounds logical, doesn't it?
6950       (if (and (not (listp (cdr read)))
6951                (or (< (car read) bottom)
6952                    (progn (setq read (list read))
6953                           nil)))
6954           (setq first (max bottom (1+ (cdr read))))
6955         ;; `read' is a list of ranges.
6956         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6957                                   (caar read)))
6958                   1)
6959           (setq first bottom))
6960         (while read
6961           (when first
6962             (while (< first nlast)
6963               (setq unread (cons first unread)
6964                     first (1+ first))))
6965           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6966           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6967           (setq read (cdr read)))))
6968     ;; And add the last unread articles.
6969     (while (<= first last)
6970       (setq unread (cons first unread)
6971             first (1+ first)))
6972     ;; Return the list of unread articles.
6973     (delq 0 (nreverse unread))))
6974
6975 (defun gnus-list-of-read-articles (group)
6976   "Return a list of unread, unticked and non-dormant articles."
6977   (let* ((info (gnus-get-info group))
6978          (marked (gnus-info-marks info))
6979          (active (gnus-active group)))
6980     (and info active
6981          (gnus-list-range-difference
6982           (gnus-list-range-difference
6983            (gnus-sorted-complement
6984             (gnus-uncompress-range
6985              (if gnus-newsgroup-maximum-articles
6986                  (cons (max (car active)
6987                             (- (cdr active)
6988                                gnus-newsgroup-maximum-articles
6989                                -1))
6990                        (cdr active))
6991                active))
6992             (gnus-list-of-unread-articles group))
6993            (cdr (assq 'dormant marked)))
6994           (cdr (assq 'tick marked))))))
6995
6996 ;; This function returns a sequence of article numbers based on the
6997 ;; difference between the ranges of read articles in this group and
6998 ;; the range of active articles.
6999 (defun gnus-sequence-of-unread-articles (group)
7000   (let* ((read (gnus-info-read (gnus-get-info group)))
7001          (active (or (gnus-active group) (gnus-activate-group group)))
7002          (last (cdr active))
7003          (bottom (if gnus-newsgroup-maximum-articles
7004                      (max (car active)
7005                           (- last gnus-newsgroup-maximum-articles -1))
7006                    (car active)))
7007          first nlast unread)
7008     ;; If none are read, then all are unread.
7009     (if (not read)
7010         (setq first bottom)
7011       ;; If the range of read articles is a single range, then the
7012       ;; first unread article is the article after the last read
7013       ;; article.  Sounds logical, doesn't it?
7014       (if (and (not (listp (cdr read)))
7015                (or (< (car read) bottom)
7016                    (progn (setq read (list read))
7017                           nil)))
7018           (setq first (max bottom (1+ (cdr read))))
7019         ;; `read' is a list of ranges.
7020         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7021                                   (caar read)))
7022                   1)
7023           (setq first bottom))
7024         (while read
7025           (when first
7026             (push (cons first nlast) unread))
7027           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7028           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7029           (setq read (cdr read)))))
7030     ;; And add the last unread articles.
7031     (cond ((not (and first last))
7032            nil)
7033           ((< first last)
7034            (push (cons first last) unread))
7035           ((= first last)
7036            (push first unread)))
7037     ;; Return the sequence of unread articles.
7038     (delq 0 (nreverse unread))))
7039
7040 ;; Various summary commands
7041
7042 (defun gnus-summary-select-article-buffer ()
7043   "Reconfigure windows to show the article buffer.
7044 If `gnus-widen-article-window' is set, show only the article
7045 buffer."
7046   (interactive)
7047   (if (not (gnus-buffer-live-p gnus-article-buffer))
7048       (error "There is no article buffer for this summary buffer")
7049     (unless (get-buffer-window gnus-article-buffer)
7050       (gnus-summary-show-article))
7051     (gnus-configure-windows
7052      (if gnus-widen-article-window
7053          'only-article
7054        'article)
7055      t)
7056     (select-window (get-buffer-window gnus-article-buffer))))
7057
7058 (defun gnus-summary-universal-argument (arg)
7059   "Perform any operation on all articles that are process/prefixed."
7060   (interactive "P")
7061   (let ((articles (gnus-summary-work-articles arg))
7062         func article)
7063     (if (eq
7064          (setq
7065           func
7066           (key-binding
7067            (read-key-sequence
7068             (substitute-command-keys
7069              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7070          'undefined)
7071         (gnus-error 1 "Undefined key")
7072       (save-excursion
7073         (while articles
7074           (gnus-summary-goto-subject (setq article (pop articles)))
7075           (let (gnus-newsgroup-processable)
7076             (command-execute func))
7077           (gnus-summary-remove-process-mark article)))))
7078   (gnus-summary-position-point))
7079
7080 (defun gnus-summary-toggle-truncation (&optional arg)
7081   "Toggle truncation of summary lines.
7082 With ARG, turn line truncation on if ARG is positive."
7083   (interactive "P")
7084   (setq truncate-lines
7085         (if (null arg) (not truncate-lines)
7086           (> (prefix-numeric-value arg) 0)))
7087   (redraw-display))
7088
7089 (defun gnus-summary-find-for-reselect ()
7090   "Return the number of an article to stay on across a reselect.
7091 The current article is considered, then following articles, then previous
7092 articles.  An article is sought which is not canceled and isn't a temporary
7093 insertion from another group.  If there's no such then return a dummy 0."
7094   (let (found)
7095     (dolist (rev '(nil t))
7096       (unless found      ; don't demand the reverse list if we don't need it
7097         (let ((data (gnus-data-find-list
7098                      (gnus-summary-article-number) (gnus-data-list rev))))
7099           (while (and data (not found))
7100             (if (and (< 0 (gnus-data-number (car data)))
7101                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7102                 (setq found (gnus-data-number (car data))))
7103             (setq data (cdr data))))))
7104     (or found 0)))
7105
7106 (defun gnus-summary-reselect-current-group (&optional all rescan)
7107   "Exit and then reselect the current newsgroup.
7108 The prefix argument ALL means to select all articles."
7109   (interactive "P")
7110   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7111     (error "Ephemeral groups can't be reselected"))
7112   (let ((current-subject (gnus-summary-find-for-reselect))
7113         (group gnus-newsgroup-name))
7114     (setq gnus-newsgroup-begin nil)
7115     (gnus-summary-exit nil 'leave-hidden)
7116     ;; We have to adjust the point of group mode buffer because
7117     ;; point was moved to the next unread newsgroup by exiting.
7118     (gnus-summary-jump-to-group group)
7119     (when rescan
7120       (save-excursion
7121         (gnus-group-get-new-news-this-group 1)))
7122     (gnus-group-read-group all t)
7123     (gnus-summary-goto-subject current-subject nil t)))
7124
7125 (defun gnus-summary-rescan-group (&optional all)
7126   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7127   (interactive "P")
7128   (let ((config gnus-current-window-configuration))
7129     (gnus-summary-reselect-current-group all t)
7130     (gnus-configure-windows config)
7131     (when (eq config 'article)
7132       (gnus-summary-select-article))))
7133
7134 (defun gnus-summary-update-info (&optional non-destructive)
7135   (save-excursion
7136     (let ((group gnus-newsgroup-name))
7137       (when group
7138         (when gnus-newsgroup-kill-headers
7139           (setq gnus-newsgroup-killed
7140                 (gnus-compress-sequence
7141                  (gnus-sorted-union
7142                   (gnus-list-range-intersection
7143                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7144                   gnus-newsgroup-unreads)
7145                  t)))
7146         (unless (listp (cdr gnus-newsgroup-killed))
7147           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7148         (let ((headers gnus-newsgroup-headers)
7149               (ephemeral-p (gnus-ephemeral-group-p group))
7150               info)
7151           (unless ephemeral-p
7152             (setq info (copy-sequence (gnus-get-info group))
7153                   info (delq (gnus-info-params info) info)))
7154           ;; Set the new ranges of read articles.
7155           (with-current-buffer gnus-group-buffer
7156             (gnus-undo-force-boundary))
7157           (gnus-update-read-articles
7158            group (gnus-sorted-union
7159                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7160           ;; Set the current article marks.
7161           (let ((gnus-newsgroup-scored
7162                  (if (and (not gnus-save-score)
7163                           (not non-destructive))
7164                      nil
7165                    gnus-newsgroup-scored)))
7166             (save-excursion
7167               (gnus-update-marks)))
7168           ;; Do the cross-ref thing.
7169           (when gnus-use-cross-reference
7170             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7171           ;; Do not switch windows but change the buffer to work.
7172           (set-buffer gnus-group-buffer)
7173           (unless ephemeral-p
7174             (gnus-group-update-group
7175              group nil
7176              (equal info
7177                     (setq info (copy-sequence (gnus-get-info group))
7178                           info (delq (gnus-info-params info) info))))))))))
7179
7180 (defun gnus-summary-save-newsrc (&optional force)
7181   "Save the current number of read/marked articles in the dribble buffer.
7182 The dribble buffer will then be saved.
7183 If FORCE (the prefix), also save the .newsrc file(s)."
7184   (interactive "P")
7185   (gnus-summary-update-info t)
7186   (if force
7187       (gnus-save-newsrc-file)
7188     (gnus-dribble-save)))
7189
7190 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7191
7192 (defun gnus-summary-exit (&optional temporary leave-hidden)
7193   "Exit reading current newsgroup, and then return to group selection mode.
7194 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7195   (interactive)
7196   (gnus-set-global-variables)
7197   (when (gnus-buffer-live-p gnus-article-buffer)
7198     (with-current-buffer gnus-article-buffer
7199       (mm-destroy-parts gnus-article-mime-handles)
7200       ;; Set it to nil for safety reason.
7201       (setq gnus-article-mime-handle-alist nil)
7202       (setq gnus-article-mime-handles nil)))
7203   (gnus-kill-save-kill-buffer)
7204   (gnus-async-halt-prefetch)
7205   (let* ((group gnus-newsgroup-name)
7206          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7207          (gnus-group-is-exiting-p t)
7208          (article-buffer gnus-article-buffer)
7209          (original-article-buffer gnus-original-article-buffer)
7210          (mode major-mode)
7211          (group-point nil)
7212          (buf (current-buffer))
7213          ;; `gnus-single-article-buffer' is nil buffer-locally in
7214          ;; ephemeral group of which summary buffer will be killed,
7215          ;; but the global value may be non-nil.
7216          (single-article-buffer gnus-single-article-buffer))
7217     (unless quit-config
7218       ;; Do adaptive scoring, and possibly save score files.
7219       (when gnus-newsgroup-adaptive
7220         (gnus-score-adaptive))
7221       (when gnus-use-scoring
7222         (gnus-score-save)))
7223     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7224     (when gnus-use-cache
7225       (gnus-cache-possibly-remove-articles)
7226       (gnus-cache-save-buffers))
7227     (gnus-async-prefetch-remove-group group)
7228     (when gnus-suppress-duplicates
7229       (gnus-dup-enter-articles))
7230     (when gnus-use-trees
7231       (gnus-tree-close group))
7232     (when gnus-use-cache
7233       (gnus-cache-write-active))
7234     ;; Remove entries for this group.
7235     (nnmail-purge-split-history (gnus-group-real-name group))
7236     ;; Make all changes in this group permanent.
7237     (unless quit-config
7238       (gnus-run-hooks 'gnus-exit-group-hook)
7239       (gnus-summary-update-info))
7240     (gnus-close-group group)
7241     ;; Make sure where we were, and go to next newsgroup.
7242     (set-buffer gnus-group-buffer)
7243     (unless quit-config
7244       (gnus-group-jump-to-group group))
7245     (gnus-run-hooks 'gnus-summary-exit-hook)
7246     (unless (or quit-config
7247                 (not gnus-summary-next-group-on-exit)
7248                 ;; If this group has disappeared from the summary
7249                 ;; buffer, don't skip forwards.
7250                 (not (string= group (gnus-group-group-name))))
7251       (gnus-group-next-unread-group 1))
7252     (setq group-point (point))
7253     (if temporary
7254         nil                             ;Nothing to do.
7255       (set-buffer buf)
7256       (if (not gnus-kill-summary-on-exit)
7257           (progn
7258             (gnus-deaden-summary)
7259             (setq mode nil))
7260         (when (get-buffer gnus-article-buffer)
7261           (bury-buffer gnus-article-buffer))
7262         ;; Return to group mode buffer.
7263         (when (eq mode 'gnus-summary-mode)
7264           (gnus-kill-buffer buf)))
7265
7266       (setq gnus-current-select-method gnus-select-method)
7267       (set-buffer gnus-group-buffer)
7268       (if quit-config
7269           (gnus-handle-ephemeral-exit quit-config)
7270         (goto-char group-point)
7271         ;; If gnus-group-buffer is already displayed, make sure we also move
7272         ;; the cursor in the window that displays it.
7273         (let ((win (get-buffer-window (current-buffer) 0)))
7274           (if win (set-window-point win (point))))
7275         (unless leave-hidden
7276           (gnus-configure-windows 'group 'force)))
7277
7278       ;; If we have several article buffers, we kill them at exit.
7279       (unless single-article-buffer
7280         (when (gnus-buffer-live-p article-buffer)
7281           (with-current-buffer article-buffer
7282             ;; Don't kill sticky article buffers
7283             (unless (eq major-mode 'gnus-sticky-article-mode)
7284               (gnus-kill-buffer article-buffer)
7285               (setq gnus-article-current nil))))
7286         (gnus-kill-buffer original-article-buffer))
7287
7288       ;; Clear the current group name.
7289       (unless quit-config
7290         (setq gnus-newsgroup-name nil)))))
7291
7292 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7293 (defun gnus-summary-exit-no-update (&optional no-questions)
7294   "Quit reading current newsgroup without updating read article info."
7295   (interactive)
7296   (let* ((group gnus-newsgroup-name)
7297          (gnus-group-is-exiting-p t)
7298          (gnus-group-is-exiting-without-update-p t)
7299          (quit-config (gnus-group-quit-config group)))
7300     (when (or no-questions
7301               gnus-expert-user
7302               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7303       (gnus-async-halt-prefetch)
7304       (run-hooks 'gnus-summary-prepare-exit-hook)
7305       (when (gnus-buffer-live-p gnus-article-buffer)
7306         (with-current-buffer gnus-article-buffer
7307           (gnus-article-stop-animations)
7308           (mm-destroy-parts gnus-article-mime-handles)
7309           ;; Set it to nil for safety reason.
7310           (setq gnus-article-mime-handle-alist nil)
7311           (setq gnus-article-mime-handles nil)))
7312       ;; If we have several article buffers, we kill them at exit.
7313       (unless gnus-single-article-buffer
7314         (gnus-kill-buffer gnus-article-buffer)
7315         (gnus-kill-buffer gnus-original-article-buffer)
7316         (setq gnus-article-current nil))
7317       ;; Return to the group buffer.
7318       (gnus-configure-windows 'group 'force)
7319       (if (not gnus-kill-summary-on-exit)
7320           (gnus-deaden-summary)
7321         (gnus-close-group group)
7322         (gnus-kill-buffer gnus-summary-buffer))
7323       (unless gnus-single-article-buffer
7324         (setq gnus-article-current nil))
7325       (when gnus-use-trees
7326         (gnus-tree-close group))
7327       (gnus-async-prefetch-remove-group group)
7328       (when (get-buffer gnus-article-buffer)
7329         (bury-buffer gnus-article-buffer))
7330       ;; Clear the current group name.
7331       (setq gnus-newsgroup-name nil)
7332       (unless (gnus-ephemeral-group-p group)
7333         (gnus-group-update-group group nil t))
7334       (when (equal (gnus-group-group-name) group)
7335         (gnus-group-next-unread-group 1))
7336       (when quit-config
7337         (gnus-handle-ephemeral-exit quit-config)))))
7338
7339 (defun gnus-handle-ephemeral-exit (quit-config)
7340   "Handle movement when leaving an ephemeral group.
7341 The state which existed when entering the ephemeral is reset."
7342   (if (not (buffer-name (car quit-config)))
7343       (gnus-configure-windows 'group 'force)
7344     (set-buffer (car quit-config))
7345     (unless (eq (cdr quit-config) 'group)
7346       (setq gnus-current-select-method
7347             (gnus-find-method-for-group gnus-newsgroup-name)))
7348     (cond ((eq major-mode 'gnus-summary-mode)
7349            (gnus-set-global-variables))
7350           ((eq major-mode 'gnus-article-mode)
7351            (save-current-buffer
7352              ;; The `gnus-summary-buffer' variable may point
7353              ;; to the old summary buffer when using a single
7354              ;; article buffer.
7355              (unless (gnus-buffer-live-p gnus-summary-buffer)
7356                (set-buffer gnus-group-buffer))
7357              (set-buffer gnus-summary-buffer)
7358              (gnus-set-global-variables))))
7359     (if (or (eq (cdr quit-config) 'article)
7360             (eq (cdr quit-config) 'pick))
7361         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7362             (gnus-configure-windows 'pick 'force)
7363           (gnus-configure-windows (cdr quit-config) 'force))
7364       (gnus-configure-windows (cdr quit-config) 'force))
7365     (when (eq major-mode 'gnus-summary-mode)
7366       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7367                                                      next-unread-noselect))
7368           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7369                                   'next-noselect)
7370                               (gnus-summary-next-subject 1 nil t))
7371                              ((eq gnus-auto-select-on-ephemeral-exit
7372                                   'next-unread-noselect)
7373                               (gnus-summary-next-subject 1 t t))))
7374             ;; Hide the article buffer which displays the article different
7375             ;; from the one that the cursor points to in the summary buffer.
7376             (gnus-configure-windows 'summary 'force))
7377         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7378                (gnus-summary-next-subject 1))
7379               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7380                (gnus-summary-next-subject 1 t))))
7381       (gnus-summary-recenter)
7382       (gnus-summary-position-point))))
7383
7384 ;;; Dead summaries.
7385
7386 (defvar gnus-dead-summary-mode-map
7387   (let ((map (make-keymap)))
7388     (suppress-keymap map)
7389     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7390     (dolist (key '("\C-d" "\r" "\177" [delete]))
7391       (define-key map key 'gnus-summary-wake-up-the-dead))
7392     (dolist (key '("q" "Q"))
7393       (define-key map key 'bury-buffer))
7394     map))
7395
7396 (define-minor-mode gnus-dead-summary-mode
7397   "Minor mode for Gnus summary buffers."
7398   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7399   (unless (derived-mode-p 'gnus-summary-mode)
7400     (setq gnus-dead-summary-mode nil)))
7401
7402 (defun gnus-deaden-summary ()
7403   "Make the current summary buffer into a dead summary buffer."
7404   ;; Kill any previous dead summary buffer.
7405   (when (and gnus-dead-summary
7406              (buffer-name gnus-dead-summary))
7407     (with-current-buffer gnus-dead-summary
7408       (when gnus-dead-summary-mode
7409         (kill-buffer (current-buffer)))))
7410   ;; Make this the current dead summary.
7411   (setq gnus-dead-summary (current-buffer))
7412   (gnus-dead-summary-mode 1)
7413   (let ((name (buffer-name)))
7414     (when (string-match "Summary" name)
7415       (rename-buffer
7416        (concat (substring name 0 (match-beginning 0)) "Dead "
7417                (substring name (match-beginning 0)))
7418        t)
7419       (bury-buffer))))
7420
7421 (defun gnus-kill-or-deaden-summary (buffer)
7422   "Kill or deaden the summary BUFFER."
7423   (save-excursion
7424     (when (and (buffer-name buffer)
7425                (not gnus-single-article-buffer))
7426       (with-current-buffer buffer
7427         (gnus-kill-buffer gnus-article-buffer)
7428         (gnus-kill-buffer gnus-original-article-buffer)))
7429     (cond
7430      ;; Kill the buffer.
7431      (gnus-kill-summary-on-exit
7432       (when (and gnus-use-trees
7433                  (gnus-buffer-exists-p buffer))
7434         (with-current-buffer buffer
7435           (gnus-tree-close gnus-newsgroup-name)))
7436       (gnus-kill-buffer buffer))
7437      ;; Deaden the buffer.
7438      ((gnus-buffer-exists-p buffer)
7439       (with-current-buffer buffer
7440         (gnus-deaden-summary))))))
7441
7442 (defun gnus-summary-wake-up-the-dead (&rest args)
7443   "Wake up the dead summary buffer."
7444   (interactive)
7445   (gnus-dead-summary-mode -1)
7446   (let ((name (buffer-name)))
7447     (when (string-match "Dead " name)
7448       (rename-buffer
7449        (concat (substring name 0 (match-beginning 0))
7450                (substring name (match-end 0)))
7451        t)))
7452   (gnus-message 3 "This dead summary is now alive again"))
7453
7454 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7455 (defun gnus-summary-describe-group (&optional force)
7456   "Describe the current newsgroup."
7457   (interactive "P")
7458   (gnus-group-describe-group force gnus-newsgroup-name))
7459
7460 (defun gnus-summary-describe-briefly ()
7461   "Describe summary mode commands briefly."
7462   (interactive)
7463   (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")))
7464
7465 ;; Walking around group mode buffer from summary mode.
7466
7467 (defun gnus-summary-next-group (&optional no-article target-group backward)
7468   "Exit current newsgroup and then select next unread newsgroup.
7469 If prefix argument NO-ARTICLE is non-nil, no article is selected
7470 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7471 previous group instead."
7472   (interactive "P")
7473   ;; Stop pre-fetching.
7474   (gnus-async-halt-prefetch)
7475   (let ((current-group gnus-newsgroup-name)
7476         (current-buffer (current-buffer))
7477         entered)
7478     ;; First we semi-exit this group to update Xrefs and all variables.
7479     ;; We can't do a real exit, because the window conf must remain
7480     ;; the same in case the user is prompted for info, and we don't
7481     ;; want the window conf to change before that...
7482     (gnus-summary-exit t)
7483     (while (not entered)
7484       ;; Then we find what group we are supposed to enter.
7485       (set-buffer gnus-group-buffer)
7486       (gnus-group-jump-to-group current-group)
7487       (setq target-group
7488             (or target-group
7489                 (if (eq gnus-keep-same-level 'best)
7490                     (gnus-summary-best-group gnus-newsgroup-name)
7491                   (gnus-summary-search-group backward gnus-keep-same-level))))
7492       (if (not target-group)
7493           ;; There are no further groups, so we return to the group
7494           ;; buffer.
7495           (progn
7496             (gnus-message 5 "Returning to the group buffer")
7497             (setq entered t)
7498             (when (gnus-buffer-live-p current-buffer)
7499               (set-buffer current-buffer)
7500               (gnus-summary-exit))
7501             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7502         ;; We try to enter the target group.
7503         (gnus-group-jump-to-group target-group)
7504         (let ((unreads (gnus-group-group-unread)))
7505           (if (and (or (eq t unreads)
7506                        (and unreads (not (zerop unreads))))
7507                    (gnus-summary-read-group
7508                     target-group nil no-article
7509                     (and (buffer-name current-buffer) current-buffer)
7510                     nil backward))
7511               (setq entered t)
7512             (setq current-group target-group
7513                   target-group nil)))))))
7514
7515 (defun gnus-summary-prev-group (&optional no-article)
7516   "Exit current newsgroup and then select previous unread newsgroup.
7517 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7518   (interactive "P")
7519   (gnus-summary-next-group no-article nil t))
7520
7521 ;; Walking around summary lines.
7522
7523 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7524   "Go to the first subject satisfying any non-nil constraint.
7525 If UNREAD is non-nil, the article should be unread.
7526 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7527 If UNSEEN is non-nil, the article should be unseen as well as unread.
7528 Returns the article selected or nil if there are no matching articles."
7529   (interactive "P")
7530   (cond
7531    ;; Empty summary.
7532    ((null gnus-newsgroup-data)
7533     (gnus-message 3 "No articles in the group")
7534     nil)
7535    ;; Pick the first article.
7536    ((not (or unread undownloaded unseen))
7537     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7538     (gnus-data-number (car gnus-newsgroup-data)))
7539    ;; Find the first unread article.
7540    (t
7541     (let ((data gnus-newsgroup-data))
7542       (while (and data
7543                   (let ((num (gnus-data-number (car data))))
7544                     (or (memq num gnus-newsgroup-unfetched)
7545                         (not (or (and unread
7546                                       (memq num gnus-newsgroup-unreads))
7547                                  (and undownloaded
7548                                       (memq num gnus-newsgroup-undownloaded))
7549                                  (and unseen
7550                                       (memq num gnus-newsgroup-unseen)
7551                                       (memq num gnus-newsgroup-unreads)))))))
7552         (setq data (cdr data)))
7553       (prog1
7554           (if data
7555               (progn
7556                 (goto-char (gnus-data-pos (car data)))
7557                 (gnus-data-number (car data)))
7558             (gnus-message 3 "No more%s articles"
7559                           (let* ((r (when unread " unread"))
7560                                  (d (when undownloaded " undownloaded"))
7561                                  (s (when unseen " unseen"))
7562                                  (l (delq nil (list r d s))))
7563                             (cond ((= 3 (length l))
7564                                    (concat r "," d ", or" s))
7565                                   ((= 2 (length l))
7566                                    (concat (car l) ", or" (cadr l)))
7567                                   ((= 1 (length l))
7568                                    (car l))
7569                                   (t
7570                                    ""))))
7571             nil
7572             )
7573         (gnus-summary-position-point))))))
7574
7575 (defun gnus-summary-next-subject (n &optional unread dont-display)
7576   "Go to next N'th summary line.
7577 If N is negative, go to the previous N'th subject line.
7578 If UNREAD is non-nil, only unread articles are selected.
7579 The difference between N and the actual number of steps taken is
7580 returned."
7581   (interactive "p")
7582   (let ((backward (< n 0))
7583         (n (abs n)))
7584     (while (and (> n 0)
7585                 (if backward
7586                     (gnus-summary-find-prev unread)
7587                   (gnus-summary-find-next unread)))
7588       (unless (zerop (setq n (1- n)))
7589         (gnus-summary-show-thread)))
7590     (when (/= 0 n)
7591       (gnus-message 7 "No more%s articles"
7592                     (if unread " unread" "")))
7593     (unless dont-display
7594       (gnus-summary-recenter)
7595       (gnus-summary-position-point))
7596     n))
7597
7598 (defun gnus-summary-next-unread-subject (n)
7599   "Go to next N'th unread summary line."
7600   (interactive "p")
7601   (gnus-summary-next-subject n t))
7602
7603 (defun gnus-summary-prev-subject (n &optional unread)
7604   "Go to previous N'th summary line.
7605 If optional argument UNREAD is non-nil, only unread article is selected."
7606   (interactive "p")
7607   (gnus-summary-next-subject (- n) unread))
7608
7609 (defun gnus-summary-prev-unread-subject (n)
7610   "Go to previous N'th unread summary line."
7611   (interactive "p")
7612   (gnus-summary-next-subject (- n) t))
7613
7614 (defun gnus-summary-goto-subjects (articles)
7615   "Insert the subject header for ARTICLES in the current buffer."
7616   (save-excursion
7617     (dolist (article articles)
7618       (gnus-summary-goto-subject article t)))
7619   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7620   (gnus-summary-position-point))
7621
7622 (defun gnus-summary-goto-subject (article &optional force silent)
7623   "Go to the subject line of ARTICLE.
7624 If FORCE, also allow jumping to articles not currently shown."
7625   (interactive "nArticle number: ")
7626   (unless (numberp article)
7627     (error "Article %s is not a number" article))
7628   (let ((b (point))
7629         (data (gnus-data-find article)))
7630     ;; We read in the article if we have to.
7631     (and (not data)
7632          force
7633          (gnus-summary-insert-subject
7634           article
7635           (if (or (numberp force) (vectorp force)) force)
7636           t)
7637          (setq data (gnus-data-find article)))
7638     (goto-char b)
7639     (if (not data)
7640         (progn
7641           (unless silent
7642             (gnus-message 3 "Can't find article %d" article))
7643           nil)
7644       (let ((pt (gnus-data-pos data)))
7645         (goto-char pt)
7646         (gnus-summary-set-article-display-arrow pt))
7647       (gnus-summary-position-point)
7648       article)))
7649
7650 ;; Walking around summary lines with displaying articles.
7651
7652 (defun gnus-summary-expand-window (&optional arg)
7653   "Make the summary buffer take up the entire Emacs frame.
7654 Given a prefix, will force an `article' buffer configuration."
7655   (interactive "P")
7656   (if arg
7657       (gnus-configure-windows 'article 'force)
7658     (gnus-configure-windows 'summary 'force)))
7659
7660 (defun gnus-summary-display-article (article &optional all-header)
7661   "Display ARTICLE in article buffer."
7662   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7663                (with-current-buffer gnus-article-buffer
7664                  (eq major-mode 'gnus-article-mode)))
7665     (gnus-article-setup-buffer))
7666   (gnus-set-global-variables)
7667   (with-current-buffer gnus-article-buffer
7668     (setq gnus-article-charset gnus-newsgroup-charset)
7669     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7670     (mm-enable-multibyte))
7671   (if (null article)
7672       nil
7673     (prog1
7674         (if gnus-summary-display-article-function
7675             (funcall gnus-summary-display-article-function article all-header)
7676           (gnus-article-prepare article all-header))
7677       (gnus-run-hooks 'gnus-select-article-hook)
7678       (when (and gnus-current-article
7679                  (not (zerop gnus-current-article)))
7680         (gnus-summary-goto-subject gnus-current-article))
7681       (gnus-summary-recenter)
7682       (when (and gnus-use-trees gnus-show-threads)
7683         (gnus-possibly-generate-tree article)
7684         (gnus-highlight-selected-tree article))
7685       ;; Successfully display article.
7686       (gnus-article-set-window-start
7687        (cdr (assq article gnus-newsgroup-bookmarks))))))
7688
7689 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7690   "Select the current article.
7691 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7692 non-nil, the article will be re-fetched even if it already present in
7693 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7694 be displayed."
7695   ;; Make sure we are in the summary buffer to work around bbdb bug.
7696   (unless (eq major-mode 'gnus-summary-mode)
7697     (set-buffer gnus-summary-buffer))
7698   (let ((article (or article (gnus-summary-article-number)))
7699         (all-headers (not (not all-headers))) ;Must be t or nil.
7700         gnus-summary-display-article-function)
7701     (and (not pseudo)
7702          (gnus-summary-article-pseudo-p article)
7703          (error "This is a pseudo-article"))
7704     (with-current-buffer gnus-summary-buffer
7705       (if (or (and gnus-single-article-buffer
7706                    (or (null gnus-current-article)
7707                        (null gnus-article-current)
7708                        (null (get-buffer gnus-article-buffer))
7709                        (not (eq article (cdr gnus-article-current)))
7710                        (not (equal (car gnus-article-current)
7711                                    gnus-newsgroup-name))
7712                        (not (get-buffer gnus-original-article-buffer))))
7713               (and (not gnus-single-article-buffer)
7714                    (or (null gnus-current-article)
7715                        (not (get-buffer gnus-original-article-buffer))
7716                        (not (eq gnus-current-article article))))
7717               force)
7718           ;; The requested article is different from the current article.
7719           (progn
7720             (gnus-summary-display-article article all-headers)
7721             (when (gnus-buffer-live-p gnus-article-buffer)
7722               (with-current-buffer gnus-article-buffer
7723                 (if (not gnus-article-decoded-p) ;; a local variable
7724                     (mm-disable-multibyte))))
7725             (gnus-article-set-window-start
7726              (cdr (assq article gnus-newsgroup-bookmarks)))
7727             article)
7728         'old))))
7729
7730 (defun gnus-summary-force-verify-and-decrypt ()
7731   "Display buttons for signed/encrypted parts and verify/decrypt them."
7732   (interactive)
7733   (let ((mm-verify-option 'known)
7734         (mm-decrypt-option 'known)
7735         (gnus-article-emulate-mime t)
7736         (gnus-buttonized-mime-types (append (list "multipart/signed"
7737                                                   "multipart/encrypted")
7738                                             gnus-buttonized-mime-types)))
7739     (gnus-summary-select-article nil 'force)))
7740
7741 (defun gnus-summary-set-current-mark (&optional current-mark)
7742   "Obsolete function."
7743   nil)
7744
7745 (defun gnus-summary-next-article (&optional unread subject backward push)
7746   "Select the next article.
7747 If UNREAD, only unread articles are selected.
7748 If SUBJECT, only articles with SUBJECT are selected.
7749 If BACKWARD, the previous article is selected instead of the next."
7750   (interactive "P")
7751   ;; Make sure we are in the summary buffer.
7752   (unless (eq major-mode 'gnus-summary-mode)
7753     (set-buffer gnus-summary-buffer))
7754   (cond
7755    ;; Is there such an article?
7756    ((and (gnus-summary-search-forward unread subject backward)
7757          (or (gnus-summary-display-article (gnus-summary-article-number))
7758              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7759     (gnus-summary-position-point))
7760    ;; If not, we try the first unread, if that is wanted.
7761    ((and subject
7762          gnus-auto-select-same
7763          (gnus-summary-first-unread-article))
7764     (gnus-summary-position-point)
7765     (gnus-message 6 "Wrapped"))
7766    ;; Try to get next/previous article not displayed in this group.
7767    ((and gnus-auto-extend-newsgroup
7768          (not unread) (not subject))
7769     (gnus-summary-goto-article
7770      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7771      nil (count-lines (point-min) (point))))
7772    ;; Go to next/previous group.
7773    (t
7774     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7775       (gnus-summary-jump-to-group gnus-newsgroup-name))
7776     (let ((cmd (if (featurep 'xemacs)
7777                    last-command-char
7778                  last-command-event))
7779           (point
7780            (with-current-buffer gnus-group-buffer
7781              (point)))
7782           (current-summary (current-buffer))
7783           (group
7784            (if (eq gnus-keep-same-level 'best)
7785                (gnus-summary-best-group gnus-newsgroup-name)
7786              (gnus-summary-search-group backward gnus-keep-same-level))))
7787       ;; Select next unread newsgroup automagically.
7788       (cond
7789        ((or (not gnus-auto-select-next)
7790             (not cmd))
7791         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7792        ((or (eq gnus-auto-select-next 'quietly)
7793             (and (eq gnus-auto-select-next 'slightly-quietly)
7794                  push)
7795             (and (eq gnus-auto-select-next 'almost-quietly)
7796                  (gnus-summary-last-article-p)))
7797         ;; Select quietly.
7798         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7799             (gnus-summary-exit)
7800           (gnus-message 7 "No more%s articles (%s)..."
7801                         (if unread " unread" "")
7802                         (if group (concat "selecting " group)
7803                           "exiting"))
7804           (gnus-summary-next-group nil group backward)))
7805        (t
7806         (when (gnus-key-press-event-p last-input-event)
7807           ;; Somehow or other, we may now have selected a different
7808           ;; window.  Make point go back to the summary buffer.
7809           (when (eq current-summary (current-buffer))
7810             ;; FIXME: This burps when get-buffer-window returns nil.
7811             (select-window (get-buffer-window current-summary 0)))
7812           (gnus-summary-walk-group-buffer
7813            gnus-newsgroup-name cmd unread backward point))))))))
7814
7815 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7816   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7817                       (?\C-p (gnus-group-prev-unread-group 1))))
7818         (cursor-in-echo-area t)
7819         keve key group ended prompt)
7820     (with-current-buffer gnus-group-buffer
7821       (goto-char start)
7822       (setq group
7823             (if (eq gnus-keep-same-level 'best)
7824                 (gnus-summary-best-group gnus-newsgroup-name)
7825               (gnus-summary-search-group backward gnus-keep-same-level))))
7826     (while (not ended)
7827       (setq prompt
7828             (format
7829              "No more%s articles%s " (if unread " unread" "")
7830              (if (and group
7831                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7832                  (format " (Type %s for %s [%s])"
7833                          (single-key-description cmd)
7834                          (gnus-group-decoded-name group)
7835                          (gnus-group-unread group))
7836                (format " (Type %s to exit %s)"
7837                        (single-key-description cmd)
7838                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7839       ;; Confirm auto selection.
7840       (setq key (car (setq keve (gnus-read-event-char prompt)))
7841             ended t)
7842       (cond
7843        ((assq key keystrokes)
7844         (let ((obuf (current-buffer)))
7845           (switch-to-buffer gnus-group-buffer)
7846           (when group
7847             (gnus-group-jump-to-group group))
7848           (eval (cadr (assq key keystrokes)))
7849           (setq group (gnus-group-group-name))
7850           (switch-to-buffer obuf))
7851         (setq ended nil))
7852        ((equal key cmd)
7853         (if (or (not group)
7854                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7855             (gnus-summary-exit)
7856           (gnus-summary-next-group nil group backward)))
7857        (t
7858         (push (cdr keve) unread-command-events))))))
7859
7860 (defun gnus-summary-next-unread-article ()
7861   "Select unread article after current one."
7862   (interactive)
7863   (gnus-summary-next-article
7864    (or (not (eq gnus-summary-goto-unread 'never))
7865        (gnus-summary-last-article-p (gnus-summary-article-number)))
7866    (and gnus-auto-select-same
7867         (gnus-summary-article-subject))))
7868
7869 (defun gnus-summary-prev-article (&optional unread subject)
7870   "Select the article before the current one.
7871 If UNREAD is non-nil, only unread articles are selected."
7872   (interactive "P")
7873   (gnus-summary-next-article unread subject t))
7874
7875 (defun gnus-summary-prev-unread-article ()
7876   "Select unread article before current one."
7877   (interactive)
7878   (gnus-summary-prev-article
7879    (or (not (eq gnus-summary-goto-unread 'never))
7880        (gnus-summary-first-article-p (gnus-summary-article-number)))
7881    (and gnus-auto-select-same
7882         (gnus-summary-article-subject))))
7883
7884 (defun gnus-summary-next-page (&optional lines circular stop)
7885   "Show next page of the selected article.
7886 If at the end of the current article, select the next article.
7887 LINES says how many lines should be scrolled up.
7888
7889 If CIRCULAR is non-nil, go to the start of the article instead of
7890 selecting the next article when reaching the end of the current
7891 article.
7892
7893 If STOP is non-nil, just stop when reaching the end of the message.
7894
7895 Also see the variable `gnus-article-skip-boring'."
7896   (interactive "P")
7897   (setq gnus-summary-buffer (current-buffer))
7898   (gnus-set-global-variables)
7899   (let ((article (gnus-summary-article-number))
7900         (article-window (get-buffer-window gnus-article-buffer t))
7901         endp)
7902     ;; If the buffer is empty, we have no article.
7903     (unless article
7904       (error "No article to select"))
7905     (gnus-configure-windows 'article)
7906     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7907         (if (and (eq gnus-summary-goto-unread 'never)
7908                  (not (gnus-summary-last-article-p article)))
7909             (gnus-summary-next-article)
7910           (gnus-summary-next-unread-article))
7911       (if (or (null gnus-current-article)
7912               (null gnus-article-current)
7913               (/= article (cdr gnus-article-current))
7914               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7915           ;; Selected subject is different from current article's.
7916           (gnus-summary-display-article article)
7917         (when article-window
7918           (gnus-eval-in-buffer-window gnus-article-buffer
7919             (setq endp (or (gnus-article-next-page lines)
7920                            (gnus-article-only-boring-p))))
7921           (when endp
7922             (cond ((or stop gnus-summary-stop-at-end-of-message)
7923                    (gnus-message 3 "End of message"))
7924                   (circular
7925                    (gnus-summary-beginning-of-article))
7926                   (lines
7927                    (gnus-message 3 "End of message"))
7928                   ((null lines)
7929                    (if (and (eq gnus-summary-goto-unread 'never)
7930                             (not (gnus-summary-last-article-p article)))
7931                        (gnus-summary-next-article)
7932                      (gnus-summary-next-unread-article))))))))
7933     (gnus-summary-recenter)
7934     (gnus-summary-position-point)))
7935
7936 (defun gnus-summary-prev-page (&optional lines move)
7937   "Show previous page of selected article.
7938 Argument LINES specifies lines to be scrolled down.
7939 If MOVE, move to the previous unread article if point is at
7940 the beginning of the buffer."
7941   (interactive "P")
7942   (let ((article (gnus-summary-article-number))
7943         (article-window (get-buffer-window gnus-article-buffer t))
7944         endp)
7945     (gnus-configure-windows 'article)
7946     (if (or (null gnus-current-article)
7947             (null gnus-article-current)
7948             (/= article (cdr gnus-article-current))
7949             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7950         ;; Selected subject is different from current article's.
7951         (gnus-summary-display-article article)
7952       (gnus-summary-recenter)
7953       (when article-window
7954         (gnus-eval-in-buffer-window gnus-article-buffer
7955           (setq endp (gnus-article-prev-page lines)))
7956         (when (and move endp)
7957           (cond (lines
7958                  (gnus-message 3 "Beginning of message"))
7959                 ((null lines)
7960                  (if (and (eq gnus-summary-goto-unread 'never)
7961                           (not (gnus-summary-first-article-p article)))
7962                      (gnus-summary-prev-article)
7963                    (gnus-summary-prev-unread-article))))))))
7964   (gnus-summary-position-point))
7965
7966 (defun gnus-summary-prev-page-or-article (&optional lines)
7967   "Show previous page of selected article.
7968 Argument LINES specifies lines to be scrolled down.
7969 If at the beginning of the article, go to the next article."
7970   (interactive "P")
7971   (gnus-summary-prev-page lines t))
7972
7973 (defun gnus-summary-scroll-up (lines)
7974   "Scroll up (or down) one line current article.
7975 Argument LINES specifies lines to be scrolled up (or down if negative).
7976 If no article is selected, then the current article will be selected first."
7977   (interactive "p")
7978   (gnus-configure-windows 'article)
7979   (gnus-summary-show-thread)
7980   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7981     (gnus-eval-in-buffer-window gnus-article-buffer
7982       (cond ((> lines 0)
7983              (when (gnus-article-next-page lines)
7984                (gnus-message 3 "End of message")))
7985             ((< lines 0)
7986              (gnus-article-prev-page (- lines))))))
7987   (gnus-summary-recenter)
7988   (gnus-summary-position-point))
7989
7990 (defun gnus-summary-scroll-down (lines)
7991   "Scroll down (or up) one line current article.
7992 Argument LINES specifies lines to be scrolled down (or up if negative).
7993 If no article is selected, then the current article will be selected first."
7994   (interactive "p")
7995   (gnus-summary-scroll-up (- lines)))
7996
7997 (defun gnus-summary-next-same-subject ()
7998   "Select next article which has the same subject as current one."
7999   (interactive)
8000   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8001
8002 (defun gnus-summary-prev-same-subject ()
8003   "Select previous article which has the same subject as current one."
8004   (interactive)
8005   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8006
8007 (defun gnus-summary-next-unread-same-subject ()
8008   "Select next unread article which has the same subject as current one."
8009   (interactive)
8010   (gnus-summary-next-article t (gnus-summary-article-subject)))
8011
8012 (defun gnus-summary-prev-unread-same-subject ()
8013   "Select previous unread article which has the same subject as current one."
8014   (interactive)
8015   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8016
8017 (defun gnus-summary-first-unread-article ()
8018   "Select the first unread article.
8019 Return nil if there are no unread articles."
8020   (interactive)
8021   (prog1
8022       (when (gnus-summary-first-subject t)
8023         (gnus-summary-show-thread)
8024         (gnus-summary-first-subject t)
8025         (gnus-summary-display-article (gnus-summary-article-number)))
8026     (gnus-summary-position-point)))
8027
8028 (defun gnus-summary-first-unread-subject ()
8029   "Place the point on the subject line of the first unread article.
8030 Return nil if there are no unread articles."
8031   (interactive)
8032   (prog1
8033       (when (gnus-summary-first-subject t)
8034         (gnus-summary-show-thread)
8035         (gnus-summary-first-subject t))
8036     (gnus-summary-position-point)))
8037
8038 (defun gnus-summary-first-unseen-subject ()
8039   "Place the point on the subject line of the first unseen article.
8040 Return nil if there are no unseen articles."
8041   (interactive)
8042   (prog1
8043       (when (gnus-summary-first-subject nil nil t)
8044         (gnus-summary-show-thread)
8045         (gnus-summary-first-subject nil nil t))
8046     (gnus-summary-position-point)))
8047
8048 (defun gnus-summary-first-unseen-or-unread-subject ()
8049   "Place the point on the subject line of the first unseen and unread article.
8050 If all article have been seen, on the subject line of the first unread
8051 article."
8052   (interactive)
8053   (prog1
8054       (unless (when (gnus-summary-first-subject nil nil t)
8055                 (gnus-summary-show-thread)
8056                 (gnus-summary-first-subject nil nil t))
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-article ()
8063   "Select the first article.
8064 Return nil if there are no articles."
8065   (interactive)
8066   (prog1
8067       (when (gnus-summary-first-subject)
8068         (gnus-summary-show-thread)
8069         (gnus-summary-first-subject)
8070         (gnus-summary-display-article (gnus-summary-article-number)))
8071     (gnus-summary-position-point)))
8072
8073 (defun gnus-summary-best-unread-article (&optional arg)
8074   "Select the unread article with the highest score.
8075 If given a prefix argument, select the next unread article that has a
8076 score higher than the default score."
8077   (interactive "P")
8078   (let ((article (if arg
8079                      (gnus-summary-better-unread-subject)
8080                    (gnus-summary-best-unread-subject))))
8081     (if article
8082         (gnus-summary-goto-article article)
8083       (error "No unread articles"))))
8084
8085 (defun gnus-summary-best-unread-subject ()
8086   "Select the unread subject with the highest score."
8087   (interactive)
8088   (let ((best -1000000)
8089         (data gnus-newsgroup-data)
8090         article score)
8091     (while data
8092       (and (gnus-data-unread-p (car data))
8093            (> (setq score
8094                     (gnus-summary-article-score (gnus-data-number (car data))))
8095               best)
8096            (setq best score
8097                  article (gnus-data-number (car data))))
8098       (setq data (cdr data)))
8099     (when article
8100       (gnus-summary-goto-subject article))
8101     (gnus-summary-position-point)
8102     article))
8103
8104 (defun gnus-summary-better-unread-subject ()
8105   "Select the first unread subject that has a score over the default score."
8106   (interactive)
8107   (let ((data gnus-newsgroup-data)
8108         article score)
8109     (while (and (setq article (gnus-data-number (car data)))
8110                 (or (gnus-data-read-p (car data))
8111                     (not (> (gnus-summary-article-score article)
8112                             gnus-summary-default-score))))
8113       (setq data (cdr data)))
8114     (when article
8115       (gnus-summary-goto-subject article))
8116     (gnus-summary-position-point)
8117     article))
8118
8119 (defun gnus-summary-last-subject ()
8120   "Go to the last displayed subject line in the group."
8121   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8122     (when article
8123       (gnus-summary-goto-subject article))))
8124
8125 (defun gnus-summary-goto-article (article &optional all-headers force)
8126   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8127 If ALL-HEADERS is non-nil, no header lines are hidden.
8128 If FORCE, go to the article even if it isn't displayed.  If FORCE
8129 is a number, it is the line the article is to be displayed on."
8130   (interactive
8131    (list
8132     (gnus-completing-read
8133      "Article number or Message-ID"
8134      (mapcar 'int-to-string gnus-newsgroup-limit))
8135     current-prefix-arg
8136     t))
8137   (prog1
8138       (if (and (stringp article)
8139                (string-match "@\\|%40" article))
8140           (gnus-summary-refer-article article)
8141         (when (stringp article)
8142           (setq article (string-to-number article)))
8143         (if (gnus-summary-goto-subject article force)
8144             (gnus-summary-display-article article all-headers)
8145           (gnus-message 4 "Couldn't go to article %s" article) nil))
8146     (gnus-summary-position-point)))
8147
8148 (defun gnus-summary-goto-last-article ()
8149   "Go to the previously read article."
8150   (interactive)
8151   (prog1
8152       (when gnus-last-article
8153         (gnus-summary-goto-article gnus-last-article nil t))
8154     (gnus-summary-position-point)))
8155
8156 (defun gnus-summary-pop-article (number)
8157   "Pop one article off the history and go to the previous.
8158 NUMBER articles will be popped off."
8159   (interactive "p")
8160   (let (to)
8161     (setq gnus-newsgroup-history
8162           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8163     (if to
8164         (gnus-summary-goto-article (car to) nil t)
8165       (error "Article history empty")))
8166   (gnus-summary-position-point))
8167
8168 ;; Summary commands and functions for limiting the summary buffer.
8169
8170 (defun gnus-summary-limit-to-articles (n)
8171   "Limit the summary buffer to the next N articles.
8172 If not given a prefix, use the process marked articles instead."
8173   (interactive "P")
8174   (prog1
8175       (let ((articles (gnus-summary-work-articles n)))
8176         (setq gnus-newsgroup-processable nil)
8177         (gnus-summary-limit articles))
8178     (gnus-summary-position-point)))
8179
8180 (defun gnus-summary-pop-limit (&optional total)
8181   "Restore the previous limit.
8182 If given a prefix, remove all limits."
8183   (interactive "P")
8184   (when total
8185     (setq gnus-newsgroup-limits
8186           (list (mapcar (lambda (h) (mail-header-number h))
8187                         gnus-newsgroup-headers))))
8188   (unless gnus-newsgroup-limits
8189     (error "No limit to pop"))
8190   (prog1
8191       (gnus-summary-limit nil 'pop)
8192     (gnus-summary-position-point)))
8193
8194 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8195   "Limit the summary buffer to articles that have subjects that match a regexp.
8196 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8197   (interactive
8198    (list (read-string (if current-prefix-arg
8199                           "Exclude subject (regexp): "
8200                         "Limit to subject (regexp): "))
8201          nil current-prefix-arg))
8202   (unless header
8203     (setq header "subject"))
8204   (when (not (equal "" subject))
8205     (prog1
8206         (let ((articles (gnus-summary-find-matching
8207                          (or header "subject") subject 'all nil nil
8208                          not-matching)))
8209           (unless articles
8210             (error "Found no matches for \"%s\"" subject))
8211           (gnus-summary-limit articles))
8212       (gnus-summary-position-point))))
8213
8214 (defun gnus-summary-limit-to-author (from &optional not-matching)
8215   "Limit the summary buffer to articles that have authors that match a regexp.
8216 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8217   (interactive
8218    (list (read-string (if current-prefix-arg
8219                           "Exclude author (regexp): "
8220                         "Limit to author (regexp): "))
8221          current-prefix-arg))
8222   (gnus-summary-limit-to-subject from "from" not-matching))
8223
8224 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8225   "Limit the summary buffer to articles with the given RECIPIENT.
8226
8227 If NOT-MATCHING, exclude RECIPIENT.
8228
8229 To and Cc headers are checked.  You need to include them in
8230 `nnmail-extra-headers'."
8231   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8232   (interactive
8233    (list (read-string (format "%s recipient (regexp): "
8234                               (if current-prefix-arg "Exclude" "Limit to")))
8235          current-prefix-arg))
8236   (when (not (equal "" recipient))
8237     (prog1 (let* ((to
8238                    (if (memq 'To nnmail-extra-headers)
8239                        (gnus-summary-find-matching
8240                         (cons 'extra 'To) recipient 'all nil nil
8241                         not-matching)
8242                      (gnus-message
8243                       1 "`To' isn't present in `nnmail-extra-headers'")
8244                      (sit-for 1)
8245                      nil))
8246                   (cc
8247                    (if (memq 'Cc nnmail-extra-headers)
8248                        (gnus-summary-find-matching
8249                         (cons 'extra 'Cc) recipient 'all nil nil
8250                         not-matching)
8251                      (gnus-message
8252                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8253                      (sit-for 1)
8254                      nil))
8255                   (articles
8256                    (if not-matching
8257                        ;; We need the numbers that are in both lists:
8258                        (mapcar (lambda (a)
8259                                  (and (memq a to) a))
8260                                cc)
8261                      (nconc to cc))))
8262              (unless articles
8263                (error "Found no matches for \"%s\"" recipient))
8264              (gnus-summary-limit articles))
8265       (gnus-summary-position-point))))
8266
8267 (defun gnus-summary-limit-to-address (address &optional not-matching)
8268   "Limit the summary buffer to articles with the given ADDRESS.
8269
8270 If NOT-MATCHING, exclude ADDRESS.
8271
8272 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8273 in `nnmail-extra-headers'."
8274   (interactive
8275    (list (read-string (format "%s address (regexp): "
8276                               (if current-prefix-arg "Exclude" "Limit to")))
8277          current-prefix-arg))
8278   (when (not (equal "" address))
8279     (prog1 (let* ((to
8280                    (if (memq 'To nnmail-extra-headers)
8281                        (gnus-summary-find-matching
8282                         (cons 'extra 'To) address 'all nil nil
8283                         not-matching)
8284                      (gnus-message
8285                       1 "`To' isn't present in `nnmail-extra-headers'")
8286                      (sit-for 1)
8287                      t))
8288                   (cc
8289                    (if (memq 'Cc nnmail-extra-headers)
8290                        (gnus-summary-find-matching
8291                         (cons 'extra 'Cc) address 'all nil nil
8292                         not-matching)
8293                      (gnus-message
8294                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8295                      (sit-for 1)
8296                      t))
8297                   (from
8298                    (gnus-summary-find-matching "from" address
8299                                                'all nil nil not-matching))
8300                   (articles
8301                    (if not-matching
8302                        ;; We need the numbers that are in all lists:
8303                        (if (eq cc t)
8304                            (if (eq to t)
8305                                from
8306                              (mapcar (lambda (a) (car (memq a from))) to))
8307                          (if (eq to t)
8308                              (mapcar (lambda (a) (car (memq a from))) cc)
8309                            (mapcar (lambda (a) (car (memq a from)))
8310                                    (mapcar (lambda (a) (car (memq a to)))
8311                                            cc))))
8312                      (nconc (if (eq to t) nil to)
8313                             (if (eq cc t) nil cc)
8314                             from))))
8315              (unless articles
8316                (error "Found no matches for \"%s\"" address))
8317              (gnus-summary-limit articles))
8318       (gnus-summary-position-point))))
8319
8320 (defun gnus-summary-limit-strange-charsets-predicate (header)
8321   (when (fboundp 'char-charset)
8322     (let ((string (concat (mail-header-subject header)
8323                           (mail-header-from header)))
8324           charset found)
8325       (dotimes (i (1- (length string)))
8326         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8327         (when (string-match "unicode\\|big\\|japanese" charset)
8328           (setq found t)))
8329       found)))
8330
8331 (defun gnus-summary-limit-to-predicate (predicate)
8332   "Limit to articles where PREDICATE returns non-nil.
8333 PREDICATE will be called with the header structures of the
8334 articles."
8335   (let ((articles nil)
8336         (case-fold-search t))
8337     (dolist (header gnus-newsgroup-headers)
8338       (when (funcall predicate header)
8339         (push (mail-header-number header) articles)))
8340     (gnus-summary-limit (nreverse articles))))
8341
8342 (defun gnus-summary-limit-to-age (age &optional younger-p)
8343   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8344 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8345 articles that are younger than AGE days."
8346   (interactive
8347    (let ((younger current-prefix-arg)
8348          (days-got nil)
8349          days)
8350      (while (not days-got)
8351        (setq days (if younger
8352                       (read-string "Limit to articles younger than (in days, older when negative): ")
8353                     (read-string
8354                      "Limit to articles older than (in days, younger when negative): ")))
8355        (when (> (length days) 0)
8356          (setq days (read days)))
8357        (if (numberp days)
8358            (progn
8359              (setq days-got t)
8360              (when (< days 0)
8361                (setq younger (not younger))
8362                (setq days (* days -1))))
8363          (message "Please enter a number.")
8364          (sleep-for 1)))
8365      (list days younger)))
8366   (prog1
8367       (let ((data gnus-newsgroup-data)
8368             (cutoff (days-to-time age))
8369             articles d date is-younger)
8370         (while (setq d (pop data))
8371           (when (and (vectorp (gnus-data-header d))
8372                      (setq date (mail-header-date (gnus-data-header d))))
8373             (setq is-younger (time-less-p
8374                               (time-since (gnus-date-get-time date))
8375                               cutoff))
8376             (when (if younger-p
8377                       is-younger
8378                     (not is-younger))
8379               (push (gnus-data-number d) articles))))
8380         (gnus-summary-limit (nreverse articles)))
8381     (gnus-summary-position-point)))
8382
8383 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8384   "Limit the summary buffer to articles that match an 'extra' header."
8385   (interactive
8386    (let ((header
8387           (intern
8388            (gnus-completing-read
8389             (if current-prefix-arg
8390                 "Exclude extra header"
8391               "Limit extra header")
8392             (mapcar 'symbol-name gnus-extra-headers)
8393             t nil nil
8394             (symbol-name (car gnus-extra-headers))))))
8395      (list header
8396            (read-string (format "%s header %s (regexp): "
8397                                 (if current-prefix-arg "Exclude" "Limit to")
8398                                 header))
8399            current-prefix-arg)))
8400   (when (not (equal "" regexp))
8401     (prog1
8402         (let ((articles (gnus-summary-find-matching
8403                          (cons 'extra header) regexp 'all nil nil
8404                          not-matching)))
8405           (unless articles
8406             (error "Found no matches for \"%s\"" regexp))
8407           (gnus-summary-limit articles))
8408       (gnus-summary-position-point))))
8409
8410 (defun gnus-summary-limit-to-display-predicate ()
8411   "Limit the summary buffer to the predicated in the `display' group parameter."
8412   (interactive)
8413   (unless gnus-newsgroup-display
8414     (error "There is no `display' group parameter"))
8415   (let (articles)
8416     (dolist (gnus-number gnus-newsgroup-articles)
8417       (when (funcall gnus-newsgroup-display)
8418         (push gnus-number articles)))
8419     (gnus-summary-limit articles))
8420   (gnus-summary-position-point))
8421
8422 (defun gnus-summary-limit-to-unread (&optional all)
8423   "Limit the summary buffer to articles that are not marked as read.
8424 If ALL is non-nil, limit strictly to unread articles."
8425   (interactive "P")
8426   (if all
8427       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8428     (gnus-summary-limit-to-marks
8429      ;; Concat all the marks that say that an article is read and have
8430      ;; those removed.
8431      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8432            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8433            gnus-low-score-mark gnus-expirable-mark
8434            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8435            gnus-duplicate-mark)
8436      'reverse)))
8437
8438 (defun gnus-summary-limit-to-headers (match &optional reverse)
8439   "Limit the summary buffer to articles that have headers that match MATCH.
8440 If REVERSE (the prefix), limit to articles that don't match."
8441   (interactive "sMatch headers (regexp): \nP")
8442   (gnus-summary-limit-to-bodies match reverse t))
8443
8444 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8445   "Limit the summary buffer to articles that have bodies that match MATCH.
8446 If REVERSE (the prefix), limit to articles that don't match."
8447   (interactive "sMatch body (regexp): \nP")
8448   (let ((articles nil)
8449         (gnus-select-article-hook nil)  ;Disable hook.
8450         (gnus-article-prepare-hook nil)
8451         (gnus-use-article-prefetch nil)
8452         (gnus-keep-backlog nil)
8453         (gnus-break-pages nil)
8454         (gnus-summary-display-arrow nil)
8455         (gnus-updated-mode-lines nil)
8456         (gnus-auto-center-summary nil)
8457         (gnus-display-mime-function nil))
8458     (dolist (data gnus-newsgroup-data)
8459       (let (gnus-mark-article-hook)
8460         (gnus-summary-select-article t t nil (gnus-data-number data)))
8461       (with-current-buffer gnus-article-buffer
8462         (article-goto-body)
8463         (let* ((case-fold-search t)
8464                (found (if headersp
8465                           (re-search-backward match nil t)
8466                         (re-search-forward match nil t))))
8467           (when (or (and found
8468                          (not reverse))
8469                     (and (not found)
8470                          reverse))
8471             (push (gnus-data-number data) articles)))))
8472     (if (not articles)
8473         (message "No messages matched")
8474       (gnus-summary-limit articles)))
8475   (gnus-summary-position-point))
8476
8477 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8478   "Limit the summary buffer to articles that aren't part on any thread.
8479 If THREADSP (the prefix), limit to articles that are in threads."
8480   (interactive "P")
8481   (let ((articles nil)
8482         thread-articles
8483         threads)
8484     (dolist (thread gnus-newsgroup-threads)
8485       (if (stringp (car thread))
8486           (dolist (thread (cdr thread))
8487             (push thread threads))
8488         (push thread threads)))
8489     (dolist (thread threads)
8490       (setq thread-articles (gnus-articles-in-thread thread))
8491       (when (or (and threadsp
8492                      (> (length thread-articles) 1))
8493                 (and (not threadsp)
8494                      (= (length thread-articles) 1)))
8495         (setq articles (nconc thread-articles 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-replied (&optional unreplied)
8502   "Limit the summary buffer to replied articles.
8503 If UNREPLIED (the prefix), limit to unreplied articles."
8504   (interactive "P")
8505   (if unreplied
8506       (gnus-summary-limit
8507        (gnus-set-difference gnus-newsgroup-articles
8508         gnus-newsgroup-replied))
8509     (gnus-summary-limit gnus-newsgroup-replied))
8510   (gnus-summary-position-point))
8511
8512 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8513   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8514 If REVERSE, limit the summary buffer to articles that are marked
8515 with MARKS.  MARKS can either be a string of marks or a list of marks.
8516 Returns how many articles were removed."
8517   (interactive "sMarks: ")
8518   (gnus-summary-limit-to-marks marks t))
8519
8520 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8521   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8522 If REVERSE (the prefix), limit the summary buffer to articles that are
8523 not marked with MARKS.  MARKS can either be a string of marks or a
8524 list of marks.
8525 Returns how many articles were removed."
8526   (interactive "sMarks: \nP")
8527   (prog1
8528       (let ((data gnus-newsgroup-data)
8529             (marks (if (listp marks) marks
8530                      (append marks nil))) ; Transform to list.
8531             articles)
8532         (while data
8533           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8534                   (memq (gnus-data-mark (car data)) marks))
8535             (push (gnus-data-number (car data)) articles))
8536           (setq data (cdr data)))
8537         (gnus-summary-limit articles))
8538     (gnus-summary-position-point)))
8539
8540 (defun gnus-summary-limit-to-score (score)
8541   "Limit to articles with score at or above SCORE."
8542   (interactive "NLimit to articles with score of at least: ")
8543   (let ((data gnus-newsgroup-data)
8544         articles)
8545     (while data
8546       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8547                 score)
8548         (push (gnus-data-number (car data)) articles))
8549       (setq data (cdr data)))
8550     (prog1
8551         (gnus-summary-limit articles)
8552       (gnus-summary-position-point))))
8553
8554 (defun gnus-summary-limit-to-unseen ()
8555   "Limit to unseen articles."
8556   (interactive)
8557   (prog1
8558       (gnus-summary-limit gnus-newsgroup-unseen)
8559     (gnus-summary-position-point)))
8560
8561 (defun gnus-summary-limit-include-thread (id)
8562   "Display all the hidden articles that is in the thread with ID in it.
8563 When called interactively, ID is the Message-ID of the current
8564 article."
8565   (interactive (list (mail-header-id (gnus-summary-article-header))))
8566   (let ((articles (gnus-articles-in-thread
8567                    (gnus-id-to-thread (gnus-root-id id))))
8568         ;;we REALLY want the whole thread---this prevents cut-threads
8569         ;;from removing the thread we want to include.
8570         (gnus-fetch-old-headers nil)
8571         (gnus-build-sparse-threads nil))
8572     (prog1
8573         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8574       (gnus-summary-limit-include-matching-articles
8575        "subject"
8576        (regexp-quote (gnus-simplify-subject-re
8577                       (mail-header-subject (gnus-id-to-header id)))))
8578       (gnus-summary-position-point))))
8579
8580 (defun gnus-summary-limit-include-matching-articles (header regexp)
8581   "Display all the hidden articles that have HEADERs that match REGEXP."
8582   (interactive (list (read-string "Match on header: ")
8583                      (read-string "Regexp: ")))
8584   (let ((articles (gnus-find-matching-articles header regexp)))
8585     (prog1
8586         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8587       (gnus-summary-position-point))))
8588
8589 (defun gnus-summary-insert-dormant-articles ()
8590   "Insert all the dormant articles for this group into the current buffer."
8591   (interactive)
8592   (let ((gnus-verbose (max 6 gnus-verbose)))
8593     (if (not gnus-newsgroup-dormant)
8594         (gnus-message 3 "No dormant articles for this group")
8595       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8596
8597 (defun gnus-summary-insert-ticked-articles ()
8598   "Insert ticked articles for this group into the current buffer."
8599   (interactive)
8600   (let ((gnus-verbose (max 6 gnus-verbose)))
8601     (if (not gnus-newsgroup-marked)
8602         (gnus-message 3 "No ticked articles for this group")
8603       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8604
8605 (defun gnus-summary-limit-include-dormant ()
8606   "Display all the hidden articles that are marked as dormant.
8607 Note that this command only works on a subset of the articles currently
8608 fetched for this group."
8609   (interactive)
8610   (unless gnus-newsgroup-dormant
8611     (error "There are no dormant articles in this group"))
8612   (prog1
8613       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8614     (gnus-summary-position-point)))
8615
8616 (defun gnus-summary-include-articles (articles)
8617   "Fetch the headers for ARTICLES and then display the summary lines."
8618   (let ((gnus-inhibit-demon t)
8619         (gnus-agent nil)
8620         (gnus-read-all-available-headers t))
8621     (setq gnus-newsgroup-headers
8622           (gnus-merge
8623            'list gnus-newsgroup-headers
8624            (gnus-fetch-headers articles nil t)
8625            'gnus-article-sort-by-number))
8626     (setq gnus-newsgroup-articles
8627           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8628     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8629
8630 (defun gnus-summary-limit-exclude-dormant ()
8631   "Hide all dormant articles."
8632   (interactive)
8633   (prog1
8634       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8635     (gnus-summary-position-point)))
8636
8637 (defun gnus-summary-limit-exclude-childless-dormant ()
8638   "Hide all dormant articles that have no children."
8639   (interactive)
8640   (let ((data (gnus-data-list t))
8641         articles d children)
8642     ;; Find all articles that are either not dormant or have
8643     ;; children.
8644     (while (setq d (pop data))
8645       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8646                 (and (setq children
8647                            (gnus-article-children (gnus-data-number d)))
8648                      (let (found)
8649                        (while children
8650                          (when (memq (car children) articles)
8651                            (setq children nil
8652                                  found t))
8653                          (pop children))
8654                        found)))
8655         (push (gnus-data-number d) articles)))
8656     ;; Do the limiting.
8657     (prog1
8658         (gnus-summary-limit articles)
8659       (gnus-summary-position-point))))
8660
8661 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8662   "Mark all unread excluded articles as read.
8663 If ALL, mark even excluded ticked and dormants as read."
8664   (interactive "P")
8665   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8666   (let ((articles (gnus-sorted-ndifference
8667                    (sort
8668                     (mapcar (lambda (h) (mail-header-number h))
8669                             gnus-newsgroup-headers)
8670                     '<)
8671                    gnus-newsgroup-limit))
8672         article)
8673     (setq gnus-newsgroup-unreads
8674           (gnus-sorted-intersection gnus-newsgroup-unreads
8675                                     gnus-newsgroup-limit))
8676     (if all
8677         (setq gnus-newsgroup-dormant nil
8678               gnus-newsgroup-marked nil
8679               gnus-newsgroup-reads
8680               (nconc
8681                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8682                gnus-newsgroup-reads))
8683       (while (setq article (pop articles))
8684         (unless (or (memq article gnus-newsgroup-dormant)
8685                     (memq article gnus-newsgroup-marked))
8686           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8687
8688 (defun gnus-summary-limit (articles &optional pop)
8689   (if pop
8690       ;; We pop the previous limit off the stack and use that.
8691       (setq articles (car gnus-newsgroup-limits)
8692             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8693     ;; We use the new limit, so we push the old limit on the stack.
8694     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8695   ;; Set the limit.
8696   (setq gnus-newsgroup-limit articles)
8697   (let ((total (length gnus-newsgroup-data))
8698         (data (gnus-data-find-list (gnus-summary-article-number)))
8699         (gnus-summary-mark-below nil)   ; Inhibit this.
8700         found)
8701     ;; This will do all the work of generating the new summary buffer
8702     ;; according to the new limit.
8703     (gnus-summary-prepare)
8704     ;; Hide any threads, possibly.
8705     (gnus-summary-maybe-hide-threads)
8706     ;; Try to return to the article you were at, or one in the
8707     ;; neighborhood.
8708     (when data
8709       ;; We try to find some article after the current one.
8710       (while data
8711         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8712           (setq data nil
8713                 found t))
8714         (setq data (cdr data))))
8715     (unless found
8716       ;; If there is no data, that means that we were after the last
8717       ;; article.  The same goes when we can't find any articles
8718       ;; after the current one.
8719       (goto-char (point-max))
8720       (gnus-summary-find-prev))
8721     (gnus-set-mode-line 'summary)
8722     ;; We return how many articles were removed from the summary
8723     ;; buffer as a result of the new limit.
8724     (- total (length gnus-newsgroup-data))))
8725
8726 (defsubst gnus-invisible-cut-children (threads)
8727   (let ((num 0))
8728     (while threads
8729       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8730         (incf num))
8731       (pop threads))
8732     (< num 2)))
8733
8734 (defsubst gnus-cut-thread (thread)
8735   "Go forwards in the thread until we find an article that we want to display."
8736   (when (or (eq gnus-fetch-old-headers 'some)
8737             (eq gnus-fetch-old-headers 'invisible)
8738             (numberp gnus-fetch-old-headers)
8739             (eq gnus-build-sparse-threads 'some)
8740             (eq gnus-build-sparse-threads 'more))
8741     ;; Deal with old-fetched headers and sparse threads.
8742     (while (and
8743             thread
8744             (or
8745              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8746              (gnus-summary-article-ancient-p
8747               (mail-header-number (car thread))))
8748             (if (or (<= (length (cdr thread)) 1)
8749                     (eq gnus-fetch-old-headers 'invisible))
8750                 (setq gnus-newsgroup-limit
8751                       (delq (mail-header-number (car thread))
8752                             gnus-newsgroup-limit)
8753                       thread (cadr thread))
8754               (when (gnus-invisible-cut-children (cdr thread))
8755                 (let ((th (cdr thread)))
8756                   (while th
8757                     (if (memq (mail-header-number (caar th))
8758                               gnus-newsgroup-limit)
8759                         (setq thread (car th)
8760                               th nil)
8761                       (setq th (cdr th))))))))))
8762   thread)
8763
8764 (defun gnus-cut-threads (threads)
8765   "Cut off all uninteresting articles from the beginning of THREADS."
8766   (when (or (eq gnus-fetch-old-headers 'some)
8767             (eq gnus-fetch-old-headers 'invisible)
8768             (numberp gnus-fetch-old-headers)
8769             (eq gnus-build-sparse-threads 'some)
8770             (eq gnus-build-sparse-threads 'more))
8771     (let ((th threads))
8772       (while th
8773         (setcar th (gnus-cut-thread (car th)))
8774         (setq th (cdr th)))))
8775   ;; Remove nixed out threads.
8776   (delq nil threads))
8777
8778 (defun gnus-summary-initial-limit (&optional show-if-empty)
8779   "Figure out what the initial limit is supposed to be on group entry.
8780 This entails weeding out unwanted dormants, low-scored articles,
8781 fetch-old-headers verbiage, and so on."
8782   ;; Most groups have nothing to remove.
8783   (unless (or gnus-inhibit-limiting
8784               (and (null gnus-newsgroup-dormant)
8785                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8786                    (not (eq gnus-fetch-old-headers 'some))
8787                    (not (numberp gnus-fetch-old-headers))
8788                    (not (eq gnus-fetch-old-headers 'invisible))
8789                    (null gnus-summary-expunge-below)
8790                    (not (eq gnus-build-sparse-threads 'some))
8791                    (not (eq gnus-build-sparse-threads 'more))
8792                    (null gnus-thread-expunge-below)))
8793     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8794     (setq gnus-newsgroup-limit nil)
8795     (mapatoms
8796      (lambda (node)
8797        (unless (car (symbol-value node))
8798          ;; These threads have no parents -- they are roots.
8799          (let ((nodes (cdr (symbol-value node)))
8800                thread)
8801            (while nodes
8802              (if (and gnus-thread-expunge-below
8803                       (< (gnus-thread-total-score (car nodes))
8804                          gnus-thread-expunge-below))
8805                  (gnus-expunge-thread (pop nodes))
8806                (setq thread (pop nodes))
8807                (gnus-summary-limit-children thread))))))
8808      gnus-newsgroup-dependencies)
8809     ;; If this limitation resulted in an empty group, we might
8810     ;; pop the previous limit and use it instead.
8811     (when (and (not gnus-newsgroup-limit)
8812                show-if-empty)
8813       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8814     gnus-newsgroup-limit))
8815
8816 (defun gnus-summary-limit-children (thread)
8817   "Return 1 if this subthread is visible and 0 if it is not."
8818   ;; First we get the number of visible children to this thread.  This
8819   ;; is done by recursing down the thread using this function, so this
8820   ;; will really go down to a leaf article first, before slowly
8821   ;; working its way up towards the root.
8822   (when thread
8823     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8824            (children
8825            (if (cdr thread)
8826                (apply '+ (mapcar 'gnus-summary-limit-children
8827                                  (cdr thread)))
8828              0))
8829            (number (mail-header-number (car thread)))
8830            score)
8831       (if (and
8832            (not (memq number gnus-newsgroup-marked))
8833            (or
8834             ;; If this article is dormant and has absolutely no visible
8835             ;; children, then this article isn't visible.
8836             (and (memq number gnus-newsgroup-dormant)
8837                  (zerop children))
8838             ;; If this is "fetch-old-headered" and there is no
8839             ;; visible children, then we don't want this article.
8840             (and (or (eq gnus-fetch-old-headers 'some)
8841                      (numberp gnus-fetch-old-headers))
8842                  (gnus-summary-article-ancient-p number)
8843                  (zerop children))
8844             ;; If this is "fetch-old-headered" and `invisible', then
8845             ;; we don't want this article.
8846             (and (eq gnus-fetch-old-headers 'invisible)
8847                  (gnus-summary-article-ancient-p number))
8848             ;; If this is a sparsely inserted article with no children,
8849             ;; we don't want it.
8850             (and (eq gnus-build-sparse-threads 'some)
8851                  (gnus-summary-article-sparse-p number)
8852                  (zerop children))
8853             ;; If we use expunging, and this article is really
8854             ;; low-scored, then we don't want this article.
8855             (when (and gnus-summary-expunge-below
8856                        (< (setq score
8857                                 (or (cdr (assq number gnus-newsgroup-scored))
8858                                     gnus-summary-default-score))
8859                           gnus-summary-expunge-below))
8860               ;; We increase the expunge-tally here, but that has
8861               ;; nothing to do with the limits, really.
8862               (incf gnus-newsgroup-expunged-tally)
8863               ;; We also mark as read here, if that's wanted.
8864               (when (and gnus-summary-mark-below
8865                          (< score gnus-summary-mark-below))
8866                 (setq gnus-newsgroup-unreads
8867                       (delq number gnus-newsgroup-unreads))
8868                 (if gnus-newsgroup-auto-expire
8869                     (push number gnus-newsgroup-expirable)
8870                   (push (cons number gnus-low-score-mark)
8871                         gnus-newsgroup-reads)))
8872               t)
8873             ;; Do the `display' group parameter.
8874             (and gnus-newsgroup-display
8875                  (let ((gnus-number number))
8876                    (not (funcall gnus-newsgroup-display))))))
8877           ;; Nope, invisible article.
8878           0
8879         ;; Ok, this article is to be visible, so we add it to the limit
8880         ;; and return 1.
8881         (push number gnus-newsgroup-limit)
8882         1))))
8883
8884 (defun gnus-expunge-thread (thread)
8885   "Mark all articles in THREAD as read."
8886   (let* ((number (mail-header-number (car thread))))
8887     (incf gnus-newsgroup-expunged-tally)
8888     ;; We also mark as read here, if that's wanted.
8889     (setq gnus-newsgroup-unreads
8890           (delq number gnus-newsgroup-unreads))
8891     (if gnus-newsgroup-auto-expire
8892         (push number gnus-newsgroup-expirable)
8893       (push (cons number gnus-low-score-mark)
8894             gnus-newsgroup-reads)))
8895   ;; Go recursively through all subthreads.
8896   (mapcar 'gnus-expunge-thread (cdr thread)))
8897
8898 ;; Summary article oriented commands
8899
8900 (defun gnus-summary-refer-parent-article (n)
8901   "Refer parent article N times.
8902 If N is negative, go to ancestor -N instead.
8903 The difference between N and the number of articles fetched is returned."
8904   (interactive "p")
8905   (let ((skip 1)
8906         error header ref)
8907     (when (not (natnump n))
8908       (setq skip (abs n)
8909             n 1))
8910     (while (and (> n 0)
8911                 (not error))
8912       (setq header (gnus-summary-article-header))
8913       (if (and (eq (mail-header-number header)
8914                    (cdr gnus-article-current))
8915                (equal gnus-newsgroup-name
8916                       (car gnus-article-current)))
8917           ;; If we try to find the parent of the currently
8918           ;; displayed article, then we take a look at the actual
8919           ;; References header, since this is slightly more
8920           ;; reliable than the References field we got from the
8921           ;; server.
8922           (with-current-buffer gnus-original-article-buffer
8923             (nnheader-narrow-to-headers)
8924             (unless (setq ref (message-fetch-field "references"))
8925               (when (setq ref (message-fetch-field "in-reply-to"))
8926                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8927             (widen))
8928         (setq ref
8929               ;; It's not the current article, so we take a bet on
8930               ;; the value we got from the server.
8931               (mail-header-references header)))
8932       (if (and ref
8933                (not (equal ref "")))
8934           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8935             (gnus-message 1 "Couldn't find parent"))
8936         (gnus-message 1 "No references in article %d"
8937                       (gnus-summary-article-number))
8938         (setq error t))
8939       (decf n))
8940     (gnus-summary-position-point)
8941     n))
8942
8943 (defun gnus-summary-refer-references ()
8944   "Fetch all articles mentioned in the References header.
8945 Return the number of articles fetched."
8946   (interactive)
8947   (let ((ref (mail-header-references (gnus-summary-article-header)))
8948         (current (gnus-summary-article-number))
8949         (n 0))
8950     (if (or (not ref)
8951             (equal ref ""))
8952         (error "No References in the current article")
8953       ;; For each Message-ID in the References header...
8954       (while (string-match "<[^>]*>" ref)
8955         (incf n)
8956         ;; ... fetch that article.
8957         (gnus-summary-refer-article
8958          (prog1 (match-string 0 ref)
8959            (setq ref (substring ref (match-end 0))))))
8960       (gnus-summary-goto-subject current)
8961       (gnus-summary-position-point)
8962       n)))
8963
8964 (defun gnus-delete-duplicate-headers (headers)
8965   ;; First remove leading duplicates.
8966   (while (and (> (length headers) 1)
8967               (= (mail-header-number (car headers))
8968                  (mail-header-number (cadr headers))))
8969     (pop headers))
8970   ;; Then the rest.
8971   (let ((result headers))
8972     (while (> (length headers) 1)
8973       (if (= (mail-header-number (car headers))
8974              (mail-header-number (cadr headers)))
8975           (setcdr headers (cddr headers))
8976         (pop headers)))
8977     result))
8978
8979 (defun gnus-summary-refer-thread (&optional limit)
8980   "Fetch all articles in the current thread. For backends that
8981 know how to search for threads (currently only 'nnimap) a
8982 non-numeric prefix arg will use nnir to search the entire
8983 server; without a prefix arg only the current group is
8984 searched. If the variable `gnus-refer-thread-use-nnir' is
8985 non-nil the prefix arg has the reverse meaning. If no
8986 backend-specific 'request-thread function is available fetch
8987 LIMIT (the numerical prefix) old headers. If LIMIT is
8988 non-numeric or nil fetch the number specified by the
8989 `gnus-refer-thread-limit' variable."
8990   (interactive "P")
8991   (gnus-warp-to-article)
8992   (let* ((header (gnus-summary-article-header))
8993          (id (mail-header-id header))
8994          (gnus-inhibit-demon t)
8995          (gnus-summary-ignore-duplicates t)
8996          (gnus-read-all-available-headers t)
8997          (gnus-refer-thread-use-nnir
8998           (if (and (not (null limit)) (listp limit))
8999               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9000          (new-headers
9001           (if (gnus-check-backend-function
9002                'request-thread gnus-newsgroup-name)
9003               (gnus-request-thread header gnus-newsgroup-name)
9004             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9005                             gnus-refer-thread-limit))
9006                    (last (if (numberp limit)
9007                              (min (+ (mail-header-number header)
9008                                      limit)
9009                                   gnus-newsgroup-highest)
9010                            gnus-newsgroup-highest))
9011                    (subject (gnus-simplify-subject
9012                              (mail-header-subject header)))
9013                    (refs (split-string (or (mail-header-references header)
9014                                            "")))
9015                    (gnus-parse-headers-hook
9016                     `(lambda () (goto-char (point-min))
9017                       (keep-lines
9018                        (regexp-opt ',(append refs (list id subject)))))))
9019               (gnus-fetch-headers (list last) (if (numberp limit)
9020                                                   (* 2 limit) limit) t))))
9021          article-ids)
9022     (when (listp new-headers)
9023       (dolist (header new-headers)
9024         (push (mail-header-number header) article-ids)
9025         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9026           (push (mail-header-number header) gnus-newsgroup-unreads)
9027           (setq gnus-newsgroup-unselected
9028                 (delete (mail-header-number header)
9029                         gnus-newsgroup-unselected))))
9030       (setq gnus-newsgroup-headers
9031             (gnus-delete-duplicate-headers
9032              (gnus-merge
9033               'list gnus-newsgroup-headers new-headers
9034               'gnus-article-sort-by-number)))
9035       (setq gnus-newsgroup-articles
9036             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9037       (gnus-summary-limit-include-thread id)))
9038   (gnus-summary-show-thread))
9039
9040 (defun gnus-summary-refer-article (message-id)
9041   "Fetch an article specified by MESSAGE-ID."
9042   (interactive "sMessage-ID: ")
9043   (gnus-warp-to-article)
9044   (when (and (stringp message-id)
9045              (not (zerop (length message-id))))
9046     (setq message-id (gnus-replace-in-string message-id " " ""))
9047     ;; Construct the correct Message-ID if necessary.
9048     ;; Suggested by tale@pawl.rpi.edu.
9049     (unless (string-match "^<" message-id)
9050       (setq message-id (concat "<" message-id)))
9051     (unless (string-match ">$" message-id)
9052       (setq message-id (concat message-id ">")))
9053     ;; People often post MIDs from URLs, so unhex it:
9054     (unless (string-match "@" message-id)
9055       (setq message-id (gnus-url-unhex-string message-id)))
9056     (let* ((header (gnus-id-to-header message-id))
9057            (sparse (and header
9058                         (gnus-summary-article-sparse-p
9059                          (mail-header-number header))
9060                         (memq (mail-header-number header)
9061                               gnus-newsgroup-limit)))
9062            number)
9063       (cond
9064        ;; If the article is present in the buffer we just go to it.
9065        ((and header
9066              (or (not (gnus-summary-article-sparse-p
9067                        (mail-header-number header)))
9068                  sparse))
9069         (prog1
9070             (gnus-summary-goto-article
9071              (mail-header-number header) nil t)
9072           (when sparse
9073             (gnus-summary-update-article (mail-header-number header)))))
9074        (t
9075         ;; We fetch the article.
9076         (catch 'found
9077           (dolist (gnus-override-method (gnus-refer-article-methods))
9078             (when (and (gnus-check-server gnus-override-method)
9079                        ;; Fetch the header,
9080                        (setq number (gnus-summary-insert-subject message-id)))
9081               ;; and display the article.
9082               (gnus-summary-select-article nil nil nil number)
9083               (throw 'found t)))
9084           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9085
9086 (defun gnus-refer-article-methods ()
9087   "Return a list of referable methods."
9088   (cond
9089    ;; No method, so we default to current and native.
9090    ((null gnus-refer-article-method)
9091     (list gnus-current-select-method gnus-select-method))
9092    ;; Current.
9093    ((eq 'current gnus-refer-article-method)
9094     (list gnus-current-select-method))
9095    ;; List of select methods.
9096    ((not (and (symbolp (car gnus-refer-article-method))
9097               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9098     (let (out)
9099       (dolist (method gnus-refer-article-method)
9100         (push (if (eq 'current method)
9101                   gnus-current-select-method
9102                 (if (eq 'nnir (car method))
9103                     (list
9104                      'nnir
9105                      (or (cadr method)
9106                          (gnus-method-to-server gnus-current-select-method)))
9107                   method))
9108               out))
9109       (nreverse out)))
9110    ;; One single select method.
9111    (t
9112     (list gnus-refer-article-method))))
9113
9114 (defun gnus-summary-edit-parameters ()
9115   "Edit the group parameters of the current group."
9116   (interactive)
9117   (gnus-group-edit-group gnus-newsgroup-name 'params))
9118
9119 (defun gnus-summary-customize-parameters ()
9120   "Customize the group parameters of the current group."
9121   (interactive)
9122   (gnus-group-customize gnus-newsgroup-name))
9123
9124 (defun gnus-summary-enter-digest-group (&optional force)
9125   "Enter an nndoc group based on the current article.
9126 If FORCE, force a digest interpretation.  If not, try to guess
9127 what the document format is.
9128
9129 To control what happens when you exit the group, see the
9130 `gnus-auto-select-on-ephemeral-exit' variable."
9131   (interactive "P")
9132   (let ((conf gnus-current-window-configuration))
9133     (save-window-excursion
9134       (save-excursion
9135         (let (gnus-article-prepare-hook
9136               gnus-display-mime-function
9137               gnus-break-pages)
9138           (gnus-summary-select-article))))
9139     (setq gnus-current-window-configuration conf)
9140     (let* ((name (format "%s-%d"
9141                          (gnus-group-prefixed-name
9142                           gnus-newsgroup-name (list 'nndoc ""))
9143                          (with-current-buffer gnus-summary-buffer
9144                            gnus-current-article)))
9145            (ogroup gnus-newsgroup-name)
9146            (params (append (gnus-info-params (gnus-get-info ogroup))
9147                            (list (cons 'to-group ogroup))
9148                            (list (cons 'parent-group ogroup))
9149                            (list (cons 'save-article-group ogroup))))
9150            (case-fold-search t)
9151            (buf (current-buffer))
9152            dig to-address)
9153       (with-current-buffer gnus-original-article-buffer
9154         ;; Have the digest group inherit the main mail address of
9155         ;; the parent article.
9156         (when (setq to-address (or (gnus-fetch-field "reply-to")
9157                                    (gnus-fetch-field "from")))
9158           (setq params
9159                 (append
9160                  (list (cons 'to-address
9161                              (funcall gnus-decode-encoded-address-function
9162                                       to-address))))))
9163         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9164         (insert-buffer-substring gnus-original-article-buffer)
9165         ;; Remove lines that may lead nndoc to misinterpret the
9166         ;; document type.
9167         (narrow-to-region
9168          (goto-char (point-min))
9169          (or (search-forward "\n\n" nil t) (point)))
9170         (goto-char (point-min))
9171         (delete-matching-lines "^Path:\\|^From ")
9172         (widen))
9173       (unwind-protect
9174           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9175                     (gnus-newsgroup-ephemeral-ignored-charsets
9176                      gnus-newsgroup-ignored-charsets))
9177                 (gnus-group-read-ephemeral-group
9178                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9179                               (nndoc-article-type
9180                                ,(if force 'mbox 'guess)))
9181                  t nil nil nil
9182                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9183                                                         "ADAPT")))))
9184               ;; Make all postings to this group go to the parent group.
9185               (nconc (gnus-info-params (gnus-get-info name))
9186                      params)
9187             ;; Couldn't select this doc group.
9188             (switch-to-buffer buf)
9189             (gnus-set-global-variables)
9190             (gnus-configure-windows 'summary)
9191             (gnus-message 3 "Article couldn't be entered?"))
9192         (kill-buffer dig)))))
9193
9194 (defun gnus-summary-read-document (n)
9195   "Open a new group based on the current article(s).
9196 This will allow you to read digests and other similar
9197 documents as newsgroups.
9198 Obeys the standard process/prefix convention."
9199   (interactive "P")
9200   (let* ((ogroup gnus-newsgroup-name)
9201          (params (append (gnus-info-params (gnus-get-info ogroup))
9202                          (list (cons 'to-group ogroup))))
9203          group egroup groups vgroup)
9204     (dolist (article (gnus-summary-work-articles n))
9205       (setq group (format "%s-%d" gnus-newsgroup-name article))
9206       (gnus-summary-remove-process-mark article)
9207       (when (gnus-summary-display-article article)
9208         (save-excursion ;;What for?
9209           (with-temp-buffer
9210             (insert-buffer-substring gnus-original-article-buffer)
9211             ;; Remove some headers that may lead nndoc to make
9212             ;; the wrong guess.
9213             (message-narrow-to-head)
9214             (goto-char (point-min))
9215             (delete-matching-lines "^Path:\\|^From ")
9216             (widen)
9217             (if (setq egroup
9218                       (gnus-group-read-ephemeral-group
9219                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9220                                      (nndoc-article-type guess))
9221                        t nil t))
9222                 (progn
9223                   ;; Make all postings to this group go to the parent group.
9224                   (nconc (gnus-info-params (gnus-get-info egroup))
9225                          params)
9226                   (push egroup groups))
9227               ;; Couldn't select this doc group.
9228               (gnus-error 3 "Article couldn't be entered"))))))
9229     ;; Now we have selected all the documents.
9230     (cond
9231      ((not groups)
9232       (error "None of the articles could be interpreted as documents"))
9233      ((gnus-group-read-ephemeral-group
9234        (setq vgroup (format
9235                      "nnvirtual:%s-%s" gnus-newsgroup-name
9236                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9237        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9238        t
9239        (cons (current-buffer) 'summary)))
9240      (t
9241       (error "Couldn't select virtual nndoc group")))))
9242
9243 (defun gnus-summary-widget-forward (arg)
9244   "Move point to the next field or button in the article.
9245 With optional ARG, move across that many fields."
9246   (interactive "p")
9247   (gnus-summary-select-article)
9248   (gnus-configure-windows 'article)
9249   (select-window (gnus-get-buffer-window gnus-article-buffer))
9250   (widget-forward arg))
9251
9252 (defun gnus-summary-isearch-article (&optional regexp-p)
9253   "Do incremental search forward on the current article.
9254 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9255   (interactive "P")
9256   (gnus-summary-select-article)
9257   (gnus-configure-windows 'article)
9258   (gnus-eval-in-buffer-window gnus-article-buffer
9259     (save-restriction
9260       (widen)
9261       (isearch-forward regexp-p))))
9262
9263 (defun gnus-summary-repeat-search-article-forward ()
9264   "Repeat the previous search forwards."
9265   (interactive)
9266   (unless gnus-last-search-regexp
9267     (error "No previous search"))
9268   (gnus-summary-search-article-forward gnus-last-search-regexp))
9269
9270 (defun gnus-summary-repeat-search-article-backward ()
9271   "Repeat the previous search backwards."
9272   (interactive)
9273   (unless gnus-last-search-regexp
9274     (error "No previous search"))
9275   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9276
9277 (defun gnus-summary-search-article-forward (regexp &optional backward)
9278   "Search for an article containing REGEXP forward.
9279 If BACKWARD, search backward instead."
9280   (interactive
9281    (list (read-string
9282           (format "Search article %s (regexp%s): "
9283                   (if current-prefix-arg "backward" "forward")
9284                   (if gnus-last-search-regexp
9285                       (concat ", default " gnus-last-search-regexp)
9286                     "")))
9287          current-prefix-arg))
9288   (if (string-equal regexp "")
9289       (setq regexp (or gnus-last-search-regexp ""))
9290     (setq gnus-last-search-regexp regexp)
9291     (setq gnus-article-before-search gnus-current-article))
9292   ;; Intentionally set gnus-last-article.
9293   (setq gnus-last-article gnus-article-before-search)
9294   (let ((gnus-last-article gnus-last-article))
9295     (if (gnus-summary-search-article regexp backward)
9296         (gnus-summary-show-thread)
9297       (signal 'search-failed (list regexp)))))
9298
9299 (defun gnus-summary-search-article-backward (regexp)
9300   "Search for an article containing REGEXP backward."
9301   (interactive
9302    (list (read-string
9303           (format "Search article backward (regexp%s): "
9304                   (if gnus-last-search-regexp
9305                       (concat ", default " gnus-last-search-regexp)
9306                     "")))))
9307   (gnus-summary-search-article-forward regexp 'backward))
9308
9309 (defun gnus-summary-search-article (regexp &optional backward)
9310   "Search for an article containing REGEXP.
9311 Optional argument BACKWARD means do search for backward.
9312 `gnus-select-article-hook' is not called during the search."
9313   ;; We have to require this here to make sure that the following
9314   ;; dynamic binding isn't shadowed by autoloading.
9315   (require 'gnus-async)
9316   (require 'gnus-art)
9317   (let ((gnus-select-article-hook nil)  ;Disable hook.
9318         (gnus-article-prepare-hook nil)
9319         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9320         (gnus-use-article-prefetch nil)
9321         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9322         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9323         (gnus-visual nil)
9324         (gnus-keep-backlog nil)
9325         (gnus-break-pages nil)
9326         (gnus-summary-display-arrow nil)
9327         (gnus-updated-mode-lines nil)
9328         (gnus-auto-center-summary nil)
9329         (sum (current-buffer))
9330         (gnus-display-mime-function nil)
9331         (found nil)
9332         point)
9333     (gnus-save-hidden-threads
9334       (gnus-summary-select-article)
9335       (set-buffer gnus-article-buffer)
9336       (goto-char (window-point (get-buffer-window (current-buffer))))
9337       (when backward
9338         (forward-line -1))
9339       (while (not found)
9340         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9341         (if (if backward
9342                 (re-search-backward regexp nil t)
9343               (re-search-forward regexp nil t))
9344             ;; We found the regexp.
9345             (progn
9346               (setq found 'found)
9347               (beginning-of-line)
9348               (set-window-start
9349                (get-buffer-window (current-buffer))
9350                (point))
9351               (forward-line 1)
9352               (set-window-point
9353                (get-buffer-window (current-buffer))
9354                (point))
9355               (set-buffer sum)
9356               (setq point (point)))
9357           ;; We didn't find it, so we go to the next article.
9358           (set-buffer sum)
9359           (setq found 'not)
9360           (while (eq found 'not)
9361             (if (not (if backward (gnus-summary-find-prev)
9362                        (gnus-summary-find-next)))
9363                 ;; No more articles.
9364                 (setq found t)
9365               ;; Select the next article and adjust point.
9366               (unless (gnus-summary-article-sparse-p
9367                        (gnus-summary-article-number))
9368                 (setq found nil)
9369                 (gnus-summary-select-article)
9370                 (set-buffer gnus-article-buffer)
9371                 (widen)
9372                 (goto-char (if backward (point-max) (point-min))))))))
9373       (gnus-message 7 ""))
9374     ;; Return whether we found the regexp.
9375     (when (eq found 'found)
9376       (goto-char point)
9377       (gnus-summary-show-thread)
9378       (gnus-summary-goto-subject gnus-current-article)
9379       (gnus-summary-position-point)
9380       t)))
9381
9382 (defun gnus-find-matching-articles (header regexp)
9383   "Return a list of all articles that match REGEXP on HEADER.
9384 This search includes all articles in the current group that Gnus has
9385 fetched headers for, whether they are displayed or not."
9386   (let ((articles nil)
9387         ;; Can't eta-reduce because it's a macro.
9388         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9389         (case-fold-search t))
9390     (dolist (header gnus-newsgroup-headers)
9391       (when (string-match regexp (funcall func header))
9392         (push (mail-header-number header) articles)))
9393     (nreverse articles)))
9394
9395 (defun gnus-summary-find-matching (header regexp &optional backward unread
9396                                           not-case-fold not-matching)
9397   "Return a list of all articles that match REGEXP on HEADER.
9398 The search stars on the current article and goes forwards unless
9399 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9400 If UNREAD is non-nil, only unread articles will
9401 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9402 in the comparisons. If NOT-MATCHING, return a list of all articles that
9403 not match REGEXP on HEADER."
9404   (let ((case-fold-search (not not-case-fold))
9405         articles d func)
9406     (if (consp header)
9407         (if (eq (car header) 'extra)
9408             (setq func
9409                   `(lambda (h)
9410                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9411                          "")))
9412           (error "%s is an invalid header" header))
9413       (unless (fboundp (intern (concat "mail-header-" header)))
9414         (error "%s is not a valid header" header))
9415       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9416     (dolist (d (if (eq backward 'all)
9417                    gnus-newsgroup-data
9418                  (gnus-data-find-list
9419                   (gnus-summary-article-number)
9420                   (gnus-data-list backward))))
9421       (when (and (or (not unread)       ; We want all articles...
9422                      (gnus-data-unread-p d)) ; Or just unreads.
9423                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9424                  (if not-matching
9425                      (not (string-match
9426                            regexp
9427                            (funcall func (gnus-data-header d))))
9428                    (string-match regexp
9429                                  (funcall func (gnus-data-header d)))))
9430         (push (gnus-data-number d) articles))) ; Success!
9431     (nreverse articles)))
9432
9433 (defun gnus-summary-execute-command (header regexp command &optional backward)
9434   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9435 If HEADER is an empty string (or nil), the match is done on the entire
9436 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9437   (interactive
9438    (list (let ((completion-ignore-case t))
9439            (gnus-completing-read
9440             "Header name"
9441             (mapcar 'symbol-name
9442                     (append
9443                      '(Number Subject From Lines Date
9444                        Message-ID Xref References Body)
9445                      gnus-extra-headers))
9446             'require-match))
9447          (read-string "Regexp: ")
9448          (read-key-sequence "Command: ")
9449          current-prefix-arg))
9450   (when (equal header "Body")
9451     (setq header ""))
9452   ;; Hidden thread subtrees must be searched as well.
9453   (gnus-summary-show-all-threads)
9454   ;; We don't want to change current point nor window configuration.
9455   (save-excursion
9456     (save-window-excursion
9457       (let (gnus-visual
9458             gnus-treat-strip-trailing-blank-lines
9459             gnus-treat-strip-leading-blank-lines
9460             gnus-treat-strip-multiple-blank-lines
9461             gnus-treat-hide-boring-headers
9462             gnus-treat-fold-newsgroups
9463             gnus-article-prepare-hook)
9464         (gnus-message 6 "Executing %s..." (key-description command))
9465         ;; We'd like to execute COMMAND interactively so as to give arguments.
9466         (gnus-execute header regexp
9467                       `(call-interactively ',(key-binding command))
9468                       backward)
9469         (gnus-message 6 "Executing %s...done" (key-description command))))))
9470
9471 (defun gnus-summary-beginning-of-article ()
9472   "Scroll the article back to the beginning."
9473   (interactive)
9474   (gnus-summary-select-article)
9475   (gnus-configure-windows 'article)
9476   (gnus-eval-in-buffer-window gnus-article-buffer
9477     (widen)
9478     (goto-char (point-min))
9479     (when gnus-break-pages
9480       (gnus-narrow-to-page))))
9481
9482 (defun gnus-summary-end-of-article ()
9483   "Scroll to the end of the article."
9484   (interactive)
9485   (gnus-summary-select-article)
9486   (gnus-configure-windows 'article)
9487   (gnus-eval-in-buffer-window gnus-article-buffer
9488     (widen)
9489     (goto-char (point-max))
9490     (recenter -3)
9491     (when gnus-break-pages
9492       (gnus-narrow-to-page))))
9493
9494 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9495   "Truncate to LEN and quote all \"(\"'s in STRING."
9496   (gnus-replace-in-string (if (and len (> (length string) len))
9497                               (substring string 0 len)
9498                             string)
9499                           "[()]" "\\\\\\&"))
9500
9501 (defun gnus-summary-print-article (&optional filename n)
9502   "Generate and print a PostScript image of the process-marked (mail) articles.
9503
9504 If used interactively, print the current article if none are
9505 process-marked.  With prefix arg, prompt the user for the name of the
9506 file to save in.
9507
9508 When used from Lisp, accept two optional args FILENAME and N.  N means
9509 to print the next N articles.  If N is negative, print the N previous
9510 articles.  If N is nil and articles have been marked with the process
9511 mark, print these instead.
9512
9513 If the optional first argument FILENAME is nil, send the image to the
9514 printer.  If FILENAME is a string, save the PostScript image in a file with
9515 that name.  If FILENAME is a number, prompt the user for the name of the file
9516 to save in."
9517   (interactive (list (ps-print-preprint current-prefix-arg)))
9518   (dolist (article (gnus-summary-work-articles n))
9519     (gnus-summary-select-article nil nil 'pseudo article)
9520     (gnus-eval-in-buffer-window gnus-article-buffer
9521       (gnus-print-buffer))
9522     (gnus-summary-remove-process-mark article))
9523   (ps-despool filename))
9524
9525 (defun gnus-print-buffer ()
9526   (let ((ps-left-header
9527          (list
9528           (concat "("
9529                   (gnus-summary-print-truncate-and-quote
9530                    (mail-header-subject gnus-current-headers)
9531                    66) ")")
9532           (concat "("
9533                   (gnus-summary-print-truncate-and-quote
9534                    (mail-header-from gnus-current-headers)
9535                    45) ")")))
9536         (ps-right-header
9537          (list
9538           "/pagenumberstring load"
9539           (concat "("
9540                   (mail-header-date gnus-current-headers) ")"))))
9541     (gnus-run-hooks 'gnus-ps-print-hook)
9542     (save-excursion
9543       (if ps-print-color-p
9544           (ps-spool-buffer-with-faces)
9545         (ps-spool-buffer)))))
9546
9547 (defun gnus-summary-show-complete-article ()
9548   "Show a complete version of the current article.
9549 This is only useful if you're looking at a partial version of the
9550 article currently."
9551   (interactive)
9552   (let ((gnus-keep-backlog nil)
9553         (gnus-use-cache nil)
9554         (gnus-agent nil)
9555         (variable (intern
9556                    (format "%s-fetch-partial-articles"
9557                            (car (gnus-find-method-for-group
9558                                  gnus-newsgroup-name)))
9559                    obarray))
9560         old-val)
9561     (unwind-protect
9562         (progn
9563           (setq old-val (symbol-value variable))
9564           (set variable nil)
9565           (gnus-flush-original-article-buffer)
9566           (gnus-summary-show-article))
9567       (set variable old-val))))
9568
9569 (defun gnus-summary-show-article (&optional arg)
9570   "Force redisplaying of the current article.
9571 If ARG (the prefix) is a number, show the article with the charset
9572 defined in `gnus-summary-show-article-charset-alist', or the charset
9573 input.
9574 If ARG (the prefix) is non-nil and not a number, show the article,
9575 but without running any of the article treatment functions
9576 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9577 C-u g', show the raw article."
9578   (interactive "P")
9579   (cond
9580    ((numberp arg)
9581     (gnus-summary-show-article t)
9582     (let ((gnus-newsgroup-charset
9583            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9584                (mm-read-coding-system
9585                 "View as charset: " ;; actually it is coding system.
9586                 (with-current-buffer gnus-article-buffer
9587                   (mm-detect-coding-region (point) (point-max))))))
9588           (gnus-newsgroup-ignored-charsets 'gnus-all))
9589       (gnus-summary-select-article nil 'force)
9590       (let ((deps gnus-newsgroup-dependencies)
9591             head header lines)
9592         (with-current-buffer gnus-original-article-buffer
9593           (save-restriction
9594             (message-narrow-to-head)
9595             (setq head (buffer-string))
9596             (goto-char (point-min))
9597             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9598               (goto-char (point-max))
9599               (widen)
9600               (setq lines (1- (count-lines (point) (point-max))))))
9601           (with-temp-buffer
9602             (insert (format "211 %d Article retrieved.\n"
9603                             (cdr gnus-article-current)))
9604             (insert head)
9605             (if lines (insert (format "Lines: %d\n" lines)))
9606             (insert ".\n")
9607             (let ((nntp-server-buffer (current-buffer)))
9608               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9609         (gnus-data-set-header
9610          (gnus-data-find (cdr gnus-article-current))
9611          header)
9612         (gnus-summary-update-article-line
9613          (cdr gnus-article-current) header)
9614         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9615           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9616    ((not arg)
9617     ;; Select the article the normal way.
9618     (if (eq mm-text-html-renderer 'shr)
9619         (progn
9620           (require 'shr)
9621           (let ((shr-ignore-cache t))
9622             (gnus-summary-select-article nil 'force)))
9623       (gnus-summary-select-article nil 'force)))
9624    ((equal arg '(16))
9625     ;; C-u C-u g
9626     (let ((gnus-inhibit-article-treatments t))
9627       (gnus-summary-select-article nil 'force)))
9628    (t
9629     ;; We have to require this here to make sure that the following
9630     ;; dynamic binding isn't shadowed by autoloading.
9631     (require 'gnus-async)
9632     (require 'gnus-art)
9633     ;; Bind the article treatment functions to nil.
9634     (let ((gnus-have-all-headers t)
9635           gnus-article-prepare-hook
9636           gnus-article-decode-hook
9637           gnus-display-mime-function
9638           gnus-break-pages)
9639       ;; Destroy any MIME parts.
9640       (when (gnus-buffer-live-p gnus-article-buffer)
9641         (with-current-buffer gnus-article-buffer
9642           (gnus-article-stop-animations)
9643           (mm-destroy-parts gnus-article-mime-handles)
9644           ;; Set it to nil for safety reason.
9645           (setq gnus-article-mime-handle-alist nil)
9646           (setq gnus-article-mime-handles nil)))
9647       (gnus-summary-select-article nil 'force))))
9648   (gnus-summary-goto-subject gnus-current-article)
9649   (gnus-summary-position-point))
9650
9651 (defun gnus-summary-show-raw-article ()
9652   "Show the raw article without any article massaging functions being run."
9653   (interactive)
9654   (gnus-summary-show-article t))
9655
9656 (defun gnus-summary-verbose-headers (&optional arg)
9657   "Toggle permanent full header display.
9658 If ARG is a positive number, turn header display on.
9659 If ARG is a negative number, turn header display off."
9660   (interactive "P")
9661   (setq gnus-show-all-headers
9662         (cond ((or (not (numberp arg))
9663                    (zerop arg))
9664                (not gnus-show-all-headers))
9665               ((natnump arg)
9666                t)))
9667   (gnus-summary-show-article))
9668
9669 (defun gnus-summary-toggle-header (&optional arg)
9670   "Show the headers if they are hidden, or hide them if they are shown.
9671 If ARG is a positive number, show the entire header.
9672 If ARG is a negative number, hide the unwanted header lines."
9673   (interactive "P")
9674   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9675                      (get-buffer-window gnus-article-buffer t))))
9676     (with-current-buffer gnus-article-buffer
9677       (widen)
9678       (article-narrow-to-head)
9679       (let* ((inhibit-read-only t)
9680              (inhibit-point-motion-hooks t)
9681              (hidden (if (numberp arg)
9682                          (>= arg 0)
9683                        (or (not (looking-at "[^ \t\n]+:"))
9684                            (gnus-article-hidden-text-p 'headers))))
9685              s e)
9686         (delete-region (point-min) (point-max))
9687         (with-current-buffer gnus-original-article-buffer
9688           (goto-char (setq s (point-min)))
9689           (setq e (if (search-forward "\n\n" nil t)
9690                       (1- (point))
9691                     (point-max))))
9692         (insert-buffer-substring gnus-original-article-buffer s e)
9693         (run-hooks 'gnus-article-decode-hook)
9694         (if hidden
9695             (let ((gnus-treat-hide-headers nil)
9696                   (gnus-treat-hide-boring-headers nil))
9697               (gnus-delete-wash-type 'headers)
9698               (gnus-treat-article 'head))
9699           (gnus-treat-article 'head))
9700         (widen)
9701         (if window
9702             (set-window-start window (goto-char (point-min))))
9703         (if gnus-break-pages
9704             (gnus-narrow-to-page)
9705           (when (gnus-visual-p 'page-marker)
9706             (let ((inhibit-read-only t))
9707               (gnus-remove-text-with-property 'gnus-prev)
9708               (gnus-remove-text-with-property 'gnus-next))))
9709         (gnus-set-mode-line 'article)))))
9710
9711 (defun gnus-summary-show-all-headers ()
9712   "Make all header lines visible."
9713   (interactive)
9714   (gnus-summary-toggle-header 1))
9715
9716 (defun gnus-summary-caesar-message (&optional arg)
9717   "Caesar rotate the current article by 13.
9718 With a non-numerical prefix, also rotate headers.  A numerical
9719 prefix specifies how many places to rotate each letter forward."
9720   (interactive "P")
9721   (gnus-summary-select-article)
9722   (let ((mail-header-separator ""))
9723     (gnus-eval-in-buffer-window gnus-article-buffer
9724       (save-restriction
9725         (widen)
9726         (let ((start (window-start))
9727               (inhibit-read-only t))
9728           (if (equal arg '(4))
9729               (message-caesar-buffer-body nil t)
9730             (message-caesar-buffer-body arg))
9731           (set-window-start (get-buffer-window (current-buffer)) start)))))
9732   ;; Create buttons and stuff...
9733   (gnus-treat-article nil))
9734
9735 (declare-function idna-to-unicode "ext:idna" (str))
9736
9737 (defun gnus-summary-idna-message (&optional arg)
9738   "Decode IDNA encoded domain names in the current articles.
9739 IDNA encoded domain names looks like `xn--bar'.  If a string
9740 remain unencoded after running this function, it is likely an
9741 invalid IDNA string (`xn--bar' is invalid).
9742
9743 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9744 installed for this command to work."
9745   (interactive "P")
9746   (if (not (and (condition-case nil (require 'idna)
9747                   (file-error))
9748                 (mm-coding-system-p 'utf-8)
9749                 (executable-find (symbol-value 'idna-program))))
9750       (gnus-message
9751        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9752     (gnus-summary-select-article)
9753     (let ((mail-header-separator ""))
9754       (gnus-eval-in-buffer-window gnus-article-buffer
9755         (save-restriction
9756           (widen)
9757           (let ((start (window-start))
9758                 buffer-read-only)
9759             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9760               (replace-match (idna-to-unicode (match-string 1))))
9761             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9762
9763 (defun gnus-summary-morse-message (&optional arg)
9764   "Morse decode the current article."
9765   (interactive "P")
9766   (gnus-summary-select-article)
9767   (let ((mail-header-separator ""))
9768     (gnus-eval-in-buffer-window gnus-article-buffer
9769       (save-excursion
9770         (save-restriction
9771           (widen)
9772           (let ((pos (window-start))
9773                 (inhibit-read-only t))
9774             (goto-char (point-min))
9775             (when (message-goto-body)
9776               (gnus-narrow-to-body))
9777             (goto-char (point-min))
9778             (while (search-forward "·" (point-max) t)
9779               (replace-match "."))
9780             (unmorse-region (point-min) (point-max))
9781             (widen)
9782             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9783
9784 (defun gnus-summary-stop-page-breaking ()
9785   "Stop page breaking in the current article."
9786   (interactive)
9787   (gnus-summary-select-article)
9788   (gnus-eval-in-buffer-window gnus-article-buffer
9789     (widen)
9790     (when (gnus-visual-p 'page-marker)
9791       (let ((inhibit-read-only t))
9792         (gnus-remove-text-with-property 'gnus-prev)
9793         (gnus-remove-text-with-property 'gnus-next))
9794       (setq gnus-page-broken nil))))
9795
9796 (defun gnus-summary-move-article (&optional n to-newsgroup
9797                                             select-method action)
9798   "Move the current article to a different newsgroup.
9799 If N is a positive number, move the N next articles.
9800 If N is a negative number, move the N previous articles.
9801 If N is nil and any articles have been marked with the process mark,
9802 move those articles instead.
9803 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9804 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9805 re-spool using this method.
9806
9807 When called interactively with TO-NEWSGROUP being nil, the value of
9808 the variable `gnus-move-split-methods' is used for finding a default
9809 for the target newsgroup.
9810
9811 For this function to work, both the current newsgroup and the
9812 newsgroup that you want to move to have to support the `request-move'
9813 and `request-accept' functions.
9814
9815 ACTION can be either `move' (the default), `crosspost' or `copy'."
9816   (interactive "P")
9817   (unless action
9818     (setq action 'move))
9819   ;; Check whether the source group supports the required functions.
9820   (cond ((and (eq action 'move)
9821               (not (gnus-check-backend-function
9822                     'request-move-article gnus-newsgroup-name)))
9823          (error "The current group does not support article moving"))
9824         ((and (eq action 'crosspost)
9825               (not (gnus-check-backend-function
9826                     'request-replace-article gnus-newsgroup-name)))
9827          (error "The current group does not support article editing")))
9828   (let ((articles (gnus-summary-work-articles n))
9829         (prefix (if (gnus-check-backend-function
9830                      'request-move-article gnus-newsgroup-name)
9831                     (funcall gnus-move-group-prefix-function
9832                              gnus-newsgroup-name)
9833                   ""))
9834         (names '((move "Move" "Moving")
9835                  (copy "Copy" "Copying")
9836                  (crosspost "Crosspost" "Crossposting")))
9837         (copy-buf (save-excursion
9838                     (nnheader-set-temp-buffer " *copy article*")))
9839         art-group to-method new-xref article to-groups
9840         articles-to-update-marks encoded)
9841     (unless (assq action names)
9842       (error "Unknown action %s" action))
9843     ;; Read the newsgroup name.
9844     (when (and (not to-newsgroup)
9845                (not select-method))
9846       (if (and gnus-move-split-methods
9847                (not
9848                 (and (memq gnus-current-article articles)
9849                      (gnus-buffer-live-p gnus-original-article-buffer))))
9850           ;; When `gnus-move-split-methods' is non-nil, we have to
9851           ;; select an article to give `gnus-read-move-group-name' an
9852           ;; opportunity to suggest an appropriate default.  However,
9853           ;; we needn't render or mark the article.
9854           (let ((gnus-display-mime-function nil)
9855                 (gnus-article-prepare-hook nil)
9856                 (gnus-mark-article-hook nil))
9857             (gnus-summary-select-article nil nil nil (car articles))))
9858       (setq to-newsgroup (gnus-read-move-group-name
9859                           (cadr (assq action names))
9860                           (symbol-value
9861                            (intern (format "gnus-current-%s-group" action)))
9862                           articles prefix)
9863             encoded to-newsgroup
9864             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9865       (set (intern (format "gnus-current-%s-group" action))
9866            (mm-decode-coding-string
9867             to-newsgroup
9868             (gnus-group-name-charset to-method to-newsgroup))))
9869     (unless to-method
9870       (setq to-method (or select-method
9871                           (gnus-server-to-method
9872                            (gnus-group-method to-newsgroup)))))
9873     (setq to-newsgroup
9874           (or encoded
9875               (and to-newsgroup
9876                    (mm-encode-coding-string
9877                     to-newsgroup
9878                     (gnus-group-name-charset to-method to-newsgroup)))))
9879     ;; Check the method we are to move this article to...
9880     (unless (gnus-check-backend-function
9881              'request-accept-article (car to-method))
9882       (error "%s does not support article copying" (car to-method)))
9883     (unless (gnus-check-server to-method)
9884       (error "Can't open server %s" (car to-method)))
9885     (gnus-message 6 "%s to %s: %s..."
9886                   (caddr (assq action names))
9887                   (or (car select-method)
9888                       (gnus-group-decoded-name to-newsgroup))
9889                   articles)
9890     (while articles
9891       (setq article (pop articles))
9892       ;; Set any marks that may have changed in the summary buffer.
9893       (when gnus-preserve-marks
9894         (gnus-summary-push-marks-to-backend article))
9895       (setq
9896        art-group
9897        (cond
9898         ;; Move the article.
9899         ((eq action 'move)
9900          ;; Remove this article from future suppression.
9901          (gnus-dup-unsuppress-article article)
9902          (let* ((from-method (gnus-find-method-for-group
9903                               gnus-newsgroup-name))
9904                 (to-method (or select-method
9905                                (gnus-find-method-for-group to-newsgroup)))
9906                 (move-is-internal (gnus-server-equal from-method to-method)))
9907            (gnus-request-move-article
9908             article                     ; Article to move
9909             gnus-newsgroup-name         ; From newsgroup
9910             (nth 1 (gnus-find-method-for-group
9911                     gnus-newsgroup-name)) ; Server
9912             (list 'gnus-request-accept-article
9913                   to-newsgroup (list 'quote select-method)
9914                   (not articles) t)     ; Accept form
9915             (not articles)              ; Only save nov last time
9916             (and move-is-internal
9917                  to-newsgroup           ; Not respooling
9918                                         ; Is this move internal?
9919                  (gnus-group-real-name to-newsgroup)))))
9920         ;; Copy the article.
9921         ((eq action 'copy)
9922          (with-current-buffer copy-buf
9923            (when (gnus-request-article-this-buffer article
9924                                                    gnus-newsgroup-name)
9925              (save-restriction
9926                (nnheader-narrow-to-headers)
9927                (dolist (hdr gnus-copy-article-ignored-headers)
9928                  (message-remove-header hdr t)))
9929              (gnus-request-accept-article
9930               to-newsgroup select-method (not articles) t))))
9931         ;; Crosspost the article.
9932         ((eq action 'crosspost)
9933          (let ((xref (message-tokenize-header
9934                       (mail-header-xref (gnus-summary-article-header
9935                                          article))
9936                       " ")))
9937            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9938                                   ":" (number-to-string article)))
9939            (unless xref
9940              (setq xref (list (system-name))))
9941            (setq new-xref
9942                  (concat
9943                   (mapconcat 'identity
9944                              (delete "Xref:" (delete new-xref xref))
9945                              " ")
9946                   " " new-xref))
9947            (with-current-buffer copy-buf
9948              ;; First put the article in the destination group.
9949              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9950              (when (consp (setq art-group
9951                                 (gnus-request-accept-article
9952                                  to-newsgroup select-method (not articles)
9953                                  t)))
9954                (setq new-xref (concat new-xref " " (car art-group)
9955                                       ":"
9956                                       (number-to-string (cdr art-group))))
9957                ;; Now we have the new Xrefs header, so we insert
9958                ;; it and replace the new article.
9959                (nnheader-replace-header "Xref" new-xref)
9960                (gnus-request-replace-article
9961                 (cdr art-group) to-newsgroup (current-buffer) t)
9962                art-group))))))
9963       (cond
9964        ((not art-group)
9965         (gnus-message 1 "Couldn't %s article %s: %s"
9966                       (cadr (assq action names)) article
9967                       (nnheader-get-report (car to-method))))
9968        ((eq art-group 'junk)
9969         (when (eq action 'move)
9970           (gnus-summary-mark-article article gnus-canceled-mark)
9971           (gnus-message 4 "Deleted article %s" article)
9972           ;; run the delete hook
9973           (run-hook-with-args 'gnus-summary-article-delete-hook
9974                               action
9975                               (gnus-data-header
9976                                (assoc article (gnus-data-list nil)))
9977                               gnus-newsgroup-name nil
9978                               select-method)))
9979        (t
9980         (let* ((pto-group (gnus-group-prefixed-name
9981                            (car art-group) to-method))
9982                (info (gnus-get-info pto-group))
9983                (to-group (gnus-info-group info))
9984                to-marks)
9985           ;; Update the group that has been moved to.
9986           (when (and info
9987                      (memq action '(move copy)))
9988             (unless (member to-group to-groups)
9989               (push to-group to-groups))
9990
9991             (when (and (not (memq article gnus-newsgroup-unreads))
9992                        (cdr art-group))
9993               (push 'read to-marks)
9994               (gnus-info-set-read
9995                info (gnus-add-to-range (gnus-info-read info)
9996                                        (list (cdr art-group)))))
9997
9998             ;; See whether the article is to be put in the cache.
9999             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10000                    (marks (if expirable
10001                               gnus-article-mark-lists
10002                             (delete '(expirable . expire)
10003                                     (copy-sequence
10004                                      gnus-article-mark-lists))))
10005                    (to-article (cdr art-group)))
10006
10007               ;; Enter the article into the cache in the new group,
10008               ;; if that is required.
10009               (when (and to-article
10010                          gnus-use-cache)
10011                 (gnus-cache-possibly-enter-article
10012                  to-group to-article
10013                  (memq article gnus-newsgroup-marked)
10014                  (memq article gnus-newsgroup-dormant)
10015                  (memq article gnus-newsgroup-unreads)))
10016
10017               (when (and gnus-preserve-marks
10018                          to-article)
10019                 ;; Copy any marks over to the new group.
10020                 (when (and (equal to-group gnus-newsgroup-name)
10021                            (not (memq article gnus-newsgroup-unreads)))
10022                   ;; Mark this article as read in this group.
10023                   (push (cons to-article gnus-read-mark)
10024                         gnus-newsgroup-reads)
10025                   ;; Increase the active status of this group.
10026                   (setcdr (gnus-active to-group) to-article)
10027                   (setcdr gnus-newsgroup-active to-article))
10028
10029                 (while marks
10030                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10031                     (when (memq article (symbol-value
10032                                          (intern (format "gnus-newsgroup-%s"
10033                                                          (caar marks)))))
10034                       (push (cdar marks) to-marks)
10035                       ;; If the other group is the same as this group,
10036                       ;; then we have to add the mark to the list.
10037                       (when (equal to-group gnus-newsgroup-name)
10038                         (set (intern (format "gnus-newsgroup-%s"
10039                                              (caar marks)))
10040                              (cons to-article
10041                                    (symbol-value
10042                                     (intern (format "gnus-newsgroup-%s"
10043                                                     (caar marks)))))))
10044                       ;; Copy the marks to other group.
10045                       (gnus-add-marked-articles
10046                        to-group (cdar marks) (list to-article) info)))
10047                   (setq marks (cdr marks)))
10048
10049                 (when (and expirable
10050                            gnus-mark-copied-or-moved-articles-as-expirable
10051                            (not (memq 'expire to-marks)))
10052                   ;; Mark this article as expirable.
10053                   (push 'expire to-marks)
10054                   (when (equal to-group gnus-newsgroup-name)
10055                     (push to-article gnus-newsgroup-expirable))
10056                   ;; Copy the expirable mark to other group.
10057                   (gnus-add-marked-articles
10058                    to-group 'expire (list to-article) info))
10059
10060                 (when to-marks
10061                   (gnus-request-set-mark
10062                    to-group (list (list (list to-article) 'add to-marks)))))
10063
10064               (gnus-dribble-enter
10065                (concat "(gnus-group-set-info '"
10066                        (gnus-prin1-to-string (gnus-get-info to-group))
10067                        ")")
10068                (concat "^(gnus-group-set-info '(\""
10069                        (regexp-quote to-group) "\""))))
10070
10071           ;; Update the Xref header in this article to point to
10072           ;; the new crossposted article we have just created.
10073           (when (eq action 'crosspost)
10074             (with-current-buffer copy-buf
10075               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10076               (nnheader-replace-header "Xref" new-xref)
10077               (gnus-request-replace-article
10078                article gnus-newsgroup-name (current-buffer) t)))
10079
10080           ;; run the move/copy/crosspost/respool hook
10081           (run-hook-with-args 'gnus-summary-article-move-hook
10082                               action
10083                               (gnus-data-header
10084                                (assoc article (gnus-data-list nil)))
10085                               gnus-newsgroup-name
10086                               to-newsgroup
10087                               select-method))
10088
10089         ;;;!!!Why is this necessary?
10090         (set-buffer gnus-summary-buffer)
10091
10092         (when (eq action 'move)
10093           (save-excursion
10094             (gnus-summary-goto-subject article)
10095             (gnus-summary-mark-article article gnus-canceled-mark)))))
10096       (push article articles-to-update-marks))
10097
10098     (save-excursion
10099       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10100     ;; Re-activate all groups that have been moved to.
10101     (with-current-buffer gnus-group-buffer
10102       (let ((gnus-group-marked to-groups))
10103         (gnus-group-get-new-news-this-group nil t)))
10104
10105     (gnus-kill-buffer copy-buf)
10106     (gnus-summary-position-point)
10107     (gnus-set-mode-line 'summary)))
10108
10109 (defun gnus-summary-push-marks-to-backend (article)
10110   (let ((set nil)
10111         (marks gnus-article-mark-lists))
10112     (unless (memq article gnus-newsgroup-unreads)
10113       (push 'read set))
10114     (while marks
10115       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10116                  (memq article (symbol-value
10117                                 (intern (format "gnus-newsgroup-%s"
10118                                                 (caar marks))))))
10119         (push (cdar marks) set))
10120       (pop marks))
10121     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10122
10123 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10124   "Copy the current article to some other group.
10125 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10126 When called interactively, if TO-NEWSGROUP is nil, use the value of
10127 the variable `gnus-move-split-methods' for finding a default target
10128 newsgroup.
10129 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10130 re-spool using this method."
10131   (interactive "P")
10132   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10133
10134 (defun gnus-summary-crosspost-article (&optional n)
10135   "Crosspost the current article to some other group."
10136   (interactive "P")
10137   (gnus-summary-move-article n nil nil 'crosspost))
10138
10139 (defcustom gnus-summary-respool-default-method nil
10140   "Default method type for respooling an article.
10141 If nil, use to the current newsgroup method."
10142   :type 'symbol
10143   :group 'gnus-summary-mail)
10144
10145 (defun gnus-summary-respool-article (&optional n method)
10146   "Respool the current article.
10147 The article will be squeezed through the mail spooling process again,
10148 which means that it will be put in some mail newsgroup or other
10149 depending on `nnmail-split-methods'.
10150 If N is a positive number, respool the N next articles.
10151 If N is a negative number, respool the N previous articles.
10152 If N is nil and any articles have been marked with the process mark,
10153 respool those articles instead.
10154
10155 Respooling can be done both from mail groups and \"real\" newsgroups.
10156 In the former case, the articles in question will be moved from the
10157 current group into whatever groups they are destined to.  In the
10158 latter case, they will be copied into the relevant groups."
10159   (interactive
10160    (list current-prefix-arg
10161          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10162                 (methname
10163                  (symbol-name (or gnus-summary-respool-default-method
10164                                   (car (gnus-find-method-for-group
10165                                         gnus-newsgroup-name)))))
10166                 (method
10167                  (gnus-completing-read
10168                   "Backend to use when respooling"
10169                   methods t nil 'gnus-mail-method-history methname))
10170                 ms)
10171            (cond
10172             ((zerop (length (setq ms (gnus-servers-using-backend
10173                                       (intern method)))))
10174              (list (intern method) ""))
10175             ((= 1 (length ms))
10176              (car ms))
10177             (t
10178              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10179                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10180                            ms-alist))))))))
10181   (unless method
10182     (error "No method given for respooling"))
10183   (if (assoc (symbol-name
10184               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10185              (gnus-methods-using 'respool))
10186       (gnus-summary-move-article n nil method)
10187     (gnus-summary-copy-article n nil method)))
10188
10189 (defun gnus-summary-import-article (file &optional edit)
10190   "Import an arbitrary file into a mail newsgroup."
10191   (interactive "fImport file: \nP")
10192   (let ((group gnus-newsgroup-name)
10193         (now (current-time))
10194         atts lines group-art)
10195     (unless (gnus-check-backend-function 'request-accept-article group)
10196       (error "%s does not support article importing" group))
10197     (or (file-readable-p file)
10198         (not (file-regular-p file))
10199         (error "Can't read %s" file))
10200     (with-current-buffer (gnus-get-buffer-create " *import file*")
10201       (erase-buffer)
10202       (nnheader-insert-file-contents file)
10203       (goto-char (point-min))
10204       (if (nnheader-article-p)
10205           (save-restriction
10206             (goto-char (point-min))
10207             (search-forward "\n\n" nil t)
10208             (narrow-to-region (point-min) (1- (point)))
10209             (goto-char (point-min))
10210             (unless (re-search-forward "^date:" nil t)
10211               (goto-char (point-max))
10212               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10213        ;; This doesn't look like an article, so we fudge some headers.
10214         (setq atts (file-attributes file)
10215               lines (count-lines (point-min) (point-max)))
10216         (insert "From: " (read-string "From: ") "\n"
10217                 "Subject: " (read-string "Subject: ") "\n"
10218                 "Date: " (message-make-date (nth 5 atts)) "\n"
10219                 "Message-ID: " (message-make-message-id) "\n"
10220                 "Lines: " (int-to-string lines) "\n"
10221                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10222       (setq group-art (gnus-request-accept-article group nil t))
10223       (kill-buffer (current-buffer)))
10224     (setq gnus-newsgroup-active (gnus-activate-group group))
10225     (forward-line 1)
10226     (gnus-summary-goto-article (cdr group-art) nil t)
10227     (when edit
10228       (gnus-summary-edit-article))))
10229
10230 (defun gnus-summary-create-article ()
10231   "Create an article in a mail newsgroup."
10232   (interactive)
10233   (let ((group gnus-newsgroup-name)
10234         (now (current-time))
10235         group-art)
10236     (unless (gnus-check-backend-function 'request-accept-article group)
10237       (error "%s does not support article importing" group))
10238     (with-current-buffer (gnus-get-buffer-create " *import file*")
10239       (erase-buffer)
10240       (goto-char (point-min))
10241       ;; This doesn't look like an article, so we fudge some headers.
10242       (insert "From: " (read-string "From: ") "\n"
10243               "Subject: " (read-string "Subject: ") "\n"
10244               "Date: " (message-make-date now) "\n"
10245               "Message-ID: " (message-make-message-id) "\n")
10246       (setq group-art (gnus-request-accept-article group nil t))
10247       (kill-buffer (current-buffer)))
10248     (setq gnus-newsgroup-active (gnus-activate-group group))
10249     (forward-line 1)
10250     (gnus-summary-goto-article (cdr group-art) nil t)
10251     (gnus-summary-edit-article)))
10252
10253 (defun gnus-summary-article-posted-p ()
10254   "Say whether the current (mail) article is available from news as well.
10255 This will be the case if the article has both been mailed and posted."
10256   (interactive)
10257   (let ((id (mail-header-references (gnus-summary-article-header)))
10258         (gnus-override-method (car (gnus-refer-article-methods))))
10259     (if (gnus-request-head id "")
10260         (gnus-message 2 "The current message was found on %s"
10261                       gnus-override-method)
10262       (gnus-message 2 "The current message couldn't be found on %s"
10263                     gnus-override-method)
10264       nil)))
10265
10266 (defun gnus-summary-expire-articles (&optional now)
10267   "Expire all articles that are marked as expirable in the current group."
10268   (interactive)
10269   (when (and (not gnus-group-is-exiting-without-update-p)
10270              (gnus-check-backend-function
10271               'request-expire-articles gnus-newsgroup-name))
10272     ;; This backend supports expiry.
10273     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10274            (expirable (if total
10275                           (progn
10276                             ;; We need to update the info for
10277                             ;; this group for `gnus-list-of-read-articles'
10278                             ;; to give us the right answer.
10279                             (gnus-run-hooks 'gnus-exit-group-hook)
10280                             (gnus-summary-update-info)
10281                             (gnus-list-of-read-articles gnus-newsgroup-name))
10282                         (setq gnus-newsgroup-expirable
10283                               (sort gnus-newsgroup-expirable '<))))
10284            (expiry-wait (if now 'immediate
10285                           (gnus-group-find-parameter
10286                            gnus-newsgroup-name 'expiry-wait)))
10287            (nnmail-expiry-target
10288             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10289                 nnmail-expiry-target))
10290            es)
10291       (when expirable
10292         ;; There are expirable articles in this group, so we run them
10293         ;; through the expiry process.
10294         (gnus-message 6 "Expiring articles...")
10295         (when (gnus-check-group gnus-newsgroup-name)
10296           ;; The list of articles that weren't expired is returned.
10297           (save-excursion
10298             (if expiry-wait
10299                 (let ((nnmail-expiry-wait-function nil)
10300                       (nnmail-expiry-wait expiry-wait))
10301                   (setq es (gnus-request-expire-articles
10302                             expirable gnus-newsgroup-name)))
10303               (setq es (gnus-request-expire-articles
10304                         expirable gnus-newsgroup-name)))
10305             (unless total
10306               (setq gnus-newsgroup-expirable es))
10307             ;; We go through the old list of expirable, and mark all
10308             ;; really expired articles as nonexistent.
10309             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10310               (let ((gnus-use-cache nil))
10311                 (dolist (article expirable)
10312                   (when (and (not (memq article es))
10313                              (gnus-data-find article))
10314                     (gnus-summary-mark-article article gnus-canceled-mark)
10315                     (run-hook-with-args 'gnus-summary-article-expire-hook
10316                                         'delete
10317                                         (gnus-data-header
10318                                          (assoc article (gnus-data-list nil)))
10319                                         gnus-newsgroup-name
10320                                         nil
10321                                         nil)))))))
10322         (gnus-message 6 "Expiring articles...done")))))
10323
10324 (defun gnus-summary-expire-articles-now ()
10325   "Expunge all expirable articles in the current group.
10326 This means that *all* articles that are marked as expirable will be
10327 deleted forever, right now."
10328   (interactive)
10329   (or gnus-expert-user
10330       (gnus-yes-or-no-p
10331        "Are you really, really sure you want to delete all expirable messages? ")
10332       (error "Phew!"))
10333   (gnus-summary-expire-articles t))
10334
10335 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10336 (defun gnus-summary-delete-article (&optional n)
10337   "Delete the N next (mail) articles.
10338 This command actually deletes articles.  This is not a marking
10339 command.  The article will disappear forever from your life, never to
10340 return.
10341
10342 If N is negative, delete backwards.
10343 If N is nil and articles have been marked with the process mark,
10344 delete these instead.
10345
10346 If `gnus-novice-user' is non-nil you will be asked for
10347 confirmation before the articles are deleted."
10348   (interactive "P")
10349   (unless (gnus-check-backend-function 'request-expire-articles
10350                                        gnus-newsgroup-name)
10351     (error "The current newsgroup does not support article deletion"))
10352   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10353     (error "Couldn't open server"))
10354   ;; Compute the list of articles to delete.
10355   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10356         (nnmail-expiry-target 'delete)
10357         not-deleted)
10358     (if (and gnus-novice-user
10359              (not (gnus-yes-or-no-p
10360                    (format "Do you really want to delete %s forever? "
10361                            (if (> (length articles) 1)
10362                                (format "these %s articles" (length articles))
10363                              "this article")))))
10364         ()
10365       ;; Delete the articles.
10366       (setq not-deleted (gnus-request-expire-articles
10367                          articles gnus-newsgroup-name 'force))
10368       (save-excursion
10369         (while articles
10370           (gnus-summary-remove-process-mark (car articles))
10371           ;; The backend might not have been able to delete the article
10372           ;; after all.
10373           (unless (memq (car articles) not-deleted)
10374             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10375             (let* ((article (car articles))
10376                    (ghead  (gnus-data-header
10377                             (assoc article (gnus-data-list nil)))))
10378               (run-hook-with-args 'gnus-summary-article-delete-hook
10379                                   'delete ghead gnus-newsgroup-name nil
10380                                   nil)))
10381           (setq articles (cdr articles))))
10382       (when not-deleted
10383         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10384     (gnus-summary-position-point)
10385     (gnus-set-mode-line 'summary)
10386     not-deleted))
10387
10388 (defun gnus-summary-edit-article (&optional arg)
10389   "Edit the current article.
10390 This will have permanent effect only in mail groups.
10391 If ARG is nil, edit the decoded articles.
10392 If ARG is 1, edit the raw articles.
10393 If ARG is 2, edit the raw articles even in read-only groups.
10394 If ARG is 3, edit the articles with the current handles.
10395 Otherwise, allow editing of articles even in read-only
10396 groups."
10397   (interactive "P")
10398   (let (force raw current-handles)
10399     (cond
10400      ((null arg))
10401      ((eq arg 1)
10402       (setq raw t))
10403      ((eq arg 2)
10404       (setq raw t
10405             force t))
10406      ((eq arg 3)
10407       (setq current-handles
10408             (and (gnus-buffer-live-p gnus-article-buffer)
10409                  (with-current-buffer gnus-article-buffer
10410                    (prog1
10411                        gnus-article-mime-handles
10412                      (setq gnus-article-mime-handles nil))))))
10413      (t
10414       (setq force t)))
10415     (when (and raw (not force)
10416                (member gnus-newsgroup-name '("nndraft:delayed"
10417                                              "nndraft:drafts"
10418                                              "nndraft:queue")))
10419       (error "Can't edit the raw article in group %s"
10420              gnus-newsgroup-name))
10421     (with-current-buffer gnus-summary-buffer
10422       (let ((mail-parse-charset gnus-newsgroup-charset)
10423             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10424         (gnus-set-global-variables)
10425         (when (and (not force)
10426                    (gnus-group-read-only-p))
10427           (error "The current newsgroup does not support article editing"))
10428         (gnus-summary-show-article t)
10429         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10430           (with-current-buffer gnus-article-buffer
10431             (mm-enable-multibyte)))
10432         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10433             (setq raw t))
10434         (gnus-article-edit-article
10435          (if raw 'ignore
10436            `(lambda ()
10437               (let ((mbl mml-buffer-list))
10438                 (setq mml-buffer-list nil)
10439                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10440                   (mime-to-mml ,'current-handles))
10441                 (let ((mbl1 mml-buffer-list))
10442                   (setq mml-buffer-list mbl)
10443                   (set (make-local-variable 'mml-buffer-list) mbl1))
10444                 (gnus-make-local-hook 'kill-buffer-hook)
10445                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10446          `(lambda (no-highlight)
10447             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10448                   (message-options message-options)
10449                   (message-options-set-recipient)
10450                   (mail-parse-ignored-charsets
10451                    ',gnus-newsgroup-ignored-charsets)
10452                   (rfc2047-header-encoding-alist
10453                    ',(let ((charset (gnus-group-name-charset
10454                                      (gnus-find-method-for-group
10455                                       gnus-newsgroup-name)
10456                                      gnus-newsgroup-name)))
10457                        (append (list (cons "Newsgroups" charset)
10458                                      (cons "Followup-To" charset)
10459                                      (cons "Xref" charset))
10460                                rfc2047-header-encoding-alist))))
10461               ,(if (not raw) '(progn
10462                                 (mml-to-mime)
10463                                 (mml-destroy-buffers)
10464                                 (remove-hook 'kill-buffer-hook
10465                                              'mml-destroy-buffers t)
10466                                 (kill-local-variable 'mml-buffer-list)))
10467               (gnus-summary-edit-article-done
10468                ,(or (mail-header-references gnus-current-headers) "")
10469                ,(gnus-group-read-only-p)
10470                ,gnus-summary-buffer no-highlight))))))))
10471
10472 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10473
10474 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10475                                                  no-highlight)
10476   "Make edits to the current article permanent."
10477   (interactive)
10478   (save-excursion
10479     ;; The buffer restriction contains the entire article if it exists.
10480     (when (article-goto-body)
10481       (let ((lines (count-lines (point) (point-max)))
10482             (length (- (point-max) (point)))
10483             (case-fold-search t)
10484             (body (copy-marker (point))))
10485         (goto-char (point-min))
10486         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10487           (delete-region (match-beginning 1) (match-end 1))
10488           (insert (number-to-string length)))
10489         (goto-char (point-min))
10490         (when (re-search-forward
10491                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10492           (delete-region (match-beginning 1) (match-end 1))
10493           (insert (number-to-string length)))
10494         (goto-char (point-min))
10495         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10496           (delete-region (match-beginning 1) (match-end 1))
10497           (insert (number-to-string lines))))))
10498   ;; Replace the article.
10499   (let ((buf (current-buffer))
10500         (article (cdr gnus-article-current))
10501         replace-result)
10502     (with-temp-buffer
10503       (insert-buffer-substring buf)
10504       (if (and (not read-only)
10505                (not (setq replace-result
10506                           (gnus-request-replace-article
10507                            article (car gnus-article-current)
10508                            (current-buffer) t))))
10509           (error "Couldn't replace article")
10510         ;; If we got a number back, then that's the new article number
10511         ;; for this article.  Otherwise, the article number didn't change.
10512         (when (numberp replace-result)
10513           (with-current-buffer gnus-summary-buffer
10514             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10515             (gnus-summary-limit gnus-newsgroup-limit)
10516             (setq article replace-result)
10517             (gnus-summary-goto-subject article t)))
10518         ;; Update the summary buffer.
10519         (if (and references
10520                  (equal (message-tokenize-header references " ")
10521                         (message-tokenize-header
10522                          (or (message-fetch-field "references") "") " ")))
10523             ;; We only have to update this line.
10524             (save-excursion
10525               (save-restriction
10526                 (message-narrow-to-head)
10527                 (let ((head (buffer-substring-no-properties
10528                              (point-min) (point-max)))
10529                       header)
10530                   (with-temp-buffer
10531                     (insert (format "211 %d Article retrieved.\n" article))
10532                     (insert head)
10533                     (insert ".\n")
10534                     (let ((nntp-server-buffer (current-buffer)))
10535                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10536                     (with-current-buffer gnus-summary-buffer
10537                       (gnus-data-set-header (gnus-data-find article) header)
10538                       (gnus-summary-update-article-line article header)
10539                       (if (gnus-summary-goto-subject article nil t)
10540                           (gnus-summary-update-secondary-mark article)))))))
10541           ;; Update threads.
10542           (set-buffer (or buffer gnus-summary-buffer))
10543           (gnus-summary-update-article article)
10544           (if (gnus-summary-goto-subject article nil t)
10545               (gnus-summary-update-secondary-mark article)))
10546         ;; Prettify the article buffer again.
10547         (unless no-highlight
10548           (with-current-buffer gnus-article-buffer
10549             ;;!!! Fix this -- article should be rehighlighted.
10550             ;;(gnus-run-hooks 'gnus-article-display-hook)
10551             (set-buffer gnus-original-article-buffer)
10552             (gnus-request-article
10553              article (car gnus-article-current) (current-buffer))))
10554         ;; Prettify the summary buffer line.
10555         (when (gnus-visual-p 'summary-highlight 'highlight)
10556           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10557
10558 (defun gnus-summary-edit-wash (key)
10559   "Perform editing command KEY in the article buffer."
10560   (interactive
10561    (list
10562     (progn
10563       (message "%s" (concat (this-command-keys) "- "))
10564       (read-char))))
10565   (message "")
10566   (gnus-summary-edit-article)
10567   (execute-kbd-macro (concat (this-command-keys) key))
10568   (gnus-article-edit-done))
10569
10570 ;;; Respooling
10571
10572 (defun gnus-summary-respool-query (&optional silent trace)
10573   "Query where the respool algorithm would put this article."
10574   (interactive)
10575   (let (gnus-mark-article-hook)
10576     (gnus-summary-select-article)
10577     (with-current-buffer gnus-original-article-buffer
10578       (let ((groups (nnmail-article-group 'identity trace)))
10579         (unless silent
10580           (if groups
10581               (message "This message would go to %s"
10582                        (mapconcat 'car groups ", "))
10583             (message "This message would go to no groups"))
10584           groups)))))
10585
10586 (defun gnus-summary-respool-trace ()
10587   "Trace where the respool algorithm would put this article.
10588 Display a buffer showing all fancy splitting patterns which matched."
10589   (interactive)
10590   (gnus-summary-respool-query nil t))
10591
10592 ;; Summary marking commands.
10593
10594 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10595   "Mark articles which has the same subject as read, and then select the next.
10596 If UNMARK is positive, remove any kind of mark.
10597 If UNMARK is negative, tick articles."
10598   (interactive "P")
10599   (when unmark
10600     (setq unmark (prefix-numeric-value unmark)))
10601   (let ((count
10602          (gnus-summary-mark-same-subject
10603           (gnus-summary-article-subject) unmark)))
10604     ;; Select next unread article.  If auto-select-same mode, should
10605     ;; select the first unread article.
10606     (gnus-summary-next-article t (and gnus-auto-select-same
10607                                       (gnus-summary-article-subject)))
10608     (gnus-message 7 "%d article%s marked as %s"
10609                   count (if (= count 1) " is" "s are")
10610                   (if unmark "unread" "read"))))
10611
10612 (defun gnus-summary-kill-same-subject (&optional unmark)
10613   "Mark articles which has the same subject as read.
10614 If UNMARK is positive, remove any kind of mark.
10615 If UNMARK is negative, tick articles."
10616   (interactive "P")
10617   (when unmark
10618     (setq unmark (prefix-numeric-value unmark)))
10619   (let ((count
10620          (gnus-summary-mark-same-subject
10621           (gnus-summary-article-subject) unmark)))
10622     ;; If marked as read, go to next unread subject.
10623     (when (null unmark)
10624       ;; Go to next unread subject.
10625       (gnus-summary-next-subject 1 t))
10626     (gnus-message 7 "%d articles are marked as %s"
10627                   count (if unmark "unread" "read"))))
10628
10629 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10630   "Mark articles with same SUBJECT as read, and return marked number.
10631 If optional argument UNMARK is positive, remove any kinds of marks.
10632 If optional argument UNMARK is negative, mark articles as unread instead."
10633   (let ((count 1))
10634     (save-excursion
10635       (cond
10636        ((null unmark)                   ; Mark as read.
10637         (while (and
10638                 (progn
10639                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10640                   (gnus-summary-show-thread) t)
10641                 (gnus-summary-find-subject subject))
10642           (setq count (1+ count))))
10643        ((> unmark 0)                    ; Tick.
10644         (while (and
10645                 (progn
10646                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10647                   (gnus-summary-show-thread) t)
10648                 (gnus-summary-find-subject subject))
10649           (setq count (1+ count))))
10650        (t                               ; Mark as unread.
10651         (while (and
10652                 (progn
10653                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10654                   (gnus-summary-show-thread) t)
10655                 (gnus-summary-find-subject subject))
10656           (setq count (1+ count)))))
10657       (gnus-set-mode-line 'summary)
10658       ;; Return the number of marked articles.
10659       count)))
10660
10661 (defun gnus-summary-mark-as-processable (n &optional unmark)
10662   "Set the process mark on the next N articles.
10663 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10664 the process mark instead.  The difference between N and the actual
10665 number of articles marked is returned."
10666   (interactive "P")
10667   (if (and (null n) (gnus-region-active-p))
10668       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10669     (setq n (prefix-numeric-value n))
10670     (let ((backward (< n 0))
10671           (n (abs n)))
10672       (while (and
10673               (> n 0)
10674               (if unmark
10675                   (gnus-summary-remove-process-mark
10676                    (gnus-summary-article-number))
10677                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10678               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10679         (setq n (1- n)))
10680       (when (/= 0 n)
10681         (gnus-message 7 "No more articles"))
10682       (gnus-summary-recenter)
10683       (gnus-summary-position-point)
10684       n)))
10685
10686 (defun gnus-summary-unmark-as-processable (n)
10687   "Remove the process mark from the next N articles.
10688 If N is negative, unmark backward instead.  The difference between N and
10689 the actual number of articles unmarked is returned."
10690   (interactive "P")
10691   (gnus-summary-mark-as-processable n t))
10692
10693 (defun gnus-summary-unmark-all-processable ()
10694   "Remove the process mark from all articles."
10695   (interactive)
10696   (save-excursion
10697     (while gnus-newsgroup-processable
10698       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10699   (gnus-summary-position-point))
10700
10701 (defun gnus-summary-add-mark (article type)
10702   "Mark ARTICLE with a mark of TYPE."
10703   (let ((vtype (car (assq type gnus-article-mark-lists)))
10704         var)
10705     (if (not vtype)
10706         (error "No such mark type: %s" type)
10707       (setq var (intern (format "gnus-newsgroup-%s" type)))
10708       (set var (cons article (symbol-value var)))
10709       (if (memq type '(processable cached replied forwarded recent saved))
10710           (gnus-summary-update-secondary-mark article)
10711         ;;; !!! This is bogus.  We should find out what primary
10712         ;;; !!! mark we want to set.
10713         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10714
10715 (defun gnus-summary-mark-as-expirable (n)
10716   "Mark N articles forward as expirable.
10717 If N is negative, mark backward instead.  The difference between N and
10718 the actual number of articles marked is returned."
10719   (interactive "p")
10720   (gnus-summary-mark-forward n gnus-expirable-mark))
10721
10722 (defun gnus-summary-mark-as-spam (n)
10723   "Mark N articles forward as spam.
10724 If N is negative, mark backward instead.  The difference between N and
10725 the actual number of articles marked is returned."
10726   (interactive "p")
10727   (gnus-summary-mark-forward n gnus-spam-mark))
10728
10729 (defun gnus-summary-mark-article-as-replied (article)
10730   "Mark ARTICLE as replied to and update the summary line.
10731 ARTICLE can also be a list of articles."
10732   (interactive (list (gnus-summary-article-number)))
10733   (let ((articles (if (listp article) article (list article))))
10734     (dolist (article articles)
10735       (unless (numberp article)
10736         (error "%s is not a number" article))
10737       (push article gnus-newsgroup-replied)
10738       (let ((inhibit-read-only t))
10739         (when (gnus-summary-goto-subject article nil t)
10740           (gnus-summary-update-secondary-mark article))))))
10741
10742 (defun gnus-summary-mark-article-as-forwarded (article)
10743   "Mark ARTICLE as forwarded and update the summary line.
10744 ARTICLE can also be a list of articles."
10745   (let ((articles (if (listp article) article (list article))))
10746     (dolist (article articles)
10747       (push article gnus-newsgroup-forwarded)
10748       (let ((inhibit-read-only t))
10749         (when (gnus-summary-goto-subject article nil t)
10750           (gnus-summary-update-secondary-mark article))))))
10751
10752 (defun gnus-summary-set-bookmark (article)
10753   "Set a bookmark in current article."
10754   (interactive (list (gnus-summary-article-number)))
10755   (when (or (not (get-buffer gnus-article-buffer))
10756             (not gnus-current-article)
10757             (not gnus-article-current)
10758             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10759     (error "No current article selected"))
10760   ;; Remove old bookmark, if one exists.
10761   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10762   ;; Set the new bookmark, which is on the form
10763   ;; (article-number . line-number-in-body).
10764   (push
10765    (cons article
10766          (with-current-buffer gnus-article-buffer
10767            (count-lines
10768             (min (point)
10769                  (save-excursion
10770                    (article-goto-body)
10771                    (point)))
10772             (point))))
10773    gnus-newsgroup-bookmarks)
10774   (gnus-message 6 "A bookmark has been added to the current article."))
10775
10776 (defun gnus-summary-remove-bookmark (article)
10777   "Remove the bookmark from the current article."
10778   (interactive (list (gnus-summary-article-number)))
10779   ;; Remove old bookmark, if one exists.
10780   (if (not (assq article gnus-newsgroup-bookmarks))
10781       (gnus-message 6 "No bookmark in current article.")
10782     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10783     (gnus-message 6 "Removed bookmark.")))
10784
10785 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10786 (defun gnus-summary-mark-as-dormant (n)
10787   "Mark N articles forward as dormant.
10788 If N is negative, mark backward instead.  The difference between N and
10789 the actual number of articles marked is returned."
10790   (interactive "p")
10791   (gnus-summary-mark-forward n gnus-dormant-mark))
10792
10793 (defun gnus-summary-set-process-mark (article)
10794   "Set the process mark on ARTICLE and update the summary line."
10795   (setq gnus-newsgroup-processable
10796         (cons article
10797               (delq article gnus-newsgroup-processable)))
10798   (when (gnus-summary-goto-subject article)
10799     (gnus-summary-show-thread)
10800     (gnus-summary-goto-subject article)
10801     (gnus-summary-update-secondary-mark article)))
10802
10803 (defun gnus-summary-remove-process-mark (&rest articles)
10804   "Remove the process mark from ARTICLES and update the summary line."
10805   (dolist (article articles)
10806     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10807     (when (gnus-summary-goto-subject article)
10808       (gnus-summary-show-thread)
10809       (gnus-summary-goto-subject article)
10810       (gnus-summary-update-secondary-mark article)))
10811   t)
10812
10813 (defun gnus-summary-set-saved-mark (article)
10814   "Set the process mark on ARTICLE and update the summary line."
10815   (push article gnus-newsgroup-saved)
10816   (when (gnus-summary-goto-subject article)
10817     (gnus-summary-update-secondary-mark article)))
10818
10819 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10820   "Mark N articles as read forwards.
10821 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10822 The difference between N and the actual number of articles marked is
10823 returned.
10824 If NO-EXPIRE, auto-expiry will be inhibited."
10825   (interactive "p")
10826   (gnus-summary-show-thread)
10827   (let ((backward (< n 0))
10828         (gnus-summary-goto-unread
10829          (and gnus-summary-goto-unread
10830               (not (eq gnus-summary-goto-unread 'never))
10831               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10832                                     gnus-ticked-mark gnus-dormant-mark)))))
10833         (n (abs n))
10834         (mark (or mark gnus-del-mark)))
10835     (while (and (> n 0)
10836                 (gnus-summary-mark-article nil mark no-expire)
10837                 (zerop (gnus-summary-next-subject
10838                         (if backward -1 1)
10839                         (and gnus-summary-goto-unread
10840                              (not (eq gnus-summary-goto-unread 'never)))
10841                         t)))
10842       (setq n (1- n)))
10843     (when (/= 0 n)
10844       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10845     (gnus-summary-recenter)
10846     (gnus-summary-position-point)
10847     (gnus-set-mode-line 'summary)
10848     n))
10849
10850 (defun gnus-summary-mark-article-as-read (mark)
10851   "Mark the current article quickly as read with MARK."
10852   (let ((article (gnus-summary-article-number)))
10853     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10854     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10855     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10856     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10857     (push (cons article mark) gnus-newsgroup-reads)
10858     ;; Possibly remove from cache, if that is used.
10859     (when gnus-use-cache
10860       (gnus-cache-enter-remove-article article))
10861     ;; Allow the backend to change the mark.
10862     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10863     ;; Check for auto-expiry.
10864     (when (and gnus-newsgroup-auto-expire
10865                (memq mark gnus-auto-expirable-marks))
10866       (setq mark gnus-expirable-mark)
10867       ;; Let the backend know about the mark change.
10868       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10869       (push article gnus-newsgroup-expirable))
10870     ;; Set the mark in the buffer.
10871     (gnus-summary-update-mark mark 'unread)
10872     t))
10873
10874 (defun gnus-summary-mark-article-as-unread (mark)
10875   "Mark the current article quickly as unread with MARK."
10876   (let* ((article (gnus-summary-article-number))
10877          (old-mark (gnus-summary-article-mark article)))
10878     ;; Allow the backend to change the mark.
10879     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10880     (if (eq mark old-mark)
10881         t
10882       (if (<= article 0)
10883           (progn
10884             (gnus-error 1 "Can't mark negative article numbers")
10885             nil)
10886         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10887         (setq gnus-newsgroup-spam-marked
10888               (delq article gnus-newsgroup-spam-marked))
10889         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10890         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10891         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10892         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10893         (cond ((= mark gnus-ticked-mark)
10894                (setq gnus-newsgroup-marked
10895                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10896                                               article)))
10897               ((= mark gnus-spam-mark)
10898                (setq gnus-newsgroup-spam-marked
10899                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10900                                               article)))
10901               ((= mark gnus-dormant-mark)
10902                (setq gnus-newsgroup-dormant
10903                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10904                                               article)))
10905               (t
10906                (setq gnus-newsgroup-unreads
10907                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10908                                               article))))
10909         (gnus-alist-pull article gnus-newsgroup-reads)
10910
10911         ;; See whether the article is to be put in the cache.
10912         (and gnus-use-cache
10913              (vectorp (gnus-summary-article-header article))
10914              (save-excursion
10915                (gnus-cache-possibly-enter-article
10916                 gnus-newsgroup-name article
10917                 (= mark gnus-ticked-mark)
10918                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10919
10920         ;; Fix the mark.
10921         (gnus-summary-update-mark mark 'unread)
10922         t))))
10923
10924 (defun gnus-summary-mark-article (&optional article mark no-expire)
10925   "Mark ARTICLE with MARK.  MARK can be any character.
10926 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10927 `??' (dormant) and `?E' (expirable).
10928 If MARK is nil, then the default character `?r' is used.
10929 If ARTICLE is nil, then the article on the current line will be
10930 marked.
10931 If NO-EXPIRE, auto-expiry will be inhibited."
10932   ;; The mark might be a string.
10933   (when (stringp mark)
10934     (setq mark (aref mark 0)))
10935   ;; If no mark is given, then we check auto-expiring.
10936   (when (null mark)
10937     (setq mark gnus-del-mark))
10938   (when (and (not no-expire)
10939              gnus-newsgroup-auto-expire
10940              (memq mark gnus-auto-expirable-marks))
10941     (setq mark gnus-expirable-mark))
10942   (let ((article (or article (gnus-summary-article-number)))
10943         (old-mark (gnus-summary-article-mark article)))
10944     ;; Allow the backend to change the mark.
10945     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10946     (if (eq mark old-mark)
10947         t
10948       (unless article
10949         (error "No article on current line"))
10950       (if (not (if (or (= mark gnus-unread-mark)
10951                        (= mark gnus-ticked-mark)
10952                        (= mark gnus-spam-mark)
10953                        (= mark gnus-dormant-mark))
10954                    (gnus-mark-article-as-unread article mark)
10955                  (gnus-mark-article-as-read article mark)))
10956           t
10957         ;; See whether the article is to be put in the cache.
10958         (and gnus-use-cache
10959              (not (= mark gnus-canceled-mark))
10960              (vectorp (gnus-summary-article-header article))
10961              (save-excursion
10962                (gnus-cache-possibly-enter-article
10963                 gnus-newsgroup-name article
10964                 (= mark gnus-ticked-mark)
10965                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10966
10967         (when (gnus-summary-goto-subject article nil t)
10968           (let ((inhibit-read-only t))
10969             (gnus-summary-show-thread)
10970             ;; Fix the mark.
10971             (gnus-summary-update-mark mark 'unread)
10972             t))))))
10973
10974 (defun gnus-summary-update-secondary-mark (article)
10975   "Update the secondary (read, process, cache) mark."
10976   (gnus-summary-update-mark
10977    (cond ((memq article gnus-newsgroup-processable)
10978           gnus-process-mark)
10979          ((memq article gnus-newsgroup-cached)
10980           gnus-cached-mark)
10981          ((memq article gnus-newsgroup-replied)
10982           gnus-replied-mark)
10983          ((memq article gnus-newsgroup-forwarded)
10984           gnus-forwarded-mark)
10985          ((memq article gnus-newsgroup-saved)
10986           gnus-saved-mark)
10987          ((memq article gnus-newsgroup-unseen)
10988           gnus-unseen-mark)
10989          (t gnus-no-mark))
10990    'replied)
10991   (when (gnus-visual-p 'summary-highlight 'highlight)
10992     (gnus-summary-highlight-line)
10993     (gnus-run-hooks 'gnus-summary-update-hook))
10994   t)
10995
10996 (defun gnus-summary-update-download-mark (article)
10997   "Update the download mark."
10998   (gnus-summary-update-mark
10999    (cond ((memq article gnus-newsgroup-undownloaded)
11000           gnus-undownloaded-mark)
11001          (gnus-newsgroup-agentized
11002           gnus-downloaded-mark)
11003          (t
11004           gnus-no-mark))
11005    'download)
11006   (gnus-summary-update-line t)
11007   t)
11008
11009 (defun gnus-summary-update-mark (mark type)
11010   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11011         (inhibit-read-only t))
11012     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11013     (when forward
11014       (when (looking-at "\r")
11015         (incf forward))
11016       (when (<= (+ forward (point)) (point-max))
11017         ;; Go to the right position on the line.
11018         (goto-char (+ forward (point)))
11019         ;; Replace the old mark with the new mark.
11020         (let ((to-insert
11021                (mm-subst-char-in-string
11022                 (char-after) mark
11023                 (buffer-substring (point) (1+ (point))))))
11024           (delete-region (point) (1+ (point)))
11025           (insert to-insert))
11026         ;; Optionally update the marks by some user rule.
11027         (when (eq type 'unread)
11028           (gnus-data-set-mark
11029            (gnus-data-find (gnus-summary-article-number)) mark)
11030           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11031
11032 (defun gnus-mark-article-as-read (article &optional mark)
11033   "Enter ARTICLE in the pertinent lists and remove it from others."
11034   ;; Make the article expirable.
11035   (let ((mark (or mark gnus-del-mark)))
11036     (setq gnus-newsgroup-expirable
11037           (if (= mark gnus-expirable-mark)
11038               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11039             (delq article gnus-newsgroup-expirable)))
11040     ;; Remove from unread and marked lists.
11041     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11042     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11043     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11044     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11045     (push (cons article mark) gnus-newsgroup-reads)
11046     ;; Possibly remove from cache, if that is used.
11047     (when gnus-use-cache
11048       (gnus-cache-enter-remove-article article))
11049     t))
11050
11051 (defun gnus-mark-article-as-unread (article &optional mark)
11052   "Enter ARTICLE in the pertinent lists and remove it from others."
11053   (let ((mark (or mark gnus-ticked-mark)))
11054     (if (<= article 0)
11055         (progn
11056           (gnus-error 1 "Can't mark negative article numbers")
11057           nil)
11058       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11059             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11060             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11061             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11062             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11063
11064       ;; Unsuppress duplicates?
11065       (when gnus-suppress-duplicates
11066         (gnus-dup-unsuppress-article article))
11067
11068       (cond ((= mark gnus-ticked-mark)
11069              (setq gnus-newsgroup-marked
11070                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11071             ((= mark gnus-spam-mark)
11072              (setq gnus-newsgroup-spam-marked
11073                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11074                                             article)))
11075             ((= mark gnus-dormant-mark)
11076              (setq gnus-newsgroup-dormant
11077                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11078             (t
11079              (setq gnus-newsgroup-unreads
11080                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11081       (gnus-alist-pull article gnus-newsgroup-reads)
11082       t)))
11083
11084 (defun gnus-summary-tick-article-forward (n)
11085   "Tick N articles forwards.
11086 If N is negative, tick backwards instead.
11087 The difference between N and the number of articles ticked is returned."
11088   (interactive "p")
11089   (gnus-summary-mark-forward n gnus-ticked-mark))
11090
11091 (defun gnus-summary-tick-article-backward (n)
11092   "Tick N articles backwards.
11093 The difference between N and the number of articles ticked is returned."
11094   (interactive "p")
11095   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11096
11097 (defun gnus-summary-tick-article (&optional article clear-mark)
11098   "Mark current article as unread.
11099 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11100 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11101   (interactive)
11102   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11103                                        gnus-ticked-mark)))
11104
11105 (defun gnus-summary-mark-as-read-forward (n)
11106   "Mark N articles as read forwards.
11107 If N is negative, mark backwards instead.
11108 The difference between N and the actual number of articles marked is
11109 returned."
11110   (interactive "p")
11111   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11112
11113 (defun gnus-summary-mark-as-read-backward (n)
11114   "Mark the N articles as read backwards.
11115 The difference between N and the actual number of articles marked is
11116 returned."
11117   (interactive "p")
11118   (gnus-summary-mark-forward
11119    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11120
11121 (defun gnus-summary-mark-as-read (&optional article mark)
11122   "Mark current article as read.
11123 ARTICLE specifies the article to be marked as read.
11124 MARK specifies a string to be inserted at the beginning of the line."
11125   (gnus-summary-mark-article article mark))
11126
11127 (defun gnus-summary-clear-mark-forward (n)
11128   "Clear marks from N articles forward.
11129 If N is negative, clear backward instead.
11130 The difference between N and the number of marks cleared is returned."
11131   (interactive "p")
11132   (gnus-summary-mark-forward n gnus-unread-mark))
11133
11134 (defun gnus-summary-clear-mark-backward (n)
11135   "Clear marks from N articles backward.
11136 The difference between N and the number of marks cleared is returned."
11137   (interactive "p")
11138   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11139
11140 (defun gnus-summary-mark-unread-as-read ()
11141   "Intended to be used by `gnus-mark-article-hook'."
11142   (when (memq gnus-current-article gnus-newsgroup-unreads)
11143     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11144
11145 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11146   "Intended to be used by `gnus-mark-article-hook'."
11147   (let ((mark (gnus-summary-article-mark)))
11148     (when (or (gnus-unread-mark-p mark)
11149               (gnus-read-mark-p mark))
11150       (gnus-summary-mark-article gnus-current-article
11151                                  (or new-mark gnus-read-mark)))))
11152
11153 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11154   "Intended to be used by `gnus-mark-article-hook'."
11155   (let ((mark (gnus-summary-article-mark)))
11156     (when (or (gnus-unread-mark-p mark)
11157               (gnus-read-mark-p mark))
11158       (gnus-summary-mark-article (gnus-summary-article-number)
11159                                  (or new-mark gnus-read-mark)))))
11160
11161 (defun gnus-summary-mark-unread-as-ticked ()
11162   "Intended to be used by `gnus-mark-article-hook'."
11163   (when (memq gnus-current-article gnus-newsgroup-unreads)
11164     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11165
11166 (defun gnus-summary-mark-region-as-read (point mark all)
11167   "Mark all unread articles between point and mark as read.
11168 If given a prefix, mark all articles between point and mark as read,
11169 even ticked and dormant ones."
11170   (interactive "r\nP")
11171   (save-excursion
11172     (let (article)
11173       (goto-char point)
11174       (beginning-of-line)
11175       (while (and
11176               (< (point) mark)
11177               (progn
11178                 (when (or all
11179                           (memq (setq article (gnus-summary-article-number))
11180                                 gnus-newsgroup-unreads))
11181                   (gnus-summary-mark-article article gnus-del-mark))
11182                 t)
11183               (gnus-summary-find-next))))))
11184
11185 (defun gnus-summary-mark-below (score mark)
11186   "Mark articles with score less than SCORE with MARK."
11187   (interactive "P\ncMark: ")
11188   (setq score (if score
11189                   (prefix-numeric-value score)
11190                 (or gnus-summary-default-score 0)))
11191   (with-current-buffer gnus-summary-buffer
11192     (goto-char (point-min))
11193     (while
11194         (progn
11195           (and (< (gnus-summary-article-score) score)
11196                (gnus-summary-mark-article nil mark))
11197           (gnus-summary-find-next)))))
11198
11199 (defun gnus-summary-kill-below (&optional score)
11200   "Mark articles with score below SCORE as read."
11201   (interactive "P")
11202   (gnus-summary-mark-below score gnus-killed-mark))
11203
11204 (defun gnus-summary-clear-above (&optional score)
11205   "Clear all marks from articles with score above SCORE."
11206   (interactive "P")
11207   (gnus-summary-mark-above score gnus-unread-mark))
11208
11209 (defun gnus-summary-tick-above (&optional score)
11210   "Tick all articles with score above SCORE."
11211   (interactive "P")
11212   (gnus-summary-mark-above score gnus-ticked-mark))
11213
11214 (defun gnus-summary-mark-above (score mark)
11215   "Mark articles with score over SCORE with MARK."
11216   (interactive "P\ncMark: ")
11217   (setq score (if score
11218                   (prefix-numeric-value score)
11219                 (or gnus-summary-default-score 0)))
11220   (with-current-buffer gnus-summary-buffer
11221     (goto-char (point-min))
11222     (while (and (progn
11223                   (when (> (gnus-summary-article-score) score)
11224                     (gnus-summary-mark-article nil mark))
11225                   t)
11226                 (gnus-summary-find-next)))))
11227
11228 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11229 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11230 (defun gnus-summary-limit-include-expunged (&optional no-error)
11231   "Display all the hidden articles that were expunged for low scores."
11232   (interactive)
11233   (let ((inhibit-read-only t))
11234     (let ((scored gnus-newsgroup-scored)
11235           headers h)
11236       (while scored
11237         (unless (gnus-summary-article-header (caar scored))
11238           (and (setq h (gnus-number-to-header (caar scored)))
11239                (< (cdar scored) gnus-summary-expunge-below)
11240                (push h headers)))
11241         (setq scored (cdr scored)))
11242       (if (not headers)
11243           (when (not no-error)
11244             (error "No expunged articles hidden"))
11245         (goto-char (point-min))
11246         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11247         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11248         (dolist (x headers)
11249           (push (mail-header-number x) gnus-newsgroup-limit))
11250         (gnus-summary-prepare-unthreaded (nreverse headers))
11251         (goto-char (point-min))
11252         (gnus-summary-position-point)
11253         t))))
11254
11255 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11256   "Mark all unread articles in this newsgroup as read.
11257 If prefix argument ALL is non-nil, ticked and dormant articles will
11258 also be marked as read.
11259 If QUIETLY is non-nil, no questions will be asked.
11260
11261 If TO-HERE is non-nil, it should be a point in the buffer.  All
11262 articles before (after, if REVERSE is set) this point will be marked
11263 as read.
11264
11265 Note that this function will only catch up the unread article
11266 in the current summary buffer limitation.
11267
11268 The number of articles marked as read is returned."
11269   (interactive "P")
11270   (prog1
11271       (save-excursion
11272         (when (or quietly
11273                   (not gnus-interactive-catchup) ;Without confirmation?
11274                   gnus-expert-user
11275                   (gnus-y-or-n-p
11276                    (if all
11277                        "Mark absolutely all articles as read? "
11278                      "Mark all unread articles as read? ")))
11279           (if (and not-mark
11280                    (not gnus-newsgroup-adaptive)
11281                    (not gnus-newsgroup-auto-expire)
11282                    (not gnus-suppress-duplicates)
11283                    (or (not gnus-use-cache)
11284                        (eq gnus-use-cache 'passive)))
11285               (progn
11286                 (when all
11287                   (setq gnus-newsgroup-marked nil
11288                         gnus-newsgroup-spam-marked nil
11289                         gnus-newsgroup-dormant nil))
11290                 (setq gnus-newsgroup-unreads
11291                       (gnus-sorted-nunion
11292                        (gnus-sorted-intersection gnus-newsgroup-unreads
11293                                                  gnus-newsgroup-downloadable)
11294                        (gnus-sorted-difference gnus-newsgroup-unfetched
11295                                                gnus-newsgroup-cached))))
11296             ;; We actually mark all articles as canceled, which we
11297             ;; have to do when using auto-expiry or adaptive scoring.
11298             (gnus-summary-show-all-threads)
11299             (if (and to-here reverse)
11300                 (progn
11301                   (goto-char to-here)
11302                   (gnus-summary-mark-current-read-and-unread-as-read
11303                    gnus-catchup-mark)
11304                   (while (gnus-summary-find-next (not all))
11305                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11306               (when (gnus-summary-first-subject (not all))
11307                 (while (and
11308                         (if to-here (< (point) to-here) t)
11309                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11310                         (gnus-summary-find-next (not all))))))
11311             (gnus-set-mode-line 'summary))
11312           t))
11313     (gnus-summary-position-point)))
11314
11315 (defun gnus-summary-catchup-to-here (&optional all)
11316   "Mark all unticked articles before the current one as read.
11317 If ALL is non-nil, also mark ticked and dormant articles as read."
11318   (interactive "P")
11319   (save-excursion
11320     (gnus-save-hidden-threads
11321       (let ((beg (point)))
11322         ;; We check that there are unread articles.
11323         (when (or all (gnus-summary-find-prev))
11324           (gnus-summary-catchup all t beg)))))
11325   (gnus-summary-position-point))
11326
11327 (defun gnus-summary-catchup-from-here (&optional all)
11328   "Mark all unticked articles after (and including) the current one as read.
11329 If ALL is non-nil, also mark ticked and dormant articles as read."
11330   (interactive "P")
11331   (save-excursion
11332     (gnus-save-hidden-threads
11333       (let ((beg (point)))
11334         ;; We check that there are unread articles.
11335         (when (or all (gnus-summary-find-next))
11336           (gnus-summary-catchup all t beg nil t)))))
11337   (gnus-summary-position-point))
11338
11339 (defun gnus-summary-catchup-all (&optional quietly)
11340   "Mark all articles in this newsgroup as read.
11341 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11342 instead, which marks only unread articles as read."
11343   (interactive "P")
11344   (gnus-summary-catchup t quietly))
11345
11346 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11347   "Mark all unread articles in this group as read, then exit.
11348 If prefix argument ALL is non-nil, all articles are marked as read.
11349 If QUIETLY is non-nil, no questions will be asked."
11350   (interactive "P")
11351   (when (gnus-summary-catchup all quietly nil 'fast)
11352     ;; Select next newsgroup or exit.
11353     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11354              (eq gnus-auto-select-next 'quietly))
11355         (gnus-summary-next-group nil)
11356       (gnus-summary-exit))))
11357
11358 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11359   "Mark all articles in this newsgroup as read, and then exit.
11360 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11361 instead, which marks only unread articles as read."
11362   (interactive "P")
11363   (gnus-summary-catchup-and-exit t quietly))
11364
11365 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11366   "Mark all articles in this group as read and select the next group.
11367 If given a prefix, mark all articles, unread as well as ticked, as
11368 read."
11369   (interactive "P")
11370   (save-excursion
11371     (gnus-summary-catchup all))
11372   (gnus-summary-next-group))
11373
11374 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11375   "Mark all articles in this group as read and select the previous group.
11376 If given a prefix, mark all articles, unread as well as ticked, as
11377 read."
11378   (interactive "P")
11379   (save-excursion
11380     (gnus-summary-catchup all))
11381   (gnus-summary-next-group nil nil t))
11382
11383 ;;;
11384 ;;; with article
11385 ;;;
11386
11387 (defmacro gnus-with-article (article &rest forms)
11388   "Select ARTICLE and perform FORMS in the original article buffer.
11389 Then replace the article with the result."
11390   `(progn
11391      ;; We don't want the article to be marked as read.
11392      (let (gnus-mark-article-hook)
11393        (gnus-summary-select-article t t nil ,article))
11394      (set-buffer gnus-original-article-buffer)
11395      ,@forms
11396      (if (not (gnus-check-backend-function
11397                'request-replace-article (car gnus-article-current)))
11398          (gnus-message 5 "Read-only group; not replacing")
11399        (unless (gnus-request-replace-article
11400                 ,article (car gnus-article-current)
11401                 (current-buffer) t)
11402          (error "Couldn't replace article")))
11403      ;; The cache and backlog have to be flushed somewhat.
11404      (when gnus-keep-backlog
11405        (gnus-backlog-remove-article
11406         (car gnus-article-current) (cdr gnus-article-current)))
11407      (when gnus-use-cache
11408        (gnus-cache-update-article
11409         (car gnus-article-current) (cdr gnus-article-current)))))
11410
11411 (put 'gnus-with-article 'lisp-indent-function 1)
11412 (put 'gnus-with-article 'edebug-form-spec '(form body))
11413
11414 ;; Thread-based commands.
11415
11416 (defun gnus-summary-articles-in-thread (&optional article)
11417   "Return a list of all articles in the current thread.
11418 If ARTICLE is non-nil, return all articles in the thread that starts
11419 with that article."
11420   (let* ((article (or article (gnus-summary-article-number)))
11421          (data (gnus-data-find-list article))
11422          (top-level (gnus-data-level (car data)))
11423          (top-subject
11424           (cond ((null gnus-thread-operation-ignore-subject)
11425                  (gnus-simplify-subject-re
11426                   (mail-header-subject (gnus-data-header (car data)))))
11427                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11428                  (gnus-simplify-subject-fuzzy
11429                   (mail-header-subject (gnus-data-header (car data)))))
11430                 (t nil)))
11431          (end-point (save-excursion
11432                       (goto-char (gnus-data-pos (car data)))
11433                       (if (gnus-summary-go-to-next-thread)
11434                           (point) (point-max))))
11435          articles)
11436     (while (and data
11437                 (< (gnus-data-pos (car data)) end-point))
11438       (when (or (not top-subject)
11439                 (string= top-subject
11440                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11441                              (gnus-simplify-subject-fuzzy
11442                               (mail-header-subject
11443                                (gnus-data-header (car data))))
11444                            (gnus-simplify-subject-re
11445                             (mail-header-subject
11446                              (gnus-data-header (car data)))))))
11447         (push (gnus-data-number (car data)) articles))
11448       (unless (and (setq data (cdr data))
11449                    (> (gnus-data-level (car data)) top-level))
11450         (setq data nil)))
11451     ;; Return the list of articles.
11452     (nreverse articles)))
11453
11454 (defun gnus-summary-rethread-current ()
11455   "Rethread the thread the current article is part of."
11456   (interactive)
11457   (let* ((gnus-show-threads t)
11458          (article (gnus-summary-article-number))
11459          (id (mail-header-id (gnus-summary-article-header)))
11460          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11461     (unless id
11462       (error "No article on the current line"))
11463     (gnus-rebuild-thread id)
11464     (gnus-summary-goto-subject article)))
11465
11466 (defun gnus-summary-reparent-thread ()
11467   "Make the current article child of the marked (or previous) article.
11468
11469 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11470 is non-nil or the Subject: of both articles are the same."
11471   (interactive)
11472   (unless (not (gnus-group-read-only-p))
11473     (error "The current newsgroup does not support article editing"))
11474   (unless (<= (length gnus-newsgroup-processable) 1)
11475     (error "No more than one article may be marked"))
11476   (let ((child (gnus-summary-article-number))
11477         ;; First grab the marked article, otherwise one line up.
11478         (parent (if (not (null gnus-newsgroup-processable))
11479                     (car gnus-newsgroup-processable)
11480                   (save-excursion
11481                     (if (eq (forward-line -1) 0)
11482                         (gnus-summary-article-number)
11483                       (error "Beginning of summary buffer"))))))
11484     (gnus-summary-reparent-children parent (list child))))
11485
11486 (defun gnus-summary-reparent-children (parent children)
11487   "Make PARENT the parent of CHILDREN.
11488 When called interactively, PARENT is the current article and CHILDREN
11489 are the process-marked articles."
11490   (interactive
11491    (list (gnus-summary-article-number)
11492          (gnus-summary-work-articles nil)))
11493   (dolist (child children)
11494     (save-window-excursion
11495       (let ((gnus-article-buffer " *reparent*"))
11496         (unless (not (eq parent child))
11497           (error "An article may not be self-referential"))
11498         (let ((message-id (mail-header-id
11499                            (gnus-summary-article-header parent))))
11500           (unless (and message-id (not (equal message-id "")))
11501             (error "No message-id in desired parent"))
11502           (gnus-with-article child
11503             (save-restriction
11504               (goto-char (point-min))
11505               (message-narrow-to-head)
11506               (if (re-search-forward "^References: " nil t)
11507                   (progn
11508                     (re-search-forward "^[^ \t]" nil t)
11509                     (forward-line -1)
11510                     (end-of-line)
11511                     (insert " " message-id))
11512                 (insert "References: " message-id "\n"))))
11513           (set-buffer gnus-summary-buffer)
11514           (gnus-summary-unmark-all-processable)
11515           (gnus-summary-update-article child)
11516           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11517             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11518           (gnus-summary-rethread-current)
11519           (gnus-message 3 "Article %d is now the child of article %d"
11520                         child parent))))))
11521
11522 (defun gnus-summary-toggle-threads (&optional arg)
11523   "Toggle showing conversation threads.
11524 If ARG is positive number, turn showing conversation threads on."
11525   (interactive "P")
11526   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11527     (setq gnus-show-threads
11528           (if (null arg) (not gnus-show-threads)
11529             (> (prefix-numeric-value arg) 0)))
11530     (gnus-summary-prepare)
11531     (gnus-summary-goto-subject current)
11532     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11533     (gnus-summary-position-point)))
11534
11535 (eval-and-compile
11536   (if (fboundp 'remove-overlays)
11537       (defalias 'gnus-remove-overlays 'remove-overlays)
11538     (defun gnus-remove-overlays (beg end name val)
11539       "Clear BEG and END of overlays whose property NAME has value VAL.
11540 For compatibility with XEmacs."
11541       (dolist (ov (gnus-overlays-in beg end))
11542         (when (eq (gnus-overlay-get ov name) val)
11543           (gnus-delete-overlay ov))))))
11544
11545 (defun gnus-summary-show-all-threads ()
11546   "Show all threads."
11547   (interactive)
11548   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11549   (gnus-summary-position-point))
11550
11551 (defsubst gnus-summary--inv (p)
11552   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11553
11554 (defun gnus-summary-show-thread ()
11555   "Show thread subtrees.
11556 Returns nil if no thread was there to be shown."
11557   (interactive)
11558   (let* ((orig (point))
11559          (end (point-at-eol))
11560          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11561          ;; Leave point at bol
11562          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11563          (eoi (when end
11564                 (if (fboundp 'next-single-char-property-change)
11565                     (or (next-single-char-property-change end 'invisible)
11566                         (point-max))
11567                   (while (progn
11568                            (end-of-line 2)
11569                            (and (not (eobp))
11570                                 (eq (get-char-property (point) 'invisible)
11571                                     'gnus-sum))))
11572                   (point)))))
11573     (when eoi
11574       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11575       (goto-char orig)
11576       (gnus-summary-position-point)
11577       eoi)))
11578
11579 (defun gnus-summary-maybe-hide-threads ()
11580   "If requested, hide the threads that should be hidden."
11581   (when (and gnus-show-threads
11582              gnus-thread-hide-subtree)
11583     (gnus-summary-hide-all-threads
11584      (if (or (consp gnus-thread-hide-subtree)
11585              (functionp gnus-thread-hide-subtree))
11586          (gnus-make-predicate gnus-thread-hide-subtree)
11587        nil))))
11588
11589 ;;; Hiding predicates.
11590
11591 (defun gnus-article-unread-p (header)
11592   (memq (mail-header-number header) gnus-newsgroup-unreads))
11593
11594 (defun gnus-article-unseen-p (header)
11595   (memq (mail-header-number header) gnus-newsgroup-unseen))
11596
11597 (defun gnus-map-articles (predicate articles)
11598   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11599   (apply 'gnus-or (mapcar predicate
11600                           (mapcar (lambda (number)
11601                                     (gnus-summary-article-header number))
11602                                   articles))))
11603
11604 (defun gnus-summary-hide-all-threads (&optional predicate)
11605   "Hide all thread subtrees.
11606 If PREDICATE is supplied, threads that satisfy this predicate
11607 will not be hidden."
11608   (interactive)
11609   (save-excursion
11610     (goto-char (point-min))
11611     (let ((end nil)
11612           (count 0))
11613       (while (not end)
11614         (incf count)
11615         (when (zerop (mod count 1000))
11616           (message "Hiding all threads... %d" count))
11617         (when (or (not predicate)
11618                   (gnus-map-articles
11619                    predicate (gnus-summary-article-children)))
11620             (gnus-summary-hide-thread))
11621         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11622   (gnus-summary-position-point))
11623
11624 (defun gnus-summary-hide-thread ()
11625   "Hide thread subtrees.
11626 If PREDICATE is supplied, threads that satisfy this predicate
11627 will not be hidden.
11628 Returns nil if no threads were there to be hidden."
11629   (interactive)
11630   (let ((start (point))
11631         (starteol (line-end-position))
11632         (article (gnus-summary-article-number)))
11633     (goto-char start)
11634     ;; Go forward until either the buffer ends or the subthread ends.
11635     (when (and (not (eobp))
11636                (or (zerop (gnus-summary-next-thread 1 t))
11637                    (goto-char (point-max))))
11638       (if (and (> (point) start)
11639                ;; FIXME: this should actually search for a non-invisible \n.
11640                (search-backward "\n" start t))
11641           (progn
11642             (when (> (point) starteol)
11643               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11644               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11645                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11646                 (gnus-overlay-put ol 'evaporate t)))
11647             (gnus-summary-goto-subject article)
11648             (when (> start (point))
11649               (message "Hiding the thread moved us backwards, aborting!")
11650               (goto-char (point-max))))
11651         (goto-char start)
11652         nil))))
11653
11654 (defun gnus-summary-go-to-next-thread (&optional previous)
11655   "Go to the same level (or less) next thread.
11656 If PREVIOUS is non-nil, go to previous thread instead.
11657 Return the article number moved to, or nil if moving was impossible."
11658   (let ((level (gnus-summary-thread-level))
11659         (way (if previous -1 1))
11660         (beg (point)))
11661     (forward-line way)
11662     (while (and (not (eobp))
11663                 (< level (gnus-summary-thread-level)))
11664       (forward-line way))
11665     (if (eobp)
11666         (progn
11667           (goto-char beg)
11668           nil)
11669       (setq beg (point))
11670       (prog1
11671           (gnus-summary-article-number)
11672         (goto-char beg)))))
11673
11674 (defun gnus-summary-next-thread (n &optional silent)
11675   "Go to the same level next N'th thread.
11676 If N is negative, search backward instead.
11677 Returns the difference between N and the number of skips actually
11678 done.
11679
11680 If SILENT, don't output messages."
11681   (interactive "p")
11682   (let ((backward (< n 0))
11683         (n (abs n)))
11684     (while (and (> n 0)
11685                 (gnus-summary-go-to-next-thread backward))
11686       (decf n))
11687     (unless silent
11688       (gnus-summary-position-point))
11689     (when (and (not silent) (/= 0 n))
11690       (gnus-message 7 "No more threads"))
11691     n))
11692
11693 (defun gnus-summary-prev-thread (n)
11694   "Go to the same level previous N'th thread.
11695 Returns the difference between N and the number of skips actually
11696 done."
11697   (interactive "p")
11698   (gnus-summary-next-thread (- n)))
11699
11700 (defun gnus-summary-go-down-thread ()
11701   "Go down one level in the current thread."
11702   (let ((children (gnus-summary-article-children)))
11703     (when children
11704       (gnus-summary-goto-subject (car children)))))
11705
11706 (defun gnus-summary-go-up-thread ()
11707   "Go up one level in the current thread."
11708   (let ((parent (gnus-summary-article-parent)))
11709     (when parent
11710       (gnus-summary-goto-subject parent))))
11711
11712 (defun gnus-summary-down-thread (n)
11713   "Go down thread N steps.
11714 If N is negative, go up instead.
11715 Returns the difference between N and how many steps down that were
11716 taken."
11717   (interactive "p")
11718   (let ((up (< n 0))
11719         (n (abs n)))
11720     (while (and (> n 0)
11721                 (if up (gnus-summary-go-up-thread)
11722                   (gnus-summary-go-down-thread)))
11723       (setq n (1- n)))
11724     (gnus-summary-position-point)
11725     (when (/= 0 n)
11726       (gnus-message 7 "Can't go further"))
11727     n))
11728
11729 (defun gnus-summary-up-thread (n)
11730   "Go up thread N steps.
11731 If N is negative, go down instead.
11732 Returns the difference between N and how many steps down that were
11733 taken."
11734   (interactive "p")
11735   (gnus-summary-down-thread (- n)))
11736
11737 (defun gnus-summary-top-thread ()
11738   "Go to the top of the thread."
11739   (interactive)
11740   (while (gnus-summary-go-up-thread))
11741   (gnus-summary-article-number))
11742
11743 (defun gnus-summary-expire-thread ()
11744   "Mark articles under current thread as expired."
11745   (interactive)
11746   (gnus-summary-kill-thread 0))
11747
11748 (defun gnus-summary-kill-thread (&optional unmark)
11749   "Mark articles under current thread as read.
11750 If the prefix argument is positive, remove any kinds of marks.
11751 If the prefix argument is zero, mark thread as expired.
11752 If the prefix argument is negative, tick articles instead."
11753   (interactive "P")
11754   (when unmark
11755     (setq unmark (prefix-numeric-value unmark)))
11756   (let ((articles (gnus-summary-articles-in-thread))
11757         (hide (or (null unmark) (= unmark 0))))
11758     (save-excursion
11759       ;; Expand the thread.
11760       (gnus-summary-show-thread)
11761       ;; Mark all the articles.
11762       (while articles
11763         (gnus-summary-goto-subject (car articles))
11764         (cond ((null unmark)
11765                (gnus-summary-mark-article-as-read gnus-killed-mark))
11766               ((> unmark 0)
11767                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11768               ((= unmark 0)
11769                (gnus-summary-mark-article nil gnus-expirable-mark))
11770               (t
11771                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11772         (setq articles (cdr articles))))
11773     ;; Hide killed subtrees when hide is true.
11774     (and hide
11775          gnus-thread-hide-killed
11776          (gnus-summary-hide-thread))
11777     ;; If hide is t, go to next unread subject.
11778     (when hide
11779       ;; Go to next unread subject.
11780       (gnus-summary-next-subject 1 t)))
11781   (gnus-set-mode-line 'summary))
11782
11783 ;; Summary sorting commands
11784
11785 (defun gnus-summary-sort-by-number (&optional reverse)
11786   "Sort the summary buffer by article number.
11787 Argument REVERSE means reverse order."
11788   (interactive "P")
11789   (gnus-summary-sort 'number reverse))
11790
11791 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11792   "Sort the summary buffer by most recent article number.
11793 Argument REVERSE means reverse order."
11794   (interactive "P")
11795   (gnus-summary-sort 'most-recent-number reverse))
11796
11797 (defun gnus-summary-sort-by-random (&optional reverse)
11798   "Randomize the order in the summary buffer.
11799 Argument REVERSE means to randomize in reverse order."
11800   (interactive "P")
11801   (gnus-summary-sort 'random reverse))
11802
11803 (defun gnus-summary-sort-by-author (&optional reverse)
11804   "Sort the summary buffer by author name alphabetically.
11805 If `case-fold-search' is non-nil, case of letters is ignored.
11806 Argument REVERSE means reverse order."
11807   (interactive "P")
11808   (gnus-summary-sort 'author reverse))
11809
11810 (defun gnus-summary-sort-by-recipient (&optional reverse)
11811   "Sort the summary buffer by recipient name alphabetically.
11812 If `case-fold-search' is non-nil, case of letters is ignored.
11813 Argument REVERSE means reverse order."
11814   (interactive "P")
11815   (gnus-summary-sort 'recipient reverse))
11816
11817 (defun gnus-summary-sort-by-subject (&optional reverse)
11818   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11819 If `case-fold-search' is non-nil, case of letters is ignored.
11820 Argument REVERSE means reverse order."
11821   (interactive "P")
11822   (gnus-summary-sort 'subject reverse))
11823
11824 (defun gnus-summary-sort-by-date (&optional reverse)
11825   "Sort the summary buffer by date.
11826 Argument REVERSE means reverse order."
11827   (interactive "P")
11828   (gnus-summary-sort 'date reverse))
11829
11830 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11831   "Sort the summary buffer by most recent date.
11832 Argument REVERSE means reverse order."
11833   (interactive "P")
11834   (gnus-summary-sort 'most-recent-date reverse))
11835
11836 (defun gnus-summary-sort-by-score (&optional reverse)
11837   "Sort the summary buffer by score.
11838 Argument REVERSE means reverse order."
11839   (interactive "P")
11840   (gnus-summary-sort 'score reverse))
11841
11842 (defun gnus-summary-sort-by-lines (&optional reverse)
11843   "Sort the summary buffer by the number of lines.
11844 Argument REVERSE means reverse order."
11845   (interactive "P")
11846   (gnus-summary-sort 'lines reverse))
11847
11848 (defun gnus-summary-sort-by-chars (&optional reverse)
11849   "Sort the summary buffer by article length.
11850 Argument REVERSE means reverse order."
11851   (interactive "P")
11852   (gnus-summary-sort 'chars reverse))
11853
11854 (defun gnus-summary-sort-by-original (&optional reverse)
11855   "Sort the summary buffer using the default sorting method.
11856 Argument REVERSE means reverse order."
11857   (interactive "P")
11858   (let* ((inhibit-read-only t)
11859          (gnus-summary-prepare-hook nil))
11860     ;; We do the sorting by regenerating the threads.
11861     (gnus-summary-prepare)
11862     ;; Hide subthreads if needed.
11863     (gnus-summary-maybe-hide-threads)))
11864
11865 (defun gnus-summary-sort (predicate reverse)
11866   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11867   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11868          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11869          (gnus-thread-sort-functions
11870           (if (not reverse)
11871               thread
11872             `(lambda (t1 t2)
11873                (,thread t2 t1))))
11874          (gnus-sort-gathered-threads-function
11875           gnus-thread-sort-functions)
11876          (gnus-article-sort-functions
11877           (if (not reverse)
11878               article
11879             `(lambda (t1 t2)
11880                (,article t2 t1))))
11881          (inhibit-read-only t)
11882          (gnus-summary-prepare-hook nil))
11883     ;; We do the sorting by regenerating the threads.
11884     (gnus-summary-prepare)
11885     ;; Hide subthreads if needed.
11886     (gnus-summary-maybe-hide-threads)))
11887
11888 ;; Summary saving commands.
11889
11890 (defun gnus-summary-save-article (&optional n not-saved)
11891   "Save the current article using the default saver function.
11892 If N is a positive number, save the N next articles.
11893 If N is a negative number, save the N previous articles.
11894 If N is nil and any articles have been marked with the process mark,
11895 save those articles instead.
11896 The variable `gnus-default-article-saver' specifies the saver function.
11897
11898 If the optional second argument NOT-SAVED is non-nil, articles saved
11899 will not be marked as saved."
11900   (interactive "P")
11901   (require 'gnus-art)
11902   (let* ((articles (gnus-summary-work-articles n))
11903          (save-buffer (save-excursion
11904                         (nnheader-set-temp-buffer " *Gnus Save*")))
11905          (num (length articles))
11906          ;; Whether to save decoded articles or raw articles.
11907          (decode (when gnus-article-save-coding-system
11908                    (get gnus-default-article-saver :decode)))
11909          ;; When saving many articles in a single file, use the other
11910          ;; function to save articles other than the first one.
11911          (saver2 (get gnus-default-article-saver :function))
11912          (gnus-prompt-before-saving (if saver2
11913                                         t
11914                                       gnus-prompt-before-saving))
11915          (gnus-default-article-saver gnus-default-article-saver)
11916          header file)
11917     (dolist (article articles)
11918       (setq header (gnus-summary-article-header article))
11919       (if (not (vectorp header))
11920           ;; This is a pseudo-article.
11921           (if (assq 'name header)
11922               (gnus-copy-file (cdr (assq 'name header)))
11923             (gnus-message 1 "Article %d is unsavable" article))
11924         ;; This is a real article.
11925         (save-window-excursion
11926           (gnus-summary-select-article decode decode nil article)
11927           (gnus-summary-goto-subject article))
11928         (with-current-buffer save-buffer
11929           (erase-buffer)
11930           (insert-buffer-substring (if decode
11931                                        gnus-article-buffer
11932                                      gnus-original-article-buffer)))
11933         (setq file (gnus-article-save save-buffer file num))
11934         (gnus-summary-remove-process-mark article)
11935         (unless not-saved
11936           (gnus-summary-set-saved-mark article)))
11937       (when saver2
11938         (setq gnus-default-article-saver saver2
11939               saver2 nil)))
11940     (gnus-kill-buffer save-buffer)
11941     (gnus-summary-position-point)
11942     (gnus-set-mode-line 'summary)
11943     n))
11944
11945 (defun gnus-summary-pipe-output (&optional n sym)
11946   "Pipe the current article to a subprocess.
11947 If N is a positive number, pipe the N next articles.
11948 If N is a negative number, pipe the N previous articles.
11949 If N is nil and any articles have been marked with the process mark,
11950 pipe those articles instead.
11951 The default command to which articles are piped is specified by the
11952 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11953 will be prompted for the command.
11954
11955 The properties `:decode' and `:headers' that are put to the function
11956 symbol `gnus-summary-save-in-pipe' control whether this function
11957 decodes articles and what headers to keep (see the doc string for the
11958 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11959 is neither omitted nor the symbol `r', force including all headers
11960 regardless of the `:headers' property.  If it is the symbol `r',
11961 articles that are not decoded and include all headers will be piped
11962 no matter what the properties `:decode' and `:headers' are."
11963   (interactive (gnus-interactive "P\ny"))
11964   (require 'gnus-art)
11965   (let* ((articles (gnus-summary-work-articles n))
11966          (result-buffer "*Shell Command Output*")
11967          (all-headers (not (memq sym '(nil r))))
11968          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11969          (raw (eq sym 'r))
11970          (headers (get 'gnus-summary-save-in-pipe :headers))
11971          command result)
11972     (unless (numberp (car articles))
11973       (error "No article to pipe"))
11974     (setq command (gnus-read-shell-command
11975                    (concat "Shell command on "
11976                            (if (cdr articles)
11977                                (format "these %d articles" (length articles))
11978                              "this article")
11979                            ": ")
11980                    gnus-summary-pipe-output-default-command))
11981     (when (string-equal command "")
11982       (error "A command is required"))
11983     (when all-headers
11984       (put 'gnus-summary-save-in-pipe :headers nil))
11985     (unwind-protect
11986         (while articles
11987           (gnus-summary-goto-subject (pop articles))
11988           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11989           (when (and (get-buffer result-buffer)
11990                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11991             (setq result (concat result (with-current-buffer result-buffer
11992                                           (buffer-string))))))
11993       (put 'gnus-summary-save-in-pipe :headers headers))
11994     (unless (zerop (length result))
11995       (if (with-current-buffer (get-buffer-create result-buffer)
11996             (erase-buffer)
11997             (insert result)
11998             (prog1
11999                 (and (= (count-lines (point-min) (point)) 1)
12000                      (progn
12001                        (end-of-line 0)
12002                        (<= (current-column)
12003                            (window-width (minibuffer-window)))))
12004               (goto-char (point-min))))
12005           (message "%s" (substring result 0 -1))
12006         (message nil)
12007         (gnus-configure-windows 'pipe)))))
12008
12009 (defun gnus-summary-save-article-mail (&optional arg)
12010   "Append the current article to a Unix mail box file.
12011 If N is a positive number, save the N next articles.
12012 If N is a negative number, save the N previous articles.
12013 If N is nil and any articles have been marked with the process mark,
12014 save those articles instead."
12015   (interactive "P")
12016   (require 'gnus-art)
12017   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12018     (gnus-summary-save-article arg)))
12019
12020 (defun gnus-summary-save-article-rmail (&optional arg)
12021   "Append the current article to an rmail file.
12022 If N is a positive number, save the N next articles.
12023 If N is a negative number, save the N previous articles.
12024 If N is nil and any articles have been marked with the process mark,
12025 save those articles instead."
12026   (interactive "P")
12027   (require 'gnus-art)
12028   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12029     (gnus-summary-save-article arg)))
12030
12031 (defun gnus-summary-save-article-file (&optional arg)
12032   "Append the current article to a file.
12033 If N is a positive number, save the N next articles.
12034 If N is a negative number, save the N previous articles.
12035 If N is nil and any articles have been marked with the process mark,
12036 save those articles instead."
12037   (interactive "P")
12038   (require 'gnus-art)
12039   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12040     (gnus-summary-save-article arg)))
12041
12042 (defun gnus-summary-write-article-file (&optional arg)
12043   "Write the current article to a file, deleting the previous file.
12044 If N is a positive number, save the N next articles.
12045 If N is a negative number, save the N previous articles.
12046 If N is nil and any articles have been marked with the process mark,
12047 save those articles instead."
12048   (interactive "P")
12049   (require 'gnus-art)
12050   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12051     (gnus-summary-save-article arg)))
12052
12053 (defun gnus-summary-save-article-body-file (&optional arg)
12054   "Append the current article body to a file.
12055 If N is a positive number, save the N next articles.
12056 If N is a negative number, save the N previous articles.
12057 If N is nil and any articles have been marked with the process mark,
12058 save those articles instead."
12059   (interactive "P")
12060   (require 'gnus-art)
12061   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12062     (gnus-summary-save-article arg)))
12063
12064 (defun gnus-summary-write-article-body-file (&optional arg)
12065   "Write the current article body to a file, deleting the previous file.
12066 If N is a positive number, save the N next articles.
12067 If N is a negative number, save the N previous articles.
12068 If N is nil and any articles have been marked with the process mark,
12069 save those articles instead."
12070   (interactive "P")
12071   (require 'gnus-art)
12072   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12073     (gnus-summary-save-article arg)))
12074
12075 (defun gnus-summary-muttprint (&optional arg)
12076   "Print the current article using Muttprint.
12077 If N is a positive number, save the N next articles.
12078 If N is a negative number, save the N previous articles.
12079 If N is nil and any articles have been marked with the process mark,
12080 save those articles instead."
12081   (interactive "P")
12082   (require 'gnus-art)
12083   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12084     (gnus-summary-save-article arg t)))
12085
12086 (defun gnus-summary-pipe-message (program)
12087   "Pipe the current article through PROGRAM."
12088   (interactive "sProgram: ")
12089   (gnus-summary-select-article)
12090   (let ((mail-header-separator ""))
12091     (gnus-eval-in-buffer-window gnus-article-buffer
12092       (save-restriction
12093         (widen)
12094         (let ((start (window-start))
12095               (inhibit-read-only t))
12096           (message-pipe-buffer-body program)
12097           (set-window-start (get-buffer-window (current-buffer)) start))))))
12098
12099 (defun gnus-get-split-value (methods)
12100   "Return a value based on the split METHODS."
12101   (let (split-name method result match)
12102     (when methods
12103       (with-current-buffer gnus-original-article-buffer
12104         (save-restriction
12105           (nnheader-narrow-to-headers)
12106           (while (and methods (not split-name))
12107             (goto-char (point-min))
12108             (setq method (pop methods))
12109             (setq match (car method))
12110             (when (cond
12111                    ((stringp match)
12112                     ;; Regular expression.
12113                     (ignore-errors
12114                       (re-search-forward match nil t)))
12115                    ((functionp match)
12116                     ;; Function.
12117                     (save-restriction
12118                       (widen)
12119                       (setq result (funcall match gnus-newsgroup-name))))
12120                    ((consp match)
12121                     ;; Form.
12122                     (save-restriction
12123                       (widen)
12124                       (setq result (eval match)))))
12125               (setq split-name (cdr method))
12126               (cond ((stringp result)
12127                      (push (expand-file-name
12128                             result gnus-article-save-directory)
12129                            split-name))
12130                     ((consp result)
12131                      (setq split-name (append result split-name)))))))))
12132     (nreverse split-name)))
12133
12134 (defun gnus-valid-move-group-p (group)
12135   (and (symbolp group)
12136        (boundp group)
12137        (symbol-name group)
12138        (symbol-value group)
12139        (gnus-get-function (gnus-find-method-for-group
12140                            (symbol-name group)) 'request-accept-article t)))
12141
12142 (defun gnus-read-move-group-name (prompt default articles prefix)
12143   "Read a group name."
12144   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12145          (minibuffer-confirm-incomplete nil) ; XEmacs
12146          (prom
12147           (format "%s %s to"
12148                   prompt
12149                   (if (> (length articles) 1)
12150                       (format "these %d articles" (length articles))
12151                     "this article")))
12152          (to-newsgroup
12153           (cond
12154            ((null split-name)
12155             (gnus-group-completing-read
12156              prom
12157              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12158              nil prefix nil default))
12159            ((= 1 (length split-name))
12160             (gnus-group-completing-read
12161              prom
12162              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12163              nil prefix 'gnus-group-history (car split-name)))
12164            (t
12165             (gnus-completing-read
12166              prom (nreverse split-name) nil nil 'gnus-group-history))))
12167          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12168          encoded)
12169     (when to-newsgroup
12170       (if (or (string= to-newsgroup "")
12171               (string= to-newsgroup prefix))
12172           (setq to-newsgroup default))
12173       (unless to-newsgroup
12174         (error "No group name entered"))
12175       (setq encoded (mm-encode-coding-string
12176                      to-newsgroup
12177                      (gnus-group-name-charset to-method to-newsgroup)))
12178       (or (gnus-active encoded)
12179           (gnus-activate-group encoded nil nil to-method)
12180           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12181                                      to-newsgroup))
12182               (or (and (gnus-request-create-group encoded to-method)
12183                        (gnus-activate-group encoded nil nil to-method)
12184                        (gnus-subscribe-group encoded))
12185                   (error "Couldn't create group %s" to-newsgroup)))
12186           (error "No such group: %s" to-newsgroup))
12187       encoded)))
12188
12189 (defvar gnus-summary-save-parts-counter)
12190 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12191
12192 (defun gnus-summary-save-parts (type dir n &optional reverse)
12193   "Save parts matching TYPE to DIR.
12194 If REVERSE, save parts that do not match TYPE."
12195   (interactive
12196    (list (read-string "Save parts of type: "
12197                       (or (car gnus-summary-save-parts-type-history)
12198                           gnus-summary-save-parts-default-mime)
12199                       'gnus-summary-save-parts-type-history)
12200          (setq gnus-summary-save-parts-last-directory
12201                (read-directory-name "Save to directory: "
12202                                     gnus-summary-save-parts-last-directory
12203                                     nil t))
12204          current-prefix-arg))
12205   (gnus-summary-iterate n
12206     (let ((gnus-display-mime-function nil)
12207           gnus-article-prepare-hook
12208           gnus-article-decode-hook
12209           gnus-display-mime-function
12210           gnus-break-pages
12211           (gnus-inhibit-treatment t))
12212       (gnus-summary-select-article))
12213     (with-current-buffer gnus-article-buffer
12214       (let ((handles (or gnus-article-mime-handles
12215                          (mm-dissect-buffer nil gnus-article-loose-mime)
12216                          (and gnus-article-emulate-mime
12217                               (mm-uu-dissect))))
12218             (gnus-summary-save-parts-counter 1))
12219         (when handles
12220           (gnus-summary-save-parts-1 type dir handles reverse)
12221           (unless gnus-article-mime-handles ;; Don't destroy this case.
12222             (mm-destroy-parts handles)))))))
12223
12224 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12225   (if (stringp (car handle))
12226       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12227               (cdr handle))
12228     (when (if reverse
12229               (not (string-match type (mm-handle-media-type handle)))
12230             (string-match type (mm-handle-media-type handle)))
12231       (let ((file (expand-file-name
12232                    (gnus-map-function
12233                     mm-file-name-rewrite-functions
12234                     (file-name-nondirectory
12235                      (or
12236                       (mm-handle-filename handle)
12237                       (format "%s.%d.%d" gnus-newsgroup-name
12238                               (cdr gnus-article-current)
12239                               gnus-summary-save-parts-counter))))
12240                    dir)))
12241         (incf gnus-summary-save-parts-counter)
12242         (unless (file-exists-p file)
12243           (mm-save-part-to-file handle file))))))
12244
12245 ;; Summary extract commands
12246
12247 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12248   (let ((inhibit-read-only t)
12249         (article (gnus-summary-article-number))
12250         after-article b e)
12251     (unless (gnus-summary-goto-subject article)
12252       (error "No such article: %d" article))
12253     (gnus-summary-position-point)
12254     ;; If all commands are to be bunched up on one line, we collect
12255     ;; them here.
12256     (unless gnus-view-pseudos-separately
12257       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12258             files action)
12259         (while ps
12260           (setq action (cdr (assq 'action (car ps))))
12261           (setq files (list (cdr (assq 'name (car ps)))))
12262           (while (and ps (cdr ps)
12263                       (string= (or action "1")
12264                                (or (cdr (assq 'action (cadr ps))) "2")))
12265             (push (cdr (assq 'name (cadr ps))) files)
12266             (setcdr ps (cddr ps)))
12267           (when files
12268             (when (not (string-match "%s" action))
12269               (push " " files))
12270             (push " " files)
12271             (when (assq 'execute (car ps))
12272               (setcdr (assq 'execute (car ps))
12273                       (funcall (if (string-match "%s" action)
12274                                    'format 'concat)
12275                                action
12276                                (mapconcat
12277                                 (lambda (f)
12278                                   (if (equal f " ")
12279                                       f
12280                                     (shell-quote-argument f)))
12281                                 files " ")))))
12282           (setq ps (cdr ps)))))
12283     (if (and gnus-view-pseudos (not not-view))
12284         (while pslist
12285           (when (assq 'execute (car pslist))
12286             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12287                                   (eq gnus-view-pseudos 'not-confirm)))
12288           (setq pslist (cdr pslist)))
12289       (save-excursion
12290         (while pslist
12291           (setq after-article (or (cdr (assq 'article (car pslist)))
12292                                   (gnus-summary-article-number)))
12293           (gnus-summary-goto-subject after-article)
12294           (forward-line 1)
12295           (setq b (point))
12296           (insert "    " (file-name-nondirectory
12297                           (cdr (assq 'name (car pslist))))
12298                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12299           (setq e (point))
12300           (forward-line -1)             ; back to `b'
12301           (gnus-add-text-properties
12302            b (1- e) (list 'gnus-number gnus-reffed-article-number
12303                           gnus-mouse-face-prop gnus-mouse-face))
12304           (gnus-data-enter
12305            after-article gnus-reffed-article-number
12306            gnus-unread-mark b (car pslist) 0 (- e b))
12307           (setq gnus-newsgroup-unreads
12308                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12309                                          gnus-reffed-article-number))
12310           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12311           (setq pslist (cdr pslist)))))))
12312
12313 (defun gnus-pseudos< (p1 p2)
12314   (let ((c1 (cdr (assq 'action p1)))
12315         (c2 (cdr (assq 'action p2))))
12316     (and c1 c2 (string< c1 c2))))
12317
12318 (defun gnus-request-pseudo-article (props)
12319   (cond ((assq 'execute props)
12320          (gnus-execute-command (cdr (assq 'execute props)))))
12321   (let ((gnus-current-article (gnus-summary-article-number)))
12322     (gnus-run-hooks 'gnus-mark-article-hook)))
12323
12324 (defun gnus-execute-command (command &optional automatic)
12325   (save-excursion
12326     (gnus-article-setup-buffer)
12327     (set-buffer gnus-article-buffer)
12328     (setq buffer-read-only nil)
12329     (let ((command (if automatic command
12330                      (read-string "Command: " (cons command 0)))))
12331       (erase-buffer)
12332       (insert "$ " command "\n\n")
12333       (if gnus-view-pseudo-asynchronously
12334           (start-process "gnus-execute" (current-buffer) shell-file-name
12335                          shell-command-switch command)
12336         (call-process shell-file-name nil t nil
12337                       shell-command-switch command)))))
12338
12339 ;; Summary kill commands.
12340
12341 (defun gnus-summary-edit-global-kill (article)
12342   "Edit the \"global\" kill file."
12343   (interactive (list (gnus-summary-article-number)))
12344   (gnus-group-edit-global-kill article))
12345
12346 (defun gnus-summary-edit-local-kill ()
12347   "Edit a local kill file applied to the current newsgroup."
12348   (interactive)
12349   (setq gnus-current-headers (gnus-summary-article-header))
12350   (gnus-group-edit-local-kill
12351    (gnus-summary-article-number) gnus-newsgroup-name))
12352
12353 ;;; Header reading.
12354
12355 (defun gnus-read-header (id &optional header)
12356   "Read the headers of article ID and enter them into the Gnus system."
12357   (let ((group gnus-newsgroup-name)
12358         (gnus-override-method
12359          (or
12360           gnus-override-method
12361           (and (gnus-news-group-p gnus-newsgroup-name)
12362                (car (gnus-refer-article-methods)))))
12363         where)
12364     ;; First we check to see whether the header in question is already
12365     ;; fetched.
12366     (if (stringp id)
12367         ;; This is a Message-ID.
12368         (setq header (or header (gnus-id-to-header id)))
12369       ;; This is an article number.
12370       (setq header (or header (gnus-summary-article-header id))))
12371     (if (and header
12372              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12373         ;; We have found the header.
12374         header
12375       ;; We have to really fetch the header to this article.
12376       (with-current-buffer nntp-server-buffer
12377         (when (setq where (gnus-request-head id group))
12378           (nnheader-fold-continuation-lines)
12379           (goto-char (point-max))
12380           (insert ".\n")
12381           (goto-char (point-min))
12382           (insert "211 ")
12383           (princ (cond
12384                   ((numberp id) id)
12385                   ((cdr where) (cdr where))
12386                   (header (mail-header-number header))
12387                   (t gnus-reffed-article-number))
12388                  (current-buffer))
12389           (insert " Article retrieved.\n"))
12390         (if (or (not where)
12391                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12392             ()                          ; Malformed head.
12393           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12394             (when (and (stringp id)
12395                        (or
12396                         (not (string= (gnus-group-real-name group)
12397                                       (car where)))
12398                         (not (gnus-server-equal gnus-override-method
12399                                                 (gnus-group-method group)))))
12400               ;; If we fetched by Message-ID and the article came from
12401               ;; a different group (or server), we fudge some bogus
12402               ;; article numbers for this article.
12403               (mail-header-set-number header gnus-reffed-article-number))
12404             (with-current-buffer gnus-summary-buffer
12405               (decf gnus-reffed-article-number)
12406               (gnus-remove-header (mail-header-number header))
12407               (push header gnus-newsgroup-headers)
12408               (setq gnus-current-headers header)
12409               (push (mail-header-number header) gnus-newsgroup-limit)))
12410           header)))))
12411
12412 (defun gnus-remove-header (number)
12413   "Remove header NUMBER from `gnus-newsgroup-headers'."
12414   (if (and gnus-newsgroup-headers
12415            (= number (mail-header-number (car gnus-newsgroup-headers))))
12416       (pop gnus-newsgroup-headers)
12417     (let ((headers gnus-newsgroup-headers))
12418       (while (and (cdr headers)
12419                   (not (= number (mail-header-number (cadr headers)))))
12420         (pop headers))
12421       (when (cdr headers)
12422         (setcdr headers (cddr headers))))))
12423
12424 ;;;
12425 ;;; summary highlights
12426 ;;;
12427
12428 (defun gnus-highlight-selected-summary ()
12429   "Highlight selected article in summary buffer."
12430   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12431   (when gnus-summary-selected-face
12432     (save-excursion
12433       (let* ((beg (point-at-bol))
12434              (end (point-at-eol))
12435              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12436              (from (if (get-text-property beg gnus-mouse-face-prop)
12437                        beg
12438                      (or (next-single-property-change
12439                           beg gnus-mouse-face-prop nil end)
12440                          beg)))
12441              (to
12442               (if (= from end)
12443                   (- from 2)
12444                 (or (next-single-property-change
12445                      from gnus-mouse-face-prop nil end)
12446                     end))))
12447         ;; If no mouse-face prop on line we will have to = from = end,
12448         ;; so we highlight the entire line instead.
12449         (when (= (+ to 2) from)
12450           (setq from beg)
12451           (setq to end))
12452         (if gnus-newsgroup-selected-overlay
12453             ;; Move old overlay.
12454             (gnus-move-overlay
12455              gnus-newsgroup-selected-overlay from to (current-buffer))
12456           ;; Create new overlay.
12457           (gnus-overlay-put
12458            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12459            'face gnus-summary-selected-face))))))
12460
12461 (defvar gnus-summary-highlight-line-cached nil)
12462 (defvar gnus-summary-highlight-line-trigger nil)
12463
12464 (defun gnus-summary-highlight-line-0 ()
12465   (if (and (eq gnus-summary-highlight-line-trigger
12466                gnus-summary-highlight)
12467            gnus-summary-highlight-line-cached)
12468       gnus-summary-highlight-line-cached
12469     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12470           gnus-summary-highlight-line-cached
12471           (let* ((cond (list 'cond))
12472                  (c cond)
12473                  (list gnus-summary-highlight))
12474             (while list
12475               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12476                               nil))
12477               (setq c (cdr c)
12478                     list (cdr list)))
12479             (gnus-byte-compile (list 'lambda nil cond))))))
12480
12481 (defun gnus-summary-highlight-line ()
12482   "Highlight current line according to `gnus-summary-highlight'."
12483   (let* ((beg (point-at-bol))
12484          (article (or (gnus-summary-article-number) gnus-current-article))
12485          (score (or (cdr (assq article
12486                                gnus-newsgroup-scored))
12487                     gnus-summary-default-score 0))
12488          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12489          (inhibit-read-only t)
12490          (default gnus-summary-default-score)
12491          (default-high gnus-summary-default-high-score)
12492          (default-low gnus-summary-default-low-score)
12493          (uncached (and gnus-summary-use-undownloaded-faces
12494                         (memq article gnus-newsgroup-undownloaded)
12495                         (not (memq article gnus-newsgroup-cached)))))
12496     (let ((face (funcall (gnus-summary-highlight-line-0))))
12497       (unless (eq face (get-text-property beg 'face))
12498         (gnus-put-text-property-excluding-characters-with-faces
12499          beg (point-at-eol) 'face
12500          (setq face (if (boundp face) (symbol-value face) face)))
12501         (when gnus-summary-highlight-line-function
12502           (funcall gnus-summary-highlight-line-function article face))))))
12503
12504 (defun gnus-update-read-articles (group unread &optional compute)
12505   "Update the list of read articles in GROUP.
12506 UNREAD is a sorted list."
12507   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12508         (info (gnus-get-info group))
12509         (prev 1)
12510         read)
12511     (if (or (not info) (not active))
12512         ;; There is no info on this group if it was, in fact,
12513         ;; killed.  Gnus stores no information on killed groups, so
12514         ;; there's nothing to be done.
12515         ;; One could store the information somewhere temporarily,
12516         ;; perhaps...  Hmmm...
12517         ()
12518       ;; Remove any negative articles numbers.
12519       (while (and unread (< (car unread) 0))
12520         (setq unread (cdr unread)))
12521       ;; Remove any expired article numbers
12522       (while (and unread (< (car unread) (car active)))
12523         (setq unread (cdr unread)))
12524       ;; Compute the ranges of read articles by looking at the list of
12525       ;; unread articles.
12526       (while unread
12527         (when (/= (car unread) prev)
12528           (push (if (= prev (1- (car unread))) prev
12529                   (cons prev (1- (car unread))))
12530                 read))
12531         (setq prev (1+ (car unread)))
12532         (setq unread (cdr unread)))
12533       (when (<= prev (cdr active))
12534         (push (cons prev (cdr active)) read))
12535       (setq read (if (> (length read) 1) (nreverse read) read))
12536       (if compute
12537           read
12538         (save-excursion
12539           (let (setmarkundo)
12540             ;; Propagate the read marks to the backend.
12541             (when (and (or gnus-propagate-marks
12542                            (gnus-method-option-p
12543                             (gnus-find-method-for-group group)
12544                             'server-marks))
12545                        (gnus-check-backend-function 'request-set-mark group))
12546               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12547                     (add (gnus-remove-from-range read (gnus-info-read info))))
12548                 (when (or add del)
12549                   (unless (gnus-check-group group)
12550                     (error "Can't open server for %s" group))
12551                   (gnus-request-set-mark
12552                    group (delq nil (list (if add (list add 'add '(read)))
12553                                          (if del (list del 'del '(read))))))
12554                   (setq setmarkundo
12555                         `(gnus-request-set-mark
12556                           ,group
12557                           ',(delq nil (list
12558                                        (if del (list del 'add '(read)))
12559                                        (if add (list add 'del '(read))))))))))
12560             (set-buffer gnus-group-buffer)
12561             (gnus-undo-register
12562               `(progn
12563                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12564                  (gnus-info-set-read ',info ',(gnus-info-read info))
12565                  (gnus-get-unread-articles-in-group ',info
12566                                                     (gnus-active ,group))
12567                  (gnus-group-update-group ,group t)
12568                  ,setmarkundo))))
12569         ;; Enter this list into the group info.
12570         (gnus-info-set-read info read)
12571         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12572         (gnus-get-unread-articles-in-group info (gnus-active group))
12573         t))))
12574
12575 (defun gnus-offer-save-summaries ()
12576   "Offer to save all active summary buffers."
12577   (let (buffers)
12578     ;; Go through all buffers and find all summaries.
12579     (dolist (buffer (buffer-list))
12580       (when (and (setq buffer (buffer-name buffer))
12581                  (string-match "Summary" buffer)
12582                  (with-current-buffer buffer
12583                    ;; We check that this is, indeed, a summary buffer.
12584                    (and (eq major-mode 'gnus-summary-mode)
12585                         ;; Also make sure this isn't bogus.
12586                         gnus-newsgroup-prepared
12587                         ;; Also make sure that this isn't a
12588                         ;; dead summary buffer.
12589                         (not gnus-dead-summary-mode))))
12590         (push buffer buffers)))
12591     ;; Go through all these summary buffers and offer to save them.
12592     (when buffers
12593       (save-excursion
12594         (if (eq gnus-interactive-exit 'quiet)
12595             (dolist (buffer buffers)
12596               (switch-to-buffer buffer)
12597               (gnus-summary-exit))
12598           (map-y-or-n-p
12599            "Update summary buffer %s? "
12600            (lambda (buf)
12601              (switch-to-buffer buf)
12602              (gnus-summary-exit))
12603            buffers))))))
12604
12605 (defun gnus-summary-setup-default-charset ()
12606   "Setup newsgroup default charset."
12607   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12608       (setq gnus-newsgroup-charset nil)
12609     (let* ((ignored-charsets
12610             (or gnus-newsgroup-ephemeral-ignored-charsets
12611                 (append
12612                  (and gnus-newsgroup-name
12613                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12614                  gnus-newsgroup-ignored-charsets))))
12615       (setq gnus-newsgroup-charset
12616             (or gnus-newsgroup-ephemeral-charset
12617                 (and gnus-newsgroup-name
12618                      (gnus-parameter-charset gnus-newsgroup-name))
12619                 gnus-default-charset))
12620       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12621            ignored-charsets))))
12622
12623 ;;;
12624 ;;; Mime Commands
12625 ;;;
12626
12627 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12628   "Display the current article buffer fully MIME-buttonized.
12629 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12630 treated as multipart/mixed."
12631   (interactive "P")
12632   (require 'gnus-art)
12633   (let ((gnus-unbuttonized-mime-types nil)
12634         (gnus-mime-display-multipart-as-mixed show-all-parts))
12635     (gnus-summary-show-article)))
12636
12637 (defun gnus-summary-repair-multipart (article)
12638   "Add a Content-Type header to a multipart article without one."
12639   (interactive (list (gnus-summary-article-number)))
12640   (gnus-with-article article
12641     (message-narrow-to-head)
12642     (message-remove-header "Mime-Version")
12643     (goto-char (point-max))
12644     (insert "Mime-Version: 1.0\n")
12645     (widen)
12646     (when (search-forward "\n--" nil t)
12647       (let ((separator (buffer-substring (point) (point-at-eol))))
12648         (message-narrow-to-head)
12649         (message-remove-header "Content-Type")
12650         (goto-char (point-max))
12651         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12652                         separator))
12653         (widen))))
12654   (let (gnus-mark-article-hook)
12655     (gnus-summary-select-article t t nil article)))
12656
12657 (defun gnus-summary-toggle-display-buttonized ()
12658   "Toggle the buttonizing of the article buffer."
12659   (interactive)
12660   (require 'gnus-art)
12661   (if (setq gnus-inhibit-mime-unbuttonizing
12662             (not gnus-inhibit-mime-unbuttonizing))
12663       (let ((gnus-unbuttonized-mime-types nil))
12664         (gnus-summary-show-article))
12665     (gnus-summary-show-article)))
12666
12667 ;;;
12668 ;;; Generic summary marking commands
12669 ;;;
12670
12671 (defvar gnus-summary-marking-alist
12672   '((read gnus-del-mark "d")
12673     (unread gnus-unread-mark "u")
12674     (ticked gnus-ticked-mark "!")
12675     (dormant gnus-dormant-mark "?")
12676     (expirable gnus-expirable-mark "e"))
12677   "An alist of names/marks/keystrokes.")
12678
12679 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12680 (defvar gnus-summary-mark-map)
12681
12682 (defun gnus-summary-make-all-marking-commands ()
12683   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12684   (dolist (elem gnus-summary-marking-alist)
12685     (apply 'gnus-summary-make-marking-command elem)))
12686
12687 (defun gnus-summary-make-marking-command (name mark keystroke)
12688   (let ((map (make-sparse-keymap)))
12689     (define-key gnus-summary-generic-mark-map keystroke map)
12690     (dolist (lway `((next "next" next nil "n")
12691                     (next-unread "next unread" next t "N")
12692                     (prev "previous" prev nil "p")
12693                     (prev-unread "previous unread" prev t "P")
12694                     (nomove "" nil nil ,keystroke)))
12695       (let ((func (gnus-summary-make-marking-command-1
12696                    mark (car lway) lway name)))
12697         (setq func (eval func))
12698         (define-key map (nth 4 lway) func)))))
12699
12700 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12701   `(defun ,(intern
12702             (format "gnus-summary-put-mark-as-%s%s"
12703                     name (if (eq way 'nomove)
12704                              ""
12705                            (concat "-" (symbol-name way)))))
12706      (n)
12707      ,(format
12708        "Mark the current article as %s%s.
12709 If N, the prefix, then repeat N times.
12710 If N is negative, move in reverse order.
12711 The difference between N and the actual number of articles marked is
12712 returned."
12713        name (cadr lway))
12714      (interactive "p")
12715      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12716
12717 (defun gnus-summary-generic-mark (n mark move unread)
12718   "Mark N articles with MARK."
12719   (unless (eq major-mode 'gnus-summary-mode)
12720     (error "This command can only be used in the summary buffer"))
12721   (gnus-summary-show-thread)
12722   (let ((nummove
12723          (cond
12724           ((eq move 'next) 1)
12725           ((eq move 'prev) -1)
12726           (t 0))))
12727     (if (zerop nummove)
12728         (setq n 1)
12729       (when (< n 0)
12730         (setq n (abs n)
12731               nummove (* -1 nummove))))
12732     (while (and (> n 0)
12733                 (gnus-summary-mark-article nil mark)
12734                 (zerop (gnus-summary-next-subject nummove unread t)))
12735       (setq n (1- n)))
12736     (when (/= 0 n)
12737       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12738     (gnus-summary-recenter)
12739     (gnus-summary-position-point)
12740     (gnus-set-mode-line 'summary)
12741     n))
12742
12743 (defun gnus-summary-insert-articles (articles)
12744   (when (setq articles
12745               (gnus-sorted-difference articles
12746                                       (mapcar (lambda (h)
12747                                                 (mail-header-number h))
12748                                               gnus-newsgroup-headers)))
12749     (setq gnus-newsgroup-headers
12750           (gnus-merge 'list
12751                       gnus-newsgroup-headers
12752                       (gnus-fetch-headers articles)
12753                       'gnus-article-sort-by-number))
12754     (setq gnus-newsgroup-articles
12755           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12756     ;; Suppress duplicates?
12757     (when gnus-suppress-duplicates
12758       (gnus-dup-suppress-articles))
12759
12760     (if (and gnus-fetch-old-headers
12761              (eq gnus-headers-retrieved-by 'nov))
12762         ;; We might want to build some more threads first.
12763         (if (eq gnus-fetch-old-headers 'invisible)
12764             (gnus-build-all-threads)
12765           (gnus-build-old-threads))
12766       ;; Mark the inserted articles that are unread as unread.
12767       (setq gnus-newsgroup-unreads
12768             (gnus-sorted-nunion
12769              gnus-newsgroup-unreads
12770              (gnus-sorted-nintersection
12771               (gnus-list-of-unread-articles gnus-newsgroup-name)
12772               articles)))
12773       ;; Mark the inserted articles as selected so that the information
12774       ;; of the marks having been changed by a user may be updated when
12775       ;; exiting this group.  See `gnus-summary-update-info'.
12776       (dolist (art articles)
12777         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12778     ;; Let the Gnus agent mark articles as read.
12779     (when gnus-agent
12780       (gnus-agent-get-undownloaded-list))
12781     ;; Remove list identifiers from subject
12782     (gnus-summary-remove-list-identifiers)
12783     ;; First and last article in this newsgroup.
12784     (when gnus-newsgroup-headers
12785       (setq gnus-newsgroup-begin
12786             (mail-header-number (car gnus-newsgroup-headers))
12787             gnus-newsgroup-end
12788             (mail-header-number
12789              (gnus-last-element gnus-newsgroup-headers))))
12790     (when gnus-use-scoring
12791       (gnus-possibly-score-headers))))
12792
12793 (defun gnus-summary-insert-old-articles (&optional all)
12794   "Insert all old articles in this group.
12795 If ALL is non-nil, already read articles become readable.
12796 If ALL is a number, fetch this number of articles."
12797   (interactive "P")
12798   (prog1
12799       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12800             older len)
12801         (setq older
12802               ;; Some nntp servers lie about their active range.  When
12803               ;; this happens, the active range can be in the millions.
12804               ;; Use a compressed range to avoid creating a huge list.
12805               (gnus-range-difference (list gnus-newsgroup-active) old))
12806         (setq len (gnus-range-length older))
12807         (cond
12808          ((null older) nil)
12809          ((numberp all)
12810           (if (< all len)
12811               (let ((older-range (nreverse older)))
12812                 (setq older nil)
12813
12814                 (while (> all 0)
12815                   (let* ((r (pop older-range))
12816                          (min (if (numberp r) r (car r)))
12817                          (max (if (numberp r) r (cdr r))))
12818                     (while (and (<= min max)
12819                                 (> all 0))
12820                       (push max older)
12821                       (setq all (1- all)
12822                             max (1- max))))))
12823             (setq older (gnus-uncompress-range older))))
12824          (all
12825           (setq older (gnus-uncompress-range older)))
12826          (t
12827           (when (and (numberp gnus-large-newsgroup)
12828                    (> len gnus-large-newsgroup))
12829               (let* ((cursor-in-echo-area nil)
12830                      (initial (gnus-parameter-large-newsgroup-initial
12831                                gnus-newsgroup-name))
12832                      (input
12833                       (read-string
12834                        (format
12835                         "How many articles from %s (%s %d): "
12836                         (gnus-group-decoded-name gnus-newsgroup-name)
12837                         (if initial "max" "default")
12838                         len)
12839                        (if initial
12840                            (cons (number-to-string initial)
12841                                  0)))))
12842                 (unless (string-match "^[ \t]*$" input)
12843                   (setq all (string-to-number input))
12844                   (if (< all len)
12845                       (let ((older-range (nreverse older)))
12846                         (setq older nil)
12847
12848                         (while (> all 0)
12849                           (let* ((r (pop older-range))
12850                                  (min (if (numberp r) r (car r)))
12851                                  (max (if (numberp r) r (cdr r))))
12852                             (while (and (<= min max)
12853                                         (> all 0))
12854                               (push max older)
12855                               (setq all (1- all)
12856                                     max (1- max))))))))))
12857           (setq older (gnus-uncompress-range older))))
12858         (if (not older)
12859             (message "No old news.")
12860           (gnus-summary-insert-articles older)
12861           (gnus-summary-limit (gnus-sorted-nunion old older))))
12862     (gnus-summary-position-point)))
12863
12864 (defun gnus-summary-insert-new-articles ()
12865   "Insert all new articles in this group."
12866   (interactive)
12867   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12868         (old-high gnus-newsgroup-highest)
12869         (nnmail-fetched-sources (list t))
12870         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12871         i new)
12872     (unless new-active
12873       (error "Couldn't fetch new data"))
12874     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12875     (setq i (cdr gnus-newsgroup-active)
12876           gnus-newsgroup-highest i)
12877     (while (> i old-high)
12878       (push i new)
12879       (decf i))
12880     (if (not new)
12881         (message "No gnus is bad news")
12882       (gnus-summary-insert-articles new)
12883       (setq gnus-newsgroup-unreads
12884             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12885       (gnus-summary-limit (gnus-sorted-nunion old new))))
12886   (gnus-summary-position-point))
12887
12888 ;;; Bookmark support for Gnus.
12889 (declare-function bookmark-make-record-default
12890                   "bookmark" (&optional no-file no-context posn))
12891 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12892 (declare-function bookmark-default-handler "bookmark" (bmk))
12893 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12894 (defvar bookmark-yank-point)
12895 (defvar bookmark-current-buffer)
12896
12897 (defun gnus-summary-bookmark-make-record ()
12898   "Make a bookmark entry for a Gnus summary buffer."
12899   (let (pos buf)
12900     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12901       (save-restriction              ; FIXME is it necessary to widen?
12902         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12903       (setq buf "art") ; We are recording bookmark from article buffer.
12904       (setq bookmark-yank-point (point))
12905       (setq bookmark-current-buffer (current-buffer))
12906       (gnus-article-show-summary))      ; Go back in summary buffer.
12907     ;; We are now recording bookmark from summary buffer.
12908     (unless buf (setq buf "sum"))
12909     (let* ((subject (elt (gnus-summary-article-header) 1))
12910            (grp     (car gnus-article-current))
12911            (art     (cdr gnus-article-current))
12912            (head    (gnus-summary-article-header art))
12913            (id      (mail-header-id head)))
12914       `(,subject
12915         ,@(condition-case nil
12916               (bookmark-make-record-default 'no-file 'no-context pos)
12917             (wrong-number-of-arguments
12918              (bookmark-make-record-default 'point-only)))
12919         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12920         (group . ,grp) (article . ,art)
12921         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12922
12923 ;;;###autoload
12924 (defun gnus-summary-bookmark-jump (bookmark)
12925   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12926 BOOKMARK is a bookmark name or a bookmark record."
12927   (let ((group    (bookmark-prop-get bookmark 'group))
12928         (article  (bookmark-prop-get bookmark 'article))
12929         (id       (bookmark-prop-get bookmark 'message-id))
12930         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12931     (gnus-fetch-group group (list article))
12932     (gnus-summary-insert-cached-articles)
12933     (gnus-summary-goto-article id nil 'force)
12934     ;; FIXME we have to wait article buffer is ready (only large buffer)
12935     ;; Is there a better solution to know that?
12936     ;; If we don't wait `bookmark-default-handler' will have no chance
12937     ;; to set position. However there is no error, just wrong pos.
12938     (sit-for 1)
12939     (when (string= buf "Gnus-art")
12940       (other-window 1))
12941     (bookmark-default-handler
12942      `(""
12943        (buffer . ,(current-buffer))
12944        . ,(bookmark-get-bookmark-record bookmark)))))
12945
12946 (gnus-summary-make-all-marking-commands)
12947
12948 (gnus-ems-redefine)
12949
12950 (provide 'gnus-sum)
12951
12952 (run-hooks 'gnus-sum-load-hook)
12953
12954 ;; Local Variables:
12955 ;; coding: iso-8859-1
12956 ;; End:
12957
12958 ;;; gnus-sum.el ends here