gnus-sum.el (gnus-summary-mode): Comment fix.
[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   ;; Force paragraph direction to be left-to-right.  Don't make it
3102   ;; bound globally in old Emacsen and XEmacsen.
3103   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3104   (add-to-invisibility-spec '(gnus-sum . t))
3105   (gnus-summary-set-display-table)
3106   (gnus-set-default-directory)
3107   (make-local-variable 'gnus-summary-line-format)
3108   (make-local-variable 'gnus-summary-line-format-spec)
3109   (make-local-variable 'gnus-summary-dummy-line-format)
3110   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3111   (make-local-variable 'gnus-summary-mark-positions)
3112   (gnus-make-local-hook 'pre-command-hook)
3113   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3114   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3115   (turn-on-gnus-mailing-list-mode)
3116   (mm-enable-multibyte)
3117   (set (make-local-variable 'bookmark-make-record-function)
3118        'gnus-summary-bookmark-make-record)
3119   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3120   (gnus-update-summary-mark-positions))
3121
3122 (defun gnus-summary-make-local-variables ()
3123   "Make all the local summary buffer variables."
3124   (let (global)
3125     (dolist (local gnus-summary-local-variables)
3126       (if (consp local)
3127           (progn
3128             (if (eq (cdr local) 'global)
3129                 ;; Copy the global value of the variable.
3130                 (setq global (symbol-value (car local)))
3131               ;; Use the value from the list.
3132               (setq global (eval (cdr local))))
3133             (set (make-local-variable (car local)) global))
3134         ;; Simple nil-valued local variable.
3135         (set (make-local-variable local) nil)))))
3136
3137 ;; Summary data functions.
3138
3139 (defmacro gnus-data-number (data)
3140   `(car ,data))
3141
3142 (defmacro gnus-data-set-number (data number)
3143   `(setcar ,data ,number))
3144
3145 (defmacro gnus-data-mark (data)
3146   `(nth 1 ,data))
3147
3148 (defmacro gnus-data-set-mark (data mark)
3149   `(setcar (nthcdr 1 ,data) ,mark))
3150
3151 (defmacro gnus-data-pos (data)
3152   `(nth 2 ,data))
3153
3154 (defmacro gnus-data-set-pos (data pos)
3155   `(setcar (nthcdr 2 ,data) ,pos))
3156
3157 (defmacro gnus-data-header (data)
3158   `(nth 3 ,data))
3159
3160 (defmacro gnus-data-set-header (data header)
3161   `(setf (nth 3 ,data) ,header))
3162
3163 (defmacro gnus-data-level (data)
3164   `(nth 4 ,data))
3165
3166 (defmacro gnus-data-unread-p (data)
3167   `(= (nth 1 ,data) gnus-unread-mark))
3168
3169 (defmacro gnus-data-read-p (data)
3170   `(/= (nth 1 ,data) gnus-unread-mark))
3171
3172 (defmacro gnus-data-pseudo-p (data)
3173   `(consp (nth 3 ,data)))
3174
3175 (defmacro gnus-data-find (number)
3176   `(assq ,number gnus-newsgroup-data))
3177
3178 (defmacro gnus-data-find-list (number &optional data)
3179   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3180      (memq (assq ,number bdata)
3181            bdata)))
3182
3183 (defmacro gnus-data-make (number mark pos header level)
3184   `(list ,number ,mark ,pos ,header ,level))
3185
3186 (defun gnus-data-enter (after-article number mark pos header level offset)
3187   (let ((data (gnus-data-find-list after-article)))
3188     (unless data
3189       (error "No such article: %d" after-article))
3190     (setcdr data (cons (gnus-data-make number mark pos header level)
3191                        (cdr data)))
3192     (setq gnus-newsgroup-data-reverse nil)
3193     (gnus-data-update-list (cddr data) offset)))
3194
3195 (defun gnus-data-enter-list (after-article list &optional offset)
3196   (when list
3197     (let ((data (and after-article (gnus-data-find-list after-article)))
3198           (ilist list))
3199       (if (not (or data
3200                    after-article))
3201           (let ((odata gnus-newsgroup-data))
3202             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3203             (when offset
3204               (gnus-data-update-list odata offset)))
3205         ;; Find the last element in the list to be spliced into the main
3206         ;; list.
3207         (setq list (last list))
3208         (if (not data)
3209             (progn
3210               (setcdr list gnus-newsgroup-data)
3211               (setq gnus-newsgroup-data ilist)
3212               (when offset
3213                 (gnus-data-update-list (cdr list) offset)))
3214           (setcdr list (cdr data))
3215           (setcdr data ilist)
3216           (when offset
3217             (gnus-data-update-list (cdr list) offset))))
3218       (setq gnus-newsgroup-data-reverse nil))))
3219
3220 (defun gnus-data-remove (article &optional offset)
3221   (let ((data gnus-newsgroup-data))
3222     (if (= (gnus-data-number (car data)) article)
3223         (progn
3224           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3225                 gnus-newsgroup-data-reverse nil)
3226           (when offset
3227             (gnus-data-update-list gnus-newsgroup-data offset)))
3228       (while (cdr data)
3229         (when (= (gnus-data-number (cadr data)) article)
3230           (setcdr data (cddr data))
3231           (when offset
3232             (gnus-data-update-list (cdr data) offset))
3233           (setq data nil
3234                 gnus-newsgroup-data-reverse nil))
3235         (setq data (cdr data))))))
3236
3237 (defmacro gnus-data-list (backward)
3238   `(if ,backward
3239        (or gnus-newsgroup-data-reverse
3240            (setq gnus-newsgroup-data-reverse
3241                  (reverse gnus-newsgroup-data)))
3242      gnus-newsgroup-data))
3243
3244 (defun gnus-data-update-list (data offset)
3245   "Add OFFSET to the POS of all data entries in DATA."
3246   (setq gnus-newsgroup-data-reverse nil)
3247   (while data
3248     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3249     (setq data (cdr data))))
3250
3251 (defun gnus-summary-article-pseudo-p (article)
3252   "Say whether this article is a pseudo article or not."
3253   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3254
3255 (defmacro gnus-summary-article-sparse-p (article)
3256   "Say whether this article is a sparse article or not."
3257   `(memq ,article gnus-newsgroup-sparse))
3258
3259 (defmacro gnus-summary-article-ancient-p (article)
3260   "Say whether this article is a sparse article or not."
3261   `(memq ,article gnus-newsgroup-ancient))
3262
3263 (defun gnus-article-parent-p (number)
3264   "Say whether this article is a parent or not."
3265   (let ((data (gnus-data-find-list number)))
3266     (and (cdr data)              ; There has to be an article after...
3267          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3268             (gnus-data-level (nth 1 data))))))
3269
3270 (defun gnus-article-children (number)
3271   "Return a list of all children to NUMBER."
3272   (let* ((data (gnus-data-find-list number))
3273          (level (gnus-data-level (car data)))
3274          children)
3275     (setq data (cdr data))
3276     (while (and data
3277                 (= (gnus-data-level (car data)) (1+ level)))
3278       (push (gnus-data-number (car data)) children)
3279       (setq data (cdr data)))
3280     children))
3281
3282 (defmacro gnus-summary-skip-intangible ()
3283   "If the current article is intangible, then jump to a different article."
3284   '(let ((to (get-text-property (point) 'gnus-intangible)))
3285      (and to (gnus-summary-goto-subject to))))
3286
3287 (defmacro gnus-summary-article-intangible-p ()
3288   "Say whether this article is intangible or not."
3289   '(get-text-property (point) 'gnus-intangible))
3290
3291 (defun gnus-article-read-p (article)
3292   "Say whether ARTICLE is read or not."
3293   (not (or (memq article gnus-newsgroup-marked)
3294            (memq article gnus-newsgroup-spam-marked)
3295            (memq article gnus-newsgroup-unreads)
3296            (memq article gnus-newsgroup-unselected)
3297            (memq article gnus-newsgroup-dormant))))
3298
3299 ;; Some summary mode macros.
3300
3301 (defmacro gnus-summary-article-number ()
3302   "The article number of the article on the current line.
3303 If there isn't an article number here, then we return the current
3304 article number."
3305   '(progn
3306      (gnus-summary-skip-intangible)
3307      (or (get-text-property (point) 'gnus-number)
3308          (gnus-summary-last-subject))))
3309
3310 (defmacro gnus-summary-article-header (&optional number)
3311   "Return the header of article NUMBER."
3312   `(gnus-data-header (gnus-data-find
3313                       ,(or number '(gnus-summary-article-number)))))
3314
3315 (defmacro gnus-summary-thread-level (&optional number)
3316   "Return the level of thread that starts with article NUMBER."
3317   `(if (and (eq gnus-summary-make-false-root 'dummy)
3318             (get-text-property (point) 'gnus-intangible))
3319        0
3320      (gnus-data-level (gnus-data-find
3321                        ,(or number '(gnus-summary-article-number))))))
3322
3323 (defmacro gnus-summary-article-mark (&optional number)
3324   "Return the mark of article NUMBER."
3325   `(gnus-data-mark (gnus-data-find
3326                     ,(or number '(gnus-summary-article-number)))))
3327
3328 (defmacro gnus-summary-article-pos (&optional number)
3329   "Return the position of the line of article NUMBER."
3330   `(gnus-data-pos (gnus-data-find
3331                    ,(or number '(gnus-summary-article-number)))))
3332
3333 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3334 (defmacro gnus-summary-article-subject (&optional number)
3335   "Return current subject string or nil if nothing."
3336   `(let ((headers
3337           ,(if number
3338                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3339              '(gnus-data-header (assq (gnus-summary-article-number)
3340                                       gnus-newsgroup-data)))))
3341      (and headers
3342           (vectorp headers)
3343           (mail-header-subject headers))))
3344
3345 (defmacro gnus-summary-article-score (&optional number)
3346   "Return current article score."
3347   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3348                   gnus-newsgroup-scored))
3349        gnus-summary-default-score 0))
3350
3351 (defun gnus-summary-article-children (&optional number)
3352   "Return a list of article numbers that are children of article NUMBER."
3353   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3354          (level (gnus-data-level (car data)))
3355          l children)
3356     (while (and (setq data (cdr data))
3357                 (> (setq l (gnus-data-level (car data))) level))
3358       (and (= (1+ level) l)
3359            (push (gnus-data-number (car data))
3360                  children)))
3361     (nreverse children)))
3362
3363 (defun gnus-summary-article-parent (&optional number)
3364   "Return the article number of the parent of article NUMBER."
3365   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3366                                     (gnus-data-list t)))
3367          (level (gnus-data-level (car data))))
3368     (if (zerop level)
3369         ()                              ; This is a root.
3370       ;; We search until we find an article with a level less than
3371       ;; this one.  That function has to be the parent.
3372       (while (and (setq data (cdr data))
3373                   (not (< (gnus-data-level (car data)) level))))
3374       (and data (gnus-data-number (car data))))))
3375
3376 (defun gnus-unread-mark-p (mark)
3377   "Say whether MARK is the unread mark."
3378   (= mark gnus-unread-mark))
3379
3380 (defun gnus-read-mark-p (mark)
3381   "Say whether MARK is one of the marks that mark as read.
3382 This is all marks except unread, ticked, dormant, and expirable."
3383   (not (or (= mark gnus-unread-mark)
3384            (= mark gnus-ticked-mark)
3385            (= mark gnus-spam-mark)
3386            (= mark gnus-dormant-mark)
3387            (= mark gnus-expirable-mark))))
3388
3389 (defmacro gnus-article-mark (number)
3390   "Return the MARK of article NUMBER.
3391 This macro should only be used when computing the mark the \"first\"
3392 time; i.e., when generating the summary lines.  After that,
3393 `gnus-summary-article-mark' should be used to examine the
3394 marks of articles."
3395   `(cond
3396     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3397     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3398     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3399     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3400     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3401     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3402     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3403     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3404            gnus-ancient-mark))))
3405
3406 ;; Saving hidden threads.
3407
3408 (defmacro gnus-save-hidden-threads (&rest forms)
3409   "Save hidden threads, eval FORMS, and restore the hidden threads."
3410   (let ((config (make-symbol "config")))
3411     `(let ((,config (gnus-hidden-threads-configuration)))
3412        (unwind-protect
3413            (save-excursion
3414              ,@forms)
3415          (gnus-restore-hidden-threads-configuration ,config)))))
3416 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3417 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3418
3419 (defun gnus-data-compute-positions ()
3420   "Compute the positions of all articles."
3421   (setq gnus-newsgroup-data-reverse nil)
3422   (let ((data gnus-newsgroup-data))
3423     (save-excursion
3424       (gnus-save-hidden-threads
3425         (gnus-summary-show-all-threads)
3426         (goto-char (point-min))
3427         (while data
3428           (while (get-text-property (point) 'gnus-intangible)
3429             (forward-line 1))
3430           (gnus-data-set-pos (car data) (+ (point) 3))
3431           (setq data (cdr data))
3432           (forward-line 1))))))
3433
3434 (defun gnus-hidden-threads-configuration ()
3435   "Return the current hidden threads configuration."
3436   (save-excursion
3437     (let (config)
3438       (goto-char (point-min))
3439       (while (not (eobp))
3440         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3441           (push (save-excursion (forward-line 0) (point)) config))
3442         (forward-line 1))
3443       config)))
3444
3445 (defun gnus-restore-hidden-threads-configuration (config)
3446   "Restore hidden threads configuration from CONFIG."
3447   (save-excursion
3448     (let (point (inhibit-read-only t))
3449       (while (setq point (pop config))
3450         (goto-char point)
3451         (gnus-summary-hide-thread)))))
3452
3453 ;; Various summary mode internalish functions.
3454
3455 (defun gnus-mouse-pick-article (e)
3456   (interactive "e")
3457   (mouse-set-point e)
3458   (gnus-summary-next-page nil t))
3459
3460 (defun gnus-summary-set-display-table ()
3461   "Change the display table.
3462 Odd characters have a tendency to mess
3463 up nicely formatted displays - we make all possible glyphs
3464 display only a single character."
3465
3466   ;; We start from the standard display table, if any.
3467   (let ((table (or (copy-sequence standard-display-table)
3468                    (make-display-table)))
3469         (i 32))
3470     ;; Nix out all the control chars...
3471     (while (>= (setq i (1- i)) 0)
3472       (gnus-put-display-table i [??] table))
3473    ;; ... but not newline and cr, of course.  (cr is necessary for the
3474     ;; selective display).
3475     (gnus-put-display-table ?\n nil table)
3476     (gnus-put-display-table ?\r nil table)
3477     ;; We keep TAB as well.
3478     (gnus-put-display-table ?\t nil table)
3479     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3480     ;; Emacs 23 (unicode), that are not set already.
3481     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3482                  160
3483                256)))
3484       (while (>= (setq i (1- i)) 127)
3485         ;; Only modify if the entry is nil.
3486         (unless (gnus-get-display-table i table)
3487           (gnus-put-display-table i [??] table))))
3488     (setq buffer-display-table table)))
3489
3490 (defun gnus-summary-set-article-display-arrow (pos)
3491   "Update the overlay arrow to point to line at position POS."
3492   (when gnus-summary-display-arrow
3493     (make-local-variable 'overlay-arrow-position)
3494     (make-local-variable 'overlay-arrow-string)
3495     (save-excursion
3496       (goto-char pos)
3497       (beginning-of-line)
3498       (unless overlay-arrow-position
3499         (setq overlay-arrow-position (make-marker)))
3500       (setq overlay-arrow-string "=>"
3501             overlay-arrow-position (set-marker overlay-arrow-position
3502                                                (point)
3503                                                (current-buffer))))))
3504
3505 (defun gnus-summary-setup-buffer (group)
3506   "Initialize summary buffer.
3507 If the setup was successful, non-nil is returned."
3508   (let ((buffer (gnus-summary-buffer-name group))
3509         (dead-name (concat "*Dead Summary "
3510                            (gnus-group-decoded-name group) "*")))
3511     ;; If a dead summary buffer exists, we kill it.
3512     (when (gnus-buffer-live-p dead-name)
3513       (gnus-kill-buffer dead-name))
3514     (if (get-buffer buffer)
3515         (progn
3516           (set-buffer buffer)
3517           (setq gnus-summary-buffer (current-buffer))
3518           (not gnus-newsgroup-prepared))
3519       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3520       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3521       (gnus-summary-mode group)
3522       (when (gnus-group-quit-config group)
3523         (set (make-local-variable 'gnus-single-article-buffer) nil))
3524       (make-local-variable 'gnus-article-buffer)
3525       (make-local-variable 'gnus-article-current)
3526       (make-local-variable 'gnus-original-article-buffer)
3527       (setq gnus-newsgroup-name group)
3528       ;; Set any local variables in the group parameters.
3529       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3530       t)))
3531
3532 (defun gnus-set-global-variables ()
3533   "Set the global equivalents of the buffer-local variables.
3534 They are set to the latest values they had.  These reflect the summary
3535 buffer that was in action when the last article was fetched."
3536   (when (eq major-mode 'gnus-summary-mode)
3537     (setq gnus-summary-buffer (current-buffer))
3538     (let ((name gnus-newsgroup-name)
3539           (marked gnus-newsgroup-marked)
3540           (spam gnus-newsgroup-spam-marked)
3541           (unread gnus-newsgroup-unreads)
3542           (headers gnus-current-headers)
3543           (data gnus-newsgroup-data)
3544           (summary gnus-summary-buffer)
3545           (article-buffer gnus-article-buffer)
3546           (original gnus-original-article-buffer)
3547           (gac gnus-article-current)
3548           (reffed gnus-reffed-article-number)
3549           (score-file gnus-current-score-file)
3550           (default-charset gnus-newsgroup-charset)
3551           vlist)
3552       (let ((locals gnus-newsgroup-variables))
3553         (while locals
3554           (if (consp (car locals))
3555               (push (eval (caar locals)) vlist)
3556             (push (eval (car locals)) vlist))
3557           (setq locals (cdr locals)))
3558         (setq vlist (nreverse vlist)))
3559       (with-current-buffer gnus-group-buffer
3560         (setq gnus-newsgroup-name name
3561               gnus-newsgroup-marked marked
3562               gnus-newsgroup-spam-marked spam
3563               gnus-newsgroup-unreads unread
3564               gnus-current-headers headers
3565               gnus-newsgroup-data data
3566               gnus-article-current gac
3567               gnus-summary-buffer summary
3568               gnus-article-buffer article-buffer
3569               gnus-original-article-buffer original
3570               gnus-reffed-article-number reffed
3571               gnus-current-score-file score-file
3572               gnus-newsgroup-charset default-charset)
3573         (let ((locals gnus-newsgroup-variables))
3574           (while locals
3575             (if (consp (car locals))
3576                 (set (caar locals) (pop vlist))
3577               (set (car locals) (pop vlist)))
3578             (setq locals (cdr locals))))
3579         ;; The article buffer also has local variables.
3580         (when (gnus-buffer-live-p gnus-article-buffer)
3581           (set-buffer gnus-article-buffer)
3582           (setq gnus-summary-buffer summary))))))
3583
3584 (defun gnus-summary-article-unread-p (article)
3585   "Say whether ARTICLE is unread or not."
3586   (memq article gnus-newsgroup-unreads))
3587
3588 (defun gnus-summary-first-article-p (&optional article)
3589   "Return whether ARTICLE is the first article in the buffer."
3590   (if (not (setq article (or article (gnus-summary-article-number))))
3591       nil
3592     (eq article (caar gnus-newsgroup-data))))
3593
3594 (defun gnus-summary-last-article-p (&optional article)
3595   "Return whether ARTICLE is the last article in the buffer."
3596   (if (not (setq article (or article (gnus-summary-article-number))))
3597       ;; All non-existent numbers are the last article.  :-)
3598       t
3599     (not (cdr (gnus-data-find-list article)))))
3600
3601 (defun gnus-make-thread-indent-array (&optional n)
3602   (when (or n
3603             (progn (setq n 200) nil)
3604             (null gnus-thread-indent-array)
3605             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3606     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3607           gnus-thread-indent-array-level gnus-thread-indent-level)
3608     (while (>= n 0)
3609       (aset gnus-thread-indent-array n
3610             (make-string (* n gnus-thread-indent-level) ? ))
3611       (setq n (1- n)))))
3612
3613 (defun gnus-update-summary-mark-positions ()
3614   "Compute where the summary marks are to go."
3615   (save-excursion
3616     (when (gnus-buffer-exists-p gnus-summary-buffer)
3617       (set-buffer gnus-summary-buffer))
3618     (let ((spec gnus-summary-line-format-spec)
3619           pos)
3620       (save-excursion
3621         (gnus-set-work-buffer)
3622         (let ((gnus-tmp-unread ?Z)
3623               (gnus-replied-mark ?Z)
3624               (gnus-score-below-mark ?Z)
3625               (gnus-score-over-mark ?Z)
3626               (gnus-undownloaded-mark ?Z)
3627               (gnus-summary-line-format-spec spec)
3628               (gnus-newsgroup-downloadable '(0))
3629               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3630               case-fold-search ignores)
3631           ;; Here, all marks are bound to Z.
3632           (gnus-summary-insert-line header
3633                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3634           (goto-char (point-min))
3635           ;; Memorize the positions of the same characters as dummy marks.
3636           (while (re-search-forward "[A-D]" nil t)
3637             (push (point) ignores))
3638           (erase-buffer)
3639           ;; We use A-D as dummy marks in order to know column positions
3640           ;; where marks should be inserted.
3641           (setq gnus-tmp-unread ?A
3642                 gnus-replied-mark ?B
3643                 gnus-score-below-mark ?C
3644                 gnus-score-over-mark ?C
3645                 gnus-undownloaded-mark ?D)
3646           (gnus-summary-insert-line header
3647                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3648           ;; Ignore characters which aren't dummy marks.
3649           (dolist (p ignores)
3650             (delete-region (goto-char (1- p)) p)
3651             (insert ?Z))
3652           (goto-char (point-min))
3653           (setq pos (list (cons 'unread
3654                                 (and (search-forward "A" nil t)
3655                                      (- (point) (point-min) 1)))))
3656           (goto-char (point-min))
3657           (push (cons 'replied (and (search-forward "B" nil t)
3658                                     (- (point) (point-min) 1)))
3659                 pos)
3660           (goto-char (point-min))
3661           (push (cons 'score (and (search-forward "C" nil t)
3662                                   (- (point) (point-min) 1)))
3663                 pos)
3664           (goto-char (point-min))
3665           (push (cons 'download (and (search-forward "D" nil t)
3666                                      (- (point) (point-min) 1)))
3667                 pos)))
3668       (setq gnus-summary-mark-positions pos))))
3669
3670 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3671   "Insert a dummy root in the summary buffer."
3672   (beginning-of-line)
3673   (gnus-add-text-properties
3674    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3675    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3676
3677 (defun gnus-summary-extract-address-component (from)
3678   (or (car (funcall gnus-extract-address-components from))
3679       from))
3680
3681 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3682   (let ((mail-parse-charset gnus-newsgroup-charset)
3683         (ignored-from-addresses (gnus-ignored-from-addresses))
3684         ; Is it really necessary to do this next part for each summary line?
3685         ; Luckily, doesn't seem to slow things down much.
3686         (mail-parse-ignored-charsets
3687          (with-current-buffer gnus-summary-buffer
3688            gnus-newsgroup-ignored-charsets)))
3689     (or
3690      (and ignored-from-addresses
3691           (string-match ignored-from-addresses gnus-tmp-from)
3692           (let ((extra-headers (mail-header-extra header))
3693                 to
3694                 newsgroups)
3695             (cond
3696              ((setq to (cdr (assq 'To extra-headers)))
3697               (concat gnus-summary-to-prefix
3698                       (inline
3699                         (gnus-summary-extract-address-component
3700                          (funcall gnus-decode-encoded-address-function to)))))
3701              ((setq newsgroups
3702                     (or
3703                      (cdr (assq 'Newsgroups extra-headers))
3704                      (and
3705                       (memq 'Newsgroups gnus-extra-headers)
3706                       (eq (car (gnus-find-method-for-group
3707                                 gnus-newsgroup-name)) 'nntp)
3708                       (gnus-group-real-name gnus-newsgroup-name))))
3709               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3710      (gnus-string-mark-left-to-right
3711       (inline
3712        (gnus-summary-extract-address-component gnus-tmp-from))))))
3713
3714 (defun gnus-summary-insert-line (gnus-tmp-header
3715                                  gnus-tmp-level gnus-tmp-current
3716                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3717                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3718                                  &optional gnus-tmp-dummy gnus-tmp-score
3719                                  gnus-tmp-process)
3720   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3721       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3722                                           gnus-tmp-level)))
3723   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3724          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3725          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3726          (gnus-tmp-score-char
3727           (if (or (null gnus-summary-default-score)
3728                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3729                       gnus-summary-zcore-fuzz))
3730               ?                         ;Whitespace
3731             (if (< gnus-tmp-score gnus-summary-default-score)
3732                 gnus-score-below-mark gnus-score-over-mark)))
3733          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3734          (gnus-tmp-replied
3735           (cond (gnus-tmp-process gnus-process-mark)
3736                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3737                  gnus-cached-mark)
3738                 (gnus-tmp-replied gnus-replied-mark)
3739                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3740                  gnus-forwarded-mark)
3741                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3742                  gnus-saved-mark)
3743                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3744                  gnus-unseen-mark)
3745                 (t gnus-no-mark)))
3746          (gnus-tmp-downloaded
3747           (cond (undownloaded
3748                  gnus-undownloaded-mark)
3749                 (gnus-newsgroup-agentized
3750                  gnus-downloaded-mark)
3751                 (t
3752                  gnus-no-mark)))
3753          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3754          (gnus-tmp-name
3755           (cond
3756            ((string-match "<[^>]+> *$" gnus-tmp-from)
3757             (let ((beg (match-beginning 0)))
3758               (or (and (string-match "^\".+\"" gnus-tmp-from)
3759                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3760                   (substring gnus-tmp-from 0 beg))))
3761            ((string-match "(.+)" gnus-tmp-from)
3762             (substring gnus-tmp-from
3763                        (1+ (match-beginning 0)) (1- (match-end 0))))
3764            (t gnus-tmp-from)))
3765          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3766          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3767          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3768          (inhibit-read-only t))
3769     (when (string= gnus-tmp-name "")
3770       (setq gnus-tmp-name gnus-tmp-from))
3771     (unless (numberp gnus-tmp-lines)
3772       (setq gnus-tmp-lines -1))
3773     (if (= gnus-tmp-lines -1)
3774         (setq gnus-tmp-lines "?")
3775       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3776     (condition-case ()
3777         (gnus-put-text-property
3778          (point)
3779          (progn (eval gnus-summary-line-format-spec) (point))
3780          'gnus-number gnus-tmp-number)
3781       (error (gnus-message 5 "Error updating the summary line")))
3782     (when (gnus-visual-p 'summary-highlight 'highlight)
3783       (forward-line -1)
3784       (gnus-summary-highlight-line)
3785       (gnus-run-hooks 'gnus-summary-update-hook)
3786       (forward-line 1))))
3787
3788 (defun gnus-summary-update-line (&optional dont-update)
3789   "Update summary line after change."
3790   (when (and gnus-summary-default-score
3791              (not gnus-summary-inhibit-highlight))
3792     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3793            (article (gnus-summary-article-number))
3794            (score (gnus-summary-article-score article)))
3795       (unless dont-update
3796         (if (and gnus-summary-mark-below
3797                  (< (gnus-summary-article-score)
3798                     gnus-summary-mark-below))
3799             ;; This article has a low score, so we mark it as read.
3800             (when (memq article gnus-newsgroup-unreads)
3801               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3802           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3803             ;; This article was previously marked as read on account
3804             ;; of a low score, but now it has risen, so we mark it as
3805             ;; unread.
3806             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3807         (gnus-summary-update-mark
3808          (if (or (null gnus-summary-default-score)
3809                  (<= (abs (- score gnus-summary-default-score))
3810                      gnus-summary-zcore-fuzz))
3811              ?                          ;Whitespace
3812            (if (< score gnus-summary-default-score)
3813                gnus-score-below-mark gnus-score-over-mark))
3814          'score))
3815       ;; Do visual highlighting.
3816       (when (gnus-visual-p 'summary-highlight 'highlight)
3817         (gnus-summary-highlight-line)
3818         (gnus-run-hooks 'gnus-summary-update-hook)))))
3819
3820 (defvar gnus-tmp-new-adopts nil)
3821
3822 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3823   "Return the number of articles in THREAD.
3824 This may be 0 in some cases -- if none of the articles in
3825 the thread are to be displayed."
3826   (let* ((number
3827          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3828           (cond
3829            ((not (listp thread))
3830             1)
3831            ((and (consp thread) (cdr thread))
3832             (apply
3833              '+ 1 (mapcar
3834                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3835            ((null thread)
3836             1)
3837            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3838             1)
3839            (t 0))))
3840     (when (and level (zerop level) gnus-tmp-new-adopts)
3841       (incf number
3842             (apply '+ (mapcar
3843                        'gnus-summary-number-of-articles-in-thread
3844                        gnus-tmp-new-adopts))))
3845     (if char
3846         (if (> number 1) gnus-not-empty-thread-mark
3847           gnus-empty-thread-mark)
3848       number)))
3849
3850 (defsubst gnus-summary-line-message-size (head)
3851   "Return pretty-printed version of message size.
3852 This function is intended to be used in
3853 `gnus-summary-line-format-alist'."
3854   (let ((c (or (mail-header-chars head) -1)))
3855     (cond ((< c 0) "n/a")               ; chars not available
3856           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3857           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3858           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3859           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3860
3861 (defcustom gnus-user-date-format-alist
3862   '(((gnus-seconds-today) . "Today, %H:%M")
3863     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3864     (604800 . "%A %H:%M")               ; That's one week
3865     ((gnus-seconds-month) . "%A %d")
3866     ((gnus-seconds-year) . "%B %d")
3867     (t . "%b %d %Y"))                   ; This one is used when no other
3868                                         ; does match
3869   "Specifies date format depending on age of article.
3870 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3871 seconds) or a Lisp expression evaluating to a number.  When the age of
3872 the article is less than this number, then use `format-time-string'
3873 with the corresponding FORMAT for displaying the date of the article.
3874 If AGE is not a number or a Lisp expression evaluating to a
3875 non-number, then the corresponding FORMAT is used as a default value.
3876
3877 Note that the list is processed from the beginning, so it should be
3878 sorted by ascending AGE.  Also note that items following the first
3879 non-number AGE will be ignored.
3880
3881 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3882 and `gnus-seconds-year' in the AGE spec.  They return the number of
3883 seconds passed since the start of today, of this month, of this year,
3884 respectively."
3885   :version "24.1"
3886   :group 'gnus-summary-format
3887   :type '(alist :key-type sexp :value-type string))
3888
3889 (defun gnus-user-date (messy-date)
3890   "Format the messy-date according to `gnus-user-date-format-alist'.
3891 Returns \"  ?  \" if there's bad input or if another error occurs.
3892 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3893   (condition-case ()
3894       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3895              (now (gnus-float-time))
3896              ;;If we don't find something suitable we'll use this one
3897              (my-format "%b %d '%y"))
3898         (let* ((difference (- now messy-date))
3899                (templist gnus-user-date-format-alist)
3900                (top (eval (caar templist))))
3901           (while (if (numberp top) (< top difference) (not top))
3902             (progn
3903               (setq templist (cdr templist))
3904               (setq top (eval (caar templist)))))
3905           (if (stringp (cdr (car templist)))
3906               (setq my-format (cdr (car templist)))))
3907         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3908     (error "  ?   ")))
3909
3910 (defun gnus-summary-set-local-parameters (group)
3911   "Go through the local params of GROUP and set all variable specs in that list."
3912   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3913                                         ; really variables.
3914     (dolist (elem (gnus-group-find-parameter group))
3915       (and (consp elem)                 ; Has to be a cons.
3916            (consp (cdr elem))           ; The cdr has to be a list.
3917            (symbolp (car elem))         ; Has to be a symbol in there.
3918            (not (memq (car elem) vars))
3919            (ignore-errors
3920              (push (car elem) vars)
3921              ;; Variables like `gnus-show-threads' that are globally
3922              ;; bound, if used as group parameters, need to get to be
3923              ;; buffer-local, whereas just parameters like `gcc-self',
3924              ;; `timestamp', etc. should not be bound as variables.
3925              (if (boundp (car elem))
3926                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3927                (eval (nth 1 elem))))))))
3928
3929 (defun gnus-summary-read-group (group &optional show-all no-article
3930                                       kill-buffer no-display backward
3931                                       select-articles)
3932   "Start reading news in newsgroup GROUP.
3933 If SHOW-ALL is non-nil, already read articles are also listed.
3934 If NO-ARTICLE is non-nil, no article is selected initially.
3935 If NO-DISPLAY, don't generate the summary buffer contents.
3936 If KILL-BUFFER, it should be a buffer that's killed once the new
3937 summary buffer has been generated.
3938 If BACKWARD, move point to the previous group in the group buffer
3939 If SELECT-ARTICLES, only select those articles from GROUP."
3940   (let (result)
3941     (while (and group
3942                 (null (setq result
3943                             (let ((gnus-auto-select-next nil))
3944                               (or (gnus-summary-read-group-1
3945                                    group show-all no-article
3946                                    kill-buffer no-display
3947                                    select-articles)
3948                                   (setq show-all nil
3949                                         select-articles nil)))))
3950                 (eq gnus-auto-select-next 'quietly))
3951       (set-buffer gnus-group-buffer)
3952       ;; The entry function called above goes to the next
3953       ;; group automatically, so we go two groups back
3954       ;; if we are searching for the previous group.
3955       (when backward
3956         (gnus-group-prev-unread-group 2))
3957       (if (not (equal group (gnus-group-group-name)))
3958           (setq group (gnus-group-group-name))
3959         (setq group nil)))
3960     result))
3961
3962 (defun gnus-summary-read-group-1 (group show-all no-article
3963                                         kill-buffer no-display
3964                                         &optional select-articles)
3965   ;; Killed foreign groups can't be entered.
3966   ;;  (when (and (not (gnus-group-native-p group))
3967   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3968   ;;    (error "Dead non-native groups can't be entered"))
3969   (gnus-message 7 "Retrieving newsgroup: %s..."
3970                 (gnus-group-decoded-name group))
3971   (let* ((new-group (gnus-summary-setup-buffer group))
3972          (quit-config (gnus-group-quit-config group))
3973          (did-select (and new-group (gnus-select-newsgroup
3974                                      group show-all select-articles))))
3975     (cond
3976      ;; This summary buffer exists already, so we just select it.
3977      ((not new-group)
3978       (gnus-set-global-variables)
3979       (when kill-buffer
3980         (gnus-kill-or-deaden-summary kill-buffer))
3981       (gnus-configure-windows 'summary 'force)
3982       (gnus-set-mode-line 'summary)
3983       (gnus-summary-position-point)
3984       (message "")
3985       t)
3986      ;; We couldn't select this group.
3987      ((null did-select)
3988       (when (and (eq major-mode 'gnus-summary-mode)
3989                  (not (equal (current-buffer) kill-buffer)))
3990         (kill-buffer (current-buffer))
3991         (if (not quit-config)
3992             (progn
3993               ;; Update the info -- marks might need to be removed,
3994               ;; for instance.
3995               (gnus-summary-update-info)
3996               (set-buffer gnus-group-buffer)
3997               (gnus-group-jump-to-group group)
3998               (gnus-group-next-unread-group 1))
3999           (gnus-handle-ephemeral-exit quit-config)))
4000       (if (null (gnus-list-of-unread-articles group))
4001           (gnus-message 3 "Group %s contains no messages" group)
4002         (gnus-message 3 "Can't select group"))
4003       nil)
4004      ;; The user did a `C-g' while prompting for number of articles,
4005      ;; so we exit this group.
4006      ((eq did-select 'quit)
4007       (and (eq major-mode 'gnus-summary-mode)
4008            (not (equal (current-buffer) kill-buffer))
4009            (kill-buffer (current-buffer)))
4010       (when kill-buffer
4011         (gnus-kill-or-deaden-summary kill-buffer))
4012       (if (not quit-config)
4013           (progn
4014             (set-buffer gnus-group-buffer)
4015             (gnus-group-jump-to-group group)
4016             (gnus-configure-windows 'group 'force))
4017         (gnus-handle-ephemeral-exit quit-config))
4018       ;; Finally signal the quit.
4019       (signal 'quit nil))
4020      ;; The group was successfully selected.
4021      (t
4022       (gnus-set-global-variables)
4023       ;; Save the active value in effect when the group was entered.
4024       (setq gnus-newsgroup-active
4025             (gnus-copy-sequence
4026              (gnus-active gnus-newsgroup-name)))
4027       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4028       ;; You can change the summary buffer in some way with this hook.
4029       (gnus-run-hooks 'gnus-select-group-hook)
4030       (when (memq 'summary (gnus-update-format-specifications
4031                             nil 'summary 'summary-mode 'summary-dummy))
4032         ;; The format specification for the summary line was updated,
4033         ;; so we need to update the mark positions as well.
4034         (gnus-update-summary-mark-positions))
4035       ;; Do score processing.
4036       (when gnus-use-scoring
4037         (gnus-possibly-score-headers))
4038       ;; Check whether to fill in the gaps in the threads.
4039       (when gnus-build-sparse-threads
4040         (gnus-build-sparse-threads))
4041       ;; Find the initial limit.
4042       (if show-all
4043           (let ((gnus-newsgroup-dormant nil))
4044             (gnus-summary-initial-limit show-all))
4045         (gnus-summary-initial-limit show-all))
4046       ;; Generate the summary buffer.
4047       (unless no-display
4048         (gnus-summary-prepare))
4049       (when gnus-use-trees
4050         (gnus-tree-open group)
4051         (setq gnus-summary-highlight-line-function
4052               'gnus-tree-highlight-article))
4053       ;; If the summary buffer is empty, but there are some low-scored
4054       ;; articles or some excluded dormants, we include these in the
4055       ;; buffer.
4056       (when (and (zerop (buffer-size))
4057                  (not no-display))
4058         (cond (gnus-newsgroup-dormant
4059                (gnus-summary-limit-include-dormant))
4060               ((and gnus-newsgroup-scored show-all)
4061                (gnus-summary-limit-include-expunged t))))
4062       ;; Function `gnus-apply-kill-file' must be called in this hook.
4063       (gnus-run-hooks 'gnus-apply-kill-hook)
4064       (if (and (zerop (buffer-size))
4065                (not no-display))
4066           (progn
4067             ;; This newsgroup is empty.
4068             (gnus-summary-catchup-and-exit nil t)
4069             (gnus-message 6 "No unread news")
4070             (when kill-buffer
4071               (gnus-kill-or-deaden-summary kill-buffer))
4072             ;; Return nil from this function.
4073             nil)
4074         ;; Hide conversation thread subtrees.  We cannot do this in
4075         ;; gnus-summary-prepare-hook since kill processing may not
4076         ;; work with hidden articles.
4077         (gnus-summary-maybe-hide-threads)
4078         (gnus-configure-windows 'summary)
4079         (when kill-buffer
4080           (gnus-kill-or-deaden-summary kill-buffer))
4081         (gnus-summary-auto-select-subject)
4082         ;; Show first unread article if requested.
4083         (if (and (not no-article)
4084                  (not no-display)
4085                  gnus-newsgroup-unreads
4086                  gnus-auto-select-first)
4087             (progn
4088               (let ((art (gnus-summary-article-number)))
4089                 (unless (and (not gnus-plugged)
4090                              (or (memq art gnus-newsgroup-undownloaded)
4091                                  (memq art gnus-newsgroup-downloadable)))
4092                   (gnus-summary-goto-article art))))
4093           ;; Don't select any articles.
4094           (gnus-summary-position-point)
4095           (gnus-configure-windows 'summary 'force)
4096           (gnus-set-mode-line 'summary))
4097         (when (and gnus-auto-center-group
4098                    (get-buffer-window gnus-group-buffer t))
4099           ;; Gotta use windows, because recenter does weird stuff if
4100           ;; the current buffer ain't the displayed window.
4101           (let ((owin (selected-window)))
4102             (select-window (get-buffer-window gnus-group-buffer t))
4103             (when (gnus-group-goto-group group)
4104               (recenter))
4105             (select-window owin)))
4106         ;; Mark this buffer as "prepared".
4107         (setq gnus-newsgroup-prepared t)
4108         (gnus-run-hooks 'gnus-summary-prepared-hook)
4109         (unless (gnus-ephemeral-group-p group)
4110           (gnus-group-update-group group nil t))
4111         t)))))
4112
4113 (defun gnus-summary-auto-select-subject ()
4114   "Select the subject line on initial group entry."
4115   (goto-char (point-min))
4116   (cond
4117    ((eq gnus-auto-select-subject 'best)
4118     (gnus-summary-best-unread-subject))
4119    ((eq gnus-auto-select-subject 'unread)
4120     (gnus-summary-first-unread-subject))
4121    ((eq gnus-auto-select-subject 'unseen)
4122     (gnus-summary-first-unseen-subject))
4123    ((eq gnus-auto-select-subject 'unseen-or-unread)
4124     (gnus-summary-first-unseen-or-unread-subject))
4125    ((eq gnus-auto-select-subject 'first)
4126     ;; Do nothing.
4127     )
4128    ((functionp gnus-auto-select-subject)
4129     (funcall gnus-auto-select-subject))))
4130
4131 (defun gnus-summary-prepare ()
4132   "Generate the summary buffer."
4133   (interactive)
4134   (let ((inhibit-read-only t))
4135     (erase-buffer)
4136     (setq gnus-newsgroup-data nil
4137           gnus-newsgroup-data-reverse nil)
4138     (gnus-run-hooks 'gnus-summary-generate-hook)
4139     ;; Generate the buffer, either with threads or without.
4140     (when gnus-newsgroup-headers
4141       (gnus-summary-prepare-threads
4142        (if gnus-show-threads
4143            (gnus-sort-gathered-threads
4144             (funcall gnus-summary-thread-gathering-function
4145                      (gnus-sort-threads
4146                       (gnus-cut-threads (gnus-make-threads)))))
4147          ;; Unthreaded display.
4148          (gnus-sort-articles gnus-newsgroup-headers))))
4149     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4150     ;; Call hooks for modifying summary buffer.
4151     (goto-char (point-min))
4152     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4153
4154 (defsubst gnus-general-simplify-subject (subject)
4155   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4156   (setq subject
4157         (cond
4158          ;; Truncate the subject.
4159          (gnus-simplify-subject-functions
4160           (gnus-map-function gnus-simplify-subject-functions subject))
4161          ((numberp gnus-summary-gather-subject-limit)
4162           (setq subject (gnus-simplify-subject-re subject))
4163           (if (> (length subject) gnus-summary-gather-subject-limit)
4164               (substring subject 0 gnus-summary-gather-subject-limit)
4165             subject))
4166          ;; Fuzzily simplify it.
4167          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4168           (gnus-simplify-subject-fuzzy subject))
4169          ;; Just remove the leading "Re:".
4170          (t
4171           (gnus-simplify-subject-re subject))))
4172
4173   (if (and gnus-summary-gather-exclude-subject
4174            (string-match gnus-summary-gather-exclude-subject subject))
4175       nil                         ; This article shouldn't be gathered
4176     subject))
4177
4178 (defun gnus-summary-simplify-subject-query ()
4179   "Query where the respool algorithm would put this article."
4180   (interactive)
4181   (gnus-summary-select-article)
4182   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4183
4184 (defun gnus-gather-threads-by-subject (threads)
4185   "Gather threads by looking at Subject headers."
4186   (if (not gnus-summary-make-false-root)
4187       threads
4188     (let ((hashtb (gnus-make-hashtable 1024))
4189           (prev threads)
4190           (result threads)
4191           subject hthread whole-subject)
4192       (while threads
4193         (setq subject (gnus-general-simplify-subject
4194                        (setq whole-subject (mail-header-subject
4195                                             (caar threads)))))
4196         (when subject
4197           (if (setq hthread (gnus-gethash subject hashtb))
4198               (progn
4199                 ;; We enter a dummy root into the thread, if we
4200                 ;; haven't done that already.
4201                 (unless (stringp (caar hthread))
4202                   (setcar hthread (list whole-subject (car hthread))))
4203                 ;; We add this new gathered thread to this gathered
4204                 ;; thread.
4205                 (setcdr (car hthread)
4206                         (nconc (cdar hthread) (list (car threads))))
4207                 ;; Remove it from the list of threads.
4208                 (setcdr prev (cdr threads))
4209                 (setq threads prev))
4210             ;; Enter this thread into the hash table.
4211             (gnus-sethash subject
4212                           (if gnus-summary-make-false-root-always
4213                               (progn
4214                                 ;; If you want a dummy root above all
4215                                 ;; threads...
4216                                 (setcar threads (list whole-subject
4217                                                       (car threads)))
4218                                 threads)
4219                             threads)
4220                           hashtb)))
4221         (setq prev threads)
4222         (setq threads (cdr threads)))
4223       result)))
4224
4225 (defun gnus-gather-threads-by-references (threads)
4226   "Gather threads by looking at References headers."
4227   (let ((idhashtb (gnus-make-hashtable 1024))
4228         (thhashtb (gnus-make-hashtable 1024))
4229         (prev threads)
4230         (result threads)
4231         ids references id gthread gid entered ref)
4232     (while threads
4233       (when (setq references (mail-header-references (caar threads)))
4234         (setq id (mail-header-id (caar threads))
4235               ids (inline (gnus-split-references references))
4236               entered nil)
4237         (while (setq ref (pop ids))
4238           (setq ids (delete ref ids))
4239           (if (not (setq gid (gnus-gethash ref idhashtb)))
4240               (progn
4241                 (gnus-sethash ref id idhashtb)
4242                 (gnus-sethash id threads thhashtb))
4243             (setq gthread (gnus-gethash gid thhashtb))
4244             (unless entered
4245               ;; We enter a dummy root into the thread, if we
4246               ;; haven't done that already.
4247               (unless (stringp (caar gthread))
4248                 (setcar gthread (list (mail-header-subject (caar gthread))
4249                                       (car gthread))))
4250               ;; We add this new gathered thread to this gathered
4251               ;; thread.
4252               (setcdr (car gthread)
4253                       (nconc (cdar gthread) (list (car threads)))))
4254             ;; Add it into the thread hash table.
4255             (gnus-sethash id gthread thhashtb)
4256             (setq entered t)
4257             ;; Remove it from the list of threads.
4258             (setcdr prev (cdr threads))
4259             (setq threads prev))))
4260       (setq prev threads)
4261       (setq threads (cdr threads)))
4262     result))
4263
4264 (defun gnus-sort-gathered-threads (threads)
4265   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4266   (let ((result threads))
4267     (while threads
4268       (when (stringp (caar threads))
4269         (setcdr (car threads)
4270                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4271       (setq threads (cdr threads)))
4272     result))
4273
4274 (defun gnus-thread-loop-p (root thread)
4275   "Say whether ROOT is in THREAD."
4276   (let ((stack (list thread))
4277         (infloop 0)
4278         th)
4279     (while (setq thread (pop stack))
4280       (setq th (cdr thread))
4281       (while (and th
4282                   (not (eq (caar th) root)))
4283         (pop th))
4284       (if th
4285           ;; We have found a loop.
4286           (let (ref-dep)
4287             (setcdr thread (delq (car th) (cdr thread)))
4288             (if (boundp (setq ref-dep (intern "none"
4289                                               gnus-newsgroup-dependencies)))
4290                 (setcdr (symbol-value ref-dep)
4291                         (nconc (cdr (symbol-value ref-dep))
4292                                (list (car th))))
4293               (set ref-dep (list nil (car th))))
4294             (setq infloop 1
4295                   stack nil))
4296         ;; Push all the subthreads onto the stack.
4297         (push (cdr thread) stack)))
4298     infloop))
4299
4300 (defun gnus-make-threads ()
4301   "Go through the dependency hashtb and find the roots.  Return all threads."
4302   (let (threads)
4303     (while (catch 'infloop
4304              (mapatoms
4305               (lambda (refs)
4306                 ;; Deal with self-referencing References loops.
4307                 (when (and (car (symbol-value refs))
4308                            (not (zerop
4309                                  (apply
4310                                   '+
4311                                   (mapcar
4312                                    (lambda (thread)
4313                                      (gnus-thread-loop-p
4314                                       (car (symbol-value refs)) thread))
4315                                    (cdr (symbol-value refs)))))))
4316                   (setq threads nil)
4317                   (throw 'infloop t))
4318                 (unless (car (symbol-value refs))
4319                   ;; These threads do not refer back to any other
4320                   ;; articles, so they're roots.
4321                   (setq threads (append (cdr (symbol-value refs)) threads))))
4322               gnus-newsgroup-dependencies)))
4323     threads))
4324
4325 ;; Build the thread tree.
4326 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4327   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4328
4329 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4330 if it was already present.
4331
4332 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4333 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4334 Message-IDs will be renamed to a unique Message-ID before being
4335 entered.
4336
4337 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4338   (let* ((id (mail-header-id header))
4339          (id-dep (and id (intern id dependencies)))
4340          parent-id ref ref-dep ref-header replaced)
4341     ;; Enter this `header' in the `dependencies' table.
4342     (cond
4343      ((not id-dep)
4344       (setq header nil))
4345      ;; The first two cases do the normal part: enter a new `header'
4346      ;; in the `dependencies' table.
4347      ((not (boundp id-dep))
4348       (set id-dep (list header)))
4349      ((null (car (symbol-value id-dep)))
4350       (setcar (symbol-value id-dep) header))
4351
4352      ;; From here the `header' was already present in the
4353      ;; `dependencies' table.
4354      (force-new
4355       ;; Overrides an existing entry;
4356       ;; just set the header part of the entry.
4357       (setcar (symbol-value id-dep) header)
4358       (setq replaced t))
4359
4360      ;; Renames the existing `header' to a unique Message-ID.
4361      ((not gnus-summary-ignore-duplicates)
4362       ;; An article with this Message-ID has already been seen.
4363       ;; We rename the Message-ID.
4364       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4365            (list header))
4366       (mail-header-set-id header id))
4367
4368      ;; The last case ignores an existing entry, except it adds any
4369      ;; additional Xrefs (in case the two articles came from different
4370      ;; servers.
4371      ;; Also sets `header' to `nil' meaning that the `dependencies'
4372      ;; table was *not* modified.
4373      (t
4374       (mail-header-set-xref
4375        (car (symbol-value id-dep))
4376        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4377                    "")
4378                (or (mail-header-xref header) "")))
4379       (setq header nil)))
4380
4381     (when (and header (not replaced))
4382       ;; First check that we are not creating a References loop.
4383       (setq parent-id (gnus-parent-id (mail-header-references header)))
4384       (setq ref parent-id)
4385       (while (and ref
4386                   (setq ref-dep (intern-soft ref dependencies))
4387                   (boundp ref-dep)
4388                   (setq ref-header (car (symbol-value ref-dep))))
4389         (if (string= id ref)
4390             ;; Yuk!  This is a reference loop.  Make the article be a
4391             ;; root article.
4392             (progn
4393               (mail-header-set-references (car (symbol-value id-dep)) "none")
4394               (setq ref nil)
4395               (setq parent-id nil))
4396           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4397       (setq ref-dep (intern (or parent-id "none") dependencies))
4398       (if (boundp ref-dep)
4399           (setcdr (symbol-value ref-dep)
4400                   (nconc (cdr (symbol-value ref-dep))
4401                          (list (symbol-value id-dep))))
4402         (set ref-dep (list nil (symbol-value id-dep)))))
4403     header))
4404
4405 (defun gnus-extract-message-id-from-in-reply-to (string)
4406   (if (string-match "<[^>]+>" string)
4407       (substring string (match-beginning 0) (match-end 0))
4408     nil))
4409
4410 (defun gnus-build-sparse-threads ()
4411   (let ((headers gnus-newsgroup-headers)
4412         (mail-parse-charset gnus-newsgroup-charset)
4413         (gnus-summary-ignore-duplicates t)
4414         header references generation relations
4415         subject child end new-child date)
4416     ;; First we create an alist of generations/relations, where
4417     ;; generations is how much we trust the relation, and the relation
4418     ;; is parent/child.
4419     (gnus-message 7 "Making sparse threads...")
4420     (save-excursion
4421       (nnheader-set-temp-buffer " *gnus sparse threads*")
4422       (while (setq header (pop headers))
4423         (when (and (setq references (mail-header-references header))
4424                    (not (string= references "")))
4425           (insert references)
4426           (setq child (mail-header-id header)
4427                 subject (mail-header-subject header)
4428                 date (mail-header-date header)
4429                 generation 0)
4430           (while (search-backward ">" nil t)
4431             (setq end (1+ (point)))
4432             (when (search-backward "<" nil t)
4433               (setq new-child (buffer-substring (point) end))
4434               (push (list (incf generation)
4435                           child (setq child new-child)
4436                           subject date)
4437                     relations)))
4438           (when child
4439             (push (list (1+ generation) child nil subject) relations))
4440           (erase-buffer)))
4441       (kill-buffer (current-buffer)))
4442     ;; Sort over trustworthiness.
4443     (dolist (relation (sort relations 'car-less-than-car))
4444       (when (gnus-dependencies-add-header
4445              (make-full-mail-header
4446               gnus-reffed-article-number
4447               (nth 3 relation) "" (or (nth 4 relation) "")
4448               (nth 1 relation)
4449               (or (nth 2 relation) "") 0 0 "")
4450              gnus-newsgroup-dependencies nil)
4451         (push gnus-reffed-article-number gnus-newsgroup-limit)
4452         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4453         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4454               gnus-newsgroup-reads)
4455         (decf gnus-reffed-article-number)))
4456     (gnus-message 7 "Making sparse threads...done")))
4457
4458 (defun gnus-build-old-threads ()
4459   ;; Look at all the articles that refer back to old articles, and
4460   ;; fetch the headers for the articles that aren't there.  This will
4461   ;; build complete threads - if the roots haven't been expired by the
4462   ;; server, that is.
4463   (let ((mail-parse-charset gnus-newsgroup-charset)
4464         id heads)
4465     (mapatoms
4466      (lambda (refs)
4467        (when (not (car (symbol-value refs)))
4468          (setq heads (cdr (symbol-value refs)))
4469          (while heads
4470            (if (memq (mail-header-number (caar heads))
4471                      gnus-newsgroup-dormant)
4472                (setq heads (cdr heads))
4473              (setq id (symbol-name refs))
4474              (while (and (setq id (gnus-build-get-header id))
4475                          (not (car (gnus-id-to-thread id)))))
4476              (setq heads nil)))))
4477      gnus-newsgroup-dependencies)))
4478
4479 (defsubst gnus-remove-odd-characters (string)
4480   "Translate STRING into something that doesn't contain weird characters."
4481   (mm-subst-char-in-string
4482    ?\r ?\-
4483    (mm-subst-char-in-string ?\n ?\- string t) t))
4484
4485 ;; This function has to be called with point after the article number
4486 ;; on the beginning of the line.
4487 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4488   (let ((eol (point-at-eol))
4489         (buffer (current-buffer))
4490         header references in-reply-to)
4491
4492     ;; overview: [num subject from date id refs chars lines misc]
4493     (unwind-protect
4494         (let (x)
4495           (narrow-to-region (point) eol)
4496           (unless (eobp)
4497             (forward-char))
4498
4499           (setq header
4500                 (make-full-mail-header
4501                  number                 ; number
4502                  (condition-case ()     ; subject
4503                      (gnus-remove-odd-characters
4504                       (funcall gnus-decode-encoded-word-function
4505                                (setq x (nnheader-nov-field))))
4506                    (error x))
4507                  (condition-case ()     ; from
4508                      (gnus-remove-odd-characters
4509                       (funcall gnus-decode-encoded-address-function
4510                                (setq x (nnheader-nov-field))))
4511                    (error x))
4512                  (nnheader-nov-field)   ; date
4513                  (nnheader-nov-read-message-id number)  ; id
4514                  (setq references (nnheader-nov-field)) ; refs
4515                  (nnheader-nov-read-integer) ; chars
4516                  (nnheader-nov-read-integer) ; lines
4517                  (unless (eobp)
4518                    (if (looking-at "Xref: ")
4519                        (goto-char (match-end 0)))
4520                    (nnheader-nov-field)) ; Xref
4521                  (nnheader-nov-parse-extra)))) ; extra
4522
4523       (widen))
4524
4525     (when (and (string= references "")
4526                (setq in-reply-to (mail-header-extra header))
4527                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4528       (mail-header-set-references
4529        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4530
4531     (when gnus-alter-header-function
4532       (funcall gnus-alter-header-function header))
4533     (gnus-dependencies-add-header header dependencies force-new)))
4534
4535 (defun gnus-build-get-header (id)
4536   "Look through the buffer of NOV lines and find the header to ID.
4537 Enter this line into the dependencies hash table, and return
4538 the id of the parent article (if any)."
4539   (let ((deps gnus-newsgroup-dependencies)
4540         found header)
4541     (prog1
4542         (with-current-buffer nntp-server-buffer
4543           (let ((case-fold-search nil))
4544             (goto-char (point-min))
4545             (while (and (not found)
4546                         (search-forward id nil t))
4547               (beginning-of-line)
4548               (setq found (looking-at
4549                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4550                                    (regexp-quote id))))
4551               (or found (beginning-of-line 2)))
4552             (when found
4553               (beginning-of-line)
4554               (and
4555                (setq header (gnus-nov-parse-line
4556                              (read (current-buffer)) deps))
4557                (gnus-parent-id (mail-header-references header))))))
4558       (when header
4559         (let ((number (mail-header-number header)))
4560           (push number gnus-newsgroup-limit)
4561           (push header gnus-newsgroup-headers)
4562           (if (memq number gnus-newsgroup-unselected)
4563               (progn
4564                 (setq gnus-newsgroup-unreads
4565                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4566                                                number))
4567                 (setq gnus-newsgroup-unselected
4568                       (delq number gnus-newsgroup-unselected)))
4569             (push number gnus-newsgroup-ancient)))))))
4570
4571 (defun gnus-build-all-threads ()
4572   "Read all the headers."
4573   (let ((gnus-summary-ignore-duplicates t)
4574         (mail-parse-charset gnus-newsgroup-charset)
4575         (dependencies gnus-newsgroup-dependencies)
4576         header article)
4577     (with-current-buffer nntp-server-buffer
4578       (let ((case-fold-search nil))
4579         (goto-char (point-min))
4580         (while (not (eobp))
4581           (ignore-errors
4582             (setq article (read (current-buffer))
4583                   header (gnus-nov-parse-line article dependencies t)))
4584           (when header
4585             (with-current-buffer gnus-summary-buffer
4586               (push header gnus-newsgroup-headers)
4587               (if (memq (setq article (mail-header-number header))
4588                         gnus-newsgroup-unselected)
4589                   (progn
4590                     (setq gnus-newsgroup-unreads
4591                           (gnus-add-to-sorted-list
4592                            gnus-newsgroup-unreads article))
4593                     (setq gnus-newsgroup-unselected
4594                           (delq article gnus-newsgroup-unselected)))
4595                 (push article gnus-newsgroup-ancient)))
4596             (forward-line 1)))))))
4597
4598 (defun gnus-summary-update-article-line (article header)
4599   "Update the line for ARTICLE using HEADER."
4600   (let* ((id (mail-header-id header))
4601          (thread (gnus-id-to-thread id)))
4602     (unless thread
4603       (error "Article in no thread"))
4604     ;; Update the thread.
4605     (setcar thread header)
4606     (gnus-summary-goto-subject article)
4607     (let* ((datal (gnus-data-find-list article))
4608            (data (car datal))
4609            (inhibit-read-only t)
4610            (level (gnus-summary-thread-level)))
4611       (gnus-delete-line)
4612       (let ((inserted (- (point)
4613                          (progn
4614                            (gnus-summary-insert-line
4615                             header level nil
4616                             (memq article gnus-newsgroup-undownloaded)
4617                             (gnus-article-mark article)
4618                             (memq article gnus-newsgroup-replied)
4619                             (memq article gnus-newsgroup-expirable)
4620                             ;; Only insert the Subject string when it's different
4621                             ;; from the previous Subject string.
4622                             (if (and
4623                                  gnus-show-threads
4624                                  (gnus-subject-equal
4625                                   (condition-case ()
4626                                       (mail-header-subject
4627                                        (gnus-data-header
4628                                         (cadr
4629                                          (gnus-data-find-list
4630                                           article
4631                                           (gnus-data-list t)))))
4632                                     ;; Error on the side of excessive subjects.
4633                                     (error ""))
4634                                   (mail-header-subject header)))
4635                                 ""
4636                               (mail-header-subject header))
4637                             nil (cdr (assq article gnus-newsgroup-scored))
4638                             (memq article gnus-newsgroup-processable))
4639                            (point)))))
4640         (when (cdr datal)
4641           (gnus-data-update-list
4642            (cdr datal)
4643            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4644
4645 (defun gnus-summary-update-article (article &optional iheader)
4646   "Update ARTICLE in the summary buffer."
4647   (set-buffer gnus-summary-buffer)
4648   (let* ((header (gnus-summary-article-header article))
4649          (id (mail-header-id header))
4650          (data (gnus-data-find article))
4651          (thread (gnus-id-to-thread id))
4652          (references (mail-header-references header))
4653          (parent
4654           (gnus-id-to-thread
4655            (or (gnus-parent-id
4656                 (when (and references
4657                            (not (equal "" references)))
4658                   references))
4659                "none")))
4660          (inhibit-read-only t)
4661          (old (car thread)))
4662     (when thread
4663       (unless iheader
4664         (setcar thread nil)
4665         (when parent
4666           (delq thread parent)))
4667       (if (gnus-summary-insert-subject id header)
4668           ;; Set the (possibly) new article number in the data structure.
4669           (gnus-data-set-number data (gnus-id-to-article id))
4670         (setcar thread old)
4671         nil))))
4672
4673 (defun gnus-rebuild-thread (id &optional line)
4674   "Rebuild the thread containing ID.
4675 If LINE, insert the rebuilt thread starting on line LINE."
4676   (let ((inhibit-read-only t)
4677         old-pos current thread data)
4678     (if (not gnus-show-threads)
4679         (setq thread (list (car (gnus-id-to-thread id))))
4680       ;; Get the thread this article is part of.
4681       (setq thread (gnus-remove-thread id)))
4682     (setq old-pos (point-at-bol))
4683     (setq current (save-excursion
4684                     (and (re-search-backward "[\r\n]" nil t)
4685                          (gnus-summary-article-number))))
4686     ;; If this is a gathered thread, we have to go some re-gathering.
4687     (when (stringp (car thread))
4688       (let ((subject (car thread))
4689             roots thr)
4690         (setq thread (cdr thread))
4691         (while thread
4692           (unless (memq (setq thr (gnus-id-to-thread
4693                                    (gnus-root-id
4694                                     (mail-header-id (caar thread)))))
4695                         roots)
4696             (push thr roots))
4697           (setq thread (cdr thread)))
4698         ;; We now have all (unique) roots.
4699         (if (= (length roots) 1)
4700             ;; All the loose roots are now one solid root.
4701             (setq thread (car roots))
4702           (setq thread (cons subject (gnus-sort-threads roots))))))
4703     (let (threads)
4704       ;; We then insert this thread into the summary buffer.
4705       (when line
4706         (goto-char (point-min))
4707         (forward-line (1- line)))
4708       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4709         (if gnus-show-threads
4710             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4711           (gnus-summary-prepare-unthreaded thread))
4712         (setq data (nreverse gnus-newsgroup-data))
4713         (setq threads gnus-newsgroup-threads))
4714       ;; We splice the new data into the data structure.
4715       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4716       ;;!!! then we want to insert at the beginning of the buffer.
4717       ;;!!! That happens to be true with Gnus now, but that may
4718       ;;!!! change in the future.  Perhaps.
4719       (gnus-data-enter-list
4720        (if line nil current) data (- (point) old-pos))
4721       (setq gnus-newsgroup-threads
4722             (nconc threads gnus-newsgroup-threads))
4723       (gnus-data-compute-positions))))
4724
4725 (defun gnus-number-to-header (number)
4726   "Return the header for article NUMBER."
4727   (let ((headers gnus-newsgroup-headers))
4728     (while (and headers
4729                 (not (= number (mail-header-number (car headers)))))
4730       (pop headers))
4731     (when headers
4732       (car headers))))
4733
4734 (defun gnus-parent-headers (in-headers &optional generation)
4735   "Return the headers of the GENERATIONth parent of HEADERS."
4736   (unless generation
4737     (setq generation 1))
4738   (let ((parent t)
4739         (headers in-headers)
4740         references)
4741     (while (and parent
4742                 (not (zerop generation))
4743                 (setq references (mail-header-references headers)))
4744       (setq headers (if (and references
4745                              (setq parent (gnus-parent-id references)))
4746                         (car (gnus-id-to-thread parent))
4747                       nil))
4748       (decf generation))
4749     (and (not (eq headers in-headers))
4750          headers)))
4751
4752 (defun gnus-id-to-thread (id)
4753   "Return the (sub-)thread where ID appears."
4754   (gnus-gethash id gnus-newsgroup-dependencies))
4755
4756 (defun gnus-id-to-article (id)
4757   "Return the article number of ID."
4758   (let ((thread (gnus-id-to-thread id)))
4759     (when (and thread
4760                (car thread))
4761       (mail-header-number (car thread)))))
4762
4763 (defun gnus-id-to-header (id)
4764   "Return the article headers of ID."
4765   (car (gnus-id-to-thread id)))
4766
4767 (defun gnus-article-displayed-root-p (article)
4768   "Say whether ARTICLE is a root(ish) article."
4769   (let ((level (gnus-summary-thread-level article))
4770         (refs (mail-header-references  (gnus-summary-article-header article)))
4771         particle)
4772     (cond
4773      ((null level) nil)
4774      ((zerop level) t)
4775      ((null refs) t)
4776      ((null (gnus-parent-id refs)) t)
4777      ((and (= 1 level)
4778            (null (setq particle (gnus-id-to-article
4779                                  (gnus-parent-id refs))))
4780            (null (gnus-summary-thread-level particle)))))))
4781
4782 (defun gnus-root-id (id)
4783   "Return the id of the root of the thread where ID appears."
4784   (let (last-id prev)
4785     (while (and id (setq prev (car (gnus-id-to-thread id))))
4786       (setq last-id id
4787             id (gnus-parent-id (mail-header-references prev))))
4788     last-id))
4789
4790 (defun gnus-articles-in-thread (thread)
4791   "Return the list of articles in THREAD."
4792   (cons (mail-header-number (car thread))
4793         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4794
4795 (defun gnus-remove-thread (id &optional dont-remove)
4796   "Remove the thread that has ID in it."
4797   (let (headers thread last-id)
4798     ;; First go up in this thread until we find the root.
4799     (setq last-id (gnus-root-id id)
4800           headers (message-flatten-list (gnus-id-to-thread last-id)))
4801     ;; We have now found the real root of this thread.  It might have
4802     ;; been gathered into some loose thread, so we have to search
4803     ;; through the threads to find the thread we wanted.
4804     (let ((threads gnus-newsgroup-threads)
4805           sub)
4806       (while threads
4807         (setq sub (car threads))
4808         (if (stringp (car sub))
4809             ;; This is a gathered thread, so we look at the roots
4810             ;; below it to find whether this article is in this
4811             ;; gathered root.
4812             (progn
4813               (setq sub (cdr sub))
4814               (while sub
4815                 (when (member (caar sub) headers)
4816                   (setq thread (car threads)
4817                         threads nil
4818                         sub nil))
4819                 (setq sub (cdr sub))))
4820           ;; It's an ordinary thread, so we check it.
4821           (when (eq (car sub) (car headers))
4822             (setq thread sub
4823                   threads nil)))
4824         (setq threads (cdr threads)))
4825       ;; If this article is in no thread, then it's a root.
4826       (if thread
4827           (unless dont-remove
4828             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4829         (setq thread (gnus-id-to-thread last-id)))
4830       (when thread
4831         (prog1
4832             thread                      ; We return this thread.
4833           (unless dont-remove
4834             (if (stringp (car thread))
4835                 (progn
4836                   ;; If we use dummy roots, then we have to remove the
4837                   ;; dummy root as well.
4838                   (when (eq gnus-summary-make-false-root 'dummy)
4839                     ;; We go to the dummy root by going to
4840                     ;; the first sub-"thread", and then one line up.
4841                     (gnus-summary-goto-article
4842                      (mail-header-number (caadr thread)))
4843                     (forward-line -1)
4844                     (gnus-delete-line)
4845                     (gnus-data-compute-positions))
4846                   (setq thread (cdr thread))
4847                   (while thread
4848                     (gnus-remove-thread-1 (car thread))
4849                     (setq thread (cdr thread))))
4850               (gnus-remove-thread-1 thread))))))))
4851
4852 (defun gnus-remove-thread-1 (thread)
4853   "Remove the thread THREAD recursively."
4854   (let ((number (mail-header-number (pop thread)))
4855         d)
4856     (setq thread (reverse thread))
4857     (while thread
4858       (gnus-remove-thread-1 (pop thread)))
4859     (when (setq d (gnus-data-find number))
4860       (goto-char (gnus-data-pos d))
4861       (gnus-summary-show-thread)
4862       (gnus-data-remove
4863        number
4864        (- (point-at-bol)
4865           (prog1
4866               (1+ (point-at-eol))
4867             (gnus-delete-line)))))))
4868
4869 (defun gnus-sort-threads-recursive (threads func)
4870   (sort (mapcar (lambda (thread)
4871                   (cons (car thread)
4872                         (and (cdr thread)
4873                              (gnus-sort-threads-recursive (cdr thread) func))))
4874                 threads) func))
4875
4876 (defun gnus-sort-threads-loop (threads func)
4877   (let* ((superthread (cons nil threads))
4878          (stack (list (cons superthread threads)))
4879          remaining-threads thread)
4880     (while stack
4881       (setq remaining-threads (cdr (car stack)))
4882       (if remaining-threads
4883           (progn (setq thread (car remaining-threads))
4884                  (setcdr (car stack) (cdr remaining-threads))
4885                  (if (cdr thread)
4886                      (push (cons thread (cdr thread)) stack)))
4887         (setq thread (caar stack))
4888         (setcdr thread (sort (cdr thread) func))
4889         (pop stack)))
4890     (cdr superthread)))
4891
4892 (defun gnus-sort-threads (threads)
4893   "Sort THREADS."
4894   (if (not gnus-thread-sort-functions)
4895       threads
4896     (gnus-message 8 "Sorting threads...")
4897     (prog1
4898         (condition-case nil
4899             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4900               (gnus-sort-threads-recursive
4901                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4902           ;; Even after binding max-lisp-eval-depth, the recursive
4903           ;; sorter might fail for very long threads.  In that case,
4904           ;; try using a (less well-tested) non-recursive sorter.
4905           (error (gnus-message 9 "Sorting threads with loop...")
4906                  (gnus-sort-threads-loop
4907                   threads (gnus-make-sort-function
4908                            gnus-thread-sort-functions))))
4909       (gnus-message 8 "Sorting threads...done"))))
4910
4911 (defun gnus-sort-articles (articles)
4912   "Sort ARTICLES."
4913   (when gnus-article-sort-functions
4914     (gnus-message 7 "Sorting articles...")
4915     (prog1
4916         (setq gnus-newsgroup-headers
4917               (sort articles (gnus-make-sort-function
4918                               gnus-article-sort-functions)))
4919       (gnus-message 7 "Sorting articles...done"))))
4920
4921 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4922 (defmacro gnus-thread-header (thread)
4923   "Return header of first article in THREAD.
4924 Note that THREAD must never, ever be anything else than a variable -
4925 using some other form will lead to serious barfage."
4926   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4927   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4928   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4929         (vector thread) 2))
4930
4931 (defsubst gnus-article-sort-by-number (h1 h2)
4932   "Sort articles by article number."
4933   (< (mail-header-number h1)
4934      (mail-header-number h2)))
4935
4936 (defun gnus-thread-sort-by-number (h1 h2)
4937   "Sort threads by root article number."
4938   (gnus-article-sort-by-number
4939    (gnus-thread-header h1) (gnus-thread-header h2)))
4940
4941 (defsubst gnus-article-sort-by-random (h1 h2)
4942   "Sort articles randomly."
4943   (zerop (random 2)))
4944
4945 (defun gnus-thread-sort-by-random (h1 h2)
4946   "Sort threads randomly."
4947   (gnus-article-sort-by-random
4948    (gnus-thread-header h1) (gnus-thread-header h2)))
4949
4950 (defsubst gnus-article-sort-by-lines (h1 h2)
4951   "Sort articles by article Lines header."
4952   (< (mail-header-lines h1)
4953      (mail-header-lines h2)))
4954
4955 (defun gnus-thread-sort-by-lines (h1 h2)
4956   "Sort threads by root article Lines header."
4957   (gnus-article-sort-by-lines
4958    (gnus-thread-header h1) (gnus-thread-header h2)))
4959
4960 (defsubst gnus-article-sort-by-chars (h1 h2)
4961   "Sort articles by octet length."
4962   (< (mail-header-chars h1)
4963      (mail-header-chars h2)))
4964
4965 (defun gnus-thread-sort-by-chars (h1 h2)
4966   "Sort threads by root article octet length."
4967   (gnus-article-sort-by-chars
4968    (gnus-thread-header h1) (gnus-thread-header h2)))
4969
4970 (defsubst gnus-article-sort-by-author (h1 h2)
4971   "Sort articles by root author."
4972   (gnus-string<
4973    (let ((extract (funcall
4974                    gnus-extract-address-components
4975                    (mail-header-from h1))))
4976      (or (car extract) (cadr extract) ""))
4977    (let ((extract (funcall
4978                    gnus-extract-address-components
4979                    (mail-header-from h2))))
4980      (or (car extract) (cadr extract) ""))))
4981
4982 (defun gnus-thread-sort-by-author (h1 h2)
4983   "Sort threads by root author."
4984   (gnus-article-sort-by-author
4985    (gnus-thread-header h1)  (gnus-thread-header h2)))
4986
4987 (defsubst gnus-article-sort-by-recipient (h1 h2)
4988   "Sort articles by recipient."
4989   (gnus-string<
4990    (let ((extract (funcall
4991                    gnus-extract-address-components
4992                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4993      (or (car extract) (cadr extract)))
4994    (let ((extract (funcall
4995                    gnus-extract-address-components
4996                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4997      (or (car extract) (cadr extract)))))
4998
4999 (defun gnus-thread-sort-by-recipient (h1 h2)
5000   "Sort threads by root recipient."
5001   (gnus-article-sort-by-recipient
5002    (gnus-thread-header h1) (gnus-thread-header h2)))
5003
5004 (defsubst gnus-article-sort-by-subject (h1 h2)
5005   "Sort articles by root subject."
5006   (gnus-string<
5007    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5008    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5009
5010 (defun gnus-thread-sort-by-subject (h1 h2)
5011   "Sort threads by root subject."
5012   (gnus-article-sort-by-subject
5013    (gnus-thread-header h1) (gnus-thread-header h2)))
5014
5015 (defsubst gnus-article-sort-by-date (h1 h2)
5016   "Sort articles by root article date."
5017   (time-less-p
5018    (gnus-date-get-time (mail-header-date h1))
5019    (gnus-date-get-time (mail-header-date h2))))
5020
5021 (defun gnus-thread-sort-by-date (h1 h2)
5022   "Sort threads by root article date."
5023   (gnus-article-sort-by-date
5024    (gnus-thread-header h1) (gnus-thread-header h2)))
5025
5026 (defsubst gnus-article-sort-by-score (h1 h2)
5027   "Sort articles by root article score.
5028 Unscored articles will be counted as having a score of zero."
5029   (> (or (cdr (assq (mail-header-number h1)
5030                     gnus-newsgroup-scored))
5031          gnus-summary-default-score 0)
5032      (or (cdr (assq (mail-header-number h2)
5033                     gnus-newsgroup-scored))
5034          gnus-summary-default-score 0)))
5035
5036 (defun gnus-thread-sort-by-score (h1 h2)
5037   "Sort threads by root article score."
5038   (gnus-article-sort-by-score
5039    (gnus-thread-header h1) (gnus-thread-header h2)))
5040
5041 (defun gnus-thread-sort-by-total-score (h1 h2)
5042   "Sort threads by the sum of all scores in the thread.
5043 Unscored articles will be counted as having a score of zero."
5044   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5045
5046 (defun gnus-thread-total-score (thread)
5047   ;; This function find the total score of THREAD.
5048   (cond
5049    ((null thread)
5050     0)
5051    ((consp thread)
5052     (if (stringp (car thread))
5053         (apply gnus-thread-score-function 0
5054                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5055       (gnus-thread-total-score-1 thread)))
5056    (t
5057     (gnus-thread-total-score-1 (list thread)))))
5058
5059 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5060   "Sort articles by number."
5061   (gnus-article-sort-by-number h1 h2))
5062
5063 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5064   "Sort threads such that the thread with the most recently arrived article comes first."
5065   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5066
5067 (defun gnus-thread-highest-number (thread)
5068   "Return the highest article number in THREAD."
5069   (apply 'max (mapcar (lambda (header)
5070                         (mail-header-number header))
5071                       (message-flatten-list thread))))
5072
5073 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5074   "Sort articles by number."
5075   (gnus-article-sort-by-date h1 h2))
5076
5077 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5078   "Sort threads such that the thread with the most recently dated article comes first."
5079   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5080
5081 ; Since this is called not only to sort the top-level threads, but
5082 ; also in recursive sorts to order the articles within a thread, each
5083 ; article will be processed many times.  Thus it speeds things up
5084 ; quite a bit to use gnus-date-get-time, which caches the time value.
5085 (defun gnus-thread-latest-date (thread)
5086   "Return the highest article date in THREAD."
5087   (apply 'max
5088          (mapcar (lambda (header) (gnus-float-time
5089                                    (gnus-date-get-time
5090                                     (mail-header-date header))))
5091                  (message-flatten-list thread))))
5092
5093 (defun gnus-thread-total-score-1 (root)
5094   ;; This function find the total score of the thread below ROOT.
5095   (setq root (car root))
5096   (apply gnus-thread-score-function
5097          (or (append
5098               (mapcar 'gnus-thread-total-score
5099                       (cdr (gnus-id-to-thread (mail-header-id root))))
5100               (when (> (mail-header-number root) 0)
5101                 (list (or (cdr (assq (mail-header-number root)
5102                                      gnus-newsgroup-scored))
5103                           gnus-summary-default-score 0))))
5104              (list gnus-summary-default-score)
5105              '(0))))
5106
5107 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5108 (defvar gnus-tmp-prev-subject nil)
5109 (defvar gnus-tmp-false-parent nil)
5110 (defvar gnus-tmp-root-expunged nil)
5111 (defvar gnus-tmp-dummy-line nil)
5112
5113 (defun gnus-extra-header (type &optional header)
5114   "Return the extra header of TYPE."
5115   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5116       ""))
5117
5118 (defvar gnus-tmp-thread-tree-header-string "")
5119
5120 (defcustom gnus-sum-thread-tree-root "> "
5121   "With %B spec, used for the root of a thread.
5122 If nil, use subject instead."
5123   :version "22.1"
5124   :type '(radio (const :format "%v  " nil) string)
5125   :group 'gnus-thread)
5126
5127 (defcustom gnus-sum-thread-tree-false-root "> "
5128   "With %B spec, used for a false root of a thread.
5129 If nil, use subject instead."
5130   :version "22.1"
5131   :type '(radio (const :format "%v  " nil) string)
5132   :group 'gnus-thread)
5133
5134 (defcustom gnus-sum-thread-tree-single-indent ""
5135   "With %B spec, used for a thread with just one message.
5136 If nil, use subject instead."
5137   :version "22.1"
5138   :type '(radio (const :format "%v  " nil) string)
5139   :group 'gnus-thread)
5140
5141 (defcustom gnus-sum-thread-tree-vertical "| "
5142   "With %B spec, used for drawing a vertical line."
5143   :version "22.1"
5144   :type 'string
5145   :group 'gnus-thread)
5146
5147 (defcustom gnus-sum-thread-tree-indent "  "
5148   "With %B spec, used for indenting."
5149   :version "22.1"
5150   :type 'string
5151   :group 'gnus-thread)
5152
5153 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5154   "With %B spec, used for a leaf with brothers."
5155   :version "22.1"
5156   :type 'string
5157   :group 'gnus-thread)
5158
5159 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5160   "With %B spec, used for a leaf without brothers."
5161   :version "22.1"
5162   :type 'string
5163   :group 'gnus-thread)
5164
5165 (defcustom gnus-summary-display-while-building nil
5166   "If non-nil, show and update the summary buffer as it's being built.
5167 If the value is t, update the buffer after every line is inserted.  If
5168 the value is an integer (N), update the display every N lines."
5169   :version "22.1"
5170   :group 'gnus-thread
5171   :type '(choice (const :tag "off" nil)
5172                  number
5173                  (const :tag "frequently" t)))
5174
5175 (defun gnus-summary-prepare-threads (threads)
5176   "Prepare summary buffer from THREADS and indentation LEVEL.
5177 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5178 or a straight list of headers."
5179   (gnus-message 7 "Generating summary...")
5180
5181   (setq gnus-newsgroup-threads threads)
5182   (beginning-of-line)
5183
5184   (let ((gnus-tmp-level 0)
5185         (default-score (or gnus-summary-default-score 0))
5186         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5187         (building-line-count gnus-summary-display-while-building)
5188         (building-count (integerp gnus-summary-display-while-building))
5189         thread number subject stack state gnus-tmp-gathered beg-match
5190         new-roots gnus-tmp-new-adopts thread-end simp-subject
5191         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5192         gnus-tmp-replied gnus-tmp-subject-or-nil
5193         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5194         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5195         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5196         tree-stack)
5197
5198     (setq gnus-tmp-prev-subject nil
5199           gnus-tmp-thread-tree-header-string "")
5200
5201     (if (vectorp (car threads))
5202         ;; If this is a straight (sic) list of headers, then a
5203         ;; threaded summary display isn't required, so we just create
5204         ;; an unthreaded one.
5205         (gnus-summary-prepare-unthreaded threads)
5206
5207       ;; Do the threaded display.
5208
5209       (if gnus-summary-display-while-building
5210           (switch-to-buffer (buffer-name)))
5211       (while (or threads stack gnus-tmp-new-adopts new-roots)
5212
5213         (if (and (= gnus-tmp-level 0)
5214                  (or (not stack)
5215                      (= (caar stack) 0))
5216                  (not gnus-tmp-false-parent)
5217                  (or gnus-tmp-new-adopts new-roots))
5218             (if gnus-tmp-new-adopts
5219                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5220                       thread (list (car gnus-tmp-new-adopts))
5221                       gnus-tmp-header (caar thread)
5222                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5223               (when new-roots
5224                 (setq thread (list (car new-roots))
5225                       gnus-tmp-header (caar thread)
5226                       new-roots (cdr new-roots))))
5227
5228           (if threads
5229               ;; If there are some threads, we do them before the
5230               ;; threads on the stack.
5231               (setq thread threads
5232                     gnus-tmp-header (caar thread))
5233             ;; There were no current threads, so we pop something off
5234             ;; the stack.
5235             (setq state (car stack)
5236                   gnus-tmp-level (car state)
5237                   tree-stack (cadr state)
5238                   thread (caddr state)
5239                   stack (cdr stack)
5240                   gnus-tmp-header (caar thread))))
5241
5242         (setq gnus-tmp-false-parent nil)
5243         (setq gnus-tmp-root-expunged nil)
5244         (setq thread-end nil)
5245
5246         (if (stringp gnus-tmp-header)
5247             ;; The header is a dummy root.
5248             (cond
5249              ((eq gnus-summary-make-false-root 'adopt)
5250               ;; We let the first article adopt the rest.
5251               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5252                                                (cddar thread)))
5253               (setq gnus-tmp-gathered
5254                     (nconc (mapcar
5255                             (lambda (h) (mail-header-number (car h)))
5256                             (cddar thread))
5257                            gnus-tmp-gathered))
5258               (setq thread (cons (list (caar thread)
5259                                        (cadar thread))
5260                                  (cdr thread)))
5261               (setq gnus-tmp-level -1
5262                     gnus-tmp-false-parent t))
5263              ((eq gnus-summary-make-false-root 'empty)
5264               ;; We print adopted articles with empty subject fields.
5265               (setq gnus-tmp-gathered
5266                     (nconc (mapcar
5267                             (lambda (h) (mail-header-number (car h)))
5268                             (cddar thread))
5269                            gnus-tmp-gathered))
5270               (setq gnus-tmp-level -1))
5271              ((eq gnus-summary-make-false-root 'dummy)
5272               ;; We remember that we probably want to output a dummy
5273               ;; root.
5274               (setq gnus-tmp-dummy-line gnus-tmp-header)
5275               (setq gnus-tmp-prev-subject gnus-tmp-header))
5276              (t
5277               ;; We do not make a root for the gathered
5278               ;; sub-threads at all.
5279               (setq gnus-tmp-level -1)))
5280
5281           (setq number (mail-header-number gnus-tmp-header)
5282                 subject (mail-header-subject gnus-tmp-header)
5283                 simp-subject (gnus-simplify-subject-fully subject))
5284
5285           (cond
5286            ;; If the thread has changed subject, we might want to make
5287            ;; this subthread into a root.
5288            ((and (null gnus-thread-ignore-subject)
5289                  (not (zerop gnus-tmp-level))
5290                  gnus-tmp-prev-subject
5291                  (not (string= gnus-tmp-prev-subject simp-subject)))
5292             (setq new-roots (nconc new-roots (list (car thread)))
5293                   thread-end t
5294                   gnus-tmp-header nil))
5295            ;; If the article lies outside the current limit,
5296            ;; then we do not display it.
5297            ((not (memq number gnus-newsgroup-limit))
5298             (setq gnus-tmp-gathered
5299                   (nconc (mapcar
5300                           (lambda (h) (mail-header-number (car h)))
5301                           (cdar thread))
5302                          gnus-tmp-gathered))
5303             (setq gnus-tmp-new-adopts (if (cdar thread)
5304                                           (append gnus-tmp-new-adopts
5305                                                   (cdar thread))
5306                                         gnus-tmp-new-adopts)
5307                   thread-end t
5308                   gnus-tmp-header nil)
5309             (when (zerop gnus-tmp-level)
5310               (setq gnus-tmp-root-expunged t)))
5311            ;; Perhaps this article is to be marked as read?
5312            ((and gnus-summary-mark-below
5313                  (< (or (cdr (assq number gnus-newsgroup-scored))
5314                         default-score)
5315                     gnus-summary-mark-below)
5316                  ;; Don't touch sparse articles.
5317                  (not (gnus-summary-article-sparse-p number))
5318                  (not (gnus-summary-article-ancient-p number)))
5319             (setq gnus-newsgroup-unreads
5320                   (delq number gnus-newsgroup-unreads))
5321             (if gnus-newsgroup-auto-expire
5322                 (setq gnus-newsgroup-expirable
5323                       (gnus-add-to-sorted-list
5324                        gnus-newsgroup-expirable number))
5325               (push (cons number gnus-low-score-mark)
5326                     gnus-newsgroup-reads))))
5327
5328           (when gnus-tmp-header
5329             ;; We may have an old dummy line to output before this
5330             ;; article.
5331             (when (and gnus-tmp-dummy-line
5332                        (gnus-subject-equal
5333                         gnus-tmp-dummy-line
5334                         (mail-header-subject gnus-tmp-header)))
5335               (gnus-summary-insert-dummy-line
5336                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5337               (setq gnus-tmp-dummy-line nil))
5338
5339             ;; Compute the mark.
5340             (setq gnus-tmp-unread (gnus-article-mark number))
5341
5342             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5343                                   gnus-tmp-header gnus-tmp-level)
5344                   gnus-newsgroup-data)
5345
5346             ;; Actually insert the line.
5347             (setq
5348              gnus-tmp-subject-or-nil
5349              (cond
5350               ((and gnus-thread-ignore-subject
5351                     gnus-tmp-prev-subject
5352                     (not (string= gnus-tmp-prev-subject simp-subject)))
5353                subject)
5354               ((zerop gnus-tmp-level)
5355                (if (and (eq gnus-summary-make-false-root 'empty)
5356                         (memq number gnus-tmp-gathered)
5357                         gnus-tmp-prev-subject
5358                         (string= gnus-tmp-prev-subject simp-subject))
5359                    gnus-summary-same-subject
5360                  subject))
5361               (t gnus-summary-same-subject)))
5362             (if (and (eq gnus-summary-make-false-root 'adopt)
5363                      (= gnus-tmp-level 1)
5364                      (memq number gnus-tmp-gathered))
5365                 (setq gnus-tmp-opening-bracket ?\<
5366                       gnus-tmp-closing-bracket ?\>)
5367               (setq gnus-tmp-opening-bracket ?\[
5368                     gnus-tmp-closing-bracket ?\]))
5369             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5370                 (gnus-make-thread-indent-array
5371                  (max (* 2 (length gnus-thread-indent-array))
5372                       gnus-tmp-level)))
5373             (setq
5374              gnus-tmp-indentation
5375              (aref gnus-thread-indent-array gnus-tmp-level)
5376              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5377              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5378                                 gnus-summary-default-score 0)
5379              gnus-tmp-score-char
5380              (if (or (null gnus-summary-default-score)
5381                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5382                          gnus-summary-zcore-fuzz))
5383                  ?                      ;Whitespace
5384                (if (< gnus-tmp-score gnus-summary-default-score)
5385                    gnus-score-below-mark gnus-score-over-mark))
5386              gnus-tmp-replied
5387              (cond ((memq number gnus-newsgroup-processable)
5388                     gnus-process-mark)
5389                    ((memq number gnus-newsgroup-cached)
5390                     gnus-cached-mark)
5391                    ((memq number gnus-newsgroup-replied)
5392                     gnus-replied-mark)
5393                    ((memq number gnus-newsgroup-forwarded)
5394                     gnus-forwarded-mark)
5395                    ((memq number gnus-newsgroup-saved)
5396                     gnus-saved-mark)
5397                    ((memq number gnus-newsgroup-unseen)
5398                     gnus-unseen-mark)
5399                    (t gnus-no-mark))
5400              gnus-tmp-downloaded
5401              (cond ((memq number gnus-newsgroup-undownloaded)
5402                     gnus-undownloaded-mark)
5403                    (gnus-newsgroup-agentized
5404                     gnus-downloaded-mark)
5405                    (t
5406                     gnus-no-mark))
5407              gnus-tmp-from (mail-header-from gnus-tmp-header)
5408              gnus-tmp-name
5409              (cond
5410               ((string-match "<[^>]+> *$" gnus-tmp-from)
5411                (setq beg-match (match-beginning 0))
5412                (or (and (string-match "^\".+\"" gnus-tmp-from)
5413                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5414                    (substring gnus-tmp-from 0 beg-match)))
5415               ((string-match "(.+)" gnus-tmp-from)
5416                (substring gnus-tmp-from
5417                           (1+ (match-beginning 0)) (1- (match-end 0))))
5418               (t gnus-tmp-from))
5419
5420              ;; Do the %B string
5421              gnus-tmp-thread-tree-header-string
5422              (cond
5423               ((not gnus-show-threads) "")
5424               ((zerop gnus-tmp-level)
5425                (cond ((cdar thread)
5426                       (or gnus-sum-thread-tree-root subject))
5427                      (gnus-tmp-new-adopts
5428                       (or gnus-sum-thread-tree-false-root subject))
5429                      (t
5430                       (or gnus-sum-thread-tree-single-indent subject))))
5431               (t
5432                (concat (apply 'concat
5433                               (mapcar (lambda (item)
5434                                         (if (= item 1)
5435                                             gnus-sum-thread-tree-vertical
5436                                           gnus-sum-thread-tree-indent))
5437                                       (cdr (reverse tree-stack))))
5438                        (if (nth 1 thread)
5439                            gnus-sum-thread-tree-leaf-with-other
5440                          gnus-sum-thread-tree-single-leaf)))))
5441             (when (string= gnus-tmp-name "")
5442               (setq gnus-tmp-name gnus-tmp-from))
5443             (unless (numberp gnus-tmp-lines)
5444               (setq gnus-tmp-lines -1))
5445             (if (= gnus-tmp-lines -1)
5446                 (setq gnus-tmp-lines "?")
5447               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5448             (gnus-put-text-property
5449              (point)
5450              (progn (eval gnus-summary-line-format-spec) (point))
5451              'gnus-number number)
5452             (when gnus-visual-p
5453               (forward-line -1)
5454               (gnus-summary-highlight-line)
5455               (when gnus-summary-update-hook
5456                 (gnus-run-hooks 'gnus-summary-update-hook))
5457               (forward-line 1))
5458
5459             (setq gnus-tmp-prev-subject simp-subject)))
5460
5461         (when (nth 1 thread)
5462           (push (list (max 0 gnus-tmp-level)
5463                       (copy-sequence tree-stack)
5464                       (nthcdr 1 thread))
5465                 stack))
5466         (push (if (nth 1 thread) 1 0) tree-stack)
5467         (incf gnus-tmp-level)
5468         (setq threads (if thread-end nil (cdar thread)))
5469         (if gnus-summary-display-while-building
5470             (if building-count
5471                 (progn
5472                   ;; use a set frequency
5473                   (setq building-line-count (1- building-line-count))
5474                   (when (= building-line-count 0)
5475                     (sit-for 0)
5476                     (setq building-line-count
5477                           gnus-summary-display-while-building)))
5478               ;; always
5479               (sit-for 0)))
5480         (unless threads
5481           (setq gnus-tmp-level 0)))))
5482   (gnus-message 7 "Generating summary...done"))
5483
5484 (defun gnus-summary-prepare-unthreaded (headers)
5485   "Generate an unthreaded summary buffer based on HEADERS."
5486   (let (header number mark)
5487
5488     (beginning-of-line)
5489
5490     (while headers
5491       ;; We may have to root out some bad articles...
5492       (when (memq (setq number (mail-header-number
5493                                 (setq header (pop headers))))
5494                   gnus-newsgroup-limit)
5495         ;; Mark article as read when it has a low score.
5496         (when (and gnus-summary-mark-below
5497                    (< (or (cdr (assq number gnus-newsgroup-scored))
5498                           gnus-summary-default-score 0)
5499                       gnus-summary-mark-below)
5500                    (not (gnus-summary-article-ancient-p number)))
5501           (setq gnus-newsgroup-unreads
5502                 (delq number gnus-newsgroup-unreads))
5503           (if gnus-newsgroup-auto-expire
5504               (push number gnus-newsgroup-expirable)
5505             (push (cons number gnus-low-score-mark)
5506                   gnus-newsgroup-reads)))
5507
5508         (setq mark (gnus-article-mark number))
5509         (push (gnus-data-make number mark (1+ (point)) header 0)
5510               gnus-newsgroup-data)
5511         (gnus-summary-insert-line
5512          header 0 number
5513          (memq number gnus-newsgroup-undownloaded)
5514          mark (memq number gnus-newsgroup-replied)
5515          (memq number gnus-newsgroup-expirable)
5516          (mail-header-subject header) nil
5517          (cdr (assq number gnus-newsgroup-scored))
5518          (memq number gnus-newsgroup-processable))))))
5519
5520 (defun gnus-group-get-list-identifiers (group)
5521   "Get list identifier regexp for GROUP."
5522   (or (gnus-parameter-list-identifier group)
5523       (if (consp gnus-list-identifiers)
5524           (mapconcat 'identity gnus-list-identifiers " *\\|")
5525         gnus-list-identifiers)))
5526
5527 (defun gnus-summary-remove-list-identifiers ()
5528   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5529   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5530         changed subject)
5531     (when regexp
5532       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5533       (dolist (header gnus-newsgroup-headers)
5534         (setq subject (mail-header-subject header)
5535               changed nil)
5536         (while (string-match regexp subject)
5537           (setq subject
5538                 (concat (substring subject 0 (match-beginning 1))
5539                         (substring subject (match-end 0)))
5540                 changed t))
5541         (when changed
5542           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5543             (setq subject
5544                   (concat (substring subject 0 (match-beginning 1))
5545                           (substring subject (match-end 1)))))
5546           (mail-header-set-subject header subject))))))
5547
5548 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5549   "Fetch headers of ARTICLES."
5550   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5551     (gnus-message 7 "Fetching headers for %s..." name)
5552     (prog1
5553         (if (eq 'nov
5554                 (setq gnus-headers-retrieved-by
5555                       (gnus-retrieve-headers
5556                        articles gnus-newsgroup-name
5557                        (or limit
5558                            ;; We might want to fetch old headers, but
5559                            ;; not if there is only 1 article.
5560                            (and (or (and
5561                                      (not (eq gnus-fetch-old-headers 'some))
5562                                      (not (numberp gnus-fetch-old-headers)))
5563                                     (> (length articles) 1))
5564                                 gnus-fetch-old-headers)))))
5565             (gnus-get-newsgroup-headers-xover
5566              articles force-new dependencies gnus-newsgroup-name t)
5567           (gnus-get-newsgroup-headers dependencies force-new))
5568       (gnus-message 7 "Fetching headers for %s...done" name))))
5569
5570 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5571   "Select newsgroup GROUP.
5572 If READ-ALL is non-nil, all articles in the group are selected.
5573 If SELECT-ARTICLES, only select those articles from GROUP."
5574   (let* ((entry (gnus-group-entry group))
5575          ;;!!! Dirty hack; should be removed.
5576          (gnus-summary-ignore-duplicates
5577           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5578               t
5579             gnus-summary-ignore-duplicates))
5580          (info (nth 2 entry))
5581          charset articles fetched-articles cached)
5582
5583     (unless (gnus-check-server
5584              (set (make-local-variable 'gnus-current-select-method)
5585                   (gnus-find-method-for-group group)))
5586       (error "Couldn't open server"))
5587     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5588
5589     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5590         (gnus-activate-group group)     ; Or we can activate it...
5591         (progn                          ; Or we bug out.
5592           (when (equal major-mode 'gnus-summary-mode)
5593             (gnus-kill-buffer (current-buffer)))
5594           (error
5595            "Couldn't activate group %s: %s"
5596            (mm-decode-coding-string group charset)
5597            (mm-decode-coding-string (gnus-status-message group) charset))))
5598
5599     (unless (gnus-request-group group t)
5600       (when (equal major-mode 'gnus-summary-mode)
5601         (gnus-kill-buffer (current-buffer)))
5602       (error "Couldn't request group %s: %s"
5603              (mm-decode-coding-string group charset)
5604              (mm-decode-coding-string (gnus-status-message group) charset)))
5605
5606     (when (and gnus-agent
5607                (gnus-active group))
5608       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5609
5610       (setq gnus-summary-use-undownloaded-faces
5611             (gnus-agent-find-parameter
5612              group
5613              'agent-enable-undownloaded-faces)))
5614
5615     (setq gnus-newsgroup-name group
5616           gnus-newsgroup-unselected nil
5617           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5618
5619     (let ((display (gnus-group-find-parameter group 'display)))
5620       (setq gnus-newsgroup-display
5621             (cond
5622              ((not (zerop (or (car-safe read-all) 0)))
5623               ;; The user entered the group with C-u SPC/RET, let's show
5624               ;; all articles.
5625               'gnus-not-ignore)
5626              ((eq display 'all)
5627               'gnus-not-ignore)
5628              ((arrayp display)
5629               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5630              ((numberp display)
5631               ;; The following is probably the "correct" solution, but
5632               ;; it makes Gnus fetch all headers and then limit the
5633               ;; articles (which is slow), so instead we hack the
5634               ;; select-articles parameter instead. -- Simon Josefsson
5635               ;; <jas@kth.se>
5636               ;;
5637               ;; (gnus-byte-compile
5638               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5639               ;;                         display)))))
5640               (setq select-articles
5641                     (gnus-uncompress-range
5642                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5643                              (if (> tmp 0)
5644                                  tmp
5645                                1))
5646                            (cdr (gnus-active group)))))
5647               nil)
5648              (t
5649               nil))))
5650
5651     (gnus-summary-setup-default-charset)
5652
5653     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5654     (when (gnus-virtual-group-p group)
5655       (setq cached gnus-newsgroup-cached))
5656
5657     (setq gnus-newsgroup-unreads
5658           (gnus-sorted-ndifference
5659            (gnus-sorted-ndifference gnus-newsgroup-unreads
5660                                     gnus-newsgroup-marked)
5661            gnus-newsgroup-dormant))
5662
5663     (setq gnus-newsgroup-processable nil)
5664
5665     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5666
5667     ;; Adjust and set lists of article marks.
5668     (when info
5669       (gnus-adjust-marked-articles info))
5670     (if (setq articles select-articles)
5671         (setq gnus-newsgroup-unselected
5672               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5673       (setq articles (gnus-articles-to-read group read-all)))
5674
5675     (cond
5676      ((null articles)
5677       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5678       'quit)
5679      ((eq articles 0) nil)
5680      (t
5681       ;; Init the dependencies hash table.
5682       (setq gnus-newsgroup-dependencies
5683             (gnus-make-hashtable (length articles)))
5684       (gnus-set-global-variables)
5685       ;; Retrieve the headers and read them in.
5686
5687       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5688
5689       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5690       (when cached
5691         (setq gnus-newsgroup-cached cached))
5692
5693       ;; Suppress duplicates?
5694       (when gnus-suppress-duplicates
5695         (gnus-dup-suppress-articles))
5696
5697       ;; Set the initial limit.
5698       (setq gnus-newsgroup-limit (copy-sequence articles))
5699       ;; Remove canceled articles from the list of unread articles.
5700       (setq fetched-articles
5701             (mapcar (lambda (headers) (mail-header-number headers))
5702                     gnus-newsgroup-headers))
5703       (setq gnus-newsgroup-articles fetched-articles)
5704       (setq gnus-newsgroup-unreads
5705             (gnus-sorted-nintersection
5706              gnus-newsgroup-unreads fetched-articles))
5707       (gnus-compute-unseen-list)
5708
5709       ;; Removed marked articles that do not exist.
5710       (gnus-update-missing-marks
5711        (gnus-sorted-difference articles fetched-articles))
5712       ;; We might want to build some more threads first.
5713       (when (and gnus-fetch-old-headers
5714                  (eq gnus-headers-retrieved-by 'nov))
5715         (if (eq gnus-fetch-old-headers 'invisible)
5716             (gnus-build-all-threads)
5717           (gnus-build-old-threads)))
5718       ;; Let the Gnus agent mark articles as read.
5719       (when gnus-agent
5720         (gnus-agent-get-undownloaded-list))
5721       ;; Remove list identifiers from subject
5722       (gnus-summary-remove-list-identifiers)
5723       ;; Check whether auto-expire is to be done in this group.
5724       (setq gnus-newsgroup-auto-expire
5725             (and (gnus-group-auto-expirable-p group)
5726                  (not (gnus-group-read-only-p group))))
5727       ;; Set up the article buffer now, if necessary.
5728       (unless (and gnus-single-article-buffer
5729                    (equal gnus-article-buffer "*Article*"))
5730         (gnus-article-setup-buffer))
5731       ;; First and last article in this newsgroup.
5732       (when gnus-newsgroup-headers
5733         (setq gnus-newsgroup-begin
5734               (mail-header-number (car gnus-newsgroup-headers))
5735               gnus-newsgroup-end
5736               (mail-header-number
5737                (gnus-last-element gnus-newsgroup-headers))))
5738       ;; GROUP is successfully selected.
5739       (or gnus-newsgroup-headers t)))))
5740
5741 (defun gnus-compute-unseen-list ()
5742   ;; The `seen' marks are treated specially.
5743   (if (not gnus-newsgroup-seen)
5744       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5745     (setq gnus-newsgroup-unseen
5746           (gnus-inverse-list-range-intersection
5747            gnus-newsgroup-articles gnus-newsgroup-seen))))
5748
5749 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5750
5751 (defun gnus-summary-display-make-predicate (display)
5752   (require 'gnus-agent)
5753   (when (= (length display) 1)
5754     (setq display (car display)))
5755   (unless gnus-summary-display-cache
5756     (dolist (elem (append '((unread . unread)
5757                             (read . read)
5758                             (unseen . unseen))
5759                           gnus-article-mark-lists))
5760       (push (cons (cdr elem)
5761                   (gnus-byte-compile    ;Why bother?
5762                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5763             gnus-summary-display-cache)))
5764   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5765         (gnus-category-predicate-cache gnus-summary-display-cache))
5766     (gnus-get-predicate display)))
5767
5768 ;; Uses the dynamically bound `gnus-number' variable.
5769 (defvar gnus-number)
5770 (defun gnus-article-marked-p (type &optional article)
5771   (let ((article (or article gnus-number)))
5772     (cond
5773      ((eq type 'tick)
5774       (memq article gnus-newsgroup-marked))
5775      ((eq type 'spam)
5776       (memq article gnus-newsgroup-spam-marked))
5777      ((eq type 'unsend)
5778       (memq article gnus-newsgroup-unsendable))
5779      ((eq type 'undownload)
5780       (memq article gnus-newsgroup-undownloaded))
5781      ((eq type 'download)
5782       (memq article gnus-newsgroup-downloadable))
5783      ((eq type 'unread)
5784       (memq article gnus-newsgroup-unreads))
5785      ((eq type 'read)
5786       (memq article gnus-newsgroup-reads))
5787      ((eq type 'dormant)
5788       (memq article gnus-newsgroup-dormant) )
5789      ((eq type 'expire)
5790       (memq article gnus-newsgroup-expirable))
5791      ((eq type 'reply)
5792       (memq article gnus-newsgroup-replied))
5793      ((eq type 'killed)
5794       (memq article gnus-newsgroup-killed))
5795      ((eq type 'bookmark)
5796       (assq article gnus-newsgroup-bookmarks))
5797      ((eq type 'score)
5798       (assq article gnus-newsgroup-scored))
5799      ((eq type 'save)
5800       (memq article gnus-newsgroup-saved))
5801      ((eq type 'cache)
5802       (memq article gnus-newsgroup-cached))
5803      ((eq type 'forward)
5804       (memq article gnus-newsgroup-forwarded))
5805      ((eq type 'seen)
5806       (not (memq article gnus-newsgroup-unseen)))
5807      (t t))))
5808
5809 (defun gnus-articles-to-read (group &optional read-all)
5810   "Find out what articles the user wants to read."
5811   (let* ((only-read-p t)
5812          (articles
5813           ;; Select all articles if `read-all' is non-nil, or if there
5814           ;; are no unread articles.
5815           (if (or read-all
5816                   (and (zerop (length gnus-newsgroup-marked))
5817                        (zerop (length gnus-newsgroup-unreads)))
5818                   ;; Fetch all if the predicate is non-nil.
5819                   gnus-newsgroup-display)
5820               ;; We want to select the headers for all the articles in
5821               ;; the group, so we select either all the active
5822               ;; articles in the group, or (if that's nil), the
5823               ;; articles in the cache.
5824               (or
5825                (if gnus-newsgroup-maximum-articles
5826                    (let ((active (gnus-active group)))
5827                      (gnus-uncompress-range
5828                       (cons (max (car active)
5829                                  (- (cdr active)
5830                                     gnus-newsgroup-maximum-articles
5831                                     -1))
5832                             (cdr active))))
5833                  (gnus-uncompress-range (gnus-active group)))
5834                (gnus-cache-articles-in-group group))
5835             ;; Select only the "normal" subset of articles.
5836             (setq only-read-p nil)
5837             (gnus-sorted-nunion
5838              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5839              gnus-newsgroup-unreads)))
5840          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5841          (scored (length scored-list))
5842          (number (length articles))
5843          (marked (+ (length gnus-newsgroup-marked)
5844                     (length gnus-newsgroup-dormant)))
5845          (select
5846           (cond
5847            ((numberp read-all)
5848             read-all)
5849            ((numberp gnus-newsgroup-display)
5850             gnus-newsgroup-display)
5851            (t
5852             (condition-case ()
5853                 (cond
5854                  ((and (or (<= scored marked) (= scored number))
5855                        (numberp gnus-large-newsgroup)
5856                        (> number gnus-large-newsgroup))
5857                   (let* ((cursor-in-echo-area nil)
5858                          (initial (gnus-parameter-large-newsgroup-initial
5859                                    gnus-newsgroup-name))
5860                          (default (if only-read-p
5861                                       (or initial gnus-large-newsgroup)
5862                                     number))
5863                          (input
5864                           (read-string
5865                            (if only-read-p
5866                                (format
5867                                 "How many articles from %s (available %d, default %d): "
5868                                 (gnus-group-decoded-name
5869                                  (gnus-group-real-name gnus-newsgroup-name))
5870                                 number default)
5871                              (format
5872                               "How many articles from %s (%d default): "
5873                               (gnus-group-decoded-name
5874                                (gnus-group-real-name gnus-newsgroup-name))
5875                               default))
5876                            nil
5877                            nil
5878                            (number-to-string default))))
5879                     (if (string-match "^[ \t]*$" input) number input)))
5880                  ((and (> scored marked) (< scored number)
5881                        (> (- scored number) 20))
5882                   (let ((input
5883                          (read-string
5884                           (format "%s %s (%d scored, %d total): "
5885                                   "How many articles from"
5886                                   (gnus-group-decoded-name
5887                                    (gnus-group-real-name gnus-newsgroup-name))
5888                                   scored number))))
5889                     (if (string-match "^[ \t]*$" input)
5890                         number input)))
5891                  (t number))
5892               (quit
5893                (message "Quit getting the articles to read")
5894                nil))))))
5895     (setq select (if (stringp select) (string-to-number select) select))
5896     (if (or (null select) (zerop select))
5897         select
5898       (if (and (not (zerop scored)) (<= (abs select) scored))
5899           (progn
5900             (setq articles (sort scored-list '<))
5901             (setq number (length articles)))
5902         (setq articles (copy-sequence articles)))
5903
5904       (when (< (abs select) number)
5905         (if (< select 0)
5906             ;; Select the N oldest articles.
5907             (setcdr (nthcdr (1- (abs select)) articles) nil)
5908           ;; Select the N most recent articles.
5909           (setq articles (nthcdr (- number select) articles))))
5910       (setq gnus-newsgroup-unselected
5911             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5912       (when gnus-alter-articles-to-read-function
5913         (setq articles
5914               (sort
5915                (funcall gnus-alter-articles-to-read-function
5916                         gnus-newsgroup-name articles)
5917                '<)))
5918       articles)))
5919
5920 (defun gnus-killed-articles (killed articles)
5921   (let (out)
5922     (while articles
5923       (when (inline (gnus-member-of-range (car articles) killed))
5924         (push (car articles) out))
5925       (setq articles (cdr articles)))
5926     out))
5927
5928 (defun gnus-uncompress-marks (marks)
5929   "Uncompress the mark ranges in MARKS."
5930   (let ((uncompressed '(score bookmark))
5931         out)
5932     (while marks
5933       (if (memq (caar marks) uncompressed)
5934           (push (car marks) out)
5935         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5936       (setq marks (cdr marks)))
5937     out))
5938
5939 (defun gnus-article-mark-to-type (mark)
5940   "Return the type of MARK."
5941   (or (cadr (assq mark gnus-article-special-mark-lists))
5942       'list))
5943
5944 (defun gnus-article-unpropagatable-p (mark)
5945   "Return whether MARK should be propagated to back end."
5946   (memq mark gnus-article-unpropagated-mark-lists))
5947
5948 (defun gnus-adjust-marked-articles (info)
5949   "Set all article lists and remove all marks that are no longer valid."
5950   (let* ((marked-lists (gnus-info-marks info))
5951          (active (gnus-active (gnus-info-group info)))
5952          (min (car active))
5953          (max (cdr active))
5954          (types gnus-article-mark-lists)
5955          marks var articles article mark mark-type
5956          bgn end)
5957     ;; Hack to avoid adjusting marks for imap.
5958     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5959               'nnimap)
5960       (setq min 1))
5961
5962     (dolist (marks marked-lists)
5963       (setq mark (car marks)
5964             mark-type (gnus-article-mark-to-type mark)
5965             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5966
5967       ;; We set the variable according to the type of the marks list,
5968       ;; and then adjust the marks to a subset of the active articles.
5969       (cond
5970        ;; Adjust "simple" lists - compressed yet unsorted
5971        ((eq mark-type 'list)
5972         ;; Simultaneously uncompress and clip to active range
5973         ;; See gnus-uncompress-range for a description of possible marks
5974         (let (l lh)
5975           (if (not (cadr marks))
5976               (set var nil)
5977             (setq articles (if (numberp (cddr marks))
5978                                (list (cdr marks))
5979                              (cdr marks))
5980                   lh (cons nil nil)
5981                   l lh)
5982
5983             (while (setq article (pop articles))
5984               (cond ((consp article)
5985                      (setq bgn (max (car article) min)
5986                            end (min (cdr article) max))
5987                      (while (<= bgn end)
5988                        (setq l (setcdr l (cons bgn nil))
5989                              bgn (1+ bgn))))
5990                     ((and (<= min article)
5991                           (>= max article))
5992                      (setq l (setcdr l (cons article nil))))))
5993             (set var (cdr lh)))))
5994        ;; Adjust assocs.
5995        ((eq mark-type 'tuple)
5996         (set var (setq articles (cdr marks)))
5997         (when (not (listp (cdr (symbol-value var))))
5998           (set var (list (symbol-value var))))
5999         (when (not (listp (cdr articles)))
6000           (setq articles (list articles)))
6001         (while articles
6002           (when (or (not (consp (setq article (pop articles))))
6003                     (< (car article) min)
6004                     (> (car article) max))
6005             (set var (delq article (symbol-value var))))))
6006        ;; Adjust ranges (sloppily).
6007        ((eq mark-type 'range)
6008         (cond
6009          ((eq mark 'seen)
6010           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6011           ;; It should be (seen (NUM1 . NUM2)).
6012           (when (numberp (cddr marks))
6013             (setcdr marks (list (cdr marks))))
6014           (setq articles (cdr marks))
6015           (while (and articles
6016                       (or (and (consp (car articles))
6017                                (> min (cdar articles)))
6018                           (and (numberp (car articles))
6019                                (> min (car articles)))))
6020             (pop articles))
6021           (set var articles))))))))
6022
6023 (defun gnus-update-missing-marks (missing)
6024   "Go through the list of MISSING articles and remove them from the mark lists."
6025   (when missing
6026     (let (var m)
6027       ;; Go through all types.
6028       (dolist (elem gnus-article-mark-lists)
6029         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6030           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6031           (when (symbol-value var)
6032             ;; This list has articles.  So we delete all missing
6033             ;; articles from it.
6034             (setq m missing)
6035             (while m
6036               (set var (delq (pop m) (symbol-value var))))))))))
6037
6038 (defun gnus-update-marks ()
6039   "Enter the various lists of marked articles into the newsgroup info list."
6040   (let ((types gnus-article-mark-lists)
6041         (info (gnus-get-info gnus-newsgroup-name))
6042         type list newmarked symbol delta-marks)
6043     (when info
6044       ;; Add all marks lists to the list of marks lists.
6045       (while (setq type (pop types))
6046         (setq list (symbol-value
6047                     (setq symbol
6048                           (intern (format "gnus-newsgroup-%s" (car type))))))
6049
6050         (when list
6051           ;; Get rid of the entries of the articles that have the
6052           ;; default score.
6053           (when (and (eq (cdr type) 'score)
6054                      gnus-save-score
6055                      list)
6056             (let* ((arts list)
6057                    (prev (cons nil list))
6058                    (all prev))
6059               (while arts
6060                 (if (or (not (consp (car arts)))
6061                         (= (cdar arts) gnus-summary-default-score))
6062                     (setcdr prev (cdr arts))
6063                   (setq prev arts))
6064                 (setq arts (cdr arts)))
6065               (setq list (cdr all)))))
6066
6067         (when (eq (cdr type) 'seen)
6068           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6069
6070         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6071           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6072
6073         (when (and (gnus-check-backend-function
6074                     'request-set-mark gnus-newsgroup-name)
6075                    (not (gnus-article-unpropagatable-p (cdr type))))
6076           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6077                  ;; Don't do anything about marks for articles we
6078                  ;; didn't actually get any headers for.
6079                  (del
6080                   (gnus-list-range-intersection
6081                    gnus-newsgroup-articles
6082                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6083                  (add
6084                   (gnus-list-range-intersection
6085                    gnus-newsgroup-articles
6086                    (gnus-remove-from-range
6087                     (gnus-copy-sequence list) old))))
6088             (when add
6089               (push (list add 'add (list (cdr type))) delta-marks))
6090             (when del
6091               ;; Don't delete marks from outside the active range.
6092               ;; This shouldn't happen, but is a sanity check.
6093               (setq del (gnus-sorted-range-intersection
6094                          (gnus-active gnus-newsgroup-name) del))
6095               (push (list del 'del (list (cdr type))) delta-marks))))
6096
6097         (when list
6098           (push (cons (cdr type) list) newmarked)))
6099
6100       (when delta-marks
6101         (unless (gnus-check-group gnus-newsgroup-name)
6102           (error "Can't open server for %s" gnus-newsgroup-name))
6103         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6104
6105       ;; Enter these new marks into the info of the group.
6106       (if (nthcdr 3 info)
6107           (setcar (nthcdr 3 info) newmarked)
6108         ;; Add the marks lists to the end of the info.
6109         (when newmarked
6110           (setcdr (nthcdr 2 info) (list newmarked))))
6111
6112       ;; Cut off the end of the info if there's nothing else there.
6113       (let ((i 5))
6114         (while (and (> i 2)
6115                     (not (nth i info)))
6116           (when (nthcdr (decf i) info)
6117             (setcdr (nthcdr i info) nil)))))))
6118
6119 (defun gnus-set-mode-line (where)
6120   "Set the mode line of the article or summary buffers.
6121 If WHERE is `summary', the summary mode line format will be used."
6122   ;; Is this mode line one we keep updated?
6123   (when (and (memq where gnus-updated-mode-lines)
6124              (symbol-value
6125               (intern (format "gnus-%s-mode-line-format-spec" where))))
6126     (let (mode-string)
6127       ;; We evaluate this in the summary buffer since these
6128       ;; variables are buffer-local to that buffer.
6129       (with-current-buffer gnus-summary-buffer
6130         ;; We bind all these variables that are used in the `eval' form
6131         ;; below.
6132         (let* ((mformat (symbol-value
6133                          (intern
6134                           (format "gnus-%s-mode-line-format-spec" where))))
6135                (gnus-tmp-group-name (gnus-mode-string-quote
6136                                      (gnus-group-decoded-name
6137                                       gnus-newsgroup-name)))
6138                (gnus-tmp-article-number (or gnus-current-article 0))
6139                (gnus-tmp-unread gnus-newsgroup-unreads)
6140                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6141                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6142                (gnus-tmp-unread-and-unselected
6143                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6144                             (zerop gnus-tmp-unselected))
6145                        "")
6146                       ((zerop gnus-tmp-unselected)
6147                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6148                       (t (format "{%d(+%d) more}"
6149                                  gnus-tmp-unread-and-unticked
6150                                  gnus-tmp-unselected))))
6151                (gnus-tmp-subject
6152                 (if (and gnus-current-headers
6153                          (vectorp gnus-current-headers))
6154                     (gnus-mode-string-quote
6155                      (mail-header-subject gnus-current-headers))
6156                   ""))
6157                bufname-length max-len
6158                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6159           (setq mode-string (eval mformat))
6160           (setq bufname-length (if (string-match "%b" mode-string)
6161                                    (- (length
6162                                        (buffer-name
6163                                         (if (eq where 'summary)
6164                                             nil
6165                                           (get-buffer gnus-article-buffer))))
6166                                       2)
6167                                  0))
6168           (setq max-len (max 4 (if gnus-mode-non-string-length
6169                                    (- (window-width)
6170                                       gnus-mode-non-string-length
6171                                       bufname-length)
6172                                  (length mode-string))))
6173           ;; We might have to chop a bit of the string off...
6174           (when (> (length mode-string) max-len)
6175             (setq mode-string
6176                   (concat (truncate-string-to-width mode-string (- max-len 3))
6177                           "...")))))
6178       ;; Update the mode line.
6179       (setq mode-line-buffer-identification
6180             (gnus-mode-line-buffer-identification (list mode-string)))
6181       (set-buffer-modified-p t))))
6182
6183 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6184   "Go through the HEADERS list and add all Xrefs to a hash table.
6185 The resulting hash table is returned, or nil if no Xrefs were found."
6186   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6187          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6188          (xref-hashtb (gnus-make-hashtable))
6189          start group entry number xrefs header)
6190     (while headers
6191       (setq header (pop headers))
6192       (when (and (setq xrefs (mail-header-xref header))
6193                  (not (memq (setq number (mail-header-number header))
6194                             unreads)))
6195         (setq start 0)
6196         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6197           (setq start (match-end 0))
6198           (setq group (if prefix
6199                           (concat prefix (substring xrefs (match-beginning 1)
6200                                                     (match-end 1)))
6201                         (substring xrefs (match-beginning 1) (match-end 1))))
6202           (setq number
6203                 (string-to-number (substring xrefs (match-beginning 2)
6204                                           (match-end 2))))
6205           (if (setq entry (gnus-gethash group xref-hashtb))
6206               (setcdr entry (cons number (cdr entry)))
6207             (gnus-sethash group (cons number nil) xref-hashtb)))))
6208     (and start xref-hashtb)))
6209
6210 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6211   "Look through all the headers and mark the Xrefs as read."
6212   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6213         name info xref-hashtb idlist method nth4)
6214     (with-current-buffer gnus-group-buffer
6215       (when (setq xref-hashtb
6216                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6217         (mapatoms
6218          (lambda (group)
6219            (unless (string= from-newsgroup (setq name (symbol-name group)))
6220              (setq idlist (symbol-value group))
6221              ;; Dead groups are not updated.
6222              (and (prog1
6223                       (setq info (gnus-get-info name))
6224                     (when (stringp (setq nth4 (gnus-info-method info)))
6225                       (setq nth4 (gnus-server-to-method nth4))))
6226                   ;; Only do the xrefs if the group has the same
6227                   ;; select method as the group we have just read.
6228                   (or (gnus-methods-equal-p
6229                        nth4 (gnus-find-method-for-group from-newsgroup))
6230                       virtual
6231                       (equal nth4 (setq method (gnus-find-method-for-group
6232                                                 from-newsgroup)))
6233                       (and (equal (car nth4) (car method))
6234                            (equal (nth 1 nth4) (nth 1 method))))
6235                   gnus-use-cross-reference
6236                   (or (not (eq gnus-use-cross-reference t))
6237                       virtual
6238                       ;; Only do cross-references on subscribed
6239                       ;; groups, if that is what is wanted.
6240                       (<= (gnus-info-level info) gnus-level-subscribed))
6241                   (gnus-group-make-articles-read name idlist))))
6242          xref-hashtb)))))
6243
6244 (defun gnus-compute-read-articles (group articles)
6245   (let* ((entry (gnus-group-entry group))
6246          (info (nth 2 entry))
6247          (active (gnus-active group))
6248          ninfo)
6249     (when entry
6250       ;; First peel off all invalid article numbers.
6251       (when active
6252         (let ((ids articles)
6253               id first)
6254           (while (setq id (pop ids))
6255             (when (and first (> id (cdr active)))
6256               ;; We'll end up in this situation in one particular
6257               ;; obscure situation.  If you re-scan a group and get
6258               ;; a new article that is cross-posted to a different
6259               ;; group that has not been re-scanned, you might get
6260               ;; crossposted article that has a higher number than
6261               ;; Gnus believes possible.  So we re-activate this
6262               ;; group as well.  This might mean doing the
6263               ;; crossposting thingy will *increase* the number
6264               ;; of articles in some groups.  Tsk, tsk.
6265               (setq active (or (gnus-activate-group group) active)))
6266             (when (or (> id (cdr active))
6267                       (< id (car active)))
6268               (setq articles (delq id articles))))))
6269       ;; If the read list is nil, we init it.
6270       (if (and active
6271                (null (gnus-info-read info))
6272                (> (car active) 1))
6273           (setq ninfo (cons 1 (1- (car active))))
6274         (setq ninfo (gnus-info-read info)))
6275       ;; Then we add the read articles to the range.
6276       (gnus-add-to-range
6277        ninfo (setq articles (sort articles '<))))))
6278
6279 (defun gnus-group-make-articles-read (group articles)
6280   "Update the info of GROUP to say that ARTICLES are read."
6281   (let* ((num 0)
6282          (entry (gnus-group-entry group))
6283          (info (nth 2 entry))
6284          (active (gnus-active group))
6285          (set-marks
6286           (or gnus-propagate-marks
6287               (gnus-method-option-p
6288                (gnus-find-method-for-group group)
6289                'server-marks)))
6290          range)
6291     (if (not entry)
6292         ;; Group that Gnus doesn't know exists, but still allow the
6293         ;; backend to set marks.
6294         (when set-marks
6295           (gnus-request-set-mark
6296            group (list (list (gnus-compress-sequence (sort articles #'<))
6297                              'add '(read)))))
6298       ;; Normal, subscribed groups.
6299       (setq range (gnus-compute-read-articles group articles))
6300       (with-current-buffer gnus-group-buffer
6301         (gnus-undo-register
6302           `(progn
6303              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6304              (gnus-info-set-read ',info ',(gnus-info-read info))
6305              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6306              (when ,set-marks
6307                (gnus-request-set-mark
6308                 ,group (list (list ',range 'del '(read)))))
6309              (gnus-group-update-group ,group t))))
6310       ;; Add the read articles to the range.
6311       (gnus-info-set-read info range)
6312       (when set-marks
6313         (gnus-request-set-mark group (list (list range 'add '(read)))))
6314       ;; Then we have to re-compute how many unread
6315       ;; articles there are in this group.
6316       (when active
6317         (cond
6318          ((not range)
6319           (setq num (- (1+ (cdr active)) (car active))))
6320          ((not (listp (cdr range)))
6321           (setq num (- (cdr active) (- (1+ (cdr range))
6322                                        (car range)))))
6323          (t
6324           (while range
6325             (if (numberp (car range))
6326                 (setq num (1+ num))
6327               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6328             (setq range (cdr range)))
6329           (setq num (- (cdr active) num))))
6330         ;; Update the number of unread articles.
6331         (setcar entry num)
6332         ;; Update the group buffer.
6333         (unless (gnus-ephemeral-group-p group)
6334           (gnus-group-update-group group t))))))
6335
6336 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6337   (let ((cur nntp-server-buffer)
6338         (dependencies
6339          (or dependencies
6340              (with-current-buffer gnus-summary-buffer
6341                gnus-newsgroup-dependencies)))
6342         headers id end ref number
6343         (mail-parse-charset gnus-newsgroup-charset)
6344         (mail-parse-ignored-charsets
6345          (save-current-buffer (condition-case nil
6346                                   (set-buffer gnus-summary-buffer)
6347                                 (error))
6348                               gnus-newsgroup-ignored-charsets)))
6349     (with-current-buffer nntp-server-buffer
6350       ;; Translate all TAB characters into SPACE characters.
6351       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6352       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6353       (ietf-drums-unfold-fws)
6354       (gnus-run-hooks 'gnus-parse-headers-hook)
6355       (let ((case-fold-search t)
6356             in-reply-to header p lines chars)
6357         (goto-char (point-min))
6358         ;; Search to the beginning of the next header.  Error messages
6359         ;; do not begin with 2 or 3.
6360         (while (re-search-forward "^[23][0-9]+ " nil t)
6361           (setq id nil
6362                 ref nil)
6363           ;; This implementation of this function, with nine
6364           ;; search-forwards instead of the one re-search-forward and
6365           ;; a case (which basically was the old function) is actually
6366           ;; about twice as fast, even though it looks messier.  You
6367           ;; can't have everything, I guess.  Speed and elegance
6368           ;; doesn't always go hand in hand.
6369           (setq
6370            header
6371            (vector
6372             ;; Number.
6373             (prog1
6374                 (setq number (read cur))
6375               (end-of-line)
6376               (setq p (point))
6377               (narrow-to-region (point)
6378                                 (or (and (search-forward "\n.\n" nil t)
6379                                          (- (point) 2))
6380                                     (point))))
6381             ;; Subject.
6382             (progn
6383               (goto-char p)
6384               (if (search-forward "\nsubject:" nil t)
6385                   (funcall gnus-decode-encoded-word-function
6386                            (nnheader-header-value))
6387                 "(none)"))
6388             ;; From.
6389             (progn
6390               (goto-char p)
6391               (if (search-forward "\nfrom:" nil t)
6392                   (funcall gnus-decode-encoded-address-function
6393                            (nnheader-header-value))
6394                 "(nobody)"))
6395             ;; Date.
6396             (progn
6397               (goto-char p)
6398               (if (search-forward "\ndate:" nil t)
6399                   (nnheader-header-value) ""))
6400             ;; Message-ID.
6401             (progn
6402               (goto-char p)
6403               (setq id (if (re-search-forward
6404                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6405                            ;; We do it this way to make sure the Message-ID
6406                            ;; is (somewhat) syntactically valid.
6407                            (buffer-substring (match-beginning 1)
6408                                              (match-end 1))
6409                          ;; If there was no message-id, we just fake one
6410                          ;; to make subsequent routines simpler.
6411                          (nnheader-generate-fake-message-id number))))
6412             ;; References.
6413             (progn
6414               (goto-char p)
6415               (if (search-forward "\nreferences:" nil t)
6416                   (progn
6417                     (setq end (point))
6418                     (prog1
6419                         (nnheader-header-value)
6420                       (setq ref
6421                             (buffer-substring
6422                              (progn
6423                                (end-of-line)
6424                                (search-backward ">" end t)
6425                                (1+ (point)))
6426                              (progn
6427                                (search-backward "<" end t)
6428                                (point))))))
6429                 ;; Get the references from the in-reply-to header if there
6430                 ;; were no references and the in-reply-to header looks
6431                 ;; promising.
6432                 (if (and (search-forward "\nin-reply-to:" nil t)
6433                          (setq in-reply-to (nnheader-header-value))
6434                          (string-match "<[^>]+>" in-reply-to))
6435                     (let (ref2)
6436                       (setq ref (substring in-reply-to (match-beginning 0)
6437                                            (match-end 0)))
6438                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6439                         (setq ref2 (substring in-reply-to (match-beginning 0)
6440                                               (match-end 0)))
6441                         (when (> (length ref2) (length ref))
6442                           (setq ref ref2)))
6443                       ref)
6444                   (setq ref nil))))
6445             ;; Chars.
6446             (progn
6447               (goto-char p)
6448               (if (search-forward "\nchars: " nil t)
6449                   (if (numberp (setq chars (ignore-errors (read cur))))
6450                       chars -1)
6451                 -1))
6452             ;; Lines.
6453             (progn
6454               (goto-char p)
6455               (if (search-forward "\nlines: " nil t)
6456                   (if (numberp (setq lines (ignore-errors (read cur))))
6457                       lines -1)
6458                 -1))
6459             ;; Xref.
6460             (progn
6461               (goto-char p)
6462               (and (search-forward "\nxref:" nil t)
6463                    (nnheader-header-value)))
6464             ;; Extra.
6465             (when gnus-extra-headers
6466               (let ((extra gnus-extra-headers)
6467                     out)
6468                 (while extra
6469                   (goto-char p)
6470                   (when (search-forward
6471                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6472                     (push (cons (car extra) (nnheader-header-value))
6473                           out))
6474                   (pop extra))
6475                 out))))
6476           (when (equal id ref)
6477             (setq ref nil))
6478
6479           (when gnus-alter-header-function
6480             (funcall gnus-alter-header-function header)
6481             (setq id (mail-header-id header)
6482                   ref (gnus-parent-id (mail-header-references header))))
6483
6484           (when (setq header
6485                       (gnus-dependencies-add-header
6486                        header dependencies force-new))
6487             (push header headers))
6488           (goto-char (point-max))
6489           (widen))
6490         (nreverse headers)))))
6491
6492 ;; Goes through the xover lines and returns a list of vectors
6493 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6494                                                   force-new dependencies
6495                                                   group also-fetch-heads)
6496   "Parse the news overview data in the server buffer.
6497 Return a list of headers that match SEQUENCE (see
6498 `nntp-retrieve-headers')."
6499   ;; Get the Xref when the users reads the articles since most/some
6500   ;; NNTP servers do not include Xrefs when using XOVER.
6501   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6502   (let ((mail-parse-charset gnus-newsgroup-charset)
6503         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6504         (cur nntp-server-buffer)
6505         (dependencies (or dependencies gnus-newsgroup-dependencies))
6506         (allp (cond
6507                ((eq gnus-read-all-available-headers t)
6508                 t)
6509                ((and (stringp gnus-read-all-available-headers)
6510                      group)
6511                 (string-match gnus-read-all-available-headers group))
6512                (t
6513                 nil)))
6514         number headers header)
6515     (with-current-buffer nntp-server-buffer
6516       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6517       ;; Allow the user to mangle the headers before parsing them.
6518       (gnus-run-hooks 'gnus-parse-headers-hook)
6519       (goto-char (point-min))
6520       (gnus-parse-without-error
6521         (while (and (or sequence allp)
6522                     (not (eobp)))
6523           (setq number (read cur))
6524           (when (not allp)
6525             (while (and sequence
6526                         (< (car sequence) number))
6527               (setq sequence (cdr sequence))))
6528           (when (and (or allp
6529                          (and sequence
6530                               (eq number (car sequence))))
6531                      (progn
6532                        (setq sequence (cdr sequence))
6533                        (setq header (inline
6534                                       (gnus-nov-parse-line
6535                                        number dependencies force-new)))))
6536             (push header headers))
6537           (forward-line 1)))
6538       ;; A common bug in inn is that if you have posted an article and
6539       ;; then retrieves the active file, it will answer correctly --
6540       ;; the new article is included.  However, a NOV entry for the
6541       ;; article may not have been generated yet, so this may fail.
6542       ;; We work around this problem by retrieving the last few
6543       ;; headers using HEAD.
6544       (if (or (not also-fetch-heads)
6545               (not sequence))
6546           ;; We (probably) got all the headers.
6547           (nreverse headers)
6548         (let ((gnus-nov-is-evil t))
6549           (nconc
6550            (nreverse headers)
6551            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6552              (gnus-get-newsgroup-headers))))))))
6553
6554 (defun gnus-article-get-xrefs ()
6555   "Fill in the Xref value in `gnus-current-headers', if necessary.
6556 This is meant to be called in `gnus-article-internal-prepare-hook'."
6557   (let ((headers (with-current-buffer gnus-summary-buffer
6558                    gnus-current-headers)))
6559     (or (not gnus-use-cross-reference)
6560         (not headers)
6561         (and (mail-header-xref headers)
6562              (not (string= (mail-header-xref headers) "")))
6563         (let ((case-fold-search t)
6564               xref)
6565           (save-restriction
6566             (nnheader-narrow-to-headers)
6567             (goto-char (point-min))
6568             (when (or (and (not (eobp))
6569                            (eq (downcase (char-after)) ?x)
6570                            (looking-at "Xref:"))
6571                       (search-forward "\nXref:" nil t))
6572               (goto-char (1+ (match-end 0)))
6573               (setq xref (buffer-substring (point) (point-at-eol)))
6574               (mail-header-set-xref headers xref)))))))
6575
6576 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6577   "Find article ID and insert the summary line for that article.
6578 OLD-HEADER can either be a header or a line number to insert
6579 the subject line on.
6580 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6581 and OLD-HEADER will be used when the summary line is inserted,
6582 too, instead of trying to fetch new headers."
6583   (let* ((line (and (numberp old-header) old-header))
6584          (old-header (and (vectorp old-header) old-header))
6585          (header (cond ((and old-header use-old-header)
6586                         old-header)
6587                        ((and (numberp id)
6588                              (gnus-number-to-header id))
6589                         (gnus-number-to-header id))
6590                        (t
6591                         (gnus-read-header id))))
6592          (number (and (numberp id) id))
6593          d)
6594     (when header
6595       ;; Rebuild the thread that this article is part of and go to the
6596       ;; article we have fetched.
6597       (when (and (not gnus-show-threads)
6598                  old-header)
6599         (when (and number
6600                    (setq d (gnus-data-find (mail-header-number old-header))))
6601           (goto-char (gnus-data-pos d))
6602           (gnus-data-remove
6603            number
6604            (- (point-at-bol)
6605               (prog1
6606                   (1+ (point-at-eol))
6607                 (gnus-delete-line))))))
6608       ;; Remove list identifiers from subject.
6609       (let ((gnus-newsgroup-headers (list header)))
6610         (gnus-summary-remove-list-identifiers))
6611       (when old-header
6612         (mail-header-set-number header (mail-header-number old-header)))
6613       (setq gnus-newsgroup-sparse
6614             (delq (setq number (mail-header-number header))
6615                   gnus-newsgroup-sparse))
6616       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6617       (push number gnus-newsgroup-limit)
6618       (gnus-rebuild-thread (mail-header-id header) line)
6619       (gnus-summary-goto-subject number nil t))
6620     (when (and (numberp number)
6621                (> number 0))
6622       ;; We have to update the boundaries even if we can't fetch the
6623       ;; article if ID is a number -- so that the next `P' or `N'
6624       ;; command will fetch the previous (or next) article even
6625       ;; if the one we tried to fetch this time has been canceled.
6626       (when (> number gnus-newsgroup-end)
6627         (setq gnus-newsgroup-end number))
6628       (when (< number gnus-newsgroup-begin)
6629         (setq gnus-newsgroup-begin number))
6630       (setq gnus-newsgroup-unselected
6631             (delq number gnus-newsgroup-unselected)))
6632     ;; Report back a success?
6633     (and header (mail-header-number header))))
6634
6635 ;;; Process/prefix in the summary buffer
6636
6637 (defun gnus-summary-work-articles (n)
6638   "Return a list of articles to be worked upon.
6639 The prefix argument, the list of process marked articles, and the
6640 current article will be taken into consideration."
6641   (with-current-buffer gnus-summary-buffer
6642     (cond
6643      (n
6644       ;; A numerical prefix has been given.
6645       (setq n (prefix-numeric-value n))
6646       (let ((backward (< n 0))
6647             (n (abs (prefix-numeric-value n)))
6648             articles article)
6649         (save-excursion
6650           (while
6651               (and (> n 0)
6652                    (push (setq article (gnus-summary-article-number))
6653                          articles)
6654                    (if backward
6655                        (gnus-summary-find-prev nil article)
6656                      (gnus-summary-find-next nil article)))
6657             (decf n)))
6658         (nreverse articles)))
6659      ((and (gnus-region-active-p) (mark))
6660       (message "region active")
6661       ;; Work on the region between point and mark.
6662       (let ((max (max (point) (mark)))
6663             articles article)
6664         (save-excursion
6665           (goto-char (min (point) (mark)))
6666           (while
6667               (and
6668                (push (setq article (gnus-summary-article-number)) articles)
6669                (gnus-summary-find-next nil article)
6670                (< (point) max)))
6671           (nreverse articles))))
6672      (gnus-newsgroup-processable
6673       ;; There are process-marked articles present.
6674       ;; Save current state.
6675       (gnus-summary-save-process-mark)
6676       ;; Return the list.
6677       (reverse gnus-newsgroup-processable))
6678      (t
6679       ;; Just return the current article.
6680       (list (gnus-summary-article-number))))))
6681
6682 (defmacro gnus-summary-iterate (arg &rest forms)
6683   "Iterate over the process/prefixed articles and do FORMS.
6684 ARG is the interactive prefix given to the command.  FORMS will be
6685 executed with point over the summary line of the articles."
6686   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6687     `(let ((,articles (gnus-summary-work-articles ,arg)))
6688        (while ,articles
6689          (gnus-summary-goto-subject (car ,articles))
6690          ,@forms
6691          (pop ,articles)))))
6692
6693 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6694 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6695
6696 (defun gnus-summary-save-process-mark ()
6697   "Push the current set of process marked articles on the stack."
6698   (interactive)
6699   (push (copy-sequence gnus-newsgroup-processable)
6700         gnus-newsgroup-process-stack))
6701
6702 (defun gnus-summary-kill-process-mark ()
6703   "Push the current set of process marked articles on the stack and unmark."
6704   (interactive)
6705   (gnus-summary-save-process-mark)
6706   (gnus-summary-unmark-all-processable))
6707
6708 (defun gnus-summary-yank-process-mark ()
6709   "Pop the last process mark state off the stack and restore it."
6710   (interactive)
6711   (unless gnus-newsgroup-process-stack
6712     (error "Empty mark stack"))
6713   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6714
6715 (defun gnus-summary-process-mark-set (set)
6716   "Make SET into the current process marked articles."
6717   (gnus-summary-unmark-all-processable)
6718   (mapc 'gnus-summary-set-process-mark set))
6719
6720 ;;; Searching and stuff
6721
6722 (defun gnus-summary-search-group (&optional backward use-level)
6723   "Search for next unread newsgroup.
6724 If optional argument BACKWARD is non-nil, search backward instead."
6725   (with-current-buffer gnus-group-buffer
6726     (when (gnus-group-search-forward
6727            backward nil (if use-level (gnus-group-group-level) nil))
6728       (gnus-group-group-name))))
6729
6730 (defun gnus-summary-best-group (&optional exclude-group)
6731   "Find the name of the best unread group.
6732 If EXCLUDE-GROUP, do not go to this group."
6733   (with-current-buffer gnus-group-buffer
6734     (save-excursion
6735       (gnus-group-best-unread-group exclude-group))))
6736
6737 (defun gnus-summary-find-next (&optional unread article backward)
6738   (if backward
6739       (gnus-summary-find-prev unread article)
6740     (let* ((dummy (gnus-summary-article-intangible-p))
6741            (article (or article (gnus-summary-article-number)))
6742            (data (gnus-data-find-list article))
6743            result)
6744       (when (and (not dummy)
6745                  (or (not gnus-summary-check-current)
6746                      (not unread)
6747                      (not (gnus-data-unread-p (car data)))))
6748         (setq data (cdr data)))
6749       (when (setq result
6750                   (if unread
6751                       (progn
6752                         (while data
6753                           (unless (memq (gnus-data-number (car data))
6754                                         (cond
6755                                          ((eq gnus-auto-goto-ignores
6756                                               'always-undownloaded)
6757                                           gnus-newsgroup-undownloaded)
6758                                          (gnus-plugged
6759                                           nil)
6760                                          ((eq gnus-auto-goto-ignores
6761                                               'unfetched)
6762                                           gnus-newsgroup-unfetched)
6763                                          ((eq gnus-auto-goto-ignores
6764                                               'undownloaded)
6765                                           gnus-newsgroup-undownloaded)))
6766                             (when (gnus-data-unread-p (car data))
6767                               (setq result (car data)
6768                                     data nil)))
6769                           (setq data (cdr data)))
6770                         result)
6771                     (car data)))
6772         (goto-char (gnus-data-pos result))
6773         (gnus-data-number result)))))
6774
6775 (defun gnus-summary-find-prev (&optional unread article)
6776   (let* ((eobp (eobp))
6777          (article (or article (gnus-summary-article-number)))
6778          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6779          result)
6780     (when (and (not eobp)
6781                (or (not gnus-summary-check-current)
6782                    (not unread)
6783                    (not (gnus-data-unread-p (car data)))))
6784       (setq data (cdr data)))
6785     (when (setq result
6786                 (if unread
6787                     (progn
6788                       (while data
6789                         (unless (memq (gnus-data-number (car data))
6790                                       (cond
6791                                        ((eq gnus-auto-goto-ignores
6792                                             'always-undownloaded)
6793                                         gnus-newsgroup-undownloaded)
6794                                        (gnus-plugged
6795                                         nil)
6796                                        ((eq gnus-auto-goto-ignores
6797                                             'unfetched)
6798                                         gnus-newsgroup-unfetched)
6799                                        ((eq gnus-auto-goto-ignores
6800                                             'undownloaded)
6801                                         gnus-newsgroup-undownloaded)))
6802                           (when (gnus-data-unread-p (car data))
6803                             (setq result (car data)
6804                                   data nil)))
6805                         (setq data (cdr data)))
6806                       result)
6807                   (car data)))
6808       (goto-char (gnus-data-pos result))
6809       (gnus-data-number result))))
6810
6811 (defun gnus-summary-find-subject (subject &optional unread backward article)
6812   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6813          (article (or article (gnus-summary-article-number)))
6814          (articles (gnus-data-list backward))
6815          (arts (gnus-data-find-list article articles))
6816          result)
6817     (when (or (not gnus-summary-check-current)
6818               (not unread)
6819               (not (gnus-data-unread-p (car arts))))
6820       (setq arts (cdr arts)))
6821     (while arts
6822       (and (or (not unread)
6823                (gnus-data-unread-p (car arts)))
6824            (vectorp (gnus-data-header (car arts)))
6825            (gnus-subject-equal
6826             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6827            (setq result (car arts)
6828                  arts nil))
6829       (setq arts (cdr arts)))
6830     (and result
6831          (goto-char (gnus-data-pos result))
6832          (gnus-data-number result))))
6833
6834 (defun gnus-summary-search-forward (&optional unread subject backward)
6835   "Search forward for an article.
6836 If UNREAD, look for unread articles.  If SUBJECT, look for
6837 articles with that subject.  If BACKWARD, search backward instead."
6838   (cond (subject (gnus-summary-find-subject subject unread backward))
6839         (backward (gnus-summary-find-prev unread))
6840         (t (gnus-summary-find-next unread))))
6841
6842 (defun gnus-recenter (&optional n)
6843   "Center point in window and redisplay frame.
6844 Also do horizontal recentering."
6845   (interactive "P")
6846   (when (and gnus-auto-center-summary
6847              (not (eq gnus-auto-center-summary 'vertical)))
6848     (gnus-horizontal-recenter))
6849   (if (fboundp 'recenter-top-bottom)
6850       (recenter-top-bottom n)
6851     (recenter n)))
6852
6853 (put 'gnus-recenter 'isearch-scroll t)
6854
6855 (defun gnus-forward-line-ignore-invisible (n)
6856   "Move N lines forward (backward if N is negative).
6857 Like forward-line, but skip over (and don't count) invisible lines."
6858   (let (done)
6859     (while (and (> n 0) (not done))
6860       ;; If the following character is currently invisible,
6861       ;; skip all characters with that same `invisible' property value.
6862       (while (gnus-invisible-p (point))
6863         (goto-char (gnus-next-char-property-change (point))))
6864       (forward-line 1)
6865       (if (eobp)
6866           (setq done t)
6867         (setq n (1- n))))
6868     (while (and (< n 0) (not done))
6869       (forward-line -1)
6870       (if (bobp) (setq done t)
6871         (setq n (1+ n))
6872         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6873           (goto-char (gnus-previous-char-property-change (point))))))))
6874
6875 (defun gnus-summary-recenter ()
6876   "Center point in the summary window.
6877 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6878 displayed, no centering will be performed."
6879   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6880   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6881   (interactive)
6882   ;; The user has to want it.
6883   (when gnus-auto-center-summary
6884     (let* ((top (cond ((< (window-height) 4) 0)
6885                       ((< (window-height) 7) 1)
6886                       (t (if (numberp gnus-auto-center-summary)
6887                              gnus-auto-center-summary
6888                            (/ (1- (window-height)) 2)))))
6889            (height (1- (window-height)))
6890            (bottom (save-excursion
6891                      (goto-char (point-max))
6892                      (gnus-forward-line-ignore-invisible (- height))
6893                      (point)))
6894            (window (get-buffer-window (current-buffer))))
6895       (when (get-buffer-window gnus-article-buffer)
6896         ;; Only do recentering when the article buffer is displayed,
6897         ;; Set the window start to either `bottom', which is the biggest
6898         ;; possible valid number, or the second line from the top,
6899         ;; whichever is the least.
6900         (let ((top-pos (save-excursion
6901                          (gnus-forward-line-ignore-invisible (- top))
6902                          (point))))
6903           (if (> bottom top-pos)
6904               ;; Keep the second line from the top visible
6905               (set-window-start window top-pos)
6906             ;; Try to keep the bottom line visible; if it's partially
6907             ;; obscured, either scroll one more line to make it fully
6908             ;; visible, or revert to using TOP-POS.
6909             (save-excursion
6910               (goto-char (point-max))
6911               (gnus-forward-line-ignore-invisible -1)
6912               (let ((last-line-start (point)))
6913                 (goto-char bottom)
6914                 (set-window-start window (point) t)
6915                 (when (not (pos-visible-in-window-p last-line-start window))
6916                   (gnus-forward-line-ignore-invisible 1)
6917                   (set-window-start window (min (point) top-pos) t)))))))
6918       ;; Do horizontal recentering while we're at it.
6919       (when (and (get-buffer-window (current-buffer) t)
6920                  (not (eq gnus-auto-center-summary 'vertical)))
6921         (let ((selected (selected-window)))
6922           (select-window (get-buffer-window (current-buffer) t))
6923           (gnus-summary-position-point)
6924           (gnus-horizontal-recenter)
6925           (select-window selected))))))
6926
6927 (defun gnus-summary-jump-to-group (newsgroup)
6928   "Move point to NEWSGROUP in group mode buffer."
6929   ;; Keep update point of group mode buffer if visible.
6930   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6931       (save-window-excursion
6932         ;; Take care of tree window mode.
6933         (when (get-buffer-window gnus-group-buffer)
6934           (pop-to-buffer gnus-group-buffer))
6935         (gnus-group-jump-to-group newsgroup))
6936     (save-excursion
6937       ;; Take care of tree window mode.
6938       (if (get-buffer-window gnus-group-buffer 0)
6939           (pop-to-buffer gnus-group-buffer)
6940         (set-buffer gnus-group-buffer))
6941       (gnus-group-jump-to-group newsgroup))))
6942
6943 ;; This function returns a list of article numbers based on the
6944 ;; difference between the ranges of read articles in this group and
6945 ;; the range of active articles.
6946 (defun gnus-list-of-unread-articles (group)
6947   (let* ((read (gnus-info-read (gnus-get-info group)))
6948          (active (or (gnus-active group) (gnus-activate-group group)))
6949          (last (or (cdr active)
6950                    (error "Group %s couldn't be activated " group)))
6951          (bottom (if gnus-newsgroup-maximum-articles
6952                      (max (car active)
6953                           (- last gnus-newsgroup-maximum-articles -1))
6954                    (car active)))
6955          first nlast unread)
6956     ;; If none are read, then all are unread.
6957     (if (not read)
6958         (setq first bottom)
6959       ;; If the range of read articles is a single range, then the
6960       ;; first unread article is the article after the last read
6961       ;; article.  Sounds logical, doesn't it?
6962       (if (and (not (listp (cdr read)))
6963                (or (< (car read) bottom)
6964                    (progn (setq read (list read))
6965                           nil)))
6966           (setq first (max bottom (1+ (cdr read))))
6967         ;; `read' is a list of ranges.
6968         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6969                                   (caar read)))
6970                   1)
6971           (setq first bottom))
6972         (while read
6973           (when first
6974             (while (< first nlast)
6975               (setq unread (cons first unread)
6976                     first (1+ first))))
6977           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6978           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6979           (setq read (cdr read)))))
6980     ;; And add the last unread articles.
6981     (while (<= first last)
6982       (setq unread (cons first unread)
6983             first (1+ first)))
6984     ;; Return the list of unread articles.
6985     (delq 0 (nreverse unread))))
6986
6987 (defun gnus-list-of-read-articles (group)
6988   "Return a list of unread, unticked and non-dormant articles."
6989   (let* ((info (gnus-get-info group))
6990          (marked (gnus-info-marks info))
6991          (active (gnus-active group)))
6992     (and info active
6993          (gnus-list-range-difference
6994           (gnus-list-range-difference
6995            (gnus-sorted-complement
6996             (gnus-uncompress-range
6997              (if gnus-newsgroup-maximum-articles
6998                  (cons (max (car active)
6999                             (- (cdr active)
7000                                gnus-newsgroup-maximum-articles
7001                                -1))
7002                        (cdr active))
7003                active))
7004             (gnus-list-of-unread-articles group))
7005            (cdr (assq 'dormant marked)))
7006           (cdr (assq 'tick marked))))))
7007
7008 ;; This function returns a sequence of article numbers based on the
7009 ;; difference between the ranges of read articles in this group and
7010 ;; the range of active articles.
7011 (defun gnus-sequence-of-unread-articles (group)
7012   (let* ((read (gnus-info-read (gnus-get-info group)))
7013          (active (or (gnus-active group) (gnus-activate-group group)))
7014          (last (cdr active))
7015          (bottom (if gnus-newsgroup-maximum-articles
7016                      (max (car active)
7017                           (- last gnus-newsgroup-maximum-articles -1))
7018                    (car active)))
7019          first nlast unread)
7020     ;; If none are read, then all are unread.
7021     (if (not read)
7022         (setq first bottom)
7023       ;; If the range of read articles is a single range, then the
7024       ;; first unread article is the article after the last read
7025       ;; article.  Sounds logical, doesn't it?
7026       (if (and (not (listp (cdr read)))
7027                (or (< (car read) bottom)
7028                    (progn (setq read (list read))
7029                           nil)))
7030           (setq first (max bottom (1+ (cdr read))))
7031         ;; `read' is a list of ranges.
7032         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7033                                   (caar read)))
7034                   1)
7035           (setq first bottom))
7036         (while read
7037           (when first
7038             (push (cons first nlast) unread))
7039           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7040           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7041           (setq read (cdr read)))))
7042     ;; And add the last unread articles.
7043     (cond ((not (and first last))
7044            nil)
7045           ((< first last)
7046            (push (cons first last) unread))
7047           ((= first last)
7048            (push first unread)))
7049     ;; Return the sequence of unread articles.
7050     (delq 0 (nreverse unread))))
7051
7052 ;; Various summary commands
7053
7054 (defun gnus-summary-select-article-buffer ()
7055   "Reconfigure windows to show the article buffer.
7056 If `gnus-widen-article-window' is set, show only the article
7057 buffer."
7058   (interactive)
7059   (if (not (gnus-buffer-live-p gnus-article-buffer))
7060       (error "There is no article buffer for this summary buffer")
7061     (unless (get-buffer-window gnus-article-buffer)
7062       (gnus-summary-show-article))
7063     (gnus-configure-windows
7064      (if gnus-widen-article-window
7065          'only-article
7066        'article)
7067      t)
7068     (select-window (get-buffer-window gnus-article-buffer))))
7069
7070 (defun gnus-summary-universal-argument (arg)
7071   "Perform any operation on all articles that are process/prefixed."
7072   (interactive "P")
7073   (let ((articles (gnus-summary-work-articles arg))
7074         func article)
7075     (if (eq
7076          (setq
7077           func
7078           (key-binding
7079            (read-key-sequence
7080             (substitute-command-keys
7081              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7082          'undefined)
7083         (gnus-error 1 "Undefined key")
7084       (save-excursion
7085         (while articles
7086           (gnus-summary-goto-subject (setq article (pop articles)))
7087           (let (gnus-newsgroup-processable)
7088             (command-execute func))
7089           (gnus-summary-remove-process-mark article)))))
7090   (gnus-summary-position-point))
7091
7092 (defun gnus-summary-toggle-truncation (&optional arg)
7093   "Toggle truncation of summary lines.
7094 With ARG, turn line truncation on if ARG is positive."
7095   (interactive "P")
7096   (setq truncate-lines
7097         (if (null arg) (not truncate-lines)
7098           (> (prefix-numeric-value arg) 0)))
7099   (redraw-display))
7100
7101 (defun gnus-summary-find-for-reselect ()
7102   "Return the number of an article to stay on across a reselect.
7103 The current article is considered, then following articles, then previous
7104 articles.  An article is sought which is not canceled and isn't a temporary
7105 insertion from another group.  If there's no such then return a dummy 0."
7106   (let (found)
7107     (dolist (rev '(nil t))
7108       (unless found      ; don't demand the reverse list if we don't need it
7109         (let ((data (gnus-data-find-list
7110                      (gnus-summary-article-number) (gnus-data-list rev))))
7111           (while (and data (not found))
7112             (if (and (< 0 (gnus-data-number (car data)))
7113                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7114                 (setq found (gnus-data-number (car data))))
7115             (setq data (cdr data))))))
7116     (or found 0)))
7117
7118 (defun gnus-summary-reselect-current-group (&optional all rescan)
7119   "Exit and then reselect the current newsgroup.
7120 The prefix argument ALL means to select all articles."
7121   (interactive "P")
7122   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7123     (error "Ephemeral groups can't be reselected"))
7124   (let ((current-subject (gnus-summary-find-for-reselect))
7125         (group gnus-newsgroup-name))
7126     (setq gnus-newsgroup-begin nil)
7127     (gnus-summary-exit nil 'leave-hidden)
7128     ;; We have to adjust the point of group mode buffer because
7129     ;; point was moved to the next unread newsgroup by exiting.
7130     (gnus-summary-jump-to-group group)
7131     (when rescan
7132       (save-excursion
7133         (gnus-group-get-new-news-this-group 1)))
7134     (gnus-group-read-group all t)
7135     (gnus-summary-goto-subject current-subject nil t)))
7136
7137 (defun gnus-summary-rescan-group (&optional all)
7138   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7139   (interactive "P")
7140   (let ((config gnus-current-window-configuration))
7141     (gnus-summary-reselect-current-group all t)
7142     (gnus-configure-windows config)
7143     (when (eq config 'article)
7144       (gnus-summary-select-article))))
7145
7146 (defun gnus-summary-update-info (&optional non-destructive)
7147   (save-excursion
7148     (let ((group gnus-newsgroup-name))
7149       (when group
7150         (when gnus-newsgroup-kill-headers
7151           (setq gnus-newsgroup-killed
7152                 (gnus-compress-sequence
7153                  (gnus-sorted-union
7154                   (gnus-list-range-intersection
7155                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7156                   gnus-newsgroup-unreads)
7157                  t)))
7158         (unless (listp (cdr gnus-newsgroup-killed))
7159           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7160         (let ((headers gnus-newsgroup-headers)
7161               (ephemeral-p (gnus-ephemeral-group-p group))
7162               info)
7163           (unless ephemeral-p
7164             (setq info (copy-sequence (gnus-get-info group))
7165                   info (delq (gnus-info-params info) info)))
7166           ;; Set the new ranges of read articles.
7167           (with-current-buffer gnus-group-buffer
7168             (gnus-undo-force-boundary))
7169           (gnus-update-read-articles
7170            group (gnus-sorted-union
7171                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7172           ;; Set the current article marks.
7173           (let ((gnus-newsgroup-scored
7174                  (if (and (not gnus-save-score)
7175                           (not non-destructive))
7176                      nil
7177                    gnus-newsgroup-scored)))
7178             (save-excursion
7179               (gnus-update-marks)))
7180           ;; Do the cross-ref thing.
7181           (when gnus-use-cross-reference
7182             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7183           ;; Do not switch windows but change the buffer to work.
7184           (set-buffer gnus-group-buffer)
7185           (unless ephemeral-p
7186             (gnus-group-update-group
7187              group nil
7188              (equal info
7189                     (setq info (copy-sequence (gnus-get-info group))
7190                           info (delq (gnus-info-params info) info))))))))))
7191
7192 (defun gnus-summary-save-newsrc (&optional force)
7193   "Save the current number of read/marked articles in the dribble buffer.
7194 The dribble buffer will then be saved.
7195 If FORCE (the prefix), also save the .newsrc file(s)."
7196   (interactive "P")
7197   (gnus-summary-update-info t)
7198   (if force
7199       (gnus-save-newsrc-file)
7200     (gnus-dribble-save)))
7201
7202 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7203
7204 (defun gnus-summary-exit (&optional temporary leave-hidden)
7205   "Exit reading current newsgroup, and then return to group selection mode.
7206 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7207   (interactive)
7208   (gnus-set-global-variables)
7209   (when (gnus-buffer-live-p gnus-article-buffer)
7210     (with-current-buffer gnus-article-buffer
7211       (mm-destroy-parts gnus-article-mime-handles)
7212       ;; Set it to nil for safety reason.
7213       (setq gnus-article-mime-handle-alist nil)
7214       (setq gnus-article-mime-handles nil)))
7215   (gnus-kill-save-kill-buffer)
7216   (gnus-async-halt-prefetch)
7217   (let* ((group gnus-newsgroup-name)
7218          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7219          (gnus-group-is-exiting-p t)
7220          (article-buffer gnus-article-buffer)
7221          (original-article-buffer gnus-original-article-buffer)
7222          (mode major-mode)
7223          (group-point nil)
7224          (buf (current-buffer))
7225          ;; `gnus-single-article-buffer' is nil buffer-locally in
7226          ;; ephemeral group of which summary buffer will be killed,
7227          ;; but the global value may be non-nil.
7228          (single-article-buffer gnus-single-article-buffer))
7229     (unless quit-config
7230       ;; Do adaptive scoring, and possibly save score files.
7231       (when gnus-newsgroup-adaptive
7232         (gnus-score-adaptive))
7233       (when gnus-use-scoring
7234         (gnus-score-save)))
7235     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7236     (when gnus-use-cache
7237       (gnus-cache-possibly-remove-articles)
7238       (gnus-cache-save-buffers))
7239     (gnus-async-prefetch-remove-group group)
7240     (when gnus-suppress-duplicates
7241       (gnus-dup-enter-articles))
7242     (when gnus-use-trees
7243       (gnus-tree-close group))
7244     (when gnus-use-cache
7245       (gnus-cache-write-active))
7246     ;; Remove entries for this group.
7247     (nnmail-purge-split-history (gnus-group-real-name group))
7248     ;; Make all changes in this group permanent.
7249     (unless quit-config
7250       (gnus-run-hooks 'gnus-exit-group-hook)
7251       (gnus-summary-update-info))
7252     (gnus-close-group group)
7253     ;; Make sure where we were, and go to next newsgroup.
7254     (set-buffer gnus-group-buffer)
7255     (unless quit-config
7256       (gnus-group-jump-to-group group))
7257     (gnus-run-hooks 'gnus-summary-exit-hook)
7258     (unless (or quit-config
7259                 (not gnus-summary-next-group-on-exit)
7260                 ;; If this group has disappeared from the summary
7261                 ;; buffer, don't skip forwards.
7262                 (not (string= group (gnus-group-group-name))))
7263       (gnus-group-next-unread-group 1))
7264     (setq group-point (point))
7265     (if temporary
7266         nil                             ;Nothing to do.
7267       (set-buffer buf)
7268       (if (not gnus-kill-summary-on-exit)
7269           (progn
7270             (gnus-deaden-summary)
7271             (setq mode nil))
7272         (when (get-buffer gnus-article-buffer)
7273           (bury-buffer gnus-article-buffer))
7274         ;; Return to group mode buffer.
7275         (when (eq mode 'gnus-summary-mode)
7276           (gnus-kill-buffer buf)))
7277
7278       (setq gnus-current-select-method gnus-select-method)
7279       (set-buffer gnus-group-buffer)
7280       (if quit-config
7281           (gnus-handle-ephemeral-exit quit-config)
7282         (goto-char group-point)
7283         ;; If gnus-group-buffer is already displayed, make sure we also move
7284         ;; the cursor in the window that displays it.
7285         (let ((win (get-buffer-window (current-buffer) 0)))
7286           (if win (set-window-point win (point))))
7287         (unless leave-hidden
7288           (gnus-configure-windows 'group 'force)))
7289
7290       ;; If we have several article buffers, we kill them at exit.
7291       (unless single-article-buffer
7292         (when (gnus-buffer-live-p article-buffer)
7293           (with-current-buffer article-buffer
7294             ;; Don't kill sticky article buffers
7295             (unless (eq major-mode 'gnus-sticky-article-mode)
7296               (gnus-kill-buffer article-buffer)
7297               (setq gnus-article-current nil))))
7298         (gnus-kill-buffer original-article-buffer))
7299
7300       ;; Clear the current group name.
7301       (unless quit-config
7302         (setq gnus-newsgroup-name nil)))))
7303
7304 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7305 (defun gnus-summary-exit-no-update (&optional no-questions)
7306   "Quit reading current newsgroup without updating read article info."
7307   (interactive)
7308   (let* ((group gnus-newsgroup-name)
7309          (gnus-group-is-exiting-p t)
7310          (gnus-group-is-exiting-without-update-p t)
7311          (quit-config (gnus-group-quit-config group)))
7312     (when (or no-questions
7313               gnus-expert-user
7314               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7315       (gnus-async-halt-prefetch)
7316       (run-hooks 'gnus-summary-prepare-exit-hook)
7317       (when (gnus-buffer-live-p gnus-article-buffer)
7318         (with-current-buffer gnus-article-buffer
7319           (gnus-article-stop-animations)
7320           (mm-destroy-parts gnus-article-mime-handles)
7321           ;; Set it to nil for safety reason.
7322           (setq gnus-article-mime-handle-alist nil)
7323           (setq gnus-article-mime-handles nil)))
7324       ;; If we have several article buffers, we kill them at exit.
7325       (unless gnus-single-article-buffer
7326         (gnus-kill-buffer gnus-article-buffer)
7327         (gnus-kill-buffer gnus-original-article-buffer)
7328         (setq gnus-article-current nil))
7329       ;; Return to the group buffer.
7330       (gnus-configure-windows 'group 'force)
7331       (if (not gnus-kill-summary-on-exit)
7332           (gnus-deaden-summary)
7333         (gnus-close-group group)
7334         (gnus-kill-buffer gnus-summary-buffer))
7335       (unless gnus-single-article-buffer
7336         (setq gnus-article-current nil))
7337       (when gnus-use-trees
7338         (gnus-tree-close group))
7339       (gnus-async-prefetch-remove-group group)
7340       (when (get-buffer gnus-article-buffer)
7341         (bury-buffer gnus-article-buffer))
7342       ;; Clear the current group name.
7343       (setq gnus-newsgroup-name nil)
7344       (unless (gnus-ephemeral-group-p group)
7345         (gnus-group-update-group group nil t))
7346       (when (equal (gnus-group-group-name) group)
7347         (gnus-group-next-unread-group 1))
7348       (when quit-config
7349         (gnus-handle-ephemeral-exit quit-config)))))
7350
7351 (defun gnus-handle-ephemeral-exit (quit-config)
7352   "Handle movement when leaving an ephemeral group.
7353 The state which existed when entering the ephemeral is reset."
7354   (if (not (buffer-name (car quit-config)))
7355       (gnus-configure-windows 'group 'force)
7356     (set-buffer (car quit-config))
7357     (unless (eq (cdr quit-config) 'group)
7358       (setq gnus-current-select-method
7359             (gnus-find-method-for-group gnus-newsgroup-name)))
7360     (cond ((eq major-mode 'gnus-summary-mode)
7361            (gnus-set-global-variables))
7362           ((eq major-mode 'gnus-article-mode)
7363            (save-current-buffer
7364              ;; The `gnus-summary-buffer' variable may point
7365              ;; to the old summary buffer when using a single
7366              ;; article buffer.
7367              (unless (gnus-buffer-live-p gnus-summary-buffer)
7368                (set-buffer gnus-group-buffer))
7369              (set-buffer gnus-summary-buffer)
7370              (gnus-set-global-variables))))
7371     (if (or (eq (cdr quit-config) 'article)
7372             (eq (cdr quit-config) 'pick))
7373         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7374             (gnus-configure-windows 'pick 'force)
7375           (gnus-configure-windows (cdr quit-config) 'force))
7376       (gnus-configure-windows (cdr quit-config) 'force))
7377     (when (eq major-mode 'gnus-summary-mode)
7378       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7379                                                      next-unread-noselect))
7380           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7381                                   'next-noselect)
7382                               (gnus-summary-next-subject 1 nil t))
7383                              ((eq gnus-auto-select-on-ephemeral-exit
7384                                   'next-unread-noselect)
7385                               (gnus-summary-next-subject 1 t t))))
7386             ;; Hide the article buffer which displays the article different
7387             ;; from the one that the cursor points to in the summary buffer.
7388             (gnus-configure-windows 'summary 'force))
7389         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7390                (gnus-summary-next-subject 1))
7391               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7392                (gnus-summary-next-subject 1 t))))
7393       (gnus-summary-recenter)
7394       (gnus-summary-position-point))))
7395
7396 ;;; Dead summaries.
7397
7398 (defvar gnus-dead-summary-mode-map
7399   (let ((map (make-keymap)))
7400     (suppress-keymap map)
7401     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7402     (dolist (key '("\C-d" "\r" "\177" [delete]))
7403       (define-key map key 'gnus-summary-wake-up-the-dead))
7404     (dolist (key '("q" "Q"))
7405       (define-key map key 'bury-buffer))
7406     map))
7407
7408 (define-minor-mode gnus-dead-summary-mode
7409   "Minor mode for Gnus summary buffers."
7410   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7411   (unless (derived-mode-p 'gnus-summary-mode)
7412     (setq gnus-dead-summary-mode nil)))
7413
7414 (defun gnus-deaden-summary ()
7415   "Make the current summary buffer into a dead summary buffer."
7416   ;; Kill any previous dead summary buffer.
7417   (when (and gnus-dead-summary
7418              (buffer-name gnus-dead-summary))
7419     (with-current-buffer gnus-dead-summary
7420       (when gnus-dead-summary-mode
7421         (kill-buffer (current-buffer)))))
7422   ;; Make this the current dead summary.
7423   (setq gnus-dead-summary (current-buffer))
7424   (gnus-dead-summary-mode 1)
7425   (let ((name (buffer-name)))
7426     (when (string-match "Summary" name)
7427       (rename-buffer
7428        (concat (substring name 0 (match-beginning 0)) "Dead "
7429                (substring name (match-beginning 0)))
7430        t)
7431       (bury-buffer))))
7432
7433 (defun gnus-kill-or-deaden-summary (buffer)
7434   "Kill or deaden the summary BUFFER."
7435   (save-excursion
7436     (when (and (buffer-name buffer)
7437                (not gnus-single-article-buffer))
7438       (with-current-buffer buffer
7439         (gnus-kill-buffer gnus-article-buffer)
7440         (gnus-kill-buffer gnus-original-article-buffer)))
7441     (cond
7442      ;; Kill the buffer.
7443      (gnus-kill-summary-on-exit
7444       (when (and gnus-use-trees
7445                  (gnus-buffer-exists-p buffer))
7446         (with-current-buffer buffer
7447           (gnus-tree-close gnus-newsgroup-name)))
7448       (gnus-kill-buffer buffer))
7449      ;; Deaden the buffer.
7450      ((gnus-buffer-exists-p buffer)
7451       (with-current-buffer buffer
7452         (gnus-deaden-summary))))))
7453
7454 (defun gnus-summary-wake-up-the-dead (&rest args)
7455   "Wake up the dead summary buffer."
7456   (interactive)
7457   (gnus-dead-summary-mode -1)
7458   (let ((name (buffer-name)))
7459     (when (string-match "Dead " name)
7460       (rename-buffer
7461        (concat (substring name 0 (match-beginning 0))
7462                (substring name (match-end 0)))
7463        t)))
7464   (gnus-message 3 "This dead summary is now alive again"))
7465
7466 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7467 (defun gnus-summary-describe-group (&optional force)
7468   "Describe the current newsgroup."
7469   (interactive "P")
7470   (gnus-group-describe-group force gnus-newsgroup-name))
7471
7472 (defun gnus-summary-describe-briefly ()
7473   "Describe summary mode commands briefly."
7474   (interactive)
7475   (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")))
7476
7477 ;; Walking around group mode buffer from summary mode.
7478
7479 (defun gnus-summary-next-group (&optional no-article target-group backward)
7480   "Exit current newsgroup and then select next unread newsgroup.
7481 If prefix argument NO-ARTICLE is non-nil, no article is selected
7482 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7483 previous group instead."
7484   (interactive "P")
7485   ;; Stop pre-fetching.
7486   (gnus-async-halt-prefetch)
7487   (let ((current-group gnus-newsgroup-name)
7488         (current-buffer (current-buffer))
7489         entered)
7490     ;; First we semi-exit this group to update Xrefs and all variables.
7491     ;; We can't do a real exit, because the window conf must remain
7492     ;; the same in case the user is prompted for info, and we don't
7493     ;; want the window conf to change before that...
7494     (gnus-summary-exit t)
7495     (while (not entered)
7496       ;; Then we find what group we are supposed to enter.
7497       (set-buffer gnus-group-buffer)
7498       (gnus-group-jump-to-group current-group)
7499       (setq target-group
7500             (or target-group
7501                 (if (eq gnus-keep-same-level 'best)
7502                     (gnus-summary-best-group gnus-newsgroup-name)
7503                   (gnus-summary-search-group backward gnus-keep-same-level))))
7504       (if (not target-group)
7505           ;; There are no further groups, so we return to the group
7506           ;; buffer.
7507           (progn
7508             (gnus-message 5 "Returning to the group buffer")
7509             (setq entered t)
7510             (when (gnus-buffer-live-p current-buffer)
7511               (set-buffer current-buffer)
7512               (gnus-summary-exit))
7513             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7514         ;; We try to enter the target group.
7515         (gnus-group-jump-to-group target-group)
7516         (let ((unreads (gnus-group-group-unread)))
7517           (if (and (or (eq t unreads)
7518                        (and unreads (not (zerop unreads))))
7519                    (gnus-summary-read-group
7520                     target-group nil no-article
7521                     (and (buffer-name current-buffer) current-buffer)
7522                     nil backward))
7523               (setq entered t)
7524             (setq current-group target-group
7525                   target-group nil)))))))
7526
7527 (defun gnus-summary-prev-group (&optional no-article)
7528   "Exit current newsgroup and then select previous unread newsgroup.
7529 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7530   (interactive "P")
7531   (gnus-summary-next-group no-article nil t))
7532
7533 ;; Walking around summary lines.
7534
7535 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7536   "Go to the first subject satisfying any non-nil constraint.
7537 If UNREAD is non-nil, the article should be unread.
7538 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7539 If UNSEEN is non-nil, the article should be unseen as well as unread.
7540 Returns the article selected or nil if there are no matching articles."
7541   (interactive "P")
7542   (cond
7543    ;; Empty summary.
7544    ((null gnus-newsgroup-data)
7545     (gnus-message 3 "No articles in the group")
7546     nil)
7547    ;; Pick the first article.
7548    ((not (or unread undownloaded unseen))
7549     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7550     (gnus-data-number (car gnus-newsgroup-data)))
7551    ;; Find the first unread article.
7552    (t
7553     (let ((data gnus-newsgroup-data))
7554       (while (and data
7555                   (let ((num (gnus-data-number (car data))))
7556                     (or (memq num gnus-newsgroup-unfetched)
7557                         (not (or (and unread
7558                                       (memq num gnus-newsgroup-unreads))
7559                                  (and undownloaded
7560                                       (memq num gnus-newsgroup-undownloaded))
7561                                  (and unseen
7562                                       (memq num gnus-newsgroup-unseen)
7563                                       (memq num gnus-newsgroup-unreads)))))))
7564         (setq data (cdr data)))
7565       (prog1
7566           (if data
7567               (progn
7568                 (goto-char (gnus-data-pos (car data)))
7569                 (gnus-data-number (car data)))
7570             (gnus-message 3 "No more%s articles"
7571                           (let* ((r (when unread " unread"))
7572                                  (d (when undownloaded " undownloaded"))
7573                                  (s (when unseen " unseen"))
7574                                  (l (delq nil (list r d s))))
7575                             (cond ((= 3 (length l))
7576                                    (concat r "," d ", or" s))
7577                                   ((= 2 (length l))
7578                                    (concat (car l) ", or" (cadr l)))
7579                                   ((= 1 (length l))
7580                                    (car l))
7581                                   (t
7582                                    ""))))
7583             nil
7584             )
7585         (gnus-summary-position-point))))))
7586
7587 (defun gnus-summary-next-subject (n &optional unread dont-display)
7588   "Go to next N'th summary line.
7589 If N is negative, go to the previous N'th subject line.
7590 If UNREAD is non-nil, only unread articles are selected.
7591 The difference between N and the actual number of steps taken is
7592 returned."
7593   (interactive "p")
7594   (let ((backward (< n 0))
7595         (n (abs n)))
7596     (while (and (> n 0)
7597                 (if backward
7598                     (gnus-summary-find-prev unread)
7599                   (gnus-summary-find-next unread)))
7600       (unless (zerop (setq n (1- n)))
7601         (gnus-summary-show-thread)))
7602     (when (/= 0 n)
7603       (gnus-message 7 "No more%s articles"
7604                     (if unread " unread" "")))
7605     (unless dont-display
7606       (gnus-summary-recenter)
7607       (gnus-summary-position-point))
7608     n))
7609
7610 (defun gnus-summary-next-unread-subject (n)
7611   "Go to next N'th unread summary line."
7612   (interactive "p")
7613   (gnus-summary-next-subject n t))
7614
7615 (defun gnus-summary-prev-subject (n &optional unread)
7616   "Go to previous N'th summary line.
7617 If optional argument UNREAD is non-nil, only unread article is selected."
7618   (interactive "p")
7619   (gnus-summary-next-subject (- n) unread))
7620
7621 (defun gnus-summary-prev-unread-subject (n)
7622   "Go to previous N'th unread summary line."
7623   (interactive "p")
7624   (gnus-summary-next-subject (- n) t))
7625
7626 (defun gnus-summary-goto-subjects (articles)
7627   "Insert the subject header for ARTICLES in the current buffer."
7628   (save-excursion
7629     (dolist (article articles)
7630       (gnus-summary-goto-subject article t)))
7631   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7632   (gnus-summary-position-point))
7633
7634 (defun gnus-summary-goto-subject (article &optional force silent)
7635   "Go to the subject line of ARTICLE.
7636 If FORCE, also allow jumping to articles not currently shown."
7637   (interactive "nArticle number: ")
7638   (unless (numberp article)
7639     (error "Article %s is not a number" article))
7640   (let ((b (point))
7641         (data (gnus-data-find article)))
7642     ;; We read in the article if we have to.
7643     (and (not data)
7644          force
7645          (gnus-summary-insert-subject
7646           article
7647           (if (or (numberp force) (vectorp force)) force)
7648           t)
7649          (setq data (gnus-data-find article)))
7650     (goto-char b)
7651     (if (not data)
7652         (progn
7653           (unless silent
7654             (gnus-message 3 "Can't find article %d" article))
7655           nil)
7656       (let ((pt (gnus-data-pos data)))
7657         (goto-char pt)
7658         (gnus-summary-set-article-display-arrow pt))
7659       (gnus-summary-position-point)
7660       article)))
7661
7662 ;; Walking around summary lines with displaying articles.
7663
7664 (defun gnus-summary-expand-window (&optional arg)
7665   "Make the summary buffer take up the entire Emacs frame.
7666 Given a prefix, will force an `article' buffer configuration."
7667   (interactive "P")
7668   (if arg
7669       (gnus-configure-windows 'article 'force)
7670     (gnus-configure-windows 'summary 'force)))
7671
7672 (defun gnus-summary-display-article (article &optional all-header)
7673   "Display ARTICLE in article buffer."
7674   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7675                (with-current-buffer gnus-article-buffer
7676                  (eq major-mode 'gnus-article-mode)))
7677     (gnus-article-setup-buffer))
7678   (gnus-set-global-variables)
7679   (with-current-buffer gnus-article-buffer
7680     (setq gnus-article-charset gnus-newsgroup-charset)
7681     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7682     (mm-enable-multibyte))
7683   (if (null article)
7684       nil
7685     (prog1
7686         (if gnus-summary-display-article-function
7687             (funcall gnus-summary-display-article-function article all-header)
7688           (gnus-article-prepare article all-header))
7689       (gnus-run-hooks 'gnus-select-article-hook)
7690       (when (and gnus-current-article
7691                  (not (zerop gnus-current-article)))
7692         (gnus-summary-goto-subject gnus-current-article))
7693       (gnus-summary-recenter)
7694       (when (and gnus-use-trees gnus-show-threads)
7695         (gnus-possibly-generate-tree article)
7696         (gnus-highlight-selected-tree article))
7697       ;; Successfully display article.
7698       (gnus-article-set-window-start
7699        (cdr (assq article gnus-newsgroup-bookmarks))))))
7700
7701 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7702   "Select the current article.
7703 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7704 non-nil, the article will be re-fetched even if it already present in
7705 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7706 be displayed."
7707   ;; Make sure we are in the summary buffer to work around bbdb bug.
7708   (unless (eq major-mode 'gnus-summary-mode)
7709     (set-buffer gnus-summary-buffer))
7710   (let ((article (or article (gnus-summary-article-number)))
7711         (all-headers (not (not all-headers))) ;Must be t or nil.
7712         gnus-summary-display-article-function)
7713     (and (not pseudo)
7714          (gnus-summary-article-pseudo-p article)
7715          (error "This is a pseudo-article"))
7716     (with-current-buffer gnus-summary-buffer
7717       (if (or (and gnus-single-article-buffer
7718                    (or (null gnus-current-article)
7719                        (null gnus-article-current)
7720                        (null (get-buffer gnus-article-buffer))
7721                        (not (eq article (cdr gnus-article-current)))
7722                        (not (equal (car gnus-article-current)
7723                                    gnus-newsgroup-name))
7724                        (not (get-buffer gnus-original-article-buffer))))
7725               (and (not gnus-single-article-buffer)
7726                    (or (null gnus-current-article)
7727                        (not (get-buffer gnus-original-article-buffer))
7728                        (not (eq gnus-current-article article))))
7729               force)
7730           ;; The requested article is different from the current article.
7731           (progn
7732             (gnus-summary-display-article article all-headers)
7733             (when (gnus-buffer-live-p gnus-article-buffer)
7734               (with-current-buffer gnus-article-buffer
7735                 (if (not gnus-article-decoded-p) ;; a local variable
7736                     (mm-disable-multibyte))))
7737             (gnus-article-set-window-start
7738              (cdr (assq article gnus-newsgroup-bookmarks)))
7739             article)
7740         'old))))
7741
7742 (defun gnus-summary-force-verify-and-decrypt ()
7743   "Display buttons for signed/encrypted parts and verify/decrypt them."
7744   (interactive)
7745   (let ((mm-verify-option 'known)
7746         (mm-decrypt-option 'known)
7747         (gnus-article-emulate-mime t)
7748         (gnus-buttonized-mime-types (append (list "multipart/signed"
7749                                                   "multipart/encrypted")
7750                                             gnus-buttonized-mime-types)))
7751     (gnus-summary-select-article nil 'force)))
7752
7753 (defun gnus-summary-set-current-mark (&optional current-mark)
7754   "Obsolete function."
7755   nil)
7756
7757 (defun gnus-summary-next-article (&optional unread subject backward push)
7758   "Select the next article.
7759 If UNREAD, only unread articles are selected.
7760 If SUBJECT, only articles with SUBJECT are selected.
7761 If BACKWARD, the previous article is selected instead of the next."
7762   (interactive "P")
7763   ;; Make sure we are in the summary buffer.
7764   (unless (eq major-mode 'gnus-summary-mode)
7765     (set-buffer gnus-summary-buffer))
7766   (cond
7767    ;; Is there such an article?
7768    ((and (gnus-summary-search-forward unread subject backward)
7769          (or (gnus-summary-display-article (gnus-summary-article-number))
7770              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7771     (gnus-summary-position-point))
7772    ;; If not, we try the first unread, if that is wanted.
7773    ((and subject
7774          gnus-auto-select-same
7775          (gnus-summary-first-unread-article))
7776     (gnus-summary-position-point)
7777     (gnus-message 6 "Wrapped"))
7778    ;; Try to get next/previous article not displayed in this group.
7779    ((and gnus-auto-extend-newsgroup
7780          (not unread) (not subject))
7781     (gnus-summary-goto-article
7782      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7783      nil (count-lines (point-min) (point))))
7784    ;; Go to next/previous group.
7785    (t
7786     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7787       (gnus-summary-jump-to-group gnus-newsgroup-name))
7788     (let ((cmd (if (featurep 'xemacs)
7789                    last-command-char
7790                  last-command-event))
7791           (point
7792            (with-current-buffer gnus-group-buffer
7793              (point)))
7794           (current-summary (current-buffer))
7795           (group
7796            (if (eq gnus-keep-same-level 'best)
7797                (gnus-summary-best-group gnus-newsgroup-name)
7798              (gnus-summary-search-group backward gnus-keep-same-level))))
7799       ;; Select next unread newsgroup automagically.
7800       (cond
7801        ((or (not gnus-auto-select-next)
7802             (not cmd))
7803         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7804        ((or (eq gnus-auto-select-next 'quietly)
7805             (and (eq gnus-auto-select-next 'slightly-quietly)
7806                  push)
7807             (and (eq gnus-auto-select-next 'almost-quietly)
7808                  (gnus-summary-last-article-p)))
7809         ;; Select quietly.
7810         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7811             (gnus-summary-exit)
7812           (gnus-message 7 "No more%s articles (%s)..."
7813                         (if unread " unread" "")
7814                         (if group (concat "selecting " group)
7815                           "exiting"))
7816           (gnus-summary-next-group nil group backward)))
7817        (t
7818         (when (gnus-key-press-event-p last-input-event)
7819           ;; Somehow or other, we may now have selected a different
7820           ;; window.  Make point go back to the summary buffer.
7821           (when (eq current-summary (current-buffer))
7822             ;; FIXME: This burps when get-buffer-window returns nil.
7823             (select-window (get-buffer-window current-summary 0)))
7824           (gnus-summary-walk-group-buffer
7825            gnus-newsgroup-name cmd unread backward point))))))))
7826
7827 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7828   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7829                       (?\C-p (gnus-group-prev-unread-group 1))))
7830         (cursor-in-echo-area t)
7831         keve key group ended prompt)
7832     (with-current-buffer gnus-group-buffer
7833       (goto-char start)
7834       (setq group
7835             (if (eq gnus-keep-same-level 'best)
7836                 (gnus-summary-best-group gnus-newsgroup-name)
7837               (gnus-summary-search-group backward gnus-keep-same-level))))
7838     (while (not ended)
7839       (setq prompt
7840             (format
7841              "No more%s articles%s " (if unread " unread" "")
7842              (if (and group
7843                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7844                  (format " (Type %s for %s [%s])"
7845                          (single-key-description cmd)
7846                          (gnus-group-decoded-name group)
7847                          (gnus-group-unread group))
7848                (format " (Type %s to exit %s)"
7849                        (single-key-description cmd)
7850                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7851       ;; Confirm auto selection.
7852       (setq key (car (setq keve (gnus-read-event-char prompt)))
7853             ended t)
7854       (cond
7855        ((assq key keystrokes)
7856         (let ((obuf (current-buffer)))
7857           (switch-to-buffer gnus-group-buffer)
7858           (when group
7859             (gnus-group-jump-to-group group))
7860           (eval (cadr (assq key keystrokes)))
7861           (setq group (gnus-group-group-name))
7862           (switch-to-buffer obuf))
7863         (setq ended nil))
7864        ((equal key cmd)
7865         (if (or (not group)
7866                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7867             (gnus-summary-exit)
7868           (gnus-summary-next-group nil group backward)))
7869        (t
7870         (push (cdr keve) unread-command-events))))))
7871
7872 (defun gnus-summary-next-unread-article ()
7873   "Select unread article after current one."
7874   (interactive)
7875   (gnus-summary-next-article
7876    (or (not (eq gnus-summary-goto-unread 'never))
7877        (gnus-summary-last-article-p (gnus-summary-article-number)))
7878    (and gnus-auto-select-same
7879         (gnus-summary-article-subject))))
7880
7881 (defun gnus-summary-prev-article (&optional unread subject)
7882   "Select the article before the current one.
7883 If UNREAD is non-nil, only unread articles are selected."
7884   (interactive "P")
7885   (gnus-summary-next-article unread subject t))
7886
7887 (defun gnus-summary-prev-unread-article ()
7888   "Select unread article before current one."
7889   (interactive)
7890   (gnus-summary-prev-article
7891    (or (not (eq gnus-summary-goto-unread 'never))
7892        (gnus-summary-first-article-p (gnus-summary-article-number)))
7893    (and gnus-auto-select-same
7894         (gnus-summary-article-subject))))
7895
7896 (defun gnus-summary-next-page (&optional lines circular stop)
7897   "Show next page of the selected article.
7898 If at the end of the current article, select the next article.
7899 LINES says how many lines should be scrolled up.
7900
7901 If CIRCULAR is non-nil, go to the start of the article instead of
7902 selecting the next article when reaching the end of the current
7903 article.
7904
7905 If STOP is non-nil, just stop when reaching the end of the message.
7906
7907 Also see the variable `gnus-article-skip-boring'."
7908   (interactive "P")
7909   (setq gnus-summary-buffer (current-buffer))
7910   (gnus-set-global-variables)
7911   (let ((article (gnus-summary-article-number))
7912         (article-window (get-buffer-window gnus-article-buffer t))
7913         endp)
7914     ;; If the buffer is empty, we have no article.
7915     (unless article
7916       (error "No article to select"))
7917     (gnus-configure-windows 'article)
7918     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7919         (if (and (eq gnus-summary-goto-unread 'never)
7920                  (not (gnus-summary-last-article-p article)))
7921             (gnus-summary-next-article)
7922           (gnus-summary-next-unread-article))
7923       (if (or (null gnus-current-article)
7924               (null gnus-article-current)
7925               (/= article (cdr gnus-article-current))
7926               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7927           ;; Selected subject is different from current article's.
7928           (gnus-summary-display-article article)
7929         (when article-window
7930           (gnus-eval-in-buffer-window gnus-article-buffer
7931             (setq endp (or (gnus-article-next-page lines)
7932                            (gnus-article-only-boring-p))))
7933           (when endp
7934             (cond ((or stop gnus-summary-stop-at-end-of-message)
7935                    (gnus-message 3 "End of message"))
7936                   (circular
7937                    (gnus-summary-beginning-of-article))
7938                   (lines
7939                    (gnus-message 3 "End of message"))
7940                   ((null lines)
7941                    (if (and (eq gnus-summary-goto-unread 'never)
7942                             (not (gnus-summary-last-article-p article)))
7943                        (gnus-summary-next-article)
7944                      (gnus-summary-next-unread-article))))))))
7945     (gnus-summary-recenter)
7946     (gnus-summary-position-point)))
7947
7948 (defun gnus-summary-prev-page (&optional lines move)
7949   "Show previous page of selected article.
7950 Argument LINES specifies lines to be scrolled down.
7951 If MOVE, move to the previous unread article if point is at
7952 the beginning of the buffer."
7953   (interactive "P")
7954   (let ((article (gnus-summary-article-number))
7955         (article-window (get-buffer-window gnus-article-buffer t))
7956         endp)
7957     (gnus-configure-windows 'article)
7958     (if (or (null gnus-current-article)
7959             (null gnus-article-current)
7960             (/= article (cdr gnus-article-current))
7961             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7962         ;; Selected subject is different from current article's.
7963         (gnus-summary-display-article article)
7964       (gnus-summary-recenter)
7965       (when article-window
7966         (gnus-eval-in-buffer-window gnus-article-buffer
7967           (setq endp (gnus-article-prev-page lines)))
7968         (when (and move endp)
7969           (cond (lines
7970                  (gnus-message 3 "Beginning of message"))
7971                 ((null lines)
7972                  (if (and (eq gnus-summary-goto-unread 'never)
7973                           (not (gnus-summary-first-article-p article)))
7974                      (gnus-summary-prev-article)
7975                    (gnus-summary-prev-unread-article))))))))
7976   (gnus-summary-position-point))
7977
7978 (defun gnus-summary-prev-page-or-article (&optional lines)
7979   "Show previous page of selected article.
7980 Argument LINES specifies lines to be scrolled down.
7981 If at the beginning of the article, go to the next article."
7982   (interactive "P")
7983   (gnus-summary-prev-page lines t))
7984
7985 (defun gnus-summary-scroll-up (lines)
7986   "Scroll up (or down) one line current article.
7987 Argument LINES specifies lines to be scrolled up (or down if negative).
7988 If no article is selected, then the current article will be selected first."
7989   (interactive "p")
7990   (gnus-configure-windows 'article)
7991   (gnus-summary-show-thread)
7992   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7993     (gnus-eval-in-buffer-window gnus-article-buffer
7994       (cond ((> lines 0)
7995              (when (gnus-article-next-page lines)
7996                (gnus-message 3 "End of message")))
7997             ((< lines 0)
7998              (gnus-article-prev-page (- lines))))))
7999   (gnus-summary-recenter)
8000   (gnus-summary-position-point))
8001
8002 (defun gnus-summary-scroll-down (lines)
8003   "Scroll down (or up) one line current article.
8004 Argument LINES specifies lines to be scrolled down (or up if negative).
8005 If no article is selected, then the current article will be selected first."
8006   (interactive "p")
8007   (gnus-summary-scroll-up (- lines)))
8008
8009 (defun gnus-summary-next-same-subject ()
8010   "Select next article which has the same subject as current one."
8011   (interactive)
8012   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8013
8014 (defun gnus-summary-prev-same-subject ()
8015   "Select previous article which has the same subject as current one."
8016   (interactive)
8017   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8018
8019 (defun gnus-summary-next-unread-same-subject ()
8020   "Select next unread article which has the same subject as current one."
8021   (interactive)
8022   (gnus-summary-next-article t (gnus-summary-article-subject)))
8023
8024 (defun gnus-summary-prev-unread-same-subject ()
8025   "Select previous unread article which has the same subject as current one."
8026   (interactive)
8027   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8028
8029 (defun gnus-summary-first-unread-article ()
8030   "Select the first unread article.
8031 Return nil if there are no unread articles."
8032   (interactive)
8033   (prog1
8034       (when (gnus-summary-first-subject t)
8035         (gnus-summary-show-thread)
8036         (gnus-summary-first-subject t)
8037         (gnus-summary-display-article (gnus-summary-article-number)))
8038     (gnus-summary-position-point)))
8039
8040 (defun gnus-summary-first-unread-subject ()
8041   "Place the point on the subject line of the first unread article.
8042 Return nil if there are no unread articles."
8043   (interactive)
8044   (prog1
8045       (when (gnus-summary-first-subject t)
8046         (gnus-summary-show-thread)
8047         (gnus-summary-first-subject t))
8048     (gnus-summary-position-point)))
8049
8050 (defun gnus-summary-first-unseen-subject ()
8051   "Place the point on the subject line of the first unseen article.
8052 Return nil if there are no unseen articles."
8053   (interactive)
8054   (prog1
8055       (when (gnus-summary-first-subject nil nil t)
8056         (gnus-summary-show-thread)
8057         (gnus-summary-first-subject nil nil t))
8058     (gnus-summary-position-point)))
8059
8060 (defun gnus-summary-first-unseen-or-unread-subject ()
8061   "Place the point on the subject line of the first unseen and unread article.
8062 If all article have been seen, on the subject line of the first unread
8063 article."
8064   (interactive)
8065   (prog1
8066       (unless (when (gnus-summary-first-subject nil nil t)
8067                 (gnus-summary-show-thread)
8068                 (gnus-summary-first-subject nil nil t))
8069         (when (gnus-summary-first-subject t)
8070           (gnus-summary-show-thread)
8071           (gnus-summary-first-subject t)))
8072     (gnus-summary-position-point)))
8073
8074 (defun gnus-summary-first-article ()
8075   "Select the first article.
8076 Return nil if there are no articles."
8077   (interactive)
8078   (prog1
8079       (when (gnus-summary-first-subject)
8080         (gnus-summary-show-thread)
8081         (gnus-summary-first-subject)
8082         (gnus-summary-display-article (gnus-summary-article-number)))
8083     (gnus-summary-position-point)))
8084
8085 (defun gnus-summary-best-unread-article (&optional arg)
8086   "Select the unread article with the highest score.
8087 If given a prefix argument, select the next unread article that has a
8088 score higher than the default score."
8089   (interactive "P")
8090   (let ((article (if arg
8091                      (gnus-summary-better-unread-subject)
8092                    (gnus-summary-best-unread-subject))))
8093     (if article
8094         (gnus-summary-goto-article article)
8095       (error "No unread articles"))))
8096
8097 (defun gnus-summary-best-unread-subject ()
8098   "Select the unread subject with the highest score."
8099   (interactive)
8100   (let ((best -1000000)
8101         (data gnus-newsgroup-data)
8102         article score)
8103     (while data
8104       (and (gnus-data-unread-p (car data))
8105            (> (setq score
8106                     (gnus-summary-article-score (gnus-data-number (car data))))
8107               best)
8108            (setq best score
8109                  article (gnus-data-number (car data))))
8110       (setq data (cdr data)))
8111     (when article
8112       (gnus-summary-goto-subject article))
8113     (gnus-summary-position-point)
8114     article))
8115
8116 (defun gnus-summary-better-unread-subject ()
8117   "Select the first unread subject that has a score over the default score."
8118   (interactive)
8119   (let ((data gnus-newsgroup-data)
8120         article score)
8121     (while (and (setq article (gnus-data-number (car data)))
8122                 (or (gnus-data-read-p (car data))
8123                     (not (> (gnus-summary-article-score article)
8124                             gnus-summary-default-score))))
8125       (setq data (cdr data)))
8126     (when article
8127       (gnus-summary-goto-subject article))
8128     (gnus-summary-position-point)
8129     article))
8130
8131 (defun gnus-summary-last-subject ()
8132   "Go to the last displayed subject line in the group."
8133   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8134     (when article
8135       (gnus-summary-goto-subject article))))
8136
8137 (defun gnus-summary-goto-article (article &optional all-headers force)
8138   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8139 If ALL-HEADERS is non-nil, no header lines are hidden.
8140 If FORCE, go to the article even if it isn't displayed.  If FORCE
8141 is a number, it is the line the article is to be displayed on."
8142   (interactive
8143    (list
8144     (gnus-completing-read
8145      "Article number or Message-ID"
8146      (mapcar 'int-to-string gnus-newsgroup-limit))
8147     current-prefix-arg
8148     t))
8149   (prog1
8150       (if (and (stringp article)
8151                (string-match "@\\|%40" article))
8152           (gnus-summary-refer-article article)
8153         (when (stringp article)
8154           (setq article (string-to-number article)))
8155         (if (gnus-summary-goto-subject article force)
8156             (gnus-summary-display-article article all-headers)
8157           (gnus-message 4 "Couldn't go to article %s" article) nil))
8158     (gnus-summary-position-point)))
8159
8160 (defun gnus-summary-goto-last-article ()
8161   "Go to the previously read article."
8162   (interactive)
8163   (prog1
8164       (when gnus-last-article
8165         (gnus-summary-goto-article gnus-last-article nil t))
8166     (gnus-summary-position-point)))
8167
8168 (defun gnus-summary-pop-article (number)
8169   "Pop one article off the history and go to the previous.
8170 NUMBER articles will be popped off."
8171   (interactive "p")
8172   (let (to)
8173     (setq gnus-newsgroup-history
8174           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8175     (if to
8176         (gnus-summary-goto-article (car to) nil t)
8177       (error "Article history empty")))
8178   (gnus-summary-position-point))
8179
8180 ;; Summary commands and functions for limiting the summary buffer.
8181
8182 (defun gnus-summary-limit-to-articles (n)
8183   "Limit the summary buffer to the next N articles.
8184 If not given a prefix, use the process marked articles instead."
8185   (interactive "P")
8186   (prog1
8187       (let ((articles (gnus-summary-work-articles n)))
8188         (setq gnus-newsgroup-processable nil)
8189         (gnus-summary-limit articles))
8190     (gnus-summary-position-point)))
8191
8192 (defun gnus-summary-pop-limit (&optional total)
8193   "Restore the previous limit.
8194 If given a prefix, remove all limits."
8195   (interactive "P")
8196   (when total
8197     (setq gnus-newsgroup-limits
8198           (list (mapcar (lambda (h) (mail-header-number h))
8199                         gnus-newsgroup-headers))))
8200   (unless gnus-newsgroup-limits
8201     (error "No limit to pop"))
8202   (prog1
8203       (gnus-summary-limit nil 'pop)
8204     (gnus-summary-position-point)))
8205
8206 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8207   "Limit the summary buffer to articles that have subjects that match a regexp.
8208 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8209   (interactive
8210    (list (read-string (if current-prefix-arg
8211                           "Exclude subject (regexp): "
8212                         "Limit to subject (regexp): "))
8213          nil current-prefix-arg))
8214   (unless header
8215     (setq header "subject"))
8216   (when (not (equal "" subject))
8217     (prog1
8218         (let ((articles (gnus-summary-find-matching
8219                          (or header "subject") subject 'all nil nil
8220                          not-matching)))
8221           (unless articles
8222             (error "Found no matches for \"%s\"" subject))
8223           (gnus-summary-limit articles))
8224       (gnus-summary-position-point))))
8225
8226 (defun gnus-summary-limit-to-author (from &optional not-matching)
8227   "Limit the summary buffer to articles that have authors that match a regexp.
8228 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8229   (interactive
8230    (list (read-string (if current-prefix-arg
8231                           "Exclude author (regexp): "
8232                         "Limit to author (regexp): "))
8233          current-prefix-arg))
8234   (gnus-summary-limit-to-subject from "from" not-matching))
8235
8236 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8237   "Limit the summary buffer to articles with the given RECIPIENT.
8238
8239 If NOT-MATCHING, exclude RECIPIENT.
8240
8241 To and Cc headers are checked.  You need to include them in
8242 `nnmail-extra-headers'."
8243   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8244   (interactive
8245    (list (read-string (format "%s recipient (regexp): "
8246                               (if current-prefix-arg "Exclude" "Limit to")))
8247          current-prefix-arg))
8248   (when (not (equal "" recipient))
8249     (prog1 (let* ((to
8250                    (if (memq 'To nnmail-extra-headers)
8251                        (gnus-summary-find-matching
8252                         (cons 'extra 'To) recipient 'all nil nil
8253                         not-matching)
8254                      (gnus-message
8255                       1 "`To' isn't present in `nnmail-extra-headers'")
8256                      (sit-for 1)
8257                      nil))
8258                   (cc
8259                    (if (memq 'Cc nnmail-extra-headers)
8260                        (gnus-summary-find-matching
8261                         (cons 'extra 'Cc) recipient 'all nil nil
8262                         not-matching)
8263                      (gnus-message
8264                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8265                      (sit-for 1)
8266                      nil))
8267                   (articles
8268                    (if not-matching
8269                        ;; We need the numbers that are in both lists:
8270                        (mapcar (lambda (a)
8271                                  (and (memq a to) a))
8272                                cc)
8273                      (nconc to cc))))
8274              (unless articles
8275                (error "Found no matches for \"%s\"" recipient))
8276              (gnus-summary-limit articles))
8277       (gnus-summary-position-point))))
8278
8279 (defun gnus-summary-limit-to-address (address &optional not-matching)
8280   "Limit the summary buffer to articles with the given ADDRESS.
8281
8282 If NOT-MATCHING, exclude ADDRESS.
8283
8284 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8285 in `nnmail-extra-headers'."
8286   (interactive
8287    (list (read-string (format "%s address (regexp): "
8288                               (if current-prefix-arg "Exclude" "Limit to")))
8289          current-prefix-arg))
8290   (when (not (equal "" address))
8291     (prog1 (let* ((to
8292                    (if (memq 'To nnmail-extra-headers)
8293                        (gnus-summary-find-matching
8294                         (cons 'extra 'To) address 'all nil nil
8295                         not-matching)
8296                      (gnus-message
8297                       1 "`To' isn't present in `nnmail-extra-headers'")
8298                      (sit-for 1)
8299                      t))
8300                   (cc
8301                    (if (memq 'Cc nnmail-extra-headers)
8302                        (gnus-summary-find-matching
8303                         (cons 'extra 'Cc) address 'all nil nil
8304                         not-matching)
8305                      (gnus-message
8306                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8307                      (sit-for 1)
8308                      t))
8309                   (from
8310                    (gnus-summary-find-matching "from" address
8311                                                'all nil nil not-matching))
8312                   (articles
8313                    (if not-matching
8314                        ;; We need the numbers that are in all lists:
8315                        (if (eq cc t)
8316                            (if (eq to t)
8317                                from
8318                              (mapcar (lambda (a) (car (memq a from))) to))
8319                          (if (eq to t)
8320                              (mapcar (lambda (a) (car (memq a from))) cc)
8321                            (mapcar (lambda (a) (car (memq a from)))
8322                                    (mapcar (lambda (a) (car (memq a to)))
8323                                            cc))))
8324                      (nconc (if (eq to t) nil to)
8325                             (if (eq cc t) nil cc)
8326                             from))))
8327              (unless articles
8328                (error "Found no matches for \"%s\"" address))
8329              (gnus-summary-limit articles))
8330       (gnus-summary-position-point))))
8331
8332 (defun gnus-summary-limit-strange-charsets-predicate (header)
8333   (when (fboundp 'char-charset)
8334     (let ((string (concat (mail-header-subject header)
8335                           (mail-header-from header)))
8336           charset found)
8337       (dotimes (i (1- (length string)))
8338         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8339         (when (string-match "unicode\\|big\\|japanese" charset)
8340           (setq found t)))
8341       found)))
8342
8343 (defun gnus-summary-limit-to-predicate (predicate)
8344   "Limit to articles where PREDICATE returns non-nil.
8345 PREDICATE will be called with the header structures of the
8346 articles."
8347   (let ((articles nil)
8348         (case-fold-search t))
8349     (dolist (header gnus-newsgroup-headers)
8350       (when (funcall predicate header)
8351         (push (mail-header-number header) articles)))
8352     (gnus-summary-limit (nreverse articles))))
8353
8354 (defun gnus-summary-limit-to-age (age &optional younger-p)
8355   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8356 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8357 articles that are younger than AGE days."
8358   (interactive
8359    (let ((younger current-prefix-arg)
8360          (days-got nil)
8361          days)
8362      (while (not days-got)
8363        (setq days (if younger
8364                       (read-string "Limit to articles younger than (in days, older when negative): ")
8365                     (read-string
8366                      "Limit to articles older than (in days, younger when negative): ")))
8367        (when (> (length days) 0)
8368          (setq days (read days)))
8369        (if (numberp days)
8370            (progn
8371              (setq days-got t)
8372              (when (< days 0)
8373                (setq younger (not younger))
8374                (setq days (* days -1))))
8375          (message "Please enter a number.")
8376          (sleep-for 1)))
8377      (list days younger)))
8378   (prog1
8379       (let ((data gnus-newsgroup-data)
8380             (cutoff (days-to-time age))
8381             articles d date is-younger)
8382         (while (setq d (pop data))
8383           (when (and (vectorp (gnus-data-header d))
8384                      (setq date (mail-header-date (gnus-data-header d))))
8385             (setq is-younger (time-less-p
8386                               (time-since (gnus-date-get-time date))
8387                               cutoff))
8388             (when (if younger-p
8389                       is-younger
8390                     (not is-younger))
8391               (push (gnus-data-number d) articles))))
8392         (gnus-summary-limit (nreverse articles)))
8393     (gnus-summary-position-point)))
8394
8395 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8396   "Limit the summary buffer to articles that match an 'extra' header."
8397   (interactive
8398    (let ((header
8399           (intern
8400            (gnus-completing-read
8401             (if current-prefix-arg
8402                 "Exclude extra header"
8403               "Limit extra header")
8404             (mapcar 'symbol-name gnus-extra-headers)
8405             t nil nil
8406             (symbol-name (car gnus-extra-headers))))))
8407      (list header
8408            (read-string (format "%s header %s (regexp): "
8409                                 (if current-prefix-arg "Exclude" "Limit to")
8410                                 header))
8411            current-prefix-arg)))
8412   (when (not (equal "" regexp))
8413     (prog1
8414         (let ((articles (gnus-summary-find-matching
8415                          (cons 'extra header) regexp 'all nil nil
8416                          not-matching)))
8417           (unless articles
8418             (error "Found no matches for \"%s\"" regexp))
8419           (gnus-summary-limit articles))
8420       (gnus-summary-position-point))))
8421
8422 (defun gnus-summary-limit-to-display-predicate ()
8423   "Limit the summary buffer to the predicated in the `display' group parameter."
8424   (interactive)
8425   (unless gnus-newsgroup-display
8426     (error "There is no `display' group parameter"))
8427   (let (articles)
8428     (dolist (gnus-number gnus-newsgroup-articles)
8429       (when (funcall gnus-newsgroup-display)
8430         (push gnus-number articles)))
8431     (gnus-summary-limit articles))
8432   (gnus-summary-position-point))
8433
8434 (defun gnus-summary-limit-to-unread (&optional all)
8435   "Limit the summary buffer to articles that are not marked as read.
8436 If ALL is non-nil, limit strictly to unread articles."
8437   (interactive "P")
8438   (if all
8439       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8440     (gnus-summary-limit-to-marks
8441      ;; Concat all the marks that say that an article is read and have
8442      ;; those removed.
8443      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8444            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8445            gnus-low-score-mark gnus-expirable-mark
8446            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8447            gnus-duplicate-mark)
8448      'reverse)))
8449
8450 (defun gnus-summary-limit-to-headers (match &optional reverse)
8451   "Limit the summary buffer to articles that have headers that match MATCH.
8452 If REVERSE (the prefix), limit to articles that don't match."
8453   (interactive "sMatch headers (regexp): \nP")
8454   (gnus-summary-limit-to-bodies match reverse t))
8455
8456 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8457   "Limit the summary buffer to articles that have bodies that match MATCH.
8458 If REVERSE (the prefix), limit to articles that don't match."
8459   (interactive "sMatch body (regexp): \nP")
8460   (let ((articles nil)
8461         (gnus-select-article-hook nil)  ;Disable hook.
8462         (gnus-article-prepare-hook nil)
8463         (gnus-use-article-prefetch nil)
8464         (gnus-keep-backlog nil)
8465         (gnus-break-pages nil)
8466         (gnus-summary-display-arrow nil)
8467         (gnus-updated-mode-lines nil)
8468         (gnus-auto-center-summary nil)
8469         (gnus-display-mime-function nil))
8470     (dolist (data gnus-newsgroup-data)
8471       (let (gnus-mark-article-hook)
8472         (gnus-summary-select-article t t nil (gnus-data-number data)))
8473       (with-current-buffer gnus-article-buffer
8474         (article-goto-body)
8475         (let* ((case-fold-search t)
8476                (found (if headersp
8477                           (re-search-backward match nil t)
8478                         (re-search-forward match nil t))))
8479           (when (or (and found
8480                          (not reverse))
8481                     (and (not found)
8482                          reverse))
8483             (push (gnus-data-number data) articles)))))
8484     (if (not articles)
8485         (message "No messages matched")
8486       (gnus-summary-limit articles)))
8487   (gnus-summary-position-point))
8488
8489 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8490   "Limit the summary buffer to articles that aren't part on any thread.
8491 If THREADSP (the prefix), limit to articles that are in threads."
8492   (interactive "P")
8493   (let ((articles nil)
8494         thread-articles
8495         threads)
8496     (dolist (thread gnus-newsgroup-threads)
8497       (if (stringp (car thread))
8498           (dolist (thread (cdr thread))
8499             (push thread threads))
8500         (push thread threads)))
8501     (dolist (thread threads)
8502       (setq thread-articles (gnus-articles-in-thread thread))
8503       (when (or (and threadsp
8504                      (> (length thread-articles) 1))
8505                 (and (not threadsp)
8506                      (= (length thread-articles) 1)))
8507         (setq articles (nconc thread-articles articles))))
8508     (if (not articles)
8509         (message "No messages matched")
8510       (gnus-summary-limit articles))
8511     (gnus-summary-position-point)))
8512
8513 (defun gnus-summary-limit-to-replied (&optional unreplied)
8514   "Limit the summary buffer to replied articles.
8515 If UNREPLIED (the prefix), limit to unreplied articles."
8516   (interactive "P")
8517   (if unreplied
8518       (gnus-summary-limit
8519        (gnus-set-difference gnus-newsgroup-articles
8520         gnus-newsgroup-replied))
8521     (gnus-summary-limit gnus-newsgroup-replied))
8522   (gnus-summary-position-point))
8523
8524 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8525   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8526 If REVERSE, limit the summary buffer to articles that are marked
8527 with MARKS.  MARKS can either be a string of marks or a list of marks.
8528 Returns how many articles were removed."
8529   (interactive "sMarks: ")
8530   (gnus-summary-limit-to-marks marks t))
8531
8532 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8533   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8534 If REVERSE (the prefix), limit the summary buffer to articles that are
8535 not marked with MARKS.  MARKS can either be a string of marks or a
8536 list of marks.
8537 Returns how many articles were removed."
8538   (interactive "sMarks: \nP")
8539   (prog1
8540       (let ((data gnus-newsgroup-data)
8541             (marks (if (listp marks) marks
8542                      (append marks nil))) ; Transform to list.
8543             articles)
8544         (while data
8545           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8546                   (memq (gnus-data-mark (car data)) marks))
8547             (push (gnus-data-number (car data)) articles))
8548           (setq data (cdr data)))
8549         (gnus-summary-limit articles))
8550     (gnus-summary-position-point)))
8551
8552 (defun gnus-summary-limit-to-score (score)
8553   "Limit to articles with score at or above SCORE."
8554   (interactive "NLimit to articles with score of at least: ")
8555   (let ((data gnus-newsgroup-data)
8556         articles)
8557     (while data
8558       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8559                 score)
8560         (push (gnus-data-number (car data)) articles))
8561       (setq data (cdr data)))
8562     (prog1
8563         (gnus-summary-limit articles)
8564       (gnus-summary-position-point))))
8565
8566 (defun gnus-summary-limit-to-unseen ()
8567   "Limit to unseen articles."
8568   (interactive)
8569   (prog1
8570       (gnus-summary-limit gnus-newsgroup-unseen)
8571     (gnus-summary-position-point)))
8572
8573 (defun gnus-summary-limit-include-thread (id)
8574   "Display all the hidden articles that is in the thread with ID in it.
8575 When called interactively, ID is the Message-ID of the current
8576 article."
8577   (interactive (list (mail-header-id (gnus-summary-article-header))))
8578   (let ((articles (gnus-articles-in-thread
8579                    (gnus-id-to-thread (gnus-root-id id))))
8580         ;;we REALLY want the whole thread---this prevents cut-threads
8581         ;;from removing the thread we want to include.
8582         (gnus-fetch-old-headers nil)
8583         (gnus-build-sparse-threads nil))
8584     (prog1
8585         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8586       (gnus-summary-limit-include-matching-articles
8587        "subject"
8588        (regexp-quote (gnus-simplify-subject-re
8589                       (mail-header-subject (gnus-id-to-header id)))))
8590       (gnus-summary-position-point))))
8591
8592 (defun gnus-summary-limit-include-matching-articles (header regexp)
8593   "Display all the hidden articles that have HEADERs that match REGEXP."
8594   (interactive (list (read-string "Match on header: ")
8595                      (read-string "Regexp: ")))
8596   (let ((articles (gnus-find-matching-articles header regexp)))
8597     (prog1
8598         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8599       (gnus-summary-position-point))))
8600
8601 (defun gnus-summary-insert-dormant-articles ()
8602   "Insert all the dormant articles for this group into the current buffer."
8603   (interactive)
8604   (let ((gnus-verbose (max 6 gnus-verbose)))
8605     (if (not gnus-newsgroup-dormant)
8606         (gnus-message 3 "No dormant articles for this group")
8607       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8608
8609 (defun gnus-summary-insert-ticked-articles ()
8610   "Insert ticked articles for this group into the current buffer."
8611   (interactive)
8612   (let ((gnus-verbose (max 6 gnus-verbose)))
8613     (if (not gnus-newsgroup-marked)
8614         (gnus-message 3 "No ticked articles for this group")
8615       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8616
8617 (defun gnus-summary-limit-include-dormant ()
8618   "Display all the hidden articles that are marked as dormant.
8619 Note that this command only works on a subset of the articles currently
8620 fetched for this group."
8621   (interactive)
8622   (unless gnus-newsgroup-dormant
8623     (error "There are no dormant articles in this group"))
8624   (prog1
8625       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8626     (gnus-summary-position-point)))
8627
8628 (defun gnus-summary-include-articles (articles)
8629   "Fetch the headers for ARTICLES and then display the summary lines."
8630   (let ((gnus-inhibit-demon t)
8631         (gnus-agent nil)
8632         (gnus-read-all-available-headers t))
8633     (setq gnus-newsgroup-headers
8634           (gnus-merge
8635            'list gnus-newsgroup-headers
8636            (gnus-fetch-headers articles nil t)
8637            'gnus-article-sort-by-number))
8638     (setq gnus-newsgroup-articles
8639           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8640     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8641
8642 (defun gnus-summary-limit-exclude-dormant ()
8643   "Hide all dormant articles."
8644   (interactive)
8645   (prog1
8646       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8647     (gnus-summary-position-point)))
8648
8649 (defun gnus-summary-limit-exclude-childless-dormant ()
8650   "Hide all dormant articles that have no children."
8651   (interactive)
8652   (let ((data (gnus-data-list t))
8653         articles d children)
8654     ;; Find all articles that are either not dormant or have
8655     ;; children.
8656     (while (setq d (pop data))
8657       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8658                 (and (setq children
8659                            (gnus-article-children (gnus-data-number d)))
8660                      (let (found)
8661                        (while children
8662                          (when (memq (car children) articles)
8663                            (setq children nil
8664                                  found t))
8665                          (pop children))
8666                        found)))
8667         (push (gnus-data-number d) articles)))
8668     ;; Do the limiting.
8669     (prog1
8670         (gnus-summary-limit articles)
8671       (gnus-summary-position-point))))
8672
8673 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8674   "Mark all unread excluded articles as read.
8675 If ALL, mark even excluded ticked and dormants as read."
8676   (interactive "P")
8677   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8678   (let ((articles (gnus-sorted-ndifference
8679                    (sort
8680                     (mapcar (lambda (h) (mail-header-number h))
8681                             gnus-newsgroup-headers)
8682                     '<)
8683                    gnus-newsgroup-limit))
8684         article)
8685     (setq gnus-newsgroup-unreads
8686           (gnus-sorted-intersection gnus-newsgroup-unreads
8687                                     gnus-newsgroup-limit))
8688     (if all
8689         (setq gnus-newsgroup-dormant nil
8690               gnus-newsgroup-marked nil
8691               gnus-newsgroup-reads
8692               (nconc
8693                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8694                gnus-newsgroup-reads))
8695       (while (setq article (pop articles))
8696         (unless (or (memq article gnus-newsgroup-dormant)
8697                     (memq article gnus-newsgroup-marked))
8698           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8699
8700 (defun gnus-summary-limit (articles &optional pop)
8701   (if pop
8702       ;; We pop the previous limit off the stack and use that.
8703       (setq articles (car gnus-newsgroup-limits)
8704             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8705     ;; We use the new limit, so we push the old limit on the stack.
8706     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8707   ;; Set the limit.
8708   (setq gnus-newsgroup-limit articles)
8709   (let ((total (length gnus-newsgroup-data))
8710         (data (gnus-data-find-list (gnus-summary-article-number)))
8711         (gnus-summary-mark-below nil)   ; Inhibit this.
8712         found)
8713     ;; This will do all the work of generating the new summary buffer
8714     ;; according to the new limit.
8715     (gnus-summary-prepare)
8716     ;; Hide any threads, possibly.
8717     (gnus-summary-maybe-hide-threads)
8718     ;; Try to return to the article you were at, or one in the
8719     ;; neighborhood.
8720     (when data
8721       ;; We try to find some article after the current one.
8722       (while data
8723         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8724           (setq data nil
8725                 found t))
8726         (setq data (cdr data))))
8727     (unless found
8728       ;; If there is no data, that means that we were after the last
8729       ;; article.  The same goes when we can't find any articles
8730       ;; after the current one.
8731       (goto-char (point-max))
8732       (gnus-summary-find-prev))
8733     (gnus-set-mode-line 'summary)
8734     ;; We return how many articles were removed from the summary
8735     ;; buffer as a result of the new limit.
8736     (- total (length gnus-newsgroup-data))))
8737
8738 (defsubst gnus-invisible-cut-children (threads)
8739   (let ((num 0))
8740     (while threads
8741       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8742         (incf num))
8743       (pop threads))
8744     (< num 2)))
8745
8746 (defsubst gnus-cut-thread (thread)
8747   "Go forwards in the thread until we find an article that we want to display."
8748   (when (or (eq gnus-fetch-old-headers 'some)
8749             (eq gnus-fetch-old-headers 'invisible)
8750             (numberp gnus-fetch-old-headers)
8751             (eq gnus-build-sparse-threads 'some)
8752             (eq gnus-build-sparse-threads 'more))
8753     ;; Deal with old-fetched headers and sparse threads.
8754     (while (and
8755             thread
8756             (or
8757              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8758              (gnus-summary-article-ancient-p
8759               (mail-header-number (car thread))))
8760             (if (or (<= (length (cdr thread)) 1)
8761                     (eq gnus-fetch-old-headers 'invisible))
8762                 (setq gnus-newsgroup-limit
8763                       (delq (mail-header-number (car thread))
8764                             gnus-newsgroup-limit)
8765                       thread (cadr thread))
8766               (when (gnus-invisible-cut-children (cdr thread))
8767                 (let ((th (cdr thread)))
8768                   (while th
8769                     (if (memq (mail-header-number (caar th))
8770                               gnus-newsgroup-limit)
8771                         (setq thread (car th)
8772                               th nil)
8773                       (setq th (cdr th))))))))))
8774   thread)
8775
8776 (defun gnus-cut-threads (threads)
8777   "Cut off all uninteresting articles from the beginning of THREADS."
8778   (when (or (eq gnus-fetch-old-headers 'some)
8779             (eq gnus-fetch-old-headers 'invisible)
8780             (numberp gnus-fetch-old-headers)
8781             (eq gnus-build-sparse-threads 'some)
8782             (eq gnus-build-sparse-threads 'more))
8783     (let ((th threads))
8784       (while th
8785         (setcar th (gnus-cut-thread (car th)))
8786         (setq th (cdr th)))))
8787   ;; Remove nixed out threads.
8788   (delq nil threads))
8789
8790 (defun gnus-summary-initial-limit (&optional show-if-empty)
8791   "Figure out what the initial limit is supposed to be on group entry.
8792 This entails weeding out unwanted dormants, low-scored articles,
8793 fetch-old-headers verbiage, and so on."
8794   ;; Most groups have nothing to remove.
8795   (unless (or gnus-inhibit-limiting
8796               (and (null gnus-newsgroup-dormant)
8797                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8798                    (not (eq gnus-fetch-old-headers 'some))
8799                    (not (numberp gnus-fetch-old-headers))
8800                    (not (eq gnus-fetch-old-headers 'invisible))
8801                    (null gnus-summary-expunge-below)
8802                    (not (eq gnus-build-sparse-threads 'some))
8803                    (not (eq gnus-build-sparse-threads 'more))
8804                    (null gnus-thread-expunge-below)))
8805     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8806     (setq gnus-newsgroup-limit nil)
8807     (mapatoms
8808      (lambda (node)
8809        (unless (car (symbol-value node))
8810          ;; These threads have no parents -- they are roots.
8811          (let ((nodes (cdr (symbol-value node)))
8812                thread)
8813            (while nodes
8814              (if (and gnus-thread-expunge-below
8815                       (< (gnus-thread-total-score (car nodes))
8816                          gnus-thread-expunge-below))
8817                  (gnus-expunge-thread (pop nodes))
8818                (setq thread (pop nodes))
8819                (gnus-summary-limit-children thread))))))
8820      gnus-newsgroup-dependencies)
8821     ;; If this limitation resulted in an empty group, we might
8822     ;; pop the previous limit and use it instead.
8823     (when (and (not gnus-newsgroup-limit)
8824                show-if-empty)
8825       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8826     gnus-newsgroup-limit))
8827
8828 (defun gnus-summary-limit-children (thread)
8829   "Return 1 if this subthread is visible and 0 if it is not."
8830   ;; First we get the number of visible children to this thread.  This
8831   ;; is done by recursing down the thread using this function, so this
8832   ;; will really go down to a leaf article first, before slowly
8833   ;; working its way up towards the root.
8834   (when thread
8835     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8836            (children
8837            (if (cdr thread)
8838                (apply '+ (mapcar 'gnus-summary-limit-children
8839                                  (cdr thread)))
8840              0))
8841            (number (mail-header-number (car thread)))
8842            score)
8843       (if (and
8844            (not (memq number gnus-newsgroup-marked))
8845            (or
8846             ;; If this article is dormant and has absolutely no visible
8847             ;; children, then this article isn't visible.
8848             (and (memq number gnus-newsgroup-dormant)
8849                  (zerop children))
8850             ;; If this is "fetch-old-headered" and there is no
8851             ;; visible children, then we don't want this article.
8852             (and (or (eq gnus-fetch-old-headers 'some)
8853                      (numberp gnus-fetch-old-headers))
8854                  (gnus-summary-article-ancient-p number)
8855                  (zerop children))
8856             ;; If this is "fetch-old-headered" and `invisible', then
8857             ;; we don't want this article.
8858             (and (eq gnus-fetch-old-headers 'invisible)
8859                  (gnus-summary-article-ancient-p number))
8860             ;; If this is a sparsely inserted article with no children,
8861             ;; we don't want it.
8862             (and (eq gnus-build-sparse-threads 'some)
8863                  (gnus-summary-article-sparse-p number)
8864                  (zerop children))
8865             ;; If we use expunging, and this article is really
8866             ;; low-scored, then we don't want this article.
8867             (when (and gnus-summary-expunge-below
8868                        (< (setq score
8869                                 (or (cdr (assq number gnus-newsgroup-scored))
8870                                     gnus-summary-default-score))
8871                           gnus-summary-expunge-below))
8872               ;; We increase the expunge-tally here, but that has
8873               ;; nothing to do with the limits, really.
8874               (incf gnus-newsgroup-expunged-tally)
8875               ;; We also mark as read here, if that's wanted.
8876               (when (and gnus-summary-mark-below
8877                          (< score gnus-summary-mark-below))
8878                 (setq gnus-newsgroup-unreads
8879                       (delq number gnus-newsgroup-unreads))
8880                 (if gnus-newsgroup-auto-expire
8881                     (push number gnus-newsgroup-expirable)
8882                   (push (cons number gnus-low-score-mark)
8883                         gnus-newsgroup-reads)))
8884               t)
8885             ;; Do the `display' group parameter.
8886             (and gnus-newsgroup-display
8887                  (let ((gnus-number number))
8888                    (not (funcall gnus-newsgroup-display))))))
8889           ;; Nope, invisible article.
8890           0
8891         ;; Ok, this article is to be visible, so we add it to the limit
8892         ;; and return 1.
8893         (push number gnus-newsgroup-limit)
8894         1))))
8895
8896 (defun gnus-expunge-thread (thread)
8897   "Mark all articles in THREAD as read."
8898   (let* ((number (mail-header-number (car thread))))
8899     (incf gnus-newsgroup-expunged-tally)
8900     ;; We also mark as read here, if that's wanted.
8901     (setq gnus-newsgroup-unreads
8902           (delq number gnus-newsgroup-unreads))
8903     (if gnus-newsgroup-auto-expire
8904         (push number gnus-newsgroup-expirable)
8905       (push (cons number gnus-low-score-mark)
8906             gnus-newsgroup-reads)))
8907   ;; Go recursively through all subthreads.
8908   (mapcar 'gnus-expunge-thread (cdr thread)))
8909
8910 ;; Summary article oriented commands
8911
8912 (defun gnus-summary-refer-parent-article (n)
8913   "Refer parent article N times.
8914 If N is negative, go to ancestor -N instead.
8915 The difference between N and the number of articles fetched is returned."
8916   (interactive "p")
8917   (let ((skip 1)
8918         error header ref)
8919     (when (not (natnump n))
8920       (setq skip (abs n)
8921             n 1))
8922     (while (and (> n 0)
8923                 (not error))
8924       (setq header (gnus-summary-article-header))
8925       (if (and (eq (mail-header-number header)
8926                    (cdr gnus-article-current))
8927                (equal gnus-newsgroup-name
8928                       (car gnus-article-current)))
8929           ;; If we try to find the parent of the currently
8930           ;; displayed article, then we take a look at the actual
8931           ;; References header, since this is slightly more
8932           ;; reliable than the References field we got from the
8933           ;; server.
8934           (with-current-buffer gnus-original-article-buffer
8935             (nnheader-narrow-to-headers)
8936             (unless (setq ref (message-fetch-field "references"))
8937               (when (setq ref (message-fetch-field "in-reply-to"))
8938                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8939             (widen))
8940         (setq ref
8941               ;; It's not the current article, so we take a bet on
8942               ;; the value we got from the server.
8943               (mail-header-references header)))
8944       (if (and ref
8945                (not (equal ref "")))
8946           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8947             (gnus-message 1 "Couldn't find parent"))
8948         (gnus-message 1 "No references in article %d"
8949                       (gnus-summary-article-number))
8950         (setq error t))
8951       (decf n))
8952     (gnus-summary-position-point)
8953     n))
8954
8955 (defun gnus-summary-refer-references ()
8956   "Fetch all articles mentioned in the References header.
8957 Return the number of articles fetched."
8958   (interactive)
8959   (let ((ref (mail-header-references (gnus-summary-article-header)))
8960         (current (gnus-summary-article-number))
8961         (n 0))
8962     (if (or (not ref)
8963             (equal ref ""))
8964         (error "No References in the current article")
8965       ;; For each Message-ID in the References header...
8966       (while (string-match "<[^>]*>" ref)
8967         (incf n)
8968         ;; ... fetch that article.
8969         (gnus-summary-refer-article
8970          (prog1 (match-string 0 ref)
8971            (setq ref (substring ref (match-end 0))))))
8972       (gnus-summary-goto-subject current)
8973       (gnus-summary-position-point)
8974       n)))
8975
8976 (defun gnus-delete-duplicate-headers (headers)
8977   ;; First remove leading duplicates.
8978   (while (and (> (length headers) 1)
8979               (= (mail-header-number (car headers))
8980                  (mail-header-number (cadr headers))))
8981     (pop headers))
8982   ;; Then the rest.
8983   (let ((result headers))
8984     (while (> (length headers) 1)
8985       (if (= (mail-header-number (car headers))
8986              (mail-header-number (cadr headers)))
8987           (setcdr headers (cddr headers))
8988         (pop headers)))
8989     result))
8990
8991 (defun gnus-summary-refer-thread (&optional limit)
8992   "Fetch all articles in the current thread. For backends that
8993 know how to search for threads (currently only 'nnimap) a
8994 non-numeric prefix arg will use nnir to search the entire
8995 server; without a prefix arg only the current group is
8996 searched. If the variable `gnus-refer-thread-use-nnir' is
8997 non-nil the prefix arg has the reverse meaning. If no
8998 backend-specific 'request-thread function is available fetch
8999 LIMIT (the numerical prefix) old headers. If LIMIT is
9000 non-numeric or nil fetch the number specified by the
9001 `gnus-refer-thread-limit' variable."
9002   (interactive "P")
9003   (gnus-warp-to-article)
9004   (let* ((header (gnus-summary-article-header))
9005          (id (mail-header-id header))
9006          (gnus-inhibit-demon t)
9007          (gnus-summary-ignore-duplicates t)
9008          (gnus-read-all-available-headers t)
9009          (gnus-refer-thread-use-nnir
9010           (if (and (not (null limit)) (listp limit))
9011               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9012          (new-headers
9013           (if (gnus-check-backend-function
9014                'request-thread gnus-newsgroup-name)
9015               (gnus-request-thread header gnus-newsgroup-name)
9016             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9017                             gnus-refer-thread-limit))
9018                    (last (if (numberp limit)
9019                              (min (+ (mail-header-number header)
9020                                      limit)
9021                                   gnus-newsgroup-highest)
9022                            gnus-newsgroup-highest))
9023                    (subject (gnus-simplify-subject
9024                              (mail-header-subject header)))
9025                    (refs (split-string (or (mail-header-references header)
9026                                            "")))
9027                    (gnus-parse-headers-hook
9028                     `(lambda () (goto-char (point-min))
9029                       (keep-lines
9030                        (regexp-opt ',(append refs (list id subject)))))))
9031               (gnus-fetch-headers (list last) (if (numberp limit)
9032                                                   (* 2 limit) limit) t))))
9033          article-ids)
9034     (when (listp new-headers)
9035       (dolist (header new-headers)
9036         (push (mail-header-number header) article-ids)
9037         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9038           (push (mail-header-number header) gnus-newsgroup-unreads)
9039           (setq gnus-newsgroup-unselected
9040                 (delete (mail-header-number header)
9041                         gnus-newsgroup-unselected))))
9042       (setq gnus-newsgroup-headers
9043             (gnus-delete-duplicate-headers
9044              (gnus-merge
9045               'list gnus-newsgroup-headers new-headers
9046               'gnus-article-sort-by-number)))
9047       (setq gnus-newsgroup-articles
9048             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9049       (gnus-summary-limit-include-thread id)))
9050   (gnus-summary-show-thread))
9051
9052 (defun gnus-summary-refer-article (message-id)
9053   "Fetch an article specified by MESSAGE-ID."
9054   (interactive "sMessage-ID: ")
9055   (gnus-warp-to-article)
9056   (when (and (stringp message-id)
9057              (not (zerop (length message-id))))
9058     (setq message-id (gnus-replace-in-string message-id " " ""))
9059     ;; Construct the correct Message-ID if necessary.
9060     ;; Suggested by tale@pawl.rpi.edu.
9061     (unless (string-match "^<" message-id)
9062       (setq message-id (concat "<" message-id)))
9063     (unless (string-match ">$" message-id)
9064       (setq message-id (concat message-id ">")))
9065     ;; People often post MIDs from URLs, so unhex it:
9066     (unless (string-match "@" message-id)
9067       (setq message-id (gnus-url-unhex-string message-id)))
9068     (let* ((header (gnus-id-to-header message-id))
9069            (sparse (and header
9070                         (gnus-summary-article-sparse-p
9071                          (mail-header-number header))
9072                         (memq (mail-header-number header)
9073                               gnus-newsgroup-limit)))
9074            number)
9075       (cond
9076        ;; If the article is present in the buffer we just go to it.
9077        ((and header
9078              (or (not (gnus-summary-article-sparse-p
9079                        (mail-header-number header)))
9080                  sparse))
9081         (prog1
9082             (gnus-summary-goto-article
9083              (mail-header-number header) nil t)
9084           (when sparse
9085             (gnus-summary-update-article (mail-header-number header)))))
9086        (t
9087         ;; We fetch the article.
9088         (catch 'found
9089           (dolist (gnus-override-method (gnus-refer-article-methods))
9090             (when (and (gnus-check-server gnus-override-method)
9091                        ;; Fetch the header,
9092                        (setq number (gnus-summary-insert-subject message-id)))
9093               ;; and display the article.
9094               (gnus-summary-select-article nil nil nil number)
9095               (throw 'found t)))
9096           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9097
9098 (defun gnus-refer-article-methods ()
9099   "Return a list of referable methods."
9100   (cond
9101    ;; No method, so we default to current and native.
9102    ((null gnus-refer-article-method)
9103     (list gnus-current-select-method gnus-select-method))
9104    ;; Current.
9105    ((eq 'current gnus-refer-article-method)
9106     (list gnus-current-select-method))
9107    ;; List of select methods.
9108    ((not (and (symbolp (car gnus-refer-article-method))
9109               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9110     (let (out)
9111       (dolist (method gnus-refer-article-method)
9112         (push (if (eq 'current method)
9113                   gnus-current-select-method
9114                 (if (eq 'nnir (car method))
9115                     (list
9116                      'nnir
9117                      (or (cadr method)
9118                          (gnus-method-to-server gnus-current-select-method)))
9119                   method))
9120               out))
9121       (nreverse out)))
9122    ;; One single select method.
9123    (t
9124     (list gnus-refer-article-method))))
9125
9126 (defun gnus-summary-edit-parameters ()
9127   "Edit the group parameters of the current group."
9128   (interactive)
9129   (gnus-group-edit-group gnus-newsgroup-name 'params))
9130
9131 (defun gnus-summary-customize-parameters ()
9132   "Customize the group parameters of the current group."
9133   (interactive)
9134   (gnus-group-customize gnus-newsgroup-name))
9135
9136 (defun gnus-summary-enter-digest-group (&optional force)
9137   "Enter an nndoc group based on the current article.
9138 If FORCE, force a digest interpretation.  If not, try to guess
9139 what the document format is.
9140
9141 To control what happens when you exit the group, see the
9142 `gnus-auto-select-on-ephemeral-exit' variable."
9143   (interactive "P")
9144   (let ((conf gnus-current-window-configuration))
9145     (save-window-excursion
9146       (save-excursion
9147         (let (gnus-article-prepare-hook
9148               gnus-display-mime-function
9149               gnus-break-pages)
9150           (gnus-summary-select-article))))
9151     (setq gnus-current-window-configuration conf)
9152     (let* ((name (format "%s-%d"
9153                          (gnus-group-prefixed-name
9154                           gnus-newsgroup-name (list 'nndoc ""))
9155                          (with-current-buffer gnus-summary-buffer
9156                            gnus-current-article)))
9157            (ogroup gnus-newsgroup-name)
9158            (params (append (gnus-info-params (gnus-get-info ogroup))
9159                            (list (cons 'to-group ogroup))
9160                            (list (cons 'parent-group ogroup))
9161                            (list (cons 'save-article-group ogroup))))
9162            (case-fold-search t)
9163            (buf (current-buffer))
9164            dig to-address)
9165       (with-current-buffer gnus-original-article-buffer
9166         ;; Have the digest group inherit the main mail address of
9167         ;; the parent article.
9168         (when (setq to-address (or (gnus-fetch-field "reply-to")
9169                                    (gnus-fetch-field "from")))
9170           (setq params
9171                 (append
9172                  (list (cons 'to-address
9173                              (funcall gnus-decode-encoded-address-function
9174                                       to-address))))))
9175         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9176         (insert-buffer-substring gnus-original-article-buffer)
9177         ;; Remove lines that may lead nndoc to misinterpret the
9178         ;; document type.
9179         (narrow-to-region
9180          (goto-char (point-min))
9181          (or (search-forward "\n\n" nil t) (point)))
9182         (goto-char (point-min))
9183         (delete-matching-lines "^Path:\\|^From ")
9184         (widen))
9185       (unwind-protect
9186           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9187                     (gnus-newsgroup-ephemeral-ignored-charsets
9188                      gnus-newsgroup-ignored-charsets))
9189                 (gnus-group-read-ephemeral-group
9190                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9191                               (nndoc-article-type
9192                                ,(if force 'mbox 'guess)))
9193                  t nil nil nil
9194                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9195                                                         "ADAPT")))))
9196               ;; Make all postings to this group go to the parent group.
9197               (nconc (gnus-info-params (gnus-get-info name))
9198                      params)
9199             ;; Couldn't select this doc group.
9200             (switch-to-buffer buf)
9201             (gnus-set-global-variables)
9202             (gnus-configure-windows 'summary)
9203             (gnus-message 3 "Article couldn't be entered?"))
9204         (kill-buffer dig)))))
9205
9206 (defun gnus-summary-read-document (n)
9207   "Open a new group based on the current article(s).
9208 This will allow you to read digests and other similar
9209 documents as newsgroups.
9210 Obeys the standard process/prefix convention."
9211   (interactive "P")
9212   (let* ((ogroup gnus-newsgroup-name)
9213          (params (append (gnus-info-params (gnus-get-info ogroup))
9214                          (list (cons 'to-group ogroup))))
9215          group egroup groups vgroup)
9216     (dolist (article (gnus-summary-work-articles n))
9217       (setq group (format "%s-%d" gnus-newsgroup-name article))
9218       (gnus-summary-remove-process-mark article)
9219       (when (gnus-summary-display-article article)
9220         (save-excursion ;;What for?
9221           (with-temp-buffer
9222             (insert-buffer-substring gnus-original-article-buffer)
9223             ;; Remove some headers that may lead nndoc to make
9224             ;; the wrong guess.
9225             (message-narrow-to-head)
9226             (goto-char (point-min))
9227             (delete-matching-lines "^Path:\\|^From ")
9228             (widen)
9229             (if (setq egroup
9230                       (gnus-group-read-ephemeral-group
9231                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9232                                      (nndoc-article-type guess))
9233                        t nil t))
9234                 (progn
9235                   ;; Make all postings to this group go to the parent group.
9236                   (nconc (gnus-info-params (gnus-get-info egroup))
9237                          params)
9238                   (push egroup groups))
9239               ;; Couldn't select this doc group.
9240               (gnus-error 3 "Article couldn't be entered"))))))
9241     ;; Now we have selected all the documents.
9242     (cond
9243      ((not groups)
9244       (error "None of the articles could be interpreted as documents"))
9245      ((gnus-group-read-ephemeral-group
9246        (setq vgroup (format
9247                      "nnvirtual:%s-%s" gnus-newsgroup-name
9248                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9249        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9250        t
9251        (cons (current-buffer) 'summary)))
9252      (t
9253       (error "Couldn't select virtual nndoc group")))))
9254
9255 (defun gnus-summary-widget-forward (arg)
9256   "Move point to the next field or button in the article.
9257 With optional ARG, move across that many fields."
9258   (interactive "p")
9259   (gnus-summary-select-article)
9260   (gnus-configure-windows 'article)
9261   (select-window (gnus-get-buffer-window gnus-article-buffer))
9262   (widget-forward arg))
9263
9264 (defun gnus-summary-isearch-article (&optional regexp-p)
9265   "Do incremental search forward on the current article.
9266 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9267   (interactive "P")
9268   (gnus-summary-select-article)
9269   (gnus-configure-windows 'article)
9270   (gnus-eval-in-buffer-window gnus-article-buffer
9271     (save-restriction
9272       (widen)
9273       (isearch-forward regexp-p))))
9274
9275 (defun gnus-summary-repeat-search-article-forward ()
9276   "Repeat the previous search forwards."
9277   (interactive)
9278   (unless gnus-last-search-regexp
9279     (error "No previous search"))
9280   (gnus-summary-search-article-forward gnus-last-search-regexp))
9281
9282 (defun gnus-summary-repeat-search-article-backward ()
9283   "Repeat the previous search backwards."
9284   (interactive)
9285   (unless gnus-last-search-regexp
9286     (error "No previous search"))
9287   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9288
9289 (defun gnus-summary-search-article-forward (regexp &optional backward)
9290   "Search for an article containing REGEXP forward.
9291 If BACKWARD, search backward instead."
9292   (interactive
9293    (list (read-string
9294           (format "Search article %s (regexp%s): "
9295                   (if current-prefix-arg "backward" "forward")
9296                   (if gnus-last-search-regexp
9297                       (concat ", default " gnus-last-search-regexp)
9298                     "")))
9299          current-prefix-arg))
9300   (if (string-equal regexp "")
9301       (setq regexp (or gnus-last-search-regexp ""))
9302     (setq gnus-last-search-regexp regexp)
9303     (setq gnus-article-before-search gnus-current-article))
9304   ;; Intentionally set gnus-last-article.
9305   (setq gnus-last-article gnus-article-before-search)
9306   (let ((gnus-last-article gnus-last-article))
9307     (if (gnus-summary-search-article regexp backward)
9308         (gnus-summary-show-thread)
9309       (signal 'search-failed (list regexp)))))
9310
9311 (defun gnus-summary-search-article-backward (regexp)
9312   "Search for an article containing REGEXP backward."
9313   (interactive
9314    (list (read-string
9315           (format "Search article backward (regexp%s): "
9316                   (if gnus-last-search-regexp
9317                       (concat ", default " gnus-last-search-regexp)
9318                     "")))))
9319   (gnus-summary-search-article-forward regexp 'backward))
9320
9321 (defun gnus-summary-search-article (regexp &optional backward)
9322   "Search for an article containing REGEXP.
9323 Optional argument BACKWARD means do search for backward.
9324 `gnus-select-article-hook' is not called during the search."
9325   ;; We have to require this here to make sure that the following
9326   ;; dynamic binding isn't shadowed by autoloading.
9327   (require 'gnus-async)
9328   (require 'gnus-art)
9329   (let ((gnus-select-article-hook nil)  ;Disable hook.
9330         (gnus-article-prepare-hook nil)
9331         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9332         (gnus-use-article-prefetch nil)
9333         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9334         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9335         (gnus-visual nil)
9336         (gnus-keep-backlog nil)
9337         (gnus-break-pages nil)
9338         (gnus-summary-display-arrow nil)
9339         (gnus-updated-mode-lines nil)
9340         (gnus-auto-center-summary nil)
9341         (sum (current-buffer))
9342         (gnus-display-mime-function nil)
9343         (found nil)
9344         point)
9345     (gnus-save-hidden-threads
9346       (gnus-summary-select-article)
9347       (set-buffer gnus-article-buffer)
9348       (goto-char (window-point (get-buffer-window (current-buffer))))
9349       (when backward
9350         (forward-line -1))
9351       (while (not found)
9352         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9353         (if (if backward
9354                 (re-search-backward regexp nil t)
9355               (re-search-forward regexp nil t))
9356             ;; We found the regexp.
9357             (progn
9358               (setq found 'found)
9359               (beginning-of-line)
9360               (set-window-start
9361                (get-buffer-window (current-buffer))
9362                (point))
9363               (forward-line 1)
9364               (set-window-point
9365                (get-buffer-window (current-buffer))
9366                (point))
9367               (set-buffer sum)
9368               (setq point (point)))
9369           ;; We didn't find it, so we go to the next article.
9370           (set-buffer sum)
9371           (setq found 'not)
9372           (while (eq found 'not)
9373             (if (not (if backward (gnus-summary-find-prev)
9374                        (gnus-summary-find-next)))
9375                 ;; No more articles.
9376                 (setq found t)
9377               ;; Select the next article and adjust point.
9378               (unless (gnus-summary-article-sparse-p
9379                        (gnus-summary-article-number))
9380                 (setq found nil)
9381                 (gnus-summary-select-article)
9382                 (set-buffer gnus-article-buffer)
9383                 (widen)
9384                 (goto-char (if backward (point-max) (point-min))))))))
9385       (gnus-message 7 ""))
9386     ;; Return whether we found the regexp.
9387     (when (eq found 'found)
9388       (goto-char point)
9389       (gnus-summary-show-thread)
9390       (gnus-summary-goto-subject gnus-current-article)
9391       (gnus-summary-position-point)
9392       t)))
9393
9394 (defun gnus-find-matching-articles (header regexp)
9395   "Return a list of all articles that match REGEXP on HEADER.
9396 This search includes all articles in the current group that Gnus has
9397 fetched headers for, whether they are displayed or not."
9398   (let ((articles nil)
9399         ;; Can't eta-reduce because it's a macro.
9400         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9401         (case-fold-search t))
9402     (dolist (header gnus-newsgroup-headers)
9403       (when (string-match regexp (funcall func header))
9404         (push (mail-header-number header) articles)))
9405     (nreverse articles)))
9406
9407 (defun gnus-summary-find-matching (header regexp &optional backward unread
9408                                           not-case-fold not-matching)
9409   "Return a list of all articles that match REGEXP on HEADER.
9410 The search stars on the current article and goes forwards unless
9411 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9412 If UNREAD is non-nil, only unread articles will
9413 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9414 in the comparisons. If NOT-MATCHING, return a list of all articles that
9415 not match REGEXP on HEADER."
9416   (let ((case-fold-search (not not-case-fold))
9417         articles d func)
9418     (if (consp header)
9419         (if (eq (car header) 'extra)
9420             (setq func
9421                   `(lambda (h)
9422                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9423                          "")))
9424           (error "%s is an invalid header" header))
9425       (unless (fboundp (intern (concat "mail-header-" header)))
9426         (error "%s is not a valid header" header))
9427       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9428     (dolist (d (if (eq backward 'all)
9429                    gnus-newsgroup-data
9430                  (gnus-data-find-list
9431                   (gnus-summary-article-number)
9432                   (gnus-data-list backward))))
9433       (when (and (or (not unread)       ; We want all articles...
9434                      (gnus-data-unread-p d)) ; Or just unreads.
9435                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9436                  (if not-matching
9437                      (not (string-match
9438                            regexp
9439                            (funcall func (gnus-data-header d))))
9440                    (string-match regexp
9441                                  (funcall func (gnus-data-header d)))))
9442         (push (gnus-data-number d) articles))) ; Success!
9443     (nreverse articles)))
9444
9445 (defun gnus-summary-execute-command (header regexp command &optional backward)
9446   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9447 If HEADER is an empty string (or nil), the match is done on the entire
9448 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9449   (interactive
9450    (list (let ((completion-ignore-case t))
9451            (gnus-completing-read
9452             "Header name"
9453             (mapcar 'symbol-name
9454                     (append
9455                      '(Number Subject From Lines Date
9456                        Message-ID Xref References Body)
9457                      gnus-extra-headers))
9458             'require-match))
9459          (read-string "Regexp: ")
9460          (read-key-sequence "Command: ")
9461          current-prefix-arg))
9462   (when (equal header "Body")
9463     (setq header ""))
9464   ;; Hidden thread subtrees must be searched as well.
9465   (gnus-summary-show-all-threads)
9466   ;; We don't want to change current point nor window configuration.
9467   (save-excursion
9468     (save-window-excursion
9469       (let (gnus-visual
9470             gnus-treat-strip-trailing-blank-lines
9471             gnus-treat-strip-leading-blank-lines
9472             gnus-treat-strip-multiple-blank-lines
9473             gnus-treat-hide-boring-headers
9474             gnus-treat-fold-newsgroups
9475             gnus-article-prepare-hook)
9476         (gnus-message 6 "Executing %s..." (key-description command))
9477         ;; We'd like to execute COMMAND interactively so as to give arguments.
9478         (gnus-execute header regexp
9479                       `(call-interactively ',(key-binding command))
9480                       backward)
9481         (gnus-message 6 "Executing %s...done" (key-description command))))))
9482
9483 (defun gnus-summary-beginning-of-article ()
9484   "Scroll the article back to the beginning."
9485   (interactive)
9486   (gnus-summary-select-article)
9487   (gnus-configure-windows 'article)
9488   (gnus-eval-in-buffer-window gnus-article-buffer
9489     (widen)
9490     (goto-char (point-min))
9491     (when gnus-break-pages
9492       (gnus-narrow-to-page))))
9493
9494 (defun gnus-summary-end-of-article ()
9495   "Scroll to the end of the article."
9496   (interactive)
9497   (gnus-summary-select-article)
9498   (gnus-configure-windows 'article)
9499   (gnus-eval-in-buffer-window gnus-article-buffer
9500     (widen)
9501     (goto-char (point-max))
9502     (recenter -3)
9503     (when gnus-break-pages
9504       (gnus-narrow-to-page))))
9505
9506 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9507   "Truncate to LEN and quote all \"(\"'s in STRING."
9508   (gnus-replace-in-string (if (and len (> (length string) len))
9509                               (substring string 0 len)
9510                             string)
9511                           "[()]" "\\\\\\&"))
9512
9513 (defun gnus-summary-print-article (&optional filename n)
9514   "Generate and print a PostScript image of the process-marked (mail) articles.
9515
9516 If used interactively, print the current article if none are
9517 process-marked.  With prefix arg, prompt the user for the name of the
9518 file to save in.
9519
9520 When used from Lisp, accept two optional args FILENAME and N.  N means
9521 to print the next N articles.  If N is negative, print the N previous
9522 articles.  If N is nil and articles have been marked with the process
9523 mark, print these instead.
9524
9525 If the optional first argument FILENAME is nil, send the image to the
9526 printer.  If FILENAME is a string, save the PostScript image in a file with
9527 that name.  If FILENAME is a number, prompt the user for the name of the file
9528 to save in."
9529   (interactive (list (ps-print-preprint current-prefix-arg)))
9530   (dolist (article (gnus-summary-work-articles n))
9531     (gnus-summary-select-article nil nil 'pseudo article)
9532     (gnus-eval-in-buffer-window gnus-article-buffer
9533       (gnus-print-buffer))
9534     (gnus-summary-remove-process-mark article))
9535   (ps-despool filename))
9536
9537 (defun gnus-print-buffer ()
9538   (let ((ps-left-header
9539          (list
9540           (concat "("
9541                   (gnus-summary-print-truncate-and-quote
9542                    (mail-header-subject gnus-current-headers)
9543                    66) ")")
9544           (concat "("
9545                   (gnus-summary-print-truncate-and-quote
9546                    (mail-header-from gnus-current-headers)
9547                    45) ")")))
9548         (ps-right-header
9549          (list
9550           "/pagenumberstring load"
9551           (concat "("
9552                   (mail-header-date gnus-current-headers) ")"))))
9553     (gnus-run-hooks 'gnus-ps-print-hook)
9554     (save-excursion
9555       (if ps-print-color-p
9556           (ps-spool-buffer-with-faces)
9557         (ps-spool-buffer)))))
9558
9559 (defun gnus-summary-show-complete-article ()
9560   "Show a complete version of the current article.
9561 This is only useful if you're looking at a partial version of the
9562 article currently."
9563   (interactive)
9564   (let ((gnus-keep-backlog nil)
9565         (gnus-use-cache nil)
9566         (gnus-agent nil)
9567         (variable (intern
9568                    (format "%s-fetch-partial-articles"
9569                            (car (gnus-find-method-for-group
9570                                  gnus-newsgroup-name)))
9571                    obarray))
9572         old-val)
9573     (unwind-protect
9574         (progn
9575           (setq old-val (symbol-value variable))
9576           (set variable nil)
9577           (gnus-flush-original-article-buffer)
9578           (gnus-summary-show-article))
9579       (set variable old-val))))
9580
9581 (defun gnus-summary-show-article (&optional arg)
9582   "Force redisplaying of the current article.
9583 If ARG (the prefix) is a number, show the article with the charset
9584 defined in `gnus-summary-show-article-charset-alist', or the charset
9585 input.
9586 If ARG (the prefix) is non-nil and not a number, show the article,
9587 but without running any of the article treatment functions
9588 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9589 C-u g', show the raw article."
9590   (interactive "P")
9591   (cond
9592    ((numberp arg)
9593     (gnus-summary-show-article t)
9594     (let ((gnus-newsgroup-charset
9595            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9596                (mm-read-coding-system
9597                 "View as charset: " ;; actually it is coding system.
9598                 (with-current-buffer gnus-article-buffer
9599                   (mm-detect-coding-region (point) (point-max))))))
9600           (gnus-newsgroup-ignored-charsets 'gnus-all))
9601       (gnus-summary-select-article nil 'force)
9602       (let ((deps gnus-newsgroup-dependencies)
9603             head header lines)
9604         (with-current-buffer gnus-original-article-buffer
9605           (save-restriction
9606             (message-narrow-to-head)
9607             (setq head (buffer-string))
9608             (goto-char (point-min))
9609             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9610               (goto-char (point-max))
9611               (widen)
9612               (setq lines (1- (count-lines (point) (point-max))))))
9613           (with-temp-buffer
9614             (insert (format "211 %d Article retrieved.\n"
9615                             (cdr gnus-article-current)))
9616             (insert head)
9617             (if lines (insert (format "Lines: %d\n" lines)))
9618             (insert ".\n")
9619             (let ((nntp-server-buffer (current-buffer)))
9620               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9621         (gnus-data-set-header
9622          (gnus-data-find (cdr gnus-article-current))
9623          header)
9624         (gnus-summary-update-article-line
9625          (cdr gnus-article-current) header)
9626         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9627           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9628    ((not arg)
9629     ;; Select the article the normal way.
9630     (if (eq mm-text-html-renderer 'shr)
9631         (progn
9632           (require 'shr)
9633           (let ((shr-ignore-cache t))
9634             (gnus-summary-select-article nil 'force)))
9635       (gnus-summary-select-article nil 'force)))
9636    ((equal arg '(16))
9637     ;; C-u C-u g
9638     (let ((gnus-inhibit-article-treatments t))
9639       (gnus-summary-select-article nil 'force)))
9640    (t
9641     ;; We have to require this here to make sure that the following
9642     ;; dynamic binding isn't shadowed by autoloading.
9643     (require 'gnus-async)
9644     (require 'gnus-art)
9645     ;; Bind the article treatment functions to nil.
9646     (let ((gnus-have-all-headers t)
9647           gnus-article-prepare-hook
9648           gnus-article-decode-hook
9649           gnus-display-mime-function
9650           gnus-break-pages)
9651       ;; Destroy any MIME parts.
9652       (when (gnus-buffer-live-p gnus-article-buffer)
9653         (with-current-buffer gnus-article-buffer
9654           (gnus-article-stop-animations)
9655           (mm-destroy-parts gnus-article-mime-handles)
9656           ;; Set it to nil for safety reason.
9657           (setq gnus-article-mime-handle-alist nil)
9658           (setq gnus-article-mime-handles nil)))
9659       (gnus-summary-select-article nil 'force))))
9660   (gnus-summary-goto-subject gnus-current-article)
9661   (gnus-summary-position-point))
9662
9663 (defun gnus-summary-show-raw-article ()
9664   "Show the raw article without any article massaging functions being run."
9665   (interactive)
9666   (gnus-summary-show-article t))
9667
9668 (defun gnus-summary-verbose-headers (&optional arg)
9669   "Toggle permanent full header display.
9670 If ARG is a positive number, turn header display on.
9671 If ARG is a negative number, turn header display off."
9672   (interactive "P")
9673   (setq gnus-show-all-headers
9674         (cond ((or (not (numberp arg))
9675                    (zerop arg))
9676                (not gnus-show-all-headers))
9677               ((natnump arg)
9678                t)))
9679   (gnus-summary-show-article))
9680
9681 (defun gnus-summary-toggle-header (&optional arg)
9682   "Show the headers if they are hidden, or hide them if they are shown.
9683 If ARG is a positive number, show the entire header.
9684 If ARG is a negative number, hide the unwanted header lines."
9685   (interactive "P")
9686   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9687                      (get-buffer-window gnus-article-buffer t))))
9688     (with-current-buffer gnus-article-buffer
9689       (widen)
9690       (article-narrow-to-head)
9691       (let* ((inhibit-read-only t)
9692              (inhibit-point-motion-hooks t)
9693              (hidden (if (numberp arg)
9694                          (>= arg 0)
9695                        (or (not (looking-at "[^ \t\n]+:"))
9696                            (gnus-article-hidden-text-p 'headers))))
9697              s e)
9698         (delete-region (point-min) (point-max))
9699         (with-current-buffer gnus-original-article-buffer
9700           (goto-char (setq s (point-min)))
9701           (setq e (if (search-forward "\n\n" nil t)
9702                       (1- (point))
9703                     (point-max))))
9704         (insert-buffer-substring gnus-original-article-buffer s e)
9705         (run-hooks 'gnus-article-decode-hook)
9706         (if hidden
9707             (let ((gnus-treat-hide-headers nil)
9708                   (gnus-treat-hide-boring-headers nil))
9709               (gnus-delete-wash-type 'headers)
9710               (gnus-treat-article 'head))
9711           (gnus-treat-article 'head))
9712         (widen)
9713         (if window
9714             (set-window-start window (goto-char (point-min))))
9715         (if gnus-break-pages
9716             (gnus-narrow-to-page)
9717           (when (gnus-visual-p 'page-marker)
9718             (let ((inhibit-read-only t))
9719               (gnus-remove-text-with-property 'gnus-prev)
9720               (gnus-remove-text-with-property 'gnus-next))))
9721         (gnus-set-mode-line 'article)))))
9722
9723 (defun gnus-summary-show-all-headers ()
9724   "Make all header lines visible."
9725   (interactive)
9726   (gnus-summary-toggle-header 1))
9727
9728 (defun gnus-summary-caesar-message (&optional arg)
9729   "Caesar rotate the current article by 13.
9730 With a non-numerical prefix, also rotate headers.  A numerical
9731 prefix specifies how many places to rotate each letter forward."
9732   (interactive "P")
9733   (gnus-summary-select-article)
9734   (let ((mail-header-separator ""))
9735     (gnus-eval-in-buffer-window gnus-article-buffer
9736       (save-restriction
9737         (widen)
9738         (let ((start (window-start))
9739               (inhibit-read-only t))
9740           (if (equal arg '(4))
9741               (message-caesar-buffer-body nil t)
9742             (message-caesar-buffer-body arg))
9743           (set-window-start (get-buffer-window (current-buffer)) start)))))
9744   ;; Create buttons and stuff...
9745   (gnus-treat-article nil))
9746
9747 (declare-function idna-to-unicode "ext:idna" (str))
9748
9749 (defun gnus-summary-idna-message (&optional arg)
9750   "Decode IDNA encoded domain names in the current articles.
9751 IDNA encoded domain names looks like `xn--bar'.  If a string
9752 remain unencoded after running this function, it is likely an
9753 invalid IDNA string (`xn--bar' is invalid).
9754
9755 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9756 installed for this command to work."
9757   (interactive "P")
9758   (if (not (and (condition-case nil (require 'idna)
9759                   (file-error))
9760                 (mm-coding-system-p 'utf-8)
9761                 (executable-find (symbol-value 'idna-program))))
9762       (gnus-message
9763        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9764     (gnus-summary-select-article)
9765     (let ((mail-header-separator ""))
9766       (gnus-eval-in-buffer-window gnus-article-buffer
9767         (save-restriction
9768           (widen)
9769           (let ((start (window-start))
9770                 buffer-read-only)
9771             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9772               (replace-match (idna-to-unicode (match-string 1))))
9773             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9774
9775 (defun gnus-summary-morse-message (&optional arg)
9776   "Morse decode the current article."
9777   (interactive "P")
9778   (gnus-summary-select-article)
9779   (let ((mail-header-separator ""))
9780     (gnus-eval-in-buffer-window gnus-article-buffer
9781       (save-excursion
9782         (save-restriction
9783           (widen)
9784           (let ((pos (window-start))
9785                 (inhibit-read-only t))
9786             (goto-char (point-min))
9787             (when (message-goto-body)
9788               (gnus-narrow-to-body))
9789             (goto-char (point-min))
9790             (while (search-forward "·" (point-max) t)
9791               (replace-match "."))
9792             (unmorse-region (point-min) (point-max))
9793             (widen)
9794             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9795
9796 (defun gnus-summary-stop-page-breaking ()
9797   "Stop page breaking in the current article."
9798   (interactive)
9799   (gnus-summary-select-article)
9800   (gnus-eval-in-buffer-window gnus-article-buffer
9801     (widen)
9802     (when (gnus-visual-p 'page-marker)
9803       (let ((inhibit-read-only t))
9804         (gnus-remove-text-with-property 'gnus-prev)
9805         (gnus-remove-text-with-property 'gnus-next))
9806       (setq gnus-page-broken nil))))
9807
9808 (defun gnus-summary-move-article (&optional n to-newsgroup
9809                                             select-method action)
9810   "Move the current article to a different newsgroup.
9811 If N is a positive number, move the N next articles.
9812 If N is a negative number, move the N previous articles.
9813 If N is nil and any articles have been marked with the process mark,
9814 move those articles instead.
9815 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9816 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9817 re-spool using this method.
9818
9819 When called interactively with TO-NEWSGROUP being nil, the value of
9820 the variable `gnus-move-split-methods' is used for finding a default
9821 for the target newsgroup.
9822
9823 For this function to work, both the current newsgroup and the
9824 newsgroup that you want to move to have to support the `request-move'
9825 and `request-accept' functions.
9826
9827 ACTION can be either `move' (the default), `crosspost' or `copy'."
9828   (interactive "P")
9829   (unless action
9830     (setq action 'move))
9831   ;; Check whether the source group supports the required functions.
9832   (cond ((and (eq action 'move)
9833               (not (gnus-check-backend-function
9834                     'request-move-article gnus-newsgroup-name)))
9835          (error "The current group does not support article moving"))
9836         ((and (eq action 'crosspost)
9837               (not (gnus-check-backend-function
9838                     'request-replace-article gnus-newsgroup-name)))
9839          (error "The current group does not support article editing")))
9840   (let ((articles (gnus-summary-work-articles n))
9841         (prefix (if (gnus-check-backend-function
9842                      'request-move-article gnus-newsgroup-name)
9843                     (funcall gnus-move-group-prefix-function
9844                              gnus-newsgroup-name)
9845                   ""))
9846         (names '((move "Move" "Moving")
9847                  (copy "Copy" "Copying")
9848                  (crosspost "Crosspost" "Crossposting")))
9849         (copy-buf (save-excursion
9850                     (nnheader-set-temp-buffer " *copy article*")))
9851         art-group to-method new-xref article to-groups
9852         articles-to-update-marks encoded)
9853     (unless (assq action names)
9854       (error "Unknown action %s" action))
9855     ;; Read the newsgroup name.
9856     (when (and (not to-newsgroup)
9857                (not select-method))
9858       (if (and gnus-move-split-methods
9859                (not
9860                 (and (memq gnus-current-article articles)
9861                      (gnus-buffer-live-p gnus-original-article-buffer))))
9862           ;; When `gnus-move-split-methods' is non-nil, we have to
9863           ;; select an article to give `gnus-read-move-group-name' an
9864           ;; opportunity to suggest an appropriate default.  However,
9865           ;; we needn't render or mark the article.
9866           (let ((gnus-display-mime-function nil)
9867                 (gnus-article-prepare-hook nil)
9868                 (gnus-mark-article-hook nil))
9869             (gnus-summary-select-article nil nil nil (car articles))))
9870       (setq to-newsgroup (gnus-read-move-group-name
9871                           (cadr (assq action names))
9872                           (symbol-value
9873                            (intern (format "gnus-current-%s-group" action)))
9874                           articles prefix)
9875             encoded to-newsgroup
9876             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9877       (set (intern (format "gnus-current-%s-group" action))
9878            (mm-decode-coding-string
9879             to-newsgroup
9880             (gnus-group-name-charset to-method to-newsgroup))))
9881     (unless to-method
9882       (setq to-method (or select-method
9883                           (gnus-server-to-method
9884                            (gnus-group-method to-newsgroup)))))
9885     (setq to-newsgroup
9886           (or encoded
9887               (and to-newsgroup
9888                    (mm-encode-coding-string
9889                     to-newsgroup
9890                     (gnus-group-name-charset to-method to-newsgroup)))))
9891     ;; Check the method we are to move this article to...
9892     (unless (gnus-check-backend-function
9893              'request-accept-article (car to-method))
9894       (error "%s does not support article copying" (car to-method)))
9895     (unless (gnus-check-server to-method)
9896       (error "Can't open server %s" (car to-method)))
9897     (gnus-message 6 "%s to %s: %s..."
9898                   (caddr (assq action names))
9899                   (or (car select-method)
9900                       (gnus-group-decoded-name to-newsgroup))
9901                   articles)
9902     (while articles
9903       (setq article (pop articles))
9904       ;; Set any marks that may have changed in the summary buffer.
9905       (when gnus-preserve-marks
9906         (gnus-summary-push-marks-to-backend article))
9907       (setq
9908        art-group
9909        (cond
9910         ;; Move the article.
9911         ((eq action 'move)
9912          ;; Remove this article from future suppression.
9913          (gnus-dup-unsuppress-article article)
9914          (let* ((from-method (gnus-find-method-for-group
9915                               gnus-newsgroup-name))
9916                 (to-method (or select-method
9917                                (gnus-find-method-for-group to-newsgroup)))
9918                 (move-is-internal (gnus-server-equal from-method to-method)))
9919            (gnus-request-move-article
9920             article                     ; Article to move
9921             gnus-newsgroup-name         ; From newsgroup
9922             (nth 1 (gnus-find-method-for-group
9923                     gnus-newsgroup-name)) ; Server
9924             (list 'gnus-request-accept-article
9925                   to-newsgroup (list 'quote select-method)
9926                   (not articles) t)     ; Accept form
9927             (not articles)              ; Only save nov last time
9928             (and move-is-internal
9929                  to-newsgroup           ; Not respooling
9930                                         ; Is this move internal?
9931                  (gnus-group-real-name to-newsgroup)))))
9932         ;; Copy the article.
9933         ((eq action 'copy)
9934          (with-current-buffer copy-buf
9935            (when (gnus-request-article-this-buffer article
9936                                                    gnus-newsgroup-name)
9937              (save-restriction
9938                (nnheader-narrow-to-headers)
9939                (dolist (hdr gnus-copy-article-ignored-headers)
9940                  (message-remove-header hdr t)))
9941              (gnus-request-accept-article
9942               to-newsgroup select-method (not articles) t))))
9943         ;; Crosspost the article.
9944         ((eq action 'crosspost)
9945          (let ((xref (message-tokenize-header
9946                       (mail-header-xref (gnus-summary-article-header
9947                                          article))
9948                       " ")))
9949            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9950                                   ":" (number-to-string article)))
9951            (unless xref
9952              (setq xref (list (system-name))))
9953            (setq new-xref
9954                  (concat
9955                   (mapconcat 'identity
9956                              (delete "Xref:" (delete new-xref xref))
9957                              " ")
9958                   " " new-xref))
9959            (with-current-buffer copy-buf
9960              ;; First put the article in the destination group.
9961              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9962              (when (consp (setq art-group
9963                                 (gnus-request-accept-article
9964                                  to-newsgroup select-method (not articles)
9965                                  t)))
9966                (setq new-xref (concat new-xref " " (car art-group)
9967                                       ":"
9968                                       (number-to-string (cdr art-group))))
9969                ;; Now we have the new Xrefs header, so we insert
9970                ;; it and replace the new article.
9971                (nnheader-replace-header "Xref" new-xref)
9972                (gnus-request-replace-article
9973                 (cdr art-group) to-newsgroup (current-buffer) t)
9974                art-group))))))
9975       (cond
9976        ((not art-group)
9977         (gnus-message 1 "Couldn't %s article %s: %s"
9978                       (cadr (assq action names)) article
9979                       (nnheader-get-report (car to-method))))
9980        ((eq art-group 'junk)
9981         (when (eq action 'move)
9982           (gnus-summary-mark-article article gnus-canceled-mark)
9983           (gnus-message 4 "Deleted article %s" article)
9984           ;; run the delete hook
9985           (run-hook-with-args 'gnus-summary-article-delete-hook
9986                               action
9987                               (gnus-data-header
9988                                (assoc article (gnus-data-list nil)))
9989                               gnus-newsgroup-name nil
9990                               select-method)))
9991        (t
9992         (let* ((pto-group (gnus-group-prefixed-name
9993                            (car art-group) to-method))
9994                (info (gnus-get-info pto-group))
9995                (to-group (gnus-info-group info))
9996                to-marks)
9997           ;; Update the group that has been moved to.
9998           (when (and info
9999                      (memq action '(move copy)))
10000             (unless (member to-group to-groups)
10001               (push to-group to-groups))
10002
10003             (when (and (not (memq article gnus-newsgroup-unreads))
10004                        (cdr art-group))
10005               (push 'read to-marks)
10006               (gnus-info-set-read
10007                info (gnus-add-to-range (gnus-info-read info)
10008                                        (list (cdr art-group)))))
10009
10010             ;; See whether the article is to be put in the cache.
10011             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10012                    (marks (if expirable
10013                               gnus-article-mark-lists
10014                             (delete '(expirable . expire)
10015                                     (copy-sequence
10016                                      gnus-article-mark-lists))))
10017                    (to-article (cdr art-group)))
10018
10019               ;; Enter the article into the cache in the new group,
10020               ;; if that is required.
10021               (when (and to-article
10022                          gnus-use-cache)
10023                 (gnus-cache-possibly-enter-article
10024                  to-group to-article
10025                  (memq article gnus-newsgroup-marked)
10026                  (memq article gnus-newsgroup-dormant)
10027                  (memq article gnus-newsgroup-unreads)))
10028
10029               (when (and gnus-preserve-marks
10030                          to-article)
10031                 ;; Copy any marks over to the new group.
10032                 (when (and (equal to-group gnus-newsgroup-name)
10033                            (not (memq article gnus-newsgroup-unreads)))
10034                   ;; Mark this article as read in this group.
10035                   (push (cons to-article gnus-read-mark)
10036                         gnus-newsgroup-reads)
10037                   ;; Increase the active status of this group.
10038                   (setcdr (gnus-active to-group) to-article)
10039                   (setcdr gnus-newsgroup-active to-article))
10040
10041                 (while marks
10042                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10043                     (when (memq article (symbol-value
10044                                          (intern (format "gnus-newsgroup-%s"
10045                                                          (caar marks)))))
10046                       (push (cdar marks) to-marks)
10047                       ;; If the other group is the same as this group,
10048                       ;; then we have to add the mark to the list.
10049                       (when (equal to-group gnus-newsgroup-name)
10050                         (set (intern (format "gnus-newsgroup-%s"
10051                                              (caar marks)))
10052                              (cons to-article
10053                                    (symbol-value
10054                                     (intern (format "gnus-newsgroup-%s"
10055                                                     (caar marks)))))))
10056                       ;; Copy the marks to other group.
10057                       (gnus-add-marked-articles
10058                        to-group (cdar marks) (list to-article) info)))
10059                   (setq marks (cdr marks)))
10060
10061                 (when (and expirable
10062                            gnus-mark-copied-or-moved-articles-as-expirable
10063                            (not (memq 'expire to-marks)))
10064                   ;; Mark this article as expirable.
10065                   (push 'expire to-marks)
10066                   (when (equal to-group gnus-newsgroup-name)
10067                     (push to-article gnus-newsgroup-expirable))
10068                   ;; Copy the expirable mark to other group.
10069                   (gnus-add-marked-articles
10070                    to-group 'expire (list to-article) info))
10071
10072                 (when (and to-marks
10073                            (or gnus-propagate-marks
10074                                (gnus-method-option-p
10075                                 (gnus-find-method-for-group to-group)
10076                                 'server-marks)))
10077                   (gnus-request-set-mark
10078                    to-group (list (list (list to-article) 'add to-marks)))))
10079
10080               (gnus-dribble-enter
10081                (concat "(gnus-group-set-info '"
10082                        (gnus-prin1-to-string (gnus-get-info to-group))
10083                        ")")
10084                (concat "^(gnus-group-set-info '(\""
10085                        (regexp-quote to-group) "\""))))
10086
10087           ;; Update the Xref header in this article to point to
10088           ;; the new crossposted article we have just created.
10089           (when (eq action 'crosspost)
10090             (with-current-buffer copy-buf
10091               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10092               (nnheader-replace-header "Xref" new-xref)
10093               (gnus-request-replace-article
10094                article gnus-newsgroup-name (current-buffer) t)))
10095
10096           ;; run the move/copy/crosspost/respool hook
10097           (run-hook-with-args 'gnus-summary-article-move-hook
10098                               action
10099                               (gnus-data-header
10100                                (assoc article (gnus-data-list nil)))
10101                               gnus-newsgroup-name
10102                               to-newsgroup
10103                               select-method))
10104
10105         ;;;!!!Why is this necessary?
10106         (set-buffer gnus-summary-buffer)
10107
10108         (when (eq action 'move)
10109           (save-excursion
10110             (gnus-summary-goto-subject article)
10111             (gnus-summary-mark-article article gnus-canceled-mark)))))
10112       (push article articles-to-update-marks))
10113
10114     (save-excursion
10115       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10116     ;; Re-activate all groups that have been moved to.
10117     (with-current-buffer gnus-group-buffer
10118       (let ((gnus-group-marked to-groups))
10119         (gnus-group-get-new-news-this-group nil t)))
10120
10121     (gnus-kill-buffer copy-buf)
10122     (gnus-summary-position-point)
10123     (gnus-set-mode-line 'summary)))
10124
10125 (defun gnus-summary-push-marks-to-backend (article)
10126   (let ((set nil)
10127         (marks gnus-article-mark-lists))
10128     (unless (memq article gnus-newsgroup-unreads)
10129       (push 'read set))
10130     (while marks
10131       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10132                  (memq article (symbol-value
10133                                 (intern (format "gnus-newsgroup-%s"
10134                                                 (caar marks))))))
10135         (push (cdar marks) set))
10136       (pop marks))
10137     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10138
10139 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10140   "Copy the current article to some other group.
10141 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10142 When called interactively, if TO-NEWSGROUP is nil, use the value of
10143 the variable `gnus-move-split-methods' for finding a default target
10144 newsgroup.
10145 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10146 re-spool using this method."
10147   (interactive "P")
10148   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10149
10150 (defun gnus-summary-crosspost-article (&optional n)
10151   "Crosspost the current article to some other group."
10152   (interactive "P")
10153   (gnus-summary-move-article n nil nil 'crosspost))
10154
10155 (defcustom gnus-summary-respool-default-method nil
10156   "Default method type for respooling an article.
10157 If nil, use to the current newsgroup method."
10158   :type 'symbol
10159   :group 'gnus-summary-mail)
10160
10161 (defun gnus-summary-respool-article (&optional n method)
10162   "Respool the current article.
10163 The article will be squeezed through the mail spooling process again,
10164 which means that it will be put in some mail newsgroup or other
10165 depending on `nnmail-split-methods'.
10166 If N is a positive number, respool the N next articles.
10167 If N is a negative number, respool the N previous articles.
10168 If N is nil and any articles have been marked with the process mark,
10169 respool those articles instead.
10170
10171 Respooling can be done both from mail groups and \"real\" newsgroups.
10172 In the former case, the articles in question will be moved from the
10173 current group into whatever groups they are destined to.  In the
10174 latter case, they will be copied into the relevant groups."
10175   (interactive
10176    (list current-prefix-arg
10177          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10178                 (methname
10179                  (symbol-name (or gnus-summary-respool-default-method
10180                                   (car (gnus-find-method-for-group
10181                                         gnus-newsgroup-name)))))
10182                 (method
10183                  (gnus-completing-read
10184                   "Backend to use when respooling"
10185                   methods t nil 'gnus-mail-method-history methname))
10186                 ms)
10187            (cond
10188             ((zerop (length (setq ms (gnus-servers-using-backend
10189                                       (intern method)))))
10190              (list (intern method) ""))
10191             ((= 1 (length ms))
10192              (car ms))
10193             (t
10194              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10195                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10196                            ms-alist))))))))
10197   (unless method
10198     (error "No method given for respooling"))
10199   (if (assoc (symbol-name
10200               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10201              (gnus-methods-using 'respool))
10202       (gnus-summary-move-article n nil method)
10203     (gnus-summary-copy-article n nil method)))
10204
10205 (defun gnus-summary-import-article (file &optional edit)
10206   "Import an arbitrary file into a mail newsgroup."
10207   (interactive "fImport file: \nP")
10208   (let ((group gnus-newsgroup-name)
10209         (now (current-time))
10210         atts lines group-art)
10211     (unless (gnus-check-backend-function 'request-accept-article group)
10212       (error "%s does not support article importing" group))
10213     (or (file-readable-p file)
10214         (not (file-regular-p file))
10215         (error "Can't read %s" file))
10216     (with-current-buffer (gnus-get-buffer-create " *import file*")
10217       (erase-buffer)
10218       (nnheader-insert-file-contents file)
10219       (goto-char (point-min))
10220       (if (nnheader-article-p)
10221           (save-restriction
10222             (goto-char (point-min))
10223             (search-forward "\n\n" nil t)
10224             (narrow-to-region (point-min) (1- (point)))
10225             (goto-char (point-min))
10226             (unless (re-search-forward "^date:" nil t)
10227               (goto-char (point-max))
10228               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10229        ;; This doesn't look like an article, so we fudge some headers.
10230         (setq atts (file-attributes file)
10231               lines (count-lines (point-min) (point-max)))
10232         (insert "From: " (read-string "From: ") "\n"
10233                 "Subject: " (read-string "Subject: ") "\n"
10234                 "Date: " (message-make-date (nth 5 atts)) "\n"
10235                 "Message-ID: " (message-make-message-id) "\n"
10236                 "Lines: " (int-to-string lines) "\n"
10237                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10238       (setq group-art (gnus-request-accept-article group nil t))
10239       (kill-buffer (current-buffer)))
10240     (setq gnus-newsgroup-active (gnus-activate-group group))
10241     (forward-line 1)
10242     (gnus-summary-goto-article (cdr group-art) nil t)
10243     (when edit
10244       (gnus-summary-edit-article))))
10245
10246 (defun gnus-summary-create-article ()
10247   "Create an article in a mail newsgroup."
10248   (interactive)
10249   (let ((group gnus-newsgroup-name)
10250         (now (current-time))
10251         group-art)
10252     (unless (gnus-check-backend-function 'request-accept-article group)
10253       (error "%s does not support article importing" group))
10254     (with-current-buffer (gnus-get-buffer-create " *import file*")
10255       (erase-buffer)
10256       (goto-char (point-min))
10257       ;; This doesn't look like an article, so we fudge some headers.
10258       (insert "From: " (read-string "From: ") "\n"
10259               "Subject: " (read-string "Subject: ") "\n"
10260               "Date: " (message-make-date now) "\n"
10261               "Message-ID: " (message-make-message-id) "\n")
10262       (setq group-art (gnus-request-accept-article group nil t))
10263       (kill-buffer (current-buffer)))
10264     (setq gnus-newsgroup-active (gnus-activate-group group))
10265     (forward-line 1)
10266     (gnus-summary-goto-article (cdr group-art) nil t)
10267     (gnus-summary-edit-article)))
10268
10269 (defun gnus-summary-article-posted-p ()
10270   "Say whether the current (mail) article is available from news as well.
10271 This will be the case if the article has both been mailed and posted."
10272   (interactive)
10273   (let ((id (mail-header-references (gnus-summary-article-header)))
10274         (gnus-override-method (car (gnus-refer-article-methods))))
10275     (if (gnus-request-head id "")
10276         (gnus-message 2 "The current message was found on %s"
10277                       gnus-override-method)
10278       (gnus-message 2 "The current message couldn't be found on %s"
10279                     gnus-override-method)
10280       nil)))
10281
10282 (defun gnus-summary-expire-articles (&optional now)
10283   "Expire all articles that are marked as expirable in the current group."
10284   (interactive)
10285   (when (and (not gnus-group-is-exiting-without-update-p)
10286              (gnus-check-backend-function
10287               'request-expire-articles gnus-newsgroup-name))
10288     ;; This backend supports expiry.
10289     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10290            (expirable (if total
10291                           (progn
10292                             ;; We need to update the info for
10293                             ;; this group for `gnus-list-of-read-articles'
10294                             ;; to give us the right answer.
10295                             (gnus-run-hooks 'gnus-exit-group-hook)
10296                             (gnus-summary-update-info)
10297                             (gnus-list-of-read-articles gnus-newsgroup-name))
10298                         (setq gnus-newsgroup-expirable
10299                               (sort gnus-newsgroup-expirable '<))))
10300            (expiry-wait (if now 'immediate
10301                           (gnus-group-find-parameter
10302                            gnus-newsgroup-name 'expiry-wait)))
10303            (nnmail-expiry-target
10304             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10305                 nnmail-expiry-target))
10306            es)
10307       (when expirable
10308         ;; There are expirable articles in this group, so we run them
10309         ;; through the expiry process.
10310         (gnus-message 6 "Expiring articles...")
10311         (when (gnus-check-group gnus-newsgroup-name)
10312           ;; The list of articles that weren't expired is returned.
10313           (save-excursion
10314             (if expiry-wait
10315                 (let ((nnmail-expiry-wait-function nil)
10316                       (nnmail-expiry-wait expiry-wait))
10317                   (setq es (gnus-request-expire-articles
10318                             expirable gnus-newsgroup-name)))
10319               (setq es (gnus-request-expire-articles
10320                         expirable gnus-newsgroup-name)))
10321             (unless total
10322               (setq gnus-newsgroup-expirable es))
10323             ;; We go through the old list of expirable, and mark all
10324             ;; really expired articles as nonexistent.
10325             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10326               (let ((gnus-use-cache nil))
10327                 (dolist (article expirable)
10328                   (when (and (not (memq article es))
10329                              (gnus-data-find article))
10330                     (gnus-summary-mark-article article gnus-canceled-mark)
10331                     (run-hook-with-args 'gnus-summary-article-expire-hook
10332                                         'delete
10333                                         (gnus-data-header
10334                                          (assoc article (gnus-data-list nil)))
10335                                         gnus-newsgroup-name
10336                                         nil
10337                                         nil)))))))
10338         (gnus-message 6 "Expiring articles...done")))))
10339
10340 (defun gnus-summary-expire-articles-now ()
10341   "Expunge all expirable articles in the current group.
10342 This means that *all* articles that are marked as expirable will be
10343 deleted forever, right now."
10344   (interactive)
10345   (or gnus-expert-user
10346       (gnus-yes-or-no-p
10347        "Are you really, really sure you want to delete all expirable messages? ")
10348       (error "Phew!"))
10349   (gnus-summary-expire-articles t))
10350
10351 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10352 (defun gnus-summary-delete-article (&optional n)
10353   "Delete the N next (mail) articles.
10354 This command actually deletes articles.  This is not a marking
10355 command.  The article will disappear forever from your life, never to
10356 return.
10357
10358 If N is negative, delete backwards.
10359 If N is nil and articles have been marked with the process mark,
10360 delete these instead.
10361
10362 If `gnus-novice-user' is non-nil you will be asked for
10363 confirmation before the articles are deleted."
10364   (interactive "P")
10365   (unless (gnus-check-backend-function 'request-expire-articles
10366                                        gnus-newsgroup-name)
10367     (error "The current newsgroup does not support article deletion"))
10368   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10369     (error "Couldn't open server"))
10370   ;; Compute the list of articles to delete.
10371   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10372         (nnmail-expiry-target 'delete)
10373         not-deleted)
10374     (if (and gnus-novice-user
10375              (not (gnus-yes-or-no-p
10376                    (format "Do you really want to delete %s forever? "
10377                            (if (> (length articles) 1)
10378                                (format "these %s articles" (length articles))
10379                              "this article")))))
10380         ()
10381       ;; Delete the articles.
10382       (setq not-deleted (gnus-request-expire-articles
10383                          articles gnus-newsgroup-name 'force))
10384       (save-excursion
10385         (while articles
10386           (gnus-summary-remove-process-mark (car articles))
10387           ;; The backend might not have been able to delete the article
10388           ;; after all.
10389           (unless (memq (car articles) not-deleted)
10390             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10391             (let* ((article (car articles))
10392                    (ghead  (gnus-data-header
10393                             (assoc article (gnus-data-list nil)))))
10394               (run-hook-with-args 'gnus-summary-article-delete-hook
10395                                   'delete ghead gnus-newsgroup-name nil
10396                                   nil)))
10397           (setq articles (cdr articles))))
10398       (when not-deleted
10399         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10400     (gnus-summary-position-point)
10401     (gnus-set-mode-line 'summary)
10402     not-deleted))
10403
10404 (defun gnus-summary-edit-article (&optional arg)
10405   "Edit the current article.
10406 This will have permanent effect only in mail groups.
10407 If ARG is nil, edit the decoded articles.
10408 If ARG is 1, edit the raw articles.
10409 If ARG is 2, edit the raw articles even in read-only groups.
10410 If ARG is 3, edit the articles with the current handles.
10411 Otherwise, allow editing of articles even in read-only
10412 groups."
10413   (interactive "P")
10414   (let (force raw current-handles)
10415     (cond
10416      ((null arg))
10417      ((eq arg 1)
10418       (setq raw t))
10419      ((eq arg 2)
10420       (setq raw t
10421             force t))
10422      ((eq arg 3)
10423       (setq current-handles
10424             (and (gnus-buffer-live-p gnus-article-buffer)
10425                  (with-current-buffer gnus-article-buffer
10426                    (prog1
10427                        gnus-article-mime-handles
10428                      (setq gnus-article-mime-handles nil))))))
10429      (t
10430       (setq force t)))
10431     (when (and raw (not force)
10432                (member gnus-newsgroup-name '("nndraft:delayed"
10433                                              "nndraft:drafts"
10434                                              "nndraft:queue")))
10435       (error "Can't edit the raw article in group %s"
10436              gnus-newsgroup-name))
10437     (with-current-buffer gnus-summary-buffer
10438       (let ((mail-parse-charset gnus-newsgroup-charset)
10439             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10440         (gnus-set-global-variables)
10441         (when (and (not force)
10442                    (gnus-group-read-only-p))
10443           (error "The current newsgroup does not support article editing"))
10444         (gnus-summary-show-article t)
10445         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10446           (with-current-buffer gnus-article-buffer
10447             (mm-enable-multibyte)))
10448         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10449             (setq raw t))
10450         (gnus-article-edit-article
10451          (if raw 'ignore
10452            `(lambda ()
10453               (let ((mbl mml-buffer-list))
10454                 (setq mml-buffer-list nil)
10455                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10456                   (mime-to-mml ,'current-handles))
10457                 (let ((mbl1 mml-buffer-list))
10458                   (setq mml-buffer-list mbl)
10459                   (set (make-local-variable 'mml-buffer-list) mbl1))
10460                 (gnus-make-local-hook 'kill-buffer-hook)
10461                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10462          `(lambda (no-highlight)
10463             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10464                   (message-options message-options)
10465                   (message-options-set-recipient)
10466                   (mail-parse-ignored-charsets
10467                    ',gnus-newsgroup-ignored-charsets)
10468                   (rfc2047-header-encoding-alist
10469                    ',(let ((charset (gnus-group-name-charset
10470                                      (gnus-find-method-for-group
10471                                       gnus-newsgroup-name)
10472                                      gnus-newsgroup-name)))
10473                        (append (list (cons "Newsgroups" charset)
10474                                      (cons "Followup-To" charset)
10475                                      (cons "Xref" charset))
10476                                rfc2047-header-encoding-alist))))
10477               ,(if (not raw) '(progn
10478                                 (mml-to-mime)
10479                                 (mml-destroy-buffers)
10480                                 (remove-hook 'kill-buffer-hook
10481                                              'mml-destroy-buffers t)
10482                                 (kill-local-variable 'mml-buffer-list)))
10483               (gnus-summary-edit-article-done
10484                ,(or (mail-header-references gnus-current-headers) "")
10485                ,(gnus-group-read-only-p)
10486                ,gnus-summary-buffer no-highlight))))))))
10487
10488 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10489
10490 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10491                                                  no-highlight)
10492   "Make edits to the current article permanent."
10493   (interactive)
10494   (save-excursion
10495     ;; The buffer restriction contains the entire article if it exists.
10496     (when (article-goto-body)
10497       (let ((lines (count-lines (point) (point-max)))
10498             (length (- (point-max) (point)))
10499             (case-fold-search t)
10500             (body (copy-marker (point))))
10501         (goto-char (point-min))
10502         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10503           (delete-region (match-beginning 1) (match-end 1))
10504           (insert (number-to-string length)))
10505         (goto-char (point-min))
10506         (when (re-search-forward
10507                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10508           (delete-region (match-beginning 1) (match-end 1))
10509           (insert (number-to-string length)))
10510         (goto-char (point-min))
10511         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10512           (delete-region (match-beginning 1) (match-end 1))
10513           (insert (number-to-string lines))))))
10514   ;; Replace the article.
10515   (let ((buf (current-buffer))
10516         (article (cdr gnus-article-current))
10517         replace-result)
10518     (with-temp-buffer
10519       (insert-buffer-substring buf)
10520       (if (and (not read-only)
10521                (not (setq replace-result
10522                           (gnus-request-replace-article
10523                            article (car gnus-article-current)
10524                            (current-buffer) t))))
10525           (error "Couldn't replace article")
10526         ;; If we got a number back, then that's the new article number
10527         ;; for this article.  Otherwise, the article number didn't change.
10528         (when (numberp replace-result)
10529           (with-current-buffer gnus-summary-buffer
10530             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10531             (gnus-summary-limit gnus-newsgroup-limit)
10532             (setq article replace-result)
10533             (gnus-summary-goto-subject article t)))
10534         ;; Update the summary buffer.
10535         (if (and references
10536                  (equal (message-tokenize-header references " ")
10537                         (message-tokenize-header
10538                          (or (message-fetch-field "references") "") " ")))
10539             ;; We only have to update this line.
10540             (save-excursion
10541               (save-restriction
10542                 (message-narrow-to-head)
10543                 (let ((head (buffer-substring-no-properties
10544                              (point-min) (point-max)))
10545                       header)
10546                   (with-temp-buffer
10547                     (insert (format "211 %d Article retrieved.\n" article))
10548                     (insert head)
10549                     (insert ".\n")
10550                     (let ((nntp-server-buffer (current-buffer)))
10551                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10552                     (with-current-buffer gnus-summary-buffer
10553                       (gnus-data-set-header (gnus-data-find article) header)
10554                       (gnus-summary-update-article-line article header)
10555                       (if (gnus-summary-goto-subject article nil t)
10556                           (gnus-summary-update-secondary-mark article)))))))
10557           ;; Update threads.
10558           (set-buffer (or buffer gnus-summary-buffer))
10559           (gnus-summary-update-article article)
10560           (if (gnus-summary-goto-subject article nil t)
10561               (gnus-summary-update-secondary-mark article)))
10562         ;; Prettify the article buffer again.
10563         (unless no-highlight
10564           (with-current-buffer gnus-article-buffer
10565             ;;!!! Fix this -- article should be rehighlighted.
10566             ;;(gnus-run-hooks 'gnus-article-display-hook)
10567             (set-buffer gnus-original-article-buffer)
10568             (gnus-request-article
10569              article (car gnus-article-current) (current-buffer))))
10570         ;; Prettify the summary buffer line.
10571         (when (gnus-visual-p 'summary-highlight 'highlight)
10572           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10573
10574 (defun gnus-summary-edit-wash (key)
10575   "Perform editing command KEY in the article buffer."
10576   (interactive
10577    (list
10578     (progn
10579       (message "%s" (concat (this-command-keys) "- "))
10580       (read-char))))
10581   (message "")
10582   (gnus-summary-edit-article)
10583   (execute-kbd-macro (concat (this-command-keys) key))
10584   (gnus-article-edit-done))
10585
10586 ;;; Respooling
10587
10588 (defun gnus-summary-respool-query (&optional silent trace)
10589   "Query where the respool algorithm would put this article."
10590   (interactive)
10591   (let (gnus-mark-article-hook)
10592     (gnus-summary-select-article)
10593     (with-current-buffer gnus-original-article-buffer
10594       (let ((groups (nnmail-article-group 'identity trace)))
10595         (unless silent
10596           (if groups
10597               (message "This message would go to %s"
10598                        (mapconcat 'car groups ", "))
10599             (message "This message would go to no groups"))
10600           groups)))))
10601
10602 (defun gnus-summary-respool-trace ()
10603   "Trace where the respool algorithm would put this article.
10604 Display a buffer showing all fancy splitting patterns which matched."
10605   (interactive)
10606   (gnus-summary-respool-query nil t))
10607
10608 ;; Summary marking commands.
10609
10610 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10611   "Mark articles which has the same subject as read, and then select the next.
10612 If UNMARK is positive, remove any kind of mark.
10613 If UNMARK is negative, tick articles."
10614   (interactive "P")
10615   (when unmark
10616     (setq unmark (prefix-numeric-value unmark)))
10617   (let ((count
10618          (gnus-summary-mark-same-subject
10619           (gnus-summary-article-subject) unmark)))
10620     ;; Select next unread article.  If auto-select-same mode, should
10621     ;; select the first unread article.
10622     (gnus-summary-next-article t (and gnus-auto-select-same
10623                                       (gnus-summary-article-subject)))
10624     (gnus-message 7 "%d article%s marked as %s"
10625                   count (if (= count 1) " is" "s are")
10626                   (if unmark "unread" "read"))))
10627
10628 (defun gnus-summary-kill-same-subject (&optional unmark)
10629   "Mark articles which has the same subject as read.
10630 If UNMARK is positive, remove any kind of mark.
10631 If UNMARK is negative, tick articles."
10632   (interactive "P")
10633   (when unmark
10634     (setq unmark (prefix-numeric-value unmark)))
10635   (let ((count
10636          (gnus-summary-mark-same-subject
10637           (gnus-summary-article-subject) unmark)))
10638     ;; If marked as read, go to next unread subject.
10639     (when (null unmark)
10640       ;; Go to next unread subject.
10641       (gnus-summary-next-subject 1 t))
10642     (gnus-message 7 "%d articles are marked as %s"
10643                   count (if unmark "unread" "read"))))
10644
10645 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10646   "Mark articles with same SUBJECT as read, and return marked number.
10647 If optional argument UNMARK is positive, remove any kinds of marks.
10648 If optional argument UNMARK is negative, mark articles as unread instead."
10649   (let ((count 1))
10650     (save-excursion
10651       (cond
10652        ((null unmark)                   ; Mark as read.
10653         (while (and
10654                 (progn
10655                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10656                   (gnus-summary-show-thread) t)
10657                 (gnus-summary-find-subject subject))
10658           (setq count (1+ count))))
10659        ((> unmark 0)                    ; Tick.
10660         (while (and
10661                 (progn
10662                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10663                   (gnus-summary-show-thread) t)
10664                 (gnus-summary-find-subject subject))
10665           (setq count (1+ count))))
10666        (t                               ; Mark as unread.
10667         (while (and
10668                 (progn
10669                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10670                   (gnus-summary-show-thread) t)
10671                 (gnus-summary-find-subject subject))
10672           (setq count (1+ count)))))
10673       (gnus-set-mode-line 'summary)
10674       ;; Return the number of marked articles.
10675       count)))
10676
10677 (defun gnus-summary-mark-as-processable (n &optional unmark)
10678   "Set the process mark on the next N articles.
10679 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10680 the process mark instead.  The difference between N and the actual
10681 number of articles marked is returned."
10682   (interactive "P")
10683   (if (and (null n) (gnus-region-active-p))
10684       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10685     (setq n (prefix-numeric-value n))
10686     (let ((backward (< n 0))
10687           (n (abs n)))
10688       (while (and
10689               (> n 0)
10690               (if unmark
10691                   (gnus-summary-remove-process-mark
10692                    (gnus-summary-article-number))
10693                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10694               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10695         (setq n (1- n)))
10696       (when (/= 0 n)
10697         (gnus-message 7 "No more articles"))
10698       (gnus-summary-recenter)
10699       (gnus-summary-position-point)
10700       n)))
10701
10702 (defun gnus-summary-unmark-as-processable (n)
10703   "Remove the process mark from the next N articles.
10704 If N is negative, unmark backward instead.  The difference between N and
10705 the actual number of articles unmarked is returned."
10706   (interactive "P")
10707   (gnus-summary-mark-as-processable n t))
10708
10709 (defun gnus-summary-unmark-all-processable ()
10710   "Remove the process mark from all articles."
10711   (interactive)
10712   (save-excursion
10713     (while gnus-newsgroup-processable
10714       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10715   (gnus-summary-position-point))
10716
10717 (defun gnus-summary-add-mark (article type)
10718   "Mark ARTICLE with a mark of TYPE."
10719   (let ((vtype (car (assq type gnus-article-mark-lists)))
10720         var)
10721     (if (not vtype)
10722         (error "No such mark type: %s" type)
10723       (setq var (intern (format "gnus-newsgroup-%s" type)))
10724       (set var (cons article (symbol-value var)))
10725       (if (memq type '(processable cached replied forwarded recent saved))
10726           (gnus-summary-update-secondary-mark article)
10727         ;;; !!! This is bogus.  We should find out what primary
10728         ;;; !!! mark we want to set.
10729         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10730
10731 (defun gnus-summary-mark-as-expirable (n)
10732   "Mark N articles forward as expirable.
10733 If N is negative, mark backward instead.  The difference between N and
10734 the actual number of articles marked is returned."
10735   (interactive "p")
10736   (gnus-summary-mark-forward n gnus-expirable-mark))
10737
10738 (defun gnus-summary-mark-as-spam (n)
10739   "Mark N articles forward as spam.
10740 If N is negative, mark backward instead.  The difference between N and
10741 the actual number of articles marked is returned."
10742   (interactive "p")
10743   (gnus-summary-mark-forward n gnus-spam-mark))
10744
10745 (defun gnus-summary-mark-article-as-replied (article)
10746   "Mark ARTICLE as replied to and update the summary line.
10747 ARTICLE can also be a list of articles."
10748   (interactive (list (gnus-summary-article-number)))
10749   (let ((articles (if (listp article) article (list article))))
10750     (dolist (article articles)
10751       (unless (numberp article)
10752         (error "%s is not a number" article))
10753       (push article gnus-newsgroup-replied)
10754       (let ((inhibit-read-only t))
10755         (when (gnus-summary-goto-subject article nil t)
10756           (gnus-summary-update-secondary-mark article))))))
10757
10758 (defun gnus-summary-mark-article-as-forwarded (article)
10759   "Mark ARTICLE as forwarded and update the summary line.
10760 ARTICLE can also be a list of articles."
10761   (let ((articles (if (listp article) article (list article))))
10762     (dolist (article articles)
10763       (push article gnus-newsgroup-forwarded)
10764       (let ((inhibit-read-only t))
10765         (when (gnus-summary-goto-subject article nil t)
10766           (gnus-summary-update-secondary-mark article))))))
10767
10768 (defun gnus-summary-set-bookmark (article)
10769   "Set a bookmark in current article."
10770   (interactive (list (gnus-summary-article-number)))
10771   (when (or (not (get-buffer gnus-article-buffer))
10772             (not gnus-current-article)
10773             (not gnus-article-current)
10774             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10775     (error "No current article selected"))
10776   ;; Remove old bookmark, if one exists.
10777   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10778   ;; Set the new bookmark, which is on the form
10779   ;; (article-number . line-number-in-body).
10780   (push
10781    (cons article
10782          (with-current-buffer gnus-article-buffer
10783            (count-lines
10784             (min (point)
10785                  (save-excursion
10786                    (article-goto-body)
10787                    (point)))
10788             (point))))
10789    gnus-newsgroup-bookmarks)
10790   (gnus-message 6 "A bookmark has been added to the current article."))
10791
10792 (defun gnus-summary-remove-bookmark (article)
10793   "Remove the bookmark from the current article."
10794   (interactive (list (gnus-summary-article-number)))
10795   ;; Remove old bookmark, if one exists.
10796   (if (not (assq article gnus-newsgroup-bookmarks))
10797       (gnus-message 6 "No bookmark in current article.")
10798     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10799     (gnus-message 6 "Removed bookmark.")))
10800
10801 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10802 (defun gnus-summary-mark-as-dormant (n)
10803   "Mark N articles forward as dormant.
10804 If N is negative, mark backward instead.  The difference between N and
10805 the actual number of articles marked is returned."
10806   (interactive "p")
10807   (gnus-summary-mark-forward n gnus-dormant-mark))
10808
10809 (defun gnus-summary-set-process-mark (article)
10810   "Set the process mark on ARTICLE and update the summary line."
10811   (setq gnus-newsgroup-processable
10812         (cons article
10813               (delq article gnus-newsgroup-processable)))
10814   (when (gnus-summary-goto-subject article)
10815     (gnus-summary-show-thread)
10816     (gnus-summary-goto-subject article)
10817     (gnus-summary-update-secondary-mark article)))
10818
10819 (defun gnus-summary-remove-process-mark (&rest articles)
10820   "Remove the process mark from ARTICLES and update the summary line."
10821   (dolist (article articles)
10822     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10823     (when (gnus-summary-goto-subject article)
10824       (gnus-summary-show-thread)
10825       (gnus-summary-goto-subject article)
10826       (gnus-summary-update-secondary-mark article)))
10827   t)
10828
10829 (defun gnus-summary-set-saved-mark (article)
10830   "Set the process mark on ARTICLE and update the summary line."
10831   (push article gnus-newsgroup-saved)
10832   (when (gnus-summary-goto-subject article)
10833     (gnus-summary-update-secondary-mark article)))
10834
10835 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10836   "Mark N articles as read forwards.
10837 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10838 The difference between N and the actual number of articles marked is
10839 returned.
10840 If NO-EXPIRE, auto-expiry will be inhibited."
10841   (interactive "p")
10842   (gnus-summary-show-thread)
10843   (let ((backward (< n 0))
10844         (gnus-summary-goto-unread
10845          (and gnus-summary-goto-unread
10846               (not (eq gnus-summary-goto-unread 'never))
10847               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10848                                     gnus-ticked-mark gnus-dormant-mark)))))
10849         (n (abs n))
10850         (mark (or mark gnus-del-mark)))
10851     (while (and (> n 0)
10852                 (gnus-summary-mark-article nil mark no-expire)
10853                 (zerop (gnus-summary-next-subject
10854                         (if backward -1 1)
10855                         (and gnus-summary-goto-unread
10856                              (not (eq gnus-summary-goto-unread 'never)))
10857                         t)))
10858       (setq n (1- n)))
10859     (when (/= 0 n)
10860       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10861     (gnus-summary-recenter)
10862     (gnus-summary-position-point)
10863     (gnus-set-mode-line 'summary)
10864     n))
10865
10866 (defun gnus-summary-mark-article-as-read (mark)
10867   "Mark the current article quickly as read with MARK."
10868   (let ((article (gnus-summary-article-number)))
10869     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10870     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10871     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10872     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10873     (push (cons article mark) gnus-newsgroup-reads)
10874     ;; Possibly remove from cache, if that is used.
10875     (when gnus-use-cache
10876       (gnus-cache-enter-remove-article article))
10877     ;; Allow the backend to change the mark.
10878     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10879     ;; Check for auto-expiry.
10880     (when (and gnus-newsgroup-auto-expire
10881                (memq mark gnus-auto-expirable-marks))
10882       (setq mark gnus-expirable-mark)
10883       ;; Let the backend know about the mark change.
10884       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10885       (push article gnus-newsgroup-expirable))
10886     ;; Set the mark in the buffer.
10887     (gnus-summary-update-mark mark 'unread)
10888     t))
10889
10890 (defun gnus-summary-mark-article-as-unread (mark)
10891   "Mark the current article quickly as unread with MARK."
10892   (let* ((article (gnus-summary-article-number))
10893          (old-mark (gnus-summary-article-mark article)))
10894     ;; Allow the backend to change the mark.
10895     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10896     (if (eq mark old-mark)
10897         t
10898       (if (<= article 0)
10899           (progn
10900             (gnus-error 1 "Can't mark negative article numbers")
10901             nil)
10902         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10903         (setq gnus-newsgroup-spam-marked
10904               (delq article gnus-newsgroup-spam-marked))
10905         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10906         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10907         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10908         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10909         (cond ((= mark gnus-ticked-mark)
10910                (setq gnus-newsgroup-marked
10911                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10912                                               article)))
10913               ((= mark gnus-spam-mark)
10914                (setq gnus-newsgroup-spam-marked
10915                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10916                                               article)))
10917               ((= mark gnus-dormant-mark)
10918                (setq gnus-newsgroup-dormant
10919                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10920                                               article)))
10921               (t
10922                (setq gnus-newsgroup-unreads
10923                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10924                                               article))))
10925         (gnus-alist-pull article gnus-newsgroup-reads)
10926
10927         ;; See whether the article is to be put in the cache.
10928         (and gnus-use-cache
10929              (vectorp (gnus-summary-article-header article))
10930              (save-excursion
10931                (gnus-cache-possibly-enter-article
10932                 gnus-newsgroup-name article
10933                 (= mark gnus-ticked-mark)
10934                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10935
10936         ;; Fix the mark.
10937         (gnus-summary-update-mark mark 'unread)
10938         t))))
10939
10940 (defun gnus-summary-mark-article (&optional article mark no-expire)
10941   "Mark ARTICLE with MARK.  MARK can be any character.
10942 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10943 `??' (dormant) and `?E' (expirable).
10944 If MARK is nil, then the default character `?r' is used.
10945 If ARTICLE is nil, then the article on the current line will be
10946 marked.
10947 If NO-EXPIRE, auto-expiry will be inhibited."
10948   ;; The mark might be a string.
10949   (when (stringp mark)
10950     (setq mark (aref mark 0)))
10951   ;; If no mark is given, then we check auto-expiring.
10952   (when (null mark)
10953     (setq mark gnus-del-mark))
10954   (when (and (not no-expire)
10955              gnus-newsgroup-auto-expire
10956              (memq mark gnus-auto-expirable-marks))
10957     (setq mark gnus-expirable-mark))
10958   (let ((article (or article (gnus-summary-article-number)))
10959         (old-mark (gnus-summary-article-mark article)))
10960     ;; Allow the backend to change the mark.
10961     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10962     (if (eq mark old-mark)
10963         t
10964       (unless article
10965         (error "No article on current line"))
10966       (if (not (if (or (= mark gnus-unread-mark)
10967                        (= mark gnus-ticked-mark)
10968                        (= mark gnus-spam-mark)
10969                        (= mark gnus-dormant-mark))
10970                    (gnus-mark-article-as-unread article mark)
10971                  (gnus-mark-article-as-read article mark)))
10972           t
10973         ;; See whether the article is to be put in the cache.
10974         (and gnus-use-cache
10975              (not (= mark gnus-canceled-mark))
10976              (vectorp (gnus-summary-article-header article))
10977              (save-excursion
10978                (gnus-cache-possibly-enter-article
10979                 gnus-newsgroup-name article
10980                 (= mark gnus-ticked-mark)
10981                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10982
10983         (when (gnus-summary-goto-subject article nil t)
10984           (let ((inhibit-read-only t))
10985             (gnus-summary-show-thread)
10986             ;; Fix the mark.
10987             (gnus-summary-update-mark mark 'unread)
10988             t))))))
10989
10990 (defun gnus-summary-update-secondary-mark (article)
10991   "Update the secondary (read, process, cache) mark."
10992   (gnus-summary-update-mark
10993    (cond ((memq article gnus-newsgroup-processable)
10994           gnus-process-mark)
10995          ((memq article gnus-newsgroup-cached)
10996           gnus-cached-mark)
10997          ((memq article gnus-newsgroup-replied)
10998           gnus-replied-mark)
10999          ((memq article gnus-newsgroup-forwarded)
11000           gnus-forwarded-mark)
11001          ((memq article gnus-newsgroup-saved)
11002           gnus-saved-mark)
11003          ((memq article gnus-newsgroup-unseen)
11004           gnus-unseen-mark)
11005          (t gnus-no-mark))
11006    'replied)
11007   (when (gnus-visual-p 'summary-highlight 'highlight)
11008     (gnus-summary-highlight-line)
11009     (gnus-run-hooks 'gnus-summary-update-hook))
11010   t)
11011
11012 (defun gnus-summary-update-download-mark (article)
11013   "Update the download mark."
11014   (gnus-summary-update-mark
11015    (cond ((memq article gnus-newsgroup-undownloaded)
11016           gnus-undownloaded-mark)
11017          (gnus-newsgroup-agentized
11018           gnus-downloaded-mark)
11019          (t
11020           gnus-no-mark))
11021    'download)
11022   (gnus-summary-update-line t)
11023   t)
11024
11025 (defun gnus-summary-update-mark (mark type)
11026   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11027         (inhibit-read-only t))
11028     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11029     (when forward
11030       (when (looking-at "\r")
11031         (incf forward))
11032       (when (<= (+ forward (point)) (point-max))
11033         ;; Go to the right position on the line.
11034         (goto-char (+ forward (point)))
11035         ;; Replace the old mark with the new mark.
11036         (let ((to-insert
11037                (mm-subst-char-in-string
11038                 (char-after) mark
11039                 (buffer-substring (point) (1+ (point))))))
11040           (delete-region (point) (1+ (point)))
11041           (insert to-insert))
11042         ;; Optionally update the marks by some user rule.
11043         (when (eq type 'unread)
11044           (gnus-data-set-mark
11045            (gnus-data-find (gnus-summary-article-number)) mark)
11046           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11047
11048 (defun gnus-mark-article-as-read (article &optional mark)
11049   "Enter ARTICLE in the pertinent lists and remove it from others."
11050   ;; Make the article expirable.
11051   (let ((mark (or mark gnus-del-mark)))
11052     (setq gnus-newsgroup-expirable
11053           (if (= mark gnus-expirable-mark)
11054               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11055             (delq article gnus-newsgroup-expirable)))
11056     ;; Remove from unread and marked lists.
11057     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11058     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11059     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11060     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11061     (push (cons article mark) gnus-newsgroup-reads)
11062     ;; Possibly remove from cache, if that is used.
11063     (when gnus-use-cache
11064       (gnus-cache-enter-remove-article article))
11065     t))
11066
11067 (defun gnus-mark-article-as-unread (article &optional mark)
11068   "Enter ARTICLE in the pertinent lists and remove it from others."
11069   (let ((mark (or mark gnus-ticked-mark)))
11070     (if (<= article 0)
11071         (progn
11072           (gnus-error 1 "Can't mark negative article numbers")
11073           nil)
11074       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11075             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11076             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11077             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11078             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11079
11080       ;; Unsuppress duplicates?
11081       (when gnus-suppress-duplicates
11082         (gnus-dup-unsuppress-article article))
11083
11084       (cond ((= mark gnus-ticked-mark)
11085              (setq gnus-newsgroup-marked
11086                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11087             ((= mark gnus-spam-mark)
11088              (setq gnus-newsgroup-spam-marked
11089                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11090                                             article)))
11091             ((= mark gnus-dormant-mark)
11092              (setq gnus-newsgroup-dormant
11093                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11094             (t
11095              (setq gnus-newsgroup-unreads
11096                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11097       (gnus-alist-pull article gnus-newsgroup-reads)
11098       t)))
11099
11100 (defun gnus-summary-tick-article-forward (n)
11101   "Tick N articles forwards.
11102 If N is negative, tick backwards instead.
11103 The difference between N and the number of articles ticked is returned."
11104   (interactive "p")
11105   (gnus-summary-mark-forward n gnus-ticked-mark))
11106
11107 (defun gnus-summary-tick-article-backward (n)
11108   "Tick N articles backwards.
11109 The difference between N and the number of articles ticked is returned."
11110   (interactive "p")
11111   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11112
11113 (defun gnus-summary-tick-article (&optional article clear-mark)
11114   "Mark current article as unread.
11115 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11116 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11117   (interactive)
11118   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11119                                        gnus-ticked-mark)))
11120
11121 (defun gnus-summary-mark-as-read-forward (n)
11122   "Mark N articles as read forwards.
11123 If N is negative, mark backwards instead.
11124 The difference between N and the actual number of articles marked is
11125 returned."
11126   (interactive "p")
11127   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11128
11129 (defun gnus-summary-mark-as-read-backward (n)
11130   "Mark the N articles as read backwards.
11131 The difference between N and the actual number of articles marked is
11132 returned."
11133   (interactive "p")
11134   (gnus-summary-mark-forward
11135    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11136
11137 (defun gnus-summary-mark-as-read (&optional article mark)
11138   "Mark current article as read.
11139 ARTICLE specifies the article to be marked as read.
11140 MARK specifies a string to be inserted at the beginning of the line."
11141   (gnus-summary-mark-article article mark))
11142
11143 (defun gnus-summary-clear-mark-forward (n)
11144   "Clear marks from N articles forward.
11145 If N is negative, clear backward instead.
11146 The difference between N and the number of marks cleared is returned."
11147   (interactive "p")
11148   (gnus-summary-mark-forward n gnus-unread-mark))
11149
11150 (defun gnus-summary-clear-mark-backward (n)
11151   "Clear marks from N articles backward.
11152 The difference between N and the number of marks cleared is returned."
11153   (interactive "p")
11154   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11155
11156 (defun gnus-summary-mark-unread-as-read ()
11157   "Intended to be used by `gnus-mark-article-hook'."
11158   (when (memq gnus-current-article gnus-newsgroup-unreads)
11159     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11160
11161 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11162   "Intended to be used by `gnus-mark-article-hook'."
11163   (let ((mark (gnus-summary-article-mark)))
11164     (when (or (gnus-unread-mark-p mark)
11165               (gnus-read-mark-p mark))
11166       (gnus-summary-mark-article gnus-current-article
11167                                  (or new-mark gnus-read-mark)))))
11168
11169 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11170   "Intended to be used by `gnus-mark-article-hook'."
11171   (let ((mark (gnus-summary-article-mark)))
11172     (when (or (gnus-unread-mark-p mark)
11173               (gnus-read-mark-p mark))
11174       (gnus-summary-mark-article (gnus-summary-article-number)
11175                                  (or new-mark gnus-read-mark)))))
11176
11177 (defun gnus-summary-mark-unread-as-ticked ()
11178   "Intended to be used by `gnus-mark-article-hook'."
11179   (when (memq gnus-current-article gnus-newsgroup-unreads)
11180     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11181
11182 (defun gnus-summary-mark-region-as-read (point mark all)
11183   "Mark all unread articles between point and mark as read.
11184 If given a prefix, mark all articles between point and mark as read,
11185 even ticked and dormant ones."
11186   (interactive "r\nP")
11187   (save-excursion
11188     (let (article)
11189       (goto-char point)
11190       (beginning-of-line)
11191       (while (and
11192               (< (point) mark)
11193               (progn
11194                 (when (or all
11195                           (memq (setq article (gnus-summary-article-number))
11196                                 gnus-newsgroup-unreads))
11197                   (gnus-summary-mark-article article gnus-del-mark))
11198                 t)
11199               (gnus-summary-find-next))))))
11200
11201 (defun gnus-summary-mark-below (score mark)
11202   "Mark articles with score less than SCORE with MARK."
11203   (interactive "P\ncMark: ")
11204   (setq score (if score
11205                   (prefix-numeric-value score)
11206                 (or gnus-summary-default-score 0)))
11207   (with-current-buffer gnus-summary-buffer
11208     (goto-char (point-min))
11209     (while
11210         (progn
11211           (and (< (gnus-summary-article-score) score)
11212                (gnus-summary-mark-article nil mark))
11213           (gnus-summary-find-next)))))
11214
11215 (defun gnus-summary-kill-below (&optional score)
11216   "Mark articles with score below SCORE as read."
11217   (interactive "P")
11218   (gnus-summary-mark-below score gnus-killed-mark))
11219
11220 (defun gnus-summary-clear-above (&optional score)
11221   "Clear all marks from articles with score above SCORE."
11222   (interactive "P")
11223   (gnus-summary-mark-above score gnus-unread-mark))
11224
11225 (defun gnus-summary-tick-above (&optional score)
11226   "Tick all articles with score above SCORE."
11227   (interactive "P")
11228   (gnus-summary-mark-above score gnus-ticked-mark))
11229
11230 (defun gnus-summary-mark-above (score mark)
11231   "Mark articles with score over SCORE with MARK."
11232   (interactive "P\ncMark: ")
11233   (setq score (if score
11234                   (prefix-numeric-value score)
11235                 (or gnus-summary-default-score 0)))
11236   (with-current-buffer gnus-summary-buffer
11237     (goto-char (point-min))
11238     (while (and (progn
11239                   (when (> (gnus-summary-article-score) score)
11240                     (gnus-summary-mark-article nil mark))
11241                   t)
11242                 (gnus-summary-find-next)))))
11243
11244 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11245 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11246 (defun gnus-summary-limit-include-expunged (&optional no-error)
11247   "Display all the hidden articles that were expunged for low scores."
11248   (interactive)
11249   (let ((inhibit-read-only t))
11250     (let ((scored gnus-newsgroup-scored)
11251           headers h)
11252       (while scored
11253         (unless (gnus-summary-article-header (caar scored))
11254           (and (setq h (gnus-number-to-header (caar scored)))
11255                (< (cdar scored) gnus-summary-expunge-below)
11256                (push h headers)))
11257         (setq scored (cdr scored)))
11258       (if (not headers)
11259           (when (not no-error)
11260             (error "No expunged articles hidden"))
11261         (goto-char (point-min))
11262         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11263         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11264         (dolist (x headers)
11265           (push (mail-header-number x) gnus-newsgroup-limit))
11266         (gnus-summary-prepare-unthreaded (nreverse headers))
11267         (goto-char (point-min))
11268         (gnus-summary-position-point)
11269         t))))
11270
11271 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11272   "Mark all unread articles in this newsgroup as read.
11273 If prefix argument ALL is non-nil, ticked and dormant articles will
11274 also be marked as read.
11275 If QUIETLY is non-nil, no questions will be asked.
11276
11277 If TO-HERE is non-nil, it should be a point in the buffer.  All
11278 articles before (after, if REVERSE is set) this point will be marked
11279 as read.
11280
11281 Note that this function will only catch up the unread article
11282 in the current summary buffer limitation.
11283
11284 The number of articles marked as read is returned."
11285   (interactive "P")
11286   (prog1
11287       (save-excursion
11288         (when (or quietly
11289                   (not gnus-interactive-catchup) ;Without confirmation?
11290                   gnus-expert-user
11291                   (gnus-y-or-n-p
11292                    (if all
11293                        "Mark absolutely all articles as read? "
11294                      "Mark all unread articles as read? ")))
11295           (if (and not-mark
11296                    (not gnus-newsgroup-adaptive)
11297                    (not gnus-newsgroup-auto-expire)
11298                    (not gnus-suppress-duplicates)
11299                    (or (not gnus-use-cache)
11300                        (eq gnus-use-cache 'passive)))
11301               (progn
11302                 (when all
11303                   (setq gnus-newsgroup-marked nil
11304                         gnus-newsgroup-spam-marked nil
11305                         gnus-newsgroup-dormant nil))
11306                 (setq gnus-newsgroup-unreads
11307                       (gnus-sorted-nunion
11308                        (gnus-sorted-intersection gnus-newsgroup-unreads
11309                                                  gnus-newsgroup-downloadable)
11310                        (gnus-sorted-difference gnus-newsgroup-unfetched
11311                                                gnus-newsgroup-cached))))
11312             ;; We actually mark all articles as canceled, which we
11313             ;; have to do when using auto-expiry or adaptive scoring.
11314             (gnus-summary-show-all-threads)
11315             (if (and to-here reverse)
11316                 (progn
11317                   (goto-char to-here)
11318                   (gnus-summary-mark-current-read-and-unread-as-read
11319                    gnus-catchup-mark)
11320                   (while (gnus-summary-find-next (not all))
11321                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11322               (when (gnus-summary-first-subject (not all))
11323                 (while (and
11324                         (if to-here (< (point) to-here) t)
11325                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11326                         (gnus-summary-find-next (not all))))))
11327             (gnus-set-mode-line 'summary))
11328           t))
11329     (gnus-summary-position-point)))
11330
11331 (defun gnus-summary-catchup-to-here (&optional all)
11332   "Mark all unticked articles before the current one as read.
11333 If ALL is non-nil, also mark ticked and dormant articles as read."
11334   (interactive "P")
11335   (save-excursion
11336     (gnus-save-hidden-threads
11337       (let ((beg (point)))
11338         ;; We check that there are unread articles.
11339         (when (or all (gnus-summary-find-prev))
11340           (gnus-summary-catchup all t beg)))))
11341   (gnus-summary-position-point))
11342
11343 (defun gnus-summary-catchup-from-here (&optional all)
11344   "Mark all unticked articles after (and including) the current one as read.
11345 If ALL is non-nil, also mark ticked and dormant articles as read."
11346   (interactive "P")
11347   (save-excursion
11348     (gnus-save-hidden-threads
11349       (let ((beg (point)))
11350         ;; We check that there are unread articles.
11351         (when (or all (gnus-summary-find-next))
11352           (gnus-summary-catchup all t beg nil t)))))
11353   (gnus-summary-position-point))
11354
11355 (defun gnus-summary-catchup-all (&optional quietly)
11356   "Mark all articles in this newsgroup as read.
11357 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11358 instead, which marks only unread articles as read."
11359   (interactive "P")
11360   (gnus-summary-catchup t quietly))
11361
11362 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11363   "Mark all unread articles in this group as read, then exit.
11364 If prefix argument ALL is non-nil, all articles are marked as read.
11365 If QUIETLY is non-nil, no questions will be asked."
11366   (interactive "P")
11367   (when (gnus-summary-catchup all quietly nil 'fast)
11368     ;; Select next newsgroup or exit.
11369     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11370              (eq gnus-auto-select-next 'quietly))
11371         (gnus-summary-next-group nil)
11372       (gnus-summary-exit))))
11373
11374 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11375   "Mark all articles in this newsgroup as read, and then exit.
11376 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11377 instead, which marks only unread articles as read."
11378   (interactive "P")
11379   (gnus-summary-catchup-and-exit t quietly))
11380
11381 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11382   "Mark all articles in this group as read and select the next group.
11383 If given a prefix, mark all articles, unread as well as ticked, as
11384 read."
11385   (interactive "P")
11386   (save-excursion
11387     (gnus-summary-catchup all))
11388   (gnus-summary-next-group))
11389
11390 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11391   "Mark all articles in this group as read and select the previous group.
11392 If given a prefix, mark all articles, unread as well as ticked, as
11393 read."
11394   (interactive "P")
11395   (save-excursion
11396     (gnus-summary-catchup all))
11397   (gnus-summary-next-group nil nil t))
11398
11399 ;;;
11400 ;;; with article
11401 ;;;
11402
11403 (defmacro gnus-with-article (article &rest forms)
11404   "Select ARTICLE and perform FORMS in the original article buffer.
11405 Then replace the article with the result."
11406   `(progn
11407      ;; We don't want the article to be marked as read.
11408      (let (gnus-mark-article-hook)
11409        (gnus-summary-select-article t t nil ,article))
11410      (set-buffer gnus-original-article-buffer)
11411      ,@forms
11412      (if (not (gnus-check-backend-function
11413                'request-replace-article (car gnus-article-current)))
11414          (gnus-message 5 "Read-only group; not replacing")
11415        (unless (gnus-request-replace-article
11416                 ,article (car gnus-article-current)
11417                 (current-buffer) t)
11418          (error "Couldn't replace article")))
11419      ;; The cache and backlog have to be flushed somewhat.
11420      (when gnus-keep-backlog
11421        (gnus-backlog-remove-article
11422         (car gnus-article-current) (cdr gnus-article-current)))
11423      (when gnus-use-cache
11424        (gnus-cache-update-article
11425         (car gnus-article-current) (cdr gnus-article-current)))))
11426
11427 (put 'gnus-with-article 'lisp-indent-function 1)
11428 (put 'gnus-with-article 'edebug-form-spec '(form body))
11429
11430 ;; Thread-based commands.
11431
11432 (defun gnus-summary-articles-in-thread (&optional article)
11433   "Return a list of all articles in the current thread.
11434 If ARTICLE is non-nil, return all articles in the thread that starts
11435 with that article."
11436   (let* ((article (or article (gnus-summary-article-number)))
11437          (data (gnus-data-find-list article))
11438          (top-level (gnus-data-level (car data)))
11439          (top-subject
11440           (cond ((null gnus-thread-operation-ignore-subject)
11441                  (gnus-simplify-subject-re
11442                   (mail-header-subject (gnus-data-header (car data)))))
11443                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11444                  (gnus-simplify-subject-fuzzy
11445                   (mail-header-subject (gnus-data-header (car data)))))
11446                 (t nil)))
11447          (end-point (save-excursion
11448                       (goto-char (gnus-data-pos (car data)))
11449                       (if (gnus-summary-go-to-next-thread)
11450                           (point) (point-max))))
11451          articles)
11452     (while (and data
11453                 (< (gnus-data-pos (car data)) end-point))
11454       (when (or (not top-subject)
11455                 (string= top-subject
11456                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11457                              (gnus-simplify-subject-fuzzy
11458                               (mail-header-subject
11459                                (gnus-data-header (car data))))
11460                            (gnus-simplify-subject-re
11461                             (mail-header-subject
11462                              (gnus-data-header (car data)))))))
11463         (push (gnus-data-number (car data)) articles))
11464       (unless (and (setq data (cdr data))
11465                    (> (gnus-data-level (car data)) top-level))
11466         (setq data nil)))
11467     ;; Return the list of articles.
11468     (nreverse articles)))
11469
11470 (defun gnus-summary-rethread-current ()
11471   "Rethread the thread the current article is part of."
11472   (interactive)
11473   (let* ((gnus-show-threads t)
11474          (article (gnus-summary-article-number))
11475          (id (mail-header-id (gnus-summary-article-header)))
11476          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11477     (unless id
11478       (error "No article on the current line"))
11479     (gnus-rebuild-thread id)
11480     (gnus-summary-goto-subject article)))
11481
11482 (defun gnus-summary-reparent-thread ()
11483   "Make the current article child of the marked (or previous) article.
11484
11485 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11486 is non-nil or the Subject: of both articles are the same."
11487   (interactive)
11488   (unless (not (gnus-group-read-only-p))
11489     (error "The current newsgroup does not support article editing"))
11490   (unless (<= (length gnus-newsgroup-processable) 1)
11491     (error "No more than one article may be marked"))
11492   (let ((child (gnus-summary-article-number))
11493         ;; First grab the marked article, otherwise one line up.
11494         (parent (if (not (null gnus-newsgroup-processable))
11495                     (car gnus-newsgroup-processable)
11496                   (save-excursion
11497                     (if (eq (forward-line -1) 0)
11498                         (gnus-summary-article-number)
11499                       (error "Beginning of summary buffer"))))))
11500     (gnus-summary-reparent-children parent (list child))))
11501
11502 (defun gnus-summary-reparent-children (parent children)
11503   "Make PARENT the parent of CHILDREN.
11504 When called interactively, PARENT is the current article and CHILDREN
11505 are the process-marked articles."
11506   (interactive
11507    (list (gnus-summary-article-number)
11508          (gnus-summary-work-articles nil)))
11509   (dolist (child children)
11510     (save-window-excursion
11511       (let ((gnus-article-buffer " *reparent*"))
11512         (unless (not (eq parent child))
11513           (error "An article may not be self-referential"))
11514         (let ((message-id (mail-header-id
11515                            (gnus-summary-article-header parent))))
11516           (unless (and message-id (not (equal message-id "")))
11517             (error "No message-id in desired parent"))
11518           (gnus-with-article child
11519             (save-restriction
11520               (goto-char (point-min))
11521               (message-narrow-to-head)
11522               (if (re-search-forward "^References: " nil t)
11523                   (progn
11524                     (re-search-forward "^[^ \t]" nil t)
11525                     (forward-line -1)
11526                     (end-of-line)
11527                     (insert " " message-id))
11528                 (insert "References: " message-id "\n"))))
11529           (set-buffer gnus-summary-buffer)
11530           (gnus-summary-unmark-all-processable)
11531           (gnus-summary-update-article child)
11532           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11533             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11534           (gnus-summary-rethread-current)
11535           (gnus-message 3 "Article %d is now the child of article %d"
11536                         child parent))))))
11537
11538 (defun gnus-summary-toggle-threads (&optional arg)
11539   "Toggle showing conversation threads.
11540 If ARG is positive number, turn showing conversation threads on."
11541   (interactive "P")
11542   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11543     (setq gnus-show-threads
11544           (if (null arg) (not gnus-show-threads)
11545             (> (prefix-numeric-value arg) 0)))
11546     (gnus-summary-prepare)
11547     (gnus-summary-goto-subject current)
11548     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11549     (gnus-summary-position-point)))
11550
11551 (eval-and-compile
11552   (if (fboundp 'remove-overlays)
11553       (defalias 'gnus-remove-overlays 'remove-overlays)
11554     (defun gnus-remove-overlays (beg end name val)
11555       "Clear BEG and END of overlays whose property NAME has value VAL.
11556 For compatibility with XEmacs."
11557       (dolist (ov (gnus-overlays-in beg end))
11558         (when (eq (gnus-overlay-get ov name) val)
11559           (gnus-delete-overlay ov))))))
11560
11561 (defun gnus-summary-show-all-threads ()
11562   "Show all threads."
11563   (interactive)
11564   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11565   (gnus-summary-position-point))
11566
11567 (defsubst gnus-summary--inv (p)
11568   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11569
11570 (defun gnus-summary-show-thread ()
11571   "Show thread subtrees.
11572 Returns nil if no thread was there to be shown."
11573   (interactive)
11574   (let* ((orig (point))
11575          (end (point-at-eol))
11576          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11577          ;; Leave point at bol
11578          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11579          (eoi (when end
11580                 (if (fboundp 'next-single-char-property-change)
11581                     (or (next-single-char-property-change end 'invisible)
11582                         (point-max))
11583                   (while (progn
11584                            (end-of-line 2)
11585                            (and (not (eobp))
11586                                 (eq (get-char-property (point) 'invisible)
11587                                     'gnus-sum))))
11588                   (point)))))
11589     (when eoi
11590       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11591       (goto-char orig)
11592       (gnus-summary-position-point)
11593       eoi)))
11594
11595 (defun gnus-summary-maybe-hide-threads ()
11596   "If requested, hide the threads that should be hidden."
11597   (when (and gnus-show-threads
11598              gnus-thread-hide-subtree)
11599     (gnus-summary-hide-all-threads
11600      (if (or (consp gnus-thread-hide-subtree)
11601              (functionp gnus-thread-hide-subtree))
11602          (gnus-make-predicate gnus-thread-hide-subtree)
11603        nil))))
11604
11605 ;;; Hiding predicates.
11606
11607 (defun gnus-article-unread-p (header)
11608   (memq (mail-header-number header) gnus-newsgroup-unreads))
11609
11610 (defun gnus-article-unseen-p (header)
11611   (memq (mail-header-number header) gnus-newsgroup-unseen))
11612
11613 (defun gnus-map-articles (predicate articles)
11614   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11615   (apply 'gnus-or (mapcar predicate
11616                           (mapcar (lambda (number)
11617                                     (gnus-summary-article-header number))
11618                                   articles))))
11619
11620 (defun gnus-summary-hide-all-threads (&optional predicate)
11621   "Hide all thread subtrees.
11622 If PREDICATE is supplied, threads that satisfy this predicate
11623 will not be hidden."
11624   (interactive)
11625   (save-excursion
11626     (goto-char (point-min))
11627     (let ((end nil)
11628           (count 0))
11629       (while (not end)
11630         (incf count)
11631         (when (zerop (mod count 1000))
11632           (message "Hiding all threads... %d" count))
11633         (when (or (not predicate)
11634                   (gnus-map-articles
11635                    predicate (gnus-summary-article-children)))
11636             (gnus-summary-hide-thread))
11637         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11638   (gnus-summary-position-point))
11639
11640 (defun gnus-summary-hide-thread ()
11641   "Hide thread subtrees.
11642 If PREDICATE is supplied, threads that satisfy this predicate
11643 will not be hidden.
11644 Returns nil if no threads were there to be hidden."
11645   (interactive)
11646   (let ((start (point))
11647         (starteol (line-end-position))
11648         (article (gnus-summary-article-number)))
11649     (goto-char start)
11650     ;; Go forward until either the buffer ends or the subthread ends.
11651     (when (and (not (eobp))
11652                (or (zerop (gnus-summary-next-thread 1 t))
11653                    (goto-char (point-max))))
11654       (if (and (> (point) start)
11655                ;; FIXME: this should actually search for a non-invisible \n.
11656                (search-backward "\n" start t))
11657           (progn
11658             (when (> (point) starteol)
11659               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11660               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11661                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11662                 (gnus-overlay-put ol 'evaporate t)))
11663             (gnus-summary-goto-subject article)
11664             (when (> start (point))
11665               (message "Hiding the thread moved us backwards, aborting!")
11666               (goto-char (point-max))))
11667         (goto-char start)
11668         nil))))
11669
11670 (defun gnus-summary-go-to-next-thread (&optional previous)
11671   "Go to the same level (or less) next thread.
11672 If PREVIOUS is non-nil, go to previous thread instead.
11673 Return the article number moved to, or nil if moving was impossible."
11674   (let ((level (gnus-summary-thread-level))
11675         (way (if previous -1 1))
11676         (beg (point)))
11677     (forward-line way)
11678     (while (and (not (eobp))
11679                 (< level (gnus-summary-thread-level)))
11680       (forward-line way))
11681     (if (eobp)
11682         (progn
11683           (goto-char beg)
11684           nil)
11685       (setq beg (point))
11686       (prog1
11687           (gnus-summary-article-number)
11688         (goto-char beg)))))
11689
11690 (defun gnus-summary-next-thread (n &optional silent)
11691   "Go to the same level next N'th thread.
11692 If N is negative, search backward instead.
11693 Returns the difference between N and the number of skips actually
11694 done.
11695
11696 If SILENT, don't output messages."
11697   (interactive "p")
11698   (let ((backward (< n 0))
11699         (n (abs n)))
11700     (while (and (> n 0)
11701                 (gnus-summary-go-to-next-thread backward))
11702       (decf n))
11703     (unless silent
11704       (gnus-summary-position-point))
11705     (when (and (not silent) (/= 0 n))
11706       (gnus-message 7 "No more threads"))
11707     n))
11708
11709 (defun gnus-summary-prev-thread (n)
11710   "Go to the same level previous N'th thread.
11711 Returns the difference between N and the number of skips actually
11712 done."
11713   (interactive "p")
11714   (gnus-summary-next-thread (- n)))
11715
11716 (defun gnus-summary-go-down-thread ()
11717   "Go down one level in the current thread."
11718   (let ((children (gnus-summary-article-children)))
11719     (when children
11720       (gnus-summary-goto-subject (car children)))))
11721
11722 (defun gnus-summary-go-up-thread ()
11723   "Go up one level in the current thread."
11724   (let ((parent (gnus-summary-article-parent)))
11725     (when parent
11726       (gnus-summary-goto-subject parent))))
11727
11728 (defun gnus-summary-down-thread (n)
11729   "Go down thread N steps.
11730 If N is negative, go up instead.
11731 Returns the difference between N and how many steps down that were
11732 taken."
11733   (interactive "p")
11734   (let ((up (< n 0))
11735         (n (abs n)))
11736     (while (and (> n 0)
11737                 (if up (gnus-summary-go-up-thread)
11738                   (gnus-summary-go-down-thread)))
11739       (setq n (1- n)))
11740     (gnus-summary-position-point)
11741     (when (/= 0 n)
11742       (gnus-message 7 "Can't go further"))
11743     n))
11744
11745 (defun gnus-summary-up-thread (n)
11746   "Go up thread N steps.
11747 If N is negative, go down instead.
11748 Returns the difference between N and how many steps down that were
11749 taken."
11750   (interactive "p")
11751   (gnus-summary-down-thread (- n)))
11752
11753 (defun gnus-summary-top-thread ()
11754   "Go to the top of the thread."
11755   (interactive)
11756   (while (gnus-summary-go-up-thread))
11757   (gnus-summary-article-number))
11758
11759 (defun gnus-summary-expire-thread ()
11760   "Mark articles under current thread as expired."
11761   (interactive)
11762   (gnus-summary-kill-thread 0))
11763
11764 (defun gnus-summary-kill-thread (&optional unmark)
11765   "Mark articles under current thread as read.
11766 If the prefix argument is positive, remove any kinds of marks.
11767 If the prefix argument is zero, mark thread as expired.
11768 If the prefix argument is negative, tick articles instead."
11769   (interactive "P")
11770   (when unmark
11771     (setq unmark (prefix-numeric-value unmark)))
11772   (let ((articles (gnus-summary-articles-in-thread))
11773         (hide (or (null unmark) (= unmark 0))))
11774     (save-excursion
11775       ;; Expand the thread.
11776       (gnus-summary-show-thread)
11777       ;; Mark all the articles.
11778       (while articles
11779         (gnus-summary-goto-subject (car articles))
11780         (cond ((null unmark)
11781                (gnus-summary-mark-article-as-read gnus-killed-mark))
11782               ((> unmark 0)
11783                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11784               ((= unmark 0)
11785                (gnus-summary-mark-article nil gnus-expirable-mark))
11786               (t
11787                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11788         (setq articles (cdr articles))))
11789     ;; Hide killed subtrees when hide is true.
11790     (and hide
11791          gnus-thread-hide-killed
11792          (gnus-summary-hide-thread))
11793     ;; If hide is t, go to next unread subject.
11794     (when hide
11795       ;; Go to next unread subject.
11796       (gnus-summary-next-subject 1 t)))
11797   (gnus-set-mode-line 'summary))
11798
11799 ;; Summary sorting commands
11800
11801 (defun gnus-summary-sort-by-number (&optional reverse)
11802   "Sort the summary buffer by article number.
11803 Argument REVERSE means reverse order."
11804   (interactive "P")
11805   (gnus-summary-sort 'number reverse))
11806
11807 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11808   "Sort the summary buffer by most recent article number.
11809 Argument REVERSE means reverse order."
11810   (interactive "P")
11811   (gnus-summary-sort 'most-recent-number reverse))
11812
11813 (defun gnus-summary-sort-by-random (&optional reverse)
11814   "Randomize the order in the summary buffer.
11815 Argument REVERSE means to randomize in reverse order."
11816   (interactive "P")
11817   (gnus-summary-sort 'random reverse))
11818
11819 (defun gnus-summary-sort-by-author (&optional reverse)
11820   "Sort the summary buffer by author name alphabetically.
11821 If `case-fold-search' is non-nil, case of letters is ignored.
11822 Argument REVERSE means reverse order."
11823   (interactive "P")
11824   (gnus-summary-sort 'author reverse))
11825
11826 (defun gnus-summary-sort-by-recipient (&optional reverse)
11827   "Sort the summary buffer by recipient name alphabetically.
11828 If `case-fold-search' is non-nil, case of letters is ignored.
11829 Argument REVERSE means reverse order."
11830   (interactive "P")
11831   (gnus-summary-sort 'recipient reverse))
11832
11833 (defun gnus-summary-sort-by-subject (&optional reverse)
11834   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11835 If `case-fold-search' is non-nil, case of letters is ignored.
11836 Argument REVERSE means reverse order."
11837   (interactive "P")
11838   (gnus-summary-sort 'subject reverse))
11839
11840 (defun gnus-summary-sort-by-date (&optional reverse)
11841   "Sort the summary buffer by date.
11842 Argument REVERSE means reverse order."
11843   (interactive "P")
11844   (gnus-summary-sort 'date reverse))
11845
11846 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11847   "Sort the summary buffer by most recent date.
11848 Argument REVERSE means reverse order."
11849   (interactive "P")
11850   (gnus-summary-sort 'most-recent-date reverse))
11851
11852 (defun gnus-summary-sort-by-score (&optional reverse)
11853   "Sort the summary buffer by score.
11854 Argument REVERSE means reverse order."
11855   (interactive "P")
11856   (gnus-summary-sort 'score reverse))
11857
11858 (defun gnus-summary-sort-by-lines (&optional reverse)
11859   "Sort the summary buffer by the number of lines.
11860 Argument REVERSE means reverse order."
11861   (interactive "P")
11862   (gnus-summary-sort 'lines reverse))
11863
11864 (defun gnus-summary-sort-by-chars (&optional reverse)
11865   "Sort the summary buffer by article length.
11866 Argument REVERSE means reverse order."
11867   (interactive "P")
11868   (gnus-summary-sort 'chars reverse))
11869
11870 (defun gnus-summary-sort-by-original (&optional reverse)
11871   "Sort the summary buffer using the default sorting method.
11872 Argument REVERSE means reverse order."
11873   (interactive "P")
11874   (let* ((inhibit-read-only t)
11875          (gnus-summary-prepare-hook nil))
11876     ;; We do the sorting by regenerating the threads.
11877     (gnus-summary-prepare)
11878     ;; Hide subthreads if needed.
11879     (gnus-summary-maybe-hide-threads)))
11880
11881 (defun gnus-summary-sort (predicate reverse)
11882   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11883   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11884          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11885          (gnus-thread-sort-functions
11886           (if (not reverse)
11887               thread
11888             `(lambda (t1 t2)
11889                (,thread t2 t1))))
11890          (gnus-sort-gathered-threads-function
11891           gnus-thread-sort-functions)
11892          (gnus-article-sort-functions
11893           (if (not reverse)
11894               article
11895             `(lambda (t1 t2)
11896                (,article t2 t1))))
11897          (inhibit-read-only t)
11898          (gnus-summary-prepare-hook nil))
11899     ;; We do the sorting by regenerating the threads.
11900     (gnus-summary-prepare)
11901     ;; Hide subthreads if needed.
11902     (gnus-summary-maybe-hide-threads)))
11903
11904 ;; Summary saving commands.
11905
11906 (defun gnus-summary-save-article (&optional n not-saved)
11907   "Save the current article using the default saver function.
11908 If N is a positive number, save the N next articles.
11909 If N is a negative number, save the N previous articles.
11910 If N is nil and any articles have been marked with the process mark,
11911 save those articles instead.
11912 The variable `gnus-default-article-saver' specifies the saver function.
11913
11914 If the optional second argument NOT-SAVED is non-nil, articles saved
11915 will not be marked as saved."
11916   (interactive "P")
11917   (require 'gnus-art)
11918   (let* ((articles (gnus-summary-work-articles n))
11919          (save-buffer (save-excursion
11920                         (nnheader-set-temp-buffer " *Gnus Save*")))
11921          (num (length articles))
11922          ;; Whether to save decoded articles or raw articles.
11923          (decode (when gnus-article-save-coding-system
11924                    (get gnus-default-article-saver :decode)))
11925          ;; When saving many articles in a single file, use the other
11926          ;; function to save articles other than the first one.
11927          (saver2 (get gnus-default-article-saver :function))
11928          (gnus-prompt-before-saving (if saver2
11929                                         t
11930                                       gnus-prompt-before-saving))
11931          (gnus-default-article-saver gnus-default-article-saver)
11932          header file)
11933     (dolist (article articles)
11934       (setq header (gnus-summary-article-header article))
11935       (if (not (vectorp header))
11936           ;; This is a pseudo-article.
11937           (if (assq 'name header)
11938               (gnus-copy-file (cdr (assq 'name header)))
11939             (gnus-message 1 "Article %d is unsavable" article))
11940         ;; This is a real article.
11941         (save-window-excursion
11942           (gnus-summary-select-article decode decode nil article)
11943           (gnus-summary-goto-subject article))
11944         (with-current-buffer save-buffer
11945           (erase-buffer)
11946           (insert-buffer-substring (if decode
11947                                        gnus-article-buffer
11948                                      gnus-original-article-buffer)))
11949         (setq file (gnus-article-save save-buffer file num))
11950         (gnus-summary-remove-process-mark article)
11951         (unless not-saved
11952           (gnus-summary-set-saved-mark article)))
11953       (when saver2
11954         (setq gnus-default-article-saver saver2
11955               saver2 nil)))
11956     (gnus-kill-buffer save-buffer)
11957     (gnus-summary-position-point)
11958     (gnus-set-mode-line 'summary)
11959     n))
11960
11961 (defun gnus-summary-pipe-output (&optional n sym)
11962   "Pipe the current article to a subprocess.
11963 If N is a positive number, pipe the N next articles.
11964 If N is a negative number, pipe the N previous articles.
11965 If N is nil and any articles have been marked with the process mark,
11966 pipe those articles instead.
11967 The default command to which articles are piped is specified by the
11968 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11969 will be prompted for the command.
11970
11971 The properties `:decode' and `:headers' that are put to the function
11972 symbol `gnus-summary-save-in-pipe' control whether this function
11973 decodes articles and what headers to keep (see the doc string for the
11974 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11975 is neither omitted nor the symbol `r', force including all headers
11976 regardless of the `:headers' property.  If it is the symbol `r',
11977 articles that are not decoded and include all headers will be piped
11978 no matter what the properties `:decode' and `:headers' are."
11979   (interactive (gnus-interactive "P\ny"))
11980   (require 'gnus-art)
11981   (let* ((articles (gnus-summary-work-articles n))
11982          (result-buffer "*Shell Command Output*")
11983          (all-headers (not (memq sym '(nil r))))
11984          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11985          (raw (eq sym 'r))
11986          (headers (get 'gnus-summary-save-in-pipe :headers))
11987          command result)
11988     (unless (numberp (car articles))
11989       (error "No article to pipe"))
11990     (setq command (gnus-read-shell-command
11991                    (concat "Shell command on "
11992                            (if (cdr articles)
11993                                (format "these %d articles" (length articles))
11994                              "this article")
11995                            ": ")
11996                    gnus-summary-pipe-output-default-command))
11997     (when (string-equal command "")
11998       (error "A command is required"))
11999     (when all-headers
12000       (put 'gnus-summary-save-in-pipe :headers nil))
12001     (unwind-protect
12002         (while articles
12003           (gnus-summary-goto-subject (pop articles))
12004           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12005           (when (and (get-buffer result-buffer)
12006                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12007             (setq result (concat result (with-current-buffer result-buffer
12008                                           (buffer-string))))))
12009       (put 'gnus-summary-save-in-pipe :headers headers))
12010     (unless (zerop (length result))
12011       (if (with-current-buffer (get-buffer-create result-buffer)
12012             (erase-buffer)
12013             (insert result)
12014             (prog1
12015                 (and (= (count-lines (point-min) (point)) 1)
12016                      (progn
12017                        (end-of-line 0)
12018                        (<= (current-column)
12019                            (window-width (minibuffer-window)))))
12020               (goto-char (point-min))))
12021           (message "%s" (substring result 0 -1))
12022         (message nil)
12023         (gnus-configure-windows 'pipe)))))
12024
12025 (defun gnus-summary-save-article-mail (&optional arg)
12026   "Append the current article to a Unix mail box file.
12027 If N is a positive number, save the N next articles.
12028 If N is a negative number, save the N previous articles.
12029 If N is nil and any articles have been marked with the process mark,
12030 save those articles instead."
12031   (interactive "P")
12032   (require 'gnus-art)
12033   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12034     (gnus-summary-save-article arg)))
12035
12036 (defun gnus-summary-save-article-rmail (&optional arg)
12037   "Append the current article to an rmail file.
12038 If N is a positive number, save the N next articles.
12039 If N is a negative number, save the N previous articles.
12040 If N is nil and any articles have been marked with the process mark,
12041 save those articles instead."
12042   (interactive "P")
12043   (require 'gnus-art)
12044   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12045     (gnus-summary-save-article arg)))
12046
12047 (defun gnus-summary-save-article-file (&optional arg)
12048   "Append the current article to a file.
12049 If N is a positive number, save the N next articles.
12050 If N is a negative number, save the N previous articles.
12051 If N is nil and any articles have been marked with the process mark,
12052 save those articles instead."
12053   (interactive "P")
12054   (require 'gnus-art)
12055   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12056     (gnus-summary-save-article arg)))
12057
12058 (defun gnus-summary-write-article-file (&optional arg)
12059   "Write the current article to a file, deleting the previous file.
12060 If N is a positive number, save the N next articles.
12061 If N is a negative number, save the N previous articles.
12062 If N is nil and any articles have been marked with the process mark,
12063 save those articles instead."
12064   (interactive "P")
12065   (require 'gnus-art)
12066   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12067     (gnus-summary-save-article arg)))
12068
12069 (defun gnus-summary-save-article-body-file (&optional arg)
12070   "Append the current article body to a file.
12071 If N is a positive number, save the N next articles.
12072 If N is a negative number, save the N previous articles.
12073 If N is nil and any articles have been marked with the process mark,
12074 save those articles instead."
12075   (interactive "P")
12076   (require 'gnus-art)
12077   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12078     (gnus-summary-save-article arg)))
12079
12080 (defun gnus-summary-write-article-body-file (&optional arg)
12081   "Write the current article body to a file, deleting the previous file.
12082 If N is a positive number, save the N next articles.
12083 If N is a negative number, save the N previous articles.
12084 If N is nil and any articles have been marked with the process mark,
12085 save those articles instead."
12086   (interactive "P")
12087   (require 'gnus-art)
12088   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12089     (gnus-summary-save-article arg)))
12090
12091 (defun gnus-summary-muttprint (&optional arg)
12092   "Print the current article using Muttprint.
12093 If N is a positive number, save the N next articles.
12094 If N is a negative number, save the N previous articles.
12095 If N is nil and any articles have been marked with the process mark,
12096 save those articles instead."
12097   (interactive "P")
12098   (require 'gnus-art)
12099   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12100     (gnus-summary-save-article arg t)))
12101
12102 (defun gnus-summary-pipe-message (program)
12103   "Pipe the current article through PROGRAM."
12104   (interactive "sProgram: ")
12105   (gnus-summary-select-article)
12106   (let ((mail-header-separator ""))
12107     (gnus-eval-in-buffer-window gnus-article-buffer
12108       (save-restriction
12109         (widen)
12110         (let ((start (window-start))
12111               (inhibit-read-only t))
12112           (message-pipe-buffer-body program)
12113           (set-window-start (get-buffer-window (current-buffer)) start))))))
12114
12115 (defun gnus-get-split-value (methods)
12116   "Return a value based on the split METHODS."
12117   (let (split-name method result match)
12118     (when methods
12119       (with-current-buffer gnus-original-article-buffer
12120         (save-restriction
12121           (nnheader-narrow-to-headers)
12122           (while (and methods (not split-name))
12123             (goto-char (point-min))
12124             (setq method (pop methods))
12125             (setq match (car method))
12126             (when (cond
12127                    ((stringp match)
12128                     ;; Regular expression.
12129                     (ignore-errors
12130                       (re-search-forward match nil t)))
12131                    ((functionp match)
12132                     ;; Function.
12133                     (save-restriction
12134                       (widen)
12135                       (setq result (funcall match gnus-newsgroup-name))))
12136                    ((consp match)
12137                     ;; Form.
12138                     (save-restriction
12139                       (widen)
12140                       (setq result (eval match)))))
12141               (setq split-name (cdr method))
12142               (cond ((stringp result)
12143                      (push (expand-file-name
12144                             result gnus-article-save-directory)
12145                            split-name))
12146                     ((consp result)
12147                      (setq split-name (append result split-name)))))))))
12148     (nreverse split-name)))
12149
12150 (defun gnus-valid-move-group-p (group)
12151   (and (symbolp group)
12152        (boundp group)
12153        (symbol-name group)
12154        (symbol-value group)
12155        (gnus-get-function (gnus-find-method-for-group
12156                            (symbol-name group)) 'request-accept-article t)))
12157
12158 (defun gnus-read-move-group-name (prompt default articles prefix)
12159   "Read a group name."
12160   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12161          (minibuffer-confirm-incomplete nil) ; XEmacs
12162          (prom
12163           (format "%s %s to"
12164                   prompt
12165                   (if (> (length articles) 1)
12166                       (format "these %d articles" (length articles))
12167                     "this article")))
12168          (to-newsgroup
12169           (cond
12170            ((null split-name)
12171             (gnus-group-completing-read
12172              prom
12173              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12174              nil prefix nil default))
12175            ((= 1 (length split-name))
12176             (gnus-group-completing-read
12177              prom
12178              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12179              nil prefix 'gnus-group-history (car split-name)))
12180            (t
12181             (gnus-completing-read
12182              prom (nreverse split-name) nil nil 'gnus-group-history))))
12183          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12184          encoded)
12185     (when to-newsgroup
12186       (if (or (string= to-newsgroup "")
12187               (string= to-newsgroup prefix))
12188           (setq to-newsgroup default))
12189       (unless to-newsgroup
12190         (error "No group name entered"))
12191       (setq encoded (mm-encode-coding-string
12192                      to-newsgroup
12193                      (gnus-group-name-charset to-method to-newsgroup)))
12194       (or (gnus-active encoded)
12195           (gnus-activate-group encoded nil nil to-method)
12196           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12197                                      to-newsgroup))
12198               (or (and (gnus-request-create-group encoded to-method)
12199                        (gnus-activate-group encoded nil nil to-method)
12200                        (gnus-subscribe-group encoded))
12201                   (error "Couldn't create group %s" to-newsgroup)))
12202           (error "No such group: %s" to-newsgroup))
12203       encoded)))
12204
12205 (defvar gnus-summary-save-parts-counter)
12206 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12207
12208 (defun gnus-summary-save-parts (type dir n &optional reverse)
12209   "Save parts matching TYPE to DIR.
12210 If REVERSE, save parts that do not match TYPE."
12211   (interactive
12212    (list (read-string "Save parts of type: "
12213                       (or (car gnus-summary-save-parts-type-history)
12214                           gnus-summary-save-parts-default-mime)
12215                       'gnus-summary-save-parts-type-history)
12216          (setq gnus-summary-save-parts-last-directory
12217                (read-directory-name "Save to directory: "
12218                                     gnus-summary-save-parts-last-directory
12219                                     nil t))
12220          current-prefix-arg))
12221   (gnus-summary-iterate n
12222     (let ((gnus-display-mime-function nil)
12223           gnus-article-prepare-hook
12224           gnus-article-decode-hook
12225           gnus-display-mime-function
12226           gnus-break-pages
12227           (gnus-inhibit-treatment t))
12228       (gnus-summary-select-article))
12229     (with-current-buffer gnus-article-buffer
12230       (let ((handles (or gnus-article-mime-handles
12231                          (mm-dissect-buffer nil gnus-article-loose-mime)
12232                          (and gnus-article-emulate-mime
12233                               (mm-uu-dissect))))
12234             (gnus-summary-save-parts-counter 1))
12235         (when handles
12236           (gnus-summary-save-parts-1 type dir handles reverse)
12237           (unless gnus-article-mime-handles ;; Don't destroy this case.
12238             (mm-destroy-parts handles)))))))
12239
12240 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12241   (if (stringp (car handle))
12242       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12243               (cdr handle))
12244     (when (if reverse
12245               (not (string-match type (mm-handle-media-type handle)))
12246             (string-match type (mm-handle-media-type handle)))
12247       (let ((file (expand-file-name
12248                    (gnus-map-function
12249                     mm-file-name-rewrite-functions
12250                     (file-name-nondirectory
12251                      (or
12252                       (mm-handle-filename handle)
12253                       (format "%s.%d.%d" gnus-newsgroup-name
12254                               (cdr gnus-article-current)
12255                               gnus-summary-save-parts-counter))))
12256                    dir)))
12257         (incf gnus-summary-save-parts-counter)
12258         (unless (file-exists-p file)
12259           (mm-save-part-to-file handle file))))))
12260
12261 ;; Summary extract commands
12262
12263 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12264   (let ((inhibit-read-only t)
12265         (article (gnus-summary-article-number))
12266         after-article b e)
12267     (unless (gnus-summary-goto-subject article)
12268       (error "No such article: %d" article))
12269     (gnus-summary-position-point)
12270     ;; If all commands are to be bunched up on one line, we collect
12271     ;; them here.
12272     (unless gnus-view-pseudos-separately
12273       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12274             files action)
12275         (while ps
12276           (setq action (cdr (assq 'action (car ps))))
12277           (setq files (list (cdr (assq 'name (car ps)))))
12278           (while (and ps (cdr ps)
12279                       (string= (or action "1")
12280                                (or (cdr (assq 'action (cadr ps))) "2")))
12281             (push (cdr (assq 'name (cadr ps))) files)
12282             (setcdr ps (cddr ps)))
12283           (when files
12284             (when (not (string-match "%s" action))
12285               (push " " files))
12286             (push " " files)
12287             (when (assq 'execute (car ps))
12288               (setcdr (assq 'execute (car ps))
12289                       (funcall (if (string-match "%s" action)
12290                                    'format 'concat)
12291                                action
12292                                (mapconcat
12293                                 (lambda (f)
12294                                   (if (equal f " ")
12295                                       f
12296                                     (shell-quote-argument f)))
12297                                 files " ")))))
12298           (setq ps (cdr ps)))))
12299     (if (and gnus-view-pseudos (not not-view))
12300         (while pslist
12301           (when (assq 'execute (car pslist))
12302             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12303                                   (eq gnus-view-pseudos 'not-confirm)))
12304           (setq pslist (cdr pslist)))
12305       (save-excursion
12306         (while pslist
12307           (setq after-article (or (cdr (assq 'article (car pslist)))
12308                                   (gnus-summary-article-number)))
12309           (gnus-summary-goto-subject after-article)
12310           (forward-line 1)
12311           (setq b (point))
12312           (insert "    " (file-name-nondirectory
12313                           (cdr (assq 'name (car pslist))))
12314                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12315           (setq e (point))
12316           (forward-line -1)             ; back to `b'
12317           (gnus-add-text-properties
12318            b (1- e) (list 'gnus-number gnus-reffed-article-number
12319                           gnus-mouse-face-prop gnus-mouse-face))
12320           (gnus-data-enter
12321            after-article gnus-reffed-article-number
12322            gnus-unread-mark b (car pslist) 0 (- e b))
12323           (setq gnus-newsgroup-unreads
12324                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12325                                          gnus-reffed-article-number))
12326           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12327           (setq pslist (cdr pslist)))))))
12328
12329 (defun gnus-pseudos< (p1 p2)
12330   (let ((c1 (cdr (assq 'action p1)))
12331         (c2 (cdr (assq 'action p2))))
12332     (and c1 c2 (string< c1 c2))))
12333
12334 (defun gnus-request-pseudo-article (props)
12335   (cond ((assq 'execute props)
12336          (gnus-execute-command (cdr (assq 'execute props)))))
12337   (let ((gnus-current-article (gnus-summary-article-number)))
12338     (gnus-run-hooks 'gnus-mark-article-hook)))
12339
12340 (defun gnus-execute-command (command &optional automatic)
12341   (save-excursion
12342     (gnus-article-setup-buffer)
12343     (set-buffer gnus-article-buffer)
12344     (setq buffer-read-only nil)
12345     (let ((command (if automatic command
12346                      (read-string "Command: " (cons command 0)))))
12347       (erase-buffer)
12348       (insert "$ " command "\n\n")
12349       (if gnus-view-pseudo-asynchronously
12350           (start-process "gnus-execute" (current-buffer) shell-file-name
12351                          shell-command-switch command)
12352         (call-process shell-file-name nil t nil
12353                       shell-command-switch command)))))
12354
12355 ;; Summary kill commands.
12356
12357 (defun gnus-summary-edit-global-kill (article)
12358   "Edit the \"global\" kill file."
12359   (interactive (list (gnus-summary-article-number)))
12360   (gnus-group-edit-global-kill article))
12361
12362 (defun gnus-summary-edit-local-kill ()
12363   "Edit a local kill file applied to the current newsgroup."
12364   (interactive)
12365   (setq gnus-current-headers (gnus-summary-article-header))
12366   (gnus-group-edit-local-kill
12367    (gnus-summary-article-number) gnus-newsgroup-name))
12368
12369 ;;; Header reading.
12370
12371 (defun gnus-read-header (id &optional header)
12372   "Read the headers of article ID and enter them into the Gnus system."
12373   (let ((group gnus-newsgroup-name)
12374         (gnus-override-method
12375          (or
12376           gnus-override-method
12377           (and (gnus-news-group-p gnus-newsgroup-name)
12378                (car (gnus-refer-article-methods)))))
12379         where)
12380     ;; First we check to see whether the header in question is already
12381     ;; fetched.
12382     (if (stringp id)
12383         ;; This is a Message-ID.
12384         (setq header (or header (gnus-id-to-header id)))
12385       ;; This is an article number.
12386       (setq header (or header (gnus-summary-article-header id))))
12387     (if (and header
12388              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12389         ;; We have found the header.
12390         header
12391       ;; We have to really fetch the header to this article.
12392       (with-current-buffer nntp-server-buffer
12393         (when (setq where (gnus-request-head id group))
12394           (nnheader-fold-continuation-lines)
12395           (goto-char (point-max))
12396           (insert ".\n")
12397           (goto-char (point-min))
12398           (insert "211 ")
12399           (princ (cond
12400                   ((numberp id) id)
12401                   ((cdr where) (cdr where))
12402                   (header (mail-header-number header))
12403                   (t gnus-reffed-article-number))
12404                  (current-buffer))
12405           (insert " Article retrieved.\n"))
12406         (if (or (not where)
12407                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12408             ()                          ; Malformed head.
12409           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12410             (when (and (stringp id)
12411                        (or
12412                         (not (string= (gnus-group-real-name group)
12413                                       (car where)))
12414                         (not (gnus-server-equal gnus-override-method
12415                                                 (gnus-group-method group)))))
12416               ;; If we fetched by Message-ID and the article came from
12417               ;; a different group (or server), we fudge some bogus
12418               ;; article numbers for this article.
12419               (mail-header-set-number header gnus-reffed-article-number))
12420             (with-current-buffer gnus-summary-buffer
12421               (decf gnus-reffed-article-number)
12422               (gnus-remove-header (mail-header-number header))
12423               (push header gnus-newsgroup-headers)
12424               (setq gnus-current-headers header)
12425               (push (mail-header-number header) gnus-newsgroup-limit)))
12426           header)))))
12427
12428 (defun gnus-remove-header (number)
12429   "Remove header NUMBER from `gnus-newsgroup-headers'."
12430   (if (and gnus-newsgroup-headers
12431            (= number (mail-header-number (car gnus-newsgroup-headers))))
12432       (pop gnus-newsgroup-headers)
12433     (let ((headers gnus-newsgroup-headers))
12434       (while (and (cdr headers)
12435                   (not (= number (mail-header-number (cadr headers)))))
12436         (pop headers))
12437       (when (cdr headers)
12438         (setcdr headers (cddr headers))))))
12439
12440 ;;;
12441 ;;; summary highlights
12442 ;;;
12443
12444 (defun gnus-highlight-selected-summary ()
12445   "Highlight selected article in summary buffer."
12446   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12447   (when gnus-summary-selected-face
12448     (save-excursion
12449       (let* ((beg (point-at-bol))
12450              (end (point-at-eol))
12451              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12452              (from (if (get-text-property beg gnus-mouse-face-prop)
12453                        beg
12454                      (or (next-single-property-change
12455                           beg gnus-mouse-face-prop nil end)
12456                          beg)))
12457              (to
12458               (if (= from end)
12459                   (- from 2)
12460                 (or (next-single-property-change
12461                      from gnus-mouse-face-prop nil end)
12462                     end))))
12463         ;; If no mouse-face prop on line we will have to = from = end,
12464         ;; so we highlight the entire line instead.
12465         (when (= (+ to 2) from)
12466           (setq from beg)
12467           (setq to end))
12468         (if gnus-newsgroup-selected-overlay
12469             ;; Move old overlay.
12470             (gnus-move-overlay
12471              gnus-newsgroup-selected-overlay from to (current-buffer))
12472           ;; Create new overlay.
12473           (gnus-overlay-put
12474            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12475            'face gnus-summary-selected-face))))))
12476
12477 (defvar gnus-summary-highlight-line-cached nil)
12478 (defvar gnus-summary-highlight-line-trigger nil)
12479
12480 (defun gnus-summary-highlight-line-0 ()
12481   (if (and (eq gnus-summary-highlight-line-trigger
12482                gnus-summary-highlight)
12483            gnus-summary-highlight-line-cached)
12484       gnus-summary-highlight-line-cached
12485     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12486           gnus-summary-highlight-line-cached
12487           (let* ((cond (list 'cond))
12488                  (c cond)
12489                  (list gnus-summary-highlight))
12490             (while list
12491               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12492                               nil))
12493               (setq c (cdr c)
12494                     list (cdr list)))
12495             (gnus-byte-compile (list 'lambda nil cond))))))
12496
12497 (defun gnus-summary-highlight-line ()
12498   "Highlight current line according to `gnus-summary-highlight'."
12499   (let* ((beg (point-at-bol))
12500          (article (or (gnus-summary-article-number) gnus-current-article))
12501          (score (or (cdr (assq article
12502                                gnus-newsgroup-scored))
12503                     gnus-summary-default-score 0))
12504          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12505          (inhibit-read-only t)
12506          (default gnus-summary-default-score)
12507          (default-high gnus-summary-default-high-score)
12508          (default-low gnus-summary-default-low-score)
12509          (uncached (and gnus-summary-use-undownloaded-faces
12510                         (memq article gnus-newsgroup-undownloaded)
12511                         (not (memq article gnus-newsgroup-cached)))))
12512     (let ((face (funcall (gnus-summary-highlight-line-0))))
12513       (unless (eq face (get-text-property beg 'face))
12514         (gnus-put-text-property-excluding-characters-with-faces
12515          beg (point-at-eol) 'face
12516          (setq face (if (boundp face) (symbol-value face) face)))
12517         (when gnus-summary-highlight-line-function
12518           (funcall gnus-summary-highlight-line-function article face))))))
12519
12520 (defun gnus-update-read-articles (group unread &optional compute)
12521   "Update the list of read articles in GROUP.
12522 UNREAD is a sorted list."
12523   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12524         (info (gnus-get-info group))
12525         (prev 1)
12526         read)
12527     (if (or (not info) (not active))
12528         ;; There is no info on this group if it was, in fact,
12529         ;; killed.  Gnus stores no information on killed groups, so
12530         ;; there's nothing to be done.
12531         ;; One could store the information somewhere temporarily,
12532         ;; perhaps...  Hmmm...
12533         ()
12534       ;; Remove any negative articles numbers.
12535       (while (and unread (< (car unread) 0))
12536         (setq unread (cdr unread)))
12537       ;; Remove any expired article numbers
12538       (while (and unread (< (car unread) (car active)))
12539         (setq unread (cdr unread)))
12540       ;; Compute the ranges of read articles by looking at the list of
12541       ;; unread articles.
12542       (while unread
12543         (when (/= (car unread) prev)
12544           (push (if (= prev (1- (car unread))) prev
12545                   (cons prev (1- (car unread))))
12546                 read))
12547         (setq prev (1+ (car unread)))
12548         (setq unread (cdr unread)))
12549       (when (<= prev (cdr active))
12550         (push (cons prev (cdr active)) read))
12551       (setq read (if (> (length read) 1) (nreverse read) read))
12552       (if compute
12553           read
12554         (save-excursion
12555           (let (setmarkundo)
12556             ;; Propagate the read marks to the backend.
12557             (when (and (or gnus-propagate-marks
12558                            (gnus-method-option-p
12559                             (gnus-find-method-for-group group)
12560                             'server-marks))
12561                        (gnus-check-backend-function 'request-set-mark group))
12562               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12563                     (add (gnus-remove-from-range read (gnus-info-read info))))
12564                 (when (or add del)
12565                   (unless (gnus-check-group group)
12566                     (error "Can't open server for %s" group))
12567                   (gnus-request-set-mark
12568                    group (delq nil (list (if add (list add 'add '(read)))
12569                                          (if del (list del 'del '(read))))))
12570                   (setq setmarkundo
12571                         `(gnus-request-set-mark
12572                           ,group
12573                           ',(delq nil (list
12574                                        (if del (list del 'add '(read)))
12575                                        (if add (list add 'del '(read))))))))))
12576             (set-buffer gnus-group-buffer)
12577             (gnus-undo-register
12578               `(progn
12579                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12580                  (gnus-info-set-read ',info ',(gnus-info-read info))
12581                  (gnus-get-unread-articles-in-group ',info
12582                                                     (gnus-active ,group))
12583                  (gnus-group-update-group ,group t)
12584                  ,setmarkundo))))
12585         ;; Enter this list into the group info.
12586         (gnus-info-set-read info read)
12587         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12588         (gnus-get-unread-articles-in-group info (gnus-active group))
12589         t))))
12590
12591 (defun gnus-offer-save-summaries ()
12592   "Offer to save all active summary buffers."
12593   (let (buffers)
12594     ;; Go through all buffers and find all summaries.
12595     (dolist (buffer (buffer-list))
12596       (when (and (setq buffer (buffer-name buffer))
12597                  (string-match "Summary" buffer)
12598                  (with-current-buffer buffer
12599                    ;; We check that this is, indeed, a summary buffer.
12600                    (and (eq major-mode 'gnus-summary-mode)
12601                         ;; Also make sure this isn't bogus.
12602                         gnus-newsgroup-prepared
12603                         ;; Also make sure that this isn't a
12604                         ;; dead summary buffer.
12605                         (not gnus-dead-summary-mode))))
12606         (push buffer buffers)))
12607     ;; Go through all these summary buffers and offer to save them.
12608     (when buffers
12609       (save-excursion
12610         (if (eq gnus-interactive-exit 'quiet)
12611             (dolist (buffer buffers)
12612               (switch-to-buffer buffer)
12613               (gnus-summary-exit))
12614           (map-y-or-n-p
12615            "Update summary buffer %s? "
12616            (lambda (buf)
12617              (switch-to-buffer buf)
12618              (gnus-summary-exit))
12619            buffers))))))
12620
12621 (defun gnus-summary-setup-default-charset ()
12622   "Setup newsgroup default charset."
12623   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12624       (setq gnus-newsgroup-charset nil)
12625     (let* ((ignored-charsets
12626             (or gnus-newsgroup-ephemeral-ignored-charsets
12627                 (append
12628                  (and gnus-newsgroup-name
12629                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12630                  gnus-newsgroup-ignored-charsets))))
12631       (setq gnus-newsgroup-charset
12632             (or gnus-newsgroup-ephemeral-charset
12633                 (and gnus-newsgroup-name
12634                      (gnus-parameter-charset gnus-newsgroup-name))
12635                 gnus-default-charset))
12636       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12637            ignored-charsets))))
12638
12639 ;;;
12640 ;;; Mime Commands
12641 ;;;
12642
12643 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12644   "Display the current article buffer fully MIME-buttonized.
12645 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12646 treated as multipart/mixed."
12647   (interactive "P")
12648   (require 'gnus-art)
12649   (let ((gnus-unbuttonized-mime-types nil)
12650         (gnus-mime-display-multipart-as-mixed show-all-parts))
12651     (gnus-summary-show-article)))
12652
12653 (defun gnus-summary-repair-multipart (article)
12654   "Add a Content-Type header to a multipart article without one."
12655   (interactive (list (gnus-summary-article-number)))
12656   (gnus-with-article article
12657     (message-narrow-to-head)
12658     (message-remove-header "Mime-Version")
12659     (goto-char (point-max))
12660     (insert "Mime-Version: 1.0\n")
12661     (widen)
12662     (when (search-forward "\n--" nil t)
12663       (let ((separator (buffer-substring (point) (point-at-eol))))
12664         (message-narrow-to-head)
12665         (message-remove-header "Content-Type")
12666         (goto-char (point-max))
12667         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12668                         separator))
12669         (widen))))
12670   (let (gnus-mark-article-hook)
12671     (gnus-summary-select-article t t nil article)))
12672
12673 (defun gnus-summary-toggle-display-buttonized ()
12674   "Toggle the buttonizing of the article buffer."
12675   (interactive)
12676   (require 'gnus-art)
12677   (if (setq gnus-inhibit-mime-unbuttonizing
12678             (not gnus-inhibit-mime-unbuttonizing))
12679       (let ((gnus-unbuttonized-mime-types nil))
12680         (gnus-summary-show-article))
12681     (gnus-summary-show-article)))
12682
12683 ;;;
12684 ;;; Generic summary marking commands
12685 ;;;
12686
12687 (defvar gnus-summary-marking-alist
12688   '((read gnus-del-mark "d")
12689     (unread gnus-unread-mark "u")
12690     (ticked gnus-ticked-mark "!")
12691     (dormant gnus-dormant-mark "?")
12692     (expirable gnus-expirable-mark "e"))
12693   "An alist of names/marks/keystrokes.")
12694
12695 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12696 (defvar gnus-summary-mark-map)
12697
12698 (defun gnus-summary-make-all-marking-commands ()
12699   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12700   (dolist (elem gnus-summary-marking-alist)
12701     (apply 'gnus-summary-make-marking-command elem)))
12702
12703 (defun gnus-summary-make-marking-command (name mark keystroke)
12704   (let ((map (make-sparse-keymap)))
12705     (define-key gnus-summary-generic-mark-map keystroke map)
12706     (dolist (lway `((next "next" next nil "n")
12707                     (next-unread "next unread" next t "N")
12708                     (prev "previous" prev nil "p")
12709                     (prev-unread "previous unread" prev t "P")
12710                     (nomove "" nil nil ,keystroke)))
12711       (let ((func (gnus-summary-make-marking-command-1
12712                    mark (car lway) lway name)))
12713         (setq func (eval func))
12714         (define-key map (nth 4 lway) func)))))
12715
12716 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12717   `(defun ,(intern
12718             (format "gnus-summary-put-mark-as-%s%s"
12719                     name (if (eq way 'nomove)
12720                              ""
12721                            (concat "-" (symbol-name way)))))
12722      (n)
12723      ,(format
12724        "Mark the current article as %s%s.
12725 If N, the prefix, then repeat N times.
12726 If N is negative, move in reverse order.
12727 The difference between N and the actual number of articles marked is
12728 returned."
12729        name (cadr lway))
12730      (interactive "p")
12731      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12732
12733 (defun gnus-summary-generic-mark (n mark move unread)
12734   "Mark N articles with MARK."
12735   (unless (eq major-mode 'gnus-summary-mode)
12736     (error "This command can only be used in the summary buffer"))
12737   (gnus-summary-show-thread)
12738   (let ((nummove
12739          (cond
12740           ((eq move 'next) 1)
12741           ((eq move 'prev) -1)
12742           (t 0))))
12743     (if (zerop nummove)
12744         (setq n 1)
12745       (when (< n 0)
12746         (setq n (abs n)
12747               nummove (* -1 nummove))))
12748     (while (and (> n 0)
12749                 (gnus-summary-mark-article nil mark)
12750                 (zerop (gnus-summary-next-subject nummove unread t)))
12751       (setq n (1- n)))
12752     (when (/= 0 n)
12753       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12754     (gnus-summary-recenter)
12755     (gnus-summary-position-point)
12756     (gnus-set-mode-line 'summary)
12757     n))
12758
12759 (defun gnus-summary-insert-articles (articles)
12760   (when (setq articles
12761               (gnus-sorted-difference articles
12762                                       (mapcar (lambda (h)
12763                                                 (mail-header-number h))
12764                                               gnus-newsgroup-headers)))
12765     (setq gnus-newsgroup-headers
12766           (gnus-merge 'list
12767                       gnus-newsgroup-headers
12768                       (gnus-fetch-headers articles)
12769                       'gnus-article-sort-by-number))
12770     (setq gnus-newsgroup-articles
12771           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12772     ;; Suppress duplicates?
12773     (when gnus-suppress-duplicates
12774       (gnus-dup-suppress-articles))
12775
12776     (if (and gnus-fetch-old-headers
12777              (eq gnus-headers-retrieved-by 'nov))
12778         ;; We might want to build some more threads first.
12779         (if (eq gnus-fetch-old-headers 'invisible)
12780             (gnus-build-all-threads)
12781           (gnus-build-old-threads))
12782       ;; Mark the inserted articles that are unread as unread.
12783       (setq gnus-newsgroup-unreads
12784             (gnus-sorted-nunion
12785              gnus-newsgroup-unreads
12786              (gnus-sorted-nintersection
12787               (gnus-list-of-unread-articles gnus-newsgroup-name)
12788               articles)))
12789       ;; Mark the inserted articles as selected so that the information
12790       ;; of the marks having been changed by a user may be updated when
12791       ;; exiting this group.  See `gnus-summary-update-info'.
12792       (dolist (art articles)
12793         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12794     ;; Let the Gnus agent mark articles as read.
12795     (when gnus-agent
12796       (gnus-agent-get-undownloaded-list))
12797     ;; Remove list identifiers from subject
12798     (gnus-summary-remove-list-identifiers)
12799     ;; First and last article in this newsgroup.
12800     (when gnus-newsgroup-headers
12801       (setq gnus-newsgroup-begin
12802             (mail-header-number (car gnus-newsgroup-headers))
12803             gnus-newsgroup-end
12804             (mail-header-number
12805              (gnus-last-element gnus-newsgroup-headers))))
12806     (when gnus-use-scoring
12807       (gnus-possibly-score-headers))))
12808
12809 (defun gnus-summary-insert-old-articles (&optional all)
12810   "Insert all old articles in this group.
12811 If ALL is non-nil, already read articles become readable.
12812 If ALL is a number, fetch this number of articles."
12813   (interactive "P")
12814   (prog1
12815       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12816             older len)
12817         (setq older
12818               ;; Some nntp servers lie about their active range.  When
12819               ;; this happens, the active range can be in the millions.
12820               ;; Use a compressed range to avoid creating a huge list.
12821               (gnus-range-difference (list gnus-newsgroup-active) old))
12822         (setq len (gnus-range-length older))
12823         (cond
12824          ((null older) nil)
12825          ((numberp all)
12826           (if (< all len)
12827               (let ((older-range (nreverse older)))
12828                 (setq older nil)
12829
12830                 (while (> all 0)
12831                   (let* ((r (pop older-range))
12832                          (min (if (numberp r) r (car r)))
12833                          (max (if (numberp r) r (cdr r))))
12834                     (while (and (<= min max)
12835                                 (> all 0))
12836                       (push max older)
12837                       (setq all (1- all)
12838                             max (1- max))))))
12839             (setq older (gnus-uncompress-range older))))
12840          (all
12841           (setq older (gnus-uncompress-range older)))
12842          (t
12843           (when (and (numberp gnus-large-newsgroup)
12844                    (> len gnus-large-newsgroup))
12845               (let* ((cursor-in-echo-area nil)
12846                      (initial (gnus-parameter-large-newsgroup-initial
12847                                gnus-newsgroup-name))
12848                      (input
12849                       (read-string
12850                        (format
12851                         "How many articles from %s (%s %d): "
12852                         (gnus-group-decoded-name gnus-newsgroup-name)
12853                         (if initial "max" "default")
12854                         len)
12855                        (if initial
12856                            (cons (number-to-string initial)
12857                                  0)))))
12858                 (unless (string-match "^[ \t]*$" input)
12859                   (setq all (string-to-number input))
12860                   (if (< all len)
12861                       (let ((older-range (nreverse older)))
12862                         (setq older nil)
12863
12864                         (while (> all 0)
12865                           (let* ((r (pop older-range))
12866                                  (min (if (numberp r) r (car r)))
12867                                  (max (if (numberp r) r (cdr r))))
12868                             (while (and (<= min max)
12869                                         (> all 0))
12870                               (push max older)
12871                               (setq all (1- all)
12872                                     max (1- max))))))))))
12873           (setq older (gnus-uncompress-range older))))
12874         (if (not older)
12875             (message "No old news.")
12876           (gnus-summary-insert-articles older)
12877           (gnus-summary-limit (gnus-sorted-nunion old older))))
12878     (gnus-summary-position-point)))
12879
12880 (defun gnus-summary-insert-new-articles ()
12881   "Insert all new articles in this group."
12882   (interactive)
12883   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12884         (old-high gnus-newsgroup-highest)
12885         (nnmail-fetched-sources (list t))
12886         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12887         i new)
12888     (unless new-active
12889       (error "Couldn't fetch new data"))
12890     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12891     (setq i (cdr gnus-newsgroup-active)
12892           gnus-newsgroup-highest i)
12893     (while (> i old-high)
12894       (push i new)
12895       (decf i))
12896     (if (not new)
12897         (message "No gnus is bad news")
12898       (gnus-summary-insert-articles new)
12899       (setq gnus-newsgroup-unreads
12900             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12901       (gnus-summary-limit (gnus-sorted-nunion old new))))
12902   (gnus-summary-position-point))
12903
12904 ;;; Bookmark support for Gnus.
12905 (declare-function bookmark-make-record-default
12906                   "bookmark" (&optional no-file no-context posn))
12907 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12908 (declare-function bookmark-default-handler "bookmark" (bmk))
12909 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12910 (defvar bookmark-yank-point)
12911 (defvar bookmark-current-buffer)
12912
12913 (defun gnus-summary-bookmark-make-record ()
12914   "Make a bookmark entry for a Gnus summary buffer."
12915   (let (pos buf)
12916     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12917       (save-restriction              ; FIXME is it necessary to widen?
12918         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12919       (setq buf "art") ; We are recording bookmark from article buffer.
12920       (setq bookmark-yank-point (point))
12921       (setq bookmark-current-buffer (current-buffer))
12922       (gnus-article-show-summary))      ; Go back in summary buffer.
12923     ;; We are now recording bookmark from summary buffer.
12924     (unless buf (setq buf "sum"))
12925     (let* ((subject (elt (gnus-summary-article-header) 1))
12926            (grp     (car gnus-article-current))
12927            (art     (cdr gnus-article-current))
12928            (head    (gnus-summary-article-header art))
12929            (id      (mail-header-id head)))
12930       `(,subject
12931         ,@(condition-case nil
12932               (bookmark-make-record-default 'no-file 'no-context pos)
12933             (wrong-number-of-arguments
12934              (bookmark-make-record-default 'point-only)))
12935         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12936         (group . ,grp) (article . ,art)
12937         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12938
12939 ;;;###autoload
12940 (defun gnus-summary-bookmark-jump (bookmark)
12941   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12942 BOOKMARK is a bookmark name or a bookmark record."
12943   (let ((group    (bookmark-prop-get bookmark 'group))
12944         (article  (bookmark-prop-get bookmark 'article))
12945         (id       (bookmark-prop-get bookmark 'message-id))
12946         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12947     (gnus-fetch-group group (list article))
12948     (gnus-summary-insert-cached-articles)
12949     (gnus-summary-goto-article id nil 'force)
12950     ;; FIXME we have to wait article buffer is ready (only large buffer)
12951     ;; Is there a better solution to know that?
12952     ;; If we don't wait `bookmark-default-handler' will have no chance
12953     ;; to set position. However there is no error, just wrong pos.
12954     (sit-for 1)
12955     (when (string= buf "Gnus-art")
12956       (other-window 1))
12957     (bookmark-default-handler
12958      `(""
12959        (buffer . ,(current-buffer))
12960        . ,(bookmark-get-bookmark-record bookmark)))))
12961
12962 (gnus-summary-make-all-marking-commands)
12963
12964 (gnus-ems-redefine)
12965
12966 (provide 'gnus-sum)
12967
12968 (run-hooks 'gnus-sum-load-hook)
12969
12970 ;; Local Variables:
12971 ;; coding: iso-8859-1
12972 ;; End:
12973
12974 ;;; gnus-sum.el ends here