Remove nnultimate.el and related code.
[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   :version "24.1"
84   :type 'boolean)
85
86 (defcustom gnus-fetch-old-headers nil
87   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
88 If an unread article in the group refers to an older, already
89 read (or just marked as read) article, the old article will not
90 normally be displayed in the Summary buffer.  If this variable is
91 t, Gnus will attempt to grab the headers to the old articles, and
92 thereby build complete threads.  If it has the value `some', all
93 old headers will be fetched but only enough headers to connect
94 otherwise loose threads will be displayed.  This variable can
95 also be a number.  In that case, no more than that number of old
96 headers will be fetched.  If it has the value `invisible', all
97 old headers will be fetched, but none will be displayed.
98
99 The server has to support NOV for any of this to work.
100
101 This feature can seriously impact performance it ignores all
102 locally cached header entries.  Setting it to t for groups for a
103 server that doesn't expire articles (such as news.gmane.org),
104 leads to very slow summary generation."
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const :tag "on" t)
108                  (const some)
109                  (const invisible)
110                  number
111                  (sexp :menu-tag "other" t)))
112
113 (defcustom gnus-refer-thread-limit 500
114   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
115 If t, fetch all the available old headers."
116   :group 'gnus-thread
117   :type '(choice number
118                  (sexp :menu-tag "other" t)))
119
120 (defcustom gnus-summary-make-false-root 'adopt
121   "*nil means that Gnus won't gather loose threads.
122 If the root of a thread has expired or been read in a previous
123 session, the information necessary to build a complete thread has been
124 lost.  Instead of having many small sub-threads from this original thread
125 scattered all over the summary buffer, Gnus can gather them.
126
127 If non-nil, Gnus will try to gather all loose sub-threads from an
128 original thread into one large thread.
129
130 If this variable is non-nil, it should be one of `none', `adopt',
131 `dummy' or `empty'.
132
133 If this variable is `none', Gnus will not make a false root, but just
134 present the sub-threads after another.
135 If this variable is `dummy', Gnus will create a dummy root that will
136 have all the sub-threads as children.
137 If this variable is `adopt', Gnus will make one of the \"children\"
138 the parent and mark all the step-children as such.
139 If this variable is `empty', the \"children\" are printed with empty
140 subject fields.  (Or rather, they will be printed with a string
141 given by the `gnus-summary-same-subject' variable.)"
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const none)
145                  (const dummy)
146                  (const adopt)
147                  (const empty)))
148
149 (defcustom gnus-summary-make-false-root-always nil
150   "Always make a false dummy root."
151   :version "22.1"
152   :group 'gnus-thread
153   :type 'boolean)
154
155 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
156   "*A regexp to match subjects to be excluded from loose thread gathering.
157 As loose thread gathering is done on subjects only, that means that
158 there can be many false gatherings performed.  By rooting out certain
159 common subjects, gathering might become saner."
160   :group 'gnus-thread
161   :type 'regexp)
162
163 (defcustom gnus-summary-gather-subject-limit nil
164   "*Maximum length of subject comparisons when gathering loose threads.
165 Use nil to compare full subjects.  Setting this variable to a low
166 number will help gather threads that have been corrupted by
167 newsreaders chopping off subject lines, but it might also mean that
168 unrelated articles that have subject that happen to begin with the
169 same few characters will be incorrectly gathered.
170
171 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
172 comparing subjects."
173   :group 'gnus-thread
174   :type '(choice (const :tag "off" nil)
175                  (const fuzzy)
176                  (sexp :menu-tag "on" t)))
177
178 (defcustom gnus-simplify-subject-functions nil
179   "List of functions taking a string argument that simplify subjects.
180 The functions are applied recursively.
181
182 Useful functions to put in this list include:
183 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
184 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
185   :group 'gnus-thread
186   :type '(repeat function))
187
188 (defcustom gnus-simplify-ignored-prefixes nil
189   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
190   :group 'gnus-thread
191   :type '(choice (const :tag "off" nil)
192                  regexp))
193
194 (defcustom gnus-build-sparse-threads nil
195   "*If non-nil, fill in the gaps in threads.
196 If `some', only fill in the gaps that are needed to tie loose threads
197 together.  If `more', fill in all leaf nodes that Gnus can find.  If
198 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  (const some)
202                  (const more)
203                  (sexp :menu-tag "all" t)))
204
205 (defcustom gnus-summary-thread-gathering-function
206   'gnus-gather-threads-by-subject
207   "*Function used for gathering loose threads.
208 There are two pre-defined functions: `gnus-gather-threads-by-subject',
209 which only takes Subjects into consideration; and
210 `gnus-gather-threads-by-references', which compared the References
211 headers of the articles to find matches."
212   :group 'gnus-thread
213   :type '(radio (function-item gnus-gather-threads-by-subject)
214                 (function-item gnus-gather-threads-by-references)
215                 (function :tag "other")))
216
217 (defcustom gnus-summary-same-subject ""
218   "*String indicating that the current article has the same subject as the previous.
219 This variable will only be used if the value of
220 `gnus-summary-make-false-root' is `empty'."
221   :group 'gnus-summary-format
222   :type 'string)
223
224 (defcustom gnus-summary-goto-unread t
225   "*If t, many commands will go to the next unread article.
226 This applies to marking commands as well as other commands that
227 \"naturally\" select the next article, like, for instance, `SPC' at
228 the end of an article.
229
230 If nil, the marking commands do NOT go to the next unread article
231 \(they go to the next article instead).  If `never', commands that
232 usually go to the next unread article, will go to the next article,
233 whether it is read or not."
234   :group 'gnus-summary-marks
235   :link '(custom-manual "(gnus)Setting Marks")
236   :type '(choice (const :tag "off" nil)
237                  (const never)
238                  (sexp :menu-tag "on" t)))
239
240 (defcustom gnus-summary-default-score 0
241   "*Default article score level.
242 All scores generated by the score files will be added to this score.
243 If this variable is nil, scoring will be disabled."
244   :group 'gnus-score-default
245   :type '(choice (const :tag "disable")
246                  integer))
247
248 (defcustom gnus-summary-default-high-score 0
249   "*Default threshold for a high scored article.
250 An article will be highlighted as high scored if its score is greater
251 than this score."
252   :version "22.1"
253   :group 'gnus-score-default
254   :type 'integer)
255
256 (defcustom gnus-summary-default-low-score 0
257   "*Default threshold for a low scored article.
258 An article will be highlighted as low scored if its score is smaller
259 than this score."
260   :version "22.1"
261   :group 'gnus-score-default
262   :type 'integer)
263
264 (defcustom gnus-summary-zcore-fuzz 0
265   "*Fuzziness factor for the zcore in the summary buffer.
266 Articles with scores closer than this to `gnus-summary-default-score'
267 will not be marked."
268   :group 'gnus-summary-format
269   :type 'integer)
270
271 (defcustom gnus-simplify-subject-fuzzy-regexp nil
272   "*Strings to be removed when doing fuzzy matches.
273 This can either be a regular expression or list of regular expressions
274 that will be removed from subject strings if fuzzy subject
275 simplification is selected."
276   :group 'gnus-thread
277   :type '(repeat regexp))
278
279 (defcustom gnus-show-threads t
280   "*If non-nil, display threads in summary mode."
281   :group 'gnus-thread
282   :type 'boolean)
283
284 (defcustom gnus-thread-hide-subtree nil
285   "*If non-nil, hide all threads initially.
286 This can be a predicate specifier which says which threads to hide.
287 If threads are hidden, you have to run the command
288 `gnus-summary-show-thread' by hand or select an article."
289   :group 'gnus-thread
290   :type '(radio (sexp :format "Non-nil\n"
291                       :match (lambda (widget value)
292                                (not (or (consp value) (functionp value))))
293                       :value t)
294                 (const nil)
295                 (sexp :tag "Predicate specifier")))
296
297 (defcustom gnus-thread-hide-killed t
298   "*If non-nil, hide killed threads automatically."
299   :group 'gnus-thread
300   :type 'boolean)
301
302 (defcustom gnus-thread-ignore-subject t
303   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
304 If nil, articles that have different subjects from their parents will
305 start separate threads."
306   :group 'gnus-thread
307   :type 'boolean)
308
309 (defcustom gnus-thread-operation-ignore-subject t
310   "*If non-nil, subjects will be ignored when doing thread commands.
311 This affects commands like `gnus-summary-kill-thread' and
312 `gnus-summary-lower-thread'.
313
314 If this variable is nil, articles in the same thread with different
315 subjects will not be included in the operation in question.  If this
316 variable is `fuzzy', only articles that have subjects that are fuzzily
317 equal will be included."
318   :group 'gnus-thread
319   :type '(choice (const :tag "off" nil)
320                  (const fuzzy)
321                  (sexp :tag "on" t)))
322
323 (defcustom gnus-thread-indent-level 4
324   "*Number that says how much each sub-thread should be indented."
325   :group 'gnus-thread
326   :type 'integer)
327
328 (defcustom gnus-auto-extend-newsgroup t
329   "*If non-nil, extend newsgroup forward and backward when requested."
330   :group 'gnus-summary-choose
331   :type 'boolean)
332
333 (defcustom gnus-auto-select-first t
334   "If non-nil, select an article on group entry.
335 An article is selected automatically when entering a group
336 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
337 `gnus-summary-catchup-and-goto-next-group'.
338
339 Which article is selected is controlled by the variable
340 `gnus-auto-select-subject'.
341
342 If you want to prevent automatic selection of articles in some
343 newsgroups, set the variable to nil in `gnus-select-group-hook'."
344   ;; Commands include...
345   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
346   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
347   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
348   :group 'gnus-group-select
349   :type '(choice (const :tag "none" nil)
350                  (sexp :menu-tag "first" t)))
351
352 (defcustom gnus-auto-select-subject 'unread
353   "*Says what subject to place under point when entering a group.
354
355 This variable can either be the symbols `first' (place point on the
356 first subject), `unread' (place point on the subject line of the first
357 unread article), `best' (place point on the subject line of the
358 higest-scored article), `unseen' (place point on the subject line of
359 the first unseen article), `unseen-or-unread' (place point on the subject
360 line of the first unseen article or, if all article have been seen, on the
361 subject line of the first unread article), or a function to be called to
362 place point on some subject line."
363   :version "22.1"
364   :group 'gnus-group-select
365   :type '(choice (const best)
366                  (const unread)
367                  (const first)
368                  (const unseen)
369                  (const unseen-or-unread)))
370
371 (defcustom gnus-auto-select-next t
372   "*If non-nil, offer to go to the next group from the end of the previous.
373 If the value is t and the next newsgroup is empty, Gnus will exit
374 summary mode and go back to group mode.  If the value is neither nil
375 nor t, Gnus will select the following unread newsgroup.  In
376 particular, if the value is the symbol `quietly', the next unread
377 newsgroup will be selected without any confirmation, and if it is
378 `almost-quietly', the next group will be selected without any
379 confirmation if you are located on the last article in the group.
380 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
381 will go to the next group without confirmation."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "off" nil)
384                  (const quietly)
385                  (const almost-quietly)
386                  (const slightly-quietly)
387                  (sexp :menu-tag "on" t)))
388
389 (defcustom gnus-auto-select-same nil
390   "*If non-nil, select the next article with the same subject.
391 If there are no more articles with the same subject, go to
392 the first unread article."
393   :group 'gnus-summary-maneuvering
394   :type 'boolean)
395
396 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
397   "What article should be selected after exiting an ephemeral group.
398 Valid values include:
399
400 `next'
401   Select the next article.
402 `next-unread'
403   Select the next unread article.
404 `next-noselect'
405   Move the cursor to the next article.  This is the default.
406 `next-unread-noselect'
407   Move the cursor to the next unread article.
408
409 If it has any other value or there is no next (unread) article, the
410 article selected before entering to the ephemeral group will appear."
411   :version "23.1" ;; No Gnus
412   :group 'gnus-summary-maneuvering
413   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
414                  (const next) (const next-unread)
415                  (const next-noselect) (const next-unread-noselect)
416                  (sexp :tag "other" :value nil)))
417
418 (defcustom gnus-auto-goto-ignores 'unfetched
419   "*Says how to handle unfetched articles when maneuvering.
420
421 This variable can either be the symbols nil (maneuver to any
422 article), `undownloaded' (maneuvering while unplugged ignores articles
423 that have not been fetched), `always-undownloaded' (maneuvering always
424 ignores articles that have not been fetched), `unfetched' (maneuvering
425 ignores articles whose headers have not been fetched).
426
427 NOTE: The list of unfetched articles will always be nil when plugged
428 and, when unplugged, a subset of the undownloaded article list."
429   :version "22.1"
430   :group 'gnus-summary-maneuvering
431   :type '(choice (const :tag "None" nil)
432                  (const :tag "Undownloaded when unplugged" undownloaded)
433                  (const :tag "Undownloaded" always-undownloaded)
434                  (const :tag "Unfetched" unfetched)))
435
436 (defcustom gnus-summary-check-current nil
437   "*If non-nil, consider the current article when moving.
438 The \"unread\" movement commands will stay on the same line if the
439 current article is unread."
440   :group 'gnus-summary-maneuvering
441   :type 'boolean)
442
443 (defcustom gnus-auto-center-summary 2
444   "*If non-nil, always center the current summary buffer.
445 In particular, if `vertical' do only vertical recentering.  If non-nil
446 and non-`vertical', do both horizontal and vertical recentering."
447   :group 'gnus-summary-maneuvering
448   :type '(choice (const :tag "none" nil)
449                  (const vertical)
450                  (integer :tag "height")
451                  (sexp :menu-tag "both" t)))
452
453 (defvar gnus-auto-center-group t
454   "*If non-nil, always center the group buffer.")
455
456 (defcustom gnus-show-all-headers nil
457   "*If non-nil, don't hide any headers."
458   :group 'gnus-article-hiding
459   :group 'gnus-article-headers
460   :type 'boolean)
461
462 (defcustom gnus-summary-ignore-duplicates nil
463   "*If non-nil, ignore articles with identical Message-ID headers."
464   :group 'gnus-summary
465   :type 'boolean)
466
467 (defcustom gnus-single-article-buffer t
468   "*If non-nil, display all articles in the same buffer.
469 If nil, each group will get its own article buffer."
470   :group 'gnus-article-various
471   :type 'boolean)
472
473 (defcustom gnus-break-pages t
474   "*If non-nil, do page breaking on articles.
475 The page delimiter is specified by the `gnus-page-delimiter'
476 variable."
477   :group 'gnus-article-various
478   :type 'boolean)
479
480 (defcustom gnus-move-split-methods nil
481   "*Variable used to suggest where articles are to be moved to.
482 It uses the same syntax as the `gnus-split-methods' variable.
483 However, whereas `gnus-split-methods' specifies file names as targets,
484 this variable specifies group names."
485   :group 'gnus-summary-mail
486   :type '(repeat (choice (list :value (fun) function)
487                          (cons :value ("" "") regexp (repeat string))
488                          (sexp :value nil))))
489
490 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
491   "Function used to compute default prefix for article move/copy/etc prompts.
492 The function should take one argument, a group name, and return a
493 string with the suggested prefix."
494   :group 'gnus-summary-mail
495   :type 'function)
496
497 ;; FIXME: Although the custom type is `character' for the following variables,
498 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
499
500 (defcustom gnus-unread-mark ?           ;Whitespace
501   "*Mark used for unread articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-ticked-mark ?!
506   "*Mark used for ticked articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-dormant-mark ??
511   "*Mark used for dormant articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-del-mark ?r
516   "*Mark used for del'd articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-read-mark ?R
521   "*Mark used for read articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-expirable-mark ?E
526   "*Mark used for expirable articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-killed-mark ?K
531   "*Mark used for killed articles."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-spam-mark ?$
536   "*Mark used for spam articles."
537   :version "22.1"
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-souped-mark ?F
542   "*Mark used for souped articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-kill-file-mark ?X
547   "*Mark used for articles killed by kill files."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-low-score-mark ?Y
552   "*Mark used for articles with a low score."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-catchup-mark ?C
557   "*Mark used for articles that are caught up."
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-replied-mark ?A
562   "*Mark used for articles that have been replied to."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-forwarded-mark ?F
567   "*Mark used for articles that have been forwarded."
568   :version "22.1"
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-recent-mark ?N
573   "*Mark used for articles that are recent."
574   :version "22.1"
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-cached-mark ?*
579   "*Mark used for articles that are in the cache."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-saved-mark ?S
584   "*Mark used for articles that have been saved."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-unseen-mark ?.
589   "*Mark used for articles that haven't been seen."
590   :version "22.1"
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-no-mark ?               ;Whitespace
595   "*Mark used for articles that have no other secondary mark."
596   :version "22.1"
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-ancient-mark ?O
601   "*Mark used for ancient articles."
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-sparse-mark ?Q
606   "*Mark used for sparsely reffed articles."
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-canceled-mark ?G
611   "*Mark used for canceled articles."
612   :group 'gnus-summary-marks
613   :type 'character)
614
615 (defcustom gnus-duplicate-mark ?M
616   "*Mark used for duplicate articles."
617   :group 'gnus-summary-marks
618   :type 'character)
619
620 (defcustom gnus-undownloaded-mark ?-
621   "*Mark used for articles that weren't downloaded."
622   :version "22.1"
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-downloaded-mark ?+
627   "*Mark used for articles that were downloaded."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-downloadable-mark ?%
632   "*Mark used for articles that are to be downloaded."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-unsendable-mark ?=
637   "*Mark used for articles that won't be sent."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-score-over-mark ?+
642   "*Score mark used for articles with high scores."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-score-below-mark ?-
647   "*Score mark used for articles with low scores."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-empty-thread-mark ?     ;Whitespace
652   "*There is no thread under the article."
653   :group 'gnus-summary-marks
654   :type 'character)
655
656 (defcustom gnus-not-empty-thread-mark ?=
657   "*There is a thread under the article."
658   :group 'gnus-summary-marks
659   :type 'character)
660
661 (defcustom gnus-view-pseudo-asynchronously nil
662   "*If non-nil, Gnus will view pseudo-articles asynchronously."
663   :group 'gnus-extract-view
664   :type 'boolean)
665
666 (defcustom gnus-auto-expirable-marks
667   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
668         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
669         gnus-souped-mark gnus-duplicate-mark)
670   "*The list of marks converted into expiration if a group is auto-expirable."
671   :version "21.1"
672   :group 'gnus-summary
673   :type '(repeat character))
674
675 (defcustom gnus-inhibit-user-auto-expire t
676   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
677   :version "21.1"
678   :group 'gnus-summary
679   :type 'boolean)
680
681 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
682   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
683 If nil, the expirable marks will be unchanged except that the marks
684 will be removed when copying or moving articles to a group that has
685 not turned auto-expire on.  If non-nil, articles that have been read
686 will be marked as expirable when being copied or moved to a group in
687 which auto-expire is turned on."
688   :version "23.2"
689   :type 'boolean
690   :group 'gnus-summary-marks)
691
692 (defcustom gnus-view-pseudos nil
693   "*If `automatic', pseudo-articles will be viewed automatically.
694 If `not-confirm', pseudos will be viewed automatically, and the user
695 will not be asked to confirm the command."
696   :group 'gnus-extract-view
697   :type '(choice (const :tag "off" nil)
698                  (const automatic)
699                  (const not-confirm)))
700
701 (defcustom gnus-view-pseudos-separately t
702   "*If non-nil, one pseudo-article will be created for each file to be viewed.
703 If nil, all files that use the same viewing command will be given as a
704 list of parameters to that command."
705   :group 'gnus-extract-view
706   :type 'boolean)
707
708 (defcustom gnus-insert-pseudo-articles t
709   "*If non-nil, insert pseudo-articles when decoding articles."
710   :group 'gnus-extract-view
711   :type 'boolean)
712
713 (defcustom gnus-summary-dummy-line-format
714   "   %(:                             :%) %S\n"
715   "*The format specification for the dummy roots in the summary buffer.
716 It works along the same lines as a normal formatting string,
717 with some simple extensions.
718
719 %S  The subject
720
721 General format specifiers can also be used.
722 See `(gnus)Formatting Variables'."
723   :link '(custom-manual "(gnus)Formatting Variables")
724   :group 'gnus-threading
725   :type 'string)
726
727 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
728   "*The format specification for the summary mode line.
729 It works along the same lines as a normal formatting string,
730 with some simple extensions:
731
732 %G  Group name
733 %p  Unprefixed group name
734 %A  Current article number
735 %z  Current article score
736 %V  Gnus version
737 %U  Number of unread articles in the group
738 %e  Number of unselected articles in the group
739 %Z  A string with unread/unselected article counts
740 %g  Shortish group name
741 %S  Subject of the current article
742 %u  User-defined spec
743 %s  Current score file name
744 %d  Number of dormant articles
745 %r  Number of articles that have been marked as read in this session
746 %E  Number of articles expunged by the score files"
747   :group 'gnus-summary-format
748   :type 'string)
749
750 (defcustom gnus-list-identifiers nil
751   "Regexp that matches list identifiers to be removed from subject.
752 This can also be a list of regexps."
753   :version "21.1"
754   :group 'gnus-summary-format
755   :group 'gnus-article-hiding
756   :type '(choice (const :tag "none" nil)
757                  (regexp :value ".*")
758                  (repeat :value (".*") regexp)))
759
760 (defcustom gnus-summary-mark-below 0
761   "*Mark all articles with a score below this variable as read.
762 This variable is local to each summary buffer and usually set by the
763 score file."
764   :group 'gnus-score-default
765   :type 'integer)
766
767 (defun gnus-widget-reversible-match (widget value)
768   "Ignoring WIDGET, convert VALUE to internal form.
769 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
770   ;; (debug value)
771   (or (symbolp value)
772       (and (listp value)
773            (eq (length value) 2)
774            (eq (nth 0 value) 'not)
775            (symbolp (nth 1 value)))))
776
777 (defun gnus-widget-reversible-to-internal (widget value)
778   "Ignoring WIDGET, convert VALUE to internal form.
779 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
780 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
781   ;; (debug value)
782   (if (atom value)
783       (list value nil)
784     (list (nth 1 value) t)))
785
786 (defun gnus-widget-reversible-to-external (widget value)
787   "Ignoring WIDGET, convert VALUE to external form.
788 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
789 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
790   ;; (debug value)
791   (if (nth 1 value)
792       (list 'not (nth 0 value))
793     (nth 0 value)))
794
795 (define-widget 'gnus-widget-reversible 'group
796   "A `group' that convert values."
797   :match 'gnus-widget-reversible-match
798   :value-to-internal 'gnus-widget-reversible-to-internal
799   :value-to-external 'gnus-widget-reversible-to-external)
800
801 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
802   "*List of functions used for sorting articles in the summary buffer.
803
804 Each function takes two articles and returns non-nil if the first
805 article should be sorted before the other.  If you use more than one
806 function, the primary sort function should be the last.  You should
807 probably always include `gnus-article-sort-by-number' in the list of
808 sorting functions -- preferably first.  Also note that sorting by date
809 is often much slower than sorting by number, and the sorting order is
810 very similar.  (Sorting by date means sorting by the time the message
811 was sent, sorting by number means sorting by arrival time.)
812
813 Each item can also be a list `(not F)' where F is a function;
814 this reverses the sort order.
815
816 Ready-made functions include `gnus-article-sort-by-number',
817 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
818 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
819 and `gnus-article-sort-by-score'.
820
821 When threading is turned on, the variable `gnus-thread-sort-functions'
822 controls how articles are sorted."
823   :group 'gnus-summary-sort
824   :type '(repeat (gnus-widget-reversible
825                   (choice (function-item gnus-article-sort-by-number)
826                           (function-item gnus-article-sort-by-author)
827                           (function-item gnus-article-sort-by-subject)
828                           (function-item gnus-article-sort-by-date)
829                           (function-item gnus-article-sort-by-score)
830                           (function-item gnus-article-sort-by-random)
831                           (function :tag "other"))
832                   (boolean :tag "Reverse order"))))
833
834
835 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
836   "*List of functions used for sorting threads in the summary buffer.
837 By default, threads are sorted by article number.
838
839 Each function takes two threads and returns non-nil if the first
840 thread should be sorted before the other.  If you use more than one
841 function, the primary sort function should be the last.  You should
842 probably always include `gnus-thread-sort-by-number' in the list of
843 sorting functions -- preferably first.  Also note that sorting by date
844 is often much slower than sorting by number, and the sorting order is
845 very similar.  (Sorting by date means sorting by the time the message
846 was sent, sorting by number means sorting by arrival time.)
847
848 Each list item can also be a list `(not F)' where F is a
849 function; this specifies reversed sort order.
850
851 Ready-made functions include `gnus-thread-sort-by-number',
852 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
853 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
854 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
855 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
856 and `gnus-thread-sort-by-total-score' (see
857 `gnus-thread-score-function').
858
859 When threading is turned off, the variable
860 `gnus-article-sort-functions' controls how articles are sorted."
861   :group 'gnus-summary-sort
862   :type '(repeat
863           (gnus-widget-reversible
864            (choice (function-item gnus-thread-sort-by-number)
865                    (function-item gnus-thread-sort-by-author)
866                    (function-item gnus-thread-sort-by-recipient)
867                    (function-item gnus-thread-sort-by-subject)
868                    (function-item gnus-thread-sort-by-date)
869                    (function-item gnus-thread-sort-by-score)
870                    (function-item gnus-thread-sort-by-most-recent-number)
871                    (function-item gnus-thread-sort-by-most-recent-date)
872                    (function-item gnus-thread-sort-by-random)
873                    (function-item gnus-thread-sort-by-total-score)
874                    (function :tag "other"))
875            (boolean :tag "Reverse order"))))
876
877 (defcustom gnus-thread-score-function '+
878   "*Function used for calculating the total score of a thread.
879
880 The function is called with the scores of the article and each
881 subthread and should then return the score of the thread.
882
883 Some functions you can use are `+', `max', or `min'."
884   :group 'gnus-summary-sort
885   :type 'function)
886
887 (defcustom gnus-summary-expunge-below nil
888   "All articles that have a score less than this variable will be expunged.
889 This variable is local to the summary buffers."
890   :group 'gnus-score-default
891   :type '(choice (const :tag "off" nil)
892                  integer))
893
894 (defcustom gnus-thread-expunge-below nil
895   "All threads that have a total score less than this variable will be expunged.
896 See `gnus-thread-score-function' for en explanation of what a
897 \"thread score\" is.
898
899 This variable is local to the summary buffers."
900   :group 'gnus-threading
901   :group 'gnus-score-default
902   :type '(choice (const :tag "off" nil)
903                  integer))
904
905 (defcustom gnus-summary-mode-hook nil
906   "*A hook for Gnus summary mode.
907 This hook is run before any variables are set in the summary buffer."
908   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
909   :group 'gnus-summary-various
910   :type 'hook)
911
912 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
913 (when (featurep 'xemacs)
914   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
915   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
916   (add-hook 'gnus-summary-mode-hook
917             'gnus-xmas-switch-horizontal-scrollbar-off))
918
919 (defcustom gnus-summary-menu-hook nil
920   "*Hook run after the creation of the summary mode menu."
921   :group 'gnus-summary-visual
922   :type 'hook)
923
924 (defcustom gnus-summary-exit-hook nil
925   "*A hook called on exit from the summary buffer.
926 It will be called with point in the group buffer."
927   :group 'gnus-summary-exit
928   :type 'hook)
929
930 (defcustom gnus-summary-prepare-hook nil
931   "*A hook called after the summary buffer has been generated.
932 If you want to modify the summary buffer, you can use this hook."
933   :group 'gnus-summary-various
934   :type 'hook)
935
936 (defcustom gnus-summary-prepared-hook nil
937   "*A hook called as the last thing after the summary buffer has been generated."
938   :group 'gnus-summary-various
939   :type 'hook)
940
941 (defcustom gnus-summary-generate-hook nil
942   "*A hook run just before generating the summary buffer.
943 This hook is commonly used to customize threading variables and the
944 like."
945   :group 'gnus-summary-various
946   :type 'hook)
947
948 (defcustom gnus-select-group-hook nil
949   "*A hook called when a newsgroup is selected.
950
951 If you'd like to simplify subjects like the
952 `gnus-summary-next-same-subject' command does, you can use the
953 following hook:
954
955  (add-hook gnus-select-group-hook
956            (lambda ()
957              (mapcar (lambda (header)
958                        (mail-header-set-subject
959                         header
960                         (gnus-simplify-subject
961                          (mail-header-subject header) 're-only)))
962                      gnus-newsgroup-headers)))"
963   :group 'gnus-group-select
964   :type 'hook)
965
966 (defcustom gnus-select-article-hook nil
967   "*A hook called when an article is selected."
968   :group 'gnus-summary-choose
969   :options '(gnus-agent-fetch-selected-article)
970   :type 'hook)
971
972 (defcustom gnus-visual-mark-article-hook
973   (list 'gnus-highlight-selected-summary)
974   "*Hook run after selecting an article in the summary buffer.
975 It is meant to be used for highlighting the article in some way.  It
976 is not run if `gnus-visual' is nil."
977   :group 'gnus-summary-visual
978   :type 'hook)
979
980 (defcustom gnus-parse-headers-hook nil
981   "*A hook called before parsing the headers."
982   :group 'gnus-various
983   :type 'hook)
984
985 (defcustom gnus-exit-group-hook nil
986   "*A hook called when exiting summary mode.
987 This hook is not called from the non-updating exit commands like `Q'."
988   :group 'gnus-various
989   :type 'hook)
990
991 (defcustom gnus-summary-update-hook
992   (list 'gnus-summary-highlight-line)
993   "*A hook called when a summary line is changed.
994 The hook will not be called if `gnus-visual' is nil.
995
996 The default function `gnus-summary-highlight-line' will
997 highlight the line according to the `gnus-summary-highlight'
998 variable."
999   :group 'gnus-summary-visual
1000   :type 'hook)
1001
1002 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1003   "*A hook called when an article is selected for the first time.
1004 The hook is intended to mark an article as read (or unread)
1005 automatically when it is selected."
1006   :group 'gnus-summary-choose
1007   :type 'hook)
1008
1009 (defcustom gnus-group-no-more-groups-hook nil
1010   "*A hook run when returning to group mode having no more (unread) groups."
1011   :group 'gnus-group-select
1012   :type 'hook)
1013
1014 (defcustom gnus-ps-print-hook nil
1015   "*A hook run before ps-printing something from Gnus."
1016   :group 'gnus-summary
1017   :type 'hook)
1018
1019 (defcustom gnus-summary-article-move-hook nil
1020   "*A hook called after an article is moved, copied, respooled, or crossposted."
1021   :version "22.1"
1022   :group 'gnus-summary
1023   :type 'hook)
1024
1025 (defcustom gnus-summary-article-delete-hook nil
1026   "*A hook called after an article is deleted."
1027   :version "22.1"
1028   :group 'gnus-summary
1029   :type 'hook)
1030
1031 (defcustom gnus-summary-article-expire-hook nil
1032   "*A hook called after an article is expired."
1033   :version "22.1"
1034   :group 'gnus-summary
1035   :type 'hook)
1036
1037 (defcustom gnus-summary-display-arrow
1038   (and (fboundp 'display-graphic-p)
1039        (display-graphic-p))
1040   "*If non-nil, display an arrow highlighting the current article."
1041   :version "22.1"
1042   :group 'gnus-summary
1043   :type 'boolean)
1044
1045 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1046   "Face used for highlighting the current article in the summary buffer."
1047   :group 'gnus-summary-visual
1048   :type 'face)
1049
1050 (defvar gnus-tmp-downloaded nil)
1051
1052 (defcustom gnus-summary-highlight
1053   '(((eq mark gnus-canceled-mark)
1054      . gnus-summary-cancelled)
1055     ((and uncached (> score default-high))
1056      . gnus-summary-high-undownloaded)
1057     ((and uncached (< score default-low))
1058      . gnus-summary-low-undownloaded)
1059     (uncached
1060      . gnus-summary-normal-undownloaded)
1061     ((and (> score default-high)
1062           (or (eq mark gnus-dormant-mark)
1063               (eq mark gnus-ticked-mark)))
1064      . gnus-summary-high-ticked)
1065     ((and (< score default-low)
1066           (or (eq mark gnus-dormant-mark)
1067               (eq mark gnus-ticked-mark)))
1068      . gnus-summary-low-ticked)
1069     ((or (eq mark gnus-dormant-mark)
1070          (eq mark gnus-ticked-mark))
1071      . gnus-summary-normal-ticked)
1072     ((and (> score default-high) (eq mark gnus-ancient-mark))
1073      . gnus-summary-high-ancient)
1074     ((and (< score default-low) (eq mark gnus-ancient-mark))
1075      . gnus-summary-low-ancient)
1076     ((eq mark gnus-ancient-mark)
1077      . gnus-summary-normal-ancient)
1078     ((and (> score default-high) (eq mark gnus-unread-mark))
1079      . gnus-summary-high-unread)
1080     ((and (< score default-low) (eq mark gnus-unread-mark))
1081      . gnus-summary-low-unread)
1082     ((eq mark gnus-unread-mark)
1083      . gnus-summary-normal-unread)
1084     ((> score default-high)
1085      . gnus-summary-high-read)
1086     ((< score default-low)
1087      . gnus-summary-low-read)
1088     (t
1089      . gnus-summary-normal-read))
1090   "*Controls the highlighting of summary buffer lines.
1091
1092 A list of (FORM . FACE) pairs.  When deciding how a particular
1093 summary line should be displayed, each form is evaluated.  The content
1094 of the face field after the first true form is used.  You can change
1095 how those summary lines are displayed, by editing the face field.
1096
1097 You can use the following variables in the FORM field.
1098
1099 score:        The article's score.
1100 default:      The default article score.
1101 default-high: The default score for high scored articles.
1102 default-low:  The default score for low scored articles.
1103 below:        The score below which articles are automatically marked as read.
1104 mark:         The article's mark.
1105 uncached:     Non-nil if the article is uncached."
1106   :group 'gnus-summary-visual
1107   :type '(repeat (cons (sexp :tag "Form" nil)
1108                        face)))
1109 (put 'gnus-summary-highlight 'risky-local-variable t)
1110
1111 (defcustom gnus-alter-header-function nil
1112   "Function called to allow alteration of article header structures.
1113 The function is called with one parameter, the article header vector,
1114 which it may alter in any way."
1115   :type '(choice (const :tag "None" nil)
1116                  function)
1117   :group 'gnus-summary)
1118
1119 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1120   "Function used to decode a string with encoded words.")
1121
1122 (defvar gnus-decode-encoded-address-function
1123   'mail-decode-encoded-address-string
1124   "Function used to decode addresses with encoded words.")
1125
1126 (defcustom gnus-extra-headers '(To Newsgroups)
1127   "*Extra headers to parse."
1128   :version "21.1"
1129   :group 'gnus-summary
1130   :type '(repeat symbol))
1131
1132 (defcustom gnus-ignored-from-addresses
1133   (and user-mail-address
1134        (not (string= user-mail-address ""))
1135        (regexp-quote user-mail-address))
1136   "*From headers that may be suppressed in favor of To headers.
1137 This can be a regexp or a list of regexps."
1138   :version "21.1"
1139   :group 'gnus-summary
1140   :type '(choice regexp
1141                  (repeat :tag "Regexp List" regexp)))
1142
1143 (defsubst gnus-ignored-from-addresses ()
1144   (gmm-regexp-concat gnus-ignored-from-addresses))
1145
1146 (defcustom gnus-summary-to-prefix "-> "
1147   "*String prefixed to the To field in the summary line when
1148 using `gnus-ignored-from-addresses'."
1149   :version "22.1"
1150   :group 'gnus-summary
1151   :type 'string)
1152
1153 (defcustom gnus-summary-newsgroup-prefix "=> "
1154   "*String prefixed to the Newsgroup field in the summary
1155 line when using `gnus-ignored-from-addresses'."
1156   :version "22.1"
1157   :group 'gnus-summary
1158   :type 'string)
1159
1160 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1161   "List of charsets that should be ignored.
1162 When these charsets are used in the \"charset\" parameter, the
1163 default charset will be used instead."
1164   :version "21.1"
1165   :type '(repeat symbol)
1166   :group 'gnus-charset)
1167
1168 (defcustom gnus-newsgroup-maximum-articles nil
1169   "The maximum number of articles a newsgroup.
1170 If this is a number, old articles in a newsgroup exceeding this number
1171 are silently ignored.  If it is nil, no article is ignored.  Note that
1172 setting this variable to a number might prevent you from reading very
1173 old articles."
1174   :group 'gnus-group-select
1175   :version "22.2"
1176   :type '(choice (const :tag "No limit" nil)
1177                  integer))
1178
1179 (gnus-define-group-parameter
1180  ignored-charsets
1181  :type list
1182  :function-document
1183  "Return the ignored charsets of GROUP."
1184  :variable gnus-group-ignored-charsets-alist
1185  :variable-default
1186  '(("alt\\.chinese\\.text" iso-8859-1))
1187  :variable-document
1188  "Alist of regexps (to match group names) and charsets that should be ignored.
1189 When these charsets are used in the \"charset\" parameter, the
1190 default charset will be used instead."
1191  :variable-group gnus-charset
1192  :variable-type '(repeat (cons (regexp :tag "Group")
1193                                (repeat symbol)))
1194  :parameter-type '(choice :tag "Ignored charsets"
1195                           :value nil
1196                           (repeat (symbol)))
1197  :parameter-document       "\
1198 List of charsets that should be ignored.
1199
1200 When these charsets are used in the \"charset\" parameter, the
1201 default charset will be used instead.")
1202
1203 (defcustom gnus-group-highlight-words-alist nil
1204   "Alist of group regexps and highlight regexps.
1205 This variable uses the same syntax as `gnus-emphasis-alist'."
1206   :version "21.1"
1207   :type '(repeat (cons (regexp :tag "Group")
1208                        (repeat (list (regexp :tag "Highlight regexp")
1209                                      (number :tag "Group for entire word" 0)
1210                                      (number :tag "Group for displayed part" 0)
1211                                      (symbol :tag "Face"
1212                                              gnus-emphasis-highlight-words)))))
1213   :group 'gnus-summary-visual)
1214
1215 (defcustom gnus-summary-show-article-charset-alist
1216   nil
1217   "Alist of number and charset.
1218 The article will be shown with the charset corresponding to the
1219 numbered argument.
1220 For example: ((1 . cn-gb-2312) (2 . big5))."
1221   :version "21.1"
1222   :type '(repeat (cons (number :tag "Argument" 1)
1223                        (symbol :tag "Charset")))
1224   :group 'gnus-charset)
1225
1226 (defcustom gnus-preserve-marks t
1227   "Whether marks are preserved when moving, copying and respooling messages."
1228   :version "21.1"
1229   :type 'boolean
1230   :group 'gnus-summary-marks)
1231
1232 (defcustom gnus-propagate-marks t
1233   "If non-nil, do not propagate marks to the backends."
1234   :version "23.1" ;; No Gnus
1235   :type 'boolean
1236   :group 'gnus-summary-marks)
1237
1238 (defcustom gnus-alter-articles-to-read-function nil
1239   "Function to be called to alter the list of articles to be selected."
1240   :type '(choice (const nil) function)
1241   :group 'gnus-summary)
1242
1243 (defcustom gnus-orphan-score nil
1244   "*All orphans get this score added.  Set in the score file."
1245   :group 'gnus-score-default
1246   :type '(choice (const nil)
1247                  integer))
1248
1249 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1250   "*A regexp to match MIME parts when saving multiple parts of a
1251 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1252 This regexp will be used by default when prompting the user for which
1253 type of files to save."
1254   :group 'gnus-summary
1255   :type 'regexp)
1256
1257 (defcustom gnus-read-all-available-headers nil
1258   "Whether Gnus should parse all headers made available to it.
1259 This is mostly relevant for slow back ends where the user may
1260 wish to widen the summary buffer to include all headers
1261 that were fetched."
1262   :version "22.1"
1263   :group 'gnus-summary
1264   :type '(choice boolean regexp))
1265
1266 (defcustom gnus-summary-pipe-output-default-command nil
1267   "Command (and optional arguments) used to pipe article to subprocess.
1268 This will be used as the default command if it is non-nil.  The value
1269 will be updated if you modify it when executing the command
1270 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1271   :version "23.1" ;; No Gnus
1272   :group 'gnus-summary
1273   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1274
1275 (defcustom gnus-summary-muttprint-program "muttprint"
1276   "Command (and optional arguments) used to run Muttprint.
1277 The value will be updated if you modify it when executing the command
1278 `gnus-summary-muttprint'."
1279   :version "22.1"
1280   :group 'gnus-summary
1281   :type 'string)
1282
1283 (defcustom gnus-article-loose-mime t
1284   "If non-nil, don't require MIME-Version header.
1285 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1286 supply the MIME-Version header or deliberately strip it from the mail.
1287 If non-nil (the default), Gnus will treat some articles as MIME
1288 even if the MIME-Version header is missing."
1289   :version "22.1"
1290   :type 'boolean
1291   :group 'gnus-article-mime)
1292
1293 (defcustom gnus-article-emulate-mime t
1294   "If non-nil, use MIME emulation for uuencode and the like.
1295 This means that Gnus will search message bodies for text that look
1296 like uuencoded bits, yEncoded bits, and so on, and present that using
1297 the normal Gnus MIME machinery."
1298   :version "22.1"
1299   :type 'boolean
1300   :group 'gnus-article-mime)
1301
1302 ;;; Internal variables
1303
1304 (defvar gnus-summary-display-cache nil)
1305 (defvar gnus-article-mime-handles nil)
1306 (defvar gnus-article-decoded-p nil)
1307 (defvar gnus-article-charset nil)
1308 (defvar gnus-article-ignored-charsets nil)
1309 (defvar gnus-scores-exclude-files nil)
1310 (defvar gnus-page-broken nil)
1311
1312 (defvar gnus-original-article nil)
1313 (defvar gnus-article-internal-prepare-hook nil)
1314 (defvar gnus-newsgroup-process-stack nil)
1315
1316 (defvar gnus-thread-indent-array nil)
1317 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1318 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1319   "Function called to sort the articles within a thread after it has been gathered together.")
1320
1321 (defvar gnus-summary-save-parts-type-history nil)
1322 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1323
1324 ;; Avoid highlighting in kill files.
1325 (defvar gnus-summary-inhibit-highlight nil)
1326 (defvar gnus-newsgroup-selected-overlay nil)
1327 (defvar gnus-inhibit-limiting nil)
1328 (defvar gnus-newsgroup-adaptive-score-file nil)
1329 (defvar gnus-current-score-file nil)
1330 (defvar gnus-current-move-group nil)
1331 (defvar gnus-current-copy-group nil)
1332 (defvar gnus-current-crosspost-group nil)
1333 (defvar gnus-newsgroup-display nil)
1334
1335 (defvar gnus-newsgroup-dependencies nil)
1336 (defvar gnus-newsgroup-adaptive nil)
1337 (defvar gnus-summary-display-article-function nil)
1338 (defvar gnus-summary-highlight-line-function nil
1339   "Function called after highlighting a summary line.")
1340
1341 (defvar gnus-summary-line-format-alist
1342   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1343     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1344     (?s gnus-tmp-subject-or-nil ?s)
1345     (?n gnus-tmp-name ?s)
1346     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1347         ?s)
1348     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1349             gnus-tmp-from) ?s)
1350     (?F gnus-tmp-from ?s)
1351     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1352     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1353     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1354     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1355     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1356     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1357     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1358     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1359     (?L gnus-tmp-lines ?s)
1360     (?O gnus-tmp-downloaded ?c)
1361     (?I gnus-tmp-indentation ?s)
1362     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1363     (?R gnus-tmp-replied ?c)
1364     (?\[ gnus-tmp-opening-bracket ?c)
1365     (?\] gnus-tmp-closing-bracket ?c)
1366     (?\> (make-string gnus-tmp-level ? ) ?s)
1367     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1368     (?i gnus-tmp-score ?d)
1369     (?z gnus-tmp-score-char ?c)
1370     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1371     (?U gnus-tmp-unread ?c)
1372     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1373         ?s)
1374     (?t (gnus-summary-number-of-articles-in-thread
1375          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1376         ?d)
1377     (?e (gnus-summary-number-of-articles-in-thread
1378          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1379         ?c)
1380     (?u gnus-tmp-user-defined ?s)
1381     (?P (gnus-pick-line-number) ?d)
1382     (?B gnus-tmp-thread-tree-header-string ?s)
1383     (user-date (gnus-user-date
1384                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1385   "An alist of format specifications that can appear in summary lines.
1386 These are paired with what variables they correspond with, along with
1387 the type of the variable (string, integer, character, etc).")
1388
1389 (defvar gnus-summary-dummy-line-format-alist
1390   `((?S gnus-tmp-subject ?s)
1391     (?N gnus-tmp-number ?d)
1392     (?u gnus-tmp-user-defined ?s)))
1393
1394 (defvar gnus-summary-mode-line-format-alist
1395   `((?G gnus-tmp-group-name ?s)
1396     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1397     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1398     (?A gnus-tmp-article-number ?d)
1399     (?Z gnus-tmp-unread-and-unselected ?s)
1400     (?V gnus-version ?s)
1401     (?U gnus-tmp-unread-and-unticked ?d)
1402     (?S gnus-tmp-subject ?s)
1403     (?e gnus-tmp-unselected ?d)
1404     (?u gnus-tmp-user-defined ?s)
1405     (?d (length gnus-newsgroup-dormant) ?d)
1406     (?t (length gnus-newsgroup-marked) ?d)
1407     (?h (length gnus-newsgroup-spam-marked) ?d)
1408     (?r (length gnus-newsgroup-reads) ?d)
1409     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1410     (?E gnus-newsgroup-expunged-tally ?d)
1411     (?s (gnus-current-score-file-nondirectory) ?s)))
1412
1413 ;; This is here rather than in gnus-art for compilation reasons.
1414 (defvar gnus-article-mode-line-format-alist
1415   (nconc '((?w (gnus-article-wash-status) ?s)
1416            (?m (gnus-article-mime-part-status) ?s))
1417          gnus-summary-mode-line-format-alist))
1418
1419 (defvar gnus-last-search-regexp nil
1420   "Default regexp for article search command.")
1421
1422 (defvar gnus-last-shell-command nil
1423   "Default shell command on article.")
1424
1425 (defvar gnus-newsgroup-agentized nil
1426   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1427 (defvar gnus-newsgroup-begin nil)
1428 (defvar gnus-newsgroup-end nil)
1429 (defvar gnus-newsgroup-last-rmail nil)
1430 (defvar gnus-newsgroup-last-mail nil)
1431 (defvar gnus-newsgroup-last-folder nil)
1432 (defvar gnus-newsgroup-last-file nil)
1433 (defvar gnus-newsgroup-last-directory nil)
1434 (defvar gnus-newsgroup-auto-expire nil)
1435 (defvar gnus-newsgroup-active nil)
1436
1437 (defvar gnus-newsgroup-data nil)
1438 (defvar gnus-newsgroup-data-reverse nil)
1439 (defvar gnus-newsgroup-limit nil)
1440 (defvar gnus-newsgroup-limits nil)
1441 (defvar gnus-summary-use-undownloaded-faces nil)
1442
1443 (defvar gnus-newsgroup-unreads nil
1444   "Sorted list of unread articles in the current newsgroup.")
1445
1446 (defvar gnus-newsgroup-unselected nil
1447   "Sorted list of unselected unread articles in the current newsgroup.")
1448
1449 (defvar gnus-newsgroup-reads nil
1450   "Alist of read articles and article marks in the current newsgroup.")
1451
1452 (defvar gnus-newsgroup-expunged-tally nil)
1453
1454 (defvar gnus-newsgroup-marked nil
1455   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1456
1457 (defvar gnus-newsgroup-spam-marked nil
1458   "List of ranges of articles that have been marked as spam.")
1459
1460 (defvar gnus-newsgroup-killed nil
1461   "List of ranges of articles that have been through the scoring process.")
1462
1463 (defvar gnus-newsgroup-cached nil
1464   "Sorted list of articles that come from the article cache.")
1465
1466 (defvar gnus-newsgroup-saved nil
1467   "List of articles that have been saved.")
1468
1469 (defvar gnus-newsgroup-kill-headers nil)
1470
1471 (defvar gnus-newsgroup-replied nil
1472   "List of articles that have been replied to in the current newsgroup.")
1473
1474 (defvar gnus-newsgroup-forwarded nil
1475   "List of articles that have been forwarded in the current newsgroup.")
1476
1477 (defvar gnus-newsgroup-recent nil
1478   "List of articles that have are recent in the current newsgroup.")
1479
1480 (defvar gnus-newsgroup-expirable nil
1481   "Sorted list of articles in the current newsgroup that can be expired.")
1482
1483 (defvar gnus-newsgroup-processable nil
1484   "List of articles in the current newsgroup that can be processed.")
1485
1486 (defvar gnus-newsgroup-downloadable nil
1487   "Sorted list of articles in the current newsgroup that can be processed.")
1488
1489 (defvar gnus-newsgroup-unfetched nil
1490   "Sorted list of articles in the current newsgroup whose headers have
1491 not been fetched into the agent.
1492
1493 This list will always be a subset of gnus-newsgroup-undownloaded.")
1494
1495 (defvar gnus-newsgroup-undownloaded nil
1496   "List of articles in the current newsgroup that haven't been downloaded.")
1497
1498 (defvar gnus-newsgroup-unsendable nil
1499   "List of articles in the current newsgroup that won't be sent.")
1500
1501 (defvar gnus-newsgroup-bookmarks nil
1502   "List of articles in the current newsgroup that have bookmarks.")
1503
1504 (defvar gnus-newsgroup-dormant nil
1505   "Sorted list of dormant articles in the current newsgroup.")
1506
1507 (defvar gnus-newsgroup-unseen nil
1508   "List of unseen articles in the current newsgroup.")
1509
1510 (defvar gnus-newsgroup-seen nil
1511   "Range of seen articles in the current newsgroup.")
1512
1513 (defvar gnus-newsgroup-articles nil
1514   "List of articles in the current newsgroup.")
1515
1516 (defvar gnus-newsgroup-scored nil
1517   "List of scored articles in the current newsgroup.")
1518
1519 (defvar gnus-newsgroup-headers nil
1520   "List of article headers in the current newsgroup.")
1521
1522 (defvar gnus-newsgroup-threads nil)
1523
1524 (defvar gnus-newsgroup-prepared nil
1525   "Whether the current group has been prepared properly.")
1526
1527 (defvar gnus-newsgroup-ancient nil
1528   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1529
1530 (defvar gnus-newsgroup-sparse nil)
1531
1532 (defvar gnus-current-article nil)
1533 (defvar gnus-article-current nil)
1534 (defvar gnus-current-headers nil)
1535 (defvar gnus-have-all-headers nil)
1536 (defvar gnus-last-article nil)
1537 (defvar gnus-newsgroup-history nil)
1538 (defvar gnus-newsgroup-charset nil)
1539 (defvar gnus-newsgroup-ephemeral-charset nil)
1540 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1541
1542 (defvar gnus-article-before-search nil)
1543
1544 (defvar gnus-summary-local-variables
1545   '(gnus-newsgroup-name
1546     gnus-newsgroup-begin gnus-newsgroup-end
1547     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1548     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1549     gnus-newsgroup-last-directory
1550     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1551     gnus-newsgroup-unselected gnus-newsgroup-marked
1552     gnus-newsgroup-spam-marked
1553     gnus-newsgroup-reads gnus-newsgroup-saved
1554     gnus-newsgroup-replied gnus-newsgroup-forwarded
1555     gnus-newsgroup-recent
1556     gnus-newsgroup-expirable
1557     gnus-newsgroup-processable gnus-newsgroup-killed
1558     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1559     gnus-newsgroup-unfetched
1560     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1561     gnus-newsgroup-seen gnus-newsgroup-articles
1562     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1563     gnus-newsgroup-headers gnus-newsgroup-threads
1564     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1565     gnus-current-article gnus-current-headers gnus-have-all-headers
1566     gnus-last-article gnus-article-internal-prepare-hook
1567     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1568     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1569     gnus-thread-expunge-below
1570     gnus-score-alist gnus-current-score-file
1571     (gnus-summary-expunge-below . global)
1572     (gnus-summary-mark-below . global)
1573     (gnus-orphan-score . global)
1574     gnus-newsgroup-active gnus-scores-exclude-files
1575     gnus-newsgroup-history gnus-newsgroup-ancient
1576     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1577     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1578     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1579     (gnus-newsgroup-expunged-tally . 0)
1580     gnus-cache-removable-articles gnus-newsgroup-cached
1581     gnus-newsgroup-data gnus-newsgroup-data-reverse
1582     gnus-newsgroup-limit gnus-newsgroup-limits
1583     gnus-newsgroup-charset gnus-newsgroup-display
1584     gnus-summary-use-undownloaded-faces)
1585   "Variables that are buffer-local to the summary buffers.")
1586
1587 (defvar gnus-newsgroup-variables nil
1588   "A list of variables that have separate values in different newsgroups.
1589 A list of newsgroup (summary buffer) local variables, or cons of
1590 variables and their default expressions to be evalled (when the default
1591 values are not nil), that should be made global while the summary buffer
1592 is active.
1593
1594 Note: The default expressions will be evaluated (using function `eval')
1595 before assignment to the local variable rather than just assigned to it.
1596 If the default expression is the symbol `global', that symbol will not
1597 be evaluated but the global value of the local variable will be used
1598 instead.
1599
1600 These variables can be used to set variables in the group parameters
1601 while still allowing them to affect operations done in other buffers.
1602 For example:
1603
1604 \(setq gnus-newsgroup-variables
1605      '(message-use-followup-to
1606        (gnus-visible-headers .
1607          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1608 ")
1609
1610 (eval-when-compile
1611   ;; Bind features so that require will believe that gnus-sum has
1612   ;; already been loaded (avoids infinite recursion)
1613   (let ((features (cons 'gnus-sum features)))
1614     (require 'gnus-art)))
1615
1616 ;; MIME stuff.
1617
1618 (defvar gnus-decode-encoded-word-methods
1619   '(mail-decode-encoded-word-string)
1620   "List of methods used to decode encoded words.
1621
1622 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1623 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1624 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1625 whose names match REGEXP.
1626
1627 For example:
1628 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1629  mail-decode-encoded-word-string
1630  (\"chinese\" . rfc1843-decode-string))")
1631
1632 (defvar gnus-decode-encoded-word-methods-cache nil)
1633
1634 (defun gnus-multi-decode-encoded-word-string (string)
1635   "Apply the functions from `gnus-encoded-word-methods' that match."
1636   (unless (and gnus-decode-encoded-word-methods-cache
1637                (eq gnus-newsgroup-name
1638                    (car gnus-decode-encoded-word-methods-cache)))
1639     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1640     (dolist (method gnus-decode-encoded-word-methods)
1641       (if (symbolp method)
1642           (nconc gnus-decode-encoded-word-methods-cache (list method))
1643         (if (and gnus-newsgroup-name
1644                  (string-match (car method) gnus-newsgroup-name))
1645             (nconc gnus-decode-encoded-word-methods-cache
1646                    (list (cdr method)))))))
1647   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1648     (setq string (funcall method string))))
1649
1650 ;; Subject simplification.
1651
1652 (defun gnus-simplify-whitespace (str)
1653   "Remove excessive whitespace from STR."
1654   ;; Multiple spaces.
1655   (while (string-match "[ \t][ \t]+" str)
1656     (setq str (concat (substring str 0 (match-beginning 0))
1657                         " "
1658                         (substring str (match-end 0)))))
1659   ;; Leading spaces.
1660   (when (string-match "^[ \t]+" str)
1661     (setq str (substring str (match-end 0))))
1662   ;; Trailing spaces.
1663   (when (string-match "[ \t]+$" str)
1664     (setq str (substring str 0 (match-beginning 0))))
1665   str)
1666
1667 (defun gnus-simplify-all-whitespace (str)
1668   "Remove all whitespace from STR."
1669   (while (string-match "[ \t\n]+" str)
1670     (setq str (replace-match "" nil nil str)))
1671   str)
1672
1673 (defsubst gnus-simplify-subject-re (subject)
1674   "Remove \"Re:\" from subject lines."
1675   (if (string-match message-subject-re-regexp subject)
1676       (substring subject (match-end 0))
1677     subject))
1678
1679 (defun gnus-simplify-subject (subject &optional re-only)
1680   "Remove `Re:' and words in parentheses.
1681 If RE-ONLY is non-nil, strip leading `Re:'s only."
1682   (let ((case-fold-search t))           ;Ignore case.
1683     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1684     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1685       (setq subject (substring subject (match-end 0))))
1686     ;; Remove uninteresting prefixes.
1687     (when (and (not re-only)
1688                gnus-simplify-ignored-prefixes
1689                (string-match gnus-simplify-ignored-prefixes subject))
1690       (setq subject (substring subject (match-end 0))))
1691     ;; Remove words in parentheses from end.
1692     (unless re-only
1693       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1694         (setq subject (substring subject 0 (match-beginning 0)))))
1695     ;; Return subject string.
1696     subject))
1697
1698 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1699 ;; all whitespace.
1700 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1701   (goto-char (point-min))
1702   (while (re-search-forward regexp nil t)
1703     (replace-match (or newtext ""))))
1704
1705 (defun gnus-simplify-buffer-fuzzy ()
1706   "Simplify string in the buffer fuzzily.
1707 The string in the accessible portion of the current buffer is simplified.
1708 It is assumed to be a single-line subject.
1709 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1710 matter is removed.  Additional things can be deleted by setting
1711 `gnus-simplify-subject-fuzzy-regexp'."
1712   (let ((case-fold-search t)
1713         (modified-tick))
1714     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1715
1716     (while (not (eq modified-tick (buffer-modified-tick)))
1717       (setq modified-tick (buffer-modified-tick))
1718       (cond
1719        ((listp gnus-simplify-subject-fuzzy-regexp)
1720         (mapc 'gnus-simplify-buffer-fuzzy-step
1721               gnus-simplify-subject-fuzzy-regexp))
1722        (gnus-simplify-subject-fuzzy-regexp
1723         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1724       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1725       (gnus-simplify-buffer-fuzzy-step
1726        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1727       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1728
1729     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1730     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1731     (gnus-simplify-buffer-fuzzy-step " $")
1732     (gnus-simplify-buffer-fuzzy-step "^ +")))
1733
1734 (defun gnus-simplify-subject-fuzzy (subject)
1735   "Simplify a subject string fuzzily.
1736 See `gnus-simplify-buffer-fuzzy' for details."
1737   (save-excursion
1738     (gnus-set-work-buffer)
1739     (let ((case-fold-search t))
1740       ;; Remove uninteresting prefixes.
1741       (when (and gnus-simplify-ignored-prefixes
1742                  (string-match gnus-simplify-ignored-prefixes subject))
1743         (setq subject (substring subject (match-end 0))))
1744       (insert subject)
1745       (inline (gnus-simplify-buffer-fuzzy))
1746       (buffer-string))))
1747
1748 (defsubst gnus-simplify-subject-fully (subject)
1749   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1750   (cond
1751    (gnus-simplify-subject-functions
1752     (gnus-map-function gnus-simplify-subject-functions subject))
1753    ((null gnus-summary-gather-subject-limit)
1754     (gnus-simplify-subject-re subject))
1755    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1756     (gnus-simplify-subject-fuzzy subject))
1757    ((numberp gnus-summary-gather-subject-limit)
1758     (truncate-string-to-width (gnus-simplify-subject-re subject)
1759                               gnus-summary-gather-subject-limit))
1760    (t
1761     subject)))
1762
1763 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1764   "Check whether two subjects are equal.
1765 If optional argument SIMPLE-FIRST is t, first argument is already
1766 simplified."
1767   (cond
1768    ((null simple-first)
1769     (equal (gnus-simplify-subject-fully s1)
1770            (gnus-simplify-subject-fully s2)))
1771    (t
1772     (equal s1
1773            (gnus-simplify-subject-fully s2)))))
1774
1775 (defun gnus-summary-bubble-group ()
1776   "Increase the score of the current group.
1777 This is a handy function to add to `gnus-summary-exit-hook' to
1778 increase the score of each group you read."
1779   (gnus-group-add-score gnus-newsgroup-name))
1780
1781 \f
1782 ;;;
1783 ;;; Gnus summary mode
1784 ;;;
1785
1786 (put 'gnus-summary-mode 'mode-class 'special)
1787
1788 (defvar gnus-article-commands-menu)
1789
1790 ;; Non-orthogonal keys
1791
1792 (gnus-define-keys gnus-summary-mode-map
1793   " " gnus-summary-next-page
1794   "\177" gnus-summary-prev-page
1795   [delete] gnus-summary-prev-page
1796   [backspace] gnus-summary-prev-page
1797   "\r" gnus-summary-scroll-up
1798   "\M-\r" gnus-summary-scroll-down
1799   "n" gnus-summary-next-unread-article
1800   "p" gnus-summary-prev-unread-article
1801   "N" gnus-summary-next-article
1802   "P" gnus-summary-prev-article
1803   "\M-\C-n" gnus-summary-next-same-subject
1804   "\M-\C-p" gnus-summary-prev-same-subject
1805   "\M-n" gnus-summary-next-unread-subject
1806   "\M-p" gnus-summary-prev-unread-subject
1807   "." gnus-summary-first-unread-article
1808   "," gnus-summary-best-unread-article
1809   "\M-s" gnus-summary-search-article-forward
1810   "\M-r" gnus-summary-search-article-backward
1811   "\M-S" gnus-summary-repeat-search-article-forward
1812   "\M-R" gnus-summary-repeat-search-article-backward
1813   "<" gnus-summary-beginning-of-article
1814   ">" gnus-summary-end-of-article
1815   "j" gnus-summary-goto-article
1816   "^" gnus-summary-refer-parent-article
1817   "\M-^" gnus-summary-refer-article
1818   "u" gnus-summary-tick-article-forward
1819   "!" gnus-summary-tick-article-forward
1820   "U" gnus-summary-tick-article-backward
1821   "d" gnus-summary-mark-as-read-forward
1822   "D" gnus-summary-mark-as-read-backward
1823   "E" gnus-summary-mark-as-expirable
1824   "\M-u" gnus-summary-clear-mark-forward
1825   "\M-U" gnus-summary-clear-mark-backward
1826   "k" gnus-summary-kill-same-subject-and-select
1827   "\C-k" gnus-summary-kill-same-subject
1828   "\M-\C-k" gnus-summary-kill-thread
1829   "\M-\C-l" gnus-summary-lower-thread
1830   "e" gnus-summary-edit-article
1831   "#" gnus-summary-mark-as-processable
1832   "\M-#" gnus-summary-unmark-as-processable
1833   "\M-\C-t" gnus-summary-toggle-threads
1834   "\M-\C-s" gnus-summary-show-thread
1835   "\M-\C-h" gnus-summary-hide-thread
1836   "\M-\C-f" gnus-summary-next-thread
1837   "\M-\C-b" gnus-summary-prev-thread
1838   [(meta down)] gnus-summary-next-thread
1839   [(meta up)] gnus-summary-prev-thread
1840   "\M-\C-u" gnus-summary-up-thread
1841   "\M-\C-d" gnus-summary-down-thread
1842   "&" gnus-summary-execute-command
1843   "c" gnus-summary-catchup-and-exit
1844   "\C-w" gnus-summary-mark-region-as-read
1845   "\C-t" gnus-summary-toggle-truncation
1846   "?" gnus-summary-mark-as-dormant
1847   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1848   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1849   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1850   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1851   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1852   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1853   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1854   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1855   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1856   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1857   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1858   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1859   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1860   "=" gnus-summary-expand-window
1861   "\C-x\C-s" gnus-summary-reselect-current-group
1862   "\M-g" gnus-summary-rescan-group
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   "x" gnus-summary-limit-to-unread
1885   "s" gnus-summary-isearch-article
1886   "t" gnus-summary-toggle-header
1887   "g" gnus-summary-show-article
1888   "l" gnus-summary-goto-last-article
1889   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1890   "\C-d" gnus-summary-enter-digest-group
1891   "\M-\C-d" gnus-summary-read-document
1892   "\M-\C-e" gnus-summary-edit-parameters
1893   "\M-\C-a" gnus-summary-customize-parameters
1894   "\C-c\C-b" gnus-bug
1895   "*" gnus-cache-enter-article
1896   "\M-*" gnus-cache-remove-article
1897   "\M-&" gnus-summary-universal-argument
1898   "\C-l" gnus-recenter
1899   "I" gnus-summary-increase-score
1900   "L" gnus-summary-lower-score
1901   "\M-i" gnus-symbolic-argument
1902   "h" gnus-summary-select-article-buffer
1903
1904   "b" gnus-article-view-part
1905   "\M-t" gnus-summary-toggle-display-buttonized
1906
1907   "V" gnus-summary-score-map
1908   "X" gnus-uu-extract-map
1909   "S" gnus-summary-send-map)
1910
1911 ;; Sort of orthogonal keymap
1912 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1913   "t" gnus-summary-tick-article-forward
1914   "!" gnus-summary-tick-article-forward
1915   "d" gnus-summary-mark-as-read-forward
1916   "r" gnus-summary-mark-as-read-forward
1917   "c" gnus-summary-clear-mark-forward
1918   " " gnus-summary-clear-mark-forward
1919   "e" gnus-summary-mark-as-expirable
1920   "x" gnus-summary-mark-as-expirable
1921   "?" gnus-summary-mark-as-dormant
1922   "b" gnus-summary-set-bookmark
1923   "B" gnus-summary-remove-bookmark
1924   "#" gnus-summary-mark-as-processable
1925   "\M-#" gnus-summary-unmark-as-processable
1926   "S" gnus-summary-limit-include-expunged
1927   "C" gnus-summary-catchup
1928   "H" gnus-summary-catchup-to-here
1929   "h" gnus-summary-catchup-from-here
1930   "\C-c" gnus-summary-catchup-all
1931   "k" gnus-summary-kill-same-subject-and-select
1932   "K" gnus-summary-kill-same-subject
1933   "P" gnus-uu-mark-map)
1934
1935 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1936   "c" gnus-summary-clear-above
1937   "u" gnus-summary-tick-above
1938   "m" gnus-summary-mark-above
1939   "k" gnus-summary-kill-below)
1940
1941 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1942   "/" gnus-summary-limit-to-subject
1943   "n" gnus-summary-limit-to-articles
1944   "b" gnus-summary-limit-to-bodies
1945   "h" gnus-summary-limit-to-headers
1946   "w" gnus-summary-pop-limit
1947   "s" gnus-summary-limit-to-subject
1948   "a" gnus-summary-limit-to-author
1949   "u" gnus-summary-limit-to-unread
1950   "m" gnus-summary-limit-to-marks
1951   "M" gnus-summary-limit-exclude-marks
1952   "v" gnus-summary-limit-to-score
1953   "*" gnus-summary-limit-include-cached
1954   "D" gnus-summary-limit-include-dormant
1955   "T" gnus-summary-limit-include-thread
1956   "d" gnus-summary-limit-exclude-dormant
1957   "t" gnus-summary-limit-to-age
1958   "." gnus-summary-limit-to-unseen
1959   "x" gnus-summary-limit-to-extra
1960   "p" gnus-summary-limit-to-display-predicate
1961   "E" gnus-summary-limit-include-expunged
1962   "c" gnus-summary-limit-exclude-childless-dormant
1963   "C" gnus-summary-limit-mark-excluded-as-read
1964   "o" gnus-summary-insert-old-articles
1965   "N" gnus-summary-insert-new-articles
1966   "S" gnus-summary-limit-to-singletons
1967   "r" gnus-summary-limit-to-replied
1968   "R" gnus-summary-limit-to-recipient
1969   "A" gnus-summary-limit-to-address)
1970
1971 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1972   "n" gnus-summary-next-unread-article
1973   "p" gnus-summary-prev-unread-article
1974   "N" gnus-summary-next-article
1975   "P" gnus-summary-prev-article
1976   "\C-n" gnus-summary-next-same-subject
1977   "\C-p" gnus-summary-prev-same-subject
1978   "\M-n" gnus-summary-next-unread-subject
1979   "\M-p" gnus-summary-prev-unread-subject
1980   "f" gnus-summary-first-unread-article
1981   "b" gnus-summary-best-unread-article
1982   "j" gnus-summary-goto-article
1983   "g" gnus-summary-goto-subject
1984   "l" gnus-summary-goto-last-article
1985   "o" gnus-summary-pop-article)
1986
1987 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1988   "k" gnus-summary-kill-thread
1989   "E" gnus-summary-expire-thread
1990   "l" gnus-summary-lower-thread
1991   "i" gnus-summary-raise-thread
1992   "T" gnus-summary-toggle-threads
1993   "t" gnus-summary-rethread-current
1994   "^" gnus-summary-reparent-thread
1995   "\M-^" gnus-summary-reparent-children
1996   "s" gnus-summary-show-thread
1997   "S" gnus-summary-show-all-threads
1998   "h" gnus-summary-hide-thread
1999   "H" gnus-summary-hide-all-threads
2000   "n" gnus-summary-next-thread
2001   "p" gnus-summary-prev-thread
2002   "u" gnus-summary-up-thread
2003   "o" gnus-summary-top-thread
2004   "d" gnus-summary-down-thread
2005   "#" gnus-uu-mark-thread
2006   "\M-#" gnus-uu-unmark-thread)
2007
2008 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2009   "g" gnus-summary-prepare
2010   "c" gnus-summary-insert-cached-articles
2011   "d" gnus-summary-insert-dormant-articles
2012   "t" gnus-summary-insert-ticked-articles)
2013
2014 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2015   "c" gnus-summary-catchup-and-exit
2016   "C" gnus-summary-catchup-all-and-exit
2017   "E" gnus-summary-exit-no-update
2018   "Q" gnus-summary-exit
2019   "Z" gnus-summary-exit
2020   "n" gnus-summary-catchup-and-goto-next-group
2021   "p" gnus-summary-catchup-and-goto-prev-group
2022   "R" gnus-summary-reselect-current-group
2023   "G" gnus-summary-rescan-group
2024   "N" gnus-summary-next-group
2025   "s" gnus-summary-save-newsrc
2026   "P" gnus-summary-prev-group)
2027
2028 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2029   " " gnus-summary-next-page
2030   "n" gnus-summary-next-page
2031   "\177" gnus-summary-prev-page
2032   [delete] gnus-summary-prev-page
2033   "p" gnus-summary-prev-page
2034   "\r" gnus-summary-scroll-up
2035   "\M-\r" gnus-summary-scroll-down
2036   "<" gnus-summary-beginning-of-article
2037   ">" gnus-summary-end-of-article
2038   "b" gnus-summary-beginning-of-article
2039   "e" gnus-summary-end-of-article
2040   "^" gnus-summary-refer-parent-article
2041   "r" gnus-summary-refer-parent-article
2042   "D" gnus-summary-enter-digest-group
2043   "R" gnus-summary-refer-references
2044   "T" gnus-summary-refer-thread
2045   "g" gnus-summary-show-article
2046   "s" gnus-summary-isearch-article
2047   "P" gnus-summary-print-article
2048   "S" gnus-sticky-article
2049   "M" gnus-mailing-list-insinuate
2050   "t" gnus-article-babel)
2051
2052 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2053   "b" gnus-article-add-buttons
2054   "B" gnus-article-add-buttons-to-head
2055   "o" gnus-article-treat-overstrike
2056   "e" gnus-article-emphasize
2057   "w" gnus-article-fill-cited-article
2058   "Q" gnus-article-fill-long-lines
2059   "L" gnus-article-toggle-truncate-lines
2060   "C" gnus-article-capitalize-sentences
2061   "c" gnus-article-remove-cr
2062   "q" gnus-article-de-quoted-unreadable
2063   "6" gnus-article-de-base64-unreadable
2064   "Z" gnus-article-decode-HZ
2065   "A" gnus-article-treat-ansi-sequences
2066   "h" gnus-article-wash-html
2067   "u" gnus-article-unsplit-urls
2068   "s" gnus-summary-force-verify-and-decrypt
2069   "f" gnus-article-display-x-face
2070   "l" gnus-summary-stop-page-breaking
2071   "r" gnus-summary-caesar-message
2072   "m" gnus-summary-morse-message
2073   "t" gnus-summary-toggle-header
2074   "g" gnus-treat-smiley
2075   "v" gnus-summary-verbose-headers
2076   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2077   "p" gnus-article-verify-x-pgp-sig
2078   "d" gnus-article-treat-dumbquotes
2079   "i" gnus-summary-idna-message)
2080
2081 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2082   ;; mnemonic: deuglif*Y*
2083   "u" gnus-article-outlook-unwrap-lines
2084   "a" gnus-article-outlook-repair-attribution
2085   "c" gnus-article-outlook-rearrange-citation
2086   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2087
2088 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2089   "a" gnus-article-hide
2090   "h" gnus-article-hide-headers
2091   "b" gnus-article-hide-boring-headers
2092   "s" gnus-article-hide-signature
2093   "c" gnus-article-hide-citation
2094   "C" gnus-article-hide-citation-in-followups
2095   "l" gnus-article-hide-list-identifiers
2096   "B" gnus-article-strip-banner
2097   "P" gnus-article-hide-pem
2098   "\C-c" gnus-article-hide-citation-maybe)
2099
2100 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2101   "a" gnus-article-highlight
2102   "h" gnus-article-highlight-headers
2103   "c" gnus-article-highlight-citation
2104   "s" gnus-article-highlight-signature)
2105
2106 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2107   "f" gnus-article-treat-fold-headers
2108   "u" gnus-article-treat-unfold-headers
2109   "n" gnus-article-treat-fold-newsgroups)
2110
2111 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2112   "x" gnus-article-display-x-face
2113   "d" gnus-article-display-face
2114   "s" gnus-treat-smiley
2115   "D" gnus-article-remove-images
2116   "f" gnus-treat-from-picon
2117   "m" gnus-treat-mail-picon
2118   "n" gnus-treat-newsgroups-picon)
2119
2120 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2121   "w" gnus-article-decode-mime-words
2122   "c" gnus-article-decode-charset
2123   "v" gnus-mime-view-all-parts
2124   "b" gnus-article-view-part)
2125
2126 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2127   "z" gnus-article-date-ut
2128   "u" gnus-article-date-ut
2129   "l" gnus-article-date-local
2130   "p" gnus-article-date-english
2131   "e" gnus-article-date-lapsed
2132   "o" gnus-article-date-original
2133   "i" gnus-article-date-iso8601
2134   "s" gnus-article-date-user)
2135
2136 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2137   "t" gnus-article-remove-trailing-blank-lines
2138   "l" gnus-article-strip-leading-blank-lines
2139   "m" gnus-article-strip-multiple-blank-lines
2140   "a" gnus-article-strip-blank-lines
2141   "A" gnus-article-strip-all-blank-lines
2142   "s" gnus-article-strip-leading-space
2143   "e" gnus-article-strip-trailing-space
2144   "w" gnus-article-remove-leading-whitespace)
2145
2146 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2147   "v" gnus-version
2148   "f" gnus-summary-fetch-faq
2149   "d" gnus-summary-describe-group
2150   "h" gnus-summary-describe-briefly
2151   "i" gnus-info-find-node
2152   "c" gnus-group-fetch-charter
2153   "C" gnus-group-fetch-control)
2154
2155 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2156   "e" gnus-summary-expire-articles
2157   "\M-\C-e" gnus-summary-expire-articles-now
2158   "\177" gnus-summary-delete-article
2159   [delete] gnus-summary-delete-article
2160   [backspace] gnus-summary-delete-article
2161   "m" gnus-summary-move-article
2162   "r" gnus-summary-respool-article
2163   "w" gnus-summary-edit-article
2164   "c" gnus-summary-copy-article
2165   "B" gnus-summary-crosspost-article
2166   "q" gnus-summary-respool-query
2167   "t" gnus-summary-respool-trace
2168   "i" gnus-summary-import-article
2169   "I" gnus-summary-create-article
2170   "p" gnus-summary-article-posted-p)
2171
2172 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2173   "o" gnus-summary-save-article
2174   "m" gnus-summary-save-article-mail
2175   "F" gnus-summary-write-article-file
2176   "r" gnus-summary-save-article-rmail
2177   "f" gnus-summary-save-article-file
2178   "b" gnus-summary-save-article-body-file
2179   "B" gnus-summary-write-article-body-file
2180   "h" gnus-summary-save-article-folder
2181   "v" gnus-summary-save-article-vm
2182   "p" gnus-summary-pipe-output
2183   "P" gnus-summary-muttprint
2184   "s" gnus-soup-add-article)
2185
2186 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2187   "b" gnus-summary-display-buttonized
2188   "m" gnus-summary-repair-multipart
2189   "v" gnus-article-view-part
2190   "o" gnus-article-save-part
2191   "O" gnus-article-save-part-and-strip
2192   "r" gnus-article-replace-part
2193   "d" gnus-article-delete-part
2194   "t" gnus-article-view-part-as-type
2195   "j" gnus-article-jump-to-part
2196   "c" gnus-article-copy-part
2197   "C" gnus-article-view-part-as-charset
2198   "e" gnus-article-view-part-externally
2199   "H" gnus-article-browse-html-article
2200   "E" gnus-article-encrypt-body
2201   "i" gnus-article-inline-part
2202   "|" gnus-article-pipe-part)
2203
2204 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2205   "p" gnus-summary-mark-as-processable
2206   "u" gnus-summary-unmark-as-processable
2207   "U" gnus-summary-unmark-all-processable
2208   "v" gnus-uu-mark-over
2209   "s" gnus-uu-mark-series
2210   "r" gnus-uu-mark-region
2211   "g" gnus-uu-unmark-region
2212   "R" gnus-uu-mark-by-regexp
2213   "G" gnus-uu-unmark-by-regexp
2214   "t" gnus-uu-mark-thread
2215   "T" gnus-uu-unmark-thread
2216   "a" gnus-uu-mark-all
2217   "b" gnus-uu-mark-buffer
2218   "S" gnus-uu-mark-sparse
2219   "k" gnus-summary-kill-process-mark
2220   "y" gnus-summary-yank-process-mark
2221   "w" gnus-summary-save-process-mark
2222   "i" gnus-uu-invert-processable)
2223
2224 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2225   ;;"x" gnus-uu-extract-any
2226   "m" gnus-summary-save-parts
2227   "u" gnus-uu-decode-uu
2228   "U" gnus-uu-decode-uu-and-save
2229   "s" gnus-uu-decode-unshar
2230   "S" gnus-uu-decode-unshar-and-save
2231   "o" gnus-uu-decode-save
2232   "O" gnus-uu-decode-save
2233   "b" gnus-uu-decode-binhex
2234   "B" gnus-uu-decode-binhex
2235   "Y" gnus-uu-decode-yenc
2236   "p" gnus-uu-decode-postscript
2237   "P" gnus-uu-decode-postscript-and-save)
2238
2239 (gnus-define-keys
2240     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2241   "u" gnus-uu-decode-uu-view
2242   "U" gnus-uu-decode-uu-and-save-view
2243   "s" gnus-uu-decode-unshar-view
2244   "S" gnus-uu-decode-unshar-and-save-view
2245   "o" gnus-uu-decode-save-view
2246   "O" gnus-uu-decode-save-view
2247   "b" gnus-uu-decode-binhex-view
2248   "B" gnus-uu-decode-binhex-view
2249   "p" gnus-uu-decode-postscript-view
2250   "P" gnus-uu-decode-postscript-and-save-view)
2251
2252 (defvar gnus-article-post-menu nil)
2253
2254 (defconst gnus-summary-menu-maxlen 20)
2255
2256 (defun gnus-summary-menu-split (menu)
2257   ;; If we have lots of elements, divide them into groups of 20
2258   ;; and make a pane (or submenu) for each one.
2259   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2260       (let ((menu menu) sublists next
2261             (i 1))
2262         (while menu
2263           ;; Pull off the next gnus-summary-menu-maxlen elements
2264           ;; and make them the next element of sublist.
2265           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2266           (if next
2267               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2268                       nil))
2269           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2270                                              (aref (car (last menu)) 0)) menu)
2271                                sublists))
2272           (setq i (1+ i))
2273           (setq menu next))
2274         (nreverse sublists))
2275     ;; Few elements--put them all in one pane.
2276     menu))
2277
2278 (defun gnus-summary-make-menu-bar ()
2279   (gnus-turn-off-edit-menu 'summary)
2280
2281   (unless (boundp 'gnus-summary-misc-menu)
2282
2283     (easy-menu-define
2284       gnus-summary-kill-menu gnus-summary-mode-map ""
2285       (cons
2286        "Score"
2287        (nconc
2288         (list
2289          ["Customize" gnus-score-customize t])
2290         (gnus-make-score-map 'increase)
2291         (gnus-make-score-map 'lower)
2292         '(("Mark"
2293            ["Kill below" gnus-summary-kill-below t]
2294            ["Mark above" gnus-summary-mark-above t]
2295            ["Tick above" gnus-summary-tick-above t]
2296            ["Clear above" gnus-summary-clear-above t])
2297           ["Current score" gnus-summary-current-score t]
2298           ["Set score" gnus-summary-set-score t]
2299           ["Switch current score file..." gnus-score-change-score-file t]
2300           ["Set mark below..." gnus-score-set-mark-below t]
2301           ["Set expunge below..." gnus-score-set-expunge-below t]
2302           ["Edit current score file" gnus-score-edit-current-scores t]
2303           ["Edit score file..." gnus-score-edit-file t]
2304           ["Trace score" gnus-score-find-trace t]
2305           ["Find words" gnus-score-find-favourite-words t]
2306           ["Rescore buffer" gnus-summary-rescore t]
2307           ["Increase score..." gnus-summary-increase-score t]
2308           ["Lower score..." gnus-summary-lower-score t]))))
2309
2310     ;; Define both the Article menu in the summary buffer and the
2311     ;; equivalent Commands menu in the article buffer here for
2312     ;; consistency.
2313     (let ((innards
2314            `(("Hide"
2315               ["All" gnus-article-hide t]
2316               ["Headers" gnus-article-hide-headers t]
2317               ["Signature" gnus-article-hide-signature t]
2318               ["Citation" gnus-article-hide-citation t]
2319               ["List identifiers" gnus-article-hide-list-identifiers t]
2320               ["Banner" gnus-article-strip-banner t]
2321               ["Boring headers" gnus-article-hide-boring-headers t])
2322              ("Highlight"
2323               ["All" gnus-article-highlight t]
2324               ["Headers" gnus-article-highlight-headers t]
2325               ["Signature" gnus-article-highlight-signature t]
2326               ["Citation" gnus-article-highlight-citation t])
2327              ("MIME"
2328               ["Words" gnus-article-decode-mime-words t]
2329               ["Charset" gnus-article-decode-charset t]
2330               ["QP" gnus-article-de-quoted-unreadable t]
2331               ["Base64" gnus-article-de-base64-unreadable t]
2332               ["View MIME buttons" gnus-summary-display-buttonized t]
2333               ["View all" gnus-mime-view-all-parts t]
2334               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2335               ["Encrypt body" gnus-article-encrypt-body
2336                :active (not (gnus-group-read-only-p))
2337                ,@(if (featurep 'xemacs) nil
2338                    '(:help "Encrypt the message body on disk"))]
2339               ["Extract all parts..." gnus-summary-save-parts t]
2340               ("Multipart"
2341                ["Repair multipart" gnus-summary-repair-multipart t]
2342                ["Pipe part..." gnus-article-pipe-part t]
2343                ["Inline part" gnus-article-inline-part t]
2344                ["View part as type..." gnus-article-view-part-as-type t]
2345                ["Encrypt body" gnus-article-encrypt-body
2346                 :active (not (gnus-group-read-only-p))
2347                ,@(if (featurep 'xemacs) nil
2348                    '(:help "Encrypt the message body on disk"))]
2349                ["View part externally" gnus-article-view-part-externally t]
2350                ["View HTML parts in browser" gnus-article-browse-html-article t]
2351                ["View part with charset..." gnus-article-view-part-as-charset t]
2352                ["Copy part" gnus-article-copy-part t]
2353                ["Save part..." gnus-article-save-part t]
2354                ["View part" gnus-article-view-part t]))
2355              ("Date"
2356               ["Local" gnus-article-date-local t]
2357               ["ISO8601" gnus-article-date-iso8601 t]
2358               ["UT" gnus-article-date-ut t]
2359               ["Original" gnus-article-date-original t]
2360               ["Lapsed" gnus-article-date-lapsed t]
2361               ["User-defined" gnus-article-date-user t])
2362              ("Display"
2363               ["Remove images" gnus-article-remove-images t]
2364               ["Toggle smiley" gnus-treat-smiley t]
2365               ["Show X-Face" gnus-article-display-x-face t]
2366               ["Show picons in From" gnus-treat-from-picon t]
2367               ["Show picons in mail headers" gnus-treat-mail-picon t]
2368               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2369               ("View as different encoding"
2370                ,@(gnus-summary-menu-split
2371                   (mapcar
2372                    (lambda (cs)
2373                      ;; Since easymenu under Emacs doesn't allow
2374                      ;; lambda forms for menu commands, we should
2375                      ;; provide intern'ed function symbols.
2376                      (let ((command (intern (format "\
2377 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2378                        (fset command
2379                              `(lambda ()
2380                                 (interactive)
2381                                 (let ((gnus-summary-show-article-charset-alist
2382                                        '((1 . ,cs))))
2383                                   (gnus-summary-show-article 1))))
2384                        `[,(symbol-name cs) ,command t]))
2385                    (sort (if (fboundp 'coding-system-list)
2386                              (coding-system-list)
2387                            (mapcar 'car mm-mime-mule-charset-alist))
2388                          'string<)))))
2389              ("Washing"
2390               ("Remove Blanks"
2391                ["Leading" gnus-article-strip-leading-blank-lines t]
2392                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2393                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2394                ["All of the above" gnus-article-strip-blank-lines t]
2395                ["All" gnus-article-strip-all-blank-lines t]
2396                ["Leading space" gnus-article-strip-leading-space t]
2397                ["Trailing space" gnus-article-strip-trailing-space t]
2398                ["Leading space in headers"
2399                 gnus-article-remove-leading-whitespace t])
2400               ["Overstrike" gnus-article-treat-overstrike t]
2401               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2402               ["Emphasis" gnus-article-emphasize t]
2403               ["Word wrap" gnus-article-fill-cited-article t]
2404               ["Fill long lines" gnus-article-fill-long-lines t]
2405               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2406               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2407               ["Remove CR" gnus-article-remove-cr t]
2408               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2409               ["Base64" gnus-article-de-base64-unreadable t]
2410               ["Rot 13" gnus-summary-caesar-message
2411                ,@(if (featurep 'xemacs) '(t)
2412                    '(:help "\"Caesar rotate\" article by 13"))]
2413               ["De-IDNA" gnus-summary-idna-message t]
2414               ["Morse decode" gnus-summary-morse-message t]
2415               ["Unix pipe..." gnus-summary-pipe-message t]
2416               ["Add buttons" gnus-article-add-buttons t]
2417               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2418               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2419               ["Verbose header" gnus-summary-verbose-headers t]
2420               ["Toggle header" gnus-summary-toggle-header t]
2421               ["Unfold headers" gnus-article-treat-unfold-headers t]
2422               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2423               ["Html" gnus-article-wash-html t]
2424               ["Unsplit URLs" gnus-article-unsplit-urls t]
2425               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2426               ["Decode HZ" gnus-article-decode-HZ t]
2427               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2428               ("(Outlook) Deuglify"
2429                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2430                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2431                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2432                ["Full (Outlook) deuglify"
2433                 gnus-article-outlook-deuglify-article t])
2434               )
2435              ("Output"
2436               ["Save in default format..." gnus-summary-save-article
2437                ,@(if (featurep 'xemacs) '(t)
2438                    '(:help "Save article using default method"))]
2439               ["Save in file..." gnus-summary-save-article-file
2440                ,@(if (featurep 'xemacs) '(t)
2441                    '(:help "Save article in file"))]
2442               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2443               ["Save in MH folder..." gnus-summary-save-article-folder t]
2444               ["Save in VM folder..." gnus-summary-save-article-vm t]
2445               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2446               ["Save body in file..." gnus-summary-save-article-body-file t]
2447               ["Pipe through a filter..." gnus-summary-pipe-output t]
2448               ["Add to SOUP packet" gnus-soup-add-article t]
2449               ["Print with Muttprint..." gnus-summary-muttprint t]
2450               ["Print" gnus-summary-print-article
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "Generate and print a PostScript image"))])
2453              ("Copy, move,... (Backend)"
2454               ,@(if (featurep 'xemacs) nil
2455                   '(:help "Copying, moving, expiring articles..."))
2456               ["Respool article..." gnus-summary-respool-article t]
2457               ["Move article..." gnus-summary-move-article
2458                (gnus-check-backend-function
2459                 'request-move-article gnus-newsgroup-name)]
2460               ["Copy article..." gnus-summary-copy-article t]
2461               ["Crosspost article..." gnus-summary-crosspost-article
2462                (gnus-check-backend-function
2463                 'request-replace-article gnus-newsgroup-name)]
2464               ["Import file..." gnus-summary-import-article
2465                (gnus-check-backend-function
2466                 'request-accept-article gnus-newsgroup-name)]
2467               ["Create article..." gnus-summary-create-article
2468                (gnus-check-backend-function
2469                 'request-accept-article gnus-newsgroup-name)]
2470               ["Check if posted" gnus-summary-article-posted-p t]
2471               ["Edit article" gnus-summary-edit-article
2472                (not (gnus-group-read-only-p))]
2473               ["Delete article" gnus-summary-delete-article
2474                (gnus-check-backend-function
2475                 'request-expire-articles gnus-newsgroup-name)]
2476               ["Query respool" gnus-summary-respool-query t]
2477               ["Trace respool" gnus-summary-respool-trace t]
2478               ["Delete expirable articles" gnus-summary-expire-articles-now
2479                (gnus-check-backend-function
2480                 'request-expire-articles gnus-newsgroup-name)])
2481              ("Extract"
2482               ["Uudecode" gnus-uu-decode-uu
2483                ,@(if (featurep 'xemacs) '(t)
2484                    '(:help "Decode uuencoded article(s)"))]
2485               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2486               ["Unshar" gnus-uu-decode-unshar t]
2487               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2488               ["Save" gnus-uu-decode-save t]
2489               ["Binhex" gnus-uu-decode-binhex t]
2490               ["Postscript" gnus-uu-decode-postscript t]
2491               ["All MIME parts" gnus-summary-save-parts t])
2492              ("Cache"
2493               ["Enter article" gnus-cache-enter-article t]
2494               ["Remove article" gnus-cache-remove-article t])
2495              ["Translate" gnus-article-babel t]
2496              ["Select article buffer" gnus-summary-select-article-buffer t]
2497              ["Make article buffer sticky" gnus-sticky-article t]
2498              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2499              ["Isearch article..." gnus-summary-isearch-article t]
2500              ["Beginning of the article" gnus-summary-beginning-of-article t]
2501              ["End of the article" gnus-summary-end-of-article t]
2502              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2503              ["Fetch referenced articles" gnus-summary-refer-references t]
2504              ["Fetch current thread" gnus-summary-refer-thread t]
2505              ["Fetch article with id..." gnus-summary-refer-article t]
2506              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2507              ["Redisplay" gnus-summary-show-article t]
2508              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2509       (easy-menu-define
2510         gnus-summary-article-menu gnus-summary-mode-map ""
2511         (cons "Article" innards))
2512
2513       (if (not (keymapp gnus-summary-article-menu))
2514           (easy-menu-define
2515             gnus-article-commands-menu gnus-article-mode-map ""
2516             (cons "Commands" innards))
2517         ;; in Emacs, don't share menu.
2518         (setq gnus-article-commands-menu
2519               (copy-keymap gnus-summary-article-menu))
2520         (define-key gnus-article-mode-map [menu-bar commands]
2521           (cons "Commands" gnus-article-commands-menu))))
2522
2523     (easy-menu-define
2524       gnus-summary-thread-menu gnus-summary-mode-map ""
2525       '("Threads"
2526         ["Find all messages in thread" gnus-summary-refer-thread t]
2527         ["Toggle threading" gnus-summary-toggle-threads t]
2528         ["Hide threads" gnus-summary-hide-all-threads t]
2529         ["Show threads" gnus-summary-show-all-threads t]
2530         ["Hide thread" gnus-summary-hide-thread t]
2531         ["Show thread" gnus-summary-show-thread t]
2532         ["Go to next thread" gnus-summary-next-thread t]
2533         ["Go to previous thread" gnus-summary-prev-thread t]
2534         ["Go down thread" gnus-summary-down-thread t]
2535         ["Go up thread" gnus-summary-up-thread t]
2536         ["Top of thread" gnus-summary-top-thread t]
2537         ["Mark thread as read" gnus-summary-kill-thread t]
2538         ["Mark thread as expired" gnus-summary-expire-thread t]
2539         ["Lower thread score" gnus-summary-lower-thread t]
2540         ["Raise thread score" gnus-summary-raise-thread t]
2541         ["Rethread current" gnus-summary-rethread-current t]))
2542
2543     (easy-menu-define
2544       gnus-summary-post-menu gnus-summary-mode-map ""
2545       `("Post"
2546         ["Send a message (mail or news)" gnus-summary-post-news
2547          ,@(if (featurep 'xemacs) '(t)
2548              '(:help "Compose a new message (mail or news)"))]
2549         ["Followup" gnus-summary-followup
2550          ,@(if (featurep 'xemacs) '(t)
2551              '(:help "Post followup to this article"))]
2552         ["Followup and yank" gnus-summary-followup-with-original
2553          ,@(if (featurep 'xemacs) '(t)
2554              '(:help "Post followup to this article, quoting its contents"))]
2555         ["Supersede article" gnus-summary-supersede-article t]
2556         ["Cancel article" gnus-summary-cancel-article
2557          ,@(if (featurep 'xemacs) '(t)
2558              '(:help "Cancel an article you posted"))]
2559         ["Reply" gnus-summary-reply t]
2560         ["Reply and yank" gnus-summary-reply-with-original t]
2561         ["Wide reply" gnus-summary-wide-reply t]
2562         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2563          ,@(if (featurep 'xemacs) '(t)
2564              '(:help "Mail a reply, quoting this article"))]
2565         ["Very wide reply" gnus-summary-very-wide-reply t]
2566         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2567          ,@(if (featurep 'xemacs) '(t)
2568              '(:help "Mail a very wide reply, quoting this article"))]
2569         ["Mail forward" gnus-summary-mail-forward t]
2570         ["Post forward" gnus-summary-post-forward t]
2571         ["Digest and mail" gnus-uu-digest-mail-forward t]
2572         ["Digest and post" gnus-uu-digest-post-forward t]
2573         ["Resend message" gnus-summary-resend-message t]
2574         ["Resend message edit" gnus-summary-resend-message-edit t]
2575         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2576         ["Send a mail" gnus-summary-mail-other-window t]
2577         ["Create a local message" gnus-summary-news-other-window t]
2578         ["Uuencode and post" gnus-uu-post-news
2579          ,@(if (featurep 'xemacs) '(t)
2580              '(:help "Post a uuencoded article"))]
2581         ["Followup via news" gnus-summary-followup-to-mail t]
2582         ["Followup via news and yank"
2583          gnus-summary-followup-to-mail-with-original t]
2584         ["Strip signature on reply"
2585          (lambda ()
2586            (interactive)
2587            (if (not (memq message-cite-function
2588                           '(message-cite-original-without-signature
2589                             message-cite-original)))
2590                ;; Stupid workaround for XEmacs not honoring :visible.
2591                (message "Can't toggle this value of `message-cite-function'")
2592              (setq message-cite-function
2593                    (if (eq message-cite-function
2594                            'message-cite-original-without-signature)
2595                        'message-cite-original
2596                      'message-cite-original-without-signature))))
2597          ;; XEmacs barfs on :visible.
2598          ,@(if (featurep 'xemacs) nil
2599              '(:visible (memq message-cite-function
2600                               '(message-cite-original-without-signature
2601                                 message-cite-original))))
2602          :style toggle
2603          :selected (eq message-cite-function
2604                        'message-cite-original-without-signature)
2605          ,@(if (featurep 'xemacs) nil
2606              '(:help "Strip signature from cited article when replying."))]
2607         ;;("Draft"
2608         ;;["Send" gnus-summary-send-draft t]
2609         ;;["Send bounced" gnus-resend-bounced-mail t])
2610         ))
2611
2612     (cond
2613      ((not (keymapp gnus-summary-post-menu))
2614       (setq gnus-article-post-menu gnus-summary-post-menu))
2615      ((not gnus-article-post-menu)
2616       ;; Don't share post menu.
2617       (setq gnus-article-post-menu
2618             (copy-keymap gnus-summary-post-menu))))
2619     (define-key gnus-article-mode-map [menu-bar post]
2620       (cons "Post" gnus-article-post-menu))
2621
2622     (easy-menu-define
2623       gnus-summary-misc-menu gnus-summary-mode-map ""
2624       `("Gnus"
2625         ("Mark Read"
2626          ["Mark as read" gnus-summary-mark-as-read-forward t]
2627          ["Mark same subject and select"
2628           gnus-summary-kill-same-subject-and-select t]
2629          ["Mark same subject" gnus-summary-kill-same-subject t]
2630          ["Catchup" gnus-summary-catchup
2631           ,@(if (featurep 'xemacs) '(t)
2632               '(:help "Mark unread articles in this group as read"))]
2633          ["Catchup all" gnus-summary-catchup-all t]
2634          ["Catchup to here" gnus-summary-catchup-to-here t]
2635          ["Catchup from here" gnus-summary-catchup-from-here t]
2636          ["Catchup region" gnus-summary-mark-region-as-read
2637           (gnus-mark-active-p)]
2638          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2639         ("Mark Various"
2640          ["Tick" gnus-summary-tick-article-forward t]
2641          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2642          ["Remove marks" gnus-summary-clear-mark-forward t]
2643          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2644          ["Set bookmark" gnus-summary-set-bookmark t]
2645          ["Remove bookmark" gnus-summary-remove-bookmark t])
2646         ("Limit to"
2647          ["Marks..." gnus-summary-limit-to-marks t]
2648          ["Subject..." gnus-summary-limit-to-subject t]
2649          ["Author..." gnus-summary-limit-to-author t]
2650          ["Recipient..." gnus-summary-limit-to-recipient t]
2651          ["Address..." gnus-summary-limit-to-address t]
2652          ["Age..." gnus-summary-limit-to-age t]
2653          ["Extra..." gnus-summary-limit-to-extra t]
2654          ["Score..." gnus-summary-limit-to-score t]
2655          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2656          ["Unread" gnus-summary-limit-to-unread t]
2657          ["Unseen" gnus-summary-limit-to-unseen t]
2658          ["Singletons" gnus-summary-limit-to-singletons t]
2659          ["Replied" gnus-summary-limit-to-replied t]
2660          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2661          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2662          ["Pop limit" gnus-summary-pop-limit t]
2663          ["Show dormant" gnus-summary-limit-include-dormant t]
2664          ["Hide childless dormant"
2665           gnus-summary-limit-exclude-childless-dormant t]
2666          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2667          ["Hide marked" gnus-summary-limit-exclude-marks t]
2668          ["Show expunged" gnus-summary-limit-include-expunged t])
2669         ("Process Mark"
2670          ["Set mark" gnus-summary-mark-as-processable t]
2671          ["Remove mark" gnus-summary-unmark-as-processable t]
2672          ["Remove all marks" gnus-summary-unmark-all-processable t]
2673          ["Invert marks" gnus-uu-invert-processable t]
2674          ["Mark above" gnus-uu-mark-over t]
2675          ["Mark series" gnus-uu-mark-series t]
2676          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2677          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2678          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2679          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2680          ["Mark all" gnus-uu-mark-all t]
2681          ["Mark buffer" gnus-uu-mark-buffer t]
2682          ["Mark sparse" gnus-uu-mark-sparse t]
2683          ["Mark thread" gnus-uu-mark-thread t]
2684          ["Unmark thread" gnus-uu-unmark-thread t]
2685          ("Process Mark Sets"
2686           ["Kill" gnus-summary-kill-process-mark t]
2687           ["Yank" gnus-summary-yank-process-mark
2688            gnus-newsgroup-process-stack]
2689           ["Save" gnus-summary-save-process-mark t]
2690           ["Run command on marked..." gnus-summary-universal-argument t]))
2691         ("Registry Marks")
2692         ("Scroll article"
2693          ["Page forward" gnus-summary-next-page
2694           ,@(if (featurep 'xemacs) '(t)
2695               '(:help "Show next page of article"))]
2696          ["Page backward" gnus-summary-prev-page
2697           ,@(if (featurep 'xemacs) '(t)
2698               '(:help "Show previous page of article"))]
2699          ["Line forward" gnus-summary-scroll-up t])
2700         ("Move"
2701          ["Next unread article" gnus-summary-next-unread-article t]
2702          ["Previous unread article" gnus-summary-prev-unread-article t]
2703          ["Next article" gnus-summary-next-article t]
2704          ["Previous article" gnus-summary-prev-article t]
2705          ["Next unread subject" gnus-summary-next-unread-subject t]
2706          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2707          ["Next article same subject" gnus-summary-next-same-subject t]
2708          ["Previous article same subject" gnus-summary-prev-same-subject t]
2709          ["First unread article" gnus-summary-first-unread-article t]
2710          ["Best unread article" gnus-summary-best-unread-article t]
2711          ["Go to subject number..." gnus-summary-goto-subject t]
2712          ["Go to article number..." gnus-summary-goto-article t]
2713          ["Go to the last article" gnus-summary-goto-last-article t]
2714          ["Pop article off history" gnus-summary-pop-article t])
2715         ("Sort"
2716          ["Sort by number" gnus-summary-sort-by-number t]
2717          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2718          ["Sort by author" gnus-summary-sort-by-author t]
2719          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2720          ["Sort by subject" gnus-summary-sort-by-subject t]
2721          ["Sort by date" gnus-summary-sort-by-date t]
2722          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2723          ["Sort by score" gnus-summary-sort-by-score t]
2724          ["Sort by lines" gnus-summary-sort-by-lines t]
2725          ["Sort by characters" gnus-summary-sort-by-chars t]
2726          ["Randomize" gnus-summary-sort-by-random t]
2727          ["Original sort" gnus-summary-sort-by-original t])
2728         ("Help"
2729          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2730          ["Describe group" gnus-summary-describe-group t]
2731          ["Fetch charter" gnus-group-fetch-charter
2732           ,@(if (featurep 'xemacs) nil
2733               '(:help "Display the charter of the current group"))]
2734          ["Fetch control message" gnus-group-fetch-control
2735           ,@(if (featurep 'xemacs) nil
2736               '(:help "Display the archived control message for the current group"))]
2737          ["Read manual" gnus-info-find-node t])
2738         ("Modes"
2739          ["Pick and read" gnus-pick-mode t]
2740          ["Binary" gnus-binary-mode t])
2741         ("Regeneration"
2742          ["Regenerate" gnus-summary-prepare t]
2743          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2744          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2745          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2746          ["Toggle threading" gnus-summary-toggle-threads t])
2747         ["See old articles" gnus-summary-insert-old-articles t]
2748         ["See new articles" gnus-summary-insert-new-articles t]
2749         ["Filter articles..." gnus-summary-execute-command t]
2750         ["Run command on articles..." gnus-summary-universal-argument t]
2751         ["Search articles forward..." gnus-summary-search-article-forward t]
2752         ["Search articles backward..." gnus-summary-search-article-backward t]
2753         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2754         ["Expand window" gnus-summary-expand-window t]
2755         ["Expire expirable articles" gnus-summary-expire-articles
2756          (gnus-check-backend-function
2757           'request-expire-articles gnus-newsgroup-name)]
2758         ["Edit local kill file" gnus-summary-edit-local-kill t]
2759         ["Edit main kill file" gnus-summary-edit-global-kill t]
2760         ["Edit group parameters" gnus-summary-edit-parameters t]
2761         ["Customize group parameters" gnus-summary-customize-parameters t]
2762         ["Send a bug report" gnus-bug t]
2763         ("Exit"
2764          ["Catchup and exit" gnus-summary-catchup-and-exit
2765           ,@(if (featurep 'xemacs) '(t)
2766               '(:help "Mark unread articles in this group as read, then exit"))]
2767          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2768          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2769          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2770          ["Exit group" gnus-summary-exit
2771           ,@(if (featurep 'xemacs) '(t)
2772               '(:help "Exit current group, return to group selection mode"))]
2773          ["Exit group without updating" gnus-summary-exit-no-update t]
2774          ["Exit and goto next group" gnus-summary-next-group t]
2775          ["Exit and goto prev group" gnus-summary-prev-group t]
2776          ["Reselect group" gnus-summary-reselect-current-group t]
2777          ["Rescan group" gnus-summary-rescan-group t]
2778          ["Update dribble" gnus-summary-save-newsrc t])))
2779
2780     (gnus-run-hooks 'gnus-summary-menu-hook)))
2781
2782 (defvar gnus-summary-tool-bar-map nil)
2783
2784 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2785 ;; affect _new_ message buffers.  We might add a function that walks thru all
2786 ;; summary-mode buffers and force the update.
2787 (defun gnus-summary-tool-bar-update (&optional symbol value)
2788   "Update summary mode toolbar.
2789 Setter function for custom variables."
2790   (setq-default gnus-summary-tool-bar-map nil)
2791   (when symbol
2792     ;; When used as ":set" function:
2793     (set-default symbol value))
2794   (when (gnus-buffer-live-p gnus-summary-buffer)
2795     (with-current-buffer gnus-summary-buffer
2796       (gnus-summary-make-tool-bar))))
2797
2798 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2799                                      'gnus-summary-tool-bar-gnome
2800                                    'gnus-summary-tool-bar-retro)
2801   "Specifies the Gnus summary tool bar.
2802
2803 It can be either a list or a symbol refering to a list.  See
2804 `gmm-tool-bar-from-list' for the format of the list.  The
2805 default key map is `gnus-summary-mode-map'.
2806
2807 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2808 `gnus-summary-tool-bar-retro'."
2809   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2810                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2811                  (repeat :tag "User defined list" gmm-tool-bar-item)
2812                  (symbol))
2813   :version "23.1" ;; No Gnus
2814   :initialize 'custom-initialize-default
2815   :set 'gnus-summary-tool-bar-update
2816   :group 'gnus-summary)
2817
2818 (defcustom gnus-summary-tool-bar-gnome
2819   '((gnus-summary-post-news "mail/compose" nil)
2820     (gnus-summary-insert-new-articles "mail/inbox" nil
2821                                       :visible (or (not gnus-agent)
2822                                                    gnus-plugged))
2823     (gnus-summary-reply-with-original "mail/reply")
2824     (gnus-summary-reply "mail/reply" nil :visible nil)
2825     (gnus-summary-followup-with-original "mail/reply-all")
2826     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2827     (gnus-summary-mail-forward "mail/forward")
2828     (gnus-summary-save-article "mail/save")
2829     (gnus-summary-search-article-forward "search" nil :visible nil)
2830     (gnus-summary-print-article "print")
2831     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2832     ;; Some new commands that may need more suitable icons:
2833     (gnus-summary-save-newsrc "save" nil :visible nil)
2834     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2835     (gnus-summary-prev-article "left-arrow")
2836     (gnus-summary-next-article "right-arrow")
2837     (gnus-summary-next-page "next-page")
2838     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2839     ;;
2840     ;; Maybe some sort-by-... could be added:
2841     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2842     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2843     (gnus-summary-mark-as-expirable
2844      "delete" nil
2845      :visible (gnus-check-backend-function 'request-expire-articles
2846                                            gnus-newsgroup-name))
2847     (gnus-summary-mark-as-spam
2848      "mail/spam" t
2849      :visible (and (fboundp 'spam-group-ham-contents-p)
2850                    (spam-group-ham-contents-p gnus-newsgroup-name))
2851      :help "Mark as spam")
2852     (gnus-summary-mark-as-read-forward
2853      "mail/not-spam" nil
2854      :visible (and (fboundp 'spam-group-spam-contents-p)
2855                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2856     ;;
2857     (gnus-summary-exit "exit")
2858     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2859     (gnus-info-find-node "help"))
2860   "List of functions for the summary tool bar (GNOME style).
2861
2862 See `gmm-tool-bar-from-list' for the format of the list."
2863   :type '(repeat gmm-tool-bar-item)
2864   :version "23.1" ;; No Gnus
2865   :initialize 'custom-initialize-default
2866   :set 'gnus-summary-tool-bar-update
2867   :group 'gnus-summary)
2868
2869 (defcustom gnus-summary-tool-bar-retro
2870   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2871     (gnus-summary-next-unread-article "gnus/next-ur")
2872     (gnus-summary-post-news "gnus/post")
2873     (gnus-summary-followup-with-original "gnus/fuwo")
2874     (gnus-summary-followup "gnus/followup")
2875     (gnus-summary-reply-with-original "gnus/reply-wo")
2876     (gnus-summary-reply "gnus/reply")
2877     (gnus-summary-caesar-message "gnus/rot13")
2878     (gnus-uu-decode-uu "gnus/uu-decode")
2879     (gnus-summary-save-article-file "gnus/save-aif")
2880     (gnus-summary-save-article "gnus/save-art")
2881     (gnus-uu-post-news "gnus/uu-post")
2882     (gnus-summary-catchup "gnus/catchup")
2883     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2884     (gnus-summary-exit "gnus/exit-summ")
2885     ;; Some new command that may need more suitable icons:
2886     (gnus-summary-print-article "gnus/print" nil :visible nil)
2887     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2888     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2889     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2890     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2891     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2892     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2893     ;;
2894     (gnus-info-find-node "gnus/help" nil :visible nil))
2895   "List of functions for the summary tool bar (retro look).
2896
2897 See `gmm-tool-bar-from-list' for the format of the list."
2898   :type '(repeat gmm-tool-bar-item)
2899   :version "23.1" ;; No Gnus
2900   :initialize 'custom-initialize-default
2901   :set 'gnus-summary-tool-bar-update
2902   :group 'gnus-summary)
2903
2904 (defcustom gnus-summary-tool-bar-zap-list t
2905   "List of icon items from the global tool bar.
2906 These items are not displayed in the Gnus summary mode tool bar.
2907
2908 See `gmm-tool-bar-from-list' for the format of the list."
2909   :type 'gmm-tool-bar-zap-list
2910   :version "23.1" ;; No Gnus
2911   :initialize 'custom-initialize-default
2912   :set 'gnus-summary-tool-bar-update
2913   :group 'gnus-summary)
2914
2915 (defvar image-load-path)
2916 (defvar tool-bar-map)
2917
2918 (defun gnus-summary-make-tool-bar (&optional force)
2919   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2920 When FORCE, rebuild the tool bar."
2921   (when (and (not (featurep 'xemacs))
2922              (boundp 'tool-bar-mode)
2923              tool-bar-mode
2924              (or (not gnus-summary-tool-bar-map) force))
2925     (let* ((load-path
2926             (gmm-image-load-path-for-library "gnus"
2927                                              "mail/save.xpm"
2928                                              nil t))
2929            (image-load-path (cons (car load-path)
2930                                   (when (boundp 'image-load-path)
2931                                     image-load-path)))
2932            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2933                                         gnus-summary-tool-bar-zap-list
2934                                         'gnus-summary-mode-map)))
2935       (when map
2936         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2937         ;; uses it's value.
2938         (setq gnus-summary-tool-bar-map map))))
2939   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2940
2941 (defun gnus-score-set-default (var value)
2942   "A version of set that updates the GNU Emacs menu-bar."
2943   (set var value)
2944   ;; It is the message that forces the active status to be updated.
2945   (message ""))
2946
2947 (defun gnus-make-score-map (type)
2948   "Make a summary score map of type TYPE."
2949   (if t
2950       nil
2951     (let ((headers '(("author" "from" string)
2952                      ("subject" "subject" string)
2953                      ("article body" "body" string)
2954                      ("article head" "head" string)
2955                      ("xref" "xref" string)
2956                      ("extra header" "extra" string)
2957                      ("lines" "lines" number)
2958                      ("followups to author" "followup" string)))
2959           (types '((number ("less than" <)
2960                            ("greater than" >)
2961                            ("equal" =))
2962                    (string ("substring" s)
2963                            ("exact string" e)
2964                            ("fuzzy string" f)
2965                            ("regexp" r))))
2966           (perms '(("temporary" (current-time-string))
2967                    ("permanent" nil)
2968                    ("immediate" now)))
2969           header)
2970       (list
2971        (apply
2972         'nconc
2973         (list
2974          (if (eq type 'lower)
2975              "Lower score"
2976            "Increase score"))
2977         (let (outh)
2978           (while headers
2979             (setq header (car headers))
2980             (setq outh
2981                   (cons
2982                    (apply
2983                     'nconc
2984                     (list (car header))
2985                     (let ((ts (cdr (assoc (nth 2 header) types)))
2986                           outt)
2987                       (while ts
2988                         (setq outt
2989                               (cons
2990                                (apply
2991                                 'nconc
2992                                 (list (caar ts))
2993                                 (let ((ps perms)
2994                                       outp)
2995                                   (while ps
2996                                     (setq outp
2997                                           (cons
2998                                            (vector
2999                                             (caar ps)
3000                                             (list
3001                                              'gnus-summary-score-entry
3002                                              (nth 1 header)
3003                                              (if (or (string= (nth 1 header)
3004                                                               "head")
3005                                                      (string= (nth 1 header)
3006                                                               "body"))
3007                                                  ""
3008                                                (list 'gnus-summary-header
3009                                                      (nth 1 header)))
3010                                              (list 'quote (nth 1 (car ts)))
3011                                              (list 'gnus-score-delta-default
3012                                                    nil)
3013                                              (nth 1 (car ps))
3014                                              t)
3015                                             t)
3016                                            outp))
3017                                     (setq ps (cdr ps)))
3018                                   (list (nreverse outp))))
3019                                outt))
3020                         (setq ts (cdr ts)))
3021                       (list (nreverse outt))))
3022                    outh))
3023             (setq headers (cdr headers)))
3024           (list (nreverse outh))))))))
3025
3026
3027 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3028 (defvar bookmark-make-record-function)
3029 \f
3030
3031 (defun gnus-summary-mode (&optional group)
3032   "Major mode for reading articles.
3033
3034 All normal editing commands are switched off.
3035 \\<gnus-summary-mode-map>
3036 Each line in this buffer represents one article.  To read an
3037 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3038 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3039 respectively.
3040
3041 You can also post articles and send mail from this buffer.  To
3042 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3043 of an article, type `\\[gnus-summary-reply]'.
3044
3045 There are approx. one gazillion commands you can execute in this
3046 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3047
3048 The following commands are available:
3049
3050 \\{gnus-summary-mode-map}"
3051   (interactive)
3052   (kill-all-local-variables)
3053   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3054     (gnus-summary-make-local-variables))
3055   (gnus-summary-make-local-variables)
3056   (setq gnus-newsgroup-name group)
3057   (when (gnus-visual-p 'summary-menu 'menu)
3058     (gnus-summary-make-menu-bar)
3059     (gnus-summary-make-tool-bar))
3060   (gnus-make-thread-indent-array)
3061   (gnus-simplify-mode-line)
3062   (setq major-mode 'gnus-summary-mode)
3063   (setq mode-name "Summary")
3064   (use-local-map gnus-summary-mode-map)
3065   (buffer-disable-undo)
3066   (setq buffer-read-only t              ;Disable modification
3067         show-trailing-whitespace nil)
3068   (setq truncate-lines t)
3069   (add-to-invisibility-spec '(gnus-sum . t))
3070   (gnus-summary-set-display-table)
3071   (gnus-set-default-directory)
3072   (make-local-variable 'gnus-summary-line-format)
3073   (make-local-variable 'gnus-summary-line-format-spec)
3074   (make-local-variable 'gnus-summary-dummy-line-format)
3075   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3076   (make-local-variable 'gnus-summary-mark-positions)
3077   (gnus-make-local-hook 'pre-command-hook)
3078   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3079   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3080   (turn-on-gnus-mailing-list-mode)
3081   (mm-enable-multibyte)
3082   (set (make-local-variable 'bookmark-make-record-function)
3083        'gnus-summary-bookmark-make-record)
3084   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3085   (gnus-update-summary-mark-positions))
3086
3087 (defun gnus-summary-make-local-variables ()
3088   "Make all the local summary buffer variables."
3089   (let (global)
3090     (dolist (local gnus-summary-local-variables)
3091       (if (consp local)
3092           (progn
3093             (if (eq (cdr local) 'global)
3094                 ;; Copy the global value of the variable.
3095                 (setq global (symbol-value (car local)))
3096               ;; Use the value from the list.
3097               (setq global (eval (cdr local))))
3098             (set (make-local-variable (car local)) global))
3099         ;; Simple nil-valued local variable.
3100         (set (make-local-variable local) nil)))))
3101
3102 (defun gnus-summary-clear-local-variables ()
3103   (let ((locals gnus-summary-local-variables))
3104     (while locals
3105       (if (consp (car locals))
3106           (and (symbolp (caar locals))
3107                (set (caar locals) nil))
3108         (and (symbolp (car locals))
3109              (set (car locals) nil)))
3110       (setq locals (cdr locals)))))
3111
3112 ;; Summary data functions.
3113
3114 (defmacro gnus-data-number (data)
3115   `(car ,data))
3116
3117 (defmacro gnus-data-set-number (data number)
3118   `(setcar ,data ,number))
3119
3120 (defmacro gnus-data-mark (data)
3121   `(nth 1 ,data))
3122
3123 (defmacro gnus-data-set-mark (data mark)
3124   `(setcar (nthcdr 1 ,data) ,mark))
3125
3126 (defmacro gnus-data-pos (data)
3127   `(nth 2 ,data))
3128
3129 (defmacro gnus-data-set-pos (data pos)
3130   `(setcar (nthcdr 2 ,data) ,pos))
3131
3132 (defmacro gnus-data-header (data)
3133   `(nth 3 ,data))
3134
3135 (defmacro gnus-data-set-header (data header)
3136   `(setf (nth 3 ,data) ,header))
3137
3138 (defmacro gnus-data-level (data)
3139   `(nth 4 ,data))
3140
3141 (defmacro gnus-data-unread-p (data)
3142   `(= (nth 1 ,data) gnus-unread-mark))
3143
3144 (defmacro gnus-data-read-p (data)
3145   `(/= (nth 1 ,data) gnus-unread-mark))
3146
3147 (defmacro gnus-data-pseudo-p (data)
3148   `(consp (nth 3 ,data)))
3149
3150 (defmacro gnus-data-find (number)
3151   `(assq ,number gnus-newsgroup-data))
3152
3153 (defmacro gnus-data-find-list (number &optional data)
3154   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3155      (memq (assq ,number bdata)
3156            bdata)))
3157
3158 (defmacro gnus-data-make (number mark pos header level)
3159   `(list ,number ,mark ,pos ,header ,level))
3160
3161 (defun gnus-data-enter (after-article number mark pos header level offset)
3162   (let ((data (gnus-data-find-list after-article)))
3163     (unless data
3164       (error "No such article: %d" after-article))
3165     (setcdr data (cons (gnus-data-make number mark pos header level)
3166                        (cdr data)))
3167     (setq gnus-newsgroup-data-reverse nil)
3168     (gnus-data-update-list (cddr data) offset)))
3169
3170 (defun gnus-data-enter-list (after-article list &optional offset)
3171   (when list
3172     (let ((data (and after-article (gnus-data-find-list after-article)))
3173           (ilist list))
3174       (if (not (or data
3175                    after-article))
3176           (let ((odata gnus-newsgroup-data))
3177             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3178             (when offset
3179               (gnus-data-update-list odata offset)))
3180         ;; Find the last element in the list to be spliced into the main
3181         ;; list.
3182         (setq list (last list))
3183         (if (not data)
3184             (progn
3185               (setcdr list gnus-newsgroup-data)
3186               (setq gnus-newsgroup-data ilist)
3187               (when offset
3188                 (gnus-data-update-list (cdr list) offset)))
3189           (setcdr list (cdr data))
3190           (setcdr data ilist)
3191           (when offset
3192             (gnus-data-update-list (cdr list) offset))))
3193       (setq gnus-newsgroup-data-reverse nil))))
3194
3195 (defun gnus-data-remove (article &optional offset)
3196   (let ((data gnus-newsgroup-data))
3197     (if (= (gnus-data-number (car data)) article)
3198         (progn
3199           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3200                 gnus-newsgroup-data-reverse nil)
3201           (when offset
3202             (gnus-data-update-list gnus-newsgroup-data offset)))
3203       (while (cdr data)
3204         (when (= (gnus-data-number (cadr data)) article)
3205           (setcdr data (cddr data))
3206           (when offset
3207             (gnus-data-update-list (cdr data) offset))
3208           (setq data nil
3209                 gnus-newsgroup-data-reverse nil))
3210         (setq data (cdr data))))))
3211
3212 (defmacro gnus-data-list (backward)
3213   `(if ,backward
3214        (or gnus-newsgroup-data-reverse
3215            (setq gnus-newsgroup-data-reverse
3216                  (reverse gnus-newsgroup-data)))
3217      gnus-newsgroup-data))
3218
3219 (defun gnus-data-update-list (data offset)
3220   "Add OFFSET to the POS of all data entries in DATA."
3221   (setq gnus-newsgroup-data-reverse nil)
3222   (while data
3223     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3224     (setq data (cdr data))))
3225
3226 (defun gnus-summary-article-pseudo-p (article)
3227   "Say whether this article is a pseudo article or not."
3228   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3229
3230 (defmacro gnus-summary-article-sparse-p (article)
3231   "Say whether this article is a sparse article or not."
3232   `(memq ,article gnus-newsgroup-sparse))
3233
3234 (defmacro gnus-summary-article-ancient-p (article)
3235   "Say whether this article is a sparse article or not."
3236   `(memq ,article gnus-newsgroup-ancient))
3237
3238 (defun gnus-article-parent-p (number)
3239   "Say whether this article is a parent or not."
3240   (let ((data (gnus-data-find-list number)))
3241     (and (cdr data)              ; There has to be an article after...
3242          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3243             (gnus-data-level (nth 1 data))))))
3244
3245 (defun gnus-article-children (number)
3246   "Return a list of all children to NUMBER."
3247   (let* ((data (gnus-data-find-list number))
3248          (level (gnus-data-level (car data)))
3249          children)
3250     (setq data (cdr data))
3251     (while (and data
3252                 (= (gnus-data-level (car data)) (1+ level)))
3253       (push (gnus-data-number (car data)) children)
3254       (setq data (cdr data)))
3255     children))
3256
3257 (defmacro gnus-summary-skip-intangible ()
3258   "If the current article is intangible, then jump to a different article."
3259   '(let ((to (get-text-property (point) 'gnus-intangible)))
3260      (and to (gnus-summary-goto-subject to))))
3261
3262 (defmacro gnus-summary-article-intangible-p ()
3263   "Say whether this article is intangible or not."
3264   '(get-text-property (point) 'gnus-intangible))
3265
3266 (defun gnus-article-read-p (article)
3267   "Say whether ARTICLE is read or not."
3268   (not (or (memq article gnus-newsgroup-marked)
3269            (memq article gnus-newsgroup-spam-marked)
3270            (memq article gnus-newsgroup-unreads)
3271            (memq article gnus-newsgroup-unselected)
3272            (memq article gnus-newsgroup-dormant))))
3273
3274 ;; Some summary mode macros.
3275
3276 (defmacro gnus-summary-article-number ()
3277   "The article number of the article on the current line.
3278 If there isn't an article number here, then we return the current
3279 article number."
3280   '(progn
3281      (gnus-summary-skip-intangible)
3282      (or (get-text-property (point) 'gnus-number)
3283          (gnus-summary-last-subject))))
3284
3285 (defmacro gnus-summary-article-header (&optional number)
3286   "Return the header of article NUMBER."
3287   `(gnus-data-header (gnus-data-find
3288                       ,(or number '(gnus-summary-article-number)))))
3289
3290 (defmacro gnus-summary-thread-level (&optional number)
3291   "Return the level of thread that starts with article NUMBER."
3292   `(if (and (eq gnus-summary-make-false-root 'dummy)
3293             (get-text-property (point) 'gnus-intangible))
3294        0
3295      (gnus-data-level (gnus-data-find
3296                        ,(or number '(gnus-summary-article-number))))))
3297
3298 (defmacro gnus-summary-article-mark (&optional number)
3299   "Return the mark of article NUMBER."
3300   `(gnus-data-mark (gnus-data-find
3301                     ,(or number '(gnus-summary-article-number)))))
3302
3303 (defmacro gnus-summary-article-pos (&optional number)
3304   "Return the position of the line of article NUMBER."
3305   `(gnus-data-pos (gnus-data-find
3306                    ,(or number '(gnus-summary-article-number)))))
3307
3308 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3309 (defmacro gnus-summary-article-subject (&optional number)
3310   "Return current subject string or nil if nothing."
3311   `(let ((headers
3312           ,(if number
3313                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3314              '(gnus-data-header (assq (gnus-summary-article-number)
3315                                       gnus-newsgroup-data)))))
3316      (and headers
3317           (vectorp headers)
3318           (mail-header-subject headers))))
3319
3320 (defmacro gnus-summary-article-score (&optional number)
3321   "Return current article score."
3322   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3323                   gnus-newsgroup-scored))
3324        gnus-summary-default-score 0))
3325
3326 (defun gnus-summary-article-children (&optional number)
3327   "Return a list of article numbers that are children of article NUMBER."
3328   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3329          (level (gnus-data-level (car data)))
3330          l children)
3331     (while (and (setq data (cdr data))
3332                 (> (setq l (gnus-data-level (car data))) level))
3333       (and (= (1+ level) l)
3334            (push (gnus-data-number (car data))
3335                  children)))
3336     (nreverse children)))
3337
3338 (defun gnus-summary-article-parent (&optional number)
3339   "Return the article number of the parent of article NUMBER."
3340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3341                                     (gnus-data-list t)))
3342          (level (gnus-data-level (car data))))
3343     (if (zerop level)
3344         ()                              ; This is a root.
3345       ;; We search until we find an article with a level less than
3346       ;; this one.  That function has to be the parent.
3347       (while (and (setq data (cdr data))
3348                   (not (< (gnus-data-level (car data)) level))))
3349       (and data (gnus-data-number (car data))))))
3350
3351 (defun gnus-unread-mark-p (mark)
3352   "Say whether MARK is the unread mark."
3353   (= mark gnus-unread-mark))
3354
3355 (defun gnus-read-mark-p (mark)
3356   "Say whether MARK is one of the marks that mark as read.
3357 This is all marks except unread, ticked, dormant, and expirable."
3358   (not (or (= mark gnus-unread-mark)
3359            (= mark gnus-ticked-mark)
3360            (= mark gnus-spam-mark)
3361            (= mark gnus-dormant-mark)
3362            (= mark gnus-expirable-mark))))
3363
3364 (defmacro gnus-article-mark (number)
3365   "Return the MARK of article NUMBER.
3366 This macro should only be used when computing the mark the \"first\"
3367 time; i.e., when generating the summary lines.  After that,
3368 `gnus-summary-article-mark' should be used to examine the
3369 marks of articles."
3370   `(cond
3371     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3372     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3373     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3374     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3375     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3376     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3377     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3378     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3379            gnus-ancient-mark))))
3380
3381 ;; Saving hidden threads.
3382
3383 (defmacro gnus-save-hidden-threads (&rest forms)
3384   "Save hidden threads, eval FORMS, and restore the hidden threads."
3385   (let ((config (make-symbol "config")))
3386     `(let ((,config (gnus-hidden-threads-configuration)))
3387        (unwind-protect
3388            (save-excursion
3389              ,@forms)
3390          (gnus-restore-hidden-threads-configuration ,config)))))
3391 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3392 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3393
3394 (defun gnus-data-compute-positions ()
3395   "Compute the positions of all articles."
3396   (setq gnus-newsgroup-data-reverse nil)
3397   (let ((data gnus-newsgroup-data))
3398     (save-excursion
3399       (gnus-save-hidden-threads
3400         (gnus-summary-show-all-threads)
3401         (goto-char (point-min))
3402         (while data
3403           (while (get-text-property (point) 'gnus-intangible)
3404             (forward-line 1))
3405           (gnus-data-set-pos (car data) (+ (point) 3))
3406           (setq data (cdr data))
3407           (forward-line 1))))))
3408
3409 (defun gnus-hidden-threads-configuration ()
3410   "Return the current hidden threads configuration."
3411   (save-excursion
3412     (let (config)
3413       (goto-char (point-min))
3414       (while (not (eobp))
3415         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3416           (push (save-excursion (forward-line 0) (point)) config))
3417         (forward-line 1))
3418       config)))
3419
3420 (defun gnus-restore-hidden-threads-configuration (config)
3421   "Restore hidden threads configuration from CONFIG."
3422   (save-excursion
3423     (let (point (inhibit-read-only t))
3424       (while (setq point (pop config))
3425         (goto-char point)
3426         (gnus-summary-hide-thread)))))
3427
3428 ;; Various summary mode internalish functions.
3429
3430 (defun gnus-mouse-pick-article (e)
3431   (interactive "e")
3432   (mouse-set-point e)
3433   (gnus-summary-next-page nil t))
3434
3435 (defun gnus-summary-set-display-table ()
3436   "Change the display table.
3437 Odd characters have a tendency to mess
3438 up nicely formatted displays - we make all possible glyphs
3439 display only a single character."
3440
3441   ;; We start from the standard display table, if any.
3442   (let ((table (or (copy-sequence standard-display-table)
3443                    (make-display-table)))
3444         (i 32))
3445     ;; Nix out all the control chars...
3446     (while (>= (setq i (1- i)) 0)
3447       (gnus-put-display-table i [??] table))
3448    ;; ... but not newline and cr, of course.  (cr is necessary for the
3449     ;; selective display).
3450     (gnus-put-display-table ?\n nil table)
3451     (gnus-put-display-table ?\r nil table)
3452     ;; We keep TAB as well.
3453     (gnus-put-display-table ?\t nil table)
3454     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3455     ;; Emacs 23 (unicode), that are not set already.
3456     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3457                  160
3458                256)))
3459       (while (>= (setq i (1- i)) 127)
3460         ;; Only modify if the entry is nil.
3461         (unless (gnus-get-display-table i table)
3462           (gnus-put-display-table i [??] table))))
3463     (setq buffer-display-table table)))
3464
3465 (defun gnus-summary-set-article-display-arrow (pos)
3466   "Update the overlay arrow to point to line at position POS."
3467   (when gnus-summary-display-arrow
3468     (make-local-variable 'overlay-arrow-position)
3469     (make-local-variable 'overlay-arrow-string)
3470     (save-excursion
3471       (goto-char pos)
3472       (beginning-of-line)
3473       (unless overlay-arrow-position
3474         (setq overlay-arrow-position (make-marker)))
3475       (setq overlay-arrow-string "=>"
3476             overlay-arrow-position (set-marker overlay-arrow-position
3477                                                (point)
3478                                                (current-buffer))))))
3479
3480 (defun gnus-summary-setup-buffer (group)
3481   "Initialize summary buffer."
3482   (let ((buffer (gnus-summary-buffer-name group))
3483         (dead-name (concat "*Dead Summary "
3484                            (gnus-group-decoded-name group) "*")))
3485     ;; If a dead summary buffer exists, we kill it.
3486     (when (gnus-buffer-live-p dead-name)
3487       (gnus-kill-buffer dead-name))
3488     (if (get-buffer buffer)
3489         (progn
3490           (set-buffer buffer)
3491           (setq gnus-summary-buffer (current-buffer))
3492           (not gnus-newsgroup-prepared))
3493       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3494       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3495       (gnus-summary-mode group)
3496       (when gnus-carpal
3497         (gnus-carpal-setup-buffer 'summary))
3498       (when (gnus-group-quit-config group)
3499         (set (make-local-variable 'gnus-single-article-buffer) nil))
3500       (make-local-variable 'gnus-article-buffer)
3501       (make-local-variable 'gnus-article-current)
3502       (make-local-variable 'gnus-original-article-buffer)
3503       (setq gnus-newsgroup-name group)
3504       ;; Set any local variables in the group parameters.
3505       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3506       t)))
3507
3508 (defun gnus-set-global-variables ()
3509   "Set the global equivalents of the buffer-local variables.
3510 They are set to the latest values they had.  These reflect the summary
3511 buffer that was in action when the last article was fetched."
3512   (when (eq major-mode 'gnus-summary-mode)
3513     (setq gnus-summary-buffer (current-buffer))
3514     (let ((name gnus-newsgroup-name)
3515           (marked gnus-newsgroup-marked)
3516           (spam gnus-newsgroup-spam-marked)
3517           (unread gnus-newsgroup-unreads)
3518           (headers gnus-current-headers)
3519           (data gnus-newsgroup-data)
3520           (summary gnus-summary-buffer)
3521           (article-buffer gnus-article-buffer)
3522           (original gnus-original-article-buffer)
3523           (gac gnus-article-current)
3524           (reffed gnus-reffed-article-number)
3525           (score-file gnus-current-score-file)
3526           (default-charset gnus-newsgroup-charset)
3527           vlist)
3528       (let ((locals gnus-newsgroup-variables))
3529         (while locals
3530           (if (consp (car locals))
3531               (push (eval (caar locals)) vlist)
3532             (push (eval (car locals)) vlist))
3533           (setq locals (cdr locals)))
3534         (setq vlist (nreverse vlist)))
3535       (with-current-buffer gnus-group-buffer
3536         (setq gnus-newsgroup-name name
3537               gnus-newsgroup-marked marked
3538               gnus-newsgroup-spam-marked spam
3539               gnus-newsgroup-unreads unread
3540               gnus-current-headers headers
3541               gnus-newsgroup-data data
3542               gnus-article-current gac
3543               gnus-summary-buffer summary
3544               gnus-article-buffer article-buffer
3545               gnus-original-article-buffer original
3546               gnus-reffed-article-number reffed
3547               gnus-current-score-file score-file
3548               gnus-newsgroup-charset default-charset)
3549         (let ((locals gnus-newsgroup-variables))
3550           (while locals
3551             (if (consp (car locals))
3552                 (set (caar locals) (pop vlist))
3553               (set (car locals) (pop vlist)))
3554             (setq locals (cdr locals))))
3555         ;; The article buffer also has local variables.
3556         (when (gnus-buffer-live-p gnus-article-buffer)
3557           (set-buffer gnus-article-buffer)
3558           (setq gnus-summary-buffer summary))))))
3559
3560 (defun gnus-summary-article-unread-p (article)
3561   "Say whether ARTICLE is unread or not."
3562   (memq article gnus-newsgroup-unreads))
3563
3564 (defun gnus-summary-first-article-p (&optional article)
3565   "Return whether ARTICLE is the first article in the buffer."
3566   (if (not (setq article (or article (gnus-summary-article-number))))
3567       nil
3568     (eq article (caar gnus-newsgroup-data))))
3569
3570 (defun gnus-summary-last-article-p (&optional article)
3571   "Return whether ARTICLE is the last article in the buffer."
3572   (if (not (setq article (or article (gnus-summary-article-number))))
3573       ;; All non-existent numbers are the last article.  :-)
3574       t
3575     (not (cdr (gnus-data-find-list article)))))
3576
3577 (defun gnus-make-thread-indent-array (&optional n)
3578   (when (or n
3579             (progn (setq n 200) nil)
3580             (null gnus-thread-indent-array)
3581             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3582     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3583           gnus-thread-indent-array-level gnus-thread-indent-level)
3584     (while (>= n 0)
3585       (aset gnus-thread-indent-array n
3586             (make-string (* n gnus-thread-indent-level) ? ))
3587       (setq n (1- n)))))
3588
3589 (defun gnus-update-summary-mark-positions ()
3590   "Compute where the summary marks are to go."
3591   (save-excursion
3592     (when (gnus-buffer-exists-p gnus-summary-buffer)
3593       (set-buffer gnus-summary-buffer))
3594     (let ((spec gnus-summary-line-format-spec)
3595           pos)
3596       (save-excursion
3597         (gnus-set-work-buffer)
3598         (let ((gnus-tmp-unread ?Z)
3599               (gnus-replied-mark ?Z)
3600               (gnus-score-below-mark ?Z)
3601               (gnus-score-over-mark ?Z)
3602               (gnus-undownloaded-mark ?Z)
3603               (gnus-summary-line-format-spec spec)
3604               (gnus-newsgroup-downloadable '(0))
3605               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3606               case-fold-search ignores)
3607           ;; Here, all marks are bound to Z.
3608           (gnus-summary-insert-line header
3609                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3610           (goto-char (point-min))
3611           ;; Memorize the positions of the same characters as dummy marks.
3612           (while (re-search-forward "[A-D]" nil t)
3613             (push (point) ignores))
3614           (erase-buffer)
3615           ;; We use A-D as dummy marks in order to know column positions
3616           ;; where marks should be inserted.
3617           (setq gnus-tmp-unread ?A
3618                 gnus-replied-mark ?B
3619                 gnus-score-below-mark ?C
3620                 gnus-score-over-mark ?C
3621                 gnus-undownloaded-mark ?D)
3622           (gnus-summary-insert-line header
3623                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3624           ;; Ignore characters which aren't dummy marks.
3625           (dolist (p ignores)
3626             (delete-region (goto-char (1- p)) p)
3627             (insert ?Z))
3628           (goto-char (point-min))
3629           (setq pos (list (cons 'unread
3630                                 (and (search-forward "A" nil t)
3631                                      (- (point) (point-min) 1)))))
3632           (goto-char (point-min))
3633           (push (cons 'replied (and (search-forward "B" nil t)
3634                                     (- (point) (point-min) 1)))
3635                 pos)
3636           (goto-char (point-min))
3637           (push (cons 'score (and (search-forward "C" nil t)
3638                                   (- (point) (point-min) 1)))
3639                 pos)
3640           (goto-char (point-min))
3641           (push (cons 'download (and (search-forward "D" nil t)
3642                                      (- (point) (point-min) 1)))
3643                 pos)))
3644       (setq gnus-summary-mark-positions pos))))
3645
3646 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3647   "Insert a dummy root in the summary buffer."
3648   (beginning-of-line)
3649   (gnus-add-text-properties
3650    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3651    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3652
3653 (defun gnus-summary-extract-address-component (from)
3654   (or (car (funcall gnus-extract-address-components from))
3655       from))
3656
3657 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3658   (let ((mail-parse-charset gnus-newsgroup-charset)
3659         (ignored-from-addresses (gnus-ignored-from-addresses))
3660         ; Is it really necessary to do this next part for each summary line?
3661         ; Luckily, doesn't seem to slow things down much.
3662         (mail-parse-ignored-charsets
3663          (with-current-buffer gnus-summary-buffer
3664            gnus-newsgroup-ignored-charsets)))
3665     (or
3666      (and ignored-from-addresses
3667           (string-match ignored-from-addresses gnus-tmp-from)
3668           (let ((extra-headers (mail-header-extra header))
3669                 to
3670                 newsgroups)
3671             (cond
3672              ((setq to (cdr (assq 'To extra-headers)))
3673               (concat gnus-summary-to-prefix
3674                       (inline
3675                         (gnus-summary-extract-address-component
3676                          (funcall gnus-decode-encoded-address-function to)))))
3677              ((setq newsgroups
3678                     (or
3679                      (cdr (assq 'Newsgroups extra-headers))
3680                      (and
3681                       (memq 'Newsgroups gnus-extra-headers)
3682                       (eq (car (gnus-find-method-for-group
3683                                 gnus-newsgroup-name)) 'nntp)
3684                       (gnus-group-real-name gnus-newsgroup-name))))
3685               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3686      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3687
3688 (defun gnus-summary-insert-line (gnus-tmp-header
3689                                  gnus-tmp-level gnus-tmp-current
3690                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3691                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3692                                  &optional gnus-tmp-dummy gnus-tmp-score
3693                                  gnus-tmp-process)
3694   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3695       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3696                                           gnus-tmp-level)))
3697   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3698          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3699          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3700          (gnus-tmp-score-char
3701           (if (or (null gnus-summary-default-score)
3702                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3703                       gnus-summary-zcore-fuzz))
3704               ?                         ;Whitespace
3705             (if (< gnus-tmp-score gnus-summary-default-score)
3706                 gnus-score-below-mark gnus-score-over-mark)))
3707          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3708          (gnus-tmp-replied
3709           (cond (gnus-tmp-process gnus-process-mark)
3710                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3711                  gnus-cached-mark)
3712                 (gnus-tmp-replied gnus-replied-mark)
3713                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3714                  gnus-forwarded-mark)
3715                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3716                  gnus-saved-mark)
3717                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3718                  gnus-recent-mark)
3719                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3720                  gnus-unseen-mark)
3721                 (t gnus-no-mark)))
3722          (gnus-tmp-downloaded
3723           (cond (undownloaded
3724                  gnus-undownloaded-mark)
3725                 (gnus-newsgroup-agentized
3726                  gnus-downloaded-mark)
3727                 (t
3728                  gnus-no-mark)))
3729          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3730          (gnus-tmp-name
3731           (cond
3732            ((string-match "<[^>]+> *$" gnus-tmp-from)
3733             (let ((beg (match-beginning 0)))
3734               (or (and (string-match "^\".+\"" gnus-tmp-from)
3735                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3736                   (substring gnus-tmp-from 0 beg))))
3737            ((string-match "(.+)" gnus-tmp-from)
3738             (substring gnus-tmp-from
3739                        (1+ (match-beginning 0)) (1- (match-end 0))))
3740            (t gnus-tmp-from)))
3741          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3742          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3743          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3744          (inhibit-read-only t))
3745     (when (string= gnus-tmp-name "")
3746       (setq gnus-tmp-name gnus-tmp-from))
3747     (unless (numberp gnus-tmp-lines)
3748       (setq gnus-tmp-lines -1))
3749     (if (= gnus-tmp-lines -1)
3750         (setq gnus-tmp-lines "?")
3751       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3752     (condition-case ()
3753         (gnus-put-text-property
3754          (point)
3755          (progn (eval gnus-summary-line-format-spec) (point))
3756          'gnus-number gnus-tmp-number)
3757       (error (gnus-message 5 "Error updating the summary line")))
3758     (when (gnus-visual-p 'summary-highlight 'highlight)
3759       (forward-line -1)
3760       (gnus-run-hooks 'gnus-summary-update-hook)
3761       (forward-line 1))))
3762
3763 (defun gnus-summary-update-line (&optional dont-update)
3764   "Update summary line after change."
3765   (when (and gnus-summary-default-score
3766              (not gnus-summary-inhibit-highlight))
3767     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3768            (article (gnus-summary-article-number))
3769            (score (gnus-summary-article-score article)))
3770       (unless dont-update
3771         (if (and gnus-summary-mark-below
3772                  (< (gnus-summary-article-score)
3773                     gnus-summary-mark-below))
3774             ;; This article has a low score, so we mark it as read.
3775             (when (memq article gnus-newsgroup-unreads)
3776               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3777           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3778             ;; This article was previously marked as read on account
3779             ;; of a low score, but now it has risen, so we mark it as
3780             ;; unread.
3781             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3782         (gnus-summary-update-mark
3783          (if (or (null gnus-summary-default-score)
3784                  (<= (abs (- score gnus-summary-default-score))
3785                      gnus-summary-zcore-fuzz))
3786              ?                          ;Whitespace
3787            (if (< score gnus-summary-default-score)
3788                gnus-score-below-mark gnus-score-over-mark))
3789          'score))
3790       ;; Do visual highlighting.
3791       (when (gnus-visual-p 'summary-highlight 'highlight)
3792         (gnus-run-hooks 'gnus-summary-update-hook)))))
3793
3794 (defvar gnus-tmp-new-adopts nil)
3795
3796 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3797   "Return the number of articles in THREAD.
3798 This may be 0 in some cases -- if none of the articles in
3799 the thread are to be displayed."
3800   (let* ((number
3801          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3802           (cond
3803            ((not (listp thread))
3804             1)
3805            ((and (consp thread) (cdr thread))
3806             (apply
3807              '+ 1 (mapcar
3808                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3809            ((null thread)
3810             1)
3811            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3812             1)
3813            (t 0))))
3814     (when (and level (zerop level) gnus-tmp-new-adopts)
3815       (incf number
3816             (apply '+ (mapcar
3817                        'gnus-summary-number-of-articles-in-thread
3818                        gnus-tmp-new-adopts))))
3819     (if char
3820         (if (> number 1) gnus-not-empty-thread-mark
3821           gnus-empty-thread-mark)
3822       number)))
3823
3824 (defsubst gnus-summary-line-message-size (head)
3825   "Return pretty-printed version of message size.
3826 This function is intended to be used in
3827 `gnus-summary-line-format-alist'."
3828   (let ((c (or (mail-header-chars head) -1)))
3829     (cond ((< c 0) "n/a")               ; chars not available
3830           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3831           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3832           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3833           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3834
3835
3836 (defun gnus-summary-set-local-parameters (group)
3837   "Go through the local params of GROUP and set all variable specs in that list."
3838   (let ((vars '(quit-config)))          ; Ignore quit-config.
3839     (dolist (elem (gnus-group-find-parameter group))
3840       (and (consp elem)                 ; Has to be a cons.
3841            (consp (cdr elem))           ; The cdr has to be a list.
3842            (symbolp (car elem))         ; Has to be a symbol in there.
3843            (not (memq (car elem) vars))
3844            (ignore-errors
3845              (push (car elem) vars)
3846              ;; Variables like `gnus-show-threads' that are globally
3847              ;; bound, if used as group parameters, need to get to be
3848              ;; buffer-local, whereas just parameters like `gcc-self',
3849              ;; `timestamp', etc. should not be bound as variables.
3850              (if (boundp (car elem))
3851                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3852                (eval (nth 1 elem))))))))
3853
3854 (defun gnus-summary-read-group (group &optional show-all no-article
3855                                       kill-buffer no-display backward
3856                                       select-articles)
3857   "Start reading news in newsgroup GROUP.
3858 If SHOW-ALL is non-nil, already read articles are also listed.
3859 If NO-ARTICLE is non-nil, no article is selected initially.
3860 If NO-DISPLAY, don't generate a summary buffer."
3861   (let (result)
3862     (while (and group
3863                 (null (setq result
3864                             (let ((gnus-auto-select-next nil))
3865                               (or (gnus-summary-read-group-1
3866                                    group show-all no-article
3867                                    kill-buffer no-display
3868                                    select-articles)
3869                                   (setq show-all nil
3870                                         select-articles nil)))))
3871                 (eq gnus-auto-select-next 'quietly))
3872       (set-buffer gnus-group-buffer)
3873       ;; The entry function called above goes to the next
3874       ;; group automatically, so we go two groups back
3875       ;; if we are searching for the previous group.
3876       (when backward
3877         (gnus-group-prev-unread-group 2))
3878       (if (not (equal group (gnus-group-group-name)))
3879           (setq group (gnus-group-group-name))
3880         (setq group nil)))
3881     result))
3882
3883 (defun gnus-summary-read-group-1 (group show-all no-article
3884                                         kill-buffer no-display
3885                                         &optional select-articles)
3886   ;; Killed foreign groups can't be entered.
3887   ;;  (when (and (not (gnus-group-native-p group))
3888   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3889   ;;    (error "Dead non-native groups can't be entered"))
3890   (gnus-message 5 "Retrieving newsgroup: %s..."
3891                 (gnus-group-decoded-name group))
3892   (let* ((new-group (gnus-summary-setup-buffer group))
3893          (quit-config (gnus-group-quit-config group))
3894          (did-select (and new-group (gnus-select-newsgroup
3895                                      group show-all select-articles))))
3896     (cond
3897      ;; This summary buffer exists already, so we just select it.
3898      ((not new-group)
3899       (gnus-set-global-variables)
3900       (when kill-buffer
3901         (gnus-kill-or-deaden-summary kill-buffer))
3902       (gnus-configure-windows 'summary 'force)
3903       (gnus-set-mode-line 'summary)
3904       (gnus-summary-position-point)
3905       (message "")
3906       t)
3907      ;; We couldn't select this group.
3908      ((null did-select)
3909       (when (and (eq major-mode 'gnus-summary-mode)
3910                  (not (equal (current-buffer) kill-buffer)))
3911         (kill-buffer (current-buffer))
3912         (if (not quit-config)
3913             (progn
3914               ;; Update the info -- marks might need to be removed,
3915               ;; for instance.
3916               (gnus-summary-update-info)
3917               (set-buffer gnus-group-buffer)
3918               (gnus-group-jump-to-group group)
3919               (gnus-group-next-unread-group 1))
3920           (gnus-handle-ephemeral-exit quit-config)))
3921       (let ((grpinfo (gnus-get-info group)))
3922         (if (null (gnus-info-read grpinfo))
3923             (gnus-message 3 "Group %s contains no messages"
3924                           (gnus-group-decoded-name group))
3925           (gnus-message 3 "Can't select group")))
3926       nil)
3927      ;; The user did a `C-g' while prompting for number of articles,
3928      ;; so we exit this group.
3929      ((eq did-select 'quit)
3930       (and (eq major-mode 'gnus-summary-mode)
3931            (not (equal (current-buffer) kill-buffer))
3932            (kill-buffer (current-buffer)))
3933       (when kill-buffer
3934         (gnus-kill-or-deaden-summary kill-buffer))
3935       (if (not quit-config)
3936           (progn
3937             (set-buffer gnus-group-buffer)
3938             (gnus-group-jump-to-group group)
3939             (gnus-configure-windows 'group 'force))
3940         (gnus-handle-ephemeral-exit quit-config))
3941       ;; Finally signal the quit.
3942       (signal 'quit nil))
3943      ;; The group was successfully selected.
3944      (t
3945       (gnus-set-global-variables)
3946       ;; Save the active value in effect when the group was entered.
3947       (setq gnus-newsgroup-active
3948             (gnus-copy-sequence
3949              (gnus-active gnus-newsgroup-name)))
3950       ;; You can change the summary buffer in some way with this hook.
3951       (gnus-run-hooks 'gnus-select-group-hook)
3952       (when (memq 'summary (gnus-update-format-specifications
3953                             nil 'summary 'summary-mode 'summary-dummy))
3954         ;; The format specification for the summary line was updated,
3955         ;; so we need to update the mark positions as well.
3956         (gnus-update-summary-mark-positions))
3957       ;; Do score processing.
3958       (when gnus-use-scoring
3959         (gnus-possibly-score-headers))
3960       ;; Check whether to fill in the gaps in the threads.
3961       (when gnus-build-sparse-threads
3962         (gnus-build-sparse-threads))
3963       ;; Find the initial limit.
3964       (if show-all
3965           (let ((gnus-newsgroup-dormant nil))
3966             (gnus-summary-initial-limit show-all))
3967         (gnus-summary-initial-limit show-all))
3968       ;; Generate the summary buffer.
3969       (unless no-display
3970         (gnus-summary-prepare))
3971       (when gnus-use-trees
3972         (gnus-tree-open group)
3973         (setq gnus-summary-highlight-line-function
3974               'gnus-tree-highlight-article))
3975       ;; If the summary buffer is empty, but there are some low-scored
3976       ;; articles or some excluded dormants, we include these in the
3977       ;; buffer.
3978       (when (and (zerop (buffer-size))
3979                  (not no-display))
3980         (cond (gnus-newsgroup-dormant
3981                (gnus-summary-limit-include-dormant))
3982               ((and gnus-newsgroup-scored show-all)
3983                (gnus-summary-limit-include-expunged t))))
3984       ;; Function `gnus-apply-kill-file' must be called in this hook.
3985       (gnus-run-hooks 'gnus-apply-kill-hook)
3986       (if (and (zerop (buffer-size))
3987                (not no-display))
3988           (progn
3989             ;; This newsgroup is empty.
3990             (gnus-summary-catchup-and-exit nil t)
3991             (gnus-message 6 "No unread news")
3992             (when kill-buffer
3993               (gnus-kill-or-deaden-summary kill-buffer))
3994             ;; Return nil from this function.
3995             nil)
3996         ;; Hide conversation thread subtrees.  We cannot do this in
3997         ;; gnus-summary-prepare-hook since kill processing may not
3998         ;; work with hidden articles.
3999         (gnus-summary-maybe-hide-threads)
4000         (when kill-buffer
4001           (gnus-kill-or-deaden-summary kill-buffer))
4002         (gnus-summary-auto-select-subject)
4003         ;; Show first unread article if requested.
4004         (if (and (not no-article)
4005                  (not no-display)
4006                  gnus-newsgroup-unreads
4007                  gnus-auto-select-first)
4008             (progn
4009               (gnus-configure-windows 'summary)
4010               (let ((art (gnus-summary-article-number)))
4011                 (unless (and (not gnus-plugged)
4012                              (or (memq art gnus-newsgroup-undownloaded)
4013                                  (memq art gnus-newsgroup-downloadable)))
4014                   (gnus-summary-goto-article art))))
4015           ;; Don't select any articles.
4016           (gnus-summary-position-point)
4017           (gnus-configure-windows 'summary 'force)
4018           (gnus-set-mode-line 'summary))
4019         (when (and gnus-auto-center-group
4020                    (get-buffer-window gnus-group-buffer t))
4021           ;; Gotta use windows, because recenter does weird stuff if
4022           ;; the current buffer ain't the displayed window.
4023           (let ((owin (selected-window)))
4024             (select-window (get-buffer-window gnus-group-buffer t))
4025             (when (gnus-group-goto-group group)
4026               (recenter))
4027             (select-window owin)))
4028         ;; Mark this buffer as "prepared".
4029         (setq gnus-newsgroup-prepared t)
4030         (gnus-run-hooks 'gnus-summary-prepared-hook)
4031         (unless (gnus-ephemeral-group-p group)
4032           (gnus-group-update-group group))
4033         t)))))
4034
4035 (defun gnus-summary-auto-select-subject ()
4036   "Select the subject line on initial group entry."
4037   (goto-char (point-min))
4038   (cond
4039    ((eq gnus-auto-select-subject 'best)
4040     (gnus-summary-best-unread-subject))
4041    ((eq gnus-auto-select-subject 'unread)
4042     (gnus-summary-first-unread-subject))
4043    ((eq gnus-auto-select-subject 'unseen)
4044     (gnus-summary-first-unseen-subject))
4045    ((eq gnus-auto-select-subject 'unseen-or-unread)
4046     (gnus-summary-first-unseen-or-unread-subject))
4047    ((eq gnus-auto-select-subject 'first)
4048     ;; Do nothing.
4049     )
4050    ((functionp gnus-auto-select-subject)
4051     (funcall gnus-auto-select-subject))))
4052
4053 (defun gnus-summary-prepare ()
4054   "Generate the summary buffer."
4055   (interactive)
4056   (let ((inhibit-read-only t))
4057     (erase-buffer)
4058     (setq gnus-newsgroup-data nil
4059           gnus-newsgroup-data-reverse nil)
4060     (gnus-run-hooks 'gnus-summary-generate-hook)
4061     ;; Generate the buffer, either with threads or without.
4062     (when gnus-newsgroup-headers
4063       (gnus-summary-prepare-threads
4064        (if gnus-show-threads
4065            (gnus-sort-gathered-threads
4066             (funcall gnus-summary-thread-gathering-function
4067                      (gnus-sort-threads
4068                       (gnus-cut-threads (gnus-make-threads)))))
4069          ;; Unthreaded display.
4070          (gnus-sort-articles gnus-newsgroup-headers))))
4071     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4072     ;; Call hooks for modifying summary buffer.
4073     (goto-char (point-min))
4074     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4075
4076 (defsubst gnus-general-simplify-subject (subject)
4077   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4078   (setq subject
4079         (cond
4080          ;; Truncate the subject.
4081          (gnus-simplify-subject-functions
4082           (gnus-map-function gnus-simplify-subject-functions subject))
4083          ((numberp gnus-summary-gather-subject-limit)
4084           (setq subject (gnus-simplify-subject-re subject))
4085           (if (> (length subject) gnus-summary-gather-subject-limit)
4086               (substring subject 0 gnus-summary-gather-subject-limit)
4087             subject))
4088          ;; Fuzzily simplify it.
4089          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4090           (gnus-simplify-subject-fuzzy subject))
4091          ;; Just remove the leading "Re:".
4092          (t
4093           (gnus-simplify-subject-re subject))))
4094
4095   (if (and gnus-summary-gather-exclude-subject
4096            (string-match gnus-summary-gather-exclude-subject subject))
4097       nil                         ; This article shouldn't be gathered
4098     subject))
4099
4100 (defun gnus-summary-simplify-subject-query ()
4101   "Query where the respool algorithm would put this article."
4102   (interactive)
4103   (gnus-summary-select-article)
4104   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4105
4106 (defun gnus-gather-threads-by-subject (threads)
4107   "Gather threads by looking at Subject headers."
4108   (if (not gnus-summary-make-false-root)
4109       threads
4110     (let ((hashtb (gnus-make-hashtable 1024))
4111           (prev threads)
4112           (result threads)
4113           subject hthread whole-subject)
4114       (while threads
4115         (setq subject (gnus-general-simplify-subject
4116                        (setq whole-subject (mail-header-subject
4117                                             (caar threads)))))
4118         (when subject
4119           (if (setq hthread (gnus-gethash subject hashtb))
4120               (progn
4121                 ;; We enter a dummy root into the thread, if we
4122                 ;; haven't done that already.
4123                 (unless (stringp (caar hthread))
4124                   (setcar hthread (list whole-subject (car hthread))))
4125                 ;; We add this new gathered thread to this gathered
4126                 ;; thread.
4127                 (setcdr (car hthread)
4128                         (nconc (cdar hthread) (list (car threads))))
4129                 ;; Remove it from the list of threads.
4130                 (setcdr prev (cdr threads))
4131                 (setq threads prev))
4132             ;; Enter this thread into the hash table.
4133             (gnus-sethash subject
4134                           (if gnus-summary-make-false-root-always
4135                               (progn
4136                                 ;; If you want a dummy root above all
4137                                 ;; threads...
4138                                 (setcar threads (list whole-subject
4139                                                       (car threads)))
4140                                 threads)
4141                             threads)
4142                           hashtb)))
4143         (setq prev threads)
4144         (setq threads (cdr threads)))
4145       result)))
4146
4147 (defun gnus-gather-threads-by-references (threads)
4148   "Gather threads by looking at References headers."
4149   (let ((idhashtb (gnus-make-hashtable 1024))
4150         (thhashtb (gnus-make-hashtable 1024))
4151         (prev threads)
4152         (result threads)
4153         ids references id gthread gid entered ref)
4154     (while threads
4155       (when (setq references (mail-header-references (caar threads)))
4156         (setq id (mail-header-id (caar threads))
4157               ids (inline (gnus-split-references references))
4158               entered nil)
4159         (while (setq ref (pop ids))
4160           (setq ids (delete ref ids))
4161           (if (not (setq gid (gnus-gethash ref idhashtb)))
4162               (progn
4163                 (gnus-sethash ref id idhashtb)
4164                 (gnus-sethash id threads thhashtb))
4165             (setq gthread (gnus-gethash gid thhashtb))
4166             (unless entered
4167               ;; We enter a dummy root into the thread, if we
4168               ;; haven't done that already.
4169               (unless (stringp (caar gthread))
4170                 (setcar gthread (list (mail-header-subject (caar gthread))
4171                                       (car gthread))))
4172               ;; We add this new gathered thread to this gathered
4173               ;; thread.
4174               (setcdr (car gthread)
4175                       (nconc (cdar gthread) (list (car threads)))))
4176             ;; Add it into the thread hash table.
4177             (gnus-sethash id gthread thhashtb)
4178             (setq entered t)
4179             ;; Remove it from the list of threads.
4180             (setcdr prev (cdr threads))
4181             (setq threads prev))))
4182       (setq prev threads)
4183       (setq threads (cdr threads)))
4184     result))
4185
4186 (defun gnus-sort-gathered-threads (threads)
4187   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4188   (let ((result threads))
4189     (while threads
4190       (when (stringp (caar threads))
4191         (setcdr (car threads)
4192                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4193       (setq threads (cdr threads)))
4194     result))
4195
4196 (defun gnus-thread-loop-p (root thread)
4197   "Say whether ROOT is in THREAD."
4198   (let ((stack (list thread))
4199         (infloop 0)
4200         th)
4201     (while (setq thread (pop stack))
4202       (setq th (cdr thread))
4203       (while (and th
4204                   (not (eq (caar th) root)))
4205         (pop th))
4206       (if th
4207           ;; We have found a loop.
4208           (let (ref-dep)
4209             (setcdr thread (delq (car th) (cdr thread)))
4210             (if (boundp (setq ref-dep (intern "none"
4211                                               gnus-newsgroup-dependencies)))
4212                 (setcdr (symbol-value ref-dep)
4213                         (nconc (cdr (symbol-value ref-dep))
4214                                (list (car th))))
4215               (set ref-dep (list nil (car th))))
4216             (setq infloop 1
4217                   stack nil))
4218         ;; Push all the subthreads onto the stack.
4219         (push (cdr thread) stack)))
4220     infloop))
4221
4222 (defun gnus-make-threads ()
4223   "Go through the dependency hashtb and find the roots.  Return all threads."
4224   (let (threads)
4225     (while (catch 'infloop
4226              (mapatoms
4227               (lambda (refs)
4228                 ;; Deal with self-referencing References loops.
4229                 (when (and (car (symbol-value refs))
4230                            (not (zerop
4231                                  (apply
4232                                   '+
4233                                   (mapcar
4234                                    (lambda (thread)
4235                                      (gnus-thread-loop-p
4236                                       (car (symbol-value refs)) thread))
4237                                    (cdr (symbol-value refs)))))))
4238                   (setq threads nil)
4239                   (throw 'infloop t))
4240                 (unless (car (symbol-value refs))
4241                   ;; These threads do not refer back to any other
4242                   ;; articles, so they're roots.
4243                   (setq threads (append (cdr (symbol-value refs)) threads))))
4244               gnus-newsgroup-dependencies)))
4245     threads))
4246
4247 ;; Build the thread tree.
4248 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4249   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4250
4251 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4252 if it was already present.
4253
4254 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4255 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4256 Message-IDs will be renamed to a unique Message-ID before being
4257 entered.
4258
4259 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4260   (let* ((id (mail-header-id header))
4261          (id-dep (and id (intern id dependencies)))
4262          parent-id ref ref-dep ref-header replaced)
4263     ;; Enter this `header' in the `dependencies' table.
4264     (cond
4265      ((not id-dep)
4266       (setq header nil))
4267      ;; The first two cases do the normal part: enter a new `header'
4268      ;; in the `dependencies' table.
4269      ((not (boundp id-dep))
4270       (set id-dep (list header)))
4271      ((null (car (symbol-value id-dep)))
4272       (setcar (symbol-value id-dep) header))
4273
4274      ;; From here the `header' was already present in the
4275      ;; `dependencies' table.
4276      (force-new
4277       ;; Overrides an existing entry;
4278       ;; just set the header part of the entry.
4279       (setcar (symbol-value id-dep) header)
4280       (setq replaced t))
4281
4282      ;; Renames the existing `header' to a unique Message-ID.
4283      ((not gnus-summary-ignore-duplicates)
4284       ;; An article with this Message-ID has already been seen.
4285       ;; We rename the Message-ID.
4286       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4287            (list header))
4288       (mail-header-set-id header id))
4289
4290      ;; The last case ignores an existing entry, except it adds any
4291      ;; additional Xrefs (in case the two articles came from different
4292      ;; servers.
4293      ;; Also sets `header' to `nil' meaning that the `dependencies'
4294      ;; table was *not* modified.
4295      (t
4296       (mail-header-set-xref
4297        (car (symbol-value id-dep))
4298        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4299                    "")
4300                (or (mail-header-xref header) "")))
4301       (setq header nil)))
4302
4303     (when (and header (not replaced))
4304       ;; First check that we are not creating a References loop.
4305       (setq parent-id (gnus-parent-id (mail-header-references header)))
4306       (setq ref parent-id)
4307       (while (and ref
4308                   (setq ref-dep (intern-soft ref dependencies))
4309                   (boundp ref-dep)
4310                   (setq ref-header (car (symbol-value ref-dep))))
4311         (if (string= id ref)
4312             ;; Yuk!  This is a reference loop.  Make the article be a
4313             ;; root article.
4314             (progn
4315               (mail-header-set-references (car (symbol-value id-dep)) "none")
4316               (setq ref nil)
4317               (setq parent-id nil))
4318           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4319       (setq ref-dep (intern (or parent-id "none") dependencies))
4320       (if (boundp ref-dep)
4321           (setcdr (symbol-value ref-dep)
4322                   (nconc (cdr (symbol-value ref-dep))
4323                          (list (symbol-value id-dep))))
4324         (set ref-dep (list nil (symbol-value id-dep)))))
4325     header))
4326
4327 (defun gnus-extract-message-id-from-in-reply-to (string)
4328   (if (string-match "<[^>]+>" string)
4329       (substring string (match-beginning 0) (match-end 0))
4330     nil))
4331
4332 (defun gnus-build-sparse-threads ()
4333   (let ((headers gnus-newsgroup-headers)
4334         (mail-parse-charset gnus-newsgroup-charset)
4335         (gnus-summary-ignore-duplicates t)
4336         header references generation relations
4337         subject child end new-child date)
4338     ;; First we create an alist of generations/relations, where
4339     ;; generations is how much we trust the relation, and the relation
4340     ;; is parent/child.
4341     (gnus-message 7 "Making sparse threads...")
4342     (save-excursion
4343       (nnheader-set-temp-buffer " *gnus sparse threads*")
4344       (while (setq header (pop headers))
4345         (when (and (setq references (mail-header-references header))
4346                    (not (string= references "")))
4347           (insert references)
4348           (setq child (mail-header-id header)
4349                 subject (mail-header-subject header)
4350                 date (mail-header-date header)
4351                 generation 0)
4352           (while (search-backward ">" nil t)
4353             (setq end (1+ (point)))
4354             (when (search-backward "<" nil t)
4355               (setq new-child (buffer-substring (point) end))
4356               (push (list (incf generation)
4357                           child (setq child new-child)
4358                           subject date)
4359                     relations)))
4360           (when child
4361             (push (list (1+ generation) child nil subject) relations))
4362           (erase-buffer)))
4363       (kill-buffer (current-buffer)))
4364     ;; Sort over trustworthiness.
4365     (dolist (relation (sort relations 'car-less-than-car))
4366       (when (gnus-dependencies-add-header
4367              (make-full-mail-header
4368               gnus-reffed-article-number
4369               (nth 3 relation) "" (or (nth 4 relation) "")
4370               (nth 1 relation)
4371               (or (nth 2 relation) "") 0 0 "")
4372              gnus-newsgroup-dependencies nil)
4373         (push gnus-reffed-article-number gnus-newsgroup-limit)
4374         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4375         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4376               gnus-newsgroup-reads)
4377         (decf gnus-reffed-article-number)))
4378     (gnus-message 7 "Making sparse threads...done")))
4379
4380 (defun gnus-build-old-threads ()
4381   ;; Look at all the articles that refer back to old articles, and
4382   ;; fetch the headers for the articles that aren't there.  This will
4383   ;; build complete threads - if the roots haven't been expired by the
4384   ;; server, that is.
4385   (let ((mail-parse-charset gnus-newsgroup-charset)
4386         id heads)
4387     (mapatoms
4388      (lambda (refs)
4389        (when (not (car (symbol-value refs)))
4390          (setq heads (cdr (symbol-value refs)))
4391          (while heads
4392            (if (memq (mail-header-number (caar heads))
4393                      gnus-newsgroup-dormant)
4394                (setq heads (cdr heads))
4395              (setq id (symbol-name refs))
4396              (while (and (setq id (gnus-build-get-header id))
4397                          (not (car (gnus-id-to-thread id)))))
4398              (setq heads nil)))))
4399      gnus-newsgroup-dependencies)))
4400
4401 (defsubst gnus-remove-odd-characters (string)
4402   "Translate STRING into something that doesn't contain weird characters."
4403   (mm-subst-char-in-string
4404    ?\r ?\-
4405    (mm-subst-char-in-string ?\n ?\- string t) t))
4406
4407 ;; This function has to be called with point after the article number
4408 ;; on the beginning of the line.
4409 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4410   (let ((eol (point-at-eol))
4411         (buffer (current-buffer))
4412         header references in-reply-to)
4413
4414     ;; overview: [num subject from date id refs chars lines misc]
4415     (unwind-protect
4416         (let (x)
4417           (narrow-to-region (point) eol)
4418           (unless (eobp)
4419             (forward-char))
4420
4421           (setq header
4422                 (make-full-mail-header
4423                  number                 ; number
4424                  (condition-case ()     ; subject
4425                      (gnus-remove-odd-characters
4426                       (funcall gnus-decode-encoded-word-function
4427                                (setq x (nnheader-nov-field))))
4428                    (error x))
4429                  (condition-case ()     ; from
4430                      (gnus-remove-odd-characters
4431                       (funcall gnus-decode-encoded-address-function
4432                                (setq x (nnheader-nov-field))))
4433                    (error x))
4434                  (nnheader-nov-field)   ; date
4435                  (nnheader-nov-read-message-id number)  ; id
4436                  (setq references (nnheader-nov-field)) ; refs
4437                  (nnheader-nov-read-integer) ; chars
4438                  (nnheader-nov-read-integer) ; lines
4439                  (unless (eobp)
4440                    (if (looking-at "Xref: ")
4441                        (goto-char (match-end 0)))
4442                    (nnheader-nov-field)) ; Xref
4443                  (nnheader-nov-parse-extra)))) ; extra
4444
4445       (widen))
4446
4447     (when (and (string= references "")
4448                (setq in-reply-to (mail-header-extra header))
4449                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4450       (mail-header-set-references
4451        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4452
4453     (when gnus-alter-header-function
4454       (funcall gnus-alter-header-function header))
4455     (gnus-dependencies-add-header header dependencies force-new)))
4456
4457 (defun gnus-build-get-header (id)
4458   "Look through the buffer of NOV lines and find the header to ID.
4459 Enter this line into the dependencies hash table, and return
4460 the id of the parent article (if any)."
4461   (let ((deps gnus-newsgroup-dependencies)
4462         found header)
4463     (prog1
4464         (with-current-buffer nntp-server-buffer
4465           (let ((case-fold-search nil))
4466             (goto-char (point-min))
4467             (while (and (not found)
4468                         (search-forward id nil t))
4469               (beginning-of-line)
4470               (setq found (looking-at
4471                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4472                                    (regexp-quote id))))
4473               (or found (beginning-of-line 2)))
4474             (when found
4475               (beginning-of-line)
4476               (and
4477                (setq header (gnus-nov-parse-line
4478                              (read (current-buffer)) deps))
4479                (gnus-parent-id (mail-header-references header))))))
4480       (when header
4481         (let ((number (mail-header-number header)))
4482           (push number gnus-newsgroup-limit)
4483           (push header gnus-newsgroup-headers)
4484           (if (memq number gnus-newsgroup-unselected)
4485               (progn
4486                 (setq gnus-newsgroup-unreads
4487                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4488                                                number))
4489                 (setq gnus-newsgroup-unselected
4490                       (delq number gnus-newsgroup-unselected)))
4491             (push number gnus-newsgroup-ancient)))))))
4492
4493 (defun gnus-build-all-threads ()
4494   "Read all the headers."
4495   (let ((gnus-summary-ignore-duplicates t)
4496         (mail-parse-charset gnus-newsgroup-charset)
4497         (dependencies gnus-newsgroup-dependencies)
4498         header article)
4499     (with-current-buffer nntp-server-buffer
4500       (let ((case-fold-search nil))
4501         (goto-char (point-min))
4502         (while (not (eobp))
4503           (ignore-errors
4504             (setq article (read (current-buffer))
4505                   header (gnus-nov-parse-line article dependencies)))
4506           (when header
4507             (with-current-buffer gnus-summary-buffer
4508               (push header gnus-newsgroup-headers)
4509               (if (memq (setq article (mail-header-number header))
4510                         gnus-newsgroup-unselected)
4511                   (progn
4512                     (setq gnus-newsgroup-unreads
4513                           (gnus-add-to-sorted-list
4514                            gnus-newsgroup-unreads article))
4515                     (setq gnus-newsgroup-unselected
4516                           (delq article gnus-newsgroup-unselected)))
4517                 (push article gnus-newsgroup-ancient)))
4518             (forward-line 1)))))))
4519
4520 (defun gnus-summary-update-article-line (article header)
4521   "Update the line for ARTICLE using HEADER."
4522   (let* ((id (mail-header-id header))
4523          (thread (gnus-id-to-thread id)))
4524     (unless thread
4525       (error "Article in no thread"))
4526     ;; Update the thread.
4527     (setcar thread header)
4528     (gnus-summary-goto-subject article)
4529     (let* ((datal (gnus-data-find-list article))
4530            (data (car datal))
4531            (inhibit-read-only t)
4532            (level (gnus-summary-thread-level)))
4533       (gnus-delete-line)
4534       (let ((inserted (- (point)
4535                          (progn
4536                            (gnus-summary-insert-line
4537                             header level nil
4538                             (memq article gnus-newsgroup-undownloaded)
4539                             (gnus-article-mark article)
4540                             (memq article gnus-newsgroup-replied)
4541                             (memq article gnus-newsgroup-expirable)
4542                             ;; Only insert the Subject string when it's different
4543                             ;; from the previous Subject string.
4544                             (if (and
4545                                  gnus-show-threads
4546                                  (gnus-subject-equal
4547                                   (condition-case ()
4548                                       (mail-header-subject
4549                                        (gnus-data-header
4550                                         (cadr
4551                                          (gnus-data-find-list
4552                                           article
4553                                           (gnus-data-list t)))))
4554                                     ;; Error on the side of excessive subjects.
4555                                     (error ""))
4556                                   (mail-header-subject header)))
4557                                 ""
4558                               (mail-header-subject header))
4559                             nil (cdr (assq article gnus-newsgroup-scored))
4560                             (memq article gnus-newsgroup-processable))
4561                            (point)))))
4562         (when (cdr datal)
4563           (gnus-data-update-list
4564            (cdr datal)
4565            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4566
4567 (defun gnus-summary-update-article (article &optional iheader)
4568   "Update ARTICLE in the summary buffer."
4569   (set-buffer gnus-summary-buffer)
4570   (let* ((header (gnus-summary-article-header article))
4571          (id (mail-header-id header))
4572          (data (gnus-data-find article))
4573          (thread (gnus-id-to-thread id))
4574          (references (mail-header-references header))
4575          (parent
4576           (gnus-id-to-thread
4577            (or (gnus-parent-id
4578                 (when (and references
4579                            (not (equal "" references)))
4580                   references))
4581                "none")))
4582          (inhibit-read-only t)
4583          (old (car thread)))
4584     (when thread
4585       (unless iheader
4586         (setcar thread nil)
4587         (when parent
4588           (delq thread parent)))
4589       (if (gnus-summary-insert-subject id header)
4590           ;; Set the (possibly) new article number in the data structure.
4591           (gnus-data-set-number data (gnus-id-to-article id))
4592         (setcar thread old)
4593         nil))))
4594
4595 (defun gnus-rebuild-thread (id &optional line)
4596   "Rebuild the thread containing ID.
4597 If LINE, insert the rebuilt thread starting on line LINE."
4598   (let ((inhibit-read-only t)
4599         old-pos current thread data)
4600     (if (not gnus-show-threads)
4601         (setq thread (list (car (gnus-id-to-thread id))))
4602       ;; Get the thread this article is part of.
4603       (setq thread (gnus-remove-thread id)))
4604     (setq old-pos (point-at-bol))
4605     (setq current (save-excursion
4606                     (and (re-search-backward "[\r\n]" nil t)
4607                          (gnus-summary-article-number))))
4608     ;; If this is a gathered thread, we have to go some re-gathering.
4609     (when (stringp (car thread))
4610       (let ((subject (car thread))
4611             roots thr)
4612         (setq thread (cdr thread))
4613         (while thread
4614           (unless (memq (setq thr (gnus-id-to-thread
4615                                    (gnus-root-id
4616                                     (mail-header-id (caar thread)))))
4617                         roots)
4618             (push thr roots))
4619           (setq thread (cdr thread)))
4620         ;; We now have all (unique) roots.
4621         (if (= (length roots) 1)
4622             ;; All the loose roots are now one solid root.
4623             (setq thread (car roots))
4624           (setq thread (cons subject (gnus-sort-threads roots))))))
4625     (let (threads)
4626       ;; We then insert this thread into the summary buffer.
4627       (when line
4628         (goto-char (point-min))
4629         (forward-line (1- line)))
4630       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4631         (if gnus-show-threads
4632             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4633           (gnus-summary-prepare-unthreaded thread))
4634         (setq data (nreverse gnus-newsgroup-data))
4635         (setq threads gnus-newsgroup-threads))
4636       ;; We splice the new data into the data structure.
4637       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4638       ;;!!! then we want to insert at the beginning of the buffer.
4639       ;;!!! That happens to be true with Gnus now, but that may
4640       ;;!!! change in the future.  Perhaps.
4641       (gnus-data-enter-list
4642        (if line nil current) data (- (point) old-pos))
4643       (setq gnus-newsgroup-threads
4644             (nconc threads gnus-newsgroup-threads))
4645       (gnus-data-compute-positions))))
4646
4647 (defun gnus-number-to-header (number)
4648   "Return the header for article NUMBER."
4649   (let ((headers gnus-newsgroup-headers))
4650     (while (and headers
4651                 (not (= number (mail-header-number (car headers)))))
4652       (pop headers))
4653     (when headers
4654       (car headers))))
4655
4656 (defun gnus-parent-headers (in-headers &optional generation)
4657   "Return the headers of the GENERATIONeth parent of HEADERS."
4658   (unless generation
4659     (setq generation 1))
4660   (let ((parent t)
4661         (headers in-headers)
4662         references)
4663     (while (and parent
4664                 (not (zerop generation))
4665                 (setq references (mail-header-references headers)))
4666       (setq headers (if (and references
4667                              (setq parent (gnus-parent-id references)))
4668                         (car (gnus-id-to-thread parent))
4669                       nil))
4670       (decf generation))
4671     (and (not (eq headers in-headers))
4672          headers)))
4673
4674 (defun gnus-id-to-thread (id)
4675   "Return the (sub-)thread where ID appears."
4676   (gnus-gethash id gnus-newsgroup-dependencies))
4677
4678 (defun gnus-id-to-article (id)
4679   "Return the article number of ID."
4680   (let ((thread (gnus-id-to-thread id)))
4681     (when (and thread
4682                (car thread))
4683       (mail-header-number (car thread)))))
4684
4685 (defun gnus-id-to-header (id)
4686   "Return the article headers of ID."
4687   (car (gnus-id-to-thread id)))
4688
4689 (defun gnus-article-displayed-root-p (article)
4690   "Say whether ARTICLE is a root(ish) article."
4691   (let ((level (gnus-summary-thread-level article))
4692         (refs (mail-header-references  (gnus-summary-article-header article)))
4693         particle)
4694     (cond
4695      ((null level) nil)
4696      ((zerop level) t)
4697      ((null refs) t)
4698      ((null (gnus-parent-id refs)) t)
4699      ((and (= 1 level)
4700            (null (setq particle (gnus-id-to-article
4701                                  (gnus-parent-id refs))))
4702            (null (gnus-summary-thread-level particle)))))))
4703
4704 (defun gnus-root-id (id)
4705   "Return the id of the root of the thread where ID appears."
4706   (let (last-id prev)
4707     (while (and id (setq prev (car (gnus-id-to-thread id))))
4708       (setq last-id id
4709             id (gnus-parent-id (mail-header-references prev))))
4710     last-id))
4711
4712 (defun gnus-articles-in-thread (thread)
4713   "Return the list of articles in THREAD."
4714   (cons (mail-header-number (car thread))
4715         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4716
4717 (defun gnus-remove-thread (id &optional dont-remove)
4718   "Remove the thread that has ID in it."
4719   (let (headers thread last-id)
4720     ;; First go up in this thread until we find the root.
4721     (setq last-id (gnus-root-id id)
4722           headers (message-flatten-list (gnus-id-to-thread last-id)))
4723     ;; We have now found the real root of this thread.  It might have
4724     ;; been gathered into some loose thread, so we have to search
4725     ;; through the threads to find the thread we wanted.
4726     (let ((threads gnus-newsgroup-threads)
4727           sub)
4728       (while threads
4729         (setq sub (car threads))
4730         (if (stringp (car sub))
4731             ;; This is a gathered thread, so we look at the roots
4732             ;; below it to find whether this article is in this
4733             ;; gathered root.
4734             (progn
4735               (setq sub (cdr sub))
4736               (while sub
4737                 (when (member (caar sub) headers)
4738                   (setq thread (car threads)
4739                         threads nil
4740                         sub nil))
4741                 (setq sub (cdr sub))))
4742           ;; It's an ordinary thread, so we check it.
4743           (when (eq (car sub) (car headers))
4744             (setq thread sub
4745                   threads nil)))
4746         (setq threads (cdr threads)))
4747       ;; If this article is in no thread, then it's a root.
4748       (if thread
4749           (unless dont-remove
4750             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4751         (setq thread (gnus-id-to-thread last-id)))
4752       (when thread
4753         (prog1
4754             thread                      ; We return this thread.
4755           (unless dont-remove
4756             (if (stringp (car thread))
4757                 (progn
4758                   ;; If we use dummy roots, then we have to remove the
4759                   ;; dummy root as well.
4760                   (when (eq gnus-summary-make-false-root 'dummy)
4761                     ;; We go to the dummy root by going to
4762                     ;; the first sub-"thread", and then one line up.
4763                     (gnus-summary-goto-article
4764                      (mail-header-number (caadr thread)))
4765                     (forward-line -1)
4766                     (gnus-delete-line)
4767                     (gnus-data-compute-positions))
4768                   (setq thread (cdr thread))
4769                   (while thread
4770                     (gnus-remove-thread-1 (car thread))
4771                     (setq thread (cdr thread))))
4772               (gnus-remove-thread-1 thread))))))))
4773
4774 (defun gnus-remove-thread-1 (thread)
4775   "Remove the thread THREAD recursively."
4776   (let ((number (mail-header-number (pop thread)))
4777         d)
4778     (setq thread (reverse thread))
4779     (while thread
4780       (gnus-remove-thread-1 (pop thread)))
4781     (when (setq d (gnus-data-find number))
4782       (goto-char (gnus-data-pos d))
4783       (gnus-summary-show-thread)
4784       (gnus-data-remove
4785        number
4786        (- (point-at-bol)
4787           (prog1
4788               (1+ (point-at-eol))
4789             (gnus-delete-line)))))))
4790
4791 (defun gnus-sort-threads-recursive (threads func)
4792   (sort (mapcar (lambda (thread)
4793                   (cons (car thread)
4794                         (and (cdr thread)
4795                              (gnus-sort-threads-recursive (cdr thread) func))))
4796                 threads) func))
4797
4798 (defun gnus-sort-threads-loop (threads func)
4799   (let* ((superthread (cons nil threads))
4800          (stack (list (cons superthread threads)))
4801          remaining-threads thread)
4802     (while stack
4803       (setq remaining-threads (cdr (car stack)))
4804       (if remaining-threads
4805           (progn (setq thread (car remaining-threads))
4806                  (setcdr (car stack) (cdr remaining-threads))
4807                  (if (cdr thread)
4808                      (push (cons thread (cdr thread)) stack)))
4809         (setq thread (caar stack))
4810         (setcdr thread (sort (cdr thread) func))
4811         (pop stack)))
4812     (cdr superthread)))
4813
4814 (defun gnus-sort-threads (threads)
4815   "Sort THREADS."
4816   (if (not gnus-thread-sort-functions)
4817       threads
4818     (gnus-message 8 "Sorting threads...")
4819     (prog1
4820         (condition-case nil
4821             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4822               (gnus-sort-threads-recursive
4823                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4824           ;; Even after binding max-lisp-eval-depth, the recursive
4825           ;; sorter might fail for very long threads.  In that case,
4826           ;; try using a (less well-tested) non-recursive sorter.
4827           (error (gnus-message 9 "Sorting threads with loop...")
4828                  (gnus-sort-threads-loop
4829                   threads (gnus-make-sort-function
4830                            gnus-thread-sort-functions))))
4831       (gnus-message 8 "Sorting threads...done"))))
4832
4833 (defun gnus-sort-articles (articles)
4834   "Sort ARTICLES."
4835   (when gnus-article-sort-functions
4836     (gnus-message 7 "Sorting articles...")
4837     (prog1
4838         (setq gnus-newsgroup-headers
4839               (sort articles (gnus-make-sort-function
4840                               gnus-article-sort-functions)))
4841       (gnus-message 7 "Sorting articles...done"))))
4842
4843 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4844 (defmacro gnus-thread-header (thread)
4845   "Return header of first article in THREAD.
4846 Note that THREAD must never, ever be anything else than a variable -
4847 using some other form will lead to serious barfage."
4848   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4849   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4850   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4851         (vector thread) 2))
4852
4853 (defsubst gnus-article-sort-by-number (h1 h2)
4854   "Sort articles by article number."
4855   (< (mail-header-number h1)
4856      (mail-header-number h2)))
4857
4858 (defun gnus-thread-sort-by-number (h1 h2)
4859   "Sort threads by root article number."
4860   (gnus-article-sort-by-number
4861    (gnus-thread-header h1) (gnus-thread-header h2)))
4862
4863 (defsubst gnus-article-sort-by-random (h1 h2)
4864   "Sort articles randomly."
4865   (zerop (random 2)))
4866
4867 (defun gnus-thread-sort-by-random (h1 h2)
4868   "Sort threads randomly."
4869   (gnus-article-sort-by-random
4870    (gnus-thread-header h1) (gnus-thread-header h2)))
4871
4872 (defsubst gnus-article-sort-by-lines (h1 h2)
4873   "Sort articles by article Lines header."
4874   (< (mail-header-lines h1)
4875      (mail-header-lines h2)))
4876
4877 (defun gnus-thread-sort-by-lines (h1 h2)
4878   "Sort threads by root article Lines header."
4879   (gnus-article-sort-by-lines
4880    (gnus-thread-header h1) (gnus-thread-header h2)))
4881
4882 (defsubst gnus-article-sort-by-chars (h1 h2)
4883   "Sort articles by octet length."
4884   (< (mail-header-chars h1)
4885      (mail-header-chars h2)))
4886
4887 (defun gnus-thread-sort-by-chars (h1 h2)
4888   "Sort threads by root article octet length."
4889   (gnus-article-sort-by-chars
4890    (gnus-thread-header h1) (gnus-thread-header h2)))
4891
4892 (defsubst gnus-article-sort-by-author (h1 h2)
4893   "Sort articles by root author."
4894   (gnus-string<
4895    (let ((extract (funcall
4896                    gnus-extract-address-components
4897                    (mail-header-from h1))))
4898      (or (car extract) (cadr extract) ""))
4899    (let ((extract (funcall
4900                    gnus-extract-address-components
4901                    (mail-header-from h2))))
4902      (or (car extract) (cadr extract) ""))))
4903
4904 (defun gnus-thread-sort-by-author (h1 h2)
4905   "Sort threads by root author."
4906   (gnus-article-sort-by-author
4907    (gnus-thread-header h1)  (gnus-thread-header h2)))
4908
4909 (defsubst gnus-article-sort-by-recipient (h1 h2)
4910   "Sort articles by recipient."
4911   (gnus-string<
4912    (let ((extract (funcall
4913                    gnus-extract-address-components
4914                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4915      (or (car extract) (cadr extract)))
4916    (let ((extract (funcall
4917                    gnus-extract-address-components
4918                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4919      (or (car extract) (cadr extract)))))
4920
4921 (defun gnus-thread-sort-by-recipient (h1 h2)
4922   "Sort threads by root recipient."
4923   (gnus-article-sort-by-recipient
4924    (gnus-thread-header h1) (gnus-thread-header h2)))
4925
4926 (defsubst gnus-article-sort-by-subject (h1 h2)
4927   "Sort articles by root subject."
4928   (gnus-string<
4929    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4930    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4931
4932 (defun gnus-thread-sort-by-subject (h1 h2)
4933   "Sort threads by root subject."
4934   (gnus-article-sort-by-subject
4935    (gnus-thread-header h1) (gnus-thread-header h2)))
4936
4937 (defsubst gnus-article-sort-by-date (h1 h2)
4938   "Sort articles by root article date."
4939   (time-less-p
4940    (gnus-date-get-time (mail-header-date h1))
4941    (gnus-date-get-time (mail-header-date h2))))
4942
4943 (defun gnus-thread-sort-by-date (h1 h2)
4944   "Sort threads by root article date."
4945   (gnus-article-sort-by-date
4946    (gnus-thread-header h1) (gnus-thread-header h2)))
4947
4948 (defsubst gnus-article-sort-by-score (h1 h2)
4949   "Sort articles by root article score.
4950 Unscored articles will be counted as having a score of zero."
4951   (> (or (cdr (assq (mail-header-number h1)
4952                     gnus-newsgroup-scored))
4953          gnus-summary-default-score 0)
4954      (or (cdr (assq (mail-header-number h2)
4955                     gnus-newsgroup-scored))
4956          gnus-summary-default-score 0)))
4957
4958 (defun gnus-thread-sort-by-score (h1 h2)
4959   "Sort threads by root article score."
4960   (gnus-article-sort-by-score
4961    (gnus-thread-header h1) (gnus-thread-header h2)))
4962
4963 (defun gnus-thread-sort-by-total-score (h1 h2)
4964   "Sort threads by the sum of all scores in the thread.
4965 Unscored articles will be counted as having a score of zero."
4966   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4967
4968 (defun gnus-thread-total-score (thread)
4969   ;; This function find the total score of THREAD.
4970   (cond
4971    ((null thread)
4972     0)
4973    ((consp thread)
4974     (if (stringp (car thread))
4975         (apply gnus-thread-score-function 0
4976                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4977       (gnus-thread-total-score-1 thread)))
4978    (t
4979     (gnus-thread-total-score-1 (list thread)))))
4980
4981 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4982   "Sort threads such that the thread with the most recently arrived article comes first."
4983   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4984
4985 (defun gnus-thread-highest-number (thread)
4986   "Return the highest article number in THREAD."
4987   (apply 'max (mapcar (lambda (header)
4988                         (mail-header-number header))
4989                       (message-flatten-list thread))))
4990
4991 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4992   "Sort threads such that the thread with the most recently dated article comes first."
4993   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4994
4995 ; Since this is called not only to sort the top-level threads, but
4996 ; also in recursive sorts to order the articles within a thread, each
4997 ; article will be processed many times.  Thus it speeds things up
4998 ; quite a bit to use gnus-date-get-time, which caches the time value.
4999 (defun gnus-thread-latest-date (thread)
5000   "Return the highest article date in THREAD."
5001   (apply 'max
5002          (mapcar (lambda (header) (gnus-float-time
5003                                    (gnus-date-get-time
5004                                     (mail-header-date header))))
5005                  (message-flatten-list thread))))
5006
5007 (defun gnus-thread-total-score-1 (root)
5008   ;; This function find the total score of the thread below ROOT.
5009   (setq root (car root))
5010   (apply gnus-thread-score-function
5011          (or (append
5012               (mapcar 'gnus-thread-total-score
5013                       (cdr (gnus-id-to-thread (mail-header-id root))))
5014               (when (> (mail-header-number root) 0)
5015                 (list (or (cdr (assq (mail-header-number root)
5016                                      gnus-newsgroup-scored))
5017                           gnus-summary-default-score 0))))
5018              (list gnus-summary-default-score)
5019              '(0))))
5020
5021 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5022 (defvar gnus-tmp-prev-subject nil)
5023 (defvar gnus-tmp-false-parent nil)
5024 (defvar gnus-tmp-root-expunged nil)
5025 (defvar gnus-tmp-dummy-line nil)
5026
5027 (defun gnus-extra-header (type &optional header)
5028   "Return the extra header of TYPE."
5029   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5030       ""))
5031
5032 (defvar gnus-tmp-thread-tree-header-string "")
5033
5034 (defcustom gnus-sum-thread-tree-root "> "
5035   "With %B spec, used for the root of a thread.
5036 If nil, use subject instead."
5037   :version "22.1"
5038   :type '(radio (const :format "%v  " nil) string)
5039   :group 'gnus-thread)
5040
5041 (defcustom gnus-sum-thread-tree-false-root "> "
5042   "With %B spec, used for a false root of a thread.
5043 If nil, use subject instead."
5044   :version "22.1"
5045   :type '(radio (const :format "%v  " nil) string)
5046   :group 'gnus-thread)
5047
5048 (defcustom gnus-sum-thread-tree-single-indent ""
5049   "With %B spec, used for a thread with just one message.
5050 If nil, use subject instead."
5051   :version "22.1"
5052   :type '(radio (const :format "%v  " nil) string)
5053   :group 'gnus-thread)
5054
5055 (defcustom gnus-sum-thread-tree-vertical "| "
5056   "With %B spec, used for drawing a vertical line."
5057   :version "22.1"
5058   :type 'string
5059   :group 'gnus-thread)
5060
5061 (defcustom gnus-sum-thread-tree-indent "  "
5062   "With %B spec, used for indenting."
5063   :version "22.1"
5064   :type 'string
5065   :group 'gnus-thread)
5066
5067 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5068   "With %B spec, used for a leaf with brothers."
5069   :version "22.1"
5070   :type 'string
5071   :group 'gnus-thread)
5072
5073 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5074   "With %B spec, used for a leaf without brothers."
5075   :version "22.1"
5076   :type 'string
5077   :group 'gnus-thread)
5078
5079 (defcustom gnus-summary-display-while-building nil
5080   "If non-nil, show and update the summary buffer as it's being built.
5081 If the value is t, update the buffer after every line is inserted.  If
5082 the value is an integer (N), update the display every N lines."
5083   :version "22.1"
5084   :group 'gnus-thread
5085   :type '(choice (const :tag "off" nil)
5086                  number
5087                  (const :tag "frequently" t)))
5088
5089 (defun gnus-summary-prepare-threads (threads)
5090   "Prepare summary buffer from THREADS and indentation LEVEL.
5091 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5092 or a straight list of headers."
5093   (gnus-message 7 "Generating summary...")
5094
5095   (setq gnus-newsgroup-threads threads)
5096   (beginning-of-line)
5097
5098   (let ((gnus-tmp-level 0)
5099         (default-score (or gnus-summary-default-score 0))
5100         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5101         (building-line-count gnus-summary-display-while-building)
5102         (building-count (integerp gnus-summary-display-while-building))
5103         thread number subject stack state gnus-tmp-gathered beg-match
5104         new-roots gnus-tmp-new-adopts thread-end simp-subject
5105         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5106         gnus-tmp-replied gnus-tmp-subject-or-nil
5107         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5108         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5109         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5110         tree-stack)
5111
5112     (setq gnus-tmp-prev-subject nil
5113           gnus-tmp-thread-tree-header-string "")
5114
5115     (if (vectorp (car threads))
5116         ;; If this is a straight (sic) list of headers, then a
5117         ;; threaded summary display isn't required, so we just create
5118         ;; an unthreaded one.
5119         (gnus-summary-prepare-unthreaded threads)
5120
5121       ;; Do the threaded display.
5122
5123       (if gnus-summary-display-while-building
5124           (switch-to-buffer (buffer-name)))
5125       (while (or threads stack gnus-tmp-new-adopts new-roots)
5126
5127         (if (and (= gnus-tmp-level 0)
5128                  (or (not stack)
5129                      (= (caar stack) 0))
5130                  (not gnus-tmp-false-parent)
5131                  (or gnus-tmp-new-adopts new-roots))
5132             (if gnus-tmp-new-adopts
5133                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5134                       thread (list (car gnus-tmp-new-adopts))
5135                       gnus-tmp-header (caar thread)
5136                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5137               (when new-roots
5138                 (setq thread (list (car new-roots))
5139                       gnus-tmp-header (caar thread)
5140                       new-roots (cdr new-roots))))
5141
5142           (if threads
5143               ;; If there are some threads, we do them before the
5144               ;; threads on the stack.
5145               (setq thread threads
5146                     gnus-tmp-header (caar thread))
5147             ;; There were no current threads, so we pop something off
5148             ;; the stack.
5149             (setq state (car stack)
5150                   gnus-tmp-level (car state)
5151                   tree-stack (cadr state)
5152                   thread (caddr state)
5153                   stack (cdr stack)
5154                   gnus-tmp-header (caar thread))))
5155
5156         (setq gnus-tmp-false-parent nil)
5157         (setq gnus-tmp-root-expunged nil)
5158         (setq thread-end nil)
5159
5160         (if (stringp gnus-tmp-header)
5161             ;; The header is a dummy root.
5162             (cond
5163              ((eq gnus-summary-make-false-root 'adopt)
5164               ;; We let the first article adopt the rest.
5165               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5166                                                (cddar thread)))
5167               (setq gnus-tmp-gathered
5168                     (nconc (mapcar
5169                             (lambda (h) (mail-header-number (car h)))
5170                             (cddar thread))
5171                            gnus-tmp-gathered))
5172               (setq thread (cons (list (caar thread)
5173                                        (cadar thread))
5174                                  (cdr thread)))
5175               (setq gnus-tmp-level -1
5176                     gnus-tmp-false-parent t))
5177              ((eq gnus-summary-make-false-root 'empty)
5178               ;; We print adopted articles with empty subject fields.
5179               (setq gnus-tmp-gathered
5180                     (nconc (mapcar
5181                             (lambda (h) (mail-header-number (car h)))
5182                             (cddar thread))
5183                            gnus-tmp-gathered))
5184               (setq gnus-tmp-level -1))
5185              ((eq gnus-summary-make-false-root 'dummy)
5186               ;; We remember that we probably want to output a dummy
5187               ;; root.
5188               (setq gnus-tmp-dummy-line gnus-tmp-header)
5189               (setq gnus-tmp-prev-subject gnus-tmp-header))
5190              (t
5191               ;; We do not make a root for the gathered
5192               ;; sub-threads at all.
5193               (setq gnus-tmp-level -1)))
5194
5195           (setq number (mail-header-number gnus-tmp-header)
5196                 subject (mail-header-subject gnus-tmp-header)
5197                 simp-subject (gnus-simplify-subject-fully subject))
5198
5199           (cond
5200            ;; If the thread has changed subject, we might want to make
5201            ;; this subthread into a root.
5202            ((and (null gnus-thread-ignore-subject)
5203                  (not (zerop gnus-tmp-level))
5204                  gnus-tmp-prev-subject
5205                  (not (string= gnus-tmp-prev-subject simp-subject)))
5206             (setq new-roots (nconc new-roots (list (car thread)))
5207                   thread-end t
5208                   gnus-tmp-header nil))
5209            ;; If the article lies outside the current limit,
5210            ;; then we do not display it.
5211            ((not (memq number gnus-newsgroup-limit))
5212             (setq gnus-tmp-gathered
5213                   (nconc (mapcar
5214                           (lambda (h) (mail-header-number (car h)))
5215                           (cdar thread))
5216                          gnus-tmp-gathered))
5217             (setq gnus-tmp-new-adopts (if (cdar thread)
5218                                           (append gnus-tmp-new-adopts
5219                                                   (cdar thread))
5220                                         gnus-tmp-new-adopts)
5221                   thread-end t
5222                   gnus-tmp-header nil)
5223             (when (zerop gnus-tmp-level)
5224               (setq gnus-tmp-root-expunged t)))
5225            ;; Perhaps this article is to be marked as read?
5226            ((and gnus-summary-mark-below
5227                  (< (or (cdr (assq number gnus-newsgroup-scored))
5228                         default-score)
5229                     gnus-summary-mark-below)
5230                  ;; Don't touch sparse articles.
5231                  (not (gnus-summary-article-sparse-p number))
5232                  (not (gnus-summary-article-ancient-p number)))
5233             (setq gnus-newsgroup-unreads
5234                   (delq number gnus-newsgroup-unreads))
5235             (if gnus-newsgroup-auto-expire
5236                 (setq gnus-newsgroup-expirable
5237                       (gnus-add-to-sorted-list
5238                        gnus-newsgroup-expirable number))
5239               (push (cons number gnus-low-score-mark)
5240                     gnus-newsgroup-reads))))
5241
5242           (when gnus-tmp-header
5243             ;; We may have an old dummy line to output before this
5244             ;; article.
5245             (when (and gnus-tmp-dummy-line
5246                        (gnus-subject-equal
5247                         gnus-tmp-dummy-line
5248                         (mail-header-subject gnus-tmp-header)))
5249               (gnus-summary-insert-dummy-line
5250                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5251               (setq gnus-tmp-dummy-line nil))
5252
5253             ;; Compute the mark.
5254             (setq gnus-tmp-unread (gnus-article-mark number))
5255
5256             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5257                                   gnus-tmp-header gnus-tmp-level)
5258                   gnus-newsgroup-data)
5259
5260             ;; Actually insert the line.
5261             (setq
5262              gnus-tmp-subject-or-nil
5263              (cond
5264               ((and gnus-thread-ignore-subject
5265                     gnus-tmp-prev-subject
5266                     (not (string= gnus-tmp-prev-subject simp-subject)))
5267                subject)
5268               ((zerop gnus-tmp-level)
5269                (if (and (eq gnus-summary-make-false-root 'empty)
5270                         (memq number gnus-tmp-gathered)
5271                         gnus-tmp-prev-subject
5272                         (string= gnus-tmp-prev-subject simp-subject))
5273                    gnus-summary-same-subject
5274                  subject))
5275               (t gnus-summary-same-subject)))
5276             (if (and (eq gnus-summary-make-false-root 'adopt)
5277                      (= gnus-tmp-level 1)
5278                      (memq number gnus-tmp-gathered))
5279                 (setq gnus-tmp-opening-bracket ?\<
5280                       gnus-tmp-closing-bracket ?\>)
5281               (setq gnus-tmp-opening-bracket ?\[
5282                     gnus-tmp-closing-bracket ?\]))
5283             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5284                 (gnus-make-thread-indent-array
5285                  (max (* 2 (length gnus-thread-indent-array))
5286                       gnus-tmp-level)))
5287             (setq
5288              gnus-tmp-indentation
5289              (aref gnus-thread-indent-array gnus-tmp-level)
5290              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5291              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5292                                 gnus-summary-default-score 0)
5293              gnus-tmp-score-char
5294              (if (or (null gnus-summary-default-score)
5295                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5296                          gnus-summary-zcore-fuzz))
5297                  ?                      ;Whitespace
5298                (if (< gnus-tmp-score gnus-summary-default-score)
5299                    gnus-score-below-mark gnus-score-over-mark))
5300              gnus-tmp-replied
5301              (cond ((memq number gnus-newsgroup-processable)
5302                     gnus-process-mark)
5303                    ((memq number gnus-newsgroup-cached)
5304                     gnus-cached-mark)
5305                    ((memq number gnus-newsgroup-replied)
5306                     gnus-replied-mark)
5307                    ((memq number gnus-newsgroup-forwarded)
5308                     gnus-forwarded-mark)
5309                    ((memq number gnus-newsgroup-saved)
5310                     gnus-saved-mark)
5311                    ((memq number gnus-newsgroup-recent)
5312                     gnus-recent-mark)
5313                    ((memq number gnus-newsgroup-unseen)
5314                     gnus-unseen-mark)
5315                    (t gnus-no-mark))
5316              gnus-tmp-downloaded
5317              (cond ((memq number gnus-newsgroup-undownloaded)
5318                     gnus-undownloaded-mark)
5319                    (gnus-newsgroup-agentized
5320                     gnus-downloaded-mark)
5321                    (t
5322                     gnus-no-mark))
5323              gnus-tmp-from (mail-header-from gnus-tmp-header)
5324              gnus-tmp-name
5325              (cond
5326               ((string-match "<[^>]+> *$" gnus-tmp-from)
5327                (setq beg-match (match-beginning 0))
5328                (or (and (string-match "^\".+\"" gnus-tmp-from)
5329                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5330                    (substring gnus-tmp-from 0 beg-match)))
5331               ((string-match "(.+)" gnus-tmp-from)
5332                (substring gnus-tmp-from
5333                           (1+ (match-beginning 0)) (1- (match-end 0))))
5334               (t gnus-tmp-from))
5335
5336              ;; Do the %B string
5337              gnus-tmp-thread-tree-header-string
5338              (cond
5339               ((not gnus-show-threads) "")
5340               ((zerop gnus-tmp-level)
5341                (cond ((cdar thread)
5342                       (or gnus-sum-thread-tree-root subject))
5343                      (gnus-tmp-new-adopts
5344                       (or gnus-sum-thread-tree-false-root subject))
5345                      (t
5346                       (or gnus-sum-thread-tree-single-indent subject))))
5347               (t
5348                (concat (apply 'concat
5349                               (mapcar (lambda (item)
5350                                         (if (= item 1)
5351                                             gnus-sum-thread-tree-vertical
5352                                           gnus-sum-thread-tree-indent))
5353                                       (cdr (reverse tree-stack))))
5354                        (if (nth 1 thread)
5355                            gnus-sum-thread-tree-leaf-with-other
5356                          gnus-sum-thread-tree-single-leaf)))))
5357             (when (string= gnus-tmp-name "")
5358               (setq gnus-tmp-name gnus-tmp-from))
5359             (unless (numberp gnus-tmp-lines)
5360               (setq gnus-tmp-lines -1))
5361             (if (= gnus-tmp-lines -1)
5362                 (setq gnus-tmp-lines "?")
5363               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5364               (gnus-put-text-property
5365                (point)
5366                (progn (eval gnus-summary-line-format-spec) (point))
5367                'gnus-number number)
5368               (when gnus-visual-p
5369                 (forward-line -1)
5370                 (gnus-run-hooks 'gnus-summary-update-hook)
5371                 (forward-line 1))
5372
5373               (setq gnus-tmp-prev-subject simp-subject)))
5374
5375         (when (nth 1 thread)
5376           (push (list (max 0 gnus-tmp-level)
5377                       (copy-sequence tree-stack)
5378                       (nthcdr 1 thread))
5379                 stack))
5380         (push (if (nth 1 thread) 1 0) tree-stack)
5381         (incf gnus-tmp-level)
5382         (setq threads (if thread-end nil (cdar thread)))
5383         (if gnus-summary-display-while-building
5384             (if building-count
5385                 (progn
5386                   ;; use a set frequency
5387                   (setq building-line-count (1- building-line-count))
5388                   (when (= building-line-count 0)
5389                     (sit-for 0)
5390                     (setq building-line-count
5391                           gnus-summary-display-while-building)))
5392               ;; always
5393               (sit-for 0)))
5394         (unless threads
5395           (setq gnus-tmp-level 0)))))
5396   (gnus-message 7 "Generating summary...done"))
5397
5398 (defun gnus-summary-prepare-unthreaded (headers)
5399   "Generate an unthreaded summary buffer based on HEADERS."
5400   (let (header number mark)
5401
5402     (beginning-of-line)
5403
5404     (while headers
5405       ;; We may have to root out some bad articles...
5406       (when (memq (setq number (mail-header-number
5407                                 (setq header (pop headers))))
5408                   gnus-newsgroup-limit)
5409         ;; Mark article as read when it has a low score.
5410         (when (and gnus-summary-mark-below
5411                    (< (or (cdr (assq number gnus-newsgroup-scored))
5412                           gnus-summary-default-score 0)
5413                       gnus-summary-mark-below)
5414                    (not (gnus-summary-article-ancient-p number)))
5415           (setq gnus-newsgroup-unreads
5416                 (delq number gnus-newsgroup-unreads))
5417           (if gnus-newsgroup-auto-expire
5418               (push number gnus-newsgroup-expirable)
5419             (push (cons number gnus-low-score-mark)
5420                   gnus-newsgroup-reads)))
5421
5422         (setq mark (gnus-article-mark number))
5423         (push (gnus-data-make number mark (1+ (point)) header 0)
5424               gnus-newsgroup-data)
5425         (gnus-summary-insert-line
5426          header 0 number
5427          (memq number gnus-newsgroup-undownloaded)
5428          mark (memq number gnus-newsgroup-replied)
5429          (memq number gnus-newsgroup-expirable)
5430          (mail-header-subject header) nil
5431          (cdr (assq number gnus-newsgroup-scored))
5432          (memq number gnus-newsgroup-processable))))))
5433
5434 (defun gnus-summary-remove-list-identifiers ()
5435   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5436   (let ((regexp (if (consp gnus-list-identifiers)
5437                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5438                   gnus-list-identifiers))
5439         changed subject)
5440     (when regexp
5441       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5442       (dolist (header gnus-newsgroup-headers)
5443         (setq subject (mail-header-subject header)
5444               changed nil)
5445         (while (string-match regexp subject)
5446           (setq subject
5447                 (concat (substring subject 0 (match-beginning 1))
5448                         (substring subject (match-end 0)))
5449                 changed t))
5450         (when changed
5451           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5452             (setq subject
5453                   (concat (substring subject 0 (match-beginning 1))
5454                           (substring subject (match-end 1)))))
5455           (mail-header-set-subject header subject))))))
5456
5457 (defun gnus-fetch-headers (articles)
5458   "Fetch headers of ARTICLES."
5459   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5460     (gnus-message 5 "Fetching headers for %s..." name)
5461     (prog1
5462         (if (eq 'nov
5463                 (setq gnus-headers-retrieved-by
5464                       (gnus-retrieve-headers
5465                        articles gnus-newsgroup-name
5466                        ;; We might want to fetch old headers, but
5467                        ;; not if there is only 1 article.
5468                        (and (or (and
5469                                  (not (eq gnus-fetch-old-headers 'some))
5470                                  (not (numberp gnus-fetch-old-headers)))
5471                                 (> (length articles) 1))
5472                             gnus-fetch-old-headers))))
5473             (gnus-get-newsgroup-headers-xover
5474              articles nil nil gnus-newsgroup-name t)
5475           (gnus-get-newsgroup-headers))
5476       (gnus-message 5 "Fetching headers for %s...done" name))))
5477
5478 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5479   "Select newsgroup GROUP.
5480 If READ-ALL is non-nil, all articles in the group are selected.
5481 If SELECT-ARTICLES, only select those articles from GROUP."
5482   (let* ((entry (gnus-group-entry group))
5483          ;;!!! Dirty hack; should be removed.
5484          (gnus-summary-ignore-duplicates
5485           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5486               t
5487             gnus-summary-ignore-duplicates))
5488          (info (nth 2 entry))
5489          charset articles fetched-articles cached)
5490
5491     (unless (gnus-check-server
5492              (set (make-local-variable 'gnus-current-select-method)
5493                   (gnus-find-method-for-group group)))
5494       (error "Couldn't open server"))
5495     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5496
5497     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5498         (gnus-activate-group group)     ; Or we can activate it...
5499         (progn                          ; Or we bug out.
5500           (when (equal major-mode 'gnus-summary-mode)
5501             (gnus-kill-buffer (current-buffer)))
5502           (error
5503            "Couldn't activate group %s: %s"
5504            (mm-decode-coding-string group charset)
5505            (mm-decode-coding-string (gnus-status-message group) charset))))
5506
5507     (unless (gnus-request-group group t)
5508         (when (equal major-mode 'gnus-summary-mode)
5509           (gnus-kill-buffer (current-buffer)))
5510         (error "Couldn't request group %s: %s"
5511                (mm-decode-coding-string group charset)
5512                (mm-decode-coding-string (gnus-status-message group) charset)))
5513
5514     (when gnus-agent
5515       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5516
5517       (setq gnus-summary-use-undownloaded-faces
5518             (gnus-agent-find-parameter
5519              group
5520              'agent-enable-undownloaded-faces)))
5521
5522     (setq gnus-newsgroup-name group
5523           gnus-newsgroup-unselected nil
5524           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5525
5526     (let ((display (gnus-group-find-parameter group 'display)))
5527       (setq gnus-newsgroup-display
5528             (cond
5529              ((not (zerop (or (car-safe read-all) 0)))
5530               ;; The user entered the group with C-u SPC/RET, let's show
5531               ;; all articles.
5532               'gnus-not-ignore)
5533              ((eq display 'all)
5534               'gnus-not-ignore)
5535              ((arrayp display)
5536               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5537              ((numberp display)
5538               ;; The following is probably the "correct" solution, but
5539               ;; it makes Gnus fetch all headers and then limit the
5540               ;; articles (which is slow), so instead we hack the
5541               ;; select-articles parameter instead. -- Simon Josefsson
5542               ;; <jas@kth.se>
5543               ;;
5544               ;; (gnus-byte-compile
5545               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5546               ;;                         display)))))
5547               (setq select-articles
5548                     (gnus-uncompress-range
5549                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5550                              (if (> tmp 0)
5551                                  tmp
5552                                1))
5553                            (cdr (gnus-active group)))))
5554               nil)
5555              (t
5556               nil))))
5557
5558     (gnus-summary-setup-default-charset)
5559
5560     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5561     (when (gnus-virtual-group-p group)
5562       (setq cached gnus-newsgroup-cached))
5563
5564     (setq gnus-newsgroup-unreads
5565           (gnus-sorted-ndifference
5566            (gnus-sorted-ndifference gnus-newsgroup-unreads
5567                                     gnus-newsgroup-marked)
5568            gnus-newsgroup-dormant))
5569
5570     (setq gnus-newsgroup-processable nil)
5571
5572     (gnus-update-read-articles group gnus-newsgroup-unreads)
5573
5574     ;; Adjust and set lists of article marks.
5575     (when info
5576       (gnus-adjust-marked-articles info))
5577     (if (setq articles select-articles)
5578         (setq gnus-newsgroup-unselected
5579               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5580       (setq articles (gnus-articles-to-read group read-all)))
5581
5582     (cond
5583      ((null articles)
5584       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5585       'quit)
5586      ((eq articles 0) nil)
5587      (t
5588       ;; Init the dependencies hash table.
5589       (setq gnus-newsgroup-dependencies
5590             (gnus-make-hashtable (length articles)))
5591       (gnus-set-global-variables)
5592       ;; Retrieve the headers and read them in.
5593
5594       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5595
5596       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5597       (when cached
5598         (setq gnus-newsgroup-cached cached))
5599
5600       ;; Suppress duplicates?
5601       (when gnus-suppress-duplicates
5602         (gnus-dup-suppress-articles))
5603
5604       ;; Set the initial limit.
5605       (setq gnus-newsgroup-limit (copy-sequence articles))
5606       ;; Remove canceled articles from the list of unread articles.
5607       (setq fetched-articles
5608             (mapcar (lambda (headers) (mail-header-number headers))
5609                     gnus-newsgroup-headers))
5610       (setq gnus-newsgroup-articles fetched-articles)
5611       (setq gnus-newsgroup-unreads
5612             (gnus-sorted-nintersection
5613              gnus-newsgroup-unreads fetched-articles))
5614       (gnus-compute-unseen-list)
5615
5616       ;; Removed marked articles that do not exist.
5617       (gnus-update-missing-marks
5618        (gnus-sorted-difference articles fetched-articles))
5619       ;; We might want to build some more threads first.
5620       (when (and gnus-fetch-old-headers
5621                  (eq gnus-headers-retrieved-by 'nov))
5622         (if (eq gnus-fetch-old-headers 'invisible)
5623             (gnus-build-all-threads)
5624           (gnus-build-old-threads)))
5625       ;; Let the Gnus agent mark articles as read.
5626       (when gnus-agent
5627         (gnus-agent-get-undownloaded-list))
5628       ;; Remove list identifiers from subject
5629       (when gnus-list-identifiers
5630         (gnus-summary-remove-list-identifiers))
5631       ;; Check whether auto-expire is to be done in this group.
5632       (setq gnus-newsgroup-auto-expire
5633             (gnus-group-auto-expirable-p group))
5634       ;; Set up the article buffer now, if necessary.
5635       (unless (and gnus-single-article-buffer
5636                    (equal gnus-article-buffer "*Article*"))
5637         (gnus-article-setup-buffer))
5638       ;; First and last article in this newsgroup.
5639       (when gnus-newsgroup-headers
5640         (setq gnus-newsgroup-begin
5641               (mail-header-number (car gnus-newsgroup-headers))
5642               gnus-newsgroup-end
5643               (mail-header-number
5644                (gnus-last-element gnus-newsgroup-headers))))
5645       ;; GROUP is successfully selected.
5646       (or gnus-newsgroup-headers t)))))
5647
5648 (defun gnus-compute-unseen-list ()
5649   ;; The `seen' marks are treated specially.
5650   (if (not gnus-newsgroup-seen)
5651       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5652     (setq gnus-newsgroup-unseen
5653           (gnus-inverse-list-range-intersection
5654            gnus-newsgroup-articles gnus-newsgroup-seen))))
5655
5656 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5657
5658 (defun gnus-summary-display-make-predicate (display)
5659   (require 'gnus-agent)
5660   (when (= (length display) 1)
5661     (setq display (car display)))
5662   (unless gnus-summary-display-cache
5663     (dolist (elem (append '((unread . unread)
5664                             (read . read)
5665                             (unseen . unseen))
5666                           gnus-article-mark-lists))
5667       (push (cons (cdr elem)
5668                   (gnus-byte-compile
5669                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5670             gnus-summary-display-cache)))
5671   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5672         (gnus-category-predicate-cache gnus-summary-display-cache))
5673     (gnus-get-predicate display)))
5674
5675 ;; Uses the dynamically bound `number' variable.
5676 (defvar number)
5677 (defun gnus-article-marked-p (type &optional article)
5678   (let ((article (or article number)))
5679     (cond
5680      ((eq type 'tick)
5681       (memq article gnus-newsgroup-marked))
5682      ((eq type 'spam)
5683       (memq article gnus-newsgroup-spam-marked))
5684      ((eq type 'unsend)
5685       (memq article gnus-newsgroup-unsendable))
5686      ((eq type 'undownload)
5687       (memq article gnus-newsgroup-undownloaded))
5688      ((eq type 'download)
5689       (memq article gnus-newsgroup-downloadable))
5690      ((eq type 'unread)
5691       (memq article gnus-newsgroup-unreads))
5692      ((eq type 'read)
5693       (memq article gnus-newsgroup-reads))
5694      ((eq type 'dormant)
5695       (memq article gnus-newsgroup-dormant) )
5696      ((eq type 'expire)
5697       (memq article gnus-newsgroup-expirable))
5698      ((eq type 'reply)
5699       (memq article gnus-newsgroup-replied))
5700      ((eq type 'killed)
5701       (memq article gnus-newsgroup-killed))
5702      ((eq type 'bookmark)
5703       (assq article gnus-newsgroup-bookmarks))
5704      ((eq type 'score)
5705       (assq article gnus-newsgroup-scored))
5706      ((eq type 'save)
5707       (memq article gnus-newsgroup-saved))
5708      ((eq type 'cache)
5709       (memq article gnus-newsgroup-cached))
5710      ((eq type 'forward)
5711       (memq article gnus-newsgroup-forwarded))
5712      ((eq type 'seen)
5713       (not (memq article gnus-newsgroup-unseen)))
5714      ((eq type 'recent)
5715       (memq article gnus-newsgroup-recent))
5716      (t t))))
5717
5718 (defun gnus-articles-to-read (group &optional read-all)
5719   "Find out what articles the user wants to read."
5720   (let* ((articles
5721           ;; Select all articles if `read-all' is non-nil, or if there
5722           ;; are no unread articles.
5723           (if (or read-all
5724                   (and (zerop (length gnus-newsgroup-marked))
5725                        (zerop (length gnus-newsgroup-unreads)))
5726                   ;; Fetch all if the predicate is non-nil.
5727                   gnus-newsgroup-display)
5728               ;; We want to select the headers for all the articles in
5729               ;; the group, so we select either all the active
5730               ;; articles in the group, or (if that's nil), the
5731               ;; articles in the cache.
5732               (or
5733                (if gnus-newsgroup-maximum-articles
5734                    (let ((active (gnus-active group)))
5735                      (gnus-uncompress-range
5736                       (cons (max (car active)
5737                                  (- (cdr active)
5738                                     gnus-newsgroup-maximum-articles
5739                                     -1))
5740                             (cdr active))))
5741                  (gnus-uncompress-range (gnus-active group)))
5742                (gnus-cache-articles-in-group group))
5743             ;; Select only the "normal" subset of articles.
5744             (gnus-sorted-nunion
5745              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5746              gnus-newsgroup-unreads)))
5747          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5748          (scored (length scored-list))
5749          (number (length articles))
5750          (marked (+ (length gnus-newsgroup-marked)
5751                     (length gnus-newsgroup-dormant)))
5752          (select
5753           (cond
5754            ((numberp read-all)
5755             read-all)
5756            ((numberp gnus-newsgroup-display)
5757             gnus-newsgroup-display)
5758            (t
5759             (condition-case ()
5760                 (cond
5761                  ((and (or (<= scored marked) (= scored number))
5762                        (numberp gnus-large-newsgroup)
5763                        (> number gnus-large-newsgroup))
5764                   (let* ((cursor-in-echo-area nil)
5765                          (initial (gnus-parameter-large-newsgroup-initial
5766                                    gnus-newsgroup-name))
5767                          (input
5768                           (read-string
5769                            (format
5770                             "How many articles from %s (%s %d): "
5771                             (gnus-group-decoded-name gnus-newsgroup-name)
5772                             (if initial "max" "default")
5773                             number)
5774                            (if initial
5775                                (cons (number-to-string initial)
5776                                      0)))))
5777                     (if (string-match "^[ \t]*$" input) number input)))
5778                  ((and (> scored marked) (< scored number)
5779                        (> (- scored number) 20))
5780                   (let ((input
5781                          (read-string
5782                           (format "%s %s (%d scored, %d total): "
5783                                   "How many articles from"
5784                                   (gnus-group-decoded-name group)
5785                                   scored number))))
5786                     (if (string-match "^[ \t]*$" input)
5787                         number input)))
5788                  (t number))
5789               (quit
5790                (message "Quit getting the articles to read")
5791                nil))))))
5792     (setq select (if (stringp select) (string-to-number select) select))
5793     (if (or (null select) (zerop select))
5794         select
5795       (if (and (not (zerop scored)) (<= (abs select) scored))
5796           (progn
5797             (setq articles (sort scored-list '<))
5798             (setq number (length articles)))
5799         (setq articles (copy-sequence articles)))
5800
5801       (when (< (abs select) number)
5802         (if (< select 0)
5803             ;; Select the N oldest articles.
5804             (setcdr (nthcdr (1- (abs select)) articles) nil)
5805           ;; Select the N most recent articles.
5806           (setq articles (nthcdr (- number select) articles))))
5807       (setq gnus-newsgroup-unselected
5808             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5809       (when gnus-alter-articles-to-read-function
5810         (setq articles
5811               (sort
5812                (funcall gnus-alter-articles-to-read-function
5813                         gnus-newsgroup-name articles)
5814                '<)))
5815       articles)))
5816
5817 (defun gnus-killed-articles (killed articles)
5818   (let (out)
5819     (while articles
5820       (when (inline (gnus-member-of-range (car articles) killed))
5821         (push (car articles) out))
5822       (setq articles (cdr articles)))
5823     out))
5824
5825 (defun gnus-uncompress-marks (marks)
5826   "Uncompress the mark ranges in MARKS."
5827   (let ((uncompressed '(score bookmark))
5828         out)
5829     (while marks
5830       (if (memq (caar marks) uncompressed)
5831           (push (car marks) out)
5832         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5833       (setq marks (cdr marks)))
5834     out))
5835
5836 (defun gnus-article-mark-to-type (mark)
5837   "Return the type of MARK."
5838   (or (cadr (assq mark gnus-article-special-mark-lists))
5839       'list))
5840
5841 (defun gnus-article-unpropagatable-p (mark)
5842   "Return whether MARK should be propagated to back end."
5843   (memq mark gnus-article-unpropagated-mark-lists))
5844
5845 (defun gnus-adjust-marked-articles (info)
5846   "Set all article lists and remove all marks that are no longer valid."
5847   (let* ((marked-lists (gnus-info-marks info))
5848          (active (gnus-active (gnus-info-group info)))
5849          (min (car active))
5850          (max (cdr active))
5851          (types gnus-article-mark-lists)
5852          marks var articles article mark mark-type
5853          bgn end)
5854
5855     (dolist (marks marked-lists)
5856       (setq mark (car marks)
5857             mark-type (gnus-article-mark-to-type mark)
5858             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5859
5860       ;; We set the variable according to the type of the marks list,
5861       ;; and then adjust the marks to a subset of the active articles.
5862       (cond
5863        ;; Adjust "simple" lists - compressed yet unsorted
5864        ((eq mark-type 'list)
5865         ;; Simultaneously uncompress and clip to active range
5866         ;; See gnus-uncompress-range for a description of possible marks
5867         (let (l lh)
5868           (if (not (cadr marks))
5869               (set var nil)
5870             (setq articles (if (numberp (cddr marks))
5871                                (list (cdr marks))
5872                              (cdr marks))
5873                   lh (cons nil nil)
5874                   l lh)
5875
5876             (while (setq article (pop articles))
5877               (cond ((consp article)
5878                      (setq bgn (max (car article) min)
5879                            end (min (cdr article) max))
5880                      (while (<= bgn end)
5881                        (setq l (setcdr l (cons bgn nil))
5882                              bgn (1+ bgn))))
5883                     ((and (<= min article)
5884                           (>= max article))
5885                      (setq l (setcdr l (cons article nil))))))
5886             (set var (cdr lh)))))
5887        ;; Adjust assocs.
5888        ((eq mark-type 'tuple)
5889         (set var (setq articles (cdr marks)))
5890         (when (not (listp (cdr (symbol-value var))))
5891           (set var (list (symbol-value var))))
5892         (when (not (listp (cdr articles)))
5893           (setq articles (list articles)))
5894         (while articles
5895           (when (or (not (consp (setq article (pop articles))))
5896                     (< (car article) min)
5897                     (> (car article) max))
5898             (set var (delq article (symbol-value var))))))
5899        ;; Adjust ranges (sloppily).
5900        ((eq mark-type 'range)
5901         (cond
5902          ((eq mark 'seen)
5903           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5904           ;; It should be (seen (NUM1 . NUM2)).
5905           (when (numberp (cddr marks))
5906             (setcdr marks (list (cdr marks))))
5907           (setq articles (cdr marks))
5908           (while (and articles
5909                       (or (and (consp (car articles))
5910                                (> min (cdar articles)))
5911                           (and (numberp (car articles))
5912                                (> min (car articles)))))
5913             (pop articles))
5914           (set var articles))))))))
5915
5916 (defun gnus-update-missing-marks (missing)
5917   "Go through the list of MISSING articles and remove them from the mark lists."
5918   (when missing
5919     (let (var m)
5920       ;; Go through all types.
5921       (dolist (elem gnus-article-mark-lists)
5922         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5923           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5924           (when (symbol-value var)
5925             ;; This list has articles.  So we delete all missing
5926             ;; articles from it.
5927             (setq m missing)
5928             (while m
5929               (set var (delq (pop m) (symbol-value var))))))))))
5930
5931 (defun gnus-update-marks ()
5932   "Enter the various lists of marked articles into the newsgroup info list."
5933   (let ((types gnus-article-mark-lists)
5934         (info (gnus-get-info gnus-newsgroup-name))
5935         type list newmarked symbol delta-marks)
5936     (when info
5937       ;; Add all marks lists to the list of marks lists.
5938       (while (setq type (pop types))
5939         (setq list (symbol-value
5940                     (setq symbol
5941                           (intern (format "gnus-newsgroup-%s" (car type))))))
5942
5943         (when list
5944           ;; Get rid of the entries of the articles that have the
5945           ;; default score.
5946           (when (and (eq (cdr type) 'score)
5947                      gnus-save-score
5948                      list)
5949             (let* ((arts list)
5950                    (prev (cons nil list))
5951                    (all prev))
5952               (while arts
5953                 (if (or (not (consp (car arts)))
5954                         (= (cdar arts) gnus-summary-default-score))
5955                     (setcdr prev (cdr arts))
5956                   (setq prev arts))
5957                 (setq arts (cdr arts)))
5958               (setq list (cdr all)))))
5959
5960         (when (eq (cdr type) 'seen)
5961           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5962
5963         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5964           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5965
5966         (when (and (gnus-check-backend-function
5967                     'request-set-mark gnus-newsgroup-name)
5968                    (not (gnus-article-unpropagatable-p (cdr type))))
5969           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5970                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5971                  (add (gnus-remove-from-range
5972                        (gnus-copy-sequence list) old)))
5973             (when add
5974               (push (list add 'add (list (cdr type))) delta-marks))
5975             (when del
5976               (push (list del 'del (list (cdr type))) delta-marks))))
5977
5978         (when list
5979           (push (cons (cdr type) list) newmarked)))
5980
5981       (when delta-marks
5982         (unless (gnus-check-group gnus-newsgroup-name)
5983           (error "Can't open server for %s" gnus-newsgroup-name))
5984         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5985
5986       ;; Enter these new marks into the info of the group.
5987       (if (nthcdr 3 info)
5988           (setcar (nthcdr 3 info) newmarked)
5989         ;; Add the marks lists to the end of the info.
5990         (when newmarked
5991           (setcdr (nthcdr 2 info) (list newmarked))))
5992
5993       ;; Cut off the end of the info if there's nothing else there.
5994       (let ((i 5))
5995         (while (and (> i 2)
5996                     (not (nth i info)))
5997           (when (nthcdr (decf i) info)
5998             (setcdr (nthcdr i info) nil)))))))
5999
6000 (defun gnus-set-mode-line (where)
6001   "Set the mode line of the article or summary buffers.
6002 If WHERE is `summary', the summary mode line format will be used."
6003   ;; Is this mode line one we keep updated?
6004   (when (and (memq where gnus-updated-mode-lines)
6005              (symbol-value
6006               (intern (format "gnus-%s-mode-line-format-spec" where))))
6007     (let (mode-string)
6008       ;; We evaluate this in the summary buffer since these
6009       ;; variables are buffer-local to that buffer.
6010       (with-current-buffer gnus-summary-buffer
6011         ;; We bind all these variables that are used in the `eval' form
6012         ;; below.
6013         (let* ((mformat (symbol-value
6014                          (intern
6015                           (format "gnus-%s-mode-line-format-spec" where))))
6016                (gnus-tmp-group-name (gnus-mode-string-quote
6017                                      (gnus-group-decoded-name
6018                                       gnus-newsgroup-name)))
6019                (gnus-tmp-article-number (or gnus-current-article 0))
6020                (gnus-tmp-unread gnus-newsgroup-unreads)
6021                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6022                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6023                (gnus-tmp-unread-and-unselected
6024                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6025                             (zerop gnus-tmp-unselected))
6026                        "")
6027                       ((zerop gnus-tmp-unselected)
6028                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6029                       (t (format "{%d(+%d) more}"
6030                                  gnus-tmp-unread-and-unticked
6031                                  gnus-tmp-unselected))))
6032                (gnus-tmp-subject
6033                 (if (and gnus-current-headers
6034                          (vectorp gnus-current-headers))
6035                     (gnus-mode-string-quote
6036                      (mail-header-subject gnus-current-headers))
6037                   ""))
6038                bufname-length max-len
6039                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6040           (setq mode-string (eval mformat))
6041           (setq bufname-length (if (string-match "%b" mode-string)
6042                                    (- (length
6043                                        (buffer-name
6044                                         (if (eq where 'summary)
6045                                             nil
6046                                           (get-buffer gnus-article-buffer))))
6047                                       2)
6048                                  0))
6049           (setq max-len (max 4 (if gnus-mode-non-string-length
6050                                    (- (window-width)
6051                                       gnus-mode-non-string-length
6052                                       bufname-length)
6053                                  (length mode-string))))
6054           ;; We might have to chop a bit of the string off...
6055           (when (> (length mode-string) max-len)
6056             (setq mode-string
6057                   (concat (truncate-string-to-width mode-string (- max-len 3))
6058                           "...")))
6059           ;; Pad the mode string a bit.
6060           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6061       ;; Update the mode line.
6062       (setq mode-line-buffer-identification
6063             (gnus-mode-line-buffer-identification (list mode-string)))
6064       (set-buffer-modified-p t))))
6065
6066 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6067   "Go through the HEADERS list and add all Xrefs to a hash table.
6068 The resulting hash table is returned, or nil if no Xrefs were found."
6069   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6070          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6071          (xref-hashtb (gnus-make-hashtable))
6072          start group entry number xrefs header)
6073     (while headers
6074       (setq header (pop headers))
6075       (when (and (setq xrefs (mail-header-xref header))
6076                  (not (memq (setq number (mail-header-number header))
6077                             unreads)))
6078         (setq start 0)
6079         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6080           (setq start (match-end 0))
6081           (setq group (if prefix
6082                           (concat prefix (substring xrefs (match-beginning 1)
6083                                                     (match-end 1)))
6084                         (substring xrefs (match-beginning 1) (match-end 1))))
6085           (setq number
6086                 (string-to-number (substring xrefs (match-beginning 2)
6087                                           (match-end 2))))
6088           (if (setq entry (gnus-gethash group xref-hashtb))
6089               (setcdr entry (cons number (cdr entry)))
6090             (gnus-sethash group (cons number nil) xref-hashtb)))))
6091     (and start xref-hashtb)))
6092
6093 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6094   "Look through all the headers and mark the Xrefs as read."
6095   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6096         name info xref-hashtb idlist method nth4)
6097     (with-current-buffer gnus-group-buffer
6098       (when (setq xref-hashtb
6099                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6100         (mapatoms
6101          (lambda (group)
6102            (unless (string= from-newsgroup (setq name (symbol-name group)))
6103              (setq idlist (symbol-value group))
6104              ;; Dead groups are not updated.
6105              (and (prog1
6106                       (setq info (gnus-get-info name))
6107                     (when (stringp (setq nth4 (gnus-info-method info)))
6108                       (setq nth4 (gnus-server-to-method nth4))))
6109                   ;; Only do the xrefs if the group has the same
6110                   ;; select method as the group we have just read.
6111                   (or (gnus-methods-equal-p
6112                        nth4 (gnus-find-method-for-group from-newsgroup))
6113                       virtual
6114                       (equal nth4 (setq method (gnus-find-method-for-group
6115                                                 from-newsgroup)))
6116                       (and (equal (car nth4) (car method))
6117                            (equal (nth 1 nth4) (nth 1 method))))
6118                   gnus-use-cross-reference
6119                   (or (not (eq gnus-use-cross-reference t))
6120                       virtual
6121                       ;; Only do cross-references on subscribed
6122                       ;; groups, if that is what is wanted.
6123                       (<= (gnus-info-level info) gnus-level-subscribed))
6124                   (gnus-group-make-articles-read name idlist))))
6125          xref-hashtb)))))
6126
6127 (defun gnus-compute-read-articles (group articles)
6128   (let* ((entry (gnus-group-entry group))
6129          (info (nth 2 entry))
6130          (active (gnus-active group))
6131          ninfo)
6132     (when entry
6133       ;; First peel off all invalid article numbers.
6134       (when active
6135         (let ((ids articles)
6136               id first)
6137           (while (setq id (pop ids))
6138             (when (and first (> id (cdr active)))
6139               ;; We'll end up in this situation in one particular
6140               ;; obscure situation.  If you re-scan a group and get
6141               ;; a new article that is cross-posted to a different
6142               ;; group that has not been re-scanned, you might get
6143               ;; crossposted article that has a higher number than
6144               ;; Gnus believes possible.  So we re-activate this
6145               ;; group as well.  This might mean doing the
6146               ;; crossposting thingy will *increase* the number
6147               ;; of articles in some groups.  Tsk, tsk.
6148               (setq active (or (gnus-activate-group group) active)))
6149             (when (or (> id (cdr active))
6150                       (< id (car active)))
6151               (setq articles (delq id articles))))))
6152       ;; If the read list is nil, we init it.
6153       (if (and active
6154                (null (gnus-info-read info))
6155                (> (car active) 1))
6156           (setq ninfo (cons 1 (1- (car active))))
6157         (setq ninfo (gnus-info-read info)))
6158       ;; Then we add the read articles to the range.
6159       (gnus-add-to-range
6160        ninfo (setq articles (sort articles '<))))))
6161
6162 (defun gnus-group-make-articles-read (group articles)
6163   "Update the info of GROUP to say that ARTICLES are read."
6164   (let* ((num 0)
6165          (entry (gnus-group-entry group))
6166          (info (nth 2 entry))
6167          (active (gnus-active group))
6168          range)
6169     (when entry
6170       (setq range (gnus-compute-read-articles group articles))
6171       (with-current-buffer gnus-group-buffer
6172         (gnus-undo-register
6173           `(progn
6174              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6175              (gnus-info-set-read ',info ',(gnus-info-read info))
6176              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6177              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6178              (gnus-group-update-group ,group t))))
6179       ;; Add the read articles to the range.
6180       (gnus-info-set-read info range)
6181       (gnus-request-set-mark group (list (list range 'add '(read))))
6182       ;; Then we have to re-compute how many unread
6183       ;; articles there are in this group.
6184       (when active
6185         (cond
6186          ((not range)
6187           (setq num (- (1+ (cdr active)) (car active))))
6188          ((not (listp (cdr range)))
6189           (setq num (- (cdr active) (- (1+ (cdr range))
6190                                        (car range)))))
6191          (t
6192           (while range
6193             (if (numberp (car range))
6194                 (setq num (1+ num))
6195               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6196             (setq range (cdr range)))
6197           (setq num (- (cdr active) num))))
6198         ;; Update the number of unread articles.
6199         (setcar entry num)
6200         ;; Update the group buffer.
6201         (unless (gnus-ephemeral-group-p group)
6202           (gnus-group-update-group group t))))))
6203
6204 (defvar gnus-newsgroup-none-id 0)
6205
6206 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6207   (let ((cur nntp-server-buffer)
6208         (dependencies
6209          (or dependencies
6210              (with-current-buffer gnus-summary-buffer
6211                gnus-newsgroup-dependencies)))
6212         headers id end ref number
6213         (mail-parse-charset gnus-newsgroup-charset)
6214         (mail-parse-ignored-charsets
6215          (save-current-buffer (condition-case nil
6216                                   (set-buffer gnus-summary-buffer)
6217                                 (error))
6218                               gnus-newsgroup-ignored-charsets)))
6219     (with-current-buffer nntp-server-buffer
6220       ;; Translate all TAB characters into SPACE characters.
6221       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6222       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6223       (ietf-drums-unfold-fws)
6224       (gnus-run-hooks 'gnus-parse-headers-hook)
6225       (let ((case-fold-search t)
6226             in-reply-to header p lines chars)
6227         (goto-char (point-min))
6228         ;; Search to the beginning of the next header.  Error messages
6229         ;; do not begin with 2 or 3.
6230         (while (re-search-forward "^[23][0-9]+ " nil t)
6231           (setq id nil
6232                 ref nil)
6233           ;; This implementation of this function, with nine
6234           ;; search-forwards instead of the one re-search-forward and
6235           ;; a case (which basically was the old function) is actually
6236           ;; about twice as fast, even though it looks messier.  You
6237           ;; can't have everything, I guess.  Speed and elegance
6238           ;; doesn't always go hand in hand.
6239           (setq
6240            header
6241            (vector
6242             ;; Number.
6243             (prog1
6244                 (setq number (read cur))
6245               (end-of-line)
6246               (setq p (point))
6247               (narrow-to-region (point)
6248                                 (or (and (search-forward "\n.\n" nil t)
6249                                          (- (point) 2))
6250                                     (point))))
6251             ;; Subject.
6252             (progn
6253               (goto-char p)
6254               (if (search-forward "\nsubject:" nil t)
6255                   (funcall gnus-decode-encoded-word-function
6256                            (nnheader-header-value))
6257                 "(none)"))
6258             ;; From.
6259             (progn
6260               (goto-char p)
6261               (if (search-forward "\nfrom:" nil t)
6262                   (funcall gnus-decode-encoded-address-function
6263                            (nnheader-header-value))
6264                 "(nobody)"))
6265             ;; Date.
6266             (progn
6267               (goto-char p)
6268               (if (search-forward "\ndate:" nil t)
6269                   (nnheader-header-value) ""))
6270             ;; Message-ID.
6271             (progn
6272               (goto-char p)
6273               (setq id (if (re-search-forward
6274                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6275                            ;; We do it this way to make sure the Message-ID
6276                            ;; is (somewhat) syntactically valid.
6277                            (buffer-substring (match-beginning 1)
6278                                              (match-end 1))
6279                          ;; If there was no message-id, we just fake one
6280                          ;; to make subsequent routines simpler.
6281                          (nnheader-generate-fake-message-id number))))
6282             ;; References.
6283             (progn
6284               (goto-char p)
6285               (if (search-forward "\nreferences:" nil t)
6286                   (progn
6287                     (setq end (point))
6288                     (prog1
6289                         (nnheader-header-value)
6290                       (setq ref
6291                             (buffer-substring
6292                              (progn
6293                                (end-of-line)
6294                                (search-backward ">" end t)
6295                                (1+ (point)))
6296                              (progn
6297                                (search-backward "<" end t)
6298                                (point))))))
6299                 ;; Get the references from the in-reply-to header if there
6300                 ;; were no references and the in-reply-to header looks
6301                 ;; promising.
6302                 (if (and (search-forward "\nin-reply-to:" nil t)
6303                          (setq in-reply-to (nnheader-header-value))
6304                          (string-match "<[^>]+>" in-reply-to))
6305                     (let (ref2)
6306                       (setq ref (substring in-reply-to (match-beginning 0)
6307                                            (match-end 0)))
6308                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6309                         (setq ref2 (substring in-reply-to (match-beginning 0)
6310                                               (match-end 0)))
6311                         (when (> (length ref2) (length ref))
6312                           (setq ref ref2)))
6313                       ref)
6314                   (setq ref nil))))
6315             ;; Chars.
6316             (progn
6317               (goto-char p)
6318               (if (search-forward "\nchars: " nil t)
6319                   (if (numberp (setq chars (ignore-errors (read cur))))
6320                       chars -1)
6321                 -1))
6322             ;; Lines.
6323             (progn
6324               (goto-char p)
6325               (if (search-forward "\nlines: " nil t)
6326                   (if (numberp (setq lines (ignore-errors (read cur))))
6327                       lines -1)
6328                 -1))
6329             ;; Xref.
6330             (progn
6331               (goto-char p)
6332               (and (search-forward "\nxref:" nil t)
6333                    (nnheader-header-value)))
6334             ;; Extra.
6335             (when gnus-extra-headers
6336               (let ((extra gnus-extra-headers)
6337                     out)
6338                 (while extra
6339                   (goto-char p)
6340                   (when (search-forward
6341                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6342                     (push (cons (car extra) (nnheader-header-value))
6343                           out))
6344                   (pop extra))
6345                 out))))
6346           (when (equal id ref)
6347             (setq ref nil))
6348
6349           (when gnus-alter-header-function
6350             (funcall gnus-alter-header-function header)
6351             (setq id (mail-header-id header)
6352                   ref (gnus-parent-id (mail-header-references header))))
6353
6354           (when (setq header
6355                       (gnus-dependencies-add-header
6356                        header dependencies force-new))
6357             (push header headers))
6358           (goto-char (point-max))
6359           (widen))
6360         (nreverse headers)))))
6361
6362 ;; Goes through the xover lines and returns a list of vectors
6363 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6364                                                   force-new dependencies
6365                                                   group also-fetch-heads)
6366   "Parse the news overview data in the server buffer.
6367 Return a list of headers that match SEQUENCE (see
6368 `nntp-retrieve-headers')."
6369   ;; Get the Xref when the users reads the articles since most/some
6370   ;; NNTP servers do not include Xrefs when using XOVER.
6371   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6372   (let ((mail-parse-charset gnus-newsgroup-charset)
6373         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6374         (cur nntp-server-buffer)
6375         (dependencies (or dependencies gnus-newsgroup-dependencies))
6376         (allp (cond
6377                ((eq gnus-read-all-available-headers t)
6378                 t)
6379                ((and (stringp gnus-read-all-available-headers)
6380                      group)
6381                 (string-match gnus-read-all-available-headers group))
6382                (t
6383                 nil)))
6384         number headers header)
6385     (with-current-buffer nntp-server-buffer
6386       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6387       ;; Allow the user to mangle the headers before parsing them.
6388       (gnus-run-hooks 'gnus-parse-headers-hook)
6389       (goto-char (point-min))
6390       (gnus-parse-without-error
6391         (while (and (or sequence allp)
6392                     (not (eobp)))
6393           (setq number (read cur))
6394           (when (not allp)
6395             (while (and sequence
6396                         (< (car sequence) number))
6397               (setq sequence (cdr sequence))))
6398           (when (and (or allp
6399                          (and sequence
6400                               (eq number (car sequence))))
6401                      (progn
6402                        (setq sequence (cdr sequence))
6403                        (setq header (inline
6404                                       (gnus-nov-parse-line
6405                                        number dependencies force-new)))))
6406             (push header headers))
6407           (forward-line 1)))
6408       ;; A common bug in inn is that if you have posted an article and
6409       ;; then retrieves the active file, it will answer correctly --
6410       ;; the new article is included.  However, a NOV entry for the
6411       ;; article may not have been generated yet, so this may fail.
6412       ;; We work around this problem by retrieving the last few
6413       ;; headers using HEAD.
6414       (if (or (not also-fetch-heads)
6415               (not sequence))
6416           ;; We (probably) got all the headers.
6417           (nreverse headers)
6418         (let ((gnus-nov-is-evil t))
6419           (nconc
6420            (nreverse headers)
6421            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6422              (gnus-get-newsgroup-headers))))))))
6423
6424 (defun gnus-article-get-xrefs ()
6425   "Fill in the Xref value in `gnus-current-headers', if necessary.
6426 This is meant to be called in `gnus-article-internal-prepare-hook'."
6427   (let ((headers (with-current-buffer gnus-summary-buffer
6428                    gnus-current-headers)))
6429     (or (not gnus-use-cross-reference)
6430         (not headers)
6431         (and (mail-header-xref headers)
6432              (not (string= (mail-header-xref headers) "")))
6433         (let ((case-fold-search t)
6434               xref)
6435           (save-restriction
6436             (nnheader-narrow-to-headers)
6437             (goto-char (point-min))
6438             (when (or (and (not (eobp))
6439                            (eq (downcase (char-after)) ?x)
6440                            (looking-at "Xref:"))
6441                       (search-forward "\nXref:" nil t))
6442               (goto-char (1+ (match-end 0)))
6443               (setq xref (buffer-substring (point) (point-at-eol)))
6444               (mail-header-set-xref headers xref)))))))
6445
6446 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6447   "Find article ID and insert the summary line for that article.
6448 OLD-HEADER can either be a header or a line number to insert
6449 the subject line on."
6450   (let* ((line (and (numberp old-header) old-header))
6451          (old-header (and (vectorp old-header) old-header))
6452          (header (cond ((and old-header use-old-header)
6453                         old-header)
6454                        ((and (numberp id)
6455                              (gnus-number-to-header id))
6456                         (gnus-number-to-header id))
6457                        (t
6458                         (gnus-read-header id))))
6459          (number (and (numberp id) id))
6460          d)
6461     (when header
6462       ;; Rebuild the thread that this article is part of and go to the
6463       ;; article we have fetched.
6464       (when (and (not gnus-show-threads)
6465                  old-header)
6466         (when (and number
6467                    (setq d (gnus-data-find (mail-header-number old-header))))
6468           (goto-char (gnus-data-pos d))
6469           (gnus-data-remove
6470            number
6471            (- (point-at-bol)
6472               (prog1
6473                   (1+ (point-at-eol))
6474                 (gnus-delete-line))))))
6475       ;; Remove list identifiers from subject.
6476       (when gnus-list-identifiers
6477         (let ((gnus-newsgroup-headers (list header)))
6478           (gnus-summary-remove-list-identifiers)))
6479       (when old-header
6480         (mail-header-set-number header (mail-header-number old-header)))
6481       (setq gnus-newsgroup-sparse
6482             (delq (setq number (mail-header-number header))
6483                   gnus-newsgroup-sparse))
6484       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6485       (push number gnus-newsgroup-limit)
6486       (gnus-rebuild-thread (mail-header-id header) line)
6487       (gnus-summary-goto-subject number nil t))
6488     (when (and (numberp number)
6489                (> number 0))
6490       ;; We have to update the boundaries even if we can't fetch the
6491       ;; article if ID is a number -- so that the next `P' or `N'
6492       ;; command will fetch the previous (or next) article even
6493       ;; if the one we tried to fetch this time has been canceled.
6494       (when (> number gnus-newsgroup-end)
6495         (setq gnus-newsgroup-end number))
6496       (when (< number gnus-newsgroup-begin)
6497         (setq gnus-newsgroup-begin number))
6498       (setq gnus-newsgroup-unselected
6499             (delq number gnus-newsgroup-unselected)))
6500     ;; Report back a success?
6501     (and header (mail-header-number header))))
6502
6503 ;;; Process/prefix in the summary buffer
6504
6505 (defun gnus-summary-work-articles (n)
6506   "Return a list of articles to be worked upon.
6507 The prefix argument, the list of process marked articles, and the
6508 current article will be taken into consideration."
6509   (with-current-buffer gnus-summary-buffer
6510     (cond
6511      (n
6512       ;; A numerical prefix has been given.
6513       (setq n (prefix-numeric-value n))
6514       (let ((backward (< n 0))
6515             (n (abs (prefix-numeric-value n)))
6516             articles article)
6517         (save-excursion
6518           (while
6519               (and (> n 0)
6520                    (push (setq article (gnus-summary-article-number))
6521                          articles)
6522                    (if backward
6523                        (gnus-summary-find-prev nil article)
6524                      (gnus-summary-find-next nil article)))
6525             (decf n)))
6526         (nreverse articles)))
6527      ((and (gnus-region-active-p) (mark))
6528       (message "region active")
6529       ;; Work on the region between point and mark.
6530       (let ((max (max (point) (mark)))
6531             articles article)
6532         (save-excursion
6533           (goto-char (min (point) (mark)))
6534           (while
6535               (and
6536                (push (setq article (gnus-summary-article-number)) articles)
6537                (gnus-summary-find-next nil article)
6538                (< (point) max)))
6539           (nreverse articles))))
6540      (gnus-newsgroup-processable
6541       ;; There are process-marked articles present.
6542       ;; Save current state.
6543       (gnus-summary-save-process-mark)
6544       ;; Return the list.
6545       (reverse gnus-newsgroup-processable))
6546      (t
6547       ;; Just return the current article.
6548       (list (gnus-summary-article-number))))))
6549
6550 (defmacro gnus-summary-iterate (arg &rest forms)
6551   "Iterate over the process/prefixed articles and do FORMS.
6552 ARG is the interactive prefix given to the command.  FORMS will be
6553 executed with point over the summary line of the articles."
6554   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6555     `(let ((,articles (gnus-summary-work-articles ,arg)))
6556        (while ,articles
6557          (gnus-summary-goto-subject (car ,articles))
6558          ,@forms
6559          (pop ,articles)))))
6560
6561 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6562 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6563
6564 (defun gnus-summary-save-process-mark ()
6565   "Push the current set of process marked articles on the stack."
6566   (interactive)
6567   (push (copy-sequence gnus-newsgroup-processable)
6568         gnus-newsgroup-process-stack))
6569
6570 (defun gnus-summary-kill-process-mark ()
6571   "Push the current set of process marked articles on the stack and unmark."
6572   (interactive)
6573   (gnus-summary-save-process-mark)
6574   (gnus-summary-unmark-all-processable))
6575
6576 (defun gnus-summary-yank-process-mark ()
6577   "Pop the last process mark state off the stack and restore it."
6578   (interactive)
6579   (unless gnus-newsgroup-process-stack
6580     (error "Empty mark stack"))
6581   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6582
6583 (defun gnus-summary-process-mark-set (set)
6584   "Make SET into the current process marked articles."
6585   (gnus-summary-unmark-all-processable)
6586   (mapc 'gnus-summary-set-process-mark set))
6587
6588 ;;; Searching and stuff
6589
6590 (defun gnus-summary-search-group (&optional backward use-level)
6591   "Search for next unread newsgroup.
6592 If optional argument BACKWARD is non-nil, search backward instead."
6593   (with-current-buffer gnus-group-buffer
6594     (when (gnus-group-search-forward
6595            backward nil (if use-level (gnus-group-group-level) nil))
6596       (gnus-group-group-name))))
6597
6598 (defun gnus-summary-best-group (&optional exclude-group)
6599   "Find the name of the best unread group.
6600 If EXCLUDE-GROUP, do not go to this group."
6601   (with-current-buffer gnus-group-buffer
6602     (save-excursion
6603       (gnus-group-best-unread-group exclude-group))))
6604
6605 (defun gnus-summary-find-next (&optional unread article backward)
6606   (if backward
6607       (gnus-summary-find-prev unread article)
6608     (let* ((dummy (gnus-summary-article-intangible-p))
6609            (article (or article (gnus-summary-article-number)))
6610            (data (gnus-data-find-list article))
6611            result)
6612       (when (and (not dummy)
6613                  (or (not gnus-summary-check-current)
6614                      (not unread)
6615                      (not (gnus-data-unread-p (car data)))))
6616         (setq data (cdr data)))
6617       (when (setq result
6618                   (if unread
6619                       (progn
6620                         (while data
6621                           (unless (memq (gnus-data-number (car data))
6622                                         (cond
6623                                          ((eq gnus-auto-goto-ignores
6624                                               'always-undownloaded)
6625                                           gnus-newsgroup-undownloaded)
6626                                          (gnus-plugged
6627                                           nil)
6628                                          ((eq gnus-auto-goto-ignores
6629                                               'unfetched)
6630                                           gnus-newsgroup-unfetched)
6631                                          ((eq gnus-auto-goto-ignores
6632                                               'undownloaded)
6633                                           gnus-newsgroup-undownloaded)))
6634                             (when (gnus-data-unread-p (car data))
6635                               (setq result (car data)
6636                                     data nil)))
6637                           (setq data (cdr data)))
6638                         result)
6639                     (car data)))
6640         (goto-char (gnus-data-pos result))
6641         (gnus-data-number result)))))
6642
6643 (defun gnus-summary-find-prev (&optional unread article)
6644   (let* ((eobp (eobp))
6645          (article (or article (gnus-summary-article-number)))
6646          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6647          result)
6648     (when (and (not eobp)
6649                (or (not gnus-summary-check-current)
6650                    (not unread)
6651                    (not (gnus-data-unread-p (car data)))))
6652       (setq data (cdr data)))
6653     (when (setq result
6654                 (if unread
6655                     (progn
6656                       (while data
6657                         (unless (memq (gnus-data-number (car data))
6658                                       (cond
6659                                        ((eq gnus-auto-goto-ignores
6660                                             'always-undownloaded)
6661                                         gnus-newsgroup-undownloaded)
6662                                        (gnus-plugged
6663                                         nil)
6664                                        ((eq gnus-auto-goto-ignores
6665                                             'unfetched)
6666                                         gnus-newsgroup-unfetched)
6667                                        ((eq gnus-auto-goto-ignores
6668                                             'undownloaded)
6669                                         gnus-newsgroup-undownloaded)))
6670                           (when (gnus-data-unread-p (car data))
6671                             (setq result (car data)
6672                                   data nil)))
6673                         (setq data (cdr data)))
6674                       result)
6675                   (car data)))
6676       (goto-char (gnus-data-pos result))
6677       (gnus-data-number result))))
6678
6679 (defun gnus-summary-find-subject (subject &optional unread backward article)
6680   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6681          (article (or article (gnus-summary-article-number)))
6682          (articles (gnus-data-list backward))
6683          (arts (gnus-data-find-list article articles))
6684          result)
6685     (when (or (not gnus-summary-check-current)
6686               (not unread)
6687               (not (gnus-data-unread-p (car arts))))
6688       (setq arts (cdr arts)))
6689     (while arts
6690       (and (or (not unread)
6691                (gnus-data-unread-p (car arts)))
6692            (vectorp (gnus-data-header (car arts)))
6693            (gnus-subject-equal
6694             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6695            (setq result (car arts)
6696                  arts nil))
6697       (setq arts (cdr arts)))
6698     (and result
6699          (goto-char (gnus-data-pos result))
6700          (gnus-data-number result))))
6701
6702 (defun gnus-summary-search-forward (&optional unread subject backward)
6703   "Search forward for an article.
6704 If UNREAD, look for unread articles.  If SUBJECT, look for
6705 articles with that subject.  If BACKWARD, search backward instead."
6706   (cond (subject (gnus-summary-find-subject subject unread backward))
6707         (backward (gnus-summary-find-prev unread))
6708         (t (gnus-summary-find-next unread))))
6709
6710 (defun gnus-recenter (&optional n)
6711   "Center point in window and redisplay frame.
6712 Also do horizontal recentering."
6713   (interactive "P")
6714   (when (and gnus-auto-center-summary
6715              (not (eq gnus-auto-center-summary 'vertical)))
6716     (gnus-horizontal-recenter))
6717   (if (fboundp 'recenter-top-bottom)
6718       (recenter-top-bottom n)
6719     (recenter n)))
6720
6721 (put 'gnus-recenter 'isearch-scroll t)
6722
6723 (defun gnus-forward-line-ignore-invisible (n)
6724   "Move N lines forward (backward if N is negative).
6725 Like forward-line, but skip over (and don't count) invisible lines."
6726   (let (done)
6727     (while (and (> n 0) (not done))
6728       ;; If the following character is currently invisible,
6729       ;; skip all characters with that same `invisible' property value.
6730       (while (gnus-invisible-p (point))
6731         (goto-char (gnus-next-char-property-change (point))))
6732       (forward-line 1)
6733       (if (eobp)
6734           (setq done t)
6735         (setq n (1- n))))
6736     (while (and (< n 0) (not done))
6737       (forward-line -1)
6738       (if (bobp) (setq done t)
6739         (setq n (1+ n))
6740         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6741           (goto-char (gnus-previous-char-property-change (point))))))))
6742
6743 (defun gnus-summary-recenter ()
6744   "Center point in the summary window.
6745 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6746 displayed, no centering will be performed."
6747   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6748   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6749   (interactive)
6750   ;; The user has to want it.
6751   (when gnus-auto-center-summary
6752     (let* ((top (cond ((< (window-height) 4) 0)
6753                       ((< (window-height) 7) 1)
6754                       (t (if (numberp gnus-auto-center-summary)
6755                              gnus-auto-center-summary
6756                            (/ (1- (window-height)) 2)))))
6757            (height (1- (window-height)))
6758            (bottom (save-excursion
6759                      (goto-char (point-max))
6760                      (gnus-forward-line-ignore-invisible (- height))
6761                      (point)))
6762            (window (get-buffer-window (current-buffer))))
6763       (when (get-buffer-window gnus-article-buffer)
6764         ;; Only do recentering when the article buffer is displayed,
6765         ;; Set the window start to either `bottom', which is the biggest
6766         ;; possible valid number, or the second line from the top,
6767         ;; whichever is the least.
6768         (let ((top-pos (save-excursion
6769                          (gnus-forward-line-ignore-invisible (- top))
6770                          (point))))
6771           (if (> bottom top-pos)
6772               ;; Keep the second line from the top visible
6773               (set-window-start window top-pos)
6774             ;; Try to keep the bottom line visible; if it's partially
6775             ;; obscured, either scroll one more line to make it fully
6776             ;; visible, or revert to using TOP-POS.
6777             (save-excursion
6778               (goto-char (point-max))
6779               (gnus-forward-line-ignore-invisible -1)
6780               (let ((last-line-start (point)))
6781                 (goto-char bottom)
6782                 (set-window-start window (point) t)
6783                 (when (not (pos-visible-in-window-p last-line-start window))
6784                   (gnus-forward-line-ignore-invisible 1)
6785                   (set-window-start window (min (point) top-pos) t)))))))
6786       ;; Do horizontal recentering while we're at it.
6787       (when (and (get-buffer-window (current-buffer) t)
6788                  (not (eq gnus-auto-center-summary 'vertical)))
6789         (let ((selected (selected-window)))
6790           (select-window (get-buffer-window (current-buffer) t))
6791           (gnus-summary-position-point)
6792           (gnus-horizontal-recenter)
6793           (select-window selected))))))
6794
6795 (defun gnus-summary-jump-to-group (newsgroup)
6796   "Move point to NEWSGROUP in group mode buffer."
6797   ;; Keep update point of group mode buffer if visible.
6798   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6799       (save-window-excursion
6800         ;; Take care of tree window mode.
6801         (when (get-buffer-window gnus-group-buffer)
6802           (pop-to-buffer gnus-group-buffer))
6803         (gnus-group-jump-to-group newsgroup))
6804     (save-excursion
6805       ;; Take care of tree window mode.
6806       (if (get-buffer-window gnus-group-buffer 0)
6807           (pop-to-buffer gnus-group-buffer)
6808         (set-buffer gnus-group-buffer))
6809       (gnus-group-jump-to-group newsgroup))))
6810
6811 ;; This function returns a list of article numbers based on the
6812 ;; difference between the ranges of read articles in this group and
6813 ;; the range of active articles.
6814 (defun gnus-list-of-unread-articles (group)
6815   (let* ((read (gnus-info-read (gnus-get-info group)))
6816          (active (or (gnus-active group) (gnus-activate-group group)))
6817          (last (or (cdr active)
6818                    (error "Group %s couldn't be activated " group)))
6819          (bottom (if gnus-newsgroup-maximum-articles
6820                      (max (car active)
6821                           (- last gnus-newsgroup-maximum-articles -1))
6822                    (car active)))
6823          first nlast unread)
6824     ;; If none are read, then all are unread.
6825     (if (not read)
6826         (setq first bottom)
6827       ;; If the range of read articles is a single range, then the
6828       ;; first unread article is the article after the last read
6829       ;; article.  Sounds logical, doesn't it?
6830       (if (and (not (listp (cdr read)))
6831                (or (< (car read) bottom)
6832                    (progn (setq read (list read))
6833                           nil)))
6834           (setq first (max bottom (1+ (cdr read))))
6835         ;; `read' is a list of ranges.
6836         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6837                                   (caar read)))
6838                   1)
6839           (setq first bottom))
6840         (while read
6841           (when first
6842             (while (< first nlast)
6843               (setq unread (cons first unread)
6844                     first (1+ first))))
6845           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6846           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6847           (setq read (cdr read)))))
6848     ;; And add the last unread articles.
6849     (while (<= first last)
6850       (setq unread (cons first unread)
6851             first (1+ first)))
6852     ;; Return the list of unread articles.
6853     (delq 0 (nreverse unread))))
6854
6855 (defun gnus-list-of-read-articles (group)
6856   "Return a list of unread, unticked and non-dormant articles."
6857   (let* ((info (gnus-get-info group))
6858          (marked (gnus-info-marks info))
6859          (active (gnus-active group)))
6860     (and info active
6861          (gnus-list-range-difference
6862           (gnus-list-range-difference
6863            (gnus-sorted-complement
6864             (gnus-uncompress-range
6865              (if gnus-newsgroup-maximum-articles
6866                  (cons (max (car active)
6867                             (- (cdr active)
6868                                gnus-newsgroup-maximum-articles
6869                                -1))
6870                        (cdr active))
6871                active))
6872             (gnus-list-of-unread-articles group))
6873            (cdr (assq 'dormant marked)))
6874           (cdr (assq 'tick marked))))))
6875
6876 ;; This function returns a sequence of article numbers based on the
6877 ;; difference between the ranges of read articles in this group and
6878 ;; the range of active articles.
6879 (defun gnus-sequence-of-unread-articles (group)
6880   (let* ((read (gnus-info-read (gnus-get-info group)))
6881          (active (or (gnus-active group) (gnus-activate-group group)))
6882          (last (cdr active))
6883          (bottom (if gnus-newsgroup-maximum-articles
6884                      (max (car active)
6885                           (- last gnus-newsgroup-maximum-articles -1))
6886                    (car active)))
6887          first nlast unread)
6888     ;; If none are read, then all are unread.
6889     (if (not read)
6890         (setq first bottom)
6891       ;; If the range of read articles is a single range, then the
6892       ;; first unread article is the article after the last read
6893       ;; article.  Sounds logical, doesn't it?
6894       (if (and (not (listp (cdr read)))
6895                (or (< (car read) bottom)
6896                    (progn (setq read (list read))
6897                           nil)))
6898           (setq first (max bottom (1+ (cdr read))))
6899         ;; `read' is a list of ranges.
6900         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6901                                   (caar read)))
6902                   1)
6903           (setq first bottom))
6904         (while read
6905           (when first
6906             (push (cons first nlast) unread))
6907           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6908           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6909           (setq read (cdr read)))))
6910     ;; And add the last unread articles.
6911     (cond ((not (and first last))
6912            nil)
6913           ((< first last)
6914            (push (cons first last) unread))
6915           ((= first last)
6916            (push first unread)))
6917     ;; Return the sequence of unread articles.
6918     (delq 0 (nreverse unread))))
6919
6920 ;; Various summary commands
6921
6922 (defun gnus-summary-select-article-buffer ()
6923   "Reconfigure windows to show article buffer."
6924   (interactive)
6925   (if (not (gnus-buffer-live-p gnus-article-buffer))
6926       (error "There is no article buffer for this summary buffer")
6927     (gnus-configure-windows 'article)
6928     (select-window (get-buffer-window gnus-article-buffer))))
6929
6930 (defun gnus-summary-universal-argument (arg)
6931   "Perform any operation on all articles that are process/prefixed."
6932   (interactive "P")
6933   (let ((articles (gnus-summary-work-articles arg))
6934         func article)
6935     (if (eq
6936          (setq
6937           func
6938           (key-binding
6939            (read-key-sequence
6940             (substitute-command-keys
6941              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6942          'undefined)
6943         (gnus-error 1 "Undefined key")
6944       (save-excursion
6945         (while articles
6946           (gnus-summary-goto-subject (setq article (pop articles)))
6947           (let (gnus-newsgroup-processable)
6948             (command-execute func))
6949           (gnus-summary-remove-process-mark article)))))
6950   (gnus-summary-position-point))
6951
6952 (defun gnus-summary-toggle-truncation (&optional arg)
6953   "Toggle truncation of summary lines.
6954 With ARG, turn line truncation on if ARG is positive."
6955   (interactive "P")
6956   (setq truncate-lines
6957         (if (null arg) (not truncate-lines)
6958           (> (prefix-numeric-value arg) 0)))
6959   (redraw-display))
6960
6961 (defun gnus-summary-find-for-reselect ()
6962   "Return the number of an article to stay on across a reselect.
6963 The current article is considered, then following articles, then previous
6964 articles.  An article is sought which is not cancelled and isn't a temporary
6965 insertion from another group.  If there's no such then return a dummy 0."
6966   (let (found)
6967     (dolist (rev '(nil t))
6968       (unless found      ; don't demand the reverse list if we don't need it
6969         (let ((data (gnus-data-find-list
6970                      (gnus-summary-article-number) (gnus-data-list rev))))
6971           (while (and data (not found))
6972             (if (and (< 0 (gnus-data-number (car data)))
6973                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6974                 (setq found (gnus-data-number (car data))))
6975             (setq data (cdr data))))))
6976     (or found 0)))
6977
6978 (defun gnus-summary-reselect-current-group (&optional all rescan)
6979   "Exit and then reselect the current newsgroup.
6980 The prefix argument ALL means to select all articles."
6981   (interactive "P")
6982   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6983     (error "Ephemeral groups can't be reselected"))
6984   (let ((current-subject (gnus-summary-find-for-reselect))
6985         (group gnus-newsgroup-name))
6986     (setq gnus-newsgroup-begin nil)
6987     (gnus-summary-exit nil 'leave-hidden)
6988     ;; We have to adjust the point of group mode buffer because
6989     ;; point was moved to the next unread newsgroup by exiting.
6990     (gnus-summary-jump-to-group group)
6991     (when rescan
6992       (save-excursion
6993         (gnus-group-get-new-news-this-group 1)))
6994     (gnus-group-read-group all t)
6995     (gnus-summary-goto-subject current-subject nil t)))
6996
6997 (defun gnus-summary-rescan-group (&optional all)
6998   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6999   (interactive "P")
7000   (gnus-summary-reselect-current-group all t))
7001
7002 (defun gnus-summary-update-info (&optional non-destructive)
7003   (save-excursion
7004     (let ((group gnus-newsgroup-name))
7005       (when group
7006         (when gnus-newsgroup-kill-headers
7007           (setq gnus-newsgroup-killed
7008                 (gnus-compress-sequence
7009                  (gnus-sorted-union
7010                   (gnus-list-range-intersection
7011                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7012                   gnus-newsgroup-unreads)
7013                  t)))
7014         (unless (listp (cdr gnus-newsgroup-killed))
7015           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7016         (let ((headers gnus-newsgroup-headers))
7017           ;; Set the new ranges of read articles.
7018           (with-current-buffer gnus-group-buffer
7019             (gnus-undo-force-boundary))
7020           (gnus-update-read-articles
7021            group (gnus-sorted-union
7022                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7023           ;; Set the current article marks.
7024           (let ((gnus-newsgroup-scored
7025                  (if (and (not gnus-save-score)
7026                           (not non-destructive))
7027                      nil
7028                    gnus-newsgroup-scored)))
7029             (save-excursion
7030               (gnus-update-marks)))
7031           ;; Do the cross-ref thing.
7032           (when gnus-use-cross-reference
7033             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7034           ;; Do not switch windows but change the buffer to work.
7035           (set-buffer gnus-group-buffer)
7036           (unless (gnus-ephemeral-group-p group)
7037             (gnus-group-update-group group)))))))
7038
7039 (defun gnus-summary-save-newsrc (&optional force)
7040   "Save the current number of read/marked articles in the dribble buffer.
7041 The dribble buffer will then be saved.
7042 If FORCE (the prefix), also save the .newsrc file(s)."
7043   (interactive "P")
7044   (gnus-summary-update-info t)
7045   (if force
7046       (gnus-save-newsrc-file)
7047     (gnus-dribble-save)))
7048
7049 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7050
7051 (defun gnus-summary-exit (&optional temporary leave-hidden)
7052   "Exit reading current newsgroup, and then return to group selection mode.
7053 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7054   (interactive)
7055   (gnus-set-global-variables)
7056   (when (gnus-buffer-live-p gnus-article-buffer)
7057     (with-current-buffer gnus-article-buffer
7058       (mm-destroy-parts gnus-article-mime-handles)
7059       ;; Set it to nil for safety reason.
7060       (setq gnus-article-mime-handle-alist nil)
7061       (setq gnus-article-mime-handles nil)))
7062   (gnus-kill-save-kill-buffer)
7063   (gnus-async-halt-prefetch)
7064   (let* ((group gnus-newsgroup-name)
7065          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7066          (gnus-group-is-exiting-p t)
7067          (mode major-mode)
7068          (group-point nil)
7069          (buf (current-buffer)))
7070     (unless quit-config
7071       ;; Do adaptive scoring, and possibly save score files.
7072       (when gnus-newsgroup-adaptive
7073         (gnus-score-adaptive))
7074       (when gnus-use-scoring
7075         (gnus-score-save)))
7076     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7077     ;; If we have several article buffers, we kill them at exit.
7078     (unless gnus-single-article-buffer
7079       (when (gnus-buffer-live-p gnus-article-buffer)
7080         (with-current-buffer gnus-article-buffer
7081           ;; Don't kill sticky article buffers
7082           (unless (eq major-mode 'gnus-sticky-article-mode)
7083             (gnus-kill-buffer gnus-article-buffer)
7084             (setq gnus-article-current nil))))
7085       (gnus-kill-buffer gnus-original-article-buffer))
7086     (when gnus-use-cache
7087       (gnus-cache-possibly-remove-articles)
7088       (gnus-cache-save-buffers))
7089     (gnus-async-prefetch-remove-group group)
7090     (when gnus-suppress-duplicates
7091       (gnus-dup-enter-articles))
7092     (when gnus-use-trees
7093       (gnus-tree-close group))
7094     (when gnus-use-cache
7095       (gnus-cache-write-active))
7096     ;; Remove entries for this group.
7097     (nnmail-purge-split-history (gnus-group-real-name group))
7098     ;; Make all changes in this group permanent.
7099     (unless quit-config
7100       (gnus-run-hooks 'gnus-exit-group-hook)
7101       (gnus-summary-update-info))
7102     (gnus-close-group group)
7103     ;; Make sure where we were, and go to next newsgroup.
7104     (set-buffer gnus-group-buffer)
7105     (unless quit-config
7106       (gnus-group-jump-to-group group))
7107     (gnus-run-hooks 'gnus-summary-exit-hook)
7108     (unless (or quit-config
7109                 (not gnus-summary-next-group-on-exit)
7110                 ;; If this group has disappeared from the summary
7111                 ;; buffer, don't skip forwards.
7112                 (not (string= group (gnus-group-group-name))))
7113       (gnus-group-next-unread-group 1))
7114     (setq group-point (point))
7115     (if temporary
7116         nil                             ;Nothing to do.
7117       (set-buffer buf)
7118       (if (not gnus-kill-summary-on-exit)
7119           (progn
7120             (gnus-deaden-summary)
7121             (setq mode nil))
7122         ;; We set all buffer-local variables to nil.  It is unclear why
7123         ;; this is needed, but if we don't, buffer-local variables are
7124         ;; not garbage-collected, it seems.  This would the lead to en
7125         ;; ever-growing Emacs.
7126         (gnus-summary-clear-local-variables)
7127         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7128           (gnus-summary-clear-local-variables))
7129         (when (get-buffer gnus-article-buffer)
7130           (bury-buffer gnus-article-buffer))
7131         ;; Return to group mode buffer.
7132         (when (eq mode 'gnus-summary-mode)
7133           (gnus-kill-buffer buf)))
7134       (setq gnus-current-select-method gnus-select-method)
7135       (set-buffer gnus-group-buffer)
7136       (if quit-config
7137           (gnus-handle-ephemeral-exit quit-config)
7138         (goto-char group-point)
7139         ;; If gnus-group-buffer is already displayed, make sure we also move
7140         ;; the cursor in the window that displays it.
7141         (let ((win (get-buffer-window (current-buffer) 0)))
7142           (if win (set-window-point win (point))))
7143         (unless leave-hidden
7144           (gnus-configure-windows 'group 'force)))
7145       ;; Clear the current group name.
7146       (unless quit-config
7147         (setq gnus-newsgroup-name nil)))))
7148
7149 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7150 (defun gnus-summary-exit-no-update (&optional no-questions)
7151   "Quit reading current newsgroup without updating read article info."
7152   (interactive)
7153   (let* ((group gnus-newsgroup-name)
7154          (gnus-group-is-exiting-p t)
7155          (gnus-group-is-exiting-without-update-p t)
7156          (quit-config (gnus-group-quit-config group)))
7157     (when (or no-questions
7158               gnus-expert-user
7159               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7160       (gnus-async-halt-prefetch)
7161       (run-hooks 'gnus-summary-prepare-exit-hook)
7162       (when (gnus-buffer-live-p gnus-article-buffer)
7163         (with-current-buffer gnus-article-buffer
7164           (mm-destroy-parts gnus-article-mime-handles)
7165           ;; Set it to nil for safety reason.
7166           (setq gnus-article-mime-handle-alist nil)
7167           (setq gnus-article-mime-handles nil)))
7168       ;; If we have several article buffers, we kill them at exit.
7169       (unless gnus-single-article-buffer
7170         (gnus-kill-buffer gnus-article-buffer)
7171         (gnus-kill-buffer gnus-original-article-buffer)
7172         (setq gnus-article-current nil))
7173       (if (not gnus-kill-summary-on-exit)
7174           (gnus-deaden-summary)
7175         (gnus-close-group group)
7176         (gnus-summary-clear-local-variables)
7177         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7178           (gnus-summary-clear-local-variables))
7179         (gnus-kill-buffer gnus-summary-buffer))
7180       (unless gnus-single-article-buffer
7181         (setq gnus-article-current nil))
7182       (when gnus-use-trees
7183         (gnus-tree-close group))
7184       (gnus-async-prefetch-remove-group group)
7185       (when (get-buffer gnus-article-buffer)
7186         (bury-buffer gnus-article-buffer))
7187       ;; Return to the group buffer.
7188       (gnus-configure-windows 'group 'force)
7189       ;; Clear the current group name.
7190       (setq gnus-newsgroup-name nil)
7191       (unless (gnus-ephemeral-group-p group)
7192         (gnus-group-update-group group))
7193       (when (equal (gnus-group-group-name) group)
7194         (gnus-group-next-unread-group 1))
7195       (when quit-config
7196         (gnus-handle-ephemeral-exit quit-config)))))
7197
7198 (defun gnus-handle-ephemeral-exit (quit-config)
7199   "Handle movement when leaving an ephemeral group.
7200 The state which existed when entering the ephemeral is reset."
7201   (if (not (buffer-name (car quit-config)))
7202       (gnus-configure-windows 'group 'force)
7203     (set-buffer (car quit-config))
7204     (cond ((eq major-mode 'gnus-summary-mode)
7205            (gnus-set-global-variables))
7206           ((eq major-mode 'gnus-article-mode)
7207            (save-current-buffer
7208              ;; The `gnus-summary-buffer' variable may point
7209              ;; to the old summary buffer when using a single
7210              ;; article buffer.
7211              (unless (gnus-buffer-live-p gnus-summary-buffer)
7212                (set-buffer gnus-group-buffer))
7213              (set-buffer gnus-summary-buffer)
7214              (gnus-set-global-variables))))
7215     (if (or (eq (cdr quit-config) 'article)
7216             (eq (cdr quit-config) 'pick))
7217         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7218             (gnus-configure-windows 'pick 'force)
7219           (gnus-configure-windows (cdr quit-config) 'force))
7220       (gnus-configure-windows (cdr quit-config) 'force))
7221     (when (eq major-mode 'gnus-summary-mode)
7222       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7223                                                      next-unread-noselect))
7224           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7225                                   'next-noselect)
7226                               (gnus-summary-next-subject 1 nil t))
7227                              ((eq gnus-auto-select-on-ephemeral-exit
7228                                   'next-unread-noselect)
7229                               (gnus-summary-next-subject 1 t t))))
7230             ;; Hide the article buffer which displays the article different
7231             ;; from the one that the cursor points to in the summary buffer.
7232             (gnus-configure-windows 'summary 'force))
7233         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7234                (gnus-summary-next-subject 1))
7235               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7236                (gnus-summary-next-subject 1 t))))
7237       (gnus-summary-recenter)
7238       (gnus-summary-position-point))))
7239
7240 ;;; Dead summaries.
7241
7242 (defvar gnus-dead-summary-mode-map
7243   (let ((map (make-keymap)))
7244     (suppress-keymap map)
7245     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7246     (dolist (key '("\C-d" "\r" "\177" [delete]))
7247       (define-key map key 'gnus-summary-wake-up-the-dead))
7248     (dolist (key '("q" "Q"))
7249       (define-key map key 'bury-buffer))
7250     map))
7251
7252 (define-minor-mode gnus-dead-summary-mode
7253   "Minor mode for Gnus summary buffers."
7254   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7255   (unless (derived-mode-p 'gnus-summary-mode)
7256     (setq gnus-dead-summary-mode nil)))
7257
7258 (defun gnus-deaden-summary ()
7259   "Make the current summary buffer into a dead summary buffer."
7260   ;; Kill any previous dead summary buffer.
7261   (when (and gnus-dead-summary
7262              (buffer-name gnus-dead-summary))
7263     (with-current-buffer gnus-dead-summary
7264       (when gnus-dead-summary-mode
7265         (kill-buffer (current-buffer)))))
7266   ;; Make this the current dead summary.
7267   (setq gnus-dead-summary (current-buffer))
7268   (gnus-dead-summary-mode 1)
7269   (let ((name (buffer-name)))
7270     (when (string-match "Summary" name)
7271       (rename-buffer
7272        (concat (substring name 0 (match-beginning 0)) "Dead "
7273                (substring name (match-beginning 0)))
7274        t)
7275       (bury-buffer))))
7276
7277 (defun gnus-kill-or-deaden-summary (buffer)
7278   "Kill or deaden the summary BUFFER."
7279   (save-excursion
7280     (when (and (buffer-name buffer)
7281                (not gnus-single-article-buffer))
7282       (with-current-buffer buffer
7283         (gnus-kill-buffer gnus-article-buffer)
7284         (gnus-kill-buffer gnus-original-article-buffer)))
7285     (cond
7286      ;; Kill the buffer.
7287      (gnus-kill-summary-on-exit
7288       (when (and gnus-use-trees
7289                  (gnus-buffer-exists-p buffer))
7290         (with-current-buffer buffer
7291           (gnus-tree-close gnus-newsgroup-name)))
7292       (gnus-kill-buffer buffer))
7293      ;; Deaden the buffer.
7294      ((gnus-buffer-exists-p buffer)
7295       (with-current-buffer buffer
7296         (gnus-deaden-summary))))))
7297
7298 (defun gnus-summary-wake-up-the-dead (&rest args)
7299   "Wake up the dead summary buffer."
7300   (interactive)
7301   (gnus-dead-summary-mode -1)
7302   (let ((name (buffer-name)))
7303     (when (string-match "Dead " name)
7304       (rename-buffer
7305        (concat (substring name 0 (match-beginning 0))
7306                (substring name (match-end 0)))
7307        t)))
7308   (gnus-message 3 "This dead summary is now alive again"))
7309
7310 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7311 (defun gnus-summary-fetch-faq (&optional faq-dir)
7312   "Fetch the FAQ for the current group.
7313 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7314 in."
7315   (interactive
7316    (list
7317     (when current-prefix-arg
7318       (completing-read
7319        "FAQ dir: " (and (listp gnus-group-faq-directory)
7320                         (mapcar 'list
7321                                 gnus-group-faq-directory))))))
7322   (let (gnus-faq-buffer)
7323     (when (setq gnus-faq-buffer
7324                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7325       (gnus-configure-windows 'summary-faq))))
7326
7327 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7328 (defun gnus-summary-describe-group (&optional force)
7329   "Describe the current newsgroup."
7330   (interactive "P")
7331   (gnus-group-describe-group force gnus-newsgroup-name))
7332
7333 (defun gnus-summary-describe-briefly ()
7334   "Describe summary mode commands briefly."
7335   (interactive)
7336   (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")))
7337
7338 ;; Walking around group mode buffer from summary mode.
7339
7340 (defun gnus-summary-next-group (&optional no-article target-group backward)
7341   "Exit current newsgroup and then select next unread newsgroup.
7342 If prefix argument NO-ARTICLE is non-nil, no article is selected
7343 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7344 previous group instead."
7345   (interactive "P")
7346   ;; Stop pre-fetching.
7347   (gnus-async-halt-prefetch)
7348   (let ((current-group gnus-newsgroup-name)
7349         (current-buffer (current-buffer))
7350         entered)
7351     ;; First we semi-exit this group to update Xrefs and all variables.
7352     ;; We can't do a real exit, because the window conf must remain
7353     ;; the same in case the user is prompted for info, and we don't
7354     ;; want the window conf to change before that...
7355     (gnus-summary-exit t)
7356     (while (not entered)
7357       ;; Then we find what group we are supposed to enter.
7358       (set-buffer gnus-group-buffer)
7359       (gnus-group-jump-to-group current-group)
7360       (setq target-group
7361             (or target-group
7362                 (if (eq gnus-keep-same-level 'best)
7363                     (gnus-summary-best-group gnus-newsgroup-name)
7364                   (gnus-summary-search-group backward gnus-keep-same-level))))
7365       (if (not target-group)
7366           ;; There are no further groups, so we return to the group
7367           ;; buffer.
7368           (progn
7369             (gnus-message 5 "Returning to the group buffer")
7370             (setq entered t)
7371             (when (gnus-buffer-live-p current-buffer)
7372               (set-buffer current-buffer)
7373               (gnus-summary-exit))
7374             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7375         ;; We try to enter the target group.
7376         (gnus-group-jump-to-group target-group)
7377         (let ((unreads (gnus-group-group-unread)))
7378           (if (and (or (eq t unreads)
7379                        (and unreads (not (zerop unreads))))
7380                    (gnus-summary-read-group
7381                     target-group nil no-article
7382                     (and (buffer-name current-buffer) current-buffer)
7383                     nil backward))
7384               (setq entered t)
7385             (setq current-group target-group
7386                   target-group nil)))))))
7387
7388 (defun gnus-summary-prev-group (&optional no-article)
7389   "Exit current newsgroup and then select previous unread newsgroup.
7390 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7391   (interactive "P")
7392   (gnus-summary-next-group no-article nil t))
7393
7394 ;; Walking around summary lines.
7395
7396 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7397   "Go to the first subject satisfying any non-nil constraint.
7398 If UNREAD is non-nil, the article should be unread.
7399 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7400 If UNSEEN is non-nil, the article should be unseen.
7401 Returns the article selected or nil if there are no matching articles."
7402   (interactive "P")
7403   (cond
7404    ;; Empty summary.
7405    ((null gnus-newsgroup-data)
7406     (gnus-message 3 "No articles in the group")
7407     nil)
7408    ;; Pick the first article.
7409    ((not (or unread undownloaded unseen))
7410     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7411     (gnus-data-number (car gnus-newsgroup-data)))
7412    ;; Find the first unread article.
7413    (t
7414     (let ((data gnus-newsgroup-data))
7415       (while (and data
7416                   (let ((num (gnus-data-number (car data))))
7417                     (or (memq num gnus-newsgroup-unfetched)
7418                         (not (or (and unread
7419                                       (memq num gnus-newsgroup-unreads))
7420                                  (and undownloaded
7421                                       (memq num gnus-newsgroup-undownloaded))
7422                                  (and unseen
7423                                       (memq num gnus-newsgroup-unseen)))))))
7424         (setq data (cdr data)))
7425       (prog1
7426           (if data
7427               (progn
7428                 (goto-char (gnus-data-pos (car data)))
7429                 (gnus-data-number (car data)))
7430             (gnus-message 3 "No more%s articles"
7431                           (let* ((r (when unread " unread"))
7432                                  (d (when undownloaded " undownloaded"))
7433                                  (s (when unseen " unseen"))
7434                                  (l (delq nil (list r d s))))
7435                             (cond ((= 3 (length l))
7436                                    (concat r "," d ", or" s))
7437                                   ((= 2 (length l))
7438                                    (concat (car l) ", or" (cadr l)))
7439                                   ((= 1 (length l))
7440                                    (car l))
7441                                   (t
7442                                    ""))))
7443             nil
7444             )
7445         (gnus-summary-position-point))))))
7446
7447 (defun gnus-summary-next-subject (n &optional unread dont-display)
7448   "Go to next N'th summary line.
7449 If N is negative, go to the previous N'th subject line.
7450 If UNREAD is non-nil, only unread articles are selected.
7451 The difference between N and the actual number of steps taken is
7452 returned."
7453   (interactive "p")
7454   (let ((backward (< n 0))
7455         (n (abs n)))
7456     (while (and (> n 0)
7457                 (if backward
7458                     (gnus-summary-find-prev unread)
7459                   (gnus-summary-find-next unread)))
7460       (unless (zerop (setq n (1- n)))
7461         (gnus-summary-show-thread)))
7462     (when (/= 0 n)
7463       (gnus-message 7 "No more%s articles"
7464                     (if unread " unread" "")))
7465     (unless dont-display
7466       (gnus-summary-recenter)
7467       (gnus-summary-position-point))
7468     n))
7469
7470 (defun gnus-summary-next-unread-subject (n)
7471   "Go to next N'th unread summary line."
7472   (interactive "p")
7473   (gnus-summary-next-subject n t))
7474
7475 (defun gnus-summary-prev-subject (n &optional unread)
7476   "Go to previous N'th summary line.
7477 If optional argument UNREAD is non-nil, only unread article is selected."
7478   (interactive "p")
7479   (gnus-summary-next-subject (- n) unread))
7480
7481 (defun gnus-summary-prev-unread-subject (n)
7482   "Go to previous N'th unread summary line."
7483   (interactive "p")
7484   (gnus-summary-next-subject (- n) t))
7485
7486 (defun gnus-summary-goto-subjects (articles)
7487   "Insert the subject header for ARTICLES in the current buffer."
7488   (save-excursion
7489     (dolist (article articles)
7490       (gnus-summary-goto-subject article t)))
7491   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7492   (gnus-summary-position-point))
7493
7494 (defun gnus-summary-goto-subject (article &optional force silent)
7495   "Go to the subject line of ARTICLE.
7496 If FORCE, also allow jumping to articles not currently shown."
7497   (interactive "nArticle number: ")
7498   (unless (numberp article)
7499     (error "Article %s is not a number" article))
7500   (let ((b (point))
7501         (data (gnus-data-find article)))
7502     ;; We read in the article if we have to.
7503     (and (not data)
7504          force
7505          (gnus-summary-insert-subject
7506           article
7507           (if (or (numberp force) (vectorp force)) force)
7508           t)
7509          (setq data (gnus-data-find article)))
7510     (goto-char b)
7511     (if (not data)
7512         (progn
7513           (unless silent
7514             (gnus-message 3 "Can't find article %d" article))
7515           nil)
7516       (let ((pt (gnus-data-pos data)))
7517         (goto-char pt)
7518         (gnus-summary-set-article-display-arrow pt))
7519       (gnus-summary-position-point)
7520       article)))
7521
7522 ;; Walking around summary lines with displaying articles.
7523
7524 (defun gnus-summary-expand-window (&optional arg)
7525   "Make the summary buffer take up the entire Emacs frame.
7526 Given a prefix, will force an `article' buffer configuration."
7527   (interactive "P")
7528   (if arg
7529       (gnus-configure-windows 'article 'force)
7530     (gnus-configure-windows 'summary 'force)))
7531
7532 (defun gnus-summary-display-article (article &optional all-header)
7533   "Display ARTICLE in article buffer."
7534   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7535                (with-current-buffer gnus-article-buffer
7536                  (eq major-mode 'gnus-article-mode)))
7537     (gnus-article-setup-buffer))
7538   (gnus-set-global-variables)
7539   (with-current-buffer gnus-article-buffer
7540     (setq gnus-article-charset gnus-newsgroup-charset)
7541     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7542     (mm-enable-multibyte))
7543   (if (null article)
7544       nil
7545     (prog1
7546         (if gnus-summary-display-article-function
7547             (funcall gnus-summary-display-article-function article all-header)
7548           (gnus-article-prepare article all-header))
7549       (gnus-run-hooks 'gnus-select-article-hook)
7550       (when (and gnus-current-article
7551                  (not (zerop gnus-current-article)))
7552         (gnus-summary-goto-subject gnus-current-article))
7553       (gnus-summary-recenter)
7554       (when (and gnus-use-trees gnus-show-threads)
7555         (gnus-possibly-generate-tree article)
7556         (gnus-highlight-selected-tree article))
7557       ;; Successfully display article.
7558       (gnus-article-set-window-start
7559        (cdr (assq article gnus-newsgroup-bookmarks))))))
7560
7561 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7562   "Select the current article.
7563 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7564 non-nil, the article will be re-fetched even if it already present in
7565 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7566 be displayed."
7567   ;; Make sure we are in the summary buffer to work around bbdb bug.
7568   (unless (eq major-mode 'gnus-summary-mode)
7569     (set-buffer gnus-summary-buffer))
7570   (let ((article (or article (gnus-summary-article-number)))
7571         (all-headers (not (not all-headers))) ;Must be t or nil.
7572         gnus-summary-display-article-function)
7573     (and (not pseudo)
7574          (gnus-summary-article-pseudo-p article)
7575          (error "This is a pseudo-article"))
7576     (with-current-buffer gnus-summary-buffer
7577       (if (or (and gnus-single-article-buffer
7578                    (or (null gnus-current-article)
7579                        (null gnus-article-current)
7580                        (null (get-buffer gnus-article-buffer))
7581                        (not (eq article (cdr gnus-article-current)))
7582                        (not (equal (car gnus-article-current)
7583                                    gnus-newsgroup-name))))
7584               (and (not gnus-single-article-buffer)
7585                    (or (null gnus-current-article)
7586                        (not (eq gnus-current-article article))))
7587               force)
7588           ;; The requested article is different from the current article.
7589           (progn
7590             (gnus-summary-display-article article all-headers)
7591             (when (gnus-buffer-live-p gnus-article-buffer)
7592               (with-current-buffer gnus-article-buffer
7593                 (if (not gnus-article-decoded-p) ;; a local variable
7594                     (mm-disable-multibyte))))
7595             (gnus-article-set-window-start
7596              (cdr (assq article gnus-newsgroup-bookmarks)))
7597             article)
7598         'old))))
7599
7600 (defun gnus-summary-force-verify-and-decrypt ()
7601   "Display buttons for signed/encrypted parts and verify/decrypt them."
7602   (interactive)
7603   (let ((mm-verify-option 'known)
7604         (mm-decrypt-option 'known)
7605         (gnus-article-emulate-mime t)
7606         (gnus-buttonized-mime-types (append (list "multipart/signed"
7607                                                   "multipart/encrypted")
7608                                             gnus-buttonized-mime-types)))
7609     (gnus-summary-select-article nil 'force)))
7610
7611 (defun gnus-summary-set-current-mark (&optional current-mark)
7612   "Obsolete function."
7613   nil)
7614
7615 (defun gnus-summary-next-article (&optional unread subject backward push)
7616   "Select the next article.
7617 If UNREAD, only unread articles are selected.
7618 If SUBJECT, only articles with SUBJECT are selected.
7619 If BACKWARD, the previous article is selected instead of the next."
7620   (interactive "P")
7621   ;; Make sure we are in the summary buffer.
7622   (unless (eq major-mode 'gnus-summary-mode)
7623     (set-buffer gnus-summary-buffer))
7624   (cond
7625    ;; Is there such an article?
7626    ((and (gnus-summary-search-forward unread subject backward)
7627          (or (gnus-summary-display-article (gnus-summary-article-number))
7628              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7629     (gnus-summary-position-point))
7630    ;; If not, we try the first unread, if that is wanted.
7631    ((and subject
7632          gnus-auto-select-same
7633          (gnus-summary-first-unread-article))
7634     (gnus-summary-position-point)
7635     (gnus-message 6 "Wrapped"))
7636    ;; Try to get next/previous article not displayed in this group.
7637    ((and gnus-auto-extend-newsgroup
7638          (not unread) (not subject))
7639     (gnus-summary-goto-article
7640      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7641      nil (count-lines (point-min) (point))))
7642    ;; Go to next/previous group.
7643    (t
7644     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7645       (gnus-summary-jump-to-group gnus-newsgroup-name))
7646     (let ((cmd (if (featurep 'xemacs)
7647                    last-command-char
7648                  last-command-event))
7649           (point
7650            (with-current-buffer gnus-group-buffer
7651              (point)))
7652           (group
7653            (if (eq gnus-keep-same-level 'best)
7654                (gnus-summary-best-group gnus-newsgroup-name)
7655              (gnus-summary-search-group backward gnus-keep-same-level))))
7656       ;; For some reason, the group window gets selected.  We change
7657       ;; it back.
7658       (select-window (get-buffer-window (current-buffer)))
7659       ;; Select next unread newsgroup automagically.
7660       (cond
7661        ((or (not gnus-auto-select-next)
7662             (not cmd))
7663         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7664        ((or (eq gnus-auto-select-next 'quietly)
7665             (and (eq gnus-auto-select-next 'slightly-quietly)
7666                  push)
7667             (and (eq gnus-auto-select-next 'almost-quietly)
7668                  (gnus-summary-last-article-p)))
7669         ;; Select quietly.
7670         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7671             (gnus-summary-exit)
7672           (gnus-message 7 "No more%s articles (%s)..."
7673                         (if unread " unread" "")
7674                         (if group (concat "selecting " group)
7675                           "exiting"))
7676           (gnus-summary-next-group nil group backward)))
7677        (t
7678         (when (gnus-key-press-event-p last-input-event)
7679           (gnus-summary-walk-group-buffer
7680            gnus-newsgroup-name cmd unread backward point))))))))
7681
7682 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7683   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7684                       (?\C-p (gnus-group-prev-unread-group 1))))
7685         (cursor-in-echo-area t)
7686         keve key group ended prompt)
7687     (with-current-buffer gnus-group-buffer
7688       (goto-char start)
7689       (setq group
7690             (if (eq gnus-keep-same-level 'best)
7691                 (gnus-summary-best-group gnus-newsgroup-name)
7692               (gnus-summary-search-group backward gnus-keep-same-level))))
7693     (while (not ended)
7694       (setq prompt
7695             (format
7696              "No more%s articles%s " (if unread " unread" "")
7697              (if (and group
7698                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7699                  (format " (Type %s for %s [%s])"
7700                          (single-key-description cmd)
7701                          (gnus-group-decoded-name group)
7702                          (gnus-group-unread group))
7703                (format " (Type %s to exit %s)"
7704                        (single-key-description cmd)
7705                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7706       ;; Confirm auto selection.
7707       (setq key (car (setq keve (gnus-read-event-char prompt)))
7708             ended t)
7709       (cond
7710        ((assq key keystrokes)
7711         (let ((obuf (current-buffer)))
7712           (switch-to-buffer gnus-group-buffer)
7713           (when group
7714             (gnus-group-jump-to-group group))
7715           (eval (cadr (assq key keystrokes)))
7716           (setq group (gnus-group-group-name))
7717           (switch-to-buffer obuf))
7718         (setq ended nil))
7719        ((equal key cmd)
7720         (if (or (not group)
7721                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7722             (gnus-summary-exit)
7723           (gnus-summary-next-group nil group backward)))
7724        (t
7725         (push (cdr keve) unread-command-events))))))
7726
7727 (defun gnus-summary-next-unread-article ()
7728   "Select unread article after current one."
7729   (interactive)
7730   (gnus-summary-next-article
7731    (or (not (eq gnus-summary-goto-unread 'never))
7732        (gnus-summary-last-article-p (gnus-summary-article-number)))
7733    (and gnus-auto-select-same
7734         (gnus-summary-article-subject))))
7735
7736 (defun gnus-summary-prev-article (&optional unread subject)
7737   "Select the article before the current one.
7738 If UNREAD is non-nil, only unread articles are selected."
7739   (interactive "P")
7740   (gnus-summary-next-article unread subject t))
7741
7742 (defun gnus-summary-prev-unread-article ()
7743   "Select unread article before current one."
7744   (interactive)
7745   (gnus-summary-prev-article
7746    (or (not (eq gnus-summary-goto-unread 'never))
7747        (gnus-summary-first-article-p (gnus-summary-article-number)))
7748    (and gnus-auto-select-same
7749         (gnus-summary-article-subject))))
7750
7751 (defun gnus-summary-next-page (&optional lines circular stop)
7752   "Show next page of the selected article.
7753 If at the end of the current article, select the next article.
7754 LINES says how many lines should be scrolled up.
7755
7756 If CIRCULAR is non-nil, go to the start of the article instead of
7757 selecting the next article when reaching the end of the current
7758 article.
7759
7760 If STOP is non-nil, just stop when reaching the end of the message.
7761
7762 Also see the variable `gnus-article-skip-boring'."
7763   (interactive "P")
7764   (setq gnus-summary-buffer (current-buffer))
7765   (gnus-set-global-variables)
7766   (let ((article (gnus-summary-article-number))
7767         (article-window (get-buffer-window gnus-article-buffer t))
7768         endp)
7769     ;; If the buffer is empty, we have no article.
7770     (unless article
7771       (error "No article to select"))
7772     (gnus-configure-windows 'article)
7773     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7774         (if (and (eq gnus-summary-goto-unread 'never)
7775                  (not (gnus-summary-last-article-p article)))
7776             (gnus-summary-next-article)
7777           (gnus-summary-next-unread-article))
7778       (if (or (null gnus-current-article)
7779               (null gnus-article-current)
7780               (/= article (cdr gnus-article-current))
7781               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7782           ;; Selected subject is different from current article's.
7783           (gnus-summary-display-article article)
7784         (when article-window
7785           (gnus-eval-in-buffer-window gnus-article-buffer
7786             (setq endp (or (gnus-article-next-page lines)
7787                            (gnus-article-only-boring-p))))
7788           (when endp
7789             (cond ((or stop gnus-summary-stop-at-end-of-message)
7790                    (gnus-message 3 "End of message"))
7791                   (circular
7792                    (gnus-summary-beginning-of-article))
7793                   (lines
7794                    (gnus-message 3 "End of message"))
7795                   ((null lines)
7796                    (if (and (eq gnus-summary-goto-unread 'never)
7797                             (not (gnus-summary-last-article-p article)))
7798                        (gnus-summary-next-article)
7799                      (gnus-summary-next-unread-article))))))))
7800     (gnus-summary-recenter)
7801     (gnus-summary-position-point)))
7802
7803 (defun gnus-summary-prev-page (&optional lines move)
7804   "Show previous page of selected article.
7805 Argument LINES specifies lines to be scrolled down.
7806 If MOVE, move to the previous unread article if point is at
7807 the beginning of the buffer."
7808   (interactive "P")
7809   (let ((article (gnus-summary-article-number))
7810         (article-window (get-buffer-window gnus-article-buffer t))
7811         endp)
7812     (gnus-configure-windows 'article)
7813     (if (or (null gnus-current-article)
7814             (null gnus-article-current)
7815             (/= article (cdr gnus-article-current))
7816             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7817         ;; Selected subject is different from current article's.
7818         (gnus-summary-display-article article)
7819       (gnus-summary-recenter)
7820       (when article-window
7821         (gnus-eval-in-buffer-window gnus-article-buffer
7822           (setq endp (gnus-article-prev-page lines)))
7823         (when (and move endp)
7824           (cond (lines
7825                  (gnus-message 3 "Beginning of message"))
7826                 ((null lines)
7827                  (if (and (eq gnus-summary-goto-unread 'never)
7828                           (not (gnus-summary-first-article-p article)))
7829                      (gnus-summary-prev-article)
7830                    (gnus-summary-prev-unread-article))))))))
7831   (gnus-summary-position-point))
7832
7833 (defun gnus-summary-prev-page-or-article (&optional lines)
7834   "Show previous page of selected article.
7835 Argument LINES specifies lines to be scrolled down.
7836 If at the beginning of the article, go to the next article."
7837   (interactive "P")
7838   (gnus-summary-prev-page lines t))
7839
7840 (defun gnus-summary-scroll-up (lines)
7841   "Scroll up (or down) one line current article.
7842 Argument LINES specifies lines to be scrolled up (or down if negative)."
7843   (interactive "p")
7844   (gnus-configure-windows 'article)
7845   (gnus-summary-show-thread)
7846   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7847     (gnus-eval-in-buffer-window gnus-article-buffer
7848       (cond ((> lines 0)
7849              (when (gnus-article-next-page lines)
7850                (gnus-message 3 "End of message")))
7851             ((< lines 0)
7852              (gnus-article-prev-page (- lines))))))
7853   (gnus-summary-recenter)
7854   (gnus-summary-position-point))
7855
7856 (defun gnus-summary-scroll-down (lines)
7857   "Scroll down (or up) one line current article.
7858 Argument LINES specifies lines to be scrolled down (or up if negative)."
7859   (interactive "p")
7860   (gnus-summary-scroll-up (- lines)))
7861
7862 (defun gnus-summary-next-same-subject ()
7863   "Select next article which has the same subject as current one."
7864   (interactive)
7865   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7866
7867 (defun gnus-summary-prev-same-subject ()
7868   "Select previous article which has the same subject as current one."
7869   (interactive)
7870   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7871
7872 (defun gnus-summary-next-unread-same-subject ()
7873   "Select next unread article which has the same subject as current one."
7874   (interactive)
7875   (gnus-summary-next-article t (gnus-summary-article-subject)))
7876
7877 (defun gnus-summary-prev-unread-same-subject ()
7878   "Select previous unread article which has the same subject as current one."
7879   (interactive)
7880   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7881
7882 (defun gnus-summary-first-unread-article ()
7883   "Select the first unread article.
7884 Return nil if there are no unread articles."
7885   (interactive)
7886   (prog1
7887       (when (gnus-summary-first-subject t)
7888         (gnus-summary-show-thread)
7889         (gnus-summary-first-subject t)
7890         (gnus-summary-display-article (gnus-summary-article-number)))
7891     (gnus-summary-position-point)))
7892
7893 (defun gnus-summary-first-unread-subject ()
7894   "Place the point on the subject line of the first unread article.
7895 Return nil if there are no unread articles."
7896   (interactive)
7897   (prog1
7898       (when (gnus-summary-first-subject t)
7899         (gnus-summary-show-thread)
7900         (gnus-summary-first-subject t))
7901     (gnus-summary-position-point)))
7902
7903 (defun gnus-summary-first-unseen-subject ()
7904   "Place the point on the subject line of the first unseen article.
7905 Return nil if there are no unseen articles."
7906   (interactive)
7907   (prog1
7908       (when (gnus-summary-first-subject nil nil t)
7909         (gnus-summary-show-thread)
7910         (gnus-summary-first-subject nil nil t))
7911     (gnus-summary-position-point)))
7912
7913 (defun gnus-summary-first-unseen-or-unread-subject ()
7914   "Place the point on the subject line of the first unseen article or,
7915 if all article have been seen, on the subject line of the first unread
7916 article."
7917   (interactive)
7918   (prog1
7919       (unless (when (gnus-summary-first-subject nil nil t)
7920                 (gnus-summary-show-thread)
7921                 (gnus-summary-first-subject nil nil t))
7922         (when (gnus-summary-first-subject t)
7923           (gnus-summary-show-thread)
7924           (gnus-summary-first-subject t)))
7925     (gnus-summary-position-point)))
7926
7927 (defun gnus-summary-first-article ()
7928   "Select the first article.
7929 Return nil if there are no articles."
7930   (interactive)
7931   (prog1
7932       (when (gnus-summary-first-subject)
7933         (gnus-summary-show-thread)
7934         (gnus-summary-first-subject)
7935         (gnus-summary-display-article (gnus-summary-article-number)))
7936     (gnus-summary-position-point)))
7937
7938 (defun gnus-summary-best-unread-article (&optional arg)
7939   "Select the unread article with the highest score.
7940 If given a prefix argument, select the next unread article that has a
7941 score higher than the default score."
7942   (interactive "P")
7943   (let ((article (if arg
7944                      (gnus-summary-better-unread-subject)
7945                    (gnus-summary-best-unread-subject))))
7946     (if article
7947         (gnus-summary-goto-article article)
7948       (error "No unread articles"))))
7949
7950 (defun gnus-summary-best-unread-subject ()
7951   "Select the unread subject with the highest score."
7952   (interactive)
7953   (let ((best -1000000)
7954         (data gnus-newsgroup-data)
7955         article score)
7956     (while data
7957       (and (gnus-data-unread-p (car data))
7958            (> (setq score
7959                     (gnus-summary-article-score (gnus-data-number (car data))))
7960               best)
7961            (setq best score
7962                  article (gnus-data-number (car data))))
7963       (setq data (cdr data)))
7964     (when article
7965       (gnus-summary-goto-subject article))
7966     (gnus-summary-position-point)
7967     article))
7968
7969 (defun gnus-summary-better-unread-subject ()
7970   "Select the first unread subject that has a score over the default score."
7971   (interactive)
7972   (let ((data gnus-newsgroup-data)
7973         article score)
7974     (while (and (setq article (gnus-data-number (car data)))
7975                 (or (gnus-data-read-p (car data))
7976                     (not (> (gnus-summary-article-score article)
7977                             gnus-summary-default-score))))
7978       (setq data (cdr data)))
7979     (when article
7980       (gnus-summary-goto-subject article))
7981     (gnus-summary-position-point)
7982     article))
7983
7984 (defun gnus-summary-last-subject ()
7985   "Go to the last displayed subject line in the group."
7986   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7987     (when article
7988       (gnus-summary-goto-subject article))))
7989
7990 (defun gnus-summary-goto-article (article &optional all-headers force)
7991   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7992 If ALL-HEADERS is non-nil, no header lines are hidden.
7993 If FORCE, go to the article even if it isn't displayed.  If FORCE
7994 is a number, it is the line the article is to be displayed on."
7995   (interactive
7996    (list
7997     (completing-read
7998      "Article number or Message-ID: "
7999      (mapcar (lambda (number) (list (int-to-string number)))
8000              gnus-newsgroup-limit))
8001     current-prefix-arg
8002     t))
8003   (prog1
8004       (if (and (stringp article)
8005                (string-match "@\\|%40" article))
8006           (gnus-summary-refer-article article)
8007         (when (stringp article)
8008           (setq article (string-to-number article)))
8009         (if (gnus-summary-goto-subject article force)
8010             (gnus-summary-display-article article all-headers)
8011           (gnus-message 4 "Couldn't go to article %s" article) nil))
8012     (gnus-summary-position-point)))
8013
8014 (defun gnus-summary-goto-last-article ()
8015   "Go to the previously read article."
8016   (interactive)
8017   (prog1
8018       (when gnus-last-article
8019         (gnus-summary-goto-article gnus-last-article nil t))
8020     (gnus-summary-position-point)))
8021
8022 (defun gnus-summary-pop-article (number)
8023   "Pop one article off the history and go to the previous.
8024 NUMBER articles will be popped off."
8025   (interactive "p")
8026   (let (to)
8027     (setq gnus-newsgroup-history
8028           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8029     (if to
8030         (gnus-summary-goto-article (car to) nil t)
8031       (error "Article history empty")))
8032   (gnus-summary-position-point))
8033
8034 ;; Summary commands and functions for limiting the summary buffer.
8035
8036 (defun gnus-summary-limit-to-articles (n)
8037   "Limit the summary buffer to the next N articles.
8038 If not given a prefix, use the process marked articles instead."
8039   (interactive "P")
8040   (prog1
8041       (let ((articles (gnus-summary-work-articles n)))
8042         (setq gnus-newsgroup-processable nil)
8043         (gnus-summary-limit articles))
8044     (gnus-summary-position-point)))
8045
8046 (defun gnus-summary-pop-limit (&optional total)
8047   "Restore the previous limit.
8048 If given a prefix, remove all limits."
8049   (interactive "P")
8050   (when total
8051     (setq gnus-newsgroup-limits
8052           (list (mapcar (lambda (h) (mail-header-number h))
8053                         gnus-newsgroup-headers))))
8054   (unless gnus-newsgroup-limits
8055     (error "No limit to pop"))
8056   (prog1
8057       (gnus-summary-limit nil 'pop)
8058     (gnus-summary-position-point)))
8059
8060 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8061   "Limit the summary buffer to articles that have subjects that match a regexp.
8062 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8063   (interactive
8064    (list (read-string (if current-prefix-arg
8065                           "Exclude subject (regexp): "
8066                         "Limit to subject (regexp): "))
8067          nil current-prefix-arg))
8068   (unless header
8069     (setq header "subject"))
8070   (when (not (equal "" subject))
8071     (prog1
8072         (let ((articles (gnus-summary-find-matching
8073                          (or header "subject") subject 'all nil nil
8074                          not-matching)))
8075           (unless articles
8076             (error "Found no matches for \"%s\"" subject))
8077           (gnus-summary-limit articles))
8078       (gnus-summary-position-point))))
8079
8080 (defun gnus-summary-limit-to-author (from &optional not-matching)
8081   "Limit the summary buffer to articles that have authors that match a regexp.
8082 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8083   (interactive
8084    (list (read-string (if current-prefix-arg
8085                           "Exclude author (regexp): "
8086                         "Limit to author (regexp): "))
8087          current-prefix-arg))
8088   (gnus-summary-limit-to-subject from "from" not-matching))
8089
8090 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8091   "Limit the summary buffer to articles with the given RECIPIENT.
8092
8093 If NOT-MATCHING, exclude RECIPIENT.
8094
8095 To and Cc headers are checked.  You need to include them in
8096 `nnmail-extra-headers'."
8097   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8098   (interactive
8099    (list (read-string (format "%s recipient (regexp): "
8100                               (if current-prefix-arg "Exclude" "Limit to")))
8101          current-prefix-arg))
8102   (when (not (equal "" recipient))
8103     (prog1 (let* ((to
8104                    (if (memq 'To nnmail-extra-headers)
8105                        (gnus-summary-find-matching
8106                         (cons 'extra 'To) recipient 'all nil nil
8107                         not-matching)
8108                      (gnus-message
8109                       1 "`To' isn't present in `nnmail-extra-headers'")
8110                      (sit-for 1)
8111                      nil))
8112                   (cc
8113                    (if (memq 'Cc nnmail-extra-headers)
8114                        (gnus-summary-find-matching
8115                         (cons 'extra 'Cc) recipient 'all nil nil
8116                         not-matching)
8117                      (gnus-message
8118                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8119                      (sit-for 1)
8120                      nil))
8121                   (articles
8122                    (if not-matching
8123                        ;; We need the numbers that are in both lists:
8124                        (mapcar (lambda (a)
8125                                  (and (memq a to) a))
8126                                cc)
8127                      (nconc to cc))))
8128              (unless articles
8129                (error "Found no matches for \"%s\"" recipient))
8130              (gnus-summary-limit articles))
8131       (gnus-summary-position-point))))
8132
8133 (defun gnus-summary-limit-to-address (address &optional not-matching)
8134   "Limit the summary buffer to articles with the given ADDRESS.
8135
8136 If NOT-MATCHING, exclude ADDRESS.
8137
8138 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8139 in `nnmail-extra-headers'."
8140   (interactive
8141    (list (read-string (format "%s address (regexp): "
8142                               (if current-prefix-arg "Exclude" "Limit to")))
8143          current-prefix-arg))
8144   (when (not (equal "" address))
8145     (prog1 (let* ((to
8146                    (if (memq 'To nnmail-extra-headers)
8147                        (gnus-summary-find-matching
8148                         (cons 'extra 'To) address 'all nil nil
8149                         not-matching)
8150                      (gnus-message
8151                       1 "`To' isn't present in `nnmail-extra-headers'")
8152                      (sit-for 1)
8153                      t))
8154                   (cc
8155                    (if (memq 'Cc nnmail-extra-headers)
8156                        (gnus-summary-find-matching
8157                         (cons 'extra 'Cc) address 'all nil nil
8158                         not-matching)
8159                      (gnus-message
8160                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8161                      (sit-for 1)
8162                      t))
8163                   (from
8164                    (gnus-summary-find-matching "from" address
8165                                                'all nil nil not-matching))
8166                   (articles
8167                    (if not-matching
8168                        ;; We need the numbers that are in all lists:
8169                        (if (eq cc t)
8170                            (if (eq to t)
8171                                from
8172                              (mapcar (lambda (a) (car (memq a from))) to))
8173                          (if (eq to t)
8174                              (mapcar (lambda (a) (car (memq a from))) cc)
8175                            (mapcar (lambda (a) (car (memq a from)))
8176                                    (mapcar (lambda (a) (car (memq a to)))
8177                                            cc))))
8178                      (nconc (if (eq to t) nil to)
8179                             (if (eq cc t) nil cc)
8180                             from))))
8181              (unless articles
8182                (error "Found no matches for \"%s\"" address))
8183              (gnus-summary-limit articles))
8184       (gnus-summary-position-point))))
8185
8186 (defun gnus-summary-limit-strange-charsets-predicate (header)
8187   (when (fboundp 'char-charset)
8188     (let ((string (concat (mail-header-subject header)
8189                           (mail-header-from header)))
8190           charset found)
8191       (dotimes (i (1- (length string)))
8192         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8193         (when (string-match "unicode\\|big\\|japanese" charset)
8194           (setq found t)))
8195       found)))
8196
8197 (defun gnus-summary-limit-to-predicate (predicate)
8198   "Limit to articles where PREDICATE returns non-nil.
8199 PREDICATE will be called with the header structures of the
8200 articles."
8201   (let ((articles nil)
8202         (case-fold-search t))
8203     (dolist (header gnus-newsgroup-headers)
8204       (when (funcall predicate header)
8205         (push (mail-header-number header) articles)))
8206     (gnus-summary-limit (nreverse articles))))
8207
8208 (defun gnus-summary-limit-to-age (age &optional younger-p)
8209   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8210 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8211 articles that are younger than AGE days."
8212   (interactive
8213    (let ((younger current-prefix-arg)
8214          (days-got nil)
8215          days)
8216      (while (not days-got)
8217        (setq days (if younger
8218                       (read-string "Limit to articles younger than (in days, older when negative): ")
8219                     (read-string
8220                      "Limit to articles older than (in days, younger when negative): ")))
8221        (when (> (length days) 0)
8222          (setq days (read days)))
8223        (if (numberp days)
8224            (progn
8225              (setq days-got t)
8226              (when (< days 0)
8227                (setq younger (not younger))
8228                (setq days (* days -1))))
8229          (message "Please enter a number.")
8230          (sleep-for 1)))
8231      (list days younger)))
8232   (prog1
8233       (let ((data gnus-newsgroup-data)
8234             (cutoff (days-to-time age))
8235             articles d date is-younger)
8236         (while (setq d (pop data))
8237           (when (and (vectorp (gnus-data-header d))
8238                      (setq date (mail-header-date (gnus-data-header d))))
8239             (setq is-younger (time-less-p
8240                               (time-since (gnus-date-get-time date))
8241                               cutoff))
8242             (when (if younger-p
8243                       is-younger
8244                     (not is-younger))
8245               (push (gnus-data-number d) articles))))
8246         (gnus-summary-limit (nreverse articles)))
8247     (gnus-summary-position-point)))
8248
8249 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8250   "Limit the summary buffer to articles that match an 'extra' header."
8251   (interactive
8252    (let ((header
8253           (intern
8254            (gnus-completing-read-with-default
8255             (symbol-name (car gnus-extra-headers))
8256             (if current-prefix-arg
8257                 "Exclude extra header"
8258               "Limit extra header")
8259             (mapcar (lambda (x)
8260                       (cons (symbol-name x) x))
8261                     gnus-extra-headers)
8262             nil
8263             t))))
8264      (list header
8265            (read-string (format "%s header %s (regexp): "
8266                                 (if current-prefix-arg "Exclude" "Limit to")
8267                                 header))
8268            current-prefix-arg)))
8269   (when (not (equal "" regexp))
8270     (prog1
8271         (let ((articles (gnus-summary-find-matching
8272                          (cons 'extra header) regexp 'all nil nil
8273                          not-matching)))
8274           (unless articles
8275             (error "Found no matches for \"%s\"" regexp))
8276           (gnus-summary-limit articles))
8277       (gnus-summary-position-point))))
8278
8279 (defun gnus-summary-limit-to-display-predicate ()
8280   "Limit the summary buffer to the predicated in the `display' group parameter."
8281   (interactive)
8282   (unless gnus-newsgroup-display
8283     (error "There is no `display' group parameter"))
8284   (let (articles)
8285     (dolist (number gnus-newsgroup-articles)
8286       (when (funcall gnus-newsgroup-display)
8287         (push number articles)))
8288     (gnus-summary-limit articles))
8289   (gnus-summary-position-point))
8290
8291 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8292 (make-obsolete
8293  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8294
8295 (defun gnus-summary-limit-to-unread (&optional all)
8296   "Limit the summary buffer to articles that are not marked as read.
8297 If ALL is non-nil, limit strictly to unread articles."
8298   (interactive "P")
8299   (if all
8300       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8301     (gnus-summary-limit-to-marks
8302      ;; Concat all the marks that say that an article is read and have
8303      ;; those removed.
8304      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8305            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8306            gnus-low-score-mark gnus-expirable-mark
8307            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8308            gnus-duplicate-mark gnus-souped-mark)
8309      'reverse)))
8310
8311 (defun gnus-summary-limit-to-headers (match &optional reverse)
8312   "Limit the summary buffer to articles that have headers that match MATCH.
8313 If REVERSE (the prefix), limit to articles that don't match."
8314   (interactive "sMatch headers (regexp): \nP")
8315   (gnus-summary-limit-to-bodies match reverse t))
8316
8317 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8318   "Limit the summary buffer to articles that have bodies that match MATCH.
8319 If REVERSE (the prefix), limit to articles that don't match."
8320   (interactive "sMatch body (regexp): \nP")
8321   (let ((articles nil)
8322         (gnus-select-article-hook nil)  ;Disable hook.
8323         (gnus-article-prepare-hook nil)
8324         (gnus-use-article-prefetch nil)
8325         (gnus-keep-backlog nil)
8326         (gnus-break-pages nil)
8327         (gnus-summary-display-arrow nil)
8328         (gnus-updated-mode-lines nil)
8329         (gnus-auto-center-summary nil)
8330         (gnus-display-mime-function nil))
8331     (dolist (data gnus-newsgroup-data)
8332       (let (gnus-mark-article-hook)
8333         (gnus-summary-select-article t t nil (gnus-data-number data)))
8334       (with-current-buffer gnus-article-buffer
8335         (article-goto-body)
8336         (let* ((case-fold-search t)
8337                (found (if headersp
8338                           (re-search-backward match nil t)
8339                         (re-search-forward match nil t))))
8340           (when (or (and found
8341                          (not reverse))
8342                     (and (not found)
8343                          reverse))
8344             (push (gnus-data-number data) articles)))))
8345     (if (not articles)
8346         (message "No messages matched")
8347       (gnus-summary-limit articles)))
8348   (gnus-summary-position-point))
8349
8350 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8351   "Limit the summary buffer to articles that aren't part on any thread.
8352 If THREADSP (the prefix), limit to articles that are in threads."
8353   (interactive "P")
8354   (let ((articles nil)
8355         thread-articles
8356         threads)
8357     (dolist (thread gnus-newsgroup-threads)
8358       (if (stringp (car thread))
8359           (dolist (thread (cdr thread))
8360             (push thread threads))
8361         (push thread threads)))
8362     (dolist (thread threads)
8363       (setq thread-articles (gnus-articles-in-thread thread))
8364       (when (or (and threadsp
8365                      (> (length thread-articles) 1))
8366                 (and (not threadsp)
8367                      (= (length thread-articles) 1)))
8368         (setq articles (nconc thread-articles articles))))
8369     (if (not articles)
8370         (message "No messages matched")
8371       (gnus-summary-limit articles))
8372     (gnus-summary-position-point)))
8373
8374 (defun gnus-summary-limit-to-replied (&optional unreplied)
8375   "Limit the summary buffer to replied articles.
8376 If UNREPLIED (the prefix), limit to unreplied articles."
8377   (interactive "P")
8378   (if unreplied
8379       (gnus-summary-limit
8380        (gnus-set-difference gnus-newsgroup-articles
8381         gnus-newsgroup-replied))
8382     (gnus-summary-limit gnus-newsgroup-replied))
8383   (gnus-summary-position-point))
8384
8385 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8386 (make-obsolete 'gnus-summary-delete-marked-with
8387                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8388
8389 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8390   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8391 If REVERSE, limit the summary buffer to articles that are marked
8392 with MARKS.  MARKS can either be a string of marks or a list of marks.
8393 Returns how many articles were removed."
8394   (interactive "sMarks: ")
8395   (gnus-summary-limit-to-marks marks t))
8396
8397 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8398   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8399 If REVERSE (the prefix), limit the summary buffer to articles that are
8400 not marked with MARKS.  MARKS can either be a string of marks or a
8401 list of marks.
8402 Returns how many articles were removed."
8403   (interactive "sMarks: \nP")
8404   (prog1
8405       (let ((data gnus-newsgroup-data)
8406             (marks (if (listp marks) marks
8407                      (append marks nil))) ; Transform to list.
8408             articles)
8409         (while data
8410           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8411                   (memq (gnus-data-mark (car data)) marks))
8412             (push (gnus-data-number (car data)) articles))
8413           (setq data (cdr data)))
8414         (gnus-summary-limit articles))
8415     (gnus-summary-position-point)))
8416
8417 (defun gnus-summary-limit-to-score (score)
8418   "Limit to articles with score at or above SCORE."
8419   (interactive "NLimit to articles with score of at least: ")
8420   (let ((data gnus-newsgroup-data)
8421         articles)
8422     (while data
8423       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8424                 score)
8425         (push (gnus-data-number (car data)) articles))
8426       (setq data (cdr data)))
8427     (prog1
8428         (gnus-summary-limit articles)
8429       (gnus-summary-position-point))))
8430
8431 (defun gnus-summary-limit-to-unseen ()
8432   "Limit to unseen articles."
8433   (interactive)
8434   (prog1
8435       (gnus-summary-limit gnus-newsgroup-unseen)
8436     (gnus-summary-position-point)))
8437
8438 (defun gnus-summary-limit-include-thread (id)
8439   "Display all the hidden articles that is in the thread with ID in it.
8440 When called interactively, ID is the Message-ID of the current
8441 article."
8442   (interactive (list (mail-header-id (gnus-summary-article-header))))
8443   (let ((articles (gnus-articles-in-thread
8444                    (gnus-id-to-thread (gnus-root-id id)))))
8445     (prog1
8446         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8447       (gnus-summary-limit-include-matching-articles
8448        "subject"
8449        (regexp-quote (gnus-simplify-subject-re
8450                       (mail-header-subject (gnus-id-to-header id)))))
8451       (gnus-summary-position-point))))
8452
8453 (defun gnus-summary-limit-include-matching-articles (header regexp)
8454   "Display all the hidden articles that have HEADERs that match REGEXP."
8455   (interactive (list (read-string "Match on header: ")
8456                      (read-string "Regexp: ")))
8457   (let ((articles (gnus-find-matching-articles header regexp)))
8458     (prog1
8459         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8460       (gnus-summary-position-point))))
8461
8462 (defun gnus-summary-insert-dormant-articles ()
8463   "Insert all the dormant articles for this group into the current buffer."
8464   (interactive)
8465   (let ((gnus-verbose (max 6 gnus-verbose)))
8466     (if (not gnus-newsgroup-dormant)
8467         (gnus-message 3 "No dormant articles for this group")
8468       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8469
8470 (defun gnus-summary-insert-ticked-articles ()
8471   "Insert ticked articles for this group into the current buffer."
8472   (interactive)
8473   (let ((gnus-verbose (max 6 gnus-verbose)))
8474     (if (not gnus-newsgroup-marked)
8475         (gnus-message 3 "No ticked articles for this group")
8476       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8477
8478 (defun gnus-summary-limit-include-dormant ()
8479   "Display all the hidden articles that are marked as dormant.
8480 Note that this command only works on a subset of the articles currently
8481 fetched for this group."
8482   (interactive)
8483   (unless gnus-newsgroup-dormant
8484     (error "There are no dormant articles in this group"))
8485   (prog1
8486       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8487     (gnus-summary-position-point)))
8488
8489 (defun gnus-summary-limit-exclude-dormant ()
8490   "Hide all dormant articles."
8491   (interactive)
8492   (prog1
8493       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8494     (gnus-summary-position-point)))
8495
8496 (defun gnus-summary-limit-exclude-childless-dormant ()
8497   "Hide all dormant articles that have no children."
8498   (interactive)
8499   (let ((data (gnus-data-list t))
8500         articles d children)
8501     ;; Find all articles that are either not dormant or have
8502     ;; children.
8503     (while (setq d (pop data))
8504       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8505                 (and (setq children
8506                            (gnus-article-children (gnus-data-number d)))
8507                      (let (found)
8508                        (while children
8509                          (when (memq (car children) articles)
8510                            (setq children nil
8511                                  found t))
8512                          (pop children))
8513                        found)))
8514         (push (gnus-data-number d) articles)))
8515     ;; Do the limiting.
8516     (prog1
8517         (gnus-summary-limit articles)
8518       (gnus-summary-position-point))))
8519
8520 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8521   "Mark all unread excluded articles as read.
8522 If ALL, mark even excluded ticked and dormants as read."
8523   (interactive "P")
8524   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8525   (let ((articles (gnus-sorted-ndifference
8526                    (sort
8527                     (mapcar (lambda (h) (mail-header-number h))
8528                             gnus-newsgroup-headers)
8529                     '<)
8530                    gnus-newsgroup-limit))
8531         article)
8532     (setq gnus-newsgroup-unreads
8533           (gnus-sorted-intersection gnus-newsgroup-unreads
8534                                     gnus-newsgroup-limit))
8535     (if all
8536         (setq gnus-newsgroup-dormant nil
8537               gnus-newsgroup-marked nil
8538               gnus-newsgroup-reads
8539               (nconc
8540                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8541                gnus-newsgroup-reads))
8542       (while (setq article (pop articles))
8543         (unless (or (memq article gnus-newsgroup-dormant)
8544                     (memq article gnus-newsgroup-marked))
8545           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8546
8547 (defun gnus-summary-limit (articles &optional pop)
8548   (if pop
8549       ;; We pop the previous limit off the stack and use that.
8550       (setq articles (car gnus-newsgroup-limits)
8551             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8552     ;; We use the new limit, so we push the old limit on the stack.
8553     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8554   ;; Set the limit.
8555   (setq gnus-newsgroup-limit articles)
8556   (let ((total (length gnus-newsgroup-data))
8557         (data (gnus-data-find-list (gnus-summary-article-number)))
8558         (gnus-summary-mark-below nil)   ; Inhibit this.
8559         found)
8560     ;; This will do all the work of generating the new summary buffer
8561     ;; according to the new limit.
8562     (gnus-summary-prepare)
8563     ;; Hide any threads, possibly.
8564     (gnus-summary-maybe-hide-threads)
8565     ;; Try to return to the article you were at, or one in the
8566     ;; neighborhood.
8567     (when data
8568       ;; We try to find some article after the current one.
8569       (while data
8570         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8571           (setq data nil
8572                 found t))
8573         (setq data (cdr data))))
8574     (unless found
8575       ;; If there is no data, that means that we were after the last
8576       ;; article.  The same goes when we can't find any articles
8577       ;; after the current one.
8578       (goto-char (point-max))
8579       (gnus-summary-find-prev))
8580     (gnus-set-mode-line 'summary)
8581     ;; We return how many articles were removed from the summary
8582     ;; buffer as a result of the new limit.
8583     (- total (length gnus-newsgroup-data))))
8584
8585 (defsubst gnus-invisible-cut-children (threads)
8586   (let ((num 0))
8587     (while threads
8588       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8589         (incf num))
8590       (pop threads))
8591     (< num 2)))
8592
8593 (defsubst gnus-cut-thread (thread)
8594   "Go forwards in the thread until we find an article that we want to display."
8595   (when (or (eq gnus-fetch-old-headers 'some)
8596             (eq gnus-fetch-old-headers 'invisible)
8597             (numberp gnus-fetch-old-headers)
8598             (eq gnus-build-sparse-threads 'some)
8599             (eq gnus-build-sparse-threads 'more))
8600     ;; Deal with old-fetched headers and sparse threads.
8601     (while (and
8602             thread
8603             (or
8604              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8605              (gnus-summary-article-ancient-p
8606               (mail-header-number (car thread))))
8607             (if (or (<= (length (cdr thread)) 1)
8608                     (eq gnus-fetch-old-headers 'invisible))
8609                 (setq gnus-newsgroup-limit
8610                       (delq (mail-header-number (car thread))
8611                             gnus-newsgroup-limit)
8612                       thread (cadr thread))
8613               (when (gnus-invisible-cut-children (cdr thread))
8614                 (let ((th (cdr thread)))
8615                   (while th
8616                     (if (memq (mail-header-number (caar th))
8617                               gnus-newsgroup-limit)
8618                         (setq thread (car th)
8619                               th nil)
8620                       (setq th (cdr th))))))))))
8621   thread)
8622
8623 (defun gnus-cut-threads (threads)
8624   "Cut off all uninteresting articles from the beginning of THREADS."
8625   (when (or (eq gnus-fetch-old-headers 'some)
8626             (eq gnus-fetch-old-headers 'invisible)
8627             (numberp gnus-fetch-old-headers)
8628             (eq gnus-build-sparse-threads 'some)
8629             (eq gnus-build-sparse-threads 'more))
8630     (let ((th threads))
8631       (while th
8632         (setcar th (gnus-cut-thread (car th)))
8633         (setq th (cdr th)))))
8634   ;; Remove nixed out threads.
8635   (delq nil threads))
8636
8637 (defun gnus-summary-initial-limit (&optional show-if-empty)
8638   "Figure out what the initial limit is supposed to be on group entry.
8639 This entails weeding out unwanted dormants, low-scored articles,
8640 fetch-old-headers verbiage, and so on."
8641   ;; Most groups have nothing to remove.
8642   (unless (or gnus-inhibit-limiting
8643               (and (null gnus-newsgroup-dormant)
8644                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8645                    (not (eq gnus-fetch-old-headers 'some))
8646                    (not (numberp gnus-fetch-old-headers))
8647                    (not (eq gnus-fetch-old-headers 'invisible))
8648                    (null gnus-summary-expunge-below)
8649                    (not (eq gnus-build-sparse-threads 'some))
8650                    (not (eq gnus-build-sparse-threads 'more))
8651                    (null gnus-thread-expunge-below)
8652                    (not gnus-use-nocem)))
8653     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8654     (setq gnus-newsgroup-limit nil)
8655     (mapatoms
8656      (lambda (node)
8657        (unless (car (symbol-value node))
8658          ;; These threads have no parents -- they are roots.
8659          (let ((nodes (cdr (symbol-value node)))
8660                thread)
8661            (while nodes
8662              (if (and gnus-thread-expunge-below
8663                       (< (gnus-thread-total-score (car nodes))
8664                          gnus-thread-expunge-below))
8665                  (gnus-expunge-thread (pop nodes))
8666                (setq thread (pop nodes))
8667                (gnus-summary-limit-children thread))))))
8668      gnus-newsgroup-dependencies)
8669     ;; If this limitation resulted in an empty group, we might
8670     ;; pop the previous limit and use it instead.
8671     (when (and (not gnus-newsgroup-limit)
8672                show-if-empty)
8673       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8674     gnus-newsgroup-limit))
8675
8676 (defun gnus-summary-limit-children (thread)
8677   "Return 1 if this subthread is visible and 0 if it is not."
8678   ;; First we get the number of visible children to this thread.  This
8679   ;; is done by recursing down the thread using this function, so this
8680   ;; will really go down to a leaf article first, before slowly
8681   ;; working its way up towards the root.
8682   (when thread
8683     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8684            (children
8685            (if (cdr thread)
8686                (apply '+ (mapcar 'gnus-summary-limit-children
8687                                  (cdr thread)))
8688              0))
8689           (number (mail-header-number (car thread)))
8690           score)
8691       (if (and
8692            (not (memq number gnus-newsgroup-marked))
8693            (or
8694             ;; If this article is dormant and has absolutely no visible
8695             ;; children, then this article isn't visible.
8696             (and (memq number gnus-newsgroup-dormant)
8697                  (zerop children))
8698             ;; If this is "fetch-old-headered" and there is no
8699             ;; visible children, then we don't want this article.
8700             (and (or (eq gnus-fetch-old-headers 'some)
8701                      (numberp gnus-fetch-old-headers))
8702                  (gnus-summary-article-ancient-p number)
8703                  (zerop children))
8704             ;; If this is "fetch-old-headered" and `invisible', then
8705             ;; we don't want this article.
8706             (and (eq gnus-fetch-old-headers 'invisible)
8707                  (gnus-summary-article-ancient-p number))
8708             ;; If this is a sparsely inserted article with no children,
8709             ;; we don't want it.
8710             (and (eq gnus-build-sparse-threads 'some)
8711                  (gnus-summary-article-sparse-p number)
8712                  (zerop children))
8713             ;; If we use expunging, and this article is really
8714             ;; low-scored, then we don't want this article.
8715             (when (and gnus-summary-expunge-below
8716                        (< (setq score
8717                                 (or (cdr (assq number gnus-newsgroup-scored))
8718                                     gnus-summary-default-score))
8719                           gnus-summary-expunge-below))
8720               ;; We increase the expunge-tally here, but that has
8721               ;; nothing to do with the limits, really.
8722               (incf gnus-newsgroup-expunged-tally)
8723               ;; We also mark as read here, if that's wanted.
8724               (when (and gnus-summary-mark-below
8725                          (< score gnus-summary-mark-below))
8726                 (setq gnus-newsgroup-unreads
8727                       (delq number gnus-newsgroup-unreads))
8728                 (if gnus-newsgroup-auto-expire
8729                     (push number gnus-newsgroup-expirable)
8730                   (push (cons number gnus-low-score-mark)
8731                         gnus-newsgroup-reads)))
8732               t)
8733             ;; Do the `display' group parameter.
8734             (and gnus-newsgroup-display
8735                  (not (funcall gnus-newsgroup-display)))
8736             ;; Check NoCeM things.
8737             (when (and gnus-use-nocem
8738                        (gnus-nocem-unwanted-article-p
8739                         (mail-header-id (car thread))))
8740               (setq gnus-newsgroup-unreads
8741                     (delq number gnus-newsgroup-unreads))
8742               t)))
8743           ;; Nope, invisible article.
8744           0
8745         ;; Ok, this article is to be visible, so we add it to the limit
8746         ;; and return 1.
8747         (push number gnus-newsgroup-limit)
8748         1))))
8749
8750 (defun gnus-expunge-thread (thread)
8751   "Mark all articles in THREAD as read."
8752   (let* ((number (mail-header-number (car thread))))
8753     (incf gnus-newsgroup-expunged-tally)
8754     ;; We also mark as read here, if that's wanted.
8755     (setq gnus-newsgroup-unreads
8756           (delq number gnus-newsgroup-unreads))
8757     (if gnus-newsgroup-auto-expire
8758         (push number gnus-newsgroup-expirable)
8759       (push (cons number gnus-low-score-mark)
8760             gnus-newsgroup-reads)))
8761   ;; Go recursively through all subthreads.
8762   (mapcar 'gnus-expunge-thread (cdr thread)))
8763
8764 ;; Summary article oriented commands
8765
8766 (defun gnus-summary-refer-parent-article (n)
8767   "Refer parent article N times.
8768 If N is negative, go to ancestor -N instead.
8769 The difference between N and the number of articles fetched is returned."
8770   (interactive "p")
8771   (let ((skip 1)
8772         error header ref)
8773     (when (not (natnump n))
8774       (setq skip (abs n)
8775             n 1))
8776     (while (and (> n 0)
8777                 (not error))
8778       (setq header (gnus-summary-article-header))
8779       (if (and (eq (mail-header-number header)
8780                    (cdr gnus-article-current))
8781                (equal gnus-newsgroup-name
8782                       (car gnus-article-current)))
8783           ;; If we try to find the parent of the currently
8784           ;; displayed article, then we take a look at the actual
8785           ;; References header, since this is slightly more
8786           ;; reliable than the References field we got from the
8787           ;; server.
8788           (with-current-buffer gnus-original-article-buffer
8789             (nnheader-narrow-to-headers)
8790             (unless (setq ref (message-fetch-field "references"))
8791               (when (setq ref (message-fetch-field "in-reply-to"))
8792                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8793             (widen))
8794         (setq ref
8795               ;; It's not the current article, so we take a bet on
8796               ;; the value we got from the server.
8797               (mail-header-references header)))
8798       (if (and ref
8799                (not (equal ref "")))
8800           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8801             (gnus-message 1 "Couldn't find parent"))
8802         (gnus-message 1 "No references in article %d"
8803                       (gnus-summary-article-number))
8804         (setq error t))
8805       (decf n))
8806     (gnus-summary-position-point)
8807     n))
8808
8809 (defun gnus-summary-refer-references ()
8810   "Fetch all articles mentioned in the References header.
8811 Return the number of articles fetched."
8812   (interactive)
8813   (let ((ref (mail-header-references (gnus-summary-article-header)))
8814         (current (gnus-summary-article-number))
8815         (n 0))
8816     (if (or (not ref)
8817             (equal ref ""))
8818         (error "No References in the current article")
8819       ;; For each Message-ID in the References header...
8820       (while (string-match "<[^>]*>" ref)
8821         (incf n)
8822         ;; ... fetch that article.
8823         (gnus-summary-refer-article
8824          (prog1 (match-string 0 ref)
8825            (setq ref (substring ref (match-end 0))))))
8826       (gnus-summary-goto-subject current)
8827       (gnus-summary-position-point)
8828       n)))
8829
8830 (defun gnus-summary-refer-thread (&optional limit)
8831   "Fetch all articles in the current thread.
8832 If LIMIT (the numerical prefix), fetch that many old headers instead
8833 of what's specified by the `gnus-refer-thread-limit' variable."
8834   (interactive "P")
8835   (let ((id (mail-header-id (gnus-summary-article-header)))
8836         (limit (if limit (prefix-numeric-value limit)
8837                  gnus-refer-thread-limit)))
8838     (unless (eq gnus-fetch-old-headers 'invisible)
8839       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8840       ;; Retrieve the headers and read them in.
8841       (if (eq (if (numberp limit)
8842                   (gnus-retrieve-headers
8843                    (list (min
8844                           (+ (mail-header-number
8845                               (gnus-summary-article-header))
8846                              limit)
8847                           gnus-newsgroup-end))
8848                    gnus-newsgroup-name (* limit 2))
8849                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8850                 ;; headers.
8851                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8852                                        gnus-newsgroup-name limit))
8853               'nov)
8854           (gnus-build-all-threads)
8855         (error "Can't fetch thread from back ends that don't support NOV"))
8856       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8857     (gnus-summary-limit-include-thread id)))
8858
8859 (defun gnus-summary-refer-article (message-id)
8860   "Fetch an article specified by MESSAGE-ID."
8861   (interactive "sMessage-ID: ")
8862   (when (and (stringp message-id)
8863              (not (zerop (length message-id))))
8864     (setq message-id (gnus-replace-in-string message-id " " ""))
8865     ;; Construct the correct Message-ID if necessary.
8866     ;; Suggested by tale@pawl.rpi.edu.
8867     (unless (string-match "^<" message-id)
8868       (setq message-id (concat "<" message-id)))
8869     (unless (string-match ">$" message-id)
8870       (setq message-id (concat message-id ">")))
8871     ;; People often post MIDs from URLs, so unhex it:
8872     (unless (string-match "@" message-id)
8873       (setq message-id (gnus-url-unhex-string message-id)))
8874     (let* ((header (gnus-id-to-header message-id))
8875            (sparse (and header
8876                         (gnus-summary-article-sparse-p
8877                          (mail-header-number header))
8878                         (memq (mail-header-number header)
8879                               gnus-newsgroup-limit)))
8880            number)
8881       (cond
8882        ;; If the article is present in the buffer we just go to it.
8883        ((and header
8884              (or (not (gnus-summary-article-sparse-p
8885                        (mail-header-number header)))
8886                  sparse))
8887         (prog1
8888             (gnus-summary-goto-article
8889              (mail-header-number header) nil t)
8890           (when sparse
8891             (gnus-summary-update-article (mail-header-number header)))))
8892        (t
8893         ;; We fetch the article.
8894         (catch 'found
8895           (dolist (gnus-override-method (gnus-refer-article-methods))
8896             (when (and (gnus-check-server gnus-override-method)
8897                        ;; Fetch the header,
8898                        (setq number (gnus-summary-insert-subject message-id)))
8899               ;; and display the article.
8900               (gnus-summary-select-article nil nil nil number)
8901               (throw 'found t)))
8902           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8903
8904 (defun gnus-refer-article-methods ()
8905   "Return a list of referable methods."
8906   (cond
8907    ;; No method, so we default to current and native.
8908    ((null gnus-refer-article-method)
8909     (list gnus-current-select-method gnus-select-method))
8910    ;; Current.
8911    ((eq 'current gnus-refer-article-method)
8912     (list gnus-current-select-method))
8913    ;; List of select methods.
8914    ((not (and (symbolp (car gnus-refer-article-method))
8915               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8916     (let (out)
8917       (dolist (method gnus-refer-article-method)
8918         (push (if (eq 'current method)
8919                   gnus-current-select-method
8920                 method)
8921               out))
8922       (nreverse out)))
8923    ;; One single select method.
8924    (t
8925     (list gnus-refer-article-method))))
8926
8927 (defun gnus-summary-edit-parameters ()
8928   "Edit the group parameters of the current group."
8929   (interactive)
8930   (gnus-group-edit-group gnus-newsgroup-name 'params))
8931
8932 (defun gnus-summary-customize-parameters ()
8933   "Customize the group parameters of the current group."
8934   (interactive)
8935   (gnus-group-customize gnus-newsgroup-name))
8936
8937 (defun gnus-summary-enter-digest-group (&optional force)
8938   "Enter an nndoc group based on the current article.
8939 If FORCE, force a digest interpretation.  If not, try
8940 to guess what the document format is."
8941   (interactive "P")
8942   (let ((conf gnus-current-window-configuration))
8943     (save-window-excursion
8944       (save-excursion
8945         (let (gnus-article-prepare-hook
8946               gnus-display-mime-function
8947               gnus-break-pages)
8948           (gnus-summary-select-article))))
8949     (setq gnus-current-window-configuration conf)
8950     (let* ((name (format "%s-%d"
8951                          (gnus-group-prefixed-name
8952                           gnus-newsgroup-name (list 'nndoc ""))
8953                          (with-current-buffer gnus-summary-buffer
8954                            gnus-current-article)))
8955            (ogroup gnus-newsgroup-name)
8956            (params (append (gnus-info-params (gnus-get-info ogroup))
8957                            (list (cons 'to-group ogroup))
8958                            (list (cons 'parent-group ogroup))
8959                            (list (cons 'save-article-group ogroup))))
8960            (case-fold-search t)
8961            (buf (current-buffer))
8962            dig to-address)
8963       (with-current-buffer gnus-original-article-buffer
8964         ;; Have the digest group inherit the main mail address of
8965         ;; the parent article.
8966         (when (setq to-address (or (gnus-fetch-field "reply-to")
8967                                    (gnus-fetch-field "from")))
8968           (setq params
8969                 (append
8970                  (list (cons 'to-address
8971                              (funcall gnus-decode-encoded-address-function
8972                                       to-address))))))
8973         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8974         (insert-buffer-substring gnus-original-article-buffer)
8975         ;; Remove lines that may lead nndoc to misinterpret the
8976         ;; document type.
8977         (narrow-to-region
8978          (goto-char (point-min))
8979          (or (search-forward "\n\n" nil t) (point)))
8980         (goto-char (point-min))
8981         (delete-matching-lines "^Path:\\|^From ")
8982         (widen))
8983       (unwind-protect
8984           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8985                     (gnus-newsgroup-ephemeral-ignored-charsets
8986                      gnus-newsgroup-ignored-charsets))
8987                 (gnus-group-read-ephemeral-group
8988                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8989                               (nndoc-article-type
8990                                ,(if force 'mbox 'guess)))
8991                  t nil nil nil
8992                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8993                                                         "ADAPT")))))
8994               ;; Make all postings to this group go to the parent group.
8995               (nconc (gnus-info-params (gnus-get-info name))
8996                      params)
8997             ;; Couldn't select this doc group.
8998             (switch-to-buffer buf)
8999             (gnus-set-global-variables)
9000             (gnus-configure-windows 'summary)
9001             (gnus-message 3 "Article couldn't be entered?"))
9002         (kill-buffer dig)))))
9003
9004 (defun gnus-summary-read-document (n)
9005   "Open a new group based on the current article(s).
9006 This will allow you to read digests and other similar
9007 documents as newsgroups.
9008 Obeys the standard process/prefix convention."
9009   (interactive "P")
9010   (let* ((ogroup gnus-newsgroup-name)
9011          (params (append (gnus-info-params (gnus-get-info ogroup))
9012                          (list (cons 'to-group ogroup))))
9013          group egroup groups vgroup)
9014     (dolist (article (gnus-summary-work-articles n))
9015       (setq group (format "%s-%d" gnus-newsgroup-name article))
9016       (gnus-summary-remove-process-mark article)
9017       (when (gnus-summary-display-article article)
9018         (save-excursion ;;What for?
9019           (with-temp-buffer
9020             (insert-buffer-substring gnus-original-article-buffer)
9021             ;; Remove some headers that may lead nndoc to make
9022             ;; the wrong guess.
9023             (message-narrow-to-head)
9024             (goto-char (point-min))
9025             (delete-matching-lines "^Path:\\|^From ")
9026             (widen)
9027             (if (setq egroup
9028                       (gnus-group-read-ephemeral-group
9029                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9030                                      (nndoc-article-type guess))
9031                        t nil t))
9032                 (progn
9033                   ;; Make all postings to this group go to the parent group.
9034                   (nconc (gnus-info-params (gnus-get-info egroup))
9035                          params)
9036                   (push egroup groups))
9037               ;; Couldn't select this doc group.
9038               (gnus-error 3 "Article couldn't be entered"))))))
9039     ;; Now we have selected all the documents.
9040     (cond
9041      ((not groups)
9042       (error "None of the articles could be interpreted as documents"))
9043      ((gnus-group-read-ephemeral-group
9044        (setq vgroup (format
9045                      "nnvirtual:%s-%s" gnus-newsgroup-name
9046                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9047        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9048        t
9049        (cons (current-buffer) 'summary)))
9050      (t
9051       (error "Couldn't select virtual nndoc group")))))
9052
9053 (defun gnus-summary-isearch-article (&optional regexp-p)
9054   "Do incremental search forward on the current article.
9055 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9056   (interactive "P")
9057   (gnus-summary-select-article)
9058   (gnus-configure-windows 'article)
9059   (gnus-eval-in-buffer-window gnus-article-buffer
9060     (save-restriction
9061       (widen)
9062       (isearch-forward regexp-p))))
9063
9064 (defun gnus-summary-repeat-search-article-forward ()
9065   "Repeat the previous search forwards."
9066   (interactive)
9067   (unless gnus-last-search-regexp
9068     (error "No previous search"))
9069   (gnus-summary-search-article-forward gnus-last-search-regexp))
9070
9071 (defun gnus-summary-repeat-search-article-backward ()
9072   "Repeat the previous search backwards."
9073   (interactive)
9074   (unless gnus-last-search-regexp
9075     (error "No previous search"))
9076   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9077
9078 (defun gnus-summary-search-article-forward (regexp &optional backward)
9079   "Search for an article containing REGEXP forward.
9080 If BACKWARD, search backward instead."
9081   (interactive
9082    (list (read-string
9083           (format "Search article %s (regexp%s): "
9084                   (if current-prefix-arg "backward" "forward")
9085                   (if gnus-last-search-regexp
9086                       (concat ", default " gnus-last-search-regexp)
9087                     "")))
9088          current-prefix-arg))
9089   (if (string-equal regexp "")
9090       (setq regexp (or gnus-last-search-regexp ""))
9091     (setq gnus-last-search-regexp regexp)
9092     (setq gnus-article-before-search gnus-current-article))
9093   ;; Intentionally set gnus-last-article.
9094   (setq gnus-last-article gnus-article-before-search)
9095   (let ((gnus-last-article gnus-last-article))
9096     (if (gnus-summary-search-article regexp backward)
9097         (gnus-summary-show-thread)
9098       (signal 'search-failed (list regexp)))))
9099
9100 (defun gnus-summary-search-article-backward (regexp)
9101   "Search for an article containing REGEXP backward."
9102   (interactive
9103    (list (read-string
9104           (format "Search article backward (regexp%s): "
9105                   (if gnus-last-search-regexp
9106                       (concat ", default " gnus-last-search-regexp)
9107                     "")))))
9108   (gnus-summary-search-article-forward regexp 'backward))
9109
9110 (defun gnus-summary-search-article (regexp &optional backward)
9111   "Search for an article containing REGEXP.
9112 Optional argument BACKWARD means do search for backward.
9113 `gnus-select-article-hook' is not called during the search."
9114   ;; We have to require this here to make sure that the following
9115   ;; dynamic binding isn't shadowed by autoloading.
9116   (require 'gnus-async)
9117   (require 'gnus-art)
9118   (let ((gnus-select-article-hook nil)  ;Disable hook.
9119         (gnus-article-prepare-hook nil)
9120         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9121         (gnus-use-article-prefetch nil)
9122         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9123         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9124         (gnus-visual nil)
9125         (gnus-keep-backlog nil)
9126         (gnus-break-pages nil)
9127         (gnus-summary-display-arrow nil)
9128         (gnus-updated-mode-lines nil)
9129         (gnus-auto-center-summary nil)
9130         (sum (current-buffer))
9131         (gnus-display-mime-function nil)
9132         (found nil)
9133         point)
9134     (gnus-save-hidden-threads
9135       (gnus-summary-select-article)
9136       (set-buffer gnus-article-buffer)
9137       (goto-char (window-point (get-buffer-window (current-buffer))))
9138       (when backward
9139         (forward-line -1))
9140       (while (not found)
9141         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9142         (if (if backward
9143                 (re-search-backward regexp nil t)
9144               (re-search-forward regexp nil t))
9145             ;; We found the regexp.
9146             (progn
9147               (setq found 'found)
9148               (beginning-of-line)
9149               (set-window-start
9150                (get-buffer-window (current-buffer))
9151                (point))
9152               (forward-line 1)
9153               (set-window-point
9154                (get-buffer-window (current-buffer))
9155                (point))
9156               (set-buffer sum)
9157               (setq point (point)))
9158           ;; We didn't find it, so we go to the next article.
9159           (set-buffer sum)
9160           (setq found 'not)
9161           (while (eq found 'not)
9162             (if (not (if backward (gnus-summary-find-prev)
9163                        (gnus-summary-find-next)))
9164                 ;; No more articles.
9165                 (setq found t)
9166               ;; Select the next article and adjust point.
9167               (unless (gnus-summary-article-sparse-p
9168                        (gnus-summary-article-number))
9169                 (setq found nil)
9170                 (gnus-summary-select-article)
9171                 (set-buffer gnus-article-buffer)
9172                 (widen)
9173                 (goto-char (if backward (point-max) (point-min))))))))
9174       (gnus-message 7 ""))
9175     ;; Return whether we found the regexp.
9176     (when (eq found 'found)
9177       (goto-char point)
9178       (gnus-summary-show-thread)
9179       (gnus-summary-goto-subject gnus-current-article)
9180       (gnus-summary-position-point)
9181       t)))
9182
9183 (defun gnus-find-matching-articles (header regexp)
9184   "Return a list of all articles that match REGEXP on HEADER.
9185 This search includes all articles in the current group that Gnus has
9186 fetched headers for, whether they are displayed or not."
9187   (let ((articles nil)
9188         ;; Can't eta-reduce because it's a macro.
9189         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9190         (case-fold-search t))
9191     (dolist (header gnus-newsgroup-headers)
9192       (when (string-match regexp (funcall func header))
9193         (push (mail-header-number header) articles)))
9194     (nreverse articles)))
9195
9196 (defun gnus-summary-find-matching (header regexp &optional backward unread
9197                                           not-case-fold not-matching)
9198   "Return a list of all articles that match REGEXP on HEADER.
9199 The search stars on the current article and goes forwards unless
9200 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9201 If UNREAD is non-nil, only unread articles will
9202 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9203 in the comparisons. If NOT-MATCHING, return a list of all articles that
9204 not match REGEXP on HEADER."
9205   (let ((case-fold-search (not not-case-fold))
9206         articles d func)
9207     (if (consp header)
9208         (if (eq (car header) 'extra)
9209             (setq func
9210                   `(lambda (h)
9211                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9212                          "")))
9213           (error "%s is an invalid header" header))
9214       (unless (fboundp (intern (concat "mail-header-" header)))
9215         (error "%s is not a valid header" header))
9216       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9217     (dolist (d (if (eq backward 'all)
9218                    gnus-newsgroup-data
9219                  (gnus-data-find-list
9220                   (gnus-summary-article-number)
9221                   (gnus-data-list backward))))
9222       (when (and (or (not unread)       ; We want all articles...
9223                      (gnus-data-unread-p d)) ; Or just unreads.
9224                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9225                  (if not-matching
9226                      (not (string-match
9227                            regexp
9228                            (funcall func (gnus-data-header d))))
9229                    (string-match regexp
9230                                  (funcall func (gnus-data-header d)))))
9231         (push (gnus-data-number d) articles))) ; Success!
9232     (nreverse articles)))
9233
9234 (defun gnus-summary-execute-command (header regexp command &optional backward)
9235   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9236 If HEADER is an empty string (or nil), the match is done on the entire
9237 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9238   (interactive
9239    (list (let ((completion-ignore-case t))
9240            (completing-read
9241             "Header name: "
9242             (mapcar (lambda (header) (list (format "%s" header)))
9243                     (append
9244                      '("Number" "Subject" "From" "Lines" "Date"
9245                        "Message-ID" "Xref" "References" "Body")
9246                      gnus-extra-headers))
9247             nil 'require-match))
9248          (read-string "Regexp: ")
9249          (read-key-sequence "Command: ")
9250          current-prefix-arg))
9251   (when (equal header "Body")
9252     (setq header ""))
9253   ;; Hidden thread subtrees must be searched as well.
9254   (gnus-summary-show-all-threads)
9255   ;; We don't want to change current point nor window configuration.
9256   (save-excursion
9257     (save-window-excursion
9258       (let (gnus-visual
9259             gnus-treat-strip-trailing-blank-lines
9260             gnus-treat-strip-leading-blank-lines
9261             gnus-treat-strip-multiple-blank-lines
9262             gnus-treat-hide-boring-headers
9263             gnus-treat-fold-newsgroups
9264             gnus-article-prepare-hook)
9265         (gnus-message 6 "Executing %s..." (key-description command))
9266         ;; We'd like to execute COMMAND interactively so as to give arguments.
9267         (gnus-execute header regexp
9268                       `(call-interactively ',(key-binding command))
9269                       backward)
9270         (gnus-message 6 "Executing %s...done" (key-description command))))))
9271
9272 (defun gnus-summary-beginning-of-article ()
9273   "Scroll the article back to the beginning."
9274   (interactive)
9275   (gnus-summary-select-article)
9276   (gnus-configure-windows 'article)
9277   (gnus-eval-in-buffer-window gnus-article-buffer
9278     (widen)
9279     (goto-char (point-min))
9280     (when gnus-break-pages
9281       (gnus-narrow-to-page))))
9282
9283 (defun gnus-summary-end-of-article ()
9284   "Scroll to the end of the article."
9285   (interactive)
9286   (gnus-summary-select-article)
9287   (gnus-configure-windows 'article)
9288   (gnus-eval-in-buffer-window gnus-article-buffer
9289     (widen)
9290     (goto-char (point-max))
9291     (recenter -3)
9292     (when gnus-break-pages
9293       (gnus-narrow-to-page))))
9294
9295 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9296   "Truncate to LEN and quote all \"(\"'s in STRING."
9297   (gnus-replace-in-string (if (and len (> (length string) len))
9298                               (substring string 0 len)
9299                             string)
9300                           "[()]" "\\\\\\&"))
9301
9302 (defun gnus-summary-print-article (&optional filename n)
9303   "Generate and print a PostScript image of the process-marked (mail) articles.
9304
9305 If used interactively, print the current article if none are
9306 process-marked.  With prefix arg, prompt the user for the name of the
9307 file to save in.
9308
9309 When used from Lisp, accept two optional args FILENAME and N.  N means
9310 to print the next N articles.  If N is negative, print the N previous
9311 articles.  If N is nil and articles have been marked with the process
9312 mark, print these instead.
9313
9314 If the optional first argument FILENAME is nil, send the image to the
9315 printer.  If FILENAME is a string, save the PostScript image in a file with
9316 that name.  If FILENAME is a number, prompt the user for the name of the file
9317 to save in."
9318   (interactive (list (ps-print-preprint current-prefix-arg)))
9319   (dolist (article (gnus-summary-work-articles n))
9320     (gnus-summary-select-article nil nil 'pseudo article)
9321     (gnus-eval-in-buffer-window gnus-article-buffer
9322       (gnus-print-buffer))
9323     (gnus-summary-remove-process-mark article))
9324   (ps-despool filename))
9325
9326 (defun gnus-print-buffer ()
9327   (let ((buffer (generate-new-buffer " *print*")))
9328     (unwind-protect
9329         (progn
9330           (copy-to-buffer buffer (point-min) (point-max))
9331           (set-buffer buffer)
9332           (gnus-remove-text-with-property 'gnus-decoration)
9333           (when (gnus-visual-p 'article-highlight 'highlight)
9334             ;; Copy-to-buffer doesn't copy overlay.  So redo
9335             ;; highlight.
9336             (let ((gnus-article-buffer buffer))
9337               (gnus-article-highlight-citation t)
9338               (gnus-article-highlight-signature)
9339               (gnus-article-emphasize)
9340               (gnus-article-delete-invisible-text)))
9341           (let ((ps-left-header
9342                  (list
9343                   (concat "("
9344                           (gnus-summary-print-truncate-and-quote
9345                            (mail-header-subject gnus-current-headers)
9346                            66) ")")
9347                   (concat "("
9348                           (gnus-summary-print-truncate-and-quote
9349                            (mail-header-from gnus-current-headers)
9350                            45) ")")))
9351                 (ps-right-header
9352                  (list
9353                   "/pagenumberstring load"
9354                   (concat "("
9355                           (mail-header-date gnus-current-headers) ")"))))
9356             (gnus-run-hooks 'gnus-ps-print-hook)
9357             (save-excursion
9358               (if ps-print-color-p
9359                   (ps-spool-buffer-with-faces)
9360                 (ps-spool-buffer)))))
9361       (kill-buffer buffer))))
9362
9363 (defun gnus-summary-show-article (&optional arg)
9364   "Force redisplaying of the current article.
9365 If ARG (the prefix) is a number, show the article with the charset
9366 defined in `gnus-summary-show-article-charset-alist', or the charset
9367 input.
9368 If ARG (the prefix) is non-nil and not a number, show the raw article
9369 without any article massaging functions being run.  Normally, the key
9370 strokes are `C-u g'."
9371   (interactive "P")
9372   (cond
9373    ((numberp arg)
9374     (gnus-summary-show-article t)
9375     (let ((gnus-newsgroup-charset
9376            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9377                (mm-read-coding-system
9378                 "View as charset: " ;; actually it is coding system.
9379                 (with-current-buffer gnus-article-buffer
9380                   (mm-detect-coding-region (point) (point-max))))))
9381           (gnus-newsgroup-ignored-charsets 'gnus-all))
9382       (gnus-summary-select-article nil 'force)
9383       (let ((deps gnus-newsgroup-dependencies)
9384             head header lines)
9385         (with-current-buffer gnus-original-article-buffer
9386           (save-restriction
9387             (message-narrow-to-head)
9388             (setq head (buffer-string))
9389             (goto-char (point-min))
9390             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9391               (goto-char (point-max))
9392               (widen)
9393               (setq lines (1- (count-lines (point) (point-max))))))
9394           (with-temp-buffer
9395             (insert (format "211 %d Article retrieved.\n"
9396                             (cdr gnus-article-current)))
9397             (insert head)
9398             (if lines (insert (format "Lines: %d\n" lines)))
9399             (insert ".\n")
9400             (let ((nntp-server-buffer (current-buffer)))
9401               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9402         (gnus-data-set-header
9403          (gnus-data-find (cdr gnus-article-current))
9404          header)
9405         (gnus-summary-update-article-line
9406          (cdr gnus-article-current) header)
9407         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9408           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9409    ((not arg)
9410     ;; Select the article the normal way.
9411     (gnus-summary-select-article nil 'force))
9412    (t
9413     ;; We have to require this here to make sure that the following
9414     ;; dynamic binding isn't shadowed by autoloading.
9415     (require 'gnus-async)
9416     (require 'gnus-art)
9417     ;; Bind the article treatment functions to nil.
9418     (let ((gnus-have-all-headers t)
9419           gnus-article-prepare-hook
9420           gnus-article-decode-hook
9421           gnus-display-mime-function
9422           gnus-break-pages)
9423       ;; Destroy any MIME parts.
9424       (when (gnus-buffer-live-p gnus-article-buffer)
9425         (with-current-buffer gnus-article-buffer
9426           (mm-destroy-parts gnus-article-mime-handles)
9427           ;; Set it to nil for safety reason.
9428           (setq gnus-article-mime-handle-alist nil)
9429           (setq gnus-article-mime-handles nil)))
9430       (gnus-summary-select-article nil 'force))))
9431   (gnus-summary-goto-subject gnus-current-article)
9432   (gnus-summary-position-point))
9433
9434 (defun gnus-summary-show-raw-article ()
9435   "Show the raw article without any article massaging functions being run."
9436   (interactive)
9437   (gnus-summary-show-article t))
9438
9439 (defun gnus-summary-verbose-headers (&optional arg)
9440   "Toggle permanent full header display.
9441 If ARG is a positive number, turn header display on.
9442 If ARG is a negative number, turn header display off."
9443   (interactive "P")
9444   (setq gnus-show-all-headers
9445         (cond ((or (not (numberp arg))
9446                    (zerop arg))
9447                (not gnus-show-all-headers))
9448               ((natnump arg)
9449                t)))
9450   (gnus-summary-show-article))
9451
9452 (defun gnus-summary-toggle-header (&optional arg)
9453   "Show the headers if they are hidden, or hide them if they are shown.
9454 If ARG is a positive number, show the entire header.
9455 If ARG is a negative number, hide the unwanted header lines."
9456   (interactive "P")
9457   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9458                      (get-buffer-window gnus-article-buffer t))))
9459     (with-current-buffer gnus-article-buffer
9460       (widen)
9461       (article-narrow-to-head)
9462       (let* ((inhibit-read-only t)
9463              (inhibit-point-motion-hooks t)
9464              (hidden (if (numberp arg)
9465                          (>= arg 0)
9466                        (or (not (looking-at "[^ \t\n]+:"))
9467                            (gnus-article-hidden-text-p 'headers))))
9468              s e)
9469         (delete-region (point-min) (point-max))
9470         (with-current-buffer gnus-original-article-buffer
9471           (goto-char (setq s (point-min)))
9472           (setq e (if (search-forward "\n\n" nil t)
9473                       (1- (point))
9474                     (point-max))))
9475         (insert-buffer-substring gnus-original-article-buffer s e)
9476         (run-hooks 'gnus-article-decode-hook)
9477         (if hidden
9478             (let ((gnus-treat-hide-headers nil)
9479                   (gnus-treat-hide-boring-headers nil))
9480               (gnus-delete-wash-type 'headers)
9481               (gnus-treat-article 'head))
9482           (gnus-treat-article 'head))
9483         (widen)
9484         (if window
9485             (set-window-start window (goto-char (point-min))))
9486         (if gnus-break-pages
9487             (gnus-narrow-to-page)
9488           (when (gnus-visual-p 'page-marker)
9489             (let ((inhibit-read-only t))
9490               (gnus-remove-text-with-property 'gnus-prev)
9491               (gnus-remove-text-with-property 'gnus-next))))
9492         (gnus-set-mode-line 'article)))))
9493
9494 (defun gnus-summary-show-all-headers ()
9495   "Make all header lines visible."
9496   (interactive)
9497   (gnus-summary-toggle-header 1))
9498
9499 (defun gnus-summary-caesar-message (&optional arg)
9500   "Caesar rotate the current article by 13.
9501 With a non-numerical prefix, also rotate headers.  A numerical
9502 prefix specifies how many places to rotate each letter forward."
9503   (interactive "P")
9504   (gnus-summary-select-article)
9505   (let ((mail-header-separator ""))
9506     (gnus-eval-in-buffer-window gnus-article-buffer
9507       (save-restriction
9508         (widen)
9509         (let ((start (window-start))
9510               (inhibit-read-only t))
9511           (if (equal arg '(4))
9512               (message-caesar-buffer-body nil t)
9513             (message-caesar-buffer-body arg))
9514           (set-window-start (get-buffer-window (current-buffer)) start)))))
9515   ;; Create buttons and stuff...
9516   (gnus-treat-article nil))
9517
9518 (declare-function idna-to-unicode "ext:idna" (str))
9519
9520 (defun gnus-summary-idna-message (&optional arg)
9521   "Decode IDNA encoded domain names in the current articles.
9522 IDNA encoded domain names looks like `xn--bar'.  If a string
9523 remain unencoded after running this function, it is likely an
9524 invalid IDNA string (`xn--bar' is invalid).
9525
9526 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9527 installed for this command to work."
9528   (interactive "P")
9529   (if (not (and (condition-case nil (require 'idna)
9530                   (file-error))
9531                 (mm-coding-system-p 'utf-8)
9532                 (executable-find (symbol-value 'idna-program))))
9533       (gnus-message
9534        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9535     (gnus-summary-select-article)
9536     (let ((mail-header-separator ""))
9537       (gnus-eval-in-buffer-window gnus-article-buffer
9538         (save-restriction
9539           (widen)
9540           (let ((start (window-start))
9541                 buffer-read-only)
9542             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9543               (replace-match (idna-to-unicode (match-string 1))))
9544             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9545
9546 (defun gnus-summary-morse-message (&optional arg)
9547   "Morse decode the current article."
9548   (interactive "P")
9549   (gnus-summary-select-article)
9550   (let ((mail-header-separator ""))
9551     (gnus-eval-in-buffer-window gnus-article-buffer
9552       (save-excursion
9553         (save-restriction
9554           (widen)
9555           (let ((pos (window-start))
9556                 (inhibit-read-only t))
9557             (goto-char (point-min))
9558             (when (message-goto-body)
9559               (gnus-narrow-to-body))
9560             (goto-char (point-min))
9561             (while (search-forward "·" (point-max) t)
9562               (replace-match "."))
9563             (unmorse-region (point-min) (point-max))
9564             (widen)
9565             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9566
9567 (defun gnus-summary-stop-page-breaking ()
9568   "Stop page breaking in the current article."
9569   (interactive)
9570   (gnus-summary-select-article)
9571   (gnus-eval-in-buffer-window gnus-article-buffer
9572     (widen)
9573     (when (gnus-visual-p 'page-marker)
9574       (let ((inhibit-read-only t))
9575         (gnus-remove-text-with-property 'gnus-prev)
9576         (gnus-remove-text-with-property 'gnus-next))
9577       (setq gnus-page-broken nil))))
9578
9579 (defun gnus-summary-move-article (&optional n to-newsgroup
9580                                             select-method action)
9581   "Move the current article to a different newsgroup.
9582 If N is a positive number, move the N next articles.
9583 If N is a negative number, move the N previous articles.
9584 If N is nil and any articles have been marked with the process mark,
9585 move those articles instead.
9586 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9587 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9588 re-spool using this method.
9589
9590 When called interactively with TO-NEWSGROUP being nil, the value of
9591 the variable `gnus-move-split-methods' is used for finding a default
9592 for the target newsgroup.
9593
9594 For this function to work, both the current newsgroup and the
9595 newsgroup that you want to move to have to support the `request-move'
9596 and `request-accept' functions.
9597
9598 ACTION can be either `move' (the default), `crosspost' or `copy'."
9599   (interactive "P")
9600   (unless action
9601     (setq action 'move))
9602   ;; Check whether the source group supports the required functions.
9603   (cond ((and (eq action 'move)
9604               (not (gnus-check-backend-function
9605                     'request-move-article gnus-newsgroup-name)))
9606          (error "The current group does not support article moving"))
9607         ((and (eq action 'crosspost)
9608               (not (gnus-check-backend-function
9609                     'request-replace-article gnus-newsgroup-name)))
9610          (error "The current group does not support article editing")))
9611   (let ((articles (gnus-summary-work-articles n))
9612         (prefix (if (gnus-check-backend-function
9613                      'request-move-article gnus-newsgroup-name)
9614                     (funcall gnus-move-group-prefix-function
9615                              gnus-newsgroup-name)
9616                   ""))
9617         (names '((move "Move" "Moving")
9618                  (copy "Copy" "Copying")
9619                  (crosspost "Crosspost" "Crossposting")))
9620         (copy-buf (save-excursion
9621                     (nnheader-set-temp-buffer " *copy article*")))
9622         art-group to-method new-xref article to-groups
9623         articles-to-update-marks encoded)
9624     (unless (assq action names)
9625       (error "Unknown action %s" action))
9626     ;; Read the newsgroup name.
9627     (when (and (not to-newsgroup)
9628                (not select-method))
9629       (if (and gnus-move-split-methods
9630                (not
9631                 (and (memq gnus-current-article articles)
9632                      (gnus-buffer-live-p gnus-original-article-buffer))))
9633           ;; When `gnus-move-split-methods' is non-nil, we have to
9634           ;; select an article to give `gnus-read-move-group-name' an
9635           ;; opportunity to suggest an appropriate default.  However,
9636           ;; we needn't render or mark the article.
9637           (let ((gnus-display-mime-function nil)
9638                 (gnus-article-prepare-hook nil)
9639                 (gnus-mark-article-hook nil))
9640             (gnus-summary-select-article nil nil nil (car articles))))
9641       (setq to-newsgroup (gnus-read-move-group-name
9642                           (cadr (assq action names))
9643                           (symbol-value
9644                            (intern (format "gnus-current-%s-group" action)))
9645                           articles prefix)
9646             encoded to-newsgroup
9647             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9648       (set (intern (format "gnus-current-%s-group" action))
9649            (mm-decode-coding-string
9650             to-newsgroup
9651             (gnus-group-name-charset to-method to-newsgroup))))
9652     (unless to-method
9653       (setq to-method (or select-method
9654                           (gnus-server-to-method
9655                            (gnus-group-method to-newsgroup)))))
9656     (setq to-newsgroup
9657           (or encoded
9658               (and to-newsgroup
9659                    (mm-encode-coding-string
9660                     to-newsgroup
9661                     (gnus-group-name-charset to-method to-newsgroup)))))
9662     ;; Check the method we are to move this article to...
9663     (unless (gnus-check-backend-function
9664              'request-accept-article (car to-method))
9665       (error "%s does not support article copying" (car to-method)))
9666     (unless (gnus-check-server to-method)
9667       (error "Can't open server %s" (car to-method)))
9668     (gnus-message 6 "%s to %s: %s..."
9669                   (caddr (assq action names))
9670                   (or (car select-method)
9671                       (gnus-group-decoded-name to-newsgroup))
9672                   articles)
9673     (while articles
9674       (setq article (pop articles))
9675       (setq
9676        art-group
9677        (cond
9678         ;; Move the article.
9679         ((eq action 'move)
9680          ;; Remove this article from future suppression.
9681          (gnus-dup-unsuppress-article article)
9682          (let* ((from-method (gnus-find-method-for-group
9683                               gnus-newsgroup-name))
9684                 (to-method (or select-method
9685                                (gnus-find-method-for-group to-newsgroup)))
9686                 (move-is-internal (gnus-method-equal from-method to-method)))
9687            (gnus-request-move-article
9688             article                     ; Article to move
9689             gnus-newsgroup-name         ; From newsgroup
9690             (nth 1 (gnus-find-method-for-group
9691                     gnus-newsgroup-name)) ; Server
9692             (list 'gnus-request-accept-article
9693                   to-newsgroup (list 'quote select-method)
9694                   (not articles) t)     ; Accept form
9695             (not articles)              ; Only save nov last time
9696             move-is-internal)))         ; is this move internal?
9697         ;; Copy the article.
9698         ((eq action 'copy)
9699          (with-current-buffer copy-buf
9700            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9701              (save-restriction
9702                (nnheader-narrow-to-headers)
9703                (dolist (hdr gnus-copy-article-ignored-headers)
9704                  (message-remove-header hdr t)))
9705              (gnus-request-accept-article
9706               to-newsgroup select-method (not articles) t))))
9707         ;; Crosspost the article.
9708         ((eq action 'crosspost)
9709          (let ((xref (message-tokenize-header
9710                       (mail-header-xref (gnus-summary-article-header article))
9711                       " ")))
9712            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9713                                   ":" (number-to-string article)))
9714            (unless xref
9715              (setq xref (list (system-name))))
9716            (setq new-xref
9717                  (concat
9718                   (mapconcat 'identity
9719                              (delete "Xref:" (delete new-xref xref))
9720                              " ")
9721                   " " new-xref))
9722            (with-current-buffer copy-buf
9723              ;; First put the article in the destination group.
9724              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9725              (when (consp (setq art-group
9726                                 (gnus-request-accept-article
9727                                  to-newsgroup select-method (not articles) t)))
9728                (setq new-xref (concat new-xref " " (car art-group)
9729                                       ":"
9730                                       (number-to-string (cdr art-group))))
9731                ;; Now we have the new Xrefs header, so we insert
9732                ;; it and replace the new article.
9733                (nnheader-replace-header "Xref" new-xref)
9734                (gnus-request-replace-article
9735                 (cdr art-group) to-newsgroup (current-buffer) t)
9736                art-group))))))
9737       (cond
9738        ((not art-group)
9739         (gnus-message 1 "Couldn't %s article %s: %s"
9740                       (cadr (assq action names)) article
9741                       (nnheader-get-report (car to-method))))
9742        ((eq art-group 'junk)
9743         (when (eq action 'move)
9744           (gnus-summary-mark-article article gnus-canceled-mark)
9745           (gnus-message 4 "Deleted article %s" article)
9746           ;; run the delete hook
9747           (run-hook-with-args 'gnus-summary-article-delete-hook
9748                               action
9749                               (gnus-data-header
9750                                (assoc article (gnus-data-list nil)))
9751                               gnus-newsgroup-name nil
9752                               select-method)))
9753        (t
9754         (let* ((pto-group (gnus-group-prefixed-name
9755                            (car art-group) to-method))
9756                (info (gnus-get-info pto-group))
9757                (to-group (gnus-info-group info))
9758                to-marks)
9759           ;; Update the group that has been moved to.
9760           (when (and info
9761                      (memq action '(move copy)))
9762             (unless (member to-group to-groups)
9763               (push to-group to-groups))
9764
9765             (unless (memq article gnus-newsgroup-unreads)
9766               (push 'read to-marks)
9767               (gnus-info-set-read
9768                info (gnus-add-to-range (gnus-info-read info)
9769                                        (list (cdr art-group)))))
9770
9771             ;; See whether the article is to be put in the cache.
9772             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9773                    (marks (if expirable
9774                               gnus-article-mark-lists
9775                             (delete '(expirable . expire)
9776                                     (copy-sequence gnus-article-mark-lists))))
9777                    (to-article (cdr art-group)))
9778
9779               ;; Enter the article into the cache in the new group,
9780               ;; if that is required.
9781               (when gnus-use-cache
9782                 (gnus-cache-possibly-enter-article
9783                  to-group to-article
9784                  (memq article gnus-newsgroup-marked)
9785                  (memq article gnus-newsgroup-dormant)
9786                  (memq article gnus-newsgroup-unreads)))
9787
9788               (when gnus-preserve-marks
9789                 ;; Copy any marks over to the new group.
9790                 (when (and (equal to-group gnus-newsgroup-name)
9791                            (not (memq article gnus-newsgroup-unreads)))
9792                   ;; Mark this article as read in this group.
9793                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9794                   (setcdr (gnus-active to-group) to-article)
9795                   (setcdr gnus-newsgroup-active to-article))
9796
9797                 (while marks
9798                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9799                     (when (memq article (symbol-value
9800                                          (intern (format "gnus-newsgroup-%s"
9801                                                          (caar marks)))))
9802                       (push (cdar marks) to-marks)
9803                       ;; If the other group is the same as this group,
9804                       ;; then we have to add the mark to the list.
9805                       (when (equal to-group gnus-newsgroup-name)
9806                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9807                              (cons to-article
9808                                    (symbol-value
9809                                     (intern (format "gnus-newsgroup-%s"
9810                                                     (caar marks)))))))
9811                       ;; Copy the marks to other group.
9812                       (gnus-add-marked-articles
9813                        to-group (cdar marks) (list to-article) info)))
9814                   (setq marks (cdr marks)))
9815
9816                 (when (and expirable
9817                            gnus-mark-copied-or-moved-articles-as-expirable
9818                            (not (memq 'expire to-marks)))
9819                   ;; Mark this article as expirable.
9820                   (push 'expire to-marks)
9821                   (when (equal to-group gnus-newsgroup-name)
9822                     (push to-article gnus-newsgroup-expirable))
9823                   ;; Copy the expirable mark to other group.
9824                   (gnus-add-marked-articles
9825                    to-group 'expire (list to-article) info))
9826
9827                 (gnus-request-set-mark
9828                  to-group (list (list (list to-article) 'add to-marks))))
9829
9830               (gnus-dribble-enter
9831                (concat "(gnus-group-set-info '"
9832                        (gnus-prin1-to-string (gnus-get-info to-group))
9833                        ")"))))
9834
9835           ;; Update the Xref header in this article to point to
9836           ;; the new crossposted article we have just created.
9837           (when (eq action 'crosspost)
9838             (with-current-buffer copy-buf
9839               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9840               (nnheader-replace-header "Xref" new-xref)
9841               (gnus-request-replace-article
9842                article gnus-newsgroup-name (current-buffer) t)))
9843
9844           ;; run the move/copy/crosspost/respool hook
9845           (run-hook-with-args 'gnus-summary-article-move-hook
9846                               action
9847                               (gnus-data-header
9848                                (assoc article (gnus-data-list nil)))
9849                               gnus-newsgroup-name
9850                               to-newsgroup
9851                               select-method))
9852
9853         ;;;!!!Why is this necessary?
9854         (set-buffer gnus-summary-buffer)
9855
9856         (when (eq action 'move)
9857           (save-excursion
9858             (gnus-summary-goto-subject article)
9859             (gnus-summary-mark-article article gnus-canceled-mark)))))
9860       (push article articles-to-update-marks))
9861
9862     (save-excursion
9863       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9864     ;; Re-activate all groups that have been moved to.
9865     (with-current-buffer gnus-group-buffer
9866       (let ((gnus-group-marked to-groups))
9867         (gnus-group-get-new-news-this-group nil t)))
9868
9869     (gnus-kill-buffer copy-buf)
9870     (gnus-summary-position-point)
9871     (gnus-set-mode-line 'summary)))
9872
9873 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9874   "Copy the current article to some other group.
9875 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9876 When called interactively, if TO-NEWSGROUP is nil, use the value of
9877 the variable `gnus-move-split-methods' for finding a default target
9878 newsgroup.
9879 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9880 re-spool using this method."
9881   (interactive "P")
9882   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9883
9884 (defun gnus-summary-crosspost-article (&optional n)
9885   "Crosspost the current article to some other group."
9886   (interactive "P")
9887   (gnus-summary-move-article n nil nil 'crosspost))
9888
9889 (defcustom gnus-summary-respool-default-method nil
9890   "Default method type for respooling an article.
9891 If nil, use to the current newsgroup method."
9892   :type 'symbol
9893   :group 'gnus-summary-mail)
9894
9895 (defun gnus-summary-respool-article (&optional n method)
9896   "Respool the current article.
9897 The article will be squeezed through the mail spooling process again,
9898 which means that it will be put in some mail newsgroup or other
9899 depending on `nnmail-split-methods'.
9900 If N is a positive number, respool the N next articles.
9901 If N is a negative number, respool the N previous articles.
9902 If N is nil and any articles have been marked with the process mark,
9903 respool those articles instead.
9904
9905 Respooling can be done both from mail groups and \"real\" newsgroups.
9906 In the former case, the articles in question will be moved from the
9907 current group into whatever groups they are destined to.  In the
9908 latter case, they will be copied into the relevant groups."
9909   (interactive
9910    (list current-prefix-arg
9911          (let* ((methods (gnus-methods-using 'respool))
9912                 (methname
9913                  (symbol-name (or gnus-summary-respool-default-method
9914                                   (car (gnus-find-method-for-group
9915                                         gnus-newsgroup-name)))))
9916                 (method
9917                  (gnus-completing-read-with-default
9918                   methname "Backend to use when respooling"
9919                   methods nil t nil 'gnus-mail-method-history))
9920                 ms)
9921            (cond
9922             ((zerop (length (setq ms (gnus-servers-using-backend
9923                                       (intern method)))))
9924              (list (intern method) ""))
9925             ((= 1 (length ms))
9926              (car ms))
9927             (t
9928              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9929                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9930                            ms-alist))))))))
9931   (unless method
9932     (error "No method given for respooling"))
9933   (if (assoc (symbol-name
9934               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9935              (gnus-methods-using 'respool))
9936       (gnus-summary-move-article n nil method)
9937     (gnus-summary-copy-article n nil method)))
9938
9939 (defun gnus-summary-import-article (file &optional edit)
9940   "Import an arbitrary file into a mail newsgroup."
9941   (interactive "fImport file: \nP")
9942   (let ((group gnus-newsgroup-name)
9943         (now (current-time))
9944         atts lines group-art)
9945     (unless (gnus-check-backend-function 'request-accept-article group)
9946       (error "%s does not support article importing" group))
9947     (or (file-readable-p file)
9948         (not (file-regular-p file))
9949         (error "Can't read %s" file))
9950     (with-current-buffer (gnus-get-buffer-create " *import file*")
9951       (erase-buffer)
9952       (nnheader-insert-file-contents file)
9953       (goto-char (point-min))
9954       (if (nnheader-article-p)
9955           (save-restriction
9956             (goto-char (point-min))
9957             (search-forward "\n\n" nil t)
9958             (narrow-to-region (point-min) (1- (point)))
9959             (goto-char (point-min))
9960             (unless (re-search-forward "^date:" nil t)
9961               (goto-char (point-max))
9962               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9963        ;; This doesn't look like an article, so we fudge some headers.
9964         (setq atts (file-attributes file)
9965               lines (count-lines (point-min) (point-max)))
9966         (insert "From: " (read-string "From: ") "\n"
9967                 "Subject: " (read-string "Subject: ") "\n"
9968                 "Date: " (message-make-date (nth 5 atts)) "\n"
9969                 "Message-ID: " (message-make-message-id) "\n"
9970                 "Lines: " (int-to-string lines) "\n"
9971                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9972       (setq group-art (gnus-request-accept-article group nil t))
9973       (kill-buffer (current-buffer)))
9974     (setq gnus-newsgroup-active (gnus-activate-group group))
9975     (forward-line 1)
9976     (gnus-summary-goto-article (cdr group-art) nil t)
9977     (when edit
9978       (gnus-summary-edit-article))))
9979
9980 (defun gnus-summary-create-article ()
9981   "Create an article in a mail newsgroup."
9982   (interactive)
9983   (let ((group gnus-newsgroup-name)
9984         (now (current-time))
9985         group-art)
9986     (unless (gnus-check-backend-function 'request-accept-article group)
9987       (error "%s does not support article importing" group))
9988     (with-current-buffer (gnus-get-buffer-create " *import file*")
9989       (erase-buffer)
9990       (goto-char (point-min))
9991       ;; This doesn't look like an article, so we fudge some headers.
9992       (insert "From: " (read-string "From: ") "\n"
9993               "Subject: " (read-string "Subject: ") "\n"
9994               "Date: " (message-make-date now) "\n"
9995               "Message-ID: " (message-make-message-id) "\n")
9996       (setq group-art (gnus-request-accept-article group nil t))
9997       (kill-buffer (current-buffer)))
9998     (setq gnus-newsgroup-active (gnus-activate-group group))
9999     (forward-line 1)
10000     (gnus-summary-goto-article (cdr group-art) nil t)
10001     (gnus-summary-edit-article)))
10002
10003 (defun gnus-summary-article-posted-p ()
10004   "Say whether the current (mail) article is available from news as well.
10005 This will be the case if the article has both been mailed and posted."
10006   (interactive)
10007   (let ((id (mail-header-references (gnus-summary-article-header)))
10008         (gnus-override-method (car (gnus-refer-article-methods))))
10009     (if (gnus-request-head id "")
10010         (gnus-message 2 "The current message was found on %s"
10011                       gnus-override-method)
10012       (gnus-message 2 "The current message couldn't be found on %s"
10013                     gnus-override-method)
10014       nil)))
10015
10016 (defun gnus-summary-expire-articles (&optional now)
10017   "Expire all articles that are marked as expirable in the current group."
10018   (interactive)
10019   (when (and (not gnus-group-is-exiting-without-update-p)
10020              (gnus-check-backend-function
10021               'request-expire-articles gnus-newsgroup-name))
10022     ;; This backend supports expiry.
10023     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10024            (expirable (if total
10025                           (progn
10026                             ;; We need to update the info for
10027                             ;; this group for `gnus-list-of-read-articles'
10028                             ;; to give us the right answer.
10029                             (gnus-run-hooks 'gnus-exit-group-hook)
10030                             (gnus-summary-update-info)
10031                             (gnus-list-of-read-articles gnus-newsgroup-name))
10032                         (setq gnus-newsgroup-expirable
10033                               (sort gnus-newsgroup-expirable '<))))
10034            (expiry-wait (if now 'immediate
10035                           (gnus-group-find-parameter
10036                            gnus-newsgroup-name 'expiry-wait)))
10037            (nnmail-expiry-target
10038             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10039                 nnmail-expiry-target))
10040            es)
10041       (when expirable
10042         ;; There are expirable articles in this group, so we run them
10043         ;; through the expiry process.
10044         (gnus-message 6 "Expiring articles...")
10045         (unless (gnus-check-group gnus-newsgroup-name)
10046           (error "Can't open server for %s" gnus-newsgroup-name))
10047         ;; The list of articles that weren't expired is returned.
10048         (save-excursion
10049           (if expiry-wait
10050               (let ((nnmail-expiry-wait-function nil)
10051                     (nnmail-expiry-wait expiry-wait))
10052                 (setq es (gnus-request-expire-articles
10053                           expirable gnus-newsgroup-name)))
10054             (setq es (gnus-request-expire-articles
10055                       expirable gnus-newsgroup-name)))
10056           (unless total
10057             (setq gnus-newsgroup-expirable es))
10058           ;; We go through the old list of expirable, and mark all
10059           ;; really expired articles as nonexistent.
10060           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10061             (let ((gnus-use-cache nil))
10062               (dolist (article expirable)
10063                 (when (and (not (memq article es))
10064                            (gnus-data-find article))
10065                   (gnus-summary-mark-article article gnus-canceled-mark)
10066                   (run-hook-with-args 'gnus-summary-article-expire-hook
10067                                       'delete
10068                                       (gnus-data-header
10069                                        (assoc article (gnus-data-list nil)))
10070                                       gnus-newsgroup-name
10071                                       nil
10072                                       nil))))))
10073         (gnus-message 6 "Expiring articles...done")))))
10074
10075 (defun gnus-summary-expire-articles-now ()
10076   "Expunge all expirable articles in the current group.
10077 This means that *all* articles that are marked as expirable will be
10078 deleted forever, right now."
10079   (interactive)
10080   (or gnus-expert-user
10081       (gnus-yes-or-no-p
10082        "Are you really, really sure you want to delete all expirable messages? ")
10083       (error "Phew!"))
10084   (gnus-summary-expire-articles t))
10085
10086 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10087 (defun gnus-summary-delete-article (&optional n)
10088   "Delete the N next (mail) articles.
10089 This command actually deletes articles.  This is not a marking
10090 command.  The article will disappear forever from your life, never to
10091 return.
10092
10093 If N is negative, delete backwards.
10094 If N is nil and articles have been marked with the process mark,
10095 delete these instead.
10096
10097 If `gnus-novice-user' is non-nil you will be asked for
10098 confirmation before the articles are deleted."
10099   (interactive "P")
10100   (unless (gnus-check-backend-function 'request-expire-articles
10101                                        gnus-newsgroup-name)
10102     (error "The current newsgroup does not support article deletion"))
10103   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10104     (error "Couldn't open server"))
10105   ;; Compute the list of articles to delete.
10106   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10107         (nnmail-expiry-target 'delete)
10108         not-deleted)
10109     (if (and gnus-novice-user
10110              (not (gnus-yes-or-no-p
10111                    (format "Do you really want to delete %s forever? "
10112                            (if (> (length articles) 1)
10113                                (format "these %s articles" (length articles))
10114                              "this article")))))
10115         ()
10116       ;; Delete the articles.
10117       (setq not-deleted (gnus-request-expire-articles
10118                          articles gnus-newsgroup-name 'force))
10119       (save-excursion
10120         (while articles
10121           (gnus-summary-remove-process-mark (car articles))
10122           ;; The backend might not have been able to delete the article
10123           ;; after all.
10124           (unless (memq (car articles) not-deleted)
10125             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10126           (let* ((article (car articles))
10127                  (ghead  (gnus-data-header
10128                           (assoc article (gnus-data-list nil)))))
10129             (run-hook-with-args 'gnus-summary-article-delete-hook
10130                                 'delete ghead gnus-newsgroup-name nil
10131                                 nil))
10132           (setq articles (cdr articles))))
10133       (when not-deleted
10134         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10135     (gnus-summary-position-point)
10136     (gnus-set-mode-line 'summary)
10137     not-deleted))
10138
10139 (defun gnus-summary-edit-article (&optional arg)
10140   "Edit the current article.
10141 This will have permanent effect only in mail groups.
10142 If ARG is nil, edit the decoded articles.
10143 If ARG is 1, edit the raw articles.
10144 If ARG is 2, edit the raw articles even in read-only groups.
10145 If ARG is 3, edit the articles with the current handles.
10146 Otherwise, allow editing of articles even in read-only
10147 groups."
10148   (interactive "P")
10149   (let (force raw current-handles)
10150     (cond
10151      ((null arg))
10152      ((eq arg 1)
10153       (setq raw t))
10154      ((eq arg 2)
10155       (setq raw t
10156             force t))
10157      ((eq arg 3)
10158       (setq current-handles
10159             (and (gnus-buffer-live-p gnus-article-buffer)
10160                  (with-current-buffer gnus-article-buffer
10161                    (prog1
10162                        gnus-article-mime-handles
10163                      (setq gnus-article-mime-handles nil))))))
10164      (t
10165       (setq force t)))
10166     (when (and raw (not force)
10167                (member gnus-newsgroup-name '("nndraft:delayed"
10168                                              "nndraft:drafts"
10169                                              "nndraft:queue")))
10170       (error "Can't edit the raw article in group %s"
10171              gnus-newsgroup-name))
10172     (with-current-buffer gnus-summary-buffer
10173       (let ((mail-parse-charset gnus-newsgroup-charset)
10174             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10175         (gnus-set-global-variables)
10176         (when (and (not force)
10177                    (gnus-group-read-only-p))
10178           (error "The current newsgroup does not support article editing"))
10179         (gnus-summary-show-article t)
10180         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10181           (with-current-buffer gnus-article-buffer
10182             (mm-enable-multibyte)))
10183         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10184             (setq raw t))
10185         (gnus-article-edit-article
10186          (if raw 'ignore
10187            `(lambda ()
10188               (let ((mbl mml-buffer-list))
10189                 (setq mml-buffer-list nil)
10190                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10191                   (mime-to-mml ,'current-handles))
10192                 (let ((mbl1 mml-buffer-list))
10193                   (setq mml-buffer-list mbl)
10194                   (set (make-local-variable 'mml-buffer-list) mbl1))
10195                 (gnus-make-local-hook 'kill-buffer-hook)
10196                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10197          `(lambda (no-highlight)
10198             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10199                   (message-options message-options)
10200                   (message-options-set-recipient)
10201                   (mail-parse-ignored-charsets
10202                    ',gnus-newsgroup-ignored-charsets)
10203                   (rfc2047-header-encoding-alist
10204                    ',(let ((charset (gnus-group-name-charset
10205                                      (gnus-find-method-for-group
10206                                       gnus-newsgroup-name)
10207                                      gnus-newsgroup-name)))
10208                        (append (list (cons "Newsgroups" charset)
10209                                      (cons "Followup-To" charset)
10210                                      (cons "Xref" charset))
10211                                rfc2047-header-encoding-alist))))
10212               ,(if (not raw) '(progn
10213                                 (mml-to-mime)
10214                                 (mml-destroy-buffers)
10215                                 (remove-hook 'kill-buffer-hook
10216                                              'mml-destroy-buffers t)
10217                                 (kill-local-variable 'mml-buffer-list)))
10218               (gnus-summary-edit-article-done
10219                ,(or (mail-header-references gnus-current-headers) "")
10220                ,(gnus-group-read-only-p)
10221                ,gnus-summary-buffer no-highlight))))))))
10222
10223 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10224
10225 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10226                                                  no-highlight)
10227   "Make edits to the current article permanent."
10228   (interactive)
10229   (save-excursion
10230    ;; The buffer restriction contains the entire article if it exists.
10231     (when (article-goto-body)
10232       (let ((lines (count-lines (point) (point-max)))
10233             (length (- (point-max) (point)))
10234             (case-fold-search t)
10235             (body (copy-marker (point))))
10236         (goto-char (point-min))
10237         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10238           (delete-region (match-beginning 1) (match-end 1))
10239           (insert (number-to-string length)))
10240         (goto-char (point-min))
10241         (when (re-search-forward
10242                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10243           (delete-region (match-beginning 1) (match-end 1))
10244           (insert (number-to-string length)))
10245         (goto-char (point-min))
10246         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10247           (delete-region (match-beginning 1) (match-end 1))
10248           (insert (number-to-string lines))))))
10249   ;; Replace the article.
10250   (let ((buf (current-buffer)))
10251     (with-temp-buffer
10252       (insert-buffer-substring buf)
10253
10254       (if (and (not read-only)
10255                (not (gnus-request-replace-article
10256                      (cdr gnus-article-current) (car gnus-article-current)
10257                      (current-buffer) t)))
10258           (error "Couldn't replace article")
10259         ;; Update the summary buffer.
10260         (if (and references
10261                  (equal (message-tokenize-header references " ")
10262                         (message-tokenize-header
10263                          (or (message-fetch-field "references") "") " ")))
10264             ;; We only have to update this line.
10265             (save-excursion
10266               (save-restriction
10267                 (message-narrow-to-head)
10268                 (let ((head (buffer-substring-no-properties
10269                              (point-min) (point-max)))
10270                       header)
10271                   (with-temp-buffer
10272                     (insert (format "211 %d Article retrieved.\n"
10273                                     (cdr gnus-article-current)))
10274                     (insert head)
10275                     (insert ".\n")
10276                     (let ((nntp-server-buffer (current-buffer)))
10277                       (setq header (car (gnus-get-newsgroup-headers
10278                                          nil t))))
10279                     (with-current-buffer gnus-summary-buffer
10280                       (gnus-data-set-header
10281                        (gnus-data-find (cdr gnus-article-current))
10282                        header)
10283                       (gnus-summary-update-article-line
10284                        (cdr gnus-article-current) header)
10285                       (if (gnus-summary-goto-subject
10286                            (cdr gnus-article-current) nil t)
10287                           (gnus-summary-update-secondary-mark
10288                            (cdr gnus-article-current))))))))
10289           ;; Update threads.
10290           (set-buffer (or buffer gnus-summary-buffer))
10291           (gnus-summary-update-article (cdr gnus-article-current))
10292           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10293               (gnus-summary-update-secondary-mark
10294                (cdr gnus-article-current))))
10295         ;; Prettify the article buffer again.
10296         (unless no-highlight
10297           (with-current-buffer gnus-article-buffer
10298             ;;;!!! Fix this -- article should be rehighlighted.
10299             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10300             (set-buffer gnus-original-article-buffer)
10301             (gnus-request-article
10302              (cdr gnus-article-current)
10303              (car gnus-article-current) (current-buffer))))
10304         ;; Prettify the summary buffer line.
10305         (when (gnus-visual-p 'summary-highlight 'highlight)
10306           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10307
10308 (defun gnus-summary-edit-wash (key)
10309   "Perform editing command KEY in the article buffer."
10310   (interactive
10311    (list
10312     (progn
10313       (message "%s" (concat (this-command-keys) "- "))
10314       (read-char))))
10315   (message "")
10316   (gnus-summary-edit-article)
10317   (execute-kbd-macro (concat (this-command-keys) key))
10318   (gnus-article-edit-done))
10319
10320 ;;; Respooling
10321
10322 (defun gnus-summary-respool-query (&optional silent trace)
10323   "Query where the respool algorithm would put this article."
10324   (interactive)
10325   (let (gnus-mark-article-hook)
10326     (gnus-summary-select-article)
10327     (with-current-buffer gnus-original-article-buffer
10328       (let ((groups (nnmail-article-group 'identity trace)))
10329         (unless silent
10330           (if groups
10331               (message "This message would go to %s"
10332                        (mapconcat 'car groups ", "))
10333             (message "This message would go to no groups"))
10334           groups)))))
10335
10336 (defun gnus-summary-respool-trace ()
10337   "Trace where the respool algorithm would put this article.
10338 Display a buffer showing all fancy splitting patterns which matched."
10339   (interactive)
10340   (gnus-summary-respool-query nil t))
10341
10342 ;; Summary marking commands.
10343
10344 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10345   "Mark articles which has the same subject as read, and then select the next.
10346 If UNMARK is positive, remove any kind of mark.
10347 If UNMARK is negative, tick articles."
10348   (interactive "P")
10349   (when unmark
10350     (setq unmark (prefix-numeric-value unmark)))
10351   (let ((count
10352          (gnus-summary-mark-same-subject
10353           (gnus-summary-article-subject) unmark)))
10354     ;; Select next unread article.  If auto-select-same mode, should
10355     ;; select the first unread article.
10356     (gnus-summary-next-article t (and gnus-auto-select-same
10357                                       (gnus-summary-article-subject)))
10358     (gnus-message 7 "%d article%s marked as %s"
10359                   count (if (= count 1) " is" "s are")
10360                   (if unmark "unread" "read"))))
10361
10362 (defun gnus-summary-kill-same-subject (&optional unmark)
10363   "Mark articles which has the same subject as read.
10364 If UNMARK is positive, remove any kind of mark.
10365 If UNMARK is negative, tick articles."
10366   (interactive "P")
10367   (when unmark
10368     (setq unmark (prefix-numeric-value unmark)))
10369   (let ((count
10370          (gnus-summary-mark-same-subject
10371           (gnus-summary-article-subject) unmark)))
10372     ;; If marked as read, go to next unread subject.
10373     (when (null unmark)
10374       ;; Go to next unread subject.
10375       (gnus-summary-next-subject 1 t))
10376     (gnus-message 7 "%d articles are marked as %s"
10377                   count (if unmark "unread" "read"))))
10378
10379 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10380   "Mark articles with same SUBJECT as read, and return marked number.
10381 If optional argument UNMARK is positive, remove any kinds of marks.
10382 If optional argument UNMARK is negative, mark articles as unread instead."
10383   (let ((count 1))
10384     (save-excursion
10385       (cond
10386        ((null unmark)                   ; Mark as read.
10387         (while (and
10388                 (progn
10389                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10390                   (gnus-summary-show-thread) t)
10391                 (gnus-summary-find-subject subject))
10392           (setq count (1+ count))))
10393        ((> unmark 0)                    ; Tick.
10394         (while (and
10395                 (progn
10396                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10397                   (gnus-summary-show-thread) t)
10398                 (gnus-summary-find-subject subject))
10399           (setq count (1+ count))))
10400        (t                               ; Mark as unread.
10401         (while (and
10402                 (progn
10403                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10404                   (gnus-summary-show-thread) t)
10405                 (gnus-summary-find-subject subject))
10406           (setq count (1+ count)))))
10407       (gnus-set-mode-line 'summary)
10408       ;; Return the number of marked articles.
10409       count)))
10410
10411 (defun gnus-summary-mark-as-processable (n &optional unmark)
10412   "Set the process mark on the next N articles.
10413 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10414 the process mark instead.  The difference between N and the actual
10415 number of articles marked is returned."
10416   (interactive "P")
10417   (if (and (null n) (gnus-region-active-p))
10418       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10419     (setq n (prefix-numeric-value n))
10420     (let ((backward (< n 0))
10421           (n (abs n)))
10422       (while (and
10423               (> n 0)
10424               (if unmark
10425                   (gnus-summary-remove-process-mark
10426                    (gnus-summary-article-number))
10427                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10428               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10429         (setq n (1- n)))
10430       (when (/= 0 n)
10431         (gnus-message 7 "No more articles"))
10432       (gnus-summary-recenter)
10433       (gnus-summary-position-point)
10434       n)))
10435
10436 (defun gnus-summary-unmark-as-processable (n)
10437   "Remove the process mark from the next N articles.
10438 If N is negative, unmark backward instead.  The difference between N and
10439 the actual number of articles unmarked is returned."
10440   (interactive "P")
10441   (gnus-summary-mark-as-processable n t))
10442
10443 (defun gnus-summary-unmark-all-processable ()
10444   "Remove the process mark from all articles."
10445   (interactive)
10446   (save-excursion
10447     (while gnus-newsgroup-processable
10448       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10449   (gnus-summary-position-point))
10450
10451 (defun gnus-summary-add-mark (article type)
10452   "Mark ARTICLE with a mark of TYPE."
10453   (let ((vtype (car (assq type gnus-article-mark-lists)))
10454         var)
10455     (if (not vtype)
10456         (error "No such mark type: %s" type)
10457       (setq var (intern (format "gnus-newsgroup-%s" type)))
10458       (set var (cons article (symbol-value var)))
10459       (if (memq type '(processable cached replied forwarded recent saved))
10460           (gnus-summary-update-secondary-mark article)
10461         ;;; !!! This is bogus.  We should find out what primary
10462         ;;; !!! mark we want to set.
10463         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10464
10465 (defun gnus-summary-mark-as-expirable (n)
10466   "Mark N articles forward as expirable.
10467 If N is negative, mark backward instead.  The difference between N and
10468 the actual number of articles marked is returned."
10469   (interactive "p")
10470   (gnus-summary-mark-forward n gnus-expirable-mark))
10471
10472 (defun gnus-summary-mark-as-spam (n)
10473   "Mark N articles forward as spam.
10474 If N is negative, mark backward instead.  The difference between N and
10475 the actual number of articles marked is returned."
10476   (interactive "p")
10477   (gnus-summary-mark-forward n gnus-spam-mark))
10478
10479 (defun gnus-summary-mark-article-as-replied (article)
10480   "Mark ARTICLE as replied to and update the summary line.
10481 ARTICLE can also be a list of articles."
10482   (interactive (list (gnus-summary-article-number)))
10483   (let ((articles (if (listp article) article (list article))))
10484     (dolist (article articles)
10485       (unless (numberp article)
10486         (error "%s is not a number" article))
10487       (push article gnus-newsgroup-replied)
10488       (let ((inhibit-read-only t))
10489         (when (gnus-summary-goto-subject article nil t)
10490           (gnus-summary-update-secondary-mark article))))))
10491
10492 (defun gnus-summary-mark-article-as-forwarded (article)
10493   "Mark ARTICLE as forwarded and update the summary line.
10494 ARTICLE can also be a list of articles."
10495   (let ((articles (if (listp article) article (list article))))
10496     (dolist (article articles)
10497       (push article gnus-newsgroup-forwarded)
10498       (let ((inhibit-read-only t))
10499         (when (gnus-summary-goto-subject article nil t)
10500           (gnus-summary-update-secondary-mark article))))))
10501
10502 (defun gnus-summary-set-bookmark (article)
10503   "Set a bookmark in current article."
10504   (interactive (list (gnus-summary-article-number)))
10505   (when (or (not (get-buffer gnus-article-buffer))
10506             (not gnus-current-article)
10507             (not gnus-article-current)
10508             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10509     (error "No current article selected"))
10510   ;; Remove old bookmark, if one exists.
10511   (gnus-pull article gnus-newsgroup-bookmarks)
10512   ;; Set the new bookmark, which is on the form
10513   ;; (article-number . line-number-in-body).
10514   (push
10515    (cons article
10516          (with-current-buffer gnus-article-buffer
10517            (count-lines
10518             (min (point)
10519                  (save-excursion
10520                    (article-goto-body)
10521                    (point)))
10522             (point))))
10523    gnus-newsgroup-bookmarks)
10524   (gnus-message 6 "A bookmark has been added to the current article."))
10525
10526 (defun gnus-summary-remove-bookmark (article)
10527   "Remove the bookmark from the current article."
10528   (interactive (list (gnus-summary-article-number)))
10529   ;; Remove old bookmark, if one exists.
10530   (if (not (assq article gnus-newsgroup-bookmarks))
10531       (gnus-message 6 "No bookmark in current article.")
10532     (gnus-pull article gnus-newsgroup-bookmarks)
10533     (gnus-message 6 "Removed bookmark.")))
10534
10535 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10536 (defun gnus-summary-mark-as-dormant (n)
10537   "Mark N articles forward as dormant.
10538 If N is negative, mark backward instead.  The difference between N and
10539 the actual number of articles marked is returned."
10540   (interactive "p")
10541   (gnus-summary-mark-forward n gnus-dormant-mark))
10542
10543 (defun gnus-summary-set-process-mark (article)
10544   "Set the process mark on ARTICLE and update the summary line."
10545   (setq gnus-newsgroup-processable
10546         (cons article
10547               (delq article gnus-newsgroup-processable)))
10548   (when (gnus-summary-goto-subject article)
10549     (gnus-summary-show-thread)
10550     (gnus-summary-goto-subject article)
10551     (gnus-summary-update-secondary-mark article)))
10552
10553 (defun gnus-summary-remove-process-mark (&rest articles)
10554   "Remove the process mark from ARTICLES and update the summary line."
10555   (dolist (article articles)
10556     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10557     (when (gnus-summary-goto-subject article)
10558       (gnus-summary-show-thread)
10559       (gnus-summary-goto-subject article)
10560       (gnus-summary-update-secondary-mark article)))
10561   t)
10562
10563 (defun gnus-summary-set-saved-mark (article)
10564   "Set the process mark on ARTICLE and update the summary line."
10565   (push article gnus-newsgroup-saved)
10566   (when (gnus-summary-goto-subject article)
10567     (gnus-summary-update-secondary-mark article)))
10568
10569 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10570   "Mark N articles as read forwards.
10571 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10572 The difference between N and the actual number of articles marked is
10573 returned.
10574 If NO-EXPIRE, auto-expiry will be inhibited."
10575   (interactive "p")
10576   (gnus-summary-show-thread)
10577   (let ((backward (< n 0))
10578         (gnus-summary-goto-unread
10579          (and gnus-summary-goto-unread
10580               (not (eq gnus-summary-goto-unread 'never))
10581               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10582                                     gnus-ticked-mark gnus-dormant-mark)))))
10583         (n (abs n))
10584         (mark (or mark gnus-del-mark)))
10585     (while (and (> n 0)
10586                 (gnus-summary-mark-article nil mark no-expire)
10587                 (zerop (gnus-summary-next-subject
10588                         (if backward -1 1)
10589                         (and gnus-summary-goto-unread
10590                              (not (eq gnus-summary-goto-unread 'never)))
10591                         t)))
10592       (setq n (1- n)))
10593     (when (/= 0 n)
10594       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10595     (gnus-summary-recenter)
10596     (gnus-summary-position-point)
10597     (gnus-set-mode-line 'summary)
10598     n))
10599
10600 (defun gnus-summary-mark-article-as-read (mark)
10601   "Mark the current article quickly as read with MARK."
10602   (let ((article (gnus-summary-article-number)))
10603     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10604     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10605     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10606     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10607     (push (cons article mark) gnus-newsgroup-reads)
10608     ;; Possibly remove from cache, if that is used.
10609     (when gnus-use-cache
10610       (gnus-cache-enter-remove-article article))
10611     ;; Allow the backend to change the mark.
10612     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10613     ;; Check for auto-expiry.
10614     (when (and gnus-newsgroup-auto-expire
10615                (memq mark gnus-auto-expirable-marks))
10616       (setq mark gnus-expirable-mark)
10617       ;; Let the backend know about the mark change.
10618       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10619       (push article gnus-newsgroup-expirable))
10620     ;; Set the mark in the buffer.
10621     (gnus-summary-update-mark mark 'unread)
10622     t))
10623
10624 (defun gnus-summary-mark-article-as-unread (mark)
10625   "Mark the current article quickly as unread with MARK."
10626   (let* ((article (gnus-summary-article-number))
10627          (old-mark (gnus-summary-article-mark article)))
10628     ;; Allow the backend to change the mark.
10629     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10630     (if (eq mark old-mark)
10631         t
10632       (if (<= article 0)
10633           (progn
10634             (gnus-error 1 "Can't mark negative article numbers")
10635             nil)
10636         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10637         (setq gnus-newsgroup-spam-marked
10638               (delq article gnus-newsgroup-spam-marked))
10639         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10640         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10641         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10642         (cond ((= mark gnus-ticked-mark)
10643                (setq gnus-newsgroup-marked
10644                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10645                                               article)))
10646               ((= mark gnus-spam-mark)
10647                (setq gnus-newsgroup-spam-marked
10648                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10649                                               article)))
10650               ((= mark gnus-dormant-mark)
10651                (setq gnus-newsgroup-dormant
10652                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10653                                               article)))
10654               (t
10655                (setq gnus-newsgroup-unreads
10656                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10657                                               article))))
10658         (gnus-pull article gnus-newsgroup-reads)
10659
10660         ;; See whether the article is to be put in the cache.
10661         (and gnus-use-cache
10662              (vectorp (gnus-summary-article-header article))
10663              (save-excursion
10664                (gnus-cache-possibly-enter-article
10665                 gnus-newsgroup-name article
10666                 (= mark gnus-ticked-mark)
10667                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10668
10669         ;; Fix the mark.
10670         (gnus-summary-update-mark mark 'unread)
10671         t))))
10672
10673 (defun gnus-summary-mark-article (&optional article mark no-expire)
10674   "Mark ARTICLE with MARK.  MARK can be any character.
10675 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10676 `??' (dormant) and `?E' (expirable).
10677 If MARK is nil, then the default character `?r' is used.
10678 If ARTICLE is nil, then the article on the current line will be
10679 marked.
10680 If NO-EXPIRE, auto-expiry will be inhibited."
10681   ;; The mark might be a string.
10682   (when (stringp mark)
10683     (setq mark (aref mark 0)))
10684   ;; If no mark is given, then we check auto-expiring.
10685   (when (null mark)
10686     (setq mark gnus-del-mark))
10687   (when (and (not no-expire)
10688              gnus-newsgroup-auto-expire
10689              (memq mark gnus-auto-expirable-marks))
10690     (setq mark gnus-expirable-mark))
10691   (let ((article (or article (gnus-summary-article-number)))
10692         (old-mark (gnus-summary-article-mark article)))
10693     ;; Allow the backend to change the mark.
10694     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10695     (if (eq mark old-mark)
10696         t
10697       (unless article
10698         (error "No article on current line"))
10699       (if (not (if (or (= mark gnus-unread-mark)
10700                        (= mark gnus-ticked-mark)
10701                        (= mark gnus-spam-mark)
10702                        (= mark gnus-dormant-mark))
10703                    (gnus-mark-article-as-unread article mark)
10704                  (gnus-mark-article-as-read article mark)))
10705           t
10706         ;; See whether the article is to be put in the cache.
10707         (and gnus-use-cache
10708              (not (= mark gnus-canceled-mark))
10709              (vectorp (gnus-summary-article-header article))
10710              (save-excursion
10711                (gnus-cache-possibly-enter-article
10712                 gnus-newsgroup-name article
10713                 (= mark gnus-ticked-mark)
10714                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10715
10716         (when (gnus-summary-goto-subject article nil t)
10717           (let ((inhibit-read-only t))
10718             (gnus-summary-show-thread)
10719             ;; Fix the mark.
10720             (gnus-summary-update-mark mark 'unread)
10721             t))))))
10722
10723 (defun gnus-summary-update-secondary-mark (article)
10724   "Update the secondary (read, process, cache) mark."
10725   (gnus-summary-update-mark
10726    (cond ((memq article gnus-newsgroup-processable)
10727           gnus-process-mark)
10728          ((memq article gnus-newsgroup-cached)
10729           gnus-cached-mark)
10730          ((memq article gnus-newsgroup-replied)
10731           gnus-replied-mark)
10732          ((memq article gnus-newsgroup-forwarded)
10733           gnus-forwarded-mark)
10734          ((memq article gnus-newsgroup-saved)
10735           gnus-saved-mark)
10736          ((memq article gnus-newsgroup-recent)
10737           gnus-recent-mark)
10738          ((memq article gnus-newsgroup-unseen)
10739           gnus-unseen-mark)
10740          (t gnus-no-mark))
10741    'replied)
10742   (when (gnus-visual-p 'summary-highlight 'highlight)
10743     (gnus-run-hooks 'gnus-summary-update-hook))
10744   t)
10745
10746 (defun gnus-summary-update-download-mark (article)
10747   "Update the download mark."
10748   (gnus-summary-update-mark
10749    (cond ((memq article gnus-newsgroup-undownloaded)
10750           gnus-undownloaded-mark)
10751          (gnus-newsgroup-agentized
10752           gnus-downloaded-mark)
10753          (t
10754           gnus-no-mark))
10755    'download)
10756   (gnus-summary-update-line t)
10757   t)
10758
10759 (defun gnus-summary-update-mark (mark type)
10760   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10761         (inhibit-read-only t))
10762     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10763     (when forward
10764       (when (looking-at "\r")
10765         (incf forward))
10766       (when (<= (+ forward (point)) (point-max))
10767         ;; Go to the right position on the line.
10768         (goto-char (+ forward (point)))
10769         ;; Replace the old mark with the new mark.
10770         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10771         ;; Optionally update the marks by some user rule.
10772         (when (eq type 'unread)
10773           (gnus-data-set-mark
10774            (gnus-data-find (gnus-summary-article-number)) mark)
10775           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10776
10777 (defun gnus-mark-article-as-read (article &optional mark)
10778   "Enter ARTICLE in the pertinent lists and remove it from others."
10779   ;; Make the article expirable.
10780   (let ((mark (or mark gnus-del-mark)))
10781     (setq gnus-newsgroup-expirable
10782           (if (= mark gnus-expirable-mark)
10783               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10784             (delq article gnus-newsgroup-expirable)))
10785     ;; Remove from unread and marked lists.
10786     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10787     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10788     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10789     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10790     (push (cons article mark) gnus-newsgroup-reads)
10791     ;; Possibly remove from cache, if that is used.
10792     (when gnus-use-cache
10793       (gnus-cache-enter-remove-article article))
10794     t))
10795
10796 (defun gnus-mark-article-as-unread (article &optional mark)
10797   "Enter ARTICLE in the pertinent lists and remove it from others."
10798   (let ((mark (or mark gnus-ticked-mark)))
10799     (if (<= article 0)
10800         (progn
10801           (gnus-error 1 "Can't mark negative article numbers")
10802           nil)
10803       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10804             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10805             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10806             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10807             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10808
10809       ;; Unsuppress duplicates?
10810       (when gnus-suppress-duplicates
10811         (gnus-dup-unsuppress-article article))
10812
10813       (cond ((= mark gnus-ticked-mark)
10814              (setq gnus-newsgroup-marked
10815                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10816             ((= mark gnus-spam-mark)
10817              (setq gnus-newsgroup-spam-marked
10818                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10819                                             article)))
10820             ((= mark gnus-dormant-mark)
10821              (setq gnus-newsgroup-dormant
10822                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10823             (t
10824              (setq gnus-newsgroup-unreads
10825                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10826       (gnus-pull article gnus-newsgroup-reads)
10827       t)))
10828
10829 (defalias 'gnus-summary-mark-as-unread-forward
10830   'gnus-summary-tick-article-forward)
10831 (make-obsolete 'gnus-summary-mark-as-unread-forward
10832                'gnus-summary-tick-article-forward "Emacs 20.4")
10833 (defun gnus-summary-tick-article-forward (n)
10834   "Tick N articles forwards.
10835 If N is negative, tick backwards instead.
10836 The difference between N and the number of articles ticked is returned."
10837   (interactive "p")
10838   (gnus-summary-mark-forward n gnus-ticked-mark))
10839
10840 (defalias 'gnus-summary-mark-as-unread-backward
10841   'gnus-summary-tick-article-backward)
10842 (make-obsolete 'gnus-summary-mark-as-unread-backward
10843                'gnus-summary-tick-article-backward "Emacs 20.4")
10844 (defun gnus-summary-tick-article-backward (n)
10845   "Tick N articles backwards.
10846 The difference between N and the number of articles ticked is returned."
10847   (interactive "p")
10848   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10849
10850 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10851 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10852 (defun gnus-summary-tick-article (&optional article clear-mark)
10853   "Mark current article as unread.
10854 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10855 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10856   (interactive)
10857   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10858                                        gnus-ticked-mark)))
10859
10860 (defun gnus-summary-mark-as-read-forward (n)
10861   "Mark N articles as read forwards.
10862 If N is negative, mark backwards instead.
10863 The difference between N and the actual number of articles marked is
10864 returned."
10865   (interactive "p")
10866   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10867
10868 (defun gnus-summary-mark-as-read-backward (n)
10869   "Mark the N articles as read backwards.
10870 The difference between N and the actual number of articles marked is
10871 returned."
10872   (interactive "p")
10873   (gnus-summary-mark-forward
10874    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10875
10876 (defun gnus-summary-mark-as-read (&optional article mark)
10877   "Mark current article as read.
10878 ARTICLE specifies the article to be marked as read.
10879 MARK specifies a string to be inserted at the beginning of the line."
10880   (gnus-summary-mark-article article mark))
10881
10882 (defun gnus-summary-clear-mark-forward (n)
10883   "Clear marks from N articles forward.
10884 If N is negative, clear backward instead.
10885 The difference between N and the number of marks cleared is returned."
10886   (interactive "p")
10887   (gnus-summary-mark-forward n gnus-unread-mark))
10888
10889 (defun gnus-summary-clear-mark-backward (n)
10890   "Clear marks from N articles backward.
10891 The difference between N and the number of marks cleared is returned."
10892   (interactive "p")
10893   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10894
10895 (defun gnus-summary-mark-unread-as-read ()
10896   "Intended to be used by `gnus-mark-article-hook'."
10897   (when (memq gnus-current-article gnus-newsgroup-unreads)
10898     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10899
10900 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10901   "Intended to be used by `gnus-mark-article-hook'."
10902   (let ((mark (gnus-summary-article-mark)))
10903     (when (or (gnus-unread-mark-p mark)
10904               (gnus-read-mark-p mark))
10905       (gnus-summary-mark-article gnus-current-article
10906                                  (or new-mark gnus-read-mark)))))
10907
10908 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10909   "Intended to be used by `gnus-mark-article-hook'."
10910   (let ((mark (gnus-summary-article-mark)))
10911     (when (or (gnus-unread-mark-p mark)
10912               (gnus-read-mark-p mark))
10913       (gnus-summary-mark-article (gnus-summary-article-number)
10914                                  (or new-mark gnus-read-mark)))))
10915
10916 (defun gnus-summary-mark-unread-as-ticked ()
10917   "Intended to be used by `gnus-mark-article-hook'."
10918   (when (memq gnus-current-article gnus-newsgroup-unreads)
10919     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10920
10921 (defun gnus-summary-mark-region-as-read (point mark all)
10922   "Mark all unread articles between point and mark as read.
10923 If given a prefix, mark all articles between point and mark as read,
10924 even ticked and dormant ones."
10925   (interactive "r\nP")
10926   (save-excursion
10927     (let (article)
10928       (goto-char point)
10929       (beginning-of-line)
10930       (while (and
10931               (< (point) mark)
10932               (progn
10933                 (when (or all
10934                           (memq (setq article (gnus-summary-article-number))
10935                                 gnus-newsgroup-unreads))
10936                   (gnus-summary-mark-article article gnus-del-mark))
10937                 t)
10938               (gnus-summary-find-next))))))
10939
10940 (defun gnus-summary-mark-below (score mark)
10941   "Mark articles with score less than SCORE with MARK."
10942   (interactive "P\ncMark: ")
10943   (setq score (if score
10944                   (prefix-numeric-value score)
10945                 (or gnus-summary-default-score 0)))
10946   (with-current-buffer gnus-summary-buffer
10947     (goto-char (point-min))
10948     (while
10949         (progn
10950           (and (< (gnus-summary-article-score) score)
10951                (gnus-summary-mark-article nil mark))
10952           (gnus-summary-find-next)))))
10953
10954 (defun gnus-summary-kill-below (&optional score)
10955   "Mark articles with score below SCORE as read."
10956   (interactive "P")
10957   (gnus-summary-mark-below score gnus-killed-mark))
10958
10959 (defun gnus-summary-clear-above (&optional score)
10960   "Clear all marks from articles with score above SCORE."
10961   (interactive "P")
10962   (gnus-summary-mark-above score gnus-unread-mark))
10963
10964 (defun gnus-summary-tick-above (&optional score)
10965   "Tick all articles with score above SCORE."
10966   (interactive "P")
10967   (gnus-summary-mark-above score gnus-ticked-mark))
10968
10969 (defun gnus-summary-mark-above (score mark)
10970   "Mark articles with score over SCORE with MARK."
10971   (interactive "P\ncMark: ")
10972   (setq score (if score
10973                   (prefix-numeric-value score)
10974                 (or gnus-summary-default-score 0)))
10975   (with-current-buffer gnus-summary-buffer
10976     (goto-char (point-min))
10977     (while (and (progn
10978                   (when (> (gnus-summary-article-score) score)
10979                     (gnus-summary-mark-article nil mark))
10980                   t)
10981                 (gnus-summary-find-next)))))
10982
10983 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10984 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10985 (defun gnus-summary-limit-include-expunged (&optional no-error)
10986   "Display all the hidden articles that were expunged for low scores."
10987   (interactive)
10988   (let ((inhibit-read-only t))
10989     (let ((scored gnus-newsgroup-scored)
10990           headers h)
10991       (while scored
10992         (unless (gnus-summary-article-header (caar scored))
10993           (and (setq h (gnus-number-to-header (caar scored)))
10994                (< (cdar scored) gnus-summary-expunge-below)
10995                (push h headers)))
10996         (setq scored (cdr scored)))
10997       (if (not headers)
10998           (when (not no-error)
10999             (error "No expunged articles hidden"))
11000         (goto-char (point-min))
11001         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11002         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11003         (dolist (x headers)
11004           (push (mail-header-number x) gnus-newsgroup-limit))
11005         (gnus-summary-prepare-unthreaded (nreverse headers))
11006         (goto-char (point-min))
11007         (gnus-summary-position-point)
11008         t))))
11009
11010 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11011   "Mark all unread articles in this newsgroup as read.
11012 If prefix argument ALL is non-nil, ticked and dormant articles will
11013 also be marked as read.
11014 If QUIETLY is non-nil, no questions will be asked.
11015
11016 If TO-HERE is non-nil, it should be a point in the buffer.  All
11017 articles before (after, if REVERSE is set) this point will be marked
11018 as read.
11019
11020 Note that this function will only catch up the unread article
11021 in the current summary buffer limitation.
11022
11023 The number of articles marked as read is returned."
11024   (interactive "P")
11025   (prog1
11026       (save-excursion
11027         (when (or quietly
11028                   (not gnus-interactive-catchup) ;Without confirmation?
11029                   gnus-expert-user
11030                   (gnus-y-or-n-p
11031                    (if all
11032                        "Mark absolutely all articles as read? "
11033                      "Mark all unread articles as read? ")))
11034           (if (and not-mark
11035                    (not gnus-newsgroup-adaptive)
11036                    (not gnus-newsgroup-auto-expire)
11037                    (not gnus-suppress-duplicates)
11038                    (or (not gnus-use-cache)
11039                        (eq gnus-use-cache 'passive)))
11040               (progn
11041                 (when all
11042                   (setq gnus-newsgroup-marked nil
11043                         gnus-newsgroup-spam-marked nil
11044                         gnus-newsgroup-dormant nil))
11045                 (setq gnus-newsgroup-unreads
11046                       (gnus-sorted-nunion
11047                        (gnus-sorted-intersection gnus-newsgroup-unreads
11048                                                  gnus-newsgroup-downloadable)
11049                        (gnus-sorted-difference gnus-newsgroup-unfetched
11050                                                gnus-newsgroup-cached))))
11051             ;; We actually mark all articles as canceled, which we
11052             ;; have to do when using auto-expiry or adaptive scoring.
11053             (gnus-summary-show-all-threads)
11054             (if (and to-here reverse)
11055                 (progn
11056                   (goto-char to-here)
11057                   (gnus-summary-mark-current-read-and-unread-as-read
11058                    gnus-catchup-mark)
11059                   (while (gnus-summary-find-next (not all))
11060                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11061               (when (gnus-summary-first-subject (not all))
11062                 (while (and
11063                         (if to-here (< (point) to-here) t)
11064                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11065                         (gnus-summary-find-next (not all))))))
11066             (gnus-set-mode-line 'summary))
11067           t))
11068     (gnus-summary-position-point)))
11069
11070 (defun gnus-summary-catchup-to-here (&optional all)
11071   "Mark all unticked articles before the current one as read.
11072 If ALL is non-nil, also mark ticked and dormant articles as read."
11073   (interactive "P")
11074   (save-excursion
11075     (gnus-save-hidden-threads
11076       (let ((beg (point)))
11077         ;; We check that there are unread articles.
11078         (when (or all (gnus-summary-find-prev))
11079           (gnus-summary-catchup all t beg)))))
11080   (gnus-summary-position-point))
11081
11082 (defun gnus-summary-catchup-from-here (&optional all)
11083   "Mark all unticked articles after (and including) the current one as read.
11084 If ALL is non-nil, also mark ticked and dormant articles as read."
11085   (interactive "P")
11086   (save-excursion
11087     (gnus-save-hidden-threads
11088       (let ((beg (point)))
11089         ;; We check that there are unread articles.
11090         (when (or all (gnus-summary-find-next))
11091           (gnus-summary-catchup all t beg nil t)))))
11092   (gnus-summary-position-point))
11093
11094 (defun gnus-summary-catchup-all (&optional quietly)
11095   "Mark all articles in this newsgroup as read.
11096 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11097 instead, which marks only unread articles as read."
11098   (interactive "P")
11099   (gnus-summary-catchup t quietly))
11100
11101 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11102   "Mark all unread articles in this group as read, then exit.
11103 If prefix argument ALL is non-nil, all articles are marked as read.
11104 If QUIETLY is non-nil, no questions will be asked."
11105   (interactive "P")
11106   (when (gnus-summary-catchup all quietly nil 'fast)
11107     ;; Select next newsgroup or exit.
11108     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11109              (eq gnus-auto-select-next 'quietly))
11110         (gnus-summary-next-group nil)
11111       (gnus-summary-exit))))
11112
11113 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11114   "Mark all articles in this newsgroup as read, and then exit.
11115 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11116 instead, which marks only unread articles as read."
11117   (interactive "P")
11118   (gnus-summary-catchup-and-exit t quietly))
11119
11120 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11121   "Mark all articles in this group as read and select the next group.
11122 If given a prefix, mark all articles, unread as well as ticked, as
11123 read."
11124   (interactive "P")
11125   (save-excursion
11126     (gnus-summary-catchup all))
11127   (gnus-summary-next-group))
11128
11129 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11130   "Mark all articles in this group as read and select the previous group.
11131 If given a prefix, mark all articles, unread as well as ticked, as
11132 read."
11133   (interactive "P")
11134   (save-excursion
11135     (gnus-summary-catchup all))
11136   (gnus-summary-next-group nil nil t))
11137
11138 ;;;
11139 ;;; with article
11140 ;;;
11141
11142 (defmacro gnus-with-article (article &rest forms)
11143   "Select ARTICLE and perform FORMS in the original article buffer.
11144 Then replace the article with the result."
11145   `(progn
11146      ;; We don't want the article to be marked as read.
11147      (let (gnus-mark-article-hook)
11148        (gnus-summary-select-article t t nil ,article))
11149      (set-buffer gnus-original-article-buffer)
11150      ,@forms
11151      (if (not (gnus-check-backend-function
11152                'request-replace-article (car gnus-article-current)))
11153          (gnus-message 5 "Read-only group; not replacing")
11154        (unless (gnus-request-replace-article
11155                 ,article (car gnus-article-current)
11156                 (current-buffer) t)
11157          (error "Couldn't replace article")))
11158      ;; The cache and backlog have to be flushed somewhat.
11159      (when gnus-keep-backlog
11160        (gnus-backlog-remove-article
11161         (car gnus-article-current) (cdr gnus-article-current)))
11162      (when gnus-use-cache
11163        (gnus-cache-update-article
11164         (car gnus-article-current) (cdr gnus-article-current)))))
11165
11166 (put 'gnus-with-article 'lisp-indent-function 1)
11167 (put 'gnus-with-article 'edebug-form-spec '(form body))
11168
11169 ;; Thread-based commands.
11170
11171 (defun gnus-summary-articles-in-thread (&optional article)
11172   "Return a list of all articles in the current thread.
11173 If ARTICLE is non-nil, return all articles in the thread that starts
11174 with that article."
11175   (let* ((article (or article (gnus-summary-article-number)))
11176          (data (gnus-data-find-list article))
11177          (top-level (gnus-data-level (car data)))
11178          (top-subject
11179           (cond ((null gnus-thread-operation-ignore-subject)
11180                  (gnus-simplify-subject-re
11181                   (mail-header-subject (gnus-data-header (car data)))))
11182                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11183                  (gnus-simplify-subject-fuzzy
11184                   (mail-header-subject (gnus-data-header (car data)))))
11185                 (t nil)))
11186          (end-point (save-excursion
11187                       (if (gnus-summary-go-to-next-thread)
11188                           (point) (point-max))))
11189          articles)
11190     (while (and data
11191                 (< (gnus-data-pos (car data)) end-point))
11192       (when (or (not top-subject)
11193                 (string= top-subject
11194                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11195                              (gnus-simplify-subject-fuzzy
11196                               (mail-header-subject
11197                                (gnus-data-header (car data))))
11198                            (gnus-simplify-subject-re
11199                             (mail-header-subject
11200                              (gnus-data-header (car data)))))))
11201         (push (gnus-data-number (car data)) articles))
11202       (unless (and (setq data (cdr data))
11203                    (> (gnus-data-level (car data)) top-level))
11204         (setq data nil)))
11205     ;; Return the list of articles.
11206     (nreverse articles)))
11207
11208 (defun gnus-summary-rethread-current ()
11209   "Rethread the thread the current article is part of."
11210   (interactive)
11211   (let* ((gnus-show-threads t)
11212          (article (gnus-summary-article-number))
11213          (id (mail-header-id (gnus-summary-article-header)))
11214          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11215     (unless id
11216       (error "No article on the current line"))
11217     (gnus-rebuild-thread id)
11218     (gnus-summary-goto-subject article)))
11219
11220 (defun gnus-summary-reparent-thread ()
11221   "Make the current article child of the marked (or previous) article.
11222
11223 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11224 is non-nil or the Subject: of both articles are the same."
11225   (interactive)
11226   (unless (not (gnus-group-read-only-p))
11227     (error "The current newsgroup does not support article editing"))
11228   (unless (<= (length gnus-newsgroup-processable) 1)
11229     (error "No more than one article may be marked"))
11230   (let ((child (gnus-summary-article-number))
11231         ;; First grab the marked article, otherwise one line up.
11232         (parent (if (not (null gnus-newsgroup-processable))
11233                     (car gnus-newsgroup-processable)
11234                   (save-excursion
11235                     (if (eq (forward-line -1) 0)
11236                         (gnus-summary-article-number)
11237                       (error "Beginning of summary buffer"))))))
11238     (gnus-summary-reparent-children parent (list child))))
11239
11240 (defun gnus-summary-reparent-children (parent children)
11241   "Make PARENT the parent of CHILDREN.
11242 When called interactively, PARENT is the current article and CHILDREN
11243 are the process-marked articles."
11244   (interactive
11245    (list (gnus-summary-article-number)
11246          (gnus-summary-work-articles nil)))
11247   (dolist (child children)
11248     (save-window-excursion
11249       (let ((gnus-article-buffer " *reparent*"))
11250         (unless (not (eq parent child))
11251           (error "An article may not be self-referential"))
11252         (let ((message-id (mail-header-id
11253                            (gnus-summary-article-header parent))))
11254           (unless (and message-id (not (equal message-id "")))
11255             (error "No message-id in desired parent"))
11256           (gnus-with-article child
11257             (save-restriction
11258               (goto-char (point-min))
11259               (message-narrow-to-head)
11260               (if (re-search-forward "^References: " nil t)
11261                   (progn
11262                     (re-search-forward "^[^ \t]" nil t)
11263                     (forward-line -1)
11264                     (end-of-line)
11265                     (insert " " message-id))
11266                 (insert "References: " message-id "\n"))))
11267           (set-buffer gnus-summary-buffer)
11268           (gnus-summary-unmark-all-processable)
11269           (gnus-summary-update-article child)
11270           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11271             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11272           (gnus-summary-rethread-current)
11273           (gnus-message 3 "Article %d is now the child of article %d"
11274                         child parent))))))
11275
11276 (defun gnus-summary-toggle-threads (&optional arg)
11277   "Toggle showing conversation threads.
11278 If ARG is positive number, turn showing conversation threads on."
11279   (interactive "P")
11280   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11281     (setq gnus-show-threads
11282           (if (null arg) (not gnus-show-threads)
11283             (> (prefix-numeric-value arg) 0)))
11284     (gnus-summary-prepare)
11285     (gnus-summary-goto-subject current)
11286     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11287     (gnus-summary-position-point)))
11288
11289 (eval-and-compile
11290   (if (fboundp 'remove-overlays)
11291       (defalias 'gnus-remove-overlays 'remove-overlays)
11292     (defun gnus-remove-overlays (beg end name val)
11293       "Clear BEG and END of overlays whose property NAME has value VAL.
11294 For compatibility with Emacs 21 and XEmacs."
11295       (dolist (ov (gnus-overlays-in beg end))
11296         (when (eq (gnus-overlay-get ov name) val)
11297           (gnus-delete-overlay ov))))))
11298
11299 (defun gnus-summary-show-all-threads ()
11300   "Show all threads."
11301   (interactive)
11302   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11303   (gnus-summary-position-point))
11304
11305 (defun gnus-summary-show-thread ()
11306   "Show thread subtrees.
11307 Returns nil if no thread was there to be shown."
11308   (interactive)
11309   (let* ((orig (point))
11310          (end (point-at-eol))
11311          ;; Leave point at bol
11312          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11313          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11314                 (if (fboundp 'next-single-char-property-change)
11315                     (or (next-single-char-property-change end 'invisible)
11316                         (point-max))
11317                   (while (progn
11318                            (end-of-line 2)
11319                            (and (not (eobp))
11320                                 (eq (get-char-property (point) 'invisible)
11321                                     'gnus-sum))))
11322                   (point)))))
11323     (when eoi
11324       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11325       (goto-char orig)
11326       (gnus-summary-position-point)
11327       eoi)))
11328
11329 (defun gnus-summary-maybe-hide-threads ()
11330   "If requested, hide the threads that should be hidden."
11331   (when (and gnus-show-threads
11332              gnus-thread-hide-subtree)
11333     (gnus-summary-hide-all-threads
11334      (if (or (consp gnus-thread-hide-subtree)
11335              (functionp gnus-thread-hide-subtree))
11336          (gnus-make-predicate gnus-thread-hide-subtree)
11337        nil))))
11338
11339 ;;; Hiding predicates.
11340
11341 (defun gnus-article-unread-p (header)
11342   (memq (mail-header-number header) gnus-newsgroup-unreads))
11343
11344 (defun gnus-article-unseen-p (header)
11345   (memq (mail-header-number header) gnus-newsgroup-unseen))
11346
11347 (defun gnus-map-articles (predicate articles)
11348   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11349   (apply 'gnus-or (mapcar predicate
11350                           (mapcar (lambda (number)
11351                                     (gnus-summary-article-header number))
11352                                   articles))))
11353
11354 (defun gnus-summary-hide-all-threads (&optional predicate)
11355   "Hide all thread subtrees.
11356 If PREDICATE is supplied, threads that satisfy this predicate
11357 will not be hidden."
11358   (interactive)
11359   (save-excursion
11360     (goto-char (point-min))
11361     (let ((end nil))
11362       (while (not end)
11363         (when (or (not predicate)
11364                   (gnus-map-articles
11365                    predicate (gnus-summary-article-children)))
11366             (gnus-summary-hide-thread))
11367         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11368   (gnus-summary-position-point))
11369
11370 (defun gnus-summary-hide-thread ()
11371   "Hide thread subtrees.
11372 If PREDICATE is supplied, threads that satisfy this predicate
11373 will not be hidden.
11374 Returns nil if no threads were there to be hidden."
11375   (interactive)
11376   (let ((start (point))
11377         (starteol (line-end-position))
11378         (article (gnus-summary-article-number)))
11379     (goto-char start)
11380     ;; Go forward until either the buffer ends or the subthread ends.
11381     (when (and (not (eobp))
11382                (or (zerop (gnus-summary-next-thread 1 t))
11383                    (goto-char (point-max))))
11384       (if (and (> (point) start)
11385                ;; FIXME: this should actually search for a non-invisible \n.
11386                (search-backward "\n" start t))
11387           (progn
11388             (when (> (point) starteol)
11389               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11390               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11391                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11392                 (gnus-overlay-put ol 'evaporate t)))
11393             (gnus-summary-goto-subject article))
11394         (goto-char start)
11395         nil))))
11396
11397 (defun gnus-summary-go-to-next-thread (&optional previous)
11398   "Go to the same level (or less) next thread.
11399 If PREVIOUS is non-nil, go to previous thread instead.
11400 Return the article number moved to, or nil if moving was impossible."
11401   (let ((level (gnus-summary-thread-level))
11402         (way (if previous -1 1))
11403         (beg (point)))
11404     (forward-line way)
11405     (while (and (not (eobp))
11406                 (< level (gnus-summary-thread-level)))
11407       (forward-line way))
11408     (if (eobp)
11409         (progn
11410           (goto-char beg)
11411           nil)
11412       (setq beg (point))
11413       (prog1
11414           (gnus-summary-article-number)
11415         (goto-char beg)))))
11416
11417 (defun gnus-summary-next-thread (n &optional silent)
11418   "Go to the same level next N'th thread.
11419 If N is negative, search backward instead.
11420 Returns the difference between N and the number of skips actually
11421 done.
11422
11423 If SILENT, don't output messages."
11424   (interactive "p")
11425   (let ((backward (< n 0))
11426         (n (abs n)))
11427     (while (and (> n 0)
11428                 (gnus-summary-go-to-next-thread backward))
11429       (decf n))
11430     (unless silent
11431       (gnus-summary-position-point))
11432     (when (and (not silent) (/= 0 n))
11433       (gnus-message 7 "No more threads"))
11434     n))
11435
11436 (defun gnus-summary-prev-thread (n)
11437   "Go to the same level previous N'th thread.
11438 Returns the difference between N and the number of skips actually
11439 done."
11440   (interactive "p")
11441   (gnus-summary-next-thread (- n)))
11442
11443 (defun gnus-summary-go-down-thread ()
11444   "Go down one level in the current thread."
11445   (let ((children (gnus-summary-article-children)))
11446     (when children
11447       (gnus-summary-goto-subject (car children)))))
11448
11449 (defun gnus-summary-go-up-thread ()
11450   "Go up one level in the current thread."
11451   (let ((parent (gnus-summary-article-parent)))
11452     (when parent
11453       (gnus-summary-goto-subject parent))))
11454
11455 (defun gnus-summary-down-thread (n)
11456   "Go down thread N steps.
11457 If N is negative, go up instead.
11458 Returns the difference between N and how many steps down that were
11459 taken."
11460   (interactive "p")
11461   (let ((up (< n 0))
11462         (n (abs n)))
11463     (while (and (> n 0)
11464                 (if up (gnus-summary-go-up-thread)
11465                   (gnus-summary-go-down-thread)))
11466       (setq n (1- n)))
11467     (gnus-summary-position-point)
11468     (when (/= 0 n)
11469       (gnus-message 7 "Can't go further"))
11470     n))
11471
11472 (defun gnus-summary-up-thread (n)
11473   "Go up thread N steps.
11474 If N is negative, go down instead.
11475 Returns the difference between N and how many steps down that were
11476 taken."
11477   (interactive "p")
11478   (gnus-summary-down-thread (- n)))
11479
11480 (defun gnus-summary-top-thread ()
11481   "Go to the top of the thread."
11482   (interactive)
11483   (while (gnus-summary-go-up-thread))
11484   (gnus-summary-article-number))
11485
11486 (defun gnus-summary-expire-thread ()
11487   "Mark articles under current thread as expired."
11488   (interactive)
11489   (gnus-summary-kill-thread 0))
11490
11491 (defun gnus-summary-kill-thread (&optional unmark)
11492   "Mark articles under current thread as read.
11493 If the prefix argument is positive, remove any kinds of marks.
11494 If the prefix argument is zero, mark thread as expired.
11495 If the prefix argument is negative, tick articles instead."
11496   (interactive "P")
11497   (when unmark
11498     (setq unmark (prefix-numeric-value unmark)))
11499   (let ((articles (gnus-summary-articles-in-thread))
11500         (hide (or (null unmark) (= unmark 0))))
11501     (save-excursion
11502       ;; Expand the thread.
11503       (gnus-summary-show-thread)
11504       ;; Mark all the articles.
11505       (while articles
11506         (gnus-summary-goto-subject (car articles))
11507         (cond ((null unmark)
11508                (gnus-summary-mark-article-as-read gnus-killed-mark))
11509               ((> unmark 0)
11510                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11511               ((= unmark 0)
11512                (gnus-summary-mark-article nil gnus-expirable-mark))
11513               (t
11514                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11515         (setq articles (cdr articles))))
11516     ;; Hide killed subtrees when hide is true.
11517     (and hide
11518          gnus-thread-hide-killed
11519          (gnus-summary-hide-thread))
11520     ;; If hide is t, go to next unread subject.
11521     (when hide
11522       ;; Go to next unread subject.
11523       (gnus-summary-next-subject 1 t)))
11524   (gnus-set-mode-line 'summary))
11525
11526 ;; Summary sorting commands
11527
11528 (defun gnus-summary-sort-by-number (&optional reverse)
11529   "Sort the summary buffer by article number.
11530 Argument REVERSE means reverse order."
11531   (interactive "P")
11532   (gnus-summary-sort 'number reverse))
11533
11534 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11535   "Sort the summary buffer by most recent article number.
11536 Argument REVERSE means reverse order."
11537   (interactive "P")
11538   (gnus-summary-sort 'most-recent-number reverse))
11539
11540 (defun gnus-summary-sort-by-random (&optional reverse)
11541   "Randomize the order in the summary buffer.
11542 Argument REVERSE means to randomize in reverse order."
11543   (interactive "P")
11544   (gnus-summary-sort 'random reverse))
11545
11546 (defun gnus-summary-sort-by-author (&optional reverse)
11547   "Sort the summary buffer by author name alphabetically.
11548 If `case-fold-search' is non-nil, case of letters is ignored.
11549 Argument REVERSE means reverse order."
11550   (interactive "P")
11551   (gnus-summary-sort 'author reverse))
11552
11553 (defun gnus-summary-sort-by-recipient (&optional reverse)
11554   "Sort the summary buffer by recipient name alphabetically.
11555 If `case-fold-search' is non-nil, case of letters is ignored.
11556 Argument REVERSE means reverse order."
11557   (interactive "P")
11558   (gnus-summary-sort 'recipient reverse))
11559
11560 (defun gnus-summary-sort-by-subject (&optional reverse)
11561   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11562 If `case-fold-search' is non-nil, case of letters is ignored.
11563 Argument REVERSE means reverse order."
11564   (interactive "P")
11565   (gnus-summary-sort 'subject reverse))
11566
11567 (defun gnus-summary-sort-by-date (&optional reverse)
11568   "Sort the summary buffer by date.
11569 Argument REVERSE means reverse order."
11570   (interactive "P")
11571   (gnus-summary-sort 'date reverse))
11572
11573 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11574   "Sort the summary buffer by most recent date.
11575 Argument REVERSE means reverse order."
11576   (interactive "P")
11577   (gnus-summary-sort 'most-recent-date reverse))
11578
11579 (defun gnus-summary-sort-by-score (&optional reverse)
11580   "Sort the summary buffer by score.
11581 Argument REVERSE means reverse order."
11582   (interactive "P")
11583   (gnus-summary-sort 'score reverse))
11584
11585 (defun gnus-summary-sort-by-lines (&optional reverse)
11586   "Sort the summary buffer by the number of lines.
11587 Argument REVERSE means reverse order."
11588   (interactive "P")
11589   (gnus-summary-sort 'lines reverse))
11590
11591 (defun gnus-summary-sort-by-chars (&optional reverse)
11592   "Sort the summary buffer by article length.
11593 Argument REVERSE means reverse order."
11594   (interactive "P")
11595   (gnus-summary-sort 'chars reverse))
11596
11597 (defun gnus-summary-sort-by-original (&optional reverse)
11598   "Sort the summary buffer using the default sorting method.
11599 Argument REVERSE means reverse order."
11600   (interactive "P")
11601   (let* ((inhibit-read-only t)
11602          (gnus-summary-prepare-hook nil))
11603     ;; We do the sorting by regenerating the threads.
11604     (gnus-summary-prepare)
11605     ;; Hide subthreads if needed.
11606     (gnus-summary-maybe-hide-threads)))
11607
11608 (defun gnus-summary-sort (predicate reverse)
11609   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11610   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11611          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11612          (gnus-thread-sort-functions
11613           (if (not reverse)
11614               thread
11615             `(lambda (t1 t2)
11616                (,thread t2 t1))))
11617          (gnus-sort-gathered-threads-function
11618           gnus-thread-sort-functions)
11619          (gnus-article-sort-functions
11620           (if (not reverse)
11621               article
11622             `(lambda (t1 t2)
11623                (,article t2 t1))))
11624          (inhibit-read-only t)
11625          (gnus-summary-prepare-hook nil))
11626     ;; We do the sorting by regenerating the threads.
11627     (gnus-summary-prepare)
11628     ;; Hide subthreads if needed.
11629     (gnus-summary-maybe-hide-threads)))
11630
11631 ;; Summary saving commands.
11632
11633 (defun gnus-summary-save-article (&optional n not-saved)
11634   "Save the current article using the default saver function.
11635 If N is a positive number, save the N next articles.
11636 If N is a negative number, save the N previous articles.
11637 If N is nil and any articles have been marked with the process mark,
11638 save those articles instead.
11639 The variable `gnus-default-article-saver' specifies the saver function.
11640
11641 If the optional second argument NOT-SAVED is non-nil, articles saved
11642 will not be marked as saved."
11643   (interactive "P")
11644   (require 'gnus-art)
11645   (let* ((articles (gnus-summary-work-articles n))
11646          (save-buffer (save-excursion
11647                         (nnheader-set-temp-buffer " *Gnus Save*")))
11648          (num (length articles))
11649          ;; Whether to save decoded articles or raw articles.
11650          (decode (when gnus-article-save-coding-system
11651                    (get gnus-default-article-saver :decode)))
11652          ;; When saving many articles in a single file, use the other
11653          ;; function to save articles other than the first one.
11654          (saver2 (get gnus-default-article-saver :function))
11655          (gnus-prompt-before-saving (if saver2
11656                                         t
11657                                       gnus-prompt-before-saving))
11658          (gnus-default-article-saver gnus-default-article-saver)
11659          header file)
11660     (dolist (article articles)
11661       (setq header (gnus-summary-article-header article))
11662       (if (not (vectorp header))
11663           ;; This is a pseudo-article.
11664           (if (assq 'name header)
11665               (gnus-copy-file (cdr (assq 'name header)))
11666             (gnus-message 1 "Article %d is unsaveable" article))
11667         ;; This is a real article.
11668         (save-window-excursion
11669           (gnus-summary-select-article decode decode nil article)
11670           (gnus-summary-goto-subject article))
11671         (with-current-buffer save-buffer
11672           (erase-buffer)
11673           (insert-buffer-substring (if decode
11674                                        gnus-article-buffer
11675                                      gnus-original-article-buffer)))
11676         (setq file (gnus-article-save save-buffer file num))
11677         (gnus-summary-remove-process-mark article)
11678         (unless not-saved
11679           (gnus-summary-set-saved-mark article)))
11680       (when saver2
11681         (setq gnus-default-article-saver saver2
11682               saver2 nil)))
11683     (gnus-kill-buffer save-buffer)
11684     (gnus-summary-position-point)
11685     (gnus-set-mode-line 'summary)
11686     n))
11687
11688 (defun gnus-summary-pipe-output (&optional n sym)
11689   "Pipe the current article to a subprocess.
11690 If N is a positive number, pipe the N next articles.
11691 If N is a negative number, pipe the N previous articles.
11692 If N is nil and any articles have been marked with the process mark,
11693 pipe those articles instead.
11694 The default command to which articles are piped is specified by the
11695 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11696 will be prompted for the command.
11697
11698 The properties `:decode' and `:headers' that are put to the function
11699 symbol `gnus-summary-save-in-pipe' control whether this function
11700 decodes articles and what headers to keep (see the doc string for the
11701 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11702 is neither omitted nor the symbol `r', force including all headers
11703 regardless of the `:headers' property.  If it is the symbol `r',
11704 articles that are not decoded and include all headers will be piped
11705 no matter what the properties `:decode' and `:headers' are."
11706   (interactive (gnus-interactive "P\ny"))
11707   (require 'gnus-art)
11708   (let* ((articles (gnus-summary-work-articles n))
11709          (result-buffer "*Shell Command Output*")
11710          (all-headers (not (memq sym '(nil r))))
11711          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11712          (raw (eq sym 'r))
11713          (headers (get 'gnus-summary-save-in-pipe :headers))
11714          command result)
11715     (unless (numberp (car articles))
11716       (error "No article to pipe"))
11717     (setq command (gnus-read-shell-command
11718                    (concat "Shell command on "
11719                            (if (cdr articles)
11720                                (format "these %d articles" (length articles))
11721                              "this article")
11722                            ": ")
11723                    gnus-summary-pipe-output-default-command))
11724     (when (string-equal command "")
11725       (error "A command is required"))
11726     (when all-headers
11727       (put 'gnus-summary-save-in-pipe :headers nil))
11728     (unwind-protect
11729         (while articles
11730           (gnus-summary-goto-subject (pop articles))
11731           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11732           (when (and (get-buffer result-buffer)
11733                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11734             (setq result (concat result (with-current-buffer result-buffer
11735                                           (buffer-string))))))
11736       (put 'gnus-summary-save-in-pipe :headers headers))
11737     (unless (zerop (length result))
11738       (if (with-current-buffer (get-buffer-create result-buffer)
11739             (erase-buffer)
11740             (insert result)
11741             (prog1
11742                 (and (= (count-lines (point-min) (point)) 1)
11743                      (progn
11744                        (end-of-line 0)
11745                        (<= (current-column)
11746                            (window-width (minibuffer-window)))))
11747               (goto-char (point-min))))
11748           (message "%s" (substring result 0 -1))
11749         (message nil)
11750         (gnus-configure-windows 'pipe)))))
11751
11752 (defun gnus-summary-save-article-mail (&optional arg)
11753   "Append the current article to a Unix mail box file.
11754 If N is a positive number, save the N next articles.
11755 If N is a negative number, save the N previous articles.
11756 If N is nil and any articles have been marked with the process mark,
11757 save those articles instead."
11758   (interactive "P")
11759   (require 'gnus-art)
11760   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11761     (gnus-summary-save-article arg)))
11762
11763 (defun gnus-summary-save-article-rmail (&optional arg)
11764   "Append the current article to an rmail file.
11765 If N is a positive number, save the N next articles.
11766 If N is a negative number, save the N previous articles.
11767 If N is nil and any articles have been marked with the process mark,
11768 save those articles instead."
11769   (interactive "P")
11770   (require 'gnus-art)
11771   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11772     (gnus-summary-save-article arg)))
11773
11774 (defun gnus-summary-save-article-file (&optional arg)
11775   "Append the current article to a file.
11776 If N is a positive number, save the N next articles.
11777 If N is a negative number, save the N previous articles.
11778 If N is nil and any articles have been marked with the process mark,
11779 save those articles instead."
11780   (interactive "P")
11781   (require 'gnus-art)
11782   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11783     (gnus-summary-save-article arg)))
11784
11785 (defun gnus-summary-write-article-file (&optional arg)
11786   "Write the current article to a file, deleting the previous file.
11787 If N is a positive number, save the N next articles.
11788 If N is a negative number, save the N previous articles.
11789 If N is nil and any articles have been marked with the process mark,
11790 save those articles instead."
11791   (interactive "P")
11792   (require 'gnus-art)
11793   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11794     (gnus-summary-save-article arg)))
11795
11796 (defun gnus-summary-save-article-body-file (&optional arg)
11797   "Append the current article body to a file.
11798 If N is a positive number, save the N next articles.
11799 If N is a negative number, save the N previous articles.
11800 If N is nil and any articles have been marked with the process mark,
11801 save those articles instead."
11802   (interactive "P")
11803   (require 'gnus-art)
11804   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11805     (gnus-summary-save-article arg)))
11806
11807 (defun gnus-summary-write-article-body-file (&optional arg)
11808   "Write the current article body to a file, deleting the previous file.
11809 If N is a positive number, save the N next articles.
11810 If N is a negative number, save the N previous articles.
11811 If N is nil and any articles have been marked with the process mark,
11812 save those articles instead."
11813   (interactive "P")
11814   (require 'gnus-art)
11815   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11816     (gnus-summary-save-article arg)))
11817
11818 (defun gnus-summary-muttprint (&optional arg)
11819   "Print the current article using Muttprint.
11820 If N is a positive number, save the N next articles.
11821 If N is a negative number, save the N previous articles.
11822 If N is nil and any articles have been marked with the process mark,
11823 save those articles instead."
11824   (interactive "P")
11825   (require 'gnus-art)
11826   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11827     (gnus-summary-save-article arg t)))
11828
11829 (defun gnus-summary-pipe-message (program)
11830   "Pipe the current article through PROGRAM."
11831   (interactive "sProgram: ")
11832   (gnus-summary-select-article)
11833   (let ((mail-header-separator ""))
11834     (gnus-eval-in-buffer-window gnus-article-buffer
11835       (save-restriction
11836         (widen)
11837         (let ((start (window-start))
11838               (inhibit-read-only t))
11839           (message-pipe-buffer-body program)
11840           (set-window-start (get-buffer-window (current-buffer)) start))))))
11841
11842 (defun gnus-get-split-value (methods)
11843   "Return a value based on the split METHODS."
11844   (let (split-name method result match)
11845     (when methods
11846       (with-current-buffer gnus-original-article-buffer
11847         (save-restriction
11848           (nnheader-narrow-to-headers)
11849           (while (and methods (not split-name))
11850             (goto-char (point-min))
11851             (setq method (pop methods))
11852             (setq match (car method))
11853             (when (cond
11854                    ((stringp match)
11855                     ;; Regular expression.
11856                     (ignore-errors
11857                       (re-search-forward match nil t)))
11858                    ((functionp match)
11859                     ;; Function.
11860                     (save-restriction
11861                       (widen)
11862                       (setq result (funcall match gnus-newsgroup-name))))
11863                    ((consp match)
11864                     ;; Form.
11865                     (save-restriction
11866                       (widen)
11867                       (setq result (eval match)))))
11868               (setq split-name (cdr method))
11869               (cond ((stringp result)
11870                      (push (expand-file-name
11871                             result gnus-article-save-directory)
11872                            split-name))
11873                     ((consp result)
11874                      (setq split-name (append result split-name)))))))))
11875     (nreverse split-name)))
11876
11877 (defun gnus-valid-move-group-p (group)
11878   (and (boundp group)
11879        (symbol-name group)
11880        (symbol-value group)
11881        (gnus-get-function (gnus-find-method-for-group
11882                            (symbol-name group)) 'request-accept-article t)))
11883
11884 (defun gnus-read-move-group-name (prompt default articles prefix)
11885   "Read a group name."
11886   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11887          (minibuffer-confirm-incomplete nil) ; XEmacs
11888          (prom
11889           (format "%s %s to"
11890                   prompt
11891                   (if (> (length articles) 1)
11892                       (format "these %d articles" (length articles))
11893                     "this article")))
11894          (to-newsgroup
11895           (let (active group)
11896             (when (or (null split-name) (= 1 (length split-name)))
11897               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11898               (mapatoms (lambda (symbol)
11899                           (setq group (symbol-name symbol))
11900                           (when (string-match "[^\000-\177]" group)
11901                             (setq group (gnus-group-decoded-name group)))
11902                           (set (intern group active) group))
11903                         gnus-active-hashtb))
11904             (cond
11905              ((null split-name)
11906               (gnus-completing-read-with-default
11907                default prom active 'gnus-valid-move-group-p nil prefix
11908                'gnus-group-history))
11909              ((= 1 (length split-name))
11910               (gnus-completing-read-with-default
11911                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11912                'gnus-group-history))
11913              (t
11914               (gnus-completing-read-with-default
11915                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11916                'gnus-group-history)))))
11917          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11918          encoded)
11919     (when to-newsgroup
11920       (if (or (string= to-newsgroup "")
11921               (string= to-newsgroup prefix))
11922           (setq to-newsgroup default))
11923       (unless to-newsgroup
11924         (error "No group name entered"))
11925       (setq encoded (mm-encode-coding-string
11926                      to-newsgroup
11927                      (gnus-group-name-charset to-method to-newsgroup)))
11928       (or (gnus-active encoded)
11929           (gnus-activate-group encoded nil nil to-method)
11930           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11931                                      to-newsgroup))
11932               (or (and (gnus-request-create-group encoded to-method)
11933                        (gnus-activate-group encoded nil nil to-method)
11934                        (gnus-subscribe-group encoded))
11935                   (error "Couldn't create group %s" to-newsgroup)))
11936           (error "No such group: %s" to-newsgroup))
11937       encoded)))
11938
11939 (defvar gnus-summary-save-parts-counter)
11940 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11941
11942 (defun gnus-summary-save-parts (type dir n &optional reverse)
11943   "Save parts matching TYPE to DIR.
11944 If REVERSE, save parts that do not match TYPE."
11945   (interactive
11946    (list (read-string "Save parts of type: "
11947                       (or (car gnus-summary-save-parts-type-history)
11948                           gnus-summary-save-parts-default-mime)
11949                       'gnus-summary-save-parts-type-history)
11950          (setq gnus-summary-save-parts-last-directory
11951                (read-file-name "Save to directory: "
11952                                gnus-summary-save-parts-last-directory
11953                                nil t))
11954          current-prefix-arg))
11955   (gnus-summary-iterate n
11956     (let ((gnus-display-mime-function nil)
11957           gnus-article-prepare-hook
11958           gnus-article-decode-hook
11959           gnus-display-mime-function
11960           gnus-break-pages
11961           (gnus-inhibit-treatment t))
11962       (gnus-summary-select-article))
11963     (with-current-buffer gnus-article-buffer
11964       (let ((handles (or gnus-article-mime-handles
11965                          (mm-dissect-buffer nil gnus-article-loose-mime)
11966                          (and gnus-article-emulate-mime
11967                               (mm-uu-dissect))))
11968             (gnus-summary-save-parts-counter 1))
11969         (when handles
11970           (gnus-summary-save-parts-1 type dir handles reverse)
11971           (unless gnus-article-mime-handles ;; Don't destroy this case.
11972             (mm-destroy-parts handles)))))))
11973
11974 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11975   (if (stringp (car handle))
11976       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11977               (cdr handle))
11978     (when (if reverse
11979               (not (string-match type (mm-handle-media-type handle)))
11980             (string-match type (mm-handle-media-type handle)))
11981       (let ((file (expand-file-name
11982                    (gnus-map-function
11983                     mm-file-name-rewrite-functions
11984                     (file-name-nondirectory
11985                      (or
11986                       (mail-content-type-get
11987                        (mm-handle-disposition handle) 'filename)
11988                       (mail-content-type-get
11989                        (mm-handle-type handle) 'name)
11990                       (format "%s.%d.%d" gnus-newsgroup-name
11991                               (cdr gnus-article-current)
11992                               gnus-summary-save-parts-counter))))
11993                    dir)))
11994         (incf gnus-summary-save-parts-counter)
11995         (unless (file-exists-p file)
11996           (mm-save-part-to-file handle file))))))
11997
11998 ;; Summary extract commands
11999
12000 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12001   (let ((inhibit-read-only t)
12002         (article (gnus-summary-article-number))
12003         after-article b e)
12004     (unless (gnus-summary-goto-subject article)
12005       (error "No such article: %d" article))
12006     (gnus-summary-position-point)
12007     ;; If all commands are to be bunched up on one line, we collect
12008     ;; them here.
12009     (unless gnus-view-pseudos-separately
12010       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12011             files action)
12012         (while ps
12013           (setq action (cdr (assq 'action (car ps))))
12014           (setq files (list (cdr (assq 'name (car ps)))))
12015           (while (and ps (cdr ps)
12016                       (string= (or action "1")
12017                                (or (cdr (assq 'action (cadr ps))) "2")))
12018             (push (cdr (assq 'name (cadr ps))) files)
12019             (setcdr ps (cddr ps)))
12020           (when files
12021             (when (not (string-match "%s" action))
12022               (push " " files))
12023             (push " " files)
12024             (when (assq 'execute (car ps))
12025               (setcdr (assq 'execute (car ps))
12026                       (funcall (if (string-match "%s" action)
12027                                    'format 'concat)
12028                                action
12029                                (mapconcat
12030                                 (lambda (f)
12031                                   (if (equal f " ")
12032                                       f
12033                                     (shell-quote-argument f)))
12034                                 files " ")))))
12035           (setq ps (cdr ps)))))
12036     (if (and gnus-view-pseudos (not not-view))
12037         (while pslist
12038           (when (assq 'execute (car pslist))
12039             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12040                                   (eq gnus-view-pseudos 'not-confirm)))
12041           (setq pslist (cdr pslist)))
12042       (save-excursion
12043         (while pslist
12044           (setq after-article (or (cdr (assq 'article (car pslist)))
12045                                   (gnus-summary-article-number)))
12046           (gnus-summary-goto-subject after-article)
12047           (forward-line 1)
12048           (setq b (point))
12049           (insert "    " (file-name-nondirectory
12050                           (cdr (assq 'name (car pslist))))
12051                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12052           (setq e (point))
12053           (forward-line -1)             ; back to `b'
12054           (gnus-add-text-properties
12055            b (1- e) (list 'gnus-number gnus-reffed-article-number
12056                           gnus-mouse-face-prop gnus-mouse-face))
12057           (gnus-data-enter
12058            after-article gnus-reffed-article-number
12059            gnus-unread-mark b (car pslist) 0 (- e b))
12060           (setq gnus-newsgroup-unreads
12061                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12062                                          gnus-reffed-article-number))
12063           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12064           (setq pslist (cdr pslist)))))))
12065
12066 (defun gnus-pseudos< (p1 p2)
12067   (let ((c1 (cdr (assq 'action p1)))
12068         (c2 (cdr (assq 'action p2))))
12069     (and c1 c2 (string< c1 c2))))
12070
12071 (defun gnus-request-pseudo-article (props)
12072   (cond ((assq 'execute props)
12073          (gnus-execute-command (cdr (assq 'execute props)))))
12074   (let ((gnus-current-article (gnus-summary-article-number)))
12075     (gnus-run-hooks 'gnus-mark-article-hook)))
12076
12077 (defun gnus-execute-command (command &optional automatic)
12078   (save-excursion
12079     (gnus-article-setup-buffer)
12080     (set-buffer gnus-article-buffer)
12081     (setq buffer-read-only nil)
12082     (let ((command (if automatic command
12083                      (read-string "Command: " (cons command 0)))))
12084       (erase-buffer)
12085       (insert "$ " command "\n\n")
12086       (if gnus-view-pseudo-asynchronously
12087           (start-process "gnus-execute" (current-buffer) shell-file-name
12088                          shell-command-switch command)
12089         (call-process shell-file-name nil t nil
12090                       shell-command-switch command)))))
12091
12092 ;; Summary kill commands.
12093
12094 (defun gnus-summary-edit-global-kill (article)
12095   "Edit the \"global\" kill file."
12096   (interactive (list (gnus-summary-article-number)))
12097   (gnus-group-edit-global-kill article))
12098
12099 (defun gnus-summary-edit-local-kill ()
12100   "Edit a local kill file applied to the current newsgroup."
12101   (interactive)
12102   (setq gnus-current-headers (gnus-summary-article-header))
12103   (gnus-group-edit-local-kill
12104    (gnus-summary-article-number) gnus-newsgroup-name))
12105
12106 ;;; Header reading.
12107
12108 (defun gnus-read-header (id &optional header)
12109   "Read the headers of article ID and enter them into the Gnus system."
12110   (let ((group gnus-newsgroup-name)
12111         (gnus-override-method
12112          (or
12113           gnus-override-method
12114           (and (gnus-news-group-p gnus-newsgroup-name)
12115                (car (gnus-refer-article-methods)))))
12116         where)
12117     ;; First we check to see whether the header in question is already
12118     ;; fetched.
12119     (if (stringp id)
12120         ;; This is a Message-ID.
12121         (setq header (or header (gnus-id-to-header id)))
12122       ;; This is an article number.
12123       (setq header (or header (gnus-summary-article-header id))))
12124     (if (and header
12125              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12126         ;; We have found the header.
12127         header
12128       ;; We have to really fetch the header to this article.
12129       (with-current-buffer nntp-server-buffer
12130         (when (setq where (gnus-request-head id group))
12131           (nnheader-fold-continuation-lines)
12132           (goto-char (point-max))
12133           (insert ".\n")
12134           (goto-char (point-min))
12135           (insert "211 ")
12136           (princ (cond
12137                   ((numberp id) id)
12138                   ((cdr where) (cdr where))
12139                   (header (mail-header-number header))
12140                   (t gnus-reffed-article-number))
12141                  (current-buffer))
12142           (insert " Article retrieved.\n"))
12143         (if (or (not where)
12144                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12145             ()                          ; Malformed head.
12146           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12147             (when (and (stringp id)
12148                        (or
12149                         (not (string= (gnus-group-real-name group)
12150                                       (car where)))
12151                         (not (gnus-server-equal gnus-override-method
12152                                                 (gnus-group-method group)))))
12153               ;; If we fetched by Message-ID and the article came from
12154               ;; a different group (or server), we fudge some bogus
12155               ;; article numbers for this article.
12156               (mail-header-set-number header gnus-reffed-article-number))
12157             (with-current-buffer gnus-summary-buffer
12158               (decf gnus-reffed-article-number)
12159               (gnus-remove-header (mail-header-number header))
12160               (push header gnus-newsgroup-headers)
12161               (setq gnus-current-headers header)
12162               (push (mail-header-number header) gnus-newsgroup-limit)))
12163           header)))))
12164
12165 (defun gnus-remove-header (number)
12166   "Remove header NUMBER from `gnus-newsgroup-headers'."
12167   (if (and gnus-newsgroup-headers
12168            (= number (mail-header-number (car gnus-newsgroup-headers))))
12169       (pop gnus-newsgroup-headers)
12170     (let ((headers gnus-newsgroup-headers))
12171       (while (and (cdr headers)
12172                   (not (= number (mail-header-number (cadr headers)))))
12173         (pop headers))
12174       (when (cdr headers)
12175         (setcdr headers (cddr headers))))))
12176
12177 ;;;
12178 ;;; summary highlights
12179 ;;;
12180
12181 (defun gnus-highlight-selected-summary ()
12182   "Highlight selected article in summary buffer."
12183   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12184   (when gnus-summary-selected-face
12185     (save-excursion
12186       (let* ((beg (point-at-bol))
12187              (end (point-at-eol))
12188              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12189              (from (if (get-text-property beg gnus-mouse-face-prop)
12190                        beg
12191                      (or (next-single-property-change
12192                           beg gnus-mouse-face-prop nil end)
12193                          beg)))
12194              (to
12195               (if (= from end)
12196                   (- from 2)
12197                 (or (next-single-property-change
12198                      from gnus-mouse-face-prop nil end)
12199                     end))))
12200         ;; If no mouse-face prop on line we will have to = from = end,
12201         ;; so we highlight the entire line instead.
12202         (when (= (+ to 2) from)
12203           (setq from beg)
12204           (setq to end))
12205         (if gnus-newsgroup-selected-overlay
12206             ;; Move old overlay.
12207             (gnus-move-overlay
12208              gnus-newsgroup-selected-overlay from to (current-buffer))
12209           ;; Create new overlay.
12210           (gnus-overlay-put
12211            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12212            'face gnus-summary-selected-face))))))
12213
12214 (defvar gnus-summary-highlight-line-cached nil)
12215 (defvar gnus-summary-highlight-line-trigger nil)
12216
12217 (defun gnus-summary-highlight-line-0 ()
12218   (if (and (eq gnus-summary-highlight-line-trigger
12219                gnus-summary-highlight)
12220            gnus-summary-highlight-line-cached)
12221       gnus-summary-highlight-line-cached
12222     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12223           gnus-summary-highlight-line-cached
12224           (let* ((cond (list 'cond))
12225                  (c cond)
12226                  (list gnus-summary-highlight))
12227             (while list
12228               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12229                               nil))
12230               (setq c (cdr c)
12231                     list (cdr list)))
12232             (gnus-byte-compile (list 'lambda nil cond))))))
12233
12234 (defun gnus-summary-highlight-line ()
12235   "Highlight current line according to `gnus-summary-highlight'."
12236   (let* ((beg (point-at-bol))
12237          (article (or (gnus-summary-article-number) gnus-current-article))
12238          (score (or (cdr (assq article
12239                                gnus-newsgroup-scored))
12240                     gnus-summary-default-score 0))
12241          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12242          (inhibit-read-only t)
12243          (default gnus-summary-default-score)
12244          (default-high gnus-summary-default-high-score)
12245          (default-low gnus-summary-default-low-score)
12246          (uncached (and gnus-summary-use-undownloaded-faces
12247                         (memq article gnus-newsgroup-undownloaded)
12248                         (not (memq article gnus-newsgroup-cached)))))
12249     (let ((face (funcall (gnus-summary-highlight-line-0))))
12250       (unless (eq face (get-text-property beg 'face))
12251         (gnus-put-text-property-excluding-characters-with-faces
12252          beg (point-at-eol) 'face
12253          (setq face (if (boundp face) (symbol-value face) face)))
12254         (when gnus-summary-highlight-line-function
12255           (funcall gnus-summary-highlight-line-function article face))))))
12256
12257 (defun gnus-update-read-articles (group unread &optional compute)
12258   "Update the list of read articles in GROUP.
12259 UNREAD is a sorted list."
12260   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12261         (info (gnus-get-info group))
12262         (prev 1)
12263         read)
12264     (if (or (not info) (not active))
12265         ;; There is no info on this group if it was, in fact,
12266         ;; killed.  Gnus stores no information on killed groups, so
12267         ;; there's nothing to be done.
12268         ;; One could store the information somewhere temporarily,
12269         ;; perhaps...  Hmmm...
12270         ()
12271       ;; Remove any negative articles numbers.
12272       (while (and unread (< (car unread) 0))
12273         (setq unread (cdr unread)))
12274       ;; Remove any expired article numbers
12275       (while (and unread (< (car unread) (car active)))
12276         (setq unread (cdr unread)))
12277       ;; Compute the ranges of read articles by looking at the list of
12278       ;; unread articles.
12279       (while unread
12280         (when (/= (car unread) prev)
12281           (push (if (= prev (1- (car unread))) prev
12282                   (cons prev (1- (car unread))))
12283                 read))
12284         (setq prev (1+ (car unread)))
12285         (setq unread (cdr unread)))
12286       (when (<= prev (cdr active))
12287         (push (cons prev (cdr active)) read))
12288       (setq read (if (> (length read) 1) (nreverse read) read))
12289       (if compute
12290           read
12291         (save-excursion
12292           (let (setmarkundo)
12293             ;; Propagate the read marks to the backend.
12294             (when (and gnus-propagate-marks
12295                        (gnus-check-backend-function 'request-set-mark group))
12296               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12297                     (add (gnus-remove-from-range read (gnus-info-read info))))
12298                 (when (or add del)
12299                   (unless (gnus-check-group group)
12300                     (error "Can't open server for %s" group))
12301                   (gnus-request-set-mark
12302                    group (delq nil (list (if add (list add 'add '(read)))
12303                                          (if del (list del 'del '(read))))))
12304                   (setq setmarkundo
12305                         `(gnus-request-set-mark
12306                           ,group
12307                           ',(delq nil (list
12308                                        (if del (list del 'add '(read)))
12309                                        (if add (list add 'del '(read))))))))))
12310             (set-buffer gnus-group-buffer)
12311             (gnus-undo-register
12312               `(progn
12313                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12314                  (gnus-info-set-read ',info ',(gnus-info-read info))
12315                  (gnus-get-unread-articles-in-group ',info
12316                                                     (gnus-active ,group))
12317                  (gnus-group-update-group ,group t)
12318                  ,setmarkundo))))
12319         ;; Enter this list into the group info.
12320         (gnus-info-set-read info read)
12321         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12322         (gnus-get-unread-articles-in-group info (gnus-active group))
12323         t))))
12324
12325 (defun gnus-offer-save-summaries ()
12326   "Offer to save all active summary buffers."
12327   (let (buffers)
12328     ;; Go through all buffers and find all summaries.
12329     (dolist (buffer (buffer-list))
12330       (when (and (setq buffer (buffer-name buffer))
12331                  (string-match "Summary" buffer)
12332                  (with-current-buffer buffer
12333                    ;; We check that this is, indeed, a summary buffer.
12334                    (and (eq major-mode 'gnus-summary-mode)
12335                         ;; Also make sure this isn't bogus.
12336                         gnus-newsgroup-prepared
12337                         ;; Also make sure that this isn't a
12338                         ;; dead summary buffer.
12339                         (not gnus-dead-summary-mode))))
12340         (push buffer buffers)))
12341     ;; Go through all these summary buffers and offer to save them.
12342     (when buffers
12343       (save-excursion
12344         (map-y-or-n-p
12345          "Update summary buffer %s? "
12346          (lambda (buf)
12347            (switch-to-buffer buf)
12348            (gnus-summary-exit))
12349          buffers)))))
12350
12351 (defun gnus-summary-setup-default-charset ()
12352   "Setup newsgroup default charset."
12353   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12354       (setq gnus-newsgroup-charset nil)
12355     (let* ((ignored-charsets
12356             (or gnus-newsgroup-ephemeral-ignored-charsets
12357                 (append
12358                  (and gnus-newsgroup-name
12359                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12360                  gnus-newsgroup-ignored-charsets))))
12361       (setq gnus-newsgroup-charset
12362             (or gnus-newsgroup-ephemeral-charset
12363                 (and gnus-newsgroup-name
12364                      (gnus-parameter-charset gnus-newsgroup-name))
12365                 gnus-default-charset))
12366       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12367            ignored-charsets))))
12368
12369 ;;;
12370 ;;; Mime Commands
12371 ;;;
12372
12373 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12374   "Display the current article buffer fully MIME-buttonized.
12375 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12376 treated as multipart/mixed."
12377   (interactive "P")
12378   (require 'gnus-art)
12379   (let ((gnus-unbuttonized-mime-types nil)
12380         (gnus-mime-display-multipart-as-mixed show-all-parts))
12381     (gnus-summary-show-article)))
12382
12383 (defun gnus-summary-repair-multipart (article)
12384   "Add a Content-Type header to a multipart article without one."
12385   (interactive (list (gnus-summary-article-number)))
12386   (gnus-with-article article
12387     (message-narrow-to-head)
12388     (message-remove-header "Mime-Version")
12389     (goto-char (point-max))
12390     (insert "Mime-Version: 1.0\n")
12391     (widen)
12392     (when (search-forward "\n--" nil t)
12393       (let ((separator (buffer-substring (point) (point-at-eol))))
12394         (message-narrow-to-head)
12395         (message-remove-header "Content-Type")
12396         (goto-char (point-max))
12397         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12398                         separator))
12399         (widen))))
12400   (let (gnus-mark-article-hook)
12401     (gnus-summary-select-article t t nil article)))
12402
12403 (defun gnus-summary-toggle-display-buttonized ()
12404   "Toggle the buttonizing of the article buffer."
12405   (interactive)
12406   (require 'gnus-art)
12407   (if (setq gnus-inhibit-mime-unbuttonizing
12408             (not gnus-inhibit-mime-unbuttonizing))
12409       (let ((gnus-unbuttonized-mime-types nil))
12410         (gnus-summary-show-article))
12411     (gnus-summary-show-article)))
12412
12413 ;;;
12414 ;;; Generic summary marking commands
12415 ;;;
12416
12417 (defvar gnus-summary-marking-alist
12418   '((read gnus-del-mark "d")
12419     (unread gnus-unread-mark "u")
12420     (ticked gnus-ticked-mark "!")
12421     (dormant gnus-dormant-mark "?")
12422     (expirable gnus-expirable-mark "e"))
12423   "An alist of names/marks/keystrokes.")
12424
12425 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12426 (defvar gnus-summary-mark-map)
12427
12428 (defun gnus-summary-make-all-marking-commands ()
12429   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12430   (dolist (elem gnus-summary-marking-alist)
12431     (apply 'gnus-summary-make-marking-command elem)))
12432
12433 (defun gnus-summary-make-marking-command (name mark keystroke)
12434   (let ((map (make-sparse-keymap)))
12435     (define-key gnus-summary-generic-mark-map keystroke map)
12436     (dolist (lway `((next "next" next nil "n")
12437                     (next-unread "next unread" next t "N")
12438                     (prev "previous" prev nil "p")
12439                     (prev-unread "previous unread" prev t "P")
12440                     (nomove "" nil nil ,keystroke)))
12441       (let ((func (gnus-summary-make-marking-command-1
12442                    mark (car lway) lway name)))
12443         (setq func (eval func))
12444         (define-key map (nth 4 lway) func)))))
12445
12446 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12447   `(defun ,(intern
12448             (format "gnus-summary-put-mark-as-%s%s"
12449                     name (if (eq way 'nomove)
12450                              ""
12451                            (concat "-" (symbol-name way)))))
12452      (n)
12453      ,(format
12454        "Mark the current article as %s%s.
12455 If N, the prefix, then repeat N times.
12456 If N is negative, move in reverse order.
12457 The difference between N and the actual number of articles marked is
12458 returned."
12459        name (cadr lway))
12460      (interactive "p")
12461      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12462
12463 (defun gnus-summary-generic-mark (n mark move unread)
12464   "Mark N articles with MARK."
12465   (unless (eq major-mode 'gnus-summary-mode)
12466     (error "This command can only be used in the summary buffer"))
12467   (gnus-summary-show-thread)
12468   (let ((nummove
12469          (cond
12470           ((eq move 'next) 1)
12471           ((eq move 'prev) -1)
12472           (t 0))))
12473     (if (zerop nummove)
12474         (setq n 1)
12475       (when (< n 0)
12476         (setq n (abs n)
12477               nummove (* -1 nummove))))
12478     (while (and (> n 0)
12479                 (gnus-summary-mark-article nil mark)
12480                 (zerop (gnus-summary-next-subject nummove unread t)))
12481       (setq n (1- n)))
12482     (when (/= 0 n)
12483       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12484     (gnus-summary-recenter)
12485     (gnus-summary-position-point)
12486     (gnus-set-mode-line 'summary)
12487     n))
12488
12489 (defun gnus-summary-insert-articles (articles)
12490   (when (setq articles
12491               (gnus-sorted-difference articles
12492                                       (mapcar (lambda (h)
12493                                                 (mail-header-number h))
12494                                               gnus-newsgroup-headers)))
12495     (setq gnus-newsgroup-headers
12496           (gnus-merge 'list
12497                       gnus-newsgroup-headers
12498                       (gnus-fetch-headers articles)
12499                       'gnus-article-sort-by-number))
12500     ;; Suppress duplicates?
12501     (when gnus-suppress-duplicates
12502       (gnus-dup-suppress-articles))
12503
12504     (if (and gnus-fetch-old-headers
12505              (eq gnus-headers-retrieved-by 'nov))
12506         ;; We might want to build some more threads first.
12507         (if (eq gnus-fetch-old-headers 'invisible)
12508             (gnus-build-all-threads)
12509           (gnus-build-old-threads))
12510       ;; Mark the inserted articles that are unread as unread.
12511       (setq gnus-newsgroup-unreads
12512             (gnus-sorted-nunion
12513              gnus-newsgroup-unreads
12514              (gnus-sorted-nintersection
12515               (gnus-list-of-unread-articles gnus-newsgroup-name)
12516               articles)))
12517       ;; Mark the inserted articles as selected so that the information
12518       ;; of the marks having been changed by a user may be updated when
12519       ;; exiting this group.  See `gnus-summary-update-info'.
12520       (dolist (art articles)
12521         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12522     ;; Let the Gnus agent mark articles as read.
12523     (when gnus-agent
12524       (gnus-agent-get-undownloaded-list))
12525     ;; Remove list identifiers from subject
12526     (when gnus-list-identifiers
12527       (gnus-summary-remove-list-identifiers))
12528     ;; First and last article in this newsgroup.
12529     (when gnus-newsgroup-headers
12530       (setq gnus-newsgroup-begin
12531             (mail-header-number (car gnus-newsgroup-headers))
12532             gnus-newsgroup-end
12533             (mail-header-number
12534              (gnus-last-element gnus-newsgroup-headers))))
12535     (when gnus-use-scoring
12536       (gnus-possibly-score-headers))))
12537
12538 (defun gnus-summary-insert-old-articles (&optional all)
12539   "Insert all old articles in this group.
12540 If ALL is non-nil, already read articles become readable.
12541 If ALL is a number, fetch this number of articles."
12542   (interactive "P")
12543   (prog1
12544       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12545             older len)
12546         (setq older
12547               ;; Some nntp servers lie about their active range.  When
12548               ;; this happens, the active range can be in the millions.
12549               ;; Use a compressed range to avoid creating a huge list.
12550               (gnus-range-difference (list gnus-newsgroup-active) old))
12551         (setq len (gnus-range-length older))
12552         (cond
12553          ((null older) nil)
12554          ((numberp all)
12555           (if (< all len)
12556               (let ((older-range (nreverse older)))
12557                 (setq older nil)
12558
12559                 (while (> all 0)
12560                   (let* ((r (pop older-range))
12561                          (min (if (numberp r) r (car r)))
12562                          (max (if (numberp r) r (cdr r))))
12563                     (while (and (<= min max)
12564                                 (> all 0))
12565                       (push max older)
12566                       (setq all (1- all)
12567                             max (1- max))))))
12568             (setq older (gnus-uncompress-range older))))
12569          (all
12570           (setq older (gnus-uncompress-range older)))
12571          (t
12572           (when (and (numberp gnus-large-newsgroup)
12573                    (> len gnus-large-newsgroup))
12574               (let* ((cursor-in-echo-area nil)
12575                      (initial (gnus-parameter-large-newsgroup-initial
12576                                gnus-newsgroup-name))
12577                      (input
12578                       (read-string
12579                        (format
12580                         "How many articles from %s (%s %d): "
12581                         (gnus-group-decoded-name gnus-newsgroup-name)
12582                         (if initial "max" "default")
12583                         len)
12584                        (if initial
12585                            (cons (number-to-string initial)
12586                                  0)))))
12587                 (unless (string-match "^[ \t]*$" input)
12588                   (setq all (string-to-number input))
12589                   (if (< all len)
12590                       (let ((older-range (nreverse older)))
12591                         (setq older nil)
12592
12593                         (while (> all 0)
12594                           (let* ((r (pop older-range))
12595                                  (min (if (numberp r) r (car r)))
12596                                  (max (if (numberp r) r (cdr r))))
12597                             (while (and (<= min max)
12598                                         (> all 0))
12599                               (push max older)
12600                               (setq all (1- all)
12601                                     max (1- max))))))))))
12602           (setq older (gnus-uncompress-range older))))
12603         (if (not older)
12604             (message "No old news.")
12605           (gnus-summary-insert-articles older)
12606           (gnus-summary-limit (gnus-sorted-nunion old older))))
12607     (gnus-summary-position-point)))
12608
12609 (defun gnus-summary-insert-new-articles ()
12610   "Insert all new articles in this group."
12611   (interactive)
12612   (prog1
12613       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12614             (old-active gnus-newsgroup-active)
12615             (nnmail-fetched-sources (list t))
12616             i new)
12617         (setq gnus-newsgroup-active
12618               (gnus-activate-group gnus-newsgroup-name 'scan))
12619         (setq i (cdr gnus-newsgroup-active))
12620         (while (> i (cdr old-active))
12621           (push i new)
12622           (decf i))
12623         (if (not new)
12624             (message "No gnus is bad news")
12625           (gnus-summary-insert-articles new)
12626           (setq gnus-newsgroup-unreads
12627                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12628           (gnus-summary-limit (gnus-sorted-nunion old new))))
12629     (gnus-summary-position-point)))
12630
12631 ;;; Bookmark support for Gnus.
12632 (declare-function bookmark-make-record-default
12633                   "bookmark" (&optional no-file no-context posn))
12634 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12635 (declare-function bookmark-default-handler "bookmark" (bmk))
12636 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12637 (defvar bookmark-yank-point)
12638 (defvar bookmark-current-buffer)
12639
12640 (defun gnus-summary-bookmark-make-record ()
12641   "Make a bookmark entry for a Gnus summary buffer."
12642   (let (pos buf)
12643     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12644       (save-restriction              ; FIXME is it necessary to widen?
12645         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12646       (setq buf "art") ; We are recording bookmark from article buffer.
12647       (setq bookmark-yank-point (point))
12648       (setq bookmark-current-buffer (current-buffer))
12649       (gnus-article-show-summary))      ; Go back in summary buffer.
12650     ;; We are now recording bookmark from summary buffer.
12651     (unless buf (setq buf "sum"))
12652     (let* ((subject (elt (gnus-summary-article-header) 1))
12653            (grp     (car gnus-article-current))
12654            (art     (cdr gnus-article-current))
12655            (head    (gnus-summary-article-header art))
12656            (id      (mail-header-id head)))
12657       `(,subject
12658         ,@(condition-case nil
12659               (bookmark-make-record-default 'no-file 'no-context pos)
12660             (wrong-number-of-arguments
12661              (bookmark-make-record-default 'point-only)))
12662         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12663         (group . ,grp) (article . ,art)
12664         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12665
12666 ;;;###autoload
12667 (defun gnus-summary-bookmark-jump (bookmark)
12668   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12669 BOOKMARK is a bookmark name or a bookmark record."
12670   (let ((group    (bookmark-prop-get bookmark 'group))
12671         (article  (bookmark-prop-get bookmark 'article))
12672         (id       (bookmark-prop-get bookmark 'message-id))
12673         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12674     (gnus-fetch-group group (list article))
12675     (gnus-summary-insert-cached-articles)
12676     (gnus-summary-goto-article id nil 'force)
12677     ;; FIXME we have to wait article buffer is ready (only large buffer)
12678     ;; Is there a better solution to know that?
12679     ;; If we don't wait `bookmark-default-handler' will have no chance
12680     ;; to set position. However there is no error, just wrong pos.
12681     (sit-for 1)
12682     (when (string= buf "Gnus-art")
12683       (other-window 1))
12684     (bookmark-default-handler
12685      `(""
12686        (buffer . ,(current-buffer))
12687        . ,(bookmark-get-bookmark-record bookmark)))))
12688
12689 (gnus-summary-make-all-marking-commands)
12690
12691 (gnus-ems-redefine)
12692
12693 (provide 'gnus-sum)
12694
12695 (run-hooks 'gnus-sum-load-hook)
12696
12697 ;; Local Variables:
12698 ;; coding: iso-8859-1
12699 ;; End:
12700
12701 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12702 ;;; gnus-sum.el ends here