gnus-html.el, message.el: Add custom version to new variables; gnus-sum.el: Bump...
[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-kill-file-mark ?X
542   "*Mark used for articles killed by kill files."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-low-score-mark ?Y
547   "*Mark used for articles with a low score."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-catchup-mark ?C
552   "*Mark used for articles that are caught up."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-replied-mark ?A
557   "*Mark used for articles that have been replied to."
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-forwarded-mark ?F
562   "*Mark used for articles that have been forwarded."
563   :version "22.1"
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-recent-mark ?N
568   "*Mark used for articles that are recent."
569   :version "22.1"
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-cached-mark ?*
574   "*Mark used for articles that are in the cache."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-saved-mark ?S
579   "*Mark used for articles that have been saved."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-unseen-mark ?.
584   "*Mark used for articles that haven't been seen."
585   :version "22.1"
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-no-mark ?               ;Whitespace
590   "*Mark used for articles that have no other secondary mark."
591   :version "22.1"
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-ancient-mark ?O
596   "*Mark used for ancient articles."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-sparse-mark ?Q
601   "*Mark used for sparsely reffed articles."
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-canceled-mark ?G
606   "*Mark used for canceled articles."
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-duplicate-mark ?M
611   "*Mark used for duplicate articles."
612   :group 'gnus-summary-marks
613   :type 'character)
614
615 (defcustom gnus-undownloaded-mark ?-
616   "*Mark used for articles that weren't downloaded."
617   :version "22.1"
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-downloaded-mark ?+
622   "*Mark used for articles that were downloaded."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-downloadable-mark ?%
627   "*Mark used for articles that are to be downloaded."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-unsendable-mark ?=
632   "*Mark used for articles that won't be sent."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-score-over-mark ?+
637   "*Score mark used for articles with high scores."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-score-below-mark ?-
642   "*Score mark used for articles with low scores."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-empty-thread-mark ?     ;Whitespace
647   "*There is no thread under the article."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-not-empty-thread-mark ?=
652   "*There is a thread under the article."
653   :group 'gnus-summary-marks
654   :type 'character)
655
656 (defcustom gnus-view-pseudo-asynchronously nil
657   "*If non-nil, Gnus will view pseudo-articles asynchronously."
658   :group 'gnus-extract-view
659   :type 'boolean)
660
661 (defcustom gnus-auto-expirable-marks
662   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
663         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
664         gnus-duplicate-mark)
665   "*The list of marks converted into expiration if a group is auto-expirable."
666   :version "24.1"
667   :group 'gnus-summary
668   :type '(repeat character))
669
670 (defcustom gnus-inhibit-user-auto-expire t
671   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
672   :version "21.1"
673   :group 'gnus-summary
674   :type 'boolean)
675
676 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
677   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
678 If nil, the expirable marks will be unchanged except that the marks
679 will be removed when copying or moving articles to a group that has
680 not turned auto-expire on.  If non-nil, articles that have been read
681 will be marked as expirable when being copied or moved to a group in
682 which auto-expire is turned on."
683   :version "23.2"
684   :type 'boolean
685   :group 'gnus-summary-marks)
686
687 (defcustom gnus-view-pseudos nil
688   "*If `automatic', pseudo-articles will be viewed automatically.
689 If `not-confirm', pseudos will be viewed automatically, and the user
690 will not be asked to confirm the command."
691   :group 'gnus-extract-view
692   :type '(choice (const :tag "off" nil)
693                  (const automatic)
694                  (const not-confirm)))
695
696 (defcustom gnus-view-pseudos-separately t
697   "*If non-nil, one pseudo-article will be created for each file to be viewed.
698 If nil, all files that use the same viewing command will be given as a
699 list of parameters to that command."
700   :group 'gnus-extract-view
701   :type 'boolean)
702
703 (defcustom gnus-insert-pseudo-articles t
704   "*If non-nil, insert pseudo-articles when decoding articles."
705   :group 'gnus-extract-view
706   :type 'boolean)
707
708 (defcustom gnus-summary-dummy-line-format
709   "   %(:                             :%) %S\n"
710   "*The format specification for the dummy roots in the summary buffer.
711 It works along the same lines as a normal formatting string,
712 with some simple extensions.
713
714 %S  The subject
715
716 General format specifiers can also be used.
717 See `(gnus)Formatting Variables'."
718   :link '(custom-manual "(gnus)Formatting Variables")
719   :group 'gnus-threading
720   :type 'string)
721
722 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
723   "*The format specification for the summary mode line.
724 It works along the same lines as a normal formatting string,
725 with some simple extensions:
726
727 %G  Group name
728 %p  Unprefixed group name
729 %A  Current article number
730 %z  Current article score
731 %V  Gnus version
732 %U  Number of unread articles in the group
733 %e  Number of unselected articles in the group
734 %Z  A string with unread/unselected article counts
735 %g  Shortish group name
736 %S  Subject of the current article
737 %u  User-defined spec
738 %s  Current score file name
739 %d  Number of dormant articles
740 %r  Number of articles that have been marked as read in this session
741 %E  Number of articles expunged by the score files"
742   :group 'gnus-summary-format
743   :type 'string)
744
745 (defcustom gnus-list-identifiers nil
746   "Regexp that matches list identifiers to be removed from subject.
747 This can also be a list of regexps."
748   :version "21.1"
749   :group 'gnus-summary-format
750   :group 'gnus-article-hiding
751   :type '(choice (const :tag "none" nil)
752                  (regexp :value ".*")
753                  (repeat :value (".*") regexp)))
754
755 (defcustom gnus-summary-mark-below 0
756   "*Mark all articles with a score below this variable as read.
757 This variable is local to each summary buffer and usually set by the
758 score file."
759   :group 'gnus-score-default
760   :type 'integer)
761
762 (defun gnus-widget-reversible-match (widget value)
763   "Ignoring WIDGET, convert VALUE to internal form.
764 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
765   ;; (debug value)
766   (or (symbolp value)
767       (and (listp value)
768            (eq (length value) 2)
769            (eq (nth 0 value) 'not)
770            (symbolp (nth 1 value)))))
771
772 (defun gnus-widget-reversible-to-internal (widget value)
773   "Ignoring WIDGET, convert VALUE to internal form.
774 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
775 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
776   ;; (debug value)
777   (if (atom value)
778       (list value nil)
779     (list (nth 1 value) t)))
780
781 (defun gnus-widget-reversible-to-external (widget value)
782   "Ignoring WIDGET, convert VALUE to external form.
783 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
784 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
785   ;; (debug value)
786   (if (nth 1 value)
787       (list 'not (nth 0 value))
788     (nth 0 value)))
789
790 (define-widget 'gnus-widget-reversible 'group
791   "A `group' that convert values."
792   :match 'gnus-widget-reversible-match
793   :value-to-internal 'gnus-widget-reversible-to-internal
794   :value-to-external 'gnus-widget-reversible-to-external)
795
796 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
797   "*List of functions used for sorting articles in the summary buffer.
798
799 Each function takes two articles and returns non-nil if the first
800 article should be sorted before the other.  If you use more than one
801 function, the primary sort function should be the last.  You should
802 probably always include `gnus-article-sort-by-number' in the list of
803 sorting functions -- preferably first.  Also note that sorting by date
804 is often much slower than sorting by number, and the sorting order is
805 very similar.  (Sorting by date means sorting by the time the message
806 was sent, sorting by number means sorting by arrival time.)
807
808 Each item can also be a list `(not F)' where F is a function;
809 this reverses the sort order.
810
811 Ready-made functions include `gnus-article-sort-by-number',
812 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
813 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
814 and `gnus-article-sort-by-score'.
815
816 When threading is turned on, the variable `gnus-thread-sort-functions'
817 controls how articles are sorted."
818   :group 'gnus-summary-sort
819   :type '(repeat (gnus-widget-reversible
820                   (choice (function-item gnus-article-sort-by-number)
821                           (function-item gnus-article-sort-by-author)
822                           (function-item gnus-article-sort-by-subject)
823                           (function-item gnus-article-sort-by-date)
824                           (function-item gnus-article-sort-by-score)
825                           (function-item gnus-article-sort-by-random)
826                           (function :tag "other"))
827                   (boolean :tag "Reverse order"))))
828
829
830 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
831   "*List of functions used for sorting threads in the summary buffer.
832 By default, threads are sorted by article number.
833
834 Each function takes two threads and returns non-nil if the first
835 thread should be sorted before the other.  If you use more than one
836 function, the primary sort function should be the last.  You should
837 probably always include `gnus-thread-sort-by-number' in the list of
838 sorting functions -- preferably first.  Also note that sorting by date
839 is often much slower than sorting by number, and the sorting order is
840 very similar.  (Sorting by date means sorting by the time the message
841 was sent, sorting by number means sorting by arrival time.)
842
843 Each list item can also be a list `(not F)' where F is a
844 function; this specifies reversed sort order.
845
846 Ready-made functions include `gnus-thread-sort-by-number',
847 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
848 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
849 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
850 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
851 and `gnus-thread-sort-by-total-score' (see
852 `gnus-thread-score-function').
853
854 When threading is turned off, the variable
855 `gnus-article-sort-functions' controls how articles are sorted."
856   :group 'gnus-summary-sort
857   :type '(repeat
858           (gnus-widget-reversible
859            (choice (function-item gnus-thread-sort-by-number)
860                    (function-item gnus-thread-sort-by-author)
861                    (function-item gnus-thread-sort-by-recipient)
862                    (function-item gnus-thread-sort-by-subject)
863                    (function-item gnus-thread-sort-by-date)
864                    (function-item gnus-thread-sort-by-score)
865                    (function-item gnus-thread-sort-by-most-recent-number)
866                    (function-item gnus-thread-sort-by-most-recent-date)
867                    (function-item gnus-thread-sort-by-random)
868                    (function-item gnus-thread-sort-by-total-score)
869                    (function :tag "other"))
870            (boolean :tag "Reverse order"))))
871
872 (defcustom gnus-thread-score-function '+
873   "*Function used for calculating the total score of a thread.
874
875 The function is called with the scores of the article and each
876 subthread and should then return the score of the thread.
877
878 Some functions you can use are `+', `max', or `min'."
879   :group 'gnus-summary-sort
880   :type 'function)
881
882 (defcustom gnus-summary-expunge-below nil
883   "All articles that have a score less than this variable will be expunged.
884 This variable is local to the summary buffers."
885   :group 'gnus-score-default
886   :type '(choice (const :tag "off" nil)
887                  integer))
888
889 (defcustom gnus-thread-expunge-below nil
890   "All threads that have a total score less than this variable will be expunged.
891 See `gnus-thread-score-function' for en explanation of what a
892 \"thread score\" is.
893
894 This variable is local to the summary buffers."
895   :group 'gnus-threading
896   :group 'gnus-score-default
897   :type '(choice (const :tag "off" nil)
898                  integer))
899
900 (defcustom gnus-summary-mode-hook nil
901   "*A hook for Gnus summary mode.
902 This hook is run before any variables are set in the summary buffer."
903   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
904   :group 'gnus-summary-various
905   :type 'hook)
906
907 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
908 (when (featurep 'xemacs)
909   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
910   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
911   (add-hook 'gnus-summary-mode-hook
912             'gnus-xmas-switch-horizontal-scrollbar-off))
913
914 (defcustom gnus-summary-menu-hook nil
915   "*Hook run after the creation of the summary mode menu."
916   :group 'gnus-summary-visual
917   :type 'hook)
918
919 (defcustom gnus-summary-exit-hook nil
920   "*A hook called on exit from the summary buffer.
921 It will be called with point in the group buffer."
922   :group 'gnus-summary-exit
923   :type 'hook)
924
925 (defcustom gnus-summary-prepare-hook nil
926   "*A hook called after the summary buffer has been generated.
927 If you want to modify the summary buffer, you can use this hook."
928   :group 'gnus-summary-various
929   :type 'hook)
930
931 (defcustom gnus-summary-prepared-hook nil
932   "*A hook called as the last thing after the summary buffer has been generated."
933   :group 'gnus-summary-various
934   :type 'hook)
935
936 (defcustom gnus-summary-generate-hook nil
937   "*A hook run just before generating the summary buffer.
938 This hook is commonly used to customize threading variables and the
939 like."
940   :group 'gnus-summary-various
941   :type 'hook)
942
943 (defcustom gnus-select-group-hook nil
944   "*A hook called when a newsgroup is selected.
945
946 If you'd like to simplify subjects like the
947 `gnus-summary-next-same-subject' command does, you can use the
948 following hook:
949
950  (add-hook gnus-select-group-hook
951            (lambda ()
952              (mapcar (lambda (header)
953                        (mail-header-set-subject
954                         header
955                         (gnus-simplify-subject
956                          (mail-header-subject header) 're-only)))
957                      gnus-newsgroup-headers)))"
958   :group 'gnus-group-select
959   :type 'hook)
960
961 (defcustom gnus-select-article-hook nil
962   "*A hook called when an article is selected."
963   :group 'gnus-summary-choose
964   :options '(gnus-agent-fetch-selected-article)
965   :type 'hook)
966
967 (defcustom gnus-visual-mark-article-hook
968   (list 'gnus-highlight-selected-summary)
969   "*Hook run after selecting an article in the summary buffer.
970 It is meant to be used for highlighting the article in some way.  It
971 is not run if `gnus-visual' is nil."
972   :group 'gnus-summary-visual
973   :type 'hook)
974
975 (defcustom gnus-parse-headers-hook nil
976   "*A hook called before parsing the headers."
977   :group 'gnus-various
978   :type 'hook)
979
980 (defcustom gnus-exit-group-hook nil
981   "*A hook called when exiting summary mode.
982 This hook is not called from the non-updating exit commands like `Q'."
983   :group 'gnus-various
984   :type 'hook)
985
986 (defcustom gnus-summary-update-hook
987   (list 'gnus-summary-highlight-line)
988   "*A hook called when a summary line is changed.
989 The hook will not be called if `gnus-visual' is nil.
990
991 The default function `gnus-summary-highlight-line' will
992 highlight the line according to the `gnus-summary-highlight'
993 variable."
994   :group 'gnus-summary-visual
995   :type 'hook)
996
997 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
998   "*A hook called when an article is selected for the first time.
999 The hook is intended to mark an article as read (or unread)
1000 automatically when it is selected."
1001   :group 'gnus-summary-choose
1002   :type 'hook)
1003
1004 (defcustom gnus-group-no-more-groups-hook nil
1005   "*A hook run when returning to group mode having no more (unread) groups."
1006   :group 'gnus-group-select
1007   :type 'hook)
1008
1009 (defcustom gnus-ps-print-hook nil
1010   "*A hook run before ps-printing something from Gnus."
1011   :group 'gnus-summary
1012   :type 'hook)
1013
1014 (defcustom gnus-summary-article-move-hook nil
1015   "*A hook called after an article is moved, copied, respooled, or crossposted."
1016   :version "22.1"
1017   :group 'gnus-summary
1018   :type 'hook)
1019
1020 (defcustom gnus-summary-article-delete-hook nil
1021   "*A hook called after an article is deleted."
1022   :version "22.1"
1023   :group 'gnus-summary
1024   :type 'hook)
1025
1026 (defcustom gnus-summary-article-expire-hook nil
1027   "*A hook called after an article is expired."
1028   :version "22.1"
1029   :group 'gnus-summary
1030   :type 'hook)
1031
1032 (defcustom gnus-summary-display-arrow
1033   (and (fboundp 'display-graphic-p)
1034        (display-graphic-p))
1035   "*If non-nil, display an arrow highlighting the current article."
1036   :version "22.1"
1037   :group 'gnus-summary
1038   :type 'boolean)
1039
1040 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1041   "Face used for highlighting the current article in the summary buffer."
1042   :group 'gnus-summary-visual
1043   :type 'face)
1044
1045 (defvar gnus-tmp-downloaded nil)
1046
1047 (defcustom gnus-summary-highlight
1048   '(((eq mark gnus-canceled-mark)
1049      . gnus-summary-cancelled)
1050     ((and uncached (> score default-high))
1051      . gnus-summary-high-undownloaded)
1052     ((and uncached (< score default-low))
1053      . gnus-summary-low-undownloaded)
1054     (uncached
1055      . gnus-summary-normal-undownloaded)
1056     ((and (> score default-high)
1057           (or (eq mark gnus-dormant-mark)
1058               (eq mark gnus-ticked-mark)))
1059      . gnus-summary-high-ticked)
1060     ((and (< score default-low)
1061           (or (eq mark gnus-dormant-mark)
1062               (eq mark gnus-ticked-mark)))
1063      . gnus-summary-low-ticked)
1064     ((or (eq mark gnus-dormant-mark)
1065          (eq mark gnus-ticked-mark))
1066      . gnus-summary-normal-ticked)
1067     ((and (> score default-high) (eq mark gnus-ancient-mark))
1068      . gnus-summary-high-ancient)
1069     ((and (< score default-low) (eq mark gnus-ancient-mark))
1070      . gnus-summary-low-ancient)
1071     ((eq mark gnus-ancient-mark)
1072      . gnus-summary-normal-ancient)
1073     ((and (> score default-high) (eq mark gnus-unread-mark))
1074      . gnus-summary-high-unread)
1075     ((and (< score default-low) (eq mark gnus-unread-mark))
1076      . gnus-summary-low-unread)
1077     ((eq mark gnus-unread-mark)
1078      . gnus-summary-normal-unread)
1079     ((> score default-high)
1080      . gnus-summary-high-read)
1081     ((< score default-low)
1082      . gnus-summary-low-read)
1083     (t
1084      . gnus-summary-normal-read))
1085   "*Controls the highlighting of summary buffer lines.
1086
1087 A list of (FORM . FACE) pairs.  When deciding how a particular
1088 summary line should be displayed, each form is evaluated.  The content
1089 of the face field after the first true form is used.  You can change
1090 how those summary lines are displayed, by editing the face field.
1091
1092 You can use the following variables in the FORM field.
1093
1094 score:        The article's score.
1095 default:      The default article score.
1096 default-high: The default score for high scored articles.
1097 default-low:  The default score for low scored articles.
1098 below:        The score below which articles are automatically marked as read.
1099 mark:         The article's mark.
1100 uncached:     Non-nil if the article is uncached."
1101   :group 'gnus-summary-visual
1102   :type '(repeat (cons (sexp :tag "Form" nil)
1103                        face)))
1104 (put 'gnus-summary-highlight 'risky-local-variable t)
1105
1106 (defcustom gnus-alter-header-function nil
1107   "Function called to allow alteration of article header structures.
1108 The function is called with one parameter, the article header vector,
1109 which it may alter in any way."
1110   :type '(choice (const :tag "None" nil)
1111                  function)
1112   :group 'gnus-summary)
1113
1114 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1115   "Function used to decode a string with encoded words.")
1116
1117 (defvar gnus-decode-encoded-address-function
1118   'mail-decode-encoded-address-string
1119   "Function used to decode addresses with encoded words.")
1120
1121 (defcustom gnus-extra-headers '(To Newsgroups)
1122   "*Extra headers to parse."
1123   :version "21.1"
1124   :group 'gnus-summary
1125   :type '(repeat symbol))
1126
1127 (defcustom gnus-ignored-from-addresses
1128   (and user-mail-address
1129        (not (string= user-mail-address ""))
1130        (regexp-quote user-mail-address))
1131   "*From headers that may be suppressed in favor of To headers.
1132 This can be a regexp or a list of regexps."
1133   :version "21.1"
1134   :group 'gnus-summary
1135   :type '(choice regexp
1136                  (repeat :tag "Regexp List" regexp)))
1137
1138 (defsubst gnus-ignored-from-addresses ()
1139   (gmm-regexp-concat gnus-ignored-from-addresses))
1140
1141 (defcustom gnus-summary-to-prefix "-> "
1142   "*String prefixed to the To field in the summary line when
1143 using `gnus-ignored-from-addresses'."
1144   :version "22.1"
1145   :group 'gnus-summary
1146   :type 'string)
1147
1148 (defcustom gnus-summary-newsgroup-prefix "=> "
1149   "*String prefixed to the Newsgroup field in the summary
1150 line when using `gnus-ignored-from-addresses'."
1151   :version "22.1"
1152   :group 'gnus-summary
1153   :type 'string)
1154
1155 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1156   "List of charsets that should be ignored.
1157 When these charsets are used in the \"charset\" parameter, the
1158 default charset will be used instead."
1159   :version "21.1"
1160   :type '(repeat symbol)
1161   :group 'gnus-charset)
1162
1163 (defcustom gnus-newsgroup-maximum-articles nil
1164   "The maximum number of articles a newsgroup.
1165 If this is a number, old articles in a newsgroup exceeding this number
1166 are silently ignored.  If it is nil, no article is ignored.  Note that
1167 setting this variable to a number might prevent you from reading very
1168 old articles."
1169   :group 'gnus-group-select
1170   :version "22.2"
1171   :type '(choice (const :tag "No limit" nil)
1172                  integer))
1173
1174 (gnus-define-group-parameter
1175  ignored-charsets
1176  :type list
1177  :function-document
1178  "Return the ignored charsets of GROUP."
1179  :variable gnus-group-ignored-charsets-alist
1180  :variable-default
1181  '(("alt\\.chinese\\.text" iso-8859-1))
1182  :variable-document
1183  "Alist of regexps (to match group names) and charsets that should be ignored.
1184 When these charsets are used in the \"charset\" parameter, the
1185 default charset will be used instead."
1186  :variable-group gnus-charset
1187  :variable-type '(repeat (cons (regexp :tag "Group")
1188                                (repeat symbol)))
1189  :parameter-type '(choice :tag "Ignored charsets"
1190                           :value nil
1191                           (repeat (symbol)))
1192  :parameter-document       "\
1193 List of charsets that should be ignored.
1194
1195 When these charsets are used in the \"charset\" parameter, the
1196 default charset will be used instead.")
1197
1198 (defcustom gnus-group-highlight-words-alist nil
1199   "Alist of group regexps and highlight regexps.
1200 This variable uses the same syntax as `gnus-emphasis-alist'."
1201   :version "21.1"
1202   :type '(repeat (cons (regexp :tag "Group")
1203                        (repeat (list (regexp :tag "Highlight regexp")
1204                                      (number :tag "Group for entire word" 0)
1205                                      (number :tag "Group for displayed part" 0)
1206                                      (symbol :tag "Face"
1207                                              gnus-emphasis-highlight-words)))))
1208   :group 'gnus-summary-visual)
1209
1210 (defcustom gnus-summary-show-article-charset-alist
1211   nil
1212   "Alist of number and charset.
1213 The article will be shown with the charset corresponding to the
1214 numbered argument.
1215 For example: ((1 . cn-gb-2312) (2 . big5))."
1216   :version "21.1"
1217   :type '(repeat (cons (number :tag "Argument" 1)
1218                        (symbol :tag "Charset")))
1219   :group 'gnus-charset)
1220
1221 (defcustom gnus-preserve-marks t
1222   "Whether marks are preserved when moving, copying and respooling messages."
1223   :version "21.1"
1224   :type 'boolean
1225   :group 'gnus-summary-marks)
1226
1227 (defcustom gnus-propagate-marks t
1228   "If non-nil, do not propagate marks to the backends."
1229   :version "23.1" ;; No Gnus
1230   :type 'boolean
1231   :group 'gnus-summary-marks)
1232
1233 (defcustom gnus-alter-articles-to-read-function nil
1234   "Function to be called to alter the list of articles to be selected."
1235   :type '(choice (const nil) function)
1236   :group 'gnus-summary)
1237
1238 (defcustom gnus-orphan-score nil
1239   "*All orphans get this score added.  Set in the score file."
1240   :group 'gnus-score-default
1241   :type '(choice (const nil)
1242                  integer))
1243
1244 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1245   "*A regexp to match MIME parts when saving multiple parts of a
1246 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1247 This regexp will be used by default when prompting the user for which
1248 type of files to save."
1249   :group 'gnus-summary
1250   :type 'regexp)
1251
1252 (defcustom gnus-read-all-available-headers nil
1253   "Whether Gnus should parse all headers made available to it.
1254 This is mostly relevant for slow back ends where the user may
1255 wish to widen the summary buffer to include all headers
1256 that were fetched."
1257   :version "22.1"
1258   :group 'gnus-summary
1259   :type '(choice boolean regexp))
1260
1261 (defcustom gnus-summary-pipe-output-default-command nil
1262   "Command (and optional arguments) used to pipe article to subprocess.
1263 This will be used as the default command if it is non-nil.  The value
1264 will be updated if you modify it when executing the command
1265 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1266   :version "23.1" ;; No Gnus
1267   :group 'gnus-summary
1268   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1269
1270 (defcustom gnus-summary-muttprint-program "muttprint"
1271   "Command (and optional arguments) used to run Muttprint.
1272 The value will be updated if you modify it when executing the command
1273 `gnus-summary-muttprint'."
1274   :version "22.1"
1275   :group 'gnus-summary
1276   :type 'string)
1277
1278 (defcustom gnus-article-loose-mime t
1279   "If non-nil, don't require MIME-Version header.
1280 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1281 supply the MIME-Version header or deliberately strip it from the mail.
1282 If non-nil (the default), Gnus will treat some articles as MIME
1283 even if the MIME-Version header is missing."
1284   :version "22.1"
1285   :type 'boolean
1286   :group 'gnus-article-mime)
1287
1288 (defcustom gnus-article-emulate-mime t
1289   "If non-nil, use MIME emulation for uuencode and the like.
1290 This means that Gnus will search message bodies for text that look
1291 like uuencoded bits, yEncoded bits, and so on, and present that using
1292 the normal Gnus MIME machinery."
1293   :version "22.1"
1294   :type 'boolean
1295   :group 'gnus-article-mime)
1296
1297 ;;; Internal variables
1298
1299 (defvar gnus-summary-display-cache nil)
1300 (defvar gnus-article-mime-handles nil)
1301 (defvar gnus-article-decoded-p nil)
1302 (defvar gnus-article-charset nil)
1303 (defvar gnus-article-ignored-charsets nil)
1304 (defvar gnus-scores-exclude-files nil)
1305 (defvar gnus-page-broken nil)
1306
1307 (defvar gnus-original-article nil)
1308 (defvar gnus-article-internal-prepare-hook nil)
1309 (defvar gnus-newsgroup-process-stack nil)
1310
1311 (defvar gnus-thread-indent-array nil)
1312 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1313 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1314   "Function called to sort the articles within a thread after it has been gathered together.")
1315
1316 (defvar gnus-summary-save-parts-type-history nil)
1317 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1318
1319 ;; Avoid highlighting in kill files.
1320 (defvar gnus-summary-inhibit-highlight nil)
1321 (defvar gnus-newsgroup-selected-overlay nil)
1322 (defvar gnus-inhibit-limiting nil)
1323 (defvar gnus-newsgroup-adaptive-score-file nil)
1324 (defvar gnus-current-score-file nil)
1325 (defvar gnus-current-move-group nil)
1326 (defvar gnus-current-copy-group nil)
1327 (defvar gnus-current-crosspost-group nil)
1328 (defvar gnus-newsgroup-display nil)
1329
1330 (defvar gnus-newsgroup-dependencies nil)
1331 (defvar gnus-newsgroup-adaptive nil)
1332 (defvar gnus-summary-display-article-function nil)
1333 (defvar gnus-summary-highlight-line-function nil
1334   "Function called after highlighting a summary line.")
1335
1336 (defvar gnus-summary-line-format-alist
1337   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1338     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1339     (?s gnus-tmp-subject-or-nil ?s)
1340     (?n gnus-tmp-name ?s)
1341     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1342         ?s)
1343     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1344             gnus-tmp-from) ?s)
1345     (?F gnus-tmp-from ?s)
1346     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1347     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1348     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1349     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1350     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1351     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1352     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1353     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1354     (?L gnus-tmp-lines ?s)
1355     (?O gnus-tmp-downloaded ?c)
1356     (?I gnus-tmp-indentation ?s)
1357     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1358     (?R gnus-tmp-replied ?c)
1359     (?\[ gnus-tmp-opening-bracket ?c)
1360     (?\] gnus-tmp-closing-bracket ?c)
1361     (?\> (make-string gnus-tmp-level ? ) ?s)
1362     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1363     (?i gnus-tmp-score ?d)
1364     (?z gnus-tmp-score-char ?c)
1365     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1366     (?U gnus-tmp-unread ?c)
1367     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1368         ?s)
1369     (?t (gnus-summary-number-of-articles-in-thread
1370          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1371         ?d)
1372     (?e (gnus-summary-number-of-articles-in-thread
1373          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1374         ?c)
1375     (?u gnus-tmp-user-defined ?s)
1376     (?P (gnus-pick-line-number) ?d)
1377     (?B gnus-tmp-thread-tree-header-string ?s)
1378     (user-date (gnus-user-date
1379                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1380   "An alist of format specifications that can appear in summary lines.
1381 These are paired with what variables they correspond with, along with
1382 the type of the variable (string, integer, character, etc).")
1383
1384 (defvar gnus-summary-dummy-line-format-alist
1385   `((?S gnus-tmp-subject ?s)
1386     (?N gnus-tmp-number ?d)
1387     (?u gnus-tmp-user-defined ?s)))
1388
1389 (defvar gnus-summary-mode-line-format-alist
1390   `((?G gnus-tmp-group-name ?s)
1391     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1392     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1393     (?A gnus-tmp-article-number ?d)
1394     (?Z gnus-tmp-unread-and-unselected ?s)
1395     (?V gnus-version ?s)
1396     (?U gnus-tmp-unread-and-unticked ?d)
1397     (?S gnus-tmp-subject ?s)
1398     (?e gnus-tmp-unselected ?d)
1399     (?u gnus-tmp-user-defined ?s)
1400     (?d (length gnus-newsgroup-dormant) ?d)
1401     (?t (length gnus-newsgroup-marked) ?d)
1402     (?h (length gnus-newsgroup-spam-marked) ?d)
1403     (?r (length gnus-newsgroup-reads) ?d)
1404     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1405     (?E gnus-newsgroup-expunged-tally ?d)
1406     (?s (gnus-current-score-file-nondirectory) ?s)))
1407
1408 ;; This is here rather than in gnus-art for compilation reasons.
1409 (defvar gnus-article-mode-line-format-alist
1410   (nconc '((?w (gnus-article-wash-status) ?s)
1411            (?m (gnus-article-mime-part-status) ?s))
1412          gnus-summary-mode-line-format-alist))
1413
1414 (defvar gnus-last-search-regexp nil
1415   "Default regexp for article search command.")
1416
1417 (defvar gnus-last-shell-command nil
1418   "Default shell command on article.")
1419
1420 (defvar gnus-newsgroup-agentized nil
1421   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1422 (defvar gnus-newsgroup-begin nil)
1423 (defvar gnus-newsgroup-end nil)
1424 (defvar gnus-newsgroup-last-rmail nil)
1425 (defvar gnus-newsgroup-last-mail nil)
1426 (defvar gnus-newsgroup-last-folder nil)
1427 (defvar gnus-newsgroup-last-file nil)
1428 (defvar gnus-newsgroup-last-directory nil)
1429 (defvar gnus-newsgroup-auto-expire nil)
1430 (defvar gnus-newsgroup-active nil)
1431
1432 (defvar gnus-newsgroup-data nil)
1433 (defvar gnus-newsgroup-data-reverse nil)
1434 (defvar gnus-newsgroup-limit nil)
1435 (defvar gnus-newsgroup-limits nil)
1436 (defvar gnus-summary-use-undownloaded-faces nil)
1437
1438 (defvar gnus-newsgroup-unreads nil
1439   "Sorted list of unread articles in the current newsgroup.")
1440
1441 (defvar gnus-newsgroup-unselected nil
1442   "Sorted list of unselected unread articles in the current newsgroup.")
1443
1444 (defvar gnus-newsgroup-reads nil
1445   "Alist of read articles and article marks in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-expunged-tally nil)
1448
1449 (defvar gnus-newsgroup-marked nil
1450   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1451
1452 (defvar gnus-newsgroup-spam-marked nil
1453   "List of ranges of articles that have been marked as spam.")
1454
1455 (defvar gnus-newsgroup-killed nil
1456   "List of ranges of articles that have been through the scoring process.")
1457
1458 (defvar gnus-newsgroup-cached nil
1459   "Sorted list of articles that come from the article cache.")
1460
1461 (defvar gnus-newsgroup-saved nil
1462   "List of articles that have been saved.")
1463
1464 (defvar gnus-newsgroup-kill-headers nil)
1465
1466 (defvar gnus-newsgroup-replied nil
1467   "List of articles that have been replied to in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-forwarded nil
1470   "List of articles that have been forwarded in the current newsgroup.")
1471
1472 (defvar gnus-newsgroup-recent nil
1473   "List of articles that have are recent in the current newsgroup.")
1474
1475 (defvar gnus-newsgroup-expirable nil
1476   "Sorted list of articles in the current newsgroup that can be expired.")
1477
1478 (defvar gnus-newsgroup-processable nil
1479   "List of articles in the current newsgroup that can be processed.")
1480
1481 (defvar gnus-newsgroup-downloadable nil
1482   "Sorted list of articles in the current newsgroup that can be processed.")
1483
1484 (defvar gnus-newsgroup-unfetched nil
1485   "Sorted list of articles in the current newsgroup whose headers have
1486 not been fetched into the agent.
1487
1488 This list will always be a subset of gnus-newsgroup-undownloaded.")
1489
1490 (defvar gnus-newsgroup-undownloaded nil
1491   "List of articles in the current newsgroup that haven't been downloaded.")
1492
1493 (defvar gnus-newsgroup-unsendable nil
1494   "List of articles in the current newsgroup that won't be sent.")
1495
1496 (defvar gnus-newsgroup-bookmarks nil
1497   "List of articles in the current newsgroup that have bookmarks.")
1498
1499 (defvar gnus-newsgroup-dormant nil
1500   "Sorted list of dormant articles in the current newsgroup.")
1501
1502 (defvar gnus-newsgroup-unseen nil
1503   "List of unseen articles in the current newsgroup.")
1504
1505 (defvar gnus-newsgroup-seen nil
1506   "Range of seen articles in the current newsgroup.")
1507
1508 (defvar gnus-newsgroup-articles nil
1509   "List of articles in the current newsgroup.")
1510
1511 (defvar gnus-newsgroup-scored nil
1512   "List of scored articles in the current newsgroup.")
1513
1514 (defvar gnus-newsgroup-headers nil
1515   "List of article headers in the current newsgroup.")
1516
1517 (defvar gnus-newsgroup-threads nil)
1518
1519 (defvar gnus-newsgroup-prepared nil
1520   "Whether the current group has been prepared properly.")
1521
1522 (defvar gnus-newsgroup-ancient nil
1523   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1524
1525 (defvar gnus-newsgroup-sparse nil)
1526
1527 (defvar gnus-current-article nil)
1528 (defvar gnus-article-current nil)
1529 (defvar gnus-current-headers nil)
1530 (defvar gnus-have-all-headers nil)
1531 (defvar gnus-last-article nil)
1532 (defvar gnus-newsgroup-history nil)
1533 (defvar gnus-newsgroup-charset nil)
1534 (defvar gnus-newsgroup-ephemeral-charset nil)
1535 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1536
1537 (defvar gnus-article-before-search nil)
1538
1539 (defvar gnus-summary-local-variables
1540   '(gnus-newsgroup-name
1541     gnus-newsgroup-begin gnus-newsgroup-end
1542     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1543     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1544     gnus-newsgroup-last-directory
1545     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1546     gnus-newsgroup-unselected gnus-newsgroup-marked
1547     gnus-newsgroup-spam-marked
1548     gnus-newsgroup-reads gnus-newsgroup-saved
1549     gnus-newsgroup-replied gnus-newsgroup-forwarded
1550     gnus-newsgroup-recent
1551     gnus-newsgroup-expirable
1552     gnus-newsgroup-processable gnus-newsgroup-killed
1553     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1554     gnus-newsgroup-unfetched
1555     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1556     gnus-newsgroup-seen gnus-newsgroup-articles
1557     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1558     gnus-newsgroup-headers gnus-newsgroup-threads
1559     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1560     gnus-current-article gnus-current-headers gnus-have-all-headers
1561     gnus-last-article gnus-article-internal-prepare-hook
1562     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1563     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1564     gnus-thread-expunge-below
1565     gnus-score-alist gnus-current-score-file
1566     (gnus-summary-expunge-below . global)
1567     (gnus-summary-mark-below . global)
1568     (gnus-orphan-score . global)
1569     gnus-newsgroup-active gnus-scores-exclude-files
1570     gnus-newsgroup-history gnus-newsgroup-ancient
1571     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1572     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1573     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1574     (gnus-newsgroup-expunged-tally . 0)
1575     gnus-cache-removable-articles gnus-newsgroup-cached
1576     gnus-newsgroup-data gnus-newsgroup-data-reverse
1577     gnus-newsgroup-limit gnus-newsgroup-limits
1578     gnus-newsgroup-charset gnus-newsgroup-display
1579     gnus-summary-use-undownloaded-faces)
1580   "Variables that are buffer-local to the summary buffers.")
1581
1582 (defvar gnus-newsgroup-variables nil
1583   "A list of variables that have separate values in different newsgroups.
1584 A list of newsgroup (summary buffer) local variables, or cons of
1585 variables and their default expressions to be evalled (when the default
1586 values are not nil), that should be made global while the summary buffer
1587 is active.
1588
1589 Note: The default expressions will be evaluated (using function `eval')
1590 before assignment to the local variable rather than just assigned to it.
1591 If the default expression is the symbol `global', that symbol will not
1592 be evaluated but the global value of the local variable will be used
1593 instead.
1594
1595 These variables can be used to set variables in the group parameters
1596 while still allowing them to affect operations done in other buffers.
1597 For example:
1598
1599 \(setq gnus-newsgroup-variables
1600      '(message-use-followup-to
1601        (gnus-visible-headers .
1602          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1603 ")
1604
1605 (eval-when-compile
1606   ;; Bind features so that require will believe that gnus-sum has
1607   ;; already been loaded (avoids infinite recursion)
1608   (let ((features (cons 'gnus-sum features)))
1609     (require 'gnus-art)))
1610
1611 ;; MIME stuff.
1612
1613 (defvar gnus-decode-encoded-word-methods
1614   '(mail-decode-encoded-word-string)
1615   "List of methods used to decode encoded words.
1616
1617 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1618 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1619 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1620 whose names match REGEXP.
1621
1622 For example:
1623 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1624  mail-decode-encoded-word-string
1625  (\"chinese\" . rfc1843-decode-string))")
1626
1627 (defvar gnus-decode-encoded-word-methods-cache nil)
1628
1629 (defun gnus-multi-decode-encoded-word-string (string)
1630   "Apply the functions from `gnus-encoded-word-methods' that match."
1631   (unless (and gnus-decode-encoded-word-methods-cache
1632                (eq gnus-newsgroup-name
1633                    (car gnus-decode-encoded-word-methods-cache)))
1634     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1635     (dolist (method gnus-decode-encoded-word-methods)
1636       (if (symbolp method)
1637           (nconc gnus-decode-encoded-word-methods-cache (list method))
1638         (if (and gnus-newsgroup-name
1639                  (string-match (car method) gnus-newsgroup-name))
1640             (nconc gnus-decode-encoded-word-methods-cache
1641                    (list (cdr method)))))))
1642   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1643     (setq string (funcall method string))))
1644
1645 ;; Subject simplification.
1646
1647 (defun gnus-simplify-whitespace (str)
1648   "Remove excessive whitespace from STR."
1649   ;; Multiple spaces.
1650   (while (string-match "[ \t][ \t]+" str)
1651     (setq str (concat (substring str 0 (match-beginning 0))
1652                         " "
1653                         (substring str (match-end 0)))))
1654   ;; Leading spaces.
1655   (when (string-match "^[ \t]+" str)
1656     (setq str (substring str (match-end 0))))
1657   ;; Trailing spaces.
1658   (when (string-match "[ \t]+$" str)
1659     (setq str (substring str 0 (match-beginning 0))))
1660   str)
1661
1662 (defun gnus-simplify-all-whitespace (str)
1663   "Remove all whitespace from STR."
1664   (while (string-match "[ \t\n]+" str)
1665     (setq str (replace-match "" nil nil str)))
1666   str)
1667
1668 (defsubst gnus-simplify-subject-re (subject)
1669   "Remove \"Re:\" from subject lines."
1670   (if (string-match message-subject-re-regexp subject)
1671       (substring subject (match-end 0))
1672     subject))
1673
1674 (defun gnus-simplify-subject (subject &optional re-only)
1675   "Remove `Re:' and words in parentheses.
1676 If RE-ONLY is non-nil, strip leading `Re:'s only."
1677   (let ((case-fold-search t))           ;Ignore case.
1678     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1679     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1680       (setq subject (substring subject (match-end 0))))
1681     ;; Remove uninteresting prefixes.
1682     (when (and (not re-only)
1683                gnus-simplify-ignored-prefixes
1684                (string-match gnus-simplify-ignored-prefixes subject))
1685       (setq subject (substring subject (match-end 0))))
1686     ;; Remove words in parentheses from end.
1687     (unless re-only
1688       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1689         (setq subject (substring subject 0 (match-beginning 0)))))
1690     ;; Return subject string.
1691     subject))
1692
1693 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1694 ;; all whitespace.
1695 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1696   (goto-char (point-min))
1697   (while (re-search-forward regexp nil t)
1698     (replace-match (or newtext ""))))
1699
1700 (defun gnus-simplify-buffer-fuzzy ()
1701   "Simplify string in the buffer fuzzily.
1702 The string in the accessible portion of the current buffer is simplified.
1703 It is assumed to be a single-line subject.
1704 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1705 matter is removed.  Additional things can be deleted by setting
1706 `gnus-simplify-subject-fuzzy-regexp'."
1707   (let ((case-fold-search t)
1708         (modified-tick))
1709     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1710
1711     (while (not (eq modified-tick (buffer-modified-tick)))
1712       (setq modified-tick (buffer-modified-tick))
1713       (cond
1714        ((listp gnus-simplify-subject-fuzzy-regexp)
1715         (mapc 'gnus-simplify-buffer-fuzzy-step
1716               gnus-simplify-subject-fuzzy-regexp))
1717        (gnus-simplify-subject-fuzzy-regexp
1718         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1719       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1720       (gnus-simplify-buffer-fuzzy-step
1721        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1722       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1723
1724     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1725     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1726     (gnus-simplify-buffer-fuzzy-step " $")
1727     (gnus-simplify-buffer-fuzzy-step "^ +")))
1728
1729 (defun gnus-simplify-subject-fuzzy (subject)
1730   "Simplify a subject string fuzzily.
1731 See `gnus-simplify-buffer-fuzzy' for details."
1732   (save-excursion
1733     (gnus-set-work-buffer)
1734     (let ((case-fold-search t))
1735       ;; Remove uninteresting prefixes.
1736       (when (and gnus-simplify-ignored-prefixes
1737                  (string-match gnus-simplify-ignored-prefixes subject))
1738         (setq subject (substring subject (match-end 0))))
1739       (insert subject)
1740       (inline (gnus-simplify-buffer-fuzzy))
1741       (buffer-string))))
1742
1743 (defsubst gnus-simplify-subject-fully (subject)
1744   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1745   (cond
1746    (gnus-simplify-subject-functions
1747     (gnus-map-function gnus-simplify-subject-functions subject))
1748    ((null gnus-summary-gather-subject-limit)
1749     (gnus-simplify-subject-re subject))
1750    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1751     (gnus-simplify-subject-fuzzy subject))
1752    ((numberp gnus-summary-gather-subject-limit)
1753     (truncate-string-to-width (gnus-simplify-subject-re subject)
1754                               gnus-summary-gather-subject-limit))
1755    (t
1756     subject)))
1757
1758 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1759   "Check whether two subjects are equal.
1760 If optional argument SIMPLE-FIRST is t, first argument is already
1761 simplified."
1762   (cond
1763    ((null simple-first)
1764     (equal (gnus-simplify-subject-fully s1)
1765            (gnus-simplify-subject-fully s2)))
1766    (t
1767     (equal s1
1768            (gnus-simplify-subject-fully s2)))))
1769
1770 (defun gnus-summary-bubble-group ()
1771   "Increase the score of the current group.
1772 This is a handy function to add to `gnus-summary-exit-hook' to
1773 increase the score of each group you read."
1774   (gnus-group-add-score gnus-newsgroup-name))
1775
1776 \f
1777 ;;;
1778 ;;; Gnus summary mode
1779 ;;;
1780
1781 (put 'gnus-summary-mode 'mode-class 'special)
1782
1783 (defvar gnus-article-commands-menu)
1784
1785 ;; Non-orthogonal keys
1786
1787 (gnus-define-keys gnus-summary-mode-map
1788   " " gnus-summary-next-page
1789   "\177" gnus-summary-prev-page
1790   [delete] gnus-summary-prev-page
1791   [backspace] gnus-summary-prev-page
1792   "\r" gnus-summary-scroll-up
1793   "\M-\r" gnus-summary-scroll-down
1794   "n" gnus-summary-next-unread-article
1795   "p" gnus-summary-prev-unread-article
1796   "N" gnus-summary-next-article
1797   "P" gnus-summary-prev-article
1798   "\M-\C-n" gnus-summary-next-same-subject
1799   "\M-\C-p" gnus-summary-prev-same-subject
1800   "\M-n" gnus-summary-next-unread-subject
1801   "\M-p" gnus-summary-prev-unread-subject
1802   "." gnus-summary-first-unread-article
1803   "," gnus-summary-best-unread-article
1804   "\M-s" gnus-summary-search-article-forward
1805   "\M-r" gnus-summary-search-article-backward
1806   "\M-S" gnus-summary-repeat-search-article-forward
1807   "\M-R" gnus-summary-repeat-search-article-backward
1808   "<" gnus-summary-beginning-of-article
1809   ">" gnus-summary-end-of-article
1810   "j" gnus-summary-goto-article
1811   "^" gnus-summary-refer-parent-article
1812   "\M-^" gnus-summary-refer-article
1813   "u" gnus-summary-tick-article-forward
1814   "!" gnus-summary-tick-article-forward
1815   "U" gnus-summary-tick-article-backward
1816   "d" gnus-summary-mark-as-read-forward
1817   "D" gnus-summary-mark-as-read-backward
1818   "E" gnus-summary-mark-as-expirable
1819   "\M-u" gnus-summary-clear-mark-forward
1820   "\M-U" gnus-summary-clear-mark-backward
1821   "k" gnus-summary-kill-same-subject-and-select
1822   "\C-k" gnus-summary-kill-same-subject
1823   "\M-\C-k" gnus-summary-kill-thread
1824   "\M-\C-l" gnus-summary-lower-thread
1825   "e" gnus-summary-edit-article
1826   "#" gnus-summary-mark-as-processable
1827   "\M-#" gnus-summary-unmark-as-processable
1828   "\M-\C-t" gnus-summary-toggle-threads
1829   "\M-\C-s" gnus-summary-show-thread
1830   "\M-\C-h" gnus-summary-hide-thread
1831   "\M-\C-f" gnus-summary-next-thread
1832   "\M-\C-b" gnus-summary-prev-thread
1833   [(meta down)] gnus-summary-next-thread
1834   [(meta up)] gnus-summary-prev-thread
1835   "\M-\C-u" gnus-summary-up-thread
1836   "\M-\C-d" gnus-summary-down-thread
1837   "&" gnus-summary-execute-command
1838   "c" gnus-summary-catchup-and-exit
1839   "\C-w" gnus-summary-mark-region-as-read
1840   "\C-t" gnus-summary-toggle-truncation
1841   "?" gnus-summary-mark-as-dormant
1842   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1843   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1844   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1845   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1846   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1847   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1848   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1849   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1850   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1851   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1852   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1853   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1854   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1855   "=" gnus-summary-expand-window
1856   "\C-x\C-s" gnus-summary-reselect-current-group
1857   "\M-g" gnus-summary-rescan-group
1858   "\C-c\C-r" gnus-summary-caesar-message
1859   "f" gnus-summary-followup
1860   "F" gnus-summary-followup-with-original
1861   "C" gnus-summary-cancel-article
1862   "r" gnus-summary-reply
1863   "R" gnus-summary-reply-with-original
1864   "\C-c\C-f" gnus-summary-mail-forward
1865   "o" gnus-summary-save-article
1866   "\C-o" gnus-summary-save-article-mail
1867   "|" gnus-summary-pipe-output
1868   "\M-k" gnus-summary-edit-local-kill
1869   "\M-K" gnus-summary-edit-global-kill
1870   ;; "V" gnus-version
1871   "\C-c\C-d" gnus-summary-describe-group
1872   "q" gnus-summary-exit
1873   "Q" gnus-summary-exit-no-update
1874   "\C-c\C-i" gnus-info-find-node
1875   gnus-mouse-2 gnus-mouse-pick-article
1876   [follow-link] mouse-face
1877   "m" gnus-summary-mail-other-window
1878   "a" gnus-summary-post-news
1879   "x" gnus-summary-limit-to-unread
1880   "s" gnus-summary-isearch-article
1881   "t" gnus-summary-toggle-header
1882   "g" gnus-summary-show-article
1883   "l" gnus-summary-goto-last-article
1884   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1885   "\C-d" gnus-summary-enter-digest-group
1886   "\M-\C-d" gnus-summary-read-document
1887   "\M-\C-e" gnus-summary-edit-parameters
1888   "\M-\C-a" gnus-summary-customize-parameters
1889   "\C-c\C-b" gnus-bug
1890   "*" gnus-cache-enter-article
1891   "\M-*" gnus-cache-remove-article
1892   "\M-&" gnus-summary-universal-argument
1893   "\C-l" gnus-recenter
1894   "I" gnus-summary-increase-score
1895   "L" gnus-summary-lower-score
1896   "\M-i" gnus-symbolic-argument
1897   "h" gnus-summary-select-article-buffer
1898
1899   "b" gnus-article-view-part
1900   "\M-t" gnus-summary-toggle-display-buttonized
1901
1902   "V" gnus-summary-score-map
1903   "X" gnus-uu-extract-map
1904   "S" gnus-summary-send-map)
1905
1906 ;; Sort of orthogonal keymap
1907 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1908   "t" gnus-summary-tick-article-forward
1909   "!" gnus-summary-tick-article-forward
1910   "d" gnus-summary-mark-as-read-forward
1911   "r" gnus-summary-mark-as-read-forward
1912   "c" gnus-summary-clear-mark-forward
1913   " " gnus-summary-clear-mark-forward
1914   "e" gnus-summary-mark-as-expirable
1915   "x" gnus-summary-mark-as-expirable
1916   "?" gnus-summary-mark-as-dormant
1917   "b" gnus-summary-set-bookmark
1918   "B" gnus-summary-remove-bookmark
1919   "#" gnus-summary-mark-as-processable
1920   "\M-#" gnus-summary-unmark-as-processable
1921   "S" gnus-summary-limit-include-expunged
1922   "C" gnus-summary-catchup
1923   "H" gnus-summary-catchup-to-here
1924   "h" gnus-summary-catchup-from-here
1925   "\C-c" gnus-summary-catchup-all
1926   "k" gnus-summary-kill-same-subject-and-select
1927   "K" gnus-summary-kill-same-subject
1928   "P" gnus-uu-mark-map)
1929
1930 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1931   "c" gnus-summary-clear-above
1932   "u" gnus-summary-tick-above
1933   "m" gnus-summary-mark-above
1934   "k" gnus-summary-kill-below)
1935
1936 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1937   "/" gnus-summary-limit-to-subject
1938   "n" gnus-summary-limit-to-articles
1939   "b" gnus-summary-limit-to-bodies
1940   "h" gnus-summary-limit-to-headers
1941   "w" gnus-summary-pop-limit
1942   "s" gnus-summary-limit-to-subject
1943   "a" gnus-summary-limit-to-author
1944   "u" gnus-summary-limit-to-unread
1945   "m" gnus-summary-limit-to-marks
1946   "M" gnus-summary-limit-exclude-marks
1947   "v" gnus-summary-limit-to-score
1948   "*" gnus-summary-limit-include-cached
1949   "D" gnus-summary-limit-include-dormant
1950   "T" gnus-summary-limit-include-thread
1951   "d" gnus-summary-limit-exclude-dormant
1952   "t" gnus-summary-limit-to-age
1953   "." gnus-summary-limit-to-unseen
1954   "x" gnus-summary-limit-to-extra
1955   "p" gnus-summary-limit-to-display-predicate
1956   "E" gnus-summary-limit-include-expunged
1957   "c" gnus-summary-limit-exclude-childless-dormant
1958   "C" gnus-summary-limit-mark-excluded-as-read
1959   "o" gnus-summary-insert-old-articles
1960   "N" gnus-summary-insert-new-articles
1961   "S" gnus-summary-limit-to-singletons
1962   "r" gnus-summary-limit-to-replied
1963   "R" gnus-summary-limit-to-recipient
1964   "A" gnus-summary-limit-to-address)
1965
1966 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1967   "n" gnus-summary-next-unread-article
1968   "p" gnus-summary-prev-unread-article
1969   "N" gnus-summary-next-article
1970   "P" gnus-summary-prev-article
1971   "\C-n" gnus-summary-next-same-subject
1972   "\C-p" gnus-summary-prev-same-subject
1973   "\M-n" gnus-summary-next-unread-subject
1974   "\M-p" gnus-summary-prev-unread-subject
1975   "f" gnus-summary-first-unread-article
1976   "b" gnus-summary-best-unread-article
1977   "j" gnus-summary-goto-article
1978   "g" gnus-summary-goto-subject
1979   "l" gnus-summary-goto-last-article
1980   "o" gnus-summary-pop-article)
1981
1982 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1983   "k" gnus-summary-kill-thread
1984   "E" gnus-summary-expire-thread
1985   "l" gnus-summary-lower-thread
1986   "i" gnus-summary-raise-thread
1987   "T" gnus-summary-toggle-threads
1988   "t" gnus-summary-rethread-current
1989   "^" gnus-summary-reparent-thread
1990   "\M-^" gnus-summary-reparent-children
1991   "s" gnus-summary-show-thread
1992   "S" gnus-summary-show-all-threads
1993   "h" gnus-summary-hide-thread
1994   "H" gnus-summary-hide-all-threads
1995   "n" gnus-summary-next-thread
1996   "p" gnus-summary-prev-thread
1997   "u" gnus-summary-up-thread
1998   "o" gnus-summary-top-thread
1999   "d" gnus-summary-down-thread
2000   "#" gnus-uu-mark-thread
2001   "\M-#" gnus-uu-unmark-thread)
2002
2003 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2004   "g" gnus-summary-prepare
2005   "c" gnus-summary-insert-cached-articles
2006   "d" gnus-summary-insert-dormant-articles
2007   "t" gnus-summary-insert-ticked-articles)
2008
2009 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2010   "c" gnus-summary-catchup-and-exit
2011   "C" gnus-summary-catchup-all-and-exit
2012   "E" gnus-summary-exit-no-update
2013   "Q" gnus-summary-exit
2014   "Z" gnus-summary-exit
2015   "n" gnus-summary-catchup-and-goto-next-group
2016   "p" gnus-summary-catchup-and-goto-prev-group
2017   "R" gnus-summary-reselect-current-group
2018   "G" gnus-summary-rescan-group
2019   "N" gnus-summary-next-group
2020   "s" gnus-summary-save-newsrc
2021   "P" gnus-summary-prev-group)
2022
2023 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2024   " " gnus-summary-next-page
2025   "n" gnus-summary-next-page
2026   "\177" gnus-summary-prev-page
2027   [delete] gnus-summary-prev-page
2028   "p" gnus-summary-prev-page
2029   "\r" gnus-summary-scroll-up
2030   "\M-\r" gnus-summary-scroll-down
2031   "<" gnus-summary-beginning-of-article
2032   ">" gnus-summary-end-of-article
2033   "b" gnus-summary-beginning-of-article
2034   "e" gnus-summary-end-of-article
2035   "^" gnus-summary-refer-parent-article
2036   "r" gnus-summary-refer-parent-article
2037   "D" gnus-summary-enter-digest-group
2038   "R" gnus-summary-refer-references
2039   "T" gnus-summary-refer-thread
2040   "g" gnus-summary-show-article
2041   "s" gnus-summary-isearch-article
2042   "P" gnus-summary-print-article
2043   "S" gnus-sticky-article
2044   "M" gnus-mailing-list-insinuate
2045   "t" gnus-article-babel)
2046
2047 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2048   "b" gnus-article-add-buttons
2049   "B" gnus-article-add-buttons-to-head
2050   "o" gnus-article-treat-overstrike
2051   "e" gnus-article-emphasize
2052   "w" gnus-article-fill-cited-article
2053   "Q" gnus-article-fill-long-lines
2054   "L" gnus-article-toggle-truncate-lines
2055   "C" gnus-article-capitalize-sentences
2056   "c" gnus-article-remove-cr
2057   "q" gnus-article-de-quoted-unreadable
2058   "6" gnus-article-de-base64-unreadable
2059   "Z" gnus-article-decode-HZ
2060   "A" gnus-article-treat-ansi-sequences
2061   "h" gnus-article-wash-html
2062   "u" gnus-article-unsplit-urls
2063   "s" gnus-summary-force-verify-and-decrypt
2064   "f" gnus-article-display-x-face
2065   "l" gnus-summary-stop-page-breaking
2066   "r" gnus-summary-caesar-message
2067   "m" gnus-summary-morse-message
2068   "t" gnus-summary-toggle-header
2069   "g" gnus-treat-smiley
2070   "v" gnus-summary-verbose-headers
2071   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2072   "p" gnus-article-verify-x-pgp-sig
2073   "d" gnus-article-treat-dumbquotes
2074   "i" gnus-summary-idna-message)
2075
2076 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2077   ;; mnemonic: deuglif*Y*
2078   "u" gnus-article-outlook-unwrap-lines
2079   "a" gnus-article-outlook-repair-attribution
2080   "c" gnus-article-outlook-rearrange-citation
2081   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2082
2083 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2084   "a" gnus-article-hide
2085   "h" gnus-article-hide-headers
2086   "b" gnus-article-hide-boring-headers
2087   "s" gnus-article-hide-signature
2088   "c" gnus-article-hide-citation
2089   "C" gnus-article-hide-citation-in-followups
2090   "l" gnus-article-hide-list-identifiers
2091   "B" gnus-article-strip-banner
2092   "P" gnus-article-hide-pem
2093   "\C-c" gnus-article-hide-citation-maybe)
2094
2095 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2096   "a" gnus-article-highlight
2097   "h" gnus-article-highlight-headers
2098   "c" gnus-article-highlight-citation
2099   "s" gnus-article-highlight-signature)
2100
2101 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2102   "f" gnus-article-treat-fold-headers
2103   "u" gnus-article-treat-unfold-headers
2104   "n" gnus-article-treat-fold-newsgroups)
2105
2106 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2107   "x" gnus-article-display-x-face
2108   "d" gnus-article-display-face
2109   "s" gnus-treat-smiley
2110   "D" gnus-article-remove-images
2111   "f" gnus-treat-from-picon
2112   "m" gnus-treat-mail-picon
2113   "n" gnus-treat-newsgroups-picon)
2114
2115 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2116   "w" gnus-article-decode-mime-words
2117   "c" gnus-article-decode-charset
2118   "v" gnus-mime-view-all-parts
2119   "b" gnus-article-view-part)
2120
2121 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2122   "z" gnus-article-date-ut
2123   "u" gnus-article-date-ut
2124   "l" gnus-article-date-local
2125   "p" gnus-article-date-english
2126   "e" gnus-article-date-lapsed
2127   "o" gnus-article-date-original
2128   "i" gnus-article-date-iso8601
2129   "s" gnus-article-date-user)
2130
2131 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2132   "t" gnus-article-remove-trailing-blank-lines
2133   "l" gnus-article-strip-leading-blank-lines
2134   "m" gnus-article-strip-multiple-blank-lines
2135   "a" gnus-article-strip-blank-lines
2136   "A" gnus-article-strip-all-blank-lines
2137   "s" gnus-article-strip-leading-space
2138   "e" gnus-article-strip-trailing-space
2139   "w" gnus-article-remove-leading-whitespace)
2140
2141 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2142   "v" gnus-version
2143   "f" gnus-summary-fetch-faq
2144   "d" gnus-summary-describe-group
2145   "h" gnus-summary-describe-briefly
2146   "i" gnus-info-find-node
2147   "c" gnus-group-fetch-charter
2148   "C" gnus-group-fetch-control)
2149
2150 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2151   "e" gnus-summary-expire-articles
2152   "\M-\C-e" gnus-summary-expire-articles-now
2153   "\177" gnus-summary-delete-article
2154   [delete] gnus-summary-delete-article
2155   [backspace] gnus-summary-delete-article
2156   "m" gnus-summary-move-article
2157   "r" gnus-summary-respool-article
2158   "w" gnus-summary-edit-article
2159   "c" gnus-summary-copy-article
2160   "B" gnus-summary-crosspost-article
2161   "q" gnus-summary-respool-query
2162   "t" gnus-summary-respool-trace
2163   "i" gnus-summary-import-article
2164   "I" gnus-summary-create-article
2165   "p" gnus-summary-article-posted-p)
2166
2167 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2168   "o" gnus-summary-save-article
2169   "m" gnus-summary-save-article-mail
2170   "F" gnus-summary-write-article-file
2171   "r" gnus-summary-save-article-rmail
2172   "f" gnus-summary-save-article-file
2173   "b" gnus-summary-save-article-body-file
2174   "B" gnus-summary-write-article-body-file
2175   "h" gnus-summary-save-article-folder
2176   "v" gnus-summary-save-article-vm
2177   "p" gnus-summary-pipe-output
2178   "P" gnus-summary-muttprint)
2179
2180 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2181   "b" gnus-summary-display-buttonized
2182   "m" gnus-summary-repair-multipart
2183   "v" gnus-article-view-part
2184   "o" gnus-article-save-part
2185   "O" gnus-article-save-part-and-strip
2186   "r" gnus-article-replace-part
2187   "d" gnus-article-delete-part
2188   "t" gnus-article-view-part-as-type
2189   "j" gnus-article-jump-to-part
2190   "c" gnus-article-copy-part
2191   "C" gnus-article-view-part-as-charset
2192   "e" gnus-article-view-part-externally
2193   "H" gnus-article-browse-html-article
2194   "E" gnus-article-encrypt-body
2195   "i" gnus-article-inline-part
2196   "|" gnus-article-pipe-part)
2197
2198 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2199   "p" gnus-summary-mark-as-processable
2200   "u" gnus-summary-unmark-as-processable
2201   "U" gnus-summary-unmark-all-processable
2202   "v" gnus-uu-mark-over
2203   "s" gnus-uu-mark-series
2204   "r" gnus-uu-mark-region
2205   "g" gnus-uu-unmark-region
2206   "R" gnus-uu-mark-by-regexp
2207   "G" gnus-uu-unmark-by-regexp
2208   "t" gnus-uu-mark-thread
2209   "T" gnus-uu-unmark-thread
2210   "a" gnus-uu-mark-all
2211   "b" gnus-uu-mark-buffer
2212   "S" gnus-uu-mark-sparse
2213   "k" gnus-summary-kill-process-mark
2214   "y" gnus-summary-yank-process-mark
2215   "w" gnus-summary-save-process-mark
2216   "i" gnus-uu-invert-processable)
2217
2218 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2219   ;;"x" gnus-uu-extract-any
2220   "m" gnus-summary-save-parts
2221   "u" gnus-uu-decode-uu
2222   "U" gnus-uu-decode-uu-and-save
2223   "s" gnus-uu-decode-unshar
2224   "S" gnus-uu-decode-unshar-and-save
2225   "o" gnus-uu-decode-save
2226   "O" gnus-uu-decode-save
2227   "b" gnus-uu-decode-binhex
2228   "B" gnus-uu-decode-binhex
2229   "Y" gnus-uu-decode-yenc
2230   "p" gnus-uu-decode-postscript
2231   "P" gnus-uu-decode-postscript-and-save)
2232
2233 (gnus-define-keys
2234     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2235   "u" gnus-uu-decode-uu-view
2236   "U" gnus-uu-decode-uu-and-save-view
2237   "s" gnus-uu-decode-unshar-view
2238   "S" gnus-uu-decode-unshar-and-save-view
2239   "o" gnus-uu-decode-save-view
2240   "O" gnus-uu-decode-save-view
2241   "b" gnus-uu-decode-binhex-view
2242   "B" gnus-uu-decode-binhex-view
2243   "p" gnus-uu-decode-postscript-view
2244   "P" gnus-uu-decode-postscript-and-save-view)
2245
2246 (defvar gnus-article-post-menu nil)
2247
2248 (defconst gnus-summary-menu-maxlen 20)
2249
2250 (defun gnus-summary-menu-split (menu)
2251   ;; If we have lots of elements, divide them into groups of 20
2252   ;; and make a pane (or submenu) for each one.
2253   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2254       (let ((menu menu) sublists next
2255             (i 1))
2256         (while menu
2257           ;; Pull off the next gnus-summary-menu-maxlen elements
2258           ;; and make them the next element of sublist.
2259           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2260           (if next
2261               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2262                       nil))
2263           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2264                                              (aref (car (last menu)) 0)) menu)
2265                                sublists))
2266           (setq i (1+ i))
2267           (setq menu next))
2268         (nreverse sublists))
2269     ;; Few elements--put them all in one pane.
2270     menu))
2271
2272 (defun gnus-summary-make-menu-bar ()
2273   (gnus-turn-off-edit-menu 'summary)
2274
2275   (unless (boundp 'gnus-summary-misc-menu)
2276
2277     (easy-menu-define
2278       gnus-summary-kill-menu gnus-summary-mode-map ""
2279       (cons
2280        "Score"
2281        (nconc
2282         (list
2283          ["Customize" gnus-score-customize t])
2284         (gnus-make-score-map 'increase)
2285         (gnus-make-score-map 'lower)
2286         '(("Mark"
2287            ["Kill below" gnus-summary-kill-below t]
2288            ["Mark above" gnus-summary-mark-above t]
2289            ["Tick above" gnus-summary-tick-above t]
2290            ["Clear above" gnus-summary-clear-above t])
2291           ["Current score" gnus-summary-current-score t]
2292           ["Set score" gnus-summary-set-score t]
2293           ["Switch current score file..." gnus-score-change-score-file t]
2294           ["Set mark below..." gnus-score-set-mark-below t]
2295           ["Set expunge below..." gnus-score-set-expunge-below t]
2296           ["Edit current score file" gnus-score-edit-current-scores t]
2297           ["Edit score file..." gnus-score-edit-file t]
2298           ["Trace score" gnus-score-find-trace t]
2299           ["Find words" gnus-score-find-favourite-words t]
2300           ["Rescore buffer" gnus-summary-rescore t]
2301           ["Increase score..." gnus-summary-increase-score t]
2302           ["Lower score..." gnus-summary-lower-score t]))))
2303
2304     ;; Define both the Article menu in the summary buffer and the
2305     ;; equivalent Commands menu in the article buffer here for
2306     ;; consistency.
2307     (let ((innards
2308            `(("Hide"
2309               ["All" gnus-article-hide t]
2310               ["Headers" gnus-article-hide-headers t]
2311               ["Signature" gnus-article-hide-signature t]
2312               ["Citation" gnus-article-hide-citation t]
2313               ["List identifiers" gnus-article-hide-list-identifiers t]
2314               ["Banner" gnus-article-strip-banner t]
2315               ["Boring headers" gnus-article-hide-boring-headers t])
2316              ("Highlight"
2317               ["All" gnus-article-highlight t]
2318               ["Headers" gnus-article-highlight-headers t]
2319               ["Signature" gnus-article-highlight-signature t]
2320               ["Citation" gnus-article-highlight-citation t])
2321              ("MIME"
2322               ["Words" gnus-article-decode-mime-words t]
2323               ["Charset" gnus-article-decode-charset t]
2324               ["QP" gnus-article-de-quoted-unreadable t]
2325               ["Base64" gnus-article-de-base64-unreadable t]
2326               ["View MIME buttons" gnus-summary-display-buttonized t]
2327               ["View all" gnus-mime-view-all-parts t]
2328               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2329               ["Encrypt body" gnus-article-encrypt-body
2330                :active (not (gnus-group-read-only-p))
2331                ,@(if (featurep 'xemacs) nil
2332                    '(:help "Encrypt the message body on disk"))]
2333               ["Extract all parts..." gnus-summary-save-parts t]
2334               ("Multipart"
2335                ["Repair multipart" gnus-summary-repair-multipart t]
2336                ["Pipe part..." gnus-article-pipe-part t]
2337                ["Inline part" gnus-article-inline-part t]
2338                ["View part as type..." gnus-article-view-part-as-type t]
2339                ["Encrypt body" gnus-article-encrypt-body
2340                 :active (not (gnus-group-read-only-p))
2341                ,@(if (featurep 'xemacs) nil
2342                    '(:help "Encrypt the message body on disk"))]
2343                ["View part externally" gnus-article-view-part-externally t]
2344                ["View HTML parts in browser" gnus-article-browse-html-article t]
2345                ["View part with charset..." gnus-article-view-part-as-charset t]
2346                ["Copy part" gnus-article-copy-part t]
2347                ["Save part..." gnus-article-save-part t]
2348                ["View part" gnus-article-view-part t]))
2349              ("Date"
2350               ["Local" gnus-article-date-local t]
2351               ["ISO8601" gnus-article-date-iso8601 t]
2352               ["UT" gnus-article-date-ut t]
2353               ["Original" gnus-article-date-original t]
2354               ["Lapsed" gnus-article-date-lapsed t]
2355               ["User-defined" gnus-article-date-user t])
2356              ("Display"
2357               ["Remove images" gnus-article-remove-images t]
2358               ["Toggle smiley" gnus-treat-smiley t]
2359               ["Show X-Face" gnus-article-display-x-face t]
2360               ["Show picons in From" gnus-treat-from-picon t]
2361               ["Show picons in mail headers" gnus-treat-mail-picon t]
2362               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2363               ("View as different encoding"
2364                ,@(gnus-summary-menu-split
2365                   (mapcar
2366                    (lambda (cs)
2367                      ;; Since easymenu under Emacs doesn't allow
2368                      ;; lambda forms for menu commands, we should
2369                      ;; provide intern'ed function symbols.
2370                      (let ((command (intern (format "\
2371 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2372                        (fset command
2373                              `(lambda ()
2374                                 (interactive)
2375                                 (let ((gnus-summary-show-article-charset-alist
2376                                        '((1 . ,cs))))
2377                                   (gnus-summary-show-article 1))))
2378                        `[,(symbol-name cs) ,command t]))
2379                    (sort (if (fboundp 'coding-system-list)
2380                              (coding-system-list)
2381                            (mapcar 'car mm-mime-mule-charset-alist))
2382                          'string<)))))
2383              ("Washing"
2384               ("Remove Blanks"
2385                ["Leading" gnus-article-strip-leading-blank-lines t]
2386                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2387                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2388                ["All of the above" gnus-article-strip-blank-lines t]
2389                ["All" gnus-article-strip-all-blank-lines t]
2390                ["Leading space" gnus-article-strip-leading-space t]
2391                ["Trailing space" gnus-article-strip-trailing-space t]
2392                ["Leading space in headers"
2393                 gnus-article-remove-leading-whitespace t])
2394               ["Overstrike" gnus-article-treat-overstrike t]
2395               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2396               ["Emphasis" gnus-article-emphasize t]
2397               ["Word wrap" gnus-article-fill-cited-article t]
2398               ["Fill long lines" gnus-article-fill-long-lines t]
2399               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2400               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2401               ["Remove CR" gnus-article-remove-cr t]
2402               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2403               ["Base64" gnus-article-de-base64-unreadable t]
2404               ["Rot 13" gnus-summary-caesar-message
2405                ,@(if (featurep 'xemacs) '(t)
2406                    '(:help "\"Caesar rotate\" article by 13"))]
2407               ["De-IDNA" gnus-summary-idna-message t]
2408               ["Morse decode" gnus-summary-morse-message t]
2409               ["Unix pipe..." gnus-summary-pipe-message t]
2410               ["Add buttons" gnus-article-add-buttons t]
2411               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2412               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2413               ["Verbose header" gnus-summary-verbose-headers t]
2414               ["Toggle header" gnus-summary-toggle-header t]
2415               ["Unfold headers" gnus-article-treat-unfold-headers t]
2416               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2417               ["Html" gnus-article-wash-html t]
2418               ["Unsplit URLs" gnus-article-unsplit-urls t]
2419               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2420               ["Decode HZ" gnus-article-decode-HZ t]
2421               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2422               ("(Outlook) Deuglify"
2423                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2424                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2425                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2426                ["Full (Outlook) deuglify"
2427                 gnus-article-outlook-deuglify-article t])
2428               )
2429              ("Output"
2430               ["Save in default format..." gnus-summary-save-article
2431                ,@(if (featurep 'xemacs) '(t)
2432                    '(:help "Save article using default method"))]
2433               ["Save in file..." gnus-summary-save-article-file
2434                ,@(if (featurep 'xemacs) '(t)
2435                    '(:help "Save article in file"))]
2436               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2437               ["Save in MH folder..." gnus-summary-save-article-folder t]
2438               ["Save in VM folder..." gnus-summary-save-article-vm t]
2439               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2440               ["Save body in file..." gnus-summary-save-article-body-file t]
2441               ["Pipe through a filter..." gnus-summary-pipe-output t]
2442               ["Print with Muttprint..." gnus-summary-muttprint t]
2443               ["Print" gnus-summary-print-article
2444                ,@(if (featurep 'xemacs) '(t)
2445                    '(:help "Generate and print a PostScript image"))])
2446              ("Copy, move,... (Backend)"
2447               ,@(if (featurep 'xemacs) nil
2448                   '(:help "Copying, moving, expiring articles..."))
2449               ["Respool article..." gnus-summary-respool-article t]
2450               ["Move article..." gnus-summary-move-article
2451                (gnus-check-backend-function
2452                 'request-move-article gnus-newsgroup-name)]
2453               ["Copy article..." gnus-summary-copy-article t]
2454               ["Crosspost article..." gnus-summary-crosspost-article
2455                (gnus-check-backend-function
2456                 'request-replace-article gnus-newsgroup-name)]
2457               ["Import file..." gnus-summary-import-article
2458                (gnus-check-backend-function
2459                 'request-accept-article gnus-newsgroup-name)]
2460               ["Create article..." gnus-summary-create-article
2461                (gnus-check-backend-function
2462                 'request-accept-article gnus-newsgroup-name)]
2463               ["Check if posted" gnus-summary-article-posted-p t]
2464               ["Edit article" gnus-summary-edit-article
2465                (not (gnus-group-read-only-p))]
2466               ["Delete article" gnus-summary-delete-article
2467                (gnus-check-backend-function
2468                 'request-expire-articles gnus-newsgroup-name)]
2469               ["Query respool" gnus-summary-respool-query t]
2470               ["Trace respool" gnus-summary-respool-trace t]
2471               ["Delete expirable articles" gnus-summary-expire-articles-now
2472                (gnus-check-backend-function
2473                 'request-expire-articles gnus-newsgroup-name)])
2474              ("Extract"
2475               ["Uudecode" gnus-uu-decode-uu
2476                ,@(if (featurep 'xemacs) '(t)
2477                    '(:help "Decode uuencoded article(s)"))]
2478               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2479               ["Unshar" gnus-uu-decode-unshar t]
2480               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2481               ["Save" gnus-uu-decode-save t]
2482               ["Binhex" gnus-uu-decode-binhex t]
2483               ["Postscript" gnus-uu-decode-postscript t]
2484               ["All MIME parts" gnus-summary-save-parts t])
2485              ("Cache"
2486               ["Enter article" gnus-cache-enter-article t]
2487               ["Remove article" gnus-cache-remove-article t])
2488              ["Translate" gnus-article-babel t]
2489              ["Select article buffer" gnus-summary-select-article-buffer t]
2490              ["Make article buffer sticky" gnus-sticky-article t]
2491              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2492              ["Isearch article..." gnus-summary-isearch-article t]
2493              ["Beginning of the article" gnus-summary-beginning-of-article t]
2494              ["End of the article" gnus-summary-end-of-article t]
2495              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2496              ["Fetch referenced articles" gnus-summary-refer-references t]
2497              ["Fetch current thread" gnus-summary-refer-thread t]
2498              ["Fetch article with id..." gnus-summary-refer-article t]
2499              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2500              ["Redisplay" gnus-summary-show-article t]
2501              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2502       (easy-menu-define
2503         gnus-summary-article-menu gnus-summary-mode-map ""
2504         (cons "Article" innards))
2505
2506       (if (not (keymapp gnus-summary-article-menu))
2507           (easy-menu-define
2508             gnus-article-commands-menu gnus-article-mode-map ""
2509             (cons "Commands" innards))
2510         ;; in Emacs, don't share menu.
2511         (setq gnus-article-commands-menu
2512               (copy-keymap gnus-summary-article-menu))
2513         (define-key gnus-article-mode-map [menu-bar commands]
2514           (cons "Commands" gnus-article-commands-menu))))
2515
2516     (easy-menu-define
2517       gnus-summary-thread-menu gnus-summary-mode-map ""
2518       '("Threads"
2519         ["Find all messages in thread" gnus-summary-refer-thread t]
2520         ["Toggle threading" gnus-summary-toggle-threads t]
2521         ["Hide threads" gnus-summary-hide-all-threads t]
2522         ["Show threads" gnus-summary-show-all-threads t]
2523         ["Hide thread" gnus-summary-hide-thread t]
2524         ["Show thread" gnus-summary-show-thread t]
2525         ["Go to next thread" gnus-summary-next-thread t]
2526         ["Go to previous thread" gnus-summary-prev-thread t]
2527         ["Go down thread" gnus-summary-down-thread t]
2528         ["Go up thread" gnus-summary-up-thread t]
2529         ["Top of thread" gnus-summary-top-thread t]
2530         ["Mark thread as read" gnus-summary-kill-thread t]
2531         ["Mark thread as expired" gnus-summary-expire-thread t]
2532         ["Lower thread score" gnus-summary-lower-thread t]
2533         ["Raise thread score" gnus-summary-raise-thread t]
2534         ["Rethread current" gnus-summary-rethread-current t]))
2535
2536     (easy-menu-define
2537       gnus-summary-post-menu gnus-summary-mode-map ""
2538       `("Post"
2539         ["Send a message (mail or news)" gnus-summary-post-news
2540          ,@(if (featurep 'xemacs) '(t)
2541              '(:help "Compose a new message (mail or news)"))]
2542         ["Followup" gnus-summary-followup
2543          ,@(if (featurep 'xemacs) '(t)
2544              '(:help "Post followup to this article"))]
2545         ["Followup and yank" gnus-summary-followup-with-original
2546          ,@(if (featurep 'xemacs) '(t)
2547              '(:help "Post followup to this article, quoting its contents"))]
2548         ["Supersede article" gnus-summary-supersede-article t]
2549         ["Cancel article" gnus-summary-cancel-article
2550          ,@(if (featurep 'xemacs) '(t)
2551              '(:help "Cancel an article you posted"))]
2552         ["Reply" gnus-summary-reply t]
2553         ["Reply and yank" gnus-summary-reply-with-original t]
2554         ["Wide reply" gnus-summary-wide-reply t]
2555         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2556          ,@(if (featurep 'xemacs) '(t)
2557              '(:help "Mail a reply, quoting this article"))]
2558         ["Very wide reply" gnus-summary-very-wide-reply t]
2559         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2560          ,@(if (featurep 'xemacs) '(t)
2561              '(:help "Mail a very wide reply, quoting this article"))]
2562         ["Mail forward" gnus-summary-mail-forward t]
2563         ["Post forward" gnus-summary-post-forward t]
2564         ["Digest and mail" gnus-uu-digest-mail-forward t]
2565         ["Digest and post" gnus-uu-digest-post-forward t]
2566         ["Resend message" gnus-summary-resend-message t]
2567         ["Resend message edit" gnus-summary-resend-message-edit t]
2568         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2569         ["Send a mail" gnus-summary-mail-other-window t]
2570         ["Create a local message" gnus-summary-news-other-window t]
2571         ["Uuencode and post" gnus-uu-post-news
2572          ,@(if (featurep 'xemacs) '(t)
2573              '(:help "Post a uuencoded article"))]
2574         ["Followup via news" gnus-summary-followup-to-mail t]
2575         ["Followup via news and yank"
2576          gnus-summary-followup-to-mail-with-original t]
2577         ["Strip signature on reply"
2578          (lambda ()
2579            (interactive)
2580            (if (not (memq message-cite-function
2581                           '(message-cite-original-without-signature
2582                             message-cite-original)))
2583                ;; Stupid workaround for XEmacs not honoring :visible.
2584                (message "Can't toggle this value of `message-cite-function'")
2585              (setq message-cite-function
2586                    (if (eq message-cite-function
2587                            'message-cite-original-without-signature)
2588                        'message-cite-original
2589                      'message-cite-original-without-signature))))
2590          ;; XEmacs barfs on :visible.
2591          ,@(if (featurep 'xemacs) nil
2592              '(:visible (memq message-cite-function
2593                               '(message-cite-original-without-signature
2594                                 message-cite-original))))
2595          :style toggle
2596          :selected (eq message-cite-function
2597                        'message-cite-original-without-signature)
2598          ,@(if (featurep 'xemacs) nil
2599              '(:help "Strip signature from cited article when replying."))]
2600         ;;("Draft"
2601         ;;["Send" gnus-summary-send-draft t]
2602         ;;["Send bounced" gnus-resend-bounced-mail t])
2603         ))
2604
2605     (cond
2606      ((not (keymapp gnus-summary-post-menu))
2607       (setq gnus-article-post-menu gnus-summary-post-menu))
2608      ((not gnus-article-post-menu)
2609       ;; Don't share post menu.
2610       (setq gnus-article-post-menu
2611             (copy-keymap gnus-summary-post-menu))))
2612     (define-key gnus-article-mode-map [menu-bar post]
2613       (cons "Post" gnus-article-post-menu))
2614
2615     (easy-menu-define
2616       gnus-summary-misc-menu gnus-summary-mode-map ""
2617       `("Gnus"
2618         ("Mark Read"
2619          ["Mark as read" gnus-summary-mark-as-read-forward t]
2620          ["Mark same subject and select"
2621           gnus-summary-kill-same-subject-and-select t]
2622          ["Mark same subject" gnus-summary-kill-same-subject t]
2623          ["Catchup" gnus-summary-catchup
2624           ,@(if (featurep 'xemacs) '(t)
2625               '(:help "Mark unread articles in this group as read"))]
2626          ["Catchup all" gnus-summary-catchup-all t]
2627          ["Catchup to here" gnus-summary-catchup-to-here t]
2628          ["Catchup from here" gnus-summary-catchup-from-here t]
2629          ["Catchup region" gnus-summary-mark-region-as-read
2630           (gnus-mark-active-p)]
2631          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2632         ("Mark Various"
2633          ["Tick" gnus-summary-tick-article-forward t]
2634          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2635          ["Remove marks" gnus-summary-clear-mark-forward t]
2636          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2637          ["Set bookmark" gnus-summary-set-bookmark t]
2638          ["Remove bookmark" gnus-summary-remove-bookmark t])
2639         ("Limit to"
2640          ["Marks..." gnus-summary-limit-to-marks t]
2641          ["Subject..." gnus-summary-limit-to-subject t]
2642          ["Author..." gnus-summary-limit-to-author t]
2643          ["Recipient..." gnus-summary-limit-to-recipient t]
2644          ["Address..." gnus-summary-limit-to-address t]
2645          ["Age..." gnus-summary-limit-to-age t]
2646          ["Extra..." gnus-summary-limit-to-extra t]
2647          ["Score..." gnus-summary-limit-to-score t]
2648          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2649          ["Unread" gnus-summary-limit-to-unread t]
2650          ["Unseen" gnus-summary-limit-to-unseen t]
2651          ["Singletons" gnus-summary-limit-to-singletons t]
2652          ["Replied" gnus-summary-limit-to-replied t]
2653          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2654          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2655          ["Pop limit" gnus-summary-pop-limit t]
2656          ["Show dormant" gnus-summary-limit-include-dormant t]
2657          ["Hide childless dormant"
2658           gnus-summary-limit-exclude-childless-dormant t]
2659          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2660          ["Hide marked" gnus-summary-limit-exclude-marks t]
2661          ["Show expunged" gnus-summary-limit-include-expunged t])
2662         ("Process Mark"
2663          ["Set mark" gnus-summary-mark-as-processable t]
2664          ["Remove mark" gnus-summary-unmark-as-processable t]
2665          ["Remove all marks" gnus-summary-unmark-all-processable t]
2666          ["Invert marks" gnus-uu-invert-processable t]
2667          ["Mark above" gnus-uu-mark-over t]
2668          ["Mark series" gnus-uu-mark-series t]
2669          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2670          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2671          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2672          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2673          ["Mark all" gnus-uu-mark-all t]
2674          ["Mark buffer" gnus-uu-mark-buffer t]
2675          ["Mark sparse" gnus-uu-mark-sparse t]
2676          ["Mark thread" gnus-uu-mark-thread t]
2677          ["Unmark thread" gnus-uu-unmark-thread t]
2678          ("Process Mark Sets"
2679           ["Kill" gnus-summary-kill-process-mark t]
2680           ["Yank" gnus-summary-yank-process-mark
2681            gnus-newsgroup-process-stack]
2682           ["Save" gnus-summary-save-process-mark t]
2683           ["Run command on marked..." gnus-summary-universal-argument t]))
2684         ("Registry Marks")
2685         ("Scroll article"
2686          ["Page forward" gnus-summary-next-page
2687           ,@(if (featurep 'xemacs) '(t)
2688               '(:help "Show next page of article"))]
2689          ["Page backward" gnus-summary-prev-page
2690           ,@(if (featurep 'xemacs) '(t)
2691               '(:help "Show previous page of article"))]
2692          ["Line forward" gnus-summary-scroll-up t])
2693         ("Move"
2694          ["Next unread article" gnus-summary-next-unread-article t]
2695          ["Previous unread article" gnus-summary-prev-unread-article t]
2696          ["Next article" gnus-summary-next-article t]
2697          ["Previous article" gnus-summary-prev-article t]
2698          ["Next unread subject" gnus-summary-next-unread-subject t]
2699          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2700          ["Next article same subject" gnus-summary-next-same-subject t]
2701          ["Previous article same subject" gnus-summary-prev-same-subject t]
2702          ["First unread article" gnus-summary-first-unread-article t]
2703          ["Best unread article" gnus-summary-best-unread-article t]
2704          ["Go to subject number..." gnus-summary-goto-subject t]
2705          ["Go to article number..." gnus-summary-goto-article t]
2706          ["Go to the last article" gnus-summary-goto-last-article t]
2707          ["Pop article off history" gnus-summary-pop-article t])
2708         ("Sort"
2709          ["Sort by number" gnus-summary-sort-by-number t]
2710          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2711          ["Sort by author" gnus-summary-sort-by-author t]
2712          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2713          ["Sort by subject" gnus-summary-sort-by-subject t]
2714          ["Sort by date" gnus-summary-sort-by-date t]
2715          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2716          ["Sort by score" gnus-summary-sort-by-score t]
2717          ["Sort by lines" gnus-summary-sort-by-lines t]
2718          ["Sort by characters" gnus-summary-sort-by-chars t]
2719          ["Randomize" gnus-summary-sort-by-random t]
2720          ["Original sort" gnus-summary-sort-by-original t])
2721         ("Help"
2722          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2723          ["Describe group" gnus-summary-describe-group t]
2724          ["Fetch charter" gnus-group-fetch-charter
2725           ,@(if (featurep 'xemacs) nil
2726               '(:help "Display the charter of the current group"))]
2727          ["Fetch control message" gnus-group-fetch-control
2728           ,@(if (featurep 'xemacs) nil
2729               '(:help "Display the archived control message for the current group"))]
2730          ["Read manual" gnus-info-find-node t])
2731         ("Modes"
2732          ["Pick and read" gnus-pick-mode t]
2733          ["Binary" gnus-binary-mode t])
2734         ("Regeneration"
2735          ["Regenerate" gnus-summary-prepare t]
2736          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2737          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2738          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2739          ["Toggle threading" gnus-summary-toggle-threads t])
2740         ["See old articles" gnus-summary-insert-old-articles t]
2741         ["See new articles" gnus-summary-insert-new-articles t]
2742         ["Filter articles..." gnus-summary-execute-command t]
2743         ["Run command on articles..." gnus-summary-universal-argument t]
2744         ["Search articles forward..." gnus-summary-search-article-forward t]
2745         ["Search articles backward..." gnus-summary-search-article-backward t]
2746         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2747         ["Expand window" gnus-summary-expand-window t]
2748         ["Expire expirable articles" gnus-summary-expire-articles
2749          (gnus-check-backend-function
2750           'request-expire-articles gnus-newsgroup-name)]
2751         ["Edit local kill file" gnus-summary-edit-local-kill t]
2752         ["Edit main kill file" gnus-summary-edit-global-kill t]
2753         ["Edit group parameters" gnus-summary-edit-parameters t]
2754         ["Customize group parameters" gnus-summary-customize-parameters t]
2755         ["Send a bug report" gnus-bug t]
2756         ("Exit"
2757          ["Catchup and exit" gnus-summary-catchup-and-exit
2758           ,@(if (featurep 'xemacs) '(t)
2759               '(:help "Mark unread articles in this group as read, then exit"))]
2760          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2761          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2762          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2763          ["Exit group" gnus-summary-exit
2764           ,@(if (featurep 'xemacs) '(t)
2765               '(:help "Exit current group, return to group selection mode"))]
2766          ["Exit group without updating" gnus-summary-exit-no-update t]
2767          ["Exit and goto next group" gnus-summary-next-group t]
2768          ["Exit and goto prev group" gnus-summary-prev-group t]
2769          ["Reselect group" gnus-summary-reselect-current-group t]
2770          ["Rescan group" gnus-summary-rescan-group t]
2771          ["Update dribble" gnus-summary-save-newsrc t])))
2772
2773     (gnus-run-hooks 'gnus-summary-menu-hook)))
2774
2775 (defvar gnus-summary-tool-bar-map nil)
2776
2777 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2778 ;; affect _new_ message buffers.  We might add a function that walks thru all
2779 ;; summary-mode buffers and force the update.
2780 (defun gnus-summary-tool-bar-update (&optional symbol value)
2781   "Update summary mode toolbar.
2782 Setter function for custom variables."
2783   (setq-default gnus-summary-tool-bar-map nil)
2784   (when symbol
2785     ;; When used as ":set" function:
2786     (set-default symbol value))
2787   (when (gnus-buffer-live-p gnus-summary-buffer)
2788     (with-current-buffer gnus-summary-buffer
2789       (gnus-summary-make-tool-bar))))
2790
2791 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2792                                      'gnus-summary-tool-bar-gnome
2793                                    'gnus-summary-tool-bar-retro)
2794   "Specifies the Gnus summary tool bar.
2795
2796 It can be either a list or a symbol refering to a list.  See
2797 `gmm-tool-bar-from-list' for the format of the list.  The
2798 default key map is `gnus-summary-mode-map'.
2799
2800 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2801 `gnus-summary-tool-bar-retro'."
2802   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2803                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2804                  (repeat :tag "User defined list" gmm-tool-bar-item)
2805                  (symbol))
2806   :version "23.1" ;; No Gnus
2807   :initialize 'custom-initialize-default
2808   :set 'gnus-summary-tool-bar-update
2809   :group 'gnus-summary)
2810
2811 (defcustom gnus-summary-tool-bar-gnome
2812   '((gnus-summary-post-news "mail/compose" nil)
2813     (gnus-summary-insert-new-articles "mail/inbox" nil
2814                                       :visible (or (not gnus-agent)
2815                                                    gnus-plugged))
2816     (gnus-summary-reply-with-original "mail/reply")
2817     (gnus-summary-reply "mail/reply" nil :visible nil)
2818     (gnus-summary-followup-with-original "mail/reply-all")
2819     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2820     (gnus-summary-mail-forward "mail/forward")
2821     (gnus-summary-save-article "mail/save")
2822     (gnus-summary-search-article-forward "search" nil :visible nil)
2823     (gnus-summary-print-article "print")
2824     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2825     ;; Some new commands that may need more suitable icons:
2826     (gnus-summary-save-newsrc "save" nil :visible nil)
2827     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2828     (gnus-summary-prev-article "left-arrow")
2829     (gnus-summary-next-article "right-arrow")
2830     (gnus-summary-next-page "next-page")
2831     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2832     ;;
2833     ;; Maybe some sort-by-... could be added:
2834     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2835     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2836     (gnus-summary-mark-as-expirable
2837      "delete" nil
2838      :visible (gnus-check-backend-function 'request-expire-articles
2839                                            gnus-newsgroup-name))
2840     (gnus-summary-mark-as-spam
2841      "mail/spam" t
2842      :visible (and (fboundp 'spam-group-ham-contents-p)
2843                    (spam-group-ham-contents-p gnus-newsgroup-name))
2844      :help "Mark as spam")
2845     (gnus-summary-mark-as-read-forward
2846      "mail/not-spam" nil
2847      :visible (and (fboundp 'spam-group-spam-contents-p)
2848                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2849     ;;
2850     (gnus-summary-exit "exit")
2851     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2852     (gnus-info-find-node "help"))
2853   "List of functions for the summary tool bar (GNOME style).
2854
2855 See `gmm-tool-bar-from-list' for the format of the list."
2856   :type '(repeat gmm-tool-bar-item)
2857   :version "23.1" ;; No Gnus
2858   :initialize 'custom-initialize-default
2859   :set 'gnus-summary-tool-bar-update
2860   :group 'gnus-summary)
2861
2862 (defcustom gnus-summary-tool-bar-retro
2863   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2864     (gnus-summary-next-unread-article "gnus/next-ur")
2865     (gnus-summary-post-news "gnus/post")
2866     (gnus-summary-followup-with-original "gnus/fuwo")
2867     (gnus-summary-followup "gnus/followup")
2868     (gnus-summary-reply-with-original "gnus/reply-wo")
2869     (gnus-summary-reply "gnus/reply")
2870     (gnus-summary-caesar-message "gnus/rot13")
2871     (gnus-uu-decode-uu "gnus/uu-decode")
2872     (gnus-summary-save-article-file "gnus/save-aif")
2873     (gnus-summary-save-article "gnus/save-art")
2874     (gnus-uu-post-news "gnus/uu-post")
2875     (gnus-summary-catchup "gnus/catchup")
2876     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2877     (gnus-summary-exit "gnus/exit-summ")
2878     ;; Some new command that may need more suitable icons:
2879     (gnus-summary-print-article "gnus/print" nil :visible nil)
2880     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2881     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2882     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2883     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2884     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2885     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2886     ;;
2887     (gnus-info-find-node "gnus/help" nil :visible nil))
2888   "List of functions for the summary tool bar (retro look).
2889
2890 See `gmm-tool-bar-from-list' for the format of the list."
2891   :type '(repeat gmm-tool-bar-item)
2892   :version "23.1" ;; No Gnus
2893   :initialize 'custom-initialize-default
2894   :set 'gnus-summary-tool-bar-update
2895   :group 'gnus-summary)
2896
2897 (defcustom gnus-summary-tool-bar-zap-list t
2898   "List of icon items from the global tool bar.
2899 These items are not displayed in the Gnus summary mode tool bar.
2900
2901 See `gmm-tool-bar-from-list' for the format of the list."
2902   :type 'gmm-tool-bar-zap-list
2903   :version "23.1" ;; No Gnus
2904   :initialize 'custom-initialize-default
2905   :set 'gnus-summary-tool-bar-update
2906   :group 'gnus-summary)
2907
2908 (defvar image-load-path)
2909 (defvar tool-bar-map)
2910
2911 (defun gnus-summary-make-tool-bar (&optional force)
2912   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2913 When FORCE, rebuild the tool bar."
2914   (when (and (not (featurep 'xemacs))
2915              (boundp 'tool-bar-mode)
2916              tool-bar-mode
2917              (or (not gnus-summary-tool-bar-map) force))
2918     (let* ((load-path
2919             (gmm-image-load-path-for-library "gnus"
2920                                              "mail/save.xpm"
2921                                              nil t))
2922            (image-load-path (cons (car load-path)
2923                                   (when (boundp 'image-load-path)
2924                                     image-load-path)))
2925            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2926                                         gnus-summary-tool-bar-zap-list
2927                                         'gnus-summary-mode-map)))
2928       (when map
2929         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2930         ;; uses it's value.
2931         (setq gnus-summary-tool-bar-map map))))
2932   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2933
2934 (defun gnus-score-set-default (var value)
2935   "A version of set that updates the GNU Emacs menu-bar."
2936   (set var value)
2937   ;; It is the message that forces the active status to be updated.
2938   (message ""))
2939
2940 (defun gnus-make-score-map (type)
2941   "Make a summary score map of type TYPE."
2942   (if t
2943       nil
2944     (let ((headers '(("author" "from" string)
2945                      ("subject" "subject" string)
2946                      ("article body" "body" string)
2947                      ("article head" "head" string)
2948                      ("xref" "xref" string)
2949                      ("extra header" "extra" string)
2950                      ("lines" "lines" number)
2951                      ("followups to author" "followup" string)))
2952           (types '((number ("less than" <)
2953                            ("greater than" >)
2954                            ("equal" =))
2955                    (string ("substring" s)
2956                            ("exact string" e)
2957                            ("fuzzy string" f)
2958                            ("regexp" r))))
2959           (perms '(("temporary" (current-time-string))
2960                    ("permanent" nil)
2961                    ("immediate" now)))
2962           header)
2963       (list
2964        (apply
2965         'nconc
2966         (list
2967          (if (eq type 'lower)
2968              "Lower score"
2969            "Increase score"))
2970         (let (outh)
2971           (while headers
2972             (setq header (car headers))
2973             (setq outh
2974                   (cons
2975                    (apply
2976                     'nconc
2977                     (list (car header))
2978                     (let ((ts (cdr (assoc (nth 2 header) types)))
2979                           outt)
2980                       (while ts
2981                         (setq outt
2982                               (cons
2983                                (apply
2984                                 'nconc
2985                                 (list (caar ts))
2986                                 (let ((ps perms)
2987                                       outp)
2988                                   (while ps
2989                                     (setq outp
2990                                           (cons
2991                                            (vector
2992                                             (caar ps)
2993                                             (list
2994                                              'gnus-summary-score-entry
2995                                              (nth 1 header)
2996                                              (if (or (string= (nth 1 header)
2997                                                               "head")
2998                                                      (string= (nth 1 header)
2999                                                               "body"))
3000                                                  ""
3001                                                (list 'gnus-summary-header
3002                                                      (nth 1 header)))
3003                                              (list 'quote (nth 1 (car ts)))
3004                                              (list 'gnus-score-delta-default
3005                                                    nil)
3006                                              (nth 1 (car ps))
3007                                              t)
3008                                             t)
3009                                            outp))
3010                                     (setq ps (cdr ps)))
3011                                   (list (nreverse outp))))
3012                                outt))
3013                         (setq ts (cdr ts)))
3014                       (list (nreverse outt))))
3015                    outh))
3016             (setq headers (cdr headers)))
3017           (list (nreverse outh))))))))
3018
3019
3020 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3021 (defvar bookmark-make-record-function)
3022 \f
3023
3024 (defun gnus-summary-mode (&optional group)
3025   "Major mode for reading articles.
3026
3027 All normal editing commands are switched off.
3028 \\<gnus-summary-mode-map>
3029 Each line in this buffer represents one article.  To read an
3030 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3031 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3032 respectively.
3033
3034 You can also post articles and send mail from this buffer.  To
3035 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3036 of an article, type `\\[gnus-summary-reply]'.
3037
3038 There are approx. one gazillion commands you can execute in this
3039 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3040
3041 The following commands are available:
3042
3043 \\{gnus-summary-mode-map}"
3044   (interactive)
3045   (kill-all-local-variables)
3046   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3047     (gnus-summary-make-local-variables))
3048   (gnus-summary-make-local-variables)
3049   (setq gnus-newsgroup-name group)
3050   (when (gnus-visual-p 'summary-menu 'menu)
3051     (gnus-summary-make-menu-bar)
3052     (gnus-summary-make-tool-bar))
3053   (gnus-make-thread-indent-array)
3054   (gnus-simplify-mode-line)
3055   (setq major-mode 'gnus-summary-mode)
3056   (setq mode-name "Summary")
3057   (use-local-map gnus-summary-mode-map)
3058   (buffer-disable-undo)
3059   (setq buffer-read-only t              ;Disable modification
3060         show-trailing-whitespace nil)
3061   (setq truncate-lines t)
3062   (add-to-invisibility-spec '(gnus-sum . t))
3063   (gnus-summary-set-display-table)
3064   (gnus-set-default-directory)
3065   (make-local-variable 'gnus-summary-line-format)
3066   (make-local-variable 'gnus-summary-line-format-spec)
3067   (make-local-variable 'gnus-summary-dummy-line-format)
3068   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3069   (make-local-variable 'gnus-summary-mark-positions)
3070   (gnus-make-local-hook 'pre-command-hook)
3071   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3072   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3073   (turn-on-gnus-mailing-list-mode)
3074   (mm-enable-multibyte)
3075   (set (make-local-variable 'bookmark-make-record-function)
3076        'gnus-summary-bookmark-make-record)
3077   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3078   (gnus-update-summary-mark-positions))
3079
3080 (defun gnus-summary-make-local-variables ()
3081   "Make all the local summary buffer variables."
3082   (let (global)
3083     (dolist (local gnus-summary-local-variables)
3084       (if (consp local)
3085           (progn
3086             (if (eq (cdr local) 'global)
3087                 ;; Copy the global value of the variable.
3088                 (setq global (symbol-value (car local)))
3089               ;; Use the value from the list.
3090               (setq global (eval (cdr local))))
3091             (set (make-local-variable (car local)) global))
3092         ;; Simple nil-valued local variable.
3093         (set (make-local-variable local) nil)))))
3094
3095 (defun gnus-summary-clear-local-variables ()
3096   (let ((locals gnus-summary-local-variables))
3097     (while locals
3098       (if (consp (car locals))
3099           (and (symbolp (caar locals))
3100                (set (caar locals) nil))
3101         (and (symbolp (car locals))
3102              (set (car locals) nil)))
3103       (setq locals (cdr locals)))))
3104
3105 ;; Summary data functions.
3106
3107 (defmacro gnus-data-number (data)
3108   `(car ,data))
3109
3110 (defmacro gnus-data-set-number (data number)
3111   `(setcar ,data ,number))
3112
3113 (defmacro gnus-data-mark (data)
3114   `(nth 1 ,data))
3115
3116 (defmacro gnus-data-set-mark (data mark)
3117   `(setcar (nthcdr 1 ,data) ,mark))
3118
3119 (defmacro gnus-data-pos (data)
3120   `(nth 2 ,data))
3121
3122 (defmacro gnus-data-set-pos (data pos)
3123   `(setcar (nthcdr 2 ,data) ,pos))
3124
3125 (defmacro gnus-data-header (data)
3126   `(nth 3 ,data))
3127
3128 (defmacro gnus-data-set-header (data header)
3129   `(setf (nth 3 ,data) ,header))
3130
3131 (defmacro gnus-data-level (data)
3132   `(nth 4 ,data))
3133
3134 (defmacro gnus-data-unread-p (data)
3135   `(= (nth 1 ,data) gnus-unread-mark))
3136
3137 (defmacro gnus-data-read-p (data)
3138   `(/= (nth 1 ,data) gnus-unread-mark))
3139
3140 (defmacro gnus-data-pseudo-p (data)
3141   `(consp (nth 3 ,data)))
3142
3143 (defmacro gnus-data-find (number)
3144   `(assq ,number gnus-newsgroup-data))
3145
3146 (defmacro gnus-data-find-list (number &optional data)
3147   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3148      (memq (assq ,number bdata)
3149            bdata)))
3150
3151 (defmacro gnus-data-make (number mark pos header level)
3152   `(list ,number ,mark ,pos ,header ,level))
3153
3154 (defun gnus-data-enter (after-article number mark pos header level offset)
3155   (let ((data (gnus-data-find-list after-article)))
3156     (unless data
3157       (error "No such article: %d" after-article))
3158     (setcdr data (cons (gnus-data-make number mark pos header level)
3159                        (cdr data)))
3160     (setq gnus-newsgroup-data-reverse nil)
3161     (gnus-data-update-list (cddr data) offset)))
3162
3163 (defun gnus-data-enter-list (after-article list &optional offset)
3164   (when list
3165     (let ((data (and after-article (gnus-data-find-list after-article)))
3166           (ilist list))
3167       (if (not (or data
3168                    after-article))
3169           (let ((odata gnus-newsgroup-data))
3170             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3171             (when offset
3172               (gnus-data-update-list odata offset)))
3173         ;; Find the last element in the list to be spliced into the main
3174         ;; list.
3175         (setq list (last list))
3176         (if (not data)
3177             (progn
3178               (setcdr list gnus-newsgroup-data)
3179               (setq gnus-newsgroup-data ilist)
3180               (when offset
3181                 (gnus-data-update-list (cdr list) offset)))
3182           (setcdr list (cdr data))
3183           (setcdr data ilist)
3184           (when offset
3185             (gnus-data-update-list (cdr list) offset))))
3186       (setq gnus-newsgroup-data-reverse nil))))
3187
3188 (defun gnus-data-remove (article &optional offset)
3189   (let ((data gnus-newsgroup-data))
3190     (if (= (gnus-data-number (car data)) article)
3191         (progn
3192           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3193                 gnus-newsgroup-data-reverse nil)
3194           (when offset
3195             (gnus-data-update-list gnus-newsgroup-data offset)))
3196       (while (cdr data)
3197         (when (= (gnus-data-number (cadr data)) article)
3198           (setcdr data (cddr data))
3199           (when offset
3200             (gnus-data-update-list (cdr data) offset))
3201           (setq data nil
3202                 gnus-newsgroup-data-reverse nil))
3203         (setq data (cdr data))))))
3204
3205 (defmacro gnus-data-list (backward)
3206   `(if ,backward
3207        (or gnus-newsgroup-data-reverse
3208            (setq gnus-newsgroup-data-reverse
3209                  (reverse gnus-newsgroup-data)))
3210      gnus-newsgroup-data))
3211
3212 (defun gnus-data-update-list (data offset)
3213   "Add OFFSET to the POS of all data entries in DATA."
3214   (setq gnus-newsgroup-data-reverse nil)
3215   (while data
3216     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3217     (setq data (cdr data))))
3218
3219 (defun gnus-summary-article-pseudo-p (article)
3220   "Say whether this article is a pseudo article or not."
3221   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3222
3223 (defmacro gnus-summary-article-sparse-p (article)
3224   "Say whether this article is a sparse article or not."
3225   `(memq ,article gnus-newsgroup-sparse))
3226
3227 (defmacro gnus-summary-article-ancient-p (article)
3228   "Say whether this article is a sparse article or not."
3229   `(memq ,article gnus-newsgroup-ancient))
3230
3231 (defun gnus-article-parent-p (number)
3232   "Say whether this article is a parent or not."
3233   (let ((data (gnus-data-find-list number)))
3234     (and (cdr data)              ; There has to be an article after...
3235          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3236             (gnus-data-level (nth 1 data))))))
3237
3238 (defun gnus-article-children (number)
3239   "Return a list of all children to NUMBER."
3240   (let* ((data (gnus-data-find-list number))
3241          (level (gnus-data-level (car data)))
3242          children)
3243     (setq data (cdr data))
3244     (while (and data
3245                 (= (gnus-data-level (car data)) (1+ level)))
3246       (push (gnus-data-number (car data)) children)
3247       (setq data (cdr data)))
3248     children))
3249
3250 (defmacro gnus-summary-skip-intangible ()
3251   "If the current article is intangible, then jump to a different article."
3252   '(let ((to (get-text-property (point) 'gnus-intangible)))
3253      (and to (gnus-summary-goto-subject to))))
3254
3255 (defmacro gnus-summary-article-intangible-p ()
3256   "Say whether this article is intangible or not."
3257   '(get-text-property (point) 'gnus-intangible))
3258
3259 (defun gnus-article-read-p (article)
3260   "Say whether ARTICLE is read or not."
3261   (not (or (memq article gnus-newsgroup-marked)
3262            (memq article gnus-newsgroup-spam-marked)
3263            (memq article gnus-newsgroup-unreads)
3264            (memq article gnus-newsgroup-unselected)
3265            (memq article gnus-newsgroup-dormant))))
3266
3267 ;; Some summary mode macros.
3268
3269 (defmacro gnus-summary-article-number ()
3270   "The article number of the article on the current line.
3271 If there isn't an article number here, then we return the current
3272 article number."
3273   '(progn
3274      (gnus-summary-skip-intangible)
3275      (or (get-text-property (point) 'gnus-number)
3276          (gnus-summary-last-subject))))
3277
3278 (defmacro gnus-summary-article-header (&optional number)
3279   "Return the header of article NUMBER."
3280   `(gnus-data-header (gnus-data-find
3281                       ,(or number '(gnus-summary-article-number)))))
3282
3283 (defmacro gnus-summary-thread-level (&optional number)
3284   "Return the level of thread that starts with article NUMBER."
3285   `(if (and (eq gnus-summary-make-false-root 'dummy)
3286             (get-text-property (point) 'gnus-intangible))
3287        0
3288      (gnus-data-level (gnus-data-find
3289                        ,(or number '(gnus-summary-article-number))))))
3290
3291 (defmacro gnus-summary-article-mark (&optional number)
3292   "Return the mark of article NUMBER."
3293   `(gnus-data-mark (gnus-data-find
3294                     ,(or number '(gnus-summary-article-number)))))
3295
3296 (defmacro gnus-summary-article-pos (&optional number)
3297   "Return the position of the line of article NUMBER."
3298   `(gnus-data-pos (gnus-data-find
3299                    ,(or number '(gnus-summary-article-number)))))
3300
3301 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3302 (defmacro gnus-summary-article-subject (&optional number)
3303   "Return current subject string or nil if nothing."
3304   `(let ((headers
3305           ,(if number
3306                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3307              '(gnus-data-header (assq (gnus-summary-article-number)
3308                                       gnus-newsgroup-data)))))
3309      (and headers
3310           (vectorp headers)
3311           (mail-header-subject headers))))
3312
3313 (defmacro gnus-summary-article-score (&optional number)
3314   "Return current article score."
3315   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3316                   gnus-newsgroup-scored))
3317        gnus-summary-default-score 0))
3318
3319 (defun gnus-summary-article-children (&optional number)
3320   "Return a list of article numbers that are children of article NUMBER."
3321   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3322          (level (gnus-data-level (car data)))
3323          l children)
3324     (while (and (setq data (cdr data))
3325                 (> (setq l (gnus-data-level (car data))) level))
3326       (and (= (1+ level) l)
3327            (push (gnus-data-number (car data))
3328                  children)))
3329     (nreverse children)))
3330
3331 (defun gnus-summary-article-parent (&optional number)
3332   "Return the article number of the parent of article NUMBER."
3333   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3334                                     (gnus-data-list t)))
3335          (level (gnus-data-level (car data))))
3336     (if (zerop level)
3337         ()                              ; This is a root.
3338       ;; We search until we find an article with a level less than
3339       ;; this one.  That function has to be the parent.
3340       (while (and (setq data (cdr data))
3341                   (not (< (gnus-data-level (car data)) level))))
3342       (and data (gnus-data-number (car data))))))
3343
3344 (defun gnus-unread-mark-p (mark)
3345   "Say whether MARK is the unread mark."
3346   (= mark gnus-unread-mark))
3347
3348 (defun gnus-read-mark-p (mark)
3349   "Say whether MARK is one of the marks that mark as read.
3350 This is all marks except unread, ticked, dormant, and expirable."
3351   (not (or (= mark gnus-unread-mark)
3352            (= mark gnus-ticked-mark)
3353            (= mark gnus-spam-mark)
3354            (= mark gnus-dormant-mark)
3355            (= mark gnus-expirable-mark))))
3356
3357 (defmacro gnus-article-mark (number)
3358   "Return the MARK of article NUMBER.
3359 This macro should only be used when computing the mark the \"first\"
3360 time; i.e., when generating the summary lines.  After that,
3361 `gnus-summary-article-mark' should be used to examine the
3362 marks of articles."
3363   `(cond
3364     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3365     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3366     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3367     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3368     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3369     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3370     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3371     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3372            gnus-ancient-mark))))
3373
3374 ;; Saving hidden threads.
3375
3376 (defmacro gnus-save-hidden-threads (&rest forms)
3377   "Save hidden threads, eval FORMS, and restore the hidden threads."
3378   (let ((config (make-symbol "config")))
3379     `(let ((,config (gnus-hidden-threads-configuration)))
3380        (unwind-protect
3381            (save-excursion
3382              ,@forms)
3383          (gnus-restore-hidden-threads-configuration ,config)))))
3384 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3385 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3386
3387 (defun gnus-data-compute-positions ()
3388   "Compute the positions of all articles."
3389   (setq gnus-newsgroup-data-reverse nil)
3390   (let ((data gnus-newsgroup-data))
3391     (save-excursion
3392       (gnus-save-hidden-threads
3393         (gnus-summary-show-all-threads)
3394         (goto-char (point-min))
3395         (while data
3396           (while (get-text-property (point) 'gnus-intangible)
3397             (forward-line 1))
3398           (gnus-data-set-pos (car data) (+ (point) 3))
3399           (setq data (cdr data))
3400           (forward-line 1))))))
3401
3402 (defun gnus-hidden-threads-configuration ()
3403   "Return the current hidden threads configuration."
3404   (save-excursion
3405     (let (config)
3406       (goto-char (point-min))
3407       (while (not (eobp))
3408         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3409           (push (save-excursion (forward-line 0) (point)) config))
3410         (forward-line 1))
3411       config)))
3412
3413 (defun gnus-restore-hidden-threads-configuration (config)
3414   "Restore hidden threads configuration from CONFIG."
3415   (save-excursion
3416     (let (point (inhibit-read-only t))
3417       (while (setq point (pop config))
3418         (goto-char point)
3419         (gnus-summary-hide-thread)))))
3420
3421 ;; Various summary mode internalish functions.
3422
3423 (defun gnus-mouse-pick-article (e)
3424   (interactive "e")
3425   (mouse-set-point e)
3426   (gnus-summary-next-page nil t))
3427
3428 (defun gnus-summary-set-display-table ()
3429   "Change the display table.
3430 Odd characters have a tendency to mess
3431 up nicely formatted displays - we make all possible glyphs
3432 display only a single character."
3433
3434   ;; We start from the standard display table, if any.
3435   (let ((table (or (copy-sequence standard-display-table)
3436                    (make-display-table)))
3437         (i 32))
3438     ;; Nix out all the control chars...
3439     (while (>= (setq i (1- i)) 0)
3440       (gnus-put-display-table i [??] table))
3441    ;; ... but not newline and cr, of course.  (cr is necessary for the
3442     ;; selective display).
3443     (gnus-put-display-table ?\n nil table)
3444     (gnus-put-display-table ?\r nil table)
3445     ;; We keep TAB as well.
3446     (gnus-put-display-table ?\t nil table)
3447     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3448     ;; Emacs 23 (unicode), that are not set already.
3449     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3450                  160
3451                256)))
3452       (while (>= (setq i (1- i)) 127)
3453         ;; Only modify if the entry is nil.
3454         (unless (gnus-get-display-table i table)
3455           (gnus-put-display-table i [??] table))))
3456     (setq buffer-display-table table)))
3457
3458 (defun gnus-summary-set-article-display-arrow (pos)
3459   "Update the overlay arrow to point to line at position POS."
3460   (when gnus-summary-display-arrow
3461     (make-local-variable 'overlay-arrow-position)
3462     (make-local-variable 'overlay-arrow-string)
3463     (save-excursion
3464       (goto-char pos)
3465       (beginning-of-line)
3466       (unless overlay-arrow-position
3467         (setq overlay-arrow-position (make-marker)))
3468       (setq overlay-arrow-string "=>"
3469             overlay-arrow-position (set-marker overlay-arrow-position
3470                                                (point)
3471                                                (current-buffer))))))
3472
3473 (defun gnus-summary-setup-buffer (group)
3474   "Initialize summary buffer."
3475   (let ((buffer (gnus-summary-buffer-name group))
3476         (dead-name (concat "*Dead Summary "
3477                            (gnus-group-decoded-name group) "*")))
3478     ;; If a dead summary buffer exists, we kill it.
3479     (when (gnus-buffer-live-p dead-name)
3480       (gnus-kill-buffer dead-name))
3481     (if (get-buffer buffer)
3482         (progn
3483           (set-buffer buffer)
3484           (setq gnus-summary-buffer (current-buffer))
3485           (not gnus-newsgroup-prepared))
3486       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3487       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3488       (gnus-summary-mode group)
3489       (when gnus-carpal
3490         (gnus-carpal-setup-buffer 'summary))
3491       (when (gnus-group-quit-config group)
3492         (set (make-local-variable 'gnus-single-article-buffer) nil))
3493       (make-local-variable 'gnus-article-buffer)
3494       (make-local-variable 'gnus-article-current)
3495       (make-local-variable 'gnus-original-article-buffer)
3496       (setq gnus-newsgroup-name group)
3497       ;; Set any local variables in the group parameters.
3498       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3499       t)))
3500
3501 (defun gnus-set-global-variables ()
3502   "Set the global equivalents of the buffer-local variables.
3503 They are set to the latest values they had.  These reflect the summary
3504 buffer that was in action when the last article was fetched."
3505   (when (eq major-mode 'gnus-summary-mode)
3506     (setq gnus-summary-buffer (current-buffer))
3507     (let ((name gnus-newsgroup-name)
3508           (marked gnus-newsgroup-marked)
3509           (spam gnus-newsgroup-spam-marked)
3510           (unread gnus-newsgroup-unreads)
3511           (headers gnus-current-headers)
3512           (data gnus-newsgroup-data)
3513           (summary gnus-summary-buffer)
3514           (article-buffer gnus-article-buffer)
3515           (original gnus-original-article-buffer)
3516           (gac gnus-article-current)
3517           (reffed gnus-reffed-article-number)
3518           (score-file gnus-current-score-file)
3519           (default-charset gnus-newsgroup-charset)
3520           vlist)
3521       (let ((locals gnus-newsgroup-variables))
3522         (while locals
3523           (if (consp (car locals))
3524               (push (eval (caar locals)) vlist)
3525             (push (eval (car locals)) vlist))
3526           (setq locals (cdr locals)))
3527         (setq vlist (nreverse vlist)))
3528       (with-current-buffer gnus-group-buffer
3529         (setq gnus-newsgroup-name name
3530               gnus-newsgroup-marked marked
3531               gnus-newsgroup-spam-marked spam
3532               gnus-newsgroup-unreads unread
3533               gnus-current-headers headers
3534               gnus-newsgroup-data data
3535               gnus-article-current gac
3536               gnus-summary-buffer summary
3537               gnus-article-buffer article-buffer
3538               gnus-original-article-buffer original
3539               gnus-reffed-article-number reffed
3540               gnus-current-score-file score-file
3541               gnus-newsgroup-charset default-charset)
3542         (let ((locals gnus-newsgroup-variables))
3543           (while locals
3544             (if (consp (car locals))
3545                 (set (caar locals) (pop vlist))
3546               (set (car locals) (pop vlist)))
3547             (setq locals (cdr locals))))
3548         ;; The article buffer also has local variables.
3549         (when (gnus-buffer-live-p gnus-article-buffer)
3550           (set-buffer gnus-article-buffer)
3551           (setq gnus-summary-buffer summary))))))
3552
3553 (defun gnus-summary-article-unread-p (article)
3554   "Say whether ARTICLE is unread or not."
3555   (memq article gnus-newsgroup-unreads))
3556
3557 (defun gnus-summary-first-article-p (&optional article)
3558   "Return whether ARTICLE is the first article in the buffer."
3559   (if (not (setq article (or article (gnus-summary-article-number))))
3560       nil
3561     (eq article (caar gnus-newsgroup-data))))
3562
3563 (defun gnus-summary-last-article-p (&optional article)
3564   "Return whether ARTICLE is the last article in the buffer."
3565   (if (not (setq article (or article (gnus-summary-article-number))))
3566       ;; All non-existent numbers are the last article.  :-)
3567       t
3568     (not (cdr (gnus-data-find-list article)))))
3569
3570 (defun gnus-make-thread-indent-array (&optional n)
3571   (when (or n
3572             (progn (setq n 200) nil)
3573             (null gnus-thread-indent-array)
3574             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3575     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3576           gnus-thread-indent-array-level gnus-thread-indent-level)
3577     (while (>= n 0)
3578       (aset gnus-thread-indent-array n
3579             (make-string (* n gnus-thread-indent-level) ? ))
3580       (setq n (1- n)))))
3581
3582 (defun gnus-update-summary-mark-positions ()
3583   "Compute where the summary marks are to go."
3584   (save-excursion
3585     (when (gnus-buffer-exists-p gnus-summary-buffer)
3586       (set-buffer gnus-summary-buffer))
3587     (let ((spec gnus-summary-line-format-spec)
3588           pos)
3589       (save-excursion
3590         (gnus-set-work-buffer)
3591         (let ((gnus-tmp-unread ?Z)
3592               (gnus-replied-mark ?Z)
3593               (gnus-score-below-mark ?Z)
3594               (gnus-score-over-mark ?Z)
3595               (gnus-undownloaded-mark ?Z)
3596               (gnus-summary-line-format-spec spec)
3597               (gnus-newsgroup-downloadable '(0))
3598               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3599               case-fold-search ignores)
3600           ;; Here, all marks are bound to Z.
3601           (gnus-summary-insert-line header
3602                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3603           (goto-char (point-min))
3604           ;; Memorize the positions of the same characters as dummy marks.
3605           (while (re-search-forward "[A-D]" nil t)
3606             (push (point) ignores))
3607           (erase-buffer)
3608           ;; We use A-D as dummy marks in order to know column positions
3609           ;; where marks should be inserted.
3610           (setq gnus-tmp-unread ?A
3611                 gnus-replied-mark ?B
3612                 gnus-score-below-mark ?C
3613                 gnus-score-over-mark ?C
3614                 gnus-undownloaded-mark ?D)
3615           (gnus-summary-insert-line header
3616                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3617           ;; Ignore characters which aren't dummy marks.
3618           (dolist (p ignores)
3619             (delete-region (goto-char (1- p)) p)
3620             (insert ?Z))
3621           (goto-char (point-min))
3622           (setq pos (list (cons 'unread
3623                                 (and (search-forward "A" nil t)
3624                                      (- (point) (point-min) 1)))))
3625           (goto-char (point-min))
3626           (push (cons 'replied (and (search-forward "B" nil t)
3627                                     (- (point) (point-min) 1)))
3628                 pos)
3629           (goto-char (point-min))
3630           (push (cons 'score (and (search-forward "C" nil t)
3631                                   (- (point) (point-min) 1)))
3632                 pos)
3633           (goto-char (point-min))
3634           (push (cons 'download (and (search-forward "D" nil t)
3635                                      (- (point) (point-min) 1)))
3636                 pos)))
3637       (setq gnus-summary-mark-positions pos))))
3638
3639 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3640   "Insert a dummy root in the summary buffer."
3641   (beginning-of-line)
3642   (gnus-add-text-properties
3643    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3644    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3645
3646 (defun gnus-summary-extract-address-component (from)
3647   (or (car (funcall gnus-extract-address-components from))
3648       from))
3649
3650 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3651   (let ((mail-parse-charset gnus-newsgroup-charset)
3652         (ignored-from-addresses (gnus-ignored-from-addresses))
3653         ; Is it really necessary to do this next part for each summary line?
3654         ; Luckily, doesn't seem to slow things down much.
3655         (mail-parse-ignored-charsets
3656          (with-current-buffer gnus-summary-buffer
3657            gnus-newsgroup-ignored-charsets)))
3658     (or
3659      (and ignored-from-addresses
3660           (string-match ignored-from-addresses gnus-tmp-from)
3661           (let ((extra-headers (mail-header-extra header))
3662                 to
3663                 newsgroups)
3664             (cond
3665              ((setq to (cdr (assq 'To extra-headers)))
3666               (concat gnus-summary-to-prefix
3667                       (inline
3668                         (gnus-summary-extract-address-component
3669                          (funcall gnus-decode-encoded-address-function to)))))
3670              ((setq newsgroups
3671                     (or
3672                      (cdr (assq 'Newsgroups extra-headers))
3673                      (and
3674                       (memq 'Newsgroups gnus-extra-headers)
3675                       (eq (car (gnus-find-method-for-group
3676                                 gnus-newsgroup-name)) 'nntp)
3677                       (gnus-group-real-name gnus-newsgroup-name))))
3678               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3679      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3680
3681 (defun gnus-summary-insert-line (gnus-tmp-header
3682                                  gnus-tmp-level gnus-tmp-current
3683                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3684                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3685                                  &optional gnus-tmp-dummy gnus-tmp-score
3686                                  gnus-tmp-process)
3687   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3688       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3689                                           gnus-tmp-level)))
3690   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3691          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3692          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3693          (gnus-tmp-score-char
3694           (if (or (null gnus-summary-default-score)
3695                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3696                       gnus-summary-zcore-fuzz))
3697               ?                         ;Whitespace
3698             (if (< gnus-tmp-score gnus-summary-default-score)
3699                 gnus-score-below-mark gnus-score-over-mark)))
3700          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3701          (gnus-tmp-replied
3702           (cond (gnus-tmp-process gnus-process-mark)
3703                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3704                  gnus-cached-mark)
3705                 (gnus-tmp-replied gnus-replied-mark)
3706                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3707                  gnus-forwarded-mark)
3708                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3709                  gnus-saved-mark)
3710                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3711                  gnus-recent-mark)
3712                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3713                  gnus-unseen-mark)
3714                 (t gnus-no-mark)))
3715          (gnus-tmp-downloaded
3716           (cond (undownloaded
3717                  gnus-undownloaded-mark)
3718                 (gnus-newsgroup-agentized
3719                  gnus-downloaded-mark)
3720                 (t
3721                  gnus-no-mark)))
3722          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3723          (gnus-tmp-name
3724           (cond
3725            ((string-match "<[^>]+> *$" gnus-tmp-from)
3726             (let ((beg (match-beginning 0)))
3727               (or (and (string-match "^\".+\"" gnus-tmp-from)
3728                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3729                   (substring gnus-tmp-from 0 beg))))
3730            ((string-match "(.+)" gnus-tmp-from)
3731             (substring gnus-tmp-from
3732                        (1+ (match-beginning 0)) (1- (match-end 0))))
3733            (t gnus-tmp-from)))
3734          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3735          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3736          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3737          (inhibit-read-only t))
3738     (when (string= gnus-tmp-name "")
3739       (setq gnus-tmp-name gnus-tmp-from))
3740     (unless (numberp gnus-tmp-lines)
3741       (setq gnus-tmp-lines -1))
3742     (if (= gnus-tmp-lines -1)
3743         (setq gnus-tmp-lines "?")
3744       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3745     (condition-case ()
3746         (gnus-put-text-property
3747          (point)
3748          (progn (eval gnus-summary-line-format-spec) (point))
3749          'gnus-number gnus-tmp-number)
3750       (error (gnus-message 5 "Error updating the summary line")))
3751     (when (gnus-visual-p 'summary-highlight 'highlight)
3752       (forward-line -1)
3753       (gnus-run-hooks 'gnus-summary-update-hook)
3754       (forward-line 1))))
3755
3756 (defun gnus-summary-update-line (&optional dont-update)
3757   "Update summary line after change."
3758   (when (and gnus-summary-default-score
3759              (not gnus-summary-inhibit-highlight))
3760     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3761            (article (gnus-summary-article-number))
3762            (score (gnus-summary-article-score article)))
3763       (unless dont-update
3764         (if (and gnus-summary-mark-below
3765                  (< (gnus-summary-article-score)
3766                     gnus-summary-mark-below))
3767             ;; This article has a low score, so we mark it as read.
3768             (when (memq article gnus-newsgroup-unreads)
3769               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3770           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3771             ;; This article was previously marked as read on account
3772             ;; of a low score, but now it has risen, so we mark it as
3773             ;; unread.
3774             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3775         (gnus-summary-update-mark
3776          (if (or (null gnus-summary-default-score)
3777                  (<= (abs (- score gnus-summary-default-score))
3778                      gnus-summary-zcore-fuzz))
3779              ?                          ;Whitespace
3780            (if (< score gnus-summary-default-score)
3781                gnus-score-below-mark gnus-score-over-mark))
3782          'score))
3783       ;; Do visual highlighting.
3784       (when (gnus-visual-p 'summary-highlight 'highlight)
3785         (gnus-run-hooks 'gnus-summary-update-hook)))))
3786
3787 (defvar gnus-tmp-new-adopts nil)
3788
3789 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3790   "Return the number of articles in THREAD.
3791 This may be 0 in some cases -- if none of the articles in
3792 the thread are to be displayed."
3793   (let* ((number
3794          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3795           (cond
3796            ((not (listp thread))
3797             1)
3798            ((and (consp thread) (cdr thread))
3799             (apply
3800              '+ 1 (mapcar
3801                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3802            ((null thread)
3803             1)
3804            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3805             1)
3806            (t 0))))
3807     (when (and level (zerop level) gnus-tmp-new-adopts)
3808       (incf number
3809             (apply '+ (mapcar
3810                        'gnus-summary-number-of-articles-in-thread
3811                        gnus-tmp-new-adopts))))
3812     (if char
3813         (if (> number 1) gnus-not-empty-thread-mark
3814           gnus-empty-thread-mark)
3815       number)))
3816
3817 (defsubst gnus-summary-line-message-size (head)
3818   "Return pretty-printed version of message size.
3819 This function is intended to be used in
3820 `gnus-summary-line-format-alist'."
3821   (let ((c (or (mail-header-chars head) -1)))
3822     (cond ((< c 0) "n/a")               ; chars not available
3823           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3824           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3825           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3826           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3827
3828
3829 (defun gnus-summary-set-local-parameters (group)
3830   "Go through the local params of GROUP and set all variable specs in that list."
3831   (let ((vars '(quit-config)))          ; Ignore quit-config.
3832     (dolist (elem (gnus-group-find-parameter group))
3833       (and (consp elem)                 ; Has to be a cons.
3834            (consp (cdr elem))           ; The cdr has to be a list.
3835            (symbolp (car elem))         ; Has to be a symbol in there.
3836            (not (memq (car elem) vars))
3837            (ignore-errors
3838              (push (car elem) vars)
3839              ;; Variables like `gnus-show-threads' that are globally
3840              ;; bound, if used as group parameters, need to get to be
3841              ;; buffer-local, whereas just parameters like `gcc-self',
3842              ;; `timestamp', etc. should not be bound as variables.
3843              (if (boundp (car elem))
3844                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3845                (eval (nth 1 elem))))))))
3846
3847 (defun gnus-summary-read-group (group &optional show-all no-article
3848                                       kill-buffer no-display backward
3849                                       select-articles)
3850   "Start reading news in newsgroup GROUP.
3851 If SHOW-ALL is non-nil, already read articles are also listed.
3852 If NO-ARTICLE is non-nil, no article is selected initially.
3853 If NO-DISPLAY, don't generate a summary buffer."
3854   (let (result)
3855     (while (and group
3856                 (null (setq result
3857                             (let ((gnus-auto-select-next nil))
3858                               (or (gnus-summary-read-group-1
3859                                    group show-all no-article
3860                                    kill-buffer no-display
3861                                    select-articles)
3862                                   (setq show-all nil
3863                                         select-articles nil)))))
3864                 (eq gnus-auto-select-next 'quietly))
3865       (set-buffer gnus-group-buffer)
3866       ;; The entry function called above goes to the next
3867       ;; group automatically, so we go two groups back
3868       ;; if we are searching for the previous group.
3869       (when backward
3870         (gnus-group-prev-unread-group 2))
3871       (if (not (equal group (gnus-group-group-name)))
3872           (setq group (gnus-group-group-name))
3873         (setq group nil)))
3874     result))
3875
3876 (defun gnus-summary-read-group-1 (group show-all no-article
3877                                         kill-buffer no-display
3878                                         &optional select-articles)
3879   ;; Killed foreign groups can't be entered.
3880   ;;  (when (and (not (gnus-group-native-p group))
3881   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3882   ;;    (error "Dead non-native groups can't be entered"))
3883   (gnus-message 5 "Retrieving newsgroup: %s..."
3884                 (gnus-group-decoded-name group))
3885   (let* ((new-group (gnus-summary-setup-buffer group))
3886          (quit-config (gnus-group-quit-config group))
3887          (did-select (and new-group (gnus-select-newsgroup
3888                                      group show-all select-articles))))
3889     (cond
3890      ;; This summary buffer exists already, so we just select it.
3891      ((not new-group)
3892       (gnus-set-global-variables)
3893       (when kill-buffer
3894         (gnus-kill-or-deaden-summary kill-buffer))
3895       (gnus-configure-windows 'summary 'force)
3896       (gnus-set-mode-line 'summary)
3897       (gnus-summary-position-point)
3898       (message "")
3899       t)
3900      ;; We couldn't select this group.
3901      ((null did-select)
3902       (when (and (eq major-mode 'gnus-summary-mode)
3903                  (not (equal (current-buffer) kill-buffer)))
3904         (kill-buffer (current-buffer))
3905         (if (not quit-config)
3906             (progn
3907               ;; Update the info -- marks might need to be removed,
3908               ;; for instance.
3909               (gnus-summary-update-info)
3910               (set-buffer gnus-group-buffer)
3911               (gnus-group-jump-to-group group)
3912               (gnus-group-next-unread-group 1))
3913           (gnus-handle-ephemeral-exit quit-config)))
3914       (let ((grpinfo (gnus-get-info group)))
3915         (if (null (gnus-info-read grpinfo))
3916             (gnus-message 3 "Group %s contains no messages"
3917                           (gnus-group-decoded-name group))
3918           (gnus-message 3 "Can't select group")))
3919       nil)
3920      ;; The user did a `C-g' while prompting for number of articles,
3921      ;; so we exit this group.
3922      ((eq did-select 'quit)
3923       (and (eq major-mode 'gnus-summary-mode)
3924            (not (equal (current-buffer) kill-buffer))
3925            (kill-buffer (current-buffer)))
3926       (when kill-buffer
3927         (gnus-kill-or-deaden-summary kill-buffer))
3928       (if (not quit-config)
3929           (progn
3930             (set-buffer gnus-group-buffer)
3931             (gnus-group-jump-to-group group)
3932             (gnus-configure-windows 'group 'force))
3933         (gnus-handle-ephemeral-exit quit-config))
3934       ;; Finally signal the quit.
3935       (signal 'quit nil))
3936      ;; The group was successfully selected.
3937      (t
3938       (gnus-set-global-variables)
3939       ;; Save the active value in effect when the group was entered.
3940       (setq gnus-newsgroup-active
3941             (gnus-copy-sequence
3942              (gnus-active gnus-newsgroup-name)))
3943       ;; You can change the summary buffer in some way with this hook.
3944       (gnus-run-hooks 'gnus-select-group-hook)
3945       (when (memq 'summary (gnus-update-format-specifications
3946                             nil 'summary 'summary-mode 'summary-dummy))
3947         ;; The format specification for the summary line was updated,
3948         ;; so we need to update the mark positions as well.
3949         (gnus-update-summary-mark-positions))
3950       ;; Do score processing.
3951       (when gnus-use-scoring
3952         (gnus-possibly-score-headers))
3953       ;; Check whether to fill in the gaps in the threads.
3954       (when gnus-build-sparse-threads
3955         (gnus-build-sparse-threads))
3956       ;; Find the initial limit.
3957       (if show-all
3958           (let ((gnus-newsgroup-dormant nil))
3959             (gnus-summary-initial-limit show-all))
3960         (gnus-summary-initial-limit show-all))
3961       ;; Generate the summary buffer.
3962       (unless no-display
3963         (gnus-summary-prepare))
3964       (when gnus-use-trees
3965         (gnus-tree-open group)
3966         (setq gnus-summary-highlight-line-function
3967               'gnus-tree-highlight-article))
3968       ;; If the summary buffer is empty, but there are some low-scored
3969       ;; articles or some excluded dormants, we include these in the
3970       ;; buffer.
3971       (when (and (zerop (buffer-size))
3972                  (not no-display))
3973         (cond (gnus-newsgroup-dormant
3974                (gnus-summary-limit-include-dormant))
3975               ((and gnus-newsgroup-scored show-all)
3976                (gnus-summary-limit-include-expunged t))))
3977       ;; Function `gnus-apply-kill-file' must be called in this hook.
3978       (gnus-run-hooks 'gnus-apply-kill-hook)
3979       (if (and (zerop (buffer-size))
3980                (not no-display))
3981           (progn
3982             ;; This newsgroup is empty.
3983             (gnus-summary-catchup-and-exit nil t)
3984             (gnus-message 6 "No unread news")
3985             (when kill-buffer
3986               (gnus-kill-or-deaden-summary kill-buffer))
3987             ;; Return nil from this function.
3988             nil)
3989         ;; Hide conversation thread subtrees.  We cannot do this in
3990         ;; gnus-summary-prepare-hook since kill processing may not
3991         ;; work with hidden articles.
3992         (gnus-summary-maybe-hide-threads)
3993         (when kill-buffer
3994           (gnus-kill-or-deaden-summary kill-buffer))
3995         (gnus-summary-auto-select-subject)
3996         ;; Show first unread article if requested.
3997         (if (and (not no-article)
3998                  (not no-display)
3999                  gnus-newsgroup-unreads
4000                  gnus-auto-select-first)
4001             (progn
4002               (gnus-configure-windows 'summary)
4003               (let ((art (gnus-summary-article-number)))
4004                 (unless (and (not gnus-plugged)
4005                              (or (memq art gnus-newsgroup-undownloaded)
4006                                  (memq art gnus-newsgroup-downloadable)))
4007                   (gnus-summary-goto-article art))))
4008           ;; Don't select any articles.
4009           (gnus-summary-position-point)
4010           (gnus-configure-windows 'summary 'force)
4011           (gnus-set-mode-line 'summary))
4012         (when (and gnus-auto-center-group
4013                    (get-buffer-window gnus-group-buffer t))
4014           ;; Gotta use windows, because recenter does weird stuff if
4015           ;; the current buffer ain't the displayed window.
4016           (let ((owin (selected-window)))
4017             (select-window (get-buffer-window gnus-group-buffer t))
4018             (when (gnus-group-goto-group group)
4019               (recenter))
4020             (select-window owin)))
4021         ;; Mark this buffer as "prepared".
4022         (setq gnus-newsgroup-prepared t)
4023         (gnus-run-hooks 'gnus-summary-prepared-hook)
4024         (unless (gnus-ephemeral-group-p group)
4025           (gnus-group-update-group group))
4026         t)))))
4027
4028 (defun gnus-summary-auto-select-subject ()
4029   "Select the subject line on initial group entry."
4030   (goto-char (point-min))
4031   (cond
4032    ((eq gnus-auto-select-subject 'best)
4033     (gnus-summary-best-unread-subject))
4034    ((eq gnus-auto-select-subject 'unread)
4035     (gnus-summary-first-unread-subject))
4036    ((eq gnus-auto-select-subject 'unseen)
4037     (gnus-summary-first-unseen-subject))
4038    ((eq gnus-auto-select-subject 'unseen-or-unread)
4039     (gnus-summary-first-unseen-or-unread-subject))
4040    ((eq gnus-auto-select-subject 'first)
4041     ;; Do nothing.
4042     )
4043    ((functionp gnus-auto-select-subject)
4044     (funcall gnus-auto-select-subject))))
4045
4046 (defun gnus-summary-prepare ()
4047   "Generate the summary buffer."
4048   (interactive)
4049   (let ((inhibit-read-only t))
4050     (erase-buffer)
4051     (setq gnus-newsgroup-data nil
4052           gnus-newsgroup-data-reverse nil)
4053     (gnus-run-hooks 'gnus-summary-generate-hook)
4054     ;; Generate the buffer, either with threads or without.
4055     (when gnus-newsgroup-headers
4056       (gnus-summary-prepare-threads
4057        (if gnus-show-threads
4058            (gnus-sort-gathered-threads
4059             (funcall gnus-summary-thread-gathering-function
4060                      (gnus-sort-threads
4061                       (gnus-cut-threads (gnus-make-threads)))))
4062          ;; Unthreaded display.
4063          (gnus-sort-articles gnus-newsgroup-headers))))
4064     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4065     ;; Call hooks for modifying summary buffer.
4066     (goto-char (point-min))
4067     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4068
4069 (defsubst gnus-general-simplify-subject (subject)
4070   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4071   (setq subject
4072         (cond
4073          ;; Truncate the subject.
4074          (gnus-simplify-subject-functions
4075           (gnus-map-function gnus-simplify-subject-functions subject))
4076          ((numberp gnus-summary-gather-subject-limit)
4077           (setq subject (gnus-simplify-subject-re subject))
4078           (if (> (length subject) gnus-summary-gather-subject-limit)
4079               (substring subject 0 gnus-summary-gather-subject-limit)
4080             subject))
4081          ;; Fuzzily simplify it.
4082          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4083           (gnus-simplify-subject-fuzzy subject))
4084          ;; Just remove the leading "Re:".
4085          (t
4086           (gnus-simplify-subject-re subject))))
4087
4088   (if (and gnus-summary-gather-exclude-subject
4089            (string-match gnus-summary-gather-exclude-subject subject))
4090       nil                         ; This article shouldn't be gathered
4091     subject))
4092
4093 (defun gnus-summary-simplify-subject-query ()
4094   "Query where the respool algorithm would put this article."
4095   (interactive)
4096   (gnus-summary-select-article)
4097   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4098
4099 (defun gnus-gather-threads-by-subject (threads)
4100   "Gather threads by looking at Subject headers."
4101   (if (not gnus-summary-make-false-root)
4102       threads
4103     (let ((hashtb (gnus-make-hashtable 1024))
4104           (prev threads)
4105           (result threads)
4106           subject hthread whole-subject)
4107       (while threads
4108         (setq subject (gnus-general-simplify-subject
4109                        (setq whole-subject (mail-header-subject
4110                                             (caar threads)))))
4111         (when subject
4112           (if (setq hthread (gnus-gethash subject hashtb))
4113               (progn
4114                 ;; We enter a dummy root into the thread, if we
4115                 ;; haven't done that already.
4116                 (unless (stringp (caar hthread))
4117                   (setcar hthread (list whole-subject (car hthread))))
4118                 ;; We add this new gathered thread to this gathered
4119                 ;; thread.
4120                 (setcdr (car hthread)
4121                         (nconc (cdar hthread) (list (car threads))))
4122                 ;; Remove it from the list of threads.
4123                 (setcdr prev (cdr threads))
4124                 (setq threads prev))
4125             ;; Enter this thread into the hash table.
4126             (gnus-sethash subject
4127                           (if gnus-summary-make-false-root-always
4128                               (progn
4129                                 ;; If you want a dummy root above all
4130                                 ;; threads...
4131                                 (setcar threads (list whole-subject
4132                                                       (car threads)))
4133                                 threads)
4134                             threads)
4135                           hashtb)))
4136         (setq prev threads)
4137         (setq threads (cdr threads)))
4138       result)))
4139
4140 (defun gnus-gather-threads-by-references (threads)
4141   "Gather threads by looking at References headers."
4142   (let ((idhashtb (gnus-make-hashtable 1024))
4143         (thhashtb (gnus-make-hashtable 1024))
4144         (prev threads)
4145         (result threads)
4146         ids references id gthread gid entered ref)
4147     (while threads
4148       (when (setq references (mail-header-references (caar threads)))
4149         (setq id (mail-header-id (caar threads))
4150               ids (inline (gnus-split-references references))
4151               entered nil)
4152         (while (setq ref (pop ids))
4153           (setq ids (delete ref ids))
4154           (if (not (setq gid (gnus-gethash ref idhashtb)))
4155               (progn
4156                 (gnus-sethash ref id idhashtb)
4157                 (gnus-sethash id threads thhashtb))
4158             (setq gthread (gnus-gethash gid thhashtb))
4159             (unless entered
4160               ;; We enter a dummy root into the thread, if we
4161               ;; haven't done that already.
4162               (unless (stringp (caar gthread))
4163                 (setcar gthread (list (mail-header-subject (caar gthread))
4164                                       (car gthread))))
4165               ;; We add this new gathered thread to this gathered
4166               ;; thread.
4167               (setcdr (car gthread)
4168                       (nconc (cdar gthread) (list (car threads)))))
4169             ;; Add it into the thread hash table.
4170             (gnus-sethash id gthread thhashtb)
4171             (setq entered t)
4172             ;; Remove it from the list of threads.
4173             (setcdr prev (cdr threads))
4174             (setq threads prev))))
4175       (setq prev threads)
4176       (setq threads (cdr threads)))
4177     result))
4178
4179 (defun gnus-sort-gathered-threads (threads)
4180   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4181   (let ((result threads))
4182     (while threads
4183       (when (stringp (caar threads))
4184         (setcdr (car threads)
4185                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4186       (setq threads (cdr threads)))
4187     result))
4188
4189 (defun gnus-thread-loop-p (root thread)
4190   "Say whether ROOT is in THREAD."
4191   (let ((stack (list thread))
4192         (infloop 0)
4193         th)
4194     (while (setq thread (pop stack))
4195       (setq th (cdr thread))
4196       (while (and th
4197                   (not (eq (caar th) root)))
4198         (pop th))
4199       (if th
4200           ;; We have found a loop.
4201           (let (ref-dep)
4202             (setcdr thread (delq (car th) (cdr thread)))
4203             (if (boundp (setq ref-dep (intern "none"
4204                                               gnus-newsgroup-dependencies)))
4205                 (setcdr (symbol-value ref-dep)
4206                         (nconc (cdr (symbol-value ref-dep))
4207                                (list (car th))))
4208               (set ref-dep (list nil (car th))))
4209             (setq infloop 1
4210                   stack nil))
4211         ;; Push all the subthreads onto the stack.
4212         (push (cdr thread) stack)))
4213     infloop))
4214
4215 (defun gnus-make-threads ()
4216   "Go through the dependency hashtb and find the roots.  Return all threads."
4217   (let (threads)
4218     (while (catch 'infloop
4219              (mapatoms
4220               (lambda (refs)
4221                 ;; Deal with self-referencing References loops.
4222                 (when (and (car (symbol-value refs))
4223                            (not (zerop
4224                                  (apply
4225                                   '+
4226                                   (mapcar
4227                                    (lambda (thread)
4228                                      (gnus-thread-loop-p
4229                                       (car (symbol-value refs)) thread))
4230                                    (cdr (symbol-value refs)))))))
4231                   (setq threads nil)
4232                   (throw 'infloop t))
4233                 (unless (car (symbol-value refs))
4234                   ;; These threads do not refer back to any other
4235                   ;; articles, so they're roots.
4236                   (setq threads (append (cdr (symbol-value refs)) threads))))
4237               gnus-newsgroup-dependencies)))
4238     threads))
4239
4240 ;; Build the thread tree.
4241 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4242   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4243
4244 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4245 if it was already present.
4246
4247 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4248 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4249 Message-IDs will be renamed to a unique Message-ID before being
4250 entered.
4251
4252 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4253   (let* ((id (mail-header-id header))
4254          (id-dep (and id (intern id dependencies)))
4255          parent-id ref ref-dep ref-header replaced)
4256     ;; Enter this `header' in the `dependencies' table.
4257     (cond
4258      ((not id-dep)
4259       (setq header nil))
4260      ;; The first two cases do the normal part: enter a new `header'
4261      ;; in the `dependencies' table.
4262      ((not (boundp id-dep))
4263       (set id-dep (list header)))
4264      ((null (car (symbol-value id-dep)))
4265       (setcar (symbol-value id-dep) header))
4266
4267      ;; From here the `header' was already present in the
4268      ;; `dependencies' table.
4269      (force-new
4270       ;; Overrides an existing entry;
4271       ;; just set the header part of the entry.
4272       (setcar (symbol-value id-dep) header)
4273       (setq replaced t))
4274
4275      ;; Renames the existing `header' to a unique Message-ID.
4276      ((not gnus-summary-ignore-duplicates)
4277       ;; An article with this Message-ID has already been seen.
4278       ;; We rename the Message-ID.
4279       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4280            (list header))
4281       (mail-header-set-id header id))
4282
4283      ;; The last case ignores an existing entry, except it adds any
4284      ;; additional Xrefs (in case the two articles came from different
4285      ;; servers.
4286      ;; Also sets `header' to `nil' meaning that the `dependencies'
4287      ;; table was *not* modified.
4288      (t
4289       (mail-header-set-xref
4290        (car (symbol-value id-dep))
4291        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4292                    "")
4293                (or (mail-header-xref header) "")))
4294       (setq header nil)))
4295
4296     (when (and header (not replaced))
4297       ;; First check that we are not creating a References loop.
4298       (setq parent-id (gnus-parent-id (mail-header-references header)))
4299       (setq ref parent-id)
4300       (while (and ref
4301                   (setq ref-dep (intern-soft ref dependencies))
4302                   (boundp ref-dep)
4303                   (setq ref-header (car (symbol-value ref-dep))))
4304         (if (string= id ref)
4305             ;; Yuk!  This is a reference loop.  Make the article be a
4306             ;; root article.
4307             (progn
4308               (mail-header-set-references (car (symbol-value id-dep)) "none")
4309               (setq ref nil)
4310               (setq parent-id nil))
4311           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4312       (setq ref-dep (intern (or parent-id "none") dependencies))
4313       (if (boundp ref-dep)
4314           (setcdr (symbol-value ref-dep)
4315                   (nconc (cdr (symbol-value ref-dep))
4316                          (list (symbol-value id-dep))))
4317         (set ref-dep (list nil (symbol-value id-dep)))))
4318     header))
4319
4320 (defun gnus-extract-message-id-from-in-reply-to (string)
4321   (if (string-match "<[^>]+>" string)
4322       (substring string (match-beginning 0) (match-end 0))
4323     nil))
4324
4325 (defun gnus-build-sparse-threads ()
4326   (let ((headers gnus-newsgroup-headers)
4327         (mail-parse-charset gnus-newsgroup-charset)
4328         (gnus-summary-ignore-duplicates t)
4329         header references generation relations
4330         subject child end new-child date)
4331     ;; First we create an alist of generations/relations, where
4332     ;; generations is how much we trust the relation, and the relation
4333     ;; is parent/child.
4334     (gnus-message 7 "Making sparse threads...")
4335     (save-excursion
4336       (nnheader-set-temp-buffer " *gnus sparse threads*")
4337       (while (setq header (pop headers))
4338         (when (and (setq references (mail-header-references header))
4339                    (not (string= references "")))
4340           (insert references)
4341           (setq child (mail-header-id header)
4342                 subject (mail-header-subject header)
4343                 date (mail-header-date header)
4344                 generation 0)
4345           (while (search-backward ">" nil t)
4346             (setq end (1+ (point)))
4347             (when (search-backward "<" nil t)
4348               (setq new-child (buffer-substring (point) end))
4349               (push (list (incf generation)
4350                           child (setq child new-child)
4351                           subject date)
4352                     relations)))
4353           (when child
4354             (push (list (1+ generation) child nil subject) relations))
4355           (erase-buffer)))
4356       (kill-buffer (current-buffer)))
4357     ;; Sort over trustworthiness.
4358     (dolist (relation (sort relations 'car-less-than-car))
4359       (when (gnus-dependencies-add-header
4360              (make-full-mail-header
4361               gnus-reffed-article-number
4362               (nth 3 relation) "" (or (nth 4 relation) "")
4363               (nth 1 relation)
4364               (or (nth 2 relation) "") 0 0 "")
4365              gnus-newsgroup-dependencies nil)
4366         (push gnus-reffed-article-number gnus-newsgroup-limit)
4367         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4368         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4369               gnus-newsgroup-reads)
4370         (decf gnus-reffed-article-number)))
4371     (gnus-message 7 "Making sparse threads...done")))
4372
4373 (defun gnus-build-old-threads ()
4374   ;; Look at all the articles that refer back to old articles, and
4375   ;; fetch the headers for the articles that aren't there.  This will
4376   ;; build complete threads - if the roots haven't been expired by the
4377   ;; server, that is.
4378   (let ((mail-parse-charset gnus-newsgroup-charset)
4379         id heads)
4380     (mapatoms
4381      (lambda (refs)
4382        (when (not (car (symbol-value refs)))
4383          (setq heads (cdr (symbol-value refs)))
4384          (while heads
4385            (if (memq (mail-header-number (caar heads))
4386                      gnus-newsgroup-dormant)
4387                (setq heads (cdr heads))
4388              (setq id (symbol-name refs))
4389              (while (and (setq id (gnus-build-get-header id))
4390                          (not (car (gnus-id-to-thread id)))))
4391              (setq heads nil)))))
4392      gnus-newsgroup-dependencies)))
4393
4394 (defsubst gnus-remove-odd-characters (string)
4395   "Translate STRING into something that doesn't contain weird characters."
4396   (mm-subst-char-in-string
4397    ?\r ?\-
4398    (mm-subst-char-in-string ?\n ?\- string t) t))
4399
4400 ;; This function has to be called with point after the article number
4401 ;; on the beginning of the line.
4402 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4403   (let ((eol (point-at-eol))
4404         (buffer (current-buffer))
4405         header references in-reply-to)
4406
4407     ;; overview: [num subject from date id refs chars lines misc]
4408     (unwind-protect
4409         (let (x)
4410           (narrow-to-region (point) eol)
4411           (unless (eobp)
4412             (forward-char))
4413
4414           (setq header
4415                 (make-full-mail-header
4416                  number                 ; number
4417                  (condition-case ()     ; subject
4418                      (gnus-remove-odd-characters
4419                       (funcall gnus-decode-encoded-word-function
4420                                (setq x (nnheader-nov-field))))
4421                    (error x))
4422                  (condition-case ()     ; from
4423                      (gnus-remove-odd-characters
4424                       (funcall gnus-decode-encoded-address-function
4425                                (setq x (nnheader-nov-field))))
4426                    (error x))
4427                  (nnheader-nov-field)   ; date
4428                  (nnheader-nov-read-message-id number)  ; id
4429                  (setq references (nnheader-nov-field)) ; refs
4430                  (nnheader-nov-read-integer) ; chars
4431                  (nnheader-nov-read-integer) ; lines
4432                  (unless (eobp)
4433                    (if (looking-at "Xref: ")
4434                        (goto-char (match-end 0)))
4435                    (nnheader-nov-field)) ; Xref
4436                  (nnheader-nov-parse-extra)))) ; extra
4437
4438       (widen))
4439
4440     (when (and (string= references "")
4441                (setq in-reply-to (mail-header-extra header))
4442                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4443       (mail-header-set-references
4444        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4445
4446     (when gnus-alter-header-function
4447       (funcall gnus-alter-header-function header))
4448     (gnus-dependencies-add-header header dependencies force-new)))
4449
4450 (defun gnus-build-get-header (id)
4451   "Look through the buffer of NOV lines and find the header to ID.
4452 Enter this line into the dependencies hash table, and return
4453 the id of the parent article (if any)."
4454   (let ((deps gnus-newsgroup-dependencies)
4455         found header)
4456     (prog1
4457         (with-current-buffer nntp-server-buffer
4458           (let ((case-fold-search nil))
4459             (goto-char (point-min))
4460             (while (and (not found)
4461                         (search-forward id nil t))
4462               (beginning-of-line)
4463               (setq found (looking-at
4464                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4465                                    (regexp-quote id))))
4466               (or found (beginning-of-line 2)))
4467             (when found
4468               (beginning-of-line)
4469               (and
4470                (setq header (gnus-nov-parse-line
4471                              (read (current-buffer)) deps))
4472                (gnus-parent-id (mail-header-references header))))))
4473       (when header
4474         (let ((number (mail-header-number header)))
4475           (push number gnus-newsgroup-limit)
4476           (push header gnus-newsgroup-headers)
4477           (if (memq number gnus-newsgroup-unselected)
4478               (progn
4479                 (setq gnus-newsgroup-unreads
4480                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4481                                                number))
4482                 (setq gnus-newsgroup-unselected
4483                       (delq number gnus-newsgroup-unselected)))
4484             (push number gnus-newsgroup-ancient)))))))
4485
4486 (defun gnus-build-all-threads ()
4487   "Read all the headers."
4488   (let ((gnus-summary-ignore-duplicates t)
4489         (mail-parse-charset gnus-newsgroup-charset)
4490         (dependencies gnus-newsgroup-dependencies)
4491         header article)
4492     (with-current-buffer nntp-server-buffer
4493       (let ((case-fold-search nil))
4494         (goto-char (point-min))
4495         (while (not (eobp))
4496           (ignore-errors
4497             (setq article (read (current-buffer))
4498                   header (gnus-nov-parse-line article dependencies)))
4499           (when header
4500             (with-current-buffer gnus-summary-buffer
4501               (push header gnus-newsgroup-headers)
4502               (if (memq (setq article (mail-header-number header))
4503                         gnus-newsgroup-unselected)
4504                   (progn
4505                     (setq gnus-newsgroup-unreads
4506                           (gnus-add-to-sorted-list
4507                            gnus-newsgroup-unreads article))
4508                     (setq gnus-newsgroup-unselected
4509                           (delq article gnus-newsgroup-unselected)))
4510                 (push article gnus-newsgroup-ancient)))
4511             (forward-line 1)))))))
4512
4513 (defun gnus-summary-update-article-line (article header)
4514   "Update the line for ARTICLE using HEADER."
4515   (let* ((id (mail-header-id header))
4516          (thread (gnus-id-to-thread id)))
4517     (unless thread
4518       (error "Article in no thread"))
4519     ;; Update the thread.
4520     (setcar thread header)
4521     (gnus-summary-goto-subject article)
4522     (let* ((datal (gnus-data-find-list article))
4523            (data (car datal))
4524            (inhibit-read-only t)
4525            (level (gnus-summary-thread-level)))
4526       (gnus-delete-line)
4527       (let ((inserted (- (point)
4528                          (progn
4529                            (gnus-summary-insert-line
4530                             header level nil
4531                             (memq article gnus-newsgroup-undownloaded)
4532                             (gnus-article-mark article)
4533                             (memq article gnus-newsgroup-replied)
4534                             (memq article gnus-newsgroup-expirable)
4535                             ;; Only insert the Subject string when it's different
4536                             ;; from the previous Subject string.
4537                             (if (and
4538                                  gnus-show-threads
4539                                  (gnus-subject-equal
4540                                   (condition-case ()
4541                                       (mail-header-subject
4542                                        (gnus-data-header
4543                                         (cadr
4544                                          (gnus-data-find-list
4545                                           article
4546                                           (gnus-data-list t)))))
4547                                     ;; Error on the side of excessive subjects.
4548                                     (error ""))
4549                                   (mail-header-subject header)))
4550                                 ""
4551                               (mail-header-subject header))
4552                             nil (cdr (assq article gnus-newsgroup-scored))
4553                             (memq article gnus-newsgroup-processable))
4554                            (point)))))
4555         (when (cdr datal)
4556           (gnus-data-update-list
4557            (cdr datal)
4558            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4559
4560 (defun gnus-summary-update-article (article &optional iheader)
4561   "Update ARTICLE in the summary buffer."
4562   (set-buffer gnus-summary-buffer)
4563   (let* ((header (gnus-summary-article-header article))
4564          (id (mail-header-id header))
4565          (data (gnus-data-find article))
4566          (thread (gnus-id-to-thread id))
4567          (references (mail-header-references header))
4568          (parent
4569           (gnus-id-to-thread
4570            (or (gnus-parent-id
4571                 (when (and references
4572                            (not (equal "" references)))
4573                   references))
4574                "none")))
4575          (inhibit-read-only t)
4576          (old (car thread)))
4577     (when thread
4578       (unless iheader
4579         (setcar thread nil)
4580         (when parent
4581           (delq thread parent)))
4582       (if (gnus-summary-insert-subject id header)
4583           ;; Set the (possibly) new article number in the data structure.
4584           (gnus-data-set-number data (gnus-id-to-article id))
4585         (setcar thread old)
4586         nil))))
4587
4588 (defun gnus-rebuild-thread (id &optional line)
4589   "Rebuild the thread containing ID.
4590 If LINE, insert the rebuilt thread starting on line LINE."
4591   (let ((inhibit-read-only t)
4592         old-pos current thread data)
4593     (if (not gnus-show-threads)
4594         (setq thread (list (car (gnus-id-to-thread id))))
4595       ;; Get the thread this article is part of.
4596       (setq thread (gnus-remove-thread id)))
4597     (setq old-pos (point-at-bol))
4598     (setq current (save-excursion
4599                     (and (re-search-backward "[\r\n]" nil t)
4600                          (gnus-summary-article-number))))
4601     ;; If this is a gathered thread, we have to go some re-gathering.
4602     (when (stringp (car thread))
4603       (let ((subject (car thread))
4604             roots thr)
4605         (setq thread (cdr thread))
4606         (while thread
4607           (unless (memq (setq thr (gnus-id-to-thread
4608                                    (gnus-root-id
4609                                     (mail-header-id (caar thread)))))
4610                         roots)
4611             (push thr roots))
4612           (setq thread (cdr thread)))
4613         ;; We now have all (unique) roots.
4614         (if (= (length roots) 1)
4615             ;; All the loose roots are now one solid root.
4616             (setq thread (car roots))
4617           (setq thread (cons subject (gnus-sort-threads roots))))))
4618     (let (threads)
4619       ;; We then insert this thread into the summary buffer.
4620       (when line
4621         (goto-char (point-min))
4622         (forward-line (1- line)))
4623       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4624         (if gnus-show-threads
4625             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4626           (gnus-summary-prepare-unthreaded thread))
4627         (setq data (nreverse gnus-newsgroup-data))
4628         (setq threads gnus-newsgroup-threads))
4629       ;; We splice the new data into the data structure.
4630       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4631       ;;!!! then we want to insert at the beginning of the buffer.
4632       ;;!!! That happens to be true with Gnus now, but that may
4633       ;;!!! change in the future.  Perhaps.
4634       (gnus-data-enter-list
4635        (if line nil current) data (- (point) old-pos))
4636       (setq gnus-newsgroup-threads
4637             (nconc threads gnus-newsgroup-threads))
4638       (gnus-data-compute-positions))))
4639
4640 (defun gnus-number-to-header (number)
4641   "Return the header for article NUMBER."
4642   (let ((headers gnus-newsgroup-headers))
4643     (while (and headers
4644                 (not (= number (mail-header-number (car headers)))))
4645       (pop headers))
4646     (when headers
4647       (car headers))))
4648
4649 (defun gnus-parent-headers (in-headers &optional generation)
4650   "Return the headers of the GENERATIONeth parent of HEADERS."
4651   (unless generation
4652     (setq generation 1))
4653   (let ((parent t)
4654         (headers in-headers)
4655         references)
4656     (while (and parent
4657                 (not (zerop generation))
4658                 (setq references (mail-header-references headers)))
4659       (setq headers (if (and references
4660                              (setq parent (gnus-parent-id references)))
4661                         (car (gnus-id-to-thread parent))
4662                       nil))
4663       (decf generation))
4664     (and (not (eq headers in-headers))
4665          headers)))
4666
4667 (defun gnus-id-to-thread (id)
4668   "Return the (sub-)thread where ID appears."
4669   (gnus-gethash id gnus-newsgroup-dependencies))
4670
4671 (defun gnus-id-to-article (id)
4672   "Return the article number of ID."
4673   (let ((thread (gnus-id-to-thread id)))
4674     (when (and thread
4675                (car thread))
4676       (mail-header-number (car thread)))))
4677
4678 (defun gnus-id-to-header (id)
4679   "Return the article headers of ID."
4680   (car (gnus-id-to-thread id)))
4681
4682 (defun gnus-article-displayed-root-p (article)
4683   "Say whether ARTICLE is a root(ish) article."
4684   (let ((level (gnus-summary-thread-level article))
4685         (refs (mail-header-references  (gnus-summary-article-header article)))
4686         particle)
4687     (cond
4688      ((null level) nil)
4689      ((zerop level) t)
4690      ((null refs) t)
4691      ((null (gnus-parent-id refs)) t)
4692      ((and (= 1 level)
4693            (null (setq particle (gnus-id-to-article
4694                                  (gnus-parent-id refs))))
4695            (null (gnus-summary-thread-level particle)))))))
4696
4697 (defun gnus-root-id (id)
4698   "Return the id of the root of the thread where ID appears."
4699   (let (last-id prev)
4700     (while (and id (setq prev (car (gnus-id-to-thread id))))
4701       (setq last-id id
4702             id (gnus-parent-id (mail-header-references prev))))
4703     last-id))
4704
4705 (defun gnus-articles-in-thread (thread)
4706   "Return the list of articles in THREAD."
4707   (cons (mail-header-number (car thread))
4708         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4709
4710 (defun gnus-remove-thread (id &optional dont-remove)
4711   "Remove the thread that has ID in it."
4712   (let (headers thread last-id)
4713     ;; First go up in this thread until we find the root.
4714     (setq last-id (gnus-root-id id)
4715           headers (message-flatten-list (gnus-id-to-thread last-id)))
4716     ;; We have now found the real root of this thread.  It might have
4717     ;; been gathered into some loose thread, so we have to search
4718     ;; through the threads to find the thread we wanted.
4719     (let ((threads gnus-newsgroup-threads)
4720           sub)
4721       (while threads
4722         (setq sub (car threads))
4723         (if (stringp (car sub))
4724             ;; This is a gathered thread, so we look at the roots
4725             ;; below it to find whether this article is in this
4726             ;; gathered root.
4727             (progn
4728               (setq sub (cdr sub))
4729               (while sub
4730                 (when (member (caar sub) headers)
4731                   (setq thread (car threads)
4732                         threads nil
4733                         sub nil))
4734                 (setq sub (cdr sub))))
4735           ;; It's an ordinary thread, so we check it.
4736           (when (eq (car sub) (car headers))
4737             (setq thread sub
4738                   threads nil)))
4739         (setq threads (cdr threads)))
4740       ;; If this article is in no thread, then it's a root.
4741       (if thread
4742           (unless dont-remove
4743             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4744         (setq thread (gnus-id-to-thread last-id)))
4745       (when thread
4746         (prog1
4747             thread                      ; We return this thread.
4748           (unless dont-remove
4749             (if (stringp (car thread))
4750                 (progn
4751                   ;; If we use dummy roots, then we have to remove the
4752                   ;; dummy root as well.
4753                   (when (eq gnus-summary-make-false-root 'dummy)
4754                     ;; We go to the dummy root by going to
4755                     ;; the first sub-"thread", and then one line up.
4756                     (gnus-summary-goto-article
4757                      (mail-header-number (caadr thread)))
4758                     (forward-line -1)
4759                     (gnus-delete-line)
4760                     (gnus-data-compute-positions))
4761                   (setq thread (cdr thread))
4762                   (while thread
4763                     (gnus-remove-thread-1 (car thread))
4764                     (setq thread (cdr thread))))
4765               (gnus-remove-thread-1 thread))))))))
4766
4767 (defun gnus-remove-thread-1 (thread)
4768   "Remove the thread THREAD recursively."
4769   (let ((number (mail-header-number (pop thread)))
4770         d)
4771     (setq thread (reverse thread))
4772     (while thread
4773       (gnus-remove-thread-1 (pop thread)))
4774     (when (setq d (gnus-data-find number))
4775       (goto-char (gnus-data-pos d))
4776       (gnus-summary-show-thread)
4777       (gnus-data-remove
4778        number
4779        (- (point-at-bol)
4780           (prog1
4781               (1+ (point-at-eol))
4782             (gnus-delete-line)))))))
4783
4784 (defun gnus-sort-threads-recursive (threads func)
4785   (sort (mapcar (lambda (thread)
4786                   (cons (car thread)
4787                         (and (cdr thread)
4788                              (gnus-sort-threads-recursive (cdr thread) func))))
4789                 threads) func))
4790
4791 (defun gnus-sort-threads-loop (threads func)
4792   (let* ((superthread (cons nil threads))
4793          (stack (list (cons superthread threads)))
4794          remaining-threads thread)
4795     (while stack
4796       (setq remaining-threads (cdr (car stack)))
4797       (if remaining-threads
4798           (progn (setq thread (car remaining-threads))
4799                  (setcdr (car stack) (cdr remaining-threads))
4800                  (if (cdr thread)
4801                      (push (cons thread (cdr thread)) stack)))
4802         (setq thread (caar stack))
4803         (setcdr thread (sort (cdr thread) func))
4804         (pop stack)))
4805     (cdr superthread)))
4806
4807 (defun gnus-sort-threads (threads)
4808   "Sort THREADS."
4809   (if (not gnus-thread-sort-functions)
4810       threads
4811     (gnus-message 8 "Sorting threads...")
4812     (prog1
4813         (condition-case nil
4814             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4815               (gnus-sort-threads-recursive
4816                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4817           ;; Even after binding max-lisp-eval-depth, the recursive
4818           ;; sorter might fail for very long threads.  In that case,
4819           ;; try using a (less well-tested) non-recursive sorter.
4820           (error (gnus-message 9 "Sorting threads with loop...")
4821                  (gnus-sort-threads-loop
4822                   threads (gnus-make-sort-function
4823                            gnus-thread-sort-functions))))
4824       (gnus-message 8 "Sorting threads...done"))))
4825
4826 (defun gnus-sort-articles (articles)
4827   "Sort ARTICLES."
4828   (when gnus-article-sort-functions
4829     (gnus-message 7 "Sorting articles...")
4830     (prog1
4831         (setq gnus-newsgroup-headers
4832               (sort articles (gnus-make-sort-function
4833                               gnus-article-sort-functions)))
4834       (gnus-message 7 "Sorting articles...done"))))
4835
4836 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4837 (defmacro gnus-thread-header (thread)
4838   "Return header of first article in THREAD.
4839 Note that THREAD must never, ever be anything else than a variable -
4840 using some other form will lead to serious barfage."
4841   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4842   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4843   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4844         (vector thread) 2))
4845
4846 (defsubst gnus-article-sort-by-number (h1 h2)
4847   "Sort articles by article number."
4848   (< (mail-header-number h1)
4849      (mail-header-number h2)))
4850
4851 (defun gnus-thread-sort-by-number (h1 h2)
4852   "Sort threads by root article number."
4853   (gnus-article-sort-by-number
4854    (gnus-thread-header h1) (gnus-thread-header h2)))
4855
4856 (defsubst gnus-article-sort-by-random (h1 h2)
4857   "Sort articles randomly."
4858   (zerop (random 2)))
4859
4860 (defun gnus-thread-sort-by-random (h1 h2)
4861   "Sort threads randomly."
4862   (gnus-article-sort-by-random
4863    (gnus-thread-header h1) (gnus-thread-header h2)))
4864
4865 (defsubst gnus-article-sort-by-lines (h1 h2)
4866   "Sort articles by article Lines header."
4867   (< (mail-header-lines h1)
4868      (mail-header-lines h2)))
4869
4870 (defun gnus-thread-sort-by-lines (h1 h2)
4871   "Sort threads by root article Lines header."
4872   (gnus-article-sort-by-lines
4873    (gnus-thread-header h1) (gnus-thread-header h2)))
4874
4875 (defsubst gnus-article-sort-by-chars (h1 h2)
4876   "Sort articles by octet length."
4877   (< (mail-header-chars h1)
4878      (mail-header-chars h2)))
4879
4880 (defun gnus-thread-sort-by-chars (h1 h2)
4881   "Sort threads by root article octet length."
4882   (gnus-article-sort-by-chars
4883    (gnus-thread-header h1) (gnus-thread-header h2)))
4884
4885 (defsubst gnus-article-sort-by-author (h1 h2)
4886   "Sort articles by root author."
4887   (gnus-string<
4888    (let ((extract (funcall
4889                    gnus-extract-address-components
4890                    (mail-header-from h1))))
4891      (or (car extract) (cadr extract) ""))
4892    (let ((extract (funcall
4893                    gnus-extract-address-components
4894                    (mail-header-from h2))))
4895      (or (car extract) (cadr extract) ""))))
4896
4897 (defun gnus-thread-sort-by-author (h1 h2)
4898   "Sort threads by root author."
4899   (gnus-article-sort-by-author
4900    (gnus-thread-header h1)  (gnus-thread-header h2)))
4901
4902 (defsubst gnus-article-sort-by-recipient (h1 h2)
4903   "Sort articles by recipient."
4904   (gnus-string<
4905    (let ((extract (funcall
4906                    gnus-extract-address-components
4907                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4908      (or (car extract) (cadr extract)))
4909    (let ((extract (funcall
4910                    gnus-extract-address-components
4911                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4912      (or (car extract) (cadr extract)))))
4913
4914 (defun gnus-thread-sort-by-recipient (h1 h2)
4915   "Sort threads by root recipient."
4916   (gnus-article-sort-by-recipient
4917    (gnus-thread-header h1) (gnus-thread-header h2)))
4918
4919 (defsubst gnus-article-sort-by-subject (h1 h2)
4920   "Sort articles by root subject."
4921   (gnus-string<
4922    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4923    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4924
4925 (defun gnus-thread-sort-by-subject (h1 h2)
4926   "Sort threads by root subject."
4927   (gnus-article-sort-by-subject
4928    (gnus-thread-header h1) (gnus-thread-header h2)))
4929
4930 (defsubst gnus-article-sort-by-date (h1 h2)
4931   "Sort articles by root article date."
4932   (time-less-p
4933    (gnus-date-get-time (mail-header-date h1))
4934    (gnus-date-get-time (mail-header-date h2))))
4935
4936 (defun gnus-thread-sort-by-date (h1 h2)
4937   "Sort threads by root article date."
4938   (gnus-article-sort-by-date
4939    (gnus-thread-header h1) (gnus-thread-header h2)))
4940
4941 (defsubst gnus-article-sort-by-score (h1 h2)
4942   "Sort articles by root article score.
4943 Unscored articles will be counted as having a score of zero."
4944   (> (or (cdr (assq (mail-header-number h1)
4945                     gnus-newsgroup-scored))
4946          gnus-summary-default-score 0)
4947      (or (cdr (assq (mail-header-number h2)
4948                     gnus-newsgroup-scored))
4949          gnus-summary-default-score 0)))
4950
4951 (defun gnus-thread-sort-by-score (h1 h2)
4952   "Sort threads by root article score."
4953   (gnus-article-sort-by-score
4954    (gnus-thread-header h1) (gnus-thread-header h2)))
4955
4956 (defun gnus-thread-sort-by-total-score (h1 h2)
4957   "Sort threads by the sum of all scores in the thread.
4958 Unscored articles will be counted as having a score of zero."
4959   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4960
4961 (defun gnus-thread-total-score (thread)
4962   ;; This function find the total score of THREAD.
4963   (cond
4964    ((null thread)
4965     0)
4966    ((consp thread)
4967     (if (stringp (car thread))
4968         (apply gnus-thread-score-function 0
4969                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4970       (gnus-thread-total-score-1 thread)))
4971    (t
4972     (gnus-thread-total-score-1 (list thread)))))
4973
4974 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4975   "Sort threads such that the thread with the most recently arrived article comes first."
4976   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4977
4978 (defun gnus-thread-highest-number (thread)
4979   "Return the highest article number in THREAD."
4980   (apply 'max (mapcar (lambda (header)
4981                         (mail-header-number header))
4982                       (message-flatten-list thread))))
4983
4984 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4985   "Sort threads such that the thread with the most recently dated article comes first."
4986   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4987
4988 ; Since this is called not only to sort the top-level threads, but
4989 ; also in recursive sorts to order the articles within a thread, each
4990 ; article will be processed many times.  Thus it speeds things up
4991 ; quite a bit to use gnus-date-get-time, which caches the time value.
4992 (defun gnus-thread-latest-date (thread)
4993   "Return the highest article date in THREAD."
4994   (apply 'max
4995          (mapcar (lambda (header) (gnus-float-time
4996                                    (gnus-date-get-time
4997                                     (mail-header-date header))))
4998                  (message-flatten-list thread))))
4999
5000 (defun gnus-thread-total-score-1 (root)
5001   ;; This function find the total score of the thread below ROOT.
5002   (setq root (car root))
5003   (apply gnus-thread-score-function
5004          (or (append
5005               (mapcar 'gnus-thread-total-score
5006                       (cdr (gnus-id-to-thread (mail-header-id root))))
5007               (when (> (mail-header-number root) 0)
5008                 (list (or (cdr (assq (mail-header-number root)
5009                                      gnus-newsgroup-scored))
5010                           gnus-summary-default-score 0))))
5011              (list gnus-summary-default-score)
5012              '(0))))
5013
5014 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5015 (defvar gnus-tmp-prev-subject nil)
5016 (defvar gnus-tmp-false-parent nil)
5017 (defvar gnus-tmp-root-expunged nil)
5018 (defvar gnus-tmp-dummy-line nil)
5019
5020 (defun gnus-extra-header (type &optional header)
5021   "Return the extra header of TYPE."
5022   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5023       ""))
5024
5025 (defvar gnus-tmp-thread-tree-header-string "")
5026
5027 (defcustom gnus-sum-thread-tree-root "> "
5028   "With %B spec, used for the root of a thread.
5029 If nil, use subject instead."
5030   :version "22.1"
5031   :type '(radio (const :format "%v  " nil) string)
5032   :group 'gnus-thread)
5033
5034 (defcustom gnus-sum-thread-tree-false-root "> "
5035   "With %B spec, used for a false 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-single-indent ""
5042   "With %B spec, used for a thread with just one message.
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-vertical "| "
5049   "With %B spec, used for drawing a vertical line."
5050   :version "22.1"
5051   :type 'string
5052   :group 'gnus-thread)
5053
5054 (defcustom gnus-sum-thread-tree-indent "  "
5055   "With %B spec, used for indenting."
5056   :version "22.1"
5057   :type 'string
5058   :group 'gnus-thread)
5059
5060 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5061   "With %B spec, used for a leaf with brothers."
5062   :version "22.1"
5063   :type 'string
5064   :group 'gnus-thread)
5065
5066 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5067   "With %B spec, used for a leaf without brothers."
5068   :version "22.1"
5069   :type 'string
5070   :group 'gnus-thread)
5071
5072 (defcustom gnus-summary-display-while-building nil
5073   "If non-nil, show and update the summary buffer as it's being built.
5074 If the value is t, update the buffer after every line is inserted.  If
5075 the value is an integer (N), update the display every N lines."
5076   :version "22.1"
5077   :group 'gnus-thread
5078   :type '(choice (const :tag "off" nil)
5079                  number
5080                  (const :tag "frequently" t)))
5081
5082 (defun gnus-summary-prepare-threads (threads)
5083   "Prepare summary buffer from THREADS and indentation LEVEL.
5084 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5085 or a straight list of headers."
5086   (gnus-message 7 "Generating summary...")
5087
5088   (setq gnus-newsgroup-threads threads)
5089   (beginning-of-line)
5090
5091   (let ((gnus-tmp-level 0)
5092         (default-score (or gnus-summary-default-score 0))
5093         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5094         (building-line-count gnus-summary-display-while-building)
5095         (building-count (integerp gnus-summary-display-while-building))
5096         thread number subject stack state gnus-tmp-gathered beg-match
5097         new-roots gnus-tmp-new-adopts thread-end simp-subject
5098         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5099         gnus-tmp-replied gnus-tmp-subject-or-nil
5100         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5101         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5102         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5103         tree-stack)
5104
5105     (setq gnus-tmp-prev-subject nil
5106           gnus-tmp-thread-tree-header-string "")
5107
5108     (if (vectorp (car threads))
5109         ;; If this is a straight (sic) list of headers, then a
5110         ;; threaded summary display isn't required, so we just create
5111         ;; an unthreaded one.
5112         (gnus-summary-prepare-unthreaded threads)
5113
5114       ;; Do the threaded display.
5115
5116       (if gnus-summary-display-while-building
5117           (switch-to-buffer (buffer-name)))
5118       (while (or threads stack gnus-tmp-new-adopts new-roots)
5119
5120         (if (and (= gnus-tmp-level 0)
5121                  (or (not stack)
5122                      (= (caar stack) 0))
5123                  (not gnus-tmp-false-parent)
5124                  (or gnus-tmp-new-adopts new-roots))
5125             (if gnus-tmp-new-adopts
5126                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5127                       thread (list (car gnus-tmp-new-adopts))
5128                       gnus-tmp-header (caar thread)
5129                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5130               (when new-roots
5131                 (setq thread (list (car new-roots))
5132                       gnus-tmp-header (caar thread)
5133                       new-roots (cdr new-roots))))
5134
5135           (if threads
5136               ;; If there are some threads, we do them before the
5137               ;; threads on the stack.
5138               (setq thread threads
5139                     gnus-tmp-header (caar thread))
5140             ;; There were no current threads, so we pop something off
5141             ;; the stack.
5142             (setq state (car stack)
5143                   gnus-tmp-level (car state)
5144                   tree-stack (cadr state)
5145                   thread (caddr state)
5146                   stack (cdr stack)
5147                   gnus-tmp-header (caar thread))))
5148
5149         (setq gnus-tmp-false-parent nil)
5150         (setq gnus-tmp-root-expunged nil)
5151         (setq thread-end nil)
5152
5153         (if (stringp gnus-tmp-header)
5154             ;; The header is a dummy root.
5155             (cond
5156              ((eq gnus-summary-make-false-root 'adopt)
5157               ;; We let the first article adopt the rest.
5158               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5159                                                (cddar thread)))
5160               (setq gnus-tmp-gathered
5161                     (nconc (mapcar
5162                             (lambda (h) (mail-header-number (car h)))
5163                             (cddar thread))
5164                            gnus-tmp-gathered))
5165               (setq thread (cons (list (caar thread)
5166                                        (cadar thread))
5167                                  (cdr thread)))
5168               (setq gnus-tmp-level -1
5169                     gnus-tmp-false-parent t))
5170              ((eq gnus-summary-make-false-root 'empty)
5171               ;; We print adopted articles with empty subject fields.
5172               (setq gnus-tmp-gathered
5173                     (nconc (mapcar
5174                             (lambda (h) (mail-header-number (car h)))
5175                             (cddar thread))
5176                            gnus-tmp-gathered))
5177               (setq gnus-tmp-level -1))
5178              ((eq gnus-summary-make-false-root 'dummy)
5179               ;; We remember that we probably want to output a dummy
5180               ;; root.
5181               (setq gnus-tmp-dummy-line gnus-tmp-header)
5182               (setq gnus-tmp-prev-subject gnus-tmp-header))
5183              (t
5184               ;; We do not make a root for the gathered
5185               ;; sub-threads at all.
5186               (setq gnus-tmp-level -1)))
5187
5188           (setq number (mail-header-number gnus-tmp-header)
5189                 subject (mail-header-subject gnus-tmp-header)
5190                 simp-subject (gnus-simplify-subject-fully subject))
5191
5192           (cond
5193            ;; If the thread has changed subject, we might want to make
5194            ;; this subthread into a root.
5195            ((and (null gnus-thread-ignore-subject)
5196                  (not (zerop gnus-tmp-level))
5197                  gnus-tmp-prev-subject
5198                  (not (string= gnus-tmp-prev-subject simp-subject)))
5199             (setq new-roots (nconc new-roots (list (car thread)))
5200                   thread-end t
5201                   gnus-tmp-header nil))
5202            ;; If the article lies outside the current limit,
5203            ;; then we do not display it.
5204            ((not (memq number gnus-newsgroup-limit))
5205             (setq gnus-tmp-gathered
5206                   (nconc (mapcar
5207                           (lambda (h) (mail-header-number (car h)))
5208                           (cdar thread))
5209                          gnus-tmp-gathered))
5210             (setq gnus-tmp-new-adopts (if (cdar thread)
5211                                           (append gnus-tmp-new-adopts
5212                                                   (cdar thread))
5213                                         gnus-tmp-new-adopts)
5214                   thread-end t
5215                   gnus-tmp-header nil)
5216             (when (zerop gnus-tmp-level)
5217               (setq gnus-tmp-root-expunged t)))
5218            ;; Perhaps this article is to be marked as read?
5219            ((and gnus-summary-mark-below
5220                  (< (or (cdr (assq number gnus-newsgroup-scored))
5221                         default-score)
5222                     gnus-summary-mark-below)
5223                  ;; Don't touch sparse articles.
5224                  (not (gnus-summary-article-sparse-p number))
5225                  (not (gnus-summary-article-ancient-p number)))
5226             (setq gnus-newsgroup-unreads
5227                   (delq number gnus-newsgroup-unreads))
5228             (if gnus-newsgroup-auto-expire
5229                 (setq gnus-newsgroup-expirable
5230                       (gnus-add-to-sorted-list
5231                        gnus-newsgroup-expirable number))
5232               (push (cons number gnus-low-score-mark)
5233                     gnus-newsgroup-reads))))
5234
5235           (when gnus-tmp-header
5236             ;; We may have an old dummy line to output before this
5237             ;; article.
5238             (when (and gnus-tmp-dummy-line
5239                        (gnus-subject-equal
5240                         gnus-tmp-dummy-line
5241                         (mail-header-subject gnus-tmp-header)))
5242               (gnus-summary-insert-dummy-line
5243                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5244               (setq gnus-tmp-dummy-line nil))
5245
5246             ;; Compute the mark.
5247             (setq gnus-tmp-unread (gnus-article-mark number))
5248
5249             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5250                                   gnus-tmp-header gnus-tmp-level)
5251                   gnus-newsgroup-data)
5252
5253             ;; Actually insert the line.
5254             (setq
5255              gnus-tmp-subject-or-nil
5256              (cond
5257               ((and gnus-thread-ignore-subject
5258                     gnus-tmp-prev-subject
5259                     (not (string= gnus-tmp-prev-subject simp-subject)))
5260                subject)
5261               ((zerop gnus-tmp-level)
5262                (if (and (eq gnus-summary-make-false-root 'empty)
5263                         (memq number gnus-tmp-gathered)
5264                         gnus-tmp-prev-subject
5265                         (string= gnus-tmp-prev-subject simp-subject))
5266                    gnus-summary-same-subject
5267                  subject))
5268               (t gnus-summary-same-subject)))
5269             (if (and (eq gnus-summary-make-false-root 'adopt)
5270                      (= gnus-tmp-level 1)
5271                      (memq number gnus-tmp-gathered))
5272                 (setq gnus-tmp-opening-bracket ?\<
5273                       gnus-tmp-closing-bracket ?\>)
5274               (setq gnus-tmp-opening-bracket ?\[
5275                     gnus-tmp-closing-bracket ?\]))
5276             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5277                 (gnus-make-thread-indent-array
5278                  (max (* 2 (length gnus-thread-indent-array))
5279                       gnus-tmp-level)))
5280             (setq
5281              gnus-tmp-indentation
5282              (aref gnus-thread-indent-array gnus-tmp-level)
5283              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5284              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5285                                 gnus-summary-default-score 0)
5286              gnus-tmp-score-char
5287              (if (or (null gnus-summary-default-score)
5288                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5289                          gnus-summary-zcore-fuzz))
5290                  ?                      ;Whitespace
5291                (if (< gnus-tmp-score gnus-summary-default-score)
5292                    gnus-score-below-mark gnus-score-over-mark))
5293              gnus-tmp-replied
5294              (cond ((memq number gnus-newsgroup-processable)
5295                     gnus-process-mark)
5296                    ((memq number gnus-newsgroup-cached)
5297                     gnus-cached-mark)
5298                    ((memq number gnus-newsgroup-replied)
5299                     gnus-replied-mark)
5300                    ((memq number gnus-newsgroup-forwarded)
5301                     gnus-forwarded-mark)
5302                    ((memq number gnus-newsgroup-saved)
5303                     gnus-saved-mark)
5304                    ((memq number gnus-newsgroup-recent)
5305                     gnus-recent-mark)
5306                    ((memq number gnus-newsgroup-unseen)
5307                     gnus-unseen-mark)
5308                    (t gnus-no-mark))
5309              gnus-tmp-downloaded
5310              (cond ((memq number gnus-newsgroup-undownloaded)
5311                     gnus-undownloaded-mark)
5312                    (gnus-newsgroup-agentized
5313                     gnus-downloaded-mark)
5314                    (t
5315                     gnus-no-mark))
5316              gnus-tmp-from (mail-header-from gnus-tmp-header)
5317              gnus-tmp-name
5318              (cond
5319               ((string-match "<[^>]+> *$" gnus-tmp-from)
5320                (setq beg-match (match-beginning 0))
5321                (or (and (string-match "^\".+\"" gnus-tmp-from)
5322                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5323                    (substring gnus-tmp-from 0 beg-match)))
5324               ((string-match "(.+)" gnus-tmp-from)
5325                (substring gnus-tmp-from
5326                           (1+ (match-beginning 0)) (1- (match-end 0))))
5327               (t gnus-tmp-from))
5328
5329              ;; Do the %B string
5330              gnus-tmp-thread-tree-header-string
5331              (cond
5332               ((not gnus-show-threads) "")
5333               ((zerop gnus-tmp-level)
5334                (cond ((cdar thread)
5335                       (or gnus-sum-thread-tree-root subject))
5336                      (gnus-tmp-new-adopts
5337                       (or gnus-sum-thread-tree-false-root subject))
5338                      (t
5339                       (or gnus-sum-thread-tree-single-indent subject))))
5340               (t
5341                (concat (apply 'concat
5342                               (mapcar (lambda (item)
5343                                         (if (= item 1)
5344                                             gnus-sum-thread-tree-vertical
5345                                           gnus-sum-thread-tree-indent))
5346                                       (cdr (reverse tree-stack))))
5347                        (if (nth 1 thread)
5348                            gnus-sum-thread-tree-leaf-with-other
5349                          gnus-sum-thread-tree-single-leaf)))))
5350             (when (string= gnus-tmp-name "")
5351               (setq gnus-tmp-name gnus-tmp-from))
5352             (unless (numberp gnus-tmp-lines)
5353               (setq gnus-tmp-lines -1))
5354             (if (= gnus-tmp-lines -1)
5355                 (setq gnus-tmp-lines "?")
5356               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5357               (gnus-put-text-property
5358                (point)
5359                (progn (eval gnus-summary-line-format-spec) (point))
5360                'gnus-number number)
5361               (when gnus-visual-p
5362                 (forward-line -1)
5363                 (gnus-run-hooks 'gnus-summary-update-hook)
5364                 (forward-line 1))
5365
5366               (setq gnus-tmp-prev-subject simp-subject)))
5367
5368         (when (nth 1 thread)
5369           (push (list (max 0 gnus-tmp-level)
5370                       (copy-sequence tree-stack)
5371                       (nthcdr 1 thread))
5372                 stack))
5373         (push (if (nth 1 thread) 1 0) tree-stack)
5374         (incf gnus-tmp-level)
5375         (setq threads (if thread-end nil (cdar thread)))
5376         (if gnus-summary-display-while-building
5377             (if building-count
5378                 (progn
5379                   ;; use a set frequency
5380                   (setq building-line-count (1- building-line-count))
5381                   (when (= building-line-count 0)
5382                     (sit-for 0)
5383                     (setq building-line-count
5384                           gnus-summary-display-while-building)))
5385               ;; always
5386               (sit-for 0)))
5387         (unless threads
5388           (setq gnus-tmp-level 0)))))
5389   (gnus-message 7 "Generating summary...done"))
5390
5391 (defun gnus-summary-prepare-unthreaded (headers)
5392   "Generate an unthreaded summary buffer based on HEADERS."
5393   (let (header number mark)
5394
5395     (beginning-of-line)
5396
5397     (while headers
5398       ;; We may have to root out some bad articles...
5399       (when (memq (setq number (mail-header-number
5400                                 (setq header (pop headers))))
5401                   gnus-newsgroup-limit)
5402         ;; Mark article as read when it has a low score.
5403         (when (and gnus-summary-mark-below
5404                    (< (or (cdr (assq number gnus-newsgroup-scored))
5405                           gnus-summary-default-score 0)
5406                       gnus-summary-mark-below)
5407                    (not (gnus-summary-article-ancient-p number)))
5408           (setq gnus-newsgroup-unreads
5409                 (delq number gnus-newsgroup-unreads))
5410           (if gnus-newsgroup-auto-expire
5411               (push number gnus-newsgroup-expirable)
5412             (push (cons number gnus-low-score-mark)
5413                   gnus-newsgroup-reads)))
5414
5415         (setq mark (gnus-article-mark number))
5416         (push (gnus-data-make number mark (1+ (point)) header 0)
5417               gnus-newsgroup-data)
5418         (gnus-summary-insert-line
5419          header 0 number
5420          (memq number gnus-newsgroup-undownloaded)
5421          mark (memq number gnus-newsgroup-replied)
5422          (memq number gnus-newsgroup-expirable)
5423          (mail-header-subject header) nil
5424          (cdr (assq number gnus-newsgroup-scored))
5425          (memq number gnus-newsgroup-processable))))))
5426
5427 (defun gnus-summary-remove-list-identifiers ()
5428   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5429   (let ((regexp (if (consp gnus-list-identifiers)
5430                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5431                   gnus-list-identifiers))
5432         changed subject)
5433     (when regexp
5434       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5435       (dolist (header gnus-newsgroup-headers)
5436         (setq subject (mail-header-subject header)
5437               changed nil)
5438         (while (string-match regexp subject)
5439           (setq subject
5440                 (concat (substring subject 0 (match-beginning 1))
5441                         (substring subject (match-end 0)))
5442                 changed t))
5443         (when changed
5444           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5445             (setq subject
5446                   (concat (substring subject 0 (match-beginning 1))
5447                           (substring subject (match-end 1)))))
5448           (mail-header-set-subject header subject))))))
5449
5450 (defun gnus-fetch-headers (articles)
5451   "Fetch headers of ARTICLES."
5452   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5453     (gnus-message 5 "Fetching headers for %s..." name)
5454     (prog1
5455         (if (eq 'nov
5456                 (setq gnus-headers-retrieved-by
5457                       (gnus-retrieve-headers
5458                        articles gnus-newsgroup-name
5459                        ;; We might want to fetch old headers, but
5460                        ;; not if there is only 1 article.
5461                        (and (or (and
5462                                  (not (eq gnus-fetch-old-headers 'some))
5463                                  (not (numberp gnus-fetch-old-headers)))
5464                                 (> (length articles) 1))
5465                             gnus-fetch-old-headers))))
5466             (gnus-get-newsgroup-headers-xover
5467              articles nil nil gnus-newsgroup-name t)
5468           (gnus-get-newsgroup-headers))
5469       (gnus-message 5 "Fetching headers for %s...done" name))))
5470
5471 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5472   "Select newsgroup GROUP.
5473 If READ-ALL is non-nil, all articles in the group are selected.
5474 If SELECT-ARTICLES, only select those articles from GROUP."
5475   (let* ((entry (gnus-group-entry group))
5476          ;;!!! Dirty hack; should be removed.
5477          (gnus-summary-ignore-duplicates
5478           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5479               t
5480             gnus-summary-ignore-duplicates))
5481          (info (nth 2 entry))
5482          charset articles fetched-articles cached)
5483
5484     (unless (gnus-check-server
5485              (set (make-local-variable 'gnus-current-select-method)
5486                   (gnus-find-method-for-group group)))
5487       (error "Couldn't open server"))
5488     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5489
5490     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5491         (gnus-activate-group group)     ; Or we can activate it...
5492         (progn                          ; Or we bug out.
5493           (when (equal major-mode 'gnus-summary-mode)
5494             (gnus-kill-buffer (current-buffer)))
5495           (error
5496            "Couldn't activate group %s: %s"
5497            (mm-decode-coding-string group charset)
5498            (mm-decode-coding-string (gnus-status-message group) charset))))
5499
5500     (unless (gnus-request-group group t)
5501         (when (equal major-mode 'gnus-summary-mode)
5502           (gnus-kill-buffer (current-buffer)))
5503         (error "Couldn't request group %s: %s"
5504                (mm-decode-coding-string group charset)
5505                (mm-decode-coding-string (gnus-status-message group) charset)))
5506
5507     (when gnus-agent
5508       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5509
5510       (setq gnus-summary-use-undownloaded-faces
5511             (gnus-agent-find-parameter
5512              group
5513              'agent-enable-undownloaded-faces)))
5514
5515     (setq gnus-newsgroup-name group
5516           gnus-newsgroup-unselected nil
5517           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5518
5519     (let ((display (gnus-group-find-parameter group 'display)))
5520       (setq gnus-newsgroup-display
5521             (cond
5522              ((not (zerop (or (car-safe read-all) 0)))
5523               ;; The user entered the group with C-u SPC/RET, let's show
5524               ;; all articles.
5525               'gnus-not-ignore)
5526              ((eq display 'all)
5527               'gnus-not-ignore)
5528              ((arrayp display)
5529               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5530              ((numberp display)
5531               ;; The following is probably the "correct" solution, but
5532               ;; it makes Gnus fetch all headers and then limit the
5533               ;; articles (which is slow), so instead we hack the
5534               ;; select-articles parameter instead. -- Simon Josefsson
5535               ;; <jas@kth.se>
5536               ;;
5537               ;; (gnus-byte-compile
5538               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5539               ;;                         display)))))
5540               (setq select-articles
5541                     (gnus-uncompress-range
5542                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5543                              (if (> tmp 0)
5544                                  tmp
5545                                1))
5546                            (cdr (gnus-active group)))))
5547               nil)
5548              (t
5549               nil))))
5550
5551     (gnus-summary-setup-default-charset)
5552
5553     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5554     (when (gnus-virtual-group-p group)
5555       (setq cached gnus-newsgroup-cached))
5556
5557     (setq gnus-newsgroup-unreads
5558           (gnus-sorted-ndifference
5559            (gnus-sorted-ndifference gnus-newsgroup-unreads
5560                                     gnus-newsgroup-marked)
5561            gnus-newsgroup-dormant))
5562
5563     (setq gnus-newsgroup-processable nil)
5564
5565     (gnus-update-read-articles group gnus-newsgroup-unreads)
5566
5567     ;; Adjust and set lists of article marks.
5568     (when info
5569       (gnus-adjust-marked-articles info))
5570     (if (setq articles select-articles)
5571         (setq gnus-newsgroup-unselected
5572               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5573       (setq articles (gnus-articles-to-read group read-all)))
5574
5575     (cond
5576      ((null articles)
5577       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5578       'quit)
5579      ((eq articles 0) nil)
5580      (t
5581       ;; Init the dependencies hash table.
5582       (setq gnus-newsgroup-dependencies
5583             (gnus-make-hashtable (length articles)))
5584       (gnus-set-global-variables)
5585       ;; Retrieve the headers and read them in.
5586
5587       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5588
5589       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5590       (when cached
5591         (setq gnus-newsgroup-cached cached))
5592
5593       ;; Suppress duplicates?
5594       (when gnus-suppress-duplicates
5595         (gnus-dup-suppress-articles))
5596
5597       ;; Set the initial limit.
5598       (setq gnus-newsgroup-limit (copy-sequence articles))
5599       ;; Remove canceled articles from the list of unread articles.
5600       (setq fetched-articles
5601             (mapcar (lambda (headers) (mail-header-number headers))
5602                     gnus-newsgroup-headers))
5603       (setq gnus-newsgroup-articles fetched-articles)
5604       (setq gnus-newsgroup-unreads
5605             (gnus-sorted-nintersection
5606              gnus-newsgroup-unreads fetched-articles))
5607       (gnus-compute-unseen-list)
5608
5609       ;; Removed marked articles that do not exist.
5610       (gnus-update-missing-marks
5611        (gnus-sorted-difference articles fetched-articles))
5612       ;; We might want to build some more threads first.
5613       (when (and gnus-fetch-old-headers
5614                  (eq gnus-headers-retrieved-by 'nov))
5615         (if (eq gnus-fetch-old-headers 'invisible)
5616             (gnus-build-all-threads)
5617           (gnus-build-old-threads)))
5618       ;; Let the Gnus agent mark articles as read.
5619       (when gnus-agent
5620         (gnus-agent-get-undownloaded-list))
5621       ;; Remove list identifiers from subject
5622       (when gnus-list-identifiers
5623         (gnus-summary-remove-list-identifiers))
5624       ;; Check whether auto-expire is to be done in this group.
5625       (setq gnus-newsgroup-auto-expire
5626             (gnus-group-auto-expirable-p group))
5627       ;; Set up the article buffer now, if necessary.
5628       (unless (and gnus-single-article-buffer
5629                    (equal gnus-article-buffer "*Article*"))
5630         (gnus-article-setup-buffer))
5631       ;; First and last article in this newsgroup.
5632       (when gnus-newsgroup-headers
5633         (setq gnus-newsgroup-begin
5634               (mail-header-number (car gnus-newsgroup-headers))
5635               gnus-newsgroup-end
5636               (mail-header-number
5637                (gnus-last-element gnus-newsgroup-headers))))
5638       ;; GROUP is successfully selected.
5639       (or gnus-newsgroup-headers t)))))
5640
5641 (defun gnus-compute-unseen-list ()
5642   ;; The `seen' marks are treated specially.
5643   (if (not gnus-newsgroup-seen)
5644       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5645     (setq gnus-newsgroup-unseen
5646           (gnus-inverse-list-range-intersection
5647            gnus-newsgroup-articles gnus-newsgroup-seen))))
5648
5649 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5650
5651 (defun gnus-summary-display-make-predicate (display)
5652   (require 'gnus-agent)
5653   (when (= (length display) 1)
5654     (setq display (car display)))
5655   (unless gnus-summary-display-cache
5656     (dolist (elem (append '((unread . unread)
5657                             (read . read)
5658                             (unseen . unseen))
5659                           gnus-article-mark-lists))
5660       (push (cons (cdr elem)
5661                   (gnus-byte-compile
5662                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5663             gnus-summary-display-cache)))
5664   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5665         (gnus-category-predicate-cache gnus-summary-display-cache))
5666     (gnus-get-predicate display)))
5667
5668 ;; Uses the dynamically bound `number' variable.
5669 (defvar number)
5670 (defun gnus-article-marked-p (type &optional article)
5671   (let ((article (or article number)))
5672     (cond
5673      ((eq type 'tick)
5674       (memq article gnus-newsgroup-marked))
5675      ((eq type 'spam)
5676       (memq article gnus-newsgroup-spam-marked))
5677      ((eq type 'unsend)
5678       (memq article gnus-newsgroup-unsendable))
5679      ((eq type 'undownload)
5680       (memq article gnus-newsgroup-undownloaded))
5681      ((eq type 'download)
5682       (memq article gnus-newsgroup-downloadable))
5683      ((eq type 'unread)
5684       (memq article gnus-newsgroup-unreads))
5685      ((eq type 'read)
5686       (memq article gnus-newsgroup-reads))
5687      ((eq type 'dormant)
5688       (memq article gnus-newsgroup-dormant) )
5689      ((eq type 'expire)
5690       (memq article gnus-newsgroup-expirable))
5691      ((eq type 'reply)
5692       (memq article gnus-newsgroup-replied))
5693      ((eq type 'killed)
5694       (memq article gnus-newsgroup-killed))
5695      ((eq type 'bookmark)
5696       (assq article gnus-newsgroup-bookmarks))
5697      ((eq type 'score)
5698       (assq article gnus-newsgroup-scored))
5699      ((eq type 'save)
5700       (memq article gnus-newsgroup-saved))
5701      ((eq type 'cache)
5702       (memq article gnus-newsgroup-cached))
5703      ((eq type 'forward)
5704       (memq article gnus-newsgroup-forwarded))
5705      ((eq type 'seen)
5706       (not (memq article gnus-newsgroup-unseen)))
5707      ((eq type 'recent)
5708       (memq article gnus-newsgroup-recent))
5709      (t t))))
5710
5711 (defun gnus-articles-to-read (group &optional read-all)
5712   "Find out what articles the user wants to read."
5713   (let* ((articles
5714           ;; Select all articles if `read-all' is non-nil, or if there
5715           ;; are no unread articles.
5716           (if (or read-all
5717                   (and (zerop (length gnus-newsgroup-marked))
5718                        (zerop (length gnus-newsgroup-unreads)))
5719                   ;; Fetch all if the predicate is non-nil.
5720                   gnus-newsgroup-display)
5721               ;; We want to select the headers for all the articles in
5722               ;; the group, so we select either all the active
5723               ;; articles in the group, or (if that's nil), the
5724               ;; articles in the cache.
5725               (or
5726                (if gnus-newsgroup-maximum-articles
5727                    (let ((active (gnus-active group)))
5728                      (gnus-uncompress-range
5729                       (cons (max (car active)
5730                                  (- (cdr active)
5731                                     gnus-newsgroup-maximum-articles
5732                                     -1))
5733                             (cdr active))))
5734                  (gnus-uncompress-range (gnus-active group)))
5735                (gnus-cache-articles-in-group group))
5736             ;; Select only the "normal" subset of articles.
5737             (gnus-sorted-nunion
5738              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5739              gnus-newsgroup-unreads)))
5740          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5741          (scored (length scored-list))
5742          (number (length articles))
5743          (marked (+ (length gnus-newsgroup-marked)
5744                     (length gnus-newsgroup-dormant)))
5745          (select
5746           (cond
5747            ((numberp read-all)
5748             read-all)
5749            ((numberp gnus-newsgroup-display)
5750             gnus-newsgroup-display)
5751            (t
5752             (condition-case ()
5753                 (cond
5754                  ((and (or (<= scored marked) (= scored number))
5755                        (numberp gnus-large-newsgroup)
5756                        (> number gnus-large-newsgroup))
5757                   (let* ((cursor-in-echo-area nil)
5758                          (initial (gnus-parameter-large-newsgroup-initial
5759                                    gnus-newsgroup-name))
5760                          (input
5761                           (read-string
5762                            (format
5763                             "How many articles from %s (%s %d): "
5764                             (gnus-group-decoded-name gnus-newsgroup-name)
5765                             (if initial "max" "default")
5766                             number)
5767                            (if initial
5768                                (cons (number-to-string initial)
5769                                      0)))))
5770                     (if (string-match "^[ \t]*$" input) number input)))
5771                  ((and (> scored marked) (< scored number)
5772                        (> (- scored number) 20))
5773                   (let ((input
5774                          (read-string
5775                           (format "%s %s (%d scored, %d total): "
5776                                   "How many articles from"
5777                                   (gnus-group-decoded-name group)
5778                                   scored number))))
5779                     (if (string-match "^[ \t]*$" input)
5780                         number input)))
5781                  (t number))
5782               (quit
5783                (message "Quit getting the articles to read")
5784                nil))))))
5785     (setq select (if (stringp select) (string-to-number select) select))
5786     (if (or (null select) (zerop select))
5787         select
5788       (if (and (not (zerop scored)) (<= (abs select) scored))
5789           (progn
5790             (setq articles (sort scored-list '<))
5791             (setq number (length articles)))
5792         (setq articles (copy-sequence articles)))
5793
5794       (when (< (abs select) number)
5795         (if (< select 0)
5796             ;; Select the N oldest articles.
5797             (setcdr (nthcdr (1- (abs select)) articles) nil)
5798           ;; Select the N most recent articles.
5799           (setq articles (nthcdr (- number select) articles))))
5800       (setq gnus-newsgroup-unselected
5801             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5802       (when gnus-alter-articles-to-read-function
5803         (setq articles
5804               (sort
5805                (funcall gnus-alter-articles-to-read-function
5806                         gnus-newsgroup-name articles)
5807                '<)))
5808       articles)))
5809
5810 (defun gnus-killed-articles (killed articles)
5811   (let (out)
5812     (while articles
5813       (when (inline (gnus-member-of-range (car articles) killed))
5814         (push (car articles) out))
5815       (setq articles (cdr articles)))
5816     out))
5817
5818 (defun gnus-uncompress-marks (marks)
5819   "Uncompress the mark ranges in MARKS."
5820   (let ((uncompressed '(score bookmark))
5821         out)
5822     (while marks
5823       (if (memq (caar marks) uncompressed)
5824           (push (car marks) out)
5825         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5826       (setq marks (cdr marks)))
5827     out))
5828
5829 (defun gnus-article-mark-to-type (mark)
5830   "Return the type of MARK."
5831   (or (cadr (assq mark gnus-article-special-mark-lists))
5832       'list))
5833
5834 (defun gnus-article-unpropagatable-p (mark)
5835   "Return whether MARK should be propagated to back end."
5836   (memq mark gnus-article-unpropagated-mark-lists))
5837
5838 (defun gnus-adjust-marked-articles (info)
5839   "Set all article lists and remove all marks that are no longer valid."
5840   (let* ((marked-lists (gnus-info-marks info))
5841          (active (gnus-active (gnus-info-group info)))
5842          (min (car active))
5843          (max (cdr active))
5844          (types gnus-article-mark-lists)
5845          marks var articles article mark mark-type
5846          bgn end)
5847
5848     (dolist (marks marked-lists)
5849       (setq mark (car marks)
5850             mark-type (gnus-article-mark-to-type mark)
5851             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5852
5853       ;; We set the variable according to the type of the marks list,
5854       ;; and then adjust the marks to a subset of the active articles.
5855       (cond
5856        ;; Adjust "simple" lists - compressed yet unsorted
5857        ((eq mark-type 'list)
5858         ;; Simultaneously uncompress and clip to active range
5859         ;; See gnus-uncompress-range for a description of possible marks
5860         (let (l lh)
5861           (if (not (cadr marks))
5862               (set var nil)
5863             (setq articles (if (numberp (cddr marks))
5864                                (list (cdr marks))
5865                              (cdr marks))
5866                   lh (cons nil nil)
5867                   l lh)
5868
5869             (while (setq article (pop articles))
5870               (cond ((consp article)
5871                      (setq bgn (max (car article) min)
5872                            end (min (cdr article) max))
5873                      (while (<= bgn end)
5874                        (setq l (setcdr l (cons bgn nil))
5875                              bgn (1+ bgn))))
5876                     ((and (<= min article)
5877                           (>= max article))
5878                      (setq l (setcdr l (cons article nil))))))
5879             (set var (cdr lh)))))
5880        ;; Adjust assocs.
5881        ((eq mark-type 'tuple)
5882         (set var (setq articles (cdr marks)))
5883         (when (not (listp (cdr (symbol-value var))))
5884           (set var (list (symbol-value var))))
5885         (when (not (listp (cdr articles)))
5886           (setq articles (list articles)))
5887         (while articles
5888           (when (or (not (consp (setq article (pop articles))))
5889                     (< (car article) min)
5890                     (> (car article) max))
5891             (set var (delq article (symbol-value var))))))
5892        ;; Adjust ranges (sloppily).
5893        ((eq mark-type 'range)
5894         (cond
5895          ((eq mark 'seen)
5896           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5897           ;; It should be (seen (NUM1 . NUM2)).
5898           (when (numberp (cddr marks))
5899             (setcdr marks (list (cdr marks))))
5900           (setq articles (cdr marks))
5901           (while (and articles
5902                       (or (and (consp (car articles))
5903                                (> min (cdar articles)))
5904                           (and (numberp (car articles))
5905                                (> min (car articles)))))
5906             (pop articles))
5907           (set var articles))))))))
5908
5909 (defun gnus-update-missing-marks (missing)
5910   "Go through the list of MISSING articles and remove them from the mark lists."
5911   (when missing
5912     (let (var m)
5913       ;; Go through all types.
5914       (dolist (elem gnus-article-mark-lists)
5915         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5916           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5917           (when (symbol-value var)
5918             ;; This list has articles.  So we delete all missing
5919             ;; articles from it.
5920             (setq m missing)
5921             (while m
5922               (set var (delq (pop m) (symbol-value var))))))))))
5923
5924 (defun gnus-update-marks ()
5925   "Enter the various lists of marked articles into the newsgroup info list."
5926   (let ((types gnus-article-mark-lists)
5927         (info (gnus-get-info gnus-newsgroup-name))
5928         type list newmarked symbol delta-marks)
5929     (when info
5930       ;; Add all marks lists to the list of marks lists.
5931       (while (setq type (pop types))
5932         (setq list (symbol-value
5933                     (setq symbol
5934                           (intern (format "gnus-newsgroup-%s" (car type))))))
5935
5936         (when list
5937           ;; Get rid of the entries of the articles that have the
5938           ;; default score.
5939           (when (and (eq (cdr type) 'score)
5940                      gnus-save-score
5941                      list)
5942             (let* ((arts list)
5943                    (prev (cons nil list))
5944                    (all prev))
5945               (while arts
5946                 (if (or (not (consp (car arts)))
5947                         (= (cdar arts) gnus-summary-default-score))
5948                     (setcdr prev (cdr arts))
5949                   (setq prev arts))
5950                 (setq arts (cdr arts)))
5951               (setq list (cdr all)))))
5952
5953         (when (eq (cdr type) 'seen)
5954           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5955
5956         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5957           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5958
5959         (when (and (gnus-check-backend-function
5960                     'request-set-mark gnus-newsgroup-name)
5961                    (not (gnus-article-unpropagatable-p (cdr type))))
5962           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5963                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5964                  (add (gnus-remove-from-range
5965                        (gnus-copy-sequence list) old)))
5966             (when add
5967               (push (list add 'add (list (cdr type))) delta-marks))
5968             (when del
5969               (push (list del 'del (list (cdr type))) delta-marks))))
5970
5971         (when list
5972           (push (cons (cdr type) list) newmarked)))
5973
5974       (when delta-marks
5975         (unless (gnus-check-group gnus-newsgroup-name)
5976           (error "Can't open server for %s" gnus-newsgroup-name))
5977         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5978
5979       ;; Enter these new marks into the info of the group.
5980       (if (nthcdr 3 info)
5981           (setcar (nthcdr 3 info) newmarked)
5982         ;; Add the marks lists to the end of the info.
5983         (when newmarked
5984           (setcdr (nthcdr 2 info) (list newmarked))))
5985
5986       ;; Cut off the end of the info if there's nothing else there.
5987       (let ((i 5))
5988         (while (and (> i 2)
5989                     (not (nth i info)))
5990           (when (nthcdr (decf i) info)
5991             (setcdr (nthcdr i info) nil)))))))
5992
5993 (defun gnus-set-mode-line (where)
5994   "Set the mode line of the article or summary buffers.
5995 If WHERE is `summary', the summary mode line format will be used."
5996   ;; Is this mode line one we keep updated?
5997   (when (and (memq where gnus-updated-mode-lines)
5998              (symbol-value
5999               (intern (format "gnus-%s-mode-line-format-spec" where))))
6000     (let (mode-string)
6001       ;; We evaluate this in the summary buffer since these
6002       ;; variables are buffer-local to that buffer.
6003       (with-current-buffer gnus-summary-buffer
6004         ;; We bind all these variables that are used in the `eval' form
6005         ;; below.
6006         (let* ((mformat (symbol-value
6007                          (intern
6008                           (format "gnus-%s-mode-line-format-spec" where))))
6009                (gnus-tmp-group-name (gnus-mode-string-quote
6010                                      (gnus-group-decoded-name
6011                                       gnus-newsgroup-name)))
6012                (gnus-tmp-article-number (or gnus-current-article 0))
6013                (gnus-tmp-unread gnus-newsgroup-unreads)
6014                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6015                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6016                (gnus-tmp-unread-and-unselected
6017                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6018                             (zerop gnus-tmp-unselected))
6019                        "")
6020                       ((zerop gnus-tmp-unselected)
6021                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6022                       (t (format "{%d(+%d) more}"
6023                                  gnus-tmp-unread-and-unticked
6024                                  gnus-tmp-unselected))))
6025                (gnus-tmp-subject
6026                 (if (and gnus-current-headers
6027                          (vectorp gnus-current-headers))
6028                     (gnus-mode-string-quote
6029                      (mail-header-subject gnus-current-headers))
6030                   ""))
6031                bufname-length max-len
6032                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6033           (setq mode-string (eval mformat))
6034           (setq bufname-length (if (string-match "%b" mode-string)
6035                                    (- (length
6036                                        (buffer-name
6037                                         (if (eq where 'summary)
6038                                             nil
6039                                           (get-buffer gnus-article-buffer))))
6040                                       2)
6041                                  0))
6042           (setq max-len (max 4 (if gnus-mode-non-string-length
6043                                    (- (window-width)
6044                                       gnus-mode-non-string-length
6045                                       bufname-length)
6046                                  (length mode-string))))
6047           ;; We might have to chop a bit of the string off...
6048           (when (> (length mode-string) max-len)
6049             (setq mode-string
6050                   (concat (truncate-string-to-width mode-string (- max-len 3))
6051                           "...")))
6052           ;; Pad the mode string a bit.
6053           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6054       ;; Update the mode line.
6055       (setq mode-line-buffer-identification
6056             (gnus-mode-line-buffer-identification (list mode-string)))
6057       (set-buffer-modified-p t))))
6058
6059 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6060   "Go through the HEADERS list and add all Xrefs to a hash table.
6061 The resulting hash table is returned, or nil if no Xrefs were found."
6062   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6063          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6064          (xref-hashtb (gnus-make-hashtable))
6065          start group entry number xrefs header)
6066     (while headers
6067       (setq header (pop headers))
6068       (when (and (setq xrefs (mail-header-xref header))
6069                  (not (memq (setq number (mail-header-number header))
6070                             unreads)))
6071         (setq start 0)
6072         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6073           (setq start (match-end 0))
6074           (setq group (if prefix
6075                           (concat prefix (substring xrefs (match-beginning 1)
6076                                                     (match-end 1)))
6077                         (substring xrefs (match-beginning 1) (match-end 1))))
6078           (setq number
6079                 (string-to-number (substring xrefs (match-beginning 2)
6080                                           (match-end 2))))
6081           (if (setq entry (gnus-gethash group xref-hashtb))
6082               (setcdr entry (cons number (cdr entry)))
6083             (gnus-sethash group (cons number nil) xref-hashtb)))))
6084     (and start xref-hashtb)))
6085
6086 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6087   "Look through all the headers and mark the Xrefs as read."
6088   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6089         name info xref-hashtb idlist method nth4)
6090     (with-current-buffer gnus-group-buffer
6091       (when (setq xref-hashtb
6092                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6093         (mapatoms
6094          (lambda (group)
6095            (unless (string= from-newsgroup (setq name (symbol-name group)))
6096              (setq idlist (symbol-value group))
6097              ;; Dead groups are not updated.
6098              (and (prog1
6099                       (setq info (gnus-get-info name))
6100                     (when (stringp (setq nth4 (gnus-info-method info)))
6101                       (setq nth4 (gnus-server-to-method nth4))))
6102                   ;; Only do the xrefs if the group has the same
6103                   ;; select method as the group we have just read.
6104                   (or (gnus-methods-equal-p
6105                        nth4 (gnus-find-method-for-group from-newsgroup))
6106                       virtual
6107                       (equal nth4 (setq method (gnus-find-method-for-group
6108                                                 from-newsgroup)))
6109                       (and (equal (car nth4) (car method))
6110                            (equal (nth 1 nth4) (nth 1 method))))
6111                   gnus-use-cross-reference
6112                   (or (not (eq gnus-use-cross-reference t))
6113                       virtual
6114                       ;; Only do cross-references on subscribed
6115                       ;; groups, if that is what is wanted.
6116                       (<= (gnus-info-level info) gnus-level-subscribed))
6117                   (gnus-group-make-articles-read name idlist))))
6118          xref-hashtb)))))
6119
6120 (defun gnus-compute-read-articles (group articles)
6121   (let* ((entry (gnus-group-entry group))
6122          (info (nth 2 entry))
6123          (active (gnus-active group))
6124          ninfo)
6125     (when entry
6126       ;; First peel off all invalid article numbers.
6127       (when active
6128         (let ((ids articles)
6129               id first)
6130           (while (setq id (pop ids))
6131             (when (and first (> id (cdr active)))
6132               ;; We'll end up in this situation in one particular
6133               ;; obscure situation.  If you re-scan a group and get
6134               ;; a new article that is cross-posted to a different
6135               ;; group that has not been re-scanned, you might get
6136               ;; crossposted article that has a higher number than
6137               ;; Gnus believes possible.  So we re-activate this
6138               ;; group as well.  This might mean doing the
6139               ;; crossposting thingy will *increase* the number
6140               ;; of articles in some groups.  Tsk, tsk.
6141               (setq active (or (gnus-activate-group group) active)))
6142             (when (or (> id (cdr active))
6143                       (< id (car active)))
6144               (setq articles (delq id articles))))))
6145       ;; If the read list is nil, we init it.
6146       (if (and active
6147                (null (gnus-info-read info))
6148                (> (car active) 1))
6149           (setq ninfo (cons 1 (1- (car active))))
6150         (setq ninfo (gnus-info-read info)))
6151       ;; Then we add the read articles to the range.
6152       (gnus-add-to-range
6153        ninfo (setq articles (sort articles '<))))))
6154
6155 (defun gnus-group-make-articles-read (group articles)
6156   "Update the info of GROUP to say that ARTICLES are read."
6157   (let* ((num 0)
6158          (entry (gnus-group-entry group))
6159          (info (nth 2 entry))
6160          (active (gnus-active group))
6161          range)
6162     (when entry
6163       (setq range (gnus-compute-read-articles group articles))
6164       (with-current-buffer gnus-group-buffer
6165         (gnus-undo-register
6166           `(progn
6167              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6168              (gnus-info-set-read ',info ',(gnus-info-read info))
6169              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6170              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6171              (gnus-group-update-group ,group t))))
6172       ;; Add the read articles to the range.
6173       (gnus-info-set-read info range)
6174       (gnus-request-set-mark group (list (list range 'add '(read))))
6175       ;; Then we have to re-compute how many unread
6176       ;; articles there are in this group.
6177       (when active
6178         (cond
6179          ((not range)
6180           (setq num (- (1+ (cdr active)) (car active))))
6181          ((not (listp (cdr range)))
6182           (setq num (- (cdr active) (- (1+ (cdr range))
6183                                        (car range)))))
6184          (t
6185           (while range
6186             (if (numberp (car range))
6187                 (setq num (1+ num))
6188               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6189             (setq range (cdr range)))
6190           (setq num (- (cdr active) num))))
6191         ;; Update the number of unread articles.
6192         (setcar entry num)
6193         ;; Update the group buffer.
6194         (unless (gnus-ephemeral-group-p group)
6195           (gnus-group-update-group group t))))))
6196
6197 (defvar gnus-newsgroup-none-id 0)
6198
6199 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6200   (let ((cur nntp-server-buffer)
6201         (dependencies
6202          (or dependencies
6203              (with-current-buffer gnus-summary-buffer
6204                gnus-newsgroup-dependencies)))
6205         headers id end ref number
6206         (mail-parse-charset gnus-newsgroup-charset)
6207         (mail-parse-ignored-charsets
6208          (save-current-buffer (condition-case nil
6209                                   (set-buffer gnus-summary-buffer)
6210                                 (error))
6211                               gnus-newsgroup-ignored-charsets)))
6212     (with-current-buffer nntp-server-buffer
6213       ;; Translate all TAB characters into SPACE characters.
6214       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6215       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6216       (ietf-drums-unfold-fws)
6217       (gnus-run-hooks 'gnus-parse-headers-hook)
6218       (let ((case-fold-search t)
6219             in-reply-to header p lines chars)
6220         (goto-char (point-min))
6221         ;; Search to the beginning of the next header.  Error messages
6222         ;; do not begin with 2 or 3.
6223         (while (re-search-forward "^[23][0-9]+ " nil t)
6224           (setq id nil
6225                 ref nil)
6226           ;; This implementation of this function, with nine
6227           ;; search-forwards instead of the one re-search-forward and
6228           ;; a case (which basically was the old function) is actually
6229           ;; about twice as fast, even though it looks messier.  You
6230           ;; can't have everything, I guess.  Speed and elegance
6231           ;; doesn't always go hand in hand.
6232           (setq
6233            header
6234            (vector
6235             ;; Number.
6236             (prog1
6237                 (setq number (read cur))
6238               (end-of-line)
6239               (setq p (point))
6240               (narrow-to-region (point)
6241                                 (or (and (search-forward "\n.\n" nil t)
6242                                          (- (point) 2))
6243                                     (point))))
6244             ;; Subject.
6245             (progn
6246               (goto-char p)
6247               (if (search-forward "\nsubject:" nil t)
6248                   (funcall gnus-decode-encoded-word-function
6249                            (nnheader-header-value))
6250                 "(none)"))
6251             ;; From.
6252             (progn
6253               (goto-char p)
6254               (if (search-forward "\nfrom:" nil t)
6255                   (funcall gnus-decode-encoded-address-function
6256                            (nnheader-header-value))
6257                 "(nobody)"))
6258             ;; Date.
6259             (progn
6260               (goto-char p)
6261               (if (search-forward "\ndate:" nil t)
6262                   (nnheader-header-value) ""))
6263             ;; Message-ID.
6264             (progn
6265               (goto-char p)
6266               (setq id (if (re-search-forward
6267                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6268                            ;; We do it this way to make sure the Message-ID
6269                            ;; is (somewhat) syntactically valid.
6270                            (buffer-substring (match-beginning 1)
6271                                              (match-end 1))
6272                          ;; If there was no message-id, we just fake one
6273                          ;; to make subsequent routines simpler.
6274                          (nnheader-generate-fake-message-id number))))
6275             ;; References.
6276             (progn
6277               (goto-char p)
6278               (if (search-forward "\nreferences:" nil t)
6279                   (progn
6280                     (setq end (point))
6281                     (prog1
6282                         (nnheader-header-value)
6283                       (setq ref
6284                             (buffer-substring
6285                              (progn
6286                                (end-of-line)
6287                                (search-backward ">" end t)
6288                                (1+ (point)))
6289                              (progn
6290                                (search-backward "<" end t)
6291                                (point))))))
6292                 ;; Get the references from the in-reply-to header if there
6293                 ;; were no references and the in-reply-to header looks
6294                 ;; promising.
6295                 (if (and (search-forward "\nin-reply-to:" nil t)
6296                          (setq in-reply-to (nnheader-header-value))
6297                          (string-match "<[^>]+>" in-reply-to))
6298                     (let (ref2)
6299                       (setq ref (substring in-reply-to (match-beginning 0)
6300                                            (match-end 0)))
6301                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6302                         (setq ref2 (substring in-reply-to (match-beginning 0)
6303                                               (match-end 0)))
6304                         (when (> (length ref2) (length ref))
6305                           (setq ref ref2)))
6306                       ref)
6307                   (setq ref nil))))
6308             ;; Chars.
6309             (progn
6310               (goto-char p)
6311               (if (search-forward "\nchars: " nil t)
6312                   (if (numberp (setq chars (ignore-errors (read cur))))
6313                       chars -1)
6314                 -1))
6315             ;; Lines.
6316             (progn
6317               (goto-char p)
6318               (if (search-forward "\nlines: " nil t)
6319                   (if (numberp (setq lines (ignore-errors (read cur))))
6320                       lines -1)
6321                 -1))
6322             ;; Xref.
6323             (progn
6324               (goto-char p)
6325               (and (search-forward "\nxref:" nil t)
6326                    (nnheader-header-value)))
6327             ;; Extra.
6328             (when gnus-extra-headers
6329               (let ((extra gnus-extra-headers)
6330                     out)
6331                 (while extra
6332                   (goto-char p)
6333                   (when (search-forward
6334                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6335                     (push (cons (car extra) (nnheader-header-value))
6336                           out))
6337                   (pop extra))
6338                 out))))
6339           (when (equal id ref)
6340             (setq ref nil))
6341
6342           (when gnus-alter-header-function
6343             (funcall gnus-alter-header-function header)
6344             (setq id (mail-header-id header)
6345                   ref (gnus-parent-id (mail-header-references header))))
6346
6347           (when (setq header
6348                       (gnus-dependencies-add-header
6349                        header dependencies force-new))
6350             (push header headers))
6351           (goto-char (point-max))
6352           (widen))
6353         (nreverse headers)))))
6354
6355 ;; Goes through the xover lines and returns a list of vectors
6356 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6357                                                   force-new dependencies
6358                                                   group also-fetch-heads)
6359   "Parse the news overview data in the server buffer.
6360 Return a list of headers that match SEQUENCE (see
6361 `nntp-retrieve-headers')."
6362   ;; Get the Xref when the users reads the articles since most/some
6363   ;; NNTP servers do not include Xrefs when using XOVER.
6364   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6365   (let ((mail-parse-charset gnus-newsgroup-charset)
6366         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6367         (cur nntp-server-buffer)
6368         (dependencies (or dependencies gnus-newsgroup-dependencies))
6369         (allp (cond
6370                ((eq gnus-read-all-available-headers t)
6371                 t)
6372                ((and (stringp gnus-read-all-available-headers)
6373                      group)
6374                 (string-match gnus-read-all-available-headers group))
6375                (t
6376                 nil)))
6377         number headers header)
6378     (with-current-buffer nntp-server-buffer
6379       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6380       ;; Allow the user to mangle the headers before parsing them.
6381       (gnus-run-hooks 'gnus-parse-headers-hook)
6382       (goto-char (point-min))
6383       (gnus-parse-without-error
6384         (while (and (or sequence allp)
6385                     (not (eobp)))
6386           (setq number (read cur))
6387           (when (not allp)
6388             (while (and sequence
6389                         (< (car sequence) number))
6390               (setq sequence (cdr sequence))))
6391           (when (and (or allp
6392                          (and sequence
6393                               (eq number (car sequence))))
6394                      (progn
6395                        (setq sequence (cdr sequence))
6396                        (setq header (inline
6397                                       (gnus-nov-parse-line
6398                                        number dependencies force-new)))))
6399             (push header headers))
6400           (forward-line 1)))
6401       ;; A common bug in inn is that if you have posted an article and
6402       ;; then retrieves the active file, it will answer correctly --
6403       ;; the new article is included.  However, a NOV entry for the
6404       ;; article may not have been generated yet, so this may fail.
6405       ;; We work around this problem by retrieving the last few
6406       ;; headers using HEAD.
6407       (if (or (not also-fetch-heads)
6408               (not sequence))
6409           ;; We (probably) got all the headers.
6410           (nreverse headers)
6411         (let ((gnus-nov-is-evil t))
6412           (nconc
6413            (nreverse headers)
6414            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6415              (gnus-get-newsgroup-headers))))))))
6416
6417 (defun gnus-article-get-xrefs ()
6418   "Fill in the Xref value in `gnus-current-headers', if necessary.
6419 This is meant to be called in `gnus-article-internal-prepare-hook'."
6420   (let ((headers (with-current-buffer gnus-summary-buffer
6421                    gnus-current-headers)))
6422     (or (not gnus-use-cross-reference)
6423         (not headers)
6424         (and (mail-header-xref headers)
6425              (not (string= (mail-header-xref headers) "")))
6426         (let ((case-fold-search t)
6427               xref)
6428           (save-restriction
6429             (nnheader-narrow-to-headers)
6430             (goto-char (point-min))
6431             (when (or (and (not (eobp))
6432                            (eq (downcase (char-after)) ?x)
6433                            (looking-at "Xref:"))
6434                       (search-forward "\nXref:" nil t))
6435               (goto-char (1+ (match-end 0)))
6436               (setq xref (buffer-substring (point) (point-at-eol)))
6437               (mail-header-set-xref headers xref)))))))
6438
6439 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6440   "Find article ID and insert the summary line for that article.
6441 OLD-HEADER can either be a header or a line number to insert
6442 the subject line on."
6443   (let* ((line (and (numberp old-header) old-header))
6444          (old-header (and (vectorp old-header) old-header))
6445          (header (cond ((and old-header use-old-header)
6446                         old-header)
6447                        ((and (numberp id)
6448                              (gnus-number-to-header id))
6449                         (gnus-number-to-header id))
6450                        (t
6451                         (gnus-read-header id))))
6452          (number (and (numberp id) id))
6453          d)
6454     (when header
6455       ;; Rebuild the thread that this article is part of and go to the
6456       ;; article we have fetched.
6457       (when (and (not gnus-show-threads)
6458                  old-header)
6459         (when (and number
6460                    (setq d (gnus-data-find (mail-header-number old-header))))
6461           (goto-char (gnus-data-pos d))
6462           (gnus-data-remove
6463            number
6464            (- (point-at-bol)
6465               (prog1
6466                   (1+ (point-at-eol))
6467                 (gnus-delete-line))))))
6468       ;; Remove list identifiers from subject.
6469       (when gnus-list-identifiers
6470         (let ((gnus-newsgroup-headers (list header)))
6471           (gnus-summary-remove-list-identifiers)))
6472       (when old-header
6473         (mail-header-set-number header (mail-header-number old-header)))
6474       (setq gnus-newsgroup-sparse
6475             (delq (setq number (mail-header-number header))
6476                   gnus-newsgroup-sparse))
6477       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6478       (push number gnus-newsgroup-limit)
6479       (gnus-rebuild-thread (mail-header-id header) line)
6480       (gnus-summary-goto-subject number nil t))
6481     (when (and (numberp number)
6482                (> number 0))
6483       ;; We have to update the boundaries even if we can't fetch the
6484       ;; article if ID is a number -- so that the next `P' or `N'
6485       ;; command will fetch the previous (or next) article even
6486       ;; if the one we tried to fetch this time has been canceled.
6487       (when (> number gnus-newsgroup-end)
6488         (setq gnus-newsgroup-end number))
6489       (when (< number gnus-newsgroup-begin)
6490         (setq gnus-newsgroup-begin number))
6491       (setq gnus-newsgroup-unselected
6492             (delq number gnus-newsgroup-unselected)))
6493     ;; Report back a success?
6494     (and header (mail-header-number header))))
6495
6496 ;;; Process/prefix in the summary buffer
6497
6498 (defun gnus-summary-work-articles (n)
6499   "Return a list of articles to be worked upon.
6500 The prefix argument, the list of process marked articles, and the
6501 current article will be taken into consideration."
6502   (with-current-buffer gnus-summary-buffer
6503     (cond
6504      (n
6505       ;; A numerical prefix has been given.
6506       (setq n (prefix-numeric-value n))
6507       (let ((backward (< n 0))
6508             (n (abs (prefix-numeric-value n)))
6509             articles article)
6510         (save-excursion
6511           (while
6512               (and (> n 0)
6513                    (push (setq article (gnus-summary-article-number))
6514                          articles)
6515                    (if backward
6516                        (gnus-summary-find-prev nil article)
6517                      (gnus-summary-find-next nil article)))
6518             (decf n)))
6519         (nreverse articles)))
6520      ((and (gnus-region-active-p) (mark))
6521       (message "region active")
6522       ;; Work on the region between point and mark.
6523       (let ((max (max (point) (mark)))
6524             articles article)
6525         (save-excursion
6526           (goto-char (min (point) (mark)))
6527           (while
6528               (and
6529                (push (setq article (gnus-summary-article-number)) articles)
6530                (gnus-summary-find-next nil article)
6531                (< (point) max)))
6532           (nreverse articles))))
6533      (gnus-newsgroup-processable
6534       ;; There are process-marked articles present.
6535       ;; Save current state.
6536       (gnus-summary-save-process-mark)
6537       ;; Return the list.
6538       (reverse gnus-newsgroup-processable))
6539      (t
6540       ;; Just return the current article.
6541       (list (gnus-summary-article-number))))))
6542
6543 (defmacro gnus-summary-iterate (arg &rest forms)
6544   "Iterate over the process/prefixed articles and do FORMS.
6545 ARG is the interactive prefix given to the command.  FORMS will be
6546 executed with point over the summary line of the articles."
6547   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6548     `(let ((,articles (gnus-summary-work-articles ,arg)))
6549        (while ,articles
6550          (gnus-summary-goto-subject (car ,articles))
6551          ,@forms
6552          (pop ,articles)))))
6553
6554 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6555 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6556
6557 (defun gnus-summary-save-process-mark ()
6558   "Push the current set of process marked articles on the stack."
6559   (interactive)
6560   (push (copy-sequence gnus-newsgroup-processable)
6561         gnus-newsgroup-process-stack))
6562
6563 (defun gnus-summary-kill-process-mark ()
6564   "Push the current set of process marked articles on the stack and unmark."
6565   (interactive)
6566   (gnus-summary-save-process-mark)
6567   (gnus-summary-unmark-all-processable))
6568
6569 (defun gnus-summary-yank-process-mark ()
6570   "Pop the last process mark state off the stack and restore it."
6571   (interactive)
6572   (unless gnus-newsgroup-process-stack
6573     (error "Empty mark stack"))
6574   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6575
6576 (defun gnus-summary-process-mark-set (set)
6577   "Make SET into the current process marked articles."
6578   (gnus-summary-unmark-all-processable)
6579   (mapc 'gnus-summary-set-process-mark set))
6580
6581 ;;; Searching and stuff
6582
6583 (defun gnus-summary-search-group (&optional backward use-level)
6584   "Search for next unread newsgroup.
6585 If optional argument BACKWARD is non-nil, search backward instead."
6586   (with-current-buffer gnus-group-buffer
6587     (when (gnus-group-search-forward
6588            backward nil (if use-level (gnus-group-group-level) nil))
6589       (gnus-group-group-name))))
6590
6591 (defun gnus-summary-best-group (&optional exclude-group)
6592   "Find the name of the best unread group.
6593 If EXCLUDE-GROUP, do not go to this group."
6594   (with-current-buffer gnus-group-buffer
6595     (save-excursion
6596       (gnus-group-best-unread-group exclude-group))))
6597
6598 (defun gnus-summary-find-next (&optional unread article backward)
6599   (if backward
6600       (gnus-summary-find-prev unread article)
6601     (let* ((dummy (gnus-summary-article-intangible-p))
6602            (article (or article (gnus-summary-article-number)))
6603            (data (gnus-data-find-list article))
6604            result)
6605       (when (and (not dummy)
6606                  (or (not gnus-summary-check-current)
6607                      (not unread)
6608                      (not (gnus-data-unread-p (car data)))))
6609         (setq data (cdr data)))
6610       (when (setq result
6611                   (if unread
6612                       (progn
6613                         (while data
6614                           (unless (memq (gnus-data-number (car data))
6615                                         (cond
6616                                          ((eq gnus-auto-goto-ignores
6617                                               'always-undownloaded)
6618                                           gnus-newsgroup-undownloaded)
6619                                          (gnus-plugged
6620                                           nil)
6621                                          ((eq gnus-auto-goto-ignores
6622                                               'unfetched)
6623                                           gnus-newsgroup-unfetched)
6624                                          ((eq gnus-auto-goto-ignores
6625                                               'undownloaded)
6626                                           gnus-newsgroup-undownloaded)))
6627                             (when (gnus-data-unread-p (car data))
6628                               (setq result (car data)
6629                                     data nil)))
6630                           (setq data (cdr data)))
6631                         result)
6632                     (car data)))
6633         (goto-char (gnus-data-pos result))
6634         (gnus-data-number result)))))
6635
6636 (defun gnus-summary-find-prev (&optional unread article)
6637   (let* ((eobp (eobp))
6638          (article (or article (gnus-summary-article-number)))
6639          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6640          result)
6641     (when (and (not eobp)
6642                (or (not gnus-summary-check-current)
6643                    (not unread)
6644                    (not (gnus-data-unread-p (car data)))))
6645       (setq data (cdr data)))
6646     (when (setq result
6647                 (if unread
6648                     (progn
6649                       (while data
6650                         (unless (memq (gnus-data-number (car data))
6651                                       (cond
6652                                        ((eq gnus-auto-goto-ignores
6653                                             'always-undownloaded)
6654                                         gnus-newsgroup-undownloaded)
6655                                        (gnus-plugged
6656                                         nil)
6657                                        ((eq gnus-auto-goto-ignores
6658                                             'unfetched)
6659                                         gnus-newsgroup-unfetched)
6660                                        ((eq gnus-auto-goto-ignores
6661                                             'undownloaded)
6662                                         gnus-newsgroup-undownloaded)))
6663                           (when (gnus-data-unread-p (car data))
6664                             (setq result (car data)
6665                                   data nil)))
6666                         (setq data (cdr data)))
6667                       result)
6668                   (car data)))
6669       (goto-char (gnus-data-pos result))
6670       (gnus-data-number result))))
6671
6672 (defun gnus-summary-find-subject (subject &optional unread backward article)
6673   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6674          (article (or article (gnus-summary-article-number)))
6675          (articles (gnus-data-list backward))
6676          (arts (gnus-data-find-list article articles))
6677          result)
6678     (when (or (not gnus-summary-check-current)
6679               (not unread)
6680               (not (gnus-data-unread-p (car arts))))
6681       (setq arts (cdr arts)))
6682     (while arts
6683       (and (or (not unread)
6684                (gnus-data-unread-p (car arts)))
6685            (vectorp (gnus-data-header (car arts)))
6686            (gnus-subject-equal
6687             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6688            (setq result (car arts)
6689                  arts nil))
6690       (setq arts (cdr arts)))
6691     (and result
6692          (goto-char (gnus-data-pos result))
6693          (gnus-data-number result))))
6694
6695 (defun gnus-summary-search-forward (&optional unread subject backward)
6696   "Search forward for an article.
6697 If UNREAD, look for unread articles.  If SUBJECT, look for
6698 articles with that subject.  If BACKWARD, search backward instead."
6699   (cond (subject (gnus-summary-find-subject subject unread backward))
6700         (backward (gnus-summary-find-prev unread))
6701         (t (gnus-summary-find-next unread))))
6702
6703 (defun gnus-recenter (&optional n)
6704   "Center point in window and redisplay frame.
6705 Also do horizontal recentering."
6706   (interactive "P")
6707   (when (and gnus-auto-center-summary
6708              (not (eq gnus-auto-center-summary 'vertical)))
6709     (gnus-horizontal-recenter))
6710   (if (fboundp 'recenter-top-bottom)
6711       (recenter-top-bottom n)
6712     (recenter n)))
6713
6714 (put 'gnus-recenter 'isearch-scroll t)
6715
6716 (defun gnus-forward-line-ignore-invisible (n)
6717   "Move N lines forward (backward if N is negative).
6718 Like forward-line, but skip over (and don't count) invisible lines."
6719   (let (done)
6720     (while (and (> n 0) (not done))
6721       ;; If the following character is currently invisible,
6722       ;; skip all characters with that same `invisible' property value.
6723       (while (gnus-invisible-p (point))
6724         (goto-char (gnus-next-char-property-change (point))))
6725       (forward-line 1)
6726       (if (eobp)
6727           (setq done t)
6728         (setq n (1- n))))
6729     (while (and (< n 0) (not done))
6730       (forward-line -1)
6731       (if (bobp) (setq done t)
6732         (setq n (1+ n))
6733         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6734           (goto-char (gnus-previous-char-property-change (point))))))))
6735
6736 (defun gnus-summary-recenter ()
6737   "Center point in the summary window.
6738 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6739 displayed, no centering will be performed."
6740   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6741   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6742   (interactive)
6743   ;; The user has to want it.
6744   (when gnus-auto-center-summary
6745     (let* ((top (cond ((< (window-height) 4) 0)
6746                       ((< (window-height) 7) 1)
6747                       (t (if (numberp gnus-auto-center-summary)
6748                              gnus-auto-center-summary
6749                            (/ (1- (window-height)) 2)))))
6750            (height (1- (window-height)))
6751            (bottom (save-excursion
6752                      (goto-char (point-max))
6753                      (gnus-forward-line-ignore-invisible (- height))
6754                      (point)))
6755            (window (get-buffer-window (current-buffer))))
6756       (when (get-buffer-window gnus-article-buffer)
6757         ;; Only do recentering when the article buffer is displayed,
6758         ;; Set the window start to either `bottom', which is the biggest
6759         ;; possible valid number, or the second line from the top,
6760         ;; whichever is the least.
6761         (let ((top-pos (save-excursion
6762                          (gnus-forward-line-ignore-invisible (- top))
6763                          (point))))
6764           (if (> bottom top-pos)
6765               ;; Keep the second line from the top visible
6766               (set-window-start window top-pos)
6767             ;; Try to keep the bottom line visible; if it's partially
6768             ;; obscured, either scroll one more line to make it fully
6769             ;; visible, or revert to using TOP-POS.
6770             (save-excursion
6771               (goto-char (point-max))
6772               (gnus-forward-line-ignore-invisible -1)
6773               (let ((last-line-start (point)))
6774                 (goto-char bottom)
6775                 (set-window-start window (point) t)
6776                 (when (not (pos-visible-in-window-p last-line-start window))
6777                   (gnus-forward-line-ignore-invisible 1)
6778                   (set-window-start window (min (point) top-pos) t)))))))
6779       ;; Do horizontal recentering while we're at it.
6780       (when (and (get-buffer-window (current-buffer) t)
6781                  (not (eq gnus-auto-center-summary 'vertical)))
6782         (let ((selected (selected-window)))
6783           (select-window (get-buffer-window (current-buffer) t))
6784           (gnus-summary-position-point)
6785           (gnus-horizontal-recenter)
6786           (select-window selected))))))
6787
6788 (defun gnus-summary-jump-to-group (newsgroup)
6789   "Move point to NEWSGROUP in group mode buffer."
6790   ;; Keep update point of group mode buffer if visible.
6791   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6792       (save-window-excursion
6793         ;; Take care of tree window mode.
6794         (when (get-buffer-window gnus-group-buffer)
6795           (pop-to-buffer gnus-group-buffer))
6796         (gnus-group-jump-to-group newsgroup))
6797     (save-excursion
6798       ;; Take care of tree window mode.
6799       (if (get-buffer-window gnus-group-buffer 0)
6800           (pop-to-buffer gnus-group-buffer)
6801         (set-buffer gnus-group-buffer))
6802       (gnus-group-jump-to-group newsgroup))))
6803
6804 ;; This function returns a list of article numbers based on the
6805 ;; difference between the ranges of read articles in this group and
6806 ;; the range of active articles.
6807 (defun gnus-list-of-unread-articles (group)
6808   (let* ((read (gnus-info-read (gnus-get-info group)))
6809          (active (or (gnus-active group) (gnus-activate-group group)))
6810          (last (or (cdr active)
6811                    (error "Group %s couldn't be activated " group)))
6812          (bottom (if gnus-newsgroup-maximum-articles
6813                      (max (car active)
6814                           (- last gnus-newsgroup-maximum-articles -1))
6815                    (car active)))
6816          first nlast unread)
6817     ;; If none are read, then all are unread.
6818     (if (not read)
6819         (setq first bottom)
6820       ;; If the range of read articles is a single range, then the
6821       ;; first unread article is the article after the last read
6822       ;; article.  Sounds logical, doesn't it?
6823       (if (and (not (listp (cdr read)))
6824                (or (< (car read) bottom)
6825                    (progn (setq read (list read))
6826                           nil)))
6827           (setq first (max bottom (1+ (cdr read))))
6828         ;; `read' is a list of ranges.
6829         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6830                                   (caar read)))
6831                   1)
6832           (setq first bottom))
6833         (while read
6834           (when first
6835             (while (< first nlast)
6836               (setq unread (cons first unread)
6837                     first (1+ first))))
6838           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6839           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6840           (setq read (cdr read)))))
6841     ;; And add the last unread articles.
6842     (while (<= first last)
6843       (setq unread (cons first unread)
6844             first (1+ first)))
6845     ;; Return the list of unread articles.
6846     (delq 0 (nreverse unread))))
6847
6848 (defun gnus-list-of-read-articles (group)
6849   "Return a list of unread, unticked and non-dormant articles."
6850   (let* ((info (gnus-get-info group))
6851          (marked (gnus-info-marks info))
6852          (active (gnus-active group)))
6853     (and info active
6854          (gnus-list-range-difference
6855           (gnus-list-range-difference
6856            (gnus-sorted-complement
6857             (gnus-uncompress-range
6858              (if gnus-newsgroup-maximum-articles
6859                  (cons (max (car active)
6860                             (- (cdr active)
6861                                gnus-newsgroup-maximum-articles
6862                                -1))
6863                        (cdr active))
6864                active))
6865             (gnus-list-of-unread-articles group))
6866            (cdr (assq 'dormant marked)))
6867           (cdr (assq 'tick marked))))))
6868
6869 ;; This function returns a sequence of article numbers based on the
6870 ;; difference between the ranges of read articles in this group and
6871 ;; the range of active articles.
6872 (defun gnus-sequence-of-unread-articles (group)
6873   (let* ((read (gnus-info-read (gnus-get-info group)))
6874          (active (or (gnus-active group) (gnus-activate-group group)))
6875          (last (cdr active))
6876          (bottom (if gnus-newsgroup-maximum-articles
6877                      (max (car active)
6878                           (- last gnus-newsgroup-maximum-articles -1))
6879                    (car active)))
6880          first nlast unread)
6881     ;; If none are read, then all are unread.
6882     (if (not read)
6883         (setq first bottom)
6884       ;; If the range of read articles is a single range, then the
6885       ;; first unread article is the article after the last read
6886       ;; article.  Sounds logical, doesn't it?
6887       (if (and (not (listp (cdr read)))
6888                (or (< (car read) bottom)
6889                    (progn (setq read (list read))
6890                           nil)))
6891           (setq first (max bottom (1+ (cdr read))))
6892         ;; `read' is a list of ranges.
6893         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6894                                   (caar read)))
6895                   1)
6896           (setq first bottom))
6897         (while read
6898           (when first
6899             (push (cons first nlast) unread))
6900           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6901           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6902           (setq read (cdr read)))))
6903     ;; And add the last unread articles.
6904     (cond ((not (and first last))
6905            nil)
6906           ((< first last)
6907            (push (cons first last) unread))
6908           ((= first last)
6909            (push first unread)))
6910     ;; Return the sequence of unread articles.
6911     (delq 0 (nreverse unread))))
6912
6913 ;; Various summary commands
6914
6915 (defun gnus-summary-select-article-buffer ()
6916   "Reconfigure windows to show article buffer."
6917   (interactive)
6918   (if (not (gnus-buffer-live-p gnus-article-buffer))
6919       (error "There is no article buffer for this summary buffer")
6920     (gnus-configure-windows 'article)
6921     (select-window (get-buffer-window gnus-article-buffer))))
6922
6923 (defun gnus-summary-universal-argument (arg)
6924   "Perform any operation on all articles that are process/prefixed."
6925   (interactive "P")
6926   (let ((articles (gnus-summary-work-articles arg))
6927         func article)
6928     (if (eq
6929          (setq
6930           func
6931           (key-binding
6932            (read-key-sequence
6933             (substitute-command-keys
6934              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6935          'undefined)
6936         (gnus-error 1 "Undefined key")
6937       (save-excursion
6938         (while articles
6939           (gnus-summary-goto-subject (setq article (pop articles)))
6940           (let (gnus-newsgroup-processable)
6941             (command-execute func))
6942           (gnus-summary-remove-process-mark article)))))
6943   (gnus-summary-position-point))
6944
6945 (defun gnus-summary-toggle-truncation (&optional arg)
6946   "Toggle truncation of summary lines.
6947 With ARG, turn line truncation on if ARG is positive."
6948   (interactive "P")
6949   (setq truncate-lines
6950         (if (null arg) (not truncate-lines)
6951           (> (prefix-numeric-value arg) 0)))
6952   (redraw-display))
6953
6954 (defun gnus-summary-find-for-reselect ()
6955   "Return the number of an article to stay on across a reselect.
6956 The current article is considered, then following articles, then previous
6957 articles.  An article is sought which is not cancelled and isn't a temporary
6958 insertion from another group.  If there's no such then return a dummy 0."
6959   (let (found)
6960     (dolist (rev '(nil t))
6961       (unless found      ; don't demand the reverse list if we don't need it
6962         (let ((data (gnus-data-find-list
6963                      (gnus-summary-article-number) (gnus-data-list rev))))
6964           (while (and data (not found))
6965             (if (and (< 0 (gnus-data-number (car data)))
6966                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6967                 (setq found (gnus-data-number (car data))))
6968             (setq data (cdr data))))))
6969     (or found 0)))
6970
6971 (defun gnus-summary-reselect-current-group (&optional all rescan)
6972   "Exit and then reselect the current newsgroup.
6973 The prefix argument ALL means to select all articles."
6974   (interactive "P")
6975   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6976     (error "Ephemeral groups can't be reselected"))
6977   (let ((current-subject (gnus-summary-find-for-reselect))
6978         (group gnus-newsgroup-name))
6979     (setq gnus-newsgroup-begin nil)
6980     (gnus-summary-exit nil 'leave-hidden)
6981     ;; We have to adjust the point of group mode buffer because
6982     ;; point was moved to the next unread newsgroup by exiting.
6983     (gnus-summary-jump-to-group group)
6984     (when rescan
6985       (save-excursion
6986         (gnus-group-get-new-news-this-group 1)))
6987     (gnus-group-read-group all t)
6988     (gnus-summary-goto-subject current-subject nil t)))
6989
6990 (defun gnus-summary-rescan-group (&optional all)
6991   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6992   (interactive "P")
6993   (gnus-summary-reselect-current-group all t))
6994
6995 (defun gnus-summary-update-info (&optional non-destructive)
6996   (save-excursion
6997     (let ((group gnus-newsgroup-name))
6998       (when group
6999         (when gnus-newsgroup-kill-headers
7000           (setq gnus-newsgroup-killed
7001                 (gnus-compress-sequence
7002                  (gnus-sorted-union
7003                   (gnus-list-range-intersection
7004                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7005                   gnus-newsgroup-unreads)
7006                  t)))
7007         (unless (listp (cdr gnus-newsgroup-killed))
7008           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7009         (let ((headers gnus-newsgroup-headers))
7010           ;; Set the new ranges of read articles.
7011           (with-current-buffer gnus-group-buffer
7012             (gnus-undo-force-boundary))
7013           (gnus-update-read-articles
7014            group (gnus-sorted-union
7015                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7016           ;; Set the current article marks.
7017           (let ((gnus-newsgroup-scored
7018                  (if (and (not gnus-save-score)
7019                           (not non-destructive))
7020                      nil
7021                    gnus-newsgroup-scored)))
7022             (save-excursion
7023               (gnus-update-marks)))
7024           ;; Do the cross-ref thing.
7025           (when gnus-use-cross-reference
7026             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7027           ;; Do not switch windows but change the buffer to work.
7028           (set-buffer gnus-group-buffer)
7029           (unless (gnus-ephemeral-group-p group)
7030             (gnus-group-update-group group)))))))
7031
7032 (defun gnus-summary-save-newsrc (&optional force)
7033   "Save the current number of read/marked articles in the dribble buffer.
7034 The dribble buffer will then be saved.
7035 If FORCE (the prefix), also save the .newsrc file(s)."
7036   (interactive "P")
7037   (gnus-summary-update-info t)
7038   (if force
7039       (gnus-save-newsrc-file)
7040     (gnus-dribble-save)))
7041
7042 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7043
7044 (defun gnus-summary-exit (&optional temporary leave-hidden)
7045   "Exit reading current newsgroup, and then return to group selection mode.
7046 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7047   (interactive)
7048   (gnus-set-global-variables)
7049   (when (gnus-buffer-live-p gnus-article-buffer)
7050     (with-current-buffer gnus-article-buffer
7051       (mm-destroy-parts gnus-article-mime-handles)
7052       ;; Set it to nil for safety reason.
7053       (setq gnus-article-mime-handle-alist nil)
7054       (setq gnus-article-mime-handles nil)))
7055   (gnus-kill-save-kill-buffer)
7056   (gnus-async-halt-prefetch)
7057   (let* ((group gnus-newsgroup-name)
7058          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7059          (gnus-group-is-exiting-p t)
7060          (mode major-mode)
7061          (group-point nil)
7062          (buf (current-buffer)))
7063     (unless quit-config
7064       ;; Do adaptive scoring, and possibly save score files.
7065       (when gnus-newsgroup-adaptive
7066         (gnus-score-adaptive))
7067       (when gnus-use-scoring
7068         (gnus-score-save)))
7069     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7070     ;; If we have several article buffers, we kill them at exit.
7071     (unless gnus-single-article-buffer
7072       (when (gnus-buffer-live-p gnus-article-buffer)
7073         (with-current-buffer gnus-article-buffer
7074           ;; Don't kill sticky article buffers
7075           (unless (eq major-mode 'gnus-sticky-article-mode)
7076             (gnus-kill-buffer gnus-article-buffer)
7077             (setq gnus-article-current nil))))
7078       (gnus-kill-buffer gnus-original-article-buffer))
7079     (when gnus-use-cache
7080       (gnus-cache-possibly-remove-articles)
7081       (gnus-cache-save-buffers))
7082     (gnus-async-prefetch-remove-group group)
7083     (when gnus-suppress-duplicates
7084       (gnus-dup-enter-articles))
7085     (when gnus-use-trees
7086       (gnus-tree-close group))
7087     (when gnus-use-cache
7088       (gnus-cache-write-active))
7089     ;; Remove entries for this group.
7090     (nnmail-purge-split-history (gnus-group-real-name group))
7091     ;; Make all changes in this group permanent.
7092     (unless quit-config
7093       (gnus-run-hooks 'gnus-exit-group-hook)
7094       (gnus-summary-update-info))
7095     (gnus-close-group group)
7096     ;; Make sure where we were, and go to next newsgroup.
7097     (set-buffer gnus-group-buffer)
7098     (unless quit-config
7099       (gnus-group-jump-to-group group))
7100     (gnus-run-hooks 'gnus-summary-exit-hook)
7101     (unless (or quit-config
7102                 (not gnus-summary-next-group-on-exit)
7103                 ;; If this group has disappeared from the summary
7104                 ;; buffer, don't skip forwards.
7105                 (not (string= group (gnus-group-group-name))))
7106       (gnus-group-next-unread-group 1))
7107     (setq group-point (point))
7108     (if temporary
7109         nil                             ;Nothing to do.
7110       (set-buffer buf)
7111       (if (not gnus-kill-summary-on-exit)
7112           (progn
7113             (gnus-deaden-summary)
7114             (setq mode nil))
7115         ;; We set all buffer-local variables to nil.  It is unclear why
7116         ;; this is needed, but if we don't, buffer-local variables are
7117         ;; not garbage-collected, it seems.  This would the lead to en
7118         ;; ever-growing Emacs.
7119         (gnus-summary-clear-local-variables)
7120         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7121           (gnus-summary-clear-local-variables))
7122         (when (get-buffer gnus-article-buffer)
7123           (bury-buffer gnus-article-buffer))
7124         ;; Return to group mode buffer.
7125         (when (eq mode 'gnus-summary-mode)
7126           (gnus-kill-buffer buf)))
7127       (setq gnus-current-select-method gnus-select-method)
7128       (set-buffer gnus-group-buffer)
7129       (if quit-config
7130           (gnus-handle-ephemeral-exit quit-config)
7131         (goto-char group-point)
7132         ;; If gnus-group-buffer is already displayed, make sure we also move
7133         ;; the cursor in the window that displays it.
7134         (let ((win (get-buffer-window (current-buffer) 0)))
7135           (if win (set-window-point win (point))))
7136         (unless leave-hidden
7137           (gnus-configure-windows 'group 'force)))
7138       ;; Clear the current group name.
7139       (unless quit-config
7140         (setq gnus-newsgroup-name nil)))))
7141
7142 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7143 (defun gnus-summary-exit-no-update (&optional no-questions)
7144   "Quit reading current newsgroup without updating read article info."
7145   (interactive)
7146   (let* ((group gnus-newsgroup-name)
7147          (gnus-group-is-exiting-p t)
7148          (gnus-group-is-exiting-without-update-p t)
7149          (quit-config (gnus-group-quit-config group)))
7150     (when (or no-questions
7151               gnus-expert-user
7152               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7153       (gnus-async-halt-prefetch)
7154       (run-hooks 'gnus-summary-prepare-exit-hook)
7155       (when (gnus-buffer-live-p gnus-article-buffer)
7156         (with-current-buffer gnus-article-buffer
7157           (mm-destroy-parts gnus-article-mime-handles)
7158           ;; Set it to nil for safety reason.
7159           (setq gnus-article-mime-handle-alist nil)
7160           (setq gnus-article-mime-handles nil)))
7161       ;; If we have several article buffers, we kill them at exit.
7162       (unless gnus-single-article-buffer
7163         (gnus-kill-buffer gnus-article-buffer)
7164         (gnus-kill-buffer gnus-original-article-buffer)
7165         (setq gnus-article-current nil))
7166       (if (not gnus-kill-summary-on-exit)
7167           (gnus-deaden-summary)
7168         (gnus-close-group group)
7169         (gnus-summary-clear-local-variables)
7170         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7171           (gnus-summary-clear-local-variables))
7172         (gnus-kill-buffer gnus-summary-buffer))
7173       (unless gnus-single-article-buffer
7174         (setq gnus-article-current nil))
7175       (when gnus-use-trees
7176         (gnus-tree-close group))
7177       (gnus-async-prefetch-remove-group group)
7178       (when (get-buffer gnus-article-buffer)
7179         (bury-buffer gnus-article-buffer))
7180       ;; Return to the group buffer.
7181       (gnus-configure-windows 'group 'force)
7182       ;; Clear the current group name.
7183       (setq gnus-newsgroup-name nil)
7184       (unless (gnus-ephemeral-group-p group)
7185         (gnus-group-update-group group))
7186       (when (equal (gnus-group-group-name) group)
7187         (gnus-group-next-unread-group 1))
7188       (when quit-config
7189         (gnus-handle-ephemeral-exit quit-config)))))
7190
7191 (defun gnus-handle-ephemeral-exit (quit-config)
7192   "Handle movement when leaving an ephemeral group.
7193 The state which existed when entering the ephemeral is reset."
7194   (if (not (buffer-name (car quit-config)))
7195       (gnus-configure-windows 'group 'force)
7196     (set-buffer (car quit-config))
7197     (cond ((eq major-mode 'gnus-summary-mode)
7198            (gnus-set-global-variables))
7199           ((eq major-mode 'gnus-article-mode)
7200            (save-current-buffer
7201              ;; The `gnus-summary-buffer' variable may point
7202              ;; to the old summary buffer when using a single
7203              ;; article buffer.
7204              (unless (gnus-buffer-live-p gnus-summary-buffer)
7205                (set-buffer gnus-group-buffer))
7206              (set-buffer gnus-summary-buffer)
7207              (gnus-set-global-variables))))
7208     (if (or (eq (cdr quit-config) 'article)
7209             (eq (cdr quit-config) 'pick))
7210         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7211             (gnus-configure-windows 'pick 'force)
7212           (gnus-configure-windows (cdr quit-config) 'force))
7213       (gnus-configure-windows (cdr quit-config) 'force))
7214     (when (eq major-mode 'gnus-summary-mode)
7215       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7216                                                      next-unread-noselect))
7217           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7218                                   'next-noselect)
7219                               (gnus-summary-next-subject 1 nil t))
7220                              ((eq gnus-auto-select-on-ephemeral-exit
7221                                   'next-unread-noselect)
7222                               (gnus-summary-next-subject 1 t t))))
7223             ;; Hide the article buffer which displays the article different
7224             ;; from the one that the cursor points to in the summary buffer.
7225             (gnus-configure-windows 'summary 'force))
7226         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7227                (gnus-summary-next-subject 1))
7228               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7229                (gnus-summary-next-subject 1 t))))
7230       (gnus-summary-recenter)
7231       (gnus-summary-position-point))))
7232
7233 ;;; Dead summaries.
7234
7235 (defvar gnus-dead-summary-mode-map
7236   (let ((map (make-keymap)))
7237     (suppress-keymap map)
7238     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7239     (dolist (key '("\C-d" "\r" "\177" [delete]))
7240       (define-key map key 'gnus-summary-wake-up-the-dead))
7241     (dolist (key '("q" "Q"))
7242       (define-key map key 'bury-buffer))
7243     map))
7244
7245 (define-minor-mode gnus-dead-summary-mode
7246   "Minor mode for Gnus summary buffers."
7247   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7248   (unless (derived-mode-p 'gnus-summary-mode)
7249     (setq gnus-dead-summary-mode nil)))
7250
7251 (defun gnus-deaden-summary ()
7252   "Make the current summary buffer into a dead summary buffer."
7253   ;; Kill any previous dead summary buffer.
7254   (when (and gnus-dead-summary
7255              (buffer-name gnus-dead-summary))
7256     (with-current-buffer gnus-dead-summary
7257       (when gnus-dead-summary-mode
7258         (kill-buffer (current-buffer)))))
7259   ;; Make this the current dead summary.
7260   (setq gnus-dead-summary (current-buffer))
7261   (gnus-dead-summary-mode 1)
7262   (let ((name (buffer-name)))
7263     (when (string-match "Summary" name)
7264       (rename-buffer
7265        (concat (substring name 0 (match-beginning 0)) "Dead "
7266                (substring name (match-beginning 0)))
7267        t)
7268       (bury-buffer))))
7269
7270 (defun gnus-kill-or-deaden-summary (buffer)
7271   "Kill or deaden the summary BUFFER."
7272   (save-excursion
7273     (when (and (buffer-name buffer)
7274                (not gnus-single-article-buffer))
7275       (with-current-buffer buffer
7276         (gnus-kill-buffer gnus-article-buffer)
7277         (gnus-kill-buffer gnus-original-article-buffer)))
7278     (cond
7279      ;; Kill the buffer.
7280      (gnus-kill-summary-on-exit
7281       (when (and gnus-use-trees
7282                  (gnus-buffer-exists-p buffer))
7283         (with-current-buffer buffer
7284           (gnus-tree-close gnus-newsgroup-name)))
7285       (gnus-kill-buffer buffer))
7286      ;; Deaden the buffer.
7287      ((gnus-buffer-exists-p buffer)
7288       (with-current-buffer buffer
7289         (gnus-deaden-summary))))))
7290
7291 (defun gnus-summary-wake-up-the-dead (&rest args)
7292   "Wake up the dead summary buffer."
7293   (interactive)
7294   (gnus-dead-summary-mode -1)
7295   (let ((name (buffer-name)))
7296     (when (string-match "Dead " name)
7297       (rename-buffer
7298        (concat (substring name 0 (match-beginning 0))
7299                (substring name (match-end 0)))
7300        t)))
7301   (gnus-message 3 "This dead summary is now alive again"))
7302
7303 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7304 (defun gnus-summary-fetch-faq (&optional faq-dir)
7305   "Fetch the FAQ for the current group.
7306 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7307 in."
7308   (interactive
7309    (list
7310     (when current-prefix-arg
7311       (completing-read
7312        "FAQ dir: " (and (listp gnus-group-faq-directory)
7313                         (mapcar 'list
7314                                 gnus-group-faq-directory))))))
7315   (let (gnus-faq-buffer)
7316     (when (setq gnus-faq-buffer
7317                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7318       (gnus-configure-windows 'summary-faq))))
7319
7320 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7321 (defun gnus-summary-describe-group (&optional force)
7322   "Describe the current newsgroup."
7323   (interactive "P")
7324   (gnus-group-describe-group force gnus-newsgroup-name))
7325
7326 (defun gnus-summary-describe-briefly ()
7327   "Describe summary mode commands briefly."
7328   (interactive)
7329   (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")))
7330
7331 ;; Walking around group mode buffer from summary mode.
7332
7333 (defun gnus-summary-next-group (&optional no-article target-group backward)
7334   "Exit current newsgroup and then select next unread newsgroup.
7335 If prefix argument NO-ARTICLE is non-nil, no article is selected
7336 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7337 previous group instead."
7338   (interactive "P")
7339   ;; Stop pre-fetching.
7340   (gnus-async-halt-prefetch)
7341   (let ((current-group gnus-newsgroup-name)
7342         (current-buffer (current-buffer))
7343         entered)
7344     ;; First we semi-exit this group to update Xrefs and all variables.
7345     ;; We can't do a real exit, because the window conf must remain
7346     ;; the same in case the user is prompted for info, and we don't
7347     ;; want the window conf to change before that...
7348     (gnus-summary-exit t)
7349     (while (not entered)
7350       ;; Then we find what group we are supposed to enter.
7351       (set-buffer gnus-group-buffer)
7352       (gnus-group-jump-to-group current-group)
7353       (setq target-group
7354             (or target-group
7355                 (if (eq gnus-keep-same-level 'best)
7356                     (gnus-summary-best-group gnus-newsgroup-name)
7357                   (gnus-summary-search-group backward gnus-keep-same-level))))
7358       (if (not target-group)
7359           ;; There are no further groups, so we return to the group
7360           ;; buffer.
7361           (progn
7362             (gnus-message 5 "Returning to the group buffer")
7363             (setq entered t)
7364             (when (gnus-buffer-live-p current-buffer)
7365               (set-buffer current-buffer)
7366               (gnus-summary-exit))
7367             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7368         ;; We try to enter the target group.
7369         (gnus-group-jump-to-group target-group)
7370         (let ((unreads (gnus-group-group-unread)))
7371           (if (and (or (eq t unreads)
7372                        (and unreads (not (zerop unreads))))
7373                    (gnus-summary-read-group
7374                     target-group nil no-article
7375                     (and (buffer-name current-buffer) current-buffer)
7376                     nil backward))
7377               (setq entered t)
7378             (setq current-group target-group
7379                   target-group nil)))))))
7380
7381 (defun gnus-summary-prev-group (&optional no-article)
7382   "Exit current newsgroup and then select previous unread newsgroup.
7383 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7384   (interactive "P")
7385   (gnus-summary-next-group no-article nil t))
7386
7387 ;; Walking around summary lines.
7388
7389 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7390   "Go to the first subject satisfying any non-nil constraint.
7391 If UNREAD is non-nil, the article should be unread.
7392 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7393 If UNSEEN is non-nil, the article should be unseen.
7394 Returns the article selected or nil if there are no matching articles."
7395   (interactive "P")
7396   (cond
7397    ;; Empty summary.
7398    ((null gnus-newsgroup-data)
7399     (gnus-message 3 "No articles in the group")
7400     nil)
7401    ;; Pick the first article.
7402    ((not (or unread undownloaded unseen))
7403     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7404     (gnus-data-number (car gnus-newsgroup-data)))
7405    ;; Find the first unread article.
7406    (t
7407     (let ((data gnus-newsgroup-data))
7408       (while (and data
7409                   (let ((num (gnus-data-number (car data))))
7410                     (or (memq num gnus-newsgroup-unfetched)
7411                         (not (or (and unread
7412                                       (memq num gnus-newsgroup-unreads))
7413                                  (and undownloaded
7414                                       (memq num gnus-newsgroup-undownloaded))
7415                                  (and unseen
7416                                       (memq num gnus-newsgroup-unseen)))))))
7417         (setq data (cdr data)))
7418       (prog1
7419           (if data
7420               (progn
7421                 (goto-char (gnus-data-pos (car data)))
7422                 (gnus-data-number (car data)))
7423             (gnus-message 3 "No more%s articles"
7424                           (let* ((r (when unread " unread"))
7425                                  (d (when undownloaded " undownloaded"))
7426                                  (s (when unseen " unseen"))
7427                                  (l (delq nil (list r d s))))
7428                             (cond ((= 3 (length l))
7429                                    (concat r "," d ", or" s))
7430                                   ((= 2 (length l))
7431                                    (concat (car l) ", or" (cadr l)))
7432                                   ((= 1 (length l))
7433                                    (car l))
7434                                   (t
7435                                    ""))))
7436             nil
7437             )
7438         (gnus-summary-position-point))))))
7439
7440 (defun gnus-summary-next-subject (n &optional unread dont-display)
7441   "Go to next N'th summary line.
7442 If N is negative, go to the previous N'th subject line.
7443 If UNREAD is non-nil, only unread articles are selected.
7444 The difference between N and the actual number of steps taken is
7445 returned."
7446   (interactive "p")
7447   (let ((backward (< n 0))
7448         (n (abs n)))
7449     (while (and (> n 0)
7450                 (if backward
7451                     (gnus-summary-find-prev unread)
7452                   (gnus-summary-find-next unread)))
7453       (unless (zerop (setq n (1- n)))
7454         (gnus-summary-show-thread)))
7455     (when (/= 0 n)
7456       (gnus-message 7 "No more%s articles"
7457                     (if unread " unread" "")))
7458     (unless dont-display
7459       (gnus-summary-recenter)
7460       (gnus-summary-position-point))
7461     n))
7462
7463 (defun gnus-summary-next-unread-subject (n)
7464   "Go to next N'th unread summary line."
7465   (interactive "p")
7466   (gnus-summary-next-subject n t))
7467
7468 (defun gnus-summary-prev-subject (n &optional unread)
7469   "Go to previous N'th summary line.
7470 If optional argument UNREAD is non-nil, only unread article is selected."
7471   (interactive "p")
7472   (gnus-summary-next-subject (- n) unread))
7473
7474 (defun gnus-summary-prev-unread-subject (n)
7475   "Go to previous N'th unread summary line."
7476   (interactive "p")
7477   (gnus-summary-next-subject (- n) t))
7478
7479 (defun gnus-summary-goto-subjects (articles)
7480   "Insert the subject header for ARTICLES in the current buffer."
7481   (save-excursion
7482     (dolist (article articles)
7483       (gnus-summary-goto-subject article t)))
7484   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7485   (gnus-summary-position-point))
7486
7487 (defun gnus-summary-goto-subject (article &optional force silent)
7488   "Go to the subject line of ARTICLE.
7489 If FORCE, also allow jumping to articles not currently shown."
7490   (interactive "nArticle number: ")
7491   (unless (numberp article)
7492     (error "Article %s is not a number" article))
7493   (let ((b (point))
7494         (data (gnus-data-find article)))
7495     ;; We read in the article if we have to.
7496     (and (not data)
7497          force
7498          (gnus-summary-insert-subject
7499           article
7500           (if (or (numberp force) (vectorp force)) force)
7501           t)
7502          (setq data (gnus-data-find article)))
7503     (goto-char b)
7504     (if (not data)
7505         (progn
7506           (unless silent
7507             (gnus-message 3 "Can't find article %d" article))
7508           nil)
7509       (let ((pt (gnus-data-pos data)))
7510         (goto-char pt)
7511         (gnus-summary-set-article-display-arrow pt))
7512       (gnus-summary-position-point)
7513       article)))
7514
7515 ;; Walking around summary lines with displaying articles.
7516
7517 (defun gnus-summary-expand-window (&optional arg)
7518   "Make the summary buffer take up the entire Emacs frame.
7519 Given a prefix, will force an `article' buffer configuration."
7520   (interactive "P")
7521   (if arg
7522       (gnus-configure-windows 'article 'force)
7523     (gnus-configure-windows 'summary 'force)))
7524
7525 (defun gnus-summary-display-article (article &optional all-header)
7526   "Display ARTICLE in article buffer."
7527   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7528                (with-current-buffer gnus-article-buffer
7529                  (eq major-mode 'gnus-article-mode)))
7530     (gnus-article-setup-buffer))
7531   (gnus-set-global-variables)
7532   (with-current-buffer gnus-article-buffer
7533     (setq gnus-article-charset gnus-newsgroup-charset)
7534     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7535     (mm-enable-multibyte))
7536   (if (null article)
7537       nil
7538     (prog1
7539         (if gnus-summary-display-article-function
7540             (funcall gnus-summary-display-article-function article all-header)
7541           (gnus-article-prepare article all-header))
7542       (gnus-run-hooks 'gnus-select-article-hook)
7543       (when (and gnus-current-article
7544                  (not (zerop gnus-current-article)))
7545         (gnus-summary-goto-subject gnus-current-article))
7546       (gnus-summary-recenter)
7547       (when (and gnus-use-trees gnus-show-threads)
7548         (gnus-possibly-generate-tree article)
7549         (gnus-highlight-selected-tree article))
7550       ;; Successfully display article.
7551       (gnus-article-set-window-start
7552        (cdr (assq article gnus-newsgroup-bookmarks))))))
7553
7554 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7555   "Select the current article.
7556 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7557 non-nil, the article will be re-fetched even if it already present in
7558 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7559 be displayed."
7560   ;; Make sure we are in the summary buffer to work around bbdb bug.
7561   (unless (eq major-mode 'gnus-summary-mode)
7562     (set-buffer gnus-summary-buffer))
7563   (let ((article (or article (gnus-summary-article-number)))
7564         (all-headers (not (not all-headers))) ;Must be t or nil.
7565         gnus-summary-display-article-function)
7566     (and (not pseudo)
7567          (gnus-summary-article-pseudo-p article)
7568          (error "This is a pseudo-article"))
7569     (with-current-buffer gnus-summary-buffer
7570       (if (or (and gnus-single-article-buffer
7571                    (or (null gnus-current-article)
7572                        (null gnus-article-current)
7573                        (null (get-buffer gnus-article-buffer))
7574                        (not (eq article (cdr gnus-article-current)))
7575                        (not (equal (car gnus-article-current)
7576                                    gnus-newsgroup-name))))
7577               (and (not gnus-single-article-buffer)
7578                    (or (null gnus-current-article)
7579                        (not (eq gnus-current-article article))))
7580               force)
7581           ;; The requested article is different from the current article.
7582           (progn
7583             (gnus-summary-display-article article all-headers)
7584             (when (gnus-buffer-live-p gnus-article-buffer)
7585               (with-current-buffer gnus-article-buffer
7586                 (if (not gnus-article-decoded-p) ;; a local variable
7587                     (mm-disable-multibyte))))
7588             (gnus-article-set-window-start
7589              (cdr (assq article gnus-newsgroup-bookmarks)))
7590             article)
7591         'old))))
7592
7593 (defun gnus-summary-force-verify-and-decrypt ()
7594   "Display buttons for signed/encrypted parts and verify/decrypt them."
7595   (interactive)
7596   (let ((mm-verify-option 'known)
7597         (mm-decrypt-option 'known)
7598         (gnus-article-emulate-mime t)
7599         (gnus-buttonized-mime-types (append (list "multipart/signed"
7600                                                   "multipart/encrypted")
7601                                             gnus-buttonized-mime-types)))
7602     (gnus-summary-select-article nil 'force)))
7603
7604 (defun gnus-summary-set-current-mark (&optional current-mark)
7605   "Obsolete function."
7606   nil)
7607
7608 (defun gnus-summary-next-article (&optional unread subject backward push)
7609   "Select the next article.
7610 If UNREAD, only unread articles are selected.
7611 If SUBJECT, only articles with SUBJECT are selected.
7612 If BACKWARD, the previous article is selected instead of the next."
7613   (interactive "P")
7614   ;; Make sure we are in the summary buffer.
7615   (unless (eq major-mode 'gnus-summary-mode)
7616     (set-buffer gnus-summary-buffer))
7617   (cond
7618    ;; Is there such an article?
7619    ((and (gnus-summary-search-forward unread subject backward)
7620          (or (gnus-summary-display-article (gnus-summary-article-number))
7621              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7622     (gnus-summary-position-point))
7623    ;; If not, we try the first unread, if that is wanted.
7624    ((and subject
7625          gnus-auto-select-same
7626          (gnus-summary-first-unread-article))
7627     (gnus-summary-position-point)
7628     (gnus-message 6 "Wrapped"))
7629    ;; Try to get next/previous article not displayed in this group.
7630    ((and gnus-auto-extend-newsgroup
7631          (not unread) (not subject))
7632     (gnus-summary-goto-article
7633      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7634      nil (count-lines (point-min) (point))))
7635    ;; Go to next/previous group.
7636    (t
7637     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7638       (gnus-summary-jump-to-group gnus-newsgroup-name))
7639     (let ((cmd (if (featurep 'xemacs)
7640                    last-command-char
7641                  last-command-event))
7642           (point
7643            (with-current-buffer gnus-group-buffer
7644              (point)))
7645           (group
7646            (if (eq gnus-keep-same-level 'best)
7647                (gnus-summary-best-group gnus-newsgroup-name)
7648              (gnus-summary-search-group backward gnus-keep-same-level))))
7649       ;; For some reason, the group window gets selected.  We change
7650       ;; it back.
7651       (select-window (get-buffer-window (current-buffer)))
7652       ;; Select next unread newsgroup automagically.
7653       (cond
7654        ((or (not gnus-auto-select-next)
7655             (not cmd))
7656         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7657        ((or (eq gnus-auto-select-next 'quietly)
7658             (and (eq gnus-auto-select-next 'slightly-quietly)
7659                  push)
7660             (and (eq gnus-auto-select-next 'almost-quietly)
7661                  (gnus-summary-last-article-p)))
7662         ;; Select quietly.
7663         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7664             (gnus-summary-exit)
7665           (gnus-message 7 "No more%s articles (%s)..."
7666                         (if unread " unread" "")
7667                         (if group (concat "selecting " group)
7668                           "exiting"))
7669           (gnus-summary-next-group nil group backward)))
7670        (t
7671         (when (gnus-key-press-event-p last-input-event)
7672           (gnus-summary-walk-group-buffer
7673            gnus-newsgroup-name cmd unread backward point))))))))
7674
7675 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7676   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7677                       (?\C-p (gnus-group-prev-unread-group 1))))
7678         (cursor-in-echo-area t)
7679         keve key group ended prompt)
7680     (with-current-buffer gnus-group-buffer
7681       (goto-char start)
7682       (setq group
7683             (if (eq gnus-keep-same-level 'best)
7684                 (gnus-summary-best-group gnus-newsgroup-name)
7685               (gnus-summary-search-group backward gnus-keep-same-level))))
7686     (while (not ended)
7687       (setq prompt
7688             (format
7689              "No more%s articles%s " (if unread " unread" "")
7690              (if (and group
7691                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7692                  (format " (Type %s for %s [%s])"
7693                          (single-key-description cmd)
7694                          (gnus-group-decoded-name group)
7695                          (gnus-group-unread group))
7696                (format " (Type %s to exit %s)"
7697                        (single-key-description cmd)
7698                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7699       ;; Confirm auto selection.
7700       (setq key (car (setq keve (gnus-read-event-char prompt)))
7701             ended t)
7702       (cond
7703        ((assq key keystrokes)
7704         (let ((obuf (current-buffer)))
7705           (switch-to-buffer gnus-group-buffer)
7706           (when group
7707             (gnus-group-jump-to-group group))
7708           (eval (cadr (assq key keystrokes)))
7709           (setq group (gnus-group-group-name))
7710           (switch-to-buffer obuf))
7711         (setq ended nil))
7712        ((equal key cmd)
7713         (if (or (not group)
7714                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7715             (gnus-summary-exit)
7716           (gnus-summary-next-group nil group backward)))
7717        (t
7718         (push (cdr keve) unread-command-events))))))
7719
7720 (defun gnus-summary-next-unread-article ()
7721   "Select unread article after current one."
7722   (interactive)
7723   (gnus-summary-next-article
7724    (or (not (eq gnus-summary-goto-unread 'never))
7725        (gnus-summary-last-article-p (gnus-summary-article-number)))
7726    (and gnus-auto-select-same
7727         (gnus-summary-article-subject))))
7728
7729 (defun gnus-summary-prev-article (&optional unread subject)
7730   "Select the article before the current one.
7731 If UNREAD is non-nil, only unread articles are selected."
7732   (interactive "P")
7733   (gnus-summary-next-article unread subject t))
7734
7735 (defun gnus-summary-prev-unread-article ()
7736   "Select unread article before current one."
7737   (interactive)
7738   (gnus-summary-prev-article
7739    (or (not (eq gnus-summary-goto-unread 'never))
7740        (gnus-summary-first-article-p (gnus-summary-article-number)))
7741    (and gnus-auto-select-same
7742         (gnus-summary-article-subject))))
7743
7744 (defun gnus-summary-next-page (&optional lines circular stop)
7745   "Show next page of the selected article.
7746 If at the end of the current article, select the next article.
7747 LINES says how many lines should be scrolled up.
7748
7749 If CIRCULAR is non-nil, go to the start of the article instead of
7750 selecting the next article when reaching the end of the current
7751 article.
7752
7753 If STOP is non-nil, just stop when reaching the end of the message.
7754
7755 Also see the variable `gnus-article-skip-boring'."
7756   (interactive "P")
7757   (setq gnus-summary-buffer (current-buffer))
7758   (gnus-set-global-variables)
7759   (let ((article (gnus-summary-article-number))
7760         (article-window (get-buffer-window gnus-article-buffer t))
7761         endp)
7762     ;; If the buffer is empty, we have no article.
7763     (unless article
7764       (error "No article to select"))
7765     (gnus-configure-windows 'article)
7766     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7767         (if (and (eq gnus-summary-goto-unread 'never)
7768                  (not (gnus-summary-last-article-p article)))
7769             (gnus-summary-next-article)
7770           (gnus-summary-next-unread-article))
7771       (if (or (null gnus-current-article)
7772               (null gnus-article-current)
7773               (/= article (cdr gnus-article-current))
7774               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7775           ;; Selected subject is different from current article's.
7776           (gnus-summary-display-article article)
7777         (when article-window
7778           (gnus-eval-in-buffer-window gnus-article-buffer
7779             (setq endp (or (gnus-article-next-page lines)
7780                            (gnus-article-only-boring-p))))
7781           (when endp
7782             (cond ((or stop gnus-summary-stop-at-end-of-message)
7783                    (gnus-message 3 "End of message"))
7784                   (circular
7785                    (gnus-summary-beginning-of-article))
7786                   (lines
7787                    (gnus-message 3 "End of message"))
7788                   ((null lines)
7789                    (if (and (eq gnus-summary-goto-unread 'never)
7790                             (not (gnus-summary-last-article-p article)))
7791                        (gnus-summary-next-article)
7792                      (gnus-summary-next-unread-article))))))))
7793     (gnus-summary-recenter)
7794     (gnus-summary-position-point)))
7795
7796 (defun gnus-summary-prev-page (&optional lines move)
7797   "Show previous page of selected article.
7798 Argument LINES specifies lines to be scrolled down.
7799 If MOVE, move to the previous unread article if point is at
7800 the beginning of the buffer."
7801   (interactive "P")
7802   (let ((article (gnus-summary-article-number))
7803         (article-window (get-buffer-window gnus-article-buffer t))
7804         endp)
7805     (gnus-configure-windows 'article)
7806     (if (or (null gnus-current-article)
7807             (null gnus-article-current)
7808             (/= article (cdr gnus-article-current))
7809             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7810         ;; Selected subject is different from current article's.
7811         (gnus-summary-display-article article)
7812       (gnus-summary-recenter)
7813       (when article-window
7814         (gnus-eval-in-buffer-window gnus-article-buffer
7815           (setq endp (gnus-article-prev-page lines)))
7816         (when (and move endp)
7817           (cond (lines
7818                  (gnus-message 3 "Beginning of message"))
7819                 ((null lines)
7820                  (if (and (eq gnus-summary-goto-unread 'never)
7821                           (not (gnus-summary-first-article-p article)))
7822                      (gnus-summary-prev-article)
7823                    (gnus-summary-prev-unread-article))))))))
7824   (gnus-summary-position-point))
7825
7826 (defun gnus-summary-prev-page-or-article (&optional lines)
7827   "Show previous page of selected article.
7828 Argument LINES specifies lines to be scrolled down.
7829 If at the beginning of the article, go to the next article."
7830   (interactive "P")
7831   (gnus-summary-prev-page lines t))
7832
7833 (defun gnus-summary-scroll-up (lines)
7834   "Scroll up (or down) one line current article.
7835 Argument LINES specifies lines to be scrolled up (or down if negative)."
7836   (interactive "p")
7837   (gnus-configure-windows 'article)
7838   (gnus-summary-show-thread)
7839   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7840     (gnus-eval-in-buffer-window gnus-article-buffer
7841       (cond ((> lines 0)
7842              (when (gnus-article-next-page lines)
7843                (gnus-message 3 "End of message")))
7844             ((< lines 0)
7845              (gnus-article-prev-page (- lines))))))
7846   (gnus-summary-recenter)
7847   (gnus-summary-position-point))
7848
7849 (defun gnus-summary-scroll-down (lines)
7850   "Scroll down (or up) one line current article.
7851 Argument LINES specifies lines to be scrolled down (or up if negative)."
7852   (interactive "p")
7853   (gnus-summary-scroll-up (- lines)))
7854
7855 (defun gnus-summary-next-same-subject ()
7856   "Select next article which has the same subject as current one."
7857   (interactive)
7858   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7859
7860 (defun gnus-summary-prev-same-subject ()
7861   "Select previous article which has the same subject as current one."
7862   (interactive)
7863   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7864
7865 (defun gnus-summary-next-unread-same-subject ()
7866   "Select next unread article which has the same subject as current one."
7867   (interactive)
7868   (gnus-summary-next-article t (gnus-summary-article-subject)))
7869
7870 (defun gnus-summary-prev-unread-same-subject ()
7871   "Select previous unread article which has the same subject as current one."
7872   (interactive)
7873   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7874
7875 (defun gnus-summary-first-unread-article ()
7876   "Select the first unread article.
7877 Return nil if there are no unread articles."
7878   (interactive)
7879   (prog1
7880       (when (gnus-summary-first-subject t)
7881         (gnus-summary-show-thread)
7882         (gnus-summary-first-subject t)
7883         (gnus-summary-display-article (gnus-summary-article-number)))
7884     (gnus-summary-position-point)))
7885
7886 (defun gnus-summary-first-unread-subject ()
7887   "Place the point on the subject line of the first unread article.
7888 Return nil if there are no unread articles."
7889   (interactive)
7890   (prog1
7891       (when (gnus-summary-first-subject t)
7892         (gnus-summary-show-thread)
7893         (gnus-summary-first-subject t))
7894     (gnus-summary-position-point)))
7895
7896 (defun gnus-summary-first-unseen-subject ()
7897   "Place the point on the subject line of the first unseen article.
7898 Return nil if there are no unseen articles."
7899   (interactive)
7900   (prog1
7901       (when (gnus-summary-first-subject nil nil t)
7902         (gnus-summary-show-thread)
7903         (gnus-summary-first-subject nil nil t))
7904     (gnus-summary-position-point)))
7905
7906 (defun gnus-summary-first-unseen-or-unread-subject ()
7907   "Place the point on the subject line of the first unseen article or,
7908 if all article have been seen, on the subject line of the first unread
7909 article."
7910   (interactive)
7911   (prog1
7912       (unless (when (gnus-summary-first-subject nil nil t)
7913                 (gnus-summary-show-thread)
7914                 (gnus-summary-first-subject nil nil t))
7915         (when (gnus-summary-first-subject t)
7916           (gnus-summary-show-thread)
7917           (gnus-summary-first-subject t)))
7918     (gnus-summary-position-point)))
7919
7920 (defun gnus-summary-first-article ()
7921   "Select the first article.
7922 Return nil if there are no articles."
7923   (interactive)
7924   (prog1
7925       (when (gnus-summary-first-subject)
7926         (gnus-summary-show-thread)
7927         (gnus-summary-first-subject)
7928         (gnus-summary-display-article (gnus-summary-article-number)))
7929     (gnus-summary-position-point)))
7930
7931 (defun gnus-summary-best-unread-article (&optional arg)
7932   "Select the unread article with the highest score.
7933 If given a prefix argument, select the next unread article that has a
7934 score higher than the default score."
7935   (interactive "P")
7936   (let ((article (if arg
7937                      (gnus-summary-better-unread-subject)
7938                    (gnus-summary-best-unread-subject))))
7939     (if article
7940         (gnus-summary-goto-article article)
7941       (error "No unread articles"))))
7942
7943 (defun gnus-summary-best-unread-subject ()
7944   "Select the unread subject with the highest score."
7945   (interactive)
7946   (let ((best -1000000)
7947         (data gnus-newsgroup-data)
7948         article score)
7949     (while data
7950       (and (gnus-data-unread-p (car data))
7951            (> (setq score
7952                     (gnus-summary-article-score (gnus-data-number (car data))))
7953               best)
7954            (setq best score
7955                  article (gnus-data-number (car data))))
7956       (setq data (cdr data)))
7957     (when article
7958       (gnus-summary-goto-subject article))
7959     (gnus-summary-position-point)
7960     article))
7961
7962 (defun gnus-summary-better-unread-subject ()
7963   "Select the first unread subject that has a score over the default score."
7964   (interactive)
7965   (let ((data gnus-newsgroup-data)
7966         article score)
7967     (while (and (setq article (gnus-data-number (car data)))
7968                 (or (gnus-data-read-p (car data))
7969                     (not (> (gnus-summary-article-score article)
7970                             gnus-summary-default-score))))
7971       (setq data (cdr data)))
7972     (when article
7973       (gnus-summary-goto-subject article))
7974     (gnus-summary-position-point)
7975     article))
7976
7977 (defun gnus-summary-last-subject ()
7978   "Go to the last displayed subject line in the group."
7979   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7980     (when article
7981       (gnus-summary-goto-subject article))))
7982
7983 (defun gnus-summary-goto-article (article &optional all-headers force)
7984   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7985 If ALL-HEADERS is non-nil, no header lines are hidden.
7986 If FORCE, go to the article even if it isn't displayed.  If FORCE
7987 is a number, it is the line the article is to be displayed on."
7988   (interactive
7989    (list
7990     (completing-read
7991      "Article number or Message-ID: "
7992      (mapcar (lambda (number) (list (int-to-string number)))
7993              gnus-newsgroup-limit))
7994     current-prefix-arg
7995     t))
7996   (prog1
7997       (if (and (stringp article)
7998                (string-match "@\\|%40" article))
7999           (gnus-summary-refer-article article)
8000         (when (stringp article)
8001           (setq article (string-to-number article)))
8002         (if (gnus-summary-goto-subject article force)
8003             (gnus-summary-display-article article all-headers)
8004           (gnus-message 4 "Couldn't go to article %s" article) nil))
8005     (gnus-summary-position-point)))
8006
8007 (defun gnus-summary-goto-last-article ()
8008   "Go to the previously read article."
8009   (interactive)
8010   (prog1
8011       (when gnus-last-article
8012         (gnus-summary-goto-article gnus-last-article nil t))
8013     (gnus-summary-position-point)))
8014
8015 (defun gnus-summary-pop-article (number)
8016   "Pop one article off the history and go to the previous.
8017 NUMBER articles will be popped off."
8018   (interactive "p")
8019   (let (to)
8020     (setq gnus-newsgroup-history
8021           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8022     (if to
8023         (gnus-summary-goto-article (car to) nil t)
8024       (error "Article history empty")))
8025   (gnus-summary-position-point))
8026
8027 ;; Summary commands and functions for limiting the summary buffer.
8028
8029 (defun gnus-summary-limit-to-articles (n)
8030   "Limit the summary buffer to the next N articles.
8031 If not given a prefix, use the process marked articles instead."
8032   (interactive "P")
8033   (prog1
8034       (let ((articles (gnus-summary-work-articles n)))
8035         (setq gnus-newsgroup-processable nil)
8036         (gnus-summary-limit articles))
8037     (gnus-summary-position-point)))
8038
8039 (defun gnus-summary-pop-limit (&optional total)
8040   "Restore the previous limit.
8041 If given a prefix, remove all limits."
8042   (interactive "P")
8043   (when total
8044     (setq gnus-newsgroup-limits
8045           (list (mapcar (lambda (h) (mail-header-number h))
8046                         gnus-newsgroup-headers))))
8047   (unless gnus-newsgroup-limits
8048     (error "No limit to pop"))
8049   (prog1
8050       (gnus-summary-limit nil 'pop)
8051     (gnus-summary-position-point)))
8052
8053 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8054   "Limit the summary buffer to articles that have subjects that match a regexp.
8055 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8056   (interactive
8057    (list (read-string (if current-prefix-arg
8058                           "Exclude subject (regexp): "
8059                         "Limit to subject (regexp): "))
8060          nil current-prefix-arg))
8061   (unless header
8062     (setq header "subject"))
8063   (when (not (equal "" subject))
8064     (prog1
8065         (let ((articles (gnus-summary-find-matching
8066                          (or header "subject") subject 'all nil nil
8067                          not-matching)))
8068           (unless articles
8069             (error "Found no matches for \"%s\"" subject))
8070           (gnus-summary-limit articles))
8071       (gnus-summary-position-point))))
8072
8073 (defun gnus-summary-limit-to-author (from &optional not-matching)
8074   "Limit the summary buffer to articles that have authors that match a regexp.
8075 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8076   (interactive
8077    (list (read-string (if current-prefix-arg
8078                           "Exclude author (regexp): "
8079                         "Limit to author (regexp): "))
8080          current-prefix-arg))
8081   (gnus-summary-limit-to-subject from "from" not-matching))
8082
8083 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8084   "Limit the summary buffer to articles with the given RECIPIENT.
8085
8086 If NOT-MATCHING, exclude RECIPIENT.
8087
8088 To and Cc headers are checked.  You need to include them in
8089 `nnmail-extra-headers'."
8090   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8091   (interactive
8092    (list (read-string (format "%s recipient (regexp): "
8093                               (if current-prefix-arg "Exclude" "Limit to")))
8094          current-prefix-arg))
8095   (when (not (equal "" recipient))
8096     (prog1 (let* ((to
8097                    (if (memq 'To nnmail-extra-headers)
8098                        (gnus-summary-find-matching
8099                         (cons 'extra 'To) recipient 'all nil nil
8100                         not-matching)
8101                      (gnus-message
8102                       1 "`To' isn't present in `nnmail-extra-headers'")
8103                      (sit-for 1)
8104                      nil))
8105                   (cc
8106                    (if (memq 'Cc nnmail-extra-headers)
8107                        (gnus-summary-find-matching
8108                         (cons 'extra 'Cc) recipient 'all nil nil
8109                         not-matching)
8110                      (gnus-message
8111                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8112                      (sit-for 1)
8113                      nil))
8114                   (articles
8115                    (if not-matching
8116                        ;; We need the numbers that are in both lists:
8117                        (mapcar (lambda (a)
8118                                  (and (memq a to) a))
8119                                cc)
8120                      (nconc to cc))))
8121              (unless articles
8122                (error "Found no matches for \"%s\"" recipient))
8123              (gnus-summary-limit articles))
8124       (gnus-summary-position-point))))
8125
8126 (defun gnus-summary-limit-to-address (address &optional not-matching)
8127   "Limit the summary buffer to articles with the given ADDRESS.
8128
8129 If NOT-MATCHING, exclude ADDRESS.
8130
8131 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8132 in `nnmail-extra-headers'."
8133   (interactive
8134    (list (read-string (format "%s address (regexp): "
8135                               (if current-prefix-arg "Exclude" "Limit to")))
8136          current-prefix-arg))
8137   (when (not (equal "" address))
8138     (prog1 (let* ((to
8139                    (if (memq 'To nnmail-extra-headers)
8140                        (gnus-summary-find-matching
8141                         (cons 'extra 'To) address 'all nil nil
8142                         not-matching)
8143                      (gnus-message
8144                       1 "`To' isn't present in `nnmail-extra-headers'")
8145                      (sit-for 1)
8146                      t))
8147                   (cc
8148                    (if (memq 'Cc nnmail-extra-headers)
8149                        (gnus-summary-find-matching
8150                         (cons 'extra 'Cc) address 'all nil nil
8151                         not-matching)
8152                      (gnus-message
8153                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8154                      (sit-for 1)
8155                      t))
8156                   (from
8157                    (gnus-summary-find-matching "from" address
8158                                                'all nil nil not-matching))
8159                   (articles
8160                    (if not-matching
8161                        ;; We need the numbers that are in all lists:
8162                        (if (eq cc t)
8163                            (if (eq to t)
8164                                from
8165                              (mapcar (lambda (a) (car (memq a from))) to))
8166                          (if (eq to t)
8167                              (mapcar (lambda (a) (car (memq a from))) cc)
8168                            (mapcar (lambda (a) (car (memq a from)))
8169                                    (mapcar (lambda (a) (car (memq a to)))
8170                                            cc))))
8171                      (nconc (if (eq to t) nil to)
8172                             (if (eq cc t) nil cc)
8173                             from))))
8174              (unless articles
8175                (error "Found no matches for \"%s\"" address))
8176              (gnus-summary-limit articles))
8177       (gnus-summary-position-point))))
8178
8179 (defun gnus-summary-limit-strange-charsets-predicate (header)
8180   (when (fboundp 'char-charset)
8181     (let ((string (concat (mail-header-subject header)
8182                           (mail-header-from header)))
8183           charset found)
8184       (dotimes (i (1- (length string)))
8185         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8186         (when (string-match "unicode\\|big\\|japanese" charset)
8187           (setq found t)))
8188       found)))
8189
8190 (defun gnus-summary-limit-to-predicate (predicate)
8191   "Limit to articles where PREDICATE returns non-nil.
8192 PREDICATE will be called with the header structures of the
8193 articles."
8194   (let ((articles nil)
8195         (case-fold-search t))
8196     (dolist (header gnus-newsgroup-headers)
8197       (when (funcall predicate header)
8198         (push (mail-header-number header) articles)))
8199     (gnus-summary-limit (nreverse articles))))
8200
8201 (defun gnus-summary-limit-to-age (age &optional younger-p)
8202   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8203 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8204 articles that are younger than AGE days."
8205   (interactive
8206    (let ((younger current-prefix-arg)
8207          (days-got nil)
8208          days)
8209      (while (not days-got)
8210        (setq days (if younger
8211                       (read-string "Limit to articles younger than (in days, older when negative): ")
8212                     (read-string
8213                      "Limit to articles older than (in days, younger when negative): ")))
8214        (when (> (length days) 0)
8215          (setq days (read days)))
8216        (if (numberp days)
8217            (progn
8218              (setq days-got t)
8219              (when (< days 0)
8220                (setq younger (not younger))
8221                (setq days (* days -1))))
8222          (message "Please enter a number.")
8223          (sleep-for 1)))
8224      (list days younger)))
8225   (prog1
8226       (let ((data gnus-newsgroup-data)
8227             (cutoff (days-to-time age))
8228             articles d date is-younger)
8229         (while (setq d (pop data))
8230           (when (and (vectorp (gnus-data-header d))
8231                      (setq date (mail-header-date (gnus-data-header d))))
8232             (setq is-younger (time-less-p
8233                               (time-since (gnus-date-get-time date))
8234                               cutoff))
8235             (when (if younger-p
8236                       is-younger
8237                     (not is-younger))
8238               (push (gnus-data-number d) articles))))
8239         (gnus-summary-limit (nreverse articles)))
8240     (gnus-summary-position-point)))
8241
8242 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8243   "Limit the summary buffer to articles that match an 'extra' header."
8244   (interactive
8245    (let ((header
8246           (intern
8247            (gnus-completing-read-with-default
8248             (symbol-name (car gnus-extra-headers))
8249             (if current-prefix-arg
8250                 "Exclude extra header"
8251               "Limit extra header")
8252             (mapcar (lambda (x)
8253                       (cons (symbol-name x) x))
8254                     gnus-extra-headers)
8255             nil
8256             t))))
8257      (list header
8258            (read-string (format "%s header %s (regexp): "
8259                                 (if current-prefix-arg "Exclude" "Limit to")
8260                                 header))
8261            current-prefix-arg)))
8262   (when (not (equal "" regexp))
8263     (prog1
8264         (let ((articles (gnus-summary-find-matching
8265                          (cons 'extra header) regexp 'all nil nil
8266                          not-matching)))
8267           (unless articles
8268             (error "Found no matches for \"%s\"" regexp))
8269           (gnus-summary-limit articles))
8270       (gnus-summary-position-point))))
8271
8272 (defun gnus-summary-limit-to-display-predicate ()
8273   "Limit the summary buffer to the predicated in the `display' group parameter."
8274   (interactive)
8275   (unless gnus-newsgroup-display
8276     (error "There is no `display' group parameter"))
8277   (let (articles)
8278     (dolist (number gnus-newsgroup-articles)
8279       (when (funcall gnus-newsgroup-display)
8280         (push number articles)))
8281     (gnus-summary-limit articles))
8282   (gnus-summary-position-point))
8283
8284 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8285 (make-obsolete
8286  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8287
8288 (defun gnus-summary-limit-to-unread (&optional all)
8289   "Limit the summary buffer to articles that are not marked as read.
8290 If ALL is non-nil, limit strictly to unread articles."
8291   (interactive "P")
8292   (if all
8293       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8294     (gnus-summary-limit-to-marks
8295      ;; Concat all the marks that say that an article is read and have
8296      ;; those removed.
8297      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8298            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8299            gnus-low-score-mark gnus-expirable-mark
8300            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8301            gnus-duplicate-mark)
8302      'reverse)))
8303
8304 (defun gnus-summary-limit-to-headers (match &optional reverse)
8305   "Limit the summary buffer to articles that have headers that match MATCH.
8306 If REVERSE (the prefix), limit to articles that don't match."
8307   (interactive "sMatch headers (regexp): \nP")
8308   (gnus-summary-limit-to-bodies match reverse t))
8309
8310 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8311   "Limit the summary buffer to articles that have bodies that match MATCH.
8312 If REVERSE (the prefix), limit to articles that don't match."
8313   (interactive "sMatch body (regexp): \nP")
8314   (let ((articles nil)
8315         (gnus-select-article-hook nil)  ;Disable hook.
8316         (gnus-article-prepare-hook nil)
8317         (gnus-use-article-prefetch nil)
8318         (gnus-keep-backlog nil)
8319         (gnus-break-pages nil)
8320         (gnus-summary-display-arrow nil)
8321         (gnus-updated-mode-lines nil)
8322         (gnus-auto-center-summary nil)
8323         (gnus-display-mime-function nil))
8324     (dolist (data gnus-newsgroup-data)
8325       (let (gnus-mark-article-hook)
8326         (gnus-summary-select-article t t nil (gnus-data-number data)))
8327       (with-current-buffer gnus-article-buffer
8328         (article-goto-body)
8329         (let* ((case-fold-search t)
8330                (found (if headersp
8331                           (re-search-backward match nil t)
8332                         (re-search-forward match nil t))))
8333           (when (or (and found
8334                          (not reverse))
8335                     (and (not found)
8336                          reverse))
8337             (push (gnus-data-number data) articles)))))
8338     (if (not articles)
8339         (message "No messages matched")
8340       (gnus-summary-limit articles)))
8341   (gnus-summary-position-point))
8342
8343 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8344   "Limit the summary buffer to articles that aren't part on any thread.
8345 If THREADSP (the prefix), limit to articles that are in threads."
8346   (interactive "P")
8347   (let ((articles nil)
8348         thread-articles
8349         threads)
8350     (dolist (thread gnus-newsgroup-threads)
8351       (if (stringp (car thread))
8352           (dolist (thread (cdr thread))
8353             (push thread threads))
8354         (push thread threads)))
8355     (dolist (thread threads)
8356       (setq thread-articles (gnus-articles-in-thread thread))
8357       (when (or (and threadsp
8358                      (> (length thread-articles) 1))
8359                 (and (not threadsp)
8360                      (= (length thread-articles) 1)))
8361         (setq articles (nconc thread-articles articles))))
8362     (if (not articles)
8363         (message "No messages matched")
8364       (gnus-summary-limit articles))
8365     (gnus-summary-position-point)))
8366
8367 (defun gnus-summary-limit-to-replied (&optional unreplied)
8368   "Limit the summary buffer to replied articles.
8369 If UNREPLIED (the prefix), limit to unreplied articles."
8370   (interactive "P")
8371   (if unreplied
8372       (gnus-summary-limit
8373        (gnus-set-difference gnus-newsgroup-articles
8374         gnus-newsgroup-replied))
8375     (gnus-summary-limit gnus-newsgroup-replied))
8376   (gnus-summary-position-point))
8377
8378 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8379 (make-obsolete 'gnus-summary-delete-marked-with
8380                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8381
8382 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8383   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8384 If REVERSE, limit the summary buffer to articles that are marked
8385 with MARKS.  MARKS can either be a string of marks or a list of marks.
8386 Returns how many articles were removed."
8387   (interactive "sMarks: ")
8388   (gnus-summary-limit-to-marks marks t))
8389
8390 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8391   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8392 If REVERSE (the prefix), limit the summary buffer to articles that are
8393 not marked with MARKS.  MARKS can either be a string of marks or a
8394 list of marks.
8395 Returns how many articles were removed."
8396   (interactive "sMarks: \nP")
8397   (prog1
8398       (let ((data gnus-newsgroup-data)
8399             (marks (if (listp marks) marks
8400                      (append marks nil))) ; Transform to list.
8401             articles)
8402         (while data
8403           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8404                   (memq (gnus-data-mark (car data)) marks))
8405             (push (gnus-data-number (car data)) articles))
8406           (setq data (cdr data)))
8407         (gnus-summary-limit articles))
8408     (gnus-summary-position-point)))
8409
8410 (defun gnus-summary-limit-to-score (score)
8411   "Limit to articles with score at or above SCORE."
8412   (interactive "NLimit to articles with score of at least: ")
8413   (let ((data gnus-newsgroup-data)
8414         articles)
8415     (while data
8416       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8417                 score)
8418         (push (gnus-data-number (car data)) articles))
8419       (setq data (cdr data)))
8420     (prog1
8421         (gnus-summary-limit articles)
8422       (gnus-summary-position-point))))
8423
8424 (defun gnus-summary-limit-to-unseen ()
8425   "Limit to unseen articles."
8426   (interactive)
8427   (prog1
8428       (gnus-summary-limit gnus-newsgroup-unseen)
8429     (gnus-summary-position-point)))
8430
8431 (defun gnus-summary-limit-include-thread (id)
8432   "Display all the hidden articles that is in the thread with ID in it.
8433 When called interactively, ID is the Message-ID of the current
8434 article."
8435   (interactive (list (mail-header-id (gnus-summary-article-header))))
8436   (let ((articles (gnus-articles-in-thread
8437                    (gnus-id-to-thread (gnus-root-id id)))))
8438     (prog1
8439         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8440       (gnus-summary-limit-include-matching-articles
8441        "subject"
8442        (regexp-quote (gnus-simplify-subject-re
8443                       (mail-header-subject (gnus-id-to-header id)))))
8444       (gnus-summary-position-point))))
8445
8446 (defun gnus-summary-limit-include-matching-articles (header regexp)
8447   "Display all the hidden articles that have HEADERs that match REGEXP."
8448   (interactive (list (read-string "Match on header: ")
8449                      (read-string "Regexp: ")))
8450   (let ((articles (gnus-find-matching-articles header regexp)))
8451     (prog1
8452         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8453       (gnus-summary-position-point))))
8454
8455 (defun gnus-summary-insert-dormant-articles ()
8456   "Insert all the dormant articles for this group into the current buffer."
8457   (interactive)
8458   (let ((gnus-verbose (max 6 gnus-verbose)))
8459     (if (not gnus-newsgroup-dormant)
8460         (gnus-message 3 "No dormant articles for this group")
8461       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8462
8463 (defun gnus-summary-insert-ticked-articles ()
8464   "Insert ticked articles for this group into the current buffer."
8465   (interactive)
8466   (let ((gnus-verbose (max 6 gnus-verbose)))
8467     (if (not gnus-newsgroup-marked)
8468         (gnus-message 3 "No ticked articles for this group")
8469       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8470
8471 (defun gnus-summary-limit-include-dormant ()
8472   "Display all the hidden articles that are marked as dormant.
8473 Note that this command only works on a subset of the articles currently
8474 fetched for this group."
8475   (interactive)
8476   (unless gnus-newsgroup-dormant
8477     (error "There are no dormant articles in this group"))
8478   (prog1
8479       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8480     (gnus-summary-position-point)))
8481
8482 (defun gnus-summary-limit-exclude-dormant ()
8483   "Hide all dormant articles."
8484   (interactive)
8485   (prog1
8486       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8487     (gnus-summary-position-point)))
8488
8489 (defun gnus-summary-limit-exclude-childless-dormant ()
8490   "Hide all dormant articles that have no children."
8491   (interactive)
8492   (let ((data (gnus-data-list t))
8493         articles d children)
8494     ;; Find all articles that are either not dormant or have
8495     ;; children.
8496     (while (setq d (pop data))
8497       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8498                 (and (setq children
8499                            (gnus-article-children (gnus-data-number d)))
8500                      (let (found)
8501                        (while children
8502                          (when (memq (car children) articles)
8503                            (setq children nil
8504                                  found t))
8505                          (pop children))
8506                        found)))
8507         (push (gnus-data-number d) articles)))
8508     ;; Do the limiting.
8509     (prog1
8510         (gnus-summary-limit articles)
8511       (gnus-summary-position-point))))
8512
8513 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8514   "Mark all unread excluded articles as read.
8515 If ALL, mark even excluded ticked and dormants as read."
8516   (interactive "P")
8517   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8518   (let ((articles (gnus-sorted-ndifference
8519                    (sort
8520                     (mapcar (lambda (h) (mail-header-number h))
8521                             gnus-newsgroup-headers)
8522                     '<)
8523                    gnus-newsgroup-limit))
8524         article)
8525     (setq gnus-newsgroup-unreads
8526           (gnus-sorted-intersection gnus-newsgroup-unreads
8527                                     gnus-newsgroup-limit))
8528     (if all
8529         (setq gnus-newsgroup-dormant nil
8530               gnus-newsgroup-marked nil
8531               gnus-newsgroup-reads
8532               (nconc
8533                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8534                gnus-newsgroup-reads))
8535       (while (setq article (pop articles))
8536         (unless (or (memq article gnus-newsgroup-dormant)
8537                     (memq article gnus-newsgroup-marked))
8538           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8539
8540 (defun gnus-summary-limit (articles &optional pop)
8541   (if pop
8542       ;; We pop the previous limit off the stack and use that.
8543       (setq articles (car gnus-newsgroup-limits)
8544             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8545     ;; We use the new limit, so we push the old limit on the stack.
8546     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8547   ;; Set the limit.
8548   (setq gnus-newsgroup-limit articles)
8549   (let ((total (length gnus-newsgroup-data))
8550         (data (gnus-data-find-list (gnus-summary-article-number)))
8551         (gnus-summary-mark-below nil)   ; Inhibit this.
8552         found)
8553     ;; This will do all the work of generating the new summary buffer
8554     ;; according to the new limit.
8555     (gnus-summary-prepare)
8556     ;; Hide any threads, possibly.
8557     (gnus-summary-maybe-hide-threads)
8558     ;; Try to return to the article you were at, or one in the
8559     ;; neighborhood.
8560     (when data
8561       ;; We try to find some article after the current one.
8562       (while data
8563         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8564           (setq data nil
8565                 found t))
8566         (setq data (cdr data))))
8567     (unless found
8568       ;; If there is no data, that means that we were after the last
8569       ;; article.  The same goes when we can't find any articles
8570       ;; after the current one.
8571       (goto-char (point-max))
8572       (gnus-summary-find-prev))
8573     (gnus-set-mode-line 'summary)
8574     ;; We return how many articles were removed from the summary
8575     ;; buffer as a result of the new limit.
8576     (- total (length gnus-newsgroup-data))))
8577
8578 (defsubst gnus-invisible-cut-children (threads)
8579   (let ((num 0))
8580     (while threads
8581       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8582         (incf num))
8583       (pop threads))
8584     (< num 2)))
8585
8586 (defsubst gnus-cut-thread (thread)
8587   "Go forwards in the thread until we find an article that we want to display."
8588   (when (or (eq gnus-fetch-old-headers 'some)
8589             (eq gnus-fetch-old-headers 'invisible)
8590             (numberp gnus-fetch-old-headers)
8591             (eq gnus-build-sparse-threads 'some)
8592             (eq gnus-build-sparse-threads 'more))
8593     ;; Deal with old-fetched headers and sparse threads.
8594     (while (and
8595             thread
8596             (or
8597              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8598              (gnus-summary-article-ancient-p
8599               (mail-header-number (car thread))))
8600             (if (or (<= (length (cdr thread)) 1)
8601                     (eq gnus-fetch-old-headers 'invisible))
8602                 (setq gnus-newsgroup-limit
8603                       (delq (mail-header-number (car thread))
8604                             gnus-newsgroup-limit)
8605                       thread (cadr thread))
8606               (when (gnus-invisible-cut-children (cdr thread))
8607                 (let ((th (cdr thread)))
8608                   (while th
8609                     (if (memq (mail-header-number (caar th))
8610                               gnus-newsgroup-limit)
8611                         (setq thread (car th)
8612                               th nil)
8613                       (setq th (cdr th))))))))))
8614   thread)
8615
8616 (defun gnus-cut-threads (threads)
8617   "Cut off all uninteresting articles from the beginning of THREADS."
8618   (when (or (eq gnus-fetch-old-headers 'some)
8619             (eq gnus-fetch-old-headers 'invisible)
8620             (numberp gnus-fetch-old-headers)
8621             (eq gnus-build-sparse-threads 'some)
8622             (eq gnus-build-sparse-threads 'more))
8623     (let ((th threads))
8624       (while th
8625         (setcar th (gnus-cut-thread (car th)))
8626         (setq th (cdr th)))))
8627   ;; Remove nixed out threads.
8628   (delq nil threads))
8629
8630 (defun gnus-summary-initial-limit (&optional show-if-empty)
8631   "Figure out what the initial limit is supposed to be on group entry.
8632 This entails weeding out unwanted dormants, low-scored articles,
8633 fetch-old-headers verbiage, and so on."
8634   ;; Most groups have nothing to remove.
8635   (unless (or gnus-inhibit-limiting
8636               (and (null gnus-newsgroup-dormant)
8637                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8638                    (not (eq gnus-fetch-old-headers 'some))
8639                    (not (numberp gnus-fetch-old-headers))
8640                    (not (eq gnus-fetch-old-headers 'invisible))
8641                    (null gnus-summary-expunge-below)
8642                    (not (eq gnus-build-sparse-threads 'some))
8643                    (not (eq gnus-build-sparse-threads 'more))
8644                    (null gnus-thread-expunge-below)
8645                    (not gnus-use-nocem)))
8646     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8647     (setq gnus-newsgroup-limit nil)
8648     (mapatoms
8649      (lambda (node)
8650        (unless (car (symbol-value node))
8651          ;; These threads have no parents -- they are roots.
8652          (let ((nodes (cdr (symbol-value node)))
8653                thread)
8654            (while nodes
8655              (if (and gnus-thread-expunge-below
8656                       (< (gnus-thread-total-score (car nodes))
8657                          gnus-thread-expunge-below))
8658                  (gnus-expunge-thread (pop nodes))
8659                (setq thread (pop nodes))
8660                (gnus-summary-limit-children thread))))))
8661      gnus-newsgroup-dependencies)
8662     ;; If this limitation resulted in an empty group, we might
8663     ;; pop the previous limit and use it instead.
8664     (when (and (not gnus-newsgroup-limit)
8665                show-if-empty)
8666       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8667     gnus-newsgroup-limit))
8668
8669 (defun gnus-summary-limit-children (thread)
8670   "Return 1 if this subthread is visible and 0 if it is not."
8671   ;; First we get the number of visible children to this thread.  This
8672   ;; is done by recursing down the thread using this function, so this
8673   ;; will really go down to a leaf article first, before slowly
8674   ;; working its way up towards the root.
8675   (when thread
8676     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8677            (children
8678            (if (cdr thread)
8679                (apply '+ (mapcar 'gnus-summary-limit-children
8680                                  (cdr thread)))
8681              0))
8682           (number (mail-header-number (car thread)))
8683           score)
8684       (if (and
8685            (not (memq number gnus-newsgroup-marked))
8686            (or
8687             ;; If this article is dormant and has absolutely no visible
8688             ;; children, then this article isn't visible.
8689             (and (memq number gnus-newsgroup-dormant)
8690                  (zerop children))
8691             ;; If this is "fetch-old-headered" and there is no
8692             ;; visible children, then we don't want this article.
8693             (and (or (eq gnus-fetch-old-headers 'some)
8694                      (numberp gnus-fetch-old-headers))
8695                  (gnus-summary-article-ancient-p number)
8696                  (zerop children))
8697             ;; If this is "fetch-old-headered" and `invisible', then
8698             ;; we don't want this article.
8699             (and (eq gnus-fetch-old-headers 'invisible)
8700                  (gnus-summary-article-ancient-p number))
8701             ;; If this is a sparsely inserted article with no children,
8702             ;; we don't want it.
8703             (and (eq gnus-build-sparse-threads 'some)
8704                  (gnus-summary-article-sparse-p number)
8705                  (zerop children))
8706             ;; If we use expunging, and this article is really
8707             ;; low-scored, then we don't want this article.
8708             (when (and gnus-summary-expunge-below
8709                        (< (setq score
8710                                 (or (cdr (assq number gnus-newsgroup-scored))
8711                                     gnus-summary-default-score))
8712                           gnus-summary-expunge-below))
8713               ;; We increase the expunge-tally here, but that has
8714               ;; nothing to do with the limits, really.
8715               (incf gnus-newsgroup-expunged-tally)
8716               ;; We also mark as read here, if that's wanted.
8717               (when (and gnus-summary-mark-below
8718                          (< score gnus-summary-mark-below))
8719                 (setq gnus-newsgroup-unreads
8720                       (delq number gnus-newsgroup-unreads))
8721                 (if gnus-newsgroup-auto-expire
8722                     (push number gnus-newsgroup-expirable)
8723                   (push (cons number gnus-low-score-mark)
8724                         gnus-newsgroup-reads)))
8725               t)
8726             ;; Do the `display' group parameter.
8727             (and gnus-newsgroup-display
8728                  (not (funcall gnus-newsgroup-display)))
8729             ;; Check NoCeM things.
8730             (when (and gnus-use-nocem
8731                        (gnus-nocem-unwanted-article-p
8732                         (mail-header-id (car thread))))
8733               (setq gnus-newsgroup-unreads
8734                     (delq number gnus-newsgroup-unreads))
8735               t)))
8736           ;; Nope, invisible article.
8737           0
8738         ;; Ok, this article is to be visible, so we add it to the limit
8739         ;; and return 1.
8740         (push number gnus-newsgroup-limit)
8741         1))))
8742
8743 (defun gnus-expunge-thread (thread)
8744   "Mark all articles in THREAD as read."
8745   (let* ((number (mail-header-number (car thread))))
8746     (incf gnus-newsgroup-expunged-tally)
8747     ;; We also mark as read here, if that's wanted.
8748     (setq gnus-newsgroup-unreads
8749           (delq number gnus-newsgroup-unreads))
8750     (if gnus-newsgroup-auto-expire
8751         (push number gnus-newsgroup-expirable)
8752       (push (cons number gnus-low-score-mark)
8753             gnus-newsgroup-reads)))
8754   ;; Go recursively through all subthreads.
8755   (mapcar 'gnus-expunge-thread (cdr thread)))
8756
8757 ;; Summary article oriented commands
8758
8759 (defun gnus-summary-refer-parent-article (n)
8760   "Refer parent article N times.
8761 If N is negative, go to ancestor -N instead.
8762 The difference between N and the number of articles fetched is returned."
8763   (interactive "p")
8764   (let ((skip 1)
8765         error header ref)
8766     (when (not (natnump n))
8767       (setq skip (abs n)
8768             n 1))
8769     (while (and (> n 0)
8770                 (not error))
8771       (setq header (gnus-summary-article-header))
8772       (if (and (eq (mail-header-number header)
8773                    (cdr gnus-article-current))
8774                (equal gnus-newsgroup-name
8775                       (car gnus-article-current)))
8776           ;; If we try to find the parent of the currently
8777           ;; displayed article, then we take a look at the actual
8778           ;; References header, since this is slightly more
8779           ;; reliable than the References field we got from the
8780           ;; server.
8781           (with-current-buffer gnus-original-article-buffer
8782             (nnheader-narrow-to-headers)
8783             (unless (setq ref (message-fetch-field "references"))
8784               (when (setq ref (message-fetch-field "in-reply-to"))
8785                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8786             (widen))
8787         (setq ref
8788               ;; It's not the current article, so we take a bet on
8789               ;; the value we got from the server.
8790               (mail-header-references header)))
8791       (if (and ref
8792                (not (equal ref "")))
8793           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8794             (gnus-message 1 "Couldn't find parent"))
8795         (gnus-message 1 "No references in article %d"
8796                       (gnus-summary-article-number))
8797         (setq error t))
8798       (decf n))
8799     (gnus-summary-position-point)
8800     n))
8801
8802 (defun gnus-summary-refer-references ()
8803   "Fetch all articles mentioned in the References header.
8804 Return the number of articles fetched."
8805   (interactive)
8806   (let ((ref (mail-header-references (gnus-summary-article-header)))
8807         (current (gnus-summary-article-number))
8808         (n 0))
8809     (if (or (not ref)
8810             (equal ref ""))
8811         (error "No References in the current article")
8812       ;; For each Message-ID in the References header...
8813       (while (string-match "<[^>]*>" ref)
8814         (incf n)
8815         ;; ... fetch that article.
8816         (gnus-summary-refer-article
8817          (prog1 (match-string 0 ref)
8818            (setq ref (substring ref (match-end 0))))))
8819       (gnus-summary-goto-subject current)
8820       (gnus-summary-position-point)
8821       n)))
8822
8823 (defun gnus-summary-refer-thread (&optional limit)
8824   "Fetch all articles in the current thread.
8825 If LIMIT (the numerical prefix), fetch that many old headers instead
8826 of what's specified by the `gnus-refer-thread-limit' variable."
8827   (interactive "P")
8828   (let ((id (mail-header-id (gnus-summary-article-header)))
8829         (limit (if limit (prefix-numeric-value limit)
8830                  gnus-refer-thread-limit)))
8831     (unless (eq gnus-fetch-old-headers 'invisible)
8832       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8833       ;; Retrieve the headers and read them in.
8834       (if (eq (if (numberp limit)
8835                   (gnus-retrieve-headers
8836                    (list (min
8837                           (+ (mail-header-number
8838                               (gnus-summary-article-header))
8839                              limit)
8840                           gnus-newsgroup-end))
8841                    gnus-newsgroup-name (* limit 2))
8842                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8843                 ;; headers.
8844                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8845                                        gnus-newsgroup-name limit))
8846               'nov)
8847           (gnus-build-all-threads)
8848         (error "Can't fetch thread from back ends that don't support NOV"))
8849       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8850     (gnus-summary-limit-include-thread id)))
8851
8852 (defun gnus-summary-refer-article (message-id)
8853   "Fetch an article specified by MESSAGE-ID."
8854   (interactive "sMessage-ID: ")
8855   (when (and (stringp message-id)
8856              (not (zerop (length message-id))))
8857     (setq message-id (gnus-replace-in-string message-id " " ""))
8858     ;; Construct the correct Message-ID if necessary.
8859     ;; Suggested by tale@pawl.rpi.edu.
8860     (unless (string-match "^<" message-id)
8861       (setq message-id (concat "<" message-id)))
8862     (unless (string-match ">$" message-id)
8863       (setq message-id (concat message-id ">")))
8864     ;; People often post MIDs from URLs, so unhex it:
8865     (unless (string-match "@" message-id)
8866       (setq message-id (gnus-url-unhex-string message-id)))
8867     (let* ((header (gnus-id-to-header message-id))
8868            (sparse (and header
8869                         (gnus-summary-article-sparse-p
8870                          (mail-header-number header))
8871                         (memq (mail-header-number header)
8872                               gnus-newsgroup-limit)))
8873            number)
8874       (cond
8875        ;; If the article is present in the buffer we just go to it.
8876        ((and header
8877              (or (not (gnus-summary-article-sparse-p
8878                        (mail-header-number header)))
8879                  sparse))
8880         (prog1
8881             (gnus-summary-goto-article
8882              (mail-header-number header) nil t)
8883           (when sparse
8884             (gnus-summary-update-article (mail-header-number header)))))
8885        (t
8886         ;; We fetch the article.
8887         (catch 'found
8888           (dolist (gnus-override-method (gnus-refer-article-methods))
8889             (when (and (gnus-check-server gnus-override-method)
8890                        ;; Fetch the header,
8891                        (setq number (gnus-summary-insert-subject message-id)))
8892               ;; and display the article.
8893               (gnus-summary-select-article nil nil nil number)
8894               (throw 'found t)))
8895           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8896
8897 (defun gnus-refer-article-methods ()
8898   "Return a list of referable methods."
8899   (cond
8900    ;; No method, so we default to current and native.
8901    ((null gnus-refer-article-method)
8902     (list gnus-current-select-method gnus-select-method))
8903    ;; Current.
8904    ((eq 'current gnus-refer-article-method)
8905     (list gnus-current-select-method))
8906    ;; List of select methods.
8907    ((not (and (symbolp (car gnus-refer-article-method))
8908               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8909     (let (out)
8910       (dolist (method gnus-refer-article-method)
8911         (push (if (eq 'current method)
8912                   gnus-current-select-method
8913                 method)
8914               out))
8915       (nreverse out)))
8916    ;; One single select method.
8917    (t
8918     (list gnus-refer-article-method))))
8919
8920 (defun gnus-summary-edit-parameters ()
8921   "Edit the group parameters of the current group."
8922   (interactive)
8923   (gnus-group-edit-group gnus-newsgroup-name 'params))
8924
8925 (defun gnus-summary-customize-parameters ()
8926   "Customize the group parameters of the current group."
8927   (interactive)
8928   (gnus-group-customize gnus-newsgroup-name))
8929
8930 (defun gnus-summary-enter-digest-group (&optional force)
8931   "Enter an nndoc group based on the current article.
8932 If FORCE, force a digest interpretation.  If not, try
8933 to guess what the document format is."
8934   (interactive "P")
8935   (let ((conf gnus-current-window-configuration))
8936     (save-window-excursion
8937       (save-excursion
8938         (let (gnus-article-prepare-hook
8939               gnus-display-mime-function
8940               gnus-break-pages)
8941           (gnus-summary-select-article))))
8942     (setq gnus-current-window-configuration conf)
8943     (let* ((name (format "%s-%d"
8944                          (gnus-group-prefixed-name
8945                           gnus-newsgroup-name (list 'nndoc ""))
8946                          (with-current-buffer gnus-summary-buffer
8947                            gnus-current-article)))
8948            (ogroup gnus-newsgroup-name)
8949            (params (append (gnus-info-params (gnus-get-info ogroup))
8950                            (list (cons 'to-group ogroup))
8951                            (list (cons 'parent-group ogroup))
8952                            (list (cons 'save-article-group ogroup))))
8953            (case-fold-search t)
8954            (buf (current-buffer))
8955            dig to-address)
8956       (with-current-buffer gnus-original-article-buffer
8957         ;; Have the digest group inherit the main mail address of
8958         ;; the parent article.
8959         (when (setq to-address (or (gnus-fetch-field "reply-to")
8960                                    (gnus-fetch-field "from")))
8961           (setq params
8962                 (append
8963                  (list (cons 'to-address
8964                              (funcall gnus-decode-encoded-address-function
8965                                       to-address))))))
8966         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8967         (insert-buffer-substring gnus-original-article-buffer)
8968         ;; Remove lines that may lead nndoc to misinterpret the
8969         ;; document type.
8970         (narrow-to-region
8971          (goto-char (point-min))
8972          (or (search-forward "\n\n" nil t) (point)))
8973         (goto-char (point-min))
8974         (delete-matching-lines "^Path:\\|^From ")
8975         (widen))
8976       (unwind-protect
8977           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8978                     (gnus-newsgroup-ephemeral-ignored-charsets
8979                      gnus-newsgroup-ignored-charsets))
8980                 (gnus-group-read-ephemeral-group
8981                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8982                               (nndoc-article-type
8983                                ,(if force 'mbox 'guess)))
8984                  t nil nil nil
8985                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8986                                                         "ADAPT")))))
8987               ;; Make all postings to this group go to the parent group.
8988               (nconc (gnus-info-params (gnus-get-info name))
8989                      params)
8990             ;; Couldn't select this doc group.
8991             (switch-to-buffer buf)
8992             (gnus-set-global-variables)
8993             (gnus-configure-windows 'summary)
8994             (gnus-message 3 "Article couldn't be entered?"))
8995         (kill-buffer dig)))))
8996
8997 (defun gnus-summary-read-document (n)
8998   "Open a new group based on the current article(s).
8999 This will allow you to read digests and other similar
9000 documents as newsgroups.
9001 Obeys the standard process/prefix convention."
9002   (interactive "P")
9003   (let* ((ogroup gnus-newsgroup-name)
9004          (params (append (gnus-info-params (gnus-get-info ogroup))
9005                          (list (cons 'to-group ogroup))))
9006          group egroup groups vgroup)
9007     (dolist (article (gnus-summary-work-articles n))
9008       (setq group (format "%s-%d" gnus-newsgroup-name article))
9009       (gnus-summary-remove-process-mark article)
9010       (when (gnus-summary-display-article article)
9011         (save-excursion ;;What for?
9012           (with-temp-buffer
9013             (insert-buffer-substring gnus-original-article-buffer)
9014             ;; Remove some headers that may lead nndoc to make
9015             ;; the wrong guess.
9016             (message-narrow-to-head)
9017             (goto-char (point-min))
9018             (delete-matching-lines "^Path:\\|^From ")
9019             (widen)
9020             (if (setq egroup
9021                       (gnus-group-read-ephemeral-group
9022                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9023                                      (nndoc-article-type guess))
9024                        t nil t))
9025                 (progn
9026                   ;; Make all postings to this group go to the parent group.
9027                   (nconc (gnus-info-params (gnus-get-info egroup))
9028                          params)
9029                   (push egroup groups))
9030               ;; Couldn't select this doc group.
9031               (gnus-error 3 "Article couldn't be entered"))))))
9032     ;; Now we have selected all the documents.
9033     (cond
9034      ((not groups)
9035       (error "None of the articles could be interpreted as documents"))
9036      ((gnus-group-read-ephemeral-group
9037        (setq vgroup (format
9038                      "nnvirtual:%s-%s" gnus-newsgroup-name
9039                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9040        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9041        t
9042        (cons (current-buffer) 'summary)))
9043      (t
9044       (error "Couldn't select virtual nndoc group")))))
9045
9046 (defun gnus-summary-isearch-article (&optional regexp-p)
9047   "Do incremental search forward on the current article.
9048 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9049   (interactive "P")
9050   (gnus-summary-select-article)
9051   (gnus-configure-windows 'article)
9052   (gnus-eval-in-buffer-window gnus-article-buffer
9053     (save-restriction
9054       (widen)
9055       (isearch-forward regexp-p))))
9056
9057 (defun gnus-summary-repeat-search-article-forward ()
9058   "Repeat the previous search forwards."
9059   (interactive)
9060   (unless gnus-last-search-regexp
9061     (error "No previous search"))
9062   (gnus-summary-search-article-forward gnus-last-search-regexp))
9063
9064 (defun gnus-summary-repeat-search-article-backward ()
9065   "Repeat the previous search backwards."
9066   (interactive)
9067   (unless gnus-last-search-regexp
9068     (error "No previous search"))
9069   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9070
9071 (defun gnus-summary-search-article-forward (regexp &optional backward)
9072   "Search for an article containing REGEXP forward.
9073 If BACKWARD, search backward instead."
9074   (interactive
9075    (list (read-string
9076           (format "Search article %s (regexp%s): "
9077                   (if current-prefix-arg "backward" "forward")
9078                   (if gnus-last-search-regexp
9079                       (concat ", default " gnus-last-search-regexp)
9080                     "")))
9081          current-prefix-arg))
9082   (if (string-equal regexp "")
9083       (setq regexp (or gnus-last-search-regexp ""))
9084     (setq gnus-last-search-regexp regexp)
9085     (setq gnus-article-before-search gnus-current-article))
9086   ;; Intentionally set gnus-last-article.
9087   (setq gnus-last-article gnus-article-before-search)
9088   (let ((gnus-last-article gnus-last-article))
9089     (if (gnus-summary-search-article regexp backward)
9090         (gnus-summary-show-thread)
9091       (signal 'search-failed (list regexp)))))
9092
9093 (defun gnus-summary-search-article-backward (regexp)
9094   "Search for an article containing REGEXP backward."
9095   (interactive
9096    (list (read-string
9097           (format "Search article backward (regexp%s): "
9098                   (if gnus-last-search-regexp
9099                       (concat ", default " gnus-last-search-regexp)
9100                     "")))))
9101   (gnus-summary-search-article-forward regexp 'backward))
9102
9103 (defun gnus-summary-search-article (regexp &optional backward)
9104   "Search for an article containing REGEXP.
9105 Optional argument BACKWARD means do search for backward.
9106 `gnus-select-article-hook' is not called during the search."
9107   ;; We have to require this here to make sure that the following
9108   ;; dynamic binding isn't shadowed by autoloading.
9109   (require 'gnus-async)
9110   (require 'gnus-art)
9111   (let ((gnus-select-article-hook nil)  ;Disable hook.
9112         (gnus-article-prepare-hook nil)
9113         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9114         (gnus-use-article-prefetch nil)
9115         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9116         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9117         (gnus-visual nil)
9118         (gnus-keep-backlog nil)
9119         (gnus-break-pages nil)
9120         (gnus-summary-display-arrow nil)
9121         (gnus-updated-mode-lines nil)
9122         (gnus-auto-center-summary nil)
9123         (sum (current-buffer))
9124         (gnus-display-mime-function nil)
9125         (found nil)
9126         point)
9127     (gnus-save-hidden-threads
9128       (gnus-summary-select-article)
9129       (set-buffer gnus-article-buffer)
9130       (goto-char (window-point (get-buffer-window (current-buffer))))
9131       (when backward
9132         (forward-line -1))
9133       (while (not found)
9134         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9135         (if (if backward
9136                 (re-search-backward regexp nil t)
9137               (re-search-forward regexp nil t))
9138             ;; We found the regexp.
9139             (progn
9140               (setq found 'found)
9141               (beginning-of-line)
9142               (set-window-start
9143                (get-buffer-window (current-buffer))
9144                (point))
9145               (forward-line 1)
9146               (set-window-point
9147                (get-buffer-window (current-buffer))
9148                (point))
9149               (set-buffer sum)
9150               (setq point (point)))
9151           ;; We didn't find it, so we go to the next article.
9152           (set-buffer sum)
9153           (setq found 'not)
9154           (while (eq found 'not)
9155             (if (not (if backward (gnus-summary-find-prev)
9156                        (gnus-summary-find-next)))
9157                 ;; No more articles.
9158                 (setq found t)
9159               ;; Select the next article and adjust point.
9160               (unless (gnus-summary-article-sparse-p
9161                        (gnus-summary-article-number))
9162                 (setq found nil)
9163                 (gnus-summary-select-article)
9164                 (set-buffer gnus-article-buffer)
9165                 (widen)
9166                 (goto-char (if backward (point-max) (point-min))))))))
9167       (gnus-message 7 ""))
9168     ;; Return whether we found the regexp.
9169     (when (eq found 'found)
9170       (goto-char point)
9171       (gnus-summary-show-thread)
9172       (gnus-summary-goto-subject gnus-current-article)
9173       (gnus-summary-position-point)
9174       t)))
9175
9176 (defun gnus-find-matching-articles (header regexp)
9177   "Return a list of all articles that match REGEXP on HEADER.
9178 This search includes all articles in the current group that Gnus has
9179 fetched headers for, whether they are displayed or not."
9180   (let ((articles nil)
9181         ;; Can't eta-reduce because it's a macro.
9182         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9183         (case-fold-search t))
9184     (dolist (header gnus-newsgroup-headers)
9185       (when (string-match regexp (funcall func header))
9186         (push (mail-header-number header) articles)))
9187     (nreverse articles)))
9188
9189 (defun gnus-summary-find-matching (header regexp &optional backward unread
9190                                           not-case-fold not-matching)
9191   "Return a list of all articles that match REGEXP on HEADER.
9192 The search stars on the current article and goes forwards unless
9193 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9194 If UNREAD is non-nil, only unread articles will
9195 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9196 in the comparisons. If NOT-MATCHING, return a list of all articles that
9197 not match REGEXP on HEADER."
9198   (let ((case-fold-search (not not-case-fold))
9199         articles d func)
9200     (if (consp header)
9201         (if (eq (car header) 'extra)
9202             (setq func
9203                   `(lambda (h)
9204                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9205                          "")))
9206           (error "%s is an invalid header" header))
9207       (unless (fboundp (intern (concat "mail-header-" header)))
9208         (error "%s is not a valid header" header))
9209       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9210     (dolist (d (if (eq backward 'all)
9211                    gnus-newsgroup-data
9212                  (gnus-data-find-list
9213                   (gnus-summary-article-number)
9214                   (gnus-data-list backward))))
9215       (when (and (or (not unread)       ; We want all articles...
9216                      (gnus-data-unread-p d)) ; Or just unreads.
9217                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9218                  (if not-matching
9219                      (not (string-match
9220                            regexp
9221                            (funcall func (gnus-data-header d))))
9222                    (string-match regexp
9223                                  (funcall func (gnus-data-header d)))))
9224         (push (gnus-data-number d) articles))) ; Success!
9225     (nreverse articles)))
9226
9227 (defun gnus-summary-execute-command (header regexp command &optional backward)
9228   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9229 If HEADER is an empty string (or nil), the match is done on the entire
9230 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9231   (interactive
9232    (list (let ((completion-ignore-case t))
9233            (completing-read
9234             "Header name: "
9235             (mapcar (lambda (header) (list (format "%s" header)))
9236                     (append
9237                      '("Number" "Subject" "From" "Lines" "Date"
9238                        "Message-ID" "Xref" "References" "Body")
9239                      gnus-extra-headers))
9240             nil 'require-match))
9241          (read-string "Regexp: ")
9242          (read-key-sequence "Command: ")
9243          current-prefix-arg))
9244   (when (equal header "Body")
9245     (setq header ""))
9246   ;; Hidden thread subtrees must be searched as well.
9247   (gnus-summary-show-all-threads)
9248   ;; We don't want to change current point nor window configuration.
9249   (save-excursion
9250     (save-window-excursion
9251       (let (gnus-visual
9252             gnus-treat-strip-trailing-blank-lines
9253             gnus-treat-strip-leading-blank-lines
9254             gnus-treat-strip-multiple-blank-lines
9255             gnus-treat-hide-boring-headers
9256             gnus-treat-fold-newsgroups
9257             gnus-article-prepare-hook)
9258         (gnus-message 6 "Executing %s..." (key-description command))
9259         ;; We'd like to execute COMMAND interactively so as to give arguments.
9260         (gnus-execute header regexp
9261                       `(call-interactively ',(key-binding command))
9262                       backward)
9263         (gnus-message 6 "Executing %s...done" (key-description command))))))
9264
9265 (defun gnus-summary-beginning-of-article ()
9266   "Scroll the article back to the beginning."
9267   (interactive)
9268   (gnus-summary-select-article)
9269   (gnus-configure-windows 'article)
9270   (gnus-eval-in-buffer-window gnus-article-buffer
9271     (widen)
9272     (goto-char (point-min))
9273     (when gnus-break-pages
9274       (gnus-narrow-to-page))))
9275
9276 (defun gnus-summary-end-of-article ()
9277   "Scroll to the end of the article."
9278   (interactive)
9279   (gnus-summary-select-article)
9280   (gnus-configure-windows 'article)
9281   (gnus-eval-in-buffer-window gnus-article-buffer
9282     (widen)
9283     (goto-char (point-max))
9284     (recenter -3)
9285     (when gnus-break-pages
9286       (gnus-narrow-to-page))))
9287
9288 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9289   "Truncate to LEN and quote all \"(\"'s in STRING."
9290   (gnus-replace-in-string (if (and len (> (length string) len))
9291                               (substring string 0 len)
9292                             string)
9293                           "[()]" "\\\\\\&"))
9294
9295 (defun gnus-summary-print-article (&optional filename n)
9296   "Generate and print a PostScript image of the process-marked (mail) articles.
9297
9298 If used interactively, print the current article if none are
9299 process-marked.  With prefix arg, prompt the user for the name of the
9300 file to save in.
9301
9302 When used from Lisp, accept two optional args FILENAME and N.  N means
9303 to print the next N articles.  If N is negative, print the N previous
9304 articles.  If N is nil and articles have been marked with the process
9305 mark, print these instead.
9306
9307 If the optional first argument FILENAME is nil, send the image to the
9308 printer.  If FILENAME is a string, save the PostScript image in a file with
9309 that name.  If FILENAME is a number, prompt the user for the name of the file
9310 to save in."
9311   (interactive (list (ps-print-preprint current-prefix-arg)))
9312   (dolist (article (gnus-summary-work-articles n))
9313     (gnus-summary-select-article nil nil 'pseudo article)
9314     (gnus-eval-in-buffer-window gnus-article-buffer
9315       (gnus-print-buffer))
9316     (gnus-summary-remove-process-mark article))
9317   (ps-despool filename))
9318
9319 (defun gnus-print-buffer ()
9320   (let ((buffer (generate-new-buffer " *print*")))
9321     (unwind-protect
9322         (progn
9323           (copy-to-buffer buffer (point-min) (point-max))
9324           (set-buffer buffer)
9325           (gnus-remove-text-with-property 'gnus-decoration)
9326           (when (gnus-visual-p 'article-highlight 'highlight)
9327             ;; Copy-to-buffer doesn't copy overlay.  So redo
9328             ;; highlight.
9329             (let ((gnus-article-buffer buffer))
9330               (gnus-article-highlight-citation t)
9331               (gnus-article-highlight-signature)
9332               (gnus-article-emphasize)
9333               (gnus-article-delete-invisible-text)))
9334           (let ((ps-left-header
9335                  (list
9336                   (concat "("
9337                           (gnus-summary-print-truncate-and-quote
9338                            (mail-header-subject gnus-current-headers)
9339                            66) ")")
9340                   (concat "("
9341                           (gnus-summary-print-truncate-and-quote
9342                            (mail-header-from gnus-current-headers)
9343                            45) ")")))
9344                 (ps-right-header
9345                  (list
9346                   "/pagenumberstring load"
9347                   (concat "("
9348                           (mail-header-date gnus-current-headers) ")"))))
9349             (gnus-run-hooks 'gnus-ps-print-hook)
9350             (save-excursion
9351               (if ps-print-color-p
9352                   (ps-spool-buffer-with-faces)
9353                 (ps-spool-buffer)))))
9354       (kill-buffer buffer))))
9355
9356 (defun gnus-summary-show-article (&optional arg)
9357   "Force redisplaying of the current article.
9358 If ARG (the prefix) is a number, show the article with the charset
9359 defined in `gnus-summary-show-article-charset-alist', or the charset
9360 input.
9361 If ARG (the prefix) is non-nil and not a number, show the raw article
9362 without any article massaging functions being run.  Normally, the key
9363 strokes are `C-u g'."
9364   (interactive "P")
9365   (cond
9366    ((numberp arg)
9367     (gnus-summary-show-article t)
9368     (let ((gnus-newsgroup-charset
9369            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9370                (mm-read-coding-system
9371                 "View as charset: " ;; actually it is coding system.
9372                 (with-current-buffer gnus-article-buffer
9373                   (mm-detect-coding-region (point) (point-max))))))
9374           (gnus-newsgroup-ignored-charsets 'gnus-all))
9375       (gnus-summary-select-article nil 'force)
9376       (let ((deps gnus-newsgroup-dependencies)
9377             head header lines)
9378         (with-current-buffer gnus-original-article-buffer
9379           (save-restriction
9380             (message-narrow-to-head)
9381             (setq head (buffer-string))
9382             (goto-char (point-min))
9383             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9384               (goto-char (point-max))
9385               (widen)
9386               (setq lines (1- (count-lines (point) (point-max))))))
9387           (with-temp-buffer
9388             (insert (format "211 %d Article retrieved.\n"
9389                             (cdr gnus-article-current)))
9390             (insert head)
9391             (if lines (insert (format "Lines: %d\n" lines)))
9392             (insert ".\n")
9393             (let ((nntp-server-buffer (current-buffer)))
9394               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9395         (gnus-data-set-header
9396          (gnus-data-find (cdr gnus-article-current))
9397          header)
9398         (gnus-summary-update-article-line
9399          (cdr gnus-article-current) header)
9400         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9401           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9402    ((not arg)
9403     ;; Select the article the normal way.
9404     (gnus-summary-select-article nil 'force))
9405    (t
9406     ;; We have to require this here to make sure that the following
9407     ;; dynamic binding isn't shadowed by autoloading.
9408     (require 'gnus-async)
9409     (require 'gnus-art)
9410     ;; Bind the article treatment functions to nil.
9411     (let ((gnus-have-all-headers t)
9412           gnus-article-prepare-hook
9413           gnus-article-decode-hook
9414           gnus-display-mime-function
9415           gnus-break-pages)
9416       ;; Destroy any MIME parts.
9417       (when (gnus-buffer-live-p gnus-article-buffer)
9418         (with-current-buffer gnus-article-buffer
9419           (mm-destroy-parts gnus-article-mime-handles)
9420           ;; Set it to nil for safety reason.
9421           (setq gnus-article-mime-handle-alist nil)
9422           (setq gnus-article-mime-handles nil)))
9423       (gnus-summary-select-article nil 'force))))
9424   (gnus-summary-goto-subject gnus-current-article)
9425   (gnus-summary-position-point))
9426
9427 (defun gnus-summary-show-raw-article ()
9428   "Show the raw article without any article massaging functions being run."
9429   (interactive)
9430   (gnus-summary-show-article t))
9431
9432 (defun gnus-summary-verbose-headers (&optional arg)
9433   "Toggle permanent full header display.
9434 If ARG is a positive number, turn header display on.
9435 If ARG is a negative number, turn header display off."
9436   (interactive "P")
9437   (setq gnus-show-all-headers
9438         (cond ((or (not (numberp arg))
9439                    (zerop arg))
9440                (not gnus-show-all-headers))
9441               ((natnump arg)
9442                t)))
9443   (gnus-summary-show-article))
9444
9445 (defun gnus-summary-toggle-header (&optional arg)
9446   "Show the headers if they are hidden, or hide them if they are shown.
9447 If ARG is a positive number, show the entire header.
9448 If ARG is a negative number, hide the unwanted header lines."
9449   (interactive "P")
9450   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9451                      (get-buffer-window gnus-article-buffer t))))
9452     (with-current-buffer gnus-article-buffer
9453       (widen)
9454       (article-narrow-to-head)
9455       (let* ((inhibit-read-only t)
9456              (inhibit-point-motion-hooks t)
9457              (hidden (if (numberp arg)
9458                          (>= arg 0)
9459                        (or (not (looking-at "[^ \t\n]+:"))
9460                            (gnus-article-hidden-text-p 'headers))))
9461              s e)
9462         (delete-region (point-min) (point-max))
9463         (with-current-buffer gnus-original-article-buffer
9464           (goto-char (setq s (point-min)))
9465           (setq e (if (search-forward "\n\n" nil t)
9466                       (1- (point))
9467                     (point-max))))
9468         (insert-buffer-substring gnus-original-article-buffer s e)
9469         (run-hooks 'gnus-article-decode-hook)
9470         (if hidden
9471             (let ((gnus-treat-hide-headers nil)
9472                   (gnus-treat-hide-boring-headers nil))
9473               (gnus-delete-wash-type 'headers)
9474               (gnus-treat-article 'head))
9475           (gnus-treat-article 'head))
9476         (widen)
9477         (if window
9478             (set-window-start window (goto-char (point-min))))
9479         (if gnus-break-pages
9480             (gnus-narrow-to-page)
9481           (when (gnus-visual-p 'page-marker)
9482             (let ((inhibit-read-only t))
9483               (gnus-remove-text-with-property 'gnus-prev)
9484               (gnus-remove-text-with-property 'gnus-next))))
9485         (gnus-set-mode-line 'article)))))
9486
9487 (defun gnus-summary-show-all-headers ()
9488   "Make all header lines visible."
9489   (interactive)
9490   (gnus-summary-toggle-header 1))
9491
9492 (defun gnus-summary-caesar-message (&optional arg)
9493   "Caesar rotate the current article by 13.
9494 With a non-numerical prefix, also rotate headers.  A numerical
9495 prefix specifies how many places to rotate each letter forward."
9496   (interactive "P")
9497   (gnus-summary-select-article)
9498   (let ((mail-header-separator ""))
9499     (gnus-eval-in-buffer-window gnus-article-buffer
9500       (save-restriction
9501         (widen)
9502         (let ((start (window-start))
9503               (inhibit-read-only t))
9504           (if (equal arg '(4))
9505               (message-caesar-buffer-body nil t)
9506             (message-caesar-buffer-body arg))
9507           (set-window-start (get-buffer-window (current-buffer)) start)))))
9508   ;; Create buttons and stuff...
9509   (gnus-treat-article nil))
9510
9511 (declare-function idna-to-unicode "ext:idna" (str))
9512
9513 (defun gnus-summary-idna-message (&optional arg)
9514   "Decode IDNA encoded domain names in the current articles.
9515 IDNA encoded domain names looks like `xn--bar'.  If a string
9516 remain unencoded after running this function, it is likely an
9517 invalid IDNA string (`xn--bar' is invalid).
9518
9519 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9520 installed for this command to work."
9521   (interactive "P")
9522   (if (not (and (condition-case nil (require 'idna)
9523                   (file-error))
9524                 (mm-coding-system-p 'utf-8)
9525                 (executable-find (symbol-value 'idna-program))))
9526       (gnus-message
9527        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9528     (gnus-summary-select-article)
9529     (let ((mail-header-separator ""))
9530       (gnus-eval-in-buffer-window gnus-article-buffer
9531         (save-restriction
9532           (widen)
9533           (let ((start (window-start))
9534                 buffer-read-only)
9535             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9536               (replace-match (idna-to-unicode (match-string 1))))
9537             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9538
9539 (defun gnus-summary-morse-message (&optional arg)
9540   "Morse decode the current article."
9541   (interactive "P")
9542   (gnus-summary-select-article)
9543   (let ((mail-header-separator ""))
9544     (gnus-eval-in-buffer-window gnus-article-buffer
9545       (save-excursion
9546         (save-restriction
9547           (widen)
9548           (let ((pos (window-start))
9549                 (inhibit-read-only t))
9550             (goto-char (point-min))
9551             (when (message-goto-body)
9552               (gnus-narrow-to-body))
9553             (goto-char (point-min))
9554             (while (search-forward "·" (point-max) t)
9555               (replace-match "."))
9556             (unmorse-region (point-min) (point-max))
9557             (widen)
9558             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9559
9560 (defun gnus-summary-stop-page-breaking ()
9561   "Stop page breaking in the current article."
9562   (interactive)
9563   (gnus-summary-select-article)
9564   (gnus-eval-in-buffer-window gnus-article-buffer
9565     (widen)
9566     (when (gnus-visual-p 'page-marker)
9567       (let ((inhibit-read-only t))
9568         (gnus-remove-text-with-property 'gnus-prev)
9569         (gnus-remove-text-with-property 'gnus-next))
9570       (setq gnus-page-broken nil))))
9571
9572 (defun gnus-summary-move-article (&optional n to-newsgroup
9573                                             select-method action)
9574   "Move the current article to a different newsgroup.
9575 If N is a positive number, move the N next articles.
9576 If N is a negative number, move the N previous articles.
9577 If N is nil and any articles have been marked with the process mark,
9578 move those articles instead.
9579 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9580 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9581 re-spool using this method.
9582
9583 When called interactively with TO-NEWSGROUP being nil, the value of
9584 the variable `gnus-move-split-methods' is used for finding a default
9585 for the target newsgroup.
9586
9587 For this function to work, both the current newsgroup and the
9588 newsgroup that you want to move to have to support the `request-move'
9589 and `request-accept' functions.
9590
9591 ACTION can be either `move' (the default), `crosspost' or `copy'."
9592   (interactive "P")
9593   (unless action
9594     (setq action 'move))
9595   ;; Check whether the source group supports the required functions.
9596   (cond ((and (eq action 'move)
9597               (not (gnus-check-backend-function
9598                     'request-move-article gnus-newsgroup-name)))
9599          (error "The current group does not support article moving"))
9600         ((and (eq action 'crosspost)
9601               (not (gnus-check-backend-function
9602                     'request-replace-article gnus-newsgroup-name)))
9603          (error "The current group does not support article editing")))
9604   (let ((articles (gnus-summary-work-articles n))
9605         (prefix (if (gnus-check-backend-function
9606                      'request-move-article gnus-newsgroup-name)
9607                     (funcall gnus-move-group-prefix-function
9608                              gnus-newsgroup-name)
9609                   ""))
9610         (names '((move "Move" "Moving")
9611                  (copy "Copy" "Copying")
9612                  (crosspost "Crosspost" "Crossposting")))
9613         (copy-buf (save-excursion
9614                     (nnheader-set-temp-buffer " *copy article*")))
9615         art-group to-method new-xref article to-groups
9616         articles-to-update-marks encoded)
9617     (unless (assq action names)
9618       (error "Unknown action %s" action))
9619     ;; Read the newsgroup name.
9620     (when (and (not to-newsgroup)
9621                (not select-method))
9622       (if (and gnus-move-split-methods
9623                (not
9624                 (and (memq gnus-current-article articles)
9625                      (gnus-buffer-live-p gnus-original-article-buffer))))
9626           ;; When `gnus-move-split-methods' is non-nil, we have to
9627           ;; select an article to give `gnus-read-move-group-name' an
9628           ;; opportunity to suggest an appropriate default.  However,
9629           ;; we needn't render or mark the article.
9630           (let ((gnus-display-mime-function nil)
9631                 (gnus-article-prepare-hook nil)
9632                 (gnus-mark-article-hook nil))
9633             (gnus-summary-select-article nil nil nil (car articles))))
9634       (setq to-newsgroup (gnus-read-move-group-name
9635                           (cadr (assq action names))
9636                           (symbol-value
9637                            (intern (format "gnus-current-%s-group" action)))
9638                           articles prefix)
9639             encoded to-newsgroup
9640             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9641       (set (intern (format "gnus-current-%s-group" action))
9642            (mm-decode-coding-string
9643             to-newsgroup
9644             (gnus-group-name-charset to-method to-newsgroup))))
9645     (unless to-method
9646       (setq to-method (or select-method
9647                           (gnus-server-to-method
9648                            (gnus-group-method to-newsgroup)))))
9649     (setq to-newsgroup
9650           (or encoded
9651               (and to-newsgroup
9652                    (mm-encode-coding-string
9653                     to-newsgroup
9654                     (gnus-group-name-charset to-method to-newsgroup)))))
9655     ;; Check the method we are to move this article to...
9656     (unless (gnus-check-backend-function
9657              'request-accept-article (car to-method))
9658       (error "%s does not support article copying" (car to-method)))
9659     (unless (gnus-check-server to-method)
9660       (error "Can't open server %s" (car to-method)))
9661     (gnus-message 6 "%s to %s: %s..."
9662                   (caddr (assq action names))
9663                   (or (car select-method)
9664                       (gnus-group-decoded-name to-newsgroup))
9665                   articles)
9666     (while articles
9667       (setq article (pop articles))
9668       (setq
9669        art-group
9670        (cond
9671         ;; Move the article.
9672         ((eq action 'move)
9673          ;; Remove this article from future suppression.
9674          (gnus-dup-unsuppress-article article)
9675          (let* ((from-method (gnus-find-method-for-group
9676                               gnus-newsgroup-name))
9677                 (to-method (or select-method
9678                                (gnus-find-method-for-group to-newsgroup)))
9679                 (move-is-internal (gnus-method-equal from-method to-method)))
9680            (gnus-request-move-article
9681             article                     ; Article to move
9682             gnus-newsgroup-name         ; From newsgroup
9683             (nth 1 (gnus-find-method-for-group
9684                     gnus-newsgroup-name)) ; Server
9685             (list 'gnus-request-accept-article
9686                   to-newsgroup (list 'quote select-method)
9687                   (not articles) t)     ; Accept form
9688             (not articles)              ; Only save nov last time
9689             move-is-internal)))         ; is this move internal?
9690         ;; Copy the article.
9691         ((eq action 'copy)
9692          (with-current-buffer copy-buf
9693            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9694              (save-restriction
9695                (nnheader-narrow-to-headers)
9696                (dolist (hdr gnus-copy-article-ignored-headers)
9697                  (message-remove-header hdr t)))
9698              (gnus-request-accept-article
9699               to-newsgroup select-method (not articles) t))))
9700         ;; Crosspost the article.
9701         ((eq action 'crosspost)
9702          (let ((xref (message-tokenize-header
9703                       (mail-header-xref (gnus-summary-article-header article))
9704                       " ")))
9705            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9706                                   ":" (number-to-string article)))
9707            (unless xref
9708              (setq xref (list (system-name))))
9709            (setq new-xref
9710                  (concat
9711                   (mapconcat 'identity
9712                              (delete "Xref:" (delete new-xref xref))
9713                              " ")
9714                   " " new-xref))
9715            (with-current-buffer copy-buf
9716              ;; First put the article in the destination group.
9717              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9718              (when (consp (setq art-group
9719                                 (gnus-request-accept-article
9720                                  to-newsgroup select-method (not articles) t)))
9721                (setq new-xref (concat new-xref " " (car art-group)
9722                                       ":"
9723                                       (number-to-string (cdr art-group))))
9724                ;; Now we have the new Xrefs header, so we insert
9725                ;; it and replace the new article.
9726                (nnheader-replace-header "Xref" new-xref)
9727                (gnus-request-replace-article
9728                 (cdr art-group) to-newsgroup (current-buffer) t)
9729                art-group))))))
9730       (cond
9731        ((not art-group)
9732         (gnus-message 1 "Couldn't %s article %s: %s"
9733                       (cadr (assq action names)) article
9734                       (nnheader-get-report (car to-method))))
9735        ((eq art-group 'junk)
9736         (when (eq action 'move)
9737           (gnus-summary-mark-article article gnus-canceled-mark)
9738           (gnus-message 4 "Deleted article %s" article)
9739           ;; run the delete hook
9740           (run-hook-with-args 'gnus-summary-article-delete-hook
9741                               action
9742                               (gnus-data-header
9743                                (assoc article (gnus-data-list nil)))
9744                               gnus-newsgroup-name nil
9745                               select-method)))
9746        (t
9747         (let* ((pto-group (gnus-group-prefixed-name
9748                            (car art-group) to-method))
9749                (info (gnus-get-info pto-group))
9750                (to-group (gnus-info-group info))
9751                to-marks)
9752           ;; Update the group that has been moved to.
9753           (when (and info
9754                      (memq action '(move copy)))
9755             (unless (member to-group to-groups)
9756               (push to-group to-groups))
9757
9758             (unless (memq article gnus-newsgroup-unreads)
9759               (push 'read to-marks)
9760               (gnus-info-set-read
9761                info (gnus-add-to-range (gnus-info-read info)
9762                                        (list (cdr art-group)))))
9763
9764             ;; See whether the article is to be put in the cache.
9765             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9766                    (marks (if expirable
9767                               gnus-article-mark-lists
9768                             (delete '(expirable . expire)
9769                                     (copy-sequence gnus-article-mark-lists))))
9770                    (to-article (cdr art-group)))
9771
9772               ;; Enter the article into the cache in the new group,
9773               ;; if that is required.
9774               (when gnus-use-cache
9775                 (gnus-cache-possibly-enter-article
9776                  to-group to-article
9777                  (memq article gnus-newsgroup-marked)
9778                  (memq article gnus-newsgroup-dormant)
9779                  (memq article gnus-newsgroup-unreads)))
9780
9781               (when gnus-preserve-marks
9782                 ;; Copy any marks over to the new group.
9783                 (when (and (equal to-group gnus-newsgroup-name)
9784                            (not (memq article gnus-newsgroup-unreads)))
9785                   ;; Mark this article as read in this group.
9786                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9787                   (setcdr (gnus-active to-group) to-article)
9788                   (setcdr gnus-newsgroup-active to-article))
9789
9790                 (while marks
9791                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9792                     (when (memq article (symbol-value
9793                                          (intern (format "gnus-newsgroup-%s"
9794                                                          (caar marks)))))
9795                       (push (cdar marks) to-marks)
9796                       ;; If the other group is the same as this group,
9797                       ;; then we have to add the mark to the list.
9798                       (when (equal to-group gnus-newsgroup-name)
9799                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9800                              (cons to-article
9801                                    (symbol-value
9802                                     (intern (format "gnus-newsgroup-%s"
9803                                                     (caar marks)))))))
9804                       ;; Copy the marks to other group.
9805                       (gnus-add-marked-articles
9806                        to-group (cdar marks) (list to-article) info)))
9807                   (setq marks (cdr marks)))
9808
9809                 (when (and expirable
9810                            gnus-mark-copied-or-moved-articles-as-expirable
9811                            (not (memq 'expire to-marks)))
9812                   ;; Mark this article as expirable.
9813                   (push 'expire to-marks)
9814                   (when (equal to-group gnus-newsgroup-name)
9815                     (push to-article gnus-newsgroup-expirable))
9816                   ;; Copy the expirable mark to other group.
9817                   (gnus-add-marked-articles
9818                    to-group 'expire (list to-article) info))
9819
9820                 (gnus-request-set-mark
9821                  to-group (list (list (list to-article) 'add to-marks))))
9822
9823               (gnus-dribble-enter
9824                (concat "(gnus-group-set-info '"
9825                        (gnus-prin1-to-string (gnus-get-info to-group))
9826                        ")"))))
9827
9828           ;; Update the Xref header in this article to point to
9829           ;; the new crossposted article we have just created.
9830           (when (eq action 'crosspost)
9831             (with-current-buffer copy-buf
9832               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9833               (nnheader-replace-header "Xref" new-xref)
9834               (gnus-request-replace-article
9835                article gnus-newsgroup-name (current-buffer) t)))
9836
9837           ;; run the move/copy/crosspost/respool hook
9838           (run-hook-with-args 'gnus-summary-article-move-hook
9839                               action
9840                               (gnus-data-header
9841                                (assoc article (gnus-data-list nil)))
9842                               gnus-newsgroup-name
9843                               to-newsgroup
9844                               select-method))
9845
9846         ;;;!!!Why is this necessary?
9847         (set-buffer gnus-summary-buffer)
9848
9849         (when (eq action 'move)
9850           (save-excursion
9851             (gnus-summary-goto-subject article)
9852             (gnus-summary-mark-article article gnus-canceled-mark)))))
9853       (push article articles-to-update-marks))
9854
9855     (save-excursion
9856       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9857     ;; Re-activate all groups that have been moved to.
9858     (with-current-buffer gnus-group-buffer
9859       (let ((gnus-group-marked to-groups))
9860         (gnus-group-get-new-news-this-group nil t)))
9861
9862     (gnus-kill-buffer copy-buf)
9863     (gnus-summary-position-point)
9864     (gnus-set-mode-line 'summary)))
9865
9866 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9867   "Copy the current article to some other group.
9868 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9869 When called interactively, if TO-NEWSGROUP is nil, use the value of
9870 the variable `gnus-move-split-methods' for finding a default target
9871 newsgroup.
9872 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9873 re-spool using this method."
9874   (interactive "P")
9875   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9876
9877 (defun gnus-summary-crosspost-article (&optional n)
9878   "Crosspost the current article to some other group."
9879   (interactive "P")
9880   (gnus-summary-move-article n nil nil 'crosspost))
9881
9882 (defcustom gnus-summary-respool-default-method nil
9883   "Default method type for respooling an article.
9884 If nil, use to the current newsgroup method."
9885   :type 'symbol
9886   :group 'gnus-summary-mail)
9887
9888 (defun gnus-summary-respool-article (&optional n method)
9889   "Respool the current article.
9890 The article will be squeezed through the mail spooling process again,
9891 which means that it will be put in some mail newsgroup or other
9892 depending on `nnmail-split-methods'.
9893 If N is a positive number, respool the N next articles.
9894 If N is a negative number, respool the N previous articles.
9895 If N is nil and any articles have been marked with the process mark,
9896 respool those articles instead.
9897
9898 Respooling can be done both from mail groups and \"real\" newsgroups.
9899 In the former case, the articles in question will be moved from the
9900 current group into whatever groups they are destined to.  In the
9901 latter case, they will be copied into the relevant groups."
9902   (interactive
9903    (list current-prefix-arg
9904          (let* ((methods (gnus-methods-using 'respool))
9905                 (methname
9906                  (symbol-name (or gnus-summary-respool-default-method
9907                                   (car (gnus-find-method-for-group
9908                                         gnus-newsgroup-name)))))
9909                 (method
9910                  (gnus-completing-read-with-default
9911                   methname "Backend to use when respooling"
9912                   methods nil t nil 'gnus-mail-method-history))
9913                 ms)
9914            (cond
9915             ((zerop (length (setq ms (gnus-servers-using-backend
9916                                       (intern method)))))
9917              (list (intern method) ""))
9918             ((= 1 (length ms))
9919              (car ms))
9920             (t
9921              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9922                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9923                            ms-alist))))))))
9924   (unless method
9925     (error "No method given for respooling"))
9926   (if (assoc (symbol-name
9927               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9928              (gnus-methods-using 'respool))
9929       (gnus-summary-move-article n nil method)
9930     (gnus-summary-copy-article n nil method)))
9931
9932 (defun gnus-summary-import-article (file &optional edit)
9933   "Import an arbitrary file into a mail newsgroup."
9934   (interactive "fImport file: \nP")
9935   (let ((group gnus-newsgroup-name)
9936         (now (current-time))
9937         atts lines group-art)
9938     (unless (gnus-check-backend-function 'request-accept-article group)
9939       (error "%s does not support article importing" group))
9940     (or (file-readable-p file)
9941         (not (file-regular-p file))
9942         (error "Can't read %s" file))
9943     (with-current-buffer (gnus-get-buffer-create " *import file*")
9944       (erase-buffer)
9945       (nnheader-insert-file-contents file)
9946       (goto-char (point-min))
9947       (if (nnheader-article-p)
9948           (save-restriction
9949             (goto-char (point-min))
9950             (search-forward "\n\n" nil t)
9951             (narrow-to-region (point-min) (1- (point)))
9952             (goto-char (point-min))
9953             (unless (re-search-forward "^date:" nil t)
9954               (goto-char (point-max))
9955               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9956        ;; This doesn't look like an article, so we fudge some headers.
9957         (setq atts (file-attributes file)
9958               lines (count-lines (point-min) (point-max)))
9959         (insert "From: " (read-string "From: ") "\n"
9960                 "Subject: " (read-string "Subject: ") "\n"
9961                 "Date: " (message-make-date (nth 5 atts)) "\n"
9962                 "Message-ID: " (message-make-message-id) "\n"
9963                 "Lines: " (int-to-string lines) "\n"
9964                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9965       (setq group-art (gnus-request-accept-article group nil t))
9966       (kill-buffer (current-buffer)))
9967     (setq gnus-newsgroup-active (gnus-activate-group group))
9968     (forward-line 1)
9969     (gnus-summary-goto-article (cdr group-art) nil t)
9970     (when edit
9971       (gnus-summary-edit-article))))
9972
9973 (defun gnus-summary-create-article ()
9974   "Create an article in a mail newsgroup."
9975   (interactive)
9976   (let ((group gnus-newsgroup-name)
9977         (now (current-time))
9978         group-art)
9979     (unless (gnus-check-backend-function 'request-accept-article group)
9980       (error "%s does not support article importing" group))
9981     (with-current-buffer (gnus-get-buffer-create " *import file*")
9982       (erase-buffer)
9983       (goto-char (point-min))
9984       ;; This doesn't look like an article, so we fudge some headers.
9985       (insert "From: " (read-string "From: ") "\n"
9986               "Subject: " (read-string "Subject: ") "\n"
9987               "Date: " (message-make-date now) "\n"
9988               "Message-ID: " (message-make-message-id) "\n")
9989       (setq group-art (gnus-request-accept-article group nil t))
9990       (kill-buffer (current-buffer)))
9991     (setq gnus-newsgroup-active (gnus-activate-group group))
9992     (forward-line 1)
9993     (gnus-summary-goto-article (cdr group-art) nil t)
9994     (gnus-summary-edit-article)))
9995
9996 (defun gnus-summary-article-posted-p ()
9997   "Say whether the current (mail) article is available from news as well.
9998 This will be the case if the article has both been mailed and posted."
9999   (interactive)
10000   (let ((id (mail-header-references (gnus-summary-article-header)))
10001         (gnus-override-method (car (gnus-refer-article-methods))))
10002     (if (gnus-request-head id "")
10003         (gnus-message 2 "The current message was found on %s"
10004                       gnus-override-method)
10005       (gnus-message 2 "The current message couldn't be found on %s"
10006                     gnus-override-method)
10007       nil)))
10008
10009 (defun gnus-summary-expire-articles (&optional now)
10010   "Expire all articles that are marked as expirable in the current group."
10011   (interactive)
10012   (when (and (not gnus-group-is-exiting-without-update-p)
10013              (gnus-check-backend-function
10014               'request-expire-articles gnus-newsgroup-name))
10015     ;; This backend supports expiry.
10016     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10017            (expirable (if total
10018                           (progn
10019                             ;; We need to update the info for
10020                             ;; this group for `gnus-list-of-read-articles'
10021                             ;; to give us the right answer.
10022                             (gnus-run-hooks 'gnus-exit-group-hook)
10023                             (gnus-summary-update-info)
10024                             (gnus-list-of-read-articles gnus-newsgroup-name))
10025                         (setq gnus-newsgroup-expirable
10026                               (sort gnus-newsgroup-expirable '<))))
10027            (expiry-wait (if now 'immediate
10028                           (gnus-group-find-parameter
10029                            gnus-newsgroup-name 'expiry-wait)))
10030            (nnmail-expiry-target
10031             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10032                 nnmail-expiry-target))
10033            es)
10034       (when expirable
10035         ;; There are expirable articles in this group, so we run them
10036         ;; through the expiry process.
10037         (gnus-message 6 "Expiring articles...")
10038         (unless (gnus-check-group gnus-newsgroup-name)
10039           (error "Can't open server for %s" gnus-newsgroup-name))
10040         ;; The list of articles that weren't expired is returned.
10041         (save-excursion
10042           (if expiry-wait
10043               (let ((nnmail-expiry-wait-function nil)
10044                     (nnmail-expiry-wait expiry-wait))
10045                 (setq es (gnus-request-expire-articles
10046                           expirable gnus-newsgroup-name)))
10047             (setq es (gnus-request-expire-articles
10048                       expirable gnus-newsgroup-name)))
10049           (unless total
10050             (setq gnus-newsgroup-expirable es))
10051           ;; We go through the old list of expirable, and mark all
10052           ;; really expired articles as nonexistent.
10053           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10054             (let ((gnus-use-cache nil))
10055               (dolist (article expirable)
10056                 (when (and (not (memq article es))
10057                            (gnus-data-find article))
10058                   (gnus-summary-mark-article article gnus-canceled-mark)
10059                   (run-hook-with-args 'gnus-summary-article-expire-hook
10060                                       'delete
10061                                       (gnus-data-header
10062                                        (assoc article (gnus-data-list nil)))
10063                                       gnus-newsgroup-name
10064                                       nil
10065                                       nil))))))
10066         (gnus-message 6 "Expiring articles...done")))))
10067
10068 (defun gnus-summary-expire-articles-now ()
10069   "Expunge all expirable articles in the current group.
10070 This means that *all* articles that are marked as expirable will be
10071 deleted forever, right now."
10072   (interactive)
10073   (or gnus-expert-user
10074       (gnus-yes-or-no-p
10075        "Are you really, really sure you want to delete all expirable messages? ")
10076       (error "Phew!"))
10077   (gnus-summary-expire-articles t))
10078
10079 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10080 (defun gnus-summary-delete-article (&optional n)
10081   "Delete the N next (mail) articles.
10082 This command actually deletes articles.  This is not a marking
10083 command.  The article will disappear forever from your life, never to
10084 return.
10085
10086 If N is negative, delete backwards.
10087 If N is nil and articles have been marked with the process mark,
10088 delete these instead.
10089
10090 If `gnus-novice-user' is non-nil you will be asked for
10091 confirmation before the articles are deleted."
10092   (interactive "P")
10093   (unless (gnus-check-backend-function 'request-expire-articles
10094                                        gnus-newsgroup-name)
10095     (error "The current newsgroup does not support article deletion"))
10096   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10097     (error "Couldn't open server"))
10098   ;; Compute the list of articles to delete.
10099   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10100         (nnmail-expiry-target 'delete)
10101         not-deleted)
10102     (if (and gnus-novice-user
10103              (not (gnus-yes-or-no-p
10104                    (format "Do you really want to delete %s forever? "
10105                            (if (> (length articles) 1)
10106                                (format "these %s articles" (length articles))
10107                              "this article")))))
10108         ()
10109       ;; Delete the articles.
10110       (setq not-deleted (gnus-request-expire-articles
10111                          articles gnus-newsgroup-name 'force))
10112       (save-excursion
10113         (while articles
10114           (gnus-summary-remove-process-mark (car articles))
10115           ;; The backend might not have been able to delete the article
10116           ;; after all.
10117           (unless (memq (car articles) not-deleted)
10118             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10119           (let* ((article (car articles))
10120                  (ghead  (gnus-data-header
10121                           (assoc article (gnus-data-list nil)))))
10122             (run-hook-with-args 'gnus-summary-article-delete-hook
10123                                 'delete ghead gnus-newsgroup-name nil
10124                                 nil))
10125           (setq articles (cdr articles))))
10126       (when not-deleted
10127         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10128     (gnus-summary-position-point)
10129     (gnus-set-mode-line 'summary)
10130     not-deleted))
10131
10132 (defun gnus-summary-edit-article (&optional arg)
10133   "Edit the current article.
10134 This will have permanent effect only in mail groups.
10135 If ARG is nil, edit the decoded articles.
10136 If ARG is 1, edit the raw articles.
10137 If ARG is 2, edit the raw articles even in read-only groups.
10138 If ARG is 3, edit the articles with the current handles.
10139 Otherwise, allow editing of articles even in read-only
10140 groups."
10141   (interactive "P")
10142   (let (force raw current-handles)
10143     (cond
10144      ((null arg))
10145      ((eq arg 1)
10146       (setq raw t))
10147      ((eq arg 2)
10148       (setq raw t
10149             force t))
10150      ((eq arg 3)
10151       (setq current-handles
10152             (and (gnus-buffer-live-p gnus-article-buffer)
10153                  (with-current-buffer gnus-article-buffer
10154                    (prog1
10155                        gnus-article-mime-handles
10156                      (setq gnus-article-mime-handles nil))))))
10157      (t
10158       (setq force t)))
10159     (when (and raw (not force)
10160                (member gnus-newsgroup-name '("nndraft:delayed"
10161                                              "nndraft:drafts"
10162                                              "nndraft:queue")))
10163       (error "Can't edit the raw article in group %s"
10164              gnus-newsgroup-name))
10165     (with-current-buffer gnus-summary-buffer
10166       (let ((mail-parse-charset gnus-newsgroup-charset)
10167             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10168         (gnus-set-global-variables)
10169         (when (and (not force)
10170                    (gnus-group-read-only-p))
10171           (error "The current newsgroup does not support article editing"))
10172         (gnus-summary-show-article t)
10173         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10174           (with-current-buffer gnus-article-buffer
10175             (mm-enable-multibyte)))
10176         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10177             (setq raw t))
10178         (gnus-article-edit-article
10179          (if raw 'ignore
10180            `(lambda ()
10181               (let ((mbl mml-buffer-list))
10182                 (setq mml-buffer-list nil)
10183                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10184                   (mime-to-mml ,'current-handles))
10185                 (let ((mbl1 mml-buffer-list))
10186                   (setq mml-buffer-list mbl)
10187                   (set (make-local-variable 'mml-buffer-list) mbl1))
10188                 (gnus-make-local-hook 'kill-buffer-hook)
10189                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10190          `(lambda (no-highlight)
10191             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10192                   (message-options message-options)
10193                   (message-options-set-recipient)
10194                   (mail-parse-ignored-charsets
10195                    ',gnus-newsgroup-ignored-charsets)
10196                   (rfc2047-header-encoding-alist
10197                    ',(let ((charset (gnus-group-name-charset
10198                                      (gnus-find-method-for-group
10199                                       gnus-newsgroup-name)
10200                                      gnus-newsgroup-name)))
10201                        (append (list (cons "Newsgroups" charset)
10202                                      (cons "Followup-To" charset)
10203                                      (cons "Xref" charset))
10204                                rfc2047-header-encoding-alist))))
10205               ,(if (not raw) '(progn
10206                                 (mml-to-mime)
10207                                 (mml-destroy-buffers)
10208                                 (remove-hook 'kill-buffer-hook
10209                                              'mml-destroy-buffers t)
10210                                 (kill-local-variable 'mml-buffer-list)))
10211               (gnus-summary-edit-article-done
10212                ,(or (mail-header-references gnus-current-headers) "")
10213                ,(gnus-group-read-only-p)
10214                ,gnus-summary-buffer no-highlight))))))))
10215
10216 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10217
10218 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10219                                                  no-highlight)
10220   "Make edits to the current article permanent."
10221   (interactive)
10222   (save-excursion
10223    ;; The buffer restriction contains the entire article if it exists.
10224     (when (article-goto-body)
10225       (let ((lines (count-lines (point) (point-max)))
10226             (length (- (point-max) (point)))
10227             (case-fold-search t)
10228             (body (copy-marker (point))))
10229         (goto-char (point-min))
10230         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10231           (delete-region (match-beginning 1) (match-end 1))
10232           (insert (number-to-string length)))
10233         (goto-char (point-min))
10234         (when (re-search-forward
10235                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10236           (delete-region (match-beginning 1) (match-end 1))
10237           (insert (number-to-string length)))
10238         (goto-char (point-min))
10239         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10240           (delete-region (match-beginning 1) (match-end 1))
10241           (insert (number-to-string lines))))))
10242   ;; Replace the article.
10243   (let ((buf (current-buffer)))
10244     (with-temp-buffer
10245       (insert-buffer-substring buf)
10246
10247       (if (and (not read-only)
10248                (not (gnus-request-replace-article
10249                      (cdr gnus-article-current) (car gnus-article-current)
10250                      (current-buffer) t)))
10251           (error "Couldn't replace article")
10252         ;; Update the summary buffer.
10253         (if (and references
10254                  (equal (message-tokenize-header references " ")
10255                         (message-tokenize-header
10256                          (or (message-fetch-field "references") "") " ")))
10257             ;; We only have to update this line.
10258             (save-excursion
10259               (save-restriction
10260                 (message-narrow-to-head)
10261                 (let ((head (buffer-substring-no-properties
10262                              (point-min) (point-max)))
10263                       header)
10264                   (with-temp-buffer
10265                     (insert (format "211 %d Article retrieved.\n"
10266                                     (cdr gnus-article-current)))
10267                     (insert head)
10268                     (insert ".\n")
10269                     (let ((nntp-server-buffer (current-buffer)))
10270                       (setq header (car (gnus-get-newsgroup-headers
10271                                          nil t))))
10272                     (with-current-buffer gnus-summary-buffer
10273                       (gnus-data-set-header
10274                        (gnus-data-find (cdr gnus-article-current))
10275                        header)
10276                       (gnus-summary-update-article-line
10277                        (cdr gnus-article-current) header)
10278                       (if (gnus-summary-goto-subject
10279                            (cdr gnus-article-current) nil t)
10280                           (gnus-summary-update-secondary-mark
10281                            (cdr gnus-article-current))))))))
10282           ;; Update threads.
10283           (set-buffer (or buffer gnus-summary-buffer))
10284           (gnus-summary-update-article (cdr gnus-article-current))
10285           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10286               (gnus-summary-update-secondary-mark
10287                (cdr gnus-article-current))))
10288         ;; Prettify the article buffer again.
10289         (unless no-highlight
10290           (with-current-buffer gnus-article-buffer
10291             ;;;!!! Fix this -- article should be rehighlighted.
10292             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10293             (set-buffer gnus-original-article-buffer)
10294             (gnus-request-article
10295              (cdr gnus-article-current)
10296              (car gnus-article-current) (current-buffer))))
10297         ;; Prettify the summary buffer line.
10298         (when (gnus-visual-p 'summary-highlight 'highlight)
10299           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10300
10301 (defun gnus-summary-edit-wash (key)
10302   "Perform editing command KEY in the article buffer."
10303   (interactive
10304    (list
10305     (progn
10306       (message "%s" (concat (this-command-keys) "- "))
10307       (read-char))))
10308   (message "")
10309   (gnus-summary-edit-article)
10310   (execute-kbd-macro (concat (this-command-keys) key))
10311   (gnus-article-edit-done))
10312
10313 ;;; Respooling
10314
10315 (defun gnus-summary-respool-query (&optional silent trace)
10316   "Query where the respool algorithm would put this article."
10317   (interactive)
10318   (let (gnus-mark-article-hook)
10319     (gnus-summary-select-article)
10320     (with-current-buffer gnus-original-article-buffer
10321       (let ((groups (nnmail-article-group 'identity trace)))
10322         (unless silent
10323           (if groups
10324               (message "This message would go to %s"
10325                        (mapconcat 'car groups ", "))
10326             (message "This message would go to no groups"))
10327           groups)))))
10328
10329 (defun gnus-summary-respool-trace ()
10330   "Trace where the respool algorithm would put this article.
10331 Display a buffer showing all fancy splitting patterns which matched."
10332   (interactive)
10333   (gnus-summary-respool-query nil t))
10334
10335 ;; Summary marking commands.
10336
10337 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10338   "Mark articles which has the same subject as read, and then select the next.
10339 If UNMARK is positive, remove any kind of mark.
10340 If UNMARK is negative, tick articles."
10341   (interactive "P")
10342   (when unmark
10343     (setq unmark (prefix-numeric-value unmark)))
10344   (let ((count
10345          (gnus-summary-mark-same-subject
10346           (gnus-summary-article-subject) unmark)))
10347     ;; Select next unread article.  If auto-select-same mode, should
10348     ;; select the first unread article.
10349     (gnus-summary-next-article t (and gnus-auto-select-same
10350                                       (gnus-summary-article-subject)))
10351     (gnus-message 7 "%d article%s marked as %s"
10352                   count (if (= count 1) " is" "s are")
10353                   (if unmark "unread" "read"))))
10354
10355 (defun gnus-summary-kill-same-subject (&optional unmark)
10356   "Mark articles which has the same subject as read.
10357 If UNMARK is positive, remove any kind of mark.
10358 If UNMARK is negative, tick articles."
10359   (interactive "P")
10360   (when unmark
10361     (setq unmark (prefix-numeric-value unmark)))
10362   (let ((count
10363          (gnus-summary-mark-same-subject
10364           (gnus-summary-article-subject) unmark)))
10365     ;; If marked as read, go to next unread subject.
10366     (when (null unmark)
10367       ;; Go to next unread subject.
10368       (gnus-summary-next-subject 1 t))
10369     (gnus-message 7 "%d articles are marked as %s"
10370                   count (if unmark "unread" "read"))))
10371
10372 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10373   "Mark articles with same SUBJECT as read, and return marked number.
10374 If optional argument UNMARK is positive, remove any kinds of marks.
10375 If optional argument UNMARK is negative, mark articles as unread instead."
10376   (let ((count 1))
10377     (save-excursion
10378       (cond
10379        ((null unmark)                   ; Mark as read.
10380         (while (and
10381                 (progn
10382                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10383                   (gnus-summary-show-thread) t)
10384                 (gnus-summary-find-subject subject))
10385           (setq count (1+ count))))
10386        ((> unmark 0)                    ; Tick.
10387         (while (and
10388                 (progn
10389                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10390                   (gnus-summary-show-thread) t)
10391                 (gnus-summary-find-subject subject))
10392           (setq count (1+ count))))
10393        (t                               ; Mark as unread.
10394         (while (and
10395                 (progn
10396                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10397                   (gnus-summary-show-thread) t)
10398                 (gnus-summary-find-subject subject))
10399           (setq count (1+ count)))))
10400       (gnus-set-mode-line 'summary)
10401       ;; Return the number of marked articles.
10402       count)))
10403
10404 (defun gnus-summary-mark-as-processable (n &optional unmark)
10405   "Set the process mark on the next N articles.
10406 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10407 the process mark instead.  The difference between N and the actual
10408 number of articles marked is returned."
10409   (interactive "P")
10410   (if (and (null n) (gnus-region-active-p))
10411       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10412     (setq n (prefix-numeric-value n))
10413     (let ((backward (< n 0))
10414           (n (abs n)))
10415       (while (and
10416               (> n 0)
10417               (if unmark
10418                   (gnus-summary-remove-process-mark
10419                    (gnus-summary-article-number))
10420                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10421               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10422         (setq n (1- n)))
10423       (when (/= 0 n)
10424         (gnus-message 7 "No more articles"))
10425       (gnus-summary-recenter)
10426       (gnus-summary-position-point)
10427       n)))
10428
10429 (defun gnus-summary-unmark-as-processable (n)
10430   "Remove the process mark from the next N articles.
10431 If N is negative, unmark backward instead.  The difference between N and
10432 the actual number of articles unmarked is returned."
10433   (interactive "P")
10434   (gnus-summary-mark-as-processable n t))
10435
10436 (defun gnus-summary-unmark-all-processable ()
10437   "Remove the process mark from all articles."
10438   (interactive)
10439   (save-excursion
10440     (while gnus-newsgroup-processable
10441       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10442   (gnus-summary-position-point))
10443
10444 (defun gnus-summary-add-mark (article type)
10445   "Mark ARTICLE with a mark of TYPE."
10446   (let ((vtype (car (assq type gnus-article-mark-lists)))
10447         var)
10448     (if (not vtype)
10449         (error "No such mark type: %s" type)
10450       (setq var (intern (format "gnus-newsgroup-%s" type)))
10451       (set var (cons article (symbol-value var)))
10452       (if (memq type '(processable cached replied forwarded recent saved))
10453           (gnus-summary-update-secondary-mark article)
10454         ;;; !!! This is bogus.  We should find out what primary
10455         ;;; !!! mark we want to set.
10456         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10457
10458 (defun gnus-summary-mark-as-expirable (n)
10459   "Mark N articles forward as expirable.
10460 If N is negative, mark backward instead.  The difference between N and
10461 the actual number of articles marked is returned."
10462   (interactive "p")
10463   (gnus-summary-mark-forward n gnus-expirable-mark))
10464
10465 (defun gnus-summary-mark-as-spam (n)
10466   "Mark N articles forward as spam.
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-spam-mark))
10471
10472 (defun gnus-summary-mark-article-as-replied (article)
10473   "Mark ARTICLE as replied to and update the summary line.
10474 ARTICLE can also be a list of articles."
10475   (interactive (list (gnus-summary-article-number)))
10476   (let ((articles (if (listp article) article (list article))))
10477     (dolist (article articles)
10478       (unless (numberp article)
10479         (error "%s is not a number" article))
10480       (push article gnus-newsgroup-replied)
10481       (let ((inhibit-read-only t))
10482         (when (gnus-summary-goto-subject article nil t)
10483           (gnus-summary-update-secondary-mark article))))))
10484
10485 (defun gnus-summary-mark-article-as-forwarded (article)
10486   "Mark ARTICLE as forwarded and update the summary line.
10487 ARTICLE can also be a list of articles."
10488   (let ((articles (if (listp article) article (list article))))
10489     (dolist (article articles)
10490       (push article gnus-newsgroup-forwarded)
10491       (let ((inhibit-read-only t))
10492         (when (gnus-summary-goto-subject article nil t)
10493           (gnus-summary-update-secondary-mark article))))))
10494
10495 (defun gnus-summary-set-bookmark (article)
10496   "Set a bookmark in current article."
10497   (interactive (list (gnus-summary-article-number)))
10498   (when (or (not (get-buffer gnus-article-buffer))
10499             (not gnus-current-article)
10500             (not gnus-article-current)
10501             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10502     (error "No current article selected"))
10503   ;; Remove old bookmark, if one exists.
10504   (gnus-pull article gnus-newsgroup-bookmarks)
10505   ;; Set the new bookmark, which is on the form
10506   ;; (article-number . line-number-in-body).
10507   (push
10508    (cons article
10509          (with-current-buffer gnus-article-buffer
10510            (count-lines
10511             (min (point)
10512                  (save-excursion
10513                    (article-goto-body)
10514                    (point)))
10515             (point))))
10516    gnus-newsgroup-bookmarks)
10517   (gnus-message 6 "A bookmark has been added to the current article."))
10518
10519 (defun gnus-summary-remove-bookmark (article)
10520   "Remove the bookmark from the current article."
10521   (interactive (list (gnus-summary-article-number)))
10522   ;; Remove old bookmark, if one exists.
10523   (if (not (assq article gnus-newsgroup-bookmarks))
10524       (gnus-message 6 "No bookmark in current article.")
10525     (gnus-pull article gnus-newsgroup-bookmarks)
10526     (gnus-message 6 "Removed bookmark.")))
10527
10528 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10529 (defun gnus-summary-mark-as-dormant (n)
10530   "Mark N articles forward as dormant.
10531 If N is negative, mark backward instead.  The difference between N and
10532 the actual number of articles marked is returned."
10533   (interactive "p")
10534   (gnus-summary-mark-forward n gnus-dormant-mark))
10535
10536 (defun gnus-summary-set-process-mark (article)
10537   "Set the process mark on ARTICLE and update the summary line."
10538   (setq gnus-newsgroup-processable
10539         (cons article
10540               (delq article gnus-newsgroup-processable)))
10541   (when (gnus-summary-goto-subject article)
10542     (gnus-summary-show-thread)
10543     (gnus-summary-goto-subject article)
10544     (gnus-summary-update-secondary-mark article)))
10545
10546 (defun gnus-summary-remove-process-mark (&rest articles)
10547   "Remove the process mark from ARTICLES and update the summary line."
10548   (dolist (article articles)
10549     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10550     (when (gnus-summary-goto-subject article)
10551       (gnus-summary-show-thread)
10552       (gnus-summary-goto-subject article)
10553       (gnus-summary-update-secondary-mark article)))
10554   t)
10555
10556 (defun gnus-summary-set-saved-mark (article)
10557   "Set the process mark on ARTICLE and update the summary line."
10558   (push article gnus-newsgroup-saved)
10559   (when (gnus-summary-goto-subject article)
10560     (gnus-summary-update-secondary-mark article)))
10561
10562 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10563   "Mark N articles as read forwards.
10564 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10565 The difference between N and the actual number of articles marked is
10566 returned.
10567 If NO-EXPIRE, auto-expiry will be inhibited."
10568   (interactive "p")
10569   (gnus-summary-show-thread)
10570   (let ((backward (< n 0))
10571         (gnus-summary-goto-unread
10572          (and gnus-summary-goto-unread
10573               (not (eq gnus-summary-goto-unread 'never))
10574               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10575                                     gnus-ticked-mark gnus-dormant-mark)))))
10576         (n (abs n))
10577         (mark (or mark gnus-del-mark)))
10578     (while (and (> n 0)
10579                 (gnus-summary-mark-article nil mark no-expire)
10580                 (zerop (gnus-summary-next-subject
10581                         (if backward -1 1)
10582                         (and gnus-summary-goto-unread
10583                              (not (eq gnus-summary-goto-unread 'never)))
10584                         t)))
10585       (setq n (1- n)))
10586     (when (/= 0 n)
10587       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10588     (gnus-summary-recenter)
10589     (gnus-summary-position-point)
10590     (gnus-set-mode-line 'summary)
10591     n))
10592
10593 (defun gnus-summary-mark-article-as-read (mark)
10594   "Mark the current article quickly as read with MARK."
10595   (let ((article (gnus-summary-article-number)))
10596     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10597     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10598     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10599     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10600     (push (cons article mark) gnus-newsgroup-reads)
10601     ;; Possibly remove from cache, if that is used.
10602     (when gnus-use-cache
10603       (gnus-cache-enter-remove-article article))
10604     ;; Allow the backend to change the mark.
10605     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10606     ;; Check for auto-expiry.
10607     (when (and gnus-newsgroup-auto-expire
10608                (memq mark gnus-auto-expirable-marks))
10609       (setq mark gnus-expirable-mark)
10610       ;; Let the backend know about the mark change.
10611       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10612       (push article gnus-newsgroup-expirable))
10613     ;; Set the mark in the buffer.
10614     (gnus-summary-update-mark mark 'unread)
10615     t))
10616
10617 (defun gnus-summary-mark-article-as-unread (mark)
10618   "Mark the current article quickly as unread with MARK."
10619   (let* ((article (gnus-summary-article-number))
10620          (old-mark (gnus-summary-article-mark article)))
10621     ;; Allow the backend to change the mark.
10622     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10623     (if (eq mark old-mark)
10624         t
10625       (if (<= article 0)
10626           (progn
10627             (gnus-error 1 "Can't mark negative article numbers")
10628             nil)
10629         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10630         (setq gnus-newsgroup-spam-marked
10631               (delq article gnus-newsgroup-spam-marked))
10632         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10633         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10634         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10635         (cond ((= mark gnus-ticked-mark)
10636                (setq gnus-newsgroup-marked
10637                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10638                                               article)))
10639               ((= mark gnus-spam-mark)
10640                (setq gnus-newsgroup-spam-marked
10641                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10642                                               article)))
10643               ((= mark gnus-dormant-mark)
10644                (setq gnus-newsgroup-dormant
10645                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10646                                               article)))
10647               (t
10648                (setq gnus-newsgroup-unreads
10649                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10650                                               article))))
10651         (gnus-pull article gnus-newsgroup-reads)
10652
10653         ;; See whether the article is to be put in the cache.
10654         (and gnus-use-cache
10655              (vectorp (gnus-summary-article-header article))
10656              (save-excursion
10657                (gnus-cache-possibly-enter-article
10658                 gnus-newsgroup-name article
10659                 (= mark gnus-ticked-mark)
10660                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10661
10662         ;; Fix the mark.
10663         (gnus-summary-update-mark mark 'unread)
10664         t))))
10665
10666 (defun gnus-summary-mark-article (&optional article mark no-expire)
10667   "Mark ARTICLE with MARK.  MARK can be any character.
10668 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10669 `??' (dormant) and `?E' (expirable).
10670 If MARK is nil, then the default character `?r' is used.
10671 If ARTICLE is nil, then the article on the current line will be
10672 marked.
10673 If NO-EXPIRE, auto-expiry will be inhibited."
10674   ;; The mark might be a string.
10675   (when (stringp mark)
10676     (setq mark (aref mark 0)))
10677   ;; If no mark is given, then we check auto-expiring.
10678   (when (null mark)
10679     (setq mark gnus-del-mark))
10680   (when (and (not no-expire)
10681              gnus-newsgroup-auto-expire
10682              (memq mark gnus-auto-expirable-marks))
10683     (setq mark gnus-expirable-mark))
10684   (let ((article (or article (gnus-summary-article-number)))
10685         (old-mark (gnus-summary-article-mark article)))
10686     ;; Allow the backend to change the mark.
10687     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10688     (if (eq mark old-mark)
10689         t
10690       (unless article
10691         (error "No article on current line"))
10692       (if (not (if (or (= mark gnus-unread-mark)
10693                        (= mark gnus-ticked-mark)
10694                        (= mark gnus-spam-mark)
10695                        (= mark gnus-dormant-mark))
10696                    (gnus-mark-article-as-unread article mark)
10697                  (gnus-mark-article-as-read article mark)))
10698           t
10699         ;; See whether the article is to be put in the cache.
10700         (and gnus-use-cache
10701              (not (= mark gnus-canceled-mark))
10702              (vectorp (gnus-summary-article-header article))
10703              (save-excursion
10704                (gnus-cache-possibly-enter-article
10705                 gnus-newsgroup-name article
10706                 (= mark gnus-ticked-mark)
10707                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10708
10709         (when (gnus-summary-goto-subject article nil t)
10710           (let ((inhibit-read-only t))
10711             (gnus-summary-show-thread)
10712             ;; Fix the mark.
10713             (gnus-summary-update-mark mark 'unread)
10714             t))))))
10715
10716 (defun gnus-summary-update-secondary-mark (article)
10717   "Update the secondary (read, process, cache) mark."
10718   (gnus-summary-update-mark
10719    (cond ((memq article gnus-newsgroup-processable)
10720           gnus-process-mark)
10721          ((memq article gnus-newsgroup-cached)
10722           gnus-cached-mark)
10723          ((memq article gnus-newsgroup-replied)
10724           gnus-replied-mark)
10725          ((memq article gnus-newsgroup-forwarded)
10726           gnus-forwarded-mark)
10727          ((memq article gnus-newsgroup-saved)
10728           gnus-saved-mark)
10729          ((memq article gnus-newsgroup-recent)
10730           gnus-recent-mark)
10731          ((memq article gnus-newsgroup-unseen)
10732           gnus-unseen-mark)
10733          (t gnus-no-mark))
10734    'replied)
10735   (when (gnus-visual-p 'summary-highlight 'highlight)
10736     (gnus-run-hooks 'gnus-summary-update-hook))
10737   t)
10738
10739 (defun gnus-summary-update-download-mark (article)
10740   "Update the download mark."
10741   (gnus-summary-update-mark
10742    (cond ((memq article gnus-newsgroup-undownloaded)
10743           gnus-undownloaded-mark)
10744          (gnus-newsgroup-agentized
10745           gnus-downloaded-mark)
10746          (t
10747           gnus-no-mark))
10748    'download)
10749   (gnus-summary-update-line t)
10750   t)
10751
10752 (defun gnus-summary-update-mark (mark type)
10753   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10754         (inhibit-read-only t))
10755     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10756     (when forward
10757       (when (looking-at "\r")
10758         (incf forward))
10759       (when (<= (+ forward (point)) (point-max))
10760         ;; Go to the right position on the line.
10761         (goto-char (+ forward (point)))
10762         ;; Replace the old mark with the new mark.
10763         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10764         ;; Optionally update the marks by some user rule.
10765         (when (eq type 'unread)
10766           (gnus-data-set-mark
10767            (gnus-data-find (gnus-summary-article-number)) mark)
10768           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10769
10770 (defun gnus-mark-article-as-read (article &optional mark)
10771   "Enter ARTICLE in the pertinent lists and remove it from others."
10772   ;; Make the article expirable.
10773   (let ((mark (or mark gnus-del-mark)))
10774     (setq gnus-newsgroup-expirable
10775           (if (= mark gnus-expirable-mark)
10776               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10777             (delq article gnus-newsgroup-expirable)))
10778     ;; Remove from unread and marked lists.
10779     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10780     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10781     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10782     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10783     (push (cons article mark) gnus-newsgroup-reads)
10784     ;; Possibly remove from cache, if that is used.
10785     (when gnus-use-cache
10786       (gnus-cache-enter-remove-article article))
10787     t))
10788
10789 (defun gnus-mark-article-as-unread (article &optional mark)
10790   "Enter ARTICLE in the pertinent lists and remove it from others."
10791   (let ((mark (or mark gnus-ticked-mark)))
10792     (if (<= article 0)
10793         (progn
10794           (gnus-error 1 "Can't mark negative article numbers")
10795           nil)
10796       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10797             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10798             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10799             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10800             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10801
10802       ;; Unsuppress duplicates?
10803       (when gnus-suppress-duplicates
10804         (gnus-dup-unsuppress-article article))
10805
10806       (cond ((= mark gnus-ticked-mark)
10807              (setq gnus-newsgroup-marked
10808                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10809             ((= mark gnus-spam-mark)
10810              (setq gnus-newsgroup-spam-marked
10811                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10812                                             article)))
10813             ((= mark gnus-dormant-mark)
10814              (setq gnus-newsgroup-dormant
10815                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10816             (t
10817              (setq gnus-newsgroup-unreads
10818                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10819       (gnus-pull article gnus-newsgroup-reads)
10820       t)))
10821
10822 (defalias 'gnus-summary-mark-as-unread-forward
10823   'gnus-summary-tick-article-forward)
10824 (make-obsolete 'gnus-summary-mark-as-unread-forward
10825                'gnus-summary-tick-article-forward "Emacs 20.4")
10826 (defun gnus-summary-tick-article-forward (n)
10827   "Tick N articles forwards.
10828 If N is negative, tick backwards instead.
10829 The difference between N and the number of articles ticked is returned."
10830   (interactive "p")
10831   (gnus-summary-mark-forward n gnus-ticked-mark))
10832
10833 (defalias 'gnus-summary-mark-as-unread-backward
10834   'gnus-summary-tick-article-backward)
10835 (make-obsolete 'gnus-summary-mark-as-unread-backward
10836                'gnus-summary-tick-article-backward "Emacs 20.4")
10837 (defun gnus-summary-tick-article-backward (n)
10838   "Tick N articles backwards.
10839 The difference between N and the number of articles ticked is returned."
10840   (interactive "p")
10841   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10842
10843 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10844 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10845 (defun gnus-summary-tick-article (&optional article clear-mark)
10846   "Mark current article as unread.
10847 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10848 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10849   (interactive)
10850   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10851                                        gnus-ticked-mark)))
10852
10853 (defun gnus-summary-mark-as-read-forward (n)
10854   "Mark N articles as read forwards.
10855 If N is negative, mark backwards instead.
10856 The difference between N and the actual number of articles marked is
10857 returned."
10858   (interactive "p")
10859   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10860
10861 (defun gnus-summary-mark-as-read-backward (n)
10862   "Mark the N articles as read backwards.
10863 The difference between N and the actual number of articles marked is
10864 returned."
10865   (interactive "p")
10866   (gnus-summary-mark-forward
10867    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10868
10869 (defun gnus-summary-mark-as-read (&optional article mark)
10870   "Mark current article as read.
10871 ARTICLE specifies the article to be marked as read.
10872 MARK specifies a string to be inserted at the beginning of the line."
10873   (gnus-summary-mark-article article mark))
10874
10875 (defun gnus-summary-clear-mark-forward (n)
10876   "Clear marks from N articles forward.
10877 If N is negative, clear backward instead.
10878 The difference between N and the number of marks cleared is returned."
10879   (interactive "p")
10880   (gnus-summary-mark-forward n gnus-unread-mark))
10881
10882 (defun gnus-summary-clear-mark-backward (n)
10883   "Clear marks from N articles backward.
10884 The difference between N and the number of marks cleared is returned."
10885   (interactive "p")
10886   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10887
10888 (defun gnus-summary-mark-unread-as-read ()
10889   "Intended to be used by `gnus-mark-article-hook'."
10890   (when (memq gnus-current-article gnus-newsgroup-unreads)
10891     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10892
10893 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10894   "Intended to be used by `gnus-mark-article-hook'."
10895   (let ((mark (gnus-summary-article-mark)))
10896     (when (or (gnus-unread-mark-p mark)
10897               (gnus-read-mark-p mark))
10898       (gnus-summary-mark-article gnus-current-article
10899                                  (or new-mark gnus-read-mark)))))
10900
10901 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10902   "Intended to be used by `gnus-mark-article-hook'."
10903   (let ((mark (gnus-summary-article-mark)))
10904     (when (or (gnus-unread-mark-p mark)
10905               (gnus-read-mark-p mark))
10906       (gnus-summary-mark-article (gnus-summary-article-number)
10907                                  (or new-mark gnus-read-mark)))))
10908
10909 (defun gnus-summary-mark-unread-as-ticked ()
10910   "Intended to be used by `gnus-mark-article-hook'."
10911   (when (memq gnus-current-article gnus-newsgroup-unreads)
10912     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10913
10914 (defun gnus-summary-mark-region-as-read (point mark all)
10915   "Mark all unread articles between point and mark as read.
10916 If given a prefix, mark all articles between point and mark as read,
10917 even ticked and dormant ones."
10918   (interactive "r\nP")
10919   (save-excursion
10920     (let (article)
10921       (goto-char point)
10922       (beginning-of-line)
10923       (while (and
10924               (< (point) mark)
10925               (progn
10926                 (when (or all
10927                           (memq (setq article (gnus-summary-article-number))
10928                                 gnus-newsgroup-unreads))
10929                   (gnus-summary-mark-article article gnus-del-mark))
10930                 t)
10931               (gnus-summary-find-next))))))
10932
10933 (defun gnus-summary-mark-below (score mark)
10934   "Mark articles with score less than SCORE with MARK."
10935   (interactive "P\ncMark: ")
10936   (setq score (if score
10937                   (prefix-numeric-value score)
10938                 (or gnus-summary-default-score 0)))
10939   (with-current-buffer gnus-summary-buffer
10940     (goto-char (point-min))
10941     (while
10942         (progn
10943           (and (< (gnus-summary-article-score) score)
10944                (gnus-summary-mark-article nil mark))
10945           (gnus-summary-find-next)))))
10946
10947 (defun gnus-summary-kill-below (&optional score)
10948   "Mark articles with score below SCORE as read."
10949   (interactive "P")
10950   (gnus-summary-mark-below score gnus-killed-mark))
10951
10952 (defun gnus-summary-clear-above (&optional score)
10953   "Clear all marks from articles with score above SCORE."
10954   (interactive "P")
10955   (gnus-summary-mark-above score gnus-unread-mark))
10956
10957 (defun gnus-summary-tick-above (&optional score)
10958   "Tick all articles with score above SCORE."
10959   (interactive "P")
10960   (gnus-summary-mark-above score gnus-ticked-mark))
10961
10962 (defun gnus-summary-mark-above (score mark)
10963   "Mark articles with score over SCORE with MARK."
10964   (interactive "P\ncMark: ")
10965   (setq score (if score
10966                   (prefix-numeric-value score)
10967                 (or gnus-summary-default-score 0)))
10968   (with-current-buffer gnus-summary-buffer
10969     (goto-char (point-min))
10970     (while (and (progn
10971                   (when (> (gnus-summary-article-score) score)
10972                     (gnus-summary-mark-article nil mark))
10973                   t)
10974                 (gnus-summary-find-next)))))
10975
10976 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10977 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10978 (defun gnus-summary-limit-include-expunged (&optional no-error)
10979   "Display all the hidden articles that were expunged for low scores."
10980   (interactive)
10981   (let ((inhibit-read-only t))
10982     (let ((scored gnus-newsgroup-scored)
10983           headers h)
10984       (while scored
10985         (unless (gnus-summary-article-header (caar scored))
10986           (and (setq h (gnus-number-to-header (caar scored)))
10987                (< (cdar scored) gnus-summary-expunge-below)
10988                (push h headers)))
10989         (setq scored (cdr scored)))
10990       (if (not headers)
10991           (when (not no-error)
10992             (error "No expunged articles hidden"))
10993         (goto-char (point-min))
10994         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10995         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10996         (dolist (x headers)
10997           (push (mail-header-number x) gnus-newsgroup-limit))
10998         (gnus-summary-prepare-unthreaded (nreverse headers))
10999         (goto-char (point-min))
11000         (gnus-summary-position-point)
11001         t))))
11002
11003 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11004   "Mark all unread articles in this newsgroup as read.
11005 If prefix argument ALL is non-nil, ticked and dormant articles will
11006 also be marked as read.
11007 If QUIETLY is non-nil, no questions will be asked.
11008
11009 If TO-HERE is non-nil, it should be a point in the buffer.  All
11010 articles before (after, if REVERSE is set) this point will be marked
11011 as read.
11012
11013 Note that this function will only catch up the unread article
11014 in the current summary buffer limitation.
11015
11016 The number of articles marked as read is returned."
11017   (interactive "P")
11018   (prog1
11019       (save-excursion
11020         (when (or quietly
11021                   (not gnus-interactive-catchup) ;Without confirmation?
11022                   gnus-expert-user
11023                   (gnus-y-or-n-p
11024                    (if all
11025                        "Mark absolutely all articles as read? "
11026                      "Mark all unread articles as read? ")))
11027           (if (and not-mark
11028                    (not gnus-newsgroup-adaptive)
11029                    (not gnus-newsgroup-auto-expire)
11030                    (not gnus-suppress-duplicates)
11031                    (or (not gnus-use-cache)
11032                        (eq gnus-use-cache 'passive)))
11033               (progn
11034                 (when all
11035                   (setq gnus-newsgroup-marked nil
11036                         gnus-newsgroup-spam-marked nil
11037                         gnus-newsgroup-dormant nil))
11038                 (setq gnus-newsgroup-unreads
11039                       (gnus-sorted-nunion
11040                        (gnus-sorted-intersection gnus-newsgroup-unreads
11041                                                  gnus-newsgroup-downloadable)
11042                        (gnus-sorted-difference gnus-newsgroup-unfetched
11043                                                gnus-newsgroup-cached))))
11044             ;; We actually mark all articles as canceled, which we
11045             ;; have to do when using auto-expiry or adaptive scoring.
11046             (gnus-summary-show-all-threads)
11047             (if (and to-here reverse)
11048                 (progn
11049                   (goto-char to-here)
11050                   (gnus-summary-mark-current-read-and-unread-as-read
11051                    gnus-catchup-mark)
11052                   (while (gnus-summary-find-next (not all))
11053                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11054               (when (gnus-summary-first-subject (not all))
11055                 (while (and
11056                         (if to-here (< (point) to-here) t)
11057                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11058                         (gnus-summary-find-next (not all))))))
11059             (gnus-set-mode-line 'summary))
11060           t))
11061     (gnus-summary-position-point)))
11062
11063 (defun gnus-summary-catchup-to-here (&optional all)
11064   "Mark all unticked articles before the current one as read.
11065 If ALL is non-nil, also mark ticked and dormant articles as read."
11066   (interactive "P")
11067   (save-excursion
11068     (gnus-save-hidden-threads
11069       (let ((beg (point)))
11070         ;; We check that there are unread articles.
11071         (when (or all (gnus-summary-find-prev))
11072           (gnus-summary-catchup all t beg)))))
11073   (gnus-summary-position-point))
11074
11075 (defun gnus-summary-catchup-from-here (&optional all)
11076   "Mark all unticked articles after (and including) the current one as read.
11077 If ALL is non-nil, also mark ticked and dormant articles as read."
11078   (interactive "P")
11079   (save-excursion
11080     (gnus-save-hidden-threads
11081       (let ((beg (point)))
11082         ;; We check that there are unread articles.
11083         (when (or all (gnus-summary-find-next))
11084           (gnus-summary-catchup all t beg nil t)))))
11085   (gnus-summary-position-point))
11086
11087 (defun gnus-summary-catchup-all (&optional quietly)
11088   "Mark all articles in this newsgroup as read.
11089 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11090 instead, which marks only unread articles as read."
11091   (interactive "P")
11092   (gnus-summary-catchup t quietly))
11093
11094 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11095   "Mark all unread articles in this group as read, then exit.
11096 If prefix argument ALL is non-nil, all articles are marked as read.
11097 If QUIETLY is non-nil, no questions will be asked."
11098   (interactive "P")
11099   (when (gnus-summary-catchup all quietly nil 'fast)
11100     ;; Select next newsgroup or exit.
11101     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11102              (eq gnus-auto-select-next 'quietly))
11103         (gnus-summary-next-group nil)
11104       (gnus-summary-exit))))
11105
11106 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11107   "Mark all articles in this newsgroup as read, and then exit.
11108 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11109 instead, which marks only unread articles as read."
11110   (interactive "P")
11111   (gnus-summary-catchup-and-exit t quietly))
11112
11113 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11114   "Mark all articles in this group as read and select the next group.
11115 If given a prefix, mark all articles, unread as well as ticked, as
11116 read."
11117   (interactive "P")
11118   (save-excursion
11119     (gnus-summary-catchup all))
11120   (gnus-summary-next-group))
11121
11122 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11123   "Mark all articles in this group as read and select the previous group.
11124 If given a prefix, mark all articles, unread as well as ticked, as
11125 read."
11126   (interactive "P")
11127   (save-excursion
11128     (gnus-summary-catchup all))
11129   (gnus-summary-next-group nil nil t))
11130
11131 ;;;
11132 ;;; with article
11133 ;;;
11134
11135 (defmacro gnus-with-article (article &rest forms)
11136   "Select ARTICLE and perform FORMS in the original article buffer.
11137 Then replace the article with the result."
11138   `(progn
11139      ;; We don't want the article to be marked as read.
11140      (let (gnus-mark-article-hook)
11141        (gnus-summary-select-article t t nil ,article))
11142      (set-buffer gnus-original-article-buffer)
11143      ,@forms
11144      (if (not (gnus-check-backend-function
11145                'request-replace-article (car gnus-article-current)))
11146          (gnus-message 5 "Read-only group; not replacing")
11147        (unless (gnus-request-replace-article
11148                 ,article (car gnus-article-current)
11149                 (current-buffer) t)
11150          (error "Couldn't replace article")))
11151      ;; The cache and backlog have to be flushed somewhat.
11152      (when gnus-keep-backlog
11153        (gnus-backlog-remove-article
11154         (car gnus-article-current) (cdr gnus-article-current)))
11155      (when gnus-use-cache
11156        (gnus-cache-update-article
11157         (car gnus-article-current) (cdr gnus-article-current)))))
11158
11159 (put 'gnus-with-article 'lisp-indent-function 1)
11160 (put 'gnus-with-article 'edebug-form-spec '(form body))
11161
11162 ;; Thread-based commands.
11163
11164 (defun gnus-summary-articles-in-thread (&optional article)
11165   "Return a list of all articles in the current thread.
11166 If ARTICLE is non-nil, return all articles in the thread that starts
11167 with that article."
11168   (let* ((article (or article (gnus-summary-article-number)))
11169          (data (gnus-data-find-list article))
11170          (top-level (gnus-data-level (car data)))
11171          (top-subject
11172           (cond ((null gnus-thread-operation-ignore-subject)
11173                  (gnus-simplify-subject-re
11174                   (mail-header-subject (gnus-data-header (car data)))))
11175                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11176                  (gnus-simplify-subject-fuzzy
11177                   (mail-header-subject (gnus-data-header (car data)))))
11178                 (t nil)))
11179          (end-point (save-excursion
11180                       (if (gnus-summary-go-to-next-thread)
11181                           (point) (point-max))))
11182          articles)
11183     (while (and data
11184                 (< (gnus-data-pos (car data)) end-point))
11185       (when (or (not top-subject)
11186                 (string= top-subject
11187                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11188                              (gnus-simplify-subject-fuzzy
11189                               (mail-header-subject
11190                                (gnus-data-header (car data))))
11191                            (gnus-simplify-subject-re
11192                             (mail-header-subject
11193                              (gnus-data-header (car data)))))))
11194         (push (gnus-data-number (car data)) articles))
11195       (unless (and (setq data (cdr data))
11196                    (> (gnus-data-level (car data)) top-level))
11197         (setq data nil)))
11198     ;; Return the list of articles.
11199     (nreverse articles)))
11200
11201 (defun gnus-summary-rethread-current ()
11202   "Rethread the thread the current article is part of."
11203   (interactive)
11204   (let* ((gnus-show-threads t)
11205          (article (gnus-summary-article-number))
11206          (id (mail-header-id (gnus-summary-article-header)))
11207          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11208     (unless id
11209       (error "No article on the current line"))
11210     (gnus-rebuild-thread id)
11211     (gnus-summary-goto-subject article)))
11212
11213 (defun gnus-summary-reparent-thread ()
11214   "Make the current article child of the marked (or previous) article.
11215
11216 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11217 is non-nil or the Subject: of both articles are the same."
11218   (interactive)
11219   (unless (not (gnus-group-read-only-p))
11220     (error "The current newsgroup does not support article editing"))
11221   (unless (<= (length gnus-newsgroup-processable) 1)
11222     (error "No more than one article may be marked"))
11223   (let ((child (gnus-summary-article-number))
11224         ;; First grab the marked article, otherwise one line up.
11225         (parent (if (not (null gnus-newsgroup-processable))
11226                     (car gnus-newsgroup-processable)
11227                   (save-excursion
11228                     (if (eq (forward-line -1) 0)
11229                         (gnus-summary-article-number)
11230                       (error "Beginning of summary buffer"))))))
11231     (gnus-summary-reparent-children parent (list child))))
11232
11233 (defun gnus-summary-reparent-children (parent children)
11234   "Make PARENT the parent of CHILDREN.
11235 When called interactively, PARENT is the current article and CHILDREN
11236 are the process-marked articles."
11237   (interactive
11238    (list (gnus-summary-article-number)
11239          (gnus-summary-work-articles nil)))
11240   (dolist (child children)
11241     (save-window-excursion
11242       (let ((gnus-article-buffer " *reparent*"))
11243         (unless (not (eq parent child))
11244           (error "An article may not be self-referential"))
11245         (let ((message-id (mail-header-id
11246                            (gnus-summary-article-header parent))))
11247           (unless (and message-id (not (equal message-id "")))
11248             (error "No message-id in desired parent"))
11249           (gnus-with-article child
11250             (save-restriction
11251               (goto-char (point-min))
11252               (message-narrow-to-head)
11253               (if (re-search-forward "^References: " nil t)
11254                   (progn
11255                     (re-search-forward "^[^ \t]" nil t)
11256                     (forward-line -1)
11257                     (end-of-line)
11258                     (insert " " message-id))
11259                 (insert "References: " message-id "\n"))))
11260           (set-buffer gnus-summary-buffer)
11261           (gnus-summary-unmark-all-processable)
11262           (gnus-summary-update-article child)
11263           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11264             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11265           (gnus-summary-rethread-current)
11266           (gnus-message 3 "Article %d is now the child of article %d"
11267                         child parent))))))
11268
11269 (defun gnus-summary-toggle-threads (&optional arg)
11270   "Toggle showing conversation threads.
11271 If ARG is positive number, turn showing conversation threads on."
11272   (interactive "P")
11273   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11274     (setq gnus-show-threads
11275           (if (null arg) (not gnus-show-threads)
11276             (> (prefix-numeric-value arg) 0)))
11277     (gnus-summary-prepare)
11278     (gnus-summary-goto-subject current)
11279     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11280     (gnus-summary-position-point)))
11281
11282 (eval-and-compile
11283   (if (fboundp 'remove-overlays)
11284       (defalias 'gnus-remove-overlays 'remove-overlays)
11285     (defun gnus-remove-overlays (beg end name val)
11286       "Clear BEG and END of overlays whose property NAME has value VAL.
11287 For compatibility with Emacs 21 and XEmacs."
11288       (dolist (ov (gnus-overlays-in beg end))
11289         (when (eq (gnus-overlay-get ov name) val)
11290           (gnus-delete-overlay ov))))))
11291
11292 (defun gnus-summary-show-all-threads ()
11293   "Show all threads."
11294   (interactive)
11295   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11296   (gnus-summary-position-point))
11297
11298 (defun gnus-summary-show-thread ()
11299   "Show thread subtrees.
11300 Returns nil if no thread was there to be shown."
11301   (interactive)
11302   (let* ((orig (point))
11303          (end (point-at-eol))
11304          ;; Leave point at bol
11305          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11306          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11307                 (if (fboundp 'next-single-char-property-change)
11308                     (or (next-single-char-property-change end 'invisible)
11309                         (point-max))
11310                   (while (progn
11311                            (end-of-line 2)
11312                            (and (not (eobp))
11313                                 (eq (get-char-property (point) 'invisible)
11314                                     'gnus-sum))))
11315                   (point)))))
11316     (when eoi
11317       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11318       (goto-char orig)
11319       (gnus-summary-position-point)
11320       eoi)))
11321
11322 (defun gnus-summary-maybe-hide-threads ()
11323   "If requested, hide the threads that should be hidden."
11324   (when (and gnus-show-threads
11325              gnus-thread-hide-subtree)
11326     (gnus-summary-hide-all-threads
11327      (if (or (consp gnus-thread-hide-subtree)
11328              (functionp gnus-thread-hide-subtree))
11329          (gnus-make-predicate gnus-thread-hide-subtree)
11330        nil))))
11331
11332 ;;; Hiding predicates.
11333
11334 (defun gnus-article-unread-p (header)
11335   (memq (mail-header-number header) gnus-newsgroup-unreads))
11336
11337 (defun gnus-article-unseen-p (header)
11338   (memq (mail-header-number header) gnus-newsgroup-unseen))
11339
11340 (defun gnus-map-articles (predicate articles)
11341   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11342   (apply 'gnus-or (mapcar predicate
11343                           (mapcar (lambda (number)
11344                                     (gnus-summary-article-header number))
11345                                   articles))))
11346
11347 (defun gnus-summary-hide-all-threads (&optional predicate)
11348   "Hide all thread subtrees.
11349 If PREDICATE is supplied, threads that satisfy this predicate
11350 will not be hidden."
11351   (interactive)
11352   (save-excursion
11353     (goto-char (point-min))
11354     (let ((end nil))
11355       (while (not end)
11356         (when (or (not predicate)
11357                   (gnus-map-articles
11358                    predicate (gnus-summary-article-children)))
11359             (gnus-summary-hide-thread))
11360         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11361   (gnus-summary-position-point))
11362
11363 (defun gnus-summary-hide-thread ()
11364   "Hide thread subtrees.
11365 If PREDICATE is supplied, threads that satisfy this predicate
11366 will not be hidden.
11367 Returns nil if no threads were there to be hidden."
11368   (interactive)
11369   (let ((start (point))
11370         (starteol (line-end-position))
11371         (article (gnus-summary-article-number)))
11372     (goto-char start)
11373     ;; Go forward until either the buffer ends or the subthread ends.
11374     (when (and (not (eobp))
11375                (or (zerop (gnus-summary-next-thread 1 t))
11376                    (goto-char (point-max))))
11377       (if (and (> (point) start)
11378                ;; FIXME: this should actually search for a non-invisible \n.
11379                (search-backward "\n" start t))
11380           (progn
11381             (when (> (point) starteol)
11382               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11383               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11384                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11385                 (gnus-overlay-put ol 'evaporate t)))
11386             (gnus-summary-goto-subject article))
11387         (goto-char start)
11388         nil))))
11389
11390 (defun gnus-summary-go-to-next-thread (&optional previous)
11391   "Go to the same level (or less) next thread.
11392 If PREVIOUS is non-nil, go to previous thread instead.
11393 Return the article number moved to, or nil if moving was impossible."
11394   (let ((level (gnus-summary-thread-level))
11395         (way (if previous -1 1))
11396         (beg (point)))
11397     (forward-line way)
11398     (while (and (not (eobp))
11399                 (< level (gnus-summary-thread-level)))
11400       (forward-line way))
11401     (if (eobp)
11402         (progn
11403           (goto-char beg)
11404           nil)
11405       (setq beg (point))
11406       (prog1
11407           (gnus-summary-article-number)
11408         (goto-char beg)))))
11409
11410 (defun gnus-summary-next-thread (n &optional silent)
11411   "Go to the same level next N'th thread.
11412 If N is negative, search backward instead.
11413 Returns the difference between N and the number of skips actually
11414 done.
11415
11416 If SILENT, don't output messages."
11417   (interactive "p")
11418   (let ((backward (< n 0))
11419         (n (abs n)))
11420     (while (and (> n 0)
11421                 (gnus-summary-go-to-next-thread backward))
11422       (decf n))
11423     (unless silent
11424       (gnus-summary-position-point))
11425     (when (and (not silent) (/= 0 n))
11426       (gnus-message 7 "No more threads"))
11427     n))
11428
11429 (defun gnus-summary-prev-thread (n)
11430   "Go to the same level previous N'th thread.
11431 Returns the difference between N and the number of skips actually
11432 done."
11433   (interactive "p")
11434   (gnus-summary-next-thread (- n)))
11435
11436 (defun gnus-summary-go-down-thread ()
11437   "Go down one level in the current thread."
11438   (let ((children (gnus-summary-article-children)))
11439     (when children
11440       (gnus-summary-goto-subject (car children)))))
11441
11442 (defun gnus-summary-go-up-thread ()
11443   "Go up one level in the current thread."
11444   (let ((parent (gnus-summary-article-parent)))
11445     (when parent
11446       (gnus-summary-goto-subject parent))))
11447
11448 (defun gnus-summary-down-thread (n)
11449   "Go down thread N steps.
11450 If N is negative, go up instead.
11451 Returns the difference between N and how many steps down that were
11452 taken."
11453   (interactive "p")
11454   (let ((up (< n 0))
11455         (n (abs n)))
11456     (while (and (> n 0)
11457                 (if up (gnus-summary-go-up-thread)
11458                   (gnus-summary-go-down-thread)))
11459       (setq n (1- n)))
11460     (gnus-summary-position-point)
11461     (when (/= 0 n)
11462       (gnus-message 7 "Can't go further"))
11463     n))
11464
11465 (defun gnus-summary-up-thread (n)
11466   "Go up thread N steps.
11467 If N is negative, go down instead.
11468 Returns the difference between N and how many steps down that were
11469 taken."
11470   (interactive "p")
11471   (gnus-summary-down-thread (- n)))
11472
11473 (defun gnus-summary-top-thread ()
11474   "Go to the top of the thread."
11475   (interactive)
11476   (while (gnus-summary-go-up-thread))
11477   (gnus-summary-article-number))
11478
11479 (defun gnus-summary-expire-thread ()
11480   "Mark articles under current thread as expired."
11481   (interactive)
11482   (gnus-summary-kill-thread 0))
11483
11484 (defun gnus-summary-kill-thread (&optional unmark)
11485   "Mark articles under current thread as read.
11486 If the prefix argument is positive, remove any kinds of marks.
11487 If the prefix argument is zero, mark thread as expired.
11488 If the prefix argument is negative, tick articles instead."
11489   (interactive "P")
11490   (when unmark
11491     (setq unmark (prefix-numeric-value unmark)))
11492   (let ((articles (gnus-summary-articles-in-thread))
11493         (hide (or (null unmark) (= unmark 0))))
11494     (save-excursion
11495       ;; Expand the thread.
11496       (gnus-summary-show-thread)
11497       ;; Mark all the articles.
11498       (while articles
11499         (gnus-summary-goto-subject (car articles))
11500         (cond ((null unmark)
11501                (gnus-summary-mark-article-as-read gnus-killed-mark))
11502               ((> unmark 0)
11503                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11504               ((= unmark 0)
11505                (gnus-summary-mark-article nil gnus-expirable-mark))
11506               (t
11507                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11508         (setq articles (cdr articles))))
11509     ;; Hide killed subtrees when hide is true.
11510     (and hide
11511          gnus-thread-hide-killed
11512          (gnus-summary-hide-thread))
11513     ;; If hide is t, go to next unread subject.
11514     (when hide
11515       ;; Go to next unread subject.
11516       (gnus-summary-next-subject 1 t)))
11517   (gnus-set-mode-line 'summary))
11518
11519 ;; Summary sorting commands
11520
11521 (defun gnus-summary-sort-by-number (&optional reverse)
11522   "Sort the summary buffer by article number.
11523 Argument REVERSE means reverse order."
11524   (interactive "P")
11525   (gnus-summary-sort 'number reverse))
11526
11527 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11528   "Sort the summary buffer by most recent article number.
11529 Argument REVERSE means reverse order."
11530   (interactive "P")
11531   (gnus-summary-sort 'most-recent-number reverse))
11532
11533 (defun gnus-summary-sort-by-random (&optional reverse)
11534   "Randomize the order in the summary buffer.
11535 Argument REVERSE means to randomize in reverse order."
11536   (interactive "P")
11537   (gnus-summary-sort 'random reverse))
11538
11539 (defun gnus-summary-sort-by-author (&optional reverse)
11540   "Sort the summary buffer by author name alphabetically.
11541 If `case-fold-search' is non-nil, case of letters is ignored.
11542 Argument REVERSE means reverse order."
11543   (interactive "P")
11544   (gnus-summary-sort 'author reverse))
11545
11546 (defun gnus-summary-sort-by-recipient (&optional reverse)
11547   "Sort the summary buffer by recipient 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 'recipient reverse))
11552
11553 (defun gnus-summary-sort-by-subject (&optional reverse)
11554   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
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 'subject reverse))
11559
11560 (defun gnus-summary-sort-by-date (&optional reverse)
11561   "Sort the summary buffer by date.
11562 Argument REVERSE means reverse order."
11563   (interactive "P")
11564   (gnus-summary-sort 'date reverse))
11565
11566 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11567   "Sort the summary buffer by most recent date.
11568 Argument REVERSE means reverse order."
11569   (interactive "P")
11570   (gnus-summary-sort 'most-recent-date reverse))
11571
11572 (defun gnus-summary-sort-by-score (&optional reverse)
11573   "Sort the summary buffer by score.
11574 Argument REVERSE means reverse order."
11575   (interactive "P")
11576   (gnus-summary-sort 'score reverse))
11577
11578 (defun gnus-summary-sort-by-lines (&optional reverse)
11579   "Sort the summary buffer by the number of lines.
11580 Argument REVERSE means reverse order."
11581   (interactive "P")
11582   (gnus-summary-sort 'lines reverse))
11583
11584 (defun gnus-summary-sort-by-chars (&optional reverse)
11585   "Sort the summary buffer by article length.
11586 Argument REVERSE means reverse order."
11587   (interactive "P")
11588   (gnus-summary-sort 'chars reverse))
11589
11590 (defun gnus-summary-sort-by-original (&optional reverse)
11591   "Sort the summary buffer using the default sorting method.
11592 Argument REVERSE means reverse order."
11593   (interactive "P")
11594   (let* ((inhibit-read-only t)
11595          (gnus-summary-prepare-hook nil))
11596     ;; We do the sorting by regenerating the threads.
11597     (gnus-summary-prepare)
11598     ;; Hide subthreads if needed.
11599     (gnus-summary-maybe-hide-threads)))
11600
11601 (defun gnus-summary-sort (predicate reverse)
11602   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11603   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11604          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11605          (gnus-thread-sort-functions
11606           (if (not reverse)
11607               thread
11608             `(lambda (t1 t2)
11609                (,thread t2 t1))))
11610          (gnus-sort-gathered-threads-function
11611           gnus-thread-sort-functions)
11612          (gnus-article-sort-functions
11613           (if (not reverse)
11614               article
11615             `(lambda (t1 t2)
11616                (,article t2 t1))))
11617          (inhibit-read-only t)
11618          (gnus-summary-prepare-hook nil))
11619     ;; We do the sorting by regenerating the threads.
11620     (gnus-summary-prepare)
11621     ;; Hide subthreads if needed.
11622     (gnus-summary-maybe-hide-threads)))
11623
11624 ;; Summary saving commands.
11625
11626 (defun gnus-summary-save-article (&optional n not-saved)
11627   "Save the current article using the default saver function.
11628 If N is a positive number, save the N next articles.
11629 If N is a negative number, save the N previous articles.
11630 If N is nil and any articles have been marked with the process mark,
11631 save those articles instead.
11632 The variable `gnus-default-article-saver' specifies the saver function.
11633
11634 If the optional second argument NOT-SAVED is non-nil, articles saved
11635 will not be marked as saved."
11636   (interactive "P")
11637   (require 'gnus-art)
11638   (let* ((articles (gnus-summary-work-articles n))
11639          (save-buffer (save-excursion
11640                         (nnheader-set-temp-buffer " *Gnus Save*")))
11641          (num (length articles))
11642          ;; Whether to save decoded articles or raw articles.
11643          (decode (when gnus-article-save-coding-system
11644                    (get gnus-default-article-saver :decode)))
11645          ;; When saving many articles in a single file, use the other
11646          ;; function to save articles other than the first one.
11647          (saver2 (get gnus-default-article-saver :function))
11648          (gnus-prompt-before-saving (if saver2
11649                                         t
11650                                       gnus-prompt-before-saving))
11651          (gnus-default-article-saver gnus-default-article-saver)
11652          header file)
11653     (dolist (article articles)
11654       (setq header (gnus-summary-article-header article))
11655       (if (not (vectorp header))
11656           ;; This is a pseudo-article.
11657           (if (assq 'name header)
11658               (gnus-copy-file (cdr (assq 'name header)))
11659             (gnus-message 1 "Article %d is unsaveable" article))
11660         ;; This is a real article.
11661         (save-window-excursion
11662           (gnus-summary-select-article decode decode nil article)
11663           (gnus-summary-goto-subject article))
11664         (with-current-buffer save-buffer
11665           (erase-buffer)
11666           (insert-buffer-substring (if decode
11667                                        gnus-article-buffer
11668                                      gnus-original-article-buffer)))
11669         (setq file (gnus-article-save save-buffer file num))
11670         (gnus-summary-remove-process-mark article)
11671         (unless not-saved
11672           (gnus-summary-set-saved-mark article)))
11673       (when saver2
11674         (setq gnus-default-article-saver saver2
11675               saver2 nil)))
11676     (gnus-kill-buffer save-buffer)
11677     (gnus-summary-position-point)
11678     (gnus-set-mode-line 'summary)
11679     n))
11680
11681 (defun gnus-summary-pipe-output (&optional n sym)
11682   "Pipe the current article to a subprocess.
11683 If N is a positive number, pipe the N next articles.
11684 If N is a negative number, pipe the N previous articles.
11685 If N is nil and any articles have been marked with the process mark,
11686 pipe those articles instead.
11687 The default command to which articles are piped is specified by the
11688 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11689 will be prompted for the command.
11690
11691 The properties `:decode' and `:headers' that are put to the function
11692 symbol `gnus-summary-save-in-pipe' control whether this function
11693 decodes articles and what headers to keep (see the doc string for the
11694 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11695 is neither omitted nor the symbol `r', force including all headers
11696 regardless of the `:headers' property.  If it is the symbol `r',
11697 articles that are not decoded and include all headers will be piped
11698 no matter what the properties `:decode' and `:headers' are."
11699   (interactive (gnus-interactive "P\ny"))
11700   (require 'gnus-art)
11701   (let* ((articles (gnus-summary-work-articles n))
11702          (result-buffer "*Shell Command Output*")
11703          (all-headers (not (memq sym '(nil r))))
11704          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11705          (raw (eq sym 'r))
11706          (headers (get 'gnus-summary-save-in-pipe :headers))
11707          command result)
11708     (unless (numberp (car articles))
11709       (error "No article to pipe"))
11710     (setq command (gnus-read-shell-command
11711                    (concat "Shell command on "
11712                            (if (cdr articles)
11713                                (format "these %d articles" (length articles))
11714                              "this article")
11715                            ": ")
11716                    gnus-summary-pipe-output-default-command))
11717     (when (string-equal command "")
11718       (error "A command is required"))
11719     (when all-headers
11720       (put 'gnus-summary-save-in-pipe :headers nil))
11721     (unwind-protect
11722         (while articles
11723           (gnus-summary-goto-subject (pop articles))
11724           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11725           (when (and (get-buffer result-buffer)
11726                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11727             (setq result (concat result (with-current-buffer result-buffer
11728                                           (buffer-string))))))
11729       (put 'gnus-summary-save-in-pipe :headers headers))
11730     (unless (zerop (length result))
11731       (if (with-current-buffer (get-buffer-create result-buffer)
11732             (erase-buffer)
11733             (insert result)
11734             (prog1
11735                 (and (= (count-lines (point-min) (point)) 1)
11736                      (progn
11737                        (end-of-line 0)
11738                        (<= (current-column)
11739                            (window-width (minibuffer-window)))))
11740               (goto-char (point-min))))
11741           (message "%s" (substring result 0 -1))
11742         (message nil)
11743         (gnus-configure-windows 'pipe)))))
11744
11745 (defun gnus-summary-save-article-mail (&optional arg)
11746   "Append the current article to a Unix mail box file.
11747 If N is a positive number, save the N next articles.
11748 If N is a negative number, save the N previous articles.
11749 If N is nil and any articles have been marked with the process mark,
11750 save those articles instead."
11751   (interactive "P")
11752   (require 'gnus-art)
11753   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11754     (gnus-summary-save-article arg)))
11755
11756 (defun gnus-summary-save-article-rmail (&optional arg)
11757   "Append the current article to an rmail file.
11758 If N is a positive number, save the N next articles.
11759 If N is a negative number, save the N previous articles.
11760 If N is nil and any articles have been marked with the process mark,
11761 save those articles instead."
11762   (interactive "P")
11763   (require 'gnus-art)
11764   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11765     (gnus-summary-save-article arg)))
11766
11767 (defun gnus-summary-save-article-file (&optional arg)
11768   "Append the current article to a file.
11769 If N is a positive number, save the N next articles.
11770 If N is a negative number, save the N previous articles.
11771 If N is nil and any articles have been marked with the process mark,
11772 save those articles instead."
11773   (interactive "P")
11774   (require 'gnus-art)
11775   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11776     (gnus-summary-save-article arg)))
11777
11778 (defun gnus-summary-write-article-file (&optional arg)
11779   "Write the current article to a file, deleting the previous file.
11780 If N is a positive number, save the N next articles.
11781 If N is a negative number, save the N previous articles.
11782 If N is nil and any articles have been marked with the process mark,
11783 save those articles instead."
11784   (interactive "P")
11785   (require 'gnus-art)
11786   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11787     (gnus-summary-save-article arg)))
11788
11789 (defun gnus-summary-save-article-body-file (&optional arg)
11790   "Append the current article body to a file.
11791 If N is a positive number, save the N next articles.
11792 If N is a negative number, save the N previous articles.
11793 If N is nil and any articles have been marked with the process mark,
11794 save those articles instead."
11795   (interactive "P")
11796   (require 'gnus-art)
11797   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11798     (gnus-summary-save-article arg)))
11799
11800 (defun gnus-summary-write-article-body-file (&optional arg)
11801   "Write the current article body to a file, deleting the previous file.
11802 If N is a positive number, save the N next articles.
11803 If N is a negative number, save the N previous articles.
11804 If N is nil and any articles have been marked with the process mark,
11805 save those articles instead."
11806   (interactive "P")
11807   (require 'gnus-art)
11808   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11809     (gnus-summary-save-article arg)))
11810
11811 (defun gnus-summary-muttprint (&optional arg)
11812   "Print the current article using Muttprint.
11813 If N is a positive number, save the N next articles.
11814 If N is a negative number, save the N previous articles.
11815 If N is nil and any articles have been marked with the process mark,
11816 save those articles instead."
11817   (interactive "P")
11818   (require 'gnus-art)
11819   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11820     (gnus-summary-save-article arg t)))
11821
11822 (defun gnus-summary-pipe-message (program)
11823   "Pipe the current article through PROGRAM."
11824   (interactive "sProgram: ")
11825   (gnus-summary-select-article)
11826   (let ((mail-header-separator ""))
11827     (gnus-eval-in-buffer-window gnus-article-buffer
11828       (save-restriction
11829         (widen)
11830         (let ((start (window-start))
11831               (inhibit-read-only t))
11832           (message-pipe-buffer-body program)
11833           (set-window-start (get-buffer-window (current-buffer)) start))))))
11834
11835 (defun gnus-get-split-value (methods)
11836   "Return a value based on the split METHODS."
11837   (let (split-name method result match)
11838     (when methods
11839       (with-current-buffer gnus-original-article-buffer
11840         (save-restriction
11841           (nnheader-narrow-to-headers)
11842           (while (and methods (not split-name))
11843             (goto-char (point-min))
11844             (setq method (pop methods))
11845             (setq match (car method))
11846             (when (cond
11847                    ((stringp match)
11848                     ;; Regular expression.
11849                     (ignore-errors
11850                       (re-search-forward match nil t)))
11851                    ((functionp match)
11852                     ;; Function.
11853                     (save-restriction
11854                       (widen)
11855                       (setq result (funcall match gnus-newsgroup-name))))
11856                    ((consp match)
11857                     ;; Form.
11858                     (save-restriction
11859                       (widen)
11860                       (setq result (eval match)))))
11861               (setq split-name (cdr method))
11862               (cond ((stringp result)
11863                      (push (expand-file-name
11864                             result gnus-article-save-directory)
11865                            split-name))
11866                     ((consp result)
11867                      (setq split-name (append result split-name)))))))))
11868     (nreverse split-name)))
11869
11870 (defun gnus-valid-move-group-p (group)
11871   (and (boundp group)
11872        (symbol-name group)
11873        (symbol-value group)
11874        (gnus-get-function (gnus-find-method-for-group
11875                            (symbol-name group)) 'request-accept-article t)))
11876
11877 (defun gnus-read-move-group-name (prompt default articles prefix)
11878   "Read a group name."
11879   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11880          (minibuffer-confirm-incomplete nil) ; XEmacs
11881          (prom
11882           (format "%s %s to"
11883                   prompt
11884                   (if (> (length articles) 1)
11885                       (format "these %d articles" (length articles))
11886                     "this article")))
11887          (to-newsgroup
11888           (let (active group)
11889             (when (or (null split-name) (= 1 (length split-name)))
11890               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11891               (mapatoms (lambda (symbol)
11892                           (setq group (symbol-name symbol))
11893                           (when (string-match "[^\000-\177]" group)
11894                             (setq group (gnus-group-decoded-name group)))
11895                           (set (intern group active) group))
11896                         gnus-active-hashtb))
11897             (cond
11898              ((null split-name)
11899               (gnus-completing-read-with-default
11900                default prom active 'gnus-valid-move-group-p nil prefix
11901                'gnus-group-history))
11902              ((= 1 (length split-name))
11903               (gnus-completing-read-with-default
11904                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11905                'gnus-group-history))
11906              (t
11907               (gnus-completing-read-with-default
11908                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11909                'gnus-group-history)))))
11910          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11911          encoded)
11912     (when to-newsgroup
11913       (if (or (string= to-newsgroup "")
11914               (string= to-newsgroup prefix))
11915           (setq to-newsgroup default))
11916       (unless to-newsgroup
11917         (error "No group name entered"))
11918       (setq encoded (mm-encode-coding-string
11919                      to-newsgroup
11920                      (gnus-group-name-charset to-method to-newsgroup)))
11921       (or (gnus-active encoded)
11922           (gnus-activate-group encoded nil nil to-method)
11923           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11924                                      to-newsgroup))
11925               (or (and (gnus-request-create-group encoded to-method)
11926                        (gnus-activate-group encoded nil nil to-method)
11927                        (gnus-subscribe-group encoded))
11928                   (error "Couldn't create group %s" to-newsgroup)))
11929           (error "No such group: %s" to-newsgroup))
11930       encoded)))
11931
11932 (defvar gnus-summary-save-parts-counter)
11933 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11934
11935 (defun gnus-summary-save-parts (type dir n &optional reverse)
11936   "Save parts matching TYPE to DIR.
11937 If REVERSE, save parts that do not match TYPE."
11938   (interactive
11939    (list (read-string "Save parts of type: "
11940                       (or (car gnus-summary-save-parts-type-history)
11941                           gnus-summary-save-parts-default-mime)
11942                       'gnus-summary-save-parts-type-history)
11943          (setq gnus-summary-save-parts-last-directory
11944                (read-file-name "Save to directory: "
11945                                gnus-summary-save-parts-last-directory
11946                                nil t))
11947          current-prefix-arg))
11948   (gnus-summary-iterate n
11949     (let ((gnus-display-mime-function nil)
11950           gnus-article-prepare-hook
11951           gnus-article-decode-hook
11952           gnus-display-mime-function
11953           gnus-break-pages
11954           (gnus-inhibit-treatment t))
11955       (gnus-summary-select-article))
11956     (with-current-buffer gnus-article-buffer
11957       (let ((handles (or gnus-article-mime-handles
11958                          (mm-dissect-buffer nil gnus-article-loose-mime)
11959                          (and gnus-article-emulate-mime
11960                               (mm-uu-dissect))))
11961             (gnus-summary-save-parts-counter 1))
11962         (when handles
11963           (gnus-summary-save-parts-1 type dir handles reverse)
11964           (unless gnus-article-mime-handles ;; Don't destroy this case.
11965             (mm-destroy-parts handles)))))))
11966
11967 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11968   (if (stringp (car handle))
11969       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11970               (cdr handle))
11971     (when (if reverse
11972               (not (string-match type (mm-handle-media-type handle)))
11973             (string-match type (mm-handle-media-type handle)))
11974       (let ((file (expand-file-name
11975                    (gnus-map-function
11976                     mm-file-name-rewrite-functions
11977                     (file-name-nondirectory
11978                      (or
11979                       (mail-content-type-get
11980                        (mm-handle-disposition handle) 'filename)
11981                       (mail-content-type-get
11982                        (mm-handle-type handle) 'name)
11983                       (format "%s.%d.%d" gnus-newsgroup-name
11984                               (cdr gnus-article-current)
11985                               gnus-summary-save-parts-counter))))
11986                    dir)))
11987         (incf gnus-summary-save-parts-counter)
11988         (unless (file-exists-p file)
11989           (mm-save-part-to-file handle file))))))
11990
11991 ;; Summary extract commands
11992
11993 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11994   (let ((inhibit-read-only t)
11995         (article (gnus-summary-article-number))
11996         after-article b e)
11997     (unless (gnus-summary-goto-subject article)
11998       (error "No such article: %d" article))
11999     (gnus-summary-position-point)
12000     ;; If all commands are to be bunched up on one line, we collect
12001     ;; them here.
12002     (unless gnus-view-pseudos-separately
12003       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12004             files action)
12005         (while ps
12006           (setq action (cdr (assq 'action (car ps))))
12007           (setq files (list (cdr (assq 'name (car ps)))))
12008           (while (and ps (cdr ps)
12009                       (string= (or action "1")
12010                                (or (cdr (assq 'action (cadr ps))) "2")))
12011             (push (cdr (assq 'name (cadr ps))) files)
12012             (setcdr ps (cddr ps)))
12013           (when files
12014             (when (not (string-match "%s" action))
12015               (push " " files))
12016             (push " " files)
12017             (when (assq 'execute (car ps))
12018               (setcdr (assq 'execute (car ps))
12019                       (funcall (if (string-match "%s" action)
12020                                    'format 'concat)
12021                                action
12022                                (mapconcat
12023                                 (lambda (f)
12024                                   (if (equal f " ")
12025                                       f
12026                                     (shell-quote-argument f)))
12027                                 files " ")))))
12028           (setq ps (cdr ps)))))
12029     (if (and gnus-view-pseudos (not not-view))
12030         (while pslist
12031           (when (assq 'execute (car pslist))
12032             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12033                                   (eq gnus-view-pseudos 'not-confirm)))
12034           (setq pslist (cdr pslist)))
12035       (save-excursion
12036         (while pslist
12037           (setq after-article (or (cdr (assq 'article (car pslist)))
12038                                   (gnus-summary-article-number)))
12039           (gnus-summary-goto-subject after-article)
12040           (forward-line 1)
12041           (setq b (point))
12042           (insert "    " (file-name-nondirectory
12043                           (cdr (assq 'name (car pslist))))
12044                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12045           (setq e (point))
12046           (forward-line -1)             ; back to `b'
12047           (gnus-add-text-properties
12048            b (1- e) (list 'gnus-number gnus-reffed-article-number
12049                           gnus-mouse-face-prop gnus-mouse-face))
12050           (gnus-data-enter
12051            after-article gnus-reffed-article-number
12052            gnus-unread-mark b (car pslist) 0 (- e b))
12053           (setq gnus-newsgroup-unreads
12054                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12055                                          gnus-reffed-article-number))
12056           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12057           (setq pslist (cdr pslist)))))))
12058
12059 (defun gnus-pseudos< (p1 p2)
12060   (let ((c1 (cdr (assq 'action p1)))
12061         (c2 (cdr (assq 'action p2))))
12062     (and c1 c2 (string< c1 c2))))
12063
12064 (defun gnus-request-pseudo-article (props)
12065   (cond ((assq 'execute props)
12066          (gnus-execute-command (cdr (assq 'execute props)))))
12067   (let ((gnus-current-article (gnus-summary-article-number)))
12068     (gnus-run-hooks 'gnus-mark-article-hook)))
12069
12070 (defun gnus-execute-command (command &optional automatic)
12071   (save-excursion
12072     (gnus-article-setup-buffer)
12073     (set-buffer gnus-article-buffer)
12074     (setq buffer-read-only nil)
12075     (let ((command (if automatic command
12076                      (read-string "Command: " (cons command 0)))))
12077       (erase-buffer)
12078       (insert "$ " command "\n\n")
12079       (if gnus-view-pseudo-asynchronously
12080           (start-process "gnus-execute" (current-buffer) shell-file-name
12081                          shell-command-switch command)
12082         (call-process shell-file-name nil t nil
12083                       shell-command-switch command)))))
12084
12085 ;; Summary kill commands.
12086
12087 (defun gnus-summary-edit-global-kill (article)
12088   "Edit the \"global\" kill file."
12089   (interactive (list (gnus-summary-article-number)))
12090   (gnus-group-edit-global-kill article))
12091
12092 (defun gnus-summary-edit-local-kill ()
12093   "Edit a local kill file applied to the current newsgroup."
12094   (interactive)
12095   (setq gnus-current-headers (gnus-summary-article-header))
12096   (gnus-group-edit-local-kill
12097    (gnus-summary-article-number) gnus-newsgroup-name))
12098
12099 ;;; Header reading.
12100
12101 (defun gnus-read-header (id &optional header)
12102   "Read the headers of article ID and enter them into the Gnus system."
12103   (let ((group gnus-newsgroup-name)
12104         (gnus-override-method
12105          (or
12106           gnus-override-method
12107           (and (gnus-news-group-p gnus-newsgroup-name)
12108                (car (gnus-refer-article-methods)))))
12109         where)
12110     ;; First we check to see whether the header in question is already
12111     ;; fetched.
12112     (if (stringp id)
12113         ;; This is a Message-ID.
12114         (setq header (or header (gnus-id-to-header id)))
12115       ;; This is an article number.
12116       (setq header (or header (gnus-summary-article-header id))))
12117     (if (and header
12118              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12119         ;; We have found the header.
12120         header
12121       ;; We have to really fetch the header to this article.
12122       (with-current-buffer nntp-server-buffer
12123         (when (setq where (gnus-request-head id group))
12124           (nnheader-fold-continuation-lines)
12125           (goto-char (point-max))
12126           (insert ".\n")
12127           (goto-char (point-min))
12128           (insert "211 ")
12129           (princ (cond
12130                   ((numberp id) id)
12131                   ((cdr where) (cdr where))
12132                   (header (mail-header-number header))
12133                   (t gnus-reffed-article-number))
12134                  (current-buffer))
12135           (insert " Article retrieved.\n"))
12136         (if (or (not where)
12137                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12138             ()                          ; Malformed head.
12139           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12140             (when (and (stringp id)
12141                        (or
12142                         (not (string= (gnus-group-real-name group)
12143                                       (car where)))
12144                         (not (gnus-server-equal gnus-override-method
12145                                                 (gnus-group-method group)))))
12146               ;; If we fetched by Message-ID and the article came from
12147               ;; a different group (or server), we fudge some bogus
12148               ;; article numbers for this article.
12149               (mail-header-set-number header gnus-reffed-article-number))
12150             (with-current-buffer gnus-summary-buffer
12151               (decf gnus-reffed-article-number)
12152               (gnus-remove-header (mail-header-number header))
12153               (push header gnus-newsgroup-headers)
12154               (setq gnus-current-headers header)
12155               (push (mail-header-number header) gnus-newsgroup-limit)))
12156           header)))))
12157
12158 (defun gnus-remove-header (number)
12159   "Remove header NUMBER from `gnus-newsgroup-headers'."
12160   (if (and gnus-newsgroup-headers
12161            (= number (mail-header-number (car gnus-newsgroup-headers))))
12162       (pop gnus-newsgroup-headers)
12163     (let ((headers gnus-newsgroup-headers))
12164       (while (and (cdr headers)
12165                   (not (= number (mail-header-number (cadr headers)))))
12166         (pop headers))
12167       (when (cdr headers)
12168         (setcdr headers (cddr headers))))))
12169
12170 ;;;
12171 ;;; summary highlights
12172 ;;;
12173
12174 (defun gnus-highlight-selected-summary ()
12175   "Highlight selected article in summary buffer."
12176   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12177   (when gnus-summary-selected-face
12178     (save-excursion
12179       (let* ((beg (point-at-bol))
12180              (end (point-at-eol))
12181              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12182              (from (if (get-text-property beg gnus-mouse-face-prop)
12183                        beg
12184                      (or (next-single-property-change
12185                           beg gnus-mouse-face-prop nil end)
12186                          beg)))
12187              (to
12188               (if (= from end)
12189                   (- from 2)
12190                 (or (next-single-property-change
12191                      from gnus-mouse-face-prop nil end)
12192                     end))))
12193         ;; If no mouse-face prop on line we will have to = from = end,
12194         ;; so we highlight the entire line instead.
12195         (when (= (+ to 2) from)
12196           (setq from beg)
12197           (setq to end))
12198         (if gnus-newsgroup-selected-overlay
12199             ;; Move old overlay.
12200             (gnus-move-overlay
12201              gnus-newsgroup-selected-overlay from to (current-buffer))
12202           ;; Create new overlay.
12203           (gnus-overlay-put
12204            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12205            'face gnus-summary-selected-face))))))
12206
12207 (defvar gnus-summary-highlight-line-cached nil)
12208 (defvar gnus-summary-highlight-line-trigger nil)
12209
12210 (defun gnus-summary-highlight-line-0 ()
12211   (if (and (eq gnus-summary-highlight-line-trigger
12212                gnus-summary-highlight)
12213            gnus-summary-highlight-line-cached)
12214       gnus-summary-highlight-line-cached
12215     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12216           gnus-summary-highlight-line-cached
12217           (let* ((cond (list 'cond))
12218                  (c cond)
12219                  (list gnus-summary-highlight))
12220             (while list
12221               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12222                               nil))
12223               (setq c (cdr c)
12224                     list (cdr list)))
12225             (gnus-byte-compile (list 'lambda nil cond))))))
12226
12227 (defun gnus-summary-highlight-line ()
12228   "Highlight current line according to `gnus-summary-highlight'."
12229   (let* ((beg (point-at-bol))
12230          (article (or (gnus-summary-article-number) gnus-current-article))
12231          (score (or (cdr (assq article
12232                                gnus-newsgroup-scored))
12233                     gnus-summary-default-score 0))
12234          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12235          (inhibit-read-only t)
12236          (default gnus-summary-default-score)
12237          (default-high gnus-summary-default-high-score)
12238          (default-low gnus-summary-default-low-score)
12239          (uncached (and gnus-summary-use-undownloaded-faces
12240                         (memq article gnus-newsgroup-undownloaded)
12241                         (not (memq article gnus-newsgroup-cached)))))
12242     (let ((face (funcall (gnus-summary-highlight-line-0))))
12243       (unless (eq face (get-text-property beg 'face))
12244         (gnus-put-text-property-excluding-characters-with-faces
12245          beg (point-at-eol) 'face
12246          (setq face (if (boundp face) (symbol-value face) face)))
12247         (when gnus-summary-highlight-line-function
12248           (funcall gnus-summary-highlight-line-function article face))))))
12249
12250 (defun gnus-update-read-articles (group unread &optional compute)
12251   "Update the list of read articles in GROUP.
12252 UNREAD is a sorted list."
12253   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12254         (info (gnus-get-info group))
12255         (prev 1)
12256         read)
12257     (if (or (not info) (not active))
12258         ;; There is no info on this group if it was, in fact,
12259         ;; killed.  Gnus stores no information on killed groups, so
12260         ;; there's nothing to be done.
12261         ;; One could store the information somewhere temporarily,
12262         ;; perhaps...  Hmmm...
12263         ()
12264       ;; Remove any negative articles numbers.
12265       (while (and unread (< (car unread) 0))
12266         (setq unread (cdr unread)))
12267       ;; Remove any expired article numbers
12268       (while (and unread (< (car unread) (car active)))
12269         (setq unread (cdr unread)))
12270       ;; Compute the ranges of read articles by looking at the list of
12271       ;; unread articles.
12272       (while unread
12273         (when (/= (car unread) prev)
12274           (push (if (= prev (1- (car unread))) prev
12275                   (cons prev (1- (car unread))))
12276                 read))
12277         (setq prev (1+ (car unread)))
12278         (setq unread (cdr unread)))
12279       (when (<= prev (cdr active))
12280         (push (cons prev (cdr active)) read))
12281       (setq read (if (> (length read) 1) (nreverse read) read))
12282       (if compute
12283           read
12284         (save-excursion
12285           (let (setmarkundo)
12286             ;; Propagate the read marks to the backend.
12287             (when (and gnus-propagate-marks
12288                        (gnus-check-backend-function 'request-set-mark group))
12289               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12290                     (add (gnus-remove-from-range read (gnus-info-read info))))
12291                 (when (or add del)
12292                   (unless (gnus-check-group group)
12293                     (error "Can't open server for %s" group))
12294                   (gnus-request-set-mark
12295                    group (delq nil (list (if add (list add 'add '(read)))
12296                                          (if del (list del 'del '(read))))))
12297                   (setq setmarkundo
12298                         `(gnus-request-set-mark
12299                           ,group
12300                           ',(delq nil (list
12301                                        (if del (list del 'add '(read)))
12302                                        (if add (list add 'del '(read))))))))))
12303             (set-buffer gnus-group-buffer)
12304             (gnus-undo-register
12305               `(progn
12306                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12307                  (gnus-info-set-read ',info ',(gnus-info-read info))
12308                  (gnus-get-unread-articles-in-group ',info
12309                                                     (gnus-active ,group))
12310                  (gnus-group-update-group ,group t)
12311                  ,setmarkundo))))
12312         ;; Enter this list into the group info.
12313         (gnus-info-set-read info read)
12314         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12315         (gnus-get-unread-articles-in-group info (gnus-active group))
12316         t))))
12317
12318 (defun gnus-offer-save-summaries ()
12319   "Offer to save all active summary buffers."
12320   (let (buffers)
12321     ;; Go through all buffers and find all summaries.
12322     (dolist (buffer (buffer-list))
12323       (when (and (setq buffer (buffer-name buffer))
12324                  (string-match "Summary" buffer)
12325                  (with-current-buffer buffer
12326                    ;; We check that this is, indeed, a summary buffer.
12327                    (and (eq major-mode 'gnus-summary-mode)
12328                         ;; Also make sure this isn't bogus.
12329                         gnus-newsgroup-prepared
12330                         ;; Also make sure that this isn't a
12331                         ;; dead summary buffer.
12332                         (not gnus-dead-summary-mode))))
12333         (push buffer buffers)))
12334     ;; Go through all these summary buffers and offer to save them.
12335     (when buffers
12336       (save-excursion
12337         (map-y-or-n-p
12338          "Update summary buffer %s? "
12339          (lambda (buf)
12340            (switch-to-buffer buf)
12341            (gnus-summary-exit))
12342          buffers)))))
12343
12344 (defun gnus-summary-setup-default-charset ()
12345   "Setup newsgroup default charset."
12346   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12347       (setq gnus-newsgroup-charset nil)
12348     (let* ((ignored-charsets
12349             (or gnus-newsgroup-ephemeral-ignored-charsets
12350                 (append
12351                  (and gnus-newsgroup-name
12352                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12353                  gnus-newsgroup-ignored-charsets))))
12354       (setq gnus-newsgroup-charset
12355             (or gnus-newsgroup-ephemeral-charset
12356                 (and gnus-newsgroup-name
12357                      (gnus-parameter-charset gnus-newsgroup-name))
12358                 gnus-default-charset))
12359       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12360            ignored-charsets))))
12361
12362 ;;;
12363 ;;; Mime Commands
12364 ;;;
12365
12366 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12367   "Display the current article buffer fully MIME-buttonized.
12368 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12369 treated as multipart/mixed."
12370   (interactive "P")
12371   (require 'gnus-art)
12372   (let ((gnus-unbuttonized-mime-types nil)
12373         (gnus-mime-display-multipart-as-mixed show-all-parts))
12374     (gnus-summary-show-article)))
12375
12376 (defun gnus-summary-repair-multipart (article)
12377   "Add a Content-Type header to a multipart article without one."
12378   (interactive (list (gnus-summary-article-number)))
12379   (gnus-with-article article
12380     (message-narrow-to-head)
12381     (message-remove-header "Mime-Version")
12382     (goto-char (point-max))
12383     (insert "Mime-Version: 1.0\n")
12384     (widen)
12385     (when (search-forward "\n--" nil t)
12386       (let ((separator (buffer-substring (point) (point-at-eol))))
12387         (message-narrow-to-head)
12388         (message-remove-header "Content-Type")
12389         (goto-char (point-max))
12390         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12391                         separator))
12392         (widen))))
12393   (let (gnus-mark-article-hook)
12394     (gnus-summary-select-article t t nil article)))
12395
12396 (defun gnus-summary-toggle-display-buttonized ()
12397   "Toggle the buttonizing of the article buffer."
12398   (interactive)
12399   (require 'gnus-art)
12400   (if (setq gnus-inhibit-mime-unbuttonizing
12401             (not gnus-inhibit-mime-unbuttonizing))
12402       (let ((gnus-unbuttonized-mime-types nil))
12403         (gnus-summary-show-article))
12404     (gnus-summary-show-article)))
12405
12406 ;;;
12407 ;;; Generic summary marking commands
12408 ;;;
12409
12410 (defvar gnus-summary-marking-alist
12411   '((read gnus-del-mark "d")
12412     (unread gnus-unread-mark "u")
12413     (ticked gnus-ticked-mark "!")
12414     (dormant gnus-dormant-mark "?")
12415     (expirable gnus-expirable-mark "e"))
12416   "An alist of names/marks/keystrokes.")
12417
12418 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12419 (defvar gnus-summary-mark-map)
12420
12421 (defun gnus-summary-make-all-marking-commands ()
12422   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12423   (dolist (elem gnus-summary-marking-alist)
12424     (apply 'gnus-summary-make-marking-command elem)))
12425
12426 (defun gnus-summary-make-marking-command (name mark keystroke)
12427   (let ((map (make-sparse-keymap)))
12428     (define-key gnus-summary-generic-mark-map keystroke map)
12429     (dolist (lway `((next "next" next nil "n")
12430                     (next-unread "next unread" next t "N")
12431                     (prev "previous" prev nil "p")
12432                     (prev-unread "previous unread" prev t "P")
12433                     (nomove "" nil nil ,keystroke)))
12434       (let ((func (gnus-summary-make-marking-command-1
12435                    mark (car lway) lway name)))
12436         (setq func (eval func))
12437         (define-key map (nth 4 lway) func)))))
12438
12439 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12440   `(defun ,(intern
12441             (format "gnus-summary-put-mark-as-%s%s"
12442                     name (if (eq way 'nomove)
12443                              ""
12444                            (concat "-" (symbol-name way)))))
12445      (n)
12446      ,(format
12447        "Mark the current article as %s%s.
12448 If N, the prefix, then repeat N times.
12449 If N is negative, move in reverse order.
12450 The difference between N and the actual number of articles marked is
12451 returned."
12452        name (cadr lway))
12453      (interactive "p")
12454      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12455
12456 (defun gnus-summary-generic-mark (n mark move unread)
12457   "Mark N articles with MARK."
12458   (unless (eq major-mode 'gnus-summary-mode)
12459     (error "This command can only be used in the summary buffer"))
12460   (gnus-summary-show-thread)
12461   (let ((nummove
12462          (cond
12463           ((eq move 'next) 1)
12464           ((eq move 'prev) -1)
12465           (t 0))))
12466     (if (zerop nummove)
12467         (setq n 1)
12468       (when (< n 0)
12469         (setq n (abs n)
12470               nummove (* -1 nummove))))
12471     (while (and (> n 0)
12472                 (gnus-summary-mark-article nil mark)
12473                 (zerop (gnus-summary-next-subject nummove unread t)))
12474       (setq n (1- n)))
12475     (when (/= 0 n)
12476       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12477     (gnus-summary-recenter)
12478     (gnus-summary-position-point)
12479     (gnus-set-mode-line 'summary)
12480     n))
12481
12482 (defun gnus-summary-insert-articles (articles)
12483   (when (setq articles
12484               (gnus-sorted-difference articles
12485                                       (mapcar (lambda (h)
12486                                                 (mail-header-number h))
12487                                               gnus-newsgroup-headers)))
12488     (setq gnus-newsgroup-headers
12489           (gnus-merge 'list
12490                       gnus-newsgroup-headers
12491                       (gnus-fetch-headers articles)
12492                       'gnus-article-sort-by-number))
12493     ;; Suppress duplicates?
12494     (when gnus-suppress-duplicates
12495       (gnus-dup-suppress-articles))
12496
12497     (if (and gnus-fetch-old-headers
12498              (eq gnus-headers-retrieved-by 'nov))
12499         ;; We might want to build some more threads first.
12500         (if (eq gnus-fetch-old-headers 'invisible)
12501             (gnus-build-all-threads)
12502           (gnus-build-old-threads))
12503       ;; Mark the inserted articles that are unread as unread.
12504       (setq gnus-newsgroup-unreads
12505             (gnus-sorted-nunion
12506              gnus-newsgroup-unreads
12507              (gnus-sorted-nintersection
12508               (gnus-list-of-unread-articles gnus-newsgroup-name)
12509               articles)))
12510       ;; Mark the inserted articles as selected so that the information
12511       ;; of the marks having been changed by a user may be updated when
12512       ;; exiting this group.  See `gnus-summary-update-info'.
12513       (dolist (art articles)
12514         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12515     ;; Let the Gnus agent mark articles as read.
12516     (when gnus-agent
12517       (gnus-agent-get-undownloaded-list))
12518     ;; Remove list identifiers from subject
12519     (when gnus-list-identifiers
12520       (gnus-summary-remove-list-identifiers))
12521     ;; First and last article in this newsgroup.
12522     (when gnus-newsgroup-headers
12523       (setq gnus-newsgroup-begin
12524             (mail-header-number (car gnus-newsgroup-headers))
12525             gnus-newsgroup-end
12526             (mail-header-number
12527              (gnus-last-element gnus-newsgroup-headers))))
12528     (when gnus-use-scoring
12529       (gnus-possibly-score-headers))))
12530
12531 (defun gnus-summary-insert-old-articles (&optional all)
12532   "Insert all old articles in this group.
12533 If ALL is non-nil, already read articles become readable.
12534 If ALL is a number, fetch this number of articles."
12535   (interactive "P")
12536   (prog1
12537       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12538             older len)
12539         (setq older
12540               ;; Some nntp servers lie about their active range.  When
12541               ;; this happens, the active range can be in the millions.
12542               ;; Use a compressed range to avoid creating a huge list.
12543               (gnus-range-difference (list gnus-newsgroup-active) old))
12544         (setq len (gnus-range-length older))
12545         (cond
12546          ((null older) nil)
12547          ((numberp all)
12548           (if (< all len)
12549               (let ((older-range (nreverse older)))
12550                 (setq older nil)
12551
12552                 (while (> all 0)
12553                   (let* ((r (pop older-range))
12554                          (min (if (numberp r) r (car r)))
12555                          (max (if (numberp r) r (cdr r))))
12556                     (while (and (<= min max)
12557                                 (> all 0))
12558                       (push max older)
12559                       (setq all (1- all)
12560                             max (1- max))))))
12561             (setq older (gnus-uncompress-range older))))
12562          (all
12563           (setq older (gnus-uncompress-range older)))
12564          (t
12565           (when (and (numberp gnus-large-newsgroup)
12566                    (> len gnus-large-newsgroup))
12567               (let* ((cursor-in-echo-area nil)
12568                      (initial (gnus-parameter-large-newsgroup-initial
12569                                gnus-newsgroup-name))
12570                      (input
12571                       (read-string
12572                        (format
12573                         "How many articles from %s (%s %d): "
12574                         (gnus-group-decoded-name gnus-newsgroup-name)
12575                         (if initial "max" "default")
12576                         len)
12577                        (if initial
12578                            (cons (number-to-string initial)
12579                                  0)))))
12580                 (unless (string-match "^[ \t]*$" input)
12581                   (setq all (string-to-number input))
12582                   (if (< all len)
12583                       (let ((older-range (nreverse older)))
12584                         (setq older nil)
12585
12586                         (while (> all 0)
12587                           (let* ((r (pop older-range))
12588                                  (min (if (numberp r) r (car r)))
12589                                  (max (if (numberp r) r (cdr r))))
12590                             (while (and (<= min max)
12591                                         (> all 0))
12592                               (push max older)
12593                               (setq all (1- all)
12594                                     max (1- max))))))))))
12595           (setq older (gnus-uncompress-range older))))
12596         (if (not older)
12597             (message "No old news.")
12598           (gnus-summary-insert-articles older)
12599           (gnus-summary-limit (gnus-sorted-nunion old older))))
12600     (gnus-summary-position-point)))
12601
12602 (defun gnus-summary-insert-new-articles ()
12603   "Insert all new articles in this group."
12604   (interactive)
12605   (prog1
12606       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12607             (old-active gnus-newsgroup-active)
12608             (nnmail-fetched-sources (list t))
12609             i new)
12610         (setq gnus-newsgroup-active
12611               (gnus-activate-group gnus-newsgroup-name 'scan))
12612         (setq i (cdr gnus-newsgroup-active))
12613         (while (> i (cdr old-active))
12614           (push i new)
12615           (decf i))
12616         (if (not new)
12617             (message "No gnus is bad news")
12618           (gnus-summary-insert-articles new)
12619           (setq gnus-newsgroup-unreads
12620                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12621           (gnus-summary-limit (gnus-sorted-nunion old new))))
12622     (gnus-summary-position-point)))
12623
12624 ;;; Bookmark support for Gnus.
12625 (declare-function bookmark-make-record-default
12626                   "bookmark" (&optional no-file no-context posn))
12627 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12628 (declare-function bookmark-default-handler "bookmark" (bmk))
12629 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12630 (defvar bookmark-yank-point)
12631 (defvar bookmark-current-buffer)
12632
12633 (defun gnus-summary-bookmark-make-record ()
12634   "Make a bookmark entry for a Gnus summary buffer."
12635   (let (pos buf)
12636     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12637       (save-restriction              ; FIXME is it necessary to widen?
12638         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12639       (setq buf "art") ; We are recording bookmark from article buffer.
12640       (setq bookmark-yank-point (point))
12641       (setq bookmark-current-buffer (current-buffer))
12642       (gnus-article-show-summary))      ; Go back in summary buffer.
12643     ;; We are now recording bookmark from summary buffer.
12644     (unless buf (setq buf "sum"))
12645     (let* ((subject (elt (gnus-summary-article-header) 1))
12646            (grp     (car gnus-article-current))
12647            (art     (cdr gnus-article-current))
12648            (head    (gnus-summary-article-header art))
12649            (id      (mail-header-id head)))
12650       `(,subject
12651         ,@(condition-case nil
12652               (bookmark-make-record-default 'no-file 'no-context pos)
12653             (wrong-number-of-arguments
12654              (bookmark-make-record-default 'point-only)))
12655         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12656         (group . ,grp) (article . ,art)
12657         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12658
12659 ;;;###autoload
12660 (defun gnus-summary-bookmark-jump (bookmark)
12661   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12662 BOOKMARK is a bookmark name or a bookmark record."
12663   (let ((group    (bookmark-prop-get bookmark 'group))
12664         (article  (bookmark-prop-get bookmark 'article))
12665         (id       (bookmark-prop-get bookmark 'message-id))
12666         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12667     (gnus-fetch-group group (list article))
12668     (gnus-summary-insert-cached-articles)
12669     (gnus-summary-goto-article id nil 'force)
12670     ;; FIXME we have to wait article buffer is ready (only large buffer)
12671     ;; Is there a better solution to know that?
12672     ;; If we don't wait `bookmark-default-handler' will have no chance
12673     ;; to set position. However there is no error, just wrong pos.
12674     (sit-for 1)
12675     (when (string= buf "Gnus-art")
12676       (other-window 1))
12677     (bookmark-default-handler
12678      `(""
12679        (buffer . ,(current-buffer))
12680        . ,(bookmark-get-bookmark-record bookmark)))))
12681
12682 (gnus-summary-make-all-marking-commands)
12683
12684 (gnus-ems-redefine)
12685
12686 (provide 'gnus-sum)
12687
12688 (run-hooks 'gnus-sum-load-hook)
12689
12690 ;; Local Variables:
12691 ;; coding: iso-8859-1
12692 ;; End:
12693
12694 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12695 ;;; gnus-sum.el ends here