Hyperlink urls in docstrings with URL `...'.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (eval-when-compile
34   (when (featurep 'xemacs)
35     (require 'easy-mmode))) ; for `define-minor-mode'
36
37 (defvar tool-bar-mode)
38 (defvar gnus-tmp-header)
39
40 (require 'gnus)
41 (require 'gnus-group)
42 (require 'gnus-spec)
43 (require 'gnus-range)
44 (require 'gnus-int)
45 (require 'gnus-undo)
46 (require 'gnus-util)
47 (require 'gmm-utils)
48 (require 'mm-decode)
49 (require 'nnoo)
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-cache-write-active "gnus-cache")
53 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
54 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
55 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
56 (autoload 'mm-uu-dissect "mm-uu")
57 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
58   "Deuglify broken Outlook (Express) articles and redisplay."
59   t)
60 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-summary-next-group-on-exit t
72   "If non-nil, go to the next unread newsgroup on summary exit.
73 See `gnus-group-goto-unread'."
74   :link '(custom-manual "(gnus)Group Maneuvering")
75   :group 'gnus-summary-exit
76   :version "23.1" ;; No Gnus
77   :type 'boolean)
78
79 (defcustom gnus-summary-stop-at-end-of-message nil
80   "If non-nil, don't select the next message when using `SPC'."
81   :link '(custom-manual "(gnus)Group Maneuvering")
82   :group 'gnus-summary-maneuvering
83   :type 'boolean)
84
85 (defcustom gnus-fetch-old-headers nil
86   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
87 If an unread article in the group refers to an older, already
88 read (or just marked as read) article, the old article will not
89 normally be displayed in the Summary buffer.  If this variable is
90 t, Gnus will attempt to grab the headers to the old articles, and
91 thereby build complete threads.  If it has the value `some', all
92 old headers will be fetched but only enough headers to connect
93 otherwise loose threads will be displayed.  This variable can
94 also be a number.  In that case, no more than that number of old
95 headers will be fetched.  If it has the value `invisible', all
96 old headers will be fetched, but none will be displayed.
97
98 The server has to support NOV for any of this to work.
99
100 This feature can seriously impact performance it ignores all
101 locally cached header entries.  Setting it to t for groups for a
102 server that doesn't expire articles (such as news.gmane.org),
103 leads to very slow summary generation."
104   :group 'gnus-thread
105   :type '(choice (const :tag "off" nil)
106                  (const :tag "on" t)
107                  (const some)
108                  (const invisible)
109                  number
110                  (sexp :menu-tag "other" t)))
111
112 (defcustom gnus-refer-thread-limit 500
113   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
114 If t, fetch all the available old headers."
115   :group 'gnus-thread
116   :type '(choice number
117                  (sexp :menu-tag "other" t)))
118
119 (defcustom gnus-summary-make-false-root 'adopt
120   "*nil means that Gnus won't gather loose threads.
121 If the root of a thread has expired or been read in a previous
122 session, the information necessary to build a complete thread has been
123 lost.  Instead of having many small sub-threads from this original thread
124 scattered all over the summary buffer, Gnus can gather them.
125
126 If non-nil, Gnus will try to gather all loose sub-threads from an
127 original thread into one large thread.
128
129 If this variable is non-nil, it should be one of `none', `adopt',
130 `dummy' or `empty'.
131
132 If this variable is `none', Gnus will not make a false root, but just
133 present the sub-threads after another.
134 If this variable is `dummy', Gnus will create a dummy root that will
135 have all the sub-threads as children.
136 If this variable is `adopt', Gnus will make one of the \"children\"
137 the parent and mark all the step-children as such.
138 If this variable is `empty', the \"children\" are printed with empty
139 subject fields.  (Or rather, they will be printed with a string
140 given by the `gnus-summary-same-subject' variable.)"
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const none)
144                  (const dummy)
145                  (const adopt)
146                  (const empty)))
147
148 (defcustom gnus-summary-make-false-root-always nil
149   "Always make a false dummy root."
150   :version "22.1"
151   :group 'gnus-thread
152   :type 'boolean)
153
154 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
155   "*A regexp to match subjects to be excluded from loose thread gathering.
156 As loose thread gathering is done on subjects only, that means that
157 there can be many false gatherings performed.  By rooting out certain
158 common subjects, gathering might become saner."
159   :group 'gnus-thread
160   :type 'regexp)
161
162 (defcustom gnus-summary-gather-subject-limit nil
163   "*Maximum length of subject comparisons when gathering loose threads.
164 Use nil to compare full subjects.  Setting this variable to a low
165 number will help gather threads that have been corrupted by
166 newsreaders chopping off subject lines, but it might also mean that
167 unrelated articles that have subject that happen to begin with the
168 same few characters will be incorrectly gathered.
169
170 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
171 comparing subjects."
172   :group 'gnus-thread
173   :type '(choice (const :tag "off" nil)
174                  (const fuzzy)
175                  (sexp :menu-tag "on" t)))
176
177 (defcustom gnus-simplify-subject-functions nil
178   "List of functions taking a string argument that simplify subjects.
179 The functions are applied recursively.
180
181 Useful functions to put in this list include:
182 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
183 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
184   :group 'gnus-thread
185   :type '(repeat function))
186
187 (defcustom gnus-simplify-ignored-prefixes nil
188   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
189   :group 'gnus-thread
190   :type '(choice (const :tag "off" nil)
191                  regexp))
192
193 (defcustom gnus-build-sparse-threads nil
194   "*If non-nil, fill in the gaps in threads.
195 If `some', only fill in the gaps that are needed to tie loose threads
196 together.  If `more', fill in all leaf nodes that Gnus can find.  If
197 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
198   :group 'gnus-thread
199   :type '(choice (const :tag "off" nil)
200                  (const some)
201                  (const more)
202                  (sexp :menu-tag "all" t)))
203
204 (defcustom gnus-summary-thread-gathering-function
205   'gnus-gather-threads-by-subject
206   "*Function used for gathering loose threads.
207 There are two pre-defined functions: `gnus-gather-threads-by-subject',
208 which only takes Subjects into consideration; and
209 `gnus-gather-threads-by-references', which compared the References
210 headers of the articles to find matches."
211   :group 'gnus-thread
212   :type '(radio (function-item gnus-gather-threads-by-subject)
213                 (function-item gnus-gather-threads-by-references)
214                 (function :tag "other")))
215
216 (defcustom gnus-summary-same-subject ""
217   "*String indicating that the current article has the same subject as the previous.
218 This variable will only be used if the value of
219 `gnus-summary-make-false-root' is `empty'."
220   :group 'gnus-summary-format
221   :type 'string)
222
223 (defcustom gnus-summary-goto-unread t
224   "*If t, many commands will go to the next unread article.
225 This applies to marking commands as well as other commands that
226 \"naturally\" select the next article, like, for instance, `SPC' at
227 the end of an article.
228
229 If nil, the marking commands do NOT go to the next unread article
230 \(they go to the next article instead).  If `never', commands that
231 usually go to the next unread article, will go to the next article,
232 whether it is read or not."
233   :group 'gnus-summary-marks
234   :link '(custom-manual "(gnus)Setting Marks")
235   :type '(choice (const :tag "off" nil)
236                  (const never)
237                  (sexp :menu-tag "on" t)))
238
239 (defcustom gnus-summary-default-score 0
240   "*Default article score level.
241 All scores generated by the score files will be added to this score.
242 If this variable is nil, scoring will be disabled."
243   :group 'gnus-score-default
244   :type '(choice (const :tag "disable")
245                  integer))
246
247 (defcustom gnus-summary-default-high-score 0
248   "*Default threshold for a high scored article.
249 An article will be highlighted as high scored if its score is greater
250 than this score."
251   :version "22.1"
252   :group 'gnus-score-default
253   :type 'integer)
254
255 (defcustom gnus-summary-default-low-score 0
256   "*Default threshold for a low scored article.
257 An article will be highlighted as low scored if its score is smaller
258 than this score."
259   :version "22.1"
260   :group 'gnus-score-default
261   :type 'integer)
262
263 (defcustom gnus-summary-zcore-fuzz 0
264   "*Fuzziness factor for the zcore in the summary buffer.
265 Articles with scores closer than this to `gnus-summary-default-score'
266 will not be marked."
267   :group 'gnus-summary-format
268   :type 'integer)
269
270 (defcustom gnus-simplify-subject-fuzzy-regexp nil
271   "*Strings to be removed when doing fuzzy matches.
272 This can either be a regular expression or list of regular expressions
273 that will be removed from subject strings if fuzzy subject
274 simplification is selected."
275   :group 'gnus-thread
276   :type '(repeat regexp))
277
278 (defcustom gnus-show-threads t
279   "*If non-nil, display threads in summary mode."
280   :group 'gnus-thread
281   :type 'boolean)
282
283 (defcustom gnus-thread-hide-subtree nil
284   "*If non-nil, hide all threads initially.
285 This can be a predicate specifier which says which threads to hide.
286 If threads are hidden, you have to run the command
287 `gnus-summary-show-thread' by hand or select an article."
288   :group 'gnus-thread
289   :type '(radio (sexp :format "Non-nil\n"
290                       :match (lambda (widget value)
291                                (not (or (consp value) (functionp value))))
292                       :value t)
293                 (const nil)
294                 (sexp :tag "Predicate specifier")))
295
296 (defcustom gnus-thread-hide-killed t
297   "*If non-nil, hide killed threads automatically."
298   :group 'gnus-thread
299   :type 'boolean)
300
301 (defcustom gnus-thread-ignore-subject t
302   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
303 If nil, articles that have different subjects from their parents will
304 start separate threads."
305   :group 'gnus-thread
306   :type 'boolean)
307
308 (defcustom gnus-thread-operation-ignore-subject t
309   "*If non-nil, subjects will be ignored when doing thread commands.
310 This affects commands like `gnus-summary-kill-thread' and
311 `gnus-summary-lower-thread'.
312
313 If this variable is nil, articles in the same thread with different
314 subjects will not be included in the operation in question.  If this
315 variable is `fuzzy', only articles that have subjects that are fuzzily
316 equal will be included."
317   :group 'gnus-thread
318   :type '(choice (const :tag "off" nil)
319                  (const fuzzy)
320                  (sexp :tag "on" t)))
321
322 (defcustom gnus-thread-indent-level 4
323   "*Number that says how much each sub-thread should be indented."
324   :group 'gnus-thread
325   :type 'integer)
326
327 (defcustom gnus-auto-extend-newsgroup t
328   "*If non-nil, extend newsgroup forward and backward when requested."
329   :group 'gnus-summary-choose
330   :type 'boolean)
331
332 (defcustom gnus-auto-select-first t
333   "If non-nil, select an article on group entry.
334 An article is selected automatically when entering a group
335 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
336 `gnus-summary-catchup-and-goto-next-group'.
337
338 Which article is selected is controlled by the variable
339 `gnus-auto-select-subject'.
340
341 If you want to prevent automatic selection of articles in some
342 newsgroups, set the variable to nil in `gnus-select-group-hook'."
343   ;; Commands include...
344   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
345   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
346   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
347   :group 'gnus-group-select
348   :type '(choice (const :tag "none" nil)
349                  (sexp :menu-tag "first" t)))
350
351 (defcustom gnus-auto-select-subject 'unread
352   "*Says what subject to place under point when entering a group.
353
354 This variable can either be the symbols `first' (place point on the
355 first subject), `unread' (place point on the subject line of the first
356 unread article), `best' (place point on the subject line of the
357 higest-scored article), `unseen' (place point on the subject line of
358 the first unseen article), `unseen-or-unread' (place point on the subject
359 line of the first unseen article or, if all article have been seen, on the
360 subject line of the first unread article), or a function to be called to
361 place point on some subject line."
362   :version "22.1"
363   :group 'gnus-group-select
364   :type '(choice (const best)
365                  (const unread)
366                  (const first)
367                  (const unseen)
368                  (const unseen-or-unread)))
369
370 (defcustom gnus-auto-select-next t
371   "*If non-nil, offer to go to the next group from the end of the previous.
372 If the value is t and the next newsgroup is empty, Gnus will exit
373 summary mode and go back to group mode.  If the value is neither nil
374 nor t, Gnus will select the following unread newsgroup.  In
375 particular, if the value is the symbol `quietly', the next unread
376 newsgroup will be selected without any confirmation, and if it is
377 `almost-quietly', the next group will be selected without any
378 confirmation if you are located on the last article in the group.
379 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
380 will go to the next group without confirmation."
381   :group 'gnus-summary-maneuvering
382   :type '(choice (const :tag "off" nil)
383                  (const quietly)
384                  (const almost-quietly)
385                  (const slightly-quietly)
386                  (sexp :menu-tag "on" t)))
387
388 (defcustom gnus-auto-select-same nil
389   "*If non-nil, select the next article with the same subject.
390 If there are no more articles with the same subject, go to
391 the first unread article."
392   :group 'gnus-summary-maneuvering
393   :type 'boolean)
394
395 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
396   "What article should be selected after exiting an ephemeral group.
397 Valid values include:
398
399 `next'
400   Select the next article.
401 `next-unread'
402   Select the next unread article.
403 `next-noselect'
404   Move the cursor to the next article.  This is the default.
405 `next-unread-noselect'
406   Move the cursor to the next unread article.
407
408 If it has any other value or there is no next (unread) article, the
409 article selected before entering to the ephemeral group will appear."
410   :version "23.1" ;; No Gnus
411   :group 'gnus-summary-maneuvering
412   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
413                  (const next) (const next-unread)
414                  (const next-noselect) (const next-unread-noselect)
415                  (sexp :tag "other" :value nil)))
416
417 (defcustom gnus-auto-goto-ignores 'unfetched
418   "*Says how to handle unfetched articles when maneuvering.
419
420 This variable can either be the symbols nil (maneuver to any
421 article), `undownloaded' (maneuvering while unplugged ignores articles
422 that have not been fetched), `always-undownloaded' (maneuvering always
423 ignores articles that have not been fetched), `unfetched' (maneuvering
424 ignores articles whose headers have not been fetched).
425
426 NOTE: The list of unfetched articles will always be nil when plugged
427 and, when unplugged, a subset of the undownloaded article list."
428   :version "22.1"
429   :group 'gnus-summary-maneuvering
430   :type '(choice (const :tag "None" nil)
431                  (const :tag "Undownloaded when unplugged" undownloaded)
432                  (const :tag "Undownloaded" always-undownloaded)
433                  (const :tag "Unfetched" unfetched)))
434
435 (defcustom gnus-summary-check-current nil
436   "*If non-nil, consider the current article when moving.
437 The \"unread\" movement commands will stay on the same line if the
438 current article is unread."
439   :group 'gnus-summary-maneuvering
440   :type 'boolean)
441
442 (defcustom gnus-auto-center-summary 2
443   "*If non-nil, always center the current summary buffer.
444 In particular, if `vertical' do only vertical recentering.  If non-nil
445 and non-`vertical', do both horizontal and vertical recentering."
446   :group 'gnus-summary-maneuvering
447   :type '(choice (const :tag "none" nil)
448                  (const vertical)
449                  (integer :tag "height")
450                  (sexp :menu-tag "both" t)))
451
452 (defvar gnus-auto-center-group t
453   "*If non-nil, always center the group buffer.")
454
455 (defcustom gnus-show-all-headers nil
456   "*If non-nil, don't hide any headers."
457   :group 'gnus-article-hiding
458   :group 'gnus-article-headers
459   :type 'boolean)
460
461 (defcustom gnus-summary-ignore-duplicates nil
462   "*If non-nil, ignore articles with identical Message-ID headers."
463   :group 'gnus-summary
464   :type 'boolean)
465
466 (defcustom gnus-single-article-buffer t
467   "*If non-nil, display all articles in the same buffer.
468 If nil, each group will get its own article buffer."
469   :group 'gnus-article-various
470   :type 'boolean)
471
472 (defcustom gnus-break-pages t
473   "*If non-nil, do page breaking on articles.
474 The page delimiter is specified by the `gnus-page-delimiter'
475 variable."
476   :group 'gnus-article-various
477   :type 'boolean)
478
479 (defcustom gnus-move-split-methods nil
480   "*Variable used to suggest where articles are to be moved to.
481 It uses the same syntax as the `gnus-split-methods' variable.
482 However, whereas `gnus-split-methods' specifies file names as targets,
483 this variable specifies group names."
484   :group 'gnus-summary-mail
485   :type '(repeat (choice (list :value (fun) function)
486                          (cons :value ("" "") regexp (repeat string))
487                          (sexp :value nil))))
488
489 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
490   "Function used to compute default prefix for article move/copy/etc prompts.
491 The function should take one argument, a group name, and return a
492 string with the suggested prefix."
493   :group 'gnus-summary-mail
494   :type 'function)
495
496 ;; FIXME: Although the custom type is `character' for the following variables,
497 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
498
499 (defcustom gnus-unread-mark ?           ;Whitespace
500   "*Mark used for unread articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-ticked-mark ?!
505   "*Mark used for ticked articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-dormant-mark ??
510   "*Mark used for dormant articles."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-del-mark ?r
515   "*Mark used for del'd articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-read-mark ?R
520   "*Mark used for read articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-expirable-mark ?E
525   "*Mark used for expirable articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-killed-mark ?K
530   "*Mark used for killed articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-spam-mark ?$
535   "*Mark used for spam articles."
536   :version "22.1"
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-souped-mark ?F
541   "*Mark used for souped articles."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-kill-file-mark ?X
546   "*Mark used for articles killed by kill files."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-low-score-mark ?Y
551   "*Mark used for articles with a low score."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-catchup-mark ?C
556   "*Mark used for articles that are caught up."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-replied-mark ?A
561   "*Mark used for articles that have been replied to."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-forwarded-mark ?F
566   "*Mark used for articles that have been forwarded."
567   :version "22.1"
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-recent-mark ?N
572   "*Mark used for articles that are recent."
573   :version "22.1"
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-cached-mark ?*
578   "*Mark used for articles that are in the cache."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-saved-mark ?S
583   "*Mark used for articles that have been saved."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-unseen-mark ?.
588   "*Mark used for articles that haven't been seen."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-no-mark ?               ;Whitespace
594   "*Mark used for articles that have no other secondary mark."
595   :version "22.1"
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-ancient-mark ?O
600   "*Mark used for ancient articles."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-sparse-mark ?Q
605   "*Mark used for sparsely reffed articles."
606   :group 'gnus-summary-marks
607   :type 'character)
608
609 (defcustom gnus-canceled-mark ?G
610   "*Mark used for canceled articles."
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-duplicate-mark ?M
615   "*Mark used for duplicate articles."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-undownloaded-mark ?-
620   "*Mark used for articles that weren't downloaded."
621   :version "22.1"
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-downloaded-mark ?+
626   "*Mark used for articles that were downloaded."
627   :group 'gnus-summary-marks
628   :type 'character)
629
630 (defcustom gnus-downloadable-mark ?%
631   "*Mark used for articles that are to be downloaded."
632   :group 'gnus-summary-marks
633   :type 'character)
634
635 (defcustom gnus-unsendable-mark ?=
636   "*Mark used for articles that won't be sent."
637   :group 'gnus-summary-marks
638   :type 'character)
639
640 (defcustom gnus-score-over-mark ?+
641   "*Score mark used for articles with high scores."
642   :group 'gnus-summary-marks
643   :type 'character)
644
645 (defcustom gnus-score-below-mark ?-
646   "*Score mark used for articles with low scores."
647   :group 'gnus-summary-marks
648   :type 'character)
649
650 (defcustom gnus-empty-thread-mark ?     ;Whitespace
651   "*There is no thread under the article."
652   :group 'gnus-summary-marks
653   :type 'character)
654
655 (defcustom gnus-not-empty-thread-mark ?=
656   "*There is a thread under the article."
657   :group 'gnus-summary-marks
658   :type 'character)
659
660 (defcustom gnus-view-pseudo-asynchronously nil
661   "*If non-nil, Gnus will view pseudo-articles asynchronously."
662   :group 'gnus-extract-view
663   :type 'boolean)
664
665 (defcustom gnus-auto-expirable-marks
666   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
667         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
668         gnus-souped-mark gnus-duplicate-mark)
669   "*The list of marks converted into expiration if a group is auto-expirable."
670   :version "21.1"
671   :group 'gnus-summary
672   :type '(repeat character))
673
674 (defcustom gnus-inhibit-user-auto-expire t
675   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
676   :version "21.1"
677   :group 'gnus-summary
678   :type 'boolean)
679
680 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
681   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
682 If nil, the expirable marks will be unchanged except that the marks
683 will be removed when copying or moving articles to a group that has
684 not turned auto-expire on.  If non-nil, articles that have been read
685 will be marked as expirable when being copied or moved to a group in
686 which auto-expire is turned on."
687   :version "23.2"
688   :type 'boolean
689   :group 'gnus-summary-marks)
690
691 (defcustom gnus-view-pseudos nil
692   "*If `automatic', pseudo-articles will be viewed automatically.
693 If `not-confirm', pseudos will be viewed automatically, and the user
694 will not be asked to confirm the command."
695   :group 'gnus-extract-view
696   :type '(choice (const :tag "off" nil)
697                  (const automatic)
698                  (const not-confirm)))
699
700 (defcustom gnus-view-pseudos-separately t
701   "*If non-nil, one pseudo-article will be created for each file to be viewed.
702 If nil, all files that use the same viewing command will be given as a
703 list of parameters to that command."
704   :group 'gnus-extract-view
705   :type 'boolean)
706
707 (defcustom gnus-insert-pseudo-articles t
708   "*If non-nil, insert pseudo-articles when decoding articles."
709   :group 'gnus-extract-view
710   :type 'boolean)
711
712 (defcustom gnus-summary-dummy-line-format
713   "   %(:                             :%) %S\n"
714   "*The format specification for the dummy roots in the summary buffer.
715 It works along the same lines as a normal formatting string,
716 with some simple extensions.
717
718 %S  The subject
719
720 General format specifiers can also be used.
721 See `(gnus)Formatting Variables'."
722   :link '(custom-manual "(gnus)Formatting Variables")
723   :group 'gnus-threading
724   :type 'string)
725
726 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
727   "*The format specification for the summary mode line.
728 It works along the same lines as a normal formatting string,
729 with some simple extensions:
730
731 %G  Group name
732 %p  Unprefixed group name
733 %A  Current article number
734 %z  Current article score
735 %V  Gnus version
736 %U  Number of unread articles in the group
737 %e  Number of unselected articles in the group
738 %Z  A string with unread/unselected article counts
739 %g  Shortish group name
740 %S  Subject of the current article
741 %u  User-defined spec
742 %s  Current score file name
743 %d  Number of dormant articles
744 %r  Number of articles that have been marked as read in this session
745 %E  Number of articles expunged by the score files"
746   :group 'gnus-summary-format
747   :type 'string)
748
749 (defcustom gnus-list-identifiers nil
750   "Regexp that matches list identifiers to be removed from subject.
751 This can also be a list of regexps."
752   :version "21.1"
753   :group 'gnus-summary-format
754   :group 'gnus-article-hiding
755   :type '(choice (const :tag "none" nil)
756                  (regexp :value ".*")
757                  (repeat :value (".*") regexp)))
758
759 (defcustom gnus-summary-mark-below 0
760   "*Mark all articles with a score below this variable as read.
761 This variable is local to each summary buffer and usually set by the
762 score file."
763   :group 'gnus-score-default
764   :type 'integer)
765
766 (defun gnus-widget-reversible-match (widget value)
767   "Ignoring WIDGET, convert VALUE to internal form.
768 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
769   ;; (debug value)
770   (or (symbolp value)
771       (and (listp value)
772            (eq (length value) 2)
773            (eq (nth 0 value) 'not)
774            (symbolp (nth 1 value)))))
775
776 (defun gnus-widget-reversible-to-internal (widget value)
777   "Ignoring WIDGET, convert VALUE to internal form.
778 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
779 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
780   ;; (debug value)
781   (if (atom value)
782       (list value nil)
783     (list (nth 1 value) t)))
784
785 (defun gnus-widget-reversible-to-external (widget value)
786   "Ignoring WIDGET, convert VALUE to external form.
787 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
788 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
789   ;; (debug value)
790   (if (nth 1 value)
791       (list 'not (nth 0 value))
792     (nth 0 value)))
793
794 (define-widget 'gnus-widget-reversible 'group
795   "A `group' that convert values."
796   :match 'gnus-widget-reversible-match
797   :value-to-internal 'gnus-widget-reversible-to-internal
798   :value-to-external 'gnus-widget-reversible-to-external)
799
800 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
801   "*List of functions used for sorting articles in the summary buffer.
802
803 Each function takes two articles and returns non-nil if the first
804 article should be sorted before the other.  If you use more than one
805 function, the primary sort function should be the last.  You should
806 probably always include `gnus-article-sort-by-number' in the list of
807 sorting functions -- preferably first.  Also note that sorting by date
808 is often much slower than sorting by number, and the sorting order is
809 very similar.  (Sorting by date means sorting by the time the message
810 was sent, sorting by number means sorting by arrival time.)
811
812 Each item can also be a list `(not F)' where F is a function;
813 this reverses the sort order.
814
815 Ready-made functions include `gnus-article-sort-by-number',
816 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
817 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
818 and `gnus-article-sort-by-score'.
819
820 When threading is turned on, the variable `gnus-thread-sort-functions'
821 controls how articles are sorted."
822   :group 'gnus-summary-sort
823   :type '(repeat (gnus-widget-reversible
824                   (choice (function-item gnus-article-sort-by-number)
825                           (function-item gnus-article-sort-by-author)
826                           (function-item gnus-article-sort-by-subject)
827                           (function-item gnus-article-sort-by-date)
828                           (function-item gnus-article-sort-by-score)
829                           (function-item gnus-article-sort-by-random)
830                           (function :tag "other"))
831                   (boolean :tag "Reverse order"))))
832
833
834 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
835   "*List of functions used for sorting threads in the summary buffer.
836 By default, threads are sorted by article number.
837
838 Each function takes two threads and returns non-nil if the first
839 thread should be sorted before the other.  If you use more than one
840 function, the primary sort function should be the last.  You should
841 probably always include `gnus-thread-sort-by-number' in the list of
842 sorting functions -- preferably first.  Also note that sorting by date
843 is often much slower than sorting by number, and the sorting order is
844 very similar.  (Sorting by date means sorting by the time the message
845 was sent, sorting by number means sorting by arrival time.)
846
847 Each list item can also be a list `(not F)' where F is a
848 function; this specifies reversed sort order.
849
850 Ready-made functions include `gnus-thread-sort-by-number',
851 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
852 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
853 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
854 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
855 and `gnus-thread-sort-by-total-score' (see
856 `gnus-thread-score-function').
857
858 When threading is turned off, the variable
859 `gnus-article-sort-functions' controls how articles are sorted."
860   :group 'gnus-summary-sort
861   :type '(repeat
862           (gnus-widget-reversible
863            (choice (function-item gnus-thread-sort-by-number)
864                    (function-item gnus-thread-sort-by-author)
865                    (function-item gnus-thread-sort-by-recipient)
866                    (function-item gnus-thread-sort-by-subject)
867                    (function-item gnus-thread-sort-by-date)
868                    (function-item gnus-thread-sort-by-score)
869                    (function-item gnus-thread-sort-by-most-recent-number)
870                    (function-item gnus-thread-sort-by-most-recent-date)
871                    (function-item gnus-thread-sort-by-random)
872                    (function-item gnus-thread-sort-by-total-score)
873                    (function :tag "other"))
874            (boolean :tag "Reverse order"))))
875
876 (defcustom gnus-thread-score-function '+
877   "*Function used for calculating the total score of a thread.
878
879 The function is called with the scores of the article and each
880 subthread and should then return the score of the thread.
881
882 Some functions you can use are `+', `max', or `min'."
883   :group 'gnus-summary-sort
884   :type 'function)
885
886 (defcustom gnus-summary-expunge-below nil
887   "All articles that have a score less than this variable will be expunged.
888 This variable is local to the summary buffers."
889   :group 'gnus-score-default
890   :type '(choice (const :tag "off" nil)
891                  integer))
892
893 (defcustom gnus-thread-expunge-below nil
894   "All threads that have a total score less than this variable will be expunged.
895 See `gnus-thread-score-function' for en explanation of what a
896 \"thread score\" is.
897
898 This variable is local to the summary buffers."
899   :group 'gnus-threading
900   :group 'gnus-score-default
901   :type '(choice (const :tag "off" nil)
902                  integer))
903
904 (defcustom gnus-summary-mode-hook nil
905   "*A hook for Gnus summary mode.
906 This hook is run before any variables are set in the summary buffer."
907   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
908   :group 'gnus-summary-various
909   :type 'hook)
910
911 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
912 (when (featurep 'xemacs)
913   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
914   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
915   (add-hook 'gnus-summary-mode-hook
916             'gnus-xmas-switch-horizontal-scrollbar-off))
917
918 (defcustom gnus-summary-menu-hook nil
919   "*Hook run after the creation of the summary mode menu."
920   :group 'gnus-summary-visual
921   :type 'hook)
922
923 (defcustom gnus-summary-exit-hook nil
924   "*A hook called on exit from the summary buffer.
925 It will be called with point in the group buffer."
926   :group 'gnus-summary-exit
927   :type 'hook)
928
929 (defcustom gnus-summary-prepare-hook nil
930   "*A hook called after the summary buffer has been generated.
931 If you want to modify the summary buffer, you can use this hook."
932   :group 'gnus-summary-various
933   :type 'hook)
934
935 (defcustom gnus-summary-prepared-hook nil
936   "*A hook called as the last thing after the summary buffer has been generated."
937   :group 'gnus-summary-various
938   :type 'hook)
939
940 (defcustom gnus-summary-generate-hook nil
941   "*A hook run just before generating the summary buffer.
942 This hook is commonly used to customize threading variables and the
943 like."
944   :group 'gnus-summary-various
945   :type 'hook)
946
947 (defcustom gnus-select-group-hook nil
948   "*A hook called when a newsgroup is selected.
949
950 If you'd like to simplify subjects like the
951 `gnus-summary-next-same-subject' command does, you can use the
952 following hook:
953
954  (add-hook gnus-select-group-hook
955            (lambda ()
956              (mapcar (lambda (header)
957                        (mail-header-set-subject
958                         header
959                         (gnus-simplify-subject
960                          (mail-header-subject header) 're-only)))
961                      gnus-newsgroup-headers)))"
962   :group 'gnus-group-select
963   :type 'hook)
964
965 (defcustom gnus-select-article-hook nil
966   "*A hook called when an article is selected."
967   :group 'gnus-summary-choose
968   :options '(gnus-agent-fetch-selected-article)
969   :type 'hook)
970
971 (defcustom gnus-visual-mark-article-hook
972   (list 'gnus-highlight-selected-summary)
973   "*Hook run after selecting an article in the summary buffer.
974 It is meant to be used for highlighting the article in some way.  It
975 is not run if `gnus-visual' is nil."
976   :group 'gnus-summary-visual
977   :type 'hook)
978
979 (defcustom gnus-parse-headers-hook nil
980   "*A hook called before parsing the headers."
981   :group 'gnus-various
982   :type 'hook)
983
984 (defcustom gnus-exit-group-hook nil
985   "*A hook called when exiting summary mode.
986 This hook is not called from the non-updating exit commands like `Q'."
987   :group 'gnus-various
988   :type 'hook)
989
990 (defcustom gnus-summary-update-hook
991   (list 'gnus-summary-highlight-line)
992   "*A hook called when a summary line is changed.
993 The hook will not be called if `gnus-visual' is nil.
994
995 The default function `gnus-summary-highlight-line' will
996 highlight the line according to the `gnus-summary-highlight'
997 variable."
998   :group 'gnus-summary-visual
999   :type 'hook)
1000
1001 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1002   "*A hook called when an article is selected for the first time.
1003 The hook is intended to mark an article as read (or unread)
1004 automatically when it is selected."
1005   :group 'gnus-summary-choose
1006   :type 'hook)
1007
1008 (defcustom gnus-group-no-more-groups-hook nil
1009   "*A hook run when returning to group mode having no more (unread) groups."
1010   :group 'gnus-group-select
1011   :type 'hook)
1012
1013 (defcustom gnus-ps-print-hook nil
1014   "*A hook run before ps-printing something from Gnus."
1015   :group 'gnus-summary
1016   :type 'hook)
1017
1018 (defcustom gnus-summary-article-move-hook nil
1019   "*A hook called after an article is moved, copied, respooled, or crossposted."
1020   :version "22.1"
1021   :group 'gnus-summary
1022   :type 'hook)
1023
1024 (defcustom gnus-summary-article-delete-hook nil
1025   "*A hook called after an article is deleted."
1026   :version "22.1"
1027   :group 'gnus-summary
1028   :type 'hook)
1029
1030 (defcustom gnus-summary-article-expire-hook nil
1031   "*A hook called after an article is expired."
1032   :version "22.1"
1033   :group 'gnus-summary
1034   :type 'hook)
1035
1036 (defcustom gnus-summary-display-arrow
1037   (and (fboundp 'display-graphic-p)
1038        (display-graphic-p))
1039   "*If non-nil, display an arrow highlighting the current article."
1040   :version "22.1"
1041   :group 'gnus-summary
1042   :type 'boolean)
1043
1044 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1045   "Face used for highlighting the current article in the summary buffer."
1046   :group 'gnus-summary-visual
1047   :type 'face)
1048
1049 (defvar gnus-tmp-downloaded nil)
1050
1051 (defcustom gnus-summary-highlight
1052   '(((eq mark gnus-canceled-mark)
1053      . gnus-summary-cancelled)
1054     ((and uncached (> score default-high))
1055      . gnus-summary-high-undownloaded)
1056     ((and uncached (< score default-low))
1057      . gnus-summary-low-undownloaded)
1058     (uncached
1059      . gnus-summary-normal-undownloaded)
1060     ((and (> score default-high)
1061           (or (eq mark gnus-dormant-mark)
1062               (eq mark gnus-ticked-mark)))
1063      . gnus-summary-high-ticked)
1064     ((and (< score default-low)
1065           (or (eq mark gnus-dormant-mark)
1066               (eq mark gnus-ticked-mark)))
1067      . gnus-summary-low-ticked)
1068     ((or (eq mark gnus-dormant-mark)
1069          (eq mark gnus-ticked-mark))
1070      . gnus-summary-normal-ticked)
1071     ((and (> score default-high) (eq mark gnus-ancient-mark))
1072      . gnus-summary-high-ancient)
1073     ((and (< score default-low) (eq mark gnus-ancient-mark))
1074      . gnus-summary-low-ancient)
1075     ((eq mark gnus-ancient-mark)
1076      . gnus-summary-normal-ancient)
1077     ((and (> score default-high) (eq mark gnus-unread-mark))
1078      . gnus-summary-high-unread)
1079     ((and (< score default-low) (eq mark gnus-unread-mark))
1080      . gnus-summary-low-unread)
1081     ((eq mark gnus-unread-mark)
1082      . gnus-summary-normal-unread)
1083     ((> score default-high)
1084      . gnus-summary-high-read)
1085     ((< score default-low)
1086      . gnus-summary-low-read)
1087     (t
1088      . gnus-summary-normal-read))
1089   "*Controls the highlighting of summary buffer lines.
1090
1091 A list of (FORM . FACE) pairs.  When deciding how a particular
1092 summary line should be displayed, each form is evaluated.  The content
1093 of the face field after the first true form is used.  You can change
1094 how those summary lines are displayed, by editing the face field.
1095
1096 You can use the following variables in the FORM field.
1097
1098 score:        The article's score.
1099 default:      The default article score.
1100 default-high: The default score for high scored articles.
1101 default-low:  The default score for low scored articles.
1102 below:        The score below which articles are automatically marked as read.
1103 mark:         The article's mark.
1104 uncached:     Non-nil if the article is uncached."
1105   :group 'gnus-summary-visual
1106   :type '(repeat (cons (sexp :tag "Form" nil)
1107                        face)))
1108 (put 'gnus-summary-highlight 'risky-local-variable t)
1109
1110 (defcustom gnus-alter-header-function nil
1111   "Function called to allow alteration of article header structures.
1112 The function is called with one parameter, the article header vector,
1113 which it may alter in any way."
1114   :type '(choice (const :tag "None" nil)
1115                  function)
1116   :group 'gnus-summary)
1117
1118 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1119   "Function used to decode a string with encoded words.")
1120
1121 (defvar gnus-decode-encoded-address-function
1122   'mail-decode-encoded-address-string
1123   "Function used to decode addresses with encoded words.")
1124
1125 (defcustom gnus-extra-headers '(To Newsgroups)
1126   "*Extra headers to parse."
1127   :version "21.1"
1128   :group 'gnus-summary
1129   :type '(repeat symbol))
1130
1131 (defcustom gnus-ignored-from-addresses
1132   (and user-mail-address
1133        (not (string= user-mail-address ""))
1134        (regexp-quote user-mail-address))
1135   "*From headers that may be suppressed in favor of To headers.
1136 This can be a regexp or a list of regexps."
1137   :version "21.1"
1138   :group 'gnus-summary
1139   :type '(choice regexp
1140                  (repeat :tag "Regexp List" regexp)))
1141
1142 (defsubst gnus-ignored-from-addresses ()
1143   (gmm-regexp-concat gnus-ignored-from-addresses))
1144
1145 (defcustom gnus-summary-to-prefix "-> "
1146   "*String prefixed to the To field in the summary line when
1147 using `gnus-ignored-from-addresses'."
1148   :version "22.1"
1149   :group 'gnus-summary
1150   :type 'string)
1151
1152 (defcustom gnus-summary-newsgroup-prefix "=> "
1153   "*String prefixed to the Newsgroup field in the summary
1154 line when using `gnus-ignored-from-addresses'."
1155   :version "22.1"
1156   :group 'gnus-summary
1157   :type 'string)
1158
1159 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1160   "List of charsets that should be ignored.
1161 When these charsets are used in the \"charset\" parameter, the
1162 default charset will be used instead."
1163   :version "21.1"
1164   :type '(repeat symbol)
1165   :group 'gnus-charset)
1166
1167 (defcustom gnus-newsgroup-maximum-articles nil
1168   "The maximum number of articles a newsgroup.
1169 If this is a number, old articles in a newsgroup exceeding this number
1170 are silently ignored.  If it is nil, no article is ignored.  Note that
1171 setting this variable to a number might prevent you from reading very
1172 old articles."
1173   :group 'gnus-group-select
1174   :version "22.2"
1175   :type '(choice (const :tag "No limit" nil)
1176                  integer))
1177
1178 (gnus-define-group-parameter
1179  ignored-charsets
1180  :type list
1181  :function-document
1182  "Return the ignored charsets of GROUP."
1183  :variable gnus-group-ignored-charsets-alist
1184  :variable-default
1185  '(("alt\\.chinese\\.text" iso-8859-1))
1186  :variable-document
1187  "Alist of regexps (to match group names) and charsets that should be ignored.
1188 When these charsets are used in the \"charset\" parameter, the
1189 default charset will be used instead."
1190  :variable-group gnus-charset
1191  :variable-type '(repeat (cons (regexp :tag "Group")
1192                                (repeat symbol)))
1193  :parameter-type '(choice :tag "Ignored charsets"
1194                           :value nil
1195                           (repeat (symbol)))
1196  :parameter-document       "\
1197 List of charsets that should be ignored.
1198
1199 When these charsets are used in the \"charset\" parameter, the
1200 default charset will be used instead.")
1201
1202 (defcustom gnus-group-highlight-words-alist nil
1203   "Alist of group regexps and highlight regexps.
1204 This variable uses the same syntax as `gnus-emphasis-alist'."
1205   :version "21.1"
1206   :type '(repeat (cons (regexp :tag "Group")
1207                        (repeat (list (regexp :tag "Highlight regexp")
1208                                      (number :tag "Group for entire word" 0)
1209                                      (number :tag "Group for displayed part" 0)
1210                                      (symbol :tag "Face"
1211                                              gnus-emphasis-highlight-words)))))
1212   :group 'gnus-summary-visual)
1213
1214 (defcustom gnus-summary-show-article-charset-alist
1215   nil
1216   "Alist of number and charset.
1217 The article will be shown with the charset corresponding to the
1218 numbered argument.
1219 For example: ((1 . cn-gb-2312) (2 . big5))."
1220   :version "21.1"
1221   :type '(repeat (cons (number :tag "Argument" 1)
1222                        (symbol :tag "Charset")))
1223   :group 'gnus-charset)
1224
1225 (defcustom gnus-preserve-marks t
1226   "Whether marks are preserved when moving, copying and respooling messages."
1227   :version "21.1"
1228   :type 'boolean
1229   :group 'gnus-summary-marks)
1230
1231 (defcustom gnus-propagate-marks t
1232   "If non-nil, do not propagate marks to the backends."
1233   :version "23.1" ;; No Gnus
1234   :type 'boolean
1235   :group 'gnus-summary-marks)
1236
1237 (defcustom gnus-alter-articles-to-read-function nil
1238   "Function to be called to alter the list of articles to be selected."
1239   :type '(choice (const nil) function)
1240   :group 'gnus-summary)
1241
1242 (defcustom gnus-orphan-score nil
1243   "*All orphans get this score added.  Set in the score file."
1244   :group 'gnus-score-default
1245   :type '(choice (const nil)
1246                  integer))
1247
1248 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1249   "*A regexp to match MIME parts when saving multiple parts of a
1250 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1251 This regexp will be used by default when prompting the user for which
1252 type of files to save."
1253   :group 'gnus-summary
1254   :type 'regexp)
1255
1256 (defcustom gnus-read-all-available-headers nil
1257   "Whether Gnus should parse all headers made available to it.
1258 This is mostly relevant for slow back ends where the user may
1259 wish to widen the summary buffer to include all headers
1260 that were fetched.  Say, for nnultimate groups."
1261   :version "22.1"
1262   :group 'gnus-summary
1263   :type '(choice boolean regexp))
1264
1265 (defcustom gnus-summary-pipe-output-default-command nil
1266   "Command (and optional arguments) used to pipe article to subprocess.
1267 This will be used as the default command if it is non-nil.  The value
1268 will be updated if you modify it when executing the command
1269 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1270   :version "23.1" ;; No Gnus
1271   :group 'gnus-summary
1272   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1273
1274 (defcustom gnus-summary-muttprint-program "muttprint"
1275   "Command (and optional arguments) used to run Muttprint.
1276 The value will be updated if you modify it when executing the command
1277 `gnus-summary-muttprint'."
1278   :version "22.1"
1279   :group 'gnus-summary
1280   :type 'string)
1281
1282 (defcustom gnus-article-loose-mime t
1283   "If non-nil, don't require MIME-Version header.
1284 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1285 supply the MIME-Version header or deliberately strip it from the mail.
1286 If non-nil (the default), Gnus will treat some articles as MIME
1287 even if the MIME-Version header is missing."
1288   :version "22.1"
1289   :type 'boolean
1290   :group 'gnus-article-mime)
1291
1292 (defcustom gnus-article-emulate-mime t
1293   "If non-nil, use MIME emulation for uuencode and the like.
1294 This means that Gnus will search message bodies for text that look
1295 like uuencoded bits, yEncoded bits, and so on, and present that using
1296 the normal Gnus MIME machinery."
1297   :version "22.1"
1298   :type 'boolean
1299   :group 'gnus-article-mime)
1300
1301 ;;; Internal variables
1302
1303 (defvar gnus-summary-display-cache nil)
1304 (defvar gnus-article-mime-handles nil)
1305 (defvar gnus-article-decoded-p nil)
1306 (defvar gnus-article-charset nil)
1307 (defvar gnus-article-ignored-charsets nil)
1308 (defvar gnus-scores-exclude-files nil)
1309 (defvar gnus-page-broken nil)
1310
1311 (defvar gnus-original-article nil)
1312 (defvar gnus-article-internal-prepare-hook nil)
1313 (defvar gnus-newsgroup-process-stack nil)
1314
1315 (defvar gnus-thread-indent-array nil)
1316 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1317 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1318   "Function called to sort the articles within a thread after it has been gathered together.")
1319
1320 (defvar gnus-summary-save-parts-type-history nil)
1321 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1322
1323 ;; Avoid highlighting in kill files.
1324 (defvar gnus-summary-inhibit-highlight nil)
1325 (defvar gnus-newsgroup-selected-overlay nil)
1326 (defvar gnus-inhibit-limiting nil)
1327 (defvar gnus-newsgroup-adaptive-score-file nil)
1328 (defvar gnus-current-score-file nil)
1329 (defvar gnus-current-move-group nil)
1330 (defvar gnus-current-copy-group nil)
1331 (defvar gnus-current-crosspost-group nil)
1332 (defvar gnus-newsgroup-display nil)
1333
1334 (defvar gnus-newsgroup-dependencies nil)
1335 (defvar gnus-newsgroup-adaptive nil)
1336 (defvar gnus-summary-display-article-function nil)
1337 (defvar gnus-summary-highlight-line-function nil
1338   "Function called after highlighting a summary line.")
1339
1340 (defvar gnus-summary-line-format-alist
1341   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1342     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1343     (?s gnus-tmp-subject-or-nil ?s)
1344     (?n gnus-tmp-name ?s)
1345     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1346         ?s)
1347     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1348             gnus-tmp-from) ?s)
1349     (?F gnus-tmp-from ?s)
1350     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1351     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1352     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1353     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1354     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1355     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1356     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1357     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1358     (?L gnus-tmp-lines ?s)
1359     (?O gnus-tmp-downloaded ?c)
1360     (?I gnus-tmp-indentation ?s)
1361     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1362     (?R gnus-tmp-replied ?c)
1363     (?\[ gnus-tmp-opening-bracket ?c)
1364     (?\] gnus-tmp-closing-bracket ?c)
1365     (?\> (make-string gnus-tmp-level ? ) ?s)
1366     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1367     (?i gnus-tmp-score ?d)
1368     (?z gnus-tmp-score-char ?c)
1369     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1370     (?U gnus-tmp-unread ?c)
1371     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1372         ?s)
1373     (?t (gnus-summary-number-of-articles-in-thread
1374          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1375         ?d)
1376     (?e (gnus-summary-number-of-articles-in-thread
1377          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1378         ?c)
1379     (?u gnus-tmp-user-defined ?s)
1380     (?P (gnus-pick-line-number) ?d)
1381     (?B gnus-tmp-thread-tree-header-string ?s)
1382     (user-date (gnus-user-date
1383                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1384   "An alist of format specifications that can appear in summary lines.
1385 These are paired with what variables they correspond with, along with
1386 the type of the variable (string, integer, character, etc).")
1387
1388 (defvar gnus-summary-dummy-line-format-alist
1389   `((?S gnus-tmp-subject ?s)
1390     (?N gnus-tmp-number ?d)
1391     (?u gnus-tmp-user-defined ?s)))
1392
1393 (defvar gnus-summary-mode-line-format-alist
1394   `((?G gnus-tmp-group-name ?s)
1395     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1396     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1397     (?A gnus-tmp-article-number ?d)
1398     (?Z gnus-tmp-unread-and-unselected ?s)
1399     (?V gnus-version ?s)
1400     (?U gnus-tmp-unread-and-unticked ?d)
1401     (?S gnus-tmp-subject ?s)
1402     (?e gnus-tmp-unselected ?d)
1403     (?u gnus-tmp-user-defined ?s)
1404     (?d (length gnus-newsgroup-dormant) ?d)
1405     (?t (length gnus-newsgroup-marked) ?d)
1406     (?h (length gnus-newsgroup-spam-marked) ?d)
1407     (?r (length gnus-newsgroup-reads) ?d)
1408     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1409     (?E gnus-newsgroup-expunged-tally ?d)
1410     (?s (gnus-current-score-file-nondirectory) ?s)))
1411
1412 ;; This is here rather than in gnus-art for compilation reasons.
1413 (defvar gnus-article-mode-line-format-alist
1414   (nconc '((?w (gnus-article-wash-status) ?s)
1415            (?m (gnus-article-mime-part-status) ?s))
1416          gnus-summary-mode-line-format-alist))
1417
1418 (defvar gnus-last-search-regexp nil
1419   "Default regexp for article search command.")
1420
1421 (defvar gnus-last-shell-command nil
1422   "Default shell command on article.")
1423
1424 (defvar gnus-newsgroup-agentized nil
1425   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1426 (defvar gnus-newsgroup-begin nil)
1427 (defvar gnus-newsgroup-end nil)
1428 (defvar gnus-newsgroup-last-rmail nil)
1429 (defvar gnus-newsgroup-last-mail nil)
1430 (defvar gnus-newsgroup-last-folder nil)
1431 (defvar gnus-newsgroup-last-file nil)
1432 (defvar gnus-newsgroup-last-directory nil)
1433 (defvar gnus-newsgroup-auto-expire nil)
1434 (defvar gnus-newsgroup-active nil)
1435
1436 (defvar gnus-newsgroup-data nil)
1437 (defvar gnus-newsgroup-data-reverse nil)
1438 (defvar gnus-newsgroup-limit nil)
1439 (defvar gnus-newsgroup-limits nil)
1440 (defvar gnus-summary-use-undownloaded-faces nil)
1441
1442 (defvar gnus-newsgroup-unreads nil
1443   "Sorted list of unread articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-unselected nil
1446   "Sorted list of unselected unread articles in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-reads nil
1449   "Alist of read articles and article marks in the current newsgroup.")
1450
1451 (defvar gnus-newsgroup-expunged-tally nil)
1452
1453 (defvar gnus-newsgroup-marked nil
1454   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1455
1456 (defvar gnus-newsgroup-spam-marked nil
1457   "List of ranges of articles that have been marked as spam.")
1458
1459 (defvar gnus-newsgroup-killed nil
1460   "List of ranges of articles that have been through the scoring process.")
1461
1462 (defvar gnus-newsgroup-cached nil
1463   "Sorted list of articles that come from the article cache.")
1464
1465 (defvar gnus-newsgroup-saved nil
1466   "List of articles that have been saved.")
1467
1468 (defvar gnus-newsgroup-kill-headers nil)
1469
1470 (defvar gnus-newsgroup-replied nil
1471   "List of articles that have been replied to in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-forwarded nil
1474   "List of articles that have been forwarded in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-recent nil
1477   "List of articles that have are recent in the current newsgroup.")
1478
1479 (defvar gnus-newsgroup-expirable nil
1480   "Sorted list of articles in the current newsgroup that can be expired.")
1481
1482 (defvar gnus-newsgroup-processable nil
1483   "List of articles in the current newsgroup that can be processed.")
1484
1485 (defvar gnus-newsgroup-downloadable nil
1486   "Sorted list of articles in the current newsgroup that can be processed.")
1487
1488 (defvar gnus-newsgroup-unfetched nil
1489   "Sorted list of articles in the current newsgroup whose headers have
1490 not been fetched into the agent.
1491
1492 This list will always be a subset of gnus-newsgroup-undownloaded.")
1493
1494 (defvar gnus-newsgroup-undownloaded nil
1495   "List of articles in the current newsgroup that haven't been downloaded.")
1496
1497 (defvar gnus-newsgroup-unsendable nil
1498   "List of articles in the current newsgroup that won't be sent.")
1499
1500 (defvar gnus-newsgroup-bookmarks nil
1501   "List of articles in the current newsgroup that have bookmarks.")
1502
1503 (defvar gnus-newsgroup-dormant nil
1504   "Sorted list of dormant articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-unseen nil
1507   "List of unseen articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-seen nil
1510   "Range of seen articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-articles nil
1513   "List of articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-scored nil
1516   "List of scored articles in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-headers nil
1519   "List of article headers in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-threads nil)
1522
1523 (defvar gnus-newsgroup-prepared nil
1524   "Whether the current group has been prepared properly.")
1525
1526 (defvar gnus-newsgroup-ancient nil
1527   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-sparse nil)
1530
1531 (defvar gnus-current-article nil)
1532 (defvar gnus-article-current nil)
1533 (defvar gnus-current-headers nil)
1534 (defvar gnus-have-all-headers nil)
1535 (defvar gnus-last-article nil)
1536 (defvar gnus-newsgroup-history nil)
1537 (defvar gnus-newsgroup-charset nil)
1538 (defvar gnus-newsgroup-ephemeral-charset nil)
1539 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1540
1541 (defvar gnus-article-before-search nil)
1542
1543 (defvar gnus-summary-local-variables
1544   '(gnus-newsgroup-name
1545     gnus-newsgroup-begin gnus-newsgroup-end
1546     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1547     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1548     gnus-newsgroup-last-directory
1549     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1550     gnus-newsgroup-unselected gnus-newsgroup-marked
1551     gnus-newsgroup-spam-marked
1552     gnus-newsgroup-reads gnus-newsgroup-saved
1553     gnus-newsgroup-replied gnus-newsgroup-forwarded
1554     gnus-newsgroup-recent
1555     gnus-newsgroup-expirable
1556     gnus-newsgroup-processable gnus-newsgroup-killed
1557     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1558     gnus-newsgroup-unfetched
1559     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1560     gnus-newsgroup-seen gnus-newsgroup-articles
1561     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1562     gnus-newsgroup-headers gnus-newsgroup-threads
1563     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1564     gnus-current-article gnus-current-headers gnus-have-all-headers
1565     gnus-last-article gnus-article-internal-prepare-hook
1566     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1567     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1568     gnus-thread-expunge-below
1569     gnus-score-alist gnus-current-score-file
1570     (gnus-summary-expunge-below . global)
1571     (gnus-summary-mark-below . global)
1572     (gnus-orphan-score . global)
1573     gnus-newsgroup-active gnus-scores-exclude-files
1574     gnus-newsgroup-history gnus-newsgroup-ancient
1575     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1576     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1577     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1578     (gnus-newsgroup-expunged-tally . 0)
1579     gnus-cache-removable-articles gnus-newsgroup-cached
1580     gnus-newsgroup-data gnus-newsgroup-data-reverse
1581     gnus-newsgroup-limit gnus-newsgroup-limits
1582     gnus-newsgroup-charset gnus-newsgroup-display
1583     gnus-summary-use-undownloaded-faces)
1584   "Variables that are buffer-local to the summary buffers.")
1585
1586 (defvar gnus-newsgroup-variables nil
1587   "A list of variables that have separate values in different newsgroups.
1588 A list of newsgroup (summary buffer) local variables, or cons of
1589 variables and their default expressions to be evalled (when the default
1590 values are not nil), that should be made global while the summary buffer
1591 is active.
1592
1593 Note: The default expressions will be evaluated (using function `eval')
1594 before assignment to the local variable rather than just assigned to it.
1595 If the default expression is the symbol `global', that symbol will not
1596 be evaluated but the global value of the local variable will be used
1597 instead.
1598
1599 These variables can be used to set variables in the group parameters
1600 while still allowing them to affect operations done in other buffers.
1601 For example:
1602
1603 \(setq gnus-newsgroup-variables
1604      '(message-use-followup-to
1605        (gnus-visible-headers .
1606          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1607 ")
1608
1609 (eval-when-compile
1610   ;; Bind features so that require will believe that gnus-sum has
1611   ;; already been loaded (avoids infinite recursion)
1612   (let ((features (cons 'gnus-sum features)))
1613     (require 'gnus-art)))
1614
1615 ;; MIME stuff.
1616
1617 (defvar gnus-decode-encoded-word-methods
1618   '(mail-decode-encoded-word-string)
1619   "List of methods used to decode encoded words.
1620
1621 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1622 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1623 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1624 whose names match REGEXP.
1625
1626 For example:
1627 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1628  mail-decode-encoded-word-string
1629  (\"chinese\" . rfc1843-decode-string))")
1630
1631 (defvar gnus-decode-encoded-word-methods-cache nil)
1632
1633 (defun gnus-multi-decode-encoded-word-string (string)
1634   "Apply the functions from `gnus-encoded-word-methods' that match."
1635   (unless (and gnus-decode-encoded-word-methods-cache
1636                (eq gnus-newsgroup-name
1637                    (car gnus-decode-encoded-word-methods-cache)))
1638     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1639     (dolist (method gnus-decode-encoded-word-methods)
1640       (if (symbolp method)
1641           (nconc gnus-decode-encoded-word-methods-cache (list method))
1642         (if (and gnus-newsgroup-name
1643                  (string-match (car method) gnus-newsgroup-name))
1644             (nconc gnus-decode-encoded-word-methods-cache
1645                    (list (cdr method)))))))
1646   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1647     (setq string (funcall method string))))
1648
1649 ;; Subject simplification.
1650
1651 (defun gnus-simplify-whitespace (str)
1652   "Remove excessive whitespace from STR."
1653   ;; Multiple spaces.
1654   (while (string-match "[ \t][ \t]+" str)
1655     (setq str (concat (substring str 0 (match-beginning 0))
1656                         " "
1657                         (substring str (match-end 0)))))
1658   ;; Leading spaces.
1659   (when (string-match "^[ \t]+" str)
1660     (setq str (substring str (match-end 0))))
1661   ;; Trailing spaces.
1662   (when (string-match "[ \t]+$" str)
1663     (setq str (substring str 0 (match-beginning 0))))
1664   str)
1665
1666 (defun gnus-simplify-all-whitespace (str)
1667   "Remove all whitespace from STR."
1668   (while (string-match "[ \t\n]+" str)
1669     (setq str (replace-match "" nil nil str)))
1670   str)
1671
1672 (defsubst gnus-simplify-subject-re (subject)
1673   "Remove \"Re:\" from subject lines."
1674   (if (string-match message-subject-re-regexp subject)
1675       (substring subject (match-end 0))
1676     subject))
1677
1678 (defun gnus-simplify-subject (subject &optional re-only)
1679   "Remove `Re:' and words in parentheses.
1680 If RE-ONLY is non-nil, strip leading `Re:'s only."
1681   (let ((case-fold-search t))           ;Ignore case.
1682     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1683     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1684       (setq subject (substring subject (match-end 0))))
1685     ;; Remove uninteresting prefixes.
1686     (when (and (not re-only)
1687                gnus-simplify-ignored-prefixes
1688                (string-match gnus-simplify-ignored-prefixes subject))
1689       (setq subject (substring subject (match-end 0))))
1690     ;; Remove words in parentheses from end.
1691     (unless re-only
1692       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1693         (setq subject (substring subject 0 (match-beginning 0)))))
1694     ;; Return subject string.
1695     subject))
1696
1697 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1698 ;; all whitespace.
1699 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1700   (goto-char (point-min))
1701   (while (re-search-forward regexp nil t)
1702     (replace-match (or newtext ""))))
1703
1704 (defun gnus-simplify-buffer-fuzzy ()
1705   "Simplify string in the buffer fuzzily.
1706 The string in the accessible portion of the current buffer is simplified.
1707 It is assumed to be a single-line subject.
1708 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1709 matter is removed.  Additional things can be deleted by setting
1710 `gnus-simplify-subject-fuzzy-regexp'."
1711   (let ((case-fold-search t)
1712         (modified-tick))
1713     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1714
1715     (while (not (eq modified-tick (buffer-modified-tick)))
1716       (setq modified-tick (buffer-modified-tick))
1717       (cond
1718        ((listp gnus-simplify-subject-fuzzy-regexp)
1719         (mapc 'gnus-simplify-buffer-fuzzy-step
1720               gnus-simplify-subject-fuzzy-regexp))
1721        (gnus-simplify-subject-fuzzy-regexp
1722         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1723       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1724       (gnus-simplify-buffer-fuzzy-step
1725        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1726       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1727
1728     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1729     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1730     (gnus-simplify-buffer-fuzzy-step " $")
1731     (gnus-simplify-buffer-fuzzy-step "^ +")))
1732
1733 (defun gnus-simplify-subject-fuzzy (subject)
1734   "Simplify a subject string fuzzily.
1735 See `gnus-simplify-buffer-fuzzy' for details."
1736   (save-excursion
1737     (gnus-set-work-buffer)
1738     (let ((case-fold-search t))
1739       ;; Remove uninteresting prefixes.
1740       (when (and gnus-simplify-ignored-prefixes
1741                  (string-match gnus-simplify-ignored-prefixes subject))
1742         (setq subject (substring subject (match-end 0))))
1743       (insert subject)
1744       (inline (gnus-simplify-buffer-fuzzy))
1745       (buffer-string))))
1746
1747 (defsubst gnus-simplify-subject-fully (subject)
1748   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1749   (cond
1750    (gnus-simplify-subject-functions
1751     (gnus-map-function gnus-simplify-subject-functions subject))
1752    ((null gnus-summary-gather-subject-limit)
1753     (gnus-simplify-subject-re subject))
1754    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1755     (gnus-simplify-subject-fuzzy subject))
1756    ((numberp gnus-summary-gather-subject-limit)
1757     (truncate-string-to-width (gnus-simplify-subject-re subject)
1758                               gnus-summary-gather-subject-limit))
1759    (t
1760     subject)))
1761
1762 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1763   "Check whether two subjects are equal.
1764 If optional argument SIMPLE-FIRST is t, first argument is already
1765 simplified."
1766   (cond
1767    ((null simple-first)
1768     (equal (gnus-simplify-subject-fully s1)
1769            (gnus-simplify-subject-fully s2)))
1770    (t
1771     (equal s1
1772            (gnus-simplify-subject-fully s2)))))
1773
1774 (defun gnus-summary-bubble-group ()
1775   "Increase the score of the current group.
1776 This is a handy function to add to `gnus-summary-exit-hook' to
1777 increase the score of each group you read."
1778   (gnus-group-add-score gnus-newsgroup-name))
1779
1780 \f
1781 ;;;
1782 ;;; Gnus summary mode
1783 ;;;
1784
1785 (put 'gnus-summary-mode 'mode-class 'special)
1786
1787 (defvar gnus-article-commands-menu)
1788
1789 ;; Non-orthogonal keys
1790
1791 (gnus-define-keys gnus-summary-mode-map
1792   " " gnus-summary-next-page
1793   "\177" gnus-summary-prev-page
1794   [delete] gnus-summary-prev-page
1795   [backspace] gnus-summary-prev-page
1796   "\r" gnus-summary-scroll-up
1797   "\M-\r" gnus-summary-scroll-down
1798   "n" gnus-summary-next-unread-article
1799   "p" gnus-summary-prev-unread-article
1800   "N" gnus-summary-next-article
1801   "P" gnus-summary-prev-article
1802   "\M-\C-n" gnus-summary-next-same-subject
1803   "\M-\C-p" gnus-summary-prev-same-subject
1804   "\M-n" gnus-summary-next-unread-subject
1805   "\M-p" gnus-summary-prev-unread-subject
1806   "." gnus-summary-first-unread-article
1807   "," gnus-summary-best-unread-article
1808   "\M-s" gnus-summary-search-article-forward
1809   "\M-r" gnus-summary-search-article-backward
1810   "\M-S" gnus-summary-repeat-search-article-forward
1811   "\M-R" gnus-summary-repeat-search-article-backward
1812   "<" gnus-summary-beginning-of-article
1813   ">" gnus-summary-end-of-article
1814   "j" gnus-summary-goto-article
1815   "^" gnus-summary-refer-parent-article
1816   "\M-^" gnus-summary-refer-article
1817   "u" gnus-summary-tick-article-forward
1818   "!" gnus-summary-tick-article-forward
1819   "U" gnus-summary-tick-article-backward
1820   "d" gnus-summary-mark-as-read-forward
1821   "D" gnus-summary-mark-as-read-backward
1822   "E" gnus-summary-mark-as-expirable
1823   "\M-u" gnus-summary-clear-mark-forward
1824   "\M-U" gnus-summary-clear-mark-backward
1825   "k" gnus-summary-kill-same-subject-and-select
1826   "\C-k" gnus-summary-kill-same-subject
1827   "\M-\C-k" gnus-summary-kill-thread
1828   "\M-\C-l" gnus-summary-lower-thread
1829   "e" gnus-summary-edit-article
1830   "#" gnus-summary-mark-as-processable
1831   "\M-#" gnus-summary-unmark-as-processable
1832   "\M-\C-t" gnus-summary-toggle-threads
1833   "\M-\C-s" gnus-summary-show-thread
1834   "\M-\C-h" gnus-summary-hide-thread
1835   "\M-\C-f" gnus-summary-next-thread
1836   "\M-\C-b" gnus-summary-prev-thread
1837   [(meta down)] gnus-summary-next-thread
1838   [(meta up)] gnus-summary-prev-thread
1839   "\M-\C-u" gnus-summary-up-thread
1840   "\M-\C-d" gnus-summary-down-thread
1841   "&" gnus-summary-execute-command
1842   "c" gnus-summary-catchup-and-exit
1843   "\C-w" gnus-summary-mark-region-as-read
1844   "\C-t" gnus-summary-toggle-truncation
1845   "?" gnus-summary-mark-as-dormant
1846   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1847   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1848   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1849   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1850   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1851   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1852   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1853   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1854   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1855   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1856   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1857   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1858   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1859   "=" gnus-summary-expand-window
1860   "\C-x\C-s" gnus-summary-reselect-current-group
1861   "\M-g" gnus-summary-rescan-group
1862   "w" gnus-summary-stop-page-breaking
1863   "\C-c\C-r" gnus-summary-caesar-message
1864   "f" gnus-summary-followup
1865   "F" gnus-summary-followup-with-original
1866   "C" gnus-summary-cancel-article
1867   "r" gnus-summary-reply
1868   "R" gnus-summary-reply-with-original
1869   "\C-c\C-f" gnus-summary-mail-forward
1870   "o" gnus-summary-save-article
1871   "\C-o" gnus-summary-save-article-mail
1872   "|" gnus-summary-pipe-output
1873   "\M-k" gnus-summary-edit-local-kill
1874   "\M-K" gnus-summary-edit-global-kill
1875   ;; "V" gnus-version
1876   "\C-c\C-d" gnus-summary-describe-group
1877   "q" gnus-summary-exit
1878   "Q" gnus-summary-exit-no-update
1879   "\C-c\C-i" gnus-info-find-node
1880   gnus-mouse-2 gnus-mouse-pick-article
1881   [follow-link] mouse-face
1882   "m" gnus-summary-mail-other-window
1883   "a" gnus-summary-post-news
1884   "i" gnus-summary-news-other-window
1885   "x" gnus-summary-limit-to-unread
1886   "s" gnus-summary-isearch-article
1887   "t" gnus-summary-toggle-header
1888   "g" gnus-summary-show-article
1889   "l" gnus-summary-goto-last-article
1890   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1891   "\C-d" gnus-summary-enter-digest-group
1892   "\M-\C-d" gnus-summary-read-document
1893   "\M-\C-e" gnus-summary-edit-parameters
1894   "\M-\C-a" gnus-summary-customize-parameters
1895   "\C-c\C-b" gnus-bug
1896   "*" gnus-cache-enter-article
1897   "\M-*" gnus-cache-remove-article
1898   "\M-&" gnus-summary-universal-argument
1899   "\C-l" gnus-recenter
1900   "I" gnus-summary-increase-score
1901   "L" gnus-summary-lower-score
1902   "\M-i" gnus-symbolic-argument
1903   "h" gnus-summary-select-article-buffer
1904
1905   "b" gnus-article-view-part
1906   "\M-t" gnus-summary-toggle-display-buttonized
1907
1908   "V" gnus-summary-score-map
1909   "X" gnus-uu-extract-map
1910   "S" gnus-summary-send-map)
1911
1912 ;; Sort of orthogonal keymap
1913 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1914   "t" gnus-summary-tick-article-forward
1915   "!" gnus-summary-tick-article-forward
1916   "d" gnus-summary-mark-as-read-forward
1917   "r" gnus-summary-mark-as-read-forward
1918   "c" gnus-summary-clear-mark-forward
1919   " " gnus-summary-clear-mark-forward
1920   "e" gnus-summary-mark-as-expirable
1921   "x" gnus-summary-mark-as-expirable
1922   "?" gnus-summary-mark-as-dormant
1923   "b" gnus-summary-set-bookmark
1924   "B" gnus-summary-remove-bookmark
1925   "#" gnus-summary-mark-as-processable
1926   "\M-#" gnus-summary-unmark-as-processable
1927   "S" gnus-summary-limit-include-expunged
1928   "C" gnus-summary-catchup
1929   "H" gnus-summary-catchup-to-here
1930   "h" gnus-summary-catchup-from-here
1931   "\C-c" gnus-summary-catchup-all
1932   "k" gnus-summary-kill-same-subject-and-select
1933   "K" gnus-summary-kill-same-subject
1934   "P" gnus-uu-mark-map)
1935
1936 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1937   "c" gnus-summary-clear-above
1938   "u" gnus-summary-tick-above
1939   "m" gnus-summary-mark-above
1940   "k" gnus-summary-kill-below)
1941
1942 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1943   "/" gnus-summary-limit-to-subject
1944   "n" gnus-summary-limit-to-articles
1945   "b" gnus-summary-limit-to-bodies
1946   "h" gnus-summary-limit-to-headers
1947   "w" gnus-summary-pop-limit
1948   "s" gnus-summary-limit-to-subject
1949   "a" gnus-summary-limit-to-author
1950   "u" gnus-summary-limit-to-unread
1951   "m" gnus-summary-limit-to-marks
1952   "M" gnus-summary-limit-exclude-marks
1953   "v" gnus-summary-limit-to-score
1954   "*" gnus-summary-limit-include-cached
1955   "D" gnus-summary-limit-include-dormant
1956   "T" gnus-summary-limit-include-thread
1957   "d" gnus-summary-limit-exclude-dormant
1958   "t" gnus-summary-limit-to-age
1959   "." gnus-summary-limit-to-unseen
1960   "x" gnus-summary-limit-to-extra
1961   "p" gnus-summary-limit-to-display-predicate
1962   "E" gnus-summary-limit-include-expunged
1963   "c" gnus-summary-limit-exclude-childless-dormant
1964   "C" gnus-summary-limit-mark-excluded-as-read
1965   "o" gnus-summary-insert-old-articles
1966   "N" gnus-summary-insert-new-articles
1967   "S" gnus-summary-limit-to-singletons
1968   "r" gnus-summary-limit-to-replied
1969   "R" gnus-summary-limit-to-recipient
1970   "A" gnus-summary-limit-to-address)
1971
1972 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1973   "n" gnus-summary-next-unread-article
1974   "p" gnus-summary-prev-unread-article
1975   "N" gnus-summary-next-article
1976   "P" gnus-summary-prev-article
1977   "\C-n" gnus-summary-next-same-subject
1978   "\C-p" gnus-summary-prev-same-subject
1979   "\M-n" gnus-summary-next-unread-subject
1980   "\M-p" gnus-summary-prev-unread-subject
1981   "f" gnus-summary-first-unread-article
1982   "b" gnus-summary-best-unread-article
1983   "j" gnus-summary-goto-article
1984   "g" gnus-summary-goto-subject
1985   "l" gnus-summary-goto-last-article
1986   "o" gnus-summary-pop-article)
1987
1988 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1989   "k" gnus-summary-kill-thread
1990   "E" gnus-summary-expire-thread
1991   "l" gnus-summary-lower-thread
1992   "i" gnus-summary-raise-thread
1993   "T" gnus-summary-toggle-threads
1994   "t" gnus-summary-rethread-current
1995   "^" gnus-summary-reparent-thread
1996   "\M-^" gnus-summary-reparent-children
1997   "s" gnus-summary-show-thread
1998   "S" gnus-summary-show-all-threads
1999   "h" gnus-summary-hide-thread
2000   "H" gnus-summary-hide-all-threads
2001   "n" gnus-summary-next-thread
2002   "p" gnus-summary-prev-thread
2003   "u" gnus-summary-up-thread
2004   "o" gnus-summary-top-thread
2005   "d" gnus-summary-down-thread
2006   "#" gnus-uu-mark-thread
2007   "\M-#" gnus-uu-unmark-thread)
2008
2009 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2010   "g" gnus-summary-prepare
2011   "c" gnus-summary-insert-cached-articles
2012   "d" gnus-summary-insert-dormant-articles
2013   "t" gnus-summary-insert-ticked-articles)
2014
2015 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2016   "c" gnus-summary-catchup-and-exit
2017   "C" gnus-summary-catchup-all-and-exit
2018   "E" gnus-summary-exit-no-update
2019   "Q" gnus-summary-exit
2020   "Z" gnus-summary-exit
2021   "n" gnus-summary-catchup-and-goto-next-group
2022   "p" gnus-summary-catchup-and-goto-prev-group
2023   "R" gnus-summary-reselect-current-group
2024   "G" gnus-summary-rescan-group
2025   "N" gnus-summary-next-group
2026   "s" gnus-summary-save-newsrc
2027   "P" gnus-summary-prev-group)
2028
2029 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2030   " " gnus-summary-next-page
2031   "n" gnus-summary-next-page
2032   "\177" gnus-summary-prev-page
2033   [delete] gnus-summary-prev-page
2034   "p" gnus-summary-prev-page
2035   "\r" gnus-summary-scroll-up
2036   "\M-\r" gnus-summary-scroll-down
2037   "<" gnus-summary-beginning-of-article
2038   ">" gnus-summary-end-of-article
2039   "b" gnus-summary-beginning-of-article
2040   "e" gnus-summary-end-of-article
2041   "^" gnus-summary-refer-parent-article
2042   "r" gnus-summary-refer-parent-article
2043   "D" gnus-summary-enter-digest-group
2044   "R" gnus-summary-refer-references
2045   "T" gnus-summary-refer-thread
2046   "g" gnus-summary-show-article
2047   "s" gnus-summary-isearch-article
2048   "P" gnus-summary-print-article
2049   "S" gnus-sticky-article
2050   "M" gnus-mailing-list-insinuate
2051   "t" gnus-article-babel)
2052
2053 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2054   "b" gnus-article-add-buttons
2055   "B" gnus-article-add-buttons-to-head
2056   "o" gnus-article-treat-overstrike
2057   "e" gnus-article-emphasize
2058   "w" gnus-article-fill-cited-article
2059   "Q" gnus-article-fill-long-lines
2060   "L" gnus-article-toggle-truncate-lines
2061   "C" gnus-article-capitalize-sentences
2062   "c" gnus-article-remove-cr
2063   "q" gnus-article-de-quoted-unreadable
2064   "6" gnus-article-de-base64-unreadable
2065   "Z" gnus-article-decode-HZ
2066   "A" gnus-article-treat-ansi-sequences
2067   "h" gnus-article-wash-html
2068   "u" gnus-article-unsplit-urls
2069   "s" gnus-summary-force-verify-and-decrypt
2070   "f" gnus-article-display-x-face
2071   "l" gnus-summary-stop-page-breaking
2072   "r" gnus-summary-caesar-message
2073   "m" gnus-summary-morse-message
2074   "t" gnus-summary-toggle-header
2075   "g" gnus-treat-smiley
2076   "v" gnus-summary-verbose-headers
2077   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2078   "p" gnus-article-verify-x-pgp-sig
2079   "d" gnus-article-treat-dumbquotes
2080   "i" gnus-summary-idna-message)
2081
2082 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2083   ;; mnemonic: deuglif*Y*
2084   "u" gnus-article-outlook-unwrap-lines
2085   "a" gnus-article-outlook-repair-attribution
2086   "c" gnus-article-outlook-rearrange-citation
2087   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2088
2089 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2090   "a" gnus-article-hide
2091   "h" gnus-article-hide-headers
2092   "b" gnus-article-hide-boring-headers
2093   "s" gnus-article-hide-signature
2094   "c" gnus-article-hide-citation
2095   "C" gnus-article-hide-citation-in-followups
2096   "l" gnus-article-hide-list-identifiers
2097   "B" gnus-article-strip-banner
2098   "P" gnus-article-hide-pem
2099   "\C-c" gnus-article-hide-citation-maybe)
2100
2101 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2102   "a" gnus-article-highlight
2103   "h" gnus-article-highlight-headers
2104   "c" gnus-article-highlight-citation
2105   "s" gnus-article-highlight-signature)
2106
2107 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2108   "f" gnus-article-treat-fold-headers
2109   "u" gnus-article-treat-unfold-headers
2110   "n" gnus-article-treat-fold-newsgroups)
2111
2112 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2113   "x" gnus-article-display-x-face
2114   "d" gnus-article-display-face
2115   "s" gnus-treat-smiley
2116   "D" gnus-article-remove-images
2117   "f" gnus-treat-from-picon
2118   "m" gnus-treat-mail-picon
2119   "n" gnus-treat-newsgroups-picon)
2120
2121 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2122   "w" gnus-article-decode-mime-words
2123   "c" gnus-article-decode-charset
2124   "v" gnus-mime-view-all-parts
2125   "b" gnus-article-view-part)
2126
2127 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2128   "z" gnus-article-date-ut
2129   "u" gnus-article-date-ut
2130   "l" gnus-article-date-local
2131   "p" gnus-article-date-english
2132   "e" gnus-article-date-lapsed
2133   "o" gnus-article-date-original
2134   "i" gnus-article-date-iso8601
2135   "s" gnus-article-date-user)
2136
2137 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2138   "t" gnus-article-remove-trailing-blank-lines
2139   "l" gnus-article-strip-leading-blank-lines
2140   "m" gnus-article-strip-multiple-blank-lines
2141   "a" gnus-article-strip-blank-lines
2142   "A" gnus-article-strip-all-blank-lines
2143   "s" gnus-article-strip-leading-space
2144   "e" gnus-article-strip-trailing-space
2145   "w" gnus-article-remove-leading-whitespace)
2146
2147 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2148   "v" gnus-version
2149   "f" gnus-summary-fetch-faq
2150   "d" gnus-summary-describe-group
2151   "h" gnus-summary-describe-briefly
2152   "i" gnus-info-find-node
2153   "c" gnus-group-fetch-charter
2154   "C" gnus-group-fetch-control)
2155
2156 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2157   "e" gnus-summary-expire-articles
2158   "\M-\C-e" gnus-summary-expire-articles-now
2159   "\177" gnus-summary-delete-article
2160   [delete] gnus-summary-delete-article
2161   [backspace] gnus-summary-delete-article
2162   "m" gnus-summary-move-article
2163   "r" gnus-summary-respool-article
2164   "w" gnus-summary-edit-article
2165   "c" gnus-summary-copy-article
2166   "B" gnus-summary-crosspost-article
2167   "q" gnus-summary-respool-query
2168   "t" gnus-summary-respool-trace
2169   "i" gnus-summary-import-article
2170   "I" gnus-summary-create-article
2171   "p" gnus-summary-article-posted-p)
2172
2173 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2174   "o" gnus-summary-save-article
2175   "m" gnus-summary-save-article-mail
2176   "F" gnus-summary-write-article-file
2177   "r" gnus-summary-save-article-rmail
2178   "f" gnus-summary-save-article-file
2179   "b" gnus-summary-save-article-body-file
2180   "B" gnus-summary-write-article-body-file
2181   "h" gnus-summary-save-article-folder
2182   "v" gnus-summary-save-article-vm
2183   "p" gnus-summary-pipe-output
2184   "P" gnus-summary-muttprint
2185   "s" gnus-soup-add-article)
2186
2187 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2188   "b" gnus-summary-display-buttonized
2189   "m" gnus-summary-repair-multipart
2190   "v" gnus-article-view-part
2191   "o" gnus-article-save-part
2192   "O" gnus-article-save-part-and-strip
2193   "r" gnus-article-replace-part
2194   "d" gnus-article-delete-part
2195   "t" gnus-article-view-part-as-type
2196   "j" gnus-article-jump-to-part
2197   "c" gnus-article-copy-part
2198   "C" gnus-article-view-part-as-charset
2199   "e" gnus-article-view-part-externally
2200   "H" gnus-article-browse-html-article
2201   "E" gnus-article-encrypt-body
2202   "i" gnus-article-inline-part
2203   "|" gnus-article-pipe-part)
2204
2205 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2206   "p" gnus-summary-mark-as-processable
2207   "u" gnus-summary-unmark-as-processable
2208   "U" gnus-summary-unmark-all-processable
2209   "v" gnus-uu-mark-over
2210   "s" gnus-uu-mark-series
2211   "r" gnus-uu-mark-region
2212   "g" gnus-uu-unmark-region
2213   "R" gnus-uu-mark-by-regexp
2214   "G" gnus-uu-unmark-by-regexp
2215   "t" gnus-uu-mark-thread
2216   "T" gnus-uu-unmark-thread
2217   "a" gnus-uu-mark-all
2218   "b" gnus-uu-mark-buffer
2219   "S" gnus-uu-mark-sparse
2220   "k" gnus-summary-kill-process-mark
2221   "y" gnus-summary-yank-process-mark
2222   "w" gnus-summary-save-process-mark
2223   "i" gnus-uu-invert-processable)
2224
2225 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2226   ;;"x" gnus-uu-extract-any
2227   "m" gnus-summary-save-parts
2228   "u" gnus-uu-decode-uu
2229   "U" gnus-uu-decode-uu-and-save
2230   "s" gnus-uu-decode-unshar
2231   "S" gnus-uu-decode-unshar-and-save
2232   "o" gnus-uu-decode-save
2233   "O" gnus-uu-decode-save
2234   "b" gnus-uu-decode-binhex
2235   "B" gnus-uu-decode-binhex
2236   "Y" gnus-uu-decode-yenc
2237   "p" gnus-uu-decode-postscript
2238   "P" gnus-uu-decode-postscript-and-save)
2239
2240 (gnus-define-keys
2241     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2242   "u" gnus-uu-decode-uu-view
2243   "U" gnus-uu-decode-uu-and-save-view
2244   "s" gnus-uu-decode-unshar-view
2245   "S" gnus-uu-decode-unshar-and-save-view
2246   "o" gnus-uu-decode-save-view
2247   "O" gnus-uu-decode-save-view
2248   "b" gnus-uu-decode-binhex-view
2249   "B" gnus-uu-decode-binhex-view
2250   "p" gnus-uu-decode-postscript-view
2251   "P" gnus-uu-decode-postscript-and-save-view)
2252
2253 (defvar gnus-article-post-menu nil)
2254
2255 (defconst gnus-summary-menu-maxlen 20)
2256
2257 (defun gnus-summary-menu-split (menu)
2258   ;; If we have lots of elements, divide them into groups of 20
2259   ;; and make a pane (or submenu) for each one.
2260   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2261       (let ((menu menu) sublists next
2262             (i 1))
2263         (while menu
2264           ;; Pull off the next gnus-summary-menu-maxlen elements
2265           ;; and make them the next element of sublist.
2266           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2267           (if next
2268               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2269                       nil))
2270           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2271                                              (aref (car (last menu)) 0)) menu)
2272                                sublists))
2273           (setq i (1+ i))
2274           (setq menu next))
2275         (nreverse sublists))
2276     ;; Few elements--put them all in one pane.
2277     menu))
2278
2279 (defun gnus-summary-make-menu-bar ()
2280   (gnus-turn-off-edit-menu 'summary)
2281
2282   (unless (boundp 'gnus-summary-misc-menu)
2283
2284     (easy-menu-define
2285       gnus-summary-kill-menu gnus-summary-mode-map ""
2286       (cons
2287        "Score"
2288        (nconc
2289         (list
2290          ["Customize" gnus-score-customize t])
2291         (gnus-make-score-map 'increase)
2292         (gnus-make-score-map 'lower)
2293         '(("Mark"
2294            ["Kill below" gnus-summary-kill-below t]
2295            ["Mark above" gnus-summary-mark-above t]
2296            ["Tick above" gnus-summary-tick-above t]
2297            ["Clear above" gnus-summary-clear-above t])
2298           ["Current score" gnus-summary-current-score t]
2299           ["Set score" gnus-summary-set-score t]
2300           ["Switch current score file..." gnus-score-change-score-file t]
2301           ["Set mark below..." gnus-score-set-mark-below t]
2302           ["Set expunge below..." gnus-score-set-expunge-below t]
2303           ["Edit current score file" gnus-score-edit-current-scores t]
2304           ["Edit score file..." gnus-score-edit-file t]
2305           ["Trace score" gnus-score-find-trace t]
2306           ["Find words" gnus-score-find-favourite-words t]
2307           ["Rescore buffer" gnus-summary-rescore t]
2308           ["Increase score..." gnus-summary-increase-score t]
2309           ["Lower score..." gnus-summary-lower-score t]))))
2310
2311     ;; Define both the Article menu in the summary buffer and the
2312     ;; equivalent Commands menu in the article buffer here for
2313     ;; consistency.
2314     (let ((innards
2315            `(("Hide"
2316               ["All" gnus-article-hide t]
2317               ["Headers" gnus-article-hide-headers t]
2318               ["Signature" gnus-article-hide-signature t]
2319               ["Citation" gnus-article-hide-citation t]
2320               ["List identifiers" gnus-article-hide-list-identifiers t]
2321               ["Banner" gnus-article-strip-banner t]
2322               ["Boring headers" gnus-article-hide-boring-headers t])
2323              ("Highlight"
2324               ["All" gnus-article-highlight t]
2325               ["Headers" gnus-article-highlight-headers t]
2326               ["Signature" gnus-article-highlight-signature t]
2327               ["Citation" gnus-article-highlight-citation t])
2328              ("MIME"
2329               ["Words" gnus-article-decode-mime-words t]
2330               ["Charset" gnus-article-decode-charset t]
2331               ["QP" gnus-article-de-quoted-unreadable t]
2332               ["Base64" gnus-article-de-base64-unreadable t]
2333               ["View MIME buttons" gnus-summary-display-buttonized t]
2334               ["View all" gnus-mime-view-all-parts t]
2335               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2336               ["Encrypt body" gnus-article-encrypt-body
2337                :active (not (gnus-group-read-only-p))
2338                ,@(if (featurep 'xemacs) nil
2339                    '(:help "Encrypt the message body on disk"))]
2340               ["Extract all parts..." gnus-summary-save-parts t]
2341               ("Multipart"
2342                ["Repair multipart" gnus-summary-repair-multipart t]
2343                ["Pipe part..." gnus-article-pipe-part t]
2344                ["Inline part" gnus-article-inline-part t]
2345                ["View part as type..." gnus-article-view-part-as-type t]
2346                ["Encrypt body" gnus-article-encrypt-body
2347                 :active (not (gnus-group-read-only-p))
2348                ,@(if (featurep 'xemacs) nil
2349                    '(:help "Encrypt the message body on disk"))]
2350                ["View part externally" gnus-article-view-part-externally t]
2351                ["View HTML parts in browser" gnus-article-browse-html-article t]
2352                ["View part with charset..." gnus-article-view-part-as-charset t]
2353                ["Copy part" gnus-article-copy-part t]
2354                ["Save part..." gnus-article-save-part t]
2355                ["View part" gnus-article-view-part t]))
2356              ("Date"
2357               ["Local" gnus-article-date-local t]
2358               ["ISO8601" gnus-article-date-iso8601 t]
2359               ["UT" gnus-article-date-ut t]
2360               ["Original" gnus-article-date-original t]
2361               ["Lapsed" gnus-article-date-lapsed t]
2362               ["User-defined" gnus-article-date-user t])
2363              ("Display"
2364               ["Remove images" gnus-article-remove-images t]
2365               ["Toggle smiley" gnus-treat-smiley t]
2366               ["Show X-Face" gnus-article-display-x-face t]
2367               ["Show picons in From" gnus-treat-from-picon t]
2368               ["Show picons in mail headers" gnus-treat-mail-picon t]
2369               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2370               ("View as different encoding"
2371                ,@(gnus-summary-menu-split
2372                   (mapcar
2373                    (lambda (cs)
2374                      ;; Since easymenu under Emacs doesn't allow
2375                      ;; lambda forms for menu commands, we should
2376                      ;; provide intern'ed function symbols.
2377                      (let ((command (intern (format "\
2378 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2379                        (fset command
2380                              `(lambda ()
2381                                 (interactive)
2382                                 (let ((gnus-summary-show-article-charset-alist
2383                                        '((1 . ,cs))))
2384                                   (gnus-summary-show-article 1))))
2385                        `[,(symbol-name cs) ,command t]))
2386                    (sort (if (fboundp 'coding-system-list)
2387                              (coding-system-list)
2388                            (mapcar 'car mm-mime-mule-charset-alist))
2389                          'string<)))))
2390              ("Washing"
2391               ("Remove Blanks"
2392                ["Leading" gnus-article-strip-leading-blank-lines t]
2393                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2394                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2395                ["All of the above" gnus-article-strip-blank-lines t]
2396                ["All" gnus-article-strip-all-blank-lines t]
2397                ["Leading space" gnus-article-strip-leading-space t]
2398                ["Trailing space" gnus-article-strip-trailing-space t]
2399                ["Leading space in headers"
2400                 gnus-article-remove-leading-whitespace t])
2401               ["Overstrike" gnus-article-treat-overstrike t]
2402               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2403               ["Emphasis" gnus-article-emphasize t]
2404               ["Word wrap" gnus-article-fill-cited-article t]
2405               ["Fill long lines" gnus-article-fill-long-lines t]
2406               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2407               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2408               ["Remove CR" gnus-article-remove-cr t]
2409               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2410               ["Base64" gnus-article-de-base64-unreadable t]
2411               ["Rot 13" gnus-summary-caesar-message
2412                ,@(if (featurep 'xemacs) '(t)
2413                    '(:help "\"Caesar rotate\" article by 13"))]
2414               ["De-IDNA" gnus-summary-idna-message t]
2415               ["Morse decode" gnus-summary-morse-message t]
2416               ["Unix pipe..." gnus-summary-pipe-message t]
2417               ["Add buttons" gnus-article-add-buttons t]
2418               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2419               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2420               ["Verbose header" gnus-summary-verbose-headers t]
2421               ["Toggle header" gnus-summary-toggle-header t]
2422               ["Unfold headers" gnus-article-treat-unfold-headers t]
2423               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2424               ["Html" gnus-article-wash-html t]
2425               ["Unsplit URLs" gnus-article-unsplit-urls t]
2426               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2427               ["Decode HZ" gnus-article-decode-HZ t]
2428               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2429               ("(Outlook) Deuglify"
2430                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2431                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2432                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2433                ["Full (Outlook) deuglify"
2434                 gnus-article-outlook-deuglify-article t])
2435               )
2436              ("Output"
2437               ["Save in default format..." gnus-summary-save-article
2438                ,@(if (featurep 'xemacs) '(t)
2439                    '(:help "Save article using default method"))]
2440               ["Save in file..." gnus-summary-save-article-file
2441                ,@(if (featurep 'xemacs) '(t)
2442                    '(:help "Save article in file"))]
2443               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2444               ["Save in MH folder..." gnus-summary-save-article-folder t]
2445               ["Save in VM folder..." gnus-summary-save-article-vm t]
2446               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2447               ["Save body in file..." gnus-summary-save-article-body-file t]
2448               ["Pipe through a filter..." gnus-summary-pipe-output t]
2449               ["Add to SOUP packet" gnus-soup-add-article t]
2450               ["Print with Muttprint..." gnus-summary-muttprint t]
2451               ["Print" gnus-summary-print-article
2452                ,@(if (featurep 'xemacs) '(t)
2453                    '(:help "Generate and print a PostScript image"))])
2454              ("Copy, move,... (Backend)"
2455               ,@(if (featurep 'xemacs) nil
2456                   '(:help "Copying, moving, expiring articles..."))
2457               ["Respool article..." gnus-summary-respool-article t]
2458               ["Move article..." gnus-summary-move-article
2459                (gnus-check-backend-function
2460                 'request-move-article gnus-newsgroup-name)]
2461               ["Copy article..." gnus-summary-copy-article t]
2462               ["Crosspost article..." gnus-summary-crosspost-article
2463                (gnus-check-backend-function
2464                 'request-replace-article gnus-newsgroup-name)]
2465               ["Import file..." gnus-summary-import-article
2466                (gnus-check-backend-function
2467                 'request-accept-article gnus-newsgroup-name)]
2468               ["Create article..." gnus-summary-create-article
2469                (gnus-check-backend-function
2470                 'request-accept-article gnus-newsgroup-name)]
2471               ["Check if posted" gnus-summary-article-posted-p t]
2472               ["Edit article" gnus-summary-edit-article
2473                (not (gnus-group-read-only-p))]
2474               ["Delete article" gnus-summary-delete-article
2475                (gnus-check-backend-function
2476                 'request-expire-articles gnus-newsgroup-name)]
2477               ["Query respool" gnus-summary-respool-query t]
2478               ["Trace respool" gnus-summary-respool-trace t]
2479               ["Delete expirable articles" gnus-summary-expire-articles-now
2480                (gnus-check-backend-function
2481                 'request-expire-articles gnus-newsgroup-name)])
2482              ("Extract"
2483               ["Uudecode" gnus-uu-decode-uu
2484                ,@(if (featurep 'xemacs) '(t)
2485                    '(:help "Decode uuencoded article(s)"))]
2486               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2487               ["Unshar" gnus-uu-decode-unshar t]
2488               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2489               ["Save" gnus-uu-decode-save t]
2490               ["Binhex" gnus-uu-decode-binhex t]
2491               ["Postscript" gnus-uu-decode-postscript t]
2492               ["All MIME parts" gnus-summary-save-parts t])
2493              ("Cache"
2494               ["Enter article" gnus-cache-enter-article t]
2495               ["Remove article" gnus-cache-remove-article t])
2496              ["Translate" gnus-article-babel t]
2497              ["Select article buffer" gnus-summary-select-article-buffer t]
2498              ["Make article buffer sticky" gnus-sticky-article t]
2499              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2500              ["Isearch article..." gnus-summary-isearch-article t]
2501              ["Beginning of the article" gnus-summary-beginning-of-article t]
2502              ["End of the article" gnus-summary-end-of-article t]
2503              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2504              ["Fetch referenced articles" gnus-summary-refer-references t]
2505              ["Fetch current thread" gnus-summary-refer-thread t]
2506              ["Fetch article with id..." gnus-summary-refer-article t]
2507              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2508              ["Redisplay" gnus-summary-show-article t]
2509              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2510       (easy-menu-define
2511         gnus-summary-article-menu gnus-summary-mode-map ""
2512         (cons "Article" innards))
2513
2514       (if (not (keymapp gnus-summary-article-menu))
2515           (easy-menu-define
2516             gnus-article-commands-menu gnus-article-mode-map ""
2517             (cons "Commands" innards))
2518         ;; in Emacs, don't share menu.
2519         (setq gnus-article-commands-menu
2520               (copy-keymap gnus-summary-article-menu))
2521         (define-key gnus-article-mode-map [menu-bar commands]
2522           (cons "Commands" gnus-article-commands-menu))))
2523
2524     (easy-menu-define
2525       gnus-summary-thread-menu gnus-summary-mode-map ""
2526       '("Threads"
2527         ["Find all messages in thread" gnus-summary-refer-thread t]
2528         ["Toggle threading" gnus-summary-toggle-threads t]
2529         ["Hide threads" gnus-summary-hide-all-threads t]
2530         ["Show threads" gnus-summary-show-all-threads t]
2531         ["Hide thread" gnus-summary-hide-thread t]
2532         ["Show thread" gnus-summary-show-thread t]
2533         ["Go to next thread" gnus-summary-next-thread t]
2534         ["Go to previous thread" gnus-summary-prev-thread t]
2535         ["Go down thread" gnus-summary-down-thread t]
2536         ["Go up thread" gnus-summary-up-thread t]
2537         ["Top of thread" gnus-summary-top-thread t]
2538         ["Mark thread as read" gnus-summary-kill-thread t]
2539         ["Mark thread as expired" gnus-summary-expire-thread t]
2540         ["Lower thread score" gnus-summary-lower-thread t]
2541         ["Raise thread score" gnus-summary-raise-thread t]
2542         ["Rethread current" gnus-summary-rethread-current t]))
2543
2544     (easy-menu-define
2545       gnus-summary-post-menu gnus-summary-mode-map ""
2546       `("Post"
2547         ["Send a message (mail or news)" gnus-summary-post-news
2548          ,@(if (featurep 'xemacs) '(t)
2549              '(:help "Compose a new message (mail or news)"))]
2550         ["Followup" gnus-summary-followup
2551          ,@(if (featurep 'xemacs) '(t)
2552              '(:help "Post followup to this article"))]
2553         ["Followup and yank" gnus-summary-followup-with-original
2554          ,@(if (featurep 'xemacs) '(t)
2555              '(:help "Post followup to this article, quoting its contents"))]
2556         ["Supersede article" gnus-summary-supersede-article t]
2557         ["Cancel article" gnus-summary-cancel-article
2558          ,@(if (featurep 'xemacs) '(t)
2559              '(:help "Cancel an article you posted"))]
2560         ["Reply" gnus-summary-reply t]
2561         ["Reply and yank" gnus-summary-reply-with-original t]
2562         ["Wide reply" gnus-summary-wide-reply t]
2563         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2564          ,@(if (featurep 'xemacs) '(t)
2565              '(:help "Mail a reply, quoting this article"))]
2566         ["Very wide reply" gnus-summary-very-wide-reply t]
2567         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2568          ,@(if (featurep 'xemacs) '(t)
2569              '(:help "Mail a very wide reply, quoting this article"))]
2570         ["Mail forward" gnus-summary-mail-forward t]
2571         ["Post forward" gnus-summary-post-forward t]
2572         ["Digest and mail" gnus-uu-digest-mail-forward t]
2573         ["Digest and post" gnus-uu-digest-post-forward t]
2574         ["Resend message" gnus-summary-resend-message t]
2575         ["Resend message edit" gnus-summary-resend-message-edit t]
2576         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2577         ["Send a mail" gnus-summary-mail-other-window t]
2578         ["Create a local message" gnus-summary-news-other-window t]
2579         ["Uuencode and post" gnus-uu-post-news
2580          ,@(if (featurep 'xemacs) '(t)
2581              '(:help "Post a uuencoded article"))]
2582         ["Followup via news" gnus-summary-followup-to-mail t]
2583         ["Followup via news and yank"
2584          gnus-summary-followup-to-mail-with-original t]
2585         ["Strip signature on reply"
2586          (lambda ()
2587            (interactive)
2588            (if (not (memq message-cite-function
2589                           '(message-cite-original-without-signature
2590                             message-cite-original)))
2591                ;; Stupid workaround for XEmacs not honoring :visible.
2592                (message "Can't toggle this value of `message-cite-function'")
2593              (setq message-cite-function
2594                    (if (eq message-cite-function
2595                            'message-cite-original-without-signature)
2596                        'message-cite-original
2597                      'message-cite-original-without-signature))))
2598          ;; XEmacs barfs on :visible.
2599          ,@(if (featurep 'xemacs) nil
2600              '(:visible (memq message-cite-function
2601                               '(message-cite-original-without-signature
2602                                 message-cite-original))))
2603          :style toggle
2604          :selected (eq message-cite-function
2605                        'message-cite-original-without-signature)
2606          ,@(if (featurep 'xemacs) nil
2607              '(:help "Strip signature from cited article when replying."))]
2608         ;;("Draft"
2609         ;;["Send" gnus-summary-send-draft t]
2610         ;;["Send bounced" gnus-resend-bounced-mail t])
2611         ))
2612
2613     (cond
2614      ((not (keymapp gnus-summary-post-menu))
2615       (setq gnus-article-post-menu gnus-summary-post-menu))
2616      ((not gnus-article-post-menu)
2617       ;; Don't share post menu.
2618       (setq gnus-article-post-menu
2619             (copy-keymap gnus-summary-post-menu))))
2620     (define-key gnus-article-mode-map [menu-bar post]
2621       (cons "Post" gnus-article-post-menu))
2622
2623     (easy-menu-define
2624       gnus-summary-misc-menu gnus-summary-mode-map ""
2625       `("Gnus"
2626         ("Mark Read"
2627          ["Mark as read" gnus-summary-mark-as-read-forward t]
2628          ["Mark same subject and select"
2629           gnus-summary-kill-same-subject-and-select t]
2630          ["Mark same subject" gnus-summary-kill-same-subject t]
2631          ["Catchup" gnus-summary-catchup
2632           ,@(if (featurep 'xemacs) '(t)
2633               '(:help "Mark unread articles in this group as read"))]
2634          ["Catchup all" gnus-summary-catchup-all t]
2635          ["Catchup to here" gnus-summary-catchup-to-here t]
2636          ["Catchup from here" gnus-summary-catchup-from-here t]
2637          ["Catchup region" gnus-summary-mark-region-as-read
2638           (gnus-mark-active-p)]
2639          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2640         ("Mark Various"
2641          ["Tick" gnus-summary-tick-article-forward t]
2642          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2643          ["Remove marks" gnus-summary-clear-mark-forward t]
2644          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2645          ["Set bookmark" gnus-summary-set-bookmark t]
2646          ["Remove bookmark" gnus-summary-remove-bookmark t])
2647         ("Limit to"
2648          ["Marks..." gnus-summary-limit-to-marks t]
2649          ["Subject..." gnus-summary-limit-to-subject t]
2650          ["Author..." gnus-summary-limit-to-author t]
2651          ["Recipient..." gnus-summary-limit-to-recipient t]
2652          ["Address..." gnus-summary-limit-to-address t]
2653          ["Age..." gnus-summary-limit-to-age t]
2654          ["Extra..." gnus-summary-limit-to-extra t]
2655          ["Score..." gnus-summary-limit-to-score t]
2656          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2657          ["Unread" gnus-summary-limit-to-unread t]
2658          ["Unseen" gnus-summary-limit-to-unseen t]
2659          ["Singletons" gnus-summary-limit-to-singletons t]
2660          ["Replied" gnus-summary-limit-to-replied t]
2661          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2662          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2663          ["Pop limit" gnus-summary-pop-limit t]
2664          ["Show dormant" gnus-summary-limit-include-dormant t]
2665          ["Hide childless dormant"
2666           gnus-summary-limit-exclude-childless-dormant t]
2667          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2668          ["Hide marked" gnus-summary-limit-exclude-marks t]
2669          ["Show expunged" gnus-summary-limit-include-expunged t])
2670         ("Process Mark"
2671          ["Set mark" gnus-summary-mark-as-processable t]
2672          ["Remove mark" gnus-summary-unmark-as-processable t]
2673          ["Remove all marks" gnus-summary-unmark-all-processable t]
2674          ["Invert marks" gnus-uu-invert-processable t]
2675          ["Mark above" gnus-uu-mark-over t]
2676          ["Mark series" gnus-uu-mark-series t]
2677          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2678          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2679          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2680          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2681          ["Mark all" gnus-uu-mark-all t]
2682          ["Mark buffer" gnus-uu-mark-buffer t]
2683          ["Mark sparse" gnus-uu-mark-sparse t]
2684          ["Mark thread" gnus-uu-mark-thread t]
2685          ["Unmark thread" gnus-uu-unmark-thread t]
2686          ("Process Mark Sets"
2687           ["Kill" gnus-summary-kill-process-mark t]
2688           ["Yank" gnus-summary-yank-process-mark
2689            gnus-newsgroup-process-stack]
2690           ["Save" gnus-summary-save-process-mark t]
2691           ["Run command on marked..." gnus-summary-universal-argument t]))
2692         ("Registry Marks")
2693         ("Scroll article"
2694          ["Page forward" gnus-summary-next-page
2695           ,@(if (featurep 'xemacs) '(t)
2696               '(:help "Show next page of article"))]
2697          ["Page backward" gnus-summary-prev-page
2698           ,@(if (featurep 'xemacs) '(t)
2699               '(:help "Show previous page of article"))]
2700          ["Line forward" gnus-summary-scroll-up t])
2701         ("Move"
2702          ["Next unread article" gnus-summary-next-unread-article t]
2703          ["Previous unread article" gnus-summary-prev-unread-article t]
2704          ["Next article" gnus-summary-next-article t]
2705          ["Previous article" gnus-summary-prev-article t]
2706          ["Next unread subject" gnus-summary-next-unread-subject t]
2707          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2708          ["Next article same subject" gnus-summary-next-same-subject t]
2709          ["Previous article same subject" gnus-summary-prev-same-subject t]
2710          ["First unread article" gnus-summary-first-unread-article t]
2711          ["Best unread article" gnus-summary-best-unread-article t]
2712          ["Go to subject number..." gnus-summary-goto-subject t]
2713          ["Go to article number..." gnus-summary-goto-article t]
2714          ["Go to the last article" gnus-summary-goto-last-article t]
2715          ["Pop article off history" gnus-summary-pop-article t])
2716         ("Sort"
2717          ["Sort by number" gnus-summary-sort-by-number t]
2718          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2719          ["Sort by author" gnus-summary-sort-by-author t]
2720          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2721          ["Sort by subject" gnus-summary-sort-by-subject t]
2722          ["Sort by date" gnus-summary-sort-by-date t]
2723          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2724          ["Sort by score" gnus-summary-sort-by-score t]
2725          ["Sort by lines" gnus-summary-sort-by-lines t]
2726          ["Sort by characters" gnus-summary-sort-by-chars t]
2727          ["Randomize" gnus-summary-sort-by-random t]
2728          ["Original sort" gnus-summary-sort-by-original t])
2729         ("Help"
2730          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2731          ["Describe group" gnus-summary-describe-group t]
2732          ["Fetch charter" gnus-group-fetch-charter
2733           ,@(if (featurep 'xemacs) nil
2734               '(:help "Display the charter of the current group"))]
2735          ["Fetch control message" gnus-group-fetch-control
2736           ,@(if (featurep 'xemacs) nil
2737               '(:help "Display the archived control message for the current group"))]
2738          ["Read manual" gnus-info-find-node t])
2739         ("Modes"
2740          ["Pick and read" gnus-pick-mode t]
2741          ["Binary" gnus-binary-mode t])
2742         ("Regeneration"
2743          ["Regenerate" gnus-summary-prepare t]
2744          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2745          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2746          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2747          ["Toggle threading" gnus-summary-toggle-threads t])
2748         ["See old articles" gnus-summary-insert-old-articles t]
2749         ["See new articles" gnus-summary-insert-new-articles t]
2750         ["Filter articles..." gnus-summary-execute-command t]
2751         ["Run command on articles..." gnus-summary-universal-argument t]
2752         ["Search articles forward..." gnus-summary-search-article-forward t]
2753         ["Search articles backward..." gnus-summary-search-article-backward t]
2754         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2755         ["Expand window" gnus-summary-expand-window t]
2756         ["Expire expirable articles" gnus-summary-expire-articles
2757          (gnus-check-backend-function
2758           'request-expire-articles gnus-newsgroup-name)]
2759         ["Edit local kill file" gnus-summary-edit-local-kill t]
2760         ["Edit main kill file" gnus-summary-edit-global-kill t]
2761         ["Edit group parameters" gnus-summary-edit-parameters t]
2762         ["Customize group parameters" gnus-summary-customize-parameters t]
2763         ["Send a bug report" gnus-bug t]
2764         ("Exit"
2765          ["Catchup and exit" gnus-summary-catchup-and-exit
2766           ,@(if (featurep 'xemacs) '(t)
2767               '(:help "Mark unread articles in this group as read, then exit"))]
2768          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2769          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2770          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2771          ["Exit group" gnus-summary-exit
2772           ,@(if (featurep 'xemacs) '(t)
2773               '(:help "Exit current group, return to group selection mode"))]
2774          ["Exit group without updating" gnus-summary-exit-no-update t]
2775          ["Exit and goto next group" gnus-summary-next-group t]
2776          ["Exit and goto prev group" gnus-summary-prev-group t]
2777          ["Reselect group" gnus-summary-reselect-current-group t]
2778          ["Rescan group" gnus-summary-rescan-group t]
2779          ["Update dribble" gnus-summary-save-newsrc t])))
2780
2781     (gnus-run-hooks 'gnus-summary-menu-hook)))
2782
2783 (defvar gnus-summary-tool-bar-map nil)
2784
2785 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2786 ;; affect _new_ message buffers.  We might add a function that walks thru all
2787 ;; summary-mode buffers and force the update.
2788 (defun gnus-summary-tool-bar-update (&optional symbol value)
2789   "Update summary mode toolbar.
2790 Setter function for custom variables."
2791   (setq-default gnus-summary-tool-bar-map nil)
2792   (when symbol
2793     ;; When used as ":set" function:
2794     (set-default symbol value))
2795   (when (gnus-buffer-live-p gnus-summary-buffer)
2796     (with-current-buffer gnus-summary-buffer
2797       (gnus-summary-make-tool-bar))))
2798
2799 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2800                                      'gnus-summary-tool-bar-gnome
2801                                    'gnus-summary-tool-bar-retro)
2802   "Specifies the Gnus summary tool bar.
2803
2804 It can be either a list or a symbol refering to a list.  See
2805 `gmm-tool-bar-from-list' for the format of the list.  The
2806 default key map is `gnus-summary-mode-map'.
2807
2808 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2809 `gnus-summary-tool-bar-retro'."
2810   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2811                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2812                  (repeat :tag "User defined list" gmm-tool-bar-item)
2813                  (symbol))
2814   :version "23.1" ;; No Gnus
2815   :initialize 'custom-initialize-default
2816   :set 'gnus-summary-tool-bar-update
2817   :group 'gnus-summary)
2818
2819 (defcustom gnus-summary-tool-bar-gnome
2820   '((gnus-summary-post-news "mail/compose" nil)
2821     (gnus-summary-insert-new-articles "mail/inbox" nil
2822                                       :visible (or (not gnus-agent)
2823                                                    gnus-plugged))
2824     (gnus-summary-reply-with-original "mail/reply")
2825     (gnus-summary-reply "mail/reply" nil :visible nil)
2826     (gnus-summary-followup-with-original "mail/reply-all")
2827     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2828     (gnus-summary-mail-forward "mail/forward")
2829     (gnus-summary-save-article "mail/save")
2830     (gnus-summary-search-article-forward "search" nil :visible nil)
2831     (gnus-summary-print-article "print")
2832     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2833     ;; Some new commands that may need more suitable icons:
2834     (gnus-summary-save-newsrc "save" nil :visible nil)
2835     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2836     (gnus-summary-prev-article "left-arrow")
2837     (gnus-summary-next-article "right-arrow")
2838     (gnus-summary-next-page "next-page")
2839     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2840     ;;
2841     ;; Maybe some sort-by-... could be added:
2842     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2843     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2844     (gnus-summary-mark-as-expirable
2845      "delete" nil
2846      :visible (gnus-check-backend-function 'request-expire-articles
2847                                            gnus-newsgroup-name))
2848     (gnus-summary-mark-as-spam
2849      "mail/spam" t
2850      :visible (and (fboundp 'spam-group-ham-contents-p)
2851                    (spam-group-ham-contents-p gnus-newsgroup-name))
2852      :help "Mark as spam")
2853     (gnus-summary-mark-as-read-forward
2854      "mail/not-spam" nil
2855      :visible (and (fboundp 'spam-group-spam-contents-p)
2856                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2857     ;;
2858     (gnus-summary-exit "exit")
2859     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2860     (gnus-info-find-node "help"))
2861   "List of functions for the summary tool bar (GNOME style).
2862
2863 See `gmm-tool-bar-from-list' for the format of the list."
2864   :type '(repeat gmm-tool-bar-item)
2865   :version "23.1" ;; No Gnus
2866   :initialize 'custom-initialize-default
2867   :set 'gnus-summary-tool-bar-update
2868   :group 'gnus-summary)
2869
2870 (defcustom gnus-summary-tool-bar-retro
2871   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2872     (gnus-summary-next-unread-article "gnus/next-ur")
2873     (gnus-summary-post-news "gnus/post")
2874     (gnus-summary-followup-with-original "gnus/fuwo")
2875     (gnus-summary-followup "gnus/followup")
2876     (gnus-summary-reply-with-original "gnus/reply-wo")
2877     (gnus-summary-reply "gnus/reply")
2878     (gnus-summary-caesar-message "gnus/rot13")
2879     (gnus-uu-decode-uu "gnus/uu-decode")
2880     (gnus-summary-save-article-file "gnus/save-aif")
2881     (gnus-summary-save-article "gnus/save-art")
2882     (gnus-uu-post-news "gnus/uu-post")
2883     (gnus-summary-catchup "gnus/catchup")
2884     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2885     (gnus-summary-exit "gnus/exit-summ")
2886     ;; Some new command that may need more suitable icons:
2887     (gnus-summary-print-article "gnus/print" nil :visible nil)
2888     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2889     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2890     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2891     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2892     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2893     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2894     ;;
2895     (gnus-info-find-node "gnus/help" nil :visible nil))
2896   "List of functions for the summary tool bar (retro look).
2897
2898 See `gmm-tool-bar-from-list' for the format of the list."
2899   :type '(repeat gmm-tool-bar-item)
2900   :version "23.1" ;; No Gnus
2901   :initialize 'custom-initialize-default
2902   :set 'gnus-summary-tool-bar-update
2903   :group 'gnus-summary)
2904
2905 (defcustom gnus-summary-tool-bar-zap-list t
2906   "List of icon items from the global tool bar.
2907 These items are not displayed in the Gnus summary mode tool bar.
2908
2909 See `gmm-tool-bar-from-list' for the format of the list."
2910   :type 'gmm-tool-bar-zap-list
2911   :version "23.1" ;; No Gnus
2912   :initialize 'custom-initialize-default
2913   :set 'gnus-summary-tool-bar-update
2914   :group 'gnus-summary)
2915
2916 (defvar image-load-path)
2917 (defvar tool-bar-map)
2918
2919 (defun gnus-summary-make-tool-bar (&optional force)
2920   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2921 When FORCE, rebuild the tool bar."
2922   (when (and (not (featurep 'xemacs))
2923              (boundp 'tool-bar-mode)
2924              tool-bar-mode
2925              (or (not gnus-summary-tool-bar-map) force))
2926     (let* ((load-path
2927             (gmm-image-load-path-for-library "gnus"
2928                                              "mail/save.xpm"
2929                                              nil t))
2930            (image-load-path (cons (car load-path)
2931                                   (when (boundp 'image-load-path)
2932                                     image-load-path)))
2933            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2934                                         gnus-summary-tool-bar-zap-list
2935                                         'gnus-summary-mode-map)))
2936       (when map
2937         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2938         ;; uses it's value.
2939         (setq gnus-summary-tool-bar-map map))))
2940   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2941
2942 (defun gnus-score-set-default (var value)
2943   "A version of set that updates the GNU Emacs menu-bar."
2944   (set var value)
2945   ;; It is the message that forces the active status to be updated.
2946   (message ""))
2947
2948 (defun gnus-make-score-map (type)
2949   "Make a summary score map of type TYPE."
2950   (if t
2951       nil
2952     (let ((headers '(("author" "from" string)
2953                      ("subject" "subject" string)
2954                      ("article body" "body" string)
2955                      ("article head" "head" string)
2956                      ("xref" "xref" string)
2957                      ("extra header" "extra" string)
2958                      ("lines" "lines" number)
2959                      ("followups to author" "followup" string)))
2960           (types '((number ("less than" <)
2961                            ("greater than" >)
2962                            ("equal" =))
2963                    (string ("substring" s)
2964                            ("exact string" e)
2965                            ("fuzzy string" f)
2966                            ("regexp" r))))
2967           (perms '(("temporary" (current-time-string))
2968                    ("permanent" nil)
2969                    ("immediate" now)))
2970           header)
2971       (list
2972        (apply
2973         'nconc
2974         (list
2975          (if (eq type 'lower)
2976              "Lower score"
2977            "Increase score"))
2978         (let (outh)
2979           (while headers
2980             (setq header (car headers))
2981             (setq outh
2982                   (cons
2983                    (apply
2984                     'nconc
2985                     (list (car header))
2986                     (let ((ts (cdr (assoc (nth 2 header) types)))
2987                           outt)
2988                       (while ts
2989                         (setq outt
2990                               (cons
2991                                (apply
2992                                 'nconc
2993                                 (list (caar ts))
2994                                 (let ((ps perms)
2995                                       outp)
2996                                   (while ps
2997                                     (setq outp
2998                                           (cons
2999                                            (vector
3000                                             (caar ps)
3001                                             (list
3002                                              'gnus-summary-score-entry
3003                                              (nth 1 header)
3004                                              (if (or (string= (nth 1 header)
3005                                                               "head")
3006                                                      (string= (nth 1 header)
3007                                                               "body"))
3008                                                  ""
3009                                                (list 'gnus-summary-header
3010                                                      (nth 1 header)))
3011                                              (list 'quote (nth 1 (car ts)))
3012                                              (list 'gnus-score-delta-default
3013                                                    nil)
3014                                              (nth 1 (car ps))
3015                                              t)
3016                                             t)
3017                                            outp))
3018                                     (setq ps (cdr ps)))
3019                                   (list (nreverse outp))))
3020                                outt))
3021                         (setq ts (cdr ts)))
3022                       (list (nreverse outt))))
3023                    outh))
3024             (setq headers (cdr headers)))
3025           (list (nreverse outh))))))))
3026
3027
3028 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3029 (defvar bookmark-make-record-function)
3030 \f
3031
3032 (defun gnus-summary-mode (&optional group)
3033   "Major mode for reading articles.
3034
3035 All normal editing commands are switched off.
3036 \\<gnus-summary-mode-map>
3037 Each line in this buffer represents one article.  To read an
3038 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3039 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3040 respectively.
3041
3042 You can also post articles and send mail from this buffer.  To
3043 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3044 of an article, type `\\[gnus-summary-reply]'.
3045
3046 There are approx. one gazillion commands you can execute in this
3047 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3048
3049 The following commands are available:
3050
3051 \\{gnus-summary-mode-map}"
3052   (interactive)
3053   (kill-all-local-variables)
3054   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3055     (gnus-summary-make-local-variables))
3056   (gnus-summary-make-local-variables)
3057   (setq gnus-newsgroup-name group)
3058   (when (gnus-visual-p 'summary-menu 'menu)
3059     (gnus-summary-make-menu-bar)
3060     (gnus-summary-make-tool-bar))
3061   (gnus-make-thread-indent-array)
3062   (gnus-simplify-mode-line)
3063   (setq major-mode 'gnus-summary-mode)
3064   (setq mode-name "Summary")
3065   (use-local-map gnus-summary-mode-map)
3066   (buffer-disable-undo)
3067   (setq buffer-read-only t              ;Disable modification
3068         show-trailing-whitespace nil)
3069   (setq truncate-lines t)
3070   (add-to-invisibility-spec '(gnus-sum . t))
3071   (gnus-summary-set-display-table)
3072   (gnus-set-default-directory)
3073   (make-local-variable 'gnus-summary-line-format)
3074   (make-local-variable 'gnus-summary-line-format-spec)
3075   (make-local-variable 'gnus-summary-dummy-line-format)
3076   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3077   (make-local-variable 'gnus-summary-mark-positions)
3078   (gnus-make-local-hook 'pre-command-hook)
3079   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3080   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3081   (turn-on-gnus-mailing-list-mode)
3082   (mm-enable-multibyte)
3083   (set (make-local-variable 'bookmark-make-record-function)
3084        'gnus-summary-bookmark-make-record)
3085   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3086   (gnus-update-summary-mark-positions))
3087
3088 (defun gnus-summary-make-local-variables ()
3089   "Make all the local summary buffer variables."
3090   (let (global)
3091     (dolist (local gnus-summary-local-variables)
3092       (if (consp local)
3093           (progn
3094             (if (eq (cdr local) 'global)
3095                 ;; Copy the global value of the variable.
3096                 (setq global (symbol-value (car local)))
3097               ;; Use the value from the list.
3098               (setq global (eval (cdr local))))
3099             (set (make-local-variable (car local)) global))
3100         ;; Simple nil-valued local variable.
3101         (set (make-local-variable local) nil)))))
3102
3103 (defun gnus-summary-clear-local-variables ()
3104   (let ((locals gnus-summary-local-variables))
3105     (while locals
3106       (if (consp (car locals))
3107           (and (symbolp (caar locals))
3108                (set (caar locals) nil))
3109         (and (symbolp (car locals))
3110              (set (car locals) nil)))
3111       (setq locals (cdr locals)))))
3112
3113 ;; Summary data functions.
3114
3115 (defmacro gnus-data-number (data)
3116   `(car ,data))
3117
3118 (defmacro gnus-data-set-number (data number)
3119   `(setcar ,data ,number))
3120
3121 (defmacro gnus-data-mark (data)
3122   `(nth 1 ,data))
3123
3124 (defmacro gnus-data-set-mark (data mark)
3125   `(setcar (nthcdr 1 ,data) ,mark))
3126
3127 (defmacro gnus-data-pos (data)
3128   `(nth 2 ,data))
3129
3130 (defmacro gnus-data-set-pos (data pos)
3131   `(setcar (nthcdr 2 ,data) ,pos))
3132
3133 (defmacro gnus-data-header (data)
3134   `(nth 3 ,data))
3135
3136 (defmacro gnus-data-set-header (data header)
3137   `(setf (nth 3 ,data) ,header))
3138
3139 (defmacro gnus-data-level (data)
3140   `(nth 4 ,data))
3141
3142 (defmacro gnus-data-unread-p (data)
3143   `(= (nth 1 ,data) gnus-unread-mark))
3144
3145 (defmacro gnus-data-read-p (data)
3146   `(/= (nth 1 ,data) gnus-unread-mark))
3147
3148 (defmacro gnus-data-pseudo-p (data)
3149   `(consp (nth 3 ,data)))
3150
3151 (defmacro gnus-data-find (number)
3152   `(assq ,number gnus-newsgroup-data))
3153
3154 (defmacro gnus-data-find-list (number &optional data)
3155   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3156      (memq (assq ,number bdata)
3157            bdata)))
3158
3159 (defmacro gnus-data-make (number mark pos header level)
3160   `(list ,number ,mark ,pos ,header ,level))
3161
3162 (defun gnus-data-enter (after-article number mark pos header level offset)
3163   (let ((data (gnus-data-find-list after-article)))
3164     (unless data
3165       (error "No such article: %d" after-article))
3166     (setcdr data (cons (gnus-data-make number mark pos header level)
3167                        (cdr data)))
3168     (setq gnus-newsgroup-data-reverse nil)
3169     (gnus-data-update-list (cddr data) offset)))
3170
3171 (defun gnus-data-enter-list (after-article list &optional offset)
3172   (when list
3173     (let ((data (and after-article (gnus-data-find-list after-article)))
3174           (ilist list))
3175       (if (not (or data
3176                    after-article))
3177           (let ((odata gnus-newsgroup-data))
3178             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3179             (when offset
3180               (gnus-data-update-list odata offset)))
3181         ;; Find the last element in the list to be spliced into the main
3182         ;; list.
3183         (setq list (last list))
3184         (if (not data)
3185             (progn
3186               (setcdr list gnus-newsgroup-data)
3187               (setq gnus-newsgroup-data ilist)
3188               (when offset
3189                 (gnus-data-update-list (cdr list) offset)))
3190           (setcdr list (cdr data))
3191           (setcdr data ilist)
3192           (when offset
3193             (gnus-data-update-list (cdr list) offset))))
3194       (setq gnus-newsgroup-data-reverse nil))))
3195
3196 (defun gnus-data-remove (article &optional offset)
3197   (let ((data gnus-newsgroup-data))
3198     (if (= (gnus-data-number (car data)) article)
3199         (progn
3200           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3201                 gnus-newsgroup-data-reverse nil)
3202           (when offset
3203             (gnus-data-update-list gnus-newsgroup-data offset)))
3204       (while (cdr data)
3205         (when (= (gnus-data-number (cadr data)) article)
3206           (setcdr data (cddr data))
3207           (when offset
3208             (gnus-data-update-list (cdr data) offset))
3209           (setq data nil
3210                 gnus-newsgroup-data-reverse nil))
3211         (setq data (cdr data))))))
3212
3213 (defmacro gnus-data-list (backward)
3214   `(if ,backward
3215        (or gnus-newsgroup-data-reverse
3216            (setq gnus-newsgroup-data-reverse
3217                  (reverse gnus-newsgroup-data)))
3218      gnus-newsgroup-data))
3219
3220 (defun gnus-data-update-list (data offset)
3221   "Add OFFSET to the POS of all data entries in DATA."
3222   (setq gnus-newsgroup-data-reverse nil)
3223   (while data
3224     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3225     (setq data (cdr data))))
3226
3227 (defun gnus-summary-article-pseudo-p (article)
3228   "Say whether this article is a pseudo article or not."
3229   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3230
3231 (defmacro gnus-summary-article-sparse-p (article)
3232   "Say whether this article is a sparse article or not."
3233   `(memq ,article gnus-newsgroup-sparse))
3234
3235 (defmacro gnus-summary-article-ancient-p (article)
3236   "Say whether this article is a sparse article or not."
3237   `(memq ,article gnus-newsgroup-ancient))
3238
3239 (defun gnus-article-parent-p (number)
3240   "Say whether this article is a parent or not."
3241   (let ((data (gnus-data-find-list number)))
3242     (and (cdr data)              ; There has to be an article after...
3243          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3244             (gnus-data-level (nth 1 data))))))
3245
3246 (defun gnus-article-children (number)
3247   "Return a list of all children to NUMBER."
3248   (let* ((data (gnus-data-find-list number))
3249          (level (gnus-data-level (car data)))
3250          children)
3251     (setq data (cdr data))
3252     (while (and data
3253                 (= (gnus-data-level (car data)) (1+ level)))
3254       (push (gnus-data-number (car data)) children)
3255       (setq data (cdr data)))
3256     children))
3257
3258 (defmacro gnus-summary-skip-intangible ()
3259   "If the current article is intangible, then jump to a different article."
3260   '(let ((to (get-text-property (point) 'gnus-intangible)))
3261      (and to (gnus-summary-goto-subject to))))
3262
3263 (defmacro gnus-summary-article-intangible-p ()
3264   "Say whether this article is intangible or not."
3265   '(get-text-property (point) 'gnus-intangible))
3266
3267 (defun gnus-article-read-p (article)
3268   "Say whether ARTICLE is read or not."
3269   (not (or (memq article gnus-newsgroup-marked)
3270            (memq article gnus-newsgroup-spam-marked)
3271            (memq article gnus-newsgroup-unreads)
3272            (memq article gnus-newsgroup-unselected)
3273            (memq article gnus-newsgroup-dormant))))
3274
3275 ;; Some summary mode macros.
3276
3277 (defmacro gnus-summary-article-number ()
3278   "The article number of the article on the current line.
3279 If there isn't an article number here, then we return the current
3280 article number."
3281   '(progn
3282      (gnus-summary-skip-intangible)
3283      (or (get-text-property (point) 'gnus-number)
3284          (gnus-summary-last-subject))))
3285
3286 (defmacro gnus-summary-article-header (&optional number)
3287   "Return the header of article NUMBER."
3288   `(gnus-data-header (gnus-data-find
3289                       ,(or number '(gnus-summary-article-number)))))
3290
3291 (defmacro gnus-summary-thread-level (&optional number)
3292   "Return the level of thread that starts with article NUMBER."
3293   `(if (and (eq gnus-summary-make-false-root 'dummy)
3294             (get-text-property (point) 'gnus-intangible))
3295        0
3296      (gnus-data-level (gnus-data-find
3297                        ,(or number '(gnus-summary-article-number))))))
3298
3299 (defmacro gnus-summary-article-mark (&optional number)
3300   "Return the mark of article NUMBER."
3301   `(gnus-data-mark (gnus-data-find
3302                     ,(or number '(gnus-summary-article-number)))))
3303
3304 (defmacro gnus-summary-article-pos (&optional number)
3305   "Return the position of the line of article NUMBER."
3306   `(gnus-data-pos (gnus-data-find
3307                    ,(or number '(gnus-summary-article-number)))))
3308
3309 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3310 (defmacro gnus-summary-article-subject (&optional number)
3311   "Return current subject string or nil if nothing."
3312   `(let ((headers
3313           ,(if number
3314                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3315              '(gnus-data-header (assq (gnus-summary-article-number)
3316                                       gnus-newsgroup-data)))))
3317      (and headers
3318           (vectorp headers)
3319           (mail-header-subject headers))))
3320
3321 (defmacro gnus-summary-article-score (&optional number)
3322   "Return current article score."
3323   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3324                   gnus-newsgroup-scored))
3325        gnus-summary-default-score 0))
3326
3327 (defun gnus-summary-article-children (&optional number)
3328   "Return a list of article numbers that are children of article NUMBER."
3329   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3330          (level (gnus-data-level (car data)))
3331          l children)
3332     (while (and (setq data (cdr data))
3333                 (> (setq l (gnus-data-level (car data))) level))
3334       (and (= (1+ level) l)
3335            (push (gnus-data-number (car data))
3336                  children)))
3337     (nreverse children)))
3338
3339 (defun gnus-summary-article-parent (&optional number)
3340   "Return the article number of the parent of article NUMBER."
3341   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3342                                     (gnus-data-list t)))
3343          (level (gnus-data-level (car data))))
3344     (if (zerop level)
3345         ()                              ; This is a root.
3346       ;; We search until we find an article with a level less than
3347       ;; this one.  That function has to be the parent.
3348       (while (and (setq data (cdr data))
3349                   (not (< (gnus-data-level (car data)) level))))
3350       (and data (gnus-data-number (car data))))))
3351
3352 (defun gnus-unread-mark-p (mark)
3353   "Say whether MARK is the unread mark."
3354   (= mark gnus-unread-mark))
3355
3356 (defun gnus-read-mark-p (mark)
3357   "Say whether MARK is one of the marks that mark as read.
3358 This is all marks except unread, ticked, dormant, and expirable."
3359   (not (or (= mark gnus-unread-mark)
3360            (= mark gnus-ticked-mark)
3361            (= mark gnus-spam-mark)
3362            (= mark gnus-dormant-mark)
3363            (= mark gnus-expirable-mark))))
3364
3365 (defmacro gnus-article-mark (number)
3366   "Return the MARK of article NUMBER.
3367 This macro should only be used when computing the mark the \"first\"
3368 time; i.e., when generating the summary lines.  After that,
3369 `gnus-summary-article-mark' should be used to examine the
3370 marks of articles."
3371   `(cond
3372     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3373     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3374     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3375     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3376     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3377     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3378     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3379     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3380            gnus-ancient-mark))))
3381
3382 ;; Saving hidden threads.
3383
3384 (defmacro gnus-save-hidden-threads (&rest forms)
3385   "Save hidden threads, eval FORMS, and restore the hidden threads."
3386   (let ((config (make-symbol "config")))
3387     `(let ((,config (gnus-hidden-threads-configuration)))
3388        (unwind-protect
3389            (save-excursion
3390              ,@forms)
3391          (gnus-restore-hidden-threads-configuration ,config)))))
3392 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3393 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3394
3395 (defun gnus-data-compute-positions ()
3396   "Compute the positions of all articles."
3397   (setq gnus-newsgroup-data-reverse nil)
3398   (let ((data gnus-newsgroup-data))
3399     (save-excursion
3400       (gnus-save-hidden-threads
3401         (gnus-summary-show-all-threads)
3402         (goto-char (point-min))
3403         (while data
3404           (while (get-text-property (point) 'gnus-intangible)
3405             (forward-line 1))
3406           (gnus-data-set-pos (car data) (+ (point) 3))
3407           (setq data (cdr data))
3408           (forward-line 1))))))
3409
3410 (defun gnus-hidden-threads-configuration ()
3411   "Return the current hidden threads configuration."
3412   (save-excursion
3413     (let (config)
3414       (goto-char (point-min))
3415       (while (not (eobp))
3416         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3417           (push (save-excursion (forward-line 0) (point)) config))
3418         (forward-line 1))
3419       config)))
3420
3421 (defun gnus-restore-hidden-threads-configuration (config)
3422   "Restore hidden threads configuration from CONFIG."
3423   (save-excursion
3424     (let (point (inhibit-read-only t))
3425       (while (setq point (pop config))
3426         (goto-char point)
3427         (gnus-summary-hide-thread)))))
3428
3429 ;; Various summary mode internalish functions.
3430
3431 (defun gnus-mouse-pick-article (e)
3432   (interactive "e")
3433   (mouse-set-point e)
3434   (gnus-summary-next-page nil t))
3435
3436 (defun gnus-summary-set-display-table ()
3437   "Change the display table.
3438 Odd characters have a tendency to mess
3439 up nicely formatted displays - we make all possible glyphs
3440 display only a single character."
3441
3442   ;; We start from the standard display table, if any.
3443   (let ((table (or (copy-sequence standard-display-table)
3444                    (make-display-table)))
3445         (i 32))
3446     ;; Nix out all the control chars...
3447     (while (>= (setq i (1- i)) 0)
3448       (gnus-put-display-table i [??] table))
3449    ;; ... but not newline and cr, of course.  (cr is necessary for the
3450     ;; selective display).
3451     (gnus-put-display-table ?\n nil table)
3452     (gnus-put-display-table ?\r nil table)
3453     ;; We keep TAB as well.
3454     (gnus-put-display-table ?\t nil table)
3455     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3456     ;; Emacs 23 (unicode), that are not set already.
3457     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3458                  160
3459                256)))
3460       (while (>= (setq i (1- i)) 127)
3461         ;; Only modify if the entry is nil.
3462         (unless (gnus-get-display-table i table)
3463           (gnus-put-display-table i [??] table))))
3464     (setq buffer-display-table table)))
3465
3466 (defun gnus-summary-set-article-display-arrow (pos)
3467   "Update the overlay arrow to point to line at position POS."
3468   (when gnus-summary-display-arrow
3469     (make-local-variable 'overlay-arrow-position)
3470     (make-local-variable 'overlay-arrow-string)
3471     (save-excursion
3472       (goto-char pos)
3473       (beginning-of-line)
3474       (unless overlay-arrow-position
3475         (setq overlay-arrow-position (make-marker)))
3476       (setq overlay-arrow-string "=>"
3477             overlay-arrow-position (set-marker overlay-arrow-position
3478                                                (point)
3479                                                (current-buffer))))))
3480
3481 (defun gnus-summary-setup-buffer (group)
3482   "Initialize summary buffer."
3483   (let ((buffer (gnus-summary-buffer-name group))
3484         (dead-name (concat "*Dead Summary "
3485                            (gnus-group-decoded-name group) "*")))
3486     ;; If a dead summary buffer exists, we kill it.
3487     (when (gnus-buffer-live-p dead-name)
3488       (gnus-kill-buffer dead-name))
3489     (if (get-buffer buffer)
3490         (progn
3491           (set-buffer buffer)
3492           (setq gnus-summary-buffer (current-buffer))
3493           (not gnus-newsgroup-prepared))
3494       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3495       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3496       (gnus-summary-mode group)
3497       (when gnus-carpal
3498         (gnus-carpal-setup-buffer 'summary))
3499       (when (gnus-group-quit-config group)
3500         (set (make-local-variable 'gnus-single-article-buffer) nil))
3501       (make-local-variable 'gnus-article-buffer)
3502       (make-local-variable 'gnus-article-current)
3503       (make-local-variable 'gnus-original-article-buffer)
3504       (setq gnus-newsgroup-name group)
3505       ;; Set any local variables in the group parameters.
3506       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3507       t)))
3508
3509 (defun gnus-set-global-variables ()
3510   "Set the global equivalents of the buffer-local variables.
3511 They are set to the latest values they had.  These reflect the summary
3512 buffer that was in action when the last article was fetched."
3513   (when (eq major-mode 'gnus-summary-mode)
3514     (setq gnus-summary-buffer (current-buffer))
3515     (let ((name gnus-newsgroup-name)
3516           (marked gnus-newsgroup-marked)
3517           (spam gnus-newsgroup-spam-marked)
3518           (unread gnus-newsgroup-unreads)
3519           (headers gnus-current-headers)
3520           (data gnus-newsgroup-data)
3521           (summary gnus-summary-buffer)
3522           (article-buffer gnus-article-buffer)
3523           (original gnus-original-article-buffer)
3524           (gac gnus-article-current)
3525           (reffed gnus-reffed-article-number)
3526           (score-file gnus-current-score-file)
3527           (default-charset gnus-newsgroup-charset)
3528           vlist)
3529       (let ((locals gnus-newsgroup-variables))
3530         (while locals
3531           (if (consp (car locals))
3532               (push (eval (caar locals)) vlist)
3533             (push (eval (car locals)) vlist))
3534           (setq locals (cdr locals)))
3535         (setq vlist (nreverse vlist)))
3536       (with-current-buffer gnus-group-buffer
3537         (setq gnus-newsgroup-name name
3538               gnus-newsgroup-marked marked
3539               gnus-newsgroup-spam-marked spam
3540               gnus-newsgroup-unreads unread
3541               gnus-current-headers headers
3542               gnus-newsgroup-data data
3543               gnus-article-current gac
3544               gnus-summary-buffer summary
3545               gnus-article-buffer article-buffer
3546               gnus-original-article-buffer original
3547               gnus-reffed-article-number reffed
3548               gnus-current-score-file score-file
3549               gnus-newsgroup-charset default-charset)
3550         (let ((locals gnus-newsgroup-variables))
3551           (while locals
3552             (if (consp (car locals))
3553                 (set (caar locals) (pop vlist))
3554               (set (car locals) (pop vlist)))
3555             (setq locals (cdr locals))))
3556         ;; The article buffer also has local variables.
3557         (when (gnus-buffer-live-p gnus-article-buffer)
3558           (set-buffer gnus-article-buffer)
3559           (setq gnus-summary-buffer summary))))))
3560
3561 (defun gnus-summary-article-unread-p (article)
3562   "Say whether ARTICLE is unread or not."
3563   (memq article gnus-newsgroup-unreads))
3564
3565 (defun gnus-summary-first-article-p (&optional article)
3566   "Return whether ARTICLE is the first article in the buffer."
3567   (if (not (setq article (or article (gnus-summary-article-number))))
3568       nil
3569     (eq article (caar gnus-newsgroup-data))))
3570
3571 (defun gnus-summary-last-article-p (&optional article)
3572   "Return whether ARTICLE is the last article in the buffer."
3573   (if (not (setq article (or article (gnus-summary-article-number))))
3574       ;; All non-existent numbers are the last article.  :-)
3575       t
3576     (not (cdr (gnus-data-find-list article)))))
3577
3578 (defun gnus-make-thread-indent-array (&optional n)
3579   (when (or n
3580             (progn (setq n 200) nil)
3581             (null gnus-thread-indent-array)
3582             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3583     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3584           gnus-thread-indent-array-level gnus-thread-indent-level)
3585     (while (>= n 0)
3586       (aset gnus-thread-indent-array n
3587             (make-string (* n gnus-thread-indent-level) ? ))
3588       (setq n (1- n)))))
3589
3590 (defun gnus-update-summary-mark-positions ()
3591   "Compute where the summary marks are to go."
3592   (save-excursion
3593     (when (gnus-buffer-exists-p gnus-summary-buffer)
3594       (set-buffer gnus-summary-buffer))
3595     (let ((spec gnus-summary-line-format-spec)
3596           pos)
3597       (save-excursion
3598         (gnus-set-work-buffer)
3599         (let ((gnus-tmp-unread ?Z)
3600               (gnus-replied-mark ?Z)
3601               (gnus-score-below-mark ?Z)
3602               (gnus-score-over-mark ?Z)
3603               (gnus-undownloaded-mark ?Z)
3604               (gnus-summary-line-format-spec spec)
3605               (gnus-newsgroup-downloadable '(0))
3606               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3607               case-fold-search ignores)
3608           ;; Here, all marks are bound to Z.
3609           (gnus-summary-insert-line header
3610                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3611           (goto-char (point-min))
3612           ;; Memorize the positions of the same characters as dummy marks.
3613           (while (re-search-forward "[A-D]" nil t)
3614             (push (point) ignores))
3615           (erase-buffer)
3616           ;; We use A-D as dummy marks in order to know column positions
3617           ;; where marks should be inserted.
3618           (setq gnus-tmp-unread ?A
3619                 gnus-replied-mark ?B
3620                 gnus-score-below-mark ?C
3621                 gnus-score-over-mark ?C
3622                 gnus-undownloaded-mark ?D)
3623           (gnus-summary-insert-line header
3624                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3625           ;; Ignore characters which aren't dummy marks.
3626           (dolist (p ignores)
3627             (delete-region (goto-char (1- p)) p)
3628             (insert ?Z))
3629           (goto-char (point-min))
3630           (setq pos (list (cons 'unread
3631                                 (and (search-forward "A" nil t)
3632                                      (- (point) (point-min) 1)))))
3633           (goto-char (point-min))
3634           (push (cons 'replied (and (search-forward "B" nil t)
3635                                     (- (point) (point-min) 1)))
3636                 pos)
3637           (goto-char (point-min))
3638           (push (cons 'score (and (search-forward "C" nil t)
3639                                   (- (point) (point-min) 1)))
3640                 pos)
3641           (goto-char (point-min))
3642           (push (cons 'download (and (search-forward "D" nil t)
3643                                      (- (point) (point-min) 1)))
3644                 pos)))
3645       (setq gnus-summary-mark-positions pos))))
3646
3647 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3648   "Insert a dummy root in the summary buffer."
3649   (beginning-of-line)
3650   (gnus-add-text-properties
3651    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3652    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3653
3654 (defun gnus-summary-extract-address-component (from)
3655   (or (car (funcall gnus-extract-address-components from))
3656       from))
3657
3658 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3659   (let ((mail-parse-charset gnus-newsgroup-charset)
3660         (ignored-from-addresses (gnus-ignored-from-addresses))
3661         ; Is it really necessary to do this next part for each summary line?
3662         ; Luckily, doesn't seem to slow things down much.
3663         (mail-parse-ignored-charsets
3664          (with-current-buffer gnus-summary-buffer
3665            gnus-newsgroup-ignored-charsets)))
3666     (or
3667      (and ignored-from-addresses
3668           (string-match ignored-from-addresses gnus-tmp-from)
3669           (let ((extra-headers (mail-header-extra header))
3670                 to
3671                 newsgroups)
3672             (cond
3673              ((setq to (cdr (assq 'To extra-headers)))
3674               (concat gnus-summary-to-prefix
3675                       (inline
3676                         (gnus-summary-extract-address-component
3677                          (funcall gnus-decode-encoded-address-function to)))))
3678              ((setq newsgroups
3679                     (or
3680                      (cdr (assq 'Newsgroups extra-headers))
3681                      (and
3682                       (memq 'Newsgroups gnus-extra-headers)
3683                       (eq (car (gnus-find-method-for-group
3684                                 gnus-newsgroup-name)) 'nntp)
3685                       (gnus-group-real-name gnus-newsgroup-name))))
3686               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3687      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3688
3689 (defun gnus-summary-insert-line (gnus-tmp-header
3690                                  gnus-tmp-level gnus-tmp-current
3691                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3692                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3693                                  &optional gnus-tmp-dummy gnus-tmp-score
3694                                  gnus-tmp-process)
3695   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3696       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3697                                           gnus-tmp-level)))
3698   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3699          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3700          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3701          (gnus-tmp-score-char
3702           (if (or (null gnus-summary-default-score)
3703                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3704                       gnus-summary-zcore-fuzz))
3705               ?                         ;Whitespace
3706             (if (< gnus-tmp-score gnus-summary-default-score)
3707                 gnus-score-below-mark gnus-score-over-mark)))
3708          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3709          (gnus-tmp-replied
3710           (cond (gnus-tmp-process gnus-process-mark)
3711                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3712                  gnus-cached-mark)
3713                 (gnus-tmp-replied gnus-replied-mark)
3714                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3715                  gnus-forwarded-mark)
3716                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3717                  gnus-saved-mark)
3718                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3719                  gnus-recent-mark)
3720                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3721                  gnus-unseen-mark)
3722                 (t gnus-no-mark)))
3723          (gnus-tmp-downloaded
3724           (cond (undownloaded
3725                  gnus-undownloaded-mark)
3726                 (gnus-newsgroup-agentized
3727                  gnus-downloaded-mark)
3728                 (t
3729                  gnus-no-mark)))
3730          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3731          (gnus-tmp-name
3732           (cond
3733            ((string-match "<[^>]+> *$" gnus-tmp-from)
3734             (let ((beg (match-beginning 0)))
3735               (or (and (string-match "^\".+\"" gnus-tmp-from)
3736                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3737                   (substring gnus-tmp-from 0 beg))))
3738            ((string-match "(.+)" gnus-tmp-from)
3739             (substring gnus-tmp-from
3740                        (1+ (match-beginning 0)) (1- (match-end 0))))
3741            (t gnus-tmp-from)))
3742          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3743          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3744          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3745          (inhibit-read-only t))
3746     (when (string= gnus-tmp-name "")
3747       (setq gnus-tmp-name gnus-tmp-from))
3748     (unless (numberp gnus-tmp-lines)
3749       (setq gnus-tmp-lines -1))
3750     (if (= gnus-tmp-lines -1)
3751         (setq gnus-tmp-lines "?")
3752       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3753     (condition-case ()
3754         (gnus-put-text-property
3755          (point)
3756          (progn (eval gnus-summary-line-format-spec) (point))
3757          'gnus-number gnus-tmp-number)
3758       (error (gnus-message 5 "Error updating the summary line")))
3759     (when (gnus-visual-p 'summary-highlight 'highlight)
3760       (forward-line -1)
3761       (gnus-run-hooks 'gnus-summary-update-hook)
3762       (forward-line 1))))
3763
3764 (defun gnus-summary-update-line (&optional dont-update)
3765   "Update summary line after change."
3766   (when (and gnus-summary-default-score
3767              (not gnus-summary-inhibit-highlight))
3768     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3769            (article (gnus-summary-article-number))
3770            (score (gnus-summary-article-score article)))
3771       (unless dont-update
3772         (if (and gnus-summary-mark-below
3773                  (< (gnus-summary-article-score)
3774                     gnus-summary-mark-below))
3775             ;; This article has a low score, so we mark it as read.
3776             (when (memq article gnus-newsgroup-unreads)
3777               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3778           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3779             ;; This article was previously marked as read on account
3780             ;; of a low score, but now it has risen, so we mark it as
3781             ;; unread.
3782             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3783         (gnus-summary-update-mark
3784          (if (or (null gnus-summary-default-score)
3785                  (<= (abs (- score gnus-summary-default-score))
3786                      gnus-summary-zcore-fuzz))
3787              ?                          ;Whitespace
3788            (if (< score gnus-summary-default-score)
3789                gnus-score-below-mark gnus-score-over-mark))
3790          'score))
3791       ;; Do visual highlighting.
3792       (when (gnus-visual-p 'summary-highlight 'highlight)
3793         (gnus-run-hooks 'gnus-summary-update-hook)))))
3794
3795 (defvar gnus-tmp-new-adopts nil)
3796
3797 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3798   "Return the number of articles in THREAD.
3799 This may be 0 in some cases -- if none of the articles in
3800 the thread are to be displayed."
3801   (let* ((number
3802          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3803           (cond
3804            ((not (listp thread))
3805             1)
3806            ((and (consp thread) (cdr thread))
3807             (apply
3808              '+ 1 (mapcar
3809                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3810            ((null thread)
3811             1)
3812            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3813             1)
3814            (t 0))))
3815     (when (and level (zerop level) gnus-tmp-new-adopts)
3816       (incf number
3817             (apply '+ (mapcar
3818                        'gnus-summary-number-of-articles-in-thread
3819                        gnus-tmp-new-adopts))))
3820     (if char
3821         (if (> number 1) gnus-not-empty-thread-mark
3822           gnus-empty-thread-mark)
3823       number)))
3824
3825 (defsubst gnus-summary-line-message-size (head)
3826   "Return pretty-printed version of message size.
3827 This function is intended to be used in
3828 `gnus-summary-line-format-alist'."
3829   (let ((c (or (mail-header-chars head) -1)))
3830     (cond ((< c 0) "n/a")               ; chars not available
3831           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3832           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3833           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3834           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3835
3836
3837 (defun gnus-summary-set-local-parameters (group)
3838   "Go through the local params of GROUP and set all variable specs in that list."
3839   (let ((vars '(quit-config)))          ; Ignore quit-config.
3840     (dolist (elem (gnus-group-find-parameter group))
3841       (and (consp elem)                 ; Has to be a cons.
3842            (consp (cdr elem))           ; The cdr has to be a list.
3843            (symbolp (car elem))         ; Has to be a symbol in there.
3844            (not (memq (car elem) vars))
3845            (ignore-errors
3846              (push (car elem) vars)
3847              ;; Variables like `gnus-show-threads' that are globally
3848              ;; bound, if used as group parameters, need to get to be
3849              ;; buffer-local, whereas just parameters like `gcc-self',
3850              ;; `timestamp', etc. should not be bound as variables.
3851              (if (boundp (car elem))
3852                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3853                (eval (nth 1 elem))))))))
3854
3855 (defun gnus-summary-read-group (group &optional show-all no-article
3856                                       kill-buffer no-display backward
3857                                       select-articles)
3858   "Start reading news in newsgroup GROUP.
3859 If SHOW-ALL is non-nil, already read articles are also listed.
3860 If NO-ARTICLE is non-nil, no article is selected initially.
3861 If NO-DISPLAY, don't generate a summary buffer."
3862   (let (result)
3863     (while (and group
3864                 (null (setq result
3865                             (let ((gnus-auto-select-next nil))
3866                               (or (gnus-summary-read-group-1
3867                                    group show-all no-article
3868                                    kill-buffer no-display
3869                                    select-articles)
3870                                   (setq show-all nil
3871                                         select-articles nil)))))
3872                 (eq gnus-auto-select-next 'quietly))
3873       (set-buffer gnus-group-buffer)
3874       ;; The entry function called above goes to the next
3875       ;; group automatically, so we go two groups back
3876       ;; if we are searching for the previous group.
3877       (when backward
3878         (gnus-group-prev-unread-group 2))
3879       (if (not (equal group (gnus-group-group-name)))
3880           (setq group (gnus-group-group-name))
3881         (setq group nil)))
3882     result))
3883
3884 (defun gnus-summary-read-group-1 (group show-all no-article
3885                                         kill-buffer no-display
3886                                         &optional select-articles)
3887   ;; Killed foreign groups can't be entered.
3888   ;;  (when (and (not (gnus-group-native-p group))
3889   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3890   ;;    (error "Dead non-native groups can't be entered"))
3891   (gnus-message 5 "Retrieving newsgroup: %s..."
3892                 (gnus-group-decoded-name group))
3893   (let* ((new-group (gnus-summary-setup-buffer group))
3894          (quit-config (gnus-group-quit-config group))
3895          (did-select (and new-group (gnus-select-newsgroup
3896                                      group show-all select-articles))))
3897     (cond
3898      ;; This summary buffer exists already, so we just select it.
3899      ((not new-group)
3900       (gnus-set-global-variables)
3901       (when kill-buffer
3902         (gnus-kill-or-deaden-summary kill-buffer))
3903       (gnus-configure-windows 'summary 'force)
3904       (gnus-set-mode-line 'summary)
3905       (gnus-summary-position-point)
3906       (message "")
3907       t)
3908      ;; We couldn't select this group.
3909      ((null did-select)
3910       (when (and (eq major-mode 'gnus-summary-mode)
3911                  (not (equal (current-buffer) kill-buffer)))
3912         (kill-buffer (current-buffer))
3913         (if (not quit-config)
3914             (progn
3915               ;; Update the info -- marks might need to be removed,
3916               ;; for instance.
3917               (gnus-summary-update-info)
3918               (set-buffer gnus-group-buffer)
3919               (gnus-group-jump-to-group group)
3920               (gnus-group-next-unread-group 1))
3921           (gnus-handle-ephemeral-exit quit-config)))
3922       (let ((grpinfo (gnus-get-info group)))
3923         (if (null (gnus-info-read grpinfo))
3924             (gnus-message 3 "Group %s contains no messages"
3925                           (gnus-group-decoded-name group))
3926           (gnus-message 3 "Can't select group")))
3927       nil)
3928      ;; The user did a `C-g' while prompting for number of articles,
3929      ;; so we exit this group.
3930      ((eq did-select 'quit)
3931       (and (eq major-mode 'gnus-summary-mode)
3932            (not (equal (current-buffer) kill-buffer))
3933            (kill-buffer (current-buffer)))
3934       (when kill-buffer
3935         (gnus-kill-or-deaden-summary kill-buffer))
3936       (if (not quit-config)
3937           (progn
3938             (set-buffer gnus-group-buffer)
3939             (gnus-group-jump-to-group group)
3940             (gnus-configure-windows 'group 'force))
3941         (gnus-handle-ephemeral-exit quit-config))
3942       ;; Finally signal the quit.
3943       (signal 'quit nil))
3944      ;; The group was successfully selected.
3945      (t
3946       (gnus-set-global-variables)
3947       ;; Save the active value in effect when the group was entered.
3948       (setq gnus-newsgroup-active
3949             (gnus-copy-sequence
3950              (gnus-active gnus-newsgroup-name)))
3951       ;; You can change the summary buffer in some way with this hook.
3952       (gnus-run-hooks 'gnus-select-group-hook)
3953       (when (memq 'summary (gnus-update-format-specifications
3954                             nil 'summary 'summary-mode 'summary-dummy))
3955         ;; The format specification for the summary line was updated,
3956         ;; so we need to update the mark positions as well.
3957         (gnus-update-summary-mark-positions))
3958       ;; Do score processing.
3959       (when gnus-use-scoring
3960         (gnus-possibly-score-headers))
3961       ;; Check whether to fill in the gaps in the threads.
3962       (when gnus-build-sparse-threads
3963         (gnus-build-sparse-threads))
3964       ;; Find the initial limit.
3965       (if show-all
3966           (let ((gnus-newsgroup-dormant nil))
3967             (gnus-summary-initial-limit show-all))
3968         (gnus-summary-initial-limit show-all))
3969       ;; Generate the summary buffer.
3970       (unless no-display
3971         (gnus-summary-prepare))
3972       (when gnus-use-trees
3973         (gnus-tree-open group)
3974         (setq gnus-summary-highlight-line-function
3975               'gnus-tree-highlight-article))
3976       ;; If the summary buffer is empty, but there are some low-scored
3977       ;; articles or some excluded dormants, we include these in the
3978       ;; buffer.
3979       (when (and (zerop (buffer-size))
3980                  (not no-display))
3981         (cond (gnus-newsgroup-dormant
3982                (gnus-summary-limit-include-dormant))
3983               ((and gnus-newsgroup-scored show-all)
3984                (gnus-summary-limit-include-expunged t))))
3985       ;; Function `gnus-apply-kill-file' must be called in this hook.
3986       (gnus-run-hooks 'gnus-apply-kill-hook)
3987       (if (and (zerop (buffer-size))
3988                (not no-display))
3989           (progn
3990             ;; This newsgroup is empty.
3991             (gnus-summary-catchup-and-exit nil t)
3992             (gnus-message 6 "No unread news")
3993             (when kill-buffer
3994               (gnus-kill-or-deaden-summary kill-buffer))
3995             ;; Return nil from this function.
3996             nil)
3997         ;; Hide conversation thread subtrees.  We cannot do this in
3998         ;; gnus-summary-prepare-hook since kill processing may not
3999         ;; work with hidden articles.
4000         (gnus-summary-maybe-hide-threads)
4001         (when kill-buffer
4002           (gnus-kill-or-deaden-summary kill-buffer))
4003         (gnus-summary-auto-select-subject)
4004         ;; Show first unread article if requested.
4005         (if (and (not no-article)
4006                  (not no-display)
4007                  gnus-newsgroup-unreads
4008                  gnus-auto-select-first)
4009             (progn
4010               (gnus-configure-windows 'summary)
4011               (let ((art (gnus-summary-article-number)))
4012                 (unless (and (not gnus-plugged)
4013                              (or (memq art gnus-newsgroup-undownloaded)
4014                                  (memq art gnus-newsgroup-downloadable)))
4015                   (gnus-summary-goto-article art))))
4016           ;; Don't select any articles.
4017           (gnus-summary-position-point)
4018           (gnus-configure-windows 'summary 'force)
4019           (gnus-set-mode-line 'summary))
4020         (when (and gnus-auto-center-group
4021                    (get-buffer-window gnus-group-buffer t))
4022           ;; Gotta use windows, because recenter does weird stuff if
4023           ;; the current buffer ain't the displayed window.
4024           (let ((owin (selected-window)))
4025             (select-window (get-buffer-window gnus-group-buffer t))
4026             (when (gnus-group-goto-group group)
4027               (recenter))
4028             (select-window owin)))
4029         ;; Mark this buffer as "prepared".
4030         (setq gnus-newsgroup-prepared t)
4031         (gnus-run-hooks 'gnus-summary-prepared-hook)
4032         (unless (gnus-ephemeral-group-p group)
4033           (gnus-group-update-group group))
4034         t)))))
4035
4036 (defun gnus-summary-auto-select-subject ()
4037   "Select the subject line on initial group entry."
4038   (goto-char (point-min))
4039   (cond
4040    ((eq gnus-auto-select-subject 'best)
4041     (gnus-summary-best-unread-subject))
4042    ((eq gnus-auto-select-subject 'unread)
4043     (gnus-summary-first-unread-subject))
4044    ((eq gnus-auto-select-subject 'unseen)
4045     (gnus-summary-first-unseen-subject))
4046    ((eq gnus-auto-select-subject 'unseen-or-unread)
4047     (gnus-summary-first-unseen-or-unread-subject))
4048    ((eq gnus-auto-select-subject 'first)
4049     ;; Do nothing.
4050     )
4051    ((functionp gnus-auto-select-subject)
4052     (funcall gnus-auto-select-subject))))
4053
4054 (defun gnus-summary-prepare ()
4055   "Generate the summary buffer."
4056   (interactive)
4057   (let ((inhibit-read-only t))
4058     (erase-buffer)
4059     (setq gnus-newsgroup-data nil
4060           gnus-newsgroup-data-reverse nil)
4061     (gnus-run-hooks 'gnus-summary-generate-hook)
4062     ;; Generate the buffer, either with threads or without.
4063     (when gnus-newsgroup-headers
4064       (gnus-summary-prepare-threads
4065        (if gnus-show-threads
4066            (gnus-sort-gathered-threads
4067             (funcall gnus-summary-thread-gathering-function
4068                      (gnus-sort-threads
4069                       (gnus-cut-threads (gnus-make-threads)))))
4070          ;; Unthreaded display.
4071          (gnus-sort-articles gnus-newsgroup-headers))))
4072     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4073     ;; Call hooks for modifying summary buffer.
4074     (goto-char (point-min))
4075     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4076
4077 (defsubst gnus-general-simplify-subject (subject)
4078   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4079   (setq subject
4080         (cond
4081          ;; Truncate the subject.
4082          (gnus-simplify-subject-functions
4083           (gnus-map-function gnus-simplify-subject-functions subject))
4084          ((numberp gnus-summary-gather-subject-limit)
4085           (setq subject (gnus-simplify-subject-re subject))
4086           (if (> (length subject) gnus-summary-gather-subject-limit)
4087               (substring subject 0 gnus-summary-gather-subject-limit)
4088             subject))
4089          ;; Fuzzily simplify it.
4090          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4091           (gnus-simplify-subject-fuzzy subject))
4092          ;; Just remove the leading "Re:".
4093          (t
4094           (gnus-simplify-subject-re subject))))
4095
4096   (if (and gnus-summary-gather-exclude-subject
4097            (string-match gnus-summary-gather-exclude-subject subject))
4098       nil                         ; This article shouldn't be gathered
4099     subject))
4100
4101 (defun gnus-summary-simplify-subject-query ()
4102   "Query where the respool algorithm would put this article."
4103   (interactive)
4104   (gnus-summary-select-article)
4105   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4106
4107 (defun gnus-gather-threads-by-subject (threads)
4108   "Gather threads by looking at Subject headers."
4109   (if (not gnus-summary-make-false-root)
4110       threads
4111     (let ((hashtb (gnus-make-hashtable 1024))
4112           (prev threads)
4113           (result threads)
4114           subject hthread whole-subject)
4115       (while threads
4116         (setq subject (gnus-general-simplify-subject
4117                        (setq whole-subject (mail-header-subject
4118                                             (caar threads)))))
4119         (when subject
4120           (if (setq hthread (gnus-gethash subject hashtb))
4121               (progn
4122                 ;; We enter a dummy root into the thread, if we
4123                 ;; haven't done that already.
4124                 (unless (stringp (caar hthread))
4125                   (setcar hthread (list whole-subject (car hthread))))
4126                 ;; We add this new gathered thread to this gathered
4127                 ;; thread.
4128                 (setcdr (car hthread)
4129                         (nconc (cdar hthread) (list (car threads))))
4130                 ;; Remove it from the list of threads.
4131                 (setcdr prev (cdr threads))
4132                 (setq threads prev))
4133             ;; Enter this thread into the hash table.
4134             (gnus-sethash subject
4135                           (if gnus-summary-make-false-root-always
4136                               (progn
4137                                 ;; If you want a dummy root above all
4138                                 ;; threads...
4139                                 (setcar threads (list whole-subject
4140                                                       (car threads)))
4141                                 threads)
4142                             threads)
4143                           hashtb)))
4144         (setq prev threads)
4145         (setq threads (cdr threads)))
4146       result)))
4147
4148 (defun gnus-gather-threads-by-references (threads)
4149   "Gather threads by looking at References headers."
4150   (let ((idhashtb (gnus-make-hashtable 1024))
4151         (thhashtb (gnus-make-hashtable 1024))
4152         (prev threads)
4153         (result threads)
4154         ids references id gthread gid entered ref)
4155     (while threads
4156       (when (setq references (mail-header-references (caar threads)))
4157         (setq id (mail-header-id (caar threads))
4158               ids (inline (gnus-split-references references))
4159               entered nil)
4160         (while (setq ref (pop ids))
4161           (setq ids (delete ref ids))
4162           (if (not (setq gid (gnus-gethash ref idhashtb)))
4163               (progn
4164                 (gnus-sethash ref id idhashtb)
4165                 (gnus-sethash id threads thhashtb))
4166             (setq gthread (gnus-gethash gid thhashtb))
4167             (unless entered
4168               ;; We enter a dummy root into the thread, if we
4169               ;; haven't done that already.
4170               (unless (stringp (caar gthread))
4171                 (setcar gthread (list (mail-header-subject (caar gthread))
4172                                       (car gthread))))
4173               ;; We add this new gathered thread to this gathered
4174               ;; thread.
4175               (setcdr (car gthread)
4176                       (nconc (cdar gthread) (list (car threads)))))
4177             ;; Add it into the thread hash table.
4178             (gnus-sethash id gthread thhashtb)
4179             (setq entered t)
4180             ;; Remove it from the list of threads.
4181             (setcdr prev (cdr threads))
4182             (setq threads prev))))
4183       (setq prev threads)
4184       (setq threads (cdr threads)))
4185     result))
4186
4187 (defun gnus-sort-gathered-threads (threads)
4188   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4189   (let ((result threads))
4190     (while threads
4191       (when (stringp (caar threads))
4192         (setcdr (car threads)
4193                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4194       (setq threads (cdr threads)))
4195     result))
4196
4197 (defun gnus-thread-loop-p (root thread)
4198   "Say whether ROOT is in THREAD."
4199   (let ((stack (list thread))
4200         (infloop 0)
4201         th)
4202     (while (setq thread (pop stack))
4203       (setq th (cdr thread))
4204       (while (and th
4205                   (not (eq (caar th) root)))
4206         (pop th))
4207       (if th
4208           ;; We have found a loop.
4209           (let (ref-dep)
4210             (setcdr thread (delq (car th) (cdr thread)))
4211             (if (boundp (setq ref-dep (intern "none"
4212                                               gnus-newsgroup-dependencies)))
4213                 (setcdr (symbol-value ref-dep)
4214                         (nconc (cdr (symbol-value ref-dep))
4215                                (list (car th))))
4216               (set ref-dep (list nil (car th))))
4217             (setq infloop 1
4218                   stack nil))
4219         ;; Push all the subthreads onto the stack.
4220         (push (cdr thread) stack)))
4221     infloop))
4222
4223 (defun gnus-make-threads ()
4224   "Go through the dependency hashtb and find the roots.  Return all threads."
4225   (let (threads)
4226     (while (catch 'infloop
4227              (mapatoms
4228               (lambda (refs)
4229                 ;; Deal with self-referencing References loops.
4230                 (when (and (car (symbol-value refs))
4231                            (not (zerop
4232                                  (apply
4233                                   '+
4234                                   (mapcar
4235                                    (lambda (thread)
4236                                      (gnus-thread-loop-p
4237                                       (car (symbol-value refs)) thread))
4238                                    (cdr (symbol-value refs)))))))
4239                   (setq threads nil)
4240                   (throw 'infloop t))
4241                 (unless (car (symbol-value refs))
4242                   ;; These threads do not refer back to any other
4243                   ;; articles, so they're roots.
4244                   (setq threads (append (cdr (symbol-value refs)) threads))))
4245               gnus-newsgroup-dependencies)))
4246     threads))
4247
4248 ;; Build the thread tree.
4249 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4250   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4251
4252 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4253 if it was already present.
4254
4255 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4256 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4257 Message-IDs will be renamed to a unique Message-ID before being
4258 entered.
4259
4260 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4261   (let* ((id (mail-header-id header))
4262          (id-dep (and id (intern id dependencies)))
4263          parent-id ref ref-dep ref-header replaced)
4264     ;; Enter this `header' in the `dependencies' table.
4265     (cond
4266      ((not id-dep)
4267       (setq header nil))
4268      ;; The first two cases do the normal part: enter a new `header'
4269      ;; in the `dependencies' table.
4270      ((not (boundp id-dep))
4271       (set id-dep (list header)))
4272      ((null (car (symbol-value id-dep)))
4273       (setcar (symbol-value id-dep) header))
4274
4275      ;; From here the `header' was already present in the
4276      ;; `dependencies' table.
4277      (force-new
4278       ;; Overrides an existing entry;
4279       ;; just set the header part of the entry.
4280       (setcar (symbol-value id-dep) header)
4281       (setq replaced t))
4282
4283      ;; Renames the existing `header' to a unique Message-ID.
4284      ((not gnus-summary-ignore-duplicates)
4285       ;; An article with this Message-ID has already been seen.
4286       ;; We rename the Message-ID.
4287       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4288            (list header))
4289       (mail-header-set-id header id))
4290
4291      ;; The last case ignores an existing entry, except it adds any
4292      ;; additional Xrefs (in case the two articles came from different
4293      ;; servers.
4294      ;; Also sets `header' to `nil' meaning that the `dependencies'
4295      ;; table was *not* modified.
4296      (t
4297       (mail-header-set-xref
4298        (car (symbol-value id-dep))
4299        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4300                    "")
4301                (or (mail-header-xref header) "")))
4302       (setq header nil)))
4303
4304     (when (and header (not replaced))
4305       ;; First check that we are not creating a References loop.
4306       (setq parent-id (gnus-parent-id (mail-header-references header)))
4307       (setq ref parent-id)
4308       (while (and ref
4309                   (setq ref-dep (intern-soft ref dependencies))
4310                   (boundp ref-dep)
4311                   (setq ref-header (car (symbol-value ref-dep))))
4312         (if (string= id ref)
4313             ;; Yuk!  This is a reference loop.  Make the article be a
4314             ;; root article.
4315             (progn
4316               (mail-header-set-references (car (symbol-value id-dep)) "none")
4317               (setq ref nil)
4318               (setq parent-id nil))
4319           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4320       (setq ref-dep (intern (or parent-id "none") dependencies))
4321       (if (boundp ref-dep)
4322           (setcdr (symbol-value ref-dep)
4323                   (nconc (cdr (symbol-value ref-dep))
4324                          (list (symbol-value id-dep))))
4325         (set ref-dep (list nil (symbol-value id-dep)))))
4326     header))
4327
4328 (defun gnus-extract-message-id-from-in-reply-to (string)
4329   (if (string-match "<[^>]+>" string)
4330       (substring string (match-beginning 0) (match-end 0))
4331     nil))
4332
4333 (defun gnus-build-sparse-threads ()
4334   (let ((headers gnus-newsgroup-headers)
4335         (mail-parse-charset gnus-newsgroup-charset)
4336         (gnus-summary-ignore-duplicates t)
4337         header references generation relations
4338         subject child end new-child date)
4339     ;; First we create an alist of generations/relations, where
4340     ;; generations is how much we trust the relation, and the relation
4341     ;; is parent/child.
4342     (gnus-message 7 "Making sparse threads...")
4343     (save-excursion
4344       (nnheader-set-temp-buffer " *gnus sparse threads*")
4345       (while (setq header (pop headers))
4346         (when (and (setq references (mail-header-references header))
4347                    (not (string= references "")))
4348           (insert references)
4349           (setq child (mail-header-id header)
4350                 subject (mail-header-subject header)
4351                 date (mail-header-date header)
4352                 generation 0)
4353           (while (search-backward ">" nil t)
4354             (setq end (1+ (point)))
4355             (when (search-backward "<" nil t)
4356               (setq new-child (buffer-substring (point) end))
4357               (push (list (incf generation)
4358                           child (setq child new-child)
4359                           subject date)
4360                     relations)))
4361           (when child
4362             (push (list (1+ generation) child nil subject) relations))
4363           (erase-buffer)))
4364       (kill-buffer (current-buffer)))
4365     ;; Sort over trustworthiness.
4366     (dolist (relation (sort relations 'car-less-than-car))
4367       (when (gnus-dependencies-add-header
4368              (make-full-mail-header
4369               gnus-reffed-article-number
4370               (nth 3 relation) "" (or (nth 4 relation) "")
4371               (nth 1 relation)
4372               (or (nth 2 relation) "") 0 0 "")
4373              gnus-newsgroup-dependencies nil)
4374         (push gnus-reffed-article-number gnus-newsgroup-limit)
4375         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4376         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4377               gnus-newsgroup-reads)
4378         (decf gnus-reffed-article-number)))
4379     (gnus-message 7 "Making sparse threads...done")))
4380
4381 (defun gnus-build-old-threads ()
4382   ;; Look at all the articles that refer back to old articles, and
4383   ;; fetch the headers for the articles that aren't there.  This will
4384   ;; build complete threads - if the roots haven't been expired by the
4385   ;; server, that is.
4386   (let ((mail-parse-charset gnus-newsgroup-charset)
4387         id heads)
4388     (mapatoms
4389      (lambda (refs)
4390        (when (not (car (symbol-value refs)))
4391          (setq heads (cdr (symbol-value refs)))
4392          (while heads
4393            (if (memq (mail-header-number (caar heads))
4394                      gnus-newsgroup-dormant)
4395                (setq heads (cdr heads))
4396              (setq id (symbol-name refs))
4397              (while (and (setq id (gnus-build-get-header id))
4398                          (not (car (gnus-id-to-thread id)))))
4399              (setq heads nil)))))
4400      gnus-newsgroup-dependencies)))
4401
4402 (defsubst gnus-remove-odd-characters (string)
4403   "Translate STRING into something that doesn't contain weird characters."
4404   (mm-subst-char-in-string
4405    ?\r ?\-
4406    (mm-subst-char-in-string ?\n ?\- string t) t))
4407
4408 ;; This function has to be called with point after the article number
4409 ;; on the beginning of the line.
4410 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4411   (let ((eol (point-at-eol))
4412         (buffer (current-buffer))
4413         header references in-reply-to)
4414
4415     ;; overview: [num subject from date id refs chars lines misc]
4416     (unwind-protect
4417         (let (x)
4418           (narrow-to-region (point) eol)
4419           (unless (eobp)
4420             (forward-char))
4421
4422           (setq header
4423                 (make-full-mail-header
4424                  number                 ; number
4425                  (condition-case ()     ; subject
4426                      (gnus-remove-odd-characters
4427                       (funcall gnus-decode-encoded-word-function
4428                                (setq x (nnheader-nov-field))))
4429                    (error x))
4430                  (condition-case ()     ; from
4431                      (gnus-remove-odd-characters
4432                       (funcall gnus-decode-encoded-address-function
4433                                (setq x (nnheader-nov-field))))
4434                    (error x))
4435                  (nnheader-nov-field)   ; date
4436                  (nnheader-nov-read-message-id number)  ; id
4437                  (setq references (nnheader-nov-field)) ; refs
4438                  (nnheader-nov-read-integer) ; chars
4439                  (nnheader-nov-read-integer) ; lines
4440                  (unless (eobp)
4441                    (if (looking-at "Xref: ")
4442                        (goto-char (match-end 0)))
4443                    (nnheader-nov-field)) ; Xref
4444                  (nnheader-nov-parse-extra)))) ; extra
4445
4446       (widen))
4447
4448     (when (and (string= references "")
4449                (setq in-reply-to (mail-header-extra header))
4450                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4451       (mail-header-set-references
4452        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4453
4454     (when gnus-alter-header-function
4455       (funcall gnus-alter-header-function header))
4456     (gnus-dependencies-add-header header dependencies force-new)))
4457
4458 (defun gnus-build-get-header (id)
4459   "Look through the buffer of NOV lines and find the header to ID.
4460 Enter this line into the dependencies hash table, and return
4461 the id of the parent article (if any)."
4462   (let ((deps gnus-newsgroup-dependencies)
4463         found header)
4464     (prog1
4465         (with-current-buffer nntp-server-buffer
4466           (let ((case-fold-search nil))
4467             (goto-char (point-min))
4468             (while (and (not found)
4469                         (search-forward id nil t))
4470               (beginning-of-line)
4471               (setq found (looking-at
4472                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4473                                    (regexp-quote id))))
4474               (or found (beginning-of-line 2)))
4475             (when found
4476               (beginning-of-line)
4477               (and
4478                (setq header (gnus-nov-parse-line
4479                              (read (current-buffer)) deps))
4480                (gnus-parent-id (mail-header-references header))))))
4481       (when header
4482         (let ((number (mail-header-number header)))
4483           (push number gnus-newsgroup-limit)
4484           (push header gnus-newsgroup-headers)
4485           (if (memq number gnus-newsgroup-unselected)
4486               (progn
4487                 (setq gnus-newsgroup-unreads
4488                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4489                                                number))
4490                 (setq gnus-newsgroup-unselected
4491                       (delq number gnus-newsgroup-unselected)))
4492             (push number gnus-newsgroup-ancient)))))))
4493
4494 (defun gnus-build-all-threads ()
4495   "Read all the headers."
4496   (let ((gnus-summary-ignore-duplicates t)
4497         (mail-parse-charset gnus-newsgroup-charset)
4498         (dependencies gnus-newsgroup-dependencies)
4499         header article)
4500     (with-current-buffer nntp-server-buffer
4501       (let ((case-fold-search nil))
4502         (goto-char (point-min))
4503         (while (not (eobp))
4504           (ignore-errors
4505             (setq article (read (current-buffer))
4506                   header (gnus-nov-parse-line article dependencies)))
4507           (when header
4508             (with-current-buffer gnus-summary-buffer
4509               (push header gnus-newsgroup-headers)
4510               (if (memq (setq article (mail-header-number header))
4511                         gnus-newsgroup-unselected)
4512                   (progn
4513                     (setq gnus-newsgroup-unreads
4514                           (gnus-add-to-sorted-list
4515                            gnus-newsgroup-unreads article))
4516                     (setq gnus-newsgroup-unselected
4517                           (delq article gnus-newsgroup-unselected)))
4518                 (push article gnus-newsgroup-ancient)))
4519             (forward-line 1)))))))
4520
4521 (defun gnus-summary-update-article-line (article header)
4522   "Update the line for ARTICLE using HEADER."
4523   (let* ((id (mail-header-id header))
4524          (thread (gnus-id-to-thread id)))
4525     (unless thread
4526       (error "Article in no thread"))
4527     ;; Update the thread.
4528     (setcar thread header)
4529     (gnus-summary-goto-subject article)
4530     (let* ((datal (gnus-data-find-list article))
4531            (data (car datal))
4532            (inhibit-read-only t)
4533            (level (gnus-summary-thread-level)))
4534       (gnus-delete-line)
4535       (let ((inserted (- (point)
4536                          (progn
4537                            (gnus-summary-insert-line
4538                             header level nil
4539                             (memq article gnus-newsgroup-undownloaded)
4540                             (gnus-article-mark article)
4541                             (memq article gnus-newsgroup-replied)
4542                             (memq article gnus-newsgroup-expirable)
4543                             ;; Only insert the Subject string when it's different
4544                             ;; from the previous Subject string.
4545                             (if (and
4546                                  gnus-show-threads
4547                                  (gnus-subject-equal
4548                                   (condition-case ()
4549                                       (mail-header-subject
4550                                        (gnus-data-header
4551                                         (cadr
4552                                          (gnus-data-find-list
4553                                           article
4554                                           (gnus-data-list t)))))
4555                                     ;; Error on the side of excessive subjects.
4556                                     (error ""))
4557                                   (mail-header-subject header)))
4558                                 ""
4559                               (mail-header-subject header))
4560                             nil (cdr (assq article gnus-newsgroup-scored))
4561                             (memq article gnus-newsgroup-processable))
4562                            (point)))))
4563         (when (cdr datal)
4564           (gnus-data-update-list
4565            (cdr datal)
4566            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4567
4568 (defun gnus-summary-update-article (article &optional iheader)
4569   "Update ARTICLE in the summary buffer."
4570   (set-buffer gnus-summary-buffer)
4571   (let* ((header (gnus-summary-article-header article))
4572          (id (mail-header-id header))
4573          (data (gnus-data-find article))
4574          (thread (gnus-id-to-thread id))
4575          (references (mail-header-references header))
4576          (parent
4577           (gnus-id-to-thread
4578            (or (gnus-parent-id
4579                 (when (and references
4580                            (not (equal "" references)))
4581                   references))
4582                "none")))
4583          (inhibit-read-only t)
4584          (old (car thread)))
4585     (when thread
4586       (unless iheader
4587         (setcar thread nil)
4588         (when parent
4589           (delq thread parent)))
4590       (if (gnus-summary-insert-subject id header)
4591           ;; Set the (possibly) new article number in the data structure.
4592           (gnus-data-set-number data (gnus-id-to-article id))
4593         (setcar thread old)
4594         nil))))
4595
4596 (defun gnus-rebuild-thread (id &optional line)
4597   "Rebuild the thread containing ID.
4598 If LINE, insert the rebuilt thread starting on line LINE."
4599   (let ((inhibit-read-only t)
4600         old-pos current thread data)
4601     (if (not gnus-show-threads)
4602         (setq thread (list (car (gnus-id-to-thread id))))
4603       ;; Get the thread this article is part of.
4604       (setq thread (gnus-remove-thread id)))
4605     (setq old-pos (point-at-bol))
4606     (setq current (save-excursion
4607                     (and (re-search-backward "[\r\n]" nil t)
4608                          (gnus-summary-article-number))))
4609     ;; If this is a gathered thread, we have to go some re-gathering.
4610     (when (stringp (car thread))
4611       (let ((subject (car thread))
4612             roots thr)
4613         (setq thread (cdr thread))
4614         (while thread
4615           (unless (memq (setq thr (gnus-id-to-thread
4616                                    (gnus-root-id
4617                                     (mail-header-id (caar thread)))))
4618                         roots)
4619             (push thr roots))
4620           (setq thread (cdr thread)))
4621         ;; We now have all (unique) roots.
4622         (if (= (length roots) 1)
4623             ;; All the loose roots are now one solid root.
4624             (setq thread (car roots))
4625           (setq thread (cons subject (gnus-sort-threads roots))))))
4626     (let (threads)
4627       ;; We then insert this thread into the summary buffer.
4628       (when line
4629         (goto-char (point-min))
4630         (forward-line (1- line)))
4631       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4632         (if gnus-show-threads
4633             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4634           (gnus-summary-prepare-unthreaded thread))
4635         (setq data (nreverse gnus-newsgroup-data))
4636         (setq threads gnus-newsgroup-threads))
4637       ;; We splice the new data into the data structure.
4638       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4639       ;;!!! then we want to insert at the beginning of the buffer.
4640       ;;!!! That happens to be true with Gnus now, but that may
4641       ;;!!! change in the future.  Perhaps.
4642       (gnus-data-enter-list
4643        (if line nil current) data (- (point) old-pos))
4644       (setq gnus-newsgroup-threads
4645             (nconc threads gnus-newsgroup-threads))
4646       (gnus-data-compute-positions))))
4647
4648 (defun gnus-number-to-header (number)
4649   "Return the header for article NUMBER."
4650   (let ((headers gnus-newsgroup-headers))
4651     (while (and headers
4652                 (not (= number (mail-header-number (car headers)))))
4653       (pop headers))
4654     (when headers
4655       (car headers))))
4656
4657 (defun gnus-parent-headers (in-headers &optional generation)
4658   "Return the headers of the GENERATIONeth parent of HEADERS."
4659   (unless generation
4660     (setq generation 1))
4661   (let ((parent t)
4662         (headers in-headers)
4663         references)
4664     (while (and parent
4665                 (not (zerop generation))
4666                 (setq references (mail-header-references headers)))
4667       (setq headers (if (and references
4668                              (setq parent (gnus-parent-id references)))
4669                         (car (gnus-id-to-thread parent))
4670                       nil))
4671       (decf generation))
4672     (and (not (eq headers in-headers))
4673          headers)))
4674
4675 (defun gnus-id-to-thread (id)
4676   "Return the (sub-)thread where ID appears."
4677   (gnus-gethash id gnus-newsgroup-dependencies))
4678
4679 (defun gnus-id-to-article (id)
4680   "Return the article number of ID."
4681   (let ((thread (gnus-id-to-thread id)))
4682     (when (and thread
4683                (car thread))
4684       (mail-header-number (car thread)))))
4685
4686 (defun gnus-id-to-header (id)
4687   "Return the article headers of ID."
4688   (car (gnus-id-to-thread id)))
4689
4690 (defun gnus-article-displayed-root-p (article)
4691   "Say whether ARTICLE is a root(ish) article."
4692   (let ((level (gnus-summary-thread-level article))
4693         (refs (mail-header-references  (gnus-summary-article-header article)))
4694         particle)
4695     (cond
4696      ((null level) nil)
4697      ((zerop level) t)
4698      ((null refs) t)
4699      ((null (gnus-parent-id refs)) t)
4700      ((and (= 1 level)
4701            (null (setq particle (gnus-id-to-article
4702                                  (gnus-parent-id refs))))
4703            (null (gnus-summary-thread-level particle)))))))
4704
4705 (defun gnus-root-id (id)
4706   "Return the id of the root of the thread where ID appears."
4707   (let (last-id prev)
4708     (while (and id (setq prev (car (gnus-id-to-thread id))))
4709       (setq last-id id
4710             id (gnus-parent-id (mail-header-references prev))))
4711     last-id))
4712
4713 (defun gnus-articles-in-thread (thread)
4714   "Return the list of articles in THREAD."
4715   (cons (mail-header-number (car thread))
4716         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4717
4718 (defun gnus-remove-thread (id &optional dont-remove)
4719   "Remove the thread that has ID in it."
4720   (let (headers thread last-id)
4721     ;; First go up in this thread until we find the root.
4722     (setq last-id (gnus-root-id id)
4723           headers (message-flatten-list (gnus-id-to-thread last-id)))
4724     ;; We have now found the real root of this thread.  It might have
4725     ;; been gathered into some loose thread, so we have to search
4726     ;; through the threads to find the thread we wanted.
4727     (let ((threads gnus-newsgroup-threads)
4728           sub)
4729       (while threads
4730         (setq sub (car threads))
4731         (if (stringp (car sub))
4732             ;; This is a gathered thread, so we look at the roots
4733             ;; below it to find whether this article is in this
4734             ;; gathered root.
4735             (progn
4736               (setq sub (cdr sub))
4737               (while sub
4738                 (when (member (caar sub) headers)
4739                   (setq thread (car threads)
4740                         threads nil
4741                         sub nil))
4742                 (setq sub (cdr sub))))
4743           ;; It's an ordinary thread, so we check it.
4744           (when (eq (car sub) (car headers))
4745             (setq thread sub
4746                   threads nil)))
4747         (setq threads (cdr threads)))
4748       ;; If this article is in no thread, then it's a root.
4749       (if thread
4750           (unless dont-remove
4751             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4752         (setq thread (gnus-id-to-thread last-id)))
4753       (when thread
4754         (prog1
4755             thread                      ; We return this thread.
4756           (unless dont-remove
4757             (if (stringp (car thread))
4758                 (progn
4759                   ;; If we use dummy roots, then we have to remove the
4760                   ;; dummy root as well.
4761                   (when (eq gnus-summary-make-false-root 'dummy)
4762                     ;; We go to the dummy root by going to
4763                     ;; the first sub-"thread", and then one line up.
4764                     (gnus-summary-goto-article
4765                      (mail-header-number (caadr thread)))
4766                     (forward-line -1)
4767                     (gnus-delete-line)
4768                     (gnus-data-compute-positions))
4769                   (setq thread (cdr thread))
4770                   (while thread
4771                     (gnus-remove-thread-1 (car thread))
4772                     (setq thread (cdr thread))))
4773               (gnus-remove-thread-1 thread))))))))
4774
4775 (defun gnus-remove-thread-1 (thread)
4776   "Remove the thread THREAD recursively."
4777   (let ((number (mail-header-number (pop thread)))
4778         d)
4779     (setq thread (reverse thread))
4780     (while thread
4781       (gnus-remove-thread-1 (pop thread)))
4782     (when (setq d (gnus-data-find number))
4783       (goto-char (gnus-data-pos d))
4784       (gnus-summary-show-thread)
4785       (gnus-data-remove
4786        number
4787        (- (point-at-bol)
4788           (prog1
4789               (1+ (point-at-eol))
4790             (gnus-delete-line)))))))
4791
4792 (defun gnus-sort-threads-recursive (threads func)
4793   (sort (mapcar (lambda (thread)
4794                   (cons (car thread)
4795                         (and (cdr thread)
4796                              (gnus-sort-threads-recursive (cdr thread) func))))
4797                 threads) func))
4798
4799 (defun gnus-sort-threads-loop (threads func)
4800   (let* ((superthread (cons nil threads))
4801          (stack (list (cons superthread threads)))
4802          remaining-threads thread)
4803     (while stack
4804       (setq remaining-threads (cdr (car stack)))
4805       (if remaining-threads
4806           (progn (setq thread (car remaining-threads))
4807                  (setcdr (car stack) (cdr remaining-threads))
4808                  (if (cdr thread)
4809                      (push (cons thread (cdr thread)) stack)))
4810         (setq thread (caar stack))
4811         (setcdr thread (sort (cdr thread) func))
4812         (pop stack)))
4813     (cdr superthread)))
4814
4815 (defun gnus-sort-threads (threads)
4816   "Sort THREADS."
4817   (if (not gnus-thread-sort-functions)
4818       threads
4819     (gnus-message 8 "Sorting threads...")
4820     (prog1
4821         (condition-case nil
4822             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4823               (gnus-sort-threads-recursive
4824                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4825           ;; Even after binding max-lisp-eval-depth, the recursive
4826           ;; sorter might fail for very long threads.  In that case,
4827           ;; try using a (less well-tested) non-recursive sorter.
4828           (error (gnus-message 9 "Sorting threads with loop...")
4829                  (gnus-sort-threads-loop
4830                   threads (gnus-make-sort-function
4831                            gnus-thread-sort-functions))))
4832       (gnus-message 8 "Sorting threads...done"))))
4833
4834 (defun gnus-sort-articles (articles)
4835   "Sort ARTICLES."
4836   (when gnus-article-sort-functions
4837     (gnus-message 7 "Sorting articles...")
4838     (prog1
4839         (setq gnus-newsgroup-headers
4840               (sort articles (gnus-make-sort-function
4841                               gnus-article-sort-functions)))
4842       (gnus-message 7 "Sorting articles...done"))))
4843
4844 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4845 (defmacro gnus-thread-header (thread)
4846   "Return header of first article in THREAD.
4847 Note that THREAD must never, ever be anything else than a variable -
4848 using some other form will lead to serious barfage."
4849   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4850   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4851   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4852         (vector thread) 2))
4853
4854 (defsubst gnus-article-sort-by-number (h1 h2)
4855   "Sort articles by article number."
4856   (< (mail-header-number h1)
4857      (mail-header-number h2)))
4858
4859 (defun gnus-thread-sort-by-number (h1 h2)
4860   "Sort threads by root article number."
4861   (gnus-article-sort-by-number
4862    (gnus-thread-header h1) (gnus-thread-header h2)))
4863
4864 (defsubst gnus-article-sort-by-random (h1 h2)
4865   "Sort articles randomly."
4866   (zerop (random 2)))
4867
4868 (defun gnus-thread-sort-by-random (h1 h2)
4869   "Sort threads randomly."
4870   (gnus-article-sort-by-random
4871    (gnus-thread-header h1) (gnus-thread-header h2)))
4872
4873 (defsubst gnus-article-sort-by-lines (h1 h2)
4874   "Sort articles by article Lines header."
4875   (< (mail-header-lines h1)
4876      (mail-header-lines h2)))
4877
4878 (defun gnus-thread-sort-by-lines (h1 h2)
4879   "Sort threads by root article Lines header."
4880   (gnus-article-sort-by-lines
4881    (gnus-thread-header h1) (gnus-thread-header h2)))
4882
4883 (defsubst gnus-article-sort-by-chars (h1 h2)
4884   "Sort articles by octet length."
4885   (< (mail-header-chars h1)
4886      (mail-header-chars h2)))
4887
4888 (defun gnus-thread-sort-by-chars (h1 h2)
4889   "Sort threads by root article octet length."
4890   (gnus-article-sort-by-chars
4891    (gnus-thread-header h1) (gnus-thread-header h2)))
4892
4893 (defsubst gnus-article-sort-by-author (h1 h2)
4894   "Sort articles by root author."
4895   (gnus-string<
4896    (let ((extract (funcall
4897                    gnus-extract-address-components
4898                    (mail-header-from h1))))
4899      (or (car extract) (cadr extract) ""))
4900    (let ((extract (funcall
4901                    gnus-extract-address-components
4902                    (mail-header-from h2))))
4903      (or (car extract) (cadr extract) ""))))
4904
4905 (defun gnus-thread-sort-by-author (h1 h2)
4906   "Sort threads by root author."
4907   (gnus-article-sort-by-author
4908    (gnus-thread-header h1)  (gnus-thread-header h2)))
4909
4910 (defsubst gnus-article-sort-by-recipient (h1 h2)
4911   "Sort articles by recipient."
4912   (gnus-string<
4913    (let ((extract (funcall
4914                    gnus-extract-address-components
4915                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4916      (or (car extract) (cadr extract)))
4917    (let ((extract (funcall
4918                    gnus-extract-address-components
4919                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4920      (or (car extract) (cadr extract)))))
4921
4922 (defun gnus-thread-sort-by-recipient (h1 h2)
4923   "Sort threads by root recipient."
4924   (gnus-article-sort-by-recipient
4925    (gnus-thread-header h1) (gnus-thread-header h2)))
4926
4927 (defsubst gnus-article-sort-by-subject (h1 h2)
4928   "Sort articles by root subject."
4929   (gnus-string<
4930    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4931    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4932
4933 (defun gnus-thread-sort-by-subject (h1 h2)
4934   "Sort threads by root subject."
4935   (gnus-article-sort-by-subject
4936    (gnus-thread-header h1) (gnus-thread-header h2)))
4937
4938 (defsubst gnus-article-sort-by-date (h1 h2)
4939   "Sort articles by root article date."
4940   (time-less-p
4941    (gnus-date-get-time (mail-header-date h1))
4942    (gnus-date-get-time (mail-header-date h2))))
4943
4944 (defun gnus-thread-sort-by-date (h1 h2)
4945   "Sort threads by root article date."
4946   (gnus-article-sort-by-date
4947    (gnus-thread-header h1) (gnus-thread-header h2)))
4948
4949 (defsubst gnus-article-sort-by-score (h1 h2)
4950   "Sort articles by root article score.
4951 Unscored articles will be counted as having a score of zero."
4952   (> (or (cdr (assq (mail-header-number h1)
4953                     gnus-newsgroup-scored))
4954          gnus-summary-default-score 0)
4955      (or (cdr (assq (mail-header-number h2)
4956                     gnus-newsgroup-scored))
4957          gnus-summary-default-score 0)))
4958
4959 (defun gnus-thread-sort-by-score (h1 h2)
4960   "Sort threads by root article score."
4961   (gnus-article-sort-by-score
4962    (gnus-thread-header h1) (gnus-thread-header h2)))
4963
4964 (defun gnus-thread-sort-by-total-score (h1 h2)
4965   "Sort threads by the sum of all scores in the thread.
4966 Unscored articles will be counted as having a score of zero."
4967   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4968
4969 (defun gnus-thread-total-score (thread)
4970   ;; This function find the total score of THREAD.
4971   (cond
4972    ((null thread)
4973     0)
4974    ((consp thread)
4975     (if (stringp (car thread))
4976         (apply gnus-thread-score-function 0
4977                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4978       (gnus-thread-total-score-1 thread)))
4979    (t
4980     (gnus-thread-total-score-1 (list thread)))))
4981
4982 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4983   "Sort threads such that the thread with the most recently arrived article comes first."
4984   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4985
4986 (defun gnus-thread-highest-number (thread)
4987   "Return the highest article number in THREAD."
4988   (apply 'max (mapcar (lambda (header)
4989                         (mail-header-number header))
4990                       (message-flatten-list thread))))
4991
4992 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4993   "Sort threads such that the thread with the most recently dated article comes first."
4994   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4995
4996 ; Since this is called not only to sort the top-level threads, but
4997 ; also in recursive sorts to order the articles within a thread, each
4998 ; article will be processed many times.  Thus it speeds things up
4999 ; quite a bit to use gnus-date-get-time, which caches the time value.
5000 (defun gnus-thread-latest-date (thread)
5001   "Return the highest article date in THREAD."
5002   (apply 'max
5003          (mapcar (lambda (header) (gnus-float-time
5004                                    (gnus-date-get-time
5005                                     (mail-header-date header))))
5006                  (message-flatten-list thread))))
5007
5008 (defun gnus-thread-total-score-1 (root)
5009   ;; This function find the total score of the thread below ROOT.
5010   (setq root (car root))
5011   (apply gnus-thread-score-function
5012          (or (append
5013               (mapcar 'gnus-thread-total-score
5014                       (cdr (gnus-id-to-thread (mail-header-id root))))
5015               (when (> (mail-header-number root) 0)
5016                 (list (or (cdr (assq (mail-header-number root)
5017                                      gnus-newsgroup-scored))
5018                           gnus-summary-default-score 0))))
5019              (list gnus-summary-default-score)
5020              '(0))))
5021
5022 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5023 (defvar gnus-tmp-prev-subject nil)
5024 (defvar gnus-tmp-false-parent nil)
5025 (defvar gnus-tmp-root-expunged nil)
5026 (defvar gnus-tmp-dummy-line nil)
5027
5028 (defun gnus-extra-header (type &optional header)
5029   "Return the extra header of TYPE."
5030   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5031       ""))
5032
5033 (defvar gnus-tmp-thread-tree-header-string "")
5034
5035 (defcustom gnus-sum-thread-tree-root "> "
5036   "With %B spec, used for the root of a thread.
5037 If nil, use subject instead."
5038   :version "22.1"
5039   :type '(radio (const :format "%v  " nil) string)
5040   :group 'gnus-thread)
5041
5042 (defcustom gnus-sum-thread-tree-false-root "> "
5043   "With %B spec, used for a false root of a thread.
5044 If nil, use subject instead."
5045   :version "22.1"
5046   :type '(radio (const :format "%v  " nil) string)
5047   :group 'gnus-thread)
5048
5049 (defcustom gnus-sum-thread-tree-single-indent ""
5050   "With %B spec, used for a thread with just one message.
5051 If nil, use subject instead."
5052   :version "22.1"
5053   :type '(radio (const :format "%v  " nil) string)
5054   :group 'gnus-thread)
5055
5056 (defcustom gnus-sum-thread-tree-vertical "| "
5057   "With %B spec, used for drawing a vertical line."
5058   :version "22.1"
5059   :type 'string
5060   :group 'gnus-thread)
5061
5062 (defcustom gnus-sum-thread-tree-indent "  "
5063   "With %B spec, used for indenting."
5064   :version "22.1"
5065   :type 'string
5066   :group 'gnus-thread)
5067
5068 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5069   "With %B spec, used for a leaf with brothers."
5070   :version "22.1"
5071   :type 'string
5072   :group 'gnus-thread)
5073
5074 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5075   "With %B spec, used for a leaf without brothers."
5076   :version "22.1"
5077   :type 'string
5078   :group 'gnus-thread)
5079
5080 (defcustom gnus-summary-display-while-building nil
5081   "If non-nil, show and update the summary buffer as it's being built.
5082 If the value is t, update the buffer after every line is inserted.  If
5083 the value is an integer (N), update the display every N lines."
5084   :version "22.1"
5085   :group 'gnus-thread
5086   :type '(choice (const :tag "off" nil)
5087                  number
5088                  (const :tag "frequently" t)))
5089
5090 (defun gnus-summary-prepare-threads (threads)
5091   "Prepare summary buffer from THREADS and indentation LEVEL.
5092 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5093 or a straight list of headers."
5094   (gnus-message 7 "Generating summary...")
5095
5096   (setq gnus-newsgroup-threads threads)
5097   (beginning-of-line)
5098
5099   (let ((gnus-tmp-level 0)
5100         (default-score (or gnus-summary-default-score 0))
5101         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5102         (building-line-count gnus-summary-display-while-building)
5103         (building-count (integerp gnus-summary-display-while-building))
5104         thread number subject stack state gnus-tmp-gathered beg-match
5105         new-roots gnus-tmp-new-adopts thread-end simp-subject
5106         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5107         gnus-tmp-replied gnus-tmp-subject-or-nil
5108         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5109         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5110         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5111         tree-stack)
5112
5113     (setq gnus-tmp-prev-subject nil
5114           gnus-tmp-thread-tree-header-string "")
5115
5116     (if (vectorp (car threads))
5117         ;; If this is a straight (sic) list of headers, then a
5118         ;; threaded summary display isn't required, so we just create
5119         ;; an unthreaded one.
5120         (gnus-summary-prepare-unthreaded threads)
5121
5122       ;; Do the threaded display.
5123
5124       (if gnus-summary-display-while-building
5125           (switch-to-buffer (buffer-name)))
5126       (while (or threads stack gnus-tmp-new-adopts new-roots)
5127
5128         (if (and (= gnus-tmp-level 0)
5129                  (or (not stack)
5130                      (= (caar stack) 0))
5131                  (not gnus-tmp-false-parent)
5132                  (or gnus-tmp-new-adopts new-roots))
5133             (if gnus-tmp-new-adopts
5134                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5135                       thread (list (car gnus-tmp-new-adopts))
5136                       gnus-tmp-header (caar thread)
5137                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5138               (when new-roots
5139                 (setq thread (list (car new-roots))
5140                       gnus-tmp-header (caar thread)
5141                       new-roots (cdr new-roots))))
5142
5143           (if threads
5144               ;; If there are some threads, we do them before the
5145               ;; threads on the stack.
5146               (setq thread threads
5147                     gnus-tmp-header (caar thread))
5148             ;; There were no current threads, so we pop something off
5149             ;; the stack.
5150             (setq state (car stack)
5151                   gnus-tmp-level (car state)
5152                   tree-stack (cadr state)
5153                   thread (caddr state)
5154                   stack (cdr stack)
5155                   gnus-tmp-header (caar thread))))
5156
5157         (setq gnus-tmp-false-parent nil)
5158         (setq gnus-tmp-root-expunged nil)
5159         (setq thread-end nil)
5160
5161         (if (stringp gnus-tmp-header)
5162             ;; The header is a dummy root.
5163             (cond
5164              ((eq gnus-summary-make-false-root 'adopt)
5165               ;; We let the first article adopt the rest.
5166               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5167                                                (cddar thread)))
5168               (setq gnus-tmp-gathered
5169                     (nconc (mapcar
5170                             (lambda (h) (mail-header-number (car h)))
5171                             (cddar thread))
5172                            gnus-tmp-gathered))
5173               (setq thread (cons (list (caar thread)
5174                                        (cadar thread))
5175                                  (cdr thread)))
5176               (setq gnus-tmp-level -1
5177                     gnus-tmp-false-parent t))
5178              ((eq gnus-summary-make-false-root 'empty)
5179               ;; We print adopted articles with empty subject fields.
5180               (setq gnus-tmp-gathered
5181                     (nconc (mapcar
5182                             (lambda (h) (mail-header-number (car h)))
5183                             (cddar thread))
5184                            gnus-tmp-gathered))
5185               (setq gnus-tmp-level -1))
5186              ((eq gnus-summary-make-false-root 'dummy)
5187               ;; We remember that we probably want to output a dummy
5188               ;; root.
5189               (setq gnus-tmp-dummy-line gnus-tmp-header)
5190               (setq gnus-tmp-prev-subject gnus-tmp-header))
5191              (t
5192               ;; We do not make a root for the gathered
5193               ;; sub-threads at all.
5194               (setq gnus-tmp-level -1)))
5195
5196           (setq number (mail-header-number gnus-tmp-header)
5197                 subject (mail-header-subject gnus-tmp-header)
5198                 simp-subject (gnus-simplify-subject-fully subject))
5199
5200           (cond
5201            ;; If the thread has changed subject, we might want to make
5202            ;; this subthread into a root.
5203            ((and (null gnus-thread-ignore-subject)
5204                  (not (zerop gnus-tmp-level))
5205                  gnus-tmp-prev-subject
5206                  (not (string= gnus-tmp-prev-subject simp-subject)))
5207             (setq new-roots (nconc new-roots (list (car thread)))
5208                   thread-end t
5209                   gnus-tmp-header nil))
5210            ;; If the article lies outside the current limit,
5211            ;; then we do not display it.
5212            ((not (memq number gnus-newsgroup-limit))
5213             (setq gnus-tmp-gathered
5214                   (nconc (mapcar
5215                           (lambda (h) (mail-header-number (car h)))
5216                           (cdar thread))
5217                          gnus-tmp-gathered))
5218             (setq gnus-tmp-new-adopts (if (cdar thread)
5219                                           (append gnus-tmp-new-adopts
5220                                                   (cdar thread))
5221                                         gnus-tmp-new-adopts)
5222                   thread-end t
5223                   gnus-tmp-header nil)
5224             (when (zerop gnus-tmp-level)
5225               (setq gnus-tmp-root-expunged t)))
5226            ;; Perhaps this article is to be marked as read?
5227            ((and gnus-summary-mark-below
5228                  (< (or (cdr (assq number gnus-newsgroup-scored))
5229                         default-score)
5230                     gnus-summary-mark-below)
5231                  ;; Don't touch sparse articles.
5232                  (not (gnus-summary-article-sparse-p number))
5233                  (not (gnus-summary-article-ancient-p number)))
5234             (setq gnus-newsgroup-unreads
5235                   (delq number gnus-newsgroup-unreads))
5236             (if gnus-newsgroup-auto-expire
5237                 (setq gnus-newsgroup-expirable
5238                       (gnus-add-to-sorted-list
5239                        gnus-newsgroup-expirable number))
5240               (push (cons number gnus-low-score-mark)
5241                     gnus-newsgroup-reads))))
5242
5243           (when gnus-tmp-header
5244             ;; We may have an old dummy line to output before this
5245             ;; article.
5246             (when (and gnus-tmp-dummy-line
5247                        (gnus-subject-equal
5248                         gnus-tmp-dummy-line
5249                         (mail-header-subject gnus-tmp-header)))
5250               (gnus-summary-insert-dummy-line
5251                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5252               (setq gnus-tmp-dummy-line nil))
5253
5254             ;; Compute the mark.
5255             (setq gnus-tmp-unread (gnus-article-mark number))
5256
5257             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5258                                   gnus-tmp-header gnus-tmp-level)
5259                   gnus-newsgroup-data)
5260
5261             ;; Actually insert the line.
5262             (setq
5263              gnus-tmp-subject-or-nil
5264              (cond
5265               ((and gnus-thread-ignore-subject
5266                     gnus-tmp-prev-subject
5267                     (not (string= gnus-tmp-prev-subject simp-subject)))
5268                subject)
5269               ((zerop gnus-tmp-level)
5270                (if (and (eq gnus-summary-make-false-root 'empty)
5271                         (memq number gnus-tmp-gathered)
5272                         gnus-tmp-prev-subject
5273                         (string= gnus-tmp-prev-subject simp-subject))
5274                    gnus-summary-same-subject
5275                  subject))
5276               (t gnus-summary-same-subject)))
5277             (if (and (eq gnus-summary-make-false-root 'adopt)
5278                      (= gnus-tmp-level 1)
5279                      (memq number gnus-tmp-gathered))
5280                 (setq gnus-tmp-opening-bracket ?\<
5281                       gnus-tmp-closing-bracket ?\>)
5282               (setq gnus-tmp-opening-bracket ?\[
5283                     gnus-tmp-closing-bracket ?\]))
5284             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5285                 (gnus-make-thread-indent-array
5286                  (max (* 2 (length gnus-thread-indent-array))
5287                       gnus-tmp-level)))
5288             (setq
5289              gnus-tmp-indentation
5290              (aref gnus-thread-indent-array gnus-tmp-level)
5291              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5292              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5293                                 gnus-summary-default-score 0)
5294              gnus-tmp-score-char
5295              (if (or (null gnus-summary-default-score)
5296                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5297                          gnus-summary-zcore-fuzz))
5298                  ?                      ;Whitespace
5299                (if (< gnus-tmp-score gnus-summary-default-score)
5300                    gnus-score-below-mark gnus-score-over-mark))
5301              gnus-tmp-replied
5302              (cond ((memq number gnus-newsgroup-processable)
5303                     gnus-process-mark)
5304                    ((memq number gnus-newsgroup-cached)
5305                     gnus-cached-mark)
5306                    ((memq number gnus-newsgroup-replied)
5307                     gnus-replied-mark)
5308                    ((memq number gnus-newsgroup-forwarded)
5309                     gnus-forwarded-mark)
5310                    ((memq number gnus-newsgroup-saved)
5311                     gnus-saved-mark)
5312                    ((memq number gnus-newsgroup-recent)
5313                     gnus-recent-mark)
5314                    ((memq number gnus-newsgroup-unseen)
5315                     gnus-unseen-mark)
5316                    (t gnus-no-mark))
5317              gnus-tmp-downloaded
5318              (cond ((memq number gnus-newsgroup-undownloaded)
5319                     gnus-undownloaded-mark)
5320                    (gnus-newsgroup-agentized
5321                     gnus-downloaded-mark)
5322                    (t
5323                     gnus-no-mark))
5324              gnus-tmp-from (mail-header-from gnus-tmp-header)
5325              gnus-tmp-name
5326              (cond
5327               ((string-match "<[^>]+> *$" gnus-tmp-from)
5328                (setq beg-match (match-beginning 0))
5329                (or (and (string-match "^\".+\"" gnus-tmp-from)
5330                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5331                    (substring gnus-tmp-from 0 beg-match)))
5332               ((string-match "(.+)" gnus-tmp-from)
5333                (substring gnus-tmp-from
5334                           (1+ (match-beginning 0)) (1- (match-end 0))))
5335               (t gnus-tmp-from))
5336
5337              ;; Do the %B string
5338              gnus-tmp-thread-tree-header-string
5339              (cond
5340               ((not gnus-show-threads) "")
5341               ((zerop gnus-tmp-level)
5342                (cond ((cdar thread)
5343                       (or gnus-sum-thread-tree-root subject))
5344                      (gnus-tmp-new-adopts
5345                       (or gnus-sum-thread-tree-false-root subject))
5346                      (t
5347                       (or gnus-sum-thread-tree-single-indent subject))))
5348               (t
5349                (concat (apply 'concat
5350                               (mapcar (lambda (item)
5351                                         (if (= item 1)
5352                                             gnus-sum-thread-tree-vertical
5353                                           gnus-sum-thread-tree-indent))
5354                                       (cdr (reverse tree-stack))))
5355                        (if (nth 1 thread)
5356                            gnus-sum-thread-tree-leaf-with-other
5357                          gnus-sum-thread-tree-single-leaf)))))
5358             (when (string= gnus-tmp-name "")
5359               (setq gnus-tmp-name gnus-tmp-from))
5360             (unless (numberp gnus-tmp-lines)
5361               (setq gnus-tmp-lines -1))
5362             (if (= gnus-tmp-lines -1)
5363                 (setq gnus-tmp-lines "?")
5364               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5365               (gnus-put-text-property
5366                (point)
5367                (progn (eval gnus-summary-line-format-spec) (point))
5368                'gnus-number number)
5369               (when gnus-visual-p
5370                 (forward-line -1)
5371                 (gnus-run-hooks 'gnus-summary-update-hook)
5372                 (forward-line 1))
5373
5374               (setq gnus-tmp-prev-subject simp-subject)))
5375
5376         (when (nth 1 thread)
5377           (push (list (max 0 gnus-tmp-level)
5378                       (copy-sequence tree-stack)
5379                       (nthcdr 1 thread))
5380                 stack))
5381         (push (if (nth 1 thread) 1 0) tree-stack)
5382         (incf gnus-tmp-level)
5383         (setq threads (if thread-end nil (cdar thread)))
5384         (if gnus-summary-display-while-building
5385             (if building-count
5386                 (progn
5387                   ;; use a set frequency
5388                   (setq building-line-count (1- building-line-count))
5389                   (when (= building-line-count 0)
5390                     (sit-for 0)
5391                     (setq building-line-count
5392                           gnus-summary-display-while-building)))
5393               ;; always
5394               (sit-for 0)))
5395         (unless threads
5396           (setq gnus-tmp-level 0)))))
5397   (gnus-message 7 "Generating summary...done"))
5398
5399 (defun gnus-summary-prepare-unthreaded (headers)
5400   "Generate an unthreaded summary buffer based on HEADERS."
5401   (let (header number mark)
5402
5403     (beginning-of-line)
5404
5405     (while headers
5406       ;; We may have to root out some bad articles...
5407       (when (memq (setq number (mail-header-number
5408                                 (setq header (pop headers))))
5409                   gnus-newsgroup-limit)
5410         ;; Mark article as read when it has a low score.
5411         (when (and gnus-summary-mark-below
5412                    (< (or (cdr (assq number gnus-newsgroup-scored))
5413                           gnus-summary-default-score 0)
5414                       gnus-summary-mark-below)
5415                    (not (gnus-summary-article-ancient-p number)))
5416           (setq gnus-newsgroup-unreads
5417                 (delq number gnus-newsgroup-unreads))
5418           (if gnus-newsgroup-auto-expire
5419               (push number gnus-newsgroup-expirable)
5420             (push (cons number gnus-low-score-mark)
5421                   gnus-newsgroup-reads)))
5422
5423         (setq mark (gnus-article-mark number))
5424         (push (gnus-data-make number mark (1+ (point)) header 0)
5425               gnus-newsgroup-data)
5426         (gnus-summary-insert-line
5427          header 0 number
5428          (memq number gnus-newsgroup-undownloaded)
5429          mark (memq number gnus-newsgroup-replied)
5430          (memq number gnus-newsgroup-expirable)
5431          (mail-header-subject header) nil
5432          (cdr (assq number gnus-newsgroup-scored))
5433          (memq number gnus-newsgroup-processable))))))
5434
5435 (defun gnus-summary-remove-list-identifiers ()
5436   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5437   (let ((regexp (if (consp gnus-list-identifiers)
5438                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5439                   gnus-list-identifiers))
5440         changed subject)
5441     (when regexp
5442       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5443       (dolist (header gnus-newsgroup-headers)
5444         (setq subject (mail-header-subject header)
5445               changed nil)
5446         (while (string-match regexp subject)
5447           (setq subject
5448                 (concat (substring subject 0 (match-beginning 1))
5449                         (substring subject (match-end 0)))
5450                 changed t))
5451         (when changed
5452           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5453             (setq subject
5454                   (concat (substring subject 0 (match-beginning 1))
5455                           (substring subject (match-end 1)))))
5456           (mail-header-set-subject header subject))))))
5457
5458 (defun gnus-fetch-headers (articles)
5459   "Fetch headers of ARTICLES."
5460   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5461     (gnus-message 5 "Fetching headers for %s..." name)
5462     (prog1
5463         (if (eq 'nov
5464                 (setq gnus-headers-retrieved-by
5465                       (gnus-retrieve-headers
5466                        articles gnus-newsgroup-name
5467                        ;; We might want to fetch old headers, but
5468                        ;; not if there is only 1 article.
5469                        (and (or (and
5470                                  (not (eq gnus-fetch-old-headers 'some))
5471                                  (not (numberp gnus-fetch-old-headers)))
5472                                 (> (length articles) 1))
5473                             gnus-fetch-old-headers))))
5474             (gnus-get-newsgroup-headers-xover
5475              articles nil nil gnus-newsgroup-name t)
5476           (gnus-get-newsgroup-headers))
5477       (gnus-message 5 "Fetching headers for %s...done" name))))
5478
5479 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5480   "Select newsgroup GROUP.
5481 If READ-ALL is non-nil, all articles in the group are selected.
5482 If SELECT-ARTICLES, only select those articles from GROUP."
5483   (let* ((entry (gnus-group-entry group))
5484          ;;!!! Dirty hack; should be removed.
5485          (gnus-summary-ignore-duplicates
5486           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5487               t
5488             gnus-summary-ignore-duplicates))
5489          (info (nth 2 entry))
5490          charset articles fetched-articles cached)
5491
5492     (unless (gnus-check-server
5493              (set (make-local-variable 'gnus-current-select-method)
5494                   (gnus-find-method-for-group group)))
5495       (error "Couldn't open server"))
5496     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5497
5498     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5499         (gnus-activate-group group)     ; Or we can activate it...
5500         (progn                          ; Or we bug out.
5501           (when (equal major-mode 'gnus-summary-mode)
5502             (gnus-kill-buffer (current-buffer)))
5503           (error
5504            "Couldn't activate group %s: %s"
5505            (mm-decode-coding-string group charset)
5506            (mm-decode-coding-string (gnus-status-message group) charset))))
5507
5508     (unless (gnus-request-group group t)
5509         (when (equal major-mode 'gnus-summary-mode)
5510           (gnus-kill-buffer (current-buffer)))
5511         (error "Couldn't request group %s: %s"
5512                (mm-decode-coding-string group charset)
5513                (mm-decode-coding-string (gnus-status-message group) charset)))
5514
5515     (when gnus-agent
5516       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5517
5518       (setq gnus-summary-use-undownloaded-faces
5519             (gnus-agent-find-parameter
5520              group
5521              'agent-enable-undownloaded-faces)))
5522
5523     (setq gnus-newsgroup-name group
5524           gnus-newsgroup-unselected nil
5525           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5526
5527     (let ((display (gnus-group-find-parameter group 'display)))
5528       (setq gnus-newsgroup-display
5529             (cond
5530              ((not (zerop (or (car-safe read-all) 0)))
5531               ;; The user entered the group with C-u SPC/RET, let's show
5532               ;; all articles.
5533               'gnus-not-ignore)
5534              ((eq display 'all)
5535               'gnus-not-ignore)
5536              ((arrayp display)
5537               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5538              ((numberp display)
5539               ;; The following is probably the "correct" solution, but
5540               ;; it makes Gnus fetch all headers and then limit the
5541               ;; articles (which is slow), so instead we hack the
5542               ;; select-articles parameter instead. -- Simon Josefsson
5543               ;; <jas@kth.se>
5544               ;;
5545               ;; (gnus-byte-compile
5546               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5547               ;;                         display)))))
5548               (setq select-articles
5549                     (gnus-uncompress-range
5550                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5551                              (if (> tmp 0)
5552                                  tmp
5553                                1))
5554                            (cdr (gnus-active group)))))
5555               nil)
5556              (t
5557               nil))))
5558
5559     (gnus-summary-setup-default-charset)
5560
5561     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5562     (when (gnus-virtual-group-p group)
5563       (setq cached gnus-newsgroup-cached))
5564
5565     (setq gnus-newsgroup-unreads
5566           (gnus-sorted-ndifference
5567            (gnus-sorted-ndifference gnus-newsgroup-unreads
5568                                     gnus-newsgroup-marked)
5569            gnus-newsgroup-dormant))
5570
5571     (setq gnus-newsgroup-processable nil)
5572
5573     (gnus-update-read-articles group gnus-newsgroup-unreads)
5574
5575     ;; Adjust and set lists of article marks.
5576     (when info
5577       (gnus-adjust-marked-articles info))
5578     (if (setq articles select-articles)
5579         (setq gnus-newsgroup-unselected
5580               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5581       (setq articles (gnus-articles-to-read group read-all)))
5582
5583     (cond
5584      ((null articles)
5585       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5586       'quit)
5587      ((eq articles 0) nil)
5588      (t
5589       ;; Init the dependencies hash table.
5590       (setq gnus-newsgroup-dependencies
5591             (gnus-make-hashtable (length articles)))
5592       (gnus-set-global-variables)
5593       ;; Retrieve the headers and read them in.
5594
5595       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5596
5597       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5598       (when cached
5599         (setq gnus-newsgroup-cached cached))
5600
5601       ;; Suppress duplicates?
5602       (when gnus-suppress-duplicates
5603         (gnus-dup-suppress-articles))
5604
5605       ;; Set the initial limit.
5606       (setq gnus-newsgroup-limit (copy-sequence articles))
5607       ;; Remove canceled articles from the list of unread articles.
5608       (setq fetched-articles
5609             (mapcar (lambda (headers) (mail-header-number headers))
5610                     gnus-newsgroup-headers))
5611       (setq gnus-newsgroup-articles fetched-articles)
5612       (setq gnus-newsgroup-unreads
5613             (gnus-sorted-nintersection
5614              gnus-newsgroup-unreads fetched-articles))
5615       (gnus-compute-unseen-list)
5616
5617       ;; Removed marked articles that do not exist.
5618       (gnus-update-missing-marks
5619        (gnus-sorted-difference articles fetched-articles))
5620       ;; We might want to build some more threads first.
5621       (when (and gnus-fetch-old-headers
5622                  (eq gnus-headers-retrieved-by 'nov))
5623         (if (eq gnus-fetch-old-headers 'invisible)
5624             (gnus-build-all-threads)
5625           (gnus-build-old-threads)))
5626       ;; Let the Gnus agent mark articles as read.
5627       (when gnus-agent
5628         (gnus-agent-get-undownloaded-list))
5629       ;; Remove list identifiers from subject
5630       (when gnus-list-identifiers
5631         (gnus-summary-remove-list-identifiers))
5632       ;; Check whether auto-expire is to be done in this group.
5633       (setq gnus-newsgroup-auto-expire
5634             (gnus-group-auto-expirable-p group))
5635       ;; Set up the article buffer now, if necessary.
5636       (unless (and gnus-single-article-buffer
5637                    (equal gnus-article-buffer "*Article*"))
5638         (gnus-article-setup-buffer))
5639       ;; First and last article in this newsgroup.
5640       (when gnus-newsgroup-headers
5641         (setq gnus-newsgroup-begin
5642               (mail-header-number (car gnus-newsgroup-headers))
5643               gnus-newsgroup-end
5644               (mail-header-number
5645                (gnus-last-element gnus-newsgroup-headers))))
5646       ;; GROUP is successfully selected.
5647       (or gnus-newsgroup-headers t)))))
5648
5649 (defun gnus-compute-unseen-list ()
5650   ;; The `seen' marks are treated specially.
5651   (if (not gnus-newsgroup-seen)
5652       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5653     (setq gnus-newsgroup-unseen
5654           (gnus-inverse-list-range-intersection
5655            gnus-newsgroup-articles gnus-newsgroup-seen))))
5656
5657 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5658
5659 (defun gnus-summary-display-make-predicate (display)
5660   (require 'gnus-agent)
5661   (when (= (length display) 1)
5662     (setq display (car display)))
5663   (unless gnus-summary-display-cache
5664     (dolist (elem (append '((unread . unread)
5665                             (read . read)
5666                             (unseen . unseen))
5667                           gnus-article-mark-lists))
5668       (push (cons (cdr elem)
5669                   (gnus-byte-compile
5670                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5671             gnus-summary-display-cache)))
5672   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5673         (gnus-category-predicate-cache gnus-summary-display-cache))
5674     (gnus-get-predicate display)))
5675
5676 ;; Uses the dynamically bound `number' variable.
5677 (defvar number)
5678 (defun gnus-article-marked-p (type &optional article)
5679   (let ((article (or article number)))
5680     (cond
5681      ((eq type 'tick)
5682       (memq article gnus-newsgroup-marked))
5683      ((eq type 'spam)
5684       (memq article gnus-newsgroup-spam-marked))
5685      ((eq type 'unsend)
5686       (memq article gnus-newsgroup-unsendable))
5687      ((eq type 'undownload)
5688       (memq article gnus-newsgroup-undownloaded))
5689      ((eq type 'download)
5690       (memq article gnus-newsgroup-downloadable))
5691      ((eq type 'unread)
5692       (memq article gnus-newsgroup-unreads))
5693      ((eq type 'read)
5694       (memq article gnus-newsgroup-reads))
5695      ((eq type 'dormant)
5696       (memq article gnus-newsgroup-dormant) )
5697      ((eq type 'expire)
5698       (memq article gnus-newsgroup-expirable))
5699      ((eq type 'reply)
5700       (memq article gnus-newsgroup-replied))
5701      ((eq type 'killed)
5702       (memq article gnus-newsgroup-killed))
5703      ((eq type 'bookmark)
5704       (assq article gnus-newsgroup-bookmarks))
5705      ((eq type 'score)
5706       (assq article gnus-newsgroup-scored))
5707      ((eq type 'save)
5708       (memq article gnus-newsgroup-saved))
5709      ((eq type 'cache)
5710       (memq article gnus-newsgroup-cached))
5711      ((eq type 'forward)
5712       (memq article gnus-newsgroup-forwarded))
5713      ((eq type 'seen)
5714       (not (memq article gnus-newsgroup-unseen)))
5715      ((eq type 'recent)
5716       (memq article gnus-newsgroup-recent))
5717      (t t))))
5718
5719 (defun gnus-articles-to-read (group &optional read-all)
5720   "Find out what articles the user wants to read."
5721   (let* ((articles
5722           ;; Select all articles if `read-all' is non-nil, or if there
5723           ;; are no unread articles.
5724           (if (or read-all
5725                   (and (zerop (length gnus-newsgroup-marked))
5726                        (zerop (length gnus-newsgroup-unreads)))
5727                   ;; Fetch all if the predicate is non-nil.
5728                   gnus-newsgroup-display)
5729               ;; We want to select the headers for all the articles in
5730               ;; the group, so we select either all the active
5731               ;; articles in the group, or (if that's nil), the
5732               ;; articles in the cache.
5733               (or
5734                (if gnus-newsgroup-maximum-articles
5735                    (let ((active (gnus-active group)))
5736                      (gnus-uncompress-range
5737                       (cons (max (car active)
5738                                  (- (cdr active)
5739                                     gnus-newsgroup-maximum-articles
5740                                     -1))
5741                             (cdr active))))
5742                  (gnus-uncompress-range (gnus-active group)))
5743                (gnus-cache-articles-in-group group))
5744             ;; Select only the "normal" subset of articles.
5745             (gnus-sorted-nunion
5746              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5747              gnus-newsgroup-unreads)))
5748          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5749          (scored (length scored-list))
5750          (number (length articles))
5751          (marked (+ (length gnus-newsgroup-marked)
5752                     (length gnus-newsgroup-dormant)))
5753          (select
5754           (cond
5755            ((numberp read-all)
5756             read-all)
5757            ((numberp gnus-newsgroup-display)
5758             gnus-newsgroup-display)
5759            (t
5760             (condition-case ()
5761                 (cond
5762                  ((and (or (<= scored marked) (= scored number))
5763                        (numberp gnus-large-newsgroup)
5764                        (> number gnus-large-newsgroup))
5765                   (let* ((cursor-in-echo-area nil)
5766                          (initial (gnus-parameter-large-newsgroup-initial
5767                                    gnus-newsgroup-name))
5768                          (input
5769                           (read-string
5770                            (format
5771                             "How many articles from %s (%s %d): "
5772                             (gnus-group-decoded-name gnus-newsgroup-name)
5773                             (if initial "max" "default")
5774                             number)
5775                            (if initial
5776                                (cons (number-to-string initial)
5777                                      0)))))
5778                     (if (string-match "^[ \t]*$" input) number input)))
5779                  ((and (> scored marked) (< scored number)
5780                        (> (- scored number) 20))
5781                   (let ((input
5782                          (read-string
5783                           (format "%s %s (%d scored, %d total): "
5784                                   "How many articles from"
5785                                   (gnus-group-decoded-name group)
5786                                   scored number))))
5787                     (if (string-match "^[ \t]*$" input)
5788                         number input)))
5789                  (t number))
5790               (quit
5791                (message "Quit getting the articles to read")
5792                nil))))))
5793     (setq select (if (stringp select) (string-to-number select) select))
5794     (if (or (null select) (zerop select))
5795         select
5796       (if (and (not (zerop scored)) (<= (abs select) scored))
5797           (progn
5798             (setq articles (sort scored-list '<))
5799             (setq number (length articles)))
5800         (setq articles (copy-sequence articles)))
5801
5802       (when (< (abs select) number)
5803         (if (< select 0)
5804             ;; Select the N oldest articles.
5805             (setcdr (nthcdr (1- (abs select)) articles) nil)
5806           ;; Select the N most recent articles.
5807           (setq articles (nthcdr (- number select) articles))))
5808       (setq gnus-newsgroup-unselected
5809             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5810       (when gnus-alter-articles-to-read-function
5811         (setq articles
5812               (sort
5813                (funcall gnus-alter-articles-to-read-function
5814                         gnus-newsgroup-name articles)
5815                '<)))
5816       articles)))
5817
5818 (defun gnus-killed-articles (killed articles)
5819   (let (out)
5820     (while articles
5821       (when (inline (gnus-member-of-range (car articles) killed))
5822         (push (car articles) out))
5823       (setq articles (cdr articles)))
5824     out))
5825
5826 (defun gnus-uncompress-marks (marks)
5827   "Uncompress the mark ranges in MARKS."
5828   (let ((uncompressed '(score bookmark))
5829         out)
5830     (while marks
5831       (if (memq (caar marks) uncompressed)
5832           (push (car marks) out)
5833         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5834       (setq marks (cdr marks)))
5835     out))
5836
5837 (defun gnus-article-mark-to-type (mark)
5838   "Return the type of MARK."
5839   (or (cadr (assq mark gnus-article-special-mark-lists))
5840       'list))
5841
5842 (defun gnus-article-unpropagatable-p (mark)
5843   "Return whether MARK should be propagated to back end."
5844   (memq mark gnus-article-unpropagated-mark-lists))
5845
5846 (defun gnus-adjust-marked-articles (info)
5847   "Set all article lists and remove all marks that are no longer valid."
5848   (let* ((marked-lists (gnus-info-marks info))
5849          (active (gnus-active (gnus-info-group info)))
5850          (min (car active))
5851          (max (cdr active))
5852          (types gnus-article-mark-lists)
5853          marks var articles article mark mark-type
5854          bgn end)
5855
5856     (dolist (marks marked-lists)
5857       (setq mark (car marks)
5858             mark-type (gnus-article-mark-to-type mark)
5859             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5860
5861       ;; We set the variable according to the type of the marks list,
5862       ;; and then adjust the marks to a subset of the active articles.
5863       (cond
5864        ;; Adjust "simple" lists - compressed yet unsorted
5865        ((eq mark-type 'list)
5866         ;; Simultaneously uncompress and clip to active range
5867         ;; See gnus-uncompress-range for a description of possible marks
5868         (let (l lh)
5869           (if (not (cadr marks))
5870               (set var nil)
5871             (setq articles (if (numberp (cddr marks))
5872                                (list (cdr marks))
5873                              (cdr marks))
5874                   lh (cons nil nil)
5875                   l lh)
5876
5877             (while (setq article (pop articles))
5878               (cond ((consp article)
5879                      (setq bgn (max (car article) min)
5880                            end (min (cdr article) max))
5881                      (while (<= bgn end)
5882                        (setq l (setcdr l (cons bgn nil))
5883                              bgn (1+ bgn))))
5884                     ((and (<= min article)
5885                           (>= max article))
5886                      (setq l (setcdr l (cons article nil))))))
5887             (set var (cdr lh)))))
5888        ;; Adjust assocs.
5889        ((eq mark-type 'tuple)
5890         (set var (setq articles (cdr marks)))
5891         (when (not (listp (cdr (symbol-value var))))
5892           (set var (list (symbol-value var))))
5893         (when (not (listp (cdr articles)))
5894           (setq articles (list articles)))
5895         (while articles
5896           (when (or (not (consp (setq article (pop articles))))
5897                     (< (car article) min)
5898                     (> (car article) max))
5899             (set var (delq article (symbol-value var))))))
5900        ;; Adjust ranges (sloppily).
5901        ((eq mark-type 'range)
5902         (cond
5903          ((eq mark 'seen)
5904           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5905           ;; It should be (seen (NUM1 . NUM2)).
5906           (when (numberp (cddr marks))
5907             (setcdr marks (list (cdr marks))))
5908           (setq articles (cdr marks))
5909           (while (and articles
5910                       (or (and (consp (car articles))
5911                                (> min (cdar articles)))
5912                           (and (numberp (car articles))
5913                                (> min (car articles)))))
5914             (pop articles))
5915           (set var articles))))))))
5916
5917 (defun gnus-update-missing-marks (missing)
5918   "Go through the list of MISSING articles and remove them from the mark lists."
5919   (when missing
5920     (let (var m)
5921       ;; Go through all types.
5922       (dolist (elem gnus-article-mark-lists)
5923         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5924           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5925           (when (symbol-value var)
5926             ;; This list has articles.  So we delete all missing
5927             ;; articles from it.
5928             (setq m missing)
5929             (while m
5930               (set var (delq (pop m) (symbol-value var))))))))))
5931
5932 (defun gnus-update-marks ()
5933   "Enter the various lists of marked articles into the newsgroup info list."
5934   (let ((types gnus-article-mark-lists)
5935         (info (gnus-get-info gnus-newsgroup-name))
5936         type list newmarked symbol delta-marks)
5937     (when info
5938       ;; Add all marks lists to the list of marks lists.
5939       (while (setq type (pop types))
5940         (setq list (symbol-value
5941                     (setq symbol
5942                           (intern (format "gnus-newsgroup-%s" (car type))))))
5943
5944         (when list
5945           ;; Get rid of the entries of the articles that have the
5946           ;; default score.
5947           (when (and (eq (cdr type) 'score)
5948                      gnus-save-score
5949                      list)
5950             (let* ((arts list)
5951                    (prev (cons nil list))
5952                    (all prev))
5953               (while arts
5954                 (if (or (not (consp (car arts)))
5955                         (= (cdar arts) gnus-summary-default-score))
5956                     (setcdr prev (cdr arts))
5957                   (setq prev arts))
5958                 (setq arts (cdr arts)))
5959               (setq list (cdr all)))))
5960
5961         (when (eq (cdr type) 'seen)
5962           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5963
5964         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5965           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5966
5967         (when (and (gnus-check-backend-function
5968                     'request-set-mark gnus-newsgroup-name)
5969                    (not (gnus-article-unpropagatable-p (cdr type))))
5970           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5971                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5972                  (add (gnus-remove-from-range
5973                        (gnus-copy-sequence list) old)))
5974             (when add
5975               (push (list add 'add (list (cdr type))) delta-marks))
5976             (when del
5977               (push (list del 'del (list (cdr type))) delta-marks))))
5978
5979         (when list
5980           (push (cons (cdr type) list) newmarked)))
5981
5982       (when delta-marks
5983         (unless (gnus-check-group gnus-newsgroup-name)
5984           (error "Can't open server for %s" gnus-newsgroup-name))
5985         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5986
5987       ;; Enter these new marks into the info of the group.
5988       (if (nthcdr 3 info)
5989           (setcar (nthcdr 3 info) newmarked)
5990         ;; Add the marks lists to the end of the info.
5991         (when newmarked
5992           (setcdr (nthcdr 2 info) (list newmarked))))
5993
5994       ;; Cut off the end of the info if there's nothing else there.
5995       (let ((i 5))
5996         (while (and (> i 2)
5997                     (not (nth i info)))
5998           (when (nthcdr (decf i) info)
5999             (setcdr (nthcdr i info) nil)))))))
6000
6001 (defun gnus-set-mode-line (where)
6002   "Set the mode line of the article or summary buffers.
6003 If WHERE is `summary', the summary mode line format will be used."
6004   ;; Is this mode line one we keep updated?
6005   (when (and (memq where gnus-updated-mode-lines)
6006              (symbol-value
6007               (intern (format "gnus-%s-mode-line-format-spec" where))))
6008     (let (mode-string)
6009       ;; We evaluate this in the summary buffer since these
6010       ;; variables are buffer-local to that buffer.
6011       (with-current-buffer gnus-summary-buffer
6012         ;; We bind all these variables that are used in the `eval' form
6013         ;; below.
6014         (let* ((mformat (symbol-value
6015                          (intern
6016                           (format "gnus-%s-mode-line-format-spec" where))))
6017                (gnus-tmp-group-name (gnus-mode-string-quote
6018                                      (gnus-group-decoded-name
6019                                       gnus-newsgroup-name)))
6020                (gnus-tmp-article-number (or gnus-current-article 0))
6021                (gnus-tmp-unread gnus-newsgroup-unreads)
6022                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6023                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6024                (gnus-tmp-unread-and-unselected
6025                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6026                             (zerop gnus-tmp-unselected))
6027                        "")
6028                       ((zerop gnus-tmp-unselected)
6029                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6030                       (t (format "{%d(+%d) more}"
6031                                  gnus-tmp-unread-and-unticked
6032                                  gnus-tmp-unselected))))
6033                (gnus-tmp-subject
6034                 (if (and gnus-current-headers
6035                          (vectorp gnus-current-headers))
6036                     (gnus-mode-string-quote
6037                      (mail-header-subject gnus-current-headers))
6038                   ""))
6039                bufname-length max-len
6040                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6041           (setq mode-string (eval mformat))
6042           (setq bufname-length (if (string-match "%b" mode-string)
6043                                    (- (length
6044                                        (buffer-name
6045                                         (if (eq where 'summary)
6046                                             nil
6047                                           (get-buffer gnus-article-buffer))))
6048                                       2)
6049                                  0))
6050           (setq max-len (max 4 (if gnus-mode-non-string-length
6051                                    (- (window-width)
6052                                       gnus-mode-non-string-length
6053                                       bufname-length)
6054                                  (length mode-string))))
6055           ;; We might have to chop a bit of the string off...
6056           (when (> (length mode-string) max-len)
6057             (setq mode-string
6058                   (concat (truncate-string-to-width mode-string (- max-len 3))
6059                           "...")))
6060           ;; Pad the mode string a bit.
6061           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6062       ;; Update the mode line.
6063       (setq mode-line-buffer-identification
6064             (gnus-mode-line-buffer-identification (list mode-string)))
6065       (set-buffer-modified-p t))))
6066
6067 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6068   "Go through the HEADERS list and add all Xrefs to a hash table.
6069 The resulting hash table is returned, or nil if no Xrefs were found."
6070   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6071          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6072          (xref-hashtb (gnus-make-hashtable))
6073          start group entry number xrefs header)
6074     (while headers
6075       (setq header (pop headers))
6076       (when (and (setq xrefs (mail-header-xref header))
6077                  (not (memq (setq number (mail-header-number header))
6078                             unreads)))
6079         (setq start 0)
6080         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6081           (setq start (match-end 0))
6082           (setq group (if prefix
6083                           (concat prefix (substring xrefs (match-beginning 1)
6084                                                     (match-end 1)))
6085                         (substring xrefs (match-beginning 1) (match-end 1))))
6086           (setq number
6087                 (string-to-number (substring xrefs (match-beginning 2)
6088                                           (match-end 2))))
6089           (if (setq entry (gnus-gethash group xref-hashtb))
6090               (setcdr entry (cons number (cdr entry)))
6091             (gnus-sethash group (cons number nil) xref-hashtb)))))
6092     (and start xref-hashtb)))
6093
6094 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6095   "Look through all the headers and mark the Xrefs as read."
6096   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6097         name info xref-hashtb idlist method nth4)
6098     (with-current-buffer gnus-group-buffer
6099       (when (setq xref-hashtb
6100                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6101         (mapatoms
6102          (lambda (group)
6103            (unless (string= from-newsgroup (setq name (symbol-name group)))
6104              (setq idlist (symbol-value group))
6105              ;; Dead groups are not updated.
6106              (and (prog1
6107                       (setq info (gnus-get-info name))
6108                     (when (stringp (setq nth4 (gnus-info-method info)))
6109                       (setq nth4 (gnus-server-to-method nth4))))
6110                   ;; Only do the xrefs if the group has the same
6111                   ;; select method as the group we have just read.
6112                   (or (gnus-methods-equal-p
6113                        nth4 (gnus-find-method-for-group from-newsgroup))
6114                       virtual
6115                       (equal nth4 (setq method (gnus-find-method-for-group
6116                                                 from-newsgroup)))
6117                       (and (equal (car nth4) (car method))
6118                            (equal (nth 1 nth4) (nth 1 method))))
6119                   gnus-use-cross-reference
6120                   (or (not (eq gnus-use-cross-reference t))
6121                       virtual
6122                       ;; Only do cross-references on subscribed
6123                       ;; groups, if that is what is wanted.
6124                       (<= (gnus-info-level info) gnus-level-subscribed))
6125                   (gnus-group-make-articles-read name idlist))))
6126          xref-hashtb)))))
6127
6128 (defun gnus-compute-read-articles (group articles)
6129   (let* ((entry (gnus-group-entry group))
6130          (info (nth 2 entry))
6131          (active (gnus-active group))
6132          ninfo)
6133     (when entry
6134       ;; First peel off all invalid article numbers.
6135       (when active
6136         (let ((ids articles)
6137               id first)
6138           (while (setq id (pop ids))
6139             (when (and first (> id (cdr active)))
6140               ;; We'll end up in this situation in one particular
6141               ;; obscure situation.  If you re-scan a group and get
6142               ;; a new article that is cross-posted to a different
6143               ;; group that has not been re-scanned, you might get
6144               ;; crossposted article that has a higher number than
6145               ;; Gnus believes possible.  So we re-activate this
6146               ;; group as well.  This might mean doing the
6147               ;; crossposting thingy will *increase* the number
6148               ;; of articles in some groups.  Tsk, tsk.
6149               (setq active (or (gnus-activate-group group) active)))
6150             (when (or (> id (cdr active))
6151                       (< id (car active)))
6152               (setq articles (delq id articles))))))
6153       ;; If the read list is nil, we init it.
6154       (if (and active
6155                (null (gnus-info-read info))
6156                (> (car active) 1))
6157           (setq ninfo (cons 1 (1- (car active))))
6158         (setq ninfo (gnus-info-read info)))
6159       ;; Then we add the read articles to the range.
6160       (gnus-add-to-range
6161        ninfo (setq articles (sort articles '<))))))
6162
6163 (defun gnus-group-make-articles-read (group articles)
6164   "Update the info of GROUP to say that ARTICLES are read."
6165   (let* ((num 0)
6166          (entry (gnus-group-entry group))
6167          (info (nth 2 entry))
6168          (active (gnus-active group))
6169          range)
6170     (when entry
6171       (setq range (gnus-compute-read-articles group articles))
6172       (with-current-buffer gnus-group-buffer
6173         (gnus-undo-register
6174           `(progn
6175              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6176              (gnus-info-set-read ',info ',(gnus-info-read info))
6177              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6178              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6179              (gnus-group-update-group ,group t))))
6180       ;; Add the read articles to the range.
6181       (gnus-info-set-read info range)
6182       (gnus-request-set-mark group (list (list range 'add '(read))))
6183       ;; Then we have to re-compute how many unread
6184       ;; articles there are in this group.
6185       (when active
6186         (cond
6187          ((not range)
6188           (setq num (- (1+ (cdr active)) (car active))))
6189          ((not (listp (cdr range)))
6190           (setq num (- (cdr active) (- (1+ (cdr range))
6191                                        (car range)))))
6192          (t
6193           (while range
6194             (if (numberp (car range))
6195                 (setq num (1+ num))
6196               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6197             (setq range (cdr range)))
6198           (setq num (- (cdr active) num))))
6199         ;; Update the number of unread articles.
6200         (setcar entry num)
6201         ;; Update the group buffer.
6202         (unless (gnus-ephemeral-group-p group)
6203           (gnus-group-update-group group t))))))
6204
6205 (defvar gnus-newsgroup-none-id 0)
6206
6207 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6208   (let ((cur nntp-server-buffer)
6209         (dependencies
6210          (or dependencies
6211              (with-current-buffer gnus-summary-buffer
6212                gnus-newsgroup-dependencies)))
6213         headers id end ref number
6214         (mail-parse-charset gnus-newsgroup-charset)
6215         (mail-parse-ignored-charsets
6216          (save-current-buffer (condition-case nil
6217                                   (set-buffer gnus-summary-buffer)
6218                                 (error))
6219                               gnus-newsgroup-ignored-charsets)))
6220     (with-current-buffer nntp-server-buffer
6221       ;; Translate all TAB characters into SPACE characters.
6222       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6223       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6224       (ietf-drums-unfold-fws)
6225       (gnus-run-hooks 'gnus-parse-headers-hook)
6226       (let ((case-fold-search t)
6227             in-reply-to header p lines chars)
6228         (goto-char (point-min))
6229         ;; Search to the beginning of the next header.  Error messages
6230         ;; do not begin with 2 or 3.
6231         (while (re-search-forward "^[23][0-9]+ " nil t)
6232           (setq id nil
6233                 ref nil)
6234           ;; This implementation of this function, with nine
6235           ;; search-forwards instead of the one re-search-forward and
6236           ;; a case (which basically was the old function) is actually
6237           ;; about twice as fast, even though it looks messier.  You
6238           ;; can't have everything, I guess.  Speed and elegance
6239           ;; doesn't always go hand in hand.
6240           (setq
6241            header
6242            (vector
6243             ;; Number.
6244             (prog1
6245                 (setq number (read cur))
6246               (end-of-line)
6247               (setq p (point))
6248               (narrow-to-region (point)
6249                                 (or (and (search-forward "\n.\n" nil t)
6250                                          (- (point) 2))
6251                                     (point))))
6252             ;; Subject.
6253             (progn
6254               (goto-char p)
6255               (if (search-forward "\nsubject:" nil t)
6256                   (funcall gnus-decode-encoded-word-function
6257                            (nnheader-header-value))
6258                 "(none)"))
6259             ;; From.
6260             (progn
6261               (goto-char p)
6262               (if (search-forward "\nfrom:" nil t)
6263                   (funcall gnus-decode-encoded-address-function
6264                            (nnheader-header-value))
6265                 "(nobody)"))
6266             ;; Date.
6267             (progn
6268               (goto-char p)
6269               (if (search-forward "\ndate:" nil t)
6270                   (nnheader-header-value) ""))
6271             ;; Message-ID.
6272             (progn
6273               (goto-char p)
6274               (setq id (if (re-search-forward
6275                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6276                            ;; We do it this way to make sure the Message-ID
6277                            ;; is (somewhat) syntactically valid.
6278                            (buffer-substring (match-beginning 1)
6279                                              (match-end 1))
6280                          ;; If there was no message-id, we just fake one
6281                          ;; to make subsequent routines simpler.
6282                          (nnheader-generate-fake-message-id number))))
6283             ;; References.
6284             (progn
6285               (goto-char p)
6286               (if (search-forward "\nreferences:" nil t)
6287                   (progn
6288                     (setq end (point))
6289                     (prog1
6290                         (nnheader-header-value)
6291                       (setq ref
6292                             (buffer-substring
6293                              (progn
6294                                (end-of-line)
6295                                (search-backward ">" end t)
6296                                (1+ (point)))
6297                              (progn
6298                                (search-backward "<" end t)
6299                                (point))))))
6300                 ;; Get the references from the in-reply-to header if there
6301                 ;; were no references and the in-reply-to header looks
6302                 ;; promising.
6303                 (if (and (search-forward "\nin-reply-to:" nil t)
6304                          (setq in-reply-to (nnheader-header-value))
6305                          (string-match "<[^>]+>" in-reply-to))
6306                     (let (ref2)
6307                       (setq ref (substring in-reply-to (match-beginning 0)
6308                                            (match-end 0)))
6309                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6310                         (setq ref2 (substring in-reply-to (match-beginning 0)
6311                                               (match-end 0)))
6312                         (when (> (length ref2) (length ref))
6313                           (setq ref ref2)))
6314                       ref)
6315                   (setq ref nil))))
6316             ;; Chars.
6317             (progn
6318               (goto-char p)
6319               (if (search-forward "\nchars: " nil t)
6320                   (if (numberp (setq chars (ignore-errors (read cur))))
6321                       chars -1)
6322                 -1))
6323             ;; Lines.
6324             (progn
6325               (goto-char p)
6326               (if (search-forward "\nlines: " nil t)
6327                   (if (numberp (setq lines (ignore-errors (read cur))))
6328                       lines -1)
6329                 -1))
6330             ;; Xref.
6331             (progn
6332               (goto-char p)
6333               (and (search-forward "\nxref:" nil t)
6334                    (nnheader-header-value)))
6335             ;; Extra.
6336             (when gnus-extra-headers
6337               (let ((extra gnus-extra-headers)
6338                     out)
6339                 (while extra
6340                   (goto-char p)
6341                   (when (search-forward
6342                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6343                     (push (cons (car extra) (nnheader-header-value))
6344                           out))
6345                   (pop extra))
6346                 out))))
6347           (when (equal id ref)
6348             (setq ref nil))
6349
6350           (when gnus-alter-header-function
6351             (funcall gnus-alter-header-function header)
6352             (setq id (mail-header-id header)
6353                   ref (gnus-parent-id (mail-header-references header))))
6354
6355           (when (setq header
6356                       (gnus-dependencies-add-header
6357                        header dependencies force-new))
6358             (push header headers))
6359           (goto-char (point-max))
6360           (widen))
6361         (nreverse headers)))))
6362
6363 ;; Goes through the xover lines and returns a list of vectors
6364 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6365                                                   force-new dependencies
6366                                                   group also-fetch-heads)
6367   "Parse the news overview data in the server buffer.
6368 Return a list of headers that match SEQUENCE (see
6369 `nntp-retrieve-headers')."
6370   ;; Get the Xref when the users reads the articles since most/some
6371   ;; NNTP servers do not include Xrefs when using XOVER.
6372   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6373   (let ((mail-parse-charset gnus-newsgroup-charset)
6374         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6375         (cur nntp-server-buffer)
6376         (dependencies (or dependencies gnus-newsgroup-dependencies))
6377         (allp (cond
6378                ((eq gnus-read-all-available-headers t)
6379                 t)
6380                ((and (stringp gnus-read-all-available-headers)
6381                      group)
6382                 (string-match gnus-read-all-available-headers group))
6383                (t
6384                 nil)))
6385         number headers header)
6386     (with-current-buffer nntp-server-buffer
6387       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6388       ;; Allow the user to mangle the headers before parsing them.
6389       (gnus-run-hooks 'gnus-parse-headers-hook)
6390       (goto-char (point-min))
6391       (gnus-parse-without-error
6392         (while (and (or sequence allp)
6393                     (not (eobp)))
6394           (setq number (read cur))
6395           (when (not allp)
6396             (while (and sequence
6397                         (< (car sequence) number))
6398               (setq sequence (cdr sequence))))
6399           (when (and (or allp
6400                          (and sequence
6401                               (eq number (car sequence))))
6402                      (progn
6403                        (setq sequence (cdr sequence))
6404                        (setq header (inline
6405                                       (gnus-nov-parse-line
6406                                        number dependencies force-new)))))
6407             (push header headers))
6408           (forward-line 1)))
6409       ;; A common bug in inn is that if you have posted an article and
6410       ;; then retrieves the active file, it will answer correctly --
6411       ;; the new article is included.  However, a NOV entry for the
6412       ;; article may not have been generated yet, so this may fail.
6413       ;; We work around this problem by retrieving the last few
6414       ;; headers using HEAD.
6415       (if (or (not also-fetch-heads)
6416               (not sequence))
6417           ;; We (probably) got all the headers.
6418           (nreverse headers)
6419         (let ((gnus-nov-is-evil t))
6420           (nconc
6421            (nreverse headers)
6422            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6423              (gnus-get-newsgroup-headers))))))))
6424
6425 (defun gnus-article-get-xrefs ()
6426   "Fill in the Xref value in `gnus-current-headers', if necessary.
6427 This is meant to be called in `gnus-article-internal-prepare-hook'."
6428   (let ((headers (with-current-buffer gnus-summary-buffer
6429                    gnus-current-headers)))
6430     (or (not gnus-use-cross-reference)
6431         (not headers)
6432         (and (mail-header-xref headers)
6433              (not (string= (mail-header-xref headers) "")))
6434         (let ((case-fold-search t)
6435               xref)
6436           (save-restriction
6437             (nnheader-narrow-to-headers)
6438             (goto-char (point-min))
6439             (when (or (and (not (eobp))
6440                            (eq (downcase (char-after)) ?x)
6441                            (looking-at "Xref:"))
6442                       (search-forward "\nXref:" nil t))
6443               (goto-char (1+ (match-end 0)))
6444               (setq xref (buffer-substring (point) (point-at-eol)))
6445               (mail-header-set-xref headers xref)))))))
6446
6447 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6448   "Find article ID and insert the summary line for that article.
6449 OLD-HEADER can either be a header or a line number to insert
6450 the subject line on."
6451   (let* ((line (and (numberp old-header) old-header))
6452          (old-header (and (vectorp old-header) old-header))
6453          (header (cond ((and old-header use-old-header)
6454                         old-header)
6455                        ((and (numberp id)
6456                              (gnus-number-to-header id))
6457                         (gnus-number-to-header id))
6458                        (t
6459                         (gnus-read-header id))))
6460          (number (and (numberp id) id))
6461          d)
6462     (when header
6463       ;; Rebuild the thread that this article is part of and go to the
6464       ;; article we have fetched.
6465       (when (and (not gnus-show-threads)
6466                  old-header)
6467         (when (and number
6468                    (setq d (gnus-data-find (mail-header-number old-header))))
6469           (goto-char (gnus-data-pos d))
6470           (gnus-data-remove
6471            number
6472            (- (point-at-bol)
6473               (prog1
6474                   (1+ (point-at-eol))
6475                 (gnus-delete-line))))))
6476       ;; Remove list identifiers from subject.
6477       (when gnus-list-identifiers
6478         (let ((gnus-newsgroup-headers (list header)))
6479           (gnus-summary-remove-list-identifiers)))
6480       (when old-header
6481         (mail-header-set-number header (mail-header-number old-header)))
6482       (setq gnus-newsgroup-sparse
6483             (delq (setq number (mail-header-number header))
6484                   gnus-newsgroup-sparse))
6485       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6486       (push number gnus-newsgroup-limit)
6487       (gnus-rebuild-thread (mail-header-id header) line)
6488       (gnus-summary-goto-subject number nil t))
6489     (when (and (numberp number)
6490                (> number 0))
6491       ;; We have to update the boundaries even if we can't fetch the
6492       ;; article if ID is a number -- so that the next `P' or `N'
6493       ;; command will fetch the previous (or next) article even
6494       ;; if the one we tried to fetch this time has been canceled.
6495       (when (> number gnus-newsgroup-end)
6496         (setq gnus-newsgroup-end number))
6497       (when (< number gnus-newsgroup-begin)
6498         (setq gnus-newsgroup-begin number))
6499       (setq gnus-newsgroup-unselected
6500             (delq number gnus-newsgroup-unselected)))
6501     ;; Report back a success?
6502     (and header (mail-header-number header))))
6503
6504 ;;; Process/prefix in the summary buffer
6505
6506 (defun gnus-summary-work-articles (n)
6507   "Return a list of articles to be worked upon.
6508 The prefix argument, the list of process marked articles, and the
6509 current article will be taken into consideration."
6510   (with-current-buffer gnus-summary-buffer
6511     (cond
6512      (n
6513       ;; A numerical prefix has been given.
6514       (setq n (prefix-numeric-value n))
6515       (let ((backward (< n 0))
6516             (n (abs (prefix-numeric-value n)))
6517             articles article)
6518         (save-excursion
6519           (while
6520               (and (> n 0)
6521                    (push (setq article (gnus-summary-article-number))
6522                          articles)
6523                    (if backward
6524                        (gnus-summary-find-prev nil article)
6525                      (gnus-summary-find-next nil article)))
6526             (decf n)))
6527         (nreverse articles)))
6528      ((and (gnus-region-active-p) (mark))
6529       (message "region active")
6530       ;; Work on the region between point and mark.
6531       (let ((max (max (point) (mark)))
6532             articles article)
6533         (save-excursion
6534           (goto-char (min (point) (mark)))
6535           (while
6536               (and
6537                (push (setq article (gnus-summary-article-number)) articles)
6538                (gnus-summary-find-next nil article)
6539                (< (point) max)))
6540           (nreverse articles))))
6541      (gnus-newsgroup-processable
6542       ;; There are process-marked articles present.
6543       ;; Save current state.
6544       (gnus-summary-save-process-mark)
6545       ;; Return the list.
6546       (reverse gnus-newsgroup-processable))
6547      (t
6548       ;; Just return the current article.
6549       (list (gnus-summary-article-number))))))
6550
6551 (defmacro gnus-summary-iterate (arg &rest forms)
6552   "Iterate over the process/prefixed articles and do FORMS.
6553 ARG is the interactive prefix given to the command.  FORMS will be
6554 executed with point over the summary line of the articles."
6555   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6556     `(let ((,articles (gnus-summary-work-articles ,arg)))
6557        (while ,articles
6558          (gnus-summary-goto-subject (car ,articles))
6559          ,@forms
6560          (pop ,articles)))))
6561
6562 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6563 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6564
6565 (defun gnus-summary-save-process-mark ()
6566   "Push the current set of process marked articles on the stack."
6567   (interactive)
6568   (push (copy-sequence gnus-newsgroup-processable)
6569         gnus-newsgroup-process-stack))
6570
6571 (defun gnus-summary-kill-process-mark ()
6572   "Push the current set of process marked articles on the stack and unmark."
6573   (interactive)
6574   (gnus-summary-save-process-mark)
6575   (gnus-summary-unmark-all-processable))
6576
6577 (defun gnus-summary-yank-process-mark ()
6578   "Pop the last process mark state off the stack and restore it."
6579   (interactive)
6580   (unless gnus-newsgroup-process-stack
6581     (error "Empty mark stack"))
6582   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6583
6584 (defun gnus-summary-process-mark-set (set)
6585   "Make SET into the current process marked articles."
6586   (gnus-summary-unmark-all-processable)
6587   (mapc 'gnus-summary-set-process-mark set))
6588
6589 ;;; Searching and stuff
6590
6591 (defun gnus-summary-search-group (&optional backward use-level)
6592   "Search for next unread newsgroup.
6593 If optional argument BACKWARD is non-nil, search backward instead."
6594   (with-current-buffer gnus-group-buffer
6595     (when (gnus-group-search-forward
6596            backward nil (if use-level (gnus-group-group-level) nil))
6597       (gnus-group-group-name))))
6598
6599 (defun gnus-summary-best-group (&optional exclude-group)
6600   "Find the name of the best unread group.
6601 If EXCLUDE-GROUP, do not go to this group."
6602   (with-current-buffer gnus-group-buffer
6603     (save-excursion
6604       (gnus-group-best-unread-group exclude-group))))
6605
6606 (defun gnus-summary-find-next (&optional unread article backward)
6607   (if backward
6608       (gnus-summary-find-prev unread article)
6609     (let* ((dummy (gnus-summary-article-intangible-p))
6610            (article (or article (gnus-summary-article-number)))
6611            (data (gnus-data-find-list article))
6612            result)
6613       (when (and (not dummy)
6614                  (or (not gnus-summary-check-current)
6615                      (not unread)
6616                      (not (gnus-data-unread-p (car data)))))
6617         (setq data (cdr data)))
6618       (when (setq result
6619                   (if unread
6620                       (progn
6621                         (while data
6622                           (unless (memq (gnus-data-number (car data))
6623                                         (cond
6624                                          ((eq gnus-auto-goto-ignores
6625                                               'always-undownloaded)
6626                                           gnus-newsgroup-undownloaded)
6627                                          (gnus-plugged
6628                                           nil)
6629                                          ((eq gnus-auto-goto-ignores
6630                                               'unfetched)
6631                                           gnus-newsgroup-unfetched)
6632                                          ((eq gnus-auto-goto-ignores
6633                                               'undownloaded)
6634                                           gnus-newsgroup-undownloaded)))
6635                             (when (gnus-data-unread-p (car data))
6636                               (setq result (car data)
6637                                     data nil)))
6638                           (setq data (cdr data)))
6639                         result)
6640                     (car data)))
6641         (goto-char (gnus-data-pos result))
6642         (gnus-data-number result)))))
6643
6644 (defun gnus-summary-find-prev (&optional unread article)
6645   (let* ((eobp (eobp))
6646          (article (or article (gnus-summary-article-number)))
6647          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6648          result)
6649     (when (and (not eobp)
6650                (or (not gnus-summary-check-current)
6651                    (not unread)
6652                    (not (gnus-data-unread-p (car data)))))
6653       (setq data (cdr data)))
6654     (when (setq result
6655                 (if unread
6656                     (progn
6657                       (while data
6658                         (unless (memq (gnus-data-number (car data))
6659                                       (cond
6660                                        ((eq gnus-auto-goto-ignores
6661                                             'always-undownloaded)
6662                                         gnus-newsgroup-undownloaded)
6663                                        (gnus-plugged
6664                                         nil)
6665                                        ((eq gnus-auto-goto-ignores
6666                                             'unfetched)
6667                                         gnus-newsgroup-unfetched)
6668                                        ((eq gnus-auto-goto-ignores
6669                                             'undownloaded)
6670                                         gnus-newsgroup-undownloaded)))
6671                           (when (gnus-data-unread-p (car data))
6672                             (setq result (car data)
6673                                   data nil)))
6674                         (setq data (cdr data)))
6675                       result)
6676                   (car data)))
6677       (goto-char (gnus-data-pos result))
6678       (gnus-data-number result))))
6679
6680 (defun gnus-summary-find-subject (subject &optional unread backward article)
6681   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6682          (article (or article (gnus-summary-article-number)))
6683          (articles (gnus-data-list backward))
6684          (arts (gnus-data-find-list article articles))
6685          result)
6686     (when (or (not gnus-summary-check-current)
6687               (not unread)
6688               (not (gnus-data-unread-p (car arts))))
6689       (setq arts (cdr arts)))
6690     (while arts
6691       (and (or (not unread)
6692                (gnus-data-unread-p (car arts)))
6693            (vectorp (gnus-data-header (car arts)))
6694            (gnus-subject-equal
6695             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6696            (setq result (car arts)
6697                  arts nil))
6698       (setq arts (cdr arts)))
6699     (and result
6700          (goto-char (gnus-data-pos result))
6701          (gnus-data-number result))))
6702
6703 (defun gnus-summary-search-forward (&optional unread subject backward)
6704   "Search forward for an article.
6705 If UNREAD, look for unread articles.  If SUBJECT, look for
6706 articles with that subject.  If BACKWARD, search backward instead."
6707   (cond (subject (gnus-summary-find-subject subject unread backward))
6708         (backward (gnus-summary-find-prev unread))
6709         (t (gnus-summary-find-next unread))))
6710
6711 (defun gnus-recenter (&optional n)
6712   "Center point in window and redisplay frame.
6713 Also do horizontal recentering."
6714   (interactive "P")
6715   (when (and gnus-auto-center-summary
6716              (not (eq gnus-auto-center-summary 'vertical)))
6717     (gnus-horizontal-recenter))
6718   (if (fboundp 'recenter-top-bottom)
6719       (recenter-top-bottom n)
6720     (recenter n)))
6721
6722 (put 'gnus-recenter 'isearch-scroll t)
6723
6724 (defun gnus-forward-line-ignore-invisible (n)
6725   "Move N lines forward (backward if N is negative).
6726 Like forward-line, but skip over (and don't count) invisible lines."
6727   (let (done)
6728     (while (and (> n 0) (not done))
6729       ;; If the following character is currently invisible,
6730       ;; skip all characters with that same `invisible' property value.
6731       (while (gnus-invisible-p (point))
6732         (goto-char (gnus-next-char-property-change (point))))
6733       (forward-line 1)
6734       (if (eobp)
6735           (setq done t)
6736         (setq n (1- n))))
6737     (while (and (< n 0) (not done))
6738       (forward-line -1)
6739       (if (bobp) (setq done t)
6740         (setq n (1+ n))
6741         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6742           (goto-char (gnus-previous-char-property-change (point))))))))
6743
6744 (defun gnus-summary-recenter ()
6745   "Center point in the summary window.
6746 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6747 displayed, no centering will be performed."
6748   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6749   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6750   (interactive)
6751   ;; The user has to want it.
6752   (when gnus-auto-center-summary
6753     (let* ((top (cond ((< (window-height) 4) 0)
6754                       ((< (window-height) 7) 1)
6755                       (t (if (numberp gnus-auto-center-summary)
6756                              gnus-auto-center-summary
6757                            (/ (1- (window-height)) 2)))))
6758            (height (1- (window-height)))
6759            (bottom (save-excursion
6760                      (goto-char (point-max))
6761                      (gnus-forward-line-ignore-invisible (- height))
6762                      (point)))
6763            (window (get-buffer-window (current-buffer))))
6764       (when (get-buffer-window gnus-article-buffer)
6765         ;; Only do recentering when the article buffer is displayed,
6766         ;; Set the window start to either `bottom', which is the biggest
6767         ;; possible valid number, or the second line from the top,
6768         ;; whichever is the least.
6769         (let ((top-pos (save-excursion
6770                          (gnus-forward-line-ignore-invisible (- top))
6771                          (point))))
6772           (if (> bottom top-pos)
6773               ;; Keep the second line from the top visible
6774               (set-window-start window top-pos)
6775             ;; Try to keep the bottom line visible; if it's partially
6776             ;; obscured, either scroll one more line to make it fully
6777             ;; visible, or revert to using TOP-POS.
6778             (save-excursion
6779               (goto-char (point-max))
6780               (gnus-forward-line-ignore-invisible -1)
6781               (let ((last-line-start (point)))
6782                 (goto-char bottom)
6783                 (set-window-start window (point) t)
6784                 (when (not (pos-visible-in-window-p last-line-start window))
6785                   (gnus-forward-line-ignore-invisible 1)
6786                   (set-window-start window (min (point) top-pos) t)))))))
6787       ;; Do horizontal recentering while we're at it.
6788       (when (and (get-buffer-window (current-buffer) t)
6789                  (not (eq gnus-auto-center-summary 'vertical)))
6790         (let ((selected (selected-window)))
6791           (select-window (get-buffer-window (current-buffer) t))
6792           (gnus-summary-position-point)
6793           (gnus-horizontal-recenter)
6794           (select-window selected))))))
6795
6796 (defun gnus-summary-jump-to-group (newsgroup)
6797   "Move point to NEWSGROUP in group mode buffer."
6798   ;; Keep update point of group mode buffer if visible.
6799   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6800       (save-window-excursion
6801         ;; Take care of tree window mode.
6802         (when (get-buffer-window gnus-group-buffer)
6803           (pop-to-buffer gnus-group-buffer))
6804         (gnus-group-jump-to-group newsgroup))
6805     (save-excursion
6806       ;; Take care of tree window mode.
6807       (if (get-buffer-window gnus-group-buffer 0)
6808           (pop-to-buffer gnus-group-buffer)
6809         (set-buffer gnus-group-buffer))
6810       (gnus-group-jump-to-group newsgroup))))
6811
6812 ;; This function returns a list of article numbers based on the
6813 ;; difference between the ranges of read articles in this group and
6814 ;; the range of active articles.
6815 (defun gnus-list-of-unread-articles (group)
6816   (let* ((read (gnus-info-read (gnus-get-info group)))
6817          (active (or (gnus-active group) (gnus-activate-group group)))
6818          (last (or (cdr active)
6819                    (error "Group %s couldn't be activated " group)))
6820          (bottom (if gnus-newsgroup-maximum-articles
6821                      (max (car active)
6822                           (- last gnus-newsgroup-maximum-articles -1))
6823                    (car active)))
6824          first nlast unread)
6825     ;; If none are read, then all are unread.
6826     (if (not read)
6827         (setq first bottom)
6828       ;; If the range of read articles is a single range, then the
6829       ;; first unread article is the article after the last read
6830       ;; article.  Sounds logical, doesn't it?
6831       (if (and (not (listp (cdr read)))
6832                (or (< (car read) bottom)
6833                    (progn (setq read (list read))
6834                           nil)))
6835           (setq first (max bottom (1+ (cdr read))))
6836         ;; `read' is a list of ranges.
6837         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6838                                   (caar read)))
6839                   1)
6840           (setq first bottom))
6841         (while read
6842           (when first
6843             (while (< first nlast)
6844               (setq unread (cons first unread)
6845                     first (1+ first))))
6846           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6847           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6848           (setq read (cdr read)))))
6849     ;; And add the last unread articles.
6850     (while (<= first last)
6851       (setq unread (cons first unread)
6852             first (1+ first)))
6853     ;; Return the list of unread articles.
6854     (delq 0 (nreverse unread))))
6855
6856 (defun gnus-list-of-read-articles (group)
6857   "Return a list of unread, unticked and non-dormant articles."
6858   (let* ((info (gnus-get-info group))
6859          (marked (gnus-info-marks info))
6860          (active (gnus-active group)))
6861     (and info active
6862          (gnus-list-range-difference
6863           (gnus-list-range-difference
6864            (gnus-sorted-complement
6865             (gnus-uncompress-range
6866              (if gnus-newsgroup-maximum-articles
6867                  (cons (max (car active)
6868                             (- (cdr active)
6869                                gnus-newsgroup-maximum-articles
6870                                -1))
6871                        (cdr active))
6872                active))
6873             (gnus-list-of-unread-articles group))
6874            (cdr (assq 'dormant marked)))
6875           (cdr (assq 'tick marked))))))
6876
6877 ;; This function returns a sequence of article numbers based on the
6878 ;; difference between the ranges of read articles in this group and
6879 ;; the range of active articles.
6880 (defun gnus-sequence-of-unread-articles (group)
6881   (let* ((read (gnus-info-read (gnus-get-info group)))
6882          (active (or (gnus-active group) (gnus-activate-group group)))
6883          (last (cdr active))
6884          (bottom (if gnus-newsgroup-maximum-articles
6885                      (max (car active)
6886                           (- last gnus-newsgroup-maximum-articles -1))
6887                    (car active)))
6888          first nlast unread)
6889     ;; If none are read, then all are unread.
6890     (if (not read)
6891         (setq first bottom)
6892       ;; If the range of read articles is a single range, then the
6893       ;; first unread article is the article after the last read
6894       ;; article.  Sounds logical, doesn't it?
6895       (if (and (not (listp (cdr read)))
6896                (or (< (car read) bottom)
6897                    (progn (setq read (list read))
6898                           nil)))
6899           (setq first (max bottom (1+ (cdr read))))
6900         ;; `read' is a list of ranges.
6901         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6902                                   (caar read)))
6903                   1)
6904           (setq first bottom))
6905         (while read
6906           (when first
6907             (push (cons first nlast) unread))
6908           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6909           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6910           (setq read (cdr read)))))
6911     ;; And add the last unread articles.
6912     (cond ((not (and first last))
6913            nil)
6914           ((< first last)
6915            (push (cons first last) unread))
6916           ((= first last)
6917            (push first unread)))
6918     ;; Return the sequence of unread articles.
6919     (delq 0 (nreverse unread))))
6920
6921 ;; Various summary commands
6922
6923 (defun gnus-summary-select-article-buffer ()
6924   "Reconfigure windows to show article buffer."
6925   (interactive)
6926   (if (not (gnus-buffer-live-p gnus-article-buffer))
6927       (error "There is no article buffer for this summary buffer")
6928     (gnus-configure-windows 'article)
6929     (select-window (get-buffer-window gnus-article-buffer))))
6930
6931 (defun gnus-summary-universal-argument (arg)
6932   "Perform any operation on all articles that are process/prefixed."
6933   (interactive "P")
6934   (let ((articles (gnus-summary-work-articles arg))
6935         func article)
6936     (if (eq
6937          (setq
6938           func
6939           (key-binding
6940            (read-key-sequence
6941             (substitute-command-keys
6942              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6943          'undefined)
6944         (gnus-error 1 "Undefined key")
6945       (save-excursion
6946         (while articles
6947           (gnus-summary-goto-subject (setq article (pop articles)))
6948           (let (gnus-newsgroup-processable)
6949             (command-execute func))
6950           (gnus-summary-remove-process-mark article)))))
6951   (gnus-summary-position-point))
6952
6953 (defun gnus-summary-toggle-truncation (&optional arg)
6954   "Toggle truncation of summary lines.
6955 With ARG, turn line truncation on if ARG is positive."
6956   (interactive "P")
6957   (setq truncate-lines
6958         (if (null arg) (not truncate-lines)
6959           (> (prefix-numeric-value arg) 0)))
6960   (redraw-display))
6961
6962 (defun gnus-summary-find-for-reselect ()
6963   "Return the number of an article to stay on across a reselect.
6964 The current article is considered, then following articles, then previous
6965 articles.  An article is sought which is not cancelled and isn't a temporary
6966 insertion from another group.  If there's no such then return a dummy 0."
6967   (let (found)
6968     (dolist (rev '(nil t))
6969       (unless found      ; don't demand the reverse list if we don't need it
6970         (let ((data (gnus-data-find-list
6971                      (gnus-summary-article-number) (gnus-data-list rev))))
6972           (while (and data (not found))
6973             (if (and (< 0 (gnus-data-number (car data)))
6974                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6975                 (setq found (gnus-data-number (car data))))
6976             (setq data (cdr data))))))
6977     (or found 0)))
6978
6979 (defun gnus-summary-reselect-current-group (&optional all rescan)
6980   "Exit and then reselect the current newsgroup.
6981 The prefix argument ALL means to select all articles."
6982   (interactive "P")
6983   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6984     (error "Ephemeral groups can't be reselected"))
6985   (let ((current-subject (gnus-summary-find-for-reselect))
6986         (group gnus-newsgroup-name))
6987     (setq gnus-newsgroup-begin nil)
6988     (gnus-summary-exit nil 'leave-hidden)
6989     ;; We have to adjust the point of group mode buffer because
6990     ;; point was moved to the next unread newsgroup by exiting.
6991     (gnus-summary-jump-to-group group)
6992     (when rescan
6993       (save-excursion
6994         (gnus-group-get-new-news-this-group 1)))
6995     (gnus-group-read-group all t)
6996     (gnus-summary-goto-subject current-subject nil t)))
6997
6998 (defun gnus-summary-rescan-group (&optional all)
6999   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7000   (interactive "P")
7001   (gnus-summary-reselect-current-group all t))
7002
7003 (defun gnus-summary-update-info (&optional non-destructive)
7004   (save-excursion
7005     (let ((group gnus-newsgroup-name))
7006       (when group
7007         (when gnus-newsgroup-kill-headers
7008           (setq gnus-newsgroup-killed
7009                 (gnus-compress-sequence
7010                  (gnus-sorted-union
7011                   (gnus-list-range-intersection
7012                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7013                   gnus-newsgroup-unreads)
7014                  t)))
7015         (unless (listp (cdr gnus-newsgroup-killed))
7016           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7017         (let ((headers gnus-newsgroup-headers))
7018           ;; Set the new ranges of read articles.
7019           (with-current-buffer gnus-group-buffer
7020             (gnus-undo-force-boundary))
7021           (gnus-update-read-articles
7022            group (gnus-sorted-union
7023                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7024           ;; Set the current article marks.
7025           (let ((gnus-newsgroup-scored
7026                  (if (and (not gnus-save-score)
7027                           (not non-destructive))
7028                      nil
7029                    gnus-newsgroup-scored)))
7030             (save-excursion
7031               (gnus-update-marks)))
7032           ;; Do the cross-ref thing.
7033           (when gnus-use-cross-reference
7034             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7035           ;; Do not switch windows but change the buffer to work.
7036           (set-buffer gnus-group-buffer)
7037           (unless (gnus-ephemeral-group-p group)
7038             (gnus-group-update-group group)))))))
7039
7040 (defun gnus-summary-save-newsrc (&optional force)
7041   "Save the current number of read/marked articles in the dribble buffer.
7042 The dribble buffer will then be saved.
7043 If FORCE (the prefix), also save the .newsrc file(s)."
7044   (interactive "P")
7045   (gnus-summary-update-info t)
7046   (if force
7047       (gnus-save-newsrc-file)
7048     (gnus-dribble-save)))
7049
7050 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7051
7052 (defun gnus-summary-exit (&optional temporary leave-hidden)
7053   "Exit reading current newsgroup, and then return to group selection mode.
7054 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7055   (interactive)
7056   (gnus-set-global-variables)
7057   (when (gnus-buffer-live-p gnus-article-buffer)
7058     (with-current-buffer gnus-article-buffer
7059       (mm-destroy-parts gnus-article-mime-handles)
7060       ;; Set it to nil for safety reason.
7061       (setq gnus-article-mime-handle-alist nil)
7062       (setq gnus-article-mime-handles nil)))
7063   (gnus-kill-save-kill-buffer)
7064   (gnus-async-halt-prefetch)
7065   (let* ((group gnus-newsgroup-name)
7066          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7067          (gnus-group-is-exiting-p t)
7068          (mode major-mode)
7069          (group-point nil)
7070          (buf (current-buffer)))
7071     (unless quit-config
7072       ;; Do adaptive scoring, and possibly save score files.
7073       (when gnus-newsgroup-adaptive
7074         (gnus-score-adaptive))
7075       (when gnus-use-scoring
7076         (gnus-score-save)))
7077     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7078     ;; If we have several article buffers, we kill them at exit.
7079     (unless gnus-single-article-buffer
7080       (when (gnus-buffer-live-p gnus-article-buffer)
7081         (with-current-buffer gnus-article-buffer
7082           ;; Don't kill sticky article buffers
7083           (unless (eq major-mode 'gnus-sticky-article-mode)
7084             (gnus-kill-buffer gnus-article-buffer)
7085             (setq gnus-article-current nil))))
7086       (gnus-kill-buffer gnus-original-article-buffer))
7087     (when gnus-use-cache
7088       (gnus-cache-possibly-remove-articles)
7089       (gnus-cache-save-buffers))
7090     (gnus-async-prefetch-remove-group group)
7091     (when gnus-suppress-duplicates
7092       (gnus-dup-enter-articles))
7093     (when gnus-use-trees
7094       (gnus-tree-close group))
7095     (when gnus-use-cache
7096       (gnus-cache-write-active))
7097     ;; Remove entries for this group.
7098     (nnmail-purge-split-history (gnus-group-real-name group))
7099     ;; Make all changes in this group permanent.
7100     (unless quit-config
7101       (gnus-run-hooks 'gnus-exit-group-hook)
7102       (gnus-summary-update-info))
7103     (gnus-close-group group)
7104     ;; Make sure where we were, and go to next newsgroup.
7105     (set-buffer gnus-group-buffer)
7106     (unless quit-config
7107       (gnus-group-jump-to-group group))
7108     (gnus-run-hooks 'gnus-summary-exit-hook)
7109     (unless (or quit-config
7110                 (not gnus-summary-next-group-on-exit)
7111                 ;; If this group has disappeared from the summary
7112                 ;; buffer, don't skip forwards.
7113                 (not (string= group (gnus-group-group-name))))
7114       (gnus-group-next-unread-group 1))
7115     (setq group-point (point))
7116     (if temporary
7117         nil                             ;Nothing to do.
7118       (set-buffer buf)
7119       (if (not gnus-kill-summary-on-exit)
7120           (progn
7121             (gnus-deaden-summary)
7122             (setq mode nil))
7123         ;; We set all buffer-local variables to nil.  It is unclear why
7124         ;; this is needed, but if we don't, buffer-local variables are
7125         ;; not garbage-collected, it seems.  This would the lead to en
7126         ;; ever-growing Emacs.
7127         (gnus-summary-clear-local-variables)
7128         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7129           (gnus-summary-clear-local-variables))
7130         (when (get-buffer gnus-article-buffer)
7131           (bury-buffer gnus-article-buffer))
7132         ;; Return to group mode buffer.
7133         (when (eq mode 'gnus-summary-mode)
7134           (gnus-kill-buffer buf)))
7135       (setq gnus-current-select-method gnus-select-method)
7136       (set-buffer gnus-group-buffer)
7137       (if quit-config
7138           (gnus-handle-ephemeral-exit quit-config)
7139         (goto-char group-point)
7140         ;; If gnus-group-buffer is already displayed, make sure we also move
7141         ;; the cursor in the window that displays it.
7142         (let ((win (get-buffer-window (current-buffer) 0)))
7143           (if win (set-window-point win (point))))
7144         (unless leave-hidden
7145           (gnus-configure-windows 'group 'force)))
7146       ;; Clear the current group name.
7147       (unless quit-config
7148         (setq gnus-newsgroup-name nil)))))
7149
7150 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7151 (defun gnus-summary-exit-no-update (&optional no-questions)
7152   "Quit reading current newsgroup without updating read article info."
7153   (interactive)
7154   (let* ((group gnus-newsgroup-name)
7155          (gnus-group-is-exiting-p t)
7156          (gnus-group-is-exiting-without-update-p t)
7157          (quit-config (gnus-group-quit-config group)))
7158     (when (or no-questions
7159               gnus-expert-user
7160               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7161       (gnus-async-halt-prefetch)
7162       (run-hooks 'gnus-summary-prepare-exit-hook)
7163       (when (gnus-buffer-live-p gnus-article-buffer)
7164         (with-current-buffer gnus-article-buffer
7165           (mm-destroy-parts gnus-article-mime-handles)
7166           ;; Set it to nil for safety reason.
7167           (setq gnus-article-mime-handle-alist nil)
7168           (setq gnus-article-mime-handles nil)))
7169       ;; If we have several article buffers, we kill them at exit.
7170       (unless gnus-single-article-buffer
7171         (gnus-kill-buffer gnus-article-buffer)
7172         (gnus-kill-buffer gnus-original-article-buffer)
7173         (setq gnus-article-current nil))
7174       (if (not gnus-kill-summary-on-exit)
7175           (gnus-deaden-summary)
7176         (gnus-close-group group)
7177         (gnus-summary-clear-local-variables)
7178         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7179           (gnus-summary-clear-local-variables))
7180         (gnus-kill-buffer gnus-summary-buffer))
7181       (unless gnus-single-article-buffer
7182         (setq gnus-article-current nil))
7183       (when gnus-use-trees
7184         (gnus-tree-close group))
7185       (gnus-async-prefetch-remove-group group)
7186       (when (get-buffer gnus-article-buffer)
7187         (bury-buffer gnus-article-buffer))
7188       ;; Return to the group buffer.
7189       (gnus-configure-windows 'group 'force)
7190       ;; Clear the current group name.
7191       (setq gnus-newsgroup-name nil)
7192       (unless (gnus-ephemeral-group-p group)
7193         (gnus-group-update-group group))
7194       (when (equal (gnus-group-group-name) group)
7195         (gnus-group-next-unread-group 1))
7196       (when quit-config
7197         (gnus-handle-ephemeral-exit quit-config)))))
7198
7199 (defun gnus-handle-ephemeral-exit (quit-config)
7200   "Handle movement when leaving an ephemeral group.
7201 The state which existed when entering the ephemeral is reset."
7202   (if (not (buffer-name (car quit-config)))
7203       (gnus-configure-windows 'group 'force)
7204     (set-buffer (car quit-config))
7205     (cond ((eq major-mode 'gnus-summary-mode)
7206            (gnus-set-global-variables))
7207           ((eq major-mode 'gnus-article-mode)
7208            (save-current-buffer
7209              ;; The `gnus-summary-buffer' variable may point
7210              ;; to the old summary buffer when using a single
7211              ;; article buffer.
7212              (unless (gnus-buffer-live-p gnus-summary-buffer)
7213                (set-buffer gnus-group-buffer))
7214              (set-buffer gnus-summary-buffer)
7215              (gnus-set-global-variables))))
7216     (if (or (eq (cdr quit-config) 'article)
7217             (eq (cdr quit-config) 'pick))
7218         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7219             (gnus-configure-windows 'pick 'force)
7220           (gnus-configure-windows (cdr quit-config) 'force))
7221       (gnus-configure-windows (cdr quit-config) 'force))
7222     (when (eq major-mode 'gnus-summary-mode)
7223       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7224                                                      next-unread-noselect))
7225           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7226                                   'next-noselect)
7227                               (gnus-summary-next-subject 1 nil t))
7228                              ((eq gnus-auto-select-on-ephemeral-exit
7229                                   'next-unread-noselect)
7230                               (gnus-summary-next-subject 1 t t))))
7231             ;; Hide the article buffer which displays the article different
7232             ;; from the one that the cursor points to in the summary buffer.
7233             (gnus-configure-windows 'summary 'force))
7234         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7235                (gnus-summary-next-subject 1))
7236               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7237                (gnus-summary-next-subject 1 t))))
7238       (gnus-summary-recenter)
7239       (gnus-summary-position-point))))
7240
7241 ;;; Dead summaries.
7242
7243 (defvar gnus-dead-summary-mode-map
7244   (let ((map (make-keymap)))
7245     (suppress-keymap map)
7246     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7247     (dolist (key '("\C-d" "\r" "\177" [delete]))
7248       (define-key map key 'gnus-summary-wake-up-the-dead))
7249     (dolist (key '("q" "Q"))
7250       (define-key map key 'bury-buffer))
7251     map))
7252
7253 (define-minor-mode gnus-dead-summary-mode
7254   "Minor mode for Gnus summary buffers."
7255   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7256   (unless (derived-mode-p 'gnus-summary-mode)
7257     (setq gnus-dead-summary-mode nil)))
7258
7259 (defun gnus-deaden-summary ()
7260   "Make the current summary buffer into a dead summary buffer."
7261   ;; Kill any previous dead summary buffer.
7262   (when (and gnus-dead-summary
7263              (buffer-name gnus-dead-summary))
7264     (with-current-buffer gnus-dead-summary
7265       (when gnus-dead-summary-mode
7266         (kill-buffer (current-buffer)))))
7267   ;; Make this the current dead summary.
7268   (setq gnus-dead-summary (current-buffer))
7269   (gnus-dead-summary-mode 1)
7270   (let ((name (buffer-name)))
7271     (when (string-match "Summary" name)
7272       (rename-buffer
7273        (concat (substring name 0 (match-beginning 0)) "Dead "
7274                (substring name (match-beginning 0)))
7275        t)
7276       (bury-buffer))))
7277
7278 (defun gnus-kill-or-deaden-summary (buffer)
7279   "Kill or deaden the summary BUFFER."
7280   (save-excursion
7281     (when (and (buffer-name buffer)
7282                (not gnus-single-article-buffer))
7283       (with-current-buffer buffer
7284         (gnus-kill-buffer gnus-article-buffer)
7285         (gnus-kill-buffer gnus-original-article-buffer)))
7286     (cond
7287      ;; Kill the buffer.
7288      (gnus-kill-summary-on-exit
7289       (when (and gnus-use-trees
7290                  (gnus-buffer-exists-p buffer))
7291         (with-current-buffer buffer
7292           (gnus-tree-close gnus-newsgroup-name)))
7293       (gnus-kill-buffer buffer))
7294      ;; Deaden the buffer.
7295      ((gnus-buffer-exists-p buffer)
7296       (with-current-buffer buffer
7297         (gnus-deaden-summary))))))
7298
7299 (defun gnus-summary-wake-up-the-dead (&rest args)
7300   "Wake up the dead summary buffer."
7301   (interactive)
7302   (gnus-dead-summary-mode -1)
7303   (let ((name (buffer-name)))
7304     (when (string-match "Dead " name)
7305       (rename-buffer
7306        (concat (substring name 0 (match-beginning 0))
7307                (substring name (match-end 0)))
7308        t)))
7309   (gnus-message 3 "This dead summary is now alive again"))
7310
7311 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7312 (defun gnus-summary-fetch-faq (&optional faq-dir)
7313   "Fetch the FAQ for the current group.
7314 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7315 in."
7316   (interactive
7317    (list
7318     (when current-prefix-arg
7319       (completing-read
7320        "FAQ dir: " (and (listp gnus-group-faq-directory)
7321                         (mapcar 'list
7322                                 gnus-group-faq-directory))))))
7323   (let (gnus-faq-buffer)
7324     (when (setq gnus-faq-buffer
7325                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7326       (gnus-configure-windows 'summary-faq))))
7327
7328 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7329 (defun gnus-summary-describe-group (&optional force)
7330   "Describe the current newsgroup."
7331   (interactive "P")
7332   (gnus-group-describe-group force gnus-newsgroup-name))
7333
7334 (defun gnus-summary-describe-briefly ()
7335   "Describe summary mode commands briefly."
7336   (interactive)
7337   (gnus-message 6 (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")))
7338
7339 ;; Walking around group mode buffer from summary mode.
7340
7341 (defun gnus-summary-next-group (&optional no-article target-group backward)
7342   "Exit current newsgroup and then select next unread newsgroup.
7343 If prefix argument NO-ARTICLE is non-nil, no article is selected
7344 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7345 previous group instead."
7346   (interactive "P")
7347   ;; Stop pre-fetching.
7348   (gnus-async-halt-prefetch)
7349   (let ((current-group gnus-newsgroup-name)
7350         (current-buffer (current-buffer))
7351         entered)
7352     ;; First we semi-exit this group to update Xrefs and all variables.
7353     ;; We can't do a real exit, because the window conf must remain
7354     ;; the same in case the user is prompted for info, and we don't
7355     ;; want the window conf to change before that...
7356     (gnus-summary-exit t)
7357     (while (not entered)
7358       ;; Then we find what group we are supposed to enter.
7359       (set-buffer gnus-group-buffer)
7360       (gnus-group-jump-to-group current-group)
7361       (setq target-group
7362             (or target-group
7363                 (if (eq gnus-keep-same-level 'best)
7364                     (gnus-summary-best-group gnus-newsgroup-name)
7365                   (gnus-summary-search-group backward gnus-keep-same-level))))
7366       (if (not target-group)
7367           ;; There are no further groups, so we return to the group
7368           ;; buffer.
7369           (progn
7370             (gnus-message 5 "Returning to the group buffer")
7371             (setq entered t)
7372             (when (gnus-buffer-live-p current-buffer)
7373               (set-buffer current-buffer)
7374               (gnus-summary-exit))
7375             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7376         ;; We try to enter the target group.
7377         (gnus-group-jump-to-group target-group)
7378         (let ((unreads (gnus-group-group-unread)))
7379           (if (and (or (eq t unreads)
7380                        (and unreads (not (zerop unreads))))
7381                    (gnus-summary-read-group
7382                     target-group nil no-article
7383                     (and (buffer-name current-buffer) current-buffer)
7384                     nil backward))
7385               (setq entered t)
7386             (setq current-group target-group
7387                   target-group nil)))))))
7388
7389 (defun gnus-summary-prev-group (&optional no-article)
7390   "Exit current newsgroup and then select previous unread newsgroup.
7391 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7392   (interactive "P")
7393   (gnus-summary-next-group no-article nil t))
7394
7395 ;; Walking around summary lines.
7396
7397 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7398   "Go to the first subject satisfying any non-nil constraint.
7399 If UNREAD is non-nil, the article should be unread.
7400 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7401 If UNSEEN is non-nil, the article should be unseen.
7402 Returns the article selected or nil if there are no matching articles."
7403   (interactive "P")
7404   (cond
7405    ;; Empty summary.
7406    ((null gnus-newsgroup-data)
7407     (gnus-message 3 "No articles in the group")
7408     nil)
7409    ;; Pick the first article.
7410    ((not (or unread undownloaded unseen))
7411     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7412     (gnus-data-number (car gnus-newsgroup-data)))
7413    ;; Find the first unread article.
7414    (t
7415     (let ((data gnus-newsgroup-data))
7416       (while (and data
7417                   (let ((num (gnus-data-number (car data))))
7418                     (or (memq num gnus-newsgroup-unfetched)
7419                         (not (or (and unread
7420                                       (memq num gnus-newsgroup-unreads))
7421                                  (and undownloaded
7422                                       (memq num gnus-newsgroup-undownloaded))
7423                                  (and unseen
7424                                       (memq num gnus-newsgroup-unseen)))))))
7425         (setq data (cdr data)))
7426       (prog1
7427           (if data
7428               (progn
7429                 (goto-char (gnus-data-pos (car data)))
7430                 (gnus-data-number (car data)))
7431             (gnus-message 3 "No more%s articles"
7432                           (let* ((r (when unread " unread"))
7433                                  (d (when undownloaded " undownloaded"))
7434                                  (s (when unseen " unseen"))
7435                                  (l (delq nil (list r d s))))
7436                             (cond ((= 3 (length l))
7437                                    (concat r "," d ", or" s))
7438                                   ((= 2 (length l))
7439                                    (concat (car l) ", or" (cadr l)))
7440                                   ((= 1 (length l))
7441                                    (car l))
7442                                   (t
7443                                    ""))))
7444             nil
7445             )
7446         (gnus-summary-position-point))))))
7447
7448 (defun gnus-summary-next-subject (n &optional unread dont-display)
7449   "Go to next N'th summary line.
7450 If N is negative, go to the previous N'th subject line.
7451 If UNREAD is non-nil, only unread articles are selected.
7452 The difference between N and the actual number of steps taken is
7453 returned."
7454   (interactive "p")
7455   (let ((backward (< n 0))
7456         (n (abs n)))
7457     (while (and (> n 0)
7458                 (if backward
7459                     (gnus-summary-find-prev unread)
7460                   (gnus-summary-find-next unread)))
7461       (unless (zerop (setq n (1- n)))
7462         (gnus-summary-show-thread)))
7463     (when (/= 0 n)
7464       (gnus-message 7 "No more%s articles"
7465                     (if unread " unread" "")))
7466     (unless dont-display
7467       (gnus-summary-recenter)
7468       (gnus-summary-position-point))
7469     n))
7470
7471 (defun gnus-summary-next-unread-subject (n)
7472   "Go to next N'th unread summary line."
7473   (interactive "p")
7474   (gnus-summary-next-subject n t))
7475
7476 (defun gnus-summary-prev-subject (n &optional unread)
7477   "Go to previous N'th summary line.
7478 If optional argument UNREAD is non-nil, only unread article is selected."
7479   (interactive "p")
7480   (gnus-summary-next-subject (- n) unread))
7481
7482 (defun gnus-summary-prev-unread-subject (n)
7483   "Go to previous N'th unread summary line."
7484   (interactive "p")
7485   (gnus-summary-next-subject (- n) t))
7486
7487 (defun gnus-summary-goto-subjects (articles)
7488   "Insert the subject header for ARTICLES in the current buffer."
7489   (save-excursion
7490     (dolist (article articles)
7491       (gnus-summary-goto-subject article t)))
7492   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7493   (gnus-summary-position-point))
7494
7495 (defun gnus-summary-goto-subject (article &optional force silent)
7496   "Go to the subject line of ARTICLE.
7497 If FORCE, also allow jumping to articles not currently shown."
7498   (interactive "nArticle number: ")
7499   (unless (numberp article)
7500     (error "Article %s is not a number" article))
7501   (let ((b (point))
7502         (data (gnus-data-find article)))
7503     ;; We read in the article if we have to.
7504     (and (not data)
7505          force
7506          (gnus-summary-insert-subject
7507           article
7508           (if (or (numberp force) (vectorp force)) force)
7509           t)
7510          (setq data (gnus-data-find article)))
7511     (goto-char b)
7512     (if (not data)
7513         (progn
7514           (unless silent
7515             (gnus-message 3 "Can't find article %d" article))
7516           nil)
7517       (let ((pt (gnus-data-pos data)))
7518         (goto-char pt)
7519         (gnus-summary-set-article-display-arrow pt))
7520       (gnus-summary-position-point)
7521       article)))
7522
7523 ;; Walking around summary lines with displaying articles.
7524
7525 (defun gnus-summary-expand-window (&optional arg)
7526   "Make the summary buffer take up the entire Emacs frame.
7527 Given a prefix, will force an `article' buffer configuration."
7528   (interactive "P")
7529   (if arg
7530       (gnus-configure-windows 'article 'force)
7531     (gnus-configure-windows 'summary 'force)))
7532
7533 (defun gnus-summary-display-article (article &optional all-header)
7534   "Display ARTICLE in article buffer."
7535   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7536                (with-current-buffer gnus-article-buffer
7537                  (eq major-mode 'gnus-article-mode)))
7538     (gnus-article-setup-buffer))
7539   (gnus-set-global-variables)
7540   (with-current-buffer gnus-article-buffer
7541     (setq gnus-article-charset gnus-newsgroup-charset)
7542     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7543     (mm-enable-multibyte))
7544   (if (null article)
7545       nil
7546     (prog1
7547         (if gnus-summary-display-article-function
7548             (funcall gnus-summary-display-article-function article all-header)
7549           (gnus-article-prepare article all-header))
7550       (gnus-run-hooks 'gnus-select-article-hook)
7551       (when (and gnus-current-article
7552                  (not (zerop gnus-current-article)))
7553         (gnus-summary-goto-subject gnus-current-article))
7554       (gnus-summary-recenter)
7555       (when (and gnus-use-trees gnus-show-threads)
7556         (gnus-possibly-generate-tree article)
7557         (gnus-highlight-selected-tree article))
7558       ;; Successfully display article.
7559       (gnus-article-set-window-start
7560        (cdr (assq article gnus-newsgroup-bookmarks))))))
7561
7562 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7563   "Select the current article.
7564 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7565 non-nil, the article will be re-fetched even if it already present in
7566 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7567 be displayed."
7568   ;; Make sure we are in the summary buffer to work around bbdb bug.
7569   (unless (eq major-mode 'gnus-summary-mode)
7570     (set-buffer gnus-summary-buffer))
7571   (let ((article (or article (gnus-summary-article-number)))
7572         (all-headers (not (not all-headers))) ;Must be t or nil.
7573         gnus-summary-display-article-function)
7574     (and (not pseudo)
7575          (gnus-summary-article-pseudo-p article)
7576          (error "This is a pseudo-article"))
7577     (with-current-buffer gnus-summary-buffer
7578       (if (or (and gnus-single-article-buffer
7579                    (or (null gnus-current-article)
7580                        (null gnus-article-current)
7581                        (null (get-buffer gnus-article-buffer))
7582                        (not (eq article (cdr gnus-article-current)))
7583                        (not (equal (car gnus-article-current)
7584                                    gnus-newsgroup-name))))
7585               (and (not gnus-single-article-buffer)
7586                    (or (null gnus-current-article)
7587                        (not (eq gnus-current-article article))))
7588               force)
7589           ;; The requested article is different from the current article.
7590           (progn
7591             (gnus-summary-display-article article all-headers)
7592             (when (gnus-buffer-live-p gnus-article-buffer)
7593               (with-current-buffer gnus-article-buffer
7594                 (if (not gnus-article-decoded-p) ;; a local variable
7595                     (mm-disable-multibyte))))
7596             (gnus-article-set-window-start
7597              (cdr (assq article gnus-newsgroup-bookmarks)))
7598             article)
7599         'old))))
7600
7601 (defun gnus-summary-force-verify-and-decrypt ()
7602   "Display buttons for signed/encrypted parts and verify/decrypt them."
7603   (interactive)
7604   (let ((mm-verify-option 'known)
7605         (mm-decrypt-option 'known)
7606         (gnus-article-emulate-mime t)
7607         (gnus-buttonized-mime-types (append (list "multipart/signed"
7608                                                   "multipart/encrypted")
7609                                             gnus-buttonized-mime-types)))
7610     (gnus-summary-select-article nil 'force)))
7611
7612 (defun gnus-summary-set-current-mark (&optional current-mark)
7613   "Obsolete function."
7614   nil)
7615
7616 (defun gnus-summary-next-article (&optional unread subject backward push)
7617   "Select the next article.
7618 If UNREAD, only unread articles are selected.
7619 If SUBJECT, only articles with SUBJECT are selected.
7620 If BACKWARD, the previous article is selected instead of the next."
7621   (interactive "P")
7622   ;; Make sure we are in the summary buffer.
7623   (unless (eq major-mode 'gnus-summary-mode)
7624     (set-buffer gnus-summary-buffer))
7625   (cond
7626    ;; Is there such an article?
7627    ((and (gnus-summary-search-forward unread subject backward)
7628          (or (gnus-summary-display-article (gnus-summary-article-number))
7629              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7630     (gnus-summary-position-point))
7631    ;; If not, we try the first unread, if that is wanted.
7632    ((and subject
7633          gnus-auto-select-same
7634          (gnus-summary-first-unread-article))
7635     (gnus-summary-position-point)
7636     (gnus-message 6 "Wrapped"))
7637    ;; Try to get next/previous article not displayed in this group.
7638    ((and gnus-auto-extend-newsgroup
7639          (not unread) (not subject))
7640     (gnus-summary-goto-article
7641      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7642      nil (count-lines (point-min) (point))))
7643    ;; Go to next/previous group.
7644    (t
7645     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7646       (gnus-summary-jump-to-group gnus-newsgroup-name))
7647     (let ((cmd (if (featurep 'xemacs)
7648                    last-command-char
7649                  last-command-event))
7650           (point
7651            (with-current-buffer gnus-group-buffer
7652              (point)))
7653           (group
7654            (if (eq gnus-keep-same-level 'best)
7655                (gnus-summary-best-group gnus-newsgroup-name)
7656              (gnus-summary-search-group backward gnus-keep-same-level))))
7657       ;; For some reason, the group window gets selected.  We change
7658       ;; it back.
7659       (select-window (get-buffer-window (current-buffer)))
7660       ;; Select next unread newsgroup automagically.
7661       (cond
7662        ((or (not gnus-auto-select-next)
7663             (not cmd))
7664         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7665        ((or (eq gnus-auto-select-next 'quietly)
7666             (and (eq gnus-auto-select-next 'slightly-quietly)
7667                  push)
7668             (and (eq gnus-auto-select-next 'almost-quietly)
7669                  (gnus-summary-last-article-p)))
7670         ;; Select quietly.
7671         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7672             (gnus-summary-exit)
7673           (gnus-message 7 "No more%s articles (%s)..."
7674                         (if unread " unread" "")
7675                         (if group (concat "selecting " group)
7676                           "exiting"))
7677           (gnus-summary-next-group nil group backward)))
7678        (t
7679         (when (gnus-key-press-event-p last-input-event)
7680           (gnus-summary-walk-group-buffer
7681            gnus-newsgroup-name cmd unread backward point))))))))
7682
7683 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7684   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7685                       (?\C-p (gnus-group-prev-unread-group 1))))
7686         (cursor-in-echo-area t)
7687         keve key group ended prompt)
7688     (with-current-buffer gnus-group-buffer
7689       (goto-char start)
7690       (setq group
7691             (if (eq gnus-keep-same-level 'best)
7692                 (gnus-summary-best-group gnus-newsgroup-name)
7693               (gnus-summary-search-group backward gnus-keep-same-level))))
7694     (while (not ended)
7695       (setq prompt
7696             (format
7697              "No more%s articles%s " (if unread " unread" "")
7698              (if (and group
7699                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7700                  (format " (Type %s for %s [%s])"
7701                          (single-key-description cmd)
7702                          (gnus-group-decoded-name group)
7703                          (gnus-group-unread group))
7704                (format " (Type %s to exit %s)"
7705                        (single-key-description cmd)
7706                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7707       ;; Confirm auto selection.
7708       (setq key (car (setq keve (gnus-read-event-char prompt)))
7709             ended t)
7710       (cond
7711        ((assq key keystrokes)
7712         (let ((obuf (current-buffer)))
7713           (switch-to-buffer gnus-group-buffer)
7714           (when group
7715             (gnus-group-jump-to-group group))
7716           (eval (cadr (assq key keystrokes)))
7717           (setq group (gnus-group-group-name))
7718           (switch-to-buffer obuf))
7719         (setq ended nil))
7720        ((equal key cmd)
7721         (if (or (not group)
7722                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7723             (gnus-summary-exit)
7724           (gnus-summary-next-group nil group backward)))
7725        (t
7726         (push (cdr keve) unread-command-events))))))
7727
7728 (defun gnus-summary-next-unread-article ()
7729   "Select unread article after current one."
7730   (interactive)
7731   (gnus-summary-next-article
7732    (or (not (eq gnus-summary-goto-unread 'never))
7733        (gnus-summary-last-article-p (gnus-summary-article-number)))
7734    (and gnus-auto-select-same
7735         (gnus-summary-article-subject))))
7736
7737 (defun gnus-summary-prev-article (&optional unread subject)
7738   "Select the article before the current one.
7739 If UNREAD is non-nil, only unread articles are selected."
7740   (interactive "P")
7741   (gnus-summary-next-article unread subject t))
7742
7743 (defun gnus-summary-prev-unread-article ()
7744   "Select unread article before current one."
7745   (interactive)
7746   (gnus-summary-prev-article
7747    (or (not (eq gnus-summary-goto-unread 'never))
7748        (gnus-summary-first-article-p (gnus-summary-article-number)))
7749    (and gnus-auto-select-same
7750         (gnus-summary-article-subject))))
7751
7752 (defun gnus-summary-next-page (&optional lines circular stop)
7753   "Show next page of the selected article.
7754 If at the end of the current article, select the next article.
7755 LINES says how many lines should be scrolled up.
7756
7757 If CIRCULAR is non-nil, go to the start of the article instead of
7758 selecting the next article when reaching the end of the current
7759 article.
7760
7761 If STOP is non-nil, just stop when reaching the end of the message.
7762
7763 Also see the variable `gnus-article-skip-boring'."
7764   (interactive "P")
7765   (setq gnus-summary-buffer (current-buffer))
7766   (gnus-set-global-variables)
7767   (let ((article (gnus-summary-article-number))
7768         (article-window (get-buffer-window gnus-article-buffer t))
7769         endp)
7770     ;; If the buffer is empty, we have no article.
7771     (unless article
7772       (error "No article to select"))
7773     (gnus-configure-windows 'article)
7774     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7775         (if (and (eq gnus-summary-goto-unread 'never)
7776                  (not (gnus-summary-last-article-p article)))
7777             (gnus-summary-next-article)
7778           (gnus-summary-next-unread-article))
7779       (if (or (null gnus-current-article)
7780               (null gnus-article-current)
7781               (/= article (cdr gnus-article-current))
7782               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7783           ;; Selected subject is different from current article's.
7784           (gnus-summary-display-article article)
7785         (when article-window
7786           (gnus-eval-in-buffer-window gnus-article-buffer
7787             (setq endp (or (gnus-article-next-page lines)
7788                            (gnus-article-only-boring-p))))
7789           (when endp
7790             (cond ((or stop gnus-summary-stop-at-end-of-message)
7791                    (gnus-message 3 "End of message"))
7792                   (circular
7793                    (gnus-summary-beginning-of-article))
7794                   (lines
7795                    (gnus-message 3 "End of message"))
7796                   ((null lines)
7797                    (if (and (eq gnus-summary-goto-unread 'never)
7798                             (not (gnus-summary-last-article-p article)))
7799                        (gnus-summary-next-article)
7800                      (gnus-summary-next-unread-article))))))))
7801     (gnus-summary-recenter)
7802     (gnus-summary-position-point)))
7803
7804 (defun gnus-summary-prev-page (&optional lines move)
7805   "Show previous page of selected article.
7806 Argument LINES specifies lines to be scrolled down.
7807 If MOVE, move to the previous unread article if point is at
7808 the beginning of the buffer."
7809   (interactive "P")
7810   (let ((article (gnus-summary-article-number))
7811         (article-window (get-buffer-window gnus-article-buffer t))
7812         endp)
7813     (gnus-configure-windows 'article)
7814     (if (or (null gnus-current-article)
7815             (null gnus-article-current)
7816             (/= article (cdr gnus-article-current))
7817             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7818         ;; Selected subject is different from current article's.
7819         (gnus-summary-display-article article)
7820       (gnus-summary-recenter)
7821       (when article-window
7822         (gnus-eval-in-buffer-window gnus-article-buffer
7823           (setq endp (gnus-article-prev-page lines)))
7824         (when (and move endp)
7825           (cond (lines
7826                  (gnus-message 3 "Beginning of message"))
7827                 ((null lines)
7828                  (if (and (eq gnus-summary-goto-unread 'never)
7829                           (not (gnus-summary-first-article-p article)))
7830                      (gnus-summary-prev-article)
7831                    (gnus-summary-prev-unread-article))))))))
7832   (gnus-summary-position-point))
7833
7834 (defun gnus-summary-prev-page-or-article (&optional lines)
7835   "Show previous page of selected article.
7836 Argument LINES specifies lines to be scrolled down.
7837 If at the beginning of the article, go to the next article."
7838   (interactive "P")
7839   (gnus-summary-prev-page lines t))
7840
7841 (defun gnus-summary-scroll-up (lines)
7842   "Scroll up (or down) one line current article.
7843 Argument LINES specifies lines to be scrolled up (or down if negative)."
7844   (interactive "p")
7845   (gnus-configure-windows 'article)
7846   (gnus-summary-show-thread)
7847   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7848     (gnus-eval-in-buffer-window gnus-article-buffer
7849       (cond ((> lines 0)
7850              (when (gnus-article-next-page lines)
7851                (gnus-message 3 "End of message")))
7852             ((< lines 0)
7853              (gnus-article-prev-page (- lines))))))
7854   (gnus-summary-recenter)
7855   (gnus-summary-position-point))
7856
7857 (defun gnus-summary-scroll-down (lines)
7858   "Scroll down (or up) one line current article.
7859 Argument LINES specifies lines to be scrolled down (or up if negative)."
7860   (interactive "p")
7861   (gnus-summary-scroll-up (- lines)))
7862
7863 (defun gnus-summary-next-same-subject ()
7864   "Select next article which has the same subject as current one."
7865   (interactive)
7866   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7867
7868 (defun gnus-summary-prev-same-subject ()
7869   "Select previous article which has the same subject as current one."
7870   (interactive)
7871   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7872
7873 (defun gnus-summary-next-unread-same-subject ()
7874   "Select next unread article which has the same subject as current one."
7875   (interactive)
7876   (gnus-summary-next-article t (gnus-summary-article-subject)))
7877
7878 (defun gnus-summary-prev-unread-same-subject ()
7879   "Select previous unread article which has the same subject as current one."
7880   (interactive)
7881   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7882
7883 (defun gnus-summary-first-unread-article ()
7884   "Select the first unread article.
7885 Return nil if there are no unread articles."
7886   (interactive)
7887   (prog1
7888       (when (gnus-summary-first-subject t)
7889         (gnus-summary-show-thread)
7890         (gnus-summary-first-subject t)
7891         (gnus-summary-display-article (gnus-summary-article-number)))
7892     (gnus-summary-position-point)))
7893
7894 (defun gnus-summary-first-unread-subject ()
7895   "Place the point on the subject line of the first unread article.
7896 Return nil if there are no unread articles."
7897   (interactive)
7898   (prog1
7899       (when (gnus-summary-first-subject t)
7900         (gnus-summary-show-thread)
7901         (gnus-summary-first-subject t))
7902     (gnus-summary-position-point)))
7903
7904 (defun gnus-summary-first-unseen-subject ()
7905   "Place the point on the subject line of the first unseen article.
7906 Return nil if there are no unseen articles."
7907   (interactive)
7908   (prog1
7909       (when (gnus-summary-first-subject nil nil t)
7910         (gnus-summary-show-thread)
7911         (gnus-summary-first-subject nil nil t))
7912     (gnus-summary-position-point)))
7913
7914 (defun gnus-summary-first-unseen-or-unread-subject ()
7915   "Place the point on the subject line of the first unseen article or,
7916 if all article have been seen, on the subject line of the first unread
7917 article."
7918   (interactive)
7919   (prog1
7920       (unless (when (gnus-summary-first-subject nil nil t)
7921                 (gnus-summary-show-thread)
7922                 (gnus-summary-first-subject nil nil t))
7923         (when (gnus-summary-first-subject t)
7924           (gnus-summary-show-thread)
7925           (gnus-summary-first-subject t)))
7926     (gnus-summary-position-point)))
7927
7928 (defun gnus-summary-first-article ()
7929   "Select the first article.
7930 Return nil if there are no articles."
7931   (interactive)
7932   (prog1
7933       (when (gnus-summary-first-subject)
7934         (gnus-summary-show-thread)
7935         (gnus-summary-first-subject)
7936         (gnus-summary-display-article (gnus-summary-article-number)))
7937     (gnus-summary-position-point)))
7938
7939 (defun gnus-summary-best-unread-article (&optional arg)
7940   "Select the unread article with the highest score.
7941 If given a prefix argument, select the next unread article that has a
7942 score higher than the default score."
7943   (interactive "P")
7944   (let ((article (if arg
7945                      (gnus-summary-better-unread-subject)
7946                    (gnus-summary-best-unread-subject))))
7947     (if article
7948         (gnus-summary-goto-article article)
7949       (error "No unread articles"))))
7950
7951 (defun gnus-summary-best-unread-subject ()
7952   "Select the unread subject with the highest score."
7953   (interactive)
7954   (let ((best -1000000)
7955         (data gnus-newsgroup-data)
7956         article score)
7957     (while data
7958       (and (gnus-data-unread-p (car data))
7959            (> (setq score
7960                     (gnus-summary-article-score (gnus-data-number (car data))))
7961               best)
7962            (setq best score
7963                  article (gnus-data-number (car data))))
7964       (setq data (cdr data)))
7965     (when article
7966       (gnus-summary-goto-subject article))
7967     (gnus-summary-position-point)
7968     article))
7969
7970 (defun gnus-summary-better-unread-subject ()
7971   "Select the first unread subject that has a score over the default score."
7972   (interactive)
7973   (let ((data gnus-newsgroup-data)
7974         article score)
7975     (while (and (setq article (gnus-data-number (car data)))
7976                 (or (gnus-data-read-p (car data))
7977                     (not (> (gnus-summary-article-score article)
7978                             gnus-summary-default-score))))
7979       (setq data (cdr data)))
7980     (when article
7981       (gnus-summary-goto-subject article))
7982     (gnus-summary-position-point)
7983     article))
7984
7985 (defun gnus-summary-last-subject ()
7986   "Go to the last displayed subject line in the group."
7987   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7988     (when article
7989       (gnus-summary-goto-subject article))))
7990
7991 (defun gnus-summary-goto-article (article &optional all-headers force)
7992   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7993 If ALL-HEADERS is non-nil, no header lines are hidden.
7994 If FORCE, go to the article even if it isn't displayed.  If FORCE
7995 is a number, it is the line the article is to be displayed on."
7996   (interactive
7997    (list
7998     (completing-read
7999      "Article number or Message-ID: "
8000      (mapcar (lambda (number) (list (int-to-string number)))
8001              gnus-newsgroup-limit))
8002     current-prefix-arg
8003     t))
8004   (prog1
8005       (if (and (stringp article)
8006                (string-match "@\\|%40" article))
8007           (gnus-summary-refer-article article)
8008         (when (stringp article)
8009           (setq article (string-to-number article)))
8010         (if (gnus-summary-goto-subject article force)
8011             (gnus-summary-display-article article all-headers)
8012           (gnus-message 4 "Couldn't go to article %s" article) nil))
8013     (gnus-summary-position-point)))
8014
8015 (defun gnus-summary-goto-last-article ()
8016   "Go to the previously read article."
8017   (interactive)
8018   (prog1
8019       (when gnus-last-article
8020         (gnus-summary-goto-article gnus-last-article nil t))
8021     (gnus-summary-position-point)))
8022
8023 (defun gnus-summary-pop-article (number)
8024   "Pop one article off the history and go to the previous.
8025 NUMBER articles will be popped off."
8026   (interactive "p")
8027   (let (to)
8028     (setq gnus-newsgroup-history
8029           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8030     (if to
8031         (gnus-summary-goto-article (car to) nil t)
8032       (error "Article history empty")))
8033   (gnus-summary-position-point))
8034
8035 ;; Summary commands and functions for limiting the summary buffer.
8036
8037 (defun gnus-summary-limit-to-articles (n)
8038   "Limit the summary buffer to the next N articles.
8039 If not given a prefix, use the process marked articles instead."
8040   (interactive "P")
8041   (prog1
8042       (let ((articles (gnus-summary-work-articles n)))
8043         (setq gnus-newsgroup-processable nil)
8044         (gnus-summary-limit articles))
8045     (gnus-summary-position-point)))
8046
8047 (defun gnus-summary-pop-limit (&optional total)
8048   "Restore the previous limit.
8049 If given a prefix, remove all limits."
8050   (interactive "P")
8051   (when total
8052     (setq gnus-newsgroup-limits
8053           (list (mapcar (lambda (h) (mail-header-number h))
8054                         gnus-newsgroup-headers))))
8055   (unless gnus-newsgroup-limits
8056     (error "No limit to pop"))
8057   (prog1
8058       (gnus-summary-limit nil 'pop)
8059     (gnus-summary-position-point)))
8060
8061 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8062   "Limit the summary buffer to articles that have subjects that match a regexp.
8063 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8064   (interactive
8065    (list (read-string (if current-prefix-arg
8066                           "Exclude subject (regexp): "
8067                         "Limit to subject (regexp): "))
8068          nil current-prefix-arg))
8069   (unless header
8070     (setq header "subject"))
8071   (when (not (equal "" subject))
8072     (prog1
8073         (let ((articles (gnus-summary-find-matching
8074                          (or header "subject") subject 'all nil nil
8075                          not-matching)))
8076           (unless articles
8077             (error "Found no matches for \"%s\"" subject))
8078           (gnus-summary-limit articles))
8079       (gnus-summary-position-point))))
8080
8081 (defun gnus-summary-limit-to-author (from &optional not-matching)
8082   "Limit the summary buffer to articles that have authors that match a regexp.
8083 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8084   (interactive
8085    (list (read-string (if current-prefix-arg
8086                           "Exclude author (regexp): "
8087                         "Limit to author (regexp): "))
8088          current-prefix-arg))
8089   (gnus-summary-limit-to-subject from "from" not-matching))
8090
8091 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8092   "Limit the summary buffer to articles with the given RECIPIENT.
8093
8094 If NOT-MATCHING, exclude RECIPIENT.
8095
8096 To and Cc headers are checked.  You need to include them in
8097 `nnmail-extra-headers'."
8098   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8099   (interactive
8100    (list (read-string (format "%s recipient (regexp): "
8101                               (if current-prefix-arg "Exclude" "Limit to")))
8102          current-prefix-arg))
8103   (when (not (equal "" recipient))
8104     (prog1 (let* ((to
8105                    (if (memq 'To nnmail-extra-headers)
8106                        (gnus-summary-find-matching
8107                         (cons 'extra 'To) recipient 'all nil nil
8108                         not-matching)
8109                      (gnus-message
8110                       1 "`To' isn't present in `nnmail-extra-headers'")
8111                      (sit-for 1)
8112                      nil))
8113                   (cc
8114                    (if (memq 'Cc nnmail-extra-headers)
8115                        (gnus-summary-find-matching
8116                         (cons 'extra 'Cc) recipient 'all nil nil
8117                         not-matching)
8118                      (gnus-message
8119                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8120                      (sit-for 1)
8121                      nil))
8122                   (articles
8123                    (if not-matching
8124                        ;; We need the numbers that are in both lists:
8125                        (mapcar (lambda (a)
8126                                  (and (memq a to) a))
8127                                cc)
8128                      (nconc to cc))))
8129              (unless articles
8130                (error "Found no matches for \"%s\"" recipient))
8131              (gnus-summary-limit articles))
8132       (gnus-summary-position-point))))
8133
8134 (defun gnus-summary-limit-to-address (address &optional not-matching)
8135   "Limit the summary buffer to articles with the given ADDRESS.
8136
8137 If NOT-MATCHING, exclude ADDRESS.
8138
8139 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8140 in `nnmail-extra-headers'."
8141   (interactive
8142    (list (read-string (format "%s address (regexp): "
8143                               (if current-prefix-arg "Exclude" "Limit to")))
8144          current-prefix-arg))
8145   (when (not (equal "" address))
8146     (prog1 (let* ((to
8147                    (if (memq 'To nnmail-extra-headers)
8148                        (gnus-summary-find-matching
8149                         (cons 'extra 'To) address 'all nil nil
8150                         not-matching)
8151                      (gnus-message
8152                       1 "`To' isn't present in `nnmail-extra-headers'")
8153                      (sit-for 1)
8154                      t))
8155                   (cc
8156                    (if (memq 'Cc nnmail-extra-headers)
8157                        (gnus-summary-find-matching
8158                         (cons 'extra 'Cc) address 'all nil nil
8159                         not-matching)
8160                      (gnus-message
8161                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8162                      (sit-for 1)
8163                      t))
8164                   (from
8165                    (gnus-summary-find-matching "from" address
8166                                                'all nil nil not-matching))
8167                   (articles
8168                    (if not-matching
8169                        ;; We need the numbers that are in all lists:
8170                        (if (eq cc t)
8171                            (if (eq to t)
8172                                from
8173                              (mapcar (lambda (a) (car (memq a from))) to))
8174                          (if (eq to t)
8175                              (mapcar (lambda (a) (car (memq a from))) cc)
8176                            (mapcar (lambda (a) (car (memq a from)))
8177                                    (mapcar (lambda (a) (car (memq a to)))
8178                                            cc))))
8179                      (nconc (if (eq to t) nil to)
8180                             (if (eq cc t) nil cc)
8181                             from))))
8182              (unless articles
8183                (error "Found no matches for \"%s\"" address))
8184              (gnus-summary-limit articles))
8185       (gnus-summary-position-point))))
8186
8187 (defun gnus-summary-limit-strange-charsets-predicate (header)
8188   (when (fboundp 'char-charset)
8189     (let ((string (concat (mail-header-subject header)
8190                           (mail-header-from header)))
8191           charset found)
8192       (dotimes (i (1- (length string)))
8193         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8194         (when (string-match "unicode\\|big\\|japanese" charset)
8195           (setq found t)))
8196       found)))
8197
8198 (defun gnus-summary-limit-to-predicate (predicate)
8199   "Limit to articles where PREDICATE returns non-nil.
8200 PREDICATE will be called with the header structures of the
8201 articles."
8202   (let ((articles nil)
8203         (case-fold-search t))
8204     (dolist (header gnus-newsgroup-headers)
8205       (when (funcall predicate header)
8206         (push (mail-header-number header) articles)))
8207     (gnus-summary-limit (nreverse articles))))
8208
8209 (defun gnus-summary-limit-to-age (age &optional younger-p)
8210   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8211 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8212 articles that are younger than AGE days."
8213   (interactive
8214    (let ((younger current-prefix-arg)
8215          (days-got nil)
8216          days)
8217      (while (not days-got)
8218        (setq days (if younger
8219                       (read-string "Limit to articles younger than (in days, older when negative): ")
8220                     (read-string
8221                      "Limit to articles older than (in days, younger when negative): ")))
8222        (when (> (length days) 0)
8223          (setq days (read days)))
8224        (if (numberp days)
8225            (progn
8226              (setq days-got t)
8227              (when (< days 0)
8228                (setq younger (not younger))
8229                (setq days (* days -1))))
8230          (message "Please enter a number.")
8231          (sleep-for 1)))
8232      (list days younger)))
8233   (prog1
8234       (let ((data gnus-newsgroup-data)
8235             (cutoff (days-to-time age))
8236             articles d date is-younger)
8237         (while (setq d (pop data))
8238           (when (and (vectorp (gnus-data-header d))
8239                      (setq date (mail-header-date (gnus-data-header d))))
8240             (setq is-younger (time-less-p
8241                               (time-since (gnus-date-get-time date))
8242                               cutoff))
8243             (when (if younger-p
8244                       is-younger
8245                     (not is-younger))
8246               (push (gnus-data-number d) articles))))
8247         (gnus-summary-limit (nreverse articles)))
8248     (gnus-summary-position-point)))
8249
8250 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8251   "Limit the summary buffer to articles that match an 'extra' header."
8252   (interactive
8253    (let ((header
8254           (intern
8255            (gnus-completing-read-with-default
8256             (symbol-name (car gnus-extra-headers))
8257             (if current-prefix-arg
8258                 "Exclude extra header"
8259               "Limit extra header")
8260             (mapcar (lambda (x)
8261                       (cons (symbol-name x) x))
8262                     gnus-extra-headers)
8263             nil
8264             t))))
8265      (list header
8266            (read-string (format "%s header %s (regexp): "
8267                                 (if current-prefix-arg "Exclude" "Limit to")
8268                                 header))
8269            current-prefix-arg)))
8270   (when (not (equal "" regexp))
8271     (prog1
8272         (let ((articles (gnus-summary-find-matching
8273                          (cons 'extra header) regexp 'all nil nil
8274                          not-matching)))
8275           (unless articles
8276             (error "Found no matches for \"%s\"" regexp))
8277           (gnus-summary-limit articles))
8278       (gnus-summary-position-point))))
8279
8280 (defun gnus-summary-limit-to-display-predicate ()
8281   "Limit the summary buffer to the predicated in the `display' group parameter."
8282   (interactive)
8283   (unless gnus-newsgroup-display
8284     (error "There is no `display' group parameter"))
8285   (let (articles)
8286     (dolist (number gnus-newsgroup-articles)
8287       (when (funcall gnus-newsgroup-display)
8288         (push number articles)))
8289     (gnus-summary-limit articles))
8290   (gnus-summary-position-point))
8291
8292 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8293 (make-obsolete
8294  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8295
8296 (defun gnus-summary-limit-to-unread (&optional all)
8297   "Limit the summary buffer to articles that are not marked as read.
8298 If ALL is non-nil, limit strictly to unread articles."
8299   (interactive "P")
8300   (if all
8301       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8302     (gnus-summary-limit-to-marks
8303      ;; Concat all the marks that say that an article is read and have
8304      ;; those removed.
8305      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8306            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8307            gnus-low-score-mark gnus-expirable-mark
8308            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8309            gnus-duplicate-mark gnus-souped-mark)
8310      'reverse)))
8311
8312 (defun gnus-summary-limit-to-headers (match &optional reverse)
8313   "Limit the summary buffer to articles that have headers that match MATCH.
8314 If REVERSE (the prefix), limit to articles that don't match."
8315   (interactive "sMatch headers (regexp): \nP")
8316   (gnus-summary-limit-to-bodies match reverse t))
8317
8318 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8319   "Limit the summary buffer to articles that have bodies that match MATCH.
8320 If REVERSE (the prefix), limit to articles that don't match."
8321   (interactive "sMatch body (regexp): \nP")
8322   (let ((articles nil)
8323         (gnus-select-article-hook nil)  ;Disable hook.
8324         (gnus-article-prepare-hook nil)
8325         (gnus-use-article-prefetch nil)
8326         (gnus-keep-backlog nil)
8327         (gnus-break-pages nil)
8328         (gnus-summary-display-arrow nil)
8329         (gnus-updated-mode-lines nil)
8330         (gnus-auto-center-summary nil)
8331         (gnus-display-mime-function nil))
8332     (dolist (data gnus-newsgroup-data)
8333       (let (gnus-mark-article-hook)
8334         (gnus-summary-select-article t t nil (gnus-data-number data)))
8335       (with-current-buffer gnus-article-buffer
8336         (article-goto-body)
8337         (let* ((case-fold-search t)
8338                (found (if headersp
8339                           (re-search-backward match nil t)
8340                         (re-search-forward match nil t))))
8341           (when (or (and found
8342                          (not reverse))
8343                     (and (not found)
8344                          reverse))
8345             (push (gnus-data-number data) articles)))))
8346     (if (not articles)
8347         (message "No messages matched")
8348       (gnus-summary-limit articles)))
8349   (gnus-summary-position-point))
8350
8351 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8352   "Limit the summary buffer to articles that aren't part on any thread.
8353 If THREADSP (the prefix), limit to articles that are in threads."
8354   (interactive "P")
8355   (let ((articles nil)
8356         thread-articles
8357         threads)
8358     (dolist (thread gnus-newsgroup-threads)
8359       (if (stringp (car thread))
8360           (dolist (thread (cdr thread))
8361             (push thread threads))
8362         (push thread threads)))
8363     (dolist (thread threads)
8364       (setq thread-articles (gnus-articles-in-thread thread))
8365       (when (or (and threadsp
8366                      (> (length thread-articles) 1))
8367                 (and (not threadsp)
8368                      (= (length thread-articles) 1)))
8369         (setq articles (nconc thread-articles articles))))
8370     (if (not articles)
8371         (message "No messages matched")
8372       (gnus-summary-limit articles))
8373     (gnus-summary-position-point)))
8374
8375 (defun gnus-summary-limit-to-replied (&optional unreplied)
8376   "Limit the summary buffer to replied articles.
8377 If UNREPLIED (the prefix), limit to unreplied articles."
8378   (interactive "P")
8379   (if unreplied
8380       (gnus-summary-limit
8381        (gnus-set-difference gnus-newsgroup-articles
8382         gnus-newsgroup-replied))
8383     (gnus-summary-limit gnus-newsgroup-replied))
8384   (gnus-summary-position-point))
8385
8386 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8387 (make-obsolete 'gnus-summary-delete-marked-with
8388                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8389
8390 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8391   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8392 If REVERSE, limit the summary buffer to articles that are marked
8393 with MARKS.  MARKS can either be a string of marks or a list of marks.
8394 Returns how many articles were removed."
8395   (interactive "sMarks: ")
8396   (gnus-summary-limit-to-marks marks t))
8397
8398 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8399   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8400 If REVERSE (the prefix), limit the summary buffer to articles that are
8401 not marked with MARKS.  MARKS can either be a string of marks or a
8402 list of marks.
8403 Returns how many articles were removed."
8404   (interactive "sMarks: \nP")
8405   (prog1
8406       (let ((data gnus-newsgroup-data)
8407             (marks (if (listp marks) marks
8408                      (append marks nil))) ; Transform to list.
8409             articles)
8410         (while data
8411           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8412                   (memq (gnus-data-mark (car data)) marks))
8413             (push (gnus-data-number (car data)) articles))
8414           (setq data (cdr data)))
8415         (gnus-summary-limit articles))
8416     (gnus-summary-position-point)))
8417
8418 (defun gnus-summary-limit-to-score (score)
8419   "Limit to articles with score at or above SCORE."
8420   (interactive "NLimit to articles with score of at least: ")
8421   (let ((data gnus-newsgroup-data)
8422         articles)
8423     (while data
8424       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8425                 score)
8426         (push (gnus-data-number (car data)) articles))
8427       (setq data (cdr data)))
8428     (prog1
8429         (gnus-summary-limit articles)
8430       (gnus-summary-position-point))))
8431
8432 (defun gnus-summary-limit-to-unseen ()
8433   "Limit to unseen articles."
8434   (interactive)
8435   (prog1
8436       (gnus-summary-limit gnus-newsgroup-unseen)
8437     (gnus-summary-position-point)))
8438
8439 (defun gnus-summary-limit-include-thread (id)
8440   "Display all the hidden articles that is in the thread with ID in it.
8441 When called interactively, ID is the Message-ID of the current
8442 article."
8443   (interactive (list (mail-header-id (gnus-summary-article-header))))
8444   (let ((articles (gnus-articles-in-thread
8445                    (gnus-id-to-thread (gnus-root-id id)))))
8446     (prog1
8447         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8448       (gnus-summary-limit-include-matching-articles
8449        "subject"
8450        (regexp-quote (gnus-simplify-subject-re
8451                       (mail-header-subject (gnus-id-to-header id)))))
8452       (gnus-summary-position-point))))
8453
8454 (defun gnus-summary-limit-include-matching-articles (header regexp)
8455   "Display all the hidden articles that have HEADERs that match REGEXP."
8456   (interactive (list (read-string "Match on header: ")
8457                      (read-string "Regexp: ")))
8458   (let ((articles (gnus-find-matching-articles header regexp)))
8459     (prog1
8460         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8461       (gnus-summary-position-point))))
8462
8463 (defun gnus-summary-insert-dormant-articles ()
8464   "Insert all the dormant articles for this group into the current buffer."
8465   (interactive)
8466   (let ((gnus-verbose (max 6 gnus-verbose)))
8467     (if (not gnus-newsgroup-dormant)
8468         (gnus-message 3 "No dormant articles for this group")
8469       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8470
8471 (defun gnus-summary-insert-ticked-articles ()
8472   "Insert ticked articles for this group into the current buffer."
8473   (interactive)
8474   (let ((gnus-verbose (max 6 gnus-verbose)))
8475     (if (not gnus-newsgroup-marked)
8476         (gnus-message 3 "No ticked articles for this group")
8477       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8478
8479 (defun gnus-summary-limit-include-dormant ()
8480   "Display all the hidden articles that are marked as dormant.
8481 Note that this command only works on a subset of the articles currently
8482 fetched for this group."
8483   (interactive)
8484   (unless gnus-newsgroup-dormant
8485     (error "There are no dormant articles in this group"))
8486   (prog1
8487       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8488     (gnus-summary-position-point)))
8489
8490 (defun gnus-summary-limit-exclude-dormant ()
8491   "Hide all dormant articles."
8492   (interactive)
8493   (prog1
8494       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8495     (gnus-summary-position-point)))
8496
8497 (defun gnus-summary-limit-exclude-childless-dormant ()
8498   "Hide all dormant articles that have no children."
8499   (interactive)
8500   (let ((data (gnus-data-list t))
8501         articles d children)
8502     ;; Find all articles that are either not dormant or have
8503     ;; children.
8504     (while (setq d (pop data))
8505       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8506                 (and (setq children
8507                            (gnus-article-children (gnus-data-number d)))
8508                      (let (found)
8509                        (while children
8510                          (when (memq (car children) articles)
8511                            (setq children nil
8512                                  found t))
8513                          (pop children))
8514                        found)))
8515         (push (gnus-data-number d) articles)))
8516     ;; Do the limiting.
8517     (prog1
8518         (gnus-summary-limit articles)
8519       (gnus-summary-position-point))))
8520
8521 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8522   "Mark all unread excluded articles as read.
8523 If ALL, mark even excluded ticked and dormants as read."
8524   (interactive "P")
8525   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8526   (let ((articles (gnus-sorted-ndifference
8527                    (sort
8528                     (mapcar (lambda (h) (mail-header-number h))
8529                             gnus-newsgroup-headers)
8530                     '<)
8531                    gnus-newsgroup-limit))
8532         article)
8533     (setq gnus-newsgroup-unreads
8534           (gnus-sorted-intersection gnus-newsgroup-unreads
8535                                     gnus-newsgroup-limit))
8536     (if all
8537         (setq gnus-newsgroup-dormant nil
8538               gnus-newsgroup-marked nil
8539               gnus-newsgroup-reads
8540               (nconc
8541                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8542                gnus-newsgroup-reads))
8543       (while (setq article (pop articles))
8544         (unless (or (memq article gnus-newsgroup-dormant)
8545                     (memq article gnus-newsgroup-marked))
8546           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8547
8548 (defun gnus-summary-limit (articles &optional pop)
8549   (if pop
8550       ;; We pop the previous limit off the stack and use that.
8551       (setq articles (car gnus-newsgroup-limits)
8552             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8553     ;; We use the new limit, so we push the old limit on the stack.
8554     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8555   ;; Set the limit.
8556   (setq gnus-newsgroup-limit articles)
8557   (let ((total (length gnus-newsgroup-data))
8558         (data (gnus-data-find-list (gnus-summary-article-number)))
8559         (gnus-summary-mark-below nil)   ; Inhibit this.
8560         found)
8561     ;; This will do all the work of generating the new summary buffer
8562     ;; according to the new limit.
8563     (gnus-summary-prepare)
8564     ;; Hide any threads, possibly.
8565     (gnus-summary-maybe-hide-threads)
8566     ;; Try to return to the article you were at, or one in the
8567     ;; neighborhood.
8568     (when data
8569       ;; We try to find some article after the current one.
8570       (while data
8571         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8572           (setq data nil
8573                 found t))
8574         (setq data (cdr data))))
8575     (unless found
8576       ;; If there is no data, that means that we were after the last
8577       ;; article.  The same goes when we can't find any articles
8578       ;; after the current one.
8579       (goto-char (point-max))
8580       (gnus-summary-find-prev))
8581     (gnus-set-mode-line 'summary)
8582     ;; We return how many articles were removed from the summary
8583     ;; buffer as a result of the new limit.
8584     (- total (length gnus-newsgroup-data))))
8585
8586 (defsubst gnus-invisible-cut-children (threads)
8587   (let ((num 0))
8588     (while threads
8589       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8590         (incf num))
8591       (pop threads))
8592     (< num 2)))
8593
8594 (defsubst gnus-cut-thread (thread)
8595   "Go forwards in the thread until we find an article that we want to display."
8596   (when (or (eq gnus-fetch-old-headers 'some)
8597             (eq gnus-fetch-old-headers 'invisible)
8598             (numberp gnus-fetch-old-headers)
8599             (eq gnus-build-sparse-threads 'some)
8600             (eq gnus-build-sparse-threads 'more))
8601     ;; Deal with old-fetched headers and sparse threads.
8602     (while (and
8603             thread
8604             (or
8605              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8606              (gnus-summary-article-ancient-p
8607               (mail-header-number (car thread))))
8608             (if (or (<= (length (cdr thread)) 1)
8609                     (eq gnus-fetch-old-headers 'invisible))
8610                 (setq gnus-newsgroup-limit
8611                       (delq (mail-header-number (car thread))
8612                             gnus-newsgroup-limit)
8613                       thread (cadr thread))
8614               (when (gnus-invisible-cut-children (cdr thread))
8615                 (let ((th (cdr thread)))
8616                   (while th
8617                     (if (memq (mail-header-number (caar th))
8618                               gnus-newsgroup-limit)
8619                         (setq thread (car th)
8620                               th nil)
8621                       (setq th (cdr th))))))))))
8622   thread)
8623
8624 (defun gnus-cut-threads (threads)
8625   "Cut off all uninteresting articles from the beginning of THREADS."
8626   (when (or (eq gnus-fetch-old-headers 'some)
8627             (eq gnus-fetch-old-headers 'invisible)
8628             (numberp gnus-fetch-old-headers)
8629             (eq gnus-build-sparse-threads 'some)
8630             (eq gnus-build-sparse-threads 'more))
8631     (let ((th threads))
8632       (while th
8633         (setcar th (gnus-cut-thread (car th)))
8634         (setq th (cdr th)))))
8635   ;; Remove nixed out threads.
8636   (delq nil threads))
8637
8638 (defun gnus-summary-initial-limit (&optional show-if-empty)
8639   "Figure out what the initial limit is supposed to be on group entry.
8640 This entails weeding out unwanted dormants, low-scored articles,
8641 fetch-old-headers verbiage, and so on."
8642   ;; Most groups have nothing to remove.
8643   (unless (or gnus-inhibit-limiting
8644               (and (null gnus-newsgroup-dormant)
8645                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8646                    (not (eq gnus-fetch-old-headers 'some))
8647                    (not (numberp gnus-fetch-old-headers))
8648                    (not (eq gnus-fetch-old-headers 'invisible))
8649                    (null gnus-summary-expunge-below)
8650                    (not (eq gnus-build-sparse-threads 'some))
8651                    (not (eq gnus-build-sparse-threads 'more))
8652                    (null gnus-thread-expunge-below)
8653                    (not gnus-use-nocem)))
8654     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8655     (setq gnus-newsgroup-limit nil)
8656     (mapatoms
8657      (lambda (node)
8658        (unless (car (symbol-value node))
8659          ;; These threads have no parents -- they are roots.
8660          (let ((nodes (cdr (symbol-value node)))
8661                thread)
8662            (while nodes
8663              (if (and gnus-thread-expunge-below
8664                       (< (gnus-thread-total-score (car nodes))
8665                          gnus-thread-expunge-below))
8666                  (gnus-expunge-thread (pop nodes))
8667                (setq thread (pop nodes))
8668                (gnus-summary-limit-children thread))))))
8669      gnus-newsgroup-dependencies)
8670     ;; If this limitation resulted in an empty group, we might
8671     ;; pop the previous limit and use it instead.
8672     (when (and (not gnus-newsgroup-limit)
8673                show-if-empty)
8674       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8675     gnus-newsgroup-limit))
8676
8677 (defun gnus-summary-limit-children (thread)
8678   "Return 1 if this subthread is visible and 0 if it is not."
8679   ;; First we get the number of visible children to this thread.  This
8680   ;; is done by recursing down the thread using this function, so this
8681   ;; will really go down to a leaf article first, before slowly
8682   ;; working its way up towards the root.
8683   (when thread
8684     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8685            (children
8686            (if (cdr thread)
8687                (apply '+ (mapcar 'gnus-summary-limit-children
8688                                  (cdr thread)))
8689              0))
8690           (number (mail-header-number (car thread)))
8691           score)
8692       (if (and
8693            (not (memq number gnus-newsgroup-marked))
8694            (or
8695             ;; If this article is dormant and has absolutely no visible
8696             ;; children, then this article isn't visible.
8697             (and (memq number gnus-newsgroup-dormant)
8698                  (zerop children))
8699             ;; If this is "fetch-old-headered" and there is no
8700             ;; visible children, then we don't want this article.
8701             (and (or (eq gnus-fetch-old-headers 'some)
8702                      (numberp gnus-fetch-old-headers))
8703                  (gnus-summary-article-ancient-p number)
8704                  (zerop children))
8705             ;; If this is "fetch-old-headered" and `invisible', then
8706             ;; we don't want this article.
8707             (and (eq gnus-fetch-old-headers 'invisible)
8708                  (gnus-summary-article-ancient-p number))
8709             ;; If this is a sparsely inserted article with no children,
8710             ;; we don't want it.
8711             (and (eq gnus-build-sparse-threads 'some)
8712                  (gnus-summary-article-sparse-p number)
8713                  (zerop children))
8714             ;; If we use expunging, and this article is really
8715             ;; low-scored, then we don't want this article.
8716             (when (and gnus-summary-expunge-below
8717                        (< (setq score
8718                                 (or (cdr (assq number gnus-newsgroup-scored))
8719                                     gnus-summary-default-score))
8720                           gnus-summary-expunge-below))
8721               ;; We increase the expunge-tally here, but that has
8722               ;; nothing to do with the limits, really.
8723               (incf gnus-newsgroup-expunged-tally)
8724               ;; We also mark as read here, if that's wanted.
8725               (when (and gnus-summary-mark-below
8726                          (< score gnus-summary-mark-below))
8727                 (setq gnus-newsgroup-unreads
8728                       (delq number gnus-newsgroup-unreads))
8729                 (if gnus-newsgroup-auto-expire
8730                     (push number gnus-newsgroup-expirable)
8731                   (push (cons number gnus-low-score-mark)
8732                         gnus-newsgroup-reads)))
8733               t)
8734             ;; Do the `display' group parameter.
8735             (and gnus-newsgroup-display
8736                  (not (funcall gnus-newsgroup-display)))
8737             ;; Check NoCeM things.
8738             (when (and gnus-use-nocem
8739                        (gnus-nocem-unwanted-article-p
8740                         (mail-header-id (car thread))))
8741               (setq gnus-newsgroup-unreads
8742                     (delq number gnus-newsgroup-unreads))
8743               t)))
8744           ;; Nope, invisible article.
8745           0
8746         ;; Ok, this article is to be visible, so we add it to the limit
8747         ;; and return 1.
8748         (push number gnus-newsgroup-limit)
8749         1))))
8750
8751 (defun gnus-expunge-thread (thread)
8752   "Mark all articles in THREAD as read."
8753   (let* ((number (mail-header-number (car thread))))
8754     (incf gnus-newsgroup-expunged-tally)
8755     ;; We also mark as read here, if that's wanted.
8756     (setq gnus-newsgroup-unreads
8757           (delq number gnus-newsgroup-unreads))
8758     (if gnus-newsgroup-auto-expire
8759         (push number gnus-newsgroup-expirable)
8760       (push (cons number gnus-low-score-mark)
8761             gnus-newsgroup-reads)))
8762   ;; Go recursively through all subthreads.
8763   (mapcar 'gnus-expunge-thread (cdr thread)))
8764
8765 ;; Summary article oriented commands
8766
8767 (defun gnus-summary-refer-parent-article (n)
8768   "Refer parent article N times.
8769 If N is negative, go to ancestor -N instead.
8770 The difference between N and the number of articles fetched is returned."
8771   (interactive "p")
8772   (let ((skip 1)
8773         error header ref)
8774     (when (not (natnump n))
8775       (setq skip (abs n)
8776             n 1))
8777     (while (and (> n 0)
8778                 (not error))
8779       (setq header (gnus-summary-article-header))
8780       (if (and (eq (mail-header-number header)
8781                    (cdr gnus-article-current))
8782                (equal gnus-newsgroup-name
8783                       (car gnus-article-current)))
8784           ;; If we try to find the parent of the currently
8785           ;; displayed article, then we take a look at the actual
8786           ;; References header, since this is slightly more
8787           ;; reliable than the References field we got from the
8788           ;; server.
8789           (with-current-buffer gnus-original-article-buffer
8790             (nnheader-narrow-to-headers)
8791             (unless (setq ref (message-fetch-field "references"))
8792               (when (setq ref (message-fetch-field "in-reply-to"))
8793                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8794             (widen))
8795         (setq ref
8796               ;; It's not the current article, so we take a bet on
8797               ;; the value we got from the server.
8798               (mail-header-references header)))
8799       (if (and ref
8800                (not (equal ref "")))
8801           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8802             (gnus-message 1 "Couldn't find parent"))
8803         (gnus-message 1 "No references in article %d"
8804                       (gnus-summary-article-number))
8805         (setq error t))
8806       (decf n))
8807     (gnus-summary-position-point)
8808     n))
8809
8810 (defun gnus-summary-refer-references ()
8811   "Fetch all articles mentioned in the References header.
8812 Return the number of articles fetched."
8813   (interactive)
8814   (let ((ref (mail-header-references (gnus-summary-article-header)))
8815         (current (gnus-summary-article-number))
8816         (n 0))
8817     (if (or (not ref)
8818             (equal ref ""))
8819         (error "No References in the current article")
8820       ;; For each Message-ID in the References header...
8821       (while (string-match "<[^>]*>" ref)
8822         (incf n)
8823         ;; ... fetch that article.
8824         (gnus-summary-refer-article
8825          (prog1 (match-string 0 ref)
8826            (setq ref (substring ref (match-end 0))))))
8827       (gnus-summary-goto-subject current)
8828       (gnus-summary-position-point)
8829       n)))
8830
8831 (defun gnus-summary-refer-thread (&optional limit)
8832   "Fetch all articles in the current thread.
8833 If LIMIT (the numerical prefix), fetch that many old headers instead
8834 of what's specified by the `gnus-refer-thread-limit' variable."
8835   (interactive "P")
8836   (let ((id (mail-header-id (gnus-summary-article-header)))
8837         (limit (if limit (prefix-numeric-value limit)
8838                  gnus-refer-thread-limit)))
8839     (unless (eq gnus-fetch-old-headers 'invisible)
8840       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8841       ;; Retrieve the headers and read them in.
8842       (if (eq (if (numberp limit)
8843                   (gnus-retrieve-headers
8844                    (list (min
8845                           (+ (mail-header-number
8846                               (gnus-summary-article-header))
8847                              limit)
8848                           gnus-newsgroup-end))
8849                    gnus-newsgroup-name (* limit 2))
8850                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8851                 ;; headers.
8852                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8853                                        gnus-newsgroup-name limit))
8854               'nov)
8855           (gnus-build-all-threads)
8856         (error "Can't fetch thread from back ends that don't support NOV"))
8857       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8858     (gnus-summary-limit-include-thread id)))
8859
8860 (defun gnus-summary-refer-article (message-id)
8861   "Fetch an article specified by MESSAGE-ID."
8862   (interactive "sMessage-ID: ")
8863   (when (and (stringp message-id)
8864              (not (zerop (length message-id))))
8865     (setq message-id (gnus-replace-in-string message-id " " ""))
8866     ;; Construct the correct Message-ID if necessary.
8867     ;; Suggested by tale@pawl.rpi.edu.
8868     (unless (string-match "^<" message-id)
8869       (setq message-id (concat "<" message-id)))
8870     (unless (string-match ">$" message-id)
8871       (setq message-id (concat message-id ">")))
8872     ;; People often post MIDs from URLs, so unhex it:
8873     (unless (string-match "@" message-id)
8874       (setq message-id (gnus-url-unhex-string message-id)))
8875     (let* ((header (gnus-id-to-header message-id))
8876            (sparse (and header
8877                         (gnus-summary-article-sparse-p
8878                          (mail-header-number header))
8879                         (memq (mail-header-number header)
8880                               gnus-newsgroup-limit)))
8881            number)
8882       (cond
8883        ;; If the article is present in the buffer we just go to it.
8884        ((and header
8885              (or (not (gnus-summary-article-sparse-p
8886                        (mail-header-number header)))
8887                  sparse))
8888         (prog1
8889             (gnus-summary-goto-article
8890              (mail-header-number header) nil t)
8891           (when sparse
8892             (gnus-summary-update-article (mail-header-number header)))))
8893        (t
8894         ;; We fetch the article.
8895         (catch 'found
8896           (dolist (gnus-override-method (gnus-refer-article-methods))
8897             (when (and (gnus-check-server gnus-override-method)
8898                        ;; Fetch the header,
8899                        (setq number (gnus-summary-insert-subject message-id)))
8900               ;; and display the article.
8901               (gnus-summary-select-article nil nil nil number)
8902               (throw 'found t)))
8903           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8904
8905 (defun gnus-refer-article-methods ()
8906   "Return a list of referable methods."
8907   (cond
8908    ;; No method, so we default to current and native.
8909    ((null gnus-refer-article-method)
8910     (list gnus-current-select-method gnus-select-method))
8911    ;; Current.
8912    ((eq 'current gnus-refer-article-method)
8913     (list gnus-current-select-method))
8914    ;; List of select methods.
8915    ((not (and (symbolp (car gnus-refer-article-method))
8916               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8917     (let (out)
8918       (dolist (method gnus-refer-article-method)
8919         (push (if (eq 'current method)
8920                   gnus-current-select-method
8921                 method)
8922               out))
8923       (nreverse out)))
8924    ;; One single select method.
8925    (t
8926     (list gnus-refer-article-method))))
8927
8928 (defun gnus-summary-edit-parameters ()
8929   "Edit the group parameters of the current group."
8930   (interactive)
8931   (gnus-group-edit-group gnus-newsgroup-name 'params))
8932
8933 (defun gnus-summary-customize-parameters ()
8934   "Customize the group parameters of the current group."
8935   (interactive)
8936   (gnus-group-customize gnus-newsgroup-name))
8937
8938 (defun gnus-summary-enter-digest-group (&optional force)
8939   "Enter an nndoc group based on the current article.
8940 If FORCE, force a digest interpretation.  If not, try
8941 to guess what the document format is."
8942   (interactive "P")
8943   (let ((conf gnus-current-window-configuration))
8944     (save-window-excursion
8945       (save-excursion
8946         (let (gnus-article-prepare-hook
8947               gnus-display-mime-function
8948               gnus-break-pages)
8949           (gnus-summary-select-article))))
8950     (setq gnus-current-window-configuration conf)
8951     (let* ((name (format "%s-%d"
8952                          (gnus-group-prefixed-name
8953                           gnus-newsgroup-name (list 'nndoc ""))
8954                          (with-current-buffer gnus-summary-buffer
8955                            gnus-current-article)))
8956            (ogroup gnus-newsgroup-name)
8957            (params (append (gnus-info-params (gnus-get-info ogroup))
8958                            (list (cons 'to-group ogroup))
8959                            (list (cons 'parent-group ogroup))
8960                            (list (cons 'save-article-group ogroup))))
8961            (case-fold-search t)
8962            (buf (current-buffer))
8963            dig to-address)
8964       (with-current-buffer gnus-original-article-buffer
8965         ;; Have the digest group inherit the main mail address of
8966         ;; the parent article.
8967         (when (setq to-address (or (gnus-fetch-field "reply-to")
8968                                    (gnus-fetch-field "from")))
8969           (setq params
8970                 (append
8971                  (list (cons 'to-address
8972                              (funcall gnus-decode-encoded-address-function
8973                                       to-address))))))
8974         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8975         (insert-buffer-substring gnus-original-article-buffer)
8976         ;; Remove lines that may lead nndoc to misinterpret the
8977         ;; document type.
8978         (narrow-to-region
8979          (goto-char (point-min))
8980          (or (search-forward "\n\n" nil t) (point)))
8981         (goto-char (point-min))
8982         (delete-matching-lines "^Path:\\|^From ")
8983         (widen))
8984       (unwind-protect
8985           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8986                     (gnus-newsgroup-ephemeral-ignored-charsets
8987                      gnus-newsgroup-ignored-charsets))
8988                 (gnus-group-read-ephemeral-group
8989                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8990                               (nndoc-article-type
8991                                ,(if force 'mbox 'guess)))
8992                  t nil nil nil
8993                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8994                                                         "ADAPT")))))
8995               ;; Make all postings to this group go to the parent group.
8996               (nconc (gnus-info-params (gnus-get-info name))
8997                      params)
8998             ;; Couldn't select this doc group.
8999             (switch-to-buffer buf)
9000             (gnus-set-global-variables)
9001             (gnus-configure-windows 'summary)
9002             (gnus-message 3 "Article couldn't be entered?"))
9003         (kill-buffer dig)))))
9004
9005 (defun gnus-summary-read-document (n)
9006   "Open a new group based on the current article(s).
9007 This will allow you to read digests and other similar
9008 documents as newsgroups.
9009 Obeys the standard process/prefix convention."
9010   (interactive "P")
9011   (let* ((ogroup gnus-newsgroup-name)
9012          (params (append (gnus-info-params (gnus-get-info ogroup))
9013                          (list (cons 'to-group ogroup))))
9014          group egroup groups vgroup)
9015     (dolist (article (gnus-summary-work-articles n))
9016       (setq group (format "%s-%d" gnus-newsgroup-name article))
9017       (gnus-summary-remove-process-mark article)
9018       (when (gnus-summary-display-article article)
9019         (save-excursion ;;What for?
9020           (with-temp-buffer
9021             (insert-buffer-substring gnus-original-article-buffer)
9022             ;; Remove some headers that may lead nndoc to make
9023             ;; the wrong guess.
9024             (message-narrow-to-head)
9025             (goto-char (point-min))
9026             (delete-matching-lines "^Path:\\|^From ")
9027             (widen)
9028             (if (setq egroup
9029                       (gnus-group-read-ephemeral-group
9030                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9031                                      (nndoc-article-type guess))
9032                        t nil t))
9033                 (progn
9034                   ;; Make all postings to this group go to the parent group.
9035                   (nconc (gnus-info-params (gnus-get-info egroup))
9036                          params)
9037                   (push egroup groups))
9038               ;; Couldn't select this doc group.
9039               (gnus-error 3 "Article couldn't be entered"))))))
9040     ;; Now we have selected all the documents.
9041     (cond
9042      ((not groups)
9043       (error "None of the articles could be interpreted as documents"))
9044      ((gnus-group-read-ephemeral-group
9045        (setq vgroup (format
9046                      "nnvirtual:%s-%s" gnus-newsgroup-name
9047                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9048        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9049        t
9050        (cons (current-buffer) 'summary)))
9051      (t
9052       (error "Couldn't select virtual nndoc group")))))
9053
9054 (defun gnus-summary-isearch-article (&optional regexp-p)
9055   "Do incremental search forward on the current article.
9056 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9057   (interactive "P")
9058   (gnus-summary-select-article)
9059   (gnus-configure-windows 'article)
9060   (gnus-eval-in-buffer-window gnus-article-buffer
9061     (save-restriction
9062       (widen)
9063       (isearch-forward regexp-p))))
9064
9065 (defun gnus-summary-repeat-search-article-forward ()
9066   "Repeat the previous search forwards."
9067   (interactive)
9068   (unless gnus-last-search-regexp
9069     (error "No previous search"))
9070   (gnus-summary-search-article-forward gnus-last-search-regexp))
9071
9072 (defun gnus-summary-repeat-search-article-backward ()
9073   "Repeat the previous search backwards."
9074   (interactive)
9075   (unless gnus-last-search-regexp
9076     (error "No previous search"))
9077   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9078
9079 (defun gnus-summary-search-article-forward (regexp &optional backward)
9080   "Search for an article containing REGEXP forward.
9081 If BACKWARD, search backward instead."
9082   (interactive
9083    (list (read-string
9084           (format "Search article %s (regexp%s): "
9085                   (if current-prefix-arg "backward" "forward")
9086                   (if gnus-last-search-regexp
9087                       (concat ", default " gnus-last-search-regexp)
9088                     "")))
9089          current-prefix-arg))
9090   (if (string-equal regexp "")
9091       (setq regexp (or gnus-last-search-regexp ""))
9092     (setq gnus-last-search-regexp regexp)
9093     (setq gnus-article-before-search gnus-current-article))
9094   ;; Intentionally set gnus-last-article.
9095   (setq gnus-last-article gnus-article-before-search)
9096   (let ((gnus-last-article gnus-last-article))
9097     (if (gnus-summary-search-article regexp backward)
9098         (gnus-summary-show-thread)
9099       (signal 'search-failed (list regexp)))))
9100
9101 (defun gnus-summary-search-article-backward (regexp)
9102   "Search for an article containing REGEXP backward."
9103   (interactive
9104    (list (read-string
9105           (format "Search article backward (regexp%s): "
9106                   (if gnus-last-search-regexp
9107                       (concat ", default " gnus-last-search-regexp)
9108                     "")))))
9109   (gnus-summary-search-article-forward regexp 'backward))
9110
9111 (defun gnus-summary-search-article (regexp &optional backward)
9112   "Search for an article containing REGEXP.
9113 Optional argument BACKWARD means do search for backward.
9114 `gnus-select-article-hook' is not called during the search."
9115   ;; We have to require this here to make sure that the following
9116   ;; dynamic binding isn't shadowed by autoloading.
9117   (require 'gnus-async)
9118   (require 'gnus-art)
9119   (let ((gnus-select-article-hook nil)  ;Disable hook.
9120         (gnus-article-prepare-hook nil)
9121         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9122         (gnus-use-article-prefetch nil)
9123         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9124         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9125         (gnus-visual nil)
9126         (gnus-keep-backlog nil)
9127         (gnus-break-pages nil)
9128         (gnus-summary-display-arrow nil)
9129         (gnus-updated-mode-lines nil)
9130         (gnus-auto-center-summary nil)
9131         (sum (current-buffer))
9132         (gnus-display-mime-function nil)
9133         (found nil)
9134         point)
9135     (gnus-save-hidden-threads
9136       (gnus-summary-select-article)
9137       (set-buffer gnus-article-buffer)
9138       (goto-char (window-point (get-buffer-window (current-buffer))))
9139       (when backward
9140         (forward-line -1))
9141       (while (not found)
9142         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9143         (if (if backward
9144                 (re-search-backward regexp nil t)
9145               (re-search-forward regexp nil t))
9146             ;; We found the regexp.
9147             (progn
9148               (setq found 'found)
9149               (beginning-of-line)
9150               (set-window-start
9151                (get-buffer-window (current-buffer))
9152                (point))
9153               (forward-line 1)
9154               (set-window-point
9155                (get-buffer-window (current-buffer))
9156                (point))
9157               (set-buffer sum)
9158               (setq point (point)))
9159           ;; We didn't find it, so we go to the next article.
9160           (set-buffer sum)
9161           (setq found 'not)
9162           (while (eq found 'not)
9163             (if (not (if backward (gnus-summary-find-prev)
9164                        (gnus-summary-find-next)))
9165                 ;; No more articles.
9166                 (setq found t)
9167               ;; Select the next article and adjust point.
9168               (unless (gnus-summary-article-sparse-p
9169                        (gnus-summary-article-number))
9170                 (setq found nil)
9171                 (gnus-summary-select-article)
9172                 (set-buffer gnus-article-buffer)
9173                 (widen)
9174                 (goto-char (if backward (point-max) (point-min))))))))
9175       (gnus-message 7 ""))
9176     ;; Return whether we found the regexp.
9177     (when (eq found 'found)
9178       (goto-char point)
9179       (gnus-summary-show-thread)
9180       (gnus-summary-goto-subject gnus-current-article)
9181       (gnus-summary-position-point)
9182       t)))
9183
9184 (defun gnus-find-matching-articles (header regexp)
9185   "Return a list of all articles that match REGEXP on HEADER.
9186 This search includes all articles in the current group that Gnus has
9187 fetched headers for, whether they are displayed or not."
9188   (let ((articles nil)
9189         ;; Can't eta-reduce because it's a macro.
9190         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9191         (case-fold-search t))
9192     (dolist (header gnus-newsgroup-headers)
9193       (when (string-match regexp (funcall func header))
9194         (push (mail-header-number header) articles)))
9195     (nreverse articles)))
9196
9197 (defun gnus-summary-find-matching (header regexp &optional backward unread
9198                                           not-case-fold not-matching)
9199   "Return a list of all articles that match REGEXP on HEADER.
9200 The search stars on the current article and goes forwards unless
9201 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9202 If UNREAD is non-nil, only unread articles will
9203 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9204 in the comparisons. If NOT-MATCHING, return a list of all articles that
9205 not match REGEXP on HEADER."
9206   (let ((case-fold-search (not not-case-fold))
9207         articles d func)
9208     (if (consp header)
9209         (if (eq (car header) 'extra)
9210             (setq func
9211                   `(lambda (h)
9212                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9213                          "")))
9214           (error "%s is an invalid header" header))
9215       (unless (fboundp (intern (concat "mail-header-" header)))
9216         (error "%s is not a valid header" header))
9217       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9218     (dolist (d (if (eq backward 'all)
9219                    gnus-newsgroup-data
9220                  (gnus-data-find-list
9221                   (gnus-summary-article-number)
9222                   (gnus-data-list backward))))
9223       (when (and (or (not unread)       ; We want all articles...
9224                      (gnus-data-unread-p d)) ; Or just unreads.
9225                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9226                  (if not-matching
9227                      (not (string-match
9228                            regexp
9229                            (funcall func (gnus-data-header d))))
9230                    (string-match regexp
9231                                  (funcall func (gnus-data-header d)))))
9232         (push (gnus-data-number d) articles))) ; Success!
9233     (nreverse articles)))
9234
9235 (defun gnus-summary-execute-command (header regexp command &optional backward)
9236   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9237 If HEADER is an empty string (or nil), the match is done on the entire
9238 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9239   (interactive
9240    (list (let ((completion-ignore-case t))
9241            (completing-read
9242             "Header name: "
9243             (mapcar (lambda (header) (list (format "%s" header)))
9244                     (append
9245                      '("Number" "Subject" "From" "Lines" "Date"
9246                        "Message-ID" "Xref" "References" "Body")
9247                      gnus-extra-headers))
9248             nil 'require-match))
9249          (read-string "Regexp: ")
9250          (read-key-sequence "Command: ")
9251          current-prefix-arg))
9252   (when (equal header "Body")
9253     (setq header ""))
9254   ;; Hidden thread subtrees must be searched as well.
9255   (gnus-summary-show-all-threads)
9256   ;; We don't want to change current point nor window configuration.
9257   (save-excursion
9258     (save-window-excursion
9259       (let (gnus-visual
9260             gnus-treat-strip-trailing-blank-lines
9261             gnus-treat-strip-leading-blank-lines
9262             gnus-treat-strip-multiple-blank-lines
9263             gnus-treat-hide-boring-headers
9264             gnus-treat-fold-newsgroups
9265             gnus-article-prepare-hook)
9266         (gnus-message 6 "Executing %s..." (key-description command))
9267         ;; We'd like to execute COMMAND interactively so as to give arguments.
9268         (gnus-execute header regexp
9269                       `(call-interactively ',(key-binding command))
9270                       backward)
9271         (gnus-message 6 "Executing %s...done" (key-description command))))))
9272
9273 (defun gnus-summary-beginning-of-article ()
9274   "Scroll the article back to the beginning."
9275   (interactive)
9276   (gnus-summary-select-article)
9277   (gnus-configure-windows 'article)
9278   (gnus-eval-in-buffer-window gnus-article-buffer
9279     (widen)
9280     (goto-char (point-min))
9281     (when gnus-break-pages
9282       (gnus-narrow-to-page))))
9283
9284 (defun gnus-summary-end-of-article ()
9285   "Scroll to the end of the article."
9286   (interactive)
9287   (gnus-summary-select-article)
9288   (gnus-configure-windows 'article)
9289   (gnus-eval-in-buffer-window gnus-article-buffer
9290     (widen)
9291     (goto-char (point-max))
9292     (recenter -3)
9293     (when gnus-break-pages
9294       (gnus-narrow-to-page))))
9295
9296 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9297   "Truncate to LEN and quote all \"(\"'s in STRING."
9298   (gnus-replace-in-string (if (and len (> (length string) len))
9299                               (substring string 0 len)
9300                             string)
9301                           "[()]" "\\\\\\&"))
9302
9303 (defun gnus-summary-print-article (&optional filename n)
9304   "Generate and print a PostScript image of the process-marked (mail) articles.
9305
9306 If used interactively, print the current article if none are
9307 process-marked.  With prefix arg, prompt the user for the name of the
9308 file to save in.
9309
9310 When used from Lisp, accept two optional args FILENAME and N.  N means
9311 to print the next N articles.  If N is negative, print the N previous
9312 articles.  If N is nil and articles have been marked with the process
9313 mark, print these instead.
9314
9315 If the optional first argument FILENAME is nil, send the image to the
9316 printer.  If FILENAME is a string, save the PostScript image in a file with
9317 that name.  If FILENAME is a number, prompt the user for the name of the file
9318 to save in."
9319   (interactive (list (ps-print-preprint current-prefix-arg)))
9320   (dolist (article (gnus-summary-work-articles n))
9321     (gnus-summary-select-article nil nil 'pseudo article)
9322     (gnus-eval-in-buffer-window gnus-article-buffer
9323       (gnus-print-buffer))
9324     (gnus-summary-remove-process-mark article))
9325   (ps-despool filename))
9326
9327 (defun gnus-print-buffer ()
9328   (let ((buffer (generate-new-buffer " *print*")))
9329     (unwind-protect
9330         (progn
9331           (copy-to-buffer buffer (point-min) (point-max))
9332           (set-buffer buffer)
9333           (gnus-remove-text-with-property 'gnus-decoration)
9334           (when (gnus-visual-p 'article-highlight 'highlight)
9335             ;; Copy-to-buffer doesn't copy overlay.  So redo
9336             ;; highlight.
9337             (let ((gnus-article-buffer buffer))
9338               (gnus-article-highlight-citation t)
9339               (gnus-article-highlight-signature)
9340               (gnus-article-emphasize)
9341               (gnus-article-delete-invisible-text)))
9342           (let ((ps-left-header
9343                  (list
9344                   (concat "("
9345                           (gnus-summary-print-truncate-and-quote
9346                            (mail-header-subject gnus-current-headers)
9347                            66) ")")
9348                   (concat "("
9349                           (gnus-summary-print-truncate-and-quote
9350                            (mail-header-from gnus-current-headers)
9351                            45) ")")))
9352                 (ps-right-header
9353                  (list
9354                   "/pagenumberstring load"
9355                   (concat "("
9356                           (mail-header-date gnus-current-headers) ")"))))
9357             (gnus-run-hooks 'gnus-ps-print-hook)
9358             (save-excursion
9359               (if ps-print-color-p
9360                   (ps-spool-buffer-with-faces)
9361                 (ps-spool-buffer)))))
9362       (kill-buffer buffer))))
9363
9364 (defun gnus-summary-show-article (&optional arg)
9365   "Force redisplaying of the current article.
9366 If ARG (the prefix) is a number, show the article with the charset
9367 defined in `gnus-summary-show-article-charset-alist', or the charset
9368 input.
9369 If ARG (the prefix) is non-nil and not a number, show the raw article
9370 without any article massaging functions being run.  Normally, the key
9371 strokes are `C-u g'."
9372   (interactive "P")
9373   (cond
9374    ((numberp arg)
9375     (gnus-summary-show-article t)
9376     (let ((gnus-newsgroup-charset
9377            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9378                (mm-read-coding-system
9379                 "View as charset: " ;; actually it is coding system.
9380                 (with-current-buffer gnus-article-buffer
9381                   (mm-detect-coding-region (point) (point-max))))))
9382           (gnus-newsgroup-ignored-charsets 'gnus-all))
9383       (gnus-summary-select-article nil 'force)
9384       (let ((deps gnus-newsgroup-dependencies)
9385             head header lines)
9386         (with-current-buffer gnus-original-article-buffer
9387           (save-restriction
9388             (message-narrow-to-head)
9389             (setq head (buffer-string))
9390             (goto-char (point-min))
9391             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9392               (goto-char (point-max))
9393               (widen)
9394               (setq lines (1- (count-lines (point) (point-max))))))
9395           (with-temp-buffer
9396             (insert (format "211 %d Article retrieved.\n"
9397                             (cdr gnus-article-current)))
9398             (insert head)
9399             (if lines (insert (format "Lines: %d\n" lines)))
9400             (insert ".\n")
9401             (let ((nntp-server-buffer (current-buffer)))
9402               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9403         (gnus-data-set-header
9404          (gnus-data-find (cdr gnus-article-current))
9405          header)
9406         (gnus-summary-update-article-line
9407          (cdr gnus-article-current) header)
9408         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9409           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9410    ((not arg)
9411     ;; Select the article the normal way.
9412     (gnus-summary-select-article nil 'force))
9413    (t
9414     ;; We have to require this here to make sure that the following
9415     ;; dynamic binding isn't shadowed by autoloading.
9416     (require 'gnus-async)
9417     (require 'gnus-art)
9418     ;; Bind the article treatment functions to nil.
9419     (let ((gnus-have-all-headers t)
9420           gnus-article-prepare-hook
9421           gnus-article-decode-hook
9422           gnus-display-mime-function
9423           gnus-break-pages)
9424       ;; Destroy any MIME parts.
9425       (when (gnus-buffer-live-p gnus-article-buffer)
9426         (with-current-buffer gnus-article-buffer
9427           (mm-destroy-parts gnus-article-mime-handles)
9428           ;; Set it to nil for safety reason.
9429           (setq gnus-article-mime-handle-alist nil)
9430           (setq gnus-article-mime-handles nil)))
9431       (gnus-summary-select-article nil 'force))))
9432   (gnus-summary-goto-subject gnus-current-article)
9433   (gnus-summary-position-point))
9434
9435 (defun gnus-summary-show-raw-article ()
9436   "Show the raw article without any article massaging functions being run."
9437   (interactive)
9438   (gnus-summary-show-article t))
9439
9440 (defun gnus-summary-verbose-headers (&optional arg)
9441   "Toggle permanent full header display.
9442 If ARG is a positive number, turn header display on.
9443 If ARG is a negative number, turn header display off."
9444   (interactive "P")
9445   (setq gnus-show-all-headers
9446         (cond ((or (not (numberp arg))
9447                    (zerop arg))
9448                (not gnus-show-all-headers))
9449               ((natnump arg)
9450                t)))
9451   (gnus-summary-show-article))
9452
9453 (defun gnus-summary-toggle-header (&optional arg)
9454   "Show the headers if they are hidden, or hide them if they are shown.
9455 If ARG is a positive number, show the entire header.
9456 If ARG is a negative number, hide the unwanted header lines."
9457   (interactive "P")
9458   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9459                      (get-buffer-window gnus-article-buffer t))))
9460     (with-current-buffer gnus-article-buffer
9461       (widen)
9462       (article-narrow-to-head)
9463       (let* ((inhibit-read-only t)
9464              (inhibit-point-motion-hooks t)
9465              (hidden (if (numberp arg)
9466                          (>= arg 0)
9467                        (or (not (looking-at "[^ \t\n]+:"))
9468                            (gnus-article-hidden-text-p 'headers))))
9469              s e)
9470         (delete-region (point-min) (point-max))
9471         (with-current-buffer gnus-original-article-buffer
9472           (goto-char (setq s (point-min)))
9473           (setq e (if (search-forward "\n\n" nil t)
9474                       (1- (point))
9475                     (point-max))))
9476         (insert-buffer-substring gnus-original-article-buffer s e)
9477         (run-hooks 'gnus-article-decode-hook)
9478         (if hidden
9479             (let ((gnus-treat-hide-headers nil)
9480                   (gnus-treat-hide-boring-headers nil))
9481               (gnus-delete-wash-type 'headers)
9482               (gnus-treat-article 'head))
9483           (gnus-treat-article 'head))
9484         (widen)
9485         (if window
9486             (set-window-start window (goto-char (point-min))))
9487         (if gnus-break-pages
9488             (gnus-narrow-to-page)
9489           (when (gnus-visual-p 'page-marker)
9490             (let ((inhibit-read-only t))
9491               (gnus-remove-text-with-property 'gnus-prev)
9492               (gnus-remove-text-with-property 'gnus-next))))
9493         (gnus-set-mode-line 'article)))))
9494
9495 (defun gnus-summary-show-all-headers ()
9496   "Make all header lines visible."
9497   (interactive)
9498   (gnus-summary-toggle-header 1))
9499
9500 (defun gnus-summary-caesar-message (&optional arg)
9501   "Caesar rotate the current article by 13.
9502 With a non-numerical prefix, also rotate headers.  A numerical
9503 prefix specifies how many places to rotate each letter forward."
9504   (interactive "P")
9505   (gnus-summary-select-article)
9506   (let ((mail-header-separator ""))
9507     (gnus-eval-in-buffer-window gnus-article-buffer
9508       (save-restriction
9509         (widen)
9510         (let ((start (window-start))
9511               (inhibit-read-only t))
9512           (if (equal arg '(4))
9513               (message-caesar-buffer-body nil t)
9514             (message-caesar-buffer-body arg))
9515           (set-window-start (get-buffer-window (current-buffer)) start)))))
9516   ;; Create buttons and stuff...
9517   (gnus-treat-article nil))
9518
9519 (declare-function idna-to-unicode "ext:idna" (str))
9520
9521 (defun gnus-summary-idna-message (&optional arg)
9522   "Decode IDNA encoded domain names in the current articles.
9523 IDNA encoded domain names looks like `xn--bar'.  If a string
9524 remain unencoded after running this function, it is likely an
9525 invalid IDNA string (`xn--bar' is invalid).
9526
9527 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9528 installed for this command to work."
9529   (interactive "P")
9530   (if (not (and (condition-case nil (require 'idna)
9531                   (file-error))
9532                 (mm-coding-system-p 'utf-8)
9533                 (executable-find (symbol-value 'idna-program))))
9534       (gnus-message
9535        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9536     (gnus-summary-select-article)
9537     (let ((mail-header-separator ""))
9538       (gnus-eval-in-buffer-window gnus-article-buffer
9539         (save-restriction
9540           (widen)
9541           (let ((start (window-start))
9542                 buffer-read-only)
9543             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9544               (replace-match (idna-to-unicode (match-string 1))))
9545             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9546
9547 (defun gnus-summary-morse-message (&optional arg)
9548   "Morse decode the current article."
9549   (interactive "P")
9550   (gnus-summary-select-article)
9551   (let ((mail-header-separator ""))
9552     (gnus-eval-in-buffer-window gnus-article-buffer
9553       (save-excursion
9554         (save-restriction
9555           (widen)
9556           (let ((pos (window-start))
9557                 (inhibit-read-only t))
9558             (goto-char (point-min))
9559             (when (message-goto-body)
9560               (gnus-narrow-to-body))
9561             (goto-char (point-min))
9562             (while (search-forward "·" (point-max) t)
9563               (replace-match "."))
9564             (unmorse-region (point-min) (point-max))
9565             (widen)
9566             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9567
9568 (defun gnus-summary-stop-page-breaking ()
9569   "Stop page breaking in the current article."
9570   (interactive)
9571   (gnus-summary-select-article)
9572   (gnus-eval-in-buffer-window gnus-article-buffer
9573     (widen)
9574     (when (gnus-visual-p 'page-marker)
9575       (let ((inhibit-read-only t))
9576         (gnus-remove-text-with-property 'gnus-prev)
9577         (gnus-remove-text-with-property 'gnus-next))
9578       (setq gnus-page-broken nil))))
9579
9580 (defun gnus-summary-move-article (&optional n to-newsgroup
9581                                             select-method action)
9582   "Move the current article to a different newsgroup.
9583 If N is a positive number, move the N next articles.
9584 If N is a negative number, move the N previous articles.
9585 If N is nil and any articles have been marked with the process mark,
9586 move those articles instead.
9587 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9588 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9589 re-spool using this method.
9590
9591 When called interactively with TO-NEWSGROUP being nil, the value of
9592 the variable `gnus-move-split-methods' is used for finding a default
9593 for the target newsgroup.
9594
9595 For this function to work, both the current newsgroup and the
9596 newsgroup that you want to move to have to support the `request-move'
9597 and `request-accept' functions.
9598
9599 ACTION can be either `move' (the default), `crosspost' or `copy'."
9600   (interactive "P")
9601   (unless action
9602     (setq action 'move))
9603   ;; Check whether the source group supports the required functions.
9604   (cond ((and (eq action 'move)
9605               (not (gnus-check-backend-function
9606                     'request-move-article gnus-newsgroup-name)))
9607          (error "The current group does not support article moving"))
9608         ((and (eq action 'crosspost)
9609               (not (gnus-check-backend-function
9610                     'request-replace-article gnus-newsgroup-name)))
9611          (error "The current group does not support article editing")))
9612   (let ((articles (gnus-summary-work-articles n))
9613         (prefix (if (gnus-check-backend-function
9614                      'request-move-article gnus-newsgroup-name)
9615                     (funcall gnus-move-group-prefix-function
9616                              gnus-newsgroup-name)
9617                   ""))
9618         (names '((move "Move" "Moving")
9619                  (copy "Copy" "Copying")
9620                  (crosspost "Crosspost" "Crossposting")))
9621         (copy-buf (save-excursion
9622                     (nnheader-set-temp-buffer " *copy article*")))
9623         art-group to-method new-xref article to-groups
9624         articles-to-update-marks encoded)
9625     (unless (assq action names)
9626       (error "Unknown action %s" action))
9627     ;; Read the newsgroup name.
9628     (when (and (not to-newsgroup)
9629                (not select-method))
9630       (if (and gnus-move-split-methods
9631                (not
9632                 (and (memq gnus-current-article articles)
9633                      (gnus-buffer-live-p gnus-original-article-buffer))))
9634           ;; When `gnus-move-split-methods' is non-nil, we have to
9635           ;; select an article to give `gnus-read-move-group-name' an
9636           ;; opportunity to suggest an appropriate default.  However,
9637           ;; we needn't render or mark the article.
9638           (let ((gnus-display-mime-function nil)
9639                 (gnus-article-prepare-hook nil)
9640                 (gnus-mark-article-hook nil))
9641             (gnus-summary-select-article nil nil nil (car articles))))
9642       (setq to-newsgroup (gnus-read-move-group-name
9643                           (cadr (assq action names))
9644                           (symbol-value
9645                            (intern (format "gnus-current-%s-group" action)))
9646                           articles prefix)
9647             encoded to-newsgroup
9648             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9649       (set (intern (format "gnus-current-%s-group" action))
9650            (mm-decode-coding-string
9651             to-newsgroup
9652             (gnus-group-name-charset to-method to-newsgroup))))
9653     (unless to-method
9654       (setq to-method (or select-method
9655                           (gnus-server-to-method
9656                            (gnus-group-method to-newsgroup)))))
9657     (setq to-newsgroup
9658           (or encoded
9659               (and to-newsgroup
9660                    (mm-encode-coding-string
9661                     to-newsgroup
9662                     (gnus-group-name-charset to-method to-newsgroup)))))
9663     ;; Check the method we are to move this article to...
9664     (unless (gnus-check-backend-function
9665              'request-accept-article (car to-method))
9666       (error "%s does not support article copying" (car to-method)))
9667     (unless (gnus-check-server to-method)
9668       (error "Can't open server %s" (car to-method)))
9669     (gnus-message 6 "%s to %s: %s..."
9670                   (caddr (assq action names))
9671                   (or (car select-method)
9672                       (gnus-group-decoded-name to-newsgroup))
9673                   articles)
9674     (while articles
9675       (setq article (pop articles))
9676       (setq
9677        art-group
9678        (cond
9679         ;; Move the article.
9680         ((eq action 'move)
9681          ;; Remove this article from future suppression.
9682          (gnus-dup-unsuppress-article article)
9683          (let* ((from-method (gnus-find-method-for-group
9684                               gnus-newsgroup-name))
9685                 (to-method (or select-method
9686                                (gnus-find-method-for-group to-newsgroup)))
9687                 (move-is-internal (gnus-method-equal from-method to-method)))
9688            (gnus-request-move-article
9689             article                     ; Article to move
9690             gnus-newsgroup-name         ; From newsgroup
9691             (nth 1 (gnus-find-method-for-group
9692                     gnus-newsgroup-name)) ; Server
9693             (list 'gnus-request-accept-article
9694                   to-newsgroup (list 'quote select-method)
9695                   (not articles) t)     ; Accept form
9696             (not articles)              ; Only save nov last time
9697             move-is-internal)))         ; is this move internal?
9698         ;; Copy the article.
9699         ((eq action 'copy)
9700          (with-current-buffer copy-buf
9701            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9702              (save-restriction
9703                (nnheader-narrow-to-headers)
9704                (dolist (hdr gnus-copy-article-ignored-headers)
9705                  (message-remove-header hdr t)))
9706              (gnus-request-accept-article
9707               to-newsgroup select-method (not articles) t))))
9708         ;; Crosspost the article.
9709         ((eq action 'crosspost)
9710          (let ((xref (message-tokenize-header
9711                       (mail-header-xref (gnus-summary-article-header article))
9712                       " ")))
9713            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9714                                   ":" (number-to-string article)))
9715            (unless xref
9716              (setq xref (list (system-name))))
9717            (setq new-xref
9718                  (concat
9719                   (mapconcat 'identity
9720                              (delete "Xref:" (delete new-xref xref))
9721                              " ")
9722                   " " new-xref))
9723            (with-current-buffer copy-buf
9724              ;; First put the article in the destination group.
9725              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9726              (when (consp (setq art-group
9727                                 (gnus-request-accept-article
9728                                  to-newsgroup select-method (not articles) t)))
9729                (setq new-xref (concat new-xref " " (car art-group)
9730                                       ":"
9731                                       (number-to-string (cdr art-group))))
9732                ;; Now we have the new Xrefs header, so we insert
9733                ;; it and replace the new article.
9734                (nnheader-replace-header "Xref" new-xref)
9735                (gnus-request-replace-article
9736                 (cdr art-group) to-newsgroup (current-buffer) t)
9737                art-group))))))
9738       (cond
9739        ((not art-group)
9740         (gnus-message 1 "Couldn't %s article %s: %s"
9741                       (cadr (assq action names)) article
9742                       (nnheader-get-report (car to-method))))
9743        ((eq art-group 'junk)
9744         (when (eq action 'move)
9745           (gnus-summary-mark-article article gnus-canceled-mark)
9746           (gnus-message 4 "Deleted article %s" article)
9747           ;; run the delete hook
9748           (run-hook-with-args 'gnus-summary-article-delete-hook
9749                               action
9750                               (gnus-data-header
9751                                (assoc article (gnus-data-list nil)))
9752                               gnus-newsgroup-name nil
9753                               select-method)))
9754        (t
9755         (let* ((pto-group (gnus-group-prefixed-name
9756                            (car art-group) to-method))
9757                (info (gnus-get-info pto-group))
9758                (to-group (gnus-info-group info))
9759                to-marks)
9760           ;; Update the group that has been moved to.
9761           (when (and info
9762                      (memq action '(move copy)))
9763             (unless (member to-group to-groups)
9764               (push to-group to-groups))
9765
9766             (unless (memq article gnus-newsgroup-unreads)
9767               (push 'read to-marks)
9768               (gnus-info-set-read
9769                info (gnus-add-to-range (gnus-info-read info)
9770                                        (list (cdr art-group)))))
9771
9772             ;; See whether the article is to be put in the cache.
9773             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9774                    (marks (if expirable
9775                               gnus-article-mark-lists
9776                             (delete '(expirable . expire)
9777                                     (copy-sequence gnus-article-mark-lists))))
9778                    (to-article (cdr art-group)))
9779
9780               ;; Enter the article into the cache in the new group,
9781               ;; if that is required.
9782               (when gnus-use-cache
9783                 (gnus-cache-possibly-enter-article
9784                  to-group to-article
9785                  (memq article gnus-newsgroup-marked)
9786                  (memq article gnus-newsgroup-dormant)
9787                  (memq article gnus-newsgroup-unreads)))
9788
9789               (when gnus-preserve-marks
9790                 ;; Copy any marks over to the new group.
9791                 (when (and (equal to-group gnus-newsgroup-name)
9792                            (not (memq article gnus-newsgroup-unreads)))
9793                   ;; Mark this article as read in this group.
9794                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9795                   (setcdr (gnus-active to-group) to-article)
9796                   (setcdr gnus-newsgroup-active to-article))
9797
9798                 (while marks
9799                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9800                     (when (memq article (symbol-value
9801                                          (intern (format "gnus-newsgroup-%s"
9802                                                          (caar marks)))))
9803                       (push (cdar marks) to-marks)
9804                       ;; If the other group is the same as this group,
9805                       ;; then we have to add the mark to the list.
9806                       (when (equal to-group gnus-newsgroup-name)
9807                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9808                              (cons to-article
9809                                    (symbol-value
9810                                     (intern (format "gnus-newsgroup-%s"
9811                                                     (caar marks)))))))
9812                       ;; Copy the marks to other group.
9813                       (gnus-add-marked-articles
9814                        to-group (cdar marks) (list to-article) info)))
9815                   (setq marks (cdr marks)))
9816
9817                 (when (and expirable
9818                            gnus-mark-copied-or-moved-articles-as-expirable
9819                            (not (memq 'expire to-marks)))
9820                   ;; Mark this article as expirable.
9821                   (push 'expire to-marks)
9822                   (when (equal to-group gnus-newsgroup-name)
9823                     (push to-article gnus-newsgroup-expirable))
9824                   ;; Copy the expirable mark to other group.
9825                   (gnus-add-marked-articles
9826                    to-group 'expire (list to-article) info))
9827
9828                 (gnus-request-set-mark
9829                  to-group (list (list (list to-article) 'add to-marks))))
9830
9831               (gnus-dribble-enter
9832                (concat "(gnus-group-set-info '"
9833                        (gnus-prin1-to-string (gnus-get-info to-group))
9834                        ")"))))
9835
9836           ;; Update the Xref header in this article to point to
9837           ;; the new crossposted article we have just created.
9838           (when (eq action 'crosspost)
9839             (with-current-buffer copy-buf
9840               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9841               (nnheader-replace-header "Xref" new-xref)
9842               (gnus-request-replace-article
9843                article gnus-newsgroup-name (current-buffer) t)))
9844
9845           ;; run the move/copy/crosspost/respool hook
9846           (run-hook-with-args 'gnus-summary-article-move-hook
9847                               action
9848                               (gnus-data-header
9849                                (assoc article (gnus-data-list nil)))
9850                               gnus-newsgroup-name
9851                               to-newsgroup
9852                               select-method))
9853
9854         ;;;!!!Why is this necessary?
9855         (set-buffer gnus-summary-buffer)
9856
9857         (when (eq action 'move)
9858           (save-excursion
9859             (gnus-summary-goto-subject article)
9860             (gnus-summary-mark-article article gnus-canceled-mark)))))
9861       (push article articles-to-update-marks))
9862
9863     (save-excursion
9864       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9865     ;; Re-activate all groups that have been moved to.
9866     (with-current-buffer gnus-group-buffer
9867       (let ((gnus-group-marked to-groups))
9868         (gnus-group-get-new-news-this-group nil t)))
9869
9870     (gnus-kill-buffer copy-buf)
9871     (gnus-summary-position-point)
9872     (gnus-set-mode-line 'summary)))
9873
9874 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9875   "Copy the current article to some other group.
9876 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9877 When called interactively, if TO-NEWSGROUP is nil, use the value of
9878 the variable `gnus-move-split-methods' for finding a default target
9879 newsgroup.
9880 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9881 re-spool using this method."
9882   (interactive "P")
9883   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9884
9885 (defun gnus-summary-crosspost-article (&optional n)
9886   "Crosspost the current article to some other group."
9887   (interactive "P")
9888   (gnus-summary-move-article n nil nil 'crosspost))
9889
9890 (defcustom gnus-summary-respool-default-method nil
9891   "Default method type for respooling an article.
9892 If nil, use to the current newsgroup method."
9893   :type 'symbol
9894   :group 'gnus-summary-mail)
9895
9896 (defun gnus-summary-respool-article (&optional n method)
9897   "Respool the current article.
9898 The article will be squeezed through the mail spooling process again,
9899 which means that it will be put in some mail newsgroup or other
9900 depending on `nnmail-split-methods'.
9901 If N is a positive number, respool the N next articles.
9902 If N is a negative number, respool the N previous articles.
9903 If N is nil and any articles have been marked with the process mark,
9904 respool those articles instead.
9905
9906 Respooling can be done both from mail groups and \"real\" newsgroups.
9907 In the former case, the articles in question will be moved from the
9908 current group into whatever groups they are destined to.  In the
9909 latter case, they will be copied into the relevant groups."
9910   (interactive
9911    (list current-prefix-arg
9912          (let* ((methods (gnus-methods-using 'respool))
9913                 (methname
9914                  (symbol-name (or gnus-summary-respool-default-method
9915                                   (car (gnus-find-method-for-group
9916                                         gnus-newsgroup-name)))))
9917                 (method
9918                  (gnus-completing-read-with-default
9919                   methname "Backend to use when respooling"
9920                   methods nil t nil 'gnus-mail-method-history))
9921                 ms)
9922            (cond
9923             ((zerop (length (setq ms (gnus-servers-using-backend
9924                                       (intern method)))))
9925              (list (intern method) ""))
9926             ((= 1 (length ms))
9927              (car ms))
9928             (t
9929              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9930                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9931                            ms-alist))))))))
9932   (unless method
9933     (error "No method given for respooling"))
9934   (if (assoc (symbol-name
9935               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9936              (gnus-methods-using 'respool))
9937       (gnus-summary-move-article n nil method)
9938     (gnus-summary-copy-article n nil method)))
9939
9940 (defun gnus-summary-import-article (file &optional edit)
9941   "Import an arbitrary file into a mail newsgroup."
9942   (interactive "fImport file: \nP")
9943   (let ((group gnus-newsgroup-name)
9944         (now (current-time))
9945         atts lines group-art)
9946     (unless (gnus-check-backend-function 'request-accept-article group)
9947       (error "%s does not support article importing" group))
9948     (or (file-readable-p file)
9949         (not (file-regular-p file))
9950         (error "Can't read %s" file))
9951     (with-current-buffer (gnus-get-buffer-create " *import file*")
9952       (erase-buffer)
9953       (nnheader-insert-file-contents file)
9954       (goto-char (point-min))
9955       (if (nnheader-article-p)
9956           (save-restriction
9957             (goto-char (point-min))
9958             (search-forward "\n\n" nil t)
9959             (narrow-to-region (point-min) (1- (point)))
9960             (goto-char (point-min))
9961             (unless (re-search-forward "^date:" nil t)
9962               (goto-char (point-max))
9963               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9964        ;; This doesn't look like an article, so we fudge some headers.
9965         (setq atts (file-attributes file)
9966               lines (count-lines (point-min) (point-max)))
9967         (insert "From: " (read-string "From: ") "\n"
9968                 "Subject: " (read-string "Subject: ") "\n"
9969                 "Date: " (message-make-date (nth 5 atts)) "\n"
9970                 "Message-ID: " (message-make-message-id) "\n"
9971                 "Lines: " (int-to-string lines) "\n"
9972                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9973       (setq group-art (gnus-request-accept-article group nil t))
9974       (kill-buffer (current-buffer)))
9975     (setq gnus-newsgroup-active (gnus-activate-group group))
9976     (forward-line 1)
9977     (gnus-summary-goto-article (cdr group-art) nil t)
9978     (when edit
9979       (gnus-summary-edit-article))))
9980
9981 (defun gnus-summary-create-article ()
9982   "Create an article in a mail newsgroup."
9983   (interactive)
9984   (let ((group gnus-newsgroup-name)
9985         (now (current-time))
9986         group-art)
9987     (unless (gnus-check-backend-function 'request-accept-article group)
9988       (error "%s does not support article importing" group))
9989     (with-current-buffer (gnus-get-buffer-create " *import file*")
9990       (erase-buffer)
9991       (goto-char (point-min))
9992       ;; This doesn't look like an article, so we fudge some headers.
9993       (insert "From: " (read-string "From: ") "\n"
9994               "Subject: " (read-string "Subject: ") "\n"
9995               "Date: " (message-make-date now) "\n"
9996               "Message-ID: " (message-make-message-id) "\n")
9997       (setq group-art (gnus-request-accept-article group nil t))
9998       (kill-buffer (current-buffer)))
9999     (setq gnus-newsgroup-active (gnus-activate-group group))
10000     (forward-line 1)
10001     (gnus-summary-goto-article (cdr group-art) nil t)
10002     (gnus-summary-edit-article)))
10003
10004 (defun gnus-summary-article-posted-p ()
10005   "Say whether the current (mail) article is available from news as well.
10006 This will be the case if the article has both been mailed and posted."
10007   (interactive)
10008   (let ((id (mail-header-references (gnus-summary-article-header)))
10009         (gnus-override-method (car (gnus-refer-article-methods))))
10010     (if (gnus-request-head id "")
10011         (gnus-message 2 "The current message was found on %s"
10012                       gnus-override-method)
10013       (gnus-message 2 "The current message couldn't be found on %s"
10014                     gnus-override-method)
10015       nil)))
10016
10017 (defun gnus-summary-expire-articles (&optional now)
10018   "Expire all articles that are marked as expirable in the current group."
10019   (interactive)
10020   (when (and (not gnus-group-is-exiting-without-update-p)
10021              (gnus-check-backend-function
10022               'request-expire-articles gnus-newsgroup-name))
10023     ;; This backend supports expiry.
10024     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10025            (expirable (if total
10026                           (progn
10027                             ;; We need to update the info for
10028                             ;; this group for `gnus-list-of-read-articles'
10029                             ;; to give us the right answer.
10030                             (gnus-run-hooks 'gnus-exit-group-hook)
10031                             (gnus-summary-update-info)
10032                             (gnus-list-of-read-articles gnus-newsgroup-name))
10033                         (setq gnus-newsgroup-expirable
10034                               (sort gnus-newsgroup-expirable '<))))
10035            (expiry-wait (if now 'immediate
10036                           (gnus-group-find-parameter
10037                            gnus-newsgroup-name 'expiry-wait)))
10038            (nnmail-expiry-target
10039             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10040                 nnmail-expiry-target))
10041            es)
10042       (when expirable
10043         ;; There are expirable articles in this group, so we run them
10044         ;; through the expiry process.
10045         (gnus-message 6 "Expiring articles...")
10046         (unless (gnus-check-group gnus-newsgroup-name)
10047           (error "Can't open server for %s" gnus-newsgroup-name))
10048         ;; The list of articles that weren't expired is returned.
10049         (save-excursion
10050           (if expiry-wait
10051               (let ((nnmail-expiry-wait-function nil)
10052                     (nnmail-expiry-wait expiry-wait))
10053                 (setq es (gnus-request-expire-articles
10054                           expirable gnus-newsgroup-name)))
10055             (setq es (gnus-request-expire-articles
10056                       expirable gnus-newsgroup-name)))
10057           (unless total
10058             (setq gnus-newsgroup-expirable es))
10059           ;; We go through the old list of expirable, and mark all
10060           ;; really expired articles as nonexistent.
10061           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10062             (let ((gnus-use-cache nil))
10063               (dolist (article expirable)
10064                 (when (and (not (memq article es))
10065                            (gnus-data-find article))
10066                   (gnus-summary-mark-article article gnus-canceled-mark)
10067                   (run-hook-with-args 'gnus-summary-article-expire-hook
10068                                       'delete
10069                                       (gnus-data-header
10070                                        (assoc article (gnus-data-list nil)))
10071                                       gnus-newsgroup-name
10072                                       nil
10073                                       nil))))))
10074         (gnus-message 6 "Expiring articles...done")))))
10075
10076 (defun gnus-summary-expire-articles-now ()
10077   "Expunge all expirable articles in the current group.
10078 This means that *all* articles that are marked as expirable will be
10079 deleted forever, right now."
10080   (interactive)
10081   (or gnus-expert-user
10082       (gnus-yes-or-no-p
10083        "Are you really, really sure you want to delete all expirable messages? ")
10084       (error "Phew!"))
10085   (gnus-summary-expire-articles t))
10086
10087 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10088 (defun gnus-summary-delete-article (&optional n)
10089   "Delete the N next (mail) articles.
10090 This command actually deletes articles.  This is not a marking
10091 command.  The article will disappear forever from your life, never to
10092 return.
10093
10094 If N is negative, delete backwards.
10095 If N is nil and articles have been marked with the process mark,
10096 delete these instead.
10097
10098 If `gnus-novice-user' is non-nil you will be asked for
10099 confirmation before the articles are deleted."
10100   (interactive "P")
10101   (unless (gnus-check-backend-function 'request-expire-articles
10102                                        gnus-newsgroup-name)
10103     (error "The current newsgroup does not support article deletion"))
10104   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10105     (error "Couldn't open server"))
10106   ;; Compute the list of articles to delete.
10107   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10108         (nnmail-expiry-target 'delete)
10109         not-deleted)
10110     (if (and gnus-novice-user
10111              (not (gnus-yes-or-no-p
10112                    (format "Do you really want to delete %s forever? "
10113                            (if (> (length articles) 1)
10114                                (format "these %s articles" (length articles))
10115                              "this article")))))
10116         ()
10117       ;; Delete the articles.
10118       (setq not-deleted (gnus-request-expire-articles
10119                          articles gnus-newsgroup-name 'force))
10120       (save-excursion
10121         (while articles
10122           (gnus-summary-remove-process-mark (car articles))
10123           ;; The backend might not have been able to delete the article
10124           ;; after all.
10125           (unless (memq (car articles) not-deleted)
10126             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10127           (let* ((article (car articles))
10128                  (ghead  (gnus-data-header
10129                           (assoc article (gnus-data-list nil)))))
10130             (run-hook-with-args 'gnus-summary-article-delete-hook
10131                                 'delete ghead gnus-newsgroup-name nil
10132                                 nil))
10133           (setq articles (cdr articles))))
10134       (when not-deleted
10135         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10136     (gnus-summary-position-point)
10137     (gnus-set-mode-line 'summary)
10138     not-deleted))
10139
10140 (defun gnus-summary-edit-article (&optional arg)
10141   "Edit the current article.
10142 This will have permanent effect only in mail groups.
10143 If ARG is nil, edit the decoded articles.
10144 If ARG is 1, edit the raw articles.
10145 If ARG is 2, edit the raw articles even in read-only groups.
10146 If ARG is 3, edit the articles with the current handles.
10147 Otherwise, allow editing of articles even in read-only
10148 groups."
10149   (interactive "P")
10150   (let (force raw current-handles)
10151     (cond
10152      ((null arg))
10153      ((eq arg 1)
10154       (setq raw t))
10155      ((eq arg 2)
10156       (setq raw t
10157             force t))
10158      ((eq arg 3)
10159       (setq current-handles
10160             (and (gnus-buffer-live-p gnus-article-buffer)
10161                  (with-current-buffer gnus-article-buffer
10162                    (prog1
10163                        gnus-article-mime-handles
10164                      (setq gnus-article-mime-handles nil))))))
10165      (t
10166       (setq force t)))
10167     (when (and raw (not force)
10168                (member gnus-newsgroup-name '("nndraft:delayed"
10169                                              "nndraft:drafts"
10170                                              "nndraft:queue")))
10171       (error "Can't edit the raw article in group %s"
10172              gnus-newsgroup-name))
10173     (with-current-buffer gnus-summary-buffer
10174       (let ((mail-parse-charset gnus-newsgroup-charset)
10175             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10176         (gnus-set-global-variables)
10177         (when (and (not force)
10178                    (gnus-group-read-only-p))
10179           (error "The current newsgroup does not support article editing"))
10180         (gnus-summary-show-article t)
10181         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10182           (with-current-buffer gnus-article-buffer
10183             (mm-enable-multibyte)))
10184         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10185             (setq raw t))
10186         (gnus-article-edit-article
10187          (if raw 'ignore
10188            `(lambda ()
10189               (let ((mbl mml-buffer-list))
10190                 (setq mml-buffer-list nil)
10191                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10192                   (mime-to-mml ,'current-handles))
10193                 (let ((mbl1 mml-buffer-list))
10194                   (setq mml-buffer-list mbl)
10195                   (set (make-local-variable 'mml-buffer-list) mbl1))
10196                 (gnus-make-local-hook 'kill-buffer-hook)
10197                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10198          `(lambda (no-highlight)
10199             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10200                   (message-options message-options)
10201                   (message-options-set-recipient)
10202                   (mail-parse-ignored-charsets
10203                    ',gnus-newsgroup-ignored-charsets)
10204                   (rfc2047-header-encoding-alist
10205                    ',(let ((charset (gnus-group-name-charset
10206                                      (gnus-find-method-for-group
10207                                       gnus-newsgroup-name)
10208                                      gnus-newsgroup-name)))
10209                        (append (list (cons "Newsgroups" charset)
10210                                      (cons "Followup-To" charset)
10211                                      (cons "Xref" charset))
10212                                rfc2047-header-encoding-alist))))
10213               ,(if (not raw) '(progn
10214                                 (mml-to-mime)
10215                                 (mml-destroy-buffers)
10216                                 (remove-hook 'kill-buffer-hook
10217                                              'mml-destroy-buffers t)
10218                                 (kill-local-variable 'mml-buffer-list)))
10219               (gnus-summary-edit-article-done
10220                ,(or (mail-header-references gnus-current-headers) "")
10221                ,(gnus-group-read-only-p)
10222                ,gnus-summary-buffer no-highlight))))))))
10223
10224 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10225
10226 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10227                                                  no-highlight)
10228   "Make edits to the current article permanent."
10229   (interactive)
10230   (save-excursion
10231    ;; The buffer restriction contains the entire article if it exists.
10232     (when (article-goto-body)
10233       (let ((lines (count-lines (point) (point-max)))
10234             (length (- (point-max) (point)))
10235             (case-fold-search t)
10236             (body (copy-marker (point))))
10237         (goto-char (point-min))
10238         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10239           (delete-region (match-beginning 1) (match-end 1))
10240           (insert (number-to-string length)))
10241         (goto-char (point-min))
10242         (when (re-search-forward
10243                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10244           (delete-region (match-beginning 1) (match-end 1))
10245           (insert (number-to-string length)))
10246         (goto-char (point-min))
10247         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10248           (delete-region (match-beginning 1) (match-end 1))
10249           (insert (number-to-string lines))))))
10250   ;; Replace the article.
10251   (let ((buf (current-buffer)))
10252     (with-temp-buffer
10253       (insert-buffer-substring buf)
10254
10255       (if (and (not read-only)
10256                (not (gnus-request-replace-article
10257                      (cdr gnus-article-current) (car gnus-article-current)
10258                      (current-buffer) t)))
10259           (error "Couldn't replace article")
10260         ;; Update the summary buffer.
10261         (if (and references
10262                  (equal (message-tokenize-header references " ")
10263                         (message-tokenize-header
10264                          (or (message-fetch-field "references") "") " ")))
10265             ;; We only have to update this line.
10266             (save-excursion
10267               (save-restriction
10268                 (message-narrow-to-head)
10269                 (let ((head (buffer-substring-no-properties
10270                              (point-min) (point-max)))
10271                       header)
10272                   (with-temp-buffer
10273                     (insert (format "211 %d Article retrieved.\n"
10274                                     (cdr gnus-article-current)))
10275                     (insert head)
10276                     (insert ".\n")
10277                     (let ((nntp-server-buffer (current-buffer)))
10278                       (setq header (car (gnus-get-newsgroup-headers
10279                                          nil t))))
10280                     (with-current-buffer gnus-summary-buffer
10281                       (gnus-data-set-header
10282                        (gnus-data-find (cdr gnus-article-current))
10283                        header)
10284                       (gnus-summary-update-article-line
10285                        (cdr gnus-article-current) header)
10286                       (if (gnus-summary-goto-subject
10287                            (cdr gnus-article-current) nil t)
10288                           (gnus-summary-update-secondary-mark
10289                            (cdr gnus-article-current))))))))
10290           ;; Update threads.
10291           (set-buffer (or buffer gnus-summary-buffer))
10292           (gnus-summary-update-article (cdr gnus-article-current))
10293           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10294               (gnus-summary-update-secondary-mark
10295                (cdr gnus-article-current))))
10296         ;; Prettify the article buffer again.
10297         (unless no-highlight
10298           (with-current-buffer gnus-article-buffer
10299             ;;;!!! Fix this -- article should be rehighlighted.
10300             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10301             (set-buffer gnus-original-article-buffer)
10302             (gnus-request-article
10303              (cdr gnus-article-current)
10304              (car gnus-article-current) (current-buffer))))
10305         ;; Prettify the summary buffer line.
10306         (when (gnus-visual-p 'summary-highlight 'highlight)
10307           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10308
10309 (defun gnus-summary-edit-wash (key)
10310   "Perform editing command KEY in the article buffer."
10311   (interactive
10312    (list
10313     (progn
10314       (message "%s" (concat (this-command-keys) "- "))
10315       (read-char))))
10316   (message "")
10317   (gnus-summary-edit-article)
10318   (execute-kbd-macro (concat (this-command-keys) key))
10319   (gnus-article-edit-done))
10320
10321 ;;; Respooling
10322
10323 (defun gnus-summary-respool-query (&optional silent trace)
10324   "Query where the respool algorithm would put this article."
10325   (interactive)
10326   (let (gnus-mark-article-hook)
10327     (gnus-summary-select-article)
10328     (with-current-buffer gnus-original-article-buffer
10329       (let ((groups (nnmail-article-group 'identity trace)))
10330         (unless silent
10331           (if groups
10332               (message "This message would go to %s"
10333                        (mapconcat 'car groups ", "))
10334             (message "This message would go to no groups"))
10335           groups)))))
10336
10337 (defun gnus-summary-respool-trace ()
10338   "Trace where the respool algorithm would put this article.
10339 Display a buffer showing all fancy splitting patterns which matched."
10340   (interactive)
10341   (gnus-summary-respool-query nil t))
10342
10343 ;; Summary marking commands.
10344
10345 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10346   "Mark articles which has the same subject as read, and then select the next.
10347 If UNMARK is positive, remove any kind of mark.
10348 If UNMARK is negative, tick articles."
10349   (interactive "P")
10350   (when unmark
10351     (setq unmark (prefix-numeric-value unmark)))
10352   (let ((count
10353          (gnus-summary-mark-same-subject
10354           (gnus-summary-article-subject) unmark)))
10355     ;; Select next unread article.  If auto-select-same mode, should
10356     ;; select the first unread article.
10357     (gnus-summary-next-article t (and gnus-auto-select-same
10358                                       (gnus-summary-article-subject)))
10359     (gnus-message 7 "%d article%s marked as %s"
10360                   count (if (= count 1) " is" "s are")
10361                   (if unmark "unread" "read"))))
10362
10363 (defun gnus-summary-kill-same-subject (&optional unmark)
10364   "Mark articles which has the same subject as read.
10365 If UNMARK is positive, remove any kind of mark.
10366 If UNMARK is negative, tick articles."
10367   (interactive "P")
10368   (when unmark
10369     (setq unmark (prefix-numeric-value unmark)))
10370   (let ((count
10371          (gnus-summary-mark-same-subject
10372           (gnus-summary-article-subject) unmark)))
10373     ;; If marked as read, go to next unread subject.
10374     (when (null unmark)
10375       ;; Go to next unread subject.
10376       (gnus-summary-next-subject 1 t))
10377     (gnus-message 7 "%d articles are marked as %s"
10378                   count (if unmark "unread" "read"))))
10379
10380 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10381   "Mark articles with same SUBJECT as read, and return marked number.
10382 If optional argument UNMARK is positive, remove any kinds of marks.
10383 If optional argument UNMARK is negative, mark articles as unread instead."
10384   (let ((count 1))
10385     (save-excursion
10386       (cond
10387        ((null unmark)                   ; Mark as read.
10388         (while (and
10389                 (progn
10390                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10391                   (gnus-summary-show-thread) t)
10392                 (gnus-summary-find-subject subject))
10393           (setq count (1+ count))))
10394        ((> unmark 0)                    ; Tick.
10395         (while (and
10396                 (progn
10397                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10398                   (gnus-summary-show-thread) t)
10399                 (gnus-summary-find-subject subject))
10400           (setq count (1+ count))))
10401        (t                               ; Mark as unread.
10402         (while (and
10403                 (progn
10404                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10405                   (gnus-summary-show-thread) t)
10406                 (gnus-summary-find-subject subject))
10407           (setq count (1+ count)))))
10408       (gnus-set-mode-line 'summary)
10409       ;; Return the number of marked articles.
10410       count)))
10411
10412 (defun gnus-summary-mark-as-processable (n &optional unmark)
10413   "Set the process mark on the next N articles.
10414 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10415 the process mark instead.  The difference between N and the actual
10416 number of articles marked is returned."
10417   (interactive "P")
10418   (if (and (null n) (gnus-region-active-p))
10419       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10420     (setq n (prefix-numeric-value n))
10421     (let ((backward (< n 0))
10422           (n (abs n)))
10423       (while (and
10424               (> n 0)
10425               (if unmark
10426                   (gnus-summary-remove-process-mark
10427                    (gnus-summary-article-number))
10428                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10429               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10430         (setq n (1- n)))
10431       (when (/= 0 n)
10432         (gnus-message 7 "No more articles"))
10433       (gnus-summary-recenter)
10434       (gnus-summary-position-point)
10435       n)))
10436
10437 (defun gnus-summary-unmark-as-processable (n)
10438   "Remove the process mark from the next N articles.
10439 If N is negative, unmark backward instead.  The difference between N and
10440 the actual number of articles unmarked is returned."
10441   (interactive "P")
10442   (gnus-summary-mark-as-processable n t))
10443
10444 (defun gnus-summary-unmark-all-processable ()
10445   "Remove the process mark from all articles."
10446   (interactive)
10447   (save-excursion
10448     (while gnus-newsgroup-processable
10449       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10450   (gnus-summary-position-point))
10451
10452 (defun gnus-summary-add-mark (article type)
10453   "Mark ARTICLE with a mark of TYPE."
10454   (let ((vtype (car (assq type gnus-article-mark-lists)))
10455         var)
10456     (if (not vtype)
10457         (error "No such mark type: %s" type)
10458       (setq var (intern (format "gnus-newsgroup-%s" type)))
10459       (set var (cons article (symbol-value var)))
10460       (if (memq type '(processable cached replied forwarded recent saved))
10461           (gnus-summary-update-secondary-mark article)
10462         ;;; !!! This is bogus.  We should find out what primary
10463         ;;; !!! mark we want to set.
10464         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10465
10466 (defun gnus-summary-mark-as-expirable (n)
10467   "Mark N articles forward as expirable.
10468 If N is negative, mark backward instead.  The difference between N and
10469 the actual number of articles marked is returned."
10470   (interactive "p")
10471   (gnus-summary-mark-forward n gnus-expirable-mark))
10472
10473 (defun gnus-summary-mark-as-spam (n)
10474   "Mark N articles forward as spam.
10475 If N is negative, mark backward instead.  The difference between N and
10476 the actual number of articles marked is returned."
10477   (interactive "p")
10478   (gnus-summary-mark-forward n gnus-spam-mark))
10479
10480 (defun gnus-summary-mark-article-as-replied (article)
10481   "Mark ARTICLE as replied to and update the summary line.
10482 ARTICLE can also be a list of articles."
10483   (interactive (list (gnus-summary-article-number)))
10484   (let ((articles (if (listp article) article (list article))))
10485     (dolist (article articles)
10486       (unless (numberp article)
10487         (error "%s is not a number" article))
10488       (push article gnus-newsgroup-replied)
10489       (let ((inhibit-read-only t))
10490         (when (gnus-summary-goto-subject article nil t)
10491           (gnus-summary-update-secondary-mark article))))))
10492
10493 (defun gnus-summary-mark-article-as-forwarded (article)
10494   "Mark ARTICLE as forwarded and update the summary line.
10495 ARTICLE can also be a list of articles."
10496   (let ((articles (if (listp article) article (list article))))
10497     (dolist (article articles)
10498       (push article gnus-newsgroup-forwarded)
10499       (let ((inhibit-read-only t))
10500         (when (gnus-summary-goto-subject article nil t)
10501           (gnus-summary-update-secondary-mark article))))))
10502
10503 (defun gnus-summary-set-bookmark (article)
10504   "Set a bookmark in current article."
10505   (interactive (list (gnus-summary-article-number)))
10506   (when (or (not (get-buffer gnus-article-buffer))
10507             (not gnus-current-article)
10508             (not gnus-article-current)
10509             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10510     (error "No current article selected"))
10511   ;; Remove old bookmark, if one exists.
10512   (gnus-pull article gnus-newsgroup-bookmarks)
10513   ;; Set the new bookmark, which is on the form
10514   ;; (article-number . line-number-in-body).
10515   (push
10516    (cons article
10517          (with-current-buffer gnus-article-buffer
10518            (count-lines
10519             (min (point)
10520                  (save-excursion
10521                    (article-goto-body)
10522                    (point)))
10523             (point))))
10524    gnus-newsgroup-bookmarks)
10525   (gnus-message 6 "A bookmark has been added to the current article."))
10526
10527 (defun gnus-summary-remove-bookmark (article)
10528   "Remove the bookmark from the current article."
10529   (interactive (list (gnus-summary-article-number)))
10530   ;; Remove old bookmark, if one exists.
10531   (if (not (assq article gnus-newsgroup-bookmarks))
10532       (gnus-message 6 "No bookmark in current article.")
10533     (gnus-pull article gnus-newsgroup-bookmarks)
10534     (gnus-message 6 "Removed bookmark.")))
10535
10536 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10537 (defun gnus-summary-mark-as-dormant (n)
10538   "Mark N articles forward as dormant.
10539 If N is negative, mark backward instead.  The difference between N and
10540 the actual number of articles marked is returned."
10541   (interactive "p")
10542   (gnus-summary-mark-forward n gnus-dormant-mark))
10543
10544 (defun gnus-summary-set-process-mark (article)
10545   "Set the process mark on ARTICLE and update the summary line."
10546   (setq gnus-newsgroup-processable
10547         (cons article
10548               (delq article gnus-newsgroup-processable)))
10549   (when (gnus-summary-goto-subject article)
10550     (gnus-summary-show-thread)
10551     (gnus-summary-goto-subject article)
10552     (gnus-summary-update-secondary-mark article)))
10553
10554 (defun gnus-summary-remove-process-mark (&rest articles)
10555   "Remove the process mark from ARTICLES and update the summary line."
10556   (dolist (article articles)
10557     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10558     (when (gnus-summary-goto-subject article)
10559       (gnus-summary-show-thread)
10560       (gnus-summary-goto-subject article)
10561       (gnus-summary-update-secondary-mark article)))
10562   t)
10563
10564 (defun gnus-summary-set-saved-mark (article)
10565   "Set the process mark on ARTICLE and update the summary line."
10566   (push article gnus-newsgroup-saved)
10567   (when (gnus-summary-goto-subject article)
10568     (gnus-summary-update-secondary-mark article)))
10569
10570 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10571   "Mark N articles as read forwards.
10572 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10573 The difference between N and the actual number of articles marked is
10574 returned.
10575 If NO-EXPIRE, auto-expiry will be inhibited."
10576   (interactive "p")
10577   (gnus-summary-show-thread)
10578   (let ((backward (< n 0))
10579         (gnus-summary-goto-unread
10580          (and gnus-summary-goto-unread
10581               (not (eq gnus-summary-goto-unread 'never))
10582               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10583                                     gnus-ticked-mark gnus-dormant-mark)))))
10584         (n (abs n))
10585         (mark (or mark gnus-del-mark)))
10586     (while (and (> n 0)
10587                 (gnus-summary-mark-article nil mark no-expire)
10588                 (zerop (gnus-summary-next-subject
10589                         (if backward -1 1)
10590                         (and gnus-summary-goto-unread
10591                              (not (eq gnus-summary-goto-unread 'never)))
10592                         t)))
10593       (setq n (1- n)))
10594     (when (/= 0 n)
10595       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10596     (gnus-summary-recenter)
10597     (gnus-summary-position-point)
10598     (gnus-set-mode-line 'summary)
10599     n))
10600
10601 (defun gnus-summary-mark-article-as-read (mark)
10602   "Mark the current article quickly as read with MARK."
10603   (let ((article (gnus-summary-article-number)))
10604     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10605     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10606     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10607     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10608     (push (cons article mark) gnus-newsgroup-reads)
10609     ;; Possibly remove from cache, if that is used.
10610     (when gnus-use-cache
10611       (gnus-cache-enter-remove-article article))
10612     ;; Allow the backend to change the mark.
10613     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10614     ;; Check for auto-expiry.
10615     (when (and gnus-newsgroup-auto-expire
10616                (memq mark gnus-auto-expirable-marks))
10617       (setq mark gnus-expirable-mark)
10618       ;; Let the backend know about the mark change.
10619       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10620       (push article gnus-newsgroup-expirable))
10621     ;; Set the mark in the buffer.
10622     (gnus-summary-update-mark mark 'unread)
10623     t))
10624
10625 (defun gnus-summary-mark-article-as-unread (mark)
10626   "Mark the current article quickly as unread with MARK."
10627   (let* ((article (gnus-summary-article-number))
10628          (old-mark (gnus-summary-article-mark article)))
10629     ;; Allow the backend to change the mark.
10630     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10631     (if (eq mark old-mark)
10632         t
10633       (if (<= article 0)
10634           (progn
10635             (gnus-error 1 "Can't mark negative article numbers")
10636             nil)
10637         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10638         (setq gnus-newsgroup-spam-marked
10639               (delq article gnus-newsgroup-spam-marked))
10640         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10641         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10642         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10643         (cond ((= mark gnus-ticked-mark)
10644                (setq gnus-newsgroup-marked
10645                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10646                                               article)))
10647               ((= mark gnus-spam-mark)
10648                (setq gnus-newsgroup-spam-marked
10649                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10650                                               article)))
10651               ((= mark gnus-dormant-mark)
10652                (setq gnus-newsgroup-dormant
10653                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10654                                               article)))
10655               (t
10656                (setq gnus-newsgroup-unreads
10657                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10658                                               article))))
10659         (gnus-pull article gnus-newsgroup-reads)
10660
10661         ;; See whether the article is to be put in the cache.
10662         (and gnus-use-cache
10663              (vectorp (gnus-summary-article-header article))
10664              (save-excursion
10665                (gnus-cache-possibly-enter-article
10666                 gnus-newsgroup-name article
10667                 (= mark gnus-ticked-mark)
10668                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10669
10670         ;; Fix the mark.
10671         (gnus-summary-update-mark mark 'unread)
10672         t))))
10673
10674 (defun gnus-summary-mark-article (&optional article mark no-expire)
10675   "Mark ARTICLE with MARK.  MARK can be any character.
10676 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10677 `??' (dormant) and `?E' (expirable).
10678 If MARK is nil, then the default character `?r' is used.
10679 If ARTICLE is nil, then the article on the current line will be
10680 marked.
10681 If NO-EXPIRE, auto-expiry will be inhibited."
10682   ;; The mark might be a string.
10683   (when (stringp mark)
10684     (setq mark (aref mark 0)))
10685   ;; If no mark is given, then we check auto-expiring.
10686   (when (null mark)
10687     (setq mark gnus-del-mark))
10688   (when (and (not no-expire)
10689              gnus-newsgroup-auto-expire
10690              (memq mark gnus-auto-expirable-marks))
10691     (setq mark gnus-expirable-mark))
10692   (let ((article (or article (gnus-summary-article-number)))
10693         (old-mark (gnus-summary-article-mark article)))
10694     ;; Allow the backend to change the mark.
10695     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10696     (if (eq mark old-mark)
10697         t
10698       (unless article
10699         (error "No article on current line"))
10700       (if (not (if (or (= mark gnus-unread-mark)
10701                        (= mark gnus-ticked-mark)
10702                        (= mark gnus-spam-mark)
10703                        (= mark gnus-dormant-mark))
10704                    (gnus-mark-article-as-unread article mark)
10705                  (gnus-mark-article-as-read article mark)))
10706           t
10707         ;; See whether the article is to be put in the cache.
10708         (and gnus-use-cache
10709              (not (= mark gnus-canceled-mark))
10710              (vectorp (gnus-summary-article-header article))
10711              (save-excursion
10712                (gnus-cache-possibly-enter-article
10713                 gnus-newsgroup-name article
10714                 (= mark gnus-ticked-mark)
10715                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10716
10717         (when (gnus-summary-goto-subject article nil t)
10718           (let ((inhibit-read-only t))
10719             (gnus-summary-show-thread)
10720             ;; Fix the mark.
10721             (gnus-summary-update-mark mark 'unread)
10722             t))))))
10723
10724 (defun gnus-summary-update-secondary-mark (article)
10725   "Update the secondary (read, process, cache) mark."
10726   (gnus-summary-update-mark
10727    (cond ((memq article gnus-newsgroup-processable)
10728           gnus-process-mark)
10729          ((memq article gnus-newsgroup-cached)
10730           gnus-cached-mark)
10731          ((memq article gnus-newsgroup-replied)
10732           gnus-replied-mark)
10733          ((memq article gnus-newsgroup-forwarded)
10734           gnus-forwarded-mark)
10735          ((memq article gnus-newsgroup-saved)
10736           gnus-saved-mark)
10737          ((memq article gnus-newsgroup-recent)
10738           gnus-recent-mark)
10739          ((memq article gnus-newsgroup-unseen)
10740           gnus-unseen-mark)
10741          (t gnus-no-mark))
10742    'replied)
10743   (when (gnus-visual-p 'summary-highlight 'highlight)
10744     (gnus-run-hooks 'gnus-summary-update-hook))
10745   t)
10746
10747 (defun gnus-summary-update-download-mark (article)
10748   "Update the download mark."
10749   (gnus-summary-update-mark
10750    (cond ((memq article gnus-newsgroup-undownloaded)
10751           gnus-undownloaded-mark)
10752          (gnus-newsgroup-agentized
10753           gnus-downloaded-mark)
10754          (t
10755           gnus-no-mark))
10756    'download)
10757   (gnus-summary-update-line t)
10758   t)
10759
10760 (defun gnus-summary-update-mark (mark type)
10761   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10762         (inhibit-read-only t))
10763     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10764     (when forward
10765       (when (looking-at "\r")
10766         (incf forward))
10767       (when (<= (+ forward (point)) (point-max))
10768         ;; Go to the right position on the line.
10769         (goto-char (+ forward (point)))
10770         ;; Replace the old mark with the new mark.
10771         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10772         ;; Optionally update the marks by some user rule.
10773         (when (eq type 'unread)
10774           (gnus-data-set-mark
10775            (gnus-data-find (gnus-summary-article-number)) mark)
10776           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10777
10778 (defun gnus-mark-article-as-read (article &optional mark)
10779   "Enter ARTICLE in the pertinent lists and remove it from others."
10780   ;; Make the article expirable.
10781   (let ((mark (or mark gnus-del-mark)))
10782     (setq gnus-newsgroup-expirable
10783           (if (= mark gnus-expirable-mark)
10784               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10785             (delq article gnus-newsgroup-expirable)))
10786     ;; Remove from unread and marked lists.
10787     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10788     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10789     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10790     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10791     (push (cons article mark) gnus-newsgroup-reads)
10792     ;; Possibly remove from cache, if that is used.
10793     (when gnus-use-cache
10794       (gnus-cache-enter-remove-article article))
10795     t))
10796
10797 (defun gnus-mark-article-as-unread (article &optional mark)
10798   "Enter ARTICLE in the pertinent lists and remove it from others."
10799   (let ((mark (or mark gnus-ticked-mark)))
10800     (if (<= article 0)
10801         (progn
10802           (gnus-error 1 "Can't mark negative article numbers")
10803           nil)
10804       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10805             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10806             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10807             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10808             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10809
10810       ;; Unsuppress duplicates?
10811       (when gnus-suppress-duplicates
10812         (gnus-dup-unsuppress-article article))
10813
10814       (cond ((= mark gnus-ticked-mark)
10815              (setq gnus-newsgroup-marked
10816                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10817             ((= mark gnus-spam-mark)
10818              (setq gnus-newsgroup-spam-marked
10819                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10820                                             article)))
10821             ((= mark gnus-dormant-mark)
10822              (setq gnus-newsgroup-dormant
10823                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10824             (t
10825              (setq gnus-newsgroup-unreads
10826                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10827       (gnus-pull article gnus-newsgroup-reads)
10828       t)))
10829
10830 (defalias 'gnus-summary-mark-as-unread-forward
10831   'gnus-summary-tick-article-forward)
10832 (make-obsolete 'gnus-summary-mark-as-unread-forward
10833                'gnus-summary-tick-article-forward "Emacs 20.4")
10834 (defun gnus-summary-tick-article-forward (n)
10835   "Tick N articles forwards.
10836 If N is negative, tick backwards instead.
10837 The difference between N and the number of articles ticked is returned."
10838   (interactive "p")
10839   (gnus-summary-mark-forward n gnus-ticked-mark))
10840
10841 (defalias 'gnus-summary-mark-as-unread-backward
10842   'gnus-summary-tick-article-backward)
10843 (make-obsolete 'gnus-summary-mark-as-unread-backward
10844                'gnus-summary-tick-article-backward "Emacs 20.4")
10845 (defun gnus-summary-tick-article-backward (n)
10846   "Tick N articles backwards.
10847 The difference between N and the number of articles ticked is returned."
10848   (interactive "p")
10849   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10850
10851 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10852 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10853 (defun gnus-summary-tick-article (&optional article clear-mark)
10854   "Mark current article as unread.
10855 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10856 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10857   (interactive)
10858   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10859                                        gnus-ticked-mark)))
10860
10861 (defun gnus-summary-mark-as-read-forward (n)
10862   "Mark N articles as read forwards.
10863 If N is negative, mark backwards instead.
10864 The difference between N and the actual number of articles marked is
10865 returned."
10866   (interactive "p")
10867   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10868
10869 (defun gnus-summary-mark-as-read-backward (n)
10870   "Mark the N articles as read backwards.
10871 The difference between N and the actual number of articles marked is
10872 returned."
10873   (interactive "p")
10874   (gnus-summary-mark-forward
10875    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10876
10877 (defun gnus-summary-mark-as-read (&optional article mark)
10878   "Mark current article as read.
10879 ARTICLE specifies the article to be marked as read.
10880 MARK specifies a string to be inserted at the beginning of the line."
10881   (gnus-summary-mark-article article mark))
10882
10883 (defun gnus-summary-clear-mark-forward (n)
10884   "Clear marks from N articles forward.
10885 If N is negative, clear backward instead.
10886 The difference between N and the number of marks cleared is returned."
10887   (interactive "p")
10888   (gnus-summary-mark-forward n gnus-unread-mark))
10889
10890 (defun gnus-summary-clear-mark-backward (n)
10891   "Clear marks from N articles backward.
10892 The difference between N and the number of marks cleared is returned."
10893   (interactive "p")
10894   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10895
10896 (defun gnus-summary-mark-unread-as-read ()
10897   "Intended to be used by `gnus-mark-article-hook'."
10898   (when (memq gnus-current-article gnus-newsgroup-unreads)
10899     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10900
10901 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10902   "Intended to be used by `gnus-mark-article-hook'."
10903   (let ((mark (gnus-summary-article-mark)))
10904     (when (or (gnus-unread-mark-p mark)
10905               (gnus-read-mark-p mark))
10906       (gnus-summary-mark-article gnus-current-article
10907                                  (or new-mark gnus-read-mark)))))
10908
10909 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10910   "Intended to be used by `gnus-mark-article-hook'."
10911   (let ((mark (gnus-summary-article-mark)))
10912     (when (or (gnus-unread-mark-p mark)
10913               (gnus-read-mark-p mark))
10914       (gnus-summary-mark-article (gnus-summary-article-number)
10915                                  (or new-mark gnus-read-mark)))))
10916
10917 (defun gnus-summary-mark-unread-as-ticked ()
10918   "Intended to be used by `gnus-mark-article-hook'."
10919   (when (memq gnus-current-article gnus-newsgroup-unreads)
10920     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10921
10922 (defun gnus-summary-mark-region-as-read (point mark all)
10923   "Mark all unread articles between point and mark as read.
10924 If given a prefix, mark all articles between point and mark as read,
10925 even ticked and dormant ones."
10926   (interactive "r\nP")
10927   (save-excursion
10928     (let (article)
10929       (goto-char point)
10930       (beginning-of-line)
10931       (while (and
10932               (< (point) mark)
10933               (progn
10934                 (when (or all
10935                           (memq (setq article (gnus-summary-article-number))
10936                                 gnus-newsgroup-unreads))
10937                   (gnus-summary-mark-article article gnus-del-mark))
10938                 t)
10939               (gnus-summary-find-next))))))
10940
10941 (defun gnus-summary-mark-below (score mark)
10942   "Mark articles with score less than SCORE with MARK."
10943   (interactive "P\ncMark: ")
10944   (setq score (if score
10945                   (prefix-numeric-value score)
10946                 (or gnus-summary-default-score 0)))
10947   (with-current-buffer gnus-summary-buffer
10948     (goto-char (point-min))
10949     (while
10950         (progn
10951           (and (< (gnus-summary-article-score) score)
10952                (gnus-summary-mark-article nil mark))
10953           (gnus-summary-find-next)))))
10954
10955 (defun gnus-summary-kill-below (&optional score)
10956   "Mark articles with score below SCORE as read."
10957   (interactive "P")
10958   (gnus-summary-mark-below score gnus-killed-mark))
10959
10960 (defun gnus-summary-clear-above (&optional score)
10961   "Clear all marks from articles with score above SCORE."
10962   (interactive "P")
10963   (gnus-summary-mark-above score gnus-unread-mark))
10964
10965 (defun gnus-summary-tick-above (&optional score)
10966   "Tick all articles with score above SCORE."
10967   (interactive "P")
10968   (gnus-summary-mark-above score gnus-ticked-mark))
10969
10970 (defun gnus-summary-mark-above (score mark)
10971   "Mark articles with score over SCORE with MARK."
10972   (interactive "P\ncMark: ")
10973   (setq score (if score
10974                   (prefix-numeric-value score)
10975                 (or gnus-summary-default-score 0)))
10976   (with-current-buffer gnus-summary-buffer
10977     (goto-char (point-min))
10978     (while (and (progn
10979                   (when (> (gnus-summary-article-score) score)
10980                     (gnus-summary-mark-article nil mark))
10981                   t)
10982                 (gnus-summary-find-next)))))
10983
10984 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10985 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10986 (defun gnus-summary-limit-include-expunged (&optional no-error)
10987   "Display all the hidden articles that were expunged for low scores."
10988   (interactive)
10989   (let ((inhibit-read-only t))
10990     (let ((scored gnus-newsgroup-scored)
10991           headers h)
10992       (while scored
10993         (unless (gnus-summary-article-header (caar scored))
10994           (and (setq h (gnus-number-to-header (caar scored)))
10995                (< (cdar scored) gnus-summary-expunge-below)
10996                (push h headers)))
10997         (setq scored (cdr scored)))
10998       (if (not headers)
10999           (when (not no-error)
11000             (error "No expunged articles hidden"))
11001         (goto-char (point-min))
11002         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11003         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11004         (dolist (x headers)
11005           (push (mail-header-number x) gnus-newsgroup-limit))
11006         (gnus-summary-prepare-unthreaded (nreverse headers))
11007         (goto-char (point-min))
11008         (gnus-summary-position-point)
11009         t))))
11010
11011 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11012   "Mark all unread articles in this newsgroup as read.
11013 If prefix argument ALL is non-nil, ticked and dormant articles will
11014 also be marked as read.
11015 If QUIETLY is non-nil, no questions will be asked.
11016
11017 If TO-HERE is non-nil, it should be a point in the buffer.  All
11018 articles before (after, if REVERSE is set) this point will be marked
11019 as read.
11020
11021 Note that this function will only catch up the unread article
11022 in the current summary buffer limitation.
11023
11024 The number of articles marked as read is returned."
11025   (interactive "P")
11026   (prog1
11027       (save-excursion
11028         (when (or quietly
11029                   (not gnus-interactive-catchup) ;Without confirmation?
11030                   gnus-expert-user
11031                   (gnus-y-or-n-p
11032                    (if all
11033                        "Mark absolutely all articles as read? "
11034                      "Mark all unread articles as read? ")))
11035           (if (and not-mark
11036                    (not gnus-newsgroup-adaptive)
11037                    (not gnus-newsgroup-auto-expire)
11038                    (not gnus-suppress-duplicates)
11039                    (or (not gnus-use-cache)
11040                        (eq gnus-use-cache 'passive)))
11041               (progn
11042                 (when all
11043                   (setq gnus-newsgroup-marked nil
11044                         gnus-newsgroup-spam-marked nil
11045                         gnus-newsgroup-dormant nil))
11046                 (setq gnus-newsgroup-unreads
11047                       (gnus-sorted-nunion
11048                        (gnus-sorted-intersection gnus-newsgroup-unreads
11049                                                  gnus-newsgroup-downloadable)
11050                        (gnus-sorted-difference gnus-newsgroup-unfetched
11051                                                gnus-newsgroup-cached))))
11052             ;; We actually mark all articles as canceled, which we
11053             ;; have to do when using auto-expiry or adaptive scoring.
11054             (gnus-summary-show-all-threads)
11055             (if (and to-here reverse)
11056                 (progn
11057                   (goto-char to-here)
11058                   (gnus-summary-mark-current-read-and-unread-as-read
11059                    gnus-catchup-mark)
11060                   (while (gnus-summary-find-next (not all))
11061                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11062               (when (gnus-summary-first-subject (not all))
11063                 (while (and
11064                         (if to-here (< (point) to-here) t)
11065                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11066                         (gnus-summary-find-next (not all))))))
11067             (gnus-set-mode-line 'summary))
11068           t))
11069     (gnus-summary-position-point)))
11070
11071 (defun gnus-summary-catchup-to-here (&optional all)
11072   "Mark all unticked articles before the current one as read.
11073 If ALL is non-nil, also mark ticked and dormant articles as read."
11074   (interactive "P")
11075   (save-excursion
11076     (gnus-save-hidden-threads
11077       (let ((beg (point)))
11078         ;; We check that there are unread articles.
11079         (when (or all (gnus-summary-find-prev))
11080           (gnus-summary-catchup all t beg)))))
11081   (gnus-summary-position-point))
11082
11083 (defun gnus-summary-catchup-from-here (&optional all)
11084   "Mark all unticked articles after (and including) the current one as read.
11085 If ALL is non-nil, also mark ticked and dormant articles as read."
11086   (interactive "P")
11087   (save-excursion
11088     (gnus-save-hidden-threads
11089       (let ((beg (point)))
11090         ;; We check that there are unread articles.
11091         (when (or all (gnus-summary-find-next))
11092           (gnus-summary-catchup all t beg nil t)))))
11093   (gnus-summary-position-point))
11094
11095 (defun gnus-summary-catchup-all (&optional quietly)
11096   "Mark all articles in this newsgroup as read.
11097 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11098 instead, which marks only unread articles as read."
11099   (interactive "P")
11100   (gnus-summary-catchup t quietly))
11101
11102 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11103   "Mark all unread articles in this group as read, then exit.
11104 If prefix argument ALL is non-nil, all articles are marked as read.
11105 If QUIETLY is non-nil, no questions will be asked."
11106   (interactive "P")
11107   (when (gnus-summary-catchup all quietly nil 'fast)
11108     ;; Select next newsgroup or exit.
11109     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11110              (eq gnus-auto-select-next 'quietly))
11111         (gnus-summary-next-group nil)
11112       (gnus-summary-exit))))
11113
11114 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11115   "Mark all articles in this newsgroup as read, and then exit.
11116 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11117 instead, which marks only unread articles as read."
11118   (interactive "P")
11119   (gnus-summary-catchup-and-exit t quietly))
11120
11121 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11122   "Mark all articles in this group as read and select the next group.
11123 If given a prefix, mark all articles, unread as well as ticked, as
11124 read."
11125   (interactive "P")
11126   (save-excursion
11127     (gnus-summary-catchup all))
11128   (gnus-summary-next-group))
11129
11130 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11131   "Mark all articles in this group as read and select the previous group.
11132 If given a prefix, mark all articles, unread as well as ticked, as
11133 read."
11134   (interactive "P")
11135   (save-excursion
11136     (gnus-summary-catchup all))
11137   (gnus-summary-next-group nil nil t))
11138
11139 ;;;
11140 ;;; with article
11141 ;;;
11142
11143 (defmacro gnus-with-article (article &rest forms)
11144   "Select ARTICLE and perform FORMS in the original article buffer.
11145 Then replace the article with the result."
11146   `(progn
11147      ;; We don't want the article to be marked as read.
11148      (let (gnus-mark-article-hook)
11149        (gnus-summary-select-article t t nil ,article))
11150      (set-buffer gnus-original-article-buffer)
11151      ,@forms
11152      (if (not (gnus-check-backend-function
11153                'request-replace-article (car gnus-article-current)))
11154          (gnus-message 5 "Read-only group; not replacing")
11155        (unless (gnus-request-replace-article
11156                 ,article (car gnus-article-current)
11157                 (current-buffer) t)
11158          (error "Couldn't replace article")))
11159      ;; The cache and backlog have to be flushed somewhat.
11160      (when gnus-keep-backlog
11161        (gnus-backlog-remove-article
11162         (car gnus-article-current) (cdr gnus-article-current)))
11163      (when gnus-use-cache
11164        (gnus-cache-update-article
11165         (car gnus-article-current) (cdr gnus-article-current)))))
11166
11167 (put 'gnus-with-article 'lisp-indent-function 1)
11168 (put 'gnus-with-article 'edebug-form-spec '(form body))
11169
11170 ;; Thread-based commands.
11171
11172 (defun gnus-summary-articles-in-thread (&optional article)
11173   "Return a list of all articles in the current thread.
11174 If ARTICLE is non-nil, return all articles in the thread that starts
11175 with that article."
11176   (let* ((article (or article (gnus-summary-article-number)))
11177          (data (gnus-data-find-list article))
11178          (top-level (gnus-data-level (car data)))
11179          (top-subject
11180           (cond ((null gnus-thread-operation-ignore-subject)
11181                  (gnus-simplify-subject-re
11182                   (mail-header-subject (gnus-data-header (car data)))))
11183                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11184                  (gnus-simplify-subject-fuzzy
11185                   (mail-header-subject (gnus-data-header (car data)))))
11186                 (t nil)))
11187          (end-point (save-excursion
11188                       (if (gnus-summary-go-to-next-thread)
11189                           (point) (point-max))))
11190          articles)
11191     (while (and data
11192                 (< (gnus-data-pos (car data)) end-point))
11193       (when (or (not top-subject)
11194                 (string= top-subject
11195                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11196                              (gnus-simplify-subject-fuzzy
11197                               (mail-header-subject
11198                                (gnus-data-header (car data))))
11199                            (gnus-simplify-subject-re
11200                             (mail-header-subject
11201                              (gnus-data-header (car data)))))))
11202         (push (gnus-data-number (car data)) articles))
11203       (unless (and (setq data (cdr data))
11204                    (> (gnus-data-level (car data)) top-level))
11205         (setq data nil)))
11206     ;; Return the list of articles.
11207     (nreverse articles)))
11208
11209 (defun gnus-summary-rethread-current ()
11210   "Rethread the thread the current article is part of."
11211   (interactive)
11212   (let* ((gnus-show-threads t)
11213          (article (gnus-summary-article-number))
11214          (id (mail-header-id (gnus-summary-article-header)))
11215          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11216     (unless id
11217       (error "No article on the current line"))
11218     (gnus-rebuild-thread id)
11219     (gnus-summary-goto-subject article)))
11220
11221 (defun gnus-summary-reparent-thread ()
11222   "Make the current article child of the marked (or previous) article.
11223
11224 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11225 is non-nil or the Subject: of both articles are the same."
11226   (interactive)
11227   (unless (not (gnus-group-read-only-p))
11228     (error "The current newsgroup does not support article editing"))
11229   (unless (<= (length gnus-newsgroup-processable) 1)
11230     (error "No more than one article may be marked"))
11231   (let ((child (gnus-summary-article-number))
11232         ;; First grab the marked article, otherwise one line up.
11233         (parent (if (not (null gnus-newsgroup-processable))
11234                     (car gnus-newsgroup-processable)
11235                   (save-excursion
11236                     (if (eq (forward-line -1) 0)
11237                         (gnus-summary-article-number)
11238                       (error "Beginning of summary buffer"))))))
11239     (gnus-summary-reparent-children parent (list child))))
11240
11241 (defun gnus-summary-reparent-children (parent children)
11242   "Make PARENT the parent of CHILDREN.
11243 When called interactively, PARENT is the current article and CHILDREN
11244 are the process-marked articles."
11245   (interactive
11246    (list (gnus-summary-article-number)
11247          (gnus-summary-work-articles nil)))
11248   (dolist (child children)
11249     (save-window-excursion
11250       (let ((gnus-article-buffer " *reparent*"))
11251         (unless (not (eq parent child))
11252           (error "An article may not be self-referential"))
11253         (let ((message-id (mail-header-id
11254                            (gnus-summary-article-header parent))))
11255           (unless (and message-id (not (equal message-id "")))
11256             (error "No message-id in desired parent"))
11257           (gnus-with-article child
11258             (save-restriction
11259               (goto-char (point-min))
11260               (message-narrow-to-head)
11261               (if (re-search-forward "^References: " nil t)
11262                   (progn
11263                     (re-search-forward "^[^ \t]" nil t)
11264                     (forward-line -1)
11265                     (end-of-line)
11266                     (insert " " message-id))
11267                 (insert "References: " message-id "\n"))))
11268           (set-buffer gnus-summary-buffer)
11269           (gnus-summary-unmark-all-processable)
11270           (gnus-summary-update-article child)
11271           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11272             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11273           (gnus-summary-rethread-current)
11274           (gnus-message 3 "Article %d is now the child of article %d"
11275                         child parent))))))
11276
11277 (defun gnus-summary-toggle-threads (&optional arg)
11278   "Toggle showing conversation threads.
11279 If ARG is positive number, turn showing conversation threads on."
11280   (interactive "P")
11281   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11282     (setq gnus-show-threads
11283           (if (null arg) (not gnus-show-threads)
11284             (> (prefix-numeric-value arg) 0)))
11285     (gnus-summary-prepare)
11286     (gnus-summary-goto-subject current)
11287     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11288     (gnus-summary-position-point)))
11289
11290 (eval-and-compile
11291   (if (fboundp 'remove-overlays)
11292       (defalias 'gnus-remove-overlays 'remove-overlays)
11293     (defun gnus-remove-overlays (beg end name val)
11294       "Clear BEG and END of overlays whose property NAME has value VAL.
11295 For compatibility with Emacs 21 and XEmacs."
11296       (dolist (ov (gnus-overlays-in beg end))
11297         (when (eq (gnus-overlay-get ov name) val)
11298           (gnus-delete-overlay ov))))))
11299
11300 (defun gnus-summary-show-all-threads ()
11301   "Show all threads."
11302   (interactive)
11303   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11304   (gnus-summary-position-point))
11305
11306 (defun gnus-summary-show-thread ()
11307   "Show thread subtrees.
11308 Returns nil if no thread was there to be shown."
11309   (interactive)
11310   (let* ((orig (point))
11311          (end (point-at-eol))
11312          ;; Leave point at bol
11313          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11314          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11315                 (if (fboundp 'next-single-char-property-change)
11316                     (or (next-single-char-property-change end 'invisible)
11317                         (point-max))
11318                   (while (progn
11319                            (end-of-line 2)
11320                            (and (not (eobp))
11321                                 (eq (get-char-property (point) 'invisible)
11322                                     'gnus-sum))))
11323                   (point)))))
11324     (when eoi
11325       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11326       (goto-char orig)
11327       (gnus-summary-position-point)
11328       eoi)))
11329
11330 (defun gnus-summary-maybe-hide-threads ()
11331   "If requested, hide the threads that should be hidden."
11332   (when (and gnus-show-threads
11333              gnus-thread-hide-subtree)
11334     (gnus-summary-hide-all-threads
11335      (if (or (consp gnus-thread-hide-subtree)
11336              (functionp gnus-thread-hide-subtree))
11337          (gnus-make-predicate gnus-thread-hide-subtree)
11338        nil))))
11339
11340 ;;; Hiding predicates.
11341
11342 (defun gnus-article-unread-p (header)
11343   (memq (mail-header-number header) gnus-newsgroup-unreads))
11344
11345 (defun gnus-article-unseen-p (header)
11346   (memq (mail-header-number header) gnus-newsgroup-unseen))
11347
11348 (defun gnus-map-articles (predicate articles)
11349   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11350   (apply 'gnus-or (mapcar predicate
11351                           (mapcar (lambda (number)
11352                                     (gnus-summary-article-header number))
11353                                   articles))))
11354
11355 (defun gnus-summary-hide-all-threads (&optional predicate)
11356   "Hide all thread subtrees.
11357 If PREDICATE is supplied, threads that satisfy this predicate
11358 will not be hidden."
11359   (interactive)
11360   (save-excursion
11361     (goto-char (point-min))
11362     (let ((end nil))
11363       (while (not end)
11364         (when (or (not predicate)
11365                   (gnus-map-articles
11366                    predicate (gnus-summary-article-children)))
11367             (gnus-summary-hide-thread))
11368         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11369   (gnus-summary-position-point))
11370
11371 (defun gnus-summary-hide-thread ()
11372   "Hide thread subtrees.
11373 If PREDICATE is supplied, threads that satisfy this predicate
11374 will not be hidden.
11375 Returns nil if no threads were there to be hidden."
11376   (interactive)
11377   (let ((start (point))
11378         (starteol (line-end-position))
11379         (article (gnus-summary-article-number)))
11380     (goto-char start)
11381     ;; Go forward until either the buffer ends or the subthread ends.
11382     (when (and (not (eobp))
11383                (or (zerop (gnus-summary-next-thread 1 t))
11384                    (goto-char (point-max))))
11385       (if (and (> (point) start)
11386                ;; FIXME: this should actually search for a non-invisible \n.
11387                (search-backward "\n" start t))
11388           (progn
11389             (when (> (point) starteol)
11390               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11391               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11392                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11393                 (gnus-overlay-put ol 'evaporate t)))
11394             (gnus-summary-goto-subject article))
11395         (goto-char start)
11396         nil))))
11397
11398 (defun gnus-summary-go-to-next-thread (&optional previous)
11399   "Go to the same level (or less) next thread.
11400 If PREVIOUS is non-nil, go to previous thread instead.
11401 Return the article number moved to, or nil if moving was impossible."
11402   (let ((level (gnus-summary-thread-level))
11403         (way (if previous -1 1))
11404         (beg (point)))
11405     (forward-line way)
11406     (while (and (not (eobp))
11407                 (< level (gnus-summary-thread-level)))
11408       (forward-line way))
11409     (if (eobp)
11410         (progn
11411           (goto-char beg)
11412           nil)
11413       (setq beg (point))
11414       (prog1
11415           (gnus-summary-article-number)
11416         (goto-char beg)))))
11417
11418 (defun gnus-summary-next-thread (n &optional silent)
11419   "Go to the same level next N'th thread.
11420 If N is negative, search backward instead.
11421 Returns the difference between N and the number of skips actually
11422 done.
11423
11424 If SILENT, don't output messages."
11425   (interactive "p")
11426   (let ((backward (< n 0))
11427         (n (abs n)))
11428     (while (and (> n 0)
11429                 (gnus-summary-go-to-next-thread backward))
11430       (decf n))
11431     (unless silent
11432       (gnus-summary-position-point))
11433     (when (and (not silent) (/= 0 n))
11434       (gnus-message 7 "No more threads"))
11435     n))
11436
11437 (defun gnus-summary-prev-thread (n)
11438   "Go to the same level previous N'th thread.
11439 Returns the difference between N and the number of skips actually
11440 done."
11441   (interactive "p")
11442   (gnus-summary-next-thread (- n)))
11443
11444 (defun gnus-summary-go-down-thread ()
11445   "Go down one level in the current thread."
11446   (let ((children (gnus-summary-article-children)))
11447     (when children
11448       (gnus-summary-goto-subject (car children)))))
11449
11450 (defun gnus-summary-go-up-thread ()
11451   "Go up one level in the current thread."
11452   (let ((parent (gnus-summary-article-parent)))
11453     (when parent
11454       (gnus-summary-goto-subject parent))))
11455
11456 (defun gnus-summary-down-thread (n)
11457   "Go down thread N steps.
11458 If N is negative, go up instead.
11459 Returns the difference between N and how many steps down that were
11460 taken."
11461   (interactive "p")
11462   (let ((up (< n 0))
11463         (n (abs n)))
11464     (while (and (> n 0)
11465                 (if up (gnus-summary-go-up-thread)
11466                   (gnus-summary-go-down-thread)))
11467       (setq n (1- n)))
11468     (gnus-summary-position-point)
11469     (when (/= 0 n)
11470       (gnus-message 7 "Can't go further"))
11471     n))
11472
11473 (defun gnus-summary-up-thread (n)
11474   "Go up thread N steps.
11475 If N is negative, go down instead.
11476 Returns the difference between N and how many steps down that were
11477 taken."
11478   (interactive "p")
11479   (gnus-summary-down-thread (- n)))
11480
11481 (defun gnus-summary-top-thread ()
11482   "Go to the top of the thread."
11483   (interactive)
11484   (while (gnus-summary-go-up-thread))
11485   (gnus-summary-article-number))
11486
11487 (defun gnus-summary-expire-thread ()
11488   "Mark articles under current thread as expired."
11489   (interactive)
11490   (gnus-summary-kill-thread 0))
11491
11492 (defun gnus-summary-kill-thread (&optional unmark)
11493   "Mark articles under current thread as read.
11494 If the prefix argument is positive, remove any kinds of marks.
11495 If the prefix argument is zero, mark thread as expired.
11496 If the prefix argument is negative, tick articles instead."
11497   (interactive "P")
11498   (when unmark
11499     (setq unmark (prefix-numeric-value unmark)))
11500   (let ((articles (gnus-summary-articles-in-thread))
11501         (hide (or (null unmark) (= unmark 0))))
11502     (save-excursion
11503       ;; Expand the thread.
11504       (gnus-summary-show-thread)
11505       ;; Mark all the articles.
11506       (while articles
11507         (gnus-summary-goto-subject (car articles))
11508         (cond ((null unmark)
11509                (gnus-summary-mark-article-as-read gnus-killed-mark))
11510               ((> unmark 0)
11511                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11512               ((= unmark 0)
11513                (gnus-summary-mark-article nil gnus-expirable-mark))
11514               (t
11515                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11516         (setq articles (cdr articles))))
11517     ;; Hide killed subtrees when hide is true.
11518     (and hide
11519          gnus-thread-hide-killed
11520          (gnus-summary-hide-thread))
11521     ;; If hide is t, go to next unread subject.
11522     (when hide
11523       ;; Go to next unread subject.
11524       (gnus-summary-next-subject 1 t)))
11525   (gnus-set-mode-line 'summary))
11526
11527 ;; Summary sorting commands
11528
11529 (defun gnus-summary-sort-by-number (&optional reverse)
11530   "Sort the summary buffer by article number.
11531 Argument REVERSE means reverse order."
11532   (interactive "P")
11533   (gnus-summary-sort 'number reverse))
11534
11535 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11536   "Sort the summary buffer by most recent article number.
11537 Argument REVERSE means reverse order."
11538   (interactive "P")
11539   (gnus-summary-sort 'most-recent-number reverse))
11540
11541 (defun gnus-summary-sort-by-random (&optional reverse)
11542   "Randomize the order in the summary buffer.
11543 Argument REVERSE means to randomize in reverse order."
11544   (interactive "P")
11545   (gnus-summary-sort 'random reverse))
11546
11547 (defun gnus-summary-sort-by-author (&optional reverse)
11548   "Sort the summary buffer by author name alphabetically.
11549 If `case-fold-search' is non-nil, case of letters is ignored.
11550 Argument REVERSE means reverse order."
11551   (interactive "P")
11552   (gnus-summary-sort 'author reverse))
11553
11554 (defun gnus-summary-sort-by-recipient (&optional reverse)
11555   "Sort the summary buffer by recipient name alphabetically.
11556 If `case-fold-search' is non-nil, case of letters is ignored.
11557 Argument REVERSE means reverse order."
11558   (interactive "P")
11559   (gnus-summary-sort 'recipient reverse))
11560
11561 (defun gnus-summary-sort-by-subject (&optional reverse)
11562   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11563 If `case-fold-search' is non-nil, case of letters is ignored.
11564 Argument REVERSE means reverse order."
11565   (interactive "P")
11566   (gnus-summary-sort 'subject reverse))
11567
11568 (defun gnus-summary-sort-by-date (&optional reverse)
11569   "Sort the summary buffer by date.
11570 Argument REVERSE means reverse order."
11571   (interactive "P")
11572   (gnus-summary-sort 'date reverse))
11573
11574 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11575   "Sort the summary buffer by most recent date.
11576 Argument REVERSE means reverse order."
11577   (interactive "P")
11578   (gnus-summary-sort 'most-recent-date reverse))
11579
11580 (defun gnus-summary-sort-by-score (&optional reverse)
11581   "Sort the summary buffer by score.
11582 Argument REVERSE means reverse order."
11583   (interactive "P")
11584   (gnus-summary-sort 'score reverse))
11585
11586 (defun gnus-summary-sort-by-lines (&optional reverse)
11587   "Sort the summary buffer by the number of lines.
11588 Argument REVERSE means reverse order."
11589   (interactive "P")
11590   (gnus-summary-sort 'lines reverse))
11591
11592 (defun gnus-summary-sort-by-chars (&optional reverse)
11593   "Sort the summary buffer by article length.
11594 Argument REVERSE means reverse order."
11595   (interactive "P")
11596   (gnus-summary-sort 'chars reverse))
11597
11598 (defun gnus-summary-sort-by-original (&optional reverse)
11599   "Sort the summary buffer using the default sorting method.
11600 Argument REVERSE means reverse order."
11601   (interactive "P")
11602   (let* ((inhibit-read-only t)
11603          (gnus-summary-prepare-hook nil))
11604     ;; We do the sorting by regenerating the threads.
11605     (gnus-summary-prepare)
11606     ;; Hide subthreads if needed.
11607     (gnus-summary-maybe-hide-threads)))
11608
11609 (defun gnus-summary-sort (predicate reverse)
11610   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11611   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11612          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11613          (gnus-thread-sort-functions
11614           (if (not reverse)
11615               thread
11616             `(lambda (t1 t2)
11617                (,thread t2 t1))))
11618          (gnus-sort-gathered-threads-function
11619           gnus-thread-sort-functions)
11620          (gnus-article-sort-functions
11621           (if (not reverse)
11622               article
11623             `(lambda (t1 t2)
11624                (,article t2 t1))))
11625          (inhibit-read-only t)
11626          (gnus-summary-prepare-hook nil))
11627     ;; We do the sorting by regenerating the threads.
11628     (gnus-summary-prepare)
11629     ;; Hide subthreads if needed.
11630     (gnus-summary-maybe-hide-threads)))
11631
11632 ;; Summary saving commands.
11633
11634 (defun gnus-summary-save-article (&optional n not-saved)
11635   "Save the current article using the default saver function.
11636 If N is a positive number, save the N next articles.
11637 If N is a negative number, save the N previous articles.
11638 If N is nil and any articles have been marked with the process mark,
11639 save those articles instead.
11640 The variable `gnus-default-article-saver' specifies the saver function.
11641
11642 If the optional second argument NOT-SAVED is non-nil, articles saved
11643 will not be marked as saved."
11644   (interactive "P")
11645   (require 'gnus-art)
11646   (let* ((articles (gnus-summary-work-articles n))
11647          (save-buffer (save-excursion
11648                         (nnheader-set-temp-buffer " *Gnus Save*")))
11649          (num (length articles))
11650          ;; Whether to save decoded articles or raw articles.
11651          (decode (when gnus-article-save-coding-system
11652                    (get gnus-default-article-saver :decode)))
11653          ;; When saving many articles in a single file, use the other
11654          ;; function to save articles other than the first one.
11655          (saver2 (get gnus-default-article-saver :function))
11656          (gnus-prompt-before-saving (if saver2
11657                                         t
11658                                       gnus-prompt-before-saving))
11659          (gnus-default-article-saver gnus-default-article-saver)
11660          header file)
11661     (dolist (article articles)
11662       (setq header (gnus-summary-article-header article))
11663       (if (not (vectorp header))
11664           ;; This is a pseudo-article.
11665           (if (assq 'name header)
11666               (gnus-copy-file (cdr (assq 'name header)))
11667             (gnus-message 1 "Article %d is unsaveable" article))
11668         ;; This is a real article.
11669         (save-window-excursion
11670           (gnus-summary-select-article decode decode nil article)
11671           (gnus-summary-goto-subject article))
11672         (with-current-buffer save-buffer
11673           (erase-buffer)
11674           (insert-buffer-substring (if decode
11675                                        gnus-article-buffer
11676                                      gnus-original-article-buffer)))
11677         (setq file (gnus-article-save save-buffer file num))
11678         (gnus-summary-remove-process-mark article)
11679         (unless not-saved
11680           (gnus-summary-set-saved-mark article)))
11681       (when saver2
11682         (setq gnus-default-article-saver saver2
11683               saver2 nil)))
11684     (gnus-kill-buffer save-buffer)
11685     (gnus-summary-position-point)
11686     (gnus-set-mode-line 'summary)
11687     n))
11688
11689 (defun gnus-summary-pipe-output (&optional n sym)
11690   "Pipe the current article to a subprocess.
11691 If N is a positive number, pipe the N next articles.
11692 If N is a negative number, pipe the N previous articles.
11693 If N is nil and any articles have been marked with the process mark,
11694 pipe those articles instead.
11695 The default command to which articles are piped is specified by the
11696 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11697 will be prompted for the command.
11698
11699 The properties `:decode' and `:headers' that are put to the function
11700 symbol `gnus-summary-save-in-pipe' control whether this function
11701 decodes articles and what headers to keep (see the doc string for the
11702 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11703 is neither omitted nor the symbol `r', force including all headers
11704 regardless of the `:headers' property.  If it is the symbol `r',
11705 articles that are not decoded and include all headers will be piped
11706 no matter what the properties `:decode' and `:headers' are."
11707   (interactive (gnus-interactive "P\ny"))
11708   (require 'gnus-art)
11709   (let* ((articles (gnus-summary-work-articles n))
11710          (result-buffer "*Shell Command Output*")
11711          (all-headers (not (memq sym '(nil r))))
11712          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11713          (raw (eq sym 'r))
11714          (headers (get 'gnus-summary-save-in-pipe :headers))
11715          command result)
11716     (unless (numberp (car articles))
11717       (error "No article to pipe"))
11718     (setq command (gnus-read-shell-command
11719                    (concat "Shell command on "
11720                            (if (cdr articles)
11721                                (format "these %d articles" (length articles))
11722                              "this article")
11723                            ": ")
11724                    gnus-summary-pipe-output-default-command))
11725     (when (string-equal command "")
11726       (error "A command is required"))
11727     (when all-headers
11728       (put 'gnus-summary-save-in-pipe :headers nil))
11729     (unwind-protect
11730         (while articles
11731           (gnus-summary-goto-subject (pop articles))
11732           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11733           (when (and (get-buffer result-buffer)
11734                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11735             (setq result (concat result (with-current-buffer result-buffer
11736                                           (buffer-string))))))
11737       (put 'gnus-summary-save-in-pipe :headers headers))
11738     (unless (zerop (length result))
11739       (if (with-current-buffer (get-buffer-create result-buffer)
11740             (erase-buffer)
11741             (insert result)
11742             (prog1
11743                 (and (= (count-lines (point-min) (point)) 1)
11744                      (progn
11745                        (end-of-line 0)
11746                        (<= (current-column)
11747                            (window-width (minibuffer-window)))))
11748               (goto-char (point-min))))
11749           (message "%s" (substring result 0 -1))
11750         (message nil)
11751         (gnus-configure-windows 'pipe)))))
11752
11753 (defun gnus-summary-save-article-mail (&optional arg)
11754   "Append the current article to a Unix mail box file.
11755 If N is a positive number, save the N next articles.
11756 If N is a negative number, save the N previous articles.
11757 If N is nil and any articles have been marked with the process mark,
11758 save those articles instead."
11759   (interactive "P")
11760   (require 'gnus-art)
11761   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11762     (gnus-summary-save-article arg)))
11763
11764 (defun gnus-summary-save-article-rmail (&optional arg)
11765   "Append the current article to an rmail file.
11766 If N is a positive number, save the N next articles.
11767 If N is a negative number, save the N previous articles.
11768 If N is nil and any articles have been marked with the process mark,
11769 save those articles instead."
11770   (interactive "P")
11771   (require 'gnus-art)
11772   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11773     (gnus-summary-save-article arg)))
11774
11775 (defun gnus-summary-save-article-file (&optional arg)
11776   "Append the current article to a file.
11777 If N is a positive number, save the N next articles.
11778 If N is a negative number, save the N previous articles.
11779 If N is nil and any articles have been marked with the process mark,
11780 save those articles instead."
11781   (interactive "P")
11782   (require 'gnus-art)
11783   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11784     (gnus-summary-save-article arg)))
11785
11786 (defun gnus-summary-write-article-file (&optional arg)
11787   "Write the current article to a file, deleting the previous file.
11788 If N is a positive number, save the N next articles.
11789 If N is a negative number, save the N previous articles.
11790 If N is nil and any articles have been marked with the process mark,
11791 save those articles instead."
11792   (interactive "P")
11793   (require 'gnus-art)
11794   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11795     (gnus-summary-save-article arg)))
11796
11797 (defun gnus-summary-save-article-body-file (&optional arg)
11798   "Append the current article body to a file.
11799 If N is a positive number, save the N next articles.
11800 If N is a negative number, save the N previous articles.
11801 If N is nil and any articles have been marked with the process mark,
11802 save those articles instead."
11803   (interactive "P")
11804   (require 'gnus-art)
11805   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11806     (gnus-summary-save-article arg)))
11807
11808 (defun gnus-summary-write-article-body-file (&optional arg)
11809   "Write the current article body to a file, deleting the previous file.
11810 If N is a positive number, save the N next articles.
11811 If N is a negative number, save the N previous articles.
11812 If N is nil and any articles have been marked with the process mark,
11813 save those articles instead."
11814   (interactive "P")
11815   (require 'gnus-art)
11816   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11817     (gnus-summary-save-article arg)))
11818
11819 (defun gnus-summary-muttprint (&optional arg)
11820   "Print the current article using Muttprint.
11821 If N is a positive number, save the N next articles.
11822 If N is a negative number, save the N previous articles.
11823 If N is nil and any articles have been marked with the process mark,
11824 save those articles instead."
11825   (interactive "P")
11826   (require 'gnus-art)
11827   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11828     (gnus-summary-save-article arg t)))
11829
11830 (defun gnus-summary-pipe-message (program)
11831   "Pipe the current article through PROGRAM."
11832   (interactive "sProgram: ")
11833   (gnus-summary-select-article)
11834   (let ((mail-header-separator ""))
11835     (gnus-eval-in-buffer-window gnus-article-buffer
11836       (save-restriction
11837         (widen)
11838         (let ((start (window-start))
11839               (inhibit-read-only t))
11840           (message-pipe-buffer-body program)
11841           (set-window-start (get-buffer-window (current-buffer)) start))))))
11842
11843 (defun gnus-get-split-value (methods)
11844   "Return a value based on the split METHODS."
11845   (let (split-name method result match)
11846     (when methods
11847       (with-current-buffer gnus-original-article-buffer
11848         (save-restriction
11849           (nnheader-narrow-to-headers)
11850           (while (and methods (not split-name))
11851             (goto-char (point-min))
11852             (setq method (pop methods))
11853             (setq match (car method))
11854             (when (cond
11855                    ((stringp match)
11856                     ;; Regular expression.
11857                     (ignore-errors
11858                       (re-search-forward match nil t)))
11859                    ((functionp match)
11860                     ;; Function.
11861                     (save-restriction
11862                       (widen)
11863                       (setq result (funcall match gnus-newsgroup-name))))
11864                    ((consp match)
11865                     ;; Form.
11866                     (save-restriction
11867                       (widen)
11868                       (setq result (eval match)))))
11869               (setq split-name (cdr method))
11870               (cond ((stringp result)
11871                      (push (expand-file-name
11872                             result gnus-article-save-directory)
11873                            split-name))
11874                     ((consp result)
11875                      (setq split-name (append result split-name)))))))))
11876     (nreverse split-name)))
11877
11878 (defun gnus-valid-move-group-p (group)
11879   (and (boundp group)
11880        (symbol-name group)
11881        (symbol-value group)
11882        (gnus-get-function (gnus-find-method-for-group
11883                            (symbol-name group)) 'request-accept-article t)))
11884
11885 (defun gnus-read-move-group-name (prompt default articles prefix)
11886   "Read a group name."
11887   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11888          (minibuffer-confirm-incomplete nil) ; XEmacs
11889          (prom
11890           (format "%s %s to"
11891                   prompt
11892                   (if (> (length articles) 1)
11893                       (format "these %d articles" (length articles))
11894                     "this article")))
11895          (to-newsgroup
11896           (let (active group)
11897             (when (or (null split-name) (= 1 (length split-name)))
11898               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11899               (mapatoms (lambda (symbol)
11900                           (setq group (symbol-name symbol))
11901                           (when (string-match "[^\000-\177]" group)
11902                             (setq group (gnus-group-decoded-name group)))
11903                           (set (intern group active) group))
11904                         gnus-active-hashtb))
11905             (cond
11906              ((null split-name)
11907               (gnus-completing-read-with-default
11908                default prom active 'gnus-valid-move-group-p nil prefix
11909                'gnus-group-history))
11910              ((= 1 (length split-name))
11911               (gnus-completing-read-with-default
11912                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11913                'gnus-group-history))
11914              (t
11915               (gnus-completing-read-with-default
11916                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11917                'gnus-group-history)))))
11918          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11919          encoded)
11920     (when to-newsgroup
11921       (if (or (string= to-newsgroup "")
11922               (string= to-newsgroup prefix))
11923           (setq to-newsgroup default))
11924       (unless to-newsgroup
11925         (error "No group name entered"))
11926       (setq encoded (mm-encode-coding-string
11927                      to-newsgroup
11928                      (gnus-group-name-charset to-method to-newsgroup)))
11929       (or (gnus-active encoded)
11930           (gnus-activate-group encoded nil nil to-method)
11931           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11932                                      to-newsgroup))
11933               (or (and (gnus-request-create-group encoded to-method)
11934                        (gnus-activate-group encoded nil nil to-method)
11935                        (gnus-subscribe-group encoded))
11936                   (error "Couldn't create group %s" to-newsgroup)))
11937           (error "No such group: %s" to-newsgroup))
11938       encoded)))
11939
11940 (defvar gnus-summary-save-parts-counter)
11941 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11942
11943 (defun gnus-summary-save-parts (type dir n &optional reverse)
11944   "Save parts matching TYPE to DIR.
11945 If REVERSE, save parts that do not match TYPE."
11946   (interactive
11947    (list (read-string "Save parts of type: "
11948                       (or (car gnus-summary-save-parts-type-history)
11949                           gnus-summary-save-parts-default-mime)
11950                       'gnus-summary-save-parts-type-history)
11951          (setq gnus-summary-save-parts-last-directory
11952                (read-file-name "Save to directory: "
11953                                gnus-summary-save-parts-last-directory
11954                                nil t))
11955          current-prefix-arg))
11956   (gnus-summary-iterate n
11957     (let ((gnus-display-mime-function nil)
11958           gnus-article-prepare-hook
11959           gnus-article-decode-hook
11960           gnus-display-mime-function
11961           gnus-break-pages
11962           (gnus-inhibit-treatment t))
11963       (gnus-summary-select-article))
11964     (with-current-buffer gnus-article-buffer
11965       (let ((handles (or gnus-article-mime-handles
11966                          (mm-dissect-buffer nil gnus-article-loose-mime)
11967                          (and gnus-article-emulate-mime
11968                               (mm-uu-dissect))))
11969             (gnus-summary-save-parts-counter 1))
11970         (when handles
11971           (gnus-summary-save-parts-1 type dir handles reverse)
11972           (unless gnus-article-mime-handles ;; Don't destroy this case.
11973             (mm-destroy-parts handles)))))))
11974
11975 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11976   (if (stringp (car handle))
11977       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11978               (cdr handle))
11979     (when (if reverse
11980               (not (string-match type (mm-handle-media-type handle)))
11981             (string-match type (mm-handle-media-type handle)))
11982       (let ((file (expand-file-name
11983                    (gnus-map-function
11984                     mm-file-name-rewrite-functions
11985                     (file-name-nondirectory
11986                      (or
11987                       (mail-content-type-get
11988                        (mm-handle-disposition handle) 'filename)
11989                       (mail-content-type-get
11990                        (mm-handle-type handle) 'name)
11991                       (format "%s.%d.%d" gnus-newsgroup-name
11992                               (cdr gnus-article-current)
11993                               gnus-summary-save-parts-counter))))
11994                    dir)))
11995         (incf gnus-summary-save-parts-counter)
11996         (unless (file-exists-p file)
11997           (mm-save-part-to-file handle file))))))
11998
11999 ;; Summary extract commands
12000
12001 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12002   (let ((inhibit-read-only t)
12003         (article (gnus-summary-article-number))
12004         after-article b e)
12005     (unless (gnus-summary-goto-subject article)
12006       (error "No such article: %d" article))
12007     (gnus-summary-position-point)
12008     ;; If all commands are to be bunched up on one line, we collect
12009     ;; them here.
12010     (unless gnus-view-pseudos-separately
12011       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12012             files action)
12013         (while ps
12014           (setq action (cdr (assq 'action (car ps))))
12015           (setq files (list (cdr (assq 'name (car ps)))))
12016           (while (and ps (cdr ps)
12017                       (string= (or action "1")
12018                                (or (cdr (assq 'action (cadr ps))) "2")))
12019             (push (cdr (assq 'name (cadr ps))) files)
12020             (setcdr ps (cddr ps)))
12021           (when files
12022             (when (not (string-match "%s" action))
12023               (push " " files))
12024             (push " " files)
12025             (when (assq 'execute (car ps))
12026               (setcdr (assq 'execute (car ps))
12027                       (funcall (if (string-match "%s" action)
12028                                    'format 'concat)
12029                                action
12030                                (mapconcat
12031                                 (lambda (f)
12032                                   (if (equal f " ")
12033                                       f
12034                                     (shell-quote-argument f)))
12035                                 files " ")))))
12036           (setq ps (cdr ps)))))
12037     (if (and gnus-view-pseudos (not not-view))
12038         (while pslist
12039           (when (assq 'execute (car pslist))
12040             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12041                                   (eq gnus-view-pseudos 'not-confirm)))
12042           (setq pslist (cdr pslist)))
12043       (save-excursion
12044         (while pslist
12045           (setq after-article (or (cdr (assq 'article (car pslist)))
12046                                   (gnus-summary-article-number)))
12047           (gnus-summary-goto-subject after-article)
12048           (forward-line 1)
12049           (setq b (point))
12050           (insert "    " (file-name-nondirectory
12051                           (cdr (assq 'name (car pslist))))
12052                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12053           (setq e (point))
12054           (forward-line -1)             ; back to `b'
12055           (gnus-add-text-properties
12056            b (1- e) (list 'gnus-number gnus-reffed-article-number
12057                           gnus-mouse-face-prop gnus-mouse-face))
12058           (gnus-data-enter
12059            after-article gnus-reffed-article-number
12060            gnus-unread-mark b (car pslist) 0 (- e b))
12061           (setq gnus-newsgroup-unreads
12062                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12063                                          gnus-reffed-article-number))
12064           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12065           (setq pslist (cdr pslist)))))))
12066
12067 (defun gnus-pseudos< (p1 p2)
12068   (let ((c1 (cdr (assq 'action p1)))
12069         (c2 (cdr (assq 'action p2))))
12070     (and c1 c2 (string< c1 c2))))
12071
12072 (defun gnus-request-pseudo-article (props)
12073   (cond ((assq 'execute props)
12074          (gnus-execute-command (cdr (assq 'execute props)))))
12075   (let ((gnus-current-article (gnus-summary-article-number)))
12076     (gnus-run-hooks 'gnus-mark-article-hook)))
12077
12078 (defun gnus-execute-command (command &optional automatic)
12079   (save-excursion
12080     (gnus-article-setup-buffer)
12081     (set-buffer gnus-article-buffer)
12082     (setq buffer-read-only nil)
12083     (let ((command (if automatic command
12084                      (read-string "Command: " (cons command 0)))))
12085       (erase-buffer)
12086       (insert "$ " command "\n\n")
12087       (if gnus-view-pseudo-asynchronously
12088           (start-process "gnus-execute" (current-buffer) shell-file-name
12089                          shell-command-switch command)
12090         (call-process shell-file-name nil t nil
12091                       shell-command-switch command)))))
12092
12093 ;; Summary kill commands.
12094
12095 (defun gnus-summary-edit-global-kill (article)
12096   "Edit the \"global\" kill file."
12097   (interactive (list (gnus-summary-article-number)))
12098   (gnus-group-edit-global-kill article))
12099
12100 (defun gnus-summary-edit-local-kill ()
12101   "Edit a local kill file applied to the current newsgroup."
12102   (interactive)
12103   (setq gnus-current-headers (gnus-summary-article-header))
12104   (gnus-group-edit-local-kill
12105    (gnus-summary-article-number) gnus-newsgroup-name))
12106
12107 ;;; Header reading.
12108
12109 (defun gnus-read-header (id &optional header)
12110   "Read the headers of article ID and enter them into the Gnus system."
12111   (let ((group gnus-newsgroup-name)
12112         (gnus-override-method
12113          (or
12114           gnus-override-method
12115           (and (gnus-news-group-p gnus-newsgroup-name)
12116                (car (gnus-refer-article-methods)))))
12117         where)
12118     ;; First we check to see whether the header in question is already
12119     ;; fetched.
12120     (if (stringp id)
12121         ;; This is a Message-ID.
12122         (setq header (or header (gnus-id-to-header id)))
12123       ;; This is an article number.
12124       (setq header (or header (gnus-summary-article-header id))))
12125     (if (and header
12126              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12127         ;; We have found the header.
12128         header
12129       ;; We have to really fetch the header to this article.
12130       (with-current-buffer nntp-server-buffer
12131         (when (setq where (gnus-request-head id group))
12132           (nnheader-fold-continuation-lines)
12133           (goto-char (point-max))
12134           (insert ".\n")
12135           (goto-char (point-min))
12136           (insert "211 ")
12137           (princ (cond
12138                   ((numberp id) id)
12139                   ((cdr where) (cdr where))
12140                   (header (mail-header-number header))
12141                   (t gnus-reffed-article-number))
12142                  (current-buffer))
12143           (insert " Article retrieved.\n"))
12144         (if (or (not where)
12145                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12146             ()                          ; Malformed head.
12147           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12148             (when (and (stringp id)
12149                        (or
12150                         (not (string= (gnus-group-real-name group)
12151                                       (car where)))
12152                         (not (gnus-server-equal gnus-override-method
12153                                                 (gnus-group-method group)))))
12154               ;; If we fetched by Message-ID and the article came from
12155               ;; a different group (or server), we fudge some bogus
12156               ;; article numbers for this article.
12157               (mail-header-set-number header gnus-reffed-article-number))
12158             (with-current-buffer gnus-summary-buffer
12159               (decf gnus-reffed-article-number)
12160               (gnus-remove-header (mail-header-number header))
12161               (push header gnus-newsgroup-headers)
12162               (setq gnus-current-headers header)
12163               (push (mail-header-number header) gnus-newsgroup-limit)))
12164           header)))))
12165
12166 (defun gnus-remove-header (number)
12167   "Remove header NUMBER from `gnus-newsgroup-headers'."
12168   (if (and gnus-newsgroup-headers
12169            (= number (mail-header-number (car gnus-newsgroup-headers))))
12170       (pop gnus-newsgroup-headers)
12171     (let ((headers gnus-newsgroup-headers))
12172       (while (and (cdr headers)
12173                   (not (= number (mail-header-number (cadr headers)))))
12174         (pop headers))
12175       (when (cdr headers)
12176         (setcdr headers (cddr headers))))))
12177
12178 ;;;
12179 ;;; summary highlights
12180 ;;;
12181
12182 (defun gnus-highlight-selected-summary ()
12183   "Highlight selected article in summary buffer."
12184   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12185   (when gnus-summary-selected-face
12186     (save-excursion
12187       (let* ((beg (point-at-bol))
12188              (end (point-at-eol))
12189              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12190              (from (if (get-text-property beg gnus-mouse-face-prop)
12191                        beg
12192                      (or (next-single-property-change
12193                           beg gnus-mouse-face-prop nil end)
12194                          beg)))
12195              (to
12196               (if (= from end)
12197                   (- from 2)
12198                 (or (next-single-property-change
12199                      from gnus-mouse-face-prop nil end)
12200                     end))))
12201         ;; If no mouse-face prop on line we will have to = from = end,
12202         ;; so we highlight the entire line instead.
12203         (when (= (+ to 2) from)
12204           (setq from beg)
12205           (setq to end))
12206         (if gnus-newsgroup-selected-overlay
12207             ;; Move old overlay.
12208             (gnus-move-overlay
12209              gnus-newsgroup-selected-overlay from to (current-buffer))
12210           ;; Create new overlay.
12211           (gnus-overlay-put
12212            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12213            'face gnus-summary-selected-face))))))
12214
12215 (defvar gnus-summary-highlight-line-cached nil)
12216 (defvar gnus-summary-highlight-line-trigger nil)
12217
12218 (defun gnus-summary-highlight-line-0 ()
12219   (if (and (eq gnus-summary-highlight-line-trigger
12220                gnus-summary-highlight)
12221            gnus-summary-highlight-line-cached)
12222       gnus-summary-highlight-line-cached
12223     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12224           gnus-summary-highlight-line-cached
12225           (let* ((cond (list 'cond))
12226                  (c cond)
12227                  (list gnus-summary-highlight))
12228             (while list
12229               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12230                               nil))
12231               (setq c (cdr c)
12232                     list (cdr list)))
12233             (gnus-byte-compile (list 'lambda nil cond))))))
12234
12235 (defun gnus-summary-highlight-line ()
12236   "Highlight current line according to `gnus-summary-highlight'."
12237   (let* ((beg (point-at-bol))
12238          (article (or (gnus-summary-article-number) gnus-current-article))
12239          (score (or (cdr (assq article
12240                                gnus-newsgroup-scored))
12241                     gnus-summary-default-score 0))
12242          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12243          (inhibit-read-only t)
12244          (default gnus-summary-default-score)
12245          (default-high gnus-summary-default-high-score)
12246          (default-low gnus-summary-default-low-score)
12247          (uncached (and gnus-summary-use-undownloaded-faces
12248                         (memq article gnus-newsgroup-undownloaded)
12249                         (not (memq article gnus-newsgroup-cached)))))
12250     (let ((face (funcall (gnus-summary-highlight-line-0))))
12251       (unless (eq face (get-text-property beg 'face))
12252         (gnus-put-text-property-excluding-characters-with-faces
12253          beg (point-at-eol) 'face
12254          (setq face (if (boundp face) (symbol-value face) face)))
12255         (when gnus-summary-highlight-line-function
12256           (funcall gnus-summary-highlight-line-function article face))))))
12257
12258 (defun gnus-update-read-articles (group unread &optional compute)
12259   "Update the list of read articles in GROUP.
12260 UNREAD is a sorted list."
12261   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12262         (info (gnus-get-info group))
12263         (prev 1)
12264         read)
12265     (if (or (not info) (not active))
12266         ;; There is no info on this group if it was, in fact,
12267         ;; killed.  Gnus stores no information on killed groups, so
12268         ;; there's nothing to be done.
12269         ;; One could store the information somewhere temporarily,
12270         ;; perhaps...  Hmmm...
12271         ()
12272       ;; Remove any negative articles numbers.
12273       (while (and unread (< (car unread) 0))
12274         (setq unread (cdr unread)))
12275       ;; Remove any expired article numbers
12276       (while (and unread (< (car unread) (car active)))
12277         (setq unread (cdr unread)))
12278       ;; Compute the ranges of read articles by looking at the list of
12279       ;; unread articles.
12280       (while unread
12281         (when (/= (car unread) prev)
12282           (push (if (= prev (1- (car unread))) prev
12283                   (cons prev (1- (car unread))))
12284                 read))
12285         (setq prev (1+ (car unread)))
12286         (setq unread (cdr unread)))
12287       (when (<= prev (cdr active))
12288         (push (cons prev (cdr active)) read))
12289       (setq read (if (> (length read) 1) (nreverse read) read))
12290       (if compute
12291           read
12292         (save-excursion
12293           (let (setmarkundo)
12294             ;; Propagate the read marks to the backend.
12295             (when (and gnus-propagate-marks
12296                        (gnus-check-backend-function 'request-set-mark group))
12297               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12298                     (add (gnus-remove-from-range read (gnus-info-read info))))
12299                 (when (or add del)
12300                   (unless (gnus-check-group group)
12301                     (error "Can't open server for %s" group))
12302                   (gnus-request-set-mark
12303                    group (delq nil (list (if add (list add 'add '(read)))
12304                                          (if del (list del 'del '(read))))))
12305                   (setq setmarkundo
12306                         `(gnus-request-set-mark
12307                           ,group
12308                           ',(delq nil (list
12309                                        (if del (list del 'add '(read)))
12310                                        (if add (list add 'del '(read))))))))))
12311             (set-buffer gnus-group-buffer)
12312             (gnus-undo-register
12313               `(progn
12314                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12315                  (gnus-info-set-read ',info ',(gnus-info-read info))
12316                  (gnus-get-unread-articles-in-group ',info
12317                                                     (gnus-active ,group))
12318                  (gnus-group-update-group ,group t)
12319                  ,setmarkundo))))
12320         ;; Enter this list into the group info.
12321         (gnus-info-set-read info read)
12322         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12323         (gnus-get-unread-articles-in-group info (gnus-active group))
12324         t))))
12325
12326 (defun gnus-offer-save-summaries ()
12327   "Offer to save all active summary buffers."
12328   (let (buffers)
12329     ;; Go through all buffers and find all summaries.
12330     (dolist (buffer (buffer-list))
12331       (when (and (setq buffer (buffer-name buffer))
12332                  (string-match "Summary" buffer)
12333                  (with-current-buffer buffer
12334                    ;; We check that this is, indeed, a summary buffer.
12335                    (and (eq major-mode 'gnus-summary-mode)
12336                         ;; Also make sure this isn't bogus.
12337                         gnus-newsgroup-prepared
12338                         ;; Also make sure that this isn't a
12339                         ;; dead summary buffer.
12340                         (not gnus-dead-summary-mode))))
12341         (push buffer buffers)))
12342     ;; Go through all these summary buffers and offer to save them.
12343     (when buffers
12344       (save-excursion
12345         (map-y-or-n-p
12346          "Update summary buffer %s? "
12347          (lambda (buf)
12348            (switch-to-buffer buf)
12349            (gnus-summary-exit))
12350          buffers)))))
12351
12352 (defun gnus-summary-setup-default-charset ()
12353   "Setup newsgroup default charset."
12354   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12355       (setq gnus-newsgroup-charset nil)
12356     (let* ((ignored-charsets
12357             (or gnus-newsgroup-ephemeral-ignored-charsets
12358                 (append
12359                  (and gnus-newsgroup-name
12360                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12361                  gnus-newsgroup-ignored-charsets))))
12362       (setq gnus-newsgroup-charset
12363             (or gnus-newsgroup-ephemeral-charset
12364                 (and gnus-newsgroup-name
12365                      (gnus-parameter-charset gnus-newsgroup-name))
12366                 gnus-default-charset))
12367       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12368            ignored-charsets))))
12369
12370 ;;;
12371 ;;; Mime Commands
12372 ;;;
12373
12374 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12375   "Display the current article buffer fully MIME-buttonized.
12376 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12377 treated as multipart/mixed."
12378   (interactive "P")
12379   (require 'gnus-art)
12380   (let ((gnus-unbuttonized-mime-types nil)
12381         (gnus-mime-display-multipart-as-mixed show-all-parts))
12382     (gnus-summary-show-article)))
12383
12384 (defun gnus-summary-repair-multipart (article)
12385   "Add a Content-Type header to a multipart article without one."
12386   (interactive (list (gnus-summary-article-number)))
12387   (gnus-with-article article
12388     (message-narrow-to-head)
12389     (message-remove-header "Mime-Version")
12390     (goto-char (point-max))
12391     (insert "Mime-Version: 1.0\n")
12392     (widen)
12393     (when (search-forward "\n--" nil t)
12394       (let ((separator (buffer-substring (point) (point-at-eol))))
12395         (message-narrow-to-head)
12396         (message-remove-header "Content-Type")
12397         (goto-char (point-max))
12398         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12399                         separator))
12400         (widen))))
12401   (let (gnus-mark-article-hook)
12402     (gnus-summary-select-article t t nil article)))
12403
12404 (defun gnus-summary-toggle-display-buttonized ()
12405   "Toggle the buttonizing of the article buffer."
12406   (interactive)
12407   (require 'gnus-art)
12408   (if (setq gnus-inhibit-mime-unbuttonizing
12409             (not gnus-inhibit-mime-unbuttonizing))
12410       (let ((gnus-unbuttonized-mime-types nil))
12411         (gnus-summary-show-article))
12412     (gnus-summary-show-article)))
12413
12414 ;;;
12415 ;;; Generic summary marking commands
12416 ;;;
12417
12418 (defvar gnus-summary-marking-alist
12419   '((read gnus-del-mark "d")
12420     (unread gnus-unread-mark "u")
12421     (ticked gnus-ticked-mark "!")
12422     (dormant gnus-dormant-mark "?")
12423     (expirable gnus-expirable-mark "e"))
12424   "An alist of names/marks/keystrokes.")
12425
12426 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12427 (defvar gnus-summary-mark-map)
12428
12429 (defun gnus-summary-make-all-marking-commands ()
12430   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12431   (dolist (elem gnus-summary-marking-alist)
12432     (apply 'gnus-summary-make-marking-command elem)))
12433
12434 (defun gnus-summary-make-marking-command (name mark keystroke)
12435   (let ((map (make-sparse-keymap)))
12436     (define-key gnus-summary-generic-mark-map keystroke map)
12437     (dolist (lway `((next "next" next nil "n")
12438                     (next-unread "next unread" next t "N")
12439                     (prev "previous" prev nil "p")
12440                     (prev-unread "previous unread" prev t "P")
12441                     (nomove "" nil nil ,keystroke)))
12442       (let ((func (gnus-summary-make-marking-command-1
12443                    mark (car lway) lway name)))
12444         (setq func (eval func))
12445         (define-key map (nth 4 lway) func)))))
12446
12447 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12448   `(defun ,(intern
12449             (format "gnus-summary-put-mark-as-%s%s"
12450                     name (if (eq way 'nomove)
12451                              ""
12452                            (concat "-" (symbol-name way)))))
12453      (n)
12454      ,(format
12455        "Mark the current article as %s%s.
12456 If N, the prefix, then repeat N times.
12457 If N is negative, move in reverse order.
12458 The difference between N and the actual number of articles marked is
12459 returned."
12460        name (cadr lway))
12461      (interactive "p")
12462      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12463
12464 (defun gnus-summary-generic-mark (n mark move unread)
12465   "Mark N articles with MARK."
12466   (unless (eq major-mode 'gnus-summary-mode)
12467     (error "This command can only be used in the summary buffer"))
12468   (gnus-summary-show-thread)
12469   (let ((nummove
12470          (cond
12471           ((eq move 'next) 1)
12472           ((eq move 'prev) -1)
12473           (t 0))))
12474     (if (zerop nummove)
12475         (setq n 1)
12476       (when (< n 0)
12477         (setq n (abs n)
12478               nummove (* -1 nummove))))
12479     (while (and (> n 0)
12480                 (gnus-summary-mark-article nil mark)
12481                 (zerop (gnus-summary-next-subject nummove unread t)))
12482       (setq n (1- n)))
12483     (when (/= 0 n)
12484       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12485     (gnus-summary-recenter)
12486     (gnus-summary-position-point)
12487     (gnus-set-mode-line 'summary)
12488     n))
12489
12490 (defun gnus-summary-insert-articles (articles)
12491   (when (setq articles
12492               (gnus-sorted-difference articles
12493                                       (mapcar (lambda (h)
12494                                                 (mail-header-number h))
12495                                               gnus-newsgroup-headers)))
12496     (setq gnus-newsgroup-headers
12497           (gnus-merge 'list
12498                       gnus-newsgroup-headers
12499                       (gnus-fetch-headers articles)
12500                       'gnus-article-sort-by-number))
12501     ;; Suppress duplicates?
12502     (when gnus-suppress-duplicates
12503       (gnus-dup-suppress-articles))
12504
12505     (if (and gnus-fetch-old-headers
12506              (eq gnus-headers-retrieved-by 'nov))
12507         ;; We might want to build some more threads first.
12508         (if (eq gnus-fetch-old-headers 'invisible)
12509             (gnus-build-all-threads)
12510           (gnus-build-old-threads))
12511       ;; Mark the inserted articles that are unread as unread.
12512       (setq gnus-newsgroup-unreads
12513             (gnus-sorted-nunion
12514              gnus-newsgroup-unreads
12515              (gnus-sorted-nintersection
12516               (gnus-list-of-unread-articles gnus-newsgroup-name)
12517               articles)))
12518       ;; Mark the inserted articles as selected so that the information
12519       ;; of the marks having been changed by a user may be updated when
12520       ;; exiting this group.  See `gnus-summary-update-info'.
12521       (dolist (art articles)
12522         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12523     ;; Let the Gnus agent mark articles as read.
12524     (when gnus-agent
12525       (gnus-agent-get-undownloaded-list))
12526     ;; Remove list identifiers from subject
12527     (when gnus-list-identifiers
12528       (gnus-summary-remove-list-identifiers))
12529     ;; First and last article in this newsgroup.
12530     (when gnus-newsgroup-headers
12531       (setq gnus-newsgroup-begin
12532             (mail-header-number (car gnus-newsgroup-headers))
12533             gnus-newsgroup-end
12534             (mail-header-number
12535              (gnus-last-element gnus-newsgroup-headers))))
12536     (when gnus-use-scoring
12537       (gnus-possibly-score-headers))))
12538
12539 (defun gnus-summary-insert-old-articles (&optional all)
12540   "Insert all old articles in this group.
12541 If ALL is non-nil, already read articles become readable.
12542 If ALL is a number, fetch this number of articles."
12543   (interactive "P")
12544   (prog1
12545       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12546             older len)
12547         (setq older
12548               ;; Some nntp servers lie about their active range.  When
12549               ;; this happens, the active range can be in the millions.
12550               ;; Use a compressed range to avoid creating a huge list.
12551               (gnus-range-difference (list gnus-newsgroup-active) old))
12552         (setq len (gnus-range-length older))
12553         (cond
12554          ((null older) nil)
12555          ((numberp all)
12556           (if (< all len)
12557               (let ((older-range (nreverse older)))
12558                 (setq older nil)
12559
12560                 (while (> all 0)
12561                   (let* ((r (pop older-range))
12562                          (min (if (numberp r) r (car r)))
12563                          (max (if (numberp r) r (cdr r))))
12564                     (while (and (<= min max)
12565                                 (> all 0))
12566                       (push max older)
12567                       (setq all (1- all)
12568                             max (1- max))))))
12569             (setq older (gnus-uncompress-range older))))
12570          (all
12571           (setq older (gnus-uncompress-range older)))
12572          (t
12573           (when (and (numberp gnus-large-newsgroup)
12574                    (> len gnus-large-newsgroup))
12575               (let* ((cursor-in-echo-area nil)
12576                      (initial (gnus-parameter-large-newsgroup-initial
12577                                gnus-newsgroup-name))
12578                      (input
12579                       (read-string
12580                        (format
12581                         "How many articles from %s (%s %d): "
12582                         (gnus-group-decoded-name gnus-newsgroup-name)
12583                         (if initial "max" "default")
12584                         len)
12585                        (if initial
12586                            (cons (number-to-string initial)
12587                                  0)))))
12588                 (unless (string-match "^[ \t]*$" input)
12589                   (setq all (string-to-number input))
12590                   (if (< all len)
12591                       (let ((older-range (nreverse older)))
12592                         (setq older nil)
12593
12594                         (while (> all 0)
12595                           (let* ((r (pop older-range))
12596                                  (min (if (numberp r) r (car r)))
12597                                  (max (if (numberp r) r (cdr r))))
12598                             (while (and (<= min max)
12599                                         (> all 0))
12600                               (push max older)
12601                               (setq all (1- all)
12602                                     max (1- max))))))))))
12603           (setq older (gnus-uncompress-range older))))
12604         (if (not older)
12605             (message "No old news.")
12606           (gnus-summary-insert-articles older)
12607           (gnus-summary-limit (gnus-sorted-nunion old older))))
12608     (gnus-summary-position-point)))
12609
12610 (defun gnus-summary-insert-new-articles ()
12611   "Insert all new articles in this group."
12612   (interactive)
12613   (prog1
12614       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12615             (old-active gnus-newsgroup-active)
12616             (nnmail-fetched-sources (list t))
12617             i new)
12618         (setq gnus-newsgroup-active
12619               (gnus-activate-group gnus-newsgroup-name 'scan))
12620         (setq i (cdr gnus-newsgroup-active))
12621         (while (> i (cdr old-active))
12622           (push i new)
12623           (decf i))
12624         (if (not new)
12625             (message "No gnus is bad news")
12626           (gnus-summary-insert-articles new)
12627           (setq gnus-newsgroup-unreads
12628                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12629           (gnus-summary-limit (gnus-sorted-nunion old new))))
12630     (gnus-summary-position-point)))
12631
12632 ;;; Bookmark support for Gnus.
12633 (declare-function bookmark-make-record-default
12634                   "bookmark" (&optional no-file no-context posn))
12635 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12636 (declare-function bookmark-default-handler "bookmark" (bmk))
12637 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12638 (defvar bookmark-yank-point)
12639 (defvar bookmark-current-buffer)
12640
12641 (defun gnus-summary-bookmark-make-record ()
12642   "Make a bookmark entry for a Gnus summary buffer."
12643   (let (pos buf)
12644     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12645       (save-restriction              ; FIXME is it necessary to widen?
12646         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12647       (setq buf "art") ; We are recording bookmark from article buffer.
12648       (setq bookmark-yank-point (point))
12649       (setq bookmark-current-buffer (current-buffer))
12650       (gnus-article-show-summary))      ; Go back in summary buffer.
12651     ;; We are now recording bookmark from summary buffer.
12652     (unless buf (setq buf "sum"))
12653     (let* ((subject (elt (gnus-summary-article-header) 1))
12654            (grp     (car gnus-article-current))
12655            (art     (cdr gnus-article-current))
12656            (head    (gnus-summary-article-header art))
12657            (id      (mail-header-id head)))
12658       `(,subject
12659         ,@(condition-case nil
12660               (bookmark-make-record-default 'no-file 'no-context pos)
12661             (wrong-number-of-arguments
12662              (bookmark-make-record-default 'point-only)))
12663         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12664         (group . ,grp) (article . ,art)
12665         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12666
12667 ;;;###autoload
12668 (defun gnus-summary-bookmark-jump (bookmark)
12669   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12670 BOOKMARK is a bookmark name or a bookmark record."
12671   (let ((group    (bookmark-prop-get bookmark 'group))
12672         (article  (bookmark-prop-get bookmark 'article))
12673         (id       (bookmark-prop-get bookmark 'message-id))
12674         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12675     (gnus-fetch-group group (list article))
12676     (gnus-summary-insert-cached-articles)
12677     (gnus-summary-goto-article id nil 'force)
12678     ;; FIXME we have to wait article buffer is ready (only large buffer)
12679     ;; Is there a better solution to know that?
12680     ;; If we don't wait `bookmark-default-handler' will have no chance
12681     ;; to set position. However there is no error, just wrong pos.
12682     (sit-for 1)
12683     (when (string= buf "Gnus-art")
12684       (other-window 1))
12685     (bookmark-default-handler
12686      `(""
12687        (buffer . ,(current-buffer))
12688        . ,(bookmark-get-bookmark-record bookmark)))))
12689
12690 (gnus-summary-make-all-marking-commands)
12691
12692 (gnus-ems-redefine)
12693
12694 (provide 'gnus-sum)
12695
12696 (run-hooks 'gnus-sum-load-hook)
12697
12698 ;; Local Variables:
12699 ;; coding: iso-8859-1
12700 ;; End:
12701
12702 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12703 ;;; gnus-sum.el ends here