(gnus-summary-goto-unread): Change default to nil.
[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 nil
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   :version "24.1"
235   :group 'gnus-summary-marks
236   :link '(custom-manual "(gnus)Setting Marks")
237   :type '(choice (const :tag "off" nil)
238                  (const never)
239                  (sexp :menu-tag "on" t)))
240
241 (defcustom gnus-summary-default-score 0
242   "*Default article score level.
243 All scores generated by the score files will be added to this score.
244 If this variable is nil, scoring will be disabled."
245   :group 'gnus-score-default
246   :type '(choice (const :tag "disable")
247                  integer))
248
249 (defcustom gnus-summary-default-high-score 0
250   "*Default threshold for a high scored article.
251 An article will be highlighted as high scored if its score is greater
252 than this score."
253   :version "22.1"
254   :group 'gnus-score-default
255   :type 'integer)
256
257 (defcustom gnus-summary-default-low-score 0
258   "*Default threshold for a low scored article.
259 An article will be highlighted as low scored if its score is smaller
260 than this score."
261   :version "22.1"
262   :group 'gnus-score-default
263   :type 'integer)
264
265 (defcustom gnus-summary-zcore-fuzz 0
266   "*Fuzziness factor for the zcore in the summary buffer.
267 Articles with scores closer than this to `gnus-summary-default-score'
268 will not be marked."
269   :group 'gnus-summary-format
270   :type 'integer)
271
272 (defcustom gnus-simplify-subject-fuzzy-regexp nil
273   "*Strings to be removed when doing fuzzy matches.
274 This can either be a regular expression or list of regular expressions
275 that will be removed from subject strings if fuzzy subject
276 simplification is selected."
277   :group 'gnus-thread
278   :type '(repeat regexp))
279
280 (defcustom gnus-show-threads t
281   "*If non-nil, display threads in summary mode."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-hide-subtree nil
286   "*If non-nil, hide all threads initially.
287 This can be a predicate specifier which says which threads to hide.
288 If threads are hidden, you have to run the command
289 `gnus-summary-show-thread' by hand or select an article."
290   :group 'gnus-thread
291   :type '(radio (sexp :format "Non-nil\n"
292                       :match (lambda (widget value)
293                                (not (or (consp value) (functionp value))))
294                       :value t)
295                 (const nil)
296                 (sexp :tag "Predicate specifier")))
297
298 (defcustom gnus-thread-hide-killed t
299   "*If non-nil, hide killed threads automatically."
300   :group 'gnus-thread
301   :type 'boolean)
302
303 (defcustom gnus-thread-ignore-subject t
304   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
305 If nil, articles that have different subjects from their parents will
306 start separate threads."
307   :group 'gnus-thread
308   :type 'boolean)
309
310 (defcustom gnus-thread-operation-ignore-subject t
311   "*If non-nil, subjects will be ignored when doing thread commands.
312 This affects commands like `gnus-summary-kill-thread' and
313 `gnus-summary-lower-thread'.
314
315 If this variable is nil, articles in the same thread with different
316 subjects will not be included in the operation in question.  If this
317 variable is `fuzzy', only articles that have subjects that are fuzzily
318 equal will be included."
319   :group 'gnus-thread
320   :type '(choice (const :tag "off" nil)
321                  (const fuzzy)
322                  (sexp :tag "on" t)))
323
324 (defcustom gnus-thread-indent-level 4
325   "*Number that says how much each sub-thread should be indented."
326   :group 'gnus-thread
327   :type 'integer)
328
329 (defcustom gnus-auto-extend-newsgroup t
330   "*If non-nil, extend newsgroup forward and backward when requested."
331   :group 'gnus-summary-choose
332   :type 'boolean)
333
334 (defcustom gnus-auto-select-first t
335   "If non-nil, select an article on group entry.
336 An article is selected automatically when entering a group
337 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
338 `gnus-summary-catchup-and-goto-next-group'.
339
340 Which article is selected is controlled by the variable
341 `gnus-auto-select-subject'.
342
343 If you want to prevent automatic selection of articles in some
344 newsgroups, set the variable to nil in `gnus-select-group-hook'."
345   ;; Commands include...
346   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
347   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
349   :group 'gnus-group-select
350   :type '(choice (const :tag "none" nil)
351                  (sexp :menu-tag "first" t)))
352
353 (defcustom gnus-auto-select-subject 'unread
354   "*Says what subject to place under point when entering a group.
355
356 This variable can either be the symbols `first' (place point on the
357 first subject), `unread' (place point on the subject line of the first
358 unread article), `best' (place point on the subject line of the
359 higest-scored article), `unseen' (place point on the subject line of
360 the first unseen article), `unseen-or-unread' (place point on the subject
361 line of the first unseen article or, if all article have been seen, on the
362 subject line of the first unread article), or a function to be called to
363 place point on some subject line."
364   :version "22.1"
365   :group 'gnus-group-select
366   :type '(choice (const best)
367                  (const unread)
368                  (const first)
369                  (const unseen)
370                  (const unseen-or-unread)))
371
372 (defcustom gnus-auto-select-next t
373   "*If non-nil, offer to go to the next group from the end of the previous.
374 If the value is t and the next newsgroup is empty, Gnus will exit
375 summary mode and go back to group mode.  If the value is neither nil
376 nor t, Gnus will select the following unread newsgroup.  In
377 particular, if the value is the symbol `quietly', the next unread
378 newsgroup will be selected without any confirmation, and if it is
379 `almost-quietly', the next group will be selected without any
380 confirmation if you are located on the last article in the group.
381 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
382 will go to the next group without confirmation."
383   :group 'gnus-summary-maneuvering
384   :type '(choice (const :tag "off" nil)
385                  (const quietly)
386                  (const almost-quietly)
387                  (const slightly-quietly)
388                  (sexp :menu-tag "on" t)))
389
390 (defcustom gnus-auto-select-same nil
391   "*If non-nil, select the next article with the same subject.
392 If there are no more articles with the same subject, go to
393 the first unread article."
394   :group 'gnus-summary-maneuvering
395   :type 'boolean)
396
397 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
398   "What article should be selected after exiting an ephemeral group.
399 Valid values include:
400
401 `next'
402   Select the next article.
403 `next-unread'
404   Select the next unread article.
405 `next-noselect'
406   Move the cursor to the next article.  This is the default.
407 `next-unread-noselect'
408   Move the cursor to the next unread article.
409
410 If it has any other value or there is no next (unread) article, the
411 article selected before entering to the ephemeral group will appear."
412   :version "23.1" ;; No Gnus
413   :group 'gnus-summary-maneuvering
414   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
415                  (const next) (const next-unread)
416                  (const next-noselect) (const next-unread-noselect)
417                  (sexp :tag "other" :value nil)))
418
419 (defcustom gnus-auto-goto-ignores 'unfetched
420   "*Says how to handle unfetched articles when maneuvering.
421
422 This variable can either be the symbols nil (maneuver to any
423 article), `undownloaded' (maneuvering while unplugged ignores articles
424 that have not been fetched), `always-undownloaded' (maneuvering always
425 ignores articles that have not been fetched), `unfetched' (maneuvering
426 ignores articles whose headers have not been fetched).
427
428 NOTE: The list of unfetched articles will always be nil when plugged
429 and, when unplugged, a subset of the undownloaded article list."
430   :version "22.1"
431   :group 'gnus-summary-maneuvering
432   :type '(choice (const :tag "None" nil)
433                  (const :tag "Undownloaded when unplugged" undownloaded)
434                  (const :tag "Undownloaded" always-undownloaded)
435                  (const :tag "Unfetched" unfetched)))
436
437 (defcustom gnus-summary-check-current nil
438   "*If non-nil, consider the current article when moving.
439 The \"unread\" movement commands will stay on the same line if the
440 current article is unread."
441   :group 'gnus-summary-maneuvering
442   :type 'boolean)
443
444 (defcustom gnus-auto-center-summary 2
445   "*If non-nil, always center the current summary buffer.
446 In particular, if `vertical' do only vertical recentering.  If non-nil
447 and non-`vertical', do both horizontal and vertical recentering."
448   :group 'gnus-summary-maneuvering
449   :type '(choice (const :tag "none" nil)
450                  (const vertical)
451                  (integer :tag "height")
452                  (sexp :menu-tag "both" t)))
453
454 (defvar gnus-auto-center-group t
455   "*If non-nil, always center the group buffer.")
456
457 (defcustom gnus-show-all-headers nil
458   "*If non-nil, don't hide any headers."
459   :group 'gnus-article-hiding
460   :group 'gnus-article-headers
461   :type 'boolean)
462
463 (defcustom gnus-summary-ignore-duplicates nil
464   "*If non-nil, ignore articles with identical Message-ID headers."
465   :group 'gnus-summary
466   :type 'boolean)
467
468 (defcustom gnus-single-article-buffer t
469   "*If non-nil, display all articles in the same buffer.
470 If nil, each group will get its own article buffer."
471   :group 'gnus-article-various
472   :type 'boolean)
473
474 (defcustom gnus-break-pages t
475   "*If non-nil, do page breaking on articles.
476 The page delimiter is specified by the `gnus-page-delimiter'
477 variable."
478   :group 'gnus-article-various
479   :type 'boolean)
480
481 (defcustom gnus-move-split-methods nil
482   "*Variable used to suggest where articles are to be moved to.
483 It uses the same syntax as the `gnus-split-methods' variable.
484 However, whereas `gnus-split-methods' specifies file names as targets,
485 this variable specifies group names."
486   :group 'gnus-summary-mail
487   :type '(repeat (choice (list :value (fun) function)
488                          (cons :value ("" "") regexp (repeat string))
489                          (sexp :value nil))))
490
491 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
492   "Function used to compute default prefix for article move/copy/etc prompts.
493 The function should take one argument, a group name, and return a
494 string with the suggested prefix."
495   :group 'gnus-summary-mail
496   :type 'function)
497
498 ;; FIXME: Although the custom type is `character' for the following variables,
499 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
500
501 (defcustom gnus-unread-mark ?           ;Whitespace
502   "*Mark used for unread articles."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-ticked-mark ?!
507   "*Mark used for ticked articles."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-dormant-mark ??
512   "*Mark used for dormant articles."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-del-mark ?r
517   "*Mark used for del'd articles."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-read-mark ?R
522   "*Mark used for read articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-expirable-mark ?E
527   "*Mark used for expirable articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-killed-mark ?K
532   "*Mark used for killed articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-spam-mark ?$
537   "*Mark used for spam articles."
538   :version "22.1"
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-kill-file-mark ?X
543   "*Mark used for articles killed by kill files."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-low-score-mark ?Y
548   "*Mark used for articles with a low score."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-catchup-mark ?C
553   "*Mark used for articles that are caught up."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-replied-mark ?A
558   "*Mark used for articles that have been replied to."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-forwarded-mark ?F
563   "*Mark used for articles that have been forwarded."
564   :version "22.1"
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-recent-mark ?N
569   "*Mark used for articles that are recent."
570   :version "22.1"
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-cached-mark ?*
575   "*Mark used for articles that are in the cache."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-saved-mark ?S
580   "*Mark used for articles that have been saved."
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-unseen-mark ?.
585   "*Mark used for articles that haven't been seen."
586   :version "22.1"
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-no-mark ?               ;Whitespace
591   "*Mark used for articles that have no other secondary mark."
592   :version "22.1"
593   :group 'gnus-summary-marks
594   :type 'character)
595
596 (defcustom gnus-ancient-mark ?O
597   "*Mark used for ancient articles."
598   :group 'gnus-summary-marks
599   :type 'character)
600
601 (defcustom gnus-sparse-mark ?Q
602   "*Mark used for sparsely reffed articles."
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-canceled-mark ?G
607   "*Mark used for canceled articles."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-duplicate-mark ?M
612   "*Mark used for duplicate articles."
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-undownloaded-mark ?-
617   "*Mark used for articles that weren't downloaded."
618   :version "22.1"
619   :group 'gnus-summary-marks
620   :type 'character)
621
622 (defcustom gnus-downloaded-mark ?+
623   "*Mark used for articles that were downloaded."
624   :group 'gnus-summary-marks
625   :type 'character)
626
627 (defcustom gnus-downloadable-mark ?%
628   "*Mark used for articles that are to be downloaded."
629   :group 'gnus-summary-marks
630   :type 'character)
631
632 (defcustom gnus-unsendable-mark ?=
633   "*Mark used for articles that won't be sent."
634   :group 'gnus-summary-marks
635   :type 'character)
636
637 (defcustom gnus-score-over-mark ?+
638   "*Score mark used for articles with high scores."
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-score-below-mark ?-
643   "*Score mark used for articles with low scores."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-empty-thread-mark ?     ;Whitespace
648   "*There is no thread under the article."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-not-empty-thread-mark ?=
653   "*There is a thread under the article."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-view-pseudo-asynchronously nil
658   "*If non-nil, Gnus will view pseudo-articles asynchronously."
659   :group 'gnus-extract-view
660   :type 'boolean)
661
662 (defcustom gnus-auto-expirable-marks
663   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
664         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
665         gnus-duplicate-mark)
666   "*The list of marks converted into expiration if a group is auto-expirable."
667   :version "24.1"
668   :group 'gnus-summary
669   :type '(repeat character))
670
671 (defcustom gnus-inhibit-user-auto-expire t
672   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
673   :version "21.1"
674   :group 'gnus-summary
675   :type 'boolean)
676
677 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
678   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
679 If nil, the expirable marks will be unchanged except that the marks
680 will be removed when copying or moving articles to a group that has
681 not turned auto-expire on.  If non-nil, articles that have been read
682 will be marked as expirable when being copied or moved to a group in
683 which auto-expire is turned on."
684   :version "23.2"
685   :type 'boolean
686   :group 'gnus-summary-marks)
687
688 (defcustom gnus-view-pseudos nil
689   "*If `automatic', pseudo-articles will be viewed automatically.
690 If `not-confirm', pseudos will be viewed automatically, and the user
691 will not be asked to confirm the command."
692   :group 'gnus-extract-view
693   :type '(choice (const :tag "off" nil)
694                  (const automatic)
695                  (const not-confirm)))
696
697 (defcustom gnus-view-pseudos-separately t
698   "*If non-nil, one pseudo-article will be created for each file to be viewed.
699 If nil, all files that use the same viewing command will be given as a
700 list of parameters to that command."
701   :group 'gnus-extract-view
702   :type 'boolean)
703
704 (defcustom gnus-insert-pseudo-articles t
705   "*If non-nil, insert pseudo-articles when decoding articles."
706   :group 'gnus-extract-view
707   :type 'boolean)
708
709 (defcustom gnus-summary-dummy-line-format
710   "   %(:                             :%) %S\n"
711   "*The format specification for the dummy roots in the summary buffer.
712 It works along the same lines as a normal formatting string,
713 with some simple extensions.
714
715 %S  The subject
716
717 General format specifiers can also be used.
718 See `(gnus)Formatting Variables'."
719   :link '(custom-manual "(gnus)Formatting Variables")
720   :group 'gnus-threading
721   :type 'string)
722
723 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
724   "*The format specification for the summary mode line.
725 It works along the same lines as a normal formatting string,
726 with some simple extensions:
727
728 %G  Group name
729 %p  Unprefixed group name
730 %A  Current article number
731 %z  Current article score
732 %V  Gnus version
733 %U  Number of unread articles in the group
734 %e  Number of unselected articles in the group
735 %Z  A string with unread/unselected article counts
736 %g  Shortish group name
737 %S  Subject of the current article
738 %u  User-defined spec
739 %s  Current score file name
740 %d  Number of dormant articles
741 %r  Number of articles that have been marked as read in this session
742 %E  Number of articles expunged by the score files"
743   :group 'gnus-summary-format
744   :type 'string)
745
746 (defcustom gnus-list-identifiers nil
747   "Regexp that matches list identifiers to be removed from subject.
748 This can also be a list of regexps."
749   :version "21.1"
750   :group 'gnus-summary-format
751   :group 'gnus-article-hiding
752   :type '(choice (const :tag "none" nil)
753                  (regexp :value ".*")
754                  (repeat :value (".*") regexp)))
755
756 (defcustom gnus-summary-mark-below 0
757   "*Mark all articles with a score below this variable as read.
758 This variable is local to each summary buffer and usually set by the
759 score file."
760   :group 'gnus-score-default
761   :type 'integer)
762
763 (defun gnus-widget-reversible-match (widget value)
764   "Ignoring WIDGET, convert VALUE to internal form.
765 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
766   ;; (debug value)
767   (or (symbolp value)
768       (and (listp value)
769            (eq (length value) 2)
770            (eq (nth 0 value) 'not)
771            (symbolp (nth 1 value)))))
772
773 (defun gnus-widget-reversible-to-internal (widget value)
774   "Ignoring WIDGET, convert VALUE to internal form.
775 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
776 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
777   ;; (debug value)
778   (if (atom value)
779       (list value nil)
780     (list (nth 1 value) t)))
781
782 (defun gnus-widget-reversible-to-external (widget value)
783   "Ignoring WIDGET, convert VALUE to external form.
784 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
785 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
786   ;; (debug value)
787   (if (nth 1 value)
788       (list 'not (nth 0 value))
789     (nth 0 value)))
790
791 (define-widget 'gnus-widget-reversible 'group
792   "A `group' that convert values."
793   :match 'gnus-widget-reversible-match
794   :value-to-internal 'gnus-widget-reversible-to-internal
795   :value-to-external 'gnus-widget-reversible-to-external)
796
797 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
798   "*List of functions used for sorting articles in the summary buffer.
799
800 Each function takes two articles and returns non-nil if the first
801 article should be sorted before the other.  If you use more than one
802 function, the primary sort function should be the last.  You should
803 probably always include `gnus-article-sort-by-number' in the list of
804 sorting functions -- preferably first.  Also note that sorting by date
805 is often much slower than sorting by number, and the sorting order is
806 very similar.  (Sorting by date means sorting by the time the message
807 was sent, sorting by number means sorting by arrival time.)
808
809 Each item can also be a list `(not F)' where F is a function;
810 this reverses the sort order.
811
812 Ready-made functions include `gnus-article-sort-by-number',
813 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
814 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
815 and `gnus-article-sort-by-score'.
816
817 When threading is turned on, the variable `gnus-thread-sort-functions'
818 controls how articles are sorted."
819   :group 'gnus-summary-sort
820   :type '(repeat (gnus-widget-reversible
821                   (choice (function-item gnus-article-sort-by-number)
822                           (function-item gnus-article-sort-by-author)
823                           (function-item gnus-article-sort-by-subject)
824                           (function-item gnus-article-sort-by-date)
825                           (function-item gnus-article-sort-by-score)
826                           (function-item gnus-article-sort-by-random)
827                           (function :tag "other"))
828                   (boolean :tag "Reverse order"))))
829
830
831 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
832   "*List of functions used for sorting threads in the summary buffer.
833 By default, threads are sorted by article number.
834
835 Each function takes two threads and returns non-nil if the first
836 thread should be sorted before the other.  If you use more than one
837 function, the primary sort function should be the last.  You should
838 probably always include `gnus-thread-sort-by-number' in the list of
839 sorting functions -- preferably first.  Also note that sorting by date
840 is often much slower than sorting by number, and the sorting order is
841 very similar.  (Sorting by date means sorting by the time the message
842 was sent, sorting by number means sorting by arrival time.)
843
844 Each list item can also be a list `(not F)' where F is a
845 function; this specifies reversed sort order.
846
847 Ready-made functions include `gnus-thread-sort-by-number',
848 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
849 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
850 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
851 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
852 and `gnus-thread-sort-by-total-score' (see
853 `gnus-thread-score-function').
854
855 When threading is turned off, the variable
856 `gnus-article-sort-functions' controls how articles are sorted."
857   :group 'gnus-summary-sort
858   :type '(repeat
859           (gnus-widget-reversible
860            (choice (function-item gnus-thread-sort-by-number)
861                    (function-item gnus-thread-sort-by-author)
862                    (function-item gnus-thread-sort-by-recipient)
863                    (function-item gnus-thread-sort-by-subject)
864                    (function-item gnus-thread-sort-by-date)
865                    (function-item gnus-thread-sort-by-score)
866                    (function-item gnus-thread-sort-by-most-recent-number)
867                    (function-item gnus-thread-sort-by-most-recent-date)
868                    (function-item gnus-thread-sort-by-random)
869                    (function-item gnus-thread-sort-by-total-score)
870                    (function :tag "other"))
871            (boolean :tag "Reverse order"))))
872
873 (defcustom gnus-thread-score-function '+
874   "*Function used for calculating the total score of a thread.
875
876 The function is called with the scores of the article and each
877 subthread and should then return the score of the thread.
878
879 Some functions you can use are `+', `max', or `min'."
880   :group 'gnus-summary-sort
881   :type 'function)
882
883 (defcustom gnus-summary-expunge-below nil
884   "All articles that have a score less than this variable will be expunged.
885 This variable is local to the summary buffers."
886   :group 'gnus-score-default
887   :type '(choice (const :tag "off" nil)
888                  integer))
889
890 (defcustom gnus-thread-expunge-below nil
891   "All threads that have a total score less than this variable will be expunged.
892 See `gnus-thread-score-function' for en explanation of what a
893 \"thread score\" is.
894
895 This variable is local to the summary buffers."
896   :group 'gnus-threading
897   :group 'gnus-score-default
898   :type '(choice (const :tag "off" nil)
899                  integer))
900
901 (defcustom gnus-summary-mode-hook nil
902   "*A hook for Gnus summary mode.
903 This hook is run before any variables are set in the summary buffer."
904   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
905   :group 'gnus-summary-various
906   :type 'hook)
907
908 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
909 (when (featurep 'xemacs)
910   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
911   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
912   (add-hook 'gnus-summary-mode-hook
913             'gnus-xmas-switch-horizontal-scrollbar-off))
914
915 (defcustom gnus-summary-menu-hook nil
916   "*Hook run after the creation of the summary mode menu."
917   :group 'gnus-summary-visual
918   :type 'hook)
919
920 (defcustom gnus-summary-exit-hook nil
921   "*A hook called on exit from the summary buffer.
922 It will be called with point in the group buffer."
923   :group 'gnus-summary-exit
924   :type 'hook)
925
926 (defcustom gnus-summary-prepare-hook nil
927   "*A hook called after the summary buffer has been generated.
928 If you want to modify the summary buffer, you can use this hook."
929   :group 'gnus-summary-various
930   :type 'hook)
931
932 (defcustom gnus-summary-prepared-hook nil
933   "*A hook called as the last thing after the summary buffer has been generated."
934   :group 'gnus-summary-various
935   :type 'hook)
936
937 (defcustom gnus-summary-generate-hook nil
938   "*A hook run just before generating the summary buffer.
939 This hook is commonly used to customize threading variables and the
940 like."
941   :group 'gnus-summary-various
942   :type 'hook)
943
944 (defcustom gnus-select-group-hook nil
945   "*A hook called when a newsgroup is selected.
946
947 If you'd like to simplify subjects like the
948 `gnus-summary-next-same-subject' command does, you can use the
949 following hook:
950
951  (add-hook gnus-select-group-hook
952            (lambda ()
953              (mapcar (lambda (header)
954                        (mail-header-set-subject
955                         header
956                         (gnus-simplify-subject
957                          (mail-header-subject header) 're-only)))
958                      gnus-newsgroup-headers)))"
959   :group 'gnus-group-select
960   :type 'hook)
961
962 (defcustom gnus-select-article-hook nil
963   "*A hook called when an article is selected."
964   :group 'gnus-summary-choose
965   :options '(gnus-agent-fetch-selected-article)
966   :type 'hook)
967
968 (defcustom gnus-visual-mark-article-hook
969   (list 'gnus-highlight-selected-summary)
970   "*Hook run after selecting an article in the summary buffer.
971 It is meant to be used for highlighting the article in some way.  It
972 is not run if `gnus-visual' is nil."
973   :group 'gnus-summary-visual
974   :type 'hook)
975
976 (defcustom gnus-parse-headers-hook nil
977   "*A hook called before parsing the headers."
978   :group 'gnus-various
979   :type 'hook)
980
981 (defcustom gnus-exit-group-hook nil
982   "*A hook called when exiting summary mode.
983 This hook is not called from the non-updating exit commands like `Q'."
984   :group 'gnus-various
985   :type 'hook)
986
987 (defcustom gnus-summary-update-hook
988   (list 'gnus-summary-highlight-line)
989   "*A hook called when a summary line is changed.
990 The hook will not be called if `gnus-visual' is nil.
991
992 The default function `gnus-summary-highlight-line' will
993 highlight the line according to the `gnus-summary-highlight'
994 variable."
995   :group 'gnus-summary-visual
996   :type 'hook)
997
998 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
999   "*A hook called when an article is selected for the first time.
1000 The hook is intended to mark an article as read (or unread)
1001 automatically when it is selected."
1002   :group 'gnus-summary-choose
1003   :type 'hook)
1004
1005 (defcustom gnus-group-no-more-groups-hook nil
1006   "*A hook run when returning to group mode having no more (unread) groups."
1007   :group 'gnus-group-select
1008   :type 'hook)
1009
1010 (defcustom gnus-ps-print-hook nil
1011   "*A hook run before ps-printing something from Gnus."
1012   :group 'gnus-summary
1013   :type 'hook)
1014
1015 (defcustom gnus-summary-article-move-hook nil
1016   "*A hook called after an article is moved, copied, respooled, or crossposted."
1017   :version "22.1"
1018   :group 'gnus-summary
1019   :type 'hook)
1020
1021 (defcustom gnus-summary-article-delete-hook nil
1022   "*A hook called after an article is deleted."
1023   :version "22.1"
1024   :group 'gnus-summary
1025   :type 'hook)
1026
1027 (defcustom gnus-summary-article-expire-hook nil
1028   "*A hook called after an article is expired."
1029   :version "22.1"
1030   :group 'gnus-summary
1031   :type 'hook)
1032
1033 (defcustom gnus-summary-display-arrow
1034   (and (fboundp 'display-graphic-p)
1035        (display-graphic-p))
1036   "*If non-nil, display an arrow highlighting the current article."
1037   :version "22.1"
1038   :group 'gnus-summary
1039   :type 'boolean)
1040
1041 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1042   "Face used for highlighting the current article in the summary buffer."
1043   :group 'gnus-summary-visual
1044   :type 'face)
1045
1046 (defvar gnus-tmp-downloaded nil)
1047
1048 (defcustom gnus-summary-highlight
1049   '(((eq mark gnus-canceled-mark)
1050      . gnus-summary-cancelled)
1051     ((and uncached (> score default-high))
1052      . gnus-summary-high-undownloaded)
1053     ((and uncached (< score default-low))
1054      . gnus-summary-low-undownloaded)
1055     (uncached
1056      . gnus-summary-normal-undownloaded)
1057     ((and (> score default-high)
1058           (or (eq mark gnus-dormant-mark)
1059               (eq mark gnus-ticked-mark)))
1060      . gnus-summary-high-ticked)
1061     ((and (< score default-low)
1062           (or (eq mark gnus-dormant-mark)
1063               (eq mark gnus-ticked-mark)))
1064      . gnus-summary-low-ticked)
1065     ((or (eq mark gnus-dormant-mark)
1066          (eq mark gnus-ticked-mark))
1067      . gnus-summary-normal-ticked)
1068     ((and (> score default-high) (eq mark gnus-ancient-mark))
1069      . gnus-summary-high-ancient)
1070     ((and (< score default-low) (eq mark gnus-ancient-mark))
1071      . gnus-summary-low-ancient)
1072     ((eq mark gnus-ancient-mark)
1073      . gnus-summary-normal-ancient)
1074     ((and (> score default-high) (eq mark gnus-unread-mark))
1075      . gnus-summary-high-unread)
1076     ((and (< score default-low) (eq mark gnus-unread-mark))
1077      . gnus-summary-low-unread)
1078     ((eq mark gnus-unread-mark)
1079      . gnus-summary-normal-unread)
1080     ((> score default-high)
1081      . gnus-summary-high-read)
1082     ((< score default-low)
1083      . gnus-summary-low-read)
1084     (t
1085      . gnus-summary-normal-read))
1086   "*Controls the highlighting of summary buffer lines.
1087
1088 A list of (FORM . FACE) pairs.  When deciding how a particular
1089 summary line should be displayed, each form is evaluated.  The content
1090 of the face field after the first true form is used.  You can change
1091 how those summary lines are displayed, by editing the face field.
1092
1093 You can use the following variables in the FORM field.
1094
1095 score:        The article's score.
1096 default:      The default article score.
1097 default-high: The default score for high scored articles.
1098 default-low:  The default score for low scored articles.
1099 below:        The score below which articles are automatically marked as read.
1100 mark:         The article's mark.
1101 uncached:     Non-nil if the article is uncached."
1102   :group 'gnus-summary-visual
1103   :type '(repeat (cons (sexp :tag "Form" nil)
1104                        face)))
1105 (put 'gnus-summary-highlight 'risky-local-variable t)
1106
1107 (defcustom gnus-alter-header-function nil
1108   "Function called to allow alteration of article header structures.
1109 The function is called with one parameter, the article header vector,
1110 which it may alter in any way."
1111   :type '(choice (const :tag "None" nil)
1112                  function)
1113   :group 'gnus-summary)
1114
1115 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1116   "Function used to decode a string with encoded words.")
1117
1118 (defvar gnus-decode-encoded-address-function
1119   'mail-decode-encoded-address-string
1120   "Function used to decode addresses with encoded words.")
1121
1122 (defcustom gnus-extra-headers '(To Newsgroups)
1123   "*Extra headers to parse."
1124   :version "21.1"
1125   :group 'gnus-summary
1126   :type '(repeat symbol))
1127
1128 (defcustom gnus-ignored-from-addresses
1129   (and user-mail-address
1130        (not (string= user-mail-address ""))
1131        (regexp-quote user-mail-address))
1132   "*From headers that may be suppressed in favor of To headers.
1133 This can be a regexp or a list of regexps."
1134   :version "21.1"
1135   :group 'gnus-summary
1136   :type '(choice regexp
1137                  (repeat :tag "Regexp List" regexp)))
1138
1139 (defsubst gnus-ignored-from-addresses ()
1140   (gmm-regexp-concat gnus-ignored-from-addresses))
1141
1142 (defcustom gnus-summary-to-prefix "-> "
1143   "*String prefixed to the To field in the summary line when
1144 using `gnus-ignored-from-addresses'."
1145   :version "22.1"
1146   :group 'gnus-summary
1147   :type 'string)
1148
1149 (defcustom gnus-summary-newsgroup-prefix "=> "
1150   "*String prefixed to the Newsgroup field in the summary
1151 line when using `gnus-ignored-from-addresses'."
1152   :version "22.1"
1153   :group 'gnus-summary
1154   :type 'string)
1155
1156 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1157   "List of charsets that should be ignored.
1158 When these charsets are used in the \"charset\" parameter, the
1159 default charset will be used instead."
1160   :version "21.1"
1161   :type '(repeat symbol)
1162   :group 'gnus-charset)
1163
1164 (defcustom gnus-newsgroup-maximum-articles nil
1165   "The maximum number of articles a newsgroup.
1166 If this is a number, old articles in a newsgroup exceeding this number
1167 are silently ignored.  If it is nil, no article is ignored.  Note that
1168 setting this variable to a number might prevent you from reading very
1169 old articles."
1170   :group 'gnus-group-select
1171   :version "22.2"
1172   :type '(choice (const :tag "No limit" nil)
1173                  integer))
1174
1175 (gnus-define-group-parameter
1176  ignored-charsets
1177  :type list
1178  :function-document
1179  "Return the ignored charsets of GROUP."
1180  :variable gnus-group-ignored-charsets-alist
1181  :variable-default
1182  '(("alt\\.chinese\\.text" iso-8859-1))
1183  :variable-document
1184  "Alist of regexps (to match group names) and charsets that should be ignored.
1185 When these charsets are used in the \"charset\" parameter, the
1186 default charset will be used instead."
1187  :variable-group gnus-charset
1188  :variable-type '(repeat (cons (regexp :tag "Group")
1189                                (repeat symbol)))
1190  :parameter-type '(choice :tag "Ignored charsets"
1191                           :value nil
1192                           (repeat (symbol)))
1193  :parameter-document       "\
1194 List of charsets that should be ignored.
1195
1196 When these charsets are used in the \"charset\" parameter, the
1197 default charset will be used instead.")
1198
1199 (defcustom gnus-group-highlight-words-alist nil
1200   "Alist of group regexps and highlight regexps.
1201 This variable uses the same syntax as `gnus-emphasis-alist'."
1202   :version "21.1"
1203   :type '(repeat (cons (regexp :tag "Group")
1204                        (repeat (list (regexp :tag "Highlight regexp")
1205                                      (number :tag "Group for entire word" 0)
1206                                      (number :tag "Group for displayed part" 0)
1207                                      (symbol :tag "Face"
1208                                              gnus-emphasis-highlight-words)))))
1209   :group 'gnus-summary-visual)
1210
1211 (defcustom gnus-summary-show-article-charset-alist
1212   nil
1213   "Alist of number and charset.
1214 The article will be shown with the charset corresponding to the
1215 numbered argument.
1216 For example: ((1 . cn-gb-2312) (2 . big5))."
1217   :version "21.1"
1218   :type '(repeat (cons (number :tag "Argument" 1)
1219                        (symbol :tag "Charset")))
1220   :group 'gnus-charset)
1221
1222 (defcustom gnus-preserve-marks t
1223   "Whether marks are preserved when moving, copying and respooling messages."
1224   :version "21.1"
1225   :type 'boolean
1226   :group 'gnus-summary-marks)
1227
1228 (defcustom gnus-propagate-marks t
1229   "If non-nil, do not propagate marks to the backends."
1230   :version "23.1" ;; No Gnus
1231   :type 'boolean
1232   :group 'gnus-summary-marks)
1233
1234 (defcustom gnus-alter-articles-to-read-function nil
1235   "Function to be called to alter the list of articles to be selected."
1236   :type '(choice (const nil) function)
1237   :group 'gnus-summary)
1238
1239 (defcustom gnus-orphan-score nil
1240   "*All orphans get this score added.  Set in the score file."
1241   :group 'gnus-score-default
1242   :type '(choice (const nil)
1243                  integer))
1244
1245 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1246   "*A regexp to match MIME parts when saving multiple parts of a
1247 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1248 This regexp will be used by default when prompting the user for which
1249 type of files to save."
1250   :group 'gnus-summary
1251   :type 'regexp)
1252
1253 (defcustom gnus-read-all-available-headers nil
1254   "Whether Gnus should parse all headers made available to it.
1255 This is mostly relevant for slow back ends where the user may
1256 wish to widen the summary buffer to include all headers
1257 that were fetched."
1258   :version "22.1"
1259   :group 'gnus-summary
1260   :type '(choice boolean regexp))
1261
1262 (defcustom gnus-summary-pipe-output-default-command nil
1263   "Command (and optional arguments) used to pipe article to subprocess.
1264 This will be used as the default command if it is non-nil.  The value
1265 will be updated if you modify it when executing the command
1266 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1267   :version "23.1" ;; No Gnus
1268   :group 'gnus-summary
1269   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1270
1271 (defcustom gnus-summary-muttprint-program "muttprint"
1272   "Command (and optional arguments) used to run Muttprint.
1273 The value will be updated if you modify it when executing the command
1274 `gnus-summary-muttprint'."
1275   :version "22.1"
1276   :group 'gnus-summary
1277   :type 'string)
1278
1279 (defcustom gnus-article-loose-mime t
1280   "If non-nil, don't require MIME-Version header.
1281 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1282 supply the MIME-Version header or deliberately strip it from the mail.
1283 If non-nil (the default), Gnus will treat some articles as MIME
1284 even if the MIME-Version header is missing."
1285   :version "22.1"
1286   :type 'boolean
1287   :group 'gnus-article-mime)
1288
1289 (defcustom gnus-article-emulate-mime t
1290   "If non-nil, use MIME emulation for uuencode and the like.
1291 This means that Gnus will search message bodies for text that look
1292 like uuencoded bits, yEncoded bits, and so on, and present that using
1293 the normal Gnus MIME machinery."
1294   :version "22.1"
1295   :type 'boolean
1296   :group 'gnus-article-mime)
1297
1298 ;;; Internal variables
1299
1300 (defvar gnus-summary-display-cache nil)
1301 (defvar gnus-article-mime-handles nil)
1302 (defvar gnus-article-decoded-p nil)
1303 (defvar gnus-article-charset nil)
1304 (defvar gnus-article-ignored-charsets nil)
1305 (defvar gnus-scores-exclude-files nil)
1306 (defvar gnus-page-broken nil)
1307
1308 (defvar gnus-original-article nil)
1309 (defvar gnus-article-internal-prepare-hook nil)
1310 (defvar gnus-newsgroup-process-stack nil)
1311
1312 (defvar gnus-thread-indent-array nil)
1313 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1314 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1315   "Function called to sort the articles within a thread after it has been gathered together.")
1316
1317 (defvar gnus-summary-save-parts-type-history nil)
1318 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1319
1320 ;; Avoid highlighting in kill files.
1321 (defvar gnus-summary-inhibit-highlight nil)
1322 (defvar gnus-newsgroup-selected-overlay nil)
1323 (defvar gnus-inhibit-limiting nil)
1324 (defvar gnus-newsgroup-adaptive-score-file nil)
1325 (defvar gnus-current-score-file nil)
1326 (defvar gnus-current-move-group nil)
1327 (defvar gnus-current-copy-group nil)
1328 (defvar gnus-current-crosspost-group nil)
1329 (defvar gnus-newsgroup-display nil)
1330
1331 (defvar gnus-newsgroup-dependencies nil)
1332 (defvar gnus-newsgroup-adaptive nil)
1333 (defvar gnus-summary-display-article-function nil)
1334 (defvar gnus-summary-highlight-line-function nil
1335   "Function called after highlighting a summary line.")
1336
1337 (defvar gnus-summary-line-format-alist
1338   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1339     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1340     (?s gnus-tmp-subject-or-nil ?s)
1341     (?n gnus-tmp-name ?s)
1342     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1343         ?s)
1344     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1345             gnus-tmp-from) ?s)
1346     (?F gnus-tmp-from ?s)
1347     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1348     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1349     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1350     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1351     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1352     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1353     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1354     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1355     (?L gnus-tmp-lines ?s)
1356     (?O gnus-tmp-downloaded ?c)
1357     (?I gnus-tmp-indentation ?s)
1358     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1359     (?R gnus-tmp-replied ?c)
1360     (?\[ gnus-tmp-opening-bracket ?c)
1361     (?\] gnus-tmp-closing-bracket ?c)
1362     (?\> (make-string gnus-tmp-level ? ) ?s)
1363     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1364     (?i gnus-tmp-score ?d)
1365     (?z gnus-tmp-score-char ?c)
1366     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1367     (?U gnus-tmp-unread ?c)
1368     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1369         ?s)
1370     (?t (gnus-summary-number-of-articles-in-thread
1371          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1372         ?d)
1373     (?e (gnus-summary-number-of-articles-in-thread
1374          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1375         ?c)
1376     (?u gnus-tmp-user-defined ?s)
1377     (?P (gnus-pick-line-number) ?d)
1378     (?B gnus-tmp-thread-tree-header-string ?s)
1379     (user-date (gnus-user-date
1380                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1381   "An alist of format specifications that can appear in summary lines.
1382 These are paired with what variables they correspond with, along with
1383 the type of the variable (string, integer, character, etc).")
1384
1385 (defvar gnus-summary-dummy-line-format-alist
1386   `((?S gnus-tmp-subject ?s)
1387     (?N gnus-tmp-number ?d)
1388     (?u gnus-tmp-user-defined ?s)))
1389
1390 (defvar gnus-summary-mode-line-format-alist
1391   `((?G gnus-tmp-group-name ?s)
1392     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1393     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1394     (?A gnus-tmp-article-number ?d)
1395     (?Z gnus-tmp-unread-and-unselected ?s)
1396     (?V gnus-version ?s)
1397     (?U gnus-tmp-unread-and-unticked ?d)
1398     (?S gnus-tmp-subject ?s)
1399     (?e gnus-tmp-unselected ?d)
1400     (?u gnus-tmp-user-defined ?s)
1401     (?d (length gnus-newsgroup-dormant) ?d)
1402     (?t (length gnus-newsgroup-marked) ?d)
1403     (?h (length gnus-newsgroup-spam-marked) ?d)
1404     (?r (length gnus-newsgroup-reads) ?d)
1405     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1406     (?E gnus-newsgroup-expunged-tally ?d)
1407     (?s (gnus-current-score-file-nondirectory) ?s)))
1408
1409 ;; This is here rather than in gnus-art for compilation reasons.
1410 (defvar gnus-article-mode-line-format-alist
1411   (nconc '((?w (gnus-article-wash-status) ?s)
1412            (?m (gnus-article-mime-part-status) ?s))
1413          gnus-summary-mode-line-format-alist))
1414
1415 (defvar gnus-last-search-regexp nil
1416   "Default regexp for article search command.")
1417
1418 (defvar gnus-last-shell-command nil
1419   "Default shell command on article.")
1420
1421 (defvar gnus-newsgroup-agentized nil
1422   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1423 (defvar gnus-newsgroup-begin nil)
1424 (defvar gnus-newsgroup-end nil)
1425 (defvar gnus-newsgroup-last-rmail nil)
1426 (defvar gnus-newsgroup-last-mail nil)
1427 (defvar gnus-newsgroup-last-folder nil)
1428 (defvar gnus-newsgroup-last-file nil)
1429 (defvar gnus-newsgroup-last-directory nil)
1430 (defvar gnus-newsgroup-auto-expire nil)
1431 (defvar gnus-newsgroup-active nil)
1432
1433 (defvar gnus-newsgroup-data nil)
1434 (defvar gnus-newsgroup-data-reverse nil)
1435 (defvar gnus-newsgroup-limit nil)
1436 (defvar gnus-newsgroup-limits nil)
1437 (defvar gnus-summary-use-undownloaded-faces nil)
1438
1439 (defvar gnus-newsgroup-unreads nil
1440   "Sorted list of unread articles in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-unselected nil
1443   "Sorted list of unselected unread articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-reads nil
1446   "Alist of read articles and article marks in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-expunged-tally nil)
1449
1450 (defvar gnus-newsgroup-marked nil
1451   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1452
1453 (defvar gnus-newsgroup-spam-marked nil
1454   "List of ranges of articles that have been marked as spam.")
1455
1456 (defvar gnus-newsgroup-killed nil
1457   "List of ranges of articles that have been through the scoring process.")
1458
1459 (defvar gnus-newsgroup-cached nil
1460   "Sorted list of articles that come from the article cache.")
1461
1462 (defvar gnus-newsgroup-saved nil
1463   "List of articles that have been saved.")
1464
1465 (defvar gnus-newsgroup-kill-headers nil)
1466
1467 (defvar gnus-newsgroup-replied nil
1468   "List of articles that have been replied to in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-forwarded nil
1471   "List of articles that have been forwarded in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-recent nil
1474   "List of articles that have are recent in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-expirable nil
1477   "Sorted list of articles in the current newsgroup that can be expired.")
1478
1479 (defvar gnus-newsgroup-processable nil
1480   "List of articles in the current newsgroup that can be processed.")
1481
1482 (defvar gnus-newsgroup-downloadable nil
1483   "Sorted list of articles in the current newsgroup that can be processed.")
1484
1485 (defvar gnus-newsgroup-unfetched nil
1486   "Sorted list of articles in the current newsgroup whose headers have
1487 not been fetched into the agent.
1488
1489 This list will always be a subset of gnus-newsgroup-undownloaded.")
1490
1491 (defvar gnus-newsgroup-undownloaded nil
1492   "List of articles in the current newsgroup that haven't been downloaded.")
1493
1494 (defvar gnus-newsgroup-unsendable nil
1495   "List of articles in the current newsgroup that won't be sent.")
1496
1497 (defvar gnus-newsgroup-bookmarks nil
1498   "List of articles in the current newsgroup that have bookmarks.")
1499
1500 (defvar gnus-newsgroup-dormant nil
1501   "Sorted list of dormant articles in the current newsgroup.")
1502
1503 (defvar gnus-newsgroup-unseen nil
1504   "List of unseen articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-seen nil
1507   "Range of seen articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-articles nil
1510   "List of articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-scored nil
1513   "List of scored articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-headers nil
1516   "List of article headers in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-threads nil)
1519
1520 (defvar gnus-newsgroup-prepared nil
1521   "Whether the current group has been prepared properly.")
1522
1523 (defvar gnus-newsgroup-ancient nil
1524   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-sparse nil)
1527
1528 (defvar gnus-current-article nil)
1529 (defvar gnus-article-current nil)
1530 (defvar gnus-current-headers nil)
1531 (defvar gnus-have-all-headers nil)
1532 (defvar gnus-last-article nil)
1533 (defvar gnus-newsgroup-history nil)
1534 (defvar gnus-newsgroup-charset nil)
1535 (defvar gnus-newsgroup-ephemeral-charset nil)
1536 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1537
1538 (defvar gnus-article-before-search nil)
1539
1540 (defvar gnus-summary-local-variables
1541   '(gnus-newsgroup-name
1542     gnus-newsgroup-begin gnus-newsgroup-end
1543     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1544     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1545     gnus-newsgroup-last-directory
1546     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1547     gnus-newsgroup-unselected gnus-newsgroup-marked
1548     gnus-newsgroup-spam-marked
1549     gnus-newsgroup-reads gnus-newsgroup-saved
1550     gnus-newsgroup-replied gnus-newsgroup-forwarded
1551     gnus-newsgroup-recent
1552     gnus-newsgroup-expirable
1553     gnus-newsgroup-processable gnus-newsgroup-killed
1554     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1555     gnus-newsgroup-unfetched
1556     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1557     gnus-newsgroup-seen gnus-newsgroup-articles
1558     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1559     gnus-newsgroup-headers gnus-newsgroup-threads
1560     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1561     gnus-current-article gnus-current-headers gnus-have-all-headers
1562     gnus-last-article gnus-article-internal-prepare-hook
1563     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1564     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1565     gnus-thread-expunge-below
1566     gnus-score-alist gnus-current-score-file
1567     (gnus-summary-expunge-below . global)
1568     (gnus-summary-mark-below . global)
1569     (gnus-orphan-score . global)
1570     gnus-newsgroup-active gnus-scores-exclude-files
1571     gnus-newsgroup-history gnus-newsgroup-ancient
1572     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1573     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1574     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1575     (gnus-newsgroup-expunged-tally . 0)
1576     gnus-cache-removable-articles gnus-newsgroup-cached
1577     gnus-newsgroup-data gnus-newsgroup-data-reverse
1578     gnus-newsgroup-limit gnus-newsgroup-limits
1579     gnus-newsgroup-charset gnus-newsgroup-display
1580     gnus-summary-use-undownloaded-faces)
1581   "Variables that are buffer-local to the summary buffers.")
1582
1583 (defvar gnus-newsgroup-variables nil
1584   "A list of variables that have separate values in different newsgroups.
1585 A list of newsgroup (summary buffer) local variables, or cons of
1586 variables and their default expressions to be evalled (when the default
1587 values are not nil), that should be made global while the summary buffer
1588 is active.
1589
1590 Note: The default expressions will be evaluated (using function `eval')
1591 before assignment to the local variable rather than just assigned to it.
1592 If the default expression is the symbol `global', that symbol will not
1593 be evaluated but the global value of the local variable will be used
1594 instead.
1595
1596 These variables can be used to set variables in the group parameters
1597 while still allowing them to affect operations done in other buffers.
1598 For example:
1599
1600 \(setq gnus-newsgroup-variables
1601      '(message-use-followup-to
1602        (gnus-visible-headers .
1603          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1604 ")
1605
1606 (eval-when-compile
1607   ;; Bind features so that require will believe that gnus-sum has
1608   ;; already been loaded (avoids infinite recursion)
1609   (let ((features (cons 'gnus-sum features)))
1610     (require 'gnus-art)))
1611
1612 ;; MIME stuff.
1613
1614 (defvar gnus-decode-encoded-word-methods
1615   '(mail-decode-encoded-word-string)
1616   "List of methods used to decode encoded words.
1617
1618 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1619 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1620 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1621 whose names match REGEXP.
1622
1623 For example:
1624 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1625  mail-decode-encoded-word-string
1626  (\"chinese\" . rfc1843-decode-string))")
1627
1628 (defvar gnus-decode-encoded-word-methods-cache nil)
1629
1630 (defun gnus-multi-decode-encoded-word-string (string)
1631   "Apply the functions from `gnus-encoded-word-methods' that match."
1632   (unless (and gnus-decode-encoded-word-methods-cache
1633                (eq gnus-newsgroup-name
1634                    (car gnus-decode-encoded-word-methods-cache)))
1635     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1636     (dolist (method gnus-decode-encoded-word-methods)
1637       (if (symbolp method)
1638           (nconc gnus-decode-encoded-word-methods-cache (list method))
1639         (if (and gnus-newsgroup-name
1640                  (string-match (car method) gnus-newsgroup-name))
1641             (nconc gnus-decode-encoded-word-methods-cache
1642                    (list (cdr method)))))))
1643   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1644     (setq string (funcall method string))))
1645
1646 ;; Subject simplification.
1647
1648 (defun gnus-simplify-whitespace (str)
1649   "Remove excessive whitespace from STR."
1650   ;; Multiple spaces.
1651   (while (string-match "[ \t][ \t]+" str)
1652     (setq str (concat (substring str 0 (match-beginning 0))
1653                         " "
1654                         (substring str (match-end 0)))))
1655   ;; Leading spaces.
1656   (when (string-match "^[ \t]+" str)
1657     (setq str (substring str (match-end 0))))
1658   ;; Trailing spaces.
1659   (when (string-match "[ \t]+$" str)
1660     (setq str (substring str 0 (match-beginning 0))))
1661   str)
1662
1663 (defun gnus-simplify-all-whitespace (str)
1664   "Remove all whitespace from STR."
1665   (while (string-match "[ \t\n]+" str)
1666     (setq str (replace-match "" nil nil str)))
1667   str)
1668
1669 (defsubst gnus-simplify-subject-re (subject)
1670   "Remove \"Re:\" from subject lines."
1671   (if (string-match message-subject-re-regexp subject)
1672       (substring subject (match-end 0))
1673     subject))
1674
1675 (defun gnus-simplify-subject (subject &optional re-only)
1676   "Remove `Re:' and words in parentheses.
1677 If RE-ONLY is non-nil, strip leading `Re:'s only."
1678   (let ((case-fold-search t))           ;Ignore case.
1679     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1680     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1681       (setq subject (substring subject (match-end 0))))
1682     ;; Remove uninteresting prefixes.
1683     (when (and (not re-only)
1684                gnus-simplify-ignored-prefixes
1685                (string-match gnus-simplify-ignored-prefixes subject))
1686       (setq subject (substring subject (match-end 0))))
1687     ;; Remove words in parentheses from end.
1688     (unless re-only
1689       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1690         (setq subject (substring subject 0 (match-beginning 0)))))
1691     ;; Return subject string.
1692     subject))
1693
1694 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1695 ;; all whitespace.
1696 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1697   (goto-char (point-min))
1698   (while (re-search-forward regexp nil t)
1699     (replace-match (or newtext ""))))
1700
1701 (defun gnus-simplify-buffer-fuzzy ()
1702   "Simplify string in the buffer fuzzily.
1703 The string in the accessible portion of the current buffer is simplified.
1704 It is assumed to be a single-line subject.
1705 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1706 matter is removed.  Additional things can be deleted by setting
1707 `gnus-simplify-subject-fuzzy-regexp'."
1708   (let ((case-fold-search t)
1709         (modified-tick))
1710     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1711
1712     (while (not (eq modified-tick (buffer-modified-tick)))
1713       (setq modified-tick (buffer-modified-tick))
1714       (cond
1715        ((listp gnus-simplify-subject-fuzzy-regexp)
1716         (mapc 'gnus-simplify-buffer-fuzzy-step
1717               gnus-simplify-subject-fuzzy-regexp))
1718        (gnus-simplify-subject-fuzzy-regexp
1719         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1720       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1721       (gnus-simplify-buffer-fuzzy-step
1722        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1723       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1724
1725     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1726     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1727     (gnus-simplify-buffer-fuzzy-step " $")
1728     (gnus-simplify-buffer-fuzzy-step "^ +")))
1729
1730 (defun gnus-simplify-subject-fuzzy (subject)
1731   "Simplify a subject string fuzzily.
1732 See `gnus-simplify-buffer-fuzzy' for details."
1733   (save-excursion
1734     (gnus-set-work-buffer)
1735     (let ((case-fold-search t))
1736       ;; Remove uninteresting prefixes.
1737       (when (and gnus-simplify-ignored-prefixes
1738                  (string-match gnus-simplify-ignored-prefixes subject))
1739         (setq subject (substring subject (match-end 0))))
1740       (insert subject)
1741       (inline (gnus-simplify-buffer-fuzzy))
1742       (buffer-string))))
1743
1744 (defsubst gnus-simplify-subject-fully (subject)
1745   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1746   (cond
1747    (gnus-simplify-subject-functions
1748     (gnus-map-function gnus-simplify-subject-functions subject))
1749    ((null gnus-summary-gather-subject-limit)
1750     (gnus-simplify-subject-re subject))
1751    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1752     (gnus-simplify-subject-fuzzy subject))
1753    ((numberp gnus-summary-gather-subject-limit)
1754     (truncate-string-to-width (gnus-simplify-subject-re subject)
1755                               gnus-summary-gather-subject-limit))
1756    (t
1757     subject)))
1758
1759 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1760   "Check whether two subjects are equal.
1761 If optional argument SIMPLE-FIRST is t, first argument is already
1762 simplified."
1763   (cond
1764    ((null simple-first)
1765     (equal (gnus-simplify-subject-fully s1)
1766            (gnus-simplify-subject-fully s2)))
1767    (t
1768     (equal s1
1769            (gnus-simplify-subject-fully s2)))))
1770
1771 (defun gnus-summary-bubble-group ()
1772   "Increase the score of the current group.
1773 This is a handy function to add to `gnus-summary-exit-hook' to
1774 increase the score of each group you read."
1775   (gnus-group-add-score gnus-newsgroup-name))
1776
1777 \f
1778 ;;;
1779 ;;; Gnus summary mode
1780 ;;;
1781
1782 (put 'gnus-summary-mode 'mode-class 'special)
1783
1784 (defvar gnus-article-commands-menu)
1785
1786 ;; Non-orthogonal keys
1787
1788 (gnus-define-keys gnus-summary-mode-map
1789   " " gnus-summary-next-page
1790   "\177" gnus-summary-prev-page
1791   [delete] gnus-summary-prev-page
1792   [backspace] gnus-summary-prev-page
1793   "\r" gnus-summary-scroll-up
1794   "\M-\r" gnus-summary-scroll-down
1795   "n" gnus-summary-next-unread-article
1796   "p" gnus-summary-prev-unread-article
1797   "N" gnus-summary-next-article
1798   "P" gnus-summary-prev-article
1799   "\M-\C-n" gnus-summary-next-same-subject
1800   "\M-\C-p" gnus-summary-prev-same-subject
1801   "\M-n" gnus-summary-next-unread-subject
1802   "\M-p" gnus-summary-prev-unread-subject
1803   "." gnus-summary-first-unread-article
1804   "," gnus-summary-best-unread-article
1805   "\M-s" gnus-summary-search-article-forward
1806   "\M-r" gnus-summary-search-article-backward
1807   "\M-S" gnus-summary-repeat-search-article-forward
1808   "\M-R" gnus-summary-repeat-search-article-backward
1809   "<" gnus-summary-beginning-of-article
1810   ">" gnus-summary-end-of-article
1811   "j" gnus-summary-goto-article
1812   "^" gnus-summary-refer-parent-article
1813   "\M-^" gnus-summary-refer-article
1814   "u" gnus-summary-tick-article-forward
1815   "!" gnus-summary-tick-article-forward
1816   "U" gnus-summary-tick-article-backward
1817   "d" gnus-summary-mark-as-read-forward
1818   "D" gnus-summary-mark-as-read-backward
1819   "E" gnus-summary-mark-as-expirable
1820   "\M-u" gnus-summary-clear-mark-forward
1821   "\M-U" gnus-summary-clear-mark-backward
1822   "k" gnus-summary-kill-same-subject-and-select
1823   "\C-k" gnus-summary-kill-same-subject
1824   "\M-\C-k" gnus-summary-kill-thread
1825   "\M-\C-l" gnus-summary-lower-thread
1826   "e" gnus-summary-edit-article
1827   "#" gnus-summary-mark-as-processable
1828   "\M-#" gnus-summary-unmark-as-processable
1829   "\M-\C-t" gnus-summary-toggle-threads
1830   "\M-\C-s" gnus-summary-show-thread
1831   "\M-\C-h" gnus-summary-hide-thread
1832   "\M-\C-f" gnus-summary-next-thread
1833   "\M-\C-b" gnus-summary-prev-thread
1834   [(meta down)] gnus-summary-next-thread
1835   [(meta up)] gnus-summary-prev-thread
1836   "\M-\C-u" gnus-summary-up-thread
1837   "\M-\C-d" gnus-summary-down-thread
1838   "&" gnus-summary-execute-command
1839   "c" gnus-summary-catchup-and-exit
1840   "\C-w" gnus-summary-mark-region-as-read
1841   "\C-t" gnus-summary-toggle-truncation
1842   "?" gnus-summary-mark-as-dormant
1843   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1844   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1845   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1846   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1847   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1848   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1849   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1850   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1851   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1852   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1853   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1854   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1855   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1856   "=" gnus-summary-expand-window
1857   "\C-x\C-s" gnus-summary-reselect-current-group
1858   "\M-g" gnus-summary-rescan-group
1859   "\C-c\C-r" gnus-summary-caesar-message
1860   "f" gnus-summary-followup
1861   "F" gnus-summary-followup-with-original
1862   "C" gnus-summary-cancel-article
1863   "r" gnus-summary-reply
1864   "R" gnus-summary-reply-with-original
1865   "\C-c\C-f" gnus-summary-mail-forward
1866   "o" gnus-summary-save-article
1867   "\C-o" gnus-summary-save-article-mail
1868   "|" gnus-summary-pipe-output
1869   "\M-k" gnus-summary-edit-local-kill
1870   "\M-K" gnus-summary-edit-global-kill
1871   ;; "V" gnus-version
1872   "\C-c\C-d" gnus-summary-describe-group
1873   "q" gnus-summary-exit
1874   "Q" gnus-summary-exit-no-update
1875   "\C-c\C-i" gnus-info-find-node
1876   gnus-mouse-2 gnus-mouse-pick-article
1877   [follow-link] mouse-face
1878   "m" gnus-summary-mail-other-window
1879   "a" gnus-summary-post-news
1880   "x" gnus-summary-limit-to-unread
1881   "s" gnus-summary-isearch-article
1882   "t" gnus-summary-toggle-header
1883   "g" gnus-summary-show-article
1884   "l" gnus-summary-goto-last-article
1885   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1886   "\C-d" gnus-summary-enter-digest-group
1887   "\M-\C-d" gnus-summary-read-document
1888   "\M-\C-e" gnus-summary-edit-parameters
1889   "\M-\C-a" gnus-summary-customize-parameters
1890   "\C-c\C-b" gnus-bug
1891   "*" gnus-cache-enter-article
1892   "\M-*" gnus-cache-remove-article
1893   "\M-&" gnus-summary-universal-argument
1894   "\C-l" gnus-recenter
1895   "I" gnus-summary-increase-score
1896   "L" gnus-summary-lower-score
1897   "\M-i" gnus-symbolic-argument
1898   "h" gnus-summary-select-article-buffer
1899
1900   "b" gnus-article-view-part
1901   "\M-t" gnus-summary-toggle-display-buttonized
1902
1903   "V" gnus-summary-score-map
1904   "X" gnus-uu-extract-map
1905   "S" gnus-summary-send-map)
1906
1907 ;; Sort of orthogonal keymap
1908 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1909   "t" gnus-summary-tick-article-forward
1910   "!" gnus-summary-tick-article-forward
1911   "d" gnus-summary-mark-as-read-forward
1912   "r" gnus-summary-mark-as-read-forward
1913   "c" gnus-summary-clear-mark-forward
1914   " " gnus-summary-clear-mark-forward
1915   "e" gnus-summary-mark-as-expirable
1916   "x" gnus-summary-mark-as-expirable
1917   "?" gnus-summary-mark-as-dormant
1918   "b" gnus-summary-set-bookmark
1919   "B" gnus-summary-remove-bookmark
1920   "#" gnus-summary-mark-as-processable
1921   "\M-#" gnus-summary-unmark-as-processable
1922   "S" gnus-summary-limit-include-expunged
1923   "C" gnus-summary-catchup
1924   "H" gnus-summary-catchup-to-here
1925   "h" gnus-summary-catchup-from-here
1926   "\C-c" gnus-summary-catchup-all
1927   "k" gnus-summary-kill-same-subject-and-select
1928   "K" gnus-summary-kill-same-subject
1929   "P" gnus-uu-mark-map)
1930
1931 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1932   "c" gnus-summary-clear-above
1933   "u" gnus-summary-tick-above
1934   "m" gnus-summary-mark-above
1935   "k" gnus-summary-kill-below)
1936
1937 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1938   "/" gnus-summary-limit-to-subject
1939   "n" gnus-summary-limit-to-articles
1940   "b" gnus-summary-limit-to-bodies
1941   "h" gnus-summary-limit-to-headers
1942   "w" gnus-summary-pop-limit
1943   "s" gnus-summary-limit-to-subject
1944   "a" gnus-summary-limit-to-author
1945   "u" gnus-summary-limit-to-unread
1946   "m" gnus-summary-limit-to-marks
1947   "M" gnus-summary-limit-exclude-marks
1948   "v" gnus-summary-limit-to-score
1949   "*" gnus-summary-limit-include-cached
1950   "D" gnus-summary-limit-include-dormant
1951   "T" gnus-summary-limit-include-thread
1952   "d" gnus-summary-limit-exclude-dormant
1953   "t" gnus-summary-limit-to-age
1954   "." gnus-summary-limit-to-unseen
1955   "x" gnus-summary-limit-to-extra
1956   "p" gnus-summary-limit-to-display-predicate
1957   "E" gnus-summary-limit-include-expunged
1958   "c" gnus-summary-limit-exclude-childless-dormant
1959   "C" gnus-summary-limit-mark-excluded-as-read
1960   "o" gnus-summary-insert-old-articles
1961   "N" gnus-summary-insert-new-articles
1962   "S" gnus-summary-limit-to-singletons
1963   "r" gnus-summary-limit-to-replied
1964   "R" gnus-summary-limit-to-recipient
1965   "A" gnus-summary-limit-to-address)
1966
1967 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1968   "n" gnus-summary-next-unread-article
1969   "p" gnus-summary-prev-unread-article
1970   "N" gnus-summary-next-article
1971   "P" gnus-summary-prev-article
1972   "\C-n" gnus-summary-next-same-subject
1973   "\C-p" gnus-summary-prev-same-subject
1974   "\M-n" gnus-summary-next-unread-subject
1975   "\M-p" gnus-summary-prev-unread-subject
1976   "f" gnus-summary-first-unread-article
1977   "b" gnus-summary-best-unread-article
1978   "j" gnus-summary-goto-article
1979   "g" gnus-summary-goto-subject
1980   "l" gnus-summary-goto-last-article
1981   "o" gnus-summary-pop-article)
1982
1983 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1984   "k" gnus-summary-kill-thread
1985   "E" gnus-summary-expire-thread
1986   "l" gnus-summary-lower-thread
1987   "i" gnus-summary-raise-thread
1988   "T" gnus-summary-toggle-threads
1989   "t" gnus-summary-rethread-current
1990   "^" gnus-summary-reparent-thread
1991   "\M-^" gnus-summary-reparent-children
1992   "s" gnus-summary-show-thread
1993   "S" gnus-summary-show-all-threads
1994   "h" gnus-summary-hide-thread
1995   "H" gnus-summary-hide-all-threads
1996   "n" gnus-summary-next-thread
1997   "p" gnus-summary-prev-thread
1998   "u" gnus-summary-up-thread
1999   "o" gnus-summary-top-thread
2000   "d" gnus-summary-down-thread
2001   "#" gnus-uu-mark-thread
2002   "\M-#" gnus-uu-unmark-thread)
2003
2004 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2005   "g" gnus-summary-prepare
2006   "c" gnus-summary-insert-cached-articles
2007   "d" gnus-summary-insert-dormant-articles
2008   "t" gnus-summary-insert-ticked-articles)
2009
2010 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2011   "c" gnus-summary-catchup-and-exit
2012   "C" gnus-summary-catchup-all-and-exit
2013   "E" gnus-summary-exit-no-update
2014   "Q" gnus-summary-exit
2015   "Z" gnus-summary-exit
2016   "n" gnus-summary-catchup-and-goto-next-group
2017   "p" gnus-summary-catchup-and-goto-prev-group
2018   "R" gnus-summary-reselect-current-group
2019   "G" gnus-summary-rescan-group
2020   "N" gnus-summary-next-group
2021   "s" gnus-summary-save-newsrc
2022   "P" gnus-summary-prev-group)
2023
2024 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2025   " " gnus-summary-next-page
2026   "n" gnus-summary-next-page
2027   "\177" gnus-summary-prev-page
2028   [delete] gnus-summary-prev-page
2029   "p" gnus-summary-prev-page
2030   "\r" gnus-summary-scroll-up
2031   "\M-\r" gnus-summary-scroll-down
2032   "<" gnus-summary-beginning-of-article
2033   ">" gnus-summary-end-of-article
2034   "b" gnus-summary-beginning-of-article
2035   "e" gnus-summary-end-of-article
2036   "^" gnus-summary-refer-parent-article
2037   "r" gnus-summary-refer-parent-article
2038   "D" gnus-summary-enter-digest-group
2039   "R" gnus-summary-refer-references
2040   "T" gnus-summary-refer-thread
2041   "g" gnus-summary-show-article
2042   "s" gnus-summary-isearch-article
2043   "P" gnus-summary-print-article
2044   "S" gnus-sticky-article
2045   "M" gnus-mailing-list-insinuate
2046   "t" gnus-article-babel)
2047
2048 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2049   "b" gnus-article-add-buttons
2050   "B" gnus-article-add-buttons-to-head
2051   "o" gnus-article-treat-overstrike
2052   "e" gnus-article-emphasize
2053   "w" gnus-article-fill-cited-article
2054   "Q" gnus-article-fill-long-lines
2055   "L" gnus-article-toggle-truncate-lines
2056   "C" gnus-article-capitalize-sentences
2057   "c" gnus-article-remove-cr
2058   "q" gnus-article-de-quoted-unreadable
2059   "6" gnus-article-de-base64-unreadable
2060   "Z" gnus-article-decode-HZ
2061   "A" gnus-article-treat-ansi-sequences
2062   "h" gnus-article-wash-html
2063   "u" gnus-article-unsplit-urls
2064   "s" gnus-summary-force-verify-and-decrypt
2065   "f" gnus-article-display-x-face
2066   "l" gnus-summary-stop-page-breaking
2067   "r" gnus-summary-caesar-message
2068   "m" gnus-summary-morse-message
2069   "t" gnus-summary-toggle-header
2070   "g" gnus-treat-smiley
2071   "v" gnus-summary-verbose-headers
2072   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2073   "p" gnus-article-verify-x-pgp-sig
2074   "d" gnus-article-treat-dumbquotes
2075   "i" gnus-summary-idna-message)
2076
2077 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2078   ;; mnemonic: deuglif*Y*
2079   "u" gnus-article-outlook-unwrap-lines
2080   "a" gnus-article-outlook-repair-attribution
2081   "c" gnus-article-outlook-rearrange-citation
2082   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2083
2084 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2085   "a" gnus-article-hide
2086   "h" gnus-article-hide-headers
2087   "b" gnus-article-hide-boring-headers
2088   "s" gnus-article-hide-signature
2089   "c" gnus-article-hide-citation
2090   "C" gnus-article-hide-citation-in-followups
2091   "l" gnus-article-hide-list-identifiers
2092   "B" gnus-article-strip-banner
2093   "P" gnus-article-hide-pem
2094   "\C-c" gnus-article-hide-citation-maybe)
2095
2096 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2097   "a" gnus-article-highlight
2098   "h" gnus-article-highlight-headers
2099   "c" gnus-article-highlight-citation
2100   "s" gnus-article-highlight-signature)
2101
2102 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2103   "f" gnus-article-treat-fold-headers
2104   "u" gnus-article-treat-unfold-headers
2105   "n" gnus-article-treat-fold-newsgroups)
2106
2107 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2108   "x" gnus-article-display-x-face
2109   "d" gnus-article-display-face
2110   "s" gnus-treat-smiley
2111   "D" gnus-article-remove-images
2112   "f" gnus-treat-from-picon
2113   "m" gnus-treat-mail-picon
2114   "n" gnus-treat-newsgroups-picon)
2115
2116 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2117   "w" gnus-article-decode-mime-words
2118   "c" gnus-article-decode-charset
2119   "v" gnus-mime-view-all-parts
2120   "b" gnus-article-view-part)
2121
2122 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2123   "z" gnus-article-date-ut
2124   "u" gnus-article-date-ut
2125   "l" gnus-article-date-local
2126   "p" gnus-article-date-english
2127   "e" gnus-article-date-lapsed
2128   "o" gnus-article-date-original
2129   "i" gnus-article-date-iso8601
2130   "s" gnus-article-date-user)
2131
2132 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2133   "t" gnus-article-remove-trailing-blank-lines
2134   "l" gnus-article-strip-leading-blank-lines
2135   "m" gnus-article-strip-multiple-blank-lines
2136   "a" gnus-article-strip-blank-lines
2137   "A" gnus-article-strip-all-blank-lines
2138   "s" gnus-article-strip-leading-space
2139   "e" gnus-article-strip-trailing-space
2140   "w" gnus-article-remove-leading-whitespace)
2141
2142 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2143   "v" gnus-version
2144   "f" gnus-summary-fetch-faq
2145   "d" gnus-summary-describe-group
2146   "h" gnus-summary-describe-briefly
2147   "i" gnus-info-find-node
2148   "c" gnus-group-fetch-charter
2149   "C" gnus-group-fetch-control)
2150
2151 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2152   "e" gnus-summary-expire-articles
2153   "\M-\C-e" gnus-summary-expire-articles-now
2154   "\177" gnus-summary-delete-article
2155   [delete] gnus-summary-delete-article
2156   [backspace] gnus-summary-delete-article
2157   "m" gnus-summary-move-article
2158   "r" gnus-summary-respool-article
2159   "w" gnus-summary-edit-article
2160   "c" gnus-summary-copy-article
2161   "B" gnus-summary-crosspost-article
2162   "q" gnus-summary-respool-query
2163   "t" gnus-summary-respool-trace
2164   "i" gnus-summary-import-article
2165   "I" gnus-summary-create-article
2166   "p" gnus-summary-article-posted-p)
2167
2168 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2169   "o" gnus-summary-save-article
2170   "m" gnus-summary-save-article-mail
2171   "F" gnus-summary-write-article-file
2172   "r" gnus-summary-save-article-rmail
2173   "f" gnus-summary-save-article-file
2174   "b" gnus-summary-save-article-body-file
2175   "B" gnus-summary-write-article-body-file
2176   "h" gnus-summary-save-article-folder
2177   "v" gnus-summary-save-article-vm
2178   "p" gnus-summary-pipe-output
2179   "P" gnus-summary-muttprint)
2180
2181 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2182   "b" gnus-summary-display-buttonized
2183   "m" gnus-summary-repair-multipart
2184   "v" gnus-article-view-part
2185   "o" gnus-article-save-part
2186   "O" gnus-article-save-part-and-strip
2187   "r" gnus-article-replace-part
2188   "d" gnus-article-delete-part
2189   "t" gnus-article-view-part-as-type
2190   "j" gnus-article-jump-to-part
2191   "c" gnus-article-copy-part
2192   "C" gnus-article-view-part-as-charset
2193   "e" gnus-article-view-part-externally
2194   "H" gnus-article-browse-html-article
2195   "E" gnus-article-encrypt-body
2196   "i" gnus-article-inline-part
2197   "|" gnus-article-pipe-part)
2198
2199 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2200   "p" gnus-summary-mark-as-processable
2201   "u" gnus-summary-unmark-as-processable
2202   "U" gnus-summary-unmark-all-processable
2203   "v" gnus-uu-mark-over
2204   "s" gnus-uu-mark-series
2205   "r" gnus-uu-mark-region
2206   "g" gnus-uu-unmark-region
2207   "R" gnus-uu-mark-by-regexp
2208   "G" gnus-uu-unmark-by-regexp
2209   "t" gnus-uu-mark-thread
2210   "T" gnus-uu-unmark-thread
2211   "a" gnus-uu-mark-all
2212   "b" gnus-uu-mark-buffer
2213   "S" gnus-uu-mark-sparse
2214   "k" gnus-summary-kill-process-mark
2215   "y" gnus-summary-yank-process-mark
2216   "w" gnus-summary-save-process-mark
2217   "i" gnus-uu-invert-processable)
2218
2219 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2220   ;;"x" gnus-uu-extract-any
2221   "m" gnus-summary-save-parts
2222   "u" gnus-uu-decode-uu
2223   "U" gnus-uu-decode-uu-and-save
2224   "s" gnus-uu-decode-unshar
2225   "S" gnus-uu-decode-unshar-and-save
2226   "o" gnus-uu-decode-save
2227   "O" gnus-uu-decode-save
2228   "b" gnus-uu-decode-binhex
2229   "B" gnus-uu-decode-binhex
2230   "Y" gnus-uu-decode-yenc
2231   "p" gnus-uu-decode-postscript
2232   "P" gnus-uu-decode-postscript-and-save)
2233
2234 (gnus-define-keys
2235     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2236   "u" gnus-uu-decode-uu-view
2237   "U" gnus-uu-decode-uu-and-save-view
2238   "s" gnus-uu-decode-unshar-view
2239   "S" gnus-uu-decode-unshar-and-save-view
2240   "o" gnus-uu-decode-save-view
2241   "O" gnus-uu-decode-save-view
2242   "b" gnus-uu-decode-binhex-view
2243   "B" gnus-uu-decode-binhex-view
2244   "p" gnus-uu-decode-postscript-view
2245   "P" gnus-uu-decode-postscript-and-save-view)
2246
2247 (defvar gnus-article-post-menu nil)
2248
2249 (defconst gnus-summary-menu-maxlen 20)
2250
2251 (defun gnus-summary-menu-split (menu)
2252   ;; If we have lots of elements, divide them into groups of 20
2253   ;; and make a pane (or submenu) for each one.
2254   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2255       (let ((menu menu) sublists next
2256             (i 1))
2257         (while menu
2258           ;; Pull off the next gnus-summary-menu-maxlen elements
2259           ;; and make them the next element of sublist.
2260           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2261           (if next
2262               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2263                       nil))
2264           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2265                                              (aref (car (last menu)) 0)) menu)
2266                                sublists))
2267           (setq i (1+ i))
2268           (setq menu next))
2269         (nreverse sublists))
2270     ;; Few elements--put them all in one pane.
2271     menu))
2272
2273 (defun gnus-summary-make-menu-bar ()
2274   (gnus-turn-off-edit-menu 'summary)
2275
2276   (unless (boundp 'gnus-summary-misc-menu)
2277
2278     (easy-menu-define
2279       gnus-summary-kill-menu gnus-summary-mode-map ""
2280       (cons
2281        "Score"
2282        (nconc
2283         (list
2284          ["Customize" gnus-score-customize t])
2285         (gnus-make-score-map 'increase)
2286         (gnus-make-score-map 'lower)
2287         '(("Mark"
2288            ["Kill below" gnus-summary-kill-below t]
2289            ["Mark above" gnus-summary-mark-above t]
2290            ["Tick above" gnus-summary-tick-above t]
2291            ["Clear above" gnus-summary-clear-above t])
2292           ["Current score" gnus-summary-current-score t]
2293           ["Set score" gnus-summary-set-score t]
2294           ["Switch current score file..." gnus-score-change-score-file t]
2295           ["Set mark below..." gnus-score-set-mark-below t]
2296           ["Set expunge below..." gnus-score-set-expunge-below t]
2297           ["Edit current score file" gnus-score-edit-current-scores t]
2298           ["Edit score file..." gnus-score-edit-file t]
2299           ["Trace score" gnus-score-find-trace t]
2300           ["Find words" gnus-score-find-favourite-words t]
2301           ["Rescore buffer" gnus-summary-rescore t]
2302           ["Increase score..." gnus-summary-increase-score t]
2303           ["Lower score..." gnus-summary-lower-score t]))))
2304
2305     ;; Define both the Article menu in the summary buffer and the
2306     ;; equivalent Commands menu in the article buffer here for
2307     ;; consistency.
2308     (let ((innards
2309            `(("Hide"
2310               ["All" gnus-article-hide t]
2311               ["Headers" gnus-article-hide-headers t]
2312               ["Signature" gnus-article-hide-signature t]
2313               ["Citation" gnus-article-hide-citation t]
2314               ["List identifiers" gnus-article-hide-list-identifiers t]
2315               ["Banner" gnus-article-strip-banner t]
2316               ["Boring headers" gnus-article-hide-boring-headers t])
2317              ("Highlight"
2318               ["All" gnus-article-highlight t]
2319               ["Headers" gnus-article-highlight-headers t]
2320               ["Signature" gnus-article-highlight-signature t]
2321               ["Citation" gnus-article-highlight-citation t])
2322              ("MIME"
2323               ["Words" gnus-article-decode-mime-words t]
2324               ["Charset" gnus-article-decode-charset t]
2325               ["QP" gnus-article-de-quoted-unreadable t]
2326               ["Base64" gnus-article-de-base64-unreadable t]
2327               ["View MIME buttons" gnus-summary-display-buttonized t]
2328               ["View all" gnus-mime-view-all-parts t]
2329               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2330               ["Encrypt body" gnus-article-encrypt-body
2331                :active (not (gnus-group-read-only-p))
2332                ,@(if (featurep 'xemacs) nil
2333                    '(:help "Encrypt the message body on disk"))]
2334               ["Extract all parts..." gnus-summary-save-parts t]
2335               ("Multipart"
2336                ["Repair multipart" gnus-summary-repair-multipart t]
2337                ["Pipe part..." gnus-article-pipe-part t]
2338                ["Inline part" gnus-article-inline-part t]
2339                ["View part as type..." gnus-article-view-part-as-type t]
2340                ["Encrypt body" gnus-article-encrypt-body
2341                 :active (not (gnus-group-read-only-p))
2342                ,@(if (featurep 'xemacs) nil
2343                    '(:help "Encrypt the message body on disk"))]
2344                ["View part externally" gnus-article-view-part-externally t]
2345                ["View HTML parts in browser" gnus-article-browse-html-article t]
2346                ["View part with charset..." gnus-article-view-part-as-charset t]
2347                ["Copy part" gnus-article-copy-part t]
2348                ["Save part..." gnus-article-save-part t]
2349                ["View part" gnus-article-view-part t]))
2350              ("Date"
2351               ["Local" gnus-article-date-local t]
2352               ["ISO8601" gnus-article-date-iso8601 t]
2353               ["UT" gnus-article-date-ut t]
2354               ["Original" gnus-article-date-original t]
2355               ["Lapsed" gnus-article-date-lapsed t]
2356               ["User-defined" gnus-article-date-user t])
2357              ("Display"
2358               ["Remove images" gnus-article-remove-images t]
2359               ["Toggle smiley" gnus-treat-smiley t]
2360               ["Show X-Face" gnus-article-display-x-face t]
2361               ["Show picons in From" gnus-treat-from-picon t]
2362               ["Show picons in mail headers" gnus-treat-mail-picon t]
2363               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2364               ("View as different encoding"
2365                ,@(gnus-summary-menu-split
2366                   (mapcar
2367                    (lambda (cs)
2368                      ;; Since easymenu under Emacs doesn't allow
2369                      ;; lambda forms for menu commands, we should
2370                      ;; provide intern'ed function symbols.
2371                      (let ((command (intern (format "\
2372 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2373                        (fset command
2374                              `(lambda ()
2375                                 (interactive)
2376                                 (let ((gnus-summary-show-article-charset-alist
2377                                        '((1 . ,cs))))
2378                                   (gnus-summary-show-article 1))))
2379                        `[,(symbol-name cs) ,command t]))
2380                    (sort (if (fboundp 'coding-system-list)
2381                              (coding-system-list)
2382                            (mapcar 'car mm-mime-mule-charset-alist))
2383                          'string<)))))
2384              ("Washing"
2385               ("Remove Blanks"
2386                ["Leading" gnus-article-strip-leading-blank-lines t]
2387                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2388                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2389                ["All of the above" gnus-article-strip-blank-lines t]
2390                ["All" gnus-article-strip-all-blank-lines t]
2391                ["Leading space" gnus-article-strip-leading-space t]
2392                ["Trailing space" gnus-article-strip-trailing-space t]
2393                ["Leading space in headers"
2394                 gnus-article-remove-leading-whitespace t])
2395               ["Overstrike" gnus-article-treat-overstrike t]
2396               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2397               ["Emphasis" gnus-article-emphasize t]
2398               ["Word wrap" gnus-article-fill-cited-article t]
2399               ["Fill long lines" gnus-article-fill-long-lines t]
2400               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2401               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2402               ["Remove CR" gnus-article-remove-cr t]
2403               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2404               ["Base64" gnus-article-de-base64-unreadable t]
2405               ["Rot 13" gnus-summary-caesar-message
2406                ,@(if (featurep 'xemacs) '(t)
2407                    '(:help "\"Caesar rotate\" article by 13"))]
2408               ["De-IDNA" gnus-summary-idna-message t]
2409               ["Morse decode" gnus-summary-morse-message t]
2410               ["Unix pipe..." gnus-summary-pipe-message t]
2411               ["Add buttons" gnus-article-add-buttons t]
2412               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2413               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2414               ["Verbose header" gnus-summary-verbose-headers t]
2415               ["Toggle header" gnus-summary-toggle-header t]
2416               ["Unfold headers" gnus-article-treat-unfold-headers t]
2417               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2418               ["Html" gnus-article-wash-html t]
2419               ["Unsplit URLs" gnus-article-unsplit-urls t]
2420               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2421               ["Decode HZ" gnus-article-decode-HZ t]
2422               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2423               ("(Outlook) Deuglify"
2424                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2425                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2426                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2427                ["Full (Outlook) deuglify"
2428                 gnus-article-outlook-deuglify-article t])
2429               )
2430              ("Output"
2431               ["Save in default format..." gnus-summary-save-article
2432                ,@(if (featurep 'xemacs) '(t)
2433                    '(:help "Save article using default method"))]
2434               ["Save in file..." gnus-summary-save-article-file
2435                ,@(if (featurep 'xemacs) '(t)
2436                    '(:help "Save article in file"))]
2437               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2438               ["Save in MH folder..." gnus-summary-save-article-folder t]
2439               ["Save in VM folder..." gnus-summary-save-article-vm t]
2440               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2441               ["Save body in file..." gnus-summary-save-article-body-file t]
2442               ["Pipe through a filter..." gnus-summary-pipe-output t]
2443               ["Print with Muttprint..." gnus-summary-muttprint t]
2444               ["Print" gnus-summary-print-article
2445                ,@(if (featurep 'xemacs) '(t)
2446                    '(:help "Generate and print a PostScript image"))])
2447              ("Copy, move,... (Backend)"
2448               ,@(if (featurep 'xemacs) nil
2449                   '(:help "Copying, moving, expiring articles..."))
2450               ["Respool article..." gnus-summary-respool-article t]
2451               ["Move article..." gnus-summary-move-article
2452                (gnus-check-backend-function
2453                 'request-move-article gnus-newsgroup-name)]
2454               ["Copy article..." gnus-summary-copy-article t]
2455               ["Crosspost article..." gnus-summary-crosspost-article
2456                (gnus-check-backend-function
2457                 'request-replace-article gnus-newsgroup-name)]
2458               ["Import file..." gnus-summary-import-article
2459                (gnus-check-backend-function
2460                 'request-accept-article gnus-newsgroup-name)]
2461               ["Create article..." gnus-summary-create-article
2462                (gnus-check-backend-function
2463                 'request-accept-article gnus-newsgroup-name)]
2464               ["Check if posted" gnus-summary-article-posted-p t]
2465               ["Edit article" gnus-summary-edit-article
2466                (not (gnus-group-read-only-p))]
2467               ["Delete article" gnus-summary-delete-article
2468                (gnus-check-backend-function
2469                 'request-expire-articles gnus-newsgroup-name)]
2470               ["Query respool" gnus-summary-respool-query t]
2471               ["Trace respool" gnus-summary-respool-trace t]
2472               ["Delete expirable articles" gnus-summary-expire-articles-now
2473                (gnus-check-backend-function
2474                 'request-expire-articles gnus-newsgroup-name)])
2475              ("Extract"
2476               ["Uudecode" gnus-uu-decode-uu
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "Decode uuencoded article(s)"))]
2479               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2480               ["Unshar" gnus-uu-decode-unshar t]
2481               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2482               ["Save" gnus-uu-decode-save t]
2483               ["Binhex" gnus-uu-decode-binhex t]
2484               ["Postscript" gnus-uu-decode-postscript t]
2485               ["All MIME parts" gnus-summary-save-parts t])
2486              ("Cache"
2487               ["Enter article" gnus-cache-enter-article t]
2488               ["Remove article" gnus-cache-remove-article t])
2489              ["Translate" gnus-article-babel t]
2490              ["Select article buffer" gnus-summary-select-article-buffer t]
2491              ["Make article buffer sticky" gnus-sticky-article t]
2492              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2493              ["Isearch article..." gnus-summary-isearch-article t]
2494              ["Beginning of the article" gnus-summary-beginning-of-article t]
2495              ["End of the article" gnus-summary-end-of-article t]
2496              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2497              ["Fetch referenced articles" gnus-summary-refer-references t]
2498              ["Fetch current thread" gnus-summary-refer-thread t]
2499              ["Fetch article with id..." gnus-summary-refer-article t]
2500              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2501              ["Redisplay" gnus-summary-show-article t]
2502              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2503       (easy-menu-define
2504         gnus-summary-article-menu gnus-summary-mode-map ""
2505         (cons "Article" innards))
2506
2507       (if (not (keymapp gnus-summary-article-menu))
2508           (easy-menu-define
2509             gnus-article-commands-menu gnus-article-mode-map ""
2510             (cons "Commands" innards))
2511         ;; in Emacs, don't share menu.
2512         (setq gnus-article-commands-menu
2513               (copy-keymap gnus-summary-article-menu))
2514         (define-key gnus-article-mode-map [menu-bar commands]
2515           (cons "Commands" gnus-article-commands-menu))))
2516
2517     (easy-menu-define
2518       gnus-summary-thread-menu gnus-summary-mode-map ""
2519       '("Threads"
2520         ["Find all messages in thread" gnus-summary-refer-thread t]
2521         ["Toggle threading" gnus-summary-toggle-threads t]
2522         ["Hide threads" gnus-summary-hide-all-threads t]
2523         ["Show threads" gnus-summary-show-all-threads t]
2524         ["Hide thread" gnus-summary-hide-thread t]
2525         ["Show thread" gnus-summary-show-thread t]
2526         ["Go to next thread" gnus-summary-next-thread t]
2527         ["Go to previous thread" gnus-summary-prev-thread t]
2528         ["Go down thread" gnus-summary-down-thread t]
2529         ["Go up thread" gnus-summary-up-thread t]
2530         ["Top of thread" gnus-summary-top-thread t]
2531         ["Mark thread as read" gnus-summary-kill-thread t]
2532         ["Mark thread as expired" gnus-summary-expire-thread t]
2533         ["Lower thread score" gnus-summary-lower-thread t]
2534         ["Raise thread score" gnus-summary-raise-thread t]
2535         ["Rethread current" gnus-summary-rethread-current t]))
2536
2537     (easy-menu-define
2538       gnus-summary-post-menu gnus-summary-mode-map ""
2539       `("Post"
2540         ["Send a message (mail or news)" gnus-summary-post-news
2541          ,@(if (featurep 'xemacs) '(t)
2542              '(:help "Compose a new message (mail or news)"))]
2543         ["Followup" gnus-summary-followup
2544          ,@(if (featurep 'xemacs) '(t)
2545              '(:help "Post followup to this article"))]
2546         ["Followup and yank" gnus-summary-followup-with-original
2547          ,@(if (featurep 'xemacs) '(t)
2548              '(:help "Post followup to this article, quoting its contents"))]
2549         ["Supersede article" gnus-summary-supersede-article t]
2550         ["Cancel article" gnus-summary-cancel-article
2551          ,@(if (featurep 'xemacs) '(t)
2552              '(:help "Cancel an article you posted"))]
2553         ["Reply" gnus-summary-reply t]
2554         ["Reply and yank" gnus-summary-reply-with-original t]
2555         ["Wide reply" gnus-summary-wide-reply t]
2556         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2557          ,@(if (featurep 'xemacs) '(t)
2558              '(:help "Mail a reply, quoting this article"))]
2559         ["Very wide reply" gnus-summary-very-wide-reply t]
2560         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2561          ,@(if (featurep 'xemacs) '(t)
2562              '(:help "Mail a very wide reply, quoting this article"))]
2563         ["Mail forward" gnus-summary-mail-forward t]
2564         ["Post forward" gnus-summary-post-forward t]
2565         ["Digest and mail" gnus-uu-digest-mail-forward t]
2566         ["Digest and post" gnus-uu-digest-post-forward t]
2567         ["Resend message" gnus-summary-resend-message t]
2568         ["Resend message edit" gnus-summary-resend-message-edit t]
2569         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2570         ["Send a mail" gnus-summary-mail-other-window t]
2571         ["Create a local message" gnus-summary-news-other-window t]
2572         ["Uuencode and post" gnus-uu-post-news
2573          ,@(if (featurep 'xemacs) '(t)
2574              '(:help "Post a uuencoded article"))]
2575         ["Followup via news" gnus-summary-followup-to-mail t]
2576         ["Followup via news and yank"
2577          gnus-summary-followup-to-mail-with-original t]
2578         ["Strip signature on reply"
2579          (lambda ()
2580            (interactive)
2581            (if (not (memq message-cite-function
2582                           '(message-cite-original-without-signature
2583                             message-cite-original)))
2584                ;; Stupid workaround for XEmacs not honoring :visible.
2585                (message "Can't toggle this value of `message-cite-function'")
2586              (setq message-cite-function
2587                    (if (eq message-cite-function
2588                            'message-cite-original-without-signature)
2589                        'message-cite-original
2590                      'message-cite-original-without-signature))))
2591          ;; XEmacs barfs on :visible.
2592          ,@(if (featurep 'xemacs) nil
2593              '(:visible (memq message-cite-function
2594                               '(message-cite-original-without-signature
2595                                 message-cite-original))))
2596          :style toggle
2597          :selected (eq message-cite-function
2598                        'message-cite-original-without-signature)
2599          ,@(if (featurep 'xemacs) nil
2600              '(:help "Strip signature from cited article when replying."))]
2601         ;;("Draft"
2602         ;;["Send" gnus-summary-send-draft t]
2603         ;;["Send bounced" gnus-resend-bounced-mail t])
2604         ))
2605
2606     (cond
2607      ((not (keymapp gnus-summary-post-menu))
2608       (setq gnus-article-post-menu gnus-summary-post-menu))
2609      ((not gnus-article-post-menu)
2610       ;; Don't share post menu.
2611       (setq gnus-article-post-menu
2612             (copy-keymap gnus-summary-post-menu))))
2613     (define-key gnus-article-mode-map [menu-bar post]
2614       (cons "Post" gnus-article-post-menu))
2615
2616     (easy-menu-define
2617       gnus-summary-misc-menu gnus-summary-mode-map ""
2618       `("Gnus"
2619         ("Mark Read"
2620          ["Mark as read" gnus-summary-mark-as-read-forward t]
2621          ["Mark same subject and select"
2622           gnus-summary-kill-same-subject-and-select t]
2623          ["Mark same subject" gnus-summary-kill-same-subject t]
2624          ["Catchup" gnus-summary-catchup
2625           ,@(if (featurep 'xemacs) '(t)
2626               '(:help "Mark unread articles in this group as read"))]
2627          ["Catchup all" gnus-summary-catchup-all t]
2628          ["Catchup to here" gnus-summary-catchup-to-here t]
2629          ["Catchup from here" gnus-summary-catchup-from-here t]
2630          ["Catchup region" gnus-summary-mark-region-as-read
2631           (gnus-mark-active-p)]
2632          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2633         ("Mark Various"
2634          ["Tick" gnus-summary-tick-article-forward t]
2635          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2636          ["Remove marks" gnus-summary-clear-mark-forward t]
2637          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2638          ["Set bookmark" gnus-summary-set-bookmark t]
2639          ["Remove bookmark" gnus-summary-remove-bookmark t])
2640         ("Limit to"
2641          ["Marks..." gnus-summary-limit-to-marks t]
2642          ["Subject..." gnus-summary-limit-to-subject t]
2643          ["Author..." gnus-summary-limit-to-author t]
2644          ["Recipient..." gnus-summary-limit-to-recipient t]
2645          ["Address..." gnus-summary-limit-to-address t]
2646          ["Age..." gnus-summary-limit-to-age t]
2647          ["Extra..." gnus-summary-limit-to-extra t]
2648          ["Score..." gnus-summary-limit-to-score t]
2649          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2650          ["Unread" gnus-summary-limit-to-unread t]
2651          ["Unseen" gnus-summary-limit-to-unseen t]
2652          ["Singletons" gnus-summary-limit-to-singletons t]
2653          ["Replied" gnus-summary-limit-to-replied t]
2654          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2655          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2656          ["Pop limit" gnus-summary-pop-limit t]
2657          ["Show dormant" gnus-summary-limit-include-dormant t]
2658          ["Hide childless dormant"
2659           gnus-summary-limit-exclude-childless-dormant t]
2660          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2661          ["Hide marked" gnus-summary-limit-exclude-marks t]
2662          ["Show expunged" gnus-summary-limit-include-expunged t])
2663         ("Process Mark"
2664          ["Set mark" gnus-summary-mark-as-processable t]
2665          ["Remove mark" gnus-summary-unmark-as-processable t]
2666          ["Remove all marks" gnus-summary-unmark-all-processable t]
2667          ["Invert marks" gnus-uu-invert-processable t]
2668          ["Mark above" gnus-uu-mark-over t]
2669          ["Mark series" gnus-uu-mark-series t]
2670          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2671          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2672          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2673          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2674          ["Mark all" gnus-uu-mark-all t]
2675          ["Mark buffer" gnus-uu-mark-buffer t]
2676          ["Mark sparse" gnus-uu-mark-sparse t]
2677          ["Mark thread" gnus-uu-mark-thread t]
2678          ["Unmark thread" gnus-uu-unmark-thread t]
2679          ("Process Mark Sets"
2680           ["Kill" gnus-summary-kill-process-mark t]
2681           ["Yank" gnus-summary-yank-process-mark
2682            gnus-newsgroup-process-stack]
2683           ["Save" gnus-summary-save-process-mark t]
2684           ["Run command on marked..." gnus-summary-universal-argument t]))
2685         ("Registry Marks")
2686         ("Scroll article"
2687          ["Page forward" gnus-summary-next-page
2688           ,@(if (featurep 'xemacs) '(t)
2689               '(:help "Show next page of article"))]
2690          ["Page backward" gnus-summary-prev-page
2691           ,@(if (featurep 'xemacs) '(t)
2692               '(:help "Show previous page of article"))]
2693          ["Line forward" gnus-summary-scroll-up t])
2694         ("Move"
2695          ["Next unread article" gnus-summary-next-unread-article t]
2696          ["Previous unread article" gnus-summary-prev-unread-article t]
2697          ["Next article" gnus-summary-next-article t]
2698          ["Previous article" gnus-summary-prev-article t]
2699          ["Next unread subject" gnus-summary-next-unread-subject t]
2700          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2701          ["Next article same subject" gnus-summary-next-same-subject t]
2702          ["Previous article same subject" gnus-summary-prev-same-subject t]
2703          ["First unread article" gnus-summary-first-unread-article t]
2704          ["Best unread article" gnus-summary-best-unread-article t]
2705          ["Go to subject number..." gnus-summary-goto-subject t]
2706          ["Go to article number..." gnus-summary-goto-article t]
2707          ["Go to the last article" gnus-summary-goto-last-article t]
2708          ["Pop article off history" gnus-summary-pop-article t])
2709         ("Sort"
2710          ["Sort by number" gnus-summary-sort-by-number t]
2711          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2712          ["Sort by author" gnus-summary-sort-by-author t]
2713          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2714          ["Sort by subject" gnus-summary-sort-by-subject t]
2715          ["Sort by date" gnus-summary-sort-by-date t]
2716          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2717          ["Sort by score" gnus-summary-sort-by-score t]
2718          ["Sort by lines" gnus-summary-sort-by-lines t]
2719          ["Sort by characters" gnus-summary-sort-by-chars t]
2720          ["Randomize" gnus-summary-sort-by-random t]
2721          ["Original sort" gnus-summary-sort-by-original t])
2722         ("Help"
2723          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2724          ["Describe group" gnus-summary-describe-group t]
2725          ["Fetch charter" gnus-group-fetch-charter
2726           ,@(if (featurep 'xemacs) nil
2727               '(:help "Display the charter of the current group"))]
2728          ["Fetch control message" gnus-group-fetch-control
2729           ,@(if (featurep 'xemacs) nil
2730               '(:help "Display the archived control message for the current group"))]
2731          ["Read manual" gnus-info-find-node t])
2732         ("Modes"
2733          ["Pick and read" gnus-pick-mode t]
2734          ["Binary" gnus-binary-mode t])
2735         ("Regeneration"
2736          ["Regenerate" gnus-summary-prepare t]
2737          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2738          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2739          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2740          ["Toggle threading" gnus-summary-toggle-threads t])
2741         ["See old articles" gnus-summary-insert-old-articles t]
2742         ["See new articles" gnus-summary-insert-new-articles t]
2743         ["Filter articles..." gnus-summary-execute-command t]
2744         ["Run command on articles..." gnus-summary-universal-argument t]
2745         ["Search articles forward..." gnus-summary-search-article-forward t]
2746         ["Search articles backward..." gnus-summary-search-article-backward t]
2747         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2748         ["Expand window" gnus-summary-expand-window t]
2749         ["Expire expirable articles" gnus-summary-expire-articles
2750          (gnus-check-backend-function
2751           'request-expire-articles gnus-newsgroup-name)]
2752         ["Edit local kill file" gnus-summary-edit-local-kill t]
2753         ["Edit main kill file" gnus-summary-edit-global-kill t]
2754         ["Edit group parameters" gnus-summary-edit-parameters t]
2755         ["Customize group parameters" gnus-summary-customize-parameters t]
2756         ["Send a bug report" gnus-bug t]
2757         ("Exit"
2758          ["Catchup and exit" gnus-summary-catchup-and-exit
2759           ,@(if (featurep 'xemacs) '(t)
2760               '(:help "Mark unread articles in this group as read, then exit"))]
2761          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2762          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2763          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2764          ["Exit group" gnus-summary-exit
2765           ,@(if (featurep 'xemacs) '(t)
2766               '(:help "Exit current group, return to group selection mode"))]
2767          ["Exit group without updating" gnus-summary-exit-no-update t]
2768          ["Exit and goto next group" gnus-summary-next-group t]
2769          ["Exit and goto prev group" gnus-summary-prev-group t]
2770          ["Reselect group" gnus-summary-reselect-current-group t]
2771          ["Rescan group" gnus-summary-rescan-group t]
2772          ["Update dribble" gnus-summary-save-newsrc t])))
2773
2774     (gnus-run-hooks 'gnus-summary-menu-hook)))
2775
2776 (defvar gnus-summary-tool-bar-map nil)
2777
2778 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2779 ;; affect _new_ message buffers.  We might add a function that walks thru all
2780 ;; summary-mode buffers and force the update.
2781 (defun gnus-summary-tool-bar-update (&optional symbol value)
2782   "Update summary mode toolbar.
2783 Setter function for custom variables."
2784   (setq-default gnus-summary-tool-bar-map nil)
2785   (when symbol
2786     ;; When used as ":set" function:
2787     (set-default symbol value))
2788   (when (gnus-buffer-live-p gnus-summary-buffer)
2789     (with-current-buffer gnus-summary-buffer
2790       (gnus-summary-make-tool-bar))))
2791
2792 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2793                                      'gnus-summary-tool-bar-gnome
2794                                    'gnus-summary-tool-bar-retro)
2795   "Specifies the Gnus summary tool bar.
2796
2797 It can be either a list or a symbol refering to a list.  See
2798 `gmm-tool-bar-from-list' for the format of the list.  The
2799 default key map is `gnus-summary-mode-map'.
2800
2801 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2802 `gnus-summary-tool-bar-retro'."
2803   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2804                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2805                  (repeat :tag "User defined list" gmm-tool-bar-item)
2806                  (symbol))
2807   :version "23.1" ;; No Gnus
2808   :initialize 'custom-initialize-default
2809   :set 'gnus-summary-tool-bar-update
2810   :group 'gnus-summary)
2811
2812 (defcustom gnus-summary-tool-bar-gnome
2813   '((gnus-summary-post-news "mail/compose" nil)
2814     (gnus-summary-insert-new-articles "mail/inbox" nil
2815                                       :visible (or (not gnus-agent)
2816                                                    gnus-plugged))
2817     (gnus-summary-reply-with-original "mail/reply")
2818     (gnus-summary-reply "mail/reply" nil :visible nil)
2819     (gnus-summary-followup-with-original "mail/reply-all")
2820     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2821     (gnus-summary-mail-forward "mail/forward")
2822     (gnus-summary-save-article "mail/save")
2823     (gnus-summary-search-article-forward "search" nil :visible nil)
2824     (gnus-summary-print-article "print")
2825     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2826     ;; Some new commands that may need more suitable icons:
2827     (gnus-summary-save-newsrc "save" nil :visible nil)
2828     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2829     (gnus-summary-prev-article "left-arrow")
2830     (gnus-summary-next-article "right-arrow")
2831     (gnus-summary-next-page "next-page")
2832     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2833     ;;
2834     ;; Maybe some sort-by-... could be added:
2835     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2836     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2837     (gnus-summary-mark-as-expirable
2838      "delete" nil
2839      :visible (gnus-check-backend-function 'request-expire-articles
2840                                            gnus-newsgroup-name))
2841     (gnus-summary-mark-as-spam
2842      "mail/spam" t
2843      :visible (and (fboundp 'spam-group-ham-contents-p)
2844                    (spam-group-ham-contents-p gnus-newsgroup-name))
2845      :help "Mark as spam")
2846     (gnus-summary-mark-as-read-forward
2847      "mail/not-spam" nil
2848      :visible (and (fboundp 'spam-group-spam-contents-p)
2849                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2850     ;;
2851     (gnus-summary-exit "exit")
2852     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2853     (gnus-info-find-node "help"))
2854   "List of functions for the summary tool bar (GNOME style).
2855
2856 See `gmm-tool-bar-from-list' for the format of the list."
2857   :type '(repeat gmm-tool-bar-item)
2858   :version "23.1" ;; No Gnus
2859   :initialize 'custom-initialize-default
2860   :set 'gnus-summary-tool-bar-update
2861   :group 'gnus-summary)
2862
2863 (defcustom gnus-summary-tool-bar-retro
2864   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2865     (gnus-summary-next-unread-article "gnus/next-ur")
2866     (gnus-summary-post-news "gnus/post")
2867     (gnus-summary-followup-with-original "gnus/fuwo")
2868     (gnus-summary-followup "gnus/followup")
2869     (gnus-summary-reply-with-original "gnus/reply-wo")
2870     (gnus-summary-reply "gnus/reply")
2871     (gnus-summary-caesar-message "gnus/rot13")
2872     (gnus-uu-decode-uu "gnus/uu-decode")
2873     (gnus-summary-save-article-file "gnus/save-aif")
2874     (gnus-summary-save-article "gnus/save-art")
2875     (gnus-uu-post-news "gnus/uu-post")
2876     (gnus-summary-catchup "gnus/catchup")
2877     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2878     (gnus-summary-exit "gnus/exit-summ")
2879     ;; Some new command that may need more suitable icons:
2880     (gnus-summary-print-article "gnus/print" nil :visible nil)
2881     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2882     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2883     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2884     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2885     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2886     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2887     ;;
2888     (gnus-info-find-node "gnus/help" nil :visible nil))
2889   "List of functions for the summary tool bar (retro look).
2890
2891 See `gmm-tool-bar-from-list' for the format of the list."
2892   :type '(repeat gmm-tool-bar-item)
2893   :version "23.1" ;; No Gnus
2894   :initialize 'custom-initialize-default
2895   :set 'gnus-summary-tool-bar-update
2896   :group 'gnus-summary)
2897
2898 (defcustom gnus-summary-tool-bar-zap-list t
2899   "List of icon items from the global tool bar.
2900 These items are not displayed in the Gnus summary mode tool bar.
2901
2902 See `gmm-tool-bar-from-list' for the format of the list."
2903   :type 'gmm-tool-bar-zap-list
2904   :version "23.1" ;; No Gnus
2905   :initialize 'custom-initialize-default
2906   :set 'gnus-summary-tool-bar-update
2907   :group 'gnus-summary)
2908
2909 (defvar image-load-path)
2910 (defvar tool-bar-map)
2911
2912 (defun gnus-summary-make-tool-bar (&optional force)
2913   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2914 When FORCE, rebuild the tool bar."
2915   (when (and (not (featurep 'xemacs))
2916              (boundp 'tool-bar-mode)
2917              tool-bar-mode
2918              (or (not gnus-summary-tool-bar-map) force))
2919     (let* ((load-path
2920             (gmm-image-load-path-for-library "gnus"
2921                                              "mail/save.xpm"
2922                                              nil t))
2923            (image-load-path (cons (car load-path)
2924                                   (when (boundp 'image-load-path)
2925                                     image-load-path)))
2926            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2927                                         gnus-summary-tool-bar-zap-list
2928                                         'gnus-summary-mode-map)))
2929       (when map
2930         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2931         ;; uses it's value.
2932         (setq gnus-summary-tool-bar-map map))))
2933   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2934
2935 (defun gnus-score-set-default (var value)
2936   "A version of set that updates the GNU Emacs menu-bar."
2937   (set var value)
2938   ;; It is the message that forces the active status to be updated.
2939   (message ""))
2940
2941 (defun gnus-make-score-map (type)
2942   "Make a summary score map of type TYPE."
2943   (if t
2944       nil
2945     (let ((headers '(("author" "from" string)
2946                      ("subject" "subject" string)
2947                      ("article body" "body" string)
2948                      ("article head" "head" string)
2949                      ("xref" "xref" string)
2950                      ("extra header" "extra" string)
2951                      ("lines" "lines" number)
2952                      ("followups to author" "followup" string)))
2953           (types '((number ("less than" <)
2954                            ("greater than" >)
2955                            ("equal" =))
2956                    (string ("substring" s)
2957                            ("exact string" e)
2958                            ("fuzzy string" f)
2959                            ("regexp" r))))
2960           (perms '(("temporary" (current-time-string))
2961                    ("permanent" nil)
2962                    ("immediate" now)))
2963           header)
2964       (list
2965        (apply
2966         'nconc
2967         (list
2968          (if (eq type 'lower)
2969              "Lower score"
2970            "Increase score"))
2971         (let (outh)
2972           (while headers
2973             (setq header (car headers))
2974             (setq outh
2975                   (cons
2976                    (apply
2977                     'nconc
2978                     (list (car header))
2979                     (let ((ts (cdr (assoc (nth 2 header) types)))
2980                           outt)
2981                       (while ts
2982                         (setq outt
2983                               (cons
2984                                (apply
2985                                 'nconc
2986                                 (list (caar ts))
2987                                 (let ((ps perms)
2988                                       outp)
2989                                   (while ps
2990                                     (setq outp
2991                                           (cons
2992                                            (vector
2993                                             (caar ps)
2994                                             (list
2995                                              'gnus-summary-score-entry
2996                                              (nth 1 header)
2997                                              (if (or (string= (nth 1 header)
2998                                                               "head")
2999                                                      (string= (nth 1 header)
3000                                                               "body"))
3001                                                  ""
3002                                                (list 'gnus-summary-header
3003                                                      (nth 1 header)))
3004                                              (list 'quote (nth 1 (car ts)))
3005                                              (list 'gnus-score-delta-default
3006                                                    nil)
3007                                              (nth 1 (car ps))
3008                                              t)
3009                                             t)
3010                                            outp))
3011                                     (setq ps (cdr ps)))
3012                                   (list (nreverse outp))))
3013                                outt))
3014                         (setq ts (cdr ts)))
3015                       (list (nreverse outt))))
3016                    outh))
3017             (setq headers (cdr headers)))
3018           (list (nreverse outh))))))))
3019
3020
3021 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3022 (defvar bookmark-make-record-function)
3023 \f
3024
3025 (defun gnus-summary-mode (&optional group)
3026   "Major mode for reading articles.
3027
3028 All normal editing commands are switched off.
3029 \\<gnus-summary-mode-map>
3030 Each line in this buffer represents one article.  To read an
3031 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3032 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3033 respectively.
3034
3035 You can also post articles and send mail from this buffer.  To
3036 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3037 of an article, type `\\[gnus-summary-reply]'.
3038
3039 There are approx. one gazillion commands you can execute in this
3040 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3041
3042 The following commands are available:
3043
3044 \\{gnus-summary-mode-map}"
3045   (interactive)
3046   (kill-all-local-variables)
3047   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3048     (gnus-summary-make-local-variables))
3049   (gnus-summary-make-local-variables)
3050   (setq gnus-newsgroup-name group)
3051   (when (gnus-visual-p 'summary-menu 'menu)
3052     (gnus-summary-make-menu-bar)
3053     (gnus-summary-make-tool-bar))
3054   (gnus-make-thread-indent-array)
3055   (gnus-simplify-mode-line)
3056   (setq major-mode 'gnus-summary-mode)
3057   (setq mode-name "Summary")
3058   (use-local-map gnus-summary-mode-map)
3059   (buffer-disable-undo)
3060   (setq buffer-read-only t              ;Disable modification
3061         show-trailing-whitespace nil)
3062   (setq truncate-lines t)
3063   (add-to-invisibility-spec '(gnus-sum . t))
3064   (gnus-summary-set-display-table)
3065   (gnus-set-default-directory)
3066   (make-local-variable 'gnus-summary-line-format)
3067   (make-local-variable 'gnus-summary-line-format-spec)
3068   (make-local-variable 'gnus-summary-dummy-line-format)
3069   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3070   (make-local-variable 'gnus-summary-mark-positions)
3071   (gnus-make-local-hook 'pre-command-hook)
3072   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3073   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3074   (turn-on-gnus-mailing-list-mode)
3075   (mm-enable-multibyte)
3076   (set (make-local-variable 'bookmark-make-record-function)
3077        'gnus-summary-bookmark-make-record)
3078   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3079   (gnus-update-summary-mark-positions))
3080
3081 (defun gnus-summary-make-local-variables ()
3082   "Make all the local summary buffer variables."
3083   (let (global)
3084     (dolist (local gnus-summary-local-variables)
3085       (if (consp local)
3086           (progn
3087             (if (eq (cdr local) 'global)
3088                 ;; Copy the global value of the variable.
3089                 (setq global (symbol-value (car local)))
3090               ;; Use the value from the list.
3091               (setq global (eval (cdr local))))
3092             (set (make-local-variable (car local)) global))
3093         ;; Simple nil-valued local variable.
3094         (set (make-local-variable local) nil)))))
3095
3096 (defun gnus-summary-clear-local-variables ()
3097   (let ((locals gnus-summary-local-variables))
3098     (while locals
3099       (if (consp (car locals))
3100           (and (symbolp (caar locals))
3101                (set (caar locals) nil))
3102         (and (symbolp (car locals))
3103              (set (car locals) nil)))
3104       (setq locals (cdr locals)))))
3105
3106 ;; Summary data functions.
3107
3108 (defmacro gnus-data-number (data)
3109   `(car ,data))
3110
3111 (defmacro gnus-data-set-number (data number)
3112   `(setcar ,data ,number))
3113
3114 (defmacro gnus-data-mark (data)
3115   `(nth 1 ,data))
3116
3117 (defmacro gnus-data-set-mark (data mark)
3118   `(setcar (nthcdr 1 ,data) ,mark))
3119
3120 (defmacro gnus-data-pos (data)
3121   `(nth 2 ,data))
3122
3123 (defmacro gnus-data-set-pos (data pos)
3124   `(setcar (nthcdr 2 ,data) ,pos))
3125
3126 (defmacro gnus-data-header (data)
3127   `(nth 3 ,data))
3128
3129 (defmacro gnus-data-set-header (data header)
3130   `(setf (nth 3 ,data) ,header))
3131
3132 (defmacro gnus-data-level (data)
3133   `(nth 4 ,data))
3134
3135 (defmacro gnus-data-unread-p (data)
3136   `(= (nth 1 ,data) gnus-unread-mark))
3137
3138 (defmacro gnus-data-read-p (data)
3139   `(/= (nth 1 ,data) gnus-unread-mark))
3140
3141 (defmacro gnus-data-pseudo-p (data)
3142   `(consp (nth 3 ,data)))
3143
3144 (defmacro gnus-data-find (number)
3145   `(assq ,number gnus-newsgroup-data))
3146
3147 (defmacro gnus-data-find-list (number &optional data)
3148   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3149      (memq (assq ,number bdata)
3150            bdata)))
3151
3152 (defmacro gnus-data-make (number mark pos header level)
3153   `(list ,number ,mark ,pos ,header ,level))
3154
3155 (defun gnus-data-enter (after-article number mark pos header level offset)
3156   (let ((data (gnus-data-find-list after-article)))
3157     (unless data
3158       (error "No such article: %d" after-article))
3159     (setcdr data (cons (gnus-data-make number mark pos header level)
3160                        (cdr data)))
3161     (setq gnus-newsgroup-data-reverse nil)
3162     (gnus-data-update-list (cddr data) offset)))
3163
3164 (defun gnus-data-enter-list (after-article list &optional offset)
3165   (when list
3166     (let ((data (and after-article (gnus-data-find-list after-article)))
3167           (ilist list))
3168       (if (not (or data
3169                    after-article))
3170           (let ((odata gnus-newsgroup-data))
3171             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3172             (when offset
3173               (gnus-data-update-list odata offset)))
3174         ;; Find the last element in the list to be spliced into the main
3175         ;; list.
3176         (setq list (last list))
3177         (if (not data)
3178             (progn
3179               (setcdr list gnus-newsgroup-data)
3180               (setq gnus-newsgroup-data ilist)
3181               (when offset
3182                 (gnus-data-update-list (cdr list) offset)))
3183           (setcdr list (cdr data))
3184           (setcdr data ilist)
3185           (when offset
3186             (gnus-data-update-list (cdr list) offset))))
3187       (setq gnus-newsgroup-data-reverse nil))))
3188
3189 (defun gnus-data-remove (article &optional offset)
3190   (let ((data gnus-newsgroup-data))
3191     (if (= (gnus-data-number (car data)) article)
3192         (progn
3193           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3194                 gnus-newsgroup-data-reverse nil)
3195           (when offset
3196             (gnus-data-update-list gnus-newsgroup-data offset)))
3197       (while (cdr data)
3198         (when (= (gnus-data-number (cadr data)) article)
3199           (setcdr data (cddr data))
3200           (when offset
3201             (gnus-data-update-list (cdr data) offset))
3202           (setq data nil
3203                 gnus-newsgroup-data-reverse nil))
3204         (setq data (cdr data))))))
3205
3206 (defmacro gnus-data-list (backward)
3207   `(if ,backward
3208        (or gnus-newsgroup-data-reverse
3209            (setq gnus-newsgroup-data-reverse
3210                  (reverse gnus-newsgroup-data)))
3211      gnus-newsgroup-data))
3212
3213 (defun gnus-data-update-list (data offset)
3214   "Add OFFSET to the POS of all data entries in DATA."
3215   (setq gnus-newsgroup-data-reverse nil)
3216   (while data
3217     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3218     (setq data (cdr data))))
3219
3220 (defun gnus-summary-article-pseudo-p (article)
3221   "Say whether this article is a pseudo article or not."
3222   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3223
3224 (defmacro gnus-summary-article-sparse-p (article)
3225   "Say whether this article is a sparse article or not."
3226   `(memq ,article gnus-newsgroup-sparse))
3227
3228 (defmacro gnus-summary-article-ancient-p (article)
3229   "Say whether this article is a sparse article or not."
3230   `(memq ,article gnus-newsgroup-ancient))
3231
3232 (defun gnus-article-parent-p (number)
3233   "Say whether this article is a parent or not."
3234   (let ((data (gnus-data-find-list number)))
3235     (and (cdr data)              ; There has to be an article after...
3236          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3237             (gnus-data-level (nth 1 data))))))
3238
3239 (defun gnus-article-children (number)
3240   "Return a list of all children to NUMBER."
3241   (let* ((data (gnus-data-find-list number))
3242          (level (gnus-data-level (car data)))
3243          children)
3244     (setq data (cdr data))
3245     (while (and data
3246                 (= (gnus-data-level (car data)) (1+ level)))
3247       (push (gnus-data-number (car data)) children)
3248       (setq data (cdr data)))
3249     children))
3250
3251 (defmacro gnus-summary-skip-intangible ()
3252   "If the current article is intangible, then jump to a different article."
3253   '(let ((to (get-text-property (point) 'gnus-intangible)))
3254      (and to (gnus-summary-goto-subject to))))
3255
3256 (defmacro gnus-summary-article-intangible-p ()
3257   "Say whether this article is intangible or not."
3258   '(get-text-property (point) 'gnus-intangible))
3259
3260 (defun gnus-article-read-p (article)
3261   "Say whether ARTICLE is read or not."
3262   (not (or (memq article gnus-newsgroup-marked)
3263            (memq article gnus-newsgroup-spam-marked)
3264            (memq article gnus-newsgroup-unreads)
3265            (memq article gnus-newsgroup-unselected)
3266            (memq article gnus-newsgroup-dormant))))
3267
3268 ;; Some summary mode macros.
3269
3270 (defmacro gnus-summary-article-number ()
3271   "The article number of the article on the current line.
3272 If there isn't an article number here, then we return the current
3273 article number."
3274   '(progn
3275      (gnus-summary-skip-intangible)
3276      (or (get-text-property (point) 'gnus-number)
3277          (gnus-summary-last-subject))))
3278
3279 (defmacro gnus-summary-article-header (&optional number)
3280   "Return the header of article NUMBER."
3281   `(gnus-data-header (gnus-data-find
3282                       ,(or number '(gnus-summary-article-number)))))
3283
3284 (defmacro gnus-summary-thread-level (&optional number)
3285   "Return the level of thread that starts with article NUMBER."
3286   `(if (and (eq gnus-summary-make-false-root 'dummy)
3287             (get-text-property (point) 'gnus-intangible))
3288        0
3289      (gnus-data-level (gnus-data-find
3290                        ,(or number '(gnus-summary-article-number))))))
3291
3292 (defmacro gnus-summary-article-mark (&optional number)
3293   "Return the mark of article NUMBER."
3294   `(gnus-data-mark (gnus-data-find
3295                     ,(or number '(gnus-summary-article-number)))))
3296
3297 (defmacro gnus-summary-article-pos (&optional number)
3298   "Return the position of the line of article NUMBER."
3299   `(gnus-data-pos (gnus-data-find
3300                    ,(or number '(gnus-summary-article-number)))))
3301
3302 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3303 (defmacro gnus-summary-article-subject (&optional number)
3304   "Return current subject string or nil if nothing."
3305   `(let ((headers
3306           ,(if number
3307                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3308              '(gnus-data-header (assq (gnus-summary-article-number)
3309                                       gnus-newsgroup-data)))))
3310      (and headers
3311           (vectorp headers)
3312           (mail-header-subject headers))))
3313
3314 (defmacro gnus-summary-article-score (&optional number)
3315   "Return current article score."
3316   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3317                   gnus-newsgroup-scored))
3318        gnus-summary-default-score 0))
3319
3320 (defun gnus-summary-article-children (&optional number)
3321   "Return a list of article numbers that are children of article NUMBER."
3322   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3323          (level (gnus-data-level (car data)))
3324          l children)
3325     (while (and (setq data (cdr data))
3326                 (> (setq l (gnus-data-level (car data))) level))
3327       (and (= (1+ level) l)
3328            (push (gnus-data-number (car data))
3329                  children)))
3330     (nreverse children)))
3331
3332 (defun gnus-summary-article-parent (&optional number)
3333   "Return the article number of the parent of article NUMBER."
3334   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3335                                     (gnus-data-list t)))
3336          (level (gnus-data-level (car data))))
3337     (if (zerop level)
3338         ()                              ; This is a root.
3339       ;; We search until we find an article with a level less than
3340       ;; this one.  That function has to be the parent.
3341       (while (and (setq data (cdr data))
3342                   (not (< (gnus-data-level (car data)) level))))
3343       (and data (gnus-data-number (car data))))))
3344
3345 (defun gnus-unread-mark-p (mark)
3346   "Say whether MARK is the unread mark."
3347   (= mark gnus-unread-mark))
3348
3349 (defun gnus-read-mark-p (mark)
3350   "Say whether MARK is one of the marks that mark as read.
3351 This is all marks except unread, ticked, dormant, and expirable."
3352   (not (or (= mark gnus-unread-mark)
3353            (= mark gnus-ticked-mark)
3354            (= mark gnus-spam-mark)
3355            (= mark gnus-dormant-mark)
3356            (= mark gnus-expirable-mark))))
3357
3358 (defmacro gnus-article-mark (number)
3359   "Return the MARK of article NUMBER.
3360 This macro should only be used when computing the mark the \"first\"
3361 time; i.e., when generating the summary lines.  After that,
3362 `gnus-summary-article-mark' should be used to examine the
3363 marks of articles."
3364   `(cond
3365     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3366     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3367     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3368     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3369     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3370     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3371     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3372     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3373            gnus-ancient-mark))))
3374
3375 ;; Saving hidden threads.
3376
3377 (defmacro gnus-save-hidden-threads (&rest forms)
3378   "Save hidden threads, eval FORMS, and restore the hidden threads."
3379   (let ((config (make-symbol "config")))
3380     `(let ((,config (gnus-hidden-threads-configuration)))
3381        (unwind-protect
3382            (save-excursion
3383              ,@forms)
3384          (gnus-restore-hidden-threads-configuration ,config)))))
3385 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3386 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3387
3388 (defun gnus-data-compute-positions ()
3389   "Compute the positions of all articles."
3390   (setq gnus-newsgroup-data-reverse nil)
3391   (let ((data gnus-newsgroup-data))
3392     (save-excursion
3393       (gnus-save-hidden-threads
3394         (gnus-summary-show-all-threads)
3395         (goto-char (point-min))
3396         (while data
3397           (while (get-text-property (point) 'gnus-intangible)
3398             (forward-line 1))
3399           (gnus-data-set-pos (car data) (+ (point) 3))
3400           (setq data (cdr data))
3401           (forward-line 1))))))
3402
3403 (defun gnus-hidden-threads-configuration ()
3404   "Return the current hidden threads configuration."
3405   (save-excursion
3406     (let (config)
3407       (goto-char (point-min))
3408       (while (not (eobp))
3409         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3410           (push (save-excursion (forward-line 0) (point)) config))
3411         (forward-line 1))
3412       config)))
3413
3414 (defun gnus-restore-hidden-threads-configuration (config)
3415   "Restore hidden threads configuration from CONFIG."
3416   (save-excursion
3417     (let (point (inhibit-read-only t))
3418       (while (setq point (pop config))
3419         (goto-char point)
3420         (gnus-summary-hide-thread)))))
3421
3422 ;; Various summary mode internalish functions.
3423
3424 (defun gnus-mouse-pick-article (e)
3425   (interactive "e")
3426   (mouse-set-point e)
3427   (gnus-summary-next-page nil t))
3428
3429 (defun gnus-summary-set-display-table ()
3430   "Change the display table.
3431 Odd characters have a tendency to mess
3432 up nicely formatted displays - we make all possible glyphs
3433 display only a single character."
3434
3435   ;; We start from the standard display table, if any.
3436   (let ((table (or (copy-sequence standard-display-table)
3437                    (make-display-table)))
3438         (i 32))
3439     ;; Nix out all the control chars...
3440     (while (>= (setq i (1- i)) 0)
3441       (gnus-put-display-table i [??] table))
3442    ;; ... but not newline and cr, of course.  (cr is necessary for the
3443     ;; selective display).
3444     (gnus-put-display-table ?\n nil table)
3445     (gnus-put-display-table ?\r nil table)
3446     ;; We keep TAB as well.
3447     (gnus-put-display-table ?\t nil table)
3448     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3449     ;; Emacs 23 (unicode), that are not set already.
3450     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3451                  160
3452                256)))
3453       (while (>= (setq i (1- i)) 127)
3454         ;; Only modify if the entry is nil.
3455         (unless (gnus-get-display-table i table)
3456           (gnus-put-display-table i [??] table))))
3457     (setq buffer-display-table table)))
3458
3459 (defun gnus-summary-set-article-display-arrow (pos)
3460   "Update the overlay arrow to point to line at position POS."
3461   (when gnus-summary-display-arrow
3462     (make-local-variable 'overlay-arrow-position)
3463     (make-local-variable 'overlay-arrow-string)
3464     (save-excursion
3465       (goto-char pos)
3466       (beginning-of-line)
3467       (unless overlay-arrow-position
3468         (setq overlay-arrow-position (make-marker)))
3469       (setq overlay-arrow-string "=>"
3470             overlay-arrow-position (set-marker overlay-arrow-position
3471                                                (point)
3472                                                (current-buffer))))))
3473
3474 (defun gnus-summary-setup-buffer (group)
3475   "Initialize summary buffer."
3476   (let ((buffer (gnus-summary-buffer-name group))
3477         (dead-name (concat "*Dead Summary "
3478                            (gnus-group-decoded-name group) "*")))
3479     ;; If a dead summary buffer exists, we kill it.
3480     (when (gnus-buffer-live-p dead-name)
3481       (gnus-kill-buffer dead-name))
3482     (if (get-buffer buffer)
3483         (progn
3484           (set-buffer buffer)
3485           (setq gnus-summary-buffer (current-buffer))
3486           (not gnus-newsgroup-prepared))
3487       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3488       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3489       (gnus-summary-mode group)
3490       (when gnus-carpal
3491         (gnus-carpal-setup-buffer 'summary))
3492       (when (gnus-group-quit-config group)
3493         (set (make-local-variable 'gnus-single-article-buffer) nil))
3494       (make-local-variable 'gnus-article-buffer)
3495       (make-local-variable 'gnus-article-current)
3496       (make-local-variable 'gnus-original-article-buffer)
3497       (setq gnus-newsgroup-name group)
3498       ;; Set any local variables in the group parameters.
3499       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3500       t)))
3501
3502 (defun gnus-set-global-variables ()
3503   "Set the global equivalents of the buffer-local variables.
3504 They are set to the latest values they had.  These reflect the summary
3505 buffer that was in action when the last article was fetched."
3506   (when (eq major-mode 'gnus-summary-mode)
3507     (setq gnus-summary-buffer (current-buffer))
3508     (let ((name gnus-newsgroup-name)
3509           (marked gnus-newsgroup-marked)
3510           (spam gnus-newsgroup-spam-marked)
3511           (unread gnus-newsgroup-unreads)
3512           (headers gnus-current-headers)
3513           (data gnus-newsgroup-data)
3514           (summary gnus-summary-buffer)
3515           (article-buffer gnus-article-buffer)
3516           (original gnus-original-article-buffer)
3517           (gac gnus-article-current)
3518           (reffed gnus-reffed-article-number)
3519           (score-file gnus-current-score-file)
3520           (default-charset gnus-newsgroup-charset)
3521           vlist)
3522       (let ((locals gnus-newsgroup-variables))
3523         (while locals
3524           (if (consp (car locals))
3525               (push (eval (caar locals)) vlist)
3526             (push (eval (car locals)) vlist))
3527           (setq locals (cdr locals)))
3528         (setq vlist (nreverse vlist)))
3529       (with-current-buffer gnus-group-buffer
3530         (setq gnus-newsgroup-name name
3531               gnus-newsgroup-marked marked
3532               gnus-newsgroup-spam-marked spam
3533               gnus-newsgroup-unreads unread
3534               gnus-current-headers headers
3535               gnus-newsgroup-data data
3536               gnus-article-current gac
3537               gnus-summary-buffer summary
3538               gnus-article-buffer article-buffer
3539               gnus-original-article-buffer original
3540               gnus-reffed-article-number reffed
3541               gnus-current-score-file score-file
3542               gnus-newsgroup-charset default-charset)
3543         (let ((locals gnus-newsgroup-variables))
3544           (while locals
3545             (if (consp (car locals))
3546                 (set (caar locals) (pop vlist))
3547               (set (car locals) (pop vlist)))
3548             (setq locals (cdr locals))))
3549         ;; The article buffer also has local variables.
3550         (when (gnus-buffer-live-p gnus-article-buffer)
3551           (set-buffer gnus-article-buffer)
3552           (setq gnus-summary-buffer summary))))))
3553
3554 (defun gnus-summary-article-unread-p (article)
3555   "Say whether ARTICLE is unread or not."
3556   (memq article gnus-newsgroup-unreads))
3557
3558 (defun gnus-summary-first-article-p (&optional article)
3559   "Return whether ARTICLE is the first article in the buffer."
3560   (if (not (setq article (or article (gnus-summary-article-number))))
3561       nil
3562     (eq article (caar gnus-newsgroup-data))))
3563
3564 (defun gnus-summary-last-article-p (&optional article)
3565   "Return whether ARTICLE is the last article in the buffer."
3566   (if (not (setq article (or article (gnus-summary-article-number))))
3567       ;; All non-existent numbers are the last article.  :-)
3568       t
3569     (not (cdr (gnus-data-find-list article)))))
3570
3571 (defun gnus-make-thread-indent-array (&optional n)
3572   (when (or n
3573             (progn (setq n 200) nil)
3574             (null gnus-thread-indent-array)
3575             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3576     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3577           gnus-thread-indent-array-level gnus-thread-indent-level)
3578     (while (>= n 0)
3579       (aset gnus-thread-indent-array n
3580             (make-string (* n gnus-thread-indent-level) ? ))
3581       (setq n (1- n)))))
3582
3583 (defun gnus-update-summary-mark-positions ()
3584   "Compute where the summary marks are to go."
3585   (save-excursion
3586     (when (gnus-buffer-exists-p gnus-summary-buffer)
3587       (set-buffer gnus-summary-buffer))
3588     (let ((spec gnus-summary-line-format-spec)
3589           pos)
3590       (save-excursion
3591         (gnus-set-work-buffer)
3592         (let ((gnus-tmp-unread ?Z)
3593               (gnus-replied-mark ?Z)
3594               (gnus-score-below-mark ?Z)
3595               (gnus-score-over-mark ?Z)
3596               (gnus-undownloaded-mark ?Z)
3597               (gnus-summary-line-format-spec spec)
3598               (gnus-newsgroup-downloadable '(0))
3599               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3600               case-fold-search ignores)
3601           ;; Here, all marks are bound to Z.
3602           (gnus-summary-insert-line header
3603                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3604           (goto-char (point-min))
3605           ;; Memorize the positions of the same characters as dummy marks.
3606           (while (re-search-forward "[A-D]" nil t)
3607             (push (point) ignores))
3608           (erase-buffer)
3609           ;; We use A-D as dummy marks in order to know column positions
3610           ;; where marks should be inserted.
3611           (setq gnus-tmp-unread ?A
3612                 gnus-replied-mark ?B
3613                 gnus-score-below-mark ?C
3614                 gnus-score-over-mark ?C
3615                 gnus-undownloaded-mark ?D)
3616           (gnus-summary-insert-line header
3617                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3618           ;; Ignore characters which aren't dummy marks.
3619           (dolist (p ignores)
3620             (delete-region (goto-char (1- p)) p)
3621             (insert ?Z))
3622           (goto-char (point-min))
3623           (setq pos (list (cons 'unread
3624                                 (and (search-forward "A" nil t)
3625                                      (- (point) (point-min) 1)))))
3626           (goto-char (point-min))
3627           (push (cons 'replied (and (search-forward "B" nil t)
3628                                     (- (point) (point-min) 1)))
3629                 pos)
3630           (goto-char (point-min))
3631           (push (cons 'score (and (search-forward "C" nil t)
3632                                   (- (point) (point-min) 1)))
3633                 pos)
3634           (goto-char (point-min))
3635           (push (cons 'download (and (search-forward "D" nil t)
3636                                      (- (point) (point-min) 1)))
3637                 pos)))
3638       (setq gnus-summary-mark-positions pos))))
3639
3640 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3641   "Insert a dummy root in the summary buffer."
3642   (beginning-of-line)
3643   (gnus-add-text-properties
3644    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3645    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3646
3647 (defun gnus-summary-extract-address-component (from)
3648   (or (car (funcall gnus-extract-address-components from))
3649       from))
3650
3651 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3652   (let ((mail-parse-charset gnus-newsgroup-charset)
3653         (ignored-from-addresses (gnus-ignored-from-addresses))
3654         ; Is it really necessary to do this next part for each summary line?
3655         ; Luckily, doesn't seem to slow things down much.
3656         (mail-parse-ignored-charsets
3657          (with-current-buffer gnus-summary-buffer
3658            gnus-newsgroup-ignored-charsets)))
3659     (or
3660      (and ignored-from-addresses
3661           (string-match ignored-from-addresses gnus-tmp-from)
3662           (let ((extra-headers (mail-header-extra header))
3663                 to
3664                 newsgroups)
3665             (cond
3666              ((setq to (cdr (assq 'To extra-headers)))
3667               (concat gnus-summary-to-prefix
3668                       (inline
3669                         (gnus-summary-extract-address-component
3670                          (funcall gnus-decode-encoded-address-function to)))))
3671              ((setq newsgroups
3672                     (or
3673                      (cdr (assq 'Newsgroups extra-headers))
3674                      (and
3675                       (memq 'Newsgroups gnus-extra-headers)
3676                       (eq (car (gnus-find-method-for-group
3677                                 gnus-newsgroup-name)) 'nntp)
3678                       (gnus-group-real-name gnus-newsgroup-name))))
3679               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3680      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3681
3682 (defun gnus-summary-insert-line (gnus-tmp-header
3683                                  gnus-tmp-level gnus-tmp-current
3684                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3685                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3686                                  &optional gnus-tmp-dummy gnus-tmp-score
3687                                  gnus-tmp-process)
3688   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3689       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3690                                           gnus-tmp-level)))
3691   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3692          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3693          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3694          (gnus-tmp-score-char
3695           (if (or (null gnus-summary-default-score)
3696                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3697                       gnus-summary-zcore-fuzz))
3698               ?                         ;Whitespace
3699             (if (< gnus-tmp-score gnus-summary-default-score)
3700                 gnus-score-below-mark gnus-score-over-mark)))
3701          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3702          (gnus-tmp-replied
3703           (cond (gnus-tmp-process gnus-process-mark)
3704                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3705                  gnus-cached-mark)
3706                 (gnus-tmp-replied gnus-replied-mark)
3707                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3708                  gnus-forwarded-mark)
3709                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3710                  gnus-saved-mark)
3711                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3712                  gnus-recent-mark)
3713                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3714                  gnus-unseen-mark)
3715                 (t gnus-no-mark)))
3716          (gnus-tmp-downloaded
3717           (cond (undownloaded
3718                  gnus-undownloaded-mark)
3719                 (gnus-newsgroup-agentized
3720                  gnus-downloaded-mark)
3721                 (t
3722                  gnus-no-mark)))
3723          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3724          (gnus-tmp-name
3725           (cond
3726            ((string-match "<[^>]+> *$" gnus-tmp-from)
3727             (let ((beg (match-beginning 0)))
3728               (or (and (string-match "^\".+\"" gnus-tmp-from)
3729                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3730                   (substring gnus-tmp-from 0 beg))))
3731            ((string-match "(.+)" gnus-tmp-from)
3732             (substring gnus-tmp-from
3733                        (1+ (match-beginning 0)) (1- (match-end 0))))
3734            (t gnus-tmp-from)))
3735          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3736          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3737          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3738          (inhibit-read-only t))
3739     (when (string= gnus-tmp-name "")
3740       (setq gnus-tmp-name gnus-tmp-from))
3741     (unless (numberp gnus-tmp-lines)
3742       (setq gnus-tmp-lines -1))
3743     (if (= gnus-tmp-lines -1)
3744         (setq gnus-tmp-lines "?")
3745       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3746     (condition-case ()
3747         (gnus-put-text-property
3748          (point)
3749          (progn (eval gnus-summary-line-format-spec) (point))
3750          'gnus-number gnus-tmp-number)
3751       (error (gnus-message 5 "Error updating the summary line")))
3752     (when (gnus-visual-p 'summary-highlight 'highlight)
3753       (forward-line -1)
3754       (gnus-run-hooks 'gnus-summary-update-hook)
3755       (forward-line 1))))
3756
3757 (defun gnus-summary-update-line (&optional dont-update)
3758   "Update summary line after change."
3759   (when (and gnus-summary-default-score
3760              (not gnus-summary-inhibit-highlight))
3761     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3762            (article (gnus-summary-article-number))
3763            (score (gnus-summary-article-score article)))
3764       (unless dont-update
3765         (if (and gnus-summary-mark-below
3766                  (< (gnus-summary-article-score)
3767                     gnus-summary-mark-below))
3768             ;; This article has a low score, so we mark it as read.
3769             (when (memq article gnus-newsgroup-unreads)
3770               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3771           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3772             ;; This article was previously marked as read on account
3773             ;; of a low score, but now it has risen, so we mark it as
3774             ;; unread.
3775             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3776         (gnus-summary-update-mark
3777          (if (or (null gnus-summary-default-score)
3778                  (<= (abs (- score gnus-summary-default-score))
3779                      gnus-summary-zcore-fuzz))
3780              ?                          ;Whitespace
3781            (if (< score gnus-summary-default-score)
3782                gnus-score-below-mark gnus-score-over-mark))
3783          'score))
3784       ;; Do visual highlighting.
3785       (when (gnus-visual-p 'summary-highlight 'highlight)
3786         (gnus-run-hooks 'gnus-summary-update-hook)))))
3787
3788 (defvar gnus-tmp-new-adopts nil)
3789
3790 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3791   "Return the number of articles in THREAD.
3792 This may be 0 in some cases -- if none of the articles in
3793 the thread are to be displayed."
3794   (let* ((number
3795          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3796           (cond
3797            ((not (listp thread))
3798             1)
3799            ((and (consp thread) (cdr thread))
3800             (apply
3801              '+ 1 (mapcar
3802                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3803            ((null thread)
3804             1)
3805            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3806             1)
3807            (t 0))))
3808     (when (and level (zerop level) gnus-tmp-new-adopts)
3809       (incf number
3810             (apply '+ (mapcar
3811                        'gnus-summary-number-of-articles-in-thread
3812                        gnus-tmp-new-adopts))))
3813     (if char
3814         (if (> number 1) gnus-not-empty-thread-mark
3815           gnus-empty-thread-mark)
3816       number)))
3817
3818 (defsubst gnus-summary-line-message-size (head)
3819   "Return pretty-printed version of message size.
3820 This function is intended to be used in
3821 `gnus-summary-line-format-alist'."
3822   (let ((c (or (mail-header-chars head) -1)))
3823     (cond ((< c 0) "n/a")               ; chars not available
3824           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3825           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3826           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3827           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3828
3829
3830 (defun gnus-summary-set-local-parameters (group)
3831   "Go through the local params of GROUP and set all variable specs in that list."
3832   (let ((vars '(quit-config)))          ; Ignore quit-config.
3833     (dolist (elem (gnus-group-find-parameter group))
3834       (and (consp elem)                 ; Has to be a cons.
3835            (consp (cdr elem))           ; The cdr has to be a list.
3836            (symbolp (car elem))         ; Has to be a symbol in there.
3837            (not (memq (car elem) vars))
3838            (ignore-errors
3839              (push (car elem) vars)
3840              ;; Variables like `gnus-show-threads' that are globally
3841              ;; bound, if used as group parameters, need to get to be
3842              ;; buffer-local, whereas just parameters like `gcc-self',
3843              ;; `timestamp', etc. should not be bound as variables.
3844              (if (boundp (car elem))
3845                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3846                (eval (nth 1 elem))))))))
3847
3848 (defun gnus-summary-read-group (group &optional show-all no-article
3849                                       kill-buffer no-display backward
3850                                       select-articles)
3851   "Start reading news in newsgroup GROUP.
3852 If SHOW-ALL is non-nil, already read articles are also listed.
3853 If NO-ARTICLE is non-nil, no article is selected initially.
3854 If NO-DISPLAY, don't generate a summary buffer."
3855   (let (result)
3856     (while (and group
3857                 (null (setq result
3858                             (let ((gnus-auto-select-next nil))
3859                               (or (gnus-summary-read-group-1
3860                                    group show-all no-article
3861                                    kill-buffer no-display
3862                                    select-articles)
3863                                   (setq show-all nil
3864                                         select-articles nil)))))
3865                 (eq gnus-auto-select-next 'quietly))
3866       (set-buffer gnus-group-buffer)
3867       ;; The entry function called above goes to the next
3868       ;; group automatically, so we go two groups back
3869       ;; if we are searching for the previous group.
3870       (when backward
3871         (gnus-group-prev-unread-group 2))
3872       (if (not (equal group (gnus-group-group-name)))
3873           (setq group (gnus-group-group-name))
3874         (setq group nil)))
3875     result))
3876
3877 (defun gnus-summary-read-group-1 (group show-all no-article
3878                                         kill-buffer no-display
3879                                         &optional select-articles)
3880   ;; Killed foreign groups can't be entered.
3881   ;;  (when (and (not (gnus-group-native-p group))
3882   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3883   ;;    (error "Dead non-native groups can't be entered"))
3884   (gnus-message 5 "Retrieving newsgroup: %s..."
3885                 (gnus-group-decoded-name group))
3886   (let* ((new-group (gnus-summary-setup-buffer group))
3887          (quit-config (gnus-group-quit-config group))
3888          (did-select (and new-group (gnus-select-newsgroup
3889                                      group show-all select-articles))))
3890     (cond
3891      ;; This summary buffer exists already, so we just select it.
3892      ((not new-group)
3893       (gnus-set-global-variables)
3894       (when kill-buffer
3895         (gnus-kill-or-deaden-summary kill-buffer))
3896       (gnus-configure-windows 'summary 'force)
3897       (gnus-set-mode-line 'summary)
3898       (gnus-summary-position-point)
3899       (message "")
3900       t)
3901      ;; We couldn't select this group.
3902      ((null did-select)
3903       (when (and (eq major-mode 'gnus-summary-mode)
3904                  (not (equal (current-buffer) kill-buffer)))
3905         (kill-buffer (current-buffer))
3906         (if (not quit-config)
3907             (progn
3908               ;; Update the info -- marks might need to be removed,
3909               ;; for instance.
3910               (gnus-summary-update-info)
3911               (set-buffer gnus-group-buffer)
3912               (gnus-group-jump-to-group group)
3913               (gnus-group-next-unread-group 1))
3914           (gnus-handle-ephemeral-exit quit-config)))
3915       (let ((grpinfo (gnus-get-info group)))
3916         (if (null (gnus-info-read grpinfo))
3917             (gnus-message 3 "Group %s contains no messages"
3918                           (gnus-group-decoded-name group))
3919           (gnus-message 3 "Can't select group")))
3920       nil)
3921      ;; The user did a `C-g' while prompting for number of articles,
3922      ;; so we exit this group.
3923      ((eq did-select 'quit)
3924       (and (eq major-mode 'gnus-summary-mode)
3925            (not (equal (current-buffer) kill-buffer))
3926            (kill-buffer (current-buffer)))
3927       (when kill-buffer
3928         (gnus-kill-or-deaden-summary kill-buffer))
3929       (if (not quit-config)
3930           (progn
3931             (set-buffer gnus-group-buffer)
3932             (gnus-group-jump-to-group group)
3933             (gnus-configure-windows 'group 'force))
3934         (gnus-handle-ephemeral-exit quit-config))
3935       ;; Finally signal the quit.
3936       (signal 'quit nil))
3937      ;; The group was successfully selected.
3938      (t
3939       (gnus-set-global-variables)
3940       ;; Save the active value in effect when the group was entered.
3941       (setq gnus-newsgroup-active
3942             (gnus-copy-sequence
3943              (gnus-active gnus-newsgroup-name)))
3944       ;; You can change the summary buffer in some way with this hook.
3945       (gnus-run-hooks 'gnus-select-group-hook)
3946       (when (memq 'summary (gnus-update-format-specifications
3947                             nil 'summary 'summary-mode 'summary-dummy))
3948         ;; The format specification for the summary line was updated,
3949         ;; so we need to update the mark positions as well.
3950         (gnus-update-summary-mark-positions))
3951       ;; Do score processing.
3952       (when gnus-use-scoring
3953         (gnus-possibly-score-headers))
3954       ;; Check whether to fill in the gaps in the threads.
3955       (when gnus-build-sparse-threads
3956         (gnus-build-sparse-threads))
3957       ;; Find the initial limit.
3958       (if show-all
3959           (let ((gnus-newsgroup-dormant nil))
3960             (gnus-summary-initial-limit show-all))
3961         (gnus-summary-initial-limit show-all))
3962       ;; Generate the summary buffer.
3963       (unless no-display
3964         (gnus-summary-prepare))
3965       (when gnus-use-trees
3966         (gnus-tree-open group)
3967         (setq gnus-summary-highlight-line-function
3968               'gnus-tree-highlight-article))
3969       ;; If the summary buffer is empty, but there are some low-scored
3970       ;; articles or some excluded dormants, we include these in the
3971       ;; buffer.
3972       (when (and (zerop (buffer-size))
3973                  (not no-display))
3974         (cond (gnus-newsgroup-dormant
3975                (gnus-summary-limit-include-dormant))
3976               ((and gnus-newsgroup-scored show-all)
3977                (gnus-summary-limit-include-expunged t))))
3978       ;; Function `gnus-apply-kill-file' must be called in this hook.
3979       (gnus-run-hooks 'gnus-apply-kill-hook)
3980       (if (and (zerop (buffer-size))
3981                (not no-display))
3982           (progn
3983             ;; This newsgroup is empty.
3984             (gnus-summary-catchup-and-exit nil t)
3985             (gnus-message 6 "No unread news")
3986             (when kill-buffer
3987               (gnus-kill-or-deaden-summary kill-buffer))
3988             ;; Return nil from this function.
3989             nil)
3990         ;; Hide conversation thread subtrees.  We cannot do this in
3991         ;; gnus-summary-prepare-hook since kill processing may not
3992         ;; work with hidden articles.
3993         (gnus-summary-maybe-hide-threads)
3994         (when kill-buffer
3995           (gnus-kill-or-deaden-summary kill-buffer))
3996         (gnus-summary-auto-select-subject)
3997         ;; Show first unread article if requested.
3998         (if (and (not no-article)
3999                  (not no-display)
4000                  gnus-newsgroup-unreads
4001                  gnus-auto-select-first)
4002             (progn
4003               (gnus-configure-windows 'summary)
4004               (let ((art (gnus-summary-article-number)))
4005                 (unless (and (not gnus-plugged)
4006                              (or (memq art gnus-newsgroup-undownloaded)
4007                                  (memq art gnus-newsgroup-downloadable)))
4008                   (gnus-summary-goto-article art))))
4009           ;; Don't select any articles.
4010           (gnus-summary-position-point)
4011           (gnus-configure-windows 'summary 'force)
4012           (gnus-set-mode-line 'summary))
4013         (when (and gnus-auto-center-group
4014                    (get-buffer-window gnus-group-buffer t))
4015           ;; Gotta use windows, because recenter does weird stuff if
4016           ;; the current buffer ain't the displayed window.
4017           (let ((owin (selected-window)))
4018             (select-window (get-buffer-window gnus-group-buffer t))
4019             (when (gnus-group-goto-group group)
4020               (recenter))
4021             (select-window owin)))
4022         ;; Mark this buffer as "prepared".
4023         (setq gnus-newsgroup-prepared t)
4024         (gnus-run-hooks 'gnus-summary-prepared-hook)
4025         (unless (gnus-ephemeral-group-p group)
4026           (gnus-group-update-group group))
4027         t)))))
4028
4029 (defun gnus-summary-auto-select-subject ()
4030   "Select the subject line on initial group entry."
4031   (goto-char (point-min))
4032   (cond
4033    ((eq gnus-auto-select-subject 'best)
4034     (gnus-summary-best-unread-subject))
4035    ((eq gnus-auto-select-subject 'unread)
4036     (gnus-summary-first-unread-subject))
4037    ((eq gnus-auto-select-subject 'unseen)
4038     (gnus-summary-first-unseen-subject))
4039    ((eq gnus-auto-select-subject 'unseen-or-unread)
4040     (gnus-summary-first-unseen-or-unread-subject))
4041    ((eq gnus-auto-select-subject 'first)
4042     ;; Do nothing.
4043     )
4044    ((functionp gnus-auto-select-subject)
4045     (funcall gnus-auto-select-subject))))
4046
4047 (defun gnus-summary-prepare ()
4048   "Generate the summary buffer."
4049   (interactive)
4050   (let ((inhibit-read-only t))
4051     (erase-buffer)
4052     (setq gnus-newsgroup-data nil
4053           gnus-newsgroup-data-reverse nil)
4054     (gnus-run-hooks 'gnus-summary-generate-hook)
4055     ;; Generate the buffer, either with threads or without.
4056     (when gnus-newsgroup-headers
4057       (gnus-summary-prepare-threads
4058        (if gnus-show-threads
4059            (gnus-sort-gathered-threads
4060             (funcall gnus-summary-thread-gathering-function
4061                      (gnus-sort-threads
4062                       (gnus-cut-threads (gnus-make-threads)))))
4063          ;; Unthreaded display.
4064          (gnus-sort-articles gnus-newsgroup-headers))))
4065     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4066     ;; Call hooks for modifying summary buffer.
4067     (goto-char (point-min))
4068     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4069
4070 (defsubst gnus-general-simplify-subject (subject)
4071   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4072   (setq subject
4073         (cond
4074          ;; Truncate the subject.
4075          (gnus-simplify-subject-functions
4076           (gnus-map-function gnus-simplify-subject-functions subject))
4077          ((numberp gnus-summary-gather-subject-limit)
4078           (setq subject (gnus-simplify-subject-re subject))
4079           (if (> (length subject) gnus-summary-gather-subject-limit)
4080               (substring subject 0 gnus-summary-gather-subject-limit)
4081             subject))
4082          ;; Fuzzily simplify it.
4083          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4084           (gnus-simplify-subject-fuzzy subject))
4085          ;; Just remove the leading "Re:".
4086          (t
4087           (gnus-simplify-subject-re subject))))
4088
4089   (if (and gnus-summary-gather-exclude-subject
4090            (string-match gnus-summary-gather-exclude-subject subject))
4091       nil                         ; This article shouldn't be gathered
4092     subject))
4093
4094 (defun gnus-summary-simplify-subject-query ()
4095   "Query where the respool algorithm would put this article."
4096   (interactive)
4097   (gnus-summary-select-article)
4098   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4099
4100 (defun gnus-gather-threads-by-subject (threads)
4101   "Gather threads by looking at Subject headers."
4102   (if (not gnus-summary-make-false-root)
4103       threads
4104     (let ((hashtb (gnus-make-hashtable 1024))
4105           (prev threads)
4106           (result threads)
4107           subject hthread whole-subject)
4108       (while threads
4109         (setq subject (gnus-general-simplify-subject
4110                        (setq whole-subject (mail-header-subject
4111                                             (caar threads)))))
4112         (when subject
4113           (if (setq hthread (gnus-gethash subject hashtb))
4114               (progn
4115                 ;; We enter a dummy root into the thread, if we
4116                 ;; haven't done that already.
4117                 (unless (stringp (caar hthread))
4118                   (setcar hthread (list whole-subject (car hthread))))
4119                 ;; We add this new gathered thread to this gathered
4120                 ;; thread.
4121                 (setcdr (car hthread)
4122                         (nconc (cdar hthread) (list (car threads))))
4123                 ;; Remove it from the list of threads.
4124                 (setcdr prev (cdr threads))
4125                 (setq threads prev))
4126             ;; Enter this thread into the hash table.
4127             (gnus-sethash subject
4128                           (if gnus-summary-make-false-root-always
4129                               (progn
4130                                 ;; If you want a dummy root above all
4131                                 ;; threads...
4132                                 (setcar threads (list whole-subject
4133                                                       (car threads)))
4134                                 threads)
4135                             threads)
4136                           hashtb)))
4137         (setq prev threads)
4138         (setq threads (cdr threads)))
4139       result)))
4140
4141 (defun gnus-gather-threads-by-references (threads)
4142   "Gather threads by looking at References headers."
4143   (let ((idhashtb (gnus-make-hashtable 1024))
4144         (thhashtb (gnus-make-hashtable 1024))
4145         (prev threads)
4146         (result threads)
4147         ids references id gthread gid entered ref)
4148     (while threads
4149       (when (setq references (mail-header-references (caar threads)))
4150         (setq id (mail-header-id (caar threads))
4151               ids (inline (gnus-split-references references))
4152               entered nil)
4153         (while (setq ref (pop ids))
4154           (setq ids (delete ref ids))
4155           (if (not (setq gid (gnus-gethash ref idhashtb)))
4156               (progn
4157                 (gnus-sethash ref id idhashtb)
4158                 (gnus-sethash id threads thhashtb))
4159             (setq gthread (gnus-gethash gid thhashtb))
4160             (unless entered
4161               ;; We enter a dummy root into the thread, if we
4162               ;; haven't done that already.
4163               (unless (stringp (caar gthread))
4164                 (setcar gthread (list (mail-header-subject (caar gthread))
4165                                       (car gthread))))
4166               ;; We add this new gathered thread to this gathered
4167               ;; thread.
4168               (setcdr (car gthread)
4169                       (nconc (cdar gthread) (list (car threads)))))
4170             ;; Add it into the thread hash table.
4171             (gnus-sethash id gthread thhashtb)
4172             (setq entered t)
4173             ;; Remove it from the list of threads.
4174             (setcdr prev (cdr threads))
4175             (setq threads prev))))
4176       (setq prev threads)
4177       (setq threads (cdr threads)))
4178     result))
4179
4180 (defun gnus-sort-gathered-threads (threads)
4181   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4182   (let ((result threads))
4183     (while threads
4184       (when (stringp (caar threads))
4185         (setcdr (car threads)
4186                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4187       (setq threads (cdr threads)))
4188     result))
4189
4190 (defun gnus-thread-loop-p (root thread)
4191   "Say whether ROOT is in THREAD."
4192   (let ((stack (list thread))
4193         (infloop 0)
4194         th)
4195     (while (setq thread (pop stack))
4196       (setq th (cdr thread))
4197       (while (and th
4198                   (not (eq (caar th) root)))
4199         (pop th))
4200       (if th
4201           ;; We have found a loop.
4202           (let (ref-dep)
4203             (setcdr thread (delq (car th) (cdr thread)))
4204             (if (boundp (setq ref-dep (intern "none"
4205                                               gnus-newsgroup-dependencies)))
4206                 (setcdr (symbol-value ref-dep)
4207                         (nconc (cdr (symbol-value ref-dep))
4208                                (list (car th))))
4209               (set ref-dep (list nil (car th))))
4210             (setq infloop 1
4211                   stack nil))
4212         ;; Push all the subthreads onto the stack.
4213         (push (cdr thread) stack)))
4214     infloop))
4215
4216 (defun gnus-make-threads ()
4217   "Go through the dependency hashtb and find the roots.  Return all threads."
4218   (let (threads)
4219     (while (catch 'infloop
4220              (mapatoms
4221               (lambda (refs)
4222                 ;; Deal with self-referencing References loops.
4223                 (when (and (car (symbol-value refs))
4224                            (not (zerop
4225                                  (apply
4226                                   '+
4227                                   (mapcar
4228                                    (lambda (thread)
4229                                      (gnus-thread-loop-p
4230                                       (car (symbol-value refs)) thread))
4231                                    (cdr (symbol-value refs)))))))
4232                   (setq threads nil)
4233                   (throw 'infloop t))
4234                 (unless (car (symbol-value refs))
4235                   ;; These threads do not refer back to any other
4236                   ;; articles, so they're roots.
4237                   (setq threads (append (cdr (symbol-value refs)) threads))))
4238               gnus-newsgroup-dependencies)))
4239     threads))
4240
4241 ;; Build the thread tree.
4242 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4243   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4244
4245 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4246 if it was already present.
4247
4248 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4249 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4250 Message-IDs will be renamed to a unique Message-ID before being
4251 entered.
4252
4253 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4254   (let* ((id (mail-header-id header))
4255          (id-dep (and id (intern id dependencies)))
4256          parent-id ref ref-dep ref-header replaced)
4257     ;; Enter this `header' in the `dependencies' table.
4258     (cond
4259      ((not id-dep)
4260       (setq header nil))
4261      ;; The first two cases do the normal part: enter a new `header'
4262      ;; in the `dependencies' table.
4263      ((not (boundp id-dep))
4264       (set id-dep (list header)))
4265      ((null (car (symbol-value id-dep)))
4266       (setcar (symbol-value id-dep) header))
4267
4268      ;; From here the `header' was already present in the
4269      ;; `dependencies' table.
4270      (force-new
4271       ;; Overrides an existing entry;
4272       ;; just set the header part of the entry.
4273       (setcar (symbol-value id-dep) header)
4274       (setq replaced t))
4275
4276      ;; Renames the existing `header' to a unique Message-ID.
4277      ((not gnus-summary-ignore-duplicates)
4278       ;; An article with this Message-ID has already been seen.
4279       ;; We rename the Message-ID.
4280       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4281            (list header))
4282       (mail-header-set-id header id))
4283
4284      ;; The last case ignores an existing entry, except it adds any
4285      ;; additional Xrefs (in case the two articles came from different
4286      ;; servers.
4287      ;; Also sets `header' to `nil' meaning that the `dependencies'
4288      ;; table was *not* modified.
4289      (t
4290       (mail-header-set-xref
4291        (car (symbol-value id-dep))
4292        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4293                    "")
4294                (or (mail-header-xref header) "")))
4295       (setq header nil)))
4296
4297     (when (and header (not replaced))
4298       ;; First check that we are not creating a References loop.
4299       (setq parent-id (gnus-parent-id (mail-header-references header)))
4300       (setq ref parent-id)
4301       (while (and ref
4302                   (setq ref-dep (intern-soft ref dependencies))
4303                   (boundp ref-dep)
4304                   (setq ref-header (car (symbol-value ref-dep))))
4305         (if (string= id ref)
4306             ;; Yuk!  This is a reference loop.  Make the article be a
4307             ;; root article.
4308             (progn
4309               (mail-header-set-references (car (symbol-value id-dep)) "none")
4310               (setq ref nil)
4311               (setq parent-id nil))
4312           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4313       (setq ref-dep (intern (or parent-id "none") dependencies))
4314       (if (boundp ref-dep)
4315           (setcdr (symbol-value ref-dep)
4316                   (nconc (cdr (symbol-value ref-dep))
4317                          (list (symbol-value id-dep))))
4318         (set ref-dep (list nil (symbol-value id-dep)))))
4319     header))
4320
4321 (defun gnus-extract-message-id-from-in-reply-to (string)
4322   (if (string-match "<[^>]+>" string)
4323       (substring string (match-beginning 0) (match-end 0))
4324     nil))
4325
4326 (defun gnus-build-sparse-threads ()
4327   (let ((headers gnus-newsgroup-headers)
4328         (mail-parse-charset gnus-newsgroup-charset)
4329         (gnus-summary-ignore-duplicates t)
4330         header references generation relations
4331         subject child end new-child date)
4332     ;; First we create an alist of generations/relations, where
4333     ;; generations is how much we trust the relation, and the relation
4334     ;; is parent/child.
4335     (gnus-message 7 "Making sparse threads...")
4336     (save-excursion
4337       (nnheader-set-temp-buffer " *gnus sparse threads*")
4338       (while (setq header (pop headers))
4339         (when (and (setq references (mail-header-references header))
4340                    (not (string= references "")))
4341           (insert references)
4342           (setq child (mail-header-id header)
4343                 subject (mail-header-subject header)
4344                 date (mail-header-date header)
4345                 generation 0)
4346           (while (search-backward ">" nil t)
4347             (setq end (1+ (point)))
4348             (when (search-backward "<" nil t)
4349               (setq new-child (buffer-substring (point) end))
4350               (push (list (incf generation)
4351                           child (setq child new-child)
4352                           subject date)
4353                     relations)))
4354           (when child
4355             (push (list (1+ generation) child nil subject) relations))
4356           (erase-buffer)))
4357       (kill-buffer (current-buffer)))
4358     ;; Sort over trustworthiness.
4359     (dolist (relation (sort relations 'car-less-than-car))
4360       (when (gnus-dependencies-add-header
4361              (make-full-mail-header
4362               gnus-reffed-article-number
4363               (nth 3 relation) "" (or (nth 4 relation) "")
4364               (nth 1 relation)
4365               (or (nth 2 relation) "") 0 0 "")
4366              gnus-newsgroup-dependencies nil)
4367         (push gnus-reffed-article-number gnus-newsgroup-limit)
4368         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4369         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4370               gnus-newsgroup-reads)
4371         (decf gnus-reffed-article-number)))
4372     (gnus-message 7 "Making sparse threads...done")))
4373
4374 (defun gnus-build-old-threads ()
4375   ;; Look at all the articles that refer back to old articles, and
4376   ;; fetch the headers for the articles that aren't there.  This will
4377   ;; build complete threads - if the roots haven't been expired by the
4378   ;; server, that is.
4379   (let ((mail-parse-charset gnus-newsgroup-charset)
4380         id heads)
4381     (mapatoms
4382      (lambda (refs)
4383        (when (not (car (symbol-value refs)))
4384          (setq heads (cdr (symbol-value refs)))
4385          (while heads
4386            (if (memq (mail-header-number (caar heads))
4387                      gnus-newsgroup-dormant)
4388                (setq heads (cdr heads))
4389              (setq id (symbol-name refs))
4390              (while (and (setq id (gnus-build-get-header id))
4391                          (not (car (gnus-id-to-thread id)))))
4392              (setq heads nil)))))
4393      gnus-newsgroup-dependencies)))
4394
4395 (defsubst gnus-remove-odd-characters (string)
4396   "Translate STRING into something that doesn't contain weird characters."
4397   (mm-subst-char-in-string
4398    ?\r ?\-
4399    (mm-subst-char-in-string ?\n ?\- string t) t))
4400
4401 ;; This function has to be called with point after the article number
4402 ;; on the beginning of the line.
4403 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4404   (let ((eol (point-at-eol))
4405         (buffer (current-buffer))
4406         header references in-reply-to)
4407
4408     ;; overview: [num subject from date id refs chars lines misc]
4409     (unwind-protect
4410         (let (x)
4411           (narrow-to-region (point) eol)
4412           (unless (eobp)
4413             (forward-char))
4414
4415           (setq header
4416                 (make-full-mail-header
4417                  number                 ; number
4418                  (condition-case ()     ; subject
4419                      (gnus-remove-odd-characters
4420                       (funcall gnus-decode-encoded-word-function
4421                                (setq x (nnheader-nov-field))))
4422                    (error x))
4423                  (condition-case ()     ; from
4424                      (gnus-remove-odd-characters
4425                       (funcall gnus-decode-encoded-address-function
4426                                (setq x (nnheader-nov-field))))
4427                    (error x))
4428                  (nnheader-nov-field)   ; date
4429                  (nnheader-nov-read-message-id number)  ; id
4430                  (setq references (nnheader-nov-field)) ; refs
4431                  (nnheader-nov-read-integer) ; chars
4432                  (nnheader-nov-read-integer) ; lines
4433                  (unless (eobp)
4434                    (if (looking-at "Xref: ")
4435                        (goto-char (match-end 0)))
4436                    (nnheader-nov-field)) ; Xref
4437                  (nnheader-nov-parse-extra)))) ; extra
4438
4439       (widen))
4440
4441     (when (and (string= references "")
4442                (setq in-reply-to (mail-header-extra header))
4443                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4444       (mail-header-set-references
4445        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4446
4447     (when gnus-alter-header-function
4448       (funcall gnus-alter-header-function header))
4449     (gnus-dependencies-add-header header dependencies force-new)))
4450
4451 (defun gnus-build-get-header (id)
4452   "Look through the buffer of NOV lines and find the header to ID.
4453 Enter this line into the dependencies hash table, and return
4454 the id of the parent article (if any)."
4455   (let ((deps gnus-newsgroup-dependencies)
4456         found header)
4457     (prog1
4458         (with-current-buffer nntp-server-buffer
4459           (let ((case-fold-search nil))
4460             (goto-char (point-min))
4461             (while (and (not found)
4462                         (search-forward id nil t))
4463               (beginning-of-line)
4464               (setq found (looking-at
4465                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4466                                    (regexp-quote id))))
4467               (or found (beginning-of-line 2)))
4468             (when found
4469               (beginning-of-line)
4470               (and
4471                (setq header (gnus-nov-parse-line
4472                              (read (current-buffer)) deps))
4473                (gnus-parent-id (mail-header-references header))))))
4474       (when header
4475         (let ((number (mail-header-number header)))
4476           (push number gnus-newsgroup-limit)
4477           (push header gnus-newsgroup-headers)
4478           (if (memq number gnus-newsgroup-unselected)
4479               (progn
4480                 (setq gnus-newsgroup-unreads
4481                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4482                                                number))
4483                 (setq gnus-newsgroup-unselected
4484                       (delq number gnus-newsgroup-unselected)))
4485             (push number gnus-newsgroup-ancient)))))))
4486
4487 (defun gnus-build-all-threads ()
4488   "Read all the headers."
4489   (let ((gnus-summary-ignore-duplicates t)
4490         (mail-parse-charset gnus-newsgroup-charset)
4491         (dependencies gnus-newsgroup-dependencies)
4492         header article)
4493     (with-current-buffer nntp-server-buffer
4494       (let ((case-fold-search nil))
4495         (goto-char (point-min))
4496         (while (not (eobp))
4497           (ignore-errors
4498             (setq article (read (current-buffer))
4499                   header (gnus-nov-parse-line article dependencies)))
4500           (when header
4501             (with-current-buffer gnus-summary-buffer
4502               (push header gnus-newsgroup-headers)
4503               (if (memq (setq article (mail-header-number header))
4504                         gnus-newsgroup-unselected)
4505                   (progn
4506                     (setq gnus-newsgroup-unreads
4507                           (gnus-add-to-sorted-list
4508                            gnus-newsgroup-unreads article))
4509                     (setq gnus-newsgroup-unselected
4510                           (delq article gnus-newsgroup-unselected)))
4511                 (push article gnus-newsgroup-ancient)))
4512             (forward-line 1)))))))
4513
4514 (defun gnus-summary-update-article-line (article header)
4515   "Update the line for ARTICLE using HEADER."
4516   (let* ((id (mail-header-id header))
4517          (thread (gnus-id-to-thread id)))
4518     (unless thread
4519       (error "Article in no thread"))
4520     ;; Update the thread.
4521     (setcar thread header)
4522     (gnus-summary-goto-subject article)
4523     (let* ((datal (gnus-data-find-list article))
4524            (data (car datal))
4525            (inhibit-read-only t)
4526            (level (gnus-summary-thread-level)))
4527       (gnus-delete-line)
4528       (let ((inserted (- (point)
4529                          (progn
4530                            (gnus-summary-insert-line
4531                             header level nil
4532                             (memq article gnus-newsgroup-undownloaded)
4533                             (gnus-article-mark article)
4534                             (memq article gnus-newsgroup-replied)
4535                             (memq article gnus-newsgroup-expirable)
4536                             ;; Only insert the Subject string when it's different
4537                             ;; from the previous Subject string.
4538                             (if (and
4539                                  gnus-show-threads
4540                                  (gnus-subject-equal
4541                                   (condition-case ()
4542                                       (mail-header-subject
4543                                        (gnus-data-header
4544                                         (cadr
4545                                          (gnus-data-find-list
4546                                           article
4547                                           (gnus-data-list t)))))
4548                                     ;; Error on the side of excessive subjects.
4549                                     (error ""))
4550                                   (mail-header-subject header)))
4551                                 ""
4552                               (mail-header-subject header))
4553                             nil (cdr (assq article gnus-newsgroup-scored))
4554                             (memq article gnus-newsgroup-processable))
4555                            (point)))))
4556         (when (cdr datal)
4557           (gnus-data-update-list
4558            (cdr datal)
4559            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4560
4561 (defun gnus-summary-update-article (article &optional iheader)
4562   "Update ARTICLE in the summary buffer."
4563   (set-buffer gnus-summary-buffer)
4564   (let* ((header (gnus-summary-article-header article))
4565          (id (mail-header-id header))
4566          (data (gnus-data-find article))
4567          (thread (gnus-id-to-thread id))
4568          (references (mail-header-references header))
4569          (parent
4570           (gnus-id-to-thread
4571            (or (gnus-parent-id
4572                 (when (and references
4573                            (not (equal "" references)))
4574                   references))
4575                "none")))
4576          (inhibit-read-only t)
4577          (old (car thread)))
4578     (when thread
4579       (unless iheader
4580         (setcar thread nil)
4581         (when parent
4582           (delq thread parent)))
4583       (if (gnus-summary-insert-subject id header)
4584           ;; Set the (possibly) new article number in the data structure.
4585           (gnus-data-set-number data (gnus-id-to-article id))
4586         (setcar thread old)
4587         nil))))
4588
4589 (defun gnus-rebuild-thread (id &optional line)
4590   "Rebuild the thread containing ID.
4591 If LINE, insert the rebuilt thread starting on line LINE."
4592   (let ((inhibit-read-only t)
4593         old-pos current thread data)
4594     (if (not gnus-show-threads)
4595         (setq thread (list (car (gnus-id-to-thread id))))
4596       ;; Get the thread this article is part of.
4597       (setq thread (gnus-remove-thread id)))
4598     (setq old-pos (point-at-bol))
4599     (setq current (save-excursion
4600                     (and (re-search-backward "[\r\n]" nil t)
4601                          (gnus-summary-article-number))))
4602     ;; If this is a gathered thread, we have to go some re-gathering.
4603     (when (stringp (car thread))
4604       (let ((subject (car thread))
4605             roots thr)
4606         (setq thread (cdr thread))
4607         (while thread
4608           (unless (memq (setq thr (gnus-id-to-thread
4609                                    (gnus-root-id
4610                                     (mail-header-id (caar thread)))))
4611                         roots)
4612             (push thr roots))
4613           (setq thread (cdr thread)))
4614         ;; We now have all (unique) roots.
4615         (if (= (length roots) 1)
4616             ;; All the loose roots are now one solid root.
4617             (setq thread (car roots))
4618           (setq thread (cons subject (gnus-sort-threads roots))))))
4619     (let (threads)
4620       ;; We then insert this thread into the summary buffer.
4621       (when line
4622         (goto-char (point-min))
4623         (forward-line (1- line)))
4624       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4625         (if gnus-show-threads
4626             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4627           (gnus-summary-prepare-unthreaded thread))
4628         (setq data (nreverse gnus-newsgroup-data))
4629         (setq threads gnus-newsgroup-threads))
4630       ;; We splice the new data into the data structure.
4631       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4632       ;;!!! then we want to insert at the beginning of the buffer.
4633       ;;!!! That happens to be true with Gnus now, but that may
4634       ;;!!! change in the future.  Perhaps.
4635       (gnus-data-enter-list
4636        (if line nil current) data (- (point) old-pos))
4637       (setq gnus-newsgroup-threads
4638             (nconc threads gnus-newsgroup-threads))
4639       (gnus-data-compute-positions))))
4640
4641 (defun gnus-number-to-header (number)
4642   "Return the header for article NUMBER."
4643   (let ((headers gnus-newsgroup-headers))
4644     (while (and headers
4645                 (not (= number (mail-header-number (car headers)))))
4646       (pop headers))
4647     (when headers
4648       (car headers))))
4649
4650 (defun gnus-parent-headers (in-headers &optional generation)
4651   "Return the headers of the GENERATIONeth parent of HEADERS."
4652   (unless generation
4653     (setq generation 1))
4654   (let ((parent t)
4655         (headers in-headers)
4656         references)
4657     (while (and parent
4658                 (not (zerop generation))
4659                 (setq references (mail-header-references headers)))
4660       (setq headers (if (and references
4661                              (setq parent (gnus-parent-id references)))
4662                         (car (gnus-id-to-thread parent))
4663                       nil))
4664       (decf generation))
4665     (and (not (eq headers in-headers))
4666          headers)))
4667
4668 (defun gnus-id-to-thread (id)
4669   "Return the (sub-)thread where ID appears."
4670   (gnus-gethash id gnus-newsgroup-dependencies))
4671
4672 (defun gnus-id-to-article (id)
4673   "Return the article number of ID."
4674   (let ((thread (gnus-id-to-thread id)))
4675     (when (and thread
4676                (car thread))
4677       (mail-header-number (car thread)))))
4678
4679 (defun gnus-id-to-header (id)
4680   "Return the article headers of ID."
4681   (car (gnus-id-to-thread id)))
4682
4683 (defun gnus-article-displayed-root-p (article)
4684   "Say whether ARTICLE is a root(ish) article."
4685   (let ((level (gnus-summary-thread-level article))
4686         (refs (mail-header-references  (gnus-summary-article-header article)))
4687         particle)
4688     (cond
4689      ((null level) nil)
4690      ((zerop level) t)
4691      ((null refs) t)
4692      ((null (gnus-parent-id refs)) t)
4693      ((and (= 1 level)
4694            (null (setq particle (gnus-id-to-article
4695                                  (gnus-parent-id refs))))
4696            (null (gnus-summary-thread-level particle)))))))
4697
4698 (defun gnus-root-id (id)
4699   "Return the id of the root of the thread where ID appears."
4700   (let (last-id prev)
4701     (while (and id (setq prev (car (gnus-id-to-thread id))))
4702       (setq last-id id
4703             id (gnus-parent-id (mail-header-references prev))))
4704     last-id))
4705
4706 (defun gnus-articles-in-thread (thread)
4707   "Return the list of articles in THREAD."
4708   (cons (mail-header-number (car thread))
4709         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4710
4711 (defun gnus-remove-thread (id &optional dont-remove)
4712   "Remove the thread that has ID in it."
4713   (let (headers thread last-id)
4714     ;; First go up in this thread until we find the root.
4715     (setq last-id (gnus-root-id id)
4716           headers (message-flatten-list (gnus-id-to-thread last-id)))
4717     ;; We have now found the real root of this thread.  It might have
4718     ;; been gathered into some loose thread, so we have to search
4719     ;; through the threads to find the thread we wanted.
4720     (let ((threads gnus-newsgroup-threads)
4721           sub)
4722       (while threads
4723         (setq sub (car threads))
4724         (if (stringp (car sub))
4725             ;; This is a gathered thread, so we look at the roots
4726             ;; below it to find whether this article is in this
4727             ;; gathered root.
4728             (progn
4729               (setq sub (cdr sub))
4730               (while sub
4731                 (when (member (caar sub) headers)
4732                   (setq thread (car threads)
4733                         threads nil
4734                         sub nil))
4735                 (setq sub (cdr sub))))
4736           ;; It's an ordinary thread, so we check it.
4737           (when (eq (car sub) (car headers))
4738             (setq thread sub
4739                   threads nil)))
4740         (setq threads (cdr threads)))
4741       ;; If this article is in no thread, then it's a root.
4742       (if thread
4743           (unless dont-remove
4744             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4745         (setq thread (gnus-id-to-thread last-id)))
4746       (when thread
4747         (prog1
4748             thread                      ; We return this thread.
4749           (unless dont-remove
4750             (if (stringp (car thread))
4751                 (progn
4752                   ;; If we use dummy roots, then we have to remove the
4753                   ;; dummy root as well.
4754                   (when (eq gnus-summary-make-false-root 'dummy)
4755                     ;; We go to the dummy root by going to
4756                     ;; the first sub-"thread", and then one line up.
4757                     (gnus-summary-goto-article
4758                      (mail-header-number (caadr thread)))
4759                     (forward-line -1)
4760                     (gnus-delete-line)
4761                     (gnus-data-compute-positions))
4762                   (setq thread (cdr thread))
4763                   (while thread
4764                     (gnus-remove-thread-1 (car thread))
4765                     (setq thread (cdr thread))))
4766               (gnus-remove-thread-1 thread))))))))
4767
4768 (defun gnus-remove-thread-1 (thread)
4769   "Remove the thread THREAD recursively."
4770   (let ((number (mail-header-number (pop thread)))
4771         d)
4772     (setq thread (reverse thread))
4773     (while thread
4774       (gnus-remove-thread-1 (pop thread)))
4775     (when (setq d (gnus-data-find number))
4776       (goto-char (gnus-data-pos d))
4777       (gnus-summary-show-thread)
4778       (gnus-data-remove
4779        number
4780        (- (point-at-bol)
4781           (prog1
4782               (1+ (point-at-eol))
4783             (gnus-delete-line)))))))
4784
4785 (defun gnus-sort-threads-recursive (threads func)
4786   (sort (mapcar (lambda (thread)
4787                   (cons (car thread)
4788                         (and (cdr thread)
4789                              (gnus-sort-threads-recursive (cdr thread) func))))
4790                 threads) func))
4791
4792 (defun gnus-sort-threads-loop (threads func)
4793   (let* ((superthread (cons nil threads))
4794          (stack (list (cons superthread threads)))
4795          remaining-threads thread)
4796     (while stack
4797       (setq remaining-threads (cdr (car stack)))
4798       (if remaining-threads
4799           (progn (setq thread (car remaining-threads))
4800                  (setcdr (car stack) (cdr remaining-threads))
4801                  (if (cdr thread)
4802                      (push (cons thread (cdr thread)) stack)))
4803         (setq thread (caar stack))
4804         (setcdr thread (sort (cdr thread) func))
4805         (pop stack)))
4806     (cdr superthread)))
4807
4808 (defun gnus-sort-threads (threads)
4809   "Sort THREADS."
4810   (if (not gnus-thread-sort-functions)
4811       threads
4812     (gnus-message 8 "Sorting threads...")
4813     (prog1
4814         (condition-case nil
4815             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4816               (gnus-sort-threads-recursive
4817                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4818           ;; Even after binding max-lisp-eval-depth, the recursive
4819           ;; sorter might fail for very long threads.  In that case,
4820           ;; try using a (less well-tested) non-recursive sorter.
4821           (error (gnus-message 9 "Sorting threads with loop...")
4822                  (gnus-sort-threads-loop
4823                   threads (gnus-make-sort-function
4824                            gnus-thread-sort-functions))))
4825       (gnus-message 8 "Sorting threads...done"))))
4826
4827 (defun gnus-sort-articles (articles)
4828   "Sort ARTICLES."
4829   (when gnus-article-sort-functions
4830     (gnus-message 7 "Sorting articles...")
4831     (prog1
4832         (setq gnus-newsgroup-headers
4833               (sort articles (gnus-make-sort-function
4834                               gnus-article-sort-functions)))
4835       (gnus-message 7 "Sorting articles...done"))))
4836
4837 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4838 (defmacro gnus-thread-header (thread)
4839   "Return header of first article in THREAD.
4840 Note that THREAD must never, ever be anything else than a variable -
4841 using some other form will lead to serious barfage."
4842   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4843   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4844   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4845         (vector thread) 2))
4846
4847 (defsubst gnus-article-sort-by-number (h1 h2)
4848   "Sort articles by article number."
4849   (< (mail-header-number h1)
4850      (mail-header-number h2)))
4851
4852 (defun gnus-thread-sort-by-number (h1 h2)
4853   "Sort threads by root article number."
4854   (gnus-article-sort-by-number
4855    (gnus-thread-header h1) (gnus-thread-header h2)))
4856
4857 (defsubst gnus-article-sort-by-random (h1 h2)
4858   "Sort articles randomly."
4859   (zerop (random 2)))
4860
4861 (defun gnus-thread-sort-by-random (h1 h2)
4862   "Sort threads randomly."
4863   (gnus-article-sort-by-random
4864    (gnus-thread-header h1) (gnus-thread-header h2)))
4865
4866 (defsubst gnus-article-sort-by-lines (h1 h2)
4867   "Sort articles by article Lines header."
4868   (< (mail-header-lines h1)
4869      (mail-header-lines h2)))
4870
4871 (defun gnus-thread-sort-by-lines (h1 h2)
4872   "Sort threads by root article Lines header."
4873   (gnus-article-sort-by-lines
4874    (gnus-thread-header h1) (gnus-thread-header h2)))
4875
4876 (defsubst gnus-article-sort-by-chars (h1 h2)
4877   "Sort articles by octet length."
4878   (< (mail-header-chars h1)
4879      (mail-header-chars h2)))
4880
4881 (defun gnus-thread-sort-by-chars (h1 h2)
4882   "Sort threads by root article octet length."
4883   (gnus-article-sort-by-chars
4884    (gnus-thread-header h1) (gnus-thread-header h2)))
4885
4886 (defsubst gnus-article-sort-by-author (h1 h2)
4887   "Sort articles by root author."
4888   (gnus-string<
4889    (let ((extract (funcall
4890                    gnus-extract-address-components
4891                    (mail-header-from h1))))
4892      (or (car extract) (cadr extract) ""))
4893    (let ((extract (funcall
4894                    gnus-extract-address-components
4895                    (mail-header-from h2))))
4896      (or (car extract) (cadr extract) ""))))
4897
4898 (defun gnus-thread-sort-by-author (h1 h2)
4899   "Sort threads by root author."
4900   (gnus-article-sort-by-author
4901    (gnus-thread-header h1)  (gnus-thread-header h2)))
4902
4903 (defsubst gnus-article-sort-by-recipient (h1 h2)
4904   "Sort articles by recipient."
4905   (gnus-string<
4906    (let ((extract (funcall
4907                    gnus-extract-address-components
4908                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4909      (or (car extract) (cadr extract)))
4910    (let ((extract (funcall
4911                    gnus-extract-address-components
4912                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4913      (or (car extract) (cadr extract)))))
4914
4915 (defun gnus-thread-sort-by-recipient (h1 h2)
4916   "Sort threads by root recipient."
4917   (gnus-article-sort-by-recipient
4918    (gnus-thread-header h1) (gnus-thread-header h2)))
4919
4920 (defsubst gnus-article-sort-by-subject (h1 h2)
4921   "Sort articles by root subject."
4922   (gnus-string<
4923    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4924    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4925
4926 (defun gnus-thread-sort-by-subject (h1 h2)
4927   "Sort threads by root subject."
4928   (gnus-article-sort-by-subject
4929    (gnus-thread-header h1) (gnus-thread-header h2)))
4930
4931 (defsubst gnus-article-sort-by-date (h1 h2)
4932   "Sort articles by root article date."
4933   (time-less-p
4934    (gnus-date-get-time (mail-header-date h1))
4935    (gnus-date-get-time (mail-header-date h2))))
4936
4937 (defun gnus-thread-sort-by-date (h1 h2)
4938   "Sort threads by root article date."
4939   (gnus-article-sort-by-date
4940    (gnus-thread-header h1) (gnus-thread-header h2)))
4941
4942 (defsubst gnus-article-sort-by-score (h1 h2)
4943   "Sort articles by root article score.
4944 Unscored articles will be counted as having a score of zero."
4945   (> (or (cdr (assq (mail-header-number h1)
4946                     gnus-newsgroup-scored))
4947          gnus-summary-default-score 0)
4948      (or (cdr (assq (mail-header-number h2)
4949                     gnus-newsgroup-scored))
4950          gnus-summary-default-score 0)))
4951
4952 (defun gnus-thread-sort-by-score (h1 h2)
4953   "Sort threads by root article score."
4954   (gnus-article-sort-by-score
4955    (gnus-thread-header h1) (gnus-thread-header h2)))
4956
4957 (defun gnus-thread-sort-by-total-score (h1 h2)
4958   "Sort threads by the sum of all scores in the thread.
4959 Unscored articles will be counted as having a score of zero."
4960   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4961
4962 (defun gnus-thread-total-score (thread)
4963   ;; This function find the total score of THREAD.
4964   (cond
4965    ((null thread)
4966     0)
4967    ((consp thread)
4968     (if (stringp (car thread))
4969         (apply gnus-thread-score-function 0
4970                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4971       (gnus-thread-total-score-1 thread)))
4972    (t
4973     (gnus-thread-total-score-1 (list thread)))))
4974
4975 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4976   "Sort threads such that the thread with the most recently arrived article comes first."
4977   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4978
4979 (defun gnus-thread-highest-number (thread)
4980   "Return the highest article number in THREAD."
4981   (apply 'max (mapcar (lambda (header)
4982                         (mail-header-number header))
4983                       (message-flatten-list thread))))
4984
4985 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4986   "Sort threads such that the thread with the most recently dated article comes first."
4987   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4988
4989 ; Since this is called not only to sort the top-level threads, but
4990 ; also in recursive sorts to order the articles within a thread, each
4991 ; article will be processed many times.  Thus it speeds things up
4992 ; quite a bit to use gnus-date-get-time, which caches the time value.
4993 (defun gnus-thread-latest-date (thread)
4994   "Return the highest article date in THREAD."
4995   (apply 'max
4996          (mapcar (lambda (header) (gnus-float-time
4997                                    (gnus-date-get-time
4998                                     (mail-header-date header))))
4999                  (message-flatten-list thread))))
5000
5001 (defun gnus-thread-total-score-1 (root)
5002   ;; This function find the total score of the thread below ROOT.
5003   (setq root (car root))
5004   (apply gnus-thread-score-function
5005          (or (append
5006               (mapcar 'gnus-thread-total-score
5007                       (cdr (gnus-id-to-thread (mail-header-id root))))
5008               (when (> (mail-header-number root) 0)
5009                 (list (or (cdr (assq (mail-header-number root)
5010                                      gnus-newsgroup-scored))
5011                           gnus-summary-default-score 0))))
5012              (list gnus-summary-default-score)
5013              '(0))))
5014
5015 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5016 (defvar gnus-tmp-prev-subject nil)
5017 (defvar gnus-tmp-false-parent nil)
5018 (defvar gnus-tmp-root-expunged nil)
5019 (defvar gnus-tmp-dummy-line nil)
5020
5021 (defun gnus-extra-header (type &optional header)
5022   "Return the extra header of TYPE."
5023   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5024       ""))
5025
5026 (defvar gnus-tmp-thread-tree-header-string "")
5027
5028 (defcustom gnus-sum-thread-tree-root "> "
5029   "With %B spec, used for the root of a thread.
5030 If nil, use subject instead."
5031   :version "22.1"
5032   :type '(radio (const :format "%v  " nil) string)
5033   :group 'gnus-thread)
5034
5035 (defcustom gnus-sum-thread-tree-false-root "> "
5036   "With %B spec, used for a false root of a thread.
5037 If nil, use subject instead."
5038   :version "22.1"
5039   :type '(radio (const :format "%v  " nil) string)
5040   :group 'gnus-thread)
5041
5042 (defcustom gnus-sum-thread-tree-single-indent ""
5043   "With %B spec, used for a thread with just one message.
5044 If nil, use subject instead."
5045   :version "22.1"
5046   :type '(radio (const :format "%v  " nil) string)
5047   :group 'gnus-thread)
5048
5049 (defcustom gnus-sum-thread-tree-vertical "| "
5050   "With %B spec, used for drawing a vertical line."
5051   :version "22.1"
5052   :type 'string
5053   :group 'gnus-thread)
5054
5055 (defcustom gnus-sum-thread-tree-indent "  "
5056   "With %B spec, used for indenting."
5057   :version "22.1"
5058   :type 'string
5059   :group 'gnus-thread)
5060
5061 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5062   "With %B spec, used for a leaf with brothers."
5063   :version "22.1"
5064   :type 'string
5065   :group 'gnus-thread)
5066
5067 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5068   "With %B spec, used for a leaf without brothers."
5069   :version "22.1"
5070   :type 'string
5071   :group 'gnus-thread)
5072
5073 (defcustom gnus-summary-display-while-building nil
5074   "If non-nil, show and update the summary buffer as it's being built.
5075 If the value is t, update the buffer after every line is inserted.  If
5076 the value is an integer (N), update the display every N lines."
5077   :version "22.1"
5078   :group 'gnus-thread
5079   :type '(choice (const :tag "off" nil)
5080                  number
5081                  (const :tag "frequently" t)))
5082
5083 (defun gnus-summary-prepare-threads (threads)
5084   "Prepare summary buffer from THREADS and indentation LEVEL.
5085 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5086 or a straight list of headers."
5087   (gnus-message 7 "Generating summary...")
5088
5089   (setq gnus-newsgroup-threads threads)
5090   (beginning-of-line)
5091
5092   (let ((gnus-tmp-level 0)
5093         (default-score (or gnus-summary-default-score 0))
5094         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5095         (building-line-count gnus-summary-display-while-building)
5096         (building-count (integerp gnus-summary-display-while-building))
5097         thread number subject stack state gnus-tmp-gathered beg-match
5098         new-roots gnus-tmp-new-adopts thread-end simp-subject
5099         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5100         gnus-tmp-replied gnus-tmp-subject-or-nil
5101         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5102         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5103         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5104         tree-stack)
5105
5106     (setq gnus-tmp-prev-subject nil
5107           gnus-tmp-thread-tree-header-string "")
5108
5109     (if (vectorp (car threads))
5110         ;; If this is a straight (sic) list of headers, then a
5111         ;; threaded summary display isn't required, so we just create
5112         ;; an unthreaded one.
5113         (gnus-summary-prepare-unthreaded threads)
5114
5115       ;; Do the threaded display.
5116
5117       (if gnus-summary-display-while-building
5118           (switch-to-buffer (buffer-name)))
5119       (while (or threads stack gnus-tmp-new-adopts new-roots)
5120
5121         (if (and (= gnus-tmp-level 0)
5122                  (or (not stack)
5123                      (= (caar stack) 0))
5124                  (not gnus-tmp-false-parent)
5125                  (or gnus-tmp-new-adopts new-roots))
5126             (if gnus-tmp-new-adopts
5127                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5128                       thread (list (car gnus-tmp-new-adopts))
5129                       gnus-tmp-header (caar thread)
5130                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5131               (when new-roots
5132                 (setq thread (list (car new-roots))
5133                       gnus-tmp-header (caar thread)
5134                       new-roots (cdr new-roots))))
5135
5136           (if threads
5137               ;; If there are some threads, we do them before the
5138               ;; threads on the stack.
5139               (setq thread threads
5140                     gnus-tmp-header (caar thread))
5141             ;; There were no current threads, so we pop something off
5142             ;; the stack.
5143             (setq state (car stack)
5144                   gnus-tmp-level (car state)
5145                   tree-stack (cadr state)
5146                   thread (caddr state)
5147                   stack (cdr stack)
5148                   gnus-tmp-header (caar thread))))
5149
5150         (setq gnus-tmp-false-parent nil)
5151         (setq gnus-tmp-root-expunged nil)
5152         (setq thread-end nil)
5153
5154         (if (stringp gnus-tmp-header)
5155             ;; The header is a dummy root.
5156             (cond
5157              ((eq gnus-summary-make-false-root 'adopt)
5158               ;; We let the first article adopt the rest.
5159               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5160                                                (cddar thread)))
5161               (setq gnus-tmp-gathered
5162                     (nconc (mapcar
5163                             (lambda (h) (mail-header-number (car h)))
5164                             (cddar thread))
5165                            gnus-tmp-gathered))
5166               (setq thread (cons (list (caar thread)
5167                                        (cadar thread))
5168                                  (cdr thread)))
5169               (setq gnus-tmp-level -1
5170                     gnus-tmp-false-parent t))
5171              ((eq gnus-summary-make-false-root 'empty)
5172               ;; We print adopted articles with empty subject fields.
5173               (setq gnus-tmp-gathered
5174                     (nconc (mapcar
5175                             (lambda (h) (mail-header-number (car h)))
5176                             (cddar thread))
5177                            gnus-tmp-gathered))
5178               (setq gnus-tmp-level -1))
5179              ((eq gnus-summary-make-false-root 'dummy)
5180               ;; We remember that we probably want to output a dummy
5181               ;; root.
5182               (setq gnus-tmp-dummy-line gnus-tmp-header)
5183               (setq gnus-tmp-prev-subject gnus-tmp-header))
5184              (t
5185               ;; We do not make a root for the gathered
5186               ;; sub-threads at all.
5187               (setq gnus-tmp-level -1)))
5188
5189           (setq number (mail-header-number gnus-tmp-header)
5190                 subject (mail-header-subject gnus-tmp-header)
5191                 simp-subject (gnus-simplify-subject-fully subject))
5192
5193           (cond
5194            ;; If the thread has changed subject, we might want to make
5195            ;; this subthread into a root.
5196            ((and (null gnus-thread-ignore-subject)
5197                  (not (zerop gnus-tmp-level))
5198                  gnus-tmp-prev-subject
5199                  (not (string= gnus-tmp-prev-subject simp-subject)))
5200             (setq new-roots (nconc new-roots (list (car thread)))
5201                   thread-end t
5202                   gnus-tmp-header nil))
5203            ;; If the article lies outside the current limit,
5204            ;; then we do not display it.
5205            ((not (memq number gnus-newsgroup-limit))
5206             (setq gnus-tmp-gathered
5207                   (nconc (mapcar
5208                           (lambda (h) (mail-header-number (car h)))
5209                           (cdar thread))
5210                          gnus-tmp-gathered))
5211             (setq gnus-tmp-new-adopts (if (cdar thread)
5212                                           (append gnus-tmp-new-adopts
5213                                                   (cdar thread))
5214                                         gnus-tmp-new-adopts)
5215                   thread-end t
5216                   gnus-tmp-header nil)
5217             (when (zerop gnus-tmp-level)
5218               (setq gnus-tmp-root-expunged t)))
5219            ;; Perhaps this article is to be marked as read?
5220            ((and gnus-summary-mark-below
5221                  (< (or (cdr (assq number gnus-newsgroup-scored))
5222                         default-score)
5223                     gnus-summary-mark-below)
5224                  ;; Don't touch sparse articles.
5225                  (not (gnus-summary-article-sparse-p number))
5226                  (not (gnus-summary-article-ancient-p number)))
5227             (setq gnus-newsgroup-unreads
5228                   (delq number gnus-newsgroup-unreads))
5229             (if gnus-newsgroup-auto-expire
5230                 (setq gnus-newsgroup-expirable
5231                       (gnus-add-to-sorted-list
5232                        gnus-newsgroup-expirable number))
5233               (push (cons number gnus-low-score-mark)
5234                     gnus-newsgroup-reads))))
5235
5236           (when gnus-tmp-header
5237             ;; We may have an old dummy line to output before this
5238             ;; article.
5239             (when (and gnus-tmp-dummy-line
5240                        (gnus-subject-equal
5241                         gnus-tmp-dummy-line
5242                         (mail-header-subject gnus-tmp-header)))
5243               (gnus-summary-insert-dummy-line
5244                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5245               (setq gnus-tmp-dummy-line nil))
5246
5247             ;; Compute the mark.
5248             (setq gnus-tmp-unread (gnus-article-mark number))
5249
5250             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5251                                   gnus-tmp-header gnus-tmp-level)
5252                   gnus-newsgroup-data)
5253
5254             ;; Actually insert the line.
5255             (setq
5256              gnus-tmp-subject-or-nil
5257              (cond
5258               ((and gnus-thread-ignore-subject
5259                     gnus-tmp-prev-subject
5260                     (not (string= gnus-tmp-prev-subject simp-subject)))
5261                subject)
5262               ((zerop gnus-tmp-level)
5263                (if (and (eq gnus-summary-make-false-root 'empty)
5264                         (memq number gnus-tmp-gathered)
5265                         gnus-tmp-prev-subject
5266                         (string= gnus-tmp-prev-subject simp-subject))
5267                    gnus-summary-same-subject
5268                  subject))
5269               (t gnus-summary-same-subject)))
5270             (if (and (eq gnus-summary-make-false-root 'adopt)
5271                      (= gnus-tmp-level 1)
5272                      (memq number gnus-tmp-gathered))
5273                 (setq gnus-tmp-opening-bracket ?\<
5274                       gnus-tmp-closing-bracket ?\>)
5275               (setq gnus-tmp-opening-bracket ?\[
5276                     gnus-tmp-closing-bracket ?\]))
5277             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5278                 (gnus-make-thread-indent-array
5279                  (max (* 2 (length gnus-thread-indent-array))
5280                       gnus-tmp-level)))
5281             (setq
5282              gnus-tmp-indentation
5283              (aref gnus-thread-indent-array gnus-tmp-level)
5284              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5285              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5286                                 gnus-summary-default-score 0)
5287              gnus-tmp-score-char
5288              (if (or (null gnus-summary-default-score)
5289                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5290                          gnus-summary-zcore-fuzz))
5291                  ?                      ;Whitespace
5292                (if (< gnus-tmp-score gnus-summary-default-score)
5293                    gnus-score-below-mark gnus-score-over-mark))
5294              gnus-tmp-replied
5295              (cond ((memq number gnus-newsgroup-processable)
5296                     gnus-process-mark)
5297                    ((memq number gnus-newsgroup-cached)
5298                     gnus-cached-mark)
5299                    ((memq number gnus-newsgroup-replied)
5300                     gnus-replied-mark)
5301                    ((memq number gnus-newsgroup-forwarded)
5302                     gnus-forwarded-mark)
5303                    ((memq number gnus-newsgroup-saved)
5304                     gnus-saved-mark)
5305                    ((memq number gnus-newsgroup-recent)
5306                     gnus-recent-mark)
5307                    ((memq number gnus-newsgroup-unseen)
5308                     gnus-unseen-mark)
5309                    (t gnus-no-mark))
5310              gnus-tmp-downloaded
5311              (cond ((memq number gnus-newsgroup-undownloaded)
5312                     gnus-undownloaded-mark)
5313                    (gnus-newsgroup-agentized
5314                     gnus-downloaded-mark)
5315                    (t
5316                     gnus-no-mark))
5317              gnus-tmp-from (mail-header-from gnus-tmp-header)
5318              gnus-tmp-name
5319              (cond
5320               ((string-match "<[^>]+> *$" gnus-tmp-from)
5321                (setq beg-match (match-beginning 0))
5322                (or (and (string-match "^\".+\"" gnus-tmp-from)
5323                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5324                    (substring gnus-tmp-from 0 beg-match)))
5325               ((string-match "(.+)" gnus-tmp-from)
5326                (substring gnus-tmp-from
5327                           (1+ (match-beginning 0)) (1- (match-end 0))))
5328               (t gnus-tmp-from))
5329
5330              ;; Do the %B string
5331              gnus-tmp-thread-tree-header-string
5332              (cond
5333               ((not gnus-show-threads) "")
5334               ((zerop gnus-tmp-level)
5335                (cond ((cdar thread)
5336                       (or gnus-sum-thread-tree-root subject))
5337                      (gnus-tmp-new-adopts
5338                       (or gnus-sum-thread-tree-false-root subject))
5339                      (t
5340                       (or gnus-sum-thread-tree-single-indent subject))))
5341               (t
5342                (concat (apply 'concat
5343                               (mapcar (lambda (item)
5344                                         (if (= item 1)
5345                                             gnus-sum-thread-tree-vertical
5346                                           gnus-sum-thread-tree-indent))
5347                                       (cdr (reverse tree-stack))))
5348                        (if (nth 1 thread)
5349                            gnus-sum-thread-tree-leaf-with-other
5350                          gnus-sum-thread-tree-single-leaf)))))
5351             (when (string= gnus-tmp-name "")
5352               (setq gnus-tmp-name gnus-tmp-from))
5353             (unless (numberp gnus-tmp-lines)
5354               (setq gnus-tmp-lines -1))
5355             (if (= gnus-tmp-lines -1)
5356                 (setq gnus-tmp-lines "?")
5357               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5358               (gnus-put-text-property
5359                (point)
5360                (progn (eval gnus-summary-line-format-spec) (point))
5361                'gnus-number number)
5362               (when gnus-visual-p
5363                 (forward-line -1)
5364                 (gnus-run-hooks 'gnus-summary-update-hook)
5365                 (forward-line 1))
5366
5367               (setq gnus-tmp-prev-subject simp-subject)))
5368
5369         (when (nth 1 thread)
5370           (push (list (max 0 gnus-tmp-level)
5371                       (copy-sequence tree-stack)
5372                       (nthcdr 1 thread))
5373                 stack))
5374         (push (if (nth 1 thread) 1 0) tree-stack)
5375         (incf gnus-tmp-level)
5376         (setq threads (if thread-end nil (cdar thread)))
5377         (if gnus-summary-display-while-building
5378             (if building-count
5379                 (progn
5380                   ;; use a set frequency
5381                   (setq building-line-count (1- building-line-count))
5382                   (when (= building-line-count 0)
5383                     (sit-for 0)
5384                     (setq building-line-count
5385                           gnus-summary-display-while-building)))
5386               ;; always
5387               (sit-for 0)))
5388         (unless threads
5389           (setq gnus-tmp-level 0)))))
5390   (gnus-message 7 "Generating summary...done"))
5391
5392 (defun gnus-summary-prepare-unthreaded (headers)
5393   "Generate an unthreaded summary buffer based on HEADERS."
5394   (let (header number mark)
5395
5396     (beginning-of-line)
5397
5398     (while headers
5399       ;; We may have to root out some bad articles...
5400       (when (memq (setq number (mail-header-number
5401                                 (setq header (pop headers))))
5402                   gnus-newsgroup-limit)
5403         ;; Mark article as read when it has a low score.
5404         (when (and gnus-summary-mark-below
5405                    (< (or (cdr (assq number gnus-newsgroup-scored))
5406                           gnus-summary-default-score 0)
5407                       gnus-summary-mark-below)
5408                    (not (gnus-summary-article-ancient-p number)))
5409           (setq gnus-newsgroup-unreads
5410                 (delq number gnus-newsgroup-unreads))
5411           (if gnus-newsgroup-auto-expire
5412               (push number gnus-newsgroup-expirable)
5413             (push (cons number gnus-low-score-mark)
5414                   gnus-newsgroup-reads)))
5415
5416         (setq mark (gnus-article-mark number))
5417         (push (gnus-data-make number mark (1+ (point)) header 0)
5418               gnus-newsgroup-data)
5419         (gnus-summary-insert-line
5420          header 0 number
5421          (memq number gnus-newsgroup-undownloaded)
5422          mark (memq number gnus-newsgroup-replied)
5423          (memq number gnus-newsgroup-expirable)
5424          (mail-header-subject header) nil
5425          (cdr (assq number gnus-newsgroup-scored))
5426          (memq number gnus-newsgroup-processable))))))
5427
5428 (defun gnus-summary-remove-list-identifiers ()
5429   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5430   (let ((regexp (if (consp gnus-list-identifiers)
5431                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5432                   gnus-list-identifiers))
5433         changed subject)
5434     (when regexp
5435       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5436       (dolist (header gnus-newsgroup-headers)
5437         (setq subject (mail-header-subject header)
5438               changed nil)
5439         (while (string-match regexp subject)
5440           (setq subject
5441                 (concat (substring subject 0 (match-beginning 1))
5442                         (substring subject (match-end 0)))
5443                 changed t))
5444         (when changed
5445           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5446             (setq subject
5447                   (concat (substring subject 0 (match-beginning 1))
5448                           (substring subject (match-end 1)))))
5449           (mail-header-set-subject header subject))))))
5450
5451 (defun gnus-fetch-headers (articles)
5452   "Fetch headers of ARTICLES."
5453   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5454     (gnus-message 5 "Fetching headers for %s..." name)
5455     (prog1
5456         (if (eq 'nov
5457                 (setq gnus-headers-retrieved-by
5458                       (gnus-retrieve-headers
5459                        articles gnus-newsgroup-name
5460                        ;; We might want to fetch old headers, but
5461                        ;; not if there is only 1 article.
5462                        (and (or (and
5463                                  (not (eq gnus-fetch-old-headers 'some))
5464                                  (not (numberp gnus-fetch-old-headers)))
5465                                 (> (length articles) 1))
5466                             gnus-fetch-old-headers))))
5467             (gnus-get-newsgroup-headers-xover
5468              articles nil nil gnus-newsgroup-name t)
5469           (gnus-get-newsgroup-headers))
5470       (gnus-message 5 "Fetching headers for %s...done" name))))
5471
5472 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5473   "Select newsgroup GROUP.
5474 If READ-ALL is non-nil, all articles in the group are selected.
5475 If SELECT-ARTICLES, only select those articles from GROUP."
5476   (let* ((entry (gnus-group-entry group))
5477          ;;!!! Dirty hack; should be removed.
5478          (gnus-summary-ignore-duplicates
5479           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5480               t
5481             gnus-summary-ignore-duplicates))
5482          (info (nth 2 entry))
5483          charset articles fetched-articles cached)
5484
5485     (unless (gnus-check-server
5486              (set (make-local-variable 'gnus-current-select-method)
5487                   (gnus-find-method-for-group group)))
5488       (error "Couldn't open server"))
5489     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5490
5491     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5492         (gnus-activate-group group)     ; Or we can activate it...
5493         (progn                          ; Or we bug out.
5494           (when (equal major-mode 'gnus-summary-mode)
5495             (gnus-kill-buffer (current-buffer)))
5496           (error
5497            "Couldn't activate group %s: %s"
5498            (mm-decode-coding-string group charset)
5499            (mm-decode-coding-string (gnus-status-message group) charset))))
5500
5501     (unless (gnus-request-group group t)
5502         (when (equal major-mode 'gnus-summary-mode)
5503           (gnus-kill-buffer (current-buffer)))
5504         (error "Couldn't request group %s: %s"
5505                (mm-decode-coding-string group charset)
5506                (mm-decode-coding-string (gnus-status-message group) charset)))
5507
5508     (when gnus-agent
5509       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5510
5511       (setq gnus-summary-use-undownloaded-faces
5512             (gnus-agent-find-parameter
5513              group
5514              'agent-enable-undownloaded-faces)))
5515
5516     (setq gnus-newsgroup-name group
5517           gnus-newsgroup-unselected nil
5518           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5519
5520     (let ((display (gnus-group-find-parameter group 'display)))
5521       (setq gnus-newsgroup-display
5522             (cond
5523              ((not (zerop (or (car-safe read-all) 0)))
5524               ;; The user entered the group with C-u SPC/RET, let's show
5525               ;; all articles.
5526               'gnus-not-ignore)
5527              ((eq display 'all)
5528               'gnus-not-ignore)
5529              ((arrayp display)
5530               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5531              ((numberp display)
5532               ;; The following is probably the "correct" solution, but
5533               ;; it makes Gnus fetch all headers and then limit the
5534               ;; articles (which is slow), so instead we hack the
5535               ;; select-articles parameter instead. -- Simon Josefsson
5536               ;; <jas@kth.se>
5537               ;;
5538               ;; (gnus-byte-compile
5539               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5540               ;;                         display)))))
5541               (setq select-articles
5542                     (gnus-uncompress-range
5543                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5544                              (if (> tmp 0)
5545                                  tmp
5546                                1))
5547                            (cdr (gnus-active group)))))
5548               nil)
5549              (t
5550               nil))))
5551
5552     (gnus-summary-setup-default-charset)
5553
5554     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5555     (when (gnus-virtual-group-p group)
5556       (setq cached gnus-newsgroup-cached))
5557
5558     (setq gnus-newsgroup-unreads
5559           (gnus-sorted-ndifference
5560            (gnus-sorted-ndifference gnus-newsgroup-unreads
5561                                     gnus-newsgroup-marked)
5562            gnus-newsgroup-dormant))
5563
5564     (setq gnus-newsgroup-processable nil)
5565
5566     (gnus-update-read-articles group gnus-newsgroup-unreads)
5567
5568     ;; Adjust and set lists of article marks.
5569     (when info
5570       (gnus-adjust-marked-articles info))
5571     (if (setq articles select-articles)
5572         (setq gnus-newsgroup-unselected
5573               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5574       (setq articles (gnus-articles-to-read group read-all)))
5575
5576     (cond
5577      ((null articles)
5578       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5579       'quit)
5580      ((eq articles 0) nil)
5581      (t
5582       ;; Init the dependencies hash table.
5583       (setq gnus-newsgroup-dependencies
5584             (gnus-make-hashtable (length articles)))
5585       (gnus-set-global-variables)
5586       ;; Retrieve the headers and read them in.
5587
5588       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5589
5590       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5591       (when cached
5592         (setq gnus-newsgroup-cached cached))
5593
5594       ;; Suppress duplicates?
5595       (when gnus-suppress-duplicates
5596         (gnus-dup-suppress-articles))
5597
5598       ;; Set the initial limit.
5599       (setq gnus-newsgroup-limit (copy-sequence articles))
5600       ;; Remove canceled articles from the list of unread articles.
5601       (setq fetched-articles
5602             (mapcar (lambda (headers) (mail-header-number headers))
5603                     gnus-newsgroup-headers))
5604       (setq gnus-newsgroup-articles fetched-articles)
5605       (setq gnus-newsgroup-unreads
5606             (gnus-sorted-nintersection
5607              gnus-newsgroup-unreads fetched-articles))
5608       (gnus-compute-unseen-list)
5609
5610       ;; Removed marked articles that do not exist.
5611       (gnus-update-missing-marks
5612        (gnus-sorted-difference articles fetched-articles))
5613       ;; We might want to build some more threads first.
5614       (when (and gnus-fetch-old-headers
5615                  (eq gnus-headers-retrieved-by 'nov))
5616         (if (eq gnus-fetch-old-headers 'invisible)
5617             (gnus-build-all-threads)
5618           (gnus-build-old-threads)))
5619       ;; Let the Gnus agent mark articles as read.
5620       (when gnus-agent
5621         (gnus-agent-get-undownloaded-list))
5622       ;; Remove list identifiers from subject
5623       (when gnus-list-identifiers
5624         (gnus-summary-remove-list-identifiers))
5625       ;; Check whether auto-expire is to be done in this group.
5626       (setq gnus-newsgroup-auto-expire
5627             (gnus-group-auto-expirable-p group))
5628       ;; Set up the article buffer now, if necessary.
5629       (unless (and gnus-single-article-buffer
5630                    (equal gnus-article-buffer "*Article*"))
5631         (gnus-article-setup-buffer))
5632       ;; First and last article in this newsgroup.
5633       (when gnus-newsgroup-headers
5634         (setq gnus-newsgroup-begin
5635               (mail-header-number (car gnus-newsgroup-headers))
5636               gnus-newsgroup-end
5637               (mail-header-number
5638                (gnus-last-element gnus-newsgroup-headers))))
5639       ;; GROUP is successfully selected.
5640       (or gnus-newsgroup-headers t)))))
5641
5642 (defun gnus-compute-unseen-list ()
5643   ;; The `seen' marks are treated specially.
5644   (if (not gnus-newsgroup-seen)
5645       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5646     (setq gnus-newsgroup-unseen
5647           (gnus-inverse-list-range-intersection
5648            gnus-newsgroup-articles gnus-newsgroup-seen))))
5649
5650 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5651
5652 (defun gnus-summary-display-make-predicate (display)
5653   (require 'gnus-agent)
5654   (when (= (length display) 1)
5655     (setq display (car display)))
5656   (unless gnus-summary-display-cache
5657     (dolist (elem (append '((unread . unread)
5658                             (read . read)
5659                             (unseen . unseen))
5660                           gnus-article-mark-lists))
5661       (push (cons (cdr elem)
5662                   (gnus-byte-compile
5663                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5664             gnus-summary-display-cache)))
5665   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5666         (gnus-category-predicate-cache gnus-summary-display-cache))
5667     (gnus-get-predicate display)))
5668
5669 ;; Uses the dynamically bound `number' variable.
5670 (defvar number)
5671 (defun gnus-article-marked-p (type &optional article)
5672   (let ((article (or article number)))
5673     (cond
5674      ((eq type 'tick)
5675       (memq article gnus-newsgroup-marked))
5676      ((eq type 'spam)
5677       (memq article gnus-newsgroup-spam-marked))
5678      ((eq type 'unsend)
5679       (memq article gnus-newsgroup-unsendable))
5680      ((eq type 'undownload)
5681       (memq article gnus-newsgroup-undownloaded))
5682      ((eq type 'download)
5683       (memq article gnus-newsgroup-downloadable))
5684      ((eq type 'unread)
5685       (memq article gnus-newsgroup-unreads))
5686      ((eq type 'read)
5687       (memq article gnus-newsgroup-reads))
5688      ((eq type 'dormant)
5689       (memq article gnus-newsgroup-dormant) )
5690      ((eq type 'expire)
5691       (memq article gnus-newsgroup-expirable))
5692      ((eq type 'reply)
5693       (memq article gnus-newsgroup-replied))
5694      ((eq type 'killed)
5695       (memq article gnus-newsgroup-killed))
5696      ((eq type 'bookmark)
5697       (assq article gnus-newsgroup-bookmarks))
5698      ((eq type 'score)
5699       (assq article gnus-newsgroup-scored))
5700      ((eq type 'save)
5701       (memq article gnus-newsgroup-saved))
5702      ((eq type 'cache)
5703       (memq article gnus-newsgroup-cached))
5704      ((eq type 'forward)
5705       (memq article gnus-newsgroup-forwarded))
5706      ((eq type 'seen)
5707       (not (memq article gnus-newsgroup-unseen)))
5708      ((eq type 'recent)
5709       (memq article gnus-newsgroup-recent))
5710      (t t))))
5711
5712 (defun gnus-articles-to-read (group &optional read-all)
5713   "Find out what articles the user wants to read."
5714   (let* ((articles
5715           ;; Select all articles if `read-all' is non-nil, or if there
5716           ;; are no unread articles.
5717           (if (or read-all
5718                   (and (zerop (length gnus-newsgroup-marked))
5719                        (zerop (length gnus-newsgroup-unreads)))
5720                   ;; Fetch all if the predicate is non-nil.
5721                   gnus-newsgroup-display)
5722               ;; We want to select the headers for all the articles in
5723               ;; the group, so we select either all the active
5724               ;; articles in the group, or (if that's nil), the
5725               ;; articles in the cache.
5726               (or
5727                (if gnus-newsgroup-maximum-articles
5728                    (let ((active (gnus-active group)))
5729                      (gnus-uncompress-range
5730                       (cons (max (car active)
5731                                  (- (cdr active)
5732                                     gnus-newsgroup-maximum-articles
5733                                     -1))
5734                             (cdr active))))
5735                  (gnus-uncompress-range (gnus-active group)))
5736                (gnus-cache-articles-in-group group))
5737             ;; Select only the "normal" subset of articles.
5738             (gnus-sorted-nunion
5739              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5740              gnus-newsgroup-unreads)))
5741          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5742          (scored (length scored-list))
5743          (number (length articles))
5744          (marked (+ (length gnus-newsgroup-marked)
5745                     (length gnus-newsgroup-dormant)))
5746          (select
5747           (cond
5748            ((numberp read-all)
5749             read-all)
5750            ((numberp gnus-newsgroup-display)
5751             gnus-newsgroup-display)
5752            (t
5753             (condition-case ()
5754                 (cond
5755                  ((and (or (<= scored marked) (= scored number))
5756                        (numberp gnus-large-newsgroup)
5757                        (> number gnus-large-newsgroup))
5758                   (let* ((cursor-in-echo-area nil)
5759                          (initial (gnus-parameter-large-newsgroup-initial
5760                                    gnus-newsgroup-name))
5761                          (input
5762                           (read-string
5763                            (format
5764                             "How many articles from %s (%s %d): "
5765                             (gnus-group-decoded-name gnus-newsgroup-name)
5766                             (if initial "max" "default")
5767                             number)
5768                            (if initial
5769                                (cons (number-to-string initial)
5770                                      0)))))
5771                     (if (string-match "^[ \t]*$" input) number input)))
5772                  ((and (> scored marked) (< scored number)
5773                        (> (- scored number) 20))
5774                   (let ((input
5775                          (read-string
5776                           (format "%s %s (%d scored, %d total): "
5777                                   "How many articles from"
5778                                   (gnus-group-decoded-name group)
5779                                   scored number))))
5780                     (if (string-match "^[ \t]*$" input)
5781                         number input)))
5782                  (t number))
5783               (quit
5784                (message "Quit getting the articles to read")
5785                nil))))))
5786     (setq select (if (stringp select) (string-to-number select) select))
5787     (if (or (null select) (zerop select))
5788         select
5789       (if (and (not (zerop scored)) (<= (abs select) scored))
5790           (progn
5791             (setq articles (sort scored-list '<))
5792             (setq number (length articles)))
5793         (setq articles (copy-sequence articles)))
5794
5795       (when (< (abs select) number)
5796         (if (< select 0)
5797             ;; Select the N oldest articles.
5798             (setcdr (nthcdr (1- (abs select)) articles) nil)
5799           ;; Select the N most recent articles.
5800           (setq articles (nthcdr (- number select) articles))))
5801       (setq gnus-newsgroup-unselected
5802             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5803       (when gnus-alter-articles-to-read-function
5804         (setq articles
5805               (sort
5806                (funcall gnus-alter-articles-to-read-function
5807                         gnus-newsgroup-name articles)
5808                '<)))
5809       articles)))
5810
5811 (defun gnus-killed-articles (killed articles)
5812   (let (out)
5813     (while articles
5814       (when (inline (gnus-member-of-range (car articles) killed))
5815         (push (car articles) out))
5816       (setq articles (cdr articles)))
5817     out))
5818
5819 (defun gnus-uncompress-marks (marks)
5820   "Uncompress the mark ranges in MARKS."
5821   (let ((uncompressed '(score bookmark))
5822         out)
5823     (while marks
5824       (if (memq (caar marks) uncompressed)
5825           (push (car marks) out)
5826         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5827       (setq marks (cdr marks)))
5828     out))
5829
5830 (defun gnus-article-mark-to-type (mark)
5831   "Return the type of MARK."
5832   (or (cadr (assq mark gnus-article-special-mark-lists))
5833       'list))
5834
5835 (defun gnus-article-unpropagatable-p (mark)
5836   "Return whether MARK should be propagated to back end."
5837   (memq mark gnus-article-unpropagated-mark-lists))
5838
5839 (defun gnus-adjust-marked-articles (info)
5840   "Set all article lists and remove all marks that are no longer valid."
5841   (let* ((marked-lists (gnus-info-marks info))
5842          (active (gnus-active (gnus-info-group info)))
5843          (min (car active))
5844          (max (cdr active))
5845          (types gnus-article-mark-lists)
5846          marks var articles article mark mark-type
5847          bgn end)
5848
5849     (dolist (marks marked-lists)
5850       (setq mark (car marks)
5851             mark-type (gnus-article-mark-to-type mark)
5852             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5853
5854       ;; We set the variable according to the type of the marks list,
5855       ;; and then adjust the marks to a subset of the active articles.
5856       (cond
5857        ;; Adjust "simple" lists - compressed yet unsorted
5858        ((eq mark-type 'list)
5859         ;; Simultaneously uncompress and clip to active range
5860         ;; See gnus-uncompress-range for a description of possible marks
5861         (let (l lh)
5862           (if (not (cadr marks))
5863               (set var nil)
5864             (setq articles (if (numberp (cddr marks))
5865                                (list (cdr marks))
5866                              (cdr marks))
5867                   lh (cons nil nil)
5868                   l lh)
5869
5870             (while (setq article (pop articles))
5871               (cond ((consp article)
5872                      (setq bgn (max (car article) min)
5873                            end (min (cdr article) max))
5874                      (while (<= bgn end)
5875                        (setq l (setcdr l (cons bgn nil))
5876                              bgn (1+ bgn))))
5877                     ((and (<= min article)
5878                           (>= max article))
5879                      (setq l (setcdr l (cons article nil))))))
5880             (set var (cdr lh)))))
5881        ;; Adjust assocs.
5882        ((eq mark-type 'tuple)
5883         (set var (setq articles (cdr marks)))
5884         (when (not (listp (cdr (symbol-value var))))
5885           (set var (list (symbol-value var))))
5886         (when (not (listp (cdr articles)))
5887           (setq articles (list articles)))
5888         (while articles
5889           (when (or (not (consp (setq article (pop articles))))
5890                     (< (car article) min)
5891                     (> (car article) max))
5892             (set var (delq article (symbol-value var))))))
5893        ;; Adjust ranges (sloppily).
5894        ((eq mark-type 'range)
5895         (cond
5896          ((eq mark 'seen)
5897           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5898           ;; It should be (seen (NUM1 . NUM2)).
5899           (when (numberp (cddr marks))
5900             (setcdr marks (list (cdr marks))))
5901           (setq articles (cdr marks))
5902           (while (and articles
5903                       (or (and (consp (car articles))
5904                                (> min (cdar articles)))
5905                           (and (numberp (car articles))
5906                                (> min (car articles)))))
5907             (pop articles))
5908           (set var articles))))))))
5909
5910 (defun gnus-update-missing-marks (missing)
5911   "Go through the list of MISSING articles and remove them from the mark lists."
5912   (when missing
5913     (let (var m)
5914       ;; Go through all types.
5915       (dolist (elem gnus-article-mark-lists)
5916         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5917           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5918           (when (symbol-value var)
5919             ;; This list has articles.  So we delete all missing
5920             ;; articles from it.
5921             (setq m missing)
5922             (while m
5923               (set var (delq (pop m) (symbol-value var))))))))))
5924
5925 (defun gnus-update-marks ()
5926   "Enter the various lists of marked articles into the newsgroup info list."
5927   (let ((types gnus-article-mark-lists)
5928         (info (gnus-get-info gnus-newsgroup-name))
5929         type list newmarked symbol delta-marks)
5930     (when info
5931       ;; Add all marks lists to the list of marks lists.
5932       (while (setq type (pop types))
5933         (setq list (symbol-value
5934                     (setq symbol
5935                           (intern (format "gnus-newsgroup-%s" (car type))))))
5936
5937         (when list
5938           ;; Get rid of the entries of the articles that have the
5939           ;; default score.
5940           (when (and (eq (cdr type) 'score)
5941                      gnus-save-score
5942                      list)
5943             (let* ((arts list)
5944                    (prev (cons nil list))
5945                    (all prev))
5946               (while arts
5947                 (if (or (not (consp (car arts)))
5948                         (= (cdar arts) gnus-summary-default-score))
5949                     (setcdr prev (cdr arts))
5950                   (setq prev arts))
5951                 (setq arts (cdr arts)))
5952               (setq list (cdr all)))))
5953
5954         (when (eq (cdr type) 'seen)
5955           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5956
5957         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5958           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5959
5960         (when (and (gnus-check-backend-function
5961                     'request-set-mark gnus-newsgroup-name)
5962                    (not (gnus-article-unpropagatable-p (cdr type))))
5963           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5964                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5965                  (add (gnus-remove-from-range
5966                        (gnus-copy-sequence list) old)))
5967             (when add
5968               (push (list add 'add (list (cdr type))) delta-marks))
5969             (when del
5970               (push (list del 'del (list (cdr type))) delta-marks))))
5971
5972         (when list
5973           (push (cons (cdr type) list) newmarked)))
5974
5975       (when delta-marks
5976         (unless (gnus-check-group gnus-newsgroup-name)
5977           (error "Can't open server for %s" gnus-newsgroup-name))
5978         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5979
5980       ;; Enter these new marks into the info of the group.
5981       (if (nthcdr 3 info)
5982           (setcar (nthcdr 3 info) newmarked)
5983         ;; Add the marks lists to the end of the info.
5984         (when newmarked
5985           (setcdr (nthcdr 2 info) (list newmarked))))
5986
5987       ;; Cut off the end of the info if there's nothing else there.
5988       (let ((i 5))
5989         (while (and (> i 2)
5990                     (not (nth i info)))
5991           (when (nthcdr (decf i) info)
5992             (setcdr (nthcdr i info) nil)))))))
5993
5994 (defun gnus-set-mode-line (where)
5995   "Set the mode line of the article or summary buffers.
5996 If WHERE is `summary', the summary mode line format will be used."
5997   ;; Is this mode line one we keep updated?
5998   (when (and (memq where gnus-updated-mode-lines)
5999              (symbol-value
6000               (intern (format "gnus-%s-mode-line-format-spec" where))))
6001     (let (mode-string)
6002       ;; We evaluate this in the summary buffer since these
6003       ;; variables are buffer-local to that buffer.
6004       (with-current-buffer gnus-summary-buffer
6005         ;; We bind all these variables that are used in the `eval' form
6006         ;; below.
6007         (let* ((mformat (symbol-value
6008                          (intern
6009                           (format "gnus-%s-mode-line-format-spec" where))))
6010                (gnus-tmp-group-name (gnus-mode-string-quote
6011                                      (gnus-group-decoded-name
6012                                       gnus-newsgroup-name)))
6013                (gnus-tmp-article-number (or gnus-current-article 0))
6014                (gnus-tmp-unread gnus-newsgroup-unreads)
6015                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6016                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6017                (gnus-tmp-unread-and-unselected
6018                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6019                             (zerop gnus-tmp-unselected))
6020                        "")
6021                       ((zerop gnus-tmp-unselected)
6022                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6023                       (t (format "{%d(+%d) more}"
6024                                  gnus-tmp-unread-and-unticked
6025                                  gnus-tmp-unselected))))
6026                (gnus-tmp-subject
6027                 (if (and gnus-current-headers
6028                          (vectorp gnus-current-headers))
6029                     (gnus-mode-string-quote
6030                      (mail-header-subject gnus-current-headers))
6031                   ""))
6032                bufname-length max-len
6033                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6034           (setq mode-string (eval mformat))
6035           (setq bufname-length (if (string-match "%b" mode-string)
6036                                    (- (length
6037                                        (buffer-name
6038                                         (if (eq where 'summary)
6039                                             nil
6040                                           (get-buffer gnus-article-buffer))))
6041                                       2)
6042                                  0))
6043           (setq max-len (max 4 (if gnus-mode-non-string-length
6044                                    (- (window-width)
6045                                       gnus-mode-non-string-length
6046                                       bufname-length)
6047                                  (length mode-string))))
6048           ;; We might have to chop a bit of the string off...
6049           (when (> (length mode-string) max-len)
6050             (setq mode-string
6051                   (concat (truncate-string-to-width mode-string (- max-len 3))
6052                           "...")))
6053           ;; Pad the mode string a bit.
6054           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6055       ;; Update the mode line.
6056       (setq mode-line-buffer-identification
6057             (gnus-mode-line-buffer-identification (list mode-string)))
6058       (set-buffer-modified-p t))))
6059
6060 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6061   "Go through the HEADERS list and add all Xrefs to a hash table.
6062 The resulting hash table is returned, or nil if no Xrefs were found."
6063   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6064          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6065          (xref-hashtb (gnus-make-hashtable))
6066          start group entry number xrefs header)
6067     (while headers
6068       (setq header (pop headers))
6069       (when (and (setq xrefs (mail-header-xref header))
6070                  (not (memq (setq number (mail-header-number header))
6071                             unreads)))
6072         (setq start 0)
6073         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6074           (setq start (match-end 0))
6075           (setq group (if prefix
6076                           (concat prefix (substring xrefs (match-beginning 1)
6077                                                     (match-end 1)))
6078                         (substring xrefs (match-beginning 1) (match-end 1))))
6079           (setq number
6080                 (string-to-number (substring xrefs (match-beginning 2)
6081                                           (match-end 2))))
6082           (if (setq entry (gnus-gethash group xref-hashtb))
6083               (setcdr entry (cons number (cdr entry)))
6084             (gnus-sethash group (cons number nil) xref-hashtb)))))
6085     (and start xref-hashtb)))
6086
6087 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6088   "Look through all the headers and mark the Xrefs as read."
6089   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6090         name info xref-hashtb idlist method nth4)
6091     (with-current-buffer gnus-group-buffer
6092       (when (setq xref-hashtb
6093                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6094         (mapatoms
6095          (lambda (group)
6096            (unless (string= from-newsgroup (setq name (symbol-name group)))
6097              (setq idlist (symbol-value group))
6098              ;; Dead groups are not updated.
6099              (and (prog1
6100                       (setq info (gnus-get-info name))
6101                     (when (stringp (setq nth4 (gnus-info-method info)))
6102                       (setq nth4 (gnus-server-to-method nth4))))
6103                   ;; Only do the xrefs if the group has the same
6104                   ;; select method as the group we have just read.
6105                   (or (gnus-methods-equal-p
6106                        nth4 (gnus-find-method-for-group from-newsgroup))
6107                       virtual
6108                       (equal nth4 (setq method (gnus-find-method-for-group
6109                                                 from-newsgroup)))
6110                       (and (equal (car nth4) (car method))
6111                            (equal (nth 1 nth4) (nth 1 method))))
6112                   gnus-use-cross-reference
6113                   (or (not (eq gnus-use-cross-reference t))
6114                       virtual
6115                       ;; Only do cross-references on subscribed
6116                       ;; groups, if that is what is wanted.
6117                       (<= (gnus-info-level info) gnus-level-subscribed))
6118                   (gnus-group-make-articles-read name idlist))))
6119          xref-hashtb)))))
6120
6121 (defun gnus-compute-read-articles (group articles)
6122   (let* ((entry (gnus-group-entry group))
6123          (info (nth 2 entry))
6124          (active (gnus-active group))
6125          ninfo)
6126     (when entry
6127       ;; First peel off all invalid article numbers.
6128       (when active
6129         (let ((ids articles)
6130               id first)
6131           (while (setq id (pop ids))
6132             (when (and first (> id (cdr active)))
6133               ;; We'll end up in this situation in one particular
6134               ;; obscure situation.  If you re-scan a group and get
6135               ;; a new article that is cross-posted to a different
6136               ;; group that has not been re-scanned, you might get
6137               ;; crossposted article that has a higher number than
6138               ;; Gnus believes possible.  So we re-activate this
6139               ;; group as well.  This might mean doing the
6140               ;; crossposting thingy will *increase* the number
6141               ;; of articles in some groups.  Tsk, tsk.
6142               (setq active (or (gnus-activate-group group) active)))
6143             (when (or (> id (cdr active))
6144                       (< id (car active)))
6145               (setq articles (delq id articles))))))
6146       ;; If the read list is nil, we init it.
6147       (if (and active
6148                (null (gnus-info-read info))
6149                (> (car active) 1))
6150           (setq ninfo (cons 1 (1- (car active))))
6151         (setq ninfo (gnus-info-read info)))
6152       ;; Then we add the read articles to the range.
6153       (gnus-add-to-range
6154        ninfo (setq articles (sort articles '<))))))
6155
6156 (defun gnus-group-make-articles-read (group articles)
6157   "Update the info of GROUP to say that ARTICLES are read."
6158   (let* ((num 0)
6159          (entry (gnus-group-entry group))
6160          (info (nth 2 entry))
6161          (active (gnus-active group))
6162          range)
6163     (when entry
6164       (setq range (gnus-compute-read-articles group articles))
6165       (with-current-buffer gnus-group-buffer
6166         (gnus-undo-register
6167           `(progn
6168              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6169              (gnus-info-set-read ',info ',(gnus-info-read info))
6170              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6171              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6172              (gnus-group-update-group ,group t))))
6173       ;; Add the read articles to the range.
6174       (gnus-info-set-read info range)
6175       (gnus-request-set-mark group (list (list range 'add '(read))))
6176       ;; Then we have to re-compute how many unread
6177       ;; articles there are in this group.
6178       (when active
6179         (cond
6180          ((not range)
6181           (setq num (- (1+ (cdr active)) (car active))))
6182          ((not (listp (cdr range)))
6183           (setq num (- (cdr active) (- (1+ (cdr range))
6184                                        (car range)))))
6185          (t
6186           (while range
6187             (if (numberp (car range))
6188                 (setq num (1+ num))
6189               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6190             (setq range (cdr range)))
6191           (setq num (- (cdr active) num))))
6192         ;; Update the number of unread articles.
6193         (setcar entry num)
6194         ;; Update the group buffer.
6195         (unless (gnus-ephemeral-group-p group)
6196           (gnus-group-update-group group t))))))
6197
6198 (defvar gnus-newsgroup-none-id 0)
6199
6200 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6201   (let ((cur nntp-server-buffer)
6202         (dependencies
6203          (or dependencies
6204              (with-current-buffer gnus-summary-buffer
6205                gnus-newsgroup-dependencies)))
6206         headers id end ref number
6207         (mail-parse-charset gnus-newsgroup-charset)
6208         (mail-parse-ignored-charsets
6209          (save-current-buffer (condition-case nil
6210                                   (set-buffer gnus-summary-buffer)
6211                                 (error))
6212                               gnus-newsgroup-ignored-charsets)))
6213     (with-current-buffer nntp-server-buffer
6214       ;; Translate all TAB characters into SPACE characters.
6215       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6216       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6217       (ietf-drums-unfold-fws)
6218       (gnus-run-hooks 'gnus-parse-headers-hook)
6219       (let ((case-fold-search t)
6220             in-reply-to header p lines chars)
6221         (goto-char (point-min))
6222         ;; Search to the beginning of the next header.  Error messages
6223         ;; do not begin with 2 or 3.
6224         (while (re-search-forward "^[23][0-9]+ " nil t)
6225           (setq id nil
6226                 ref nil)
6227           ;; This implementation of this function, with nine
6228           ;; search-forwards instead of the one re-search-forward and
6229           ;; a case (which basically was the old function) is actually
6230           ;; about twice as fast, even though it looks messier.  You
6231           ;; can't have everything, I guess.  Speed and elegance
6232           ;; doesn't always go hand in hand.
6233           (setq
6234            header
6235            (vector
6236             ;; Number.
6237             (prog1
6238                 (setq number (read cur))
6239               (end-of-line)
6240               (setq p (point))
6241               (narrow-to-region (point)
6242                                 (or (and (search-forward "\n.\n" nil t)
6243                                          (- (point) 2))
6244                                     (point))))
6245             ;; Subject.
6246             (progn
6247               (goto-char p)
6248               (if (search-forward "\nsubject:" nil t)
6249                   (funcall gnus-decode-encoded-word-function
6250                            (nnheader-header-value))
6251                 "(none)"))
6252             ;; From.
6253             (progn
6254               (goto-char p)
6255               (if (search-forward "\nfrom:" nil t)
6256                   (funcall gnus-decode-encoded-address-function
6257                            (nnheader-header-value))
6258                 "(nobody)"))
6259             ;; Date.
6260             (progn
6261               (goto-char p)
6262               (if (search-forward "\ndate:" nil t)
6263                   (nnheader-header-value) ""))
6264             ;; Message-ID.
6265             (progn
6266               (goto-char p)
6267               (setq id (if (re-search-forward
6268                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6269                            ;; We do it this way to make sure the Message-ID
6270                            ;; is (somewhat) syntactically valid.
6271                            (buffer-substring (match-beginning 1)
6272                                              (match-end 1))
6273                          ;; If there was no message-id, we just fake one
6274                          ;; to make subsequent routines simpler.
6275                          (nnheader-generate-fake-message-id number))))
6276             ;; References.
6277             (progn
6278               (goto-char p)
6279               (if (search-forward "\nreferences:" nil t)
6280                   (progn
6281                     (setq end (point))
6282                     (prog1
6283                         (nnheader-header-value)
6284                       (setq ref
6285                             (buffer-substring
6286                              (progn
6287                                (end-of-line)
6288                                (search-backward ">" end t)
6289                                (1+ (point)))
6290                              (progn
6291                                (search-backward "<" end t)
6292                                (point))))))
6293                 ;; Get the references from the in-reply-to header if there
6294                 ;; were no references and the in-reply-to header looks
6295                 ;; promising.
6296                 (if (and (search-forward "\nin-reply-to:" nil t)
6297                          (setq in-reply-to (nnheader-header-value))
6298                          (string-match "<[^>]+>" in-reply-to))
6299                     (let (ref2)
6300                       (setq ref (substring in-reply-to (match-beginning 0)
6301                                            (match-end 0)))
6302                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6303                         (setq ref2 (substring in-reply-to (match-beginning 0)
6304                                               (match-end 0)))
6305                         (when (> (length ref2) (length ref))
6306                           (setq ref ref2)))
6307                       ref)
6308                   (setq ref nil))))
6309             ;; Chars.
6310             (progn
6311               (goto-char p)
6312               (if (search-forward "\nchars: " nil t)
6313                   (if (numberp (setq chars (ignore-errors (read cur))))
6314                       chars -1)
6315                 -1))
6316             ;; Lines.
6317             (progn
6318               (goto-char p)
6319               (if (search-forward "\nlines: " nil t)
6320                   (if (numberp (setq lines (ignore-errors (read cur))))
6321                       lines -1)
6322                 -1))
6323             ;; Xref.
6324             (progn
6325               (goto-char p)
6326               (and (search-forward "\nxref:" nil t)
6327                    (nnheader-header-value)))
6328             ;; Extra.
6329             (when gnus-extra-headers
6330               (let ((extra gnus-extra-headers)
6331                     out)
6332                 (while extra
6333                   (goto-char p)
6334                   (when (search-forward
6335                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6336                     (push (cons (car extra) (nnheader-header-value))
6337                           out))
6338                   (pop extra))
6339                 out))))
6340           (when (equal id ref)
6341             (setq ref nil))
6342
6343           (when gnus-alter-header-function
6344             (funcall gnus-alter-header-function header)
6345             (setq id (mail-header-id header)
6346                   ref (gnus-parent-id (mail-header-references header))))
6347
6348           (when (setq header
6349                       (gnus-dependencies-add-header
6350                        header dependencies force-new))
6351             (push header headers))
6352           (goto-char (point-max))
6353           (widen))
6354         (nreverse headers)))))
6355
6356 ;; Goes through the xover lines and returns a list of vectors
6357 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6358                                                   force-new dependencies
6359                                                   group also-fetch-heads)
6360   "Parse the news overview data in the server buffer.
6361 Return a list of headers that match SEQUENCE (see
6362 `nntp-retrieve-headers')."
6363   ;; Get the Xref when the users reads the articles since most/some
6364   ;; NNTP servers do not include Xrefs when using XOVER.
6365   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6366   (let ((mail-parse-charset gnus-newsgroup-charset)
6367         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6368         (cur nntp-server-buffer)
6369         (dependencies (or dependencies gnus-newsgroup-dependencies))
6370         (allp (cond
6371                ((eq gnus-read-all-available-headers t)
6372                 t)
6373                ((and (stringp gnus-read-all-available-headers)
6374                      group)
6375                 (string-match gnus-read-all-available-headers group))
6376                (t
6377                 nil)))
6378         number headers header)
6379     (with-current-buffer nntp-server-buffer
6380       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6381       ;; Allow the user to mangle the headers before parsing them.
6382       (gnus-run-hooks 'gnus-parse-headers-hook)
6383       (goto-char (point-min))
6384       (gnus-parse-without-error
6385         (while (and (or sequence allp)
6386                     (not (eobp)))
6387           (setq number (read cur))
6388           (when (not allp)
6389             (while (and sequence
6390                         (< (car sequence) number))
6391               (setq sequence (cdr sequence))))
6392           (when (and (or allp
6393                          (and sequence
6394                               (eq number (car sequence))))
6395                      (progn
6396                        (setq sequence (cdr sequence))
6397                        (setq header (inline
6398                                       (gnus-nov-parse-line
6399                                        number dependencies force-new)))))
6400             (push header headers))
6401           (forward-line 1)))
6402       ;; A common bug in inn is that if you have posted an article and
6403       ;; then retrieves the active file, it will answer correctly --
6404       ;; the new article is included.  However, a NOV entry for the
6405       ;; article may not have been generated yet, so this may fail.
6406       ;; We work around this problem by retrieving the last few
6407       ;; headers using HEAD.
6408       (if (or (not also-fetch-heads)
6409               (not sequence))
6410           ;; We (probably) got all the headers.
6411           (nreverse headers)
6412         (let ((gnus-nov-is-evil t))
6413           (nconc
6414            (nreverse headers)
6415            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6416              (gnus-get-newsgroup-headers))))))))
6417
6418 (defun gnus-article-get-xrefs ()
6419   "Fill in the Xref value in `gnus-current-headers', if necessary.
6420 This is meant to be called in `gnus-article-internal-prepare-hook'."
6421   (let ((headers (with-current-buffer gnus-summary-buffer
6422                    gnus-current-headers)))
6423     (or (not gnus-use-cross-reference)
6424         (not headers)
6425         (and (mail-header-xref headers)
6426              (not (string= (mail-header-xref headers) "")))
6427         (let ((case-fold-search t)
6428               xref)
6429           (save-restriction
6430             (nnheader-narrow-to-headers)
6431             (goto-char (point-min))
6432             (when (or (and (not (eobp))
6433                            (eq (downcase (char-after)) ?x)
6434                            (looking-at "Xref:"))
6435                       (search-forward "\nXref:" nil t))
6436               (goto-char (1+ (match-end 0)))
6437               (setq xref (buffer-substring (point) (point-at-eol)))
6438               (mail-header-set-xref headers xref)))))))
6439
6440 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6441   "Find article ID and insert the summary line for that article.
6442 OLD-HEADER can either be a header or a line number to insert
6443 the subject line on."
6444   (let* ((line (and (numberp old-header) old-header))
6445          (old-header (and (vectorp old-header) old-header))
6446          (header (cond ((and old-header use-old-header)
6447                         old-header)
6448                        ((and (numberp id)
6449                              (gnus-number-to-header id))
6450                         (gnus-number-to-header id))
6451                        (t
6452                         (gnus-read-header id))))
6453          (number (and (numberp id) id))
6454          d)
6455     (when header
6456       ;; Rebuild the thread that this article is part of and go to the
6457       ;; article we have fetched.
6458       (when (and (not gnus-show-threads)
6459                  old-header)
6460         (when (and number
6461                    (setq d (gnus-data-find (mail-header-number old-header))))
6462           (goto-char (gnus-data-pos d))
6463           (gnus-data-remove
6464            number
6465            (- (point-at-bol)
6466               (prog1
6467                   (1+ (point-at-eol))
6468                 (gnus-delete-line))))))
6469       ;; Remove list identifiers from subject.
6470       (when gnus-list-identifiers
6471         (let ((gnus-newsgroup-headers (list header)))
6472           (gnus-summary-remove-list-identifiers)))
6473       (when old-header
6474         (mail-header-set-number header (mail-header-number old-header)))
6475       (setq gnus-newsgroup-sparse
6476             (delq (setq number (mail-header-number header))
6477                   gnus-newsgroup-sparse))
6478       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6479       (push number gnus-newsgroup-limit)
6480       (gnus-rebuild-thread (mail-header-id header) line)
6481       (gnus-summary-goto-subject number nil t))
6482     (when (and (numberp number)
6483                (> number 0))
6484       ;; We have to update the boundaries even if we can't fetch the
6485       ;; article if ID is a number -- so that the next `P' or `N'
6486       ;; command will fetch the previous (or next) article even
6487       ;; if the one we tried to fetch this time has been canceled.
6488       (when (> number gnus-newsgroup-end)
6489         (setq gnus-newsgroup-end number))
6490       (when (< number gnus-newsgroup-begin)
6491         (setq gnus-newsgroup-begin number))
6492       (setq gnus-newsgroup-unselected
6493             (delq number gnus-newsgroup-unselected)))
6494     ;; Report back a success?
6495     (and header (mail-header-number header))))
6496
6497 ;;; Process/prefix in the summary buffer
6498
6499 (defun gnus-summary-work-articles (n)
6500   "Return a list of articles to be worked upon.
6501 The prefix argument, the list of process marked articles, and the
6502 current article will be taken into consideration."
6503   (with-current-buffer gnus-summary-buffer
6504     (cond
6505      (n
6506       ;; A numerical prefix has been given.
6507       (setq n (prefix-numeric-value n))
6508       (let ((backward (< n 0))
6509             (n (abs (prefix-numeric-value n)))
6510             articles article)
6511         (save-excursion
6512           (while
6513               (and (> n 0)
6514                    (push (setq article (gnus-summary-article-number))
6515                          articles)
6516                    (if backward
6517                        (gnus-summary-find-prev nil article)
6518                      (gnus-summary-find-next nil article)))
6519             (decf n)))
6520         (nreverse articles)))
6521      ((and (gnus-region-active-p) (mark))
6522       (message "region active")
6523       ;; Work on the region between point and mark.
6524       (let ((max (max (point) (mark)))
6525             articles article)
6526         (save-excursion
6527           (goto-char (min (point) (mark)))
6528           (while
6529               (and
6530                (push (setq article (gnus-summary-article-number)) articles)
6531                (gnus-summary-find-next nil article)
6532                (< (point) max)))
6533           (nreverse articles))))
6534      (gnus-newsgroup-processable
6535       ;; There are process-marked articles present.
6536       ;; Save current state.
6537       (gnus-summary-save-process-mark)
6538       ;; Return the list.
6539       (reverse gnus-newsgroup-processable))
6540      (t
6541       ;; Just return the current article.
6542       (list (gnus-summary-article-number))))))
6543
6544 (defmacro gnus-summary-iterate (arg &rest forms)
6545   "Iterate over the process/prefixed articles and do FORMS.
6546 ARG is the interactive prefix given to the command.  FORMS will be
6547 executed with point over the summary line of the articles."
6548   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6549     `(let ((,articles (gnus-summary-work-articles ,arg)))
6550        (while ,articles
6551          (gnus-summary-goto-subject (car ,articles))
6552          ,@forms
6553          (pop ,articles)))))
6554
6555 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6556 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6557
6558 (defun gnus-summary-save-process-mark ()
6559   "Push the current set of process marked articles on the stack."
6560   (interactive)
6561   (push (copy-sequence gnus-newsgroup-processable)
6562         gnus-newsgroup-process-stack))
6563
6564 (defun gnus-summary-kill-process-mark ()
6565   "Push the current set of process marked articles on the stack and unmark."
6566   (interactive)
6567   (gnus-summary-save-process-mark)
6568   (gnus-summary-unmark-all-processable))
6569
6570 (defun gnus-summary-yank-process-mark ()
6571   "Pop the last process mark state off the stack and restore it."
6572   (interactive)
6573   (unless gnus-newsgroup-process-stack
6574     (error "Empty mark stack"))
6575   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6576
6577 (defun gnus-summary-process-mark-set (set)
6578   "Make SET into the current process marked articles."
6579   (gnus-summary-unmark-all-processable)
6580   (mapc 'gnus-summary-set-process-mark set))
6581
6582 ;;; Searching and stuff
6583
6584 (defun gnus-summary-search-group (&optional backward use-level)
6585   "Search for next unread newsgroup.
6586 If optional argument BACKWARD is non-nil, search backward instead."
6587   (with-current-buffer gnus-group-buffer
6588     (when (gnus-group-search-forward
6589            backward nil (if use-level (gnus-group-group-level) nil))
6590       (gnus-group-group-name))))
6591
6592 (defun gnus-summary-best-group (&optional exclude-group)
6593   "Find the name of the best unread group.
6594 If EXCLUDE-GROUP, do not go to this group."
6595   (with-current-buffer gnus-group-buffer
6596     (save-excursion
6597       (gnus-group-best-unread-group exclude-group))))
6598
6599 (defun gnus-summary-find-next (&optional unread article backward)
6600   (if backward
6601       (gnus-summary-find-prev unread article)
6602     (let* ((dummy (gnus-summary-article-intangible-p))
6603            (article (or article (gnus-summary-article-number)))
6604            (data (gnus-data-find-list article))
6605            result)
6606       (when (and (not dummy)
6607                  (or (not gnus-summary-check-current)
6608                      (not unread)
6609                      (not (gnus-data-unread-p (car data)))))
6610         (setq data (cdr data)))
6611       (when (setq result
6612                   (if unread
6613                       (progn
6614                         (while data
6615                           (unless (memq (gnus-data-number (car data))
6616                                         (cond
6617                                          ((eq gnus-auto-goto-ignores
6618                                               'always-undownloaded)
6619                                           gnus-newsgroup-undownloaded)
6620                                          (gnus-plugged
6621                                           nil)
6622                                          ((eq gnus-auto-goto-ignores
6623                                               'unfetched)
6624                                           gnus-newsgroup-unfetched)
6625                                          ((eq gnus-auto-goto-ignores
6626                                               'undownloaded)
6627                                           gnus-newsgroup-undownloaded)))
6628                             (when (gnus-data-unread-p (car data))
6629                               (setq result (car data)
6630                                     data nil)))
6631                           (setq data (cdr data)))
6632                         result)
6633                     (car data)))
6634         (goto-char (gnus-data-pos result))
6635         (gnus-data-number result)))))
6636
6637 (defun gnus-summary-find-prev (&optional unread article)
6638   (let* ((eobp (eobp))
6639          (article (or article (gnus-summary-article-number)))
6640          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6641          result)
6642     (when (and (not eobp)
6643                (or (not gnus-summary-check-current)
6644                    (not unread)
6645                    (not (gnus-data-unread-p (car data)))))
6646       (setq data (cdr data)))
6647     (when (setq result
6648                 (if unread
6649                     (progn
6650                       (while data
6651                         (unless (memq (gnus-data-number (car data))
6652                                       (cond
6653                                        ((eq gnus-auto-goto-ignores
6654                                             'always-undownloaded)
6655                                         gnus-newsgroup-undownloaded)
6656                                        (gnus-plugged
6657                                         nil)
6658                                        ((eq gnus-auto-goto-ignores
6659                                             'unfetched)
6660                                         gnus-newsgroup-unfetched)
6661                                        ((eq gnus-auto-goto-ignores
6662                                             'undownloaded)
6663                                         gnus-newsgroup-undownloaded)))
6664                           (when (gnus-data-unread-p (car data))
6665                             (setq result (car data)
6666                                   data nil)))
6667                         (setq data (cdr data)))
6668                       result)
6669                   (car data)))
6670       (goto-char (gnus-data-pos result))
6671       (gnus-data-number result))))
6672
6673 (defun gnus-summary-find-subject (subject &optional unread backward article)
6674   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6675          (article (or article (gnus-summary-article-number)))
6676          (articles (gnus-data-list backward))
6677          (arts (gnus-data-find-list article articles))
6678          result)
6679     (when (or (not gnus-summary-check-current)
6680               (not unread)
6681               (not (gnus-data-unread-p (car arts))))
6682       (setq arts (cdr arts)))
6683     (while arts
6684       (and (or (not unread)
6685                (gnus-data-unread-p (car arts)))
6686            (vectorp (gnus-data-header (car arts)))
6687            (gnus-subject-equal
6688             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6689            (setq result (car arts)
6690                  arts nil))
6691       (setq arts (cdr arts)))
6692     (and result
6693          (goto-char (gnus-data-pos result))
6694          (gnus-data-number result))))
6695
6696 (defun gnus-summary-search-forward (&optional unread subject backward)
6697   "Search forward for an article.
6698 If UNREAD, look for unread articles.  If SUBJECT, look for
6699 articles with that subject.  If BACKWARD, search backward instead."
6700   (cond (subject (gnus-summary-find-subject subject unread backward))
6701         (backward (gnus-summary-find-prev unread))
6702         (t (gnus-summary-find-next unread))))
6703
6704 (defun gnus-recenter (&optional n)
6705   "Center point in window and redisplay frame.
6706 Also do horizontal recentering."
6707   (interactive "P")
6708   (when (and gnus-auto-center-summary
6709              (not (eq gnus-auto-center-summary 'vertical)))
6710     (gnus-horizontal-recenter))
6711   (if (fboundp 'recenter-top-bottom)
6712       (recenter-top-bottom n)
6713     (recenter n)))
6714
6715 (put 'gnus-recenter 'isearch-scroll t)
6716
6717 (defun gnus-forward-line-ignore-invisible (n)
6718   "Move N lines forward (backward if N is negative).
6719 Like forward-line, but skip over (and don't count) invisible lines."
6720   (let (done)
6721     (while (and (> n 0) (not done))
6722       ;; If the following character is currently invisible,
6723       ;; skip all characters with that same `invisible' property value.
6724       (while (gnus-invisible-p (point))
6725         (goto-char (gnus-next-char-property-change (point))))
6726       (forward-line 1)
6727       (if (eobp)
6728           (setq done t)
6729         (setq n (1- n))))
6730     (while (and (< n 0) (not done))
6731       (forward-line -1)
6732       (if (bobp) (setq done t)
6733         (setq n (1+ n))
6734         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6735           (goto-char (gnus-previous-char-property-change (point))))))))
6736
6737 (defun gnus-summary-recenter ()
6738   "Center point in the summary window.
6739 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6740 displayed, no centering will be performed."
6741   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6742   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6743   (interactive)
6744   ;; The user has to want it.
6745   (when gnus-auto-center-summary
6746     (let* ((top (cond ((< (window-height) 4) 0)
6747                       ((< (window-height) 7) 1)
6748                       (t (if (numberp gnus-auto-center-summary)
6749                              gnus-auto-center-summary
6750                            (/ (1- (window-height)) 2)))))
6751            (height (1- (window-height)))
6752            (bottom (save-excursion
6753                      (goto-char (point-max))
6754                      (gnus-forward-line-ignore-invisible (- height))
6755                      (point)))
6756            (window (get-buffer-window (current-buffer))))
6757       (when (get-buffer-window gnus-article-buffer)
6758         ;; Only do recentering when the article buffer is displayed,
6759         ;; Set the window start to either `bottom', which is the biggest
6760         ;; possible valid number, or the second line from the top,
6761         ;; whichever is the least.
6762         (let ((top-pos (save-excursion
6763                          (gnus-forward-line-ignore-invisible (- top))
6764                          (point))))
6765           (if (> bottom top-pos)
6766               ;; Keep the second line from the top visible
6767               (set-window-start window top-pos)
6768             ;; Try to keep the bottom line visible; if it's partially
6769             ;; obscured, either scroll one more line to make it fully
6770             ;; visible, or revert to using TOP-POS.
6771             (save-excursion
6772               (goto-char (point-max))
6773               (gnus-forward-line-ignore-invisible -1)
6774               (let ((last-line-start (point)))
6775                 (goto-char bottom)
6776                 (set-window-start window (point) t)
6777                 (when (not (pos-visible-in-window-p last-line-start window))
6778                   (gnus-forward-line-ignore-invisible 1)
6779                   (set-window-start window (min (point) top-pos) t)))))))
6780       ;; Do horizontal recentering while we're at it.
6781       (when (and (get-buffer-window (current-buffer) t)
6782                  (not (eq gnus-auto-center-summary 'vertical)))
6783         (let ((selected (selected-window)))
6784           (select-window (get-buffer-window (current-buffer) t))
6785           (gnus-summary-position-point)
6786           (gnus-horizontal-recenter)
6787           (select-window selected))))))
6788
6789 (defun gnus-summary-jump-to-group (newsgroup)
6790   "Move point to NEWSGROUP in group mode buffer."
6791   ;; Keep update point of group mode buffer if visible.
6792   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6793       (save-window-excursion
6794         ;; Take care of tree window mode.
6795         (when (get-buffer-window gnus-group-buffer)
6796           (pop-to-buffer gnus-group-buffer))
6797         (gnus-group-jump-to-group newsgroup))
6798     (save-excursion
6799       ;; Take care of tree window mode.
6800       (if (get-buffer-window gnus-group-buffer 0)
6801           (pop-to-buffer gnus-group-buffer)
6802         (set-buffer gnus-group-buffer))
6803       (gnus-group-jump-to-group newsgroup))))
6804
6805 ;; This function returns a list of article numbers based on the
6806 ;; difference between the ranges of read articles in this group and
6807 ;; the range of active articles.
6808 (defun gnus-list-of-unread-articles (group)
6809   (let* ((read (gnus-info-read (gnus-get-info group)))
6810          (active (or (gnus-active group) (gnus-activate-group group)))
6811          (last (or (cdr active)
6812                    (error "Group %s couldn't be activated " group)))
6813          (bottom (if gnus-newsgroup-maximum-articles
6814                      (max (car active)
6815                           (- last gnus-newsgroup-maximum-articles -1))
6816                    (car active)))
6817          first nlast unread)
6818     ;; If none are read, then all are unread.
6819     (if (not read)
6820         (setq first bottom)
6821       ;; If the range of read articles is a single range, then the
6822       ;; first unread article is the article after the last read
6823       ;; article.  Sounds logical, doesn't it?
6824       (if (and (not (listp (cdr read)))
6825                (or (< (car read) bottom)
6826                    (progn (setq read (list read))
6827                           nil)))
6828           (setq first (max bottom (1+ (cdr read))))
6829         ;; `read' is a list of ranges.
6830         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6831                                   (caar read)))
6832                   1)
6833           (setq first bottom))
6834         (while read
6835           (when first
6836             (while (< first nlast)
6837               (setq unread (cons first unread)
6838                     first (1+ first))))
6839           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6840           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6841           (setq read (cdr read)))))
6842     ;; And add the last unread articles.
6843     (while (<= first last)
6844       (setq unread (cons first unread)
6845             first (1+ first)))
6846     ;; Return the list of unread articles.
6847     (delq 0 (nreverse unread))))
6848
6849 (defun gnus-list-of-read-articles (group)
6850   "Return a list of unread, unticked and non-dormant articles."
6851   (let* ((info (gnus-get-info group))
6852          (marked (gnus-info-marks info))
6853          (active (gnus-active group)))
6854     (and info active
6855          (gnus-list-range-difference
6856           (gnus-list-range-difference
6857            (gnus-sorted-complement
6858             (gnus-uncompress-range
6859              (if gnus-newsgroup-maximum-articles
6860                  (cons (max (car active)
6861                             (- (cdr active)
6862                                gnus-newsgroup-maximum-articles
6863                                -1))
6864                        (cdr active))
6865                active))
6866             (gnus-list-of-unread-articles group))
6867            (cdr (assq 'dormant marked)))
6868           (cdr (assq 'tick marked))))))
6869
6870 ;; This function returns a sequence of article numbers based on the
6871 ;; difference between the ranges of read articles in this group and
6872 ;; the range of active articles.
6873 (defun gnus-sequence-of-unread-articles (group)
6874   (let* ((read (gnus-info-read (gnus-get-info group)))
6875          (active (or (gnus-active group) (gnus-activate-group group)))
6876          (last (cdr active))
6877          (bottom (if gnus-newsgroup-maximum-articles
6878                      (max (car active)
6879                           (- last gnus-newsgroup-maximum-articles -1))
6880                    (car active)))
6881          first nlast unread)
6882     ;; If none are read, then all are unread.
6883     (if (not read)
6884         (setq first bottom)
6885       ;; If the range of read articles is a single range, then the
6886       ;; first unread article is the article after the last read
6887       ;; article.  Sounds logical, doesn't it?
6888       (if (and (not (listp (cdr read)))
6889                (or (< (car read) bottom)
6890                    (progn (setq read (list read))
6891                           nil)))
6892           (setq first (max bottom (1+ (cdr read))))
6893         ;; `read' is a list of ranges.
6894         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6895                                   (caar read)))
6896                   1)
6897           (setq first bottom))
6898         (while read
6899           (when first
6900             (push (cons first nlast) unread))
6901           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6902           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6903           (setq read (cdr read)))))
6904     ;; And add the last unread articles.
6905     (cond ((not (and first last))
6906            nil)
6907           ((< first last)
6908            (push (cons first last) unread))
6909           ((= first last)
6910            (push first unread)))
6911     ;; Return the sequence of unread articles.
6912     (delq 0 (nreverse unread))))
6913
6914 ;; Various summary commands
6915
6916 (defun gnus-summary-select-article-buffer ()
6917   "Reconfigure windows to show article buffer."
6918   (interactive)
6919   (if (not (gnus-buffer-live-p gnus-article-buffer))
6920       (error "There is no article buffer for this summary buffer")
6921     (gnus-configure-windows 'article)
6922     (select-window (get-buffer-window gnus-article-buffer))))
6923
6924 (defun gnus-summary-universal-argument (arg)
6925   "Perform any operation on all articles that are process/prefixed."
6926   (interactive "P")
6927   (let ((articles (gnus-summary-work-articles arg))
6928         func article)
6929     (if (eq
6930          (setq
6931           func
6932           (key-binding
6933            (read-key-sequence
6934             (substitute-command-keys
6935              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6936          'undefined)
6937         (gnus-error 1 "Undefined key")
6938       (save-excursion
6939         (while articles
6940           (gnus-summary-goto-subject (setq article (pop articles)))
6941           (let (gnus-newsgroup-processable)
6942             (command-execute func))
6943           (gnus-summary-remove-process-mark article)))))
6944   (gnus-summary-position-point))
6945
6946 (defun gnus-summary-toggle-truncation (&optional arg)
6947   "Toggle truncation of summary lines.
6948 With ARG, turn line truncation on if ARG is positive."
6949   (interactive "P")
6950   (setq truncate-lines
6951         (if (null arg) (not truncate-lines)
6952           (> (prefix-numeric-value arg) 0)))
6953   (redraw-display))
6954
6955 (defun gnus-summary-find-for-reselect ()
6956   "Return the number of an article to stay on across a reselect.
6957 The current article is considered, then following articles, then previous
6958 articles.  An article is sought which is not cancelled and isn't a temporary
6959 insertion from another group.  If there's no such then return a dummy 0."
6960   (let (found)
6961     (dolist (rev '(nil t))
6962       (unless found      ; don't demand the reverse list if we don't need it
6963         (let ((data (gnus-data-find-list
6964                      (gnus-summary-article-number) (gnus-data-list rev))))
6965           (while (and data (not found))
6966             (if (and (< 0 (gnus-data-number (car data)))
6967                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6968                 (setq found (gnus-data-number (car data))))
6969             (setq data (cdr data))))))
6970     (or found 0)))
6971
6972 (defun gnus-summary-reselect-current-group (&optional all rescan)
6973   "Exit and then reselect the current newsgroup.
6974 The prefix argument ALL means to select all articles."
6975   (interactive "P")
6976   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6977     (error "Ephemeral groups can't be reselected"))
6978   (let ((current-subject (gnus-summary-find-for-reselect))
6979         (group gnus-newsgroup-name))
6980     (setq gnus-newsgroup-begin nil)
6981     (gnus-summary-exit nil 'leave-hidden)
6982     ;; We have to adjust the point of group mode buffer because
6983     ;; point was moved to the next unread newsgroup by exiting.
6984     (gnus-summary-jump-to-group group)
6985     (when rescan
6986       (save-excursion
6987         (gnus-group-get-new-news-this-group 1)))
6988     (gnus-group-read-group all t)
6989     (gnus-summary-goto-subject current-subject nil t)))
6990
6991 (defun gnus-summary-rescan-group (&optional all)
6992   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6993   (interactive "P")
6994   (gnus-summary-reselect-current-group all t))
6995
6996 (defun gnus-summary-update-info (&optional non-destructive)
6997   (save-excursion
6998     (let ((group gnus-newsgroup-name))
6999       (when group
7000         (when gnus-newsgroup-kill-headers
7001           (setq gnus-newsgroup-killed
7002                 (gnus-compress-sequence
7003                  (gnus-sorted-union
7004                   (gnus-list-range-intersection
7005                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7006                   gnus-newsgroup-unreads)
7007                  t)))
7008         (unless (listp (cdr gnus-newsgroup-killed))
7009           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7010         (let ((headers gnus-newsgroup-headers))
7011           ;; Set the new ranges of read articles.
7012           (with-current-buffer gnus-group-buffer
7013             (gnus-undo-force-boundary))
7014           (gnus-update-read-articles
7015            group (gnus-sorted-union
7016                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7017           ;; Set the current article marks.
7018           (let ((gnus-newsgroup-scored
7019                  (if (and (not gnus-save-score)
7020                           (not non-destructive))
7021                      nil
7022                    gnus-newsgroup-scored)))
7023             (save-excursion
7024               (gnus-update-marks)))
7025           ;; Do the cross-ref thing.
7026           (when gnus-use-cross-reference
7027             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7028           ;; Do not switch windows but change the buffer to work.
7029           (set-buffer gnus-group-buffer)
7030           (unless (gnus-ephemeral-group-p group)
7031             (gnus-group-update-group group)))))))
7032
7033 (defun gnus-summary-save-newsrc (&optional force)
7034   "Save the current number of read/marked articles in the dribble buffer.
7035 The dribble buffer will then be saved.
7036 If FORCE (the prefix), also save the .newsrc file(s)."
7037   (interactive "P")
7038   (gnus-summary-update-info t)
7039   (if force
7040       (gnus-save-newsrc-file)
7041     (gnus-dribble-save)))
7042
7043 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7044
7045 (defun gnus-summary-exit (&optional temporary leave-hidden)
7046   "Exit reading current newsgroup, and then return to group selection mode.
7047 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7048   (interactive)
7049   (gnus-set-global-variables)
7050   (when (gnus-buffer-live-p gnus-article-buffer)
7051     (with-current-buffer gnus-article-buffer
7052       (mm-destroy-parts gnus-article-mime-handles)
7053       ;; Set it to nil for safety reason.
7054       (setq gnus-article-mime-handle-alist nil)
7055       (setq gnus-article-mime-handles nil)))
7056   (gnus-kill-save-kill-buffer)
7057   (gnus-async-halt-prefetch)
7058   (let* ((group gnus-newsgroup-name)
7059          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7060          (gnus-group-is-exiting-p t)
7061          (mode major-mode)
7062          (group-point nil)
7063          (buf (current-buffer)))
7064     (unless quit-config
7065       ;; Do adaptive scoring, and possibly save score files.
7066       (when gnus-newsgroup-adaptive
7067         (gnus-score-adaptive))
7068       (when gnus-use-scoring
7069         (gnus-score-save)))
7070     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7071     ;; If we have several article buffers, we kill them at exit.
7072     (unless gnus-single-article-buffer
7073       (when (gnus-buffer-live-p gnus-article-buffer)
7074         (with-current-buffer gnus-article-buffer
7075           ;; Don't kill sticky article buffers
7076           (unless (eq major-mode 'gnus-sticky-article-mode)
7077             (gnus-kill-buffer gnus-article-buffer)
7078             (setq gnus-article-current nil))))
7079       (gnus-kill-buffer gnus-original-article-buffer))
7080     (when gnus-use-cache
7081       (gnus-cache-possibly-remove-articles)
7082       (gnus-cache-save-buffers))
7083     (gnus-async-prefetch-remove-group group)
7084     (when gnus-suppress-duplicates
7085       (gnus-dup-enter-articles))
7086     (when gnus-use-trees
7087       (gnus-tree-close group))
7088     (when gnus-use-cache
7089       (gnus-cache-write-active))
7090     ;; Remove entries for this group.
7091     (nnmail-purge-split-history (gnus-group-real-name group))
7092     ;; Make all changes in this group permanent.
7093     (unless quit-config
7094       (gnus-run-hooks 'gnus-exit-group-hook)
7095       (gnus-summary-update-info))
7096     (gnus-close-group group)
7097     ;; Make sure where we were, and go to next newsgroup.
7098     (set-buffer gnus-group-buffer)
7099     (unless quit-config
7100       (gnus-group-jump-to-group group))
7101     (gnus-run-hooks 'gnus-summary-exit-hook)
7102     (unless (or quit-config
7103                 (not gnus-summary-next-group-on-exit)
7104                 ;; If this group has disappeared from the summary
7105                 ;; buffer, don't skip forwards.
7106                 (not (string= group (gnus-group-group-name))))
7107       (gnus-group-next-unread-group 1))
7108     (setq group-point (point))
7109     (if temporary
7110         nil                             ;Nothing to do.
7111       (set-buffer buf)
7112       (if (not gnus-kill-summary-on-exit)
7113           (progn
7114             (gnus-deaden-summary)
7115             (setq mode nil))
7116         ;; We set all buffer-local variables to nil.  It is unclear why
7117         ;; this is needed, but if we don't, buffer-local variables are
7118         ;; not garbage-collected, it seems.  This would the lead to en
7119         ;; ever-growing Emacs.
7120         (gnus-summary-clear-local-variables)
7121         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7122           (gnus-summary-clear-local-variables))
7123         (when (get-buffer gnus-article-buffer)
7124           (bury-buffer gnus-article-buffer))
7125         ;; Return to group mode buffer.
7126         (when (eq mode 'gnus-summary-mode)
7127           (gnus-kill-buffer buf)))
7128       (setq gnus-current-select-method gnus-select-method)
7129       (set-buffer gnus-group-buffer)
7130       (if quit-config
7131           (gnus-handle-ephemeral-exit quit-config)
7132         (goto-char group-point)
7133         ;; If gnus-group-buffer is already displayed, make sure we also move
7134         ;; the cursor in the window that displays it.
7135         (let ((win (get-buffer-window (current-buffer) 0)))
7136           (if win (set-window-point win (point))))
7137         (unless leave-hidden
7138           (gnus-configure-windows 'group 'force)))
7139       ;; Clear the current group name.
7140       (unless quit-config
7141         (setq gnus-newsgroup-name nil)))))
7142
7143 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7144 (defun gnus-summary-exit-no-update (&optional no-questions)
7145   "Quit reading current newsgroup without updating read article info."
7146   (interactive)
7147   (let* ((group gnus-newsgroup-name)
7148          (gnus-group-is-exiting-p t)
7149          (gnus-group-is-exiting-without-update-p t)
7150          (quit-config (gnus-group-quit-config group)))
7151     (when (or no-questions
7152               gnus-expert-user
7153               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7154       (gnus-async-halt-prefetch)
7155       (run-hooks 'gnus-summary-prepare-exit-hook)
7156       (when (gnus-buffer-live-p gnus-article-buffer)
7157         (with-current-buffer gnus-article-buffer
7158           (mm-destroy-parts gnus-article-mime-handles)
7159           ;; Set it to nil for safety reason.
7160           (setq gnus-article-mime-handle-alist nil)
7161           (setq gnus-article-mime-handles nil)))
7162       ;; If we have several article buffers, we kill them at exit.
7163       (unless gnus-single-article-buffer
7164         (gnus-kill-buffer gnus-article-buffer)
7165         (gnus-kill-buffer gnus-original-article-buffer)
7166         (setq gnus-article-current nil))
7167       (if (not gnus-kill-summary-on-exit)
7168           (gnus-deaden-summary)
7169         (gnus-close-group group)
7170         (gnus-summary-clear-local-variables)
7171         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7172           (gnus-summary-clear-local-variables))
7173         (gnus-kill-buffer gnus-summary-buffer))
7174       (unless gnus-single-article-buffer
7175         (setq gnus-article-current nil))
7176       (when gnus-use-trees
7177         (gnus-tree-close group))
7178       (gnus-async-prefetch-remove-group group)
7179       (when (get-buffer gnus-article-buffer)
7180         (bury-buffer gnus-article-buffer))
7181       ;; Return to the group buffer.
7182       (gnus-configure-windows 'group 'force)
7183       ;; Clear the current group name.
7184       (setq gnus-newsgroup-name nil)
7185       (unless (gnus-ephemeral-group-p group)
7186         (gnus-group-update-group group))
7187       (when (equal (gnus-group-group-name) group)
7188         (gnus-group-next-unread-group 1))
7189       (when quit-config
7190         (gnus-handle-ephemeral-exit quit-config)))))
7191
7192 (defun gnus-handle-ephemeral-exit (quit-config)
7193   "Handle movement when leaving an ephemeral group.
7194 The state which existed when entering the ephemeral is reset."
7195   (if (not (buffer-name (car quit-config)))
7196       (gnus-configure-windows 'group 'force)
7197     (set-buffer (car quit-config))
7198     (cond ((eq major-mode 'gnus-summary-mode)
7199            (gnus-set-global-variables))
7200           ((eq major-mode 'gnus-article-mode)
7201            (save-current-buffer
7202              ;; The `gnus-summary-buffer' variable may point
7203              ;; to the old summary buffer when using a single
7204              ;; article buffer.
7205              (unless (gnus-buffer-live-p gnus-summary-buffer)
7206                (set-buffer gnus-group-buffer))
7207              (set-buffer gnus-summary-buffer)
7208              (gnus-set-global-variables))))
7209     (if (or (eq (cdr quit-config) 'article)
7210             (eq (cdr quit-config) 'pick))
7211         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7212             (gnus-configure-windows 'pick 'force)
7213           (gnus-configure-windows (cdr quit-config) 'force))
7214       (gnus-configure-windows (cdr quit-config) 'force))
7215     (when (eq major-mode 'gnus-summary-mode)
7216       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7217                                                      next-unread-noselect))
7218           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7219                                   'next-noselect)
7220                               (gnus-summary-next-subject 1 nil t))
7221                              ((eq gnus-auto-select-on-ephemeral-exit
7222                                   'next-unread-noselect)
7223                               (gnus-summary-next-subject 1 t t))))
7224             ;; Hide the article buffer which displays the article different
7225             ;; from the one that the cursor points to in the summary buffer.
7226             (gnus-configure-windows 'summary 'force))
7227         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7228                (gnus-summary-next-subject 1))
7229               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7230                (gnus-summary-next-subject 1 t))))
7231       (gnus-summary-recenter)
7232       (gnus-summary-position-point))))
7233
7234 ;;; Dead summaries.
7235
7236 (defvar gnus-dead-summary-mode-map
7237   (let ((map (make-keymap)))
7238     (suppress-keymap map)
7239     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7240     (dolist (key '("\C-d" "\r" "\177" [delete]))
7241       (define-key map key 'gnus-summary-wake-up-the-dead))
7242     (dolist (key '("q" "Q"))
7243       (define-key map key 'bury-buffer))
7244     map))
7245
7246 (define-minor-mode gnus-dead-summary-mode
7247   "Minor mode for Gnus summary buffers."
7248   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7249   (unless (derived-mode-p 'gnus-summary-mode)
7250     (setq gnus-dead-summary-mode nil)))
7251
7252 (defun gnus-deaden-summary ()
7253   "Make the current summary buffer into a dead summary buffer."
7254   ;; Kill any previous dead summary buffer.
7255   (when (and gnus-dead-summary
7256              (buffer-name gnus-dead-summary))
7257     (with-current-buffer gnus-dead-summary
7258       (when gnus-dead-summary-mode
7259         (kill-buffer (current-buffer)))))
7260   ;; Make this the current dead summary.
7261   (setq gnus-dead-summary (current-buffer))
7262   (gnus-dead-summary-mode 1)
7263   (let ((name (buffer-name)))
7264     (when (string-match "Summary" name)
7265       (rename-buffer
7266        (concat (substring name 0 (match-beginning 0)) "Dead "
7267                (substring name (match-beginning 0)))
7268        t)
7269       (bury-buffer))))
7270
7271 (defun gnus-kill-or-deaden-summary (buffer)
7272   "Kill or deaden the summary BUFFER."
7273   (save-excursion
7274     (when (and (buffer-name buffer)
7275                (not gnus-single-article-buffer))
7276       (with-current-buffer buffer
7277         (gnus-kill-buffer gnus-article-buffer)
7278         (gnus-kill-buffer gnus-original-article-buffer)))
7279     (cond
7280      ;; Kill the buffer.
7281      (gnus-kill-summary-on-exit
7282       (when (and gnus-use-trees
7283                  (gnus-buffer-exists-p buffer))
7284         (with-current-buffer buffer
7285           (gnus-tree-close gnus-newsgroup-name)))
7286       (gnus-kill-buffer buffer))
7287      ;; Deaden the buffer.
7288      ((gnus-buffer-exists-p buffer)
7289       (with-current-buffer buffer
7290         (gnus-deaden-summary))))))
7291
7292 (defun gnus-summary-wake-up-the-dead (&rest args)
7293   "Wake up the dead summary buffer."
7294   (interactive)
7295   (gnus-dead-summary-mode -1)
7296   (let ((name (buffer-name)))
7297     (when (string-match "Dead " name)
7298       (rename-buffer
7299        (concat (substring name 0 (match-beginning 0))
7300                (substring name (match-end 0)))
7301        t)))
7302   (gnus-message 3 "This dead summary is now alive again"))
7303
7304 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7305 (defun gnus-summary-fetch-faq (&optional faq-dir)
7306   "Fetch the FAQ for the current group.
7307 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7308 in."
7309   (interactive
7310    (list
7311     (when current-prefix-arg
7312       (completing-read
7313        "FAQ dir: " (and (listp gnus-group-faq-directory)
7314                         (mapcar 'list
7315                                 gnus-group-faq-directory))))))
7316   (let (gnus-faq-buffer)
7317     (when (setq gnus-faq-buffer
7318                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7319       (gnus-configure-windows 'summary-faq))))
7320
7321 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7322 (defun gnus-summary-describe-group (&optional force)
7323   "Describe the current newsgroup."
7324   (interactive "P")
7325   (gnus-group-describe-group force gnus-newsgroup-name))
7326
7327 (defun gnus-summary-describe-briefly ()
7328   "Describe summary mode commands briefly."
7329   (interactive)
7330   (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")))
7331
7332 ;; Walking around group mode buffer from summary mode.
7333
7334 (defun gnus-summary-next-group (&optional no-article target-group backward)
7335   "Exit current newsgroup and then select next unread newsgroup.
7336 If prefix argument NO-ARTICLE is non-nil, no article is selected
7337 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7338 previous group instead."
7339   (interactive "P")
7340   ;; Stop pre-fetching.
7341   (gnus-async-halt-prefetch)
7342   (let ((current-group gnus-newsgroup-name)
7343         (current-buffer (current-buffer))
7344         entered)
7345     ;; First we semi-exit this group to update Xrefs and all variables.
7346     ;; We can't do a real exit, because the window conf must remain
7347     ;; the same in case the user is prompted for info, and we don't
7348     ;; want the window conf to change before that...
7349     (gnus-summary-exit t)
7350     (while (not entered)
7351       ;; Then we find what group we are supposed to enter.
7352       (set-buffer gnus-group-buffer)
7353       (gnus-group-jump-to-group current-group)
7354       (setq target-group
7355             (or target-group
7356                 (if (eq gnus-keep-same-level 'best)
7357                     (gnus-summary-best-group gnus-newsgroup-name)
7358                   (gnus-summary-search-group backward gnus-keep-same-level))))
7359       (if (not target-group)
7360           ;; There are no further groups, so we return to the group
7361           ;; buffer.
7362           (progn
7363             (gnus-message 5 "Returning to the group buffer")
7364             (setq entered t)
7365             (when (gnus-buffer-live-p current-buffer)
7366               (set-buffer current-buffer)
7367               (gnus-summary-exit))
7368             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7369         ;; We try to enter the target group.
7370         (gnus-group-jump-to-group target-group)
7371         (let ((unreads (gnus-group-group-unread)))
7372           (if (and (or (eq t unreads)
7373                        (and unreads (not (zerop unreads))))
7374                    (gnus-summary-read-group
7375                     target-group nil no-article
7376                     (and (buffer-name current-buffer) current-buffer)
7377                     nil backward))
7378               (setq entered t)
7379             (setq current-group target-group
7380                   target-group nil)))))))
7381
7382 (defun gnus-summary-prev-group (&optional no-article)
7383   "Exit current newsgroup and then select previous unread newsgroup.
7384 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7385   (interactive "P")
7386   (gnus-summary-next-group no-article nil t))
7387
7388 ;; Walking around summary lines.
7389
7390 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7391   "Go to the first subject satisfying any non-nil constraint.
7392 If UNREAD is non-nil, the article should be unread.
7393 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7394 If UNSEEN is non-nil, the article should be unseen.
7395 Returns the article selected or nil if there are no matching articles."
7396   (interactive "P")
7397   (cond
7398    ;; Empty summary.
7399    ((null gnus-newsgroup-data)
7400     (gnus-message 3 "No articles in the group")
7401     nil)
7402    ;; Pick the first article.
7403    ((not (or unread undownloaded unseen))
7404     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7405     (gnus-data-number (car gnus-newsgroup-data)))
7406    ;; Find the first unread article.
7407    (t
7408     (let ((data gnus-newsgroup-data))
7409       (while (and data
7410                   (let ((num (gnus-data-number (car data))))
7411                     (or (memq num gnus-newsgroup-unfetched)
7412                         (not (or (and unread
7413                                       (memq num gnus-newsgroup-unreads))
7414                                  (and undownloaded
7415                                       (memq num gnus-newsgroup-undownloaded))
7416                                  (and unseen
7417                                       (memq num gnus-newsgroup-unseen)))))))
7418         (setq data (cdr data)))
7419       (prog1
7420           (if data
7421               (progn
7422                 (goto-char (gnus-data-pos (car data)))
7423                 (gnus-data-number (car data)))
7424             (gnus-message 3 "No more%s articles"
7425                           (let* ((r (when unread " unread"))
7426                                  (d (when undownloaded " undownloaded"))
7427                                  (s (when unseen " unseen"))
7428                                  (l (delq nil (list r d s))))
7429                             (cond ((= 3 (length l))
7430                                    (concat r "," d ", or" s))
7431                                   ((= 2 (length l))
7432                                    (concat (car l) ", or" (cadr l)))
7433                                   ((= 1 (length l))
7434                                    (car l))
7435                                   (t
7436                                    ""))))
7437             nil
7438             )
7439         (gnus-summary-position-point))))))
7440
7441 (defun gnus-summary-next-subject (n &optional unread dont-display)
7442   "Go to next N'th summary line.
7443 If N is negative, go to the previous N'th subject line.
7444 If UNREAD is non-nil, only unread articles are selected.
7445 The difference between N and the actual number of steps taken is
7446 returned."
7447   (interactive "p")
7448   (let ((backward (< n 0))
7449         (n (abs n)))
7450     (while (and (> n 0)
7451                 (if backward
7452                     (gnus-summary-find-prev unread)
7453                   (gnus-summary-find-next unread)))
7454       (unless (zerop (setq n (1- n)))
7455         (gnus-summary-show-thread)))
7456     (when (/= 0 n)
7457       (gnus-message 7 "No more%s articles"
7458                     (if unread " unread" "")))
7459     (unless dont-display
7460       (gnus-summary-recenter)
7461       (gnus-summary-position-point))
7462     n))
7463
7464 (defun gnus-summary-next-unread-subject (n)
7465   "Go to next N'th unread summary line."
7466   (interactive "p")
7467   (gnus-summary-next-subject n t))
7468
7469 (defun gnus-summary-prev-subject (n &optional unread)
7470   "Go to previous N'th summary line.
7471 If optional argument UNREAD is non-nil, only unread article is selected."
7472   (interactive "p")
7473   (gnus-summary-next-subject (- n) unread))
7474
7475 (defun gnus-summary-prev-unread-subject (n)
7476   "Go to previous N'th unread summary line."
7477   (interactive "p")
7478   (gnus-summary-next-subject (- n) t))
7479
7480 (defun gnus-summary-goto-subjects (articles)
7481   "Insert the subject header for ARTICLES in the current buffer."
7482   (save-excursion
7483     (dolist (article articles)
7484       (gnus-summary-goto-subject article t)))
7485   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7486   (gnus-summary-position-point))
7487
7488 (defun gnus-summary-goto-subject (article &optional force silent)
7489   "Go to the subject line of ARTICLE.
7490 If FORCE, also allow jumping to articles not currently shown."
7491   (interactive "nArticle number: ")
7492   (unless (numberp article)
7493     (error "Article %s is not a number" article))
7494   (let ((b (point))
7495         (data (gnus-data-find article)))
7496     ;; We read in the article if we have to.
7497     (and (not data)
7498          force
7499          (gnus-summary-insert-subject
7500           article
7501           (if (or (numberp force) (vectorp force)) force)
7502           t)
7503          (setq data (gnus-data-find article)))
7504     (goto-char b)
7505     (if (not data)
7506         (progn
7507           (unless silent
7508             (gnus-message 3 "Can't find article %d" article))
7509           nil)
7510       (let ((pt (gnus-data-pos data)))
7511         (goto-char pt)
7512         (gnus-summary-set-article-display-arrow pt))
7513       (gnus-summary-position-point)
7514       article)))
7515
7516 ;; Walking around summary lines with displaying articles.
7517
7518 (defun gnus-summary-expand-window (&optional arg)
7519   "Make the summary buffer take up the entire Emacs frame.
7520 Given a prefix, will force an `article' buffer configuration."
7521   (interactive "P")
7522   (if arg
7523       (gnus-configure-windows 'article 'force)
7524     (gnus-configure-windows 'summary 'force)))
7525
7526 (defun gnus-summary-display-article (article &optional all-header)
7527   "Display ARTICLE in article buffer."
7528   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7529                (with-current-buffer gnus-article-buffer
7530                  (eq major-mode 'gnus-article-mode)))
7531     (gnus-article-setup-buffer))
7532   (gnus-set-global-variables)
7533   (with-current-buffer gnus-article-buffer
7534     (setq gnus-article-charset gnus-newsgroup-charset)
7535     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7536     (mm-enable-multibyte))
7537   (if (null article)
7538       nil
7539     (prog1
7540         (if gnus-summary-display-article-function
7541             (funcall gnus-summary-display-article-function article all-header)
7542           (gnus-article-prepare article all-header))
7543       (gnus-run-hooks 'gnus-select-article-hook)
7544       (when (and gnus-current-article
7545                  (not (zerop gnus-current-article)))
7546         (gnus-summary-goto-subject gnus-current-article))
7547       (gnus-summary-recenter)
7548       (when (and gnus-use-trees gnus-show-threads)
7549         (gnus-possibly-generate-tree article)
7550         (gnus-highlight-selected-tree article))
7551       ;; Successfully display article.
7552       (gnus-article-set-window-start
7553        (cdr (assq article gnus-newsgroup-bookmarks))))))
7554
7555 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7556   "Select the current article.
7557 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7558 non-nil, the article will be re-fetched even if it already present in
7559 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7560 be displayed."
7561   ;; Make sure we are in the summary buffer to work around bbdb bug.
7562   (unless (eq major-mode 'gnus-summary-mode)
7563     (set-buffer gnus-summary-buffer))
7564   (let ((article (or article (gnus-summary-article-number)))
7565         (all-headers (not (not all-headers))) ;Must be t or nil.
7566         gnus-summary-display-article-function)
7567     (and (not pseudo)
7568          (gnus-summary-article-pseudo-p article)
7569          (error "This is a pseudo-article"))
7570     (with-current-buffer gnus-summary-buffer
7571       (if (or (and gnus-single-article-buffer
7572                    (or (null gnus-current-article)
7573                        (null gnus-article-current)
7574                        (null (get-buffer gnus-article-buffer))
7575                        (not (eq article (cdr gnus-article-current)))
7576                        (not (equal (car gnus-article-current)
7577                                    gnus-newsgroup-name))))
7578               (and (not gnus-single-article-buffer)
7579                    (or (null gnus-current-article)
7580                        (not (eq gnus-current-article article))))
7581               force)
7582           ;; The requested article is different from the current article.
7583           (progn
7584             (gnus-summary-display-article article all-headers)
7585             (when (gnus-buffer-live-p gnus-article-buffer)
7586               (with-current-buffer gnus-article-buffer
7587                 (if (not gnus-article-decoded-p) ;; a local variable
7588                     (mm-disable-multibyte))))
7589             (gnus-article-set-window-start
7590              (cdr (assq article gnus-newsgroup-bookmarks)))
7591             article)
7592         'old))))
7593
7594 (defun gnus-summary-force-verify-and-decrypt ()
7595   "Display buttons for signed/encrypted parts and verify/decrypt them."
7596   (interactive)
7597   (let ((mm-verify-option 'known)
7598         (mm-decrypt-option 'known)
7599         (gnus-article-emulate-mime t)
7600         (gnus-buttonized-mime-types (append (list "multipart/signed"
7601                                                   "multipart/encrypted")
7602                                             gnus-buttonized-mime-types)))
7603     (gnus-summary-select-article nil 'force)))
7604
7605 (defun gnus-summary-set-current-mark (&optional current-mark)
7606   "Obsolete function."
7607   nil)
7608
7609 (defun gnus-summary-next-article (&optional unread subject backward push)
7610   "Select the next article.
7611 If UNREAD, only unread articles are selected.
7612 If SUBJECT, only articles with SUBJECT are selected.
7613 If BACKWARD, the previous article is selected instead of the next."
7614   (interactive "P")
7615   ;; Make sure we are in the summary buffer.
7616   (unless (eq major-mode 'gnus-summary-mode)
7617     (set-buffer gnus-summary-buffer))
7618   (cond
7619    ;; Is there such an article?
7620    ((and (gnus-summary-search-forward unread subject backward)
7621          (or (gnus-summary-display-article (gnus-summary-article-number))
7622              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7623     (gnus-summary-position-point))
7624    ;; If not, we try the first unread, if that is wanted.
7625    ((and subject
7626          gnus-auto-select-same
7627          (gnus-summary-first-unread-article))
7628     (gnus-summary-position-point)
7629     (gnus-message 6 "Wrapped"))
7630    ;; Try to get next/previous article not displayed in this group.
7631    ((and gnus-auto-extend-newsgroup
7632          (not unread) (not subject))
7633     (gnus-summary-goto-article
7634      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7635      nil (count-lines (point-min) (point))))
7636    ;; Go to next/previous group.
7637    (t
7638     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7639       (gnus-summary-jump-to-group gnus-newsgroup-name))
7640     (let ((cmd (if (featurep 'xemacs)
7641                    last-command-char
7642                  last-command-event))
7643           (point
7644            (with-current-buffer gnus-group-buffer
7645              (point)))
7646           (group
7647            (if (eq gnus-keep-same-level 'best)
7648                (gnus-summary-best-group gnus-newsgroup-name)
7649              (gnus-summary-search-group backward gnus-keep-same-level))))
7650       ;; For some reason, the group window gets selected.  We change
7651       ;; it back.
7652       (select-window (get-buffer-window (current-buffer)))
7653       ;; Select next unread newsgroup automagically.
7654       (cond
7655        ((or (not gnus-auto-select-next)
7656             (not cmd))
7657         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7658        ((or (eq gnus-auto-select-next 'quietly)
7659             (and (eq gnus-auto-select-next 'slightly-quietly)
7660                  push)
7661             (and (eq gnus-auto-select-next 'almost-quietly)
7662                  (gnus-summary-last-article-p)))
7663         ;; Select quietly.
7664         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7665             (gnus-summary-exit)
7666           (gnus-message 7 "No more%s articles (%s)..."
7667                         (if unread " unread" "")
7668                         (if group (concat "selecting " group)
7669                           "exiting"))
7670           (gnus-summary-next-group nil group backward)))
7671        (t
7672         (when (gnus-key-press-event-p last-input-event)
7673           (gnus-summary-walk-group-buffer
7674            gnus-newsgroup-name cmd unread backward point))))))))
7675
7676 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7677   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7678                       (?\C-p (gnus-group-prev-unread-group 1))))
7679         (cursor-in-echo-area t)
7680         keve key group ended prompt)
7681     (with-current-buffer gnus-group-buffer
7682       (goto-char start)
7683       (setq group
7684             (if (eq gnus-keep-same-level 'best)
7685                 (gnus-summary-best-group gnus-newsgroup-name)
7686               (gnus-summary-search-group backward gnus-keep-same-level))))
7687     (while (not ended)
7688       (setq prompt
7689             (format
7690              "No more%s articles%s " (if unread " unread" "")
7691              (if (and group
7692                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7693                  (format " (Type %s for %s [%s])"
7694                          (single-key-description cmd)
7695                          (gnus-group-decoded-name group)
7696                          (gnus-group-unread group))
7697                (format " (Type %s to exit %s)"
7698                        (single-key-description cmd)
7699                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7700       ;; Confirm auto selection.
7701       (setq key (car (setq keve (gnus-read-event-char prompt)))
7702             ended t)
7703       (cond
7704        ((assq key keystrokes)
7705         (let ((obuf (current-buffer)))
7706           (switch-to-buffer gnus-group-buffer)
7707           (when group
7708             (gnus-group-jump-to-group group))
7709           (eval (cadr (assq key keystrokes)))
7710           (setq group (gnus-group-group-name))
7711           (switch-to-buffer obuf))
7712         (setq ended nil))
7713        ((equal key cmd)
7714         (if (or (not group)
7715                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7716             (gnus-summary-exit)
7717           (gnus-summary-next-group nil group backward)))
7718        (t
7719         (push (cdr keve) unread-command-events))))))
7720
7721 (defun gnus-summary-next-unread-article ()
7722   "Select unread article after current one."
7723   (interactive)
7724   (gnus-summary-next-article
7725    (or (not (eq gnus-summary-goto-unread 'never))
7726        (gnus-summary-last-article-p (gnus-summary-article-number)))
7727    (and gnus-auto-select-same
7728         (gnus-summary-article-subject))))
7729
7730 (defun gnus-summary-prev-article (&optional unread subject)
7731   "Select the article before the current one.
7732 If UNREAD is non-nil, only unread articles are selected."
7733   (interactive "P")
7734   (gnus-summary-next-article unread subject t))
7735
7736 (defun gnus-summary-prev-unread-article ()
7737   "Select unread article before current one."
7738   (interactive)
7739   (gnus-summary-prev-article
7740    (or (not (eq gnus-summary-goto-unread 'never))
7741        (gnus-summary-first-article-p (gnus-summary-article-number)))
7742    (and gnus-auto-select-same
7743         (gnus-summary-article-subject))))
7744
7745 (defun gnus-summary-next-page (&optional lines circular stop)
7746   "Show next page of the selected article.
7747 If at the end of the current article, select the next article.
7748 LINES says how many lines should be scrolled up.
7749
7750 If CIRCULAR is non-nil, go to the start of the article instead of
7751 selecting the next article when reaching the end of the current
7752 article.
7753
7754 If STOP is non-nil, just stop when reaching the end of the message.
7755
7756 Also see the variable `gnus-article-skip-boring'."
7757   (interactive "P")
7758   (setq gnus-summary-buffer (current-buffer))
7759   (gnus-set-global-variables)
7760   (let ((article (gnus-summary-article-number))
7761         (article-window (get-buffer-window gnus-article-buffer t))
7762         endp)
7763     ;; If the buffer is empty, we have no article.
7764     (unless article
7765       (error "No article to select"))
7766     (gnus-configure-windows 'article)
7767     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7768         (if (and (eq gnus-summary-goto-unread 'never)
7769                  (not (gnus-summary-last-article-p article)))
7770             (gnus-summary-next-article)
7771           (gnus-summary-next-unread-article))
7772       (if (or (null gnus-current-article)
7773               (null gnus-article-current)
7774               (/= article (cdr gnus-article-current))
7775               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7776           ;; Selected subject is different from current article's.
7777           (gnus-summary-display-article article)
7778         (when article-window
7779           (gnus-eval-in-buffer-window gnus-article-buffer
7780             (setq endp (or (gnus-article-next-page lines)
7781                            (gnus-article-only-boring-p))))
7782           (when endp
7783             (cond ((or stop gnus-summary-stop-at-end-of-message)
7784                    (gnus-message 3 "End of message"))
7785                   (circular
7786                    (gnus-summary-beginning-of-article))
7787                   (lines
7788                    (gnus-message 3 "End of message"))
7789                   ((null lines)
7790                    (if (and (eq gnus-summary-goto-unread 'never)
7791                             (not (gnus-summary-last-article-p article)))
7792                        (gnus-summary-next-article)
7793                      (gnus-summary-next-unread-article))))))))
7794     (gnus-summary-recenter)
7795     (gnus-summary-position-point)))
7796
7797 (defun gnus-summary-prev-page (&optional lines move)
7798   "Show previous page of selected article.
7799 Argument LINES specifies lines to be scrolled down.
7800 If MOVE, move to the previous unread article if point is at
7801 the beginning of the buffer."
7802   (interactive "P")
7803   (let ((article (gnus-summary-article-number))
7804         (article-window (get-buffer-window gnus-article-buffer t))
7805         endp)
7806     (gnus-configure-windows 'article)
7807     (if (or (null gnus-current-article)
7808             (null gnus-article-current)
7809             (/= article (cdr gnus-article-current))
7810             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7811         ;; Selected subject is different from current article's.
7812         (gnus-summary-display-article article)
7813       (gnus-summary-recenter)
7814       (when article-window
7815         (gnus-eval-in-buffer-window gnus-article-buffer
7816           (setq endp (gnus-article-prev-page lines)))
7817         (when (and move endp)
7818           (cond (lines
7819                  (gnus-message 3 "Beginning of message"))
7820                 ((null lines)
7821                  (if (and (eq gnus-summary-goto-unread 'never)
7822                           (not (gnus-summary-first-article-p article)))
7823                      (gnus-summary-prev-article)
7824                    (gnus-summary-prev-unread-article))))))))
7825   (gnus-summary-position-point))
7826
7827 (defun gnus-summary-prev-page-or-article (&optional lines)
7828   "Show previous page of selected article.
7829 Argument LINES specifies lines to be scrolled down.
7830 If at the beginning of the article, go to the next article."
7831   (interactive "P")
7832   (gnus-summary-prev-page lines t))
7833
7834 (defun gnus-summary-scroll-up (lines)
7835   "Scroll up (or down) one line current article.
7836 Argument LINES specifies lines to be scrolled up (or down if negative)."
7837   (interactive "p")
7838   (gnus-configure-windows 'article)
7839   (gnus-summary-show-thread)
7840   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7841     (gnus-eval-in-buffer-window gnus-article-buffer
7842       (cond ((> lines 0)
7843              (when (gnus-article-next-page lines)
7844                (gnus-message 3 "End of message")))
7845             ((< lines 0)
7846              (gnus-article-prev-page (- lines))))))
7847   (gnus-summary-recenter)
7848   (gnus-summary-position-point))
7849
7850 (defun gnus-summary-scroll-down (lines)
7851   "Scroll down (or up) one line current article.
7852 Argument LINES specifies lines to be scrolled down (or up if negative)."
7853   (interactive "p")
7854   (gnus-summary-scroll-up (- lines)))
7855
7856 (defun gnus-summary-next-same-subject ()
7857   "Select next article which has the same subject as current one."
7858   (interactive)
7859   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7860
7861 (defun gnus-summary-prev-same-subject ()
7862   "Select previous article which has the same subject as current one."
7863   (interactive)
7864   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7865
7866 (defun gnus-summary-next-unread-same-subject ()
7867   "Select next unread article which has the same subject as current one."
7868   (interactive)
7869   (gnus-summary-next-article t (gnus-summary-article-subject)))
7870
7871 (defun gnus-summary-prev-unread-same-subject ()
7872   "Select previous unread article which has the same subject as current one."
7873   (interactive)
7874   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7875
7876 (defun gnus-summary-first-unread-article ()
7877   "Select the first unread article.
7878 Return nil if there are no unread articles."
7879   (interactive)
7880   (prog1
7881       (when (gnus-summary-first-subject t)
7882         (gnus-summary-show-thread)
7883         (gnus-summary-first-subject t)
7884         (gnus-summary-display-article (gnus-summary-article-number)))
7885     (gnus-summary-position-point)))
7886
7887 (defun gnus-summary-first-unread-subject ()
7888   "Place the point on the subject line of the first unread article.
7889 Return nil if there are no unread articles."
7890   (interactive)
7891   (prog1
7892       (when (gnus-summary-first-subject t)
7893         (gnus-summary-show-thread)
7894         (gnus-summary-first-subject t))
7895     (gnus-summary-position-point)))
7896
7897 (defun gnus-summary-first-unseen-subject ()
7898   "Place the point on the subject line of the first unseen article.
7899 Return nil if there are no unseen articles."
7900   (interactive)
7901   (prog1
7902       (when (gnus-summary-first-subject nil nil t)
7903         (gnus-summary-show-thread)
7904         (gnus-summary-first-subject nil nil t))
7905     (gnus-summary-position-point)))
7906
7907 (defun gnus-summary-first-unseen-or-unread-subject ()
7908   "Place the point on the subject line of the first unseen article or,
7909 if all article have been seen, on the subject line of the first unread
7910 article."
7911   (interactive)
7912   (prog1
7913       (unless (when (gnus-summary-first-subject nil nil t)
7914                 (gnus-summary-show-thread)
7915                 (gnus-summary-first-subject nil nil t))
7916         (when (gnus-summary-first-subject t)
7917           (gnus-summary-show-thread)
7918           (gnus-summary-first-subject t)))
7919     (gnus-summary-position-point)))
7920
7921 (defun gnus-summary-first-article ()
7922   "Select the first article.
7923 Return nil if there are no articles."
7924   (interactive)
7925   (prog1
7926       (when (gnus-summary-first-subject)
7927         (gnus-summary-show-thread)
7928         (gnus-summary-first-subject)
7929         (gnus-summary-display-article (gnus-summary-article-number)))
7930     (gnus-summary-position-point)))
7931
7932 (defun gnus-summary-best-unread-article (&optional arg)
7933   "Select the unread article with the highest score.
7934 If given a prefix argument, select the next unread article that has a
7935 score higher than the default score."
7936   (interactive "P")
7937   (let ((article (if arg
7938                      (gnus-summary-better-unread-subject)
7939                    (gnus-summary-best-unread-subject))))
7940     (if article
7941         (gnus-summary-goto-article article)
7942       (error "No unread articles"))))
7943
7944 (defun gnus-summary-best-unread-subject ()
7945   "Select the unread subject with the highest score."
7946   (interactive)
7947   (let ((best -1000000)
7948         (data gnus-newsgroup-data)
7949         article score)
7950     (while data
7951       (and (gnus-data-unread-p (car data))
7952            (> (setq score
7953                     (gnus-summary-article-score (gnus-data-number (car data))))
7954               best)
7955            (setq best score
7956                  article (gnus-data-number (car data))))
7957       (setq data (cdr data)))
7958     (when article
7959       (gnus-summary-goto-subject article))
7960     (gnus-summary-position-point)
7961     article))
7962
7963 (defun gnus-summary-better-unread-subject ()
7964   "Select the first unread subject that has a score over the default score."
7965   (interactive)
7966   (let ((data gnus-newsgroup-data)
7967         article score)
7968     (while (and (setq article (gnus-data-number (car data)))
7969                 (or (gnus-data-read-p (car data))
7970                     (not (> (gnus-summary-article-score article)
7971                             gnus-summary-default-score))))
7972       (setq data (cdr data)))
7973     (when article
7974       (gnus-summary-goto-subject article))
7975     (gnus-summary-position-point)
7976     article))
7977
7978 (defun gnus-summary-last-subject ()
7979   "Go to the last displayed subject line in the group."
7980   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7981     (when article
7982       (gnus-summary-goto-subject article))))
7983
7984 (defun gnus-summary-goto-article (article &optional all-headers force)
7985   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7986 If ALL-HEADERS is non-nil, no header lines are hidden.
7987 If FORCE, go to the article even if it isn't displayed.  If FORCE
7988 is a number, it is the line the article is to be displayed on."
7989   (interactive
7990    (list
7991     (completing-read
7992      "Article number or Message-ID: "
7993      (mapcar (lambda (number) (list (int-to-string number)))
7994              gnus-newsgroup-limit))
7995     current-prefix-arg
7996     t))
7997   (prog1
7998       (if (and (stringp article)
7999                (string-match "@\\|%40" article))
8000           (gnus-summary-refer-article article)
8001         (when (stringp article)
8002           (setq article (string-to-number article)))
8003         (if (gnus-summary-goto-subject article force)
8004             (gnus-summary-display-article article all-headers)
8005           (gnus-message 4 "Couldn't go to article %s" article) nil))
8006     (gnus-summary-position-point)))
8007
8008 (defun gnus-summary-goto-last-article ()
8009   "Go to the previously read article."
8010   (interactive)
8011   (prog1
8012       (when gnus-last-article
8013         (gnus-summary-goto-article gnus-last-article nil t))
8014     (gnus-summary-position-point)))
8015
8016 (defun gnus-summary-pop-article (number)
8017   "Pop one article off the history and go to the previous.
8018 NUMBER articles will be popped off."
8019   (interactive "p")
8020   (let (to)
8021     (setq gnus-newsgroup-history
8022           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8023     (if to
8024         (gnus-summary-goto-article (car to) nil t)
8025       (error "Article history empty")))
8026   (gnus-summary-position-point))
8027
8028 ;; Summary commands and functions for limiting the summary buffer.
8029
8030 (defun gnus-summary-limit-to-articles (n)
8031   "Limit the summary buffer to the next N articles.
8032 If not given a prefix, use the process marked articles instead."
8033   (interactive "P")
8034   (prog1
8035       (let ((articles (gnus-summary-work-articles n)))
8036         (setq gnus-newsgroup-processable nil)
8037         (gnus-summary-limit articles))
8038     (gnus-summary-position-point)))
8039
8040 (defun gnus-summary-pop-limit (&optional total)
8041   "Restore the previous limit.
8042 If given a prefix, remove all limits."
8043   (interactive "P")
8044   (when total
8045     (setq gnus-newsgroup-limits
8046           (list (mapcar (lambda (h) (mail-header-number h))
8047                         gnus-newsgroup-headers))))
8048   (unless gnus-newsgroup-limits
8049     (error "No limit to pop"))
8050   (prog1
8051       (gnus-summary-limit nil 'pop)
8052     (gnus-summary-position-point)))
8053
8054 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8055   "Limit the summary buffer to articles that have subjects that match a regexp.
8056 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8057   (interactive
8058    (list (read-string (if current-prefix-arg
8059                           "Exclude subject (regexp): "
8060                         "Limit to subject (regexp): "))
8061          nil current-prefix-arg))
8062   (unless header
8063     (setq header "subject"))
8064   (when (not (equal "" subject))
8065     (prog1
8066         (let ((articles (gnus-summary-find-matching
8067                          (or header "subject") subject 'all nil nil
8068                          not-matching)))
8069           (unless articles
8070             (error "Found no matches for \"%s\"" subject))
8071           (gnus-summary-limit articles))
8072       (gnus-summary-position-point))))
8073
8074 (defun gnus-summary-limit-to-author (from &optional not-matching)
8075   "Limit the summary buffer to articles that have authors that match a regexp.
8076 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8077   (interactive
8078    (list (read-string (if current-prefix-arg
8079                           "Exclude author (regexp): "
8080                         "Limit to author (regexp): "))
8081          current-prefix-arg))
8082   (gnus-summary-limit-to-subject from "from" not-matching))
8083
8084 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8085   "Limit the summary buffer to articles with the given RECIPIENT.
8086
8087 If NOT-MATCHING, exclude RECIPIENT.
8088
8089 To and Cc headers are checked.  You need to include them in
8090 `nnmail-extra-headers'."
8091   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8092   (interactive
8093    (list (read-string (format "%s recipient (regexp): "
8094                               (if current-prefix-arg "Exclude" "Limit to")))
8095          current-prefix-arg))
8096   (when (not (equal "" recipient))
8097     (prog1 (let* ((to
8098                    (if (memq 'To nnmail-extra-headers)
8099                        (gnus-summary-find-matching
8100                         (cons 'extra 'To) recipient 'all nil nil
8101                         not-matching)
8102                      (gnus-message
8103                       1 "`To' isn't present in `nnmail-extra-headers'")
8104                      (sit-for 1)
8105                      nil))
8106                   (cc
8107                    (if (memq 'Cc nnmail-extra-headers)
8108                        (gnus-summary-find-matching
8109                         (cons 'extra 'Cc) recipient 'all nil nil
8110                         not-matching)
8111                      (gnus-message
8112                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8113                      (sit-for 1)
8114                      nil))
8115                   (articles
8116                    (if not-matching
8117                        ;; We need the numbers that are in both lists:
8118                        (mapcar (lambda (a)
8119                                  (and (memq a to) a))
8120                                cc)
8121                      (nconc to cc))))
8122              (unless articles
8123                (error "Found no matches for \"%s\"" recipient))
8124              (gnus-summary-limit articles))
8125       (gnus-summary-position-point))))
8126
8127 (defun gnus-summary-limit-to-address (address &optional not-matching)
8128   "Limit the summary buffer to articles with the given ADDRESS.
8129
8130 If NOT-MATCHING, exclude ADDRESS.
8131
8132 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8133 in `nnmail-extra-headers'."
8134   (interactive
8135    (list (read-string (format "%s address (regexp): "
8136                               (if current-prefix-arg "Exclude" "Limit to")))
8137          current-prefix-arg))
8138   (when (not (equal "" address))
8139     (prog1 (let* ((to
8140                    (if (memq 'To nnmail-extra-headers)
8141                        (gnus-summary-find-matching
8142                         (cons 'extra 'To) address 'all nil nil
8143                         not-matching)
8144                      (gnus-message
8145                       1 "`To' isn't present in `nnmail-extra-headers'")
8146                      (sit-for 1)
8147                      t))
8148                   (cc
8149                    (if (memq 'Cc nnmail-extra-headers)
8150                        (gnus-summary-find-matching
8151                         (cons 'extra 'Cc) address 'all nil nil
8152                         not-matching)
8153                      (gnus-message
8154                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8155                      (sit-for 1)
8156                      t))
8157                   (from
8158                    (gnus-summary-find-matching "from" address
8159                                                'all nil nil not-matching))
8160                   (articles
8161                    (if not-matching
8162                        ;; We need the numbers that are in all lists:
8163                        (if (eq cc t)
8164                            (if (eq to t)
8165                                from
8166                              (mapcar (lambda (a) (car (memq a from))) to))
8167                          (if (eq to t)
8168                              (mapcar (lambda (a) (car (memq a from))) cc)
8169                            (mapcar (lambda (a) (car (memq a from)))
8170                                    (mapcar (lambda (a) (car (memq a to)))
8171                                            cc))))
8172                      (nconc (if (eq to t) nil to)
8173                             (if (eq cc t) nil cc)
8174                             from))))
8175              (unless articles
8176                (error "Found no matches for \"%s\"" address))
8177              (gnus-summary-limit articles))
8178       (gnus-summary-position-point))))
8179
8180 (defun gnus-summary-limit-strange-charsets-predicate (header)
8181   (when (fboundp 'char-charset)
8182     (let ((string (concat (mail-header-subject header)
8183                           (mail-header-from header)))
8184           charset found)
8185       (dotimes (i (1- (length string)))
8186         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8187         (when (string-match "unicode\\|big\\|japanese" charset)
8188           (setq found t)))
8189       found)))
8190
8191 (defun gnus-summary-limit-to-predicate (predicate)
8192   "Limit to articles where PREDICATE returns non-nil.
8193 PREDICATE will be called with the header structures of the
8194 articles."
8195   (let ((articles nil)
8196         (case-fold-search t))
8197     (dolist (header gnus-newsgroup-headers)
8198       (when (funcall predicate header)
8199         (push (mail-header-number header) articles)))
8200     (gnus-summary-limit (nreverse articles))))
8201
8202 (defun gnus-summary-limit-to-age (age &optional younger-p)
8203   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8204 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8205 articles that are younger than AGE days."
8206   (interactive
8207    (let ((younger current-prefix-arg)
8208          (days-got nil)
8209          days)
8210      (while (not days-got)
8211        (setq days (if younger
8212                       (read-string "Limit to articles younger than (in days, older when negative): ")
8213                     (read-string
8214                      "Limit to articles older than (in days, younger when negative): ")))
8215        (when (> (length days) 0)
8216          (setq days (read days)))
8217        (if (numberp days)
8218            (progn
8219              (setq days-got t)
8220              (when (< days 0)
8221                (setq younger (not younger))
8222                (setq days (* days -1))))
8223          (message "Please enter a number.")
8224          (sleep-for 1)))
8225      (list days younger)))
8226   (prog1
8227       (let ((data gnus-newsgroup-data)
8228             (cutoff (days-to-time age))
8229             articles d date is-younger)
8230         (while (setq d (pop data))
8231           (when (and (vectorp (gnus-data-header d))
8232                      (setq date (mail-header-date (gnus-data-header d))))
8233             (setq is-younger (time-less-p
8234                               (time-since (gnus-date-get-time date))
8235                               cutoff))
8236             (when (if younger-p
8237                       is-younger
8238                     (not is-younger))
8239               (push (gnus-data-number d) articles))))
8240         (gnus-summary-limit (nreverse articles)))
8241     (gnus-summary-position-point)))
8242
8243 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8244   "Limit the summary buffer to articles that match an 'extra' header."
8245   (interactive
8246    (let ((header
8247           (intern
8248            (gnus-completing-read-with-default
8249             (symbol-name (car gnus-extra-headers))
8250             (if current-prefix-arg
8251                 "Exclude extra header"
8252               "Limit extra header")
8253             (mapcar (lambda (x)
8254                       (cons (symbol-name x) x))
8255                     gnus-extra-headers)
8256             nil
8257             t))))
8258      (list header
8259            (read-string (format "%s header %s (regexp): "
8260                                 (if current-prefix-arg "Exclude" "Limit to")
8261                                 header))
8262            current-prefix-arg)))
8263   (when (not (equal "" regexp))
8264     (prog1
8265         (let ((articles (gnus-summary-find-matching
8266                          (cons 'extra header) regexp 'all nil nil
8267                          not-matching)))
8268           (unless articles
8269             (error "Found no matches for \"%s\"" regexp))
8270           (gnus-summary-limit articles))
8271       (gnus-summary-position-point))))
8272
8273 (defun gnus-summary-limit-to-display-predicate ()
8274   "Limit the summary buffer to the predicated in the `display' group parameter."
8275   (interactive)
8276   (unless gnus-newsgroup-display
8277     (error "There is no `display' group parameter"))
8278   (let (articles)
8279     (dolist (number gnus-newsgroup-articles)
8280       (when (funcall gnus-newsgroup-display)
8281         (push number articles)))
8282     (gnus-summary-limit articles))
8283   (gnus-summary-position-point))
8284
8285 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8286 (make-obsolete
8287  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8288
8289 (defun gnus-summary-limit-to-unread (&optional all)
8290   "Limit the summary buffer to articles that are not marked as read.
8291 If ALL is non-nil, limit strictly to unread articles."
8292   (interactive "P")
8293   (if all
8294       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8295     (gnus-summary-limit-to-marks
8296      ;; Concat all the marks that say that an article is read and have
8297      ;; those removed.
8298      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8299            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8300            gnus-low-score-mark gnus-expirable-mark
8301            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8302            gnus-duplicate-mark)
8303      'reverse)))
8304
8305 (defun gnus-summary-limit-to-headers (match &optional reverse)
8306   "Limit the summary buffer to articles that have headers that match MATCH.
8307 If REVERSE (the prefix), limit to articles that don't match."
8308   (interactive "sMatch headers (regexp): \nP")
8309   (gnus-summary-limit-to-bodies match reverse t))
8310
8311 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8312   "Limit the summary buffer to articles that have bodies that match MATCH.
8313 If REVERSE (the prefix), limit to articles that don't match."
8314   (interactive "sMatch body (regexp): \nP")
8315   (let ((articles nil)
8316         (gnus-select-article-hook nil)  ;Disable hook.
8317         (gnus-article-prepare-hook nil)
8318         (gnus-use-article-prefetch nil)
8319         (gnus-keep-backlog nil)
8320         (gnus-break-pages nil)
8321         (gnus-summary-display-arrow nil)
8322         (gnus-updated-mode-lines nil)
8323         (gnus-auto-center-summary nil)
8324         (gnus-display-mime-function nil))
8325     (dolist (data gnus-newsgroup-data)
8326       (let (gnus-mark-article-hook)
8327         (gnus-summary-select-article t t nil (gnus-data-number data)))
8328       (with-current-buffer gnus-article-buffer
8329         (article-goto-body)
8330         (let* ((case-fold-search t)
8331                (found (if headersp
8332                           (re-search-backward match nil t)
8333                         (re-search-forward match nil t))))
8334           (when (or (and found
8335                          (not reverse))
8336                     (and (not found)
8337                          reverse))
8338             (push (gnus-data-number data) articles)))))
8339     (if (not articles)
8340         (message "No messages matched")
8341       (gnus-summary-limit articles)))
8342   (gnus-summary-position-point))
8343
8344 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8345   "Limit the summary buffer to articles that aren't part on any thread.
8346 If THREADSP (the prefix), limit to articles that are in threads."
8347   (interactive "P")
8348   (let ((articles nil)
8349         thread-articles
8350         threads)
8351     (dolist (thread gnus-newsgroup-threads)
8352       (if (stringp (car thread))
8353           (dolist (thread (cdr thread))
8354             (push thread threads))
8355         (push thread threads)))
8356     (dolist (thread threads)
8357       (setq thread-articles (gnus-articles-in-thread thread))
8358       (when (or (and threadsp
8359                      (> (length thread-articles) 1))
8360                 (and (not threadsp)
8361                      (= (length thread-articles) 1)))
8362         (setq articles (nconc thread-articles articles))))
8363     (if (not articles)
8364         (message "No messages matched")
8365       (gnus-summary-limit articles))
8366     (gnus-summary-position-point)))
8367
8368 (defun gnus-summary-limit-to-replied (&optional unreplied)
8369   "Limit the summary buffer to replied articles.
8370 If UNREPLIED (the prefix), limit to unreplied articles."
8371   (interactive "P")
8372   (if unreplied
8373       (gnus-summary-limit
8374        (gnus-set-difference gnus-newsgroup-articles
8375         gnus-newsgroup-replied))
8376     (gnus-summary-limit gnus-newsgroup-replied))
8377   (gnus-summary-position-point))
8378
8379 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8380 (make-obsolete 'gnus-summary-delete-marked-with
8381                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8382
8383 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8384   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8385 If REVERSE, limit the summary buffer to articles that are marked
8386 with MARKS.  MARKS can either be a string of marks or a list of marks.
8387 Returns how many articles were removed."
8388   (interactive "sMarks: ")
8389   (gnus-summary-limit-to-marks marks t))
8390
8391 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8392   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8393 If REVERSE (the prefix), limit the summary buffer to articles that are
8394 not marked with MARKS.  MARKS can either be a string of marks or a
8395 list of marks.
8396 Returns how many articles were removed."
8397   (interactive "sMarks: \nP")
8398   (prog1
8399       (let ((data gnus-newsgroup-data)
8400             (marks (if (listp marks) marks
8401                      (append marks nil))) ; Transform to list.
8402             articles)
8403         (while data
8404           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8405                   (memq (gnus-data-mark (car data)) marks))
8406             (push (gnus-data-number (car data)) articles))
8407           (setq data (cdr data)))
8408         (gnus-summary-limit articles))
8409     (gnus-summary-position-point)))
8410
8411 (defun gnus-summary-limit-to-score (score)
8412   "Limit to articles with score at or above SCORE."
8413   (interactive "NLimit to articles with score of at least: ")
8414   (let ((data gnus-newsgroup-data)
8415         articles)
8416     (while data
8417       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8418                 score)
8419         (push (gnus-data-number (car data)) articles))
8420       (setq data (cdr data)))
8421     (prog1
8422         (gnus-summary-limit articles)
8423       (gnus-summary-position-point))))
8424
8425 (defun gnus-summary-limit-to-unseen ()
8426   "Limit to unseen articles."
8427   (interactive)
8428   (prog1
8429       (gnus-summary-limit gnus-newsgroup-unseen)
8430     (gnus-summary-position-point)))
8431
8432 (defun gnus-summary-limit-include-thread (id)
8433   "Display all the hidden articles that is in the thread with ID in it.
8434 When called interactively, ID is the Message-ID of the current
8435 article."
8436   (interactive (list (mail-header-id (gnus-summary-article-header))))
8437   (let ((articles (gnus-articles-in-thread
8438                    (gnus-id-to-thread (gnus-root-id id)))))
8439     (prog1
8440         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8441       (gnus-summary-limit-include-matching-articles
8442        "subject"
8443        (regexp-quote (gnus-simplify-subject-re
8444                       (mail-header-subject (gnus-id-to-header id)))))
8445       (gnus-summary-position-point))))
8446
8447 (defun gnus-summary-limit-include-matching-articles (header regexp)
8448   "Display all the hidden articles that have HEADERs that match REGEXP."
8449   (interactive (list (read-string "Match on header: ")
8450                      (read-string "Regexp: ")))
8451   (let ((articles (gnus-find-matching-articles header regexp)))
8452     (prog1
8453         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8454       (gnus-summary-position-point))))
8455
8456 (defun gnus-summary-insert-dormant-articles ()
8457   "Insert all the dormant articles for this group into the current buffer."
8458   (interactive)
8459   (let ((gnus-verbose (max 6 gnus-verbose)))
8460     (if (not gnus-newsgroup-dormant)
8461         (gnus-message 3 "No dormant articles for this group")
8462       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8463
8464 (defun gnus-summary-insert-ticked-articles ()
8465   "Insert ticked articles for this group into the current buffer."
8466   (interactive)
8467   (let ((gnus-verbose (max 6 gnus-verbose)))
8468     (if (not gnus-newsgroup-marked)
8469         (gnus-message 3 "No ticked articles for this group")
8470       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8471
8472 (defun gnus-summary-limit-include-dormant ()
8473   "Display all the hidden articles that are marked as dormant.
8474 Note that this command only works on a subset of the articles currently
8475 fetched for this group."
8476   (interactive)
8477   (unless gnus-newsgroup-dormant
8478     (error "There are no dormant articles in this group"))
8479   (prog1
8480       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8481     (gnus-summary-position-point)))
8482
8483 (defun gnus-summary-limit-exclude-dormant ()
8484   "Hide all dormant articles."
8485   (interactive)
8486   (prog1
8487       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8488     (gnus-summary-position-point)))
8489
8490 (defun gnus-summary-limit-exclude-childless-dormant ()
8491   "Hide all dormant articles that have no children."
8492   (interactive)
8493   (let ((data (gnus-data-list t))
8494         articles d children)
8495     ;; Find all articles that are either not dormant or have
8496     ;; children.
8497     (while (setq d (pop data))
8498       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8499                 (and (setq children
8500                            (gnus-article-children (gnus-data-number d)))
8501                      (let (found)
8502                        (while children
8503                          (when (memq (car children) articles)
8504                            (setq children nil
8505                                  found t))
8506                          (pop children))
8507                        found)))
8508         (push (gnus-data-number d) articles)))
8509     ;; Do the limiting.
8510     (prog1
8511         (gnus-summary-limit articles)
8512       (gnus-summary-position-point))))
8513
8514 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8515   "Mark all unread excluded articles as read.
8516 If ALL, mark even excluded ticked and dormants as read."
8517   (interactive "P")
8518   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8519   (let ((articles (gnus-sorted-ndifference
8520                    (sort
8521                     (mapcar (lambda (h) (mail-header-number h))
8522                             gnus-newsgroup-headers)
8523                     '<)
8524                    gnus-newsgroup-limit))
8525         article)
8526     (setq gnus-newsgroup-unreads
8527           (gnus-sorted-intersection gnus-newsgroup-unreads
8528                                     gnus-newsgroup-limit))
8529     (if all
8530         (setq gnus-newsgroup-dormant nil
8531               gnus-newsgroup-marked nil
8532               gnus-newsgroup-reads
8533               (nconc
8534                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8535                gnus-newsgroup-reads))
8536       (while (setq article (pop articles))
8537         (unless (or (memq article gnus-newsgroup-dormant)
8538                     (memq article gnus-newsgroup-marked))
8539           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8540
8541 (defun gnus-summary-limit (articles &optional pop)
8542   (if pop
8543       ;; We pop the previous limit off the stack and use that.
8544       (setq articles (car gnus-newsgroup-limits)
8545             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8546     ;; We use the new limit, so we push the old limit on the stack.
8547     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8548   ;; Set the limit.
8549   (setq gnus-newsgroup-limit articles)
8550   (let ((total (length gnus-newsgroup-data))
8551         (data (gnus-data-find-list (gnus-summary-article-number)))
8552         (gnus-summary-mark-below nil)   ; Inhibit this.
8553         found)
8554     ;; This will do all the work of generating the new summary buffer
8555     ;; according to the new limit.
8556     (gnus-summary-prepare)
8557     ;; Hide any threads, possibly.
8558     (gnus-summary-maybe-hide-threads)
8559     ;; Try to return to the article you were at, or one in the
8560     ;; neighborhood.
8561     (when data
8562       ;; We try to find some article after the current one.
8563       (while data
8564         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8565           (setq data nil
8566                 found t))
8567         (setq data (cdr data))))
8568     (unless found
8569       ;; If there is no data, that means that we were after the last
8570       ;; article.  The same goes when we can't find any articles
8571       ;; after the current one.
8572       (goto-char (point-max))
8573       (gnus-summary-find-prev))
8574     (gnus-set-mode-line 'summary)
8575     ;; We return how many articles were removed from the summary
8576     ;; buffer as a result of the new limit.
8577     (- total (length gnus-newsgroup-data))))
8578
8579 (defsubst gnus-invisible-cut-children (threads)
8580   (let ((num 0))
8581     (while threads
8582       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8583         (incf num))
8584       (pop threads))
8585     (< num 2)))
8586
8587 (defsubst gnus-cut-thread (thread)
8588   "Go forwards in the thread until we find an article that we want to display."
8589   (when (or (eq gnus-fetch-old-headers 'some)
8590             (eq gnus-fetch-old-headers 'invisible)
8591             (numberp gnus-fetch-old-headers)
8592             (eq gnus-build-sparse-threads 'some)
8593             (eq gnus-build-sparse-threads 'more))
8594     ;; Deal with old-fetched headers and sparse threads.
8595     (while (and
8596             thread
8597             (or
8598              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8599              (gnus-summary-article-ancient-p
8600               (mail-header-number (car thread))))
8601             (if (or (<= (length (cdr thread)) 1)
8602                     (eq gnus-fetch-old-headers 'invisible))
8603                 (setq gnus-newsgroup-limit
8604                       (delq (mail-header-number (car thread))
8605                             gnus-newsgroup-limit)
8606                       thread (cadr thread))
8607               (when (gnus-invisible-cut-children (cdr thread))
8608                 (let ((th (cdr thread)))
8609                   (while th
8610                     (if (memq (mail-header-number (caar th))
8611                               gnus-newsgroup-limit)
8612                         (setq thread (car th)
8613                               th nil)
8614                       (setq th (cdr th))))))))))
8615   thread)
8616
8617 (defun gnus-cut-threads (threads)
8618   "Cut off all uninteresting articles from the beginning of THREADS."
8619   (when (or (eq gnus-fetch-old-headers 'some)
8620             (eq gnus-fetch-old-headers 'invisible)
8621             (numberp gnus-fetch-old-headers)
8622             (eq gnus-build-sparse-threads 'some)
8623             (eq gnus-build-sparse-threads 'more))
8624     (let ((th threads))
8625       (while th
8626         (setcar th (gnus-cut-thread (car th)))
8627         (setq th (cdr th)))))
8628   ;; Remove nixed out threads.
8629   (delq nil threads))
8630
8631 (defun gnus-summary-initial-limit (&optional show-if-empty)
8632   "Figure out what the initial limit is supposed to be on group entry.
8633 This entails weeding out unwanted dormants, low-scored articles,
8634 fetch-old-headers verbiage, and so on."
8635   ;; Most groups have nothing to remove.
8636   (unless (or gnus-inhibit-limiting
8637               (and (null gnus-newsgroup-dormant)
8638                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8639                    (not (eq gnus-fetch-old-headers 'some))
8640                    (not (numberp gnus-fetch-old-headers))
8641                    (not (eq gnus-fetch-old-headers 'invisible))
8642                    (null gnus-summary-expunge-below)
8643                    (not (eq gnus-build-sparse-threads 'some))
8644                    (not (eq gnus-build-sparse-threads 'more))
8645                    (null gnus-thread-expunge-below)
8646                    (not gnus-use-nocem)))
8647     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8648     (setq gnus-newsgroup-limit nil)
8649     (mapatoms
8650      (lambda (node)
8651        (unless (car (symbol-value node))
8652          ;; These threads have no parents -- they are roots.
8653          (let ((nodes (cdr (symbol-value node)))
8654                thread)
8655            (while nodes
8656              (if (and gnus-thread-expunge-below
8657                       (< (gnus-thread-total-score (car nodes))
8658                          gnus-thread-expunge-below))
8659                  (gnus-expunge-thread (pop nodes))
8660                (setq thread (pop nodes))
8661                (gnus-summary-limit-children thread))))))
8662      gnus-newsgroup-dependencies)
8663     ;; If this limitation resulted in an empty group, we might
8664     ;; pop the previous limit and use it instead.
8665     (when (and (not gnus-newsgroup-limit)
8666                show-if-empty)
8667       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8668     gnus-newsgroup-limit))
8669
8670 (defun gnus-summary-limit-children (thread)
8671   "Return 1 if this subthread is visible and 0 if it is not."
8672   ;; First we get the number of visible children to this thread.  This
8673   ;; is done by recursing down the thread using this function, so this
8674   ;; will really go down to a leaf article first, before slowly
8675   ;; working its way up towards the root.
8676   (when thread
8677     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8678            (children
8679            (if (cdr thread)
8680                (apply '+ (mapcar 'gnus-summary-limit-children
8681                                  (cdr thread)))
8682              0))
8683           (number (mail-header-number (car thread)))
8684           score)
8685       (if (and
8686            (not (memq number gnus-newsgroup-marked))
8687            (or
8688             ;; If this article is dormant and has absolutely no visible
8689             ;; children, then this article isn't visible.
8690             (and (memq number gnus-newsgroup-dormant)
8691                  (zerop children))
8692             ;; If this is "fetch-old-headered" and there is no
8693             ;; visible children, then we don't want this article.
8694             (and (or (eq gnus-fetch-old-headers 'some)
8695                      (numberp gnus-fetch-old-headers))
8696                  (gnus-summary-article-ancient-p number)
8697                  (zerop children))
8698             ;; If this is "fetch-old-headered" and `invisible', then
8699             ;; we don't want this article.
8700             (and (eq gnus-fetch-old-headers 'invisible)
8701                  (gnus-summary-article-ancient-p number))
8702             ;; If this is a sparsely inserted article with no children,
8703             ;; we don't want it.
8704             (and (eq gnus-build-sparse-threads 'some)
8705                  (gnus-summary-article-sparse-p number)
8706                  (zerop children))
8707             ;; If we use expunging, and this article is really
8708             ;; low-scored, then we don't want this article.
8709             (when (and gnus-summary-expunge-below
8710                        (< (setq score
8711                                 (or (cdr (assq number gnus-newsgroup-scored))
8712                                     gnus-summary-default-score))
8713                           gnus-summary-expunge-below))
8714               ;; We increase the expunge-tally here, but that has
8715               ;; nothing to do with the limits, really.
8716               (incf gnus-newsgroup-expunged-tally)
8717               ;; We also mark as read here, if that's wanted.
8718               (when (and gnus-summary-mark-below
8719                          (< score gnus-summary-mark-below))
8720                 (setq gnus-newsgroup-unreads
8721                       (delq number gnus-newsgroup-unreads))
8722                 (if gnus-newsgroup-auto-expire
8723                     (push number gnus-newsgroup-expirable)
8724                   (push (cons number gnus-low-score-mark)
8725                         gnus-newsgroup-reads)))
8726               t)
8727             ;; Do the `display' group parameter.
8728             (and gnus-newsgroup-display
8729                  (not (funcall gnus-newsgroup-display)))
8730             ;; Check NoCeM things.
8731             (when (and gnus-use-nocem
8732                        (gnus-nocem-unwanted-article-p
8733                         (mail-header-id (car thread))))
8734               (setq gnus-newsgroup-unreads
8735                     (delq number gnus-newsgroup-unreads))
8736               t)))
8737           ;; Nope, invisible article.
8738           0
8739         ;; Ok, this article is to be visible, so we add it to the limit
8740         ;; and return 1.
8741         (push number gnus-newsgroup-limit)
8742         1))))
8743
8744 (defun gnus-expunge-thread (thread)
8745   "Mark all articles in THREAD as read."
8746   (let* ((number (mail-header-number (car thread))))
8747     (incf gnus-newsgroup-expunged-tally)
8748     ;; We also mark as read here, if that's wanted.
8749     (setq gnus-newsgroup-unreads
8750           (delq number gnus-newsgroup-unreads))
8751     (if gnus-newsgroup-auto-expire
8752         (push number gnus-newsgroup-expirable)
8753       (push (cons number gnus-low-score-mark)
8754             gnus-newsgroup-reads)))
8755   ;; Go recursively through all subthreads.
8756   (mapcar 'gnus-expunge-thread (cdr thread)))
8757
8758 ;; Summary article oriented commands
8759
8760 (defun gnus-summary-refer-parent-article (n)
8761   "Refer parent article N times.
8762 If N is negative, go to ancestor -N instead.
8763 The difference between N and the number of articles fetched is returned."
8764   (interactive "p")
8765   (let ((skip 1)
8766         error header ref)
8767     (when (not (natnump n))
8768       (setq skip (abs n)
8769             n 1))
8770     (while (and (> n 0)
8771                 (not error))
8772       (setq header (gnus-summary-article-header))
8773       (if (and (eq (mail-header-number header)
8774                    (cdr gnus-article-current))
8775                (equal gnus-newsgroup-name
8776                       (car gnus-article-current)))
8777           ;; If we try to find the parent of the currently
8778           ;; displayed article, then we take a look at the actual
8779           ;; References header, since this is slightly more
8780           ;; reliable than the References field we got from the
8781           ;; server.
8782           (with-current-buffer gnus-original-article-buffer
8783             (nnheader-narrow-to-headers)
8784             (unless (setq ref (message-fetch-field "references"))
8785               (when (setq ref (message-fetch-field "in-reply-to"))
8786                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8787             (widen))
8788         (setq ref
8789               ;; It's not the current article, so we take a bet on
8790               ;; the value we got from the server.
8791               (mail-header-references header)))
8792       (if (and ref
8793                (not (equal ref "")))
8794           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8795             (gnus-message 1 "Couldn't find parent"))
8796         (gnus-message 1 "No references in article %d"
8797                       (gnus-summary-article-number))
8798         (setq error t))
8799       (decf n))
8800     (gnus-summary-position-point)
8801     n))
8802
8803 (defun gnus-summary-refer-references ()
8804   "Fetch all articles mentioned in the References header.
8805 Return the number of articles fetched."
8806   (interactive)
8807   (let ((ref (mail-header-references (gnus-summary-article-header)))
8808         (current (gnus-summary-article-number))
8809         (n 0))
8810     (if (or (not ref)
8811             (equal ref ""))
8812         (error "No References in the current article")
8813       ;; For each Message-ID in the References header...
8814       (while (string-match "<[^>]*>" ref)
8815         (incf n)
8816         ;; ... fetch that article.
8817         (gnus-summary-refer-article
8818          (prog1 (match-string 0 ref)
8819            (setq ref (substring ref (match-end 0))))))
8820       (gnus-summary-goto-subject current)
8821       (gnus-summary-position-point)
8822       n)))
8823
8824 (defun gnus-summary-refer-thread (&optional limit)
8825   "Fetch all articles in the current thread.
8826 If LIMIT (the numerical prefix), fetch that many old headers instead
8827 of what's specified by the `gnus-refer-thread-limit' variable."
8828   (interactive "P")
8829   (let ((id (mail-header-id (gnus-summary-article-header)))
8830         (limit (if limit (prefix-numeric-value limit)
8831                  gnus-refer-thread-limit)))
8832     (unless (eq gnus-fetch-old-headers 'invisible)
8833       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8834       ;; Retrieve the headers and read them in.
8835       (if (eq (if (numberp limit)
8836                   (gnus-retrieve-headers
8837                    (list (min
8838                           (+ (mail-header-number
8839                               (gnus-summary-article-header))
8840                              limit)
8841                           gnus-newsgroup-end))
8842                    gnus-newsgroup-name (* limit 2))
8843                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8844                 ;; headers.
8845                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8846                                        gnus-newsgroup-name limit))
8847               'nov)
8848           (gnus-build-all-threads)
8849         (error "Can't fetch thread from back ends that don't support NOV"))
8850       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8851     (gnus-summary-limit-include-thread id)))
8852
8853 (defun gnus-summary-refer-article (message-id)
8854   "Fetch an article specified by MESSAGE-ID."
8855   (interactive "sMessage-ID: ")
8856   (when (and (stringp message-id)
8857              (not (zerop (length message-id))))
8858     (setq message-id (gnus-replace-in-string message-id " " ""))
8859     ;; Construct the correct Message-ID if necessary.
8860     ;; Suggested by tale@pawl.rpi.edu.
8861     (unless (string-match "^<" message-id)
8862       (setq message-id (concat "<" message-id)))
8863     (unless (string-match ">$" message-id)
8864       (setq message-id (concat message-id ">")))
8865     ;; People often post MIDs from URLs, so unhex it:
8866     (unless (string-match "@" message-id)
8867       (setq message-id (gnus-url-unhex-string message-id)))
8868     (let* ((header (gnus-id-to-header message-id))
8869            (sparse (and header
8870                         (gnus-summary-article-sparse-p
8871                          (mail-header-number header))
8872                         (memq (mail-header-number header)
8873                               gnus-newsgroup-limit)))
8874            number)
8875       (cond
8876        ;; If the article is present in the buffer we just go to it.
8877        ((and header
8878              (or (not (gnus-summary-article-sparse-p
8879                        (mail-header-number header)))
8880                  sparse))
8881         (prog1
8882             (gnus-summary-goto-article
8883              (mail-header-number header) nil t)
8884           (when sparse
8885             (gnus-summary-update-article (mail-header-number header)))))
8886        (t
8887         ;; We fetch the article.
8888         (catch 'found
8889           (dolist (gnus-override-method (gnus-refer-article-methods))
8890             (when (and (gnus-check-server gnus-override-method)
8891                        ;; Fetch the header,
8892                        (setq number (gnus-summary-insert-subject message-id)))
8893               ;; and display the article.
8894               (gnus-summary-select-article nil nil nil number)
8895               (throw 'found t)))
8896           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8897
8898 (defun gnus-refer-article-methods ()
8899   "Return a list of referable methods."
8900   (cond
8901    ;; No method, so we default to current and native.
8902    ((null gnus-refer-article-method)
8903     (list gnus-current-select-method gnus-select-method))
8904    ;; Current.
8905    ((eq 'current gnus-refer-article-method)
8906     (list gnus-current-select-method))
8907    ;; List of select methods.
8908    ((not (and (symbolp (car gnus-refer-article-method))
8909               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8910     (let (out)
8911       (dolist (method gnus-refer-article-method)
8912         (push (if (eq 'current method)
8913                   gnus-current-select-method
8914                 method)
8915               out))
8916       (nreverse out)))
8917    ;; One single select method.
8918    (t
8919     (list gnus-refer-article-method))))
8920
8921 (defun gnus-summary-edit-parameters ()
8922   "Edit the group parameters of the current group."
8923   (interactive)
8924   (gnus-group-edit-group gnus-newsgroup-name 'params))
8925
8926 (defun gnus-summary-customize-parameters ()
8927   "Customize the group parameters of the current group."
8928   (interactive)
8929   (gnus-group-customize gnus-newsgroup-name))
8930
8931 (defun gnus-summary-enter-digest-group (&optional force)
8932   "Enter an nndoc group based on the current article.
8933 If FORCE, force a digest interpretation.  If not, try
8934 to guess what the document format is."
8935   (interactive "P")
8936   (let ((conf gnus-current-window-configuration))
8937     (save-window-excursion
8938       (save-excursion
8939         (let (gnus-article-prepare-hook
8940               gnus-display-mime-function
8941               gnus-break-pages)
8942           (gnus-summary-select-article))))
8943     (setq gnus-current-window-configuration conf)
8944     (let* ((name (format "%s-%d"
8945                          (gnus-group-prefixed-name
8946                           gnus-newsgroup-name (list 'nndoc ""))
8947                          (with-current-buffer gnus-summary-buffer
8948                            gnus-current-article)))
8949            (ogroup gnus-newsgroup-name)
8950            (params (append (gnus-info-params (gnus-get-info ogroup))
8951                            (list (cons 'to-group ogroup))
8952                            (list (cons 'parent-group ogroup))
8953                            (list (cons 'save-article-group ogroup))))
8954            (case-fold-search t)
8955            (buf (current-buffer))
8956            dig to-address)
8957       (with-current-buffer gnus-original-article-buffer
8958         ;; Have the digest group inherit the main mail address of
8959         ;; the parent article.
8960         (when (setq to-address (or (gnus-fetch-field "reply-to")
8961                                    (gnus-fetch-field "from")))
8962           (setq params
8963                 (append
8964                  (list (cons 'to-address
8965                              (funcall gnus-decode-encoded-address-function
8966                                       to-address))))))
8967         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8968         (insert-buffer-substring gnus-original-article-buffer)
8969         ;; Remove lines that may lead nndoc to misinterpret the
8970         ;; document type.
8971         (narrow-to-region
8972          (goto-char (point-min))
8973          (or (search-forward "\n\n" nil t) (point)))
8974         (goto-char (point-min))
8975         (delete-matching-lines "^Path:\\|^From ")
8976         (widen))
8977       (unwind-protect
8978           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8979                     (gnus-newsgroup-ephemeral-ignored-charsets
8980                      gnus-newsgroup-ignored-charsets))
8981                 (gnus-group-read-ephemeral-group
8982                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8983                               (nndoc-article-type
8984                                ,(if force 'mbox 'guess)))
8985                  t nil nil nil
8986                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8987                                                         "ADAPT")))))
8988               ;; Make all postings to this group go to the parent group.
8989               (nconc (gnus-info-params (gnus-get-info name))
8990                      params)
8991             ;; Couldn't select this doc group.
8992             (switch-to-buffer buf)
8993             (gnus-set-global-variables)
8994             (gnus-configure-windows 'summary)
8995             (gnus-message 3 "Article couldn't be entered?"))
8996         (kill-buffer dig)))))
8997
8998 (defun gnus-summary-read-document (n)
8999   "Open a new group based on the current article(s).
9000 This will allow you to read digests and other similar
9001 documents as newsgroups.
9002 Obeys the standard process/prefix convention."
9003   (interactive "P")
9004   (let* ((ogroup gnus-newsgroup-name)
9005          (params (append (gnus-info-params (gnus-get-info ogroup))
9006                          (list (cons 'to-group ogroup))))
9007          group egroup groups vgroup)
9008     (dolist (article (gnus-summary-work-articles n))
9009       (setq group (format "%s-%d" gnus-newsgroup-name article))
9010       (gnus-summary-remove-process-mark article)
9011       (when (gnus-summary-display-article article)
9012         (save-excursion ;;What for?
9013           (with-temp-buffer
9014             (insert-buffer-substring gnus-original-article-buffer)
9015             ;; Remove some headers that may lead nndoc to make
9016             ;; the wrong guess.
9017             (message-narrow-to-head)
9018             (goto-char (point-min))
9019             (delete-matching-lines "^Path:\\|^From ")
9020             (widen)
9021             (if (setq egroup
9022                       (gnus-group-read-ephemeral-group
9023                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9024                                      (nndoc-article-type guess))
9025                        t nil t))
9026                 (progn
9027                   ;; Make all postings to this group go to the parent group.
9028                   (nconc (gnus-info-params (gnus-get-info egroup))
9029                          params)
9030                   (push egroup groups))
9031               ;; Couldn't select this doc group.
9032               (gnus-error 3 "Article couldn't be entered"))))))
9033     ;; Now we have selected all the documents.
9034     (cond
9035      ((not groups)
9036       (error "None of the articles could be interpreted as documents"))
9037      ((gnus-group-read-ephemeral-group
9038        (setq vgroup (format
9039                      "nnvirtual:%s-%s" gnus-newsgroup-name
9040                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9041        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9042        t
9043        (cons (current-buffer) 'summary)))
9044      (t
9045       (error "Couldn't select virtual nndoc group")))))
9046
9047 (defun gnus-summary-isearch-article (&optional regexp-p)
9048   "Do incremental search forward on the current article.
9049 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9050   (interactive "P")
9051   (gnus-summary-select-article)
9052   (gnus-configure-windows 'article)
9053   (gnus-eval-in-buffer-window gnus-article-buffer
9054     (save-restriction
9055       (widen)
9056       (isearch-forward regexp-p))))
9057
9058 (defun gnus-summary-repeat-search-article-forward ()
9059   "Repeat the previous search forwards."
9060   (interactive)
9061   (unless gnus-last-search-regexp
9062     (error "No previous search"))
9063   (gnus-summary-search-article-forward gnus-last-search-regexp))
9064
9065 (defun gnus-summary-repeat-search-article-backward ()
9066   "Repeat the previous search backwards."
9067   (interactive)
9068   (unless gnus-last-search-regexp
9069     (error "No previous search"))
9070   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9071
9072 (defun gnus-summary-search-article-forward (regexp &optional backward)
9073   "Search for an article containing REGEXP forward.
9074 If BACKWARD, search backward instead."
9075   (interactive
9076    (list (read-string
9077           (format "Search article %s (regexp%s): "
9078                   (if current-prefix-arg "backward" "forward")
9079                   (if gnus-last-search-regexp
9080                       (concat ", default " gnus-last-search-regexp)
9081                     "")))
9082          current-prefix-arg))
9083   (if (string-equal regexp "")
9084       (setq regexp (or gnus-last-search-regexp ""))
9085     (setq gnus-last-search-regexp regexp)
9086     (setq gnus-article-before-search gnus-current-article))
9087   ;; Intentionally set gnus-last-article.
9088   (setq gnus-last-article gnus-article-before-search)
9089   (let ((gnus-last-article gnus-last-article))
9090     (if (gnus-summary-search-article regexp backward)
9091         (gnus-summary-show-thread)
9092       (signal 'search-failed (list regexp)))))
9093
9094 (defun gnus-summary-search-article-backward (regexp)
9095   "Search for an article containing REGEXP backward."
9096   (interactive
9097    (list (read-string
9098           (format "Search article backward (regexp%s): "
9099                   (if gnus-last-search-regexp
9100                       (concat ", default " gnus-last-search-regexp)
9101                     "")))))
9102   (gnus-summary-search-article-forward regexp 'backward))
9103
9104 (defun gnus-summary-search-article (regexp &optional backward)
9105   "Search for an article containing REGEXP.
9106 Optional argument BACKWARD means do search for backward.
9107 `gnus-select-article-hook' is not called during the search."
9108   ;; We have to require this here to make sure that the following
9109   ;; dynamic binding isn't shadowed by autoloading.
9110   (require 'gnus-async)
9111   (require 'gnus-art)
9112   (let ((gnus-select-article-hook nil)  ;Disable hook.
9113         (gnus-article-prepare-hook nil)
9114         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9115         (gnus-use-article-prefetch nil)
9116         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9117         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9118         (gnus-visual nil)
9119         (gnus-keep-backlog nil)
9120         (gnus-break-pages nil)
9121         (gnus-summary-display-arrow nil)
9122         (gnus-updated-mode-lines nil)
9123         (gnus-auto-center-summary nil)
9124         (sum (current-buffer))
9125         (gnus-display-mime-function nil)
9126         (found nil)
9127         point)
9128     (gnus-save-hidden-threads
9129       (gnus-summary-select-article)
9130       (set-buffer gnus-article-buffer)
9131       (goto-char (window-point (get-buffer-window (current-buffer))))
9132       (when backward
9133         (forward-line -1))
9134       (while (not found)
9135         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9136         (if (if backward
9137                 (re-search-backward regexp nil t)
9138               (re-search-forward regexp nil t))
9139             ;; We found the regexp.
9140             (progn
9141               (setq found 'found)
9142               (beginning-of-line)
9143               (set-window-start
9144                (get-buffer-window (current-buffer))
9145                (point))
9146               (forward-line 1)
9147               (set-window-point
9148                (get-buffer-window (current-buffer))
9149                (point))
9150               (set-buffer sum)
9151               (setq point (point)))
9152           ;; We didn't find it, so we go to the next article.
9153           (set-buffer sum)
9154           (setq found 'not)
9155           (while (eq found 'not)
9156             (if (not (if backward (gnus-summary-find-prev)
9157                        (gnus-summary-find-next)))
9158                 ;; No more articles.
9159                 (setq found t)
9160               ;; Select the next article and adjust point.
9161               (unless (gnus-summary-article-sparse-p
9162                        (gnus-summary-article-number))
9163                 (setq found nil)
9164                 (gnus-summary-select-article)
9165                 (set-buffer gnus-article-buffer)
9166                 (widen)
9167                 (goto-char (if backward (point-max) (point-min))))))))
9168       (gnus-message 7 ""))
9169     ;; Return whether we found the regexp.
9170     (when (eq found 'found)
9171       (goto-char point)
9172       (gnus-summary-show-thread)
9173       (gnus-summary-goto-subject gnus-current-article)
9174       (gnus-summary-position-point)
9175       t)))
9176
9177 (defun gnus-find-matching-articles (header regexp)
9178   "Return a list of all articles that match REGEXP on HEADER.
9179 This search includes all articles in the current group that Gnus has
9180 fetched headers for, whether they are displayed or not."
9181   (let ((articles nil)
9182         ;; Can't eta-reduce because it's a macro.
9183         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9184         (case-fold-search t))
9185     (dolist (header gnus-newsgroup-headers)
9186       (when (string-match regexp (funcall func header))
9187         (push (mail-header-number header) articles)))
9188     (nreverse articles)))
9189
9190 (defun gnus-summary-find-matching (header regexp &optional backward unread
9191                                           not-case-fold not-matching)
9192   "Return a list of all articles that match REGEXP on HEADER.
9193 The search stars on the current article and goes forwards unless
9194 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9195 If UNREAD is non-nil, only unread articles will
9196 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9197 in the comparisons. If NOT-MATCHING, return a list of all articles that
9198 not match REGEXP on HEADER."
9199   (let ((case-fold-search (not not-case-fold))
9200         articles d func)
9201     (if (consp header)
9202         (if (eq (car header) 'extra)
9203             (setq func
9204                   `(lambda (h)
9205                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9206                          "")))
9207           (error "%s is an invalid header" header))
9208       (unless (fboundp (intern (concat "mail-header-" header)))
9209         (error "%s is not a valid header" header))
9210       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9211     (dolist (d (if (eq backward 'all)
9212                    gnus-newsgroup-data
9213                  (gnus-data-find-list
9214                   (gnus-summary-article-number)
9215                   (gnus-data-list backward))))
9216       (when (and (or (not unread)       ; We want all articles...
9217                      (gnus-data-unread-p d)) ; Or just unreads.
9218                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9219                  (if not-matching
9220                      (not (string-match
9221                            regexp
9222                            (funcall func (gnus-data-header d))))
9223                    (string-match regexp
9224                                  (funcall func (gnus-data-header d)))))
9225         (push (gnus-data-number d) articles))) ; Success!
9226     (nreverse articles)))
9227
9228 (defun gnus-summary-execute-command (header regexp command &optional backward)
9229   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9230 If HEADER is an empty string (or nil), the match is done on the entire
9231 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9232   (interactive
9233    (list (let ((completion-ignore-case t))
9234            (completing-read
9235             "Header name: "
9236             (mapcar (lambda (header) (list (format "%s" header)))
9237                     (append
9238                      '("Number" "Subject" "From" "Lines" "Date"
9239                        "Message-ID" "Xref" "References" "Body")
9240                      gnus-extra-headers))
9241             nil 'require-match))
9242          (read-string "Regexp: ")
9243          (read-key-sequence "Command: ")
9244          current-prefix-arg))
9245   (when (equal header "Body")
9246     (setq header ""))
9247   ;; Hidden thread subtrees must be searched as well.
9248   (gnus-summary-show-all-threads)
9249   ;; We don't want to change current point nor window configuration.
9250   (save-excursion
9251     (save-window-excursion
9252       (let (gnus-visual
9253             gnus-treat-strip-trailing-blank-lines
9254             gnus-treat-strip-leading-blank-lines
9255             gnus-treat-strip-multiple-blank-lines
9256             gnus-treat-hide-boring-headers
9257             gnus-treat-fold-newsgroups
9258             gnus-article-prepare-hook)
9259         (gnus-message 6 "Executing %s..." (key-description command))
9260         ;; We'd like to execute COMMAND interactively so as to give arguments.
9261         (gnus-execute header regexp
9262                       `(call-interactively ',(key-binding command))
9263                       backward)
9264         (gnus-message 6 "Executing %s...done" (key-description command))))))
9265
9266 (defun gnus-summary-beginning-of-article ()
9267   "Scroll the article back to the beginning."
9268   (interactive)
9269   (gnus-summary-select-article)
9270   (gnus-configure-windows 'article)
9271   (gnus-eval-in-buffer-window gnus-article-buffer
9272     (widen)
9273     (goto-char (point-min))
9274     (when gnus-break-pages
9275       (gnus-narrow-to-page))))
9276
9277 (defun gnus-summary-end-of-article ()
9278   "Scroll to the end of the article."
9279   (interactive)
9280   (gnus-summary-select-article)
9281   (gnus-configure-windows 'article)
9282   (gnus-eval-in-buffer-window gnus-article-buffer
9283     (widen)
9284     (goto-char (point-max))
9285     (recenter -3)
9286     (when gnus-break-pages
9287       (gnus-narrow-to-page))))
9288
9289 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9290   "Truncate to LEN and quote all \"(\"'s in STRING."
9291   (gnus-replace-in-string (if (and len (> (length string) len))
9292                               (substring string 0 len)
9293                             string)
9294                           "[()]" "\\\\\\&"))
9295
9296 (defun gnus-summary-print-article (&optional filename n)
9297   "Generate and print a PostScript image of the process-marked (mail) articles.
9298
9299 If used interactively, print the current article if none are
9300 process-marked.  With prefix arg, prompt the user for the name of the
9301 file to save in.
9302
9303 When used from Lisp, accept two optional args FILENAME and N.  N means
9304 to print the next N articles.  If N is negative, print the N previous
9305 articles.  If N is nil and articles have been marked with the process
9306 mark, print these instead.
9307
9308 If the optional first argument FILENAME is nil, send the image to the
9309 printer.  If FILENAME is a string, save the PostScript image in a file with
9310 that name.  If FILENAME is a number, prompt the user for the name of the file
9311 to save in."
9312   (interactive (list (ps-print-preprint current-prefix-arg)))
9313   (dolist (article (gnus-summary-work-articles n))
9314     (gnus-summary-select-article nil nil 'pseudo article)
9315     (gnus-eval-in-buffer-window gnus-article-buffer
9316       (gnus-print-buffer))
9317     (gnus-summary-remove-process-mark article))
9318   (ps-despool filename))
9319
9320 (defun gnus-print-buffer ()
9321   (let ((buffer (generate-new-buffer " *print*")))
9322     (unwind-protect
9323         (progn
9324           (copy-to-buffer buffer (point-min) (point-max))
9325           (set-buffer buffer)
9326           (gnus-remove-text-with-property 'gnus-decoration)
9327           (when (gnus-visual-p 'article-highlight 'highlight)
9328             ;; Copy-to-buffer doesn't copy overlay.  So redo
9329             ;; highlight.
9330             (let ((gnus-article-buffer buffer))
9331               (gnus-article-highlight-citation t)
9332               (gnus-article-highlight-signature)
9333               (gnus-article-emphasize)
9334               (gnus-article-delete-invisible-text)))
9335           (let ((ps-left-header
9336                  (list
9337                   (concat "("
9338                           (gnus-summary-print-truncate-and-quote
9339                            (mail-header-subject gnus-current-headers)
9340                            66) ")")
9341                   (concat "("
9342                           (gnus-summary-print-truncate-and-quote
9343                            (mail-header-from gnus-current-headers)
9344                            45) ")")))
9345                 (ps-right-header
9346                  (list
9347                   "/pagenumberstring load"
9348                   (concat "("
9349                           (mail-header-date gnus-current-headers) ")"))))
9350             (gnus-run-hooks 'gnus-ps-print-hook)
9351             (save-excursion
9352               (if ps-print-color-p
9353                   (ps-spool-buffer-with-faces)
9354                 (ps-spool-buffer)))))
9355       (kill-buffer buffer))))
9356
9357 (defun gnus-summary-show-article (&optional arg)
9358   "Force redisplaying of the current article.
9359 If ARG (the prefix) is a number, show the article with the charset
9360 defined in `gnus-summary-show-article-charset-alist', or the charset
9361 input.
9362 If ARG (the prefix) is non-nil and not a number, show the raw article
9363 without any article massaging functions being run.  Normally, the key
9364 strokes are `C-u g'."
9365   (interactive "P")
9366   (cond
9367    ((numberp arg)
9368     (gnus-summary-show-article t)
9369     (let ((gnus-newsgroup-charset
9370            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9371                (mm-read-coding-system
9372                 "View as charset: " ;; actually it is coding system.
9373                 (with-current-buffer gnus-article-buffer
9374                   (mm-detect-coding-region (point) (point-max))))))
9375           (gnus-newsgroup-ignored-charsets 'gnus-all))
9376       (gnus-summary-select-article nil 'force)
9377       (let ((deps gnus-newsgroup-dependencies)
9378             head header lines)
9379         (with-current-buffer gnus-original-article-buffer
9380           (save-restriction
9381             (message-narrow-to-head)
9382             (setq head (buffer-string))
9383             (goto-char (point-min))
9384             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9385               (goto-char (point-max))
9386               (widen)
9387               (setq lines (1- (count-lines (point) (point-max))))))
9388           (with-temp-buffer
9389             (insert (format "211 %d Article retrieved.\n"
9390                             (cdr gnus-article-current)))
9391             (insert head)
9392             (if lines (insert (format "Lines: %d\n" lines)))
9393             (insert ".\n")
9394             (let ((nntp-server-buffer (current-buffer)))
9395               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9396         (gnus-data-set-header
9397          (gnus-data-find (cdr gnus-article-current))
9398          header)
9399         (gnus-summary-update-article-line
9400          (cdr gnus-article-current) header)
9401         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9402           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9403    ((not arg)
9404     ;; Select the article the normal way.
9405     (gnus-summary-select-article nil 'force))
9406    (t
9407     ;; We have to require this here to make sure that the following
9408     ;; dynamic binding isn't shadowed by autoloading.
9409     (require 'gnus-async)
9410     (require 'gnus-art)
9411     ;; Bind the article treatment functions to nil.
9412     (let ((gnus-have-all-headers t)
9413           gnus-article-prepare-hook
9414           gnus-article-decode-hook
9415           gnus-display-mime-function
9416           gnus-break-pages)
9417       ;; Destroy any MIME parts.
9418       (when (gnus-buffer-live-p gnus-article-buffer)
9419         (with-current-buffer gnus-article-buffer
9420           (mm-destroy-parts gnus-article-mime-handles)
9421           ;; Set it to nil for safety reason.
9422           (setq gnus-article-mime-handle-alist nil)
9423           (setq gnus-article-mime-handles nil)))
9424       (gnus-summary-select-article nil 'force))))
9425   (gnus-summary-goto-subject gnus-current-article)
9426   (gnus-summary-position-point))
9427
9428 (defun gnus-summary-show-raw-article ()
9429   "Show the raw article without any article massaging functions being run."
9430   (interactive)
9431   (gnus-summary-show-article t))
9432
9433 (defun gnus-summary-verbose-headers (&optional arg)
9434   "Toggle permanent full header display.
9435 If ARG is a positive number, turn header display on.
9436 If ARG is a negative number, turn header display off."
9437   (interactive "P")
9438   (setq gnus-show-all-headers
9439         (cond ((or (not (numberp arg))
9440                    (zerop arg))
9441                (not gnus-show-all-headers))
9442               ((natnump arg)
9443                t)))
9444   (gnus-summary-show-article))
9445
9446 (defun gnus-summary-toggle-header (&optional arg)
9447   "Show the headers if they are hidden, or hide them if they are shown.
9448 If ARG is a positive number, show the entire header.
9449 If ARG is a negative number, hide the unwanted header lines."
9450   (interactive "P")
9451   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9452                      (get-buffer-window gnus-article-buffer t))))
9453     (with-current-buffer gnus-article-buffer
9454       (widen)
9455       (article-narrow-to-head)
9456       (let* ((inhibit-read-only t)
9457              (inhibit-point-motion-hooks t)
9458              (hidden (if (numberp arg)
9459                          (>= arg 0)
9460                        (or (not (looking-at "[^ \t\n]+:"))
9461                            (gnus-article-hidden-text-p 'headers))))
9462              s e)
9463         (delete-region (point-min) (point-max))
9464         (with-current-buffer gnus-original-article-buffer
9465           (goto-char (setq s (point-min)))
9466           (setq e (if (search-forward "\n\n" nil t)
9467                       (1- (point))
9468                     (point-max))))
9469         (insert-buffer-substring gnus-original-article-buffer s e)
9470         (run-hooks 'gnus-article-decode-hook)
9471         (if hidden
9472             (let ((gnus-treat-hide-headers nil)
9473                   (gnus-treat-hide-boring-headers nil))
9474               (gnus-delete-wash-type 'headers)
9475               (gnus-treat-article 'head))
9476           (gnus-treat-article 'head))
9477         (widen)
9478         (if window
9479             (set-window-start window (goto-char (point-min))))
9480         (if gnus-break-pages
9481             (gnus-narrow-to-page)
9482           (when (gnus-visual-p 'page-marker)
9483             (let ((inhibit-read-only t))
9484               (gnus-remove-text-with-property 'gnus-prev)
9485               (gnus-remove-text-with-property 'gnus-next))))
9486         (gnus-set-mode-line 'article)))))
9487
9488 (defun gnus-summary-show-all-headers ()
9489   "Make all header lines visible."
9490   (interactive)
9491   (gnus-summary-toggle-header 1))
9492
9493 (defun gnus-summary-caesar-message (&optional arg)
9494   "Caesar rotate the current article by 13.
9495 With a non-numerical prefix, also rotate headers.  A numerical
9496 prefix specifies how many places to rotate each letter forward."
9497   (interactive "P")
9498   (gnus-summary-select-article)
9499   (let ((mail-header-separator ""))
9500     (gnus-eval-in-buffer-window gnus-article-buffer
9501       (save-restriction
9502         (widen)
9503         (let ((start (window-start))
9504               (inhibit-read-only t))
9505           (if (equal arg '(4))
9506               (message-caesar-buffer-body nil t)
9507             (message-caesar-buffer-body arg))
9508           (set-window-start (get-buffer-window (current-buffer)) start)))))
9509   ;; Create buttons and stuff...
9510   (gnus-treat-article nil))
9511
9512 (declare-function idna-to-unicode "ext:idna" (str))
9513
9514 (defun gnus-summary-idna-message (&optional arg)
9515   "Decode IDNA encoded domain names in the current articles.
9516 IDNA encoded domain names looks like `xn--bar'.  If a string
9517 remain unencoded after running this function, it is likely an
9518 invalid IDNA string (`xn--bar' is invalid).
9519
9520 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9521 installed for this command to work."
9522   (interactive "P")
9523   (if (not (and (condition-case nil (require 'idna)
9524                   (file-error))
9525                 (mm-coding-system-p 'utf-8)
9526                 (executable-find (symbol-value 'idna-program))))
9527       (gnus-message
9528        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9529     (gnus-summary-select-article)
9530     (let ((mail-header-separator ""))
9531       (gnus-eval-in-buffer-window gnus-article-buffer
9532         (save-restriction
9533           (widen)
9534           (let ((start (window-start))
9535                 buffer-read-only)
9536             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9537               (replace-match (idna-to-unicode (match-string 1))))
9538             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9539
9540 (defun gnus-summary-morse-message (&optional arg)
9541   "Morse decode the current article."
9542   (interactive "P")
9543   (gnus-summary-select-article)
9544   (let ((mail-header-separator ""))
9545     (gnus-eval-in-buffer-window gnus-article-buffer
9546       (save-excursion
9547         (save-restriction
9548           (widen)
9549           (let ((pos (window-start))
9550                 (inhibit-read-only t))
9551             (goto-char (point-min))
9552             (when (message-goto-body)
9553               (gnus-narrow-to-body))
9554             (goto-char (point-min))
9555             (while (search-forward "·" (point-max) t)
9556               (replace-match "."))
9557             (unmorse-region (point-min) (point-max))
9558             (widen)
9559             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9560
9561 (defun gnus-summary-stop-page-breaking ()
9562   "Stop page breaking in the current article."
9563   (interactive)
9564   (gnus-summary-select-article)
9565   (gnus-eval-in-buffer-window gnus-article-buffer
9566     (widen)
9567     (when (gnus-visual-p 'page-marker)
9568       (let ((inhibit-read-only t))
9569         (gnus-remove-text-with-property 'gnus-prev)
9570         (gnus-remove-text-with-property 'gnus-next))
9571       (setq gnus-page-broken nil))))
9572
9573 (defun gnus-summary-move-article (&optional n to-newsgroup
9574                                             select-method action)
9575   "Move the current article to a different newsgroup.
9576 If N is a positive number, move the N next articles.
9577 If N is a negative number, move the N previous articles.
9578 If N is nil and any articles have been marked with the process mark,
9579 move those articles instead.
9580 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9581 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9582 re-spool using this method.
9583
9584 When called interactively with TO-NEWSGROUP being nil, the value of
9585 the variable `gnus-move-split-methods' is used for finding a default
9586 for the target newsgroup.
9587
9588 For this function to work, both the current newsgroup and the
9589 newsgroup that you want to move to have to support the `request-move'
9590 and `request-accept' functions.
9591
9592 ACTION can be either `move' (the default), `crosspost' or `copy'."
9593   (interactive "P")
9594   (unless action
9595     (setq action 'move))
9596   ;; Check whether the source group supports the required functions.
9597   (cond ((and (eq action 'move)
9598               (not (gnus-check-backend-function
9599                     'request-move-article gnus-newsgroup-name)))
9600          (error "The current group does not support article moving"))
9601         ((and (eq action 'crosspost)
9602               (not (gnus-check-backend-function
9603                     'request-replace-article gnus-newsgroup-name)))
9604          (error "The current group does not support article editing")))
9605   (let ((articles (gnus-summary-work-articles n))
9606         (prefix (if (gnus-check-backend-function
9607                      'request-move-article gnus-newsgroup-name)
9608                     (funcall gnus-move-group-prefix-function
9609                              gnus-newsgroup-name)
9610                   ""))
9611         (names '((move "Move" "Moving")
9612                  (copy "Copy" "Copying")
9613                  (crosspost "Crosspost" "Crossposting")))
9614         (copy-buf (save-excursion
9615                     (nnheader-set-temp-buffer " *copy article*")))
9616         art-group to-method new-xref article to-groups
9617         articles-to-update-marks encoded)
9618     (unless (assq action names)
9619       (error "Unknown action %s" action))
9620     ;; Read the newsgroup name.
9621     (when (and (not to-newsgroup)
9622                (not select-method))
9623       (if (and gnus-move-split-methods
9624                (not
9625                 (and (memq gnus-current-article articles)
9626                      (gnus-buffer-live-p gnus-original-article-buffer))))
9627           ;; When `gnus-move-split-methods' is non-nil, we have to
9628           ;; select an article to give `gnus-read-move-group-name' an
9629           ;; opportunity to suggest an appropriate default.  However,
9630           ;; we needn't render or mark the article.
9631           (let ((gnus-display-mime-function nil)
9632                 (gnus-article-prepare-hook nil)
9633                 (gnus-mark-article-hook nil))
9634             (gnus-summary-select-article nil nil nil (car articles))))
9635       (setq to-newsgroup (gnus-read-move-group-name
9636                           (cadr (assq action names))
9637                           (symbol-value
9638                            (intern (format "gnus-current-%s-group" action)))
9639                           articles prefix)
9640             encoded to-newsgroup
9641             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9642       (set (intern (format "gnus-current-%s-group" action))
9643            (mm-decode-coding-string
9644             to-newsgroup
9645             (gnus-group-name-charset to-method to-newsgroup))))
9646     (unless to-method
9647       (setq to-method (or select-method
9648                           (gnus-server-to-method
9649                            (gnus-group-method to-newsgroup)))))
9650     (setq to-newsgroup
9651           (or encoded
9652               (and to-newsgroup
9653                    (mm-encode-coding-string
9654                     to-newsgroup
9655                     (gnus-group-name-charset to-method to-newsgroup)))))
9656     ;; Check the method we are to move this article to...
9657     (unless (gnus-check-backend-function
9658              'request-accept-article (car to-method))
9659       (error "%s does not support article copying" (car to-method)))
9660     (unless (gnus-check-server to-method)
9661       (error "Can't open server %s" (car to-method)))
9662     (gnus-message 6 "%s to %s: %s..."
9663                   (caddr (assq action names))
9664                   (or (car select-method)
9665                       (gnus-group-decoded-name to-newsgroup))
9666                   articles)
9667     (while articles
9668       (setq article (pop articles))
9669       (setq
9670        art-group
9671        (cond
9672         ;; Move the article.
9673         ((eq action 'move)
9674          ;; Remove this article from future suppression.
9675          (gnus-dup-unsuppress-article article)
9676          (let* ((from-method (gnus-find-method-for-group
9677                               gnus-newsgroup-name))
9678                 (to-method (or select-method
9679                                (gnus-find-method-for-group to-newsgroup)))
9680                 (move-is-internal (gnus-method-equal from-method to-method)))
9681            (gnus-request-move-article
9682             article                     ; Article to move
9683             gnus-newsgroup-name         ; From newsgroup
9684             (nth 1 (gnus-find-method-for-group
9685                     gnus-newsgroup-name)) ; Server
9686             (list 'gnus-request-accept-article
9687                   to-newsgroup (list 'quote select-method)
9688                   (not articles) t)     ; Accept form
9689             (not articles)              ; Only save nov last time
9690             move-is-internal)))         ; is this move internal?
9691         ;; Copy the article.
9692         ((eq action 'copy)
9693          (with-current-buffer copy-buf
9694            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9695              (save-restriction
9696                (nnheader-narrow-to-headers)
9697                (dolist (hdr gnus-copy-article-ignored-headers)
9698                  (message-remove-header hdr t)))
9699              (gnus-request-accept-article
9700               to-newsgroup select-method (not articles) t))))
9701         ;; Crosspost the article.
9702         ((eq action 'crosspost)
9703          (let ((xref (message-tokenize-header
9704                       (mail-header-xref (gnus-summary-article-header article))
9705                       " ")))
9706            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9707                                   ":" (number-to-string article)))
9708            (unless xref
9709              (setq xref (list (system-name))))
9710            (setq new-xref
9711                  (concat
9712                   (mapconcat 'identity
9713                              (delete "Xref:" (delete new-xref xref))
9714                              " ")
9715                   " " new-xref))
9716            (with-current-buffer copy-buf
9717              ;; First put the article in the destination group.
9718              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9719              (when (consp (setq art-group
9720                                 (gnus-request-accept-article
9721                                  to-newsgroup select-method (not articles) t)))
9722                (setq new-xref (concat new-xref " " (car art-group)
9723                                       ":"
9724                                       (number-to-string (cdr art-group))))
9725                ;; Now we have the new Xrefs header, so we insert
9726                ;; it and replace the new article.
9727                (nnheader-replace-header "Xref" new-xref)
9728                (gnus-request-replace-article
9729                 (cdr art-group) to-newsgroup (current-buffer) t)
9730                art-group))))))
9731       (cond
9732        ((not art-group)
9733         (gnus-message 1 "Couldn't %s article %s: %s"
9734                       (cadr (assq action names)) article
9735                       (nnheader-get-report (car to-method))))
9736        ((eq art-group 'junk)
9737         (when (eq action 'move)
9738           (gnus-summary-mark-article article gnus-canceled-mark)
9739           (gnus-message 4 "Deleted article %s" article)
9740           ;; run the delete hook
9741           (run-hook-with-args 'gnus-summary-article-delete-hook
9742                               action
9743                               (gnus-data-header
9744                                (assoc article (gnus-data-list nil)))
9745                               gnus-newsgroup-name nil
9746                               select-method)))
9747        (t
9748         (let* ((pto-group (gnus-group-prefixed-name
9749                            (car art-group) to-method))
9750                (info (gnus-get-info pto-group))
9751                (to-group (gnus-info-group info))
9752                to-marks)
9753           ;; Update the group that has been moved to.
9754           (when (and info
9755                      (memq action '(move copy)))
9756             (unless (member to-group to-groups)
9757               (push to-group to-groups))
9758
9759             (unless (memq article gnus-newsgroup-unreads)
9760               (push 'read to-marks)
9761               (gnus-info-set-read
9762                info (gnus-add-to-range (gnus-info-read info)
9763                                        (list (cdr art-group)))))
9764
9765             ;; See whether the article is to be put in the cache.
9766             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9767                    (marks (if expirable
9768                               gnus-article-mark-lists
9769                             (delete '(expirable . expire)
9770                                     (copy-sequence gnus-article-mark-lists))))
9771                    (to-article (cdr art-group)))
9772
9773               ;; Enter the article into the cache in the new group,
9774               ;; if that is required.
9775               (when gnus-use-cache
9776                 (gnus-cache-possibly-enter-article
9777                  to-group to-article
9778                  (memq article gnus-newsgroup-marked)
9779                  (memq article gnus-newsgroup-dormant)
9780                  (memq article gnus-newsgroup-unreads)))
9781
9782               (when gnus-preserve-marks
9783                 ;; Copy any marks over to the new group.
9784                 (when (and (equal to-group gnus-newsgroup-name)
9785                            (not (memq article gnus-newsgroup-unreads)))
9786                   ;; Mark this article as read in this group.
9787                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9788                   (setcdr (gnus-active to-group) to-article)
9789                   (setcdr gnus-newsgroup-active to-article))
9790
9791                 (while marks
9792                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9793                     (when (memq article (symbol-value
9794                                          (intern (format "gnus-newsgroup-%s"
9795                                                          (caar marks)))))
9796                       (push (cdar marks) to-marks)
9797                       ;; If the other group is the same as this group,
9798                       ;; then we have to add the mark to the list.
9799                       (when (equal to-group gnus-newsgroup-name)
9800                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9801                              (cons to-article
9802                                    (symbol-value
9803                                     (intern (format "gnus-newsgroup-%s"
9804                                                     (caar marks)))))))
9805                       ;; Copy the marks to other group.
9806                       (gnus-add-marked-articles
9807                        to-group (cdar marks) (list to-article) info)))
9808                   (setq marks (cdr marks)))
9809
9810                 (when (and expirable
9811                            gnus-mark-copied-or-moved-articles-as-expirable
9812                            (not (memq 'expire to-marks)))
9813                   ;; Mark this article as expirable.
9814                   (push 'expire to-marks)
9815                   (when (equal to-group gnus-newsgroup-name)
9816                     (push to-article gnus-newsgroup-expirable))
9817                   ;; Copy the expirable mark to other group.
9818                   (gnus-add-marked-articles
9819                    to-group 'expire (list to-article) info))
9820
9821                 (gnus-request-set-mark
9822                  to-group (list (list (list to-article) 'add to-marks))))
9823
9824               (gnus-dribble-enter
9825                (concat "(gnus-group-set-info '"
9826                        (gnus-prin1-to-string (gnus-get-info to-group))
9827                        ")"))))
9828
9829           ;; Update the Xref header in this article to point to
9830           ;; the new crossposted article we have just created.
9831           (when (eq action 'crosspost)
9832             (with-current-buffer copy-buf
9833               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9834               (nnheader-replace-header "Xref" new-xref)
9835               (gnus-request-replace-article
9836                article gnus-newsgroup-name (current-buffer) t)))
9837
9838           ;; run the move/copy/crosspost/respool hook
9839           (run-hook-with-args 'gnus-summary-article-move-hook
9840                               action
9841                               (gnus-data-header
9842                                (assoc article (gnus-data-list nil)))
9843                               gnus-newsgroup-name
9844                               to-newsgroup
9845                               select-method))
9846
9847         ;;;!!!Why is this necessary?
9848         (set-buffer gnus-summary-buffer)
9849
9850         (when (eq action 'move)
9851           (save-excursion
9852             (gnus-summary-goto-subject article)
9853             (gnus-summary-mark-article article gnus-canceled-mark)))))
9854       (push article articles-to-update-marks))
9855
9856     (save-excursion
9857       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9858     ;; Re-activate all groups that have been moved to.
9859     (with-current-buffer gnus-group-buffer
9860       (let ((gnus-group-marked to-groups))
9861         (gnus-group-get-new-news-this-group nil t)))
9862
9863     (gnus-kill-buffer copy-buf)
9864     (gnus-summary-position-point)
9865     (gnus-set-mode-line 'summary)))
9866
9867 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9868   "Copy the current article to some other group.
9869 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9870 When called interactively, if TO-NEWSGROUP is nil, use the value of
9871 the variable `gnus-move-split-methods' for finding a default target
9872 newsgroup.
9873 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9874 re-spool using this method."
9875   (interactive "P")
9876   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9877
9878 (defun gnus-summary-crosspost-article (&optional n)
9879   "Crosspost the current article to some other group."
9880   (interactive "P")
9881   (gnus-summary-move-article n nil nil 'crosspost))
9882
9883 (defcustom gnus-summary-respool-default-method nil
9884   "Default method type for respooling an article.
9885 If nil, use to the current newsgroup method."
9886   :type 'symbol
9887   :group 'gnus-summary-mail)
9888
9889 (defun gnus-summary-respool-article (&optional n method)
9890   "Respool the current article.
9891 The article will be squeezed through the mail spooling process again,
9892 which means that it will be put in some mail newsgroup or other
9893 depending on `nnmail-split-methods'.
9894 If N is a positive number, respool the N next articles.
9895 If N is a negative number, respool the N previous articles.
9896 If N is nil and any articles have been marked with the process mark,
9897 respool those articles instead.
9898
9899 Respooling can be done both from mail groups and \"real\" newsgroups.
9900 In the former case, the articles in question will be moved from the
9901 current group into whatever groups they are destined to.  In the
9902 latter case, they will be copied into the relevant groups."
9903   (interactive
9904    (list current-prefix-arg
9905          (let* ((methods (gnus-methods-using 'respool))
9906                 (methname
9907                  (symbol-name (or gnus-summary-respool-default-method
9908                                   (car (gnus-find-method-for-group
9909                                         gnus-newsgroup-name)))))
9910                 (method
9911                  (gnus-completing-read-with-default
9912                   methname "Backend to use when respooling"
9913                   methods nil t nil 'gnus-mail-method-history))
9914                 ms)
9915            (cond
9916             ((zerop (length (setq ms (gnus-servers-using-backend
9917                                       (intern method)))))
9918              (list (intern method) ""))
9919             ((= 1 (length ms))
9920              (car ms))
9921             (t
9922              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9923                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9924                            ms-alist))))))))
9925   (unless method
9926     (error "No method given for respooling"))
9927   (if (assoc (symbol-name
9928               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9929              (gnus-methods-using 'respool))
9930       (gnus-summary-move-article n nil method)
9931     (gnus-summary-copy-article n nil method)))
9932
9933 (defun gnus-summary-import-article (file &optional edit)
9934   "Import an arbitrary file into a mail newsgroup."
9935   (interactive "fImport file: \nP")
9936   (let ((group gnus-newsgroup-name)
9937         (now (current-time))
9938         atts lines group-art)
9939     (unless (gnus-check-backend-function 'request-accept-article group)
9940       (error "%s does not support article importing" group))
9941     (or (file-readable-p file)
9942         (not (file-regular-p file))
9943         (error "Can't read %s" file))
9944     (with-current-buffer (gnus-get-buffer-create " *import file*")
9945       (erase-buffer)
9946       (nnheader-insert-file-contents file)
9947       (goto-char (point-min))
9948       (if (nnheader-article-p)
9949           (save-restriction
9950             (goto-char (point-min))
9951             (search-forward "\n\n" nil t)
9952             (narrow-to-region (point-min) (1- (point)))
9953             (goto-char (point-min))
9954             (unless (re-search-forward "^date:" nil t)
9955               (goto-char (point-max))
9956               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9957        ;; This doesn't look like an article, so we fudge some headers.
9958         (setq atts (file-attributes file)
9959               lines (count-lines (point-min) (point-max)))
9960         (insert "From: " (read-string "From: ") "\n"
9961                 "Subject: " (read-string "Subject: ") "\n"
9962                 "Date: " (message-make-date (nth 5 atts)) "\n"
9963                 "Message-ID: " (message-make-message-id) "\n"
9964                 "Lines: " (int-to-string lines) "\n"
9965                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9966       (setq group-art (gnus-request-accept-article group nil t))
9967       (kill-buffer (current-buffer)))
9968     (setq gnus-newsgroup-active (gnus-activate-group group))
9969     (forward-line 1)
9970     (gnus-summary-goto-article (cdr group-art) nil t)
9971     (when edit
9972       (gnus-summary-edit-article))))
9973
9974 (defun gnus-summary-create-article ()
9975   "Create an article in a mail newsgroup."
9976   (interactive)
9977   (let ((group gnus-newsgroup-name)
9978         (now (current-time))
9979         group-art)
9980     (unless (gnus-check-backend-function 'request-accept-article group)
9981       (error "%s does not support article importing" group))
9982     (with-current-buffer (gnus-get-buffer-create " *import file*")
9983       (erase-buffer)
9984       (goto-char (point-min))
9985       ;; This doesn't look like an article, so we fudge some headers.
9986       (insert "From: " (read-string "From: ") "\n"
9987               "Subject: " (read-string "Subject: ") "\n"
9988               "Date: " (message-make-date now) "\n"
9989               "Message-ID: " (message-make-message-id) "\n")
9990       (setq group-art (gnus-request-accept-article group nil t))
9991       (kill-buffer (current-buffer)))
9992     (setq gnus-newsgroup-active (gnus-activate-group group))
9993     (forward-line 1)
9994     (gnus-summary-goto-article (cdr group-art) nil t)
9995     (gnus-summary-edit-article)))
9996
9997 (defun gnus-summary-article-posted-p ()
9998   "Say whether the current (mail) article is available from news as well.
9999 This will be the case if the article has both been mailed and posted."
10000   (interactive)
10001   (let ((id (mail-header-references (gnus-summary-article-header)))
10002         (gnus-override-method (car (gnus-refer-article-methods))))
10003     (if (gnus-request-head id "")
10004         (gnus-message 2 "The current message was found on %s"
10005                       gnus-override-method)
10006       (gnus-message 2 "The current message couldn't be found on %s"
10007                     gnus-override-method)
10008       nil)))
10009
10010 (defun gnus-summary-expire-articles (&optional now)
10011   "Expire all articles that are marked as expirable in the current group."
10012   (interactive)
10013   (when (and (not gnus-group-is-exiting-without-update-p)
10014              (gnus-check-backend-function
10015               'request-expire-articles gnus-newsgroup-name))
10016     ;; This backend supports expiry.
10017     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10018            (expirable (if total
10019                           (progn
10020                             ;; We need to update the info for
10021                             ;; this group for `gnus-list-of-read-articles'
10022                             ;; to give us the right answer.
10023                             (gnus-run-hooks 'gnus-exit-group-hook)
10024                             (gnus-summary-update-info)
10025                             (gnus-list-of-read-articles gnus-newsgroup-name))
10026                         (setq gnus-newsgroup-expirable
10027                               (sort gnus-newsgroup-expirable '<))))
10028            (expiry-wait (if now 'immediate
10029                           (gnus-group-find-parameter
10030                            gnus-newsgroup-name 'expiry-wait)))
10031            (nnmail-expiry-target
10032             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10033                 nnmail-expiry-target))
10034            es)
10035       (when expirable
10036         ;; There are expirable articles in this group, so we run them
10037         ;; through the expiry process.
10038         (gnus-message 6 "Expiring articles...")
10039         (unless (gnus-check-group gnus-newsgroup-name)
10040           (error "Can't open server for %s" gnus-newsgroup-name))
10041         ;; The list of articles that weren't expired is returned.
10042         (save-excursion
10043           (if expiry-wait
10044               (let ((nnmail-expiry-wait-function nil)
10045                     (nnmail-expiry-wait expiry-wait))
10046                 (setq es (gnus-request-expire-articles
10047                           expirable gnus-newsgroup-name)))
10048             (setq es (gnus-request-expire-articles
10049                       expirable gnus-newsgroup-name)))
10050           (unless total
10051             (setq gnus-newsgroup-expirable es))
10052           ;; We go through the old list of expirable, and mark all
10053           ;; really expired articles as nonexistent.
10054           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10055             (let ((gnus-use-cache nil))
10056               (dolist (article expirable)
10057                 (when (and (not (memq article es))
10058                            (gnus-data-find article))
10059                   (gnus-summary-mark-article article gnus-canceled-mark)
10060                   (run-hook-with-args 'gnus-summary-article-expire-hook
10061                                       'delete
10062                                       (gnus-data-header
10063                                        (assoc article (gnus-data-list nil)))
10064                                       gnus-newsgroup-name
10065                                       nil
10066                                       nil))))))
10067         (gnus-message 6 "Expiring articles...done")))))
10068
10069 (defun gnus-summary-expire-articles-now ()
10070   "Expunge all expirable articles in the current group.
10071 This means that *all* articles that are marked as expirable will be
10072 deleted forever, right now."
10073   (interactive)
10074   (or gnus-expert-user
10075       (gnus-yes-or-no-p
10076        "Are you really, really sure you want to delete all expirable messages? ")
10077       (error "Phew!"))
10078   (gnus-summary-expire-articles t))
10079
10080 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10081 (defun gnus-summary-delete-article (&optional n)
10082   "Delete the N next (mail) articles.
10083 This command actually deletes articles.  This is not a marking
10084 command.  The article will disappear forever from your life, never to
10085 return.
10086
10087 If N is negative, delete backwards.
10088 If N is nil and articles have been marked with the process mark,
10089 delete these instead.
10090
10091 If `gnus-novice-user' is non-nil you will be asked for
10092 confirmation before the articles are deleted."
10093   (interactive "P")
10094   (unless (gnus-check-backend-function 'request-expire-articles
10095                                        gnus-newsgroup-name)
10096     (error "The current newsgroup does not support article deletion"))
10097   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10098     (error "Couldn't open server"))
10099   ;; Compute the list of articles to delete.
10100   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10101         (nnmail-expiry-target 'delete)
10102         not-deleted)
10103     (if (and gnus-novice-user
10104              (not (gnus-yes-or-no-p
10105                    (format "Do you really want to delete %s forever? "
10106                            (if (> (length articles) 1)
10107                                (format "these %s articles" (length articles))
10108                              "this article")))))
10109         ()
10110       ;; Delete the articles.
10111       (setq not-deleted (gnus-request-expire-articles
10112                          articles gnus-newsgroup-name 'force))
10113       (save-excursion
10114         (while articles
10115           (gnus-summary-remove-process-mark (car articles))
10116           ;; The backend might not have been able to delete the article
10117           ;; after all.
10118           (unless (memq (car articles) not-deleted)
10119             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10120           (let* ((article (car articles))
10121                  (ghead  (gnus-data-header
10122                           (assoc article (gnus-data-list nil)))))
10123             (run-hook-with-args 'gnus-summary-article-delete-hook
10124                                 'delete ghead gnus-newsgroup-name nil
10125                                 nil))
10126           (setq articles (cdr articles))))
10127       (when not-deleted
10128         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10129     (gnus-summary-position-point)
10130     (gnus-set-mode-line 'summary)
10131     not-deleted))
10132
10133 (defun gnus-summary-edit-article (&optional arg)
10134   "Edit the current article.
10135 This will have permanent effect only in mail groups.
10136 If ARG is nil, edit the decoded articles.
10137 If ARG is 1, edit the raw articles.
10138 If ARG is 2, edit the raw articles even in read-only groups.
10139 If ARG is 3, edit the articles with the current handles.
10140 Otherwise, allow editing of articles even in read-only
10141 groups."
10142   (interactive "P")
10143   (let (force raw current-handles)
10144     (cond
10145      ((null arg))
10146      ((eq arg 1)
10147       (setq raw t))
10148      ((eq arg 2)
10149       (setq raw t
10150             force t))
10151      ((eq arg 3)
10152       (setq current-handles
10153             (and (gnus-buffer-live-p gnus-article-buffer)
10154                  (with-current-buffer gnus-article-buffer
10155                    (prog1
10156                        gnus-article-mime-handles
10157                      (setq gnus-article-mime-handles nil))))))
10158      (t
10159       (setq force t)))
10160     (when (and raw (not force)
10161                (member gnus-newsgroup-name '("nndraft:delayed"
10162                                              "nndraft:drafts"
10163                                              "nndraft:queue")))
10164       (error "Can't edit the raw article in group %s"
10165              gnus-newsgroup-name))
10166     (with-current-buffer gnus-summary-buffer
10167       (let ((mail-parse-charset gnus-newsgroup-charset)
10168             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10169         (gnus-set-global-variables)
10170         (when (and (not force)
10171                    (gnus-group-read-only-p))
10172           (error "The current newsgroup does not support article editing"))
10173         (gnus-summary-show-article t)
10174         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10175           (with-current-buffer gnus-article-buffer
10176             (mm-enable-multibyte)))
10177         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10178             (setq raw t))
10179         (gnus-article-edit-article
10180          (if raw 'ignore
10181            `(lambda ()
10182               (let ((mbl mml-buffer-list))
10183                 (setq mml-buffer-list nil)
10184                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10185                   (mime-to-mml ,'current-handles))
10186                 (let ((mbl1 mml-buffer-list))
10187                   (setq mml-buffer-list mbl)
10188                   (set (make-local-variable 'mml-buffer-list) mbl1))
10189                 (gnus-make-local-hook 'kill-buffer-hook)
10190                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10191          `(lambda (no-highlight)
10192             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10193                   (message-options message-options)
10194                   (message-options-set-recipient)
10195                   (mail-parse-ignored-charsets
10196                    ',gnus-newsgroup-ignored-charsets)
10197                   (rfc2047-header-encoding-alist
10198                    ',(let ((charset (gnus-group-name-charset
10199                                      (gnus-find-method-for-group
10200                                       gnus-newsgroup-name)
10201                                      gnus-newsgroup-name)))
10202                        (append (list (cons "Newsgroups" charset)
10203                                      (cons "Followup-To" charset)
10204                                      (cons "Xref" charset))
10205                                rfc2047-header-encoding-alist))))
10206               ,(if (not raw) '(progn
10207                                 (mml-to-mime)
10208                                 (mml-destroy-buffers)
10209                                 (remove-hook 'kill-buffer-hook
10210                                              'mml-destroy-buffers t)
10211                                 (kill-local-variable 'mml-buffer-list)))
10212               (gnus-summary-edit-article-done
10213                ,(or (mail-header-references gnus-current-headers) "")
10214                ,(gnus-group-read-only-p)
10215                ,gnus-summary-buffer no-highlight))))))))
10216
10217 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10218
10219 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10220                                                  no-highlight)
10221   "Make edits to the current article permanent."
10222   (interactive)
10223   (save-excursion
10224    ;; The buffer restriction contains the entire article if it exists.
10225     (when (article-goto-body)
10226       (let ((lines (count-lines (point) (point-max)))
10227             (length (- (point-max) (point)))
10228             (case-fold-search t)
10229             (body (copy-marker (point))))
10230         (goto-char (point-min))
10231         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10232           (delete-region (match-beginning 1) (match-end 1))
10233           (insert (number-to-string length)))
10234         (goto-char (point-min))
10235         (when (re-search-forward
10236                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10237           (delete-region (match-beginning 1) (match-end 1))
10238           (insert (number-to-string length)))
10239         (goto-char (point-min))
10240         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10241           (delete-region (match-beginning 1) (match-end 1))
10242           (insert (number-to-string lines))))))
10243   ;; Replace the article.
10244   (let ((buf (current-buffer)))
10245     (with-temp-buffer
10246       (insert-buffer-substring buf)
10247
10248       (if (and (not read-only)
10249                (not (gnus-request-replace-article
10250                      (cdr gnus-article-current) (car gnus-article-current)
10251                      (current-buffer) t)))
10252           (error "Couldn't replace article")
10253         ;; Update the summary buffer.
10254         (if (and references
10255                  (equal (message-tokenize-header references " ")
10256                         (message-tokenize-header
10257                          (or (message-fetch-field "references") "") " ")))
10258             ;; We only have to update this line.
10259             (save-excursion
10260               (save-restriction
10261                 (message-narrow-to-head)
10262                 (let ((head (buffer-substring-no-properties
10263                              (point-min) (point-max)))
10264                       header)
10265                   (with-temp-buffer
10266                     (insert (format "211 %d Article retrieved.\n"
10267                                     (cdr gnus-article-current)))
10268                     (insert head)
10269                     (insert ".\n")
10270                     (let ((nntp-server-buffer (current-buffer)))
10271                       (setq header (car (gnus-get-newsgroup-headers
10272                                          nil t))))
10273                     (with-current-buffer gnus-summary-buffer
10274                       (gnus-data-set-header
10275                        (gnus-data-find (cdr gnus-article-current))
10276                        header)
10277                       (gnus-summary-update-article-line
10278                        (cdr gnus-article-current) header)
10279                       (if (gnus-summary-goto-subject
10280                            (cdr gnus-article-current) nil t)
10281                           (gnus-summary-update-secondary-mark
10282                            (cdr gnus-article-current))))))))
10283           ;; Update threads.
10284           (set-buffer (or buffer gnus-summary-buffer))
10285           (gnus-summary-update-article (cdr gnus-article-current))
10286           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10287               (gnus-summary-update-secondary-mark
10288                (cdr gnus-article-current))))
10289         ;; Prettify the article buffer again.
10290         (unless no-highlight
10291           (with-current-buffer gnus-article-buffer
10292             ;;;!!! Fix this -- article should be rehighlighted.
10293             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10294             (set-buffer gnus-original-article-buffer)
10295             (gnus-request-article
10296              (cdr gnus-article-current)
10297              (car gnus-article-current) (current-buffer))))
10298         ;; Prettify the summary buffer line.
10299         (when (gnus-visual-p 'summary-highlight 'highlight)
10300           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10301
10302 (defun gnus-summary-edit-wash (key)
10303   "Perform editing command KEY in the article buffer."
10304   (interactive
10305    (list
10306     (progn
10307       (message "%s" (concat (this-command-keys) "- "))
10308       (read-char))))
10309   (message "")
10310   (gnus-summary-edit-article)
10311   (execute-kbd-macro (concat (this-command-keys) key))
10312   (gnus-article-edit-done))
10313
10314 ;;; Respooling
10315
10316 (defun gnus-summary-respool-query (&optional silent trace)
10317   "Query where the respool algorithm would put this article."
10318   (interactive)
10319   (let (gnus-mark-article-hook)
10320     (gnus-summary-select-article)
10321     (with-current-buffer gnus-original-article-buffer
10322       (let ((groups (nnmail-article-group 'identity trace)))
10323         (unless silent
10324           (if groups
10325               (message "This message would go to %s"
10326                        (mapconcat 'car groups ", "))
10327             (message "This message would go to no groups"))
10328           groups)))))
10329
10330 (defun gnus-summary-respool-trace ()
10331   "Trace where the respool algorithm would put this article.
10332 Display a buffer showing all fancy splitting patterns which matched."
10333   (interactive)
10334   (gnus-summary-respool-query nil t))
10335
10336 ;; Summary marking commands.
10337
10338 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10339   "Mark articles which has the same subject as read, and then select the next.
10340 If UNMARK is positive, remove any kind of mark.
10341 If UNMARK is negative, tick articles."
10342   (interactive "P")
10343   (when unmark
10344     (setq unmark (prefix-numeric-value unmark)))
10345   (let ((count
10346          (gnus-summary-mark-same-subject
10347           (gnus-summary-article-subject) unmark)))
10348     ;; Select next unread article.  If auto-select-same mode, should
10349     ;; select the first unread article.
10350     (gnus-summary-next-article t (and gnus-auto-select-same
10351                                       (gnus-summary-article-subject)))
10352     (gnus-message 7 "%d article%s marked as %s"
10353                   count (if (= count 1) " is" "s are")
10354                   (if unmark "unread" "read"))))
10355
10356 (defun gnus-summary-kill-same-subject (&optional unmark)
10357   "Mark articles which has the same subject as read.
10358 If UNMARK is positive, remove any kind of mark.
10359 If UNMARK is negative, tick articles."
10360   (interactive "P")
10361   (when unmark
10362     (setq unmark (prefix-numeric-value unmark)))
10363   (let ((count
10364          (gnus-summary-mark-same-subject
10365           (gnus-summary-article-subject) unmark)))
10366     ;; If marked as read, go to next unread subject.
10367     (when (null unmark)
10368       ;; Go to next unread subject.
10369       (gnus-summary-next-subject 1 t))
10370     (gnus-message 7 "%d articles are marked as %s"
10371                   count (if unmark "unread" "read"))))
10372
10373 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10374   "Mark articles with same SUBJECT as read, and return marked number.
10375 If optional argument UNMARK is positive, remove any kinds of marks.
10376 If optional argument UNMARK is negative, mark articles as unread instead."
10377   (let ((count 1))
10378     (save-excursion
10379       (cond
10380        ((null unmark)                   ; Mark as read.
10381         (while (and
10382                 (progn
10383                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10384                   (gnus-summary-show-thread) t)
10385                 (gnus-summary-find-subject subject))
10386           (setq count (1+ count))))
10387        ((> unmark 0)                    ; Tick.
10388         (while (and
10389                 (progn
10390                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10391                   (gnus-summary-show-thread) t)
10392                 (gnus-summary-find-subject subject))
10393           (setq count (1+ count))))
10394        (t                               ; Mark as unread.
10395         (while (and
10396                 (progn
10397                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10398                   (gnus-summary-show-thread) t)
10399                 (gnus-summary-find-subject subject))
10400           (setq count (1+ count)))))
10401       (gnus-set-mode-line 'summary)
10402       ;; Return the number of marked articles.
10403       count)))
10404
10405 (defun gnus-summary-mark-as-processable (n &optional unmark)
10406   "Set the process mark on the next N articles.
10407 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10408 the process mark instead.  The difference between N and the actual
10409 number of articles marked is returned."
10410   (interactive "P")
10411   (if (and (null n) (gnus-region-active-p))
10412       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10413     (setq n (prefix-numeric-value n))
10414     (let ((backward (< n 0))
10415           (n (abs n)))
10416       (while (and
10417               (> n 0)
10418               (if unmark
10419                   (gnus-summary-remove-process-mark
10420                    (gnus-summary-article-number))
10421                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10422               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10423         (setq n (1- n)))
10424       (when (/= 0 n)
10425         (gnus-message 7 "No more articles"))
10426       (gnus-summary-recenter)
10427       (gnus-summary-position-point)
10428       n)))
10429
10430 (defun gnus-summary-unmark-as-processable (n)
10431   "Remove the process mark from the next N articles.
10432 If N is negative, unmark backward instead.  The difference between N and
10433 the actual number of articles unmarked is returned."
10434   (interactive "P")
10435   (gnus-summary-mark-as-processable n t))
10436
10437 (defun gnus-summary-unmark-all-processable ()
10438   "Remove the process mark from all articles."
10439   (interactive)
10440   (save-excursion
10441     (while gnus-newsgroup-processable
10442       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10443   (gnus-summary-position-point))
10444
10445 (defun gnus-summary-add-mark (article type)
10446   "Mark ARTICLE with a mark of TYPE."
10447   (let ((vtype (car (assq type gnus-article-mark-lists)))
10448         var)
10449     (if (not vtype)
10450         (error "No such mark type: %s" type)
10451       (setq var (intern (format "gnus-newsgroup-%s" type)))
10452       (set var (cons article (symbol-value var)))
10453       (if (memq type '(processable cached replied forwarded recent saved))
10454           (gnus-summary-update-secondary-mark article)
10455         ;;; !!! This is bogus.  We should find out what primary
10456         ;;; !!! mark we want to set.
10457         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10458
10459 (defun gnus-summary-mark-as-expirable (n)
10460   "Mark N articles forward as expirable.
10461 If N is negative, mark backward instead.  The difference between N and
10462 the actual number of articles marked is returned."
10463   (interactive "p")
10464   (gnus-summary-mark-forward n gnus-expirable-mark))
10465
10466 (defun gnus-summary-mark-as-spam (n)
10467   "Mark N articles forward as spam.
10468 If N is negative, mark backward instead.  The difference between N and
10469 the actual number of articles marked is returned."
10470   (interactive "p")
10471   (gnus-summary-mark-forward n gnus-spam-mark))
10472
10473 (defun gnus-summary-mark-article-as-replied (article)
10474   "Mark ARTICLE as replied to and update the summary line.
10475 ARTICLE can also be a list of articles."
10476   (interactive (list (gnus-summary-article-number)))
10477   (let ((articles (if (listp article) article (list article))))
10478     (dolist (article articles)
10479       (unless (numberp article)
10480         (error "%s is not a number" article))
10481       (push article gnus-newsgroup-replied)
10482       (let ((inhibit-read-only t))
10483         (when (gnus-summary-goto-subject article nil t)
10484           (gnus-summary-update-secondary-mark article))))))
10485
10486 (defun gnus-summary-mark-article-as-forwarded (article)
10487   "Mark ARTICLE as forwarded and update the summary line.
10488 ARTICLE can also be a list of articles."
10489   (let ((articles (if (listp article) article (list article))))
10490     (dolist (article articles)
10491       (push article gnus-newsgroup-forwarded)
10492       (let ((inhibit-read-only t))
10493         (when (gnus-summary-goto-subject article nil t)
10494           (gnus-summary-update-secondary-mark article))))))
10495
10496 (defun gnus-summary-set-bookmark (article)
10497   "Set a bookmark in current article."
10498   (interactive (list (gnus-summary-article-number)))
10499   (when (or (not (get-buffer gnus-article-buffer))
10500             (not gnus-current-article)
10501             (not gnus-article-current)
10502             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10503     (error "No current article selected"))
10504   ;; Remove old bookmark, if one exists.
10505   (gnus-pull article gnus-newsgroup-bookmarks)
10506   ;; Set the new bookmark, which is on the form
10507   ;; (article-number . line-number-in-body).
10508   (push
10509    (cons article
10510          (with-current-buffer gnus-article-buffer
10511            (count-lines
10512             (min (point)
10513                  (save-excursion
10514                    (article-goto-body)
10515                    (point)))
10516             (point))))
10517    gnus-newsgroup-bookmarks)
10518   (gnus-message 6 "A bookmark has been added to the current article."))
10519
10520 (defun gnus-summary-remove-bookmark (article)
10521   "Remove the bookmark from the current article."
10522   (interactive (list (gnus-summary-article-number)))
10523   ;; Remove old bookmark, if one exists.
10524   (if (not (assq article gnus-newsgroup-bookmarks))
10525       (gnus-message 6 "No bookmark in current article.")
10526     (gnus-pull article gnus-newsgroup-bookmarks)
10527     (gnus-message 6 "Removed bookmark.")))
10528
10529 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10530 (defun gnus-summary-mark-as-dormant (n)
10531   "Mark N articles forward as dormant.
10532 If N is negative, mark backward instead.  The difference between N and
10533 the actual number of articles marked is returned."
10534   (interactive "p")
10535   (gnus-summary-mark-forward n gnus-dormant-mark))
10536
10537 (defun gnus-summary-set-process-mark (article)
10538   "Set the process mark on ARTICLE and update the summary line."
10539   (setq gnus-newsgroup-processable
10540         (cons article
10541               (delq article gnus-newsgroup-processable)))
10542   (when (gnus-summary-goto-subject article)
10543     (gnus-summary-show-thread)
10544     (gnus-summary-goto-subject article)
10545     (gnus-summary-update-secondary-mark article)))
10546
10547 (defun gnus-summary-remove-process-mark (&rest articles)
10548   "Remove the process mark from ARTICLES and update the summary line."
10549   (dolist (article articles)
10550     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10551     (when (gnus-summary-goto-subject article)
10552       (gnus-summary-show-thread)
10553       (gnus-summary-goto-subject article)
10554       (gnus-summary-update-secondary-mark article)))
10555   t)
10556
10557 (defun gnus-summary-set-saved-mark (article)
10558   "Set the process mark on ARTICLE and update the summary line."
10559   (push article gnus-newsgroup-saved)
10560   (when (gnus-summary-goto-subject article)
10561     (gnus-summary-update-secondary-mark article)))
10562
10563 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10564   "Mark N articles as read forwards.
10565 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10566 The difference between N and the actual number of articles marked is
10567 returned.
10568 If NO-EXPIRE, auto-expiry will be inhibited."
10569   (interactive "p")
10570   (gnus-summary-show-thread)
10571   (let ((backward (< n 0))
10572         (gnus-summary-goto-unread
10573          (and gnus-summary-goto-unread
10574               (not (eq gnus-summary-goto-unread 'never))
10575               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10576                                     gnus-ticked-mark gnus-dormant-mark)))))
10577         (n (abs n))
10578         (mark (or mark gnus-del-mark)))
10579     (while (and (> n 0)
10580                 (gnus-summary-mark-article nil mark no-expire)
10581                 (zerop (gnus-summary-next-subject
10582                         (if backward -1 1)
10583                         (and gnus-summary-goto-unread
10584                              (not (eq gnus-summary-goto-unread 'never)))
10585                         t)))
10586       (setq n (1- n)))
10587     (when (/= 0 n)
10588       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10589     (gnus-summary-recenter)
10590     (gnus-summary-position-point)
10591     (gnus-set-mode-line 'summary)
10592     n))
10593
10594 (defun gnus-summary-mark-article-as-read (mark)
10595   "Mark the current article quickly as read with MARK."
10596   (let ((article (gnus-summary-article-number)))
10597     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10598     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10599     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10600     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10601     (push (cons article mark) gnus-newsgroup-reads)
10602     ;; Possibly remove from cache, if that is used.
10603     (when gnus-use-cache
10604       (gnus-cache-enter-remove-article article))
10605     ;; Allow the backend to change the mark.
10606     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10607     ;; Check for auto-expiry.
10608     (when (and gnus-newsgroup-auto-expire
10609                (memq mark gnus-auto-expirable-marks))
10610       (setq mark gnus-expirable-mark)
10611       ;; Let the backend know about the mark change.
10612       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10613       (push article gnus-newsgroup-expirable))
10614     ;; Set the mark in the buffer.
10615     (gnus-summary-update-mark mark 'unread)
10616     t))
10617
10618 (defun gnus-summary-mark-article-as-unread (mark)
10619   "Mark the current article quickly as unread with MARK."
10620   (let* ((article (gnus-summary-article-number))
10621          (old-mark (gnus-summary-article-mark article)))
10622     ;; Allow the backend to change the mark.
10623     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10624     (if (eq mark old-mark)
10625         t
10626       (if (<= article 0)
10627           (progn
10628             (gnus-error 1 "Can't mark negative article numbers")
10629             nil)
10630         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10631         (setq gnus-newsgroup-spam-marked
10632               (delq article gnus-newsgroup-spam-marked))
10633         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10634         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10635         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10636         (cond ((= mark gnus-ticked-mark)
10637                (setq gnus-newsgroup-marked
10638                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10639                                               article)))
10640               ((= mark gnus-spam-mark)
10641                (setq gnus-newsgroup-spam-marked
10642                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10643                                               article)))
10644               ((= mark gnus-dormant-mark)
10645                (setq gnus-newsgroup-dormant
10646                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10647                                               article)))
10648               (t
10649                (setq gnus-newsgroup-unreads
10650                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10651                                               article))))
10652         (gnus-pull article gnus-newsgroup-reads)
10653
10654         ;; See whether the article is to be put in the cache.
10655         (and gnus-use-cache
10656              (vectorp (gnus-summary-article-header article))
10657              (save-excursion
10658                (gnus-cache-possibly-enter-article
10659                 gnus-newsgroup-name article
10660                 (= mark gnus-ticked-mark)
10661                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10662
10663         ;; Fix the mark.
10664         (gnus-summary-update-mark mark 'unread)
10665         t))))
10666
10667 (defun gnus-summary-mark-article (&optional article mark no-expire)
10668   "Mark ARTICLE with MARK.  MARK can be any character.
10669 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10670 `??' (dormant) and `?E' (expirable).
10671 If MARK is nil, then the default character `?r' is used.
10672 If ARTICLE is nil, then the article on the current line will be
10673 marked.
10674 If NO-EXPIRE, auto-expiry will be inhibited."
10675   ;; The mark might be a string.
10676   (when (stringp mark)
10677     (setq mark (aref mark 0)))
10678   ;; If no mark is given, then we check auto-expiring.
10679   (when (null mark)
10680     (setq mark gnus-del-mark))
10681   (when (and (not no-expire)
10682              gnus-newsgroup-auto-expire
10683              (memq mark gnus-auto-expirable-marks))
10684     (setq mark gnus-expirable-mark))
10685   (let ((article (or article (gnus-summary-article-number)))
10686         (old-mark (gnus-summary-article-mark article)))
10687     ;; Allow the backend to change the mark.
10688     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10689     (if (eq mark old-mark)
10690         t
10691       (unless article
10692         (error "No article on current line"))
10693       (if (not (if (or (= mark gnus-unread-mark)
10694                        (= mark gnus-ticked-mark)
10695                        (= mark gnus-spam-mark)
10696                        (= mark gnus-dormant-mark))
10697                    (gnus-mark-article-as-unread article mark)
10698                  (gnus-mark-article-as-read article mark)))
10699           t
10700         ;; See whether the article is to be put in the cache.
10701         (and gnus-use-cache
10702              (not (= mark gnus-canceled-mark))
10703              (vectorp (gnus-summary-article-header article))
10704              (save-excursion
10705                (gnus-cache-possibly-enter-article
10706                 gnus-newsgroup-name article
10707                 (= mark gnus-ticked-mark)
10708                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10709
10710         (when (gnus-summary-goto-subject article nil t)
10711           (let ((inhibit-read-only t))
10712             (gnus-summary-show-thread)
10713             ;; Fix the mark.
10714             (gnus-summary-update-mark mark 'unread)
10715             t))))))
10716
10717 (defun gnus-summary-update-secondary-mark (article)
10718   "Update the secondary (read, process, cache) mark."
10719   (gnus-summary-update-mark
10720    (cond ((memq article gnus-newsgroup-processable)
10721           gnus-process-mark)
10722          ((memq article gnus-newsgroup-cached)
10723           gnus-cached-mark)
10724          ((memq article gnus-newsgroup-replied)
10725           gnus-replied-mark)
10726          ((memq article gnus-newsgroup-forwarded)
10727           gnus-forwarded-mark)
10728          ((memq article gnus-newsgroup-saved)
10729           gnus-saved-mark)
10730          ((memq article gnus-newsgroup-recent)
10731           gnus-recent-mark)
10732          ((memq article gnus-newsgroup-unseen)
10733           gnus-unseen-mark)
10734          (t gnus-no-mark))
10735    'replied)
10736   (when (gnus-visual-p 'summary-highlight 'highlight)
10737     (gnus-run-hooks 'gnus-summary-update-hook))
10738   t)
10739
10740 (defun gnus-summary-update-download-mark (article)
10741   "Update the download mark."
10742   (gnus-summary-update-mark
10743    (cond ((memq article gnus-newsgroup-undownloaded)
10744           gnus-undownloaded-mark)
10745          (gnus-newsgroup-agentized
10746           gnus-downloaded-mark)
10747          (t
10748           gnus-no-mark))
10749    'download)
10750   (gnus-summary-update-line t)
10751   t)
10752
10753 (defun gnus-summary-update-mark (mark type)
10754   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10755         (inhibit-read-only t))
10756     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10757     (when forward
10758       (when (looking-at "\r")
10759         (incf forward))
10760       (when (<= (+ forward (point)) (point-max))
10761         ;; Go to the right position on the line.
10762         (goto-char (+ forward (point)))
10763         ;; Replace the old mark with the new mark.
10764         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10765         ;; Optionally update the marks by some user rule.
10766         (when (eq type 'unread)
10767           (gnus-data-set-mark
10768            (gnus-data-find (gnus-summary-article-number)) mark)
10769           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10770
10771 (defun gnus-mark-article-as-read (article &optional mark)
10772   "Enter ARTICLE in the pertinent lists and remove it from others."
10773   ;; Make the article expirable.
10774   (let ((mark (or mark gnus-del-mark)))
10775     (setq gnus-newsgroup-expirable
10776           (if (= mark gnus-expirable-mark)
10777               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10778             (delq article gnus-newsgroup-expirable)))
10779     ;; Remove from unread and marked lists.
10780     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10781     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10782     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10783     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10784     (push (cons article mark) gnus-newsgroup-reads)
10785     ;; Possibly remove from cache, if that is used.
10786     (when gnus-use-cache
10787       (gnus-cache-enter-remove-article article))
10788     t))
10789
10790 (defun gnus-mark-article-as-unread (article &optional mark)
10791   "Enter ARTICLE in the pertinent lists and remove it from others."
10792   (let ((mark (or mark gnus-ticked-mark)))
10793     (if (<= article 0)
10794         (progn
10795           (gnus-error 1 "Can't mark negative article numbers")
10796           nil)
10797       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10798             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10799             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10800             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10801             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10802
10803       ;; Unsuppress duplicates?
10804       (when gnus-suppress-duplicates
10805         (gnus-dup-unsuppress-article article))
10806
10807       (cond ((= mark gnus-ticked-mark)
10808              (setq gnus-newsgroup-marked
10809                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10810             ((= mark gnus-spam-mark)
10811              (setq gnus-newsgroup-spam-marked
10812                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10813                                             article)))
10814             ((= mark gnus-dormant-mark)
10815              (setq gnus-newsgroup-dormant
10816                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10817             (t
10818              (setq gnus-newsgroup-unreads
10819                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10820       (gnus-pull article gnus-newsgroup-reads)
10821       t)))
10822
10823 (defalias 'gnus-summary-mark-as-unread-forward
10824   'gnus-summary-tick-article-forward)
10825 (make-obsolete 'gnus-summary-mark-as-unread-forward
10826                'gnus-summary-tick-article-forward "Emacs 20.4")
10827 (defun gnus-summary-tick-article-forward (n)
10828   "Tick N articles forwards.
10829 If N is negative, tick backwards instead.
10830 The difference between N and the number of articles ticked is returned."
10831   (interactive "p")
10832   (gnus-summary-mark-forward n gnus-ticked-mark))
10833
10834 (defalias 'gnus-summary-mark-as-unread-backward
10835   'gnus-summary-tick-article-backward)
10836 (make-obsolete 'gnus-summary-mark-as-unread-backward
10837                'gnus-summary-tick-article-backward "Emacs 20.4")
10838 (defun gnus-summary-tick-article-backward (n)
10839   "Tick N articles backwards.
10840 The difference between N and the number of articles ticked is returned."
10841   (interactive "p")
10842   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10843
10844 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10845 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10846 (defun gnus-summary-tick-article (&optional article clear-mark)
10847   "Mark current article as unread.
10848 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10849 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10850   (interactive)
10851   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10852                                        gnus-ticked-mark)))
10853
10854 (defun gnus-summary-mark-as-read-forward (n)
10855   "Mark N articles as read forwards.
10856 If N is negative, mark backwards instead.
10857 The difference between N and the actual number of articles marked is
10858 returned."
10859   (interactive "p")
10860   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10861
10862 (defun gnus-summary-mark-as-read-backward (n)
10863   "Mark the N articles as read backwards.
10864 The difference between N and the actual number of articles marked is
10865 returned."
10866   (interactive "p")
10867   (gnus-summary-mark-forward
10868    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10869
10870 (defun gnus-summary-mark-as-read (&optional article mark)
10871   "Mark current article as read.
10872 ARTICLE specifies the article to be marked as read.
10873 MARK specifies a string to be inserted at the beginning of the line."
10874   (gnus-summary-mark-article article mark))
10875
10876 (defun gnus-summary-clear-mark-forward (n)
10877   "Clear marks from N articles forward.
10878 If N is negative, clear backward instead.
10879 The difference between N and the number of marks cleared is returned."
10880   (interactive "p")
10881   (gnus-summary-mark-forward n gnus-unread-mark))
10882
10883 (defun gnus-summary-clear-mark-backward (n)
10884   "Clear marks from N articles backward.
10885 The difference between N and the number of marks cleared is returned."
10886   (interactive "p")
10887   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10888
10889 (defun gnus-summary-mark-unread-as-read ()
10890   "Intended to be used by `gnus-mark-article-hook'."
10891   (when (memq gnus-current-article gnus-newsgroup-unreads)
10892     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10893
10894 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10895   "Intended to be used by `gnus-mark-article-hook'."
10896   (let ((mark (gnus-summary-article-mark)))
10897     (when (or (gnus-unread-mark-p mark)
10898               (gnus-read-mark-p mark))
10899       (gnus-summary-mark-article gnus-current-article
10900                                  (or new-mark gnus-read-mark)))))
10901
10902 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10903   "Intended to be used by `gnus-mark-article-hook'."
10904   (let ((mark (gnus-summary-article-mark)))
10905     (when (or (gnus-unread-mark-p mark)
10906               (gnus-read-mark-p mark))
10907       (gnus-summary-mark-article (gnus-summary-article-number)
10908                                  (or new-mark gnus-read-mark)))))
10909
10910 (defun gnus-summary-mark-unread-as-ticked ()
10911   "Intended to be used by `gnus-mark-article-hook'."
10912   (when (memq gnus-current-article gnus-newsgroup-unreads)
10913     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10914
10915 (defun gnus-summary-mark-region-as-read (point mark all)
10916   "Mark all unread articles between point and mark as read.
10917 If given a prefix, mark all articles between point and mark as read,
10918 even ticked and dormant ones."
10919   (interactive "r\nP")
10920   (save-excursion
10921     (let (article)
10922       (goto-char point)
10923       (beginning-of-line)
10924       (while (and
10925               (< (point) mark)
10926               (progn
10927                 (when (or all
10928                           (memq (setq article (gnus-summary-article-number))
10929                                 gnus-newsgroup-unreads))
10930                   (gnus-summary-mark-article article gnus-del-mark))
10931                 t)
10932               (gnus-summary-find-next))))))
10933
10934 (defun gnus-summary-mark-below (score mark)
10935   "Mark articles with score less than SCORE with MARK."
10936   (interactive "P\ncMark: ")
10937   (setq score (if score
10938                   (prefix-numeric-value score)
10939                 (or gnus-summary-default-score 0)))
10940   (with-current-buffer gnus-summary-buffer
10941     (goto-char (point-min))
10942     (while
10943         (progn
10944           (and (< (gnus-summary-article-score) score)
10945                (gnus-summary-mark-article nil mark))
10946           (gnus-summary-find-next)))))
10947
10948 (defun gnus-summary-kill-below (&optional score)
10949   "Mark articles with score below SCORE as read."
10950   (interactive "P")
10951   (gnus-summary-mark-below score gnus-killed-mark))
10952
10953 (defun gnus-summary-clear-above (&optional score)
10954   "Clear all marks from articles with score above SCORE."
10955   (interactive "P")
10956   (gnus-summary-mark-above score gnus-unread-mark))
10957
10958 (defun gnus-summary-tick-above (&optional score)
10959   "Tick all articles with score above SCORE."
10960   (interactive "P")
10961   (gnus-summary-mark-above score gnus-ticked-mark))
10962
10963 (defun gnus-summary-mark-above (score mark)
10964   "Mark articles with score over SCORE with MARK."
10965   (interactive "P\ncMark: ")
10966   (setq score (if score
10967                   (prefix-numeric-value score)
10968                 (or gnus-summary-default-score 0)))
10969   (with-current-buffer gnus-summary-buffer
10970     (goto-char (point-min))
10971     (while (and (progn
10972                   (when (> (gnus-summary-article-score) score)
10973                     (gnus-summary-mark-article nil mark))
10974                   t)
10975                 (gnus-summary-find-next)))))
10976
10977 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10978 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10979 (defun gnus-summary-limit-include-expunged (&optional no-error)
10980   "Display all the hidden articles that were expunged for low scores."
10981   (interactive)
10982   (let ((inhibit-read-only t))
10983     (let ((scored gnus-newsgroup-scored)
10984           headers h)
10985       (while scored
10986         (unless (gnus-summary-article-header (caar scored))
10987           (and (setq h (gnus-number-to-header (caar scored)))
10988                (< (cdar scored) gnus-summary-expunge-below)
10989                (push h headers)))
10990         (setq scored (cdr scored)))
10991       (if (not headers)
10992           (when (not no-error)
10993             (error "No expunged articles hidden"))
10994         (goto-char (point-min))
10995         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10996         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10997         (dolist (x headers)
10998           (push (mail-header-number x) gnus-newsgroup-limit))
10999         (gnus-summary-prepare-unthreaded (nreverse headers))
11000         (goto-char (point-min))
11001         (gnus-summary-position-point)
11002         t))))
11003
11004 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11005   "Mark all unread articles in this newsgroup as read.
11006 If prefix argument ALL is non-nil, ticked and dormant articles will
11007 also be marked as read.
11008 If QUIETLY is non-nil, no questions will be asked.
11009
11010 If TO-HERE is non-nil, it should be a point in the buffer.  All
11011 articles before (after, if REVERSE is set) this point will be marked
11012 as read.
11013
11014 Note that this function will only catch up the unread article
11015 in the current summary buffer limitation.
11016
11017 The number of articles marked as read is returned."
11018   (interactive "P")
11019   (prog1
11020       (save-excursion
11021         (when (or quietly
11022                   (not gnus-interactive-catchup) ;Without confirmation?
11023                   gnus-expert-user
11024                   (gnus-y-or-n-p
11025                    (if all
11026                        "Mark absolutely all articles as read? "
11027                      "Mark all unread articles as read? ")))
11028           (if (and not-mark
11029                    (not gnus-newsgroup-adaptive)
11030                    (not gnus-newsgroup-auto-expire)
11031                    (not gnus-suppress-duplicates)
11032                    (or (not gnus-use-cache)
11033                        (eq gnus-use-cache 'passive)))
11034               (progn
11035                 (when all
11036                   (setq gnus-newsgroup-marked nil
11037                         gnus-newsgroup-spam-marked nil
11038                         gnus-newsgroup-dormant nil))
11039                 (setq gnus-newsgroup-unreads
11040                       (gnus-sorted-nunion
11041                        (gnus-sorted-intersection gnus-newsgroup-unreads
11042                                                  gnus-newsgroup-downloadable)
11043                        (gnus-sorted-difference gnus-newsgroup-unfetched
11044                                                gnus-newsgroup-cached))))
11045             ;; We actually mark all articles as canceled, which we
11046             ;; have to do when using auto-expiry or adaptive scoring.
11047             (gnus-summary-show-all-threads)
11048             (if (and to-here reverse)
11049                 (progn
11050                   (goto-char to-here)
11051                   (gnus-summary-mark-current-read-and-unread-as-read
11052                    gnus-catchup-mark)
11053                   (while (gnus-summary-find-next (not all))
11054                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11055               (when (gnus-summary-first-subject (not all))
11056                 (while (and
11057                         (if to-here (< (point) to-here) t)
11058                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11059                         (gnus-summary-find-next (not all))))))
11060             (gnus-set-mode-line 'summary))
11061           t))
11062     (gnus-summary-position-point)))
11063
11064 (defun gnus-summary-catchup-to-here (&optional all)
11065   "Mark all unticked articles before the current one as read.
11066 If ALL is non-nil, also mark ticked and dormant articles as read."
11067   (interactive "P")
11068   (save-excursion
11069     (gnus-save-hidden-threads
11070       (let ((beg (point)))
11071         ;; We check that there are unread articles.
11072         (when (or all (gnus-summary-find-prev))
11073           (gnus-summary-catchup all t beg)))))
11074   (gnus-summary-position-point))
11075
11076 (defun gnus-summary-catchup-from-here (&optional all)
11077   "Mark all unticked articles after (and including) the current one as read.
11078 If ALL is non-nil, also mark ticked and dormant articles as read."
11079   (interactive "P")
11080   (save-excursion
11081     (gnus-save-hidden-threads
11082       (let ((beg (point)))
11083         ;; We check that there are unread articles.
11084         (when (or all (gnus-summary-find-next))
11085           (gnus-summary-catchup all t beg nil t)))))
11086   (gnus-summary-position-point))
11087
11088 (defun gnus-summary-catchup-all (&optional quietly)
11089   "Mark all articles in this newsgroup as read.
11090 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11091 instead, which marks only unread articles as read."
11092   (interactive "P")
11093   (gnus-summary-catchup t quietly))
11094
11095 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11096   "Mark all unread articles in this group as read, then exit.
11097 If prefix argument ALL is non-nil, all articles are marked as read.
11098 If QUIETLY is non-nil, no questions will be asked."
11099   (interactive "P")
11100   (when (gnus-summary-catchup all quietly nil 'fast)
11101     ;; Select next newsgroup or exit.
11102     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11103              (eq gnus-auto-select-next 'quietly))
11104         (gnus-summary-next-group nil)
11105       (gnus-summary-exit))))
11106
11107 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11108   "Mark all articles in this newsgroup as read, and then exit.
11109 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11110 instead, which marks only unread articles as read."
11111   (interactive "P")
11112   (gnus-summary-catchup-and-exit t quietly))
11113
11114 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11115   "Mark all articles in this group as read and select the next group.
11116 If given a prefix, mark all articles, unread as well as ticked, as
11117 read."
11118   (interactive "P")
11119   (save-excursion
11120     (gnus-summary-catchup all))
11121   (gnus-summary-next-group))
11122
11123 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11124   "Mark all articles in this group as read and select the previous group.
11125 If given a prefix, mark all articles, unread as well as ticked, as
11126 read."
11127   (interactive "P")
11128   (save-excursion
11129     (gnus-summary-catchup all))
11130   (gnus-summary-next-group nil nil t))
11131
11132 ;;;
11133 ;;; with article
11134 ;;;
11135
11136 (defmacro gnus-with-article (article &rest forms)
11137   "Select ARTICLE and perform FORMS in the original article buffer.
11138 Then replace the article with the result."
11139   `(progn
11140      ;; We don't want the article to be marked as read.
11141      (let (gnus-mark-article-hook)
11142        (gnus-summary-select-article t t nil ,article))
11143      (set-buffer gnus-original-article-buffer)
11144      ,@forms
11145      (if (not (gnus-check-backend-function
11146                'request-replace-article (car gnus-article-current)))
11147          (gnus-message 5 "Read-only group; not replacing")
11148        (unless (gnus-request-replace-article
11149                 ,article (car gnus-article-current)
11150                 (current-buffer) t)
11151          (error "Couldn't replace article")))
11152      ;; The cache and backlog have to be flushed somewhat.
11153      (when gnus-keep-backlog
11154        (gnus-backlog-remove-article
11155         (car gnus-article-current) (cdr gnus-article-current)))
11156      (when gnus-use-cache
11157        (gnus-cache-update-article
11158         (car gnus-article-current) (cdr gnus-article-current)))))
11159
11160 (put 'gnus-with-article 'lisp-indent-function 1)
11161 (put 'gnus-with-article 'edebug-form-spec '(form body))
11162
11163 ;; Thread-based commands.
11164
11165 (defun gnus-summary-articles-in-thread (&optional article)
11166   "Return a list of all articles in the current thread.
11167 If ARTICLE is non-nil, return all articles in the thread that starts
11168 with that article."
11169   (let* ((article (or article (gnus-summary-article-number)))
11170          (data (gnus-data-find-list article))
11171          (top-level (gnus-data-level (car data)))
11172          (top-subject
11173           (cond ((null gnus-thread-operation-ignore-subject)
11174                  (gnus-simplify-subject-re
11175                   (mail-header-subject (gnus-data-header (car data)))))
11176                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11177                  (gnus-simplify-subject-fuzzy
11178                   (mail-header-subject (gnus-data-header (car data)))))
11179                 (t nil)))
11180          (end-point (save-excursion
11181                       (if (gnus-summary-go-to-next-thread)
11182                           (point) (point-max))))
11183          articles)
11184     (while (and data
11185                 (< (gnus-data-pos (car data)) end-point))
11186       (when (or (not top-subject)
11187                 (string= top-subject
11188                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11189                              (gnus-simplify-subject-fuzzy
11190                               (mail-header-subject
11191                                (gnus-data-header (car data))))
11192                            (gnus-simplify-subject-re
11193                             (mail-header-subject
11194                              (gnus-data-header (car data)))))))
11195         (push (gnus-data-number (car data)) articles))
11196       (unless (and (setq data (cdr data))
11197                    (> (gnus-data-level (car data)) top-level))
11198         (setq data nil)))
11199     ;; Return the list of articles.
11200     (nreverse articles)))
11201
11202 (defun gnus-summary-rethread-current ()
11203   "Rethread the thread the current article is part of."
11204   (interactive)
11205   (let* ((gnus-show-threads t)
11206          (article (gnus-summary-article-number))
11207          (id (mail-header-id (gnus-summary-article-header)))
11208          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11209     (unless id
11210       (error "No article on the current line"))
11211     (gnus-rebuild-thread id)
11212     (gnus-summary-goto-subject article)))
11213
11214 (defun gnus-summary-reparent-thread ()
11215   "Make the current article child of the marked (or previous) article.
11216
11217 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11218 is non-nil or the Subject: of both articles are the same."
11219   (interactive)
11220   (unless (not (gnus-group-read-only-p))
11221     (error "The current newsgroup does not support article editing"))
11222   (unless (<= (length gnus-newsgroup-processable) 1)
11223     (error "No more than one article may be marked"))
11224   (let ((child (gnus-summary-article-number))
11225         ;; First grab the marked article, otherwise one line up.
11226         (parent (if (not (null gnus-newsgroup-processable))
11227                     (car gnus-newsgroup-processable)
11228                   (save-excursion
11229                     (if (eq (forward-line -1) 0)
11230                         (gnus-summary-article-number)
11231                       (error "Beginning of summary buffer"))))))
11232     (gnus-summary-reparent-children parent (list child))))
11233
11234 (defun gnus-summary-reparent-children (parent children)
11235   "Make PARENT the parent of CHILDREN.
11236 When called interactively, PARENT is the current article and CHILDREN
11237 are the process-marked articles."
11238   (interactive
11239    (list (gnus-summary-article-number)
11240          (gnus-summary-work-articles nil)))
11241   (dolist (child children)
11242     (save-window-excursion
11243       (let ((gnus-article-buffer " *reparent*"))
11244         (unless (not (eq parent child))
11245           (error "An article may not be self-referential"))
11246         (let ((message-id (mail-header-id
11247                            (gnus-summary-article-header parent))))
11248           (unless (and message-id (not (equal message-id "")))
11249             (error "No message-id in desired parent"))
11250           (gnus-with-article child
11251             (save-restriction
11252               (goto-char (point-min))
11253               (message-narrow-to-head)
11254               (if (re-search-forward "^References: " nil t)
11255                   (progn
11256                     (re-search-forward "^[^ \t]" nil t)
11257                     (forward-line -1)
11258                     (end-of-line)
11259                     (insert " " message-id))
11260                 (insert "References: " message-id "\n"))))
11261           (set-buffer gnus-summary-buffer)
11262           (gnus-summary-unmark-all-processable)
11263           (gnus-summary-update-article child)
11264           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11265             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11266           (gnus-summary-rethread-current)
11267           (gnus-message 3 "Article %d is now the child of article %d"
11268                         child parent))))))
11269
11270 (defun gnus-summary-toggle-threads (&optional arg)
11271   "Toggle showing conversation threads.
11272 If ARG is positive number, turn showing conversation threads on."
11273   (interactive "P")
11274   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11275     (setq gnus-show-threads
11276           (if (null arg) (not gnus-show-threads)
11277             (> (prefix-numeric-value arg) 0)))
11278     (gnus-summary-prepare)
11279     (gnus-summary-goto-subject current)
11280     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11281     (gnus-summary-position-point)))
11282
11283 (eval-and-compile
11284   (if (fboundp 'remove-overlays)
11285       (defalias 'gnus-remove-overlays 'remove-overlays)
11286     (defun gnus-remove-overlays (beg end name val)
11287       "Clear BEG and END of overlays whose property NAME has value VAL.
11288 For compatibility with Emacs 21 and XEmacs."
11289       (dolist (ov (gnus-overlays-in beg end))
11290         (when (eq (gnus-overlay-get ov name) val)
11291           (gnus-delete-overlay ov))))))
11292
11293 (defun gnus-summary-show-all-threads ()
11294   "Show all threads."
11295   (interactive)
11296   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11297   (gnus-summary-position-point))
11298
11299 (defun gnus-summary-show-thread ()
11300   "Show thread subtrees.
11301 Returns nil if no thread was there to be shown."
11302   (interactive)
11303   (let* ((orig (point))
11304          (end (point-at-eol))
11305          ;; Leave point at bol
11306          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11307          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11308                 (if (fboundp 'next-single-char-property-change)
11309                     (or (next-single-char-property-change end 'invisible)
11310                         (point-max))
11311                   (while (progn
11312                            (end-of-line 2)
11313                            (and (not (eobp))
11314                                 (eq (get-char-property (point) 'invisible)
11315                                     'gnus-sum))))
11316                   (point)))))
11317     (when eoi
11318       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11319       (goto-char orig)
11320       (gnus-summary-position-point)
11321       eoi)))
11322
11323 (defun gnus-summary-maybe-hide-threads ()
11324   "If requested, hide the threads that should be hidden."
11325   (when (and gnus-show-threads
11326              gnus-thread-hide-subtree)
11327     (gnus-summary-hide-all-threads
11328      (if (or (consp gnus-thread-hide-subtree)
11329              (functionp gnus-thread-hide-subtree))
11330          (gnus-make-predicate gnus-thread-hide-subtree)
11331        nil))))
11332
11333 ;;; Hiding predicates.
11334
11335 (defun gnus-article-unread-p (header)
11336   (memq (mail-header-number header) gnus-newsgroup-unreads))
11337
11338 (defun gnus-article-unseen-p (header)
11339   (memq (mail-header-number header) gnus-newsgroup-unseen))
11340
11341 (defun gnus-map-articles (predicate articles)
11342   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11343   (apply 'gnus-or (mapcar predicate
11344                           (mapcar (lambda (number)
11345                                     (gnus-summary-article-header number))
11346                                   articles))))
11347
11348 (defun gnus-summary-hide-all-threads (&optional predicate)
11349   "Hide all thread subtrees.
11350 If PREDICATE is supplied, threads that satisfy this predicate
11351 will not be hidden."
11352   (interactive)
11353   (save-excursion
11354     (goto-char (point-min))
11355     (let ((end nil))
11356       (while (not end)
11357         (when (or (not predicate)
11358                   (gnus-map-articles
11359                    predicate (gnus-summary-article-children)))
11360             (gnus-summary-hide-thread))
11361         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11362   (gnus-summary-position-point))
11363
11364 (defun gnus-summary-hide-thread ()
11365   "Hide thread subtrees.
11366 If PREDICATE is supplied, threads that satisfy this predicate
11367 will not be hidden.
11368 Returns nil if no threads were there to be hidden."
11369   (interactive)
11370   (let ((start (point))
11371         (starteol (line-end-position))
11372         (article (gnus-summary-article-number)))
11373     (goto-char start)
11374     ;; Go forward until either the buffer ends or the subthread ends.
11375     (when (and (not (eobp))
11376                (or (zerop (gnus-summary-next-thread 1 t))
11377                    (goto-char (point-max))))
11378       (if (and (> (point) start)
11379                ;; FIXME: this should actually search for a non-invisible \n.
11380                (search-backward "\n" start t))
11381           (progn
11382             (when (> (point) starteol)
11383               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11384               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11385                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11386                 (gnus-overlay-put ol 'evaporate t)))
11387             (gnus-summary-goto-subject article))
11388         (goto-char start)
11389         nil))))
11390
11391 (defun gnus-summary-go-to-next-thread (&optional previous)
11392   "Go to the same level (or less) next thread.
11393 If PREVIOUS is non-nil, go to previous thread instead.
11394 Return the article number moved to, or nil if moving was impossible."
11395   (let ((level (gnus-summary-thread-level))
11396         (way (if previous -1 1))
11397         (beg (point)))
11398     (forward-line way)
11399     (while (and (not (eobp))
11400                 (< level (gnus-summary-thread-level)))
11401       (forward-line way))
11402     (if (eobp)
11403         (progn
11404           (goto-char beg)
11405           nil)
11406       (setq beg (point))
11407       (prog1
11408           (gnus-summary-article-number)
11409         (goto-char beg)))))
11410
11411 (defun gnus-summary-next-thread (n &optional silent)
11412   "Go to the same level next N'th thread.
11413 If N is negative, search backward instead.
11414 Returns the difference between N and the number of skips actually
11415 done.
11416
11417 If SILENT, don't output messages."
11418   (interactive "p")
11419   (let ((backward (< n 0))
11420         (n (abs n)))
11421     (while (and (> n 0)
11422                 (gnus-summary-go-to-next-thread backward))
11423       (decf n))
11424     (unless silent
11425       (gnus-summary-position-point))
11426     (when (and (not silent) (/= 0 n))
11427       (gnus-message 7 "No more threads"))
11428     n))
11429
11430 (defun gnus-summary-prev-thread (n)
11431   "Go to the same level previous N'th thread.
11432 Returns the difference between N and the number of skips actually
11433 done."
11434   (interactive "p")
11435   (gnus-summary-next-thread (- n)))
11436
11437 (defun gnus-summary-go-down-thread ()
11438   "Go down one level in the current thread."
11439   (let ((children (gnus-summary-article-children)))
11440     (when children
11441       (gnus-summary-goto-subject (car children)))))
11442
11443 (defun gnus-summary-go-up-thread ()
11444   "Go up one level in the current thread."
11445   (let ((parent (gnus-summary-article-parent)))
11446     (when parent
11447       (gnus-summary-goto-subject parent))))
11448
11449 (defun gnus-summary-down-thread (n)
11450   "Go down thread N steps.
11451 If N is negative, go up instead.
11452 Returns the difference between N and how many steps down that were
11453 taken."
11454   (interactive "p")
11455   (let ((up (< n 0))
11456         (n (abs n)))
11457     (while (and (> n 0)
11458                 (if up (gnus-summary-go-up-thread)
11459                   (gnus-summary-go-down-thread)))
11460       (setq n (1- n)))
11461     (gnus-summary-position-point)
11462     (when (/= 0 n)
11463       (gnus-message 7 "Can't go further"))
11464     n))
11465
11466 (defun gnus-summary-up-thread (n)
11467   "Go up thread N steps.
11468 If N is negative, go down instead.
11469 Returns the difference between N and how many steps down that were
11470 taken."
11471   (interactive "p")
11472   (gnus-summary-down-thread (- n)))
11473
11474 (defun gnus-summary-top-thread ()
11475   "Go to the top of the thread."
11476   (interactive)
11477   (while (gnus-summary-go-up-thread))
11478   (gnus-summary-article-number))
11479
11480 (defun gnus-summary-expire-thread ()
11481   "Mark articles under current thread as expired."
11482   (interactive)
11483   (gnus-summary-kill-thread 0))
11484
11485 (defun gnus-summary-kill-thread (&optional unmark)
11486   "Mark articles under current thread as read.
11487 If the prefix argument is positive, remove any kinds of marks.
11488 If the prefix argument is zero, mark thread as expired.
11489 If the prefix argument is negative, tick articles instead."
11490   (interactive "P")
11491   (when unmark
11492     (setq unmark (prefix-numeric-value unmark)))
11493   (let ((articles (gnus-summary-articles-in-thread))
11494         (hide (or (null unmark) (= unmark 0))))
11495     (save-excursion
11496       ;; Expand the thread.
11497       (gnus-summary-show-thread)
11498       ;; Mark all the articles.
11499       (while articles
11500         (gnus-summary-goto-subject (car articles))
11501         (cond ((null unmark)
11502                (gnus-summary-mark-article-as-read gnus-killed-mark))
11503               ((> unmark 0)
11504                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11505               ((= unmark 0)
11506                (gnus-summary-mark-article nil gnus-expirable-mark))
11507               (t
11508                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11509         (setq articles (cdr articles))))
11510     ;; Hide killed subtrees when hide is true.
11511     (and hide
11512          gnus-thread-hide-killed
11513          (gnus-summary-hide-thread))
11514     ;; If hide is t, go to next unread subject.
11515     (when hide
11516       ;; Go to next unread subject.
11517       (gnus-summary-next-subject 1 t)))
11518   (gnus-set-mode-line 'summary))
11519
11520 ;; Summary sorting commands
11521
11522 (defun gnus-summary-sort-by-number (&optional reverse)
11523   "Sort the summary buffer by article number.
11524 Argument REVERSE means reverse order."
11525   (interactive "P")
11526   (gnus-summary-sort 'number reverse))
11527
11528 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11529   "Sort the summary buffer by most recent article number.
11530 Argument REVERSE means reverse order."
11531   (interactive "P")
11532   (gnus-summary-sort 'most-recent-number reverse))
11533
11534 (defun gnus-summary-sort-by-random (&optional reverse)
11535   "Randomize the order in the summary buffer.
11536 Argument REVERSE means to randomize in reverse order."
11537   (interactive "P")
11538   (gnus-summary-sort 'random reverse))
11539
11540 (defun gnus-summary-sort-by-author (&optional reverse)
11541   "Sort the summary buffer by author name alphabetically.
11542 If `case-fold-search' is non-nil, case of letters is ignored.
11543 Argument REVERSE means reverse order."
11544   (interactive "P")
11545   (gnus-summary-sort 'author reverse))
11546
11547 (defun gnus-summary-sort-by-recipient (&optional reverse)
11548   "Sort the summary buffer by recipient name alphabetically.
11549 If `case-fold-search' is non-nil, case of letters is ignored.
11550 Argument REVERSE means reverse order."
11551   (interactive "P")
11552   (gnus-summary-sort 'recipient reverse))
11553
11554 (defun gnus-summary-sort-by-subject (&optional reverse)
11555   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11556 If `case-fold-search' is non-nil, case of letters is ignored.
11557 Argument REVERSE means reverse order."
11558   (interactive "P")
11559   (gnus-summary-sort 'subject reverse))
11560
11561 (defun gnus-summary-sort-by-date (&optional reverse)
11562   "Sort the summary buffer by date.
11563 Argument REVERSE means reverse order."
11564   (interactive "P")
11565   (gnus-summary-sort 'date reverse))
11566
11567 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11568   "Sort the summary buffer by most recent date.
11569 Argument REVERSE means reverse order."
11570   (interactive "P")
11571   (gnus-summary-sort 'most-recent-date reverse))
11572
11573 (defun gnus-summary-sort-by-score (&optional reverse)
11574   "Sort the summary buffer by score.
11575 Argument REVERSE means reverse order."
11576   (interactive "P")
11577   (gnus-summary-sort 'score reverse))
11578
11579 (defun gnus-summary-sort-by-lines (&optional reverse)
11580   "Sort the summary buffer by the number of lines.
11581 Argument REVERSE means reverse order."
11582   (interactive "P")
11583   (gnus-summary-sort 'lines reverse))
11584
11585 (defun gnus-summary-sort-by-chars (&optional reverse)
11586   "Sort the summary buffer by article length.
11587 Argument REVERSE means reverse order."
11588   (interactive "P")
11589   (gnus-summary-sort 'chars reverse))
11590
11591 (defun gnus-summary-sort-by-original (&optional reverse)
11592   "Sort the summary buffer using the default sorting method.
11593 Argument REVERSE means reverse order."
11594   (interactive "P")
11595   (let* ((inhibit-read-only t)
11596          (gnus-summary-prepare-hook nil))
11597     ;; We do the sorting by regenerating the threads.
11598     (gnus-summary-prepare)
11599     ;; Hide subthreads if needed.
11600     (gnus-summary-maybe-hide-threads)))
11601
11602 (defun gnus-summary-sort (predicate reverse)
11603   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11604   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11605          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11606          (gnus-thread-sort-functions
11607           (if (not reverse)
11608               thread
11609             `(lambda (t1 t2)
11610                (,thread t2 t1))))
11611          (gnus-sort-gathered-threads-function
11612           gnus-thread-sort-functions)
11613          (gnus-article-sort-functions
11614           (if (not reverse)
11615               article
11616             `(lambda (t1 t2)
11617                (,article t2 t1))))
11618          (inhibit-read-only t)
11619          (gnus-summary-prepare-hook nil))
11620     ;; We do the sorting by regenerating the threads.
11621     (gnus-summary-prepare)
11622     ;; Hide subthreads if needed.
11623     (gnus-summary-maybe-hide-threads)))
11624
11625 ;; Summary saving commands.
11626
11627 (defun gnus-summary-save-article (&optional n not-saved)
11628   "Save the current article using the default saver function.
11629 If N is a positive number, save the N next articles.
11630 If N is a negative number, save the N previous articles.
11631 If N is nil and any articles have been marked with the process mark,
11632 save those articles instead.
11633 The variable `gnus-default-article-saver' specifies the saver function.
11634
11635 If the optional second argument NOT-SAVED is non-nil, articles saved
11636 will not be marked as saved."
11637   (interactive "P")
11638   (require 'gnus-art)
11639   (let* ((articles (gnus-summary-work-articles n))
11640          (save-buffer (save-excursion
11641                         (nnheader-set-temp-buffer " *Gnus Save*")))
11642          (num (length articles))
11643          ;; Whether to save decoded articles or raw articles.
11644          (decode (when gnus-article-save-coding-system
11645                    (get gnus-default-article-saver :decode)))
11646          ;; When saving many articles in a single file, use the other
11647          ;; function to save articles other than the first one.
11648          (saver2 (get gnus-default-article-saver :function))
11649          (gnus-prompt-before-saving (if saver2
11650                                         t
11651                                       gnus-prompt-before-saving))
11652          (gnus-default-article-saver gnus-default-article-saver)
11653          header file)
11654     (dolist (article articles)
11655       (setq header (gnus-summary-article-header article))
11656       (if (not (vectorp header))
11657           ;; This is a pseudo-article.
11658           (if (assq 'name header)
11659               (gnus-copy-file (cdr (assq 'name header)))
11660             (gnus-message 1 "Article %d is unsaveable" article))
11661         ;; This is a real article.
11662         (save-window-excursion
11663           (gnus-summary-select-article decode decode nil article)
11664           (gnus-summary-goto-subject article))
11665         (with-current-buffer save-buffer
11666           (erase-buffer)
11667           (insert-buffer-substring (if decode
11668                                        gnus-article-buffer
11669                                      gnus-original-article-buffer)))
11670         (setq file (gnus-article-save save-buffer file num))
11671         (gnus-summary-remove-process-mark article)
11672         (unless not-saved
11673           (gnus-summary-set-saved-mark article)))
11674       (when saver2
11675         (setq gnus-default-article-saver saver2
11676               saver2 nil)))
11677     (gnus-kill-buffer save-buffer)
11678     (gnus-summary-position-point)
11679     (gnus-set-mode-line 'summary)
11680     n))
11681
11682 (defun gnus-summary-pipe-output (&optional n sym)
11683   "Pipe the current article to a subprocess.
11684 If N is a positive number, pipe the N next articles.
11685 If N is a negative number, pipe the N previous articles.
11686 If N is nil and any articles have been marked with the process mark,
11687 pipe those articles instead.
11688 The default command to which articles are piped is specified by the
11689 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11690 will be prompted for the command.
11691
11692 The properties `:decode' and `:headers' that are put to the function
11693 symbol `gnus-summary-save-in-pipe' control whether this function
11694 decodes articles and what headers to keep (see the doc string for the
11695 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11696 is neither omitted nor the symbol `r', force including all headers
11697 regardless of the `:headers' property.  If it is the symbol `r',
11698 articles that are not decoded and include all headers will be piped
11699 no matter what the properties `:decode' and `:headers' are."
11700   (interactive (gnus-interactive "P\ny"))
11701   (require 'gnus-art)
11702   (let* ((articles (gnus-summary-work-articles n))
11703          (result-buffer "*Shell Command Output*")
11704          (all-headers (not (memq sym '(nil r))))
11705          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11706          (raw (eq sym 'r))
11707          (headers (get 'gnus-summary-save-in-pipe :headers))
11708          command result)
11709     (unless (numberp (car articles))
11710       (error "No article to pipe"))
11711     (setq command (gnus-read-shell-command
11712                    (concat "Shell command on "
11713                            (if (cdr articles)
11714                                (format "these %d articles" (length articles))
11715                              "this article")
11716                            ": ")
11717                    gnus-summary-pipe-output-default-command))
11718     (when (string-equal command "")
11719       (error "A command is required"))
11720     (when all-headers
11721       (put 'gnus-summary-save-in-pipe :headers nil))
11722     (unwind-protect
11723         (while articles
11724           (gnus-summary-goto-subject (pop articles))
11725           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11726           (when (and (get-buffer result-buffer)
11727                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11728             (setq result (concat result (with-current-buffer result-buffer
11729                                           (buffer-string))))))
11730       (put 'gnus-summary-save-in-pipe :headers headers))
11731     (unless (zerop (length result))
11732       (if (with-current-buffer (get-buffer-create result-buffer)
11733             (erase-buffer)
11734             (insert result)
11735             (prog1
11736                 (and (= (count-lines (point-min) (point)) 1)
11737                      (progn
11738                        (end-of-line 0)
11739                        (<= (current-column)
11740                            (window-width (minibuffer-window)))))
11741               (goto-char (point-min))))
11742           (message "%s" (substring result 0 -1))
11743         (message nil)
11744         (gnus-configure-windows 'pipe)))))
11745
11746 (defun gnus-summary-save-article-mail (&optional arg)
11747   "Append the current article to a Unix mail box file.
11748 If N is a positive number, save the N next articles.
11749 If N is a negative number, save the N previous articles.
11750 If N is nil and any articles have been marked with the process mark,
11751 save those articles instead."
11752   (interactive "P")
11753   (require 'gnus-art)
11754   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11755     (gnus-summary-save-article arg)))
11756
11757 (defun gnus-summary-save-article-rmail (&optional arg)
11758   "Append the current article to an rmail file.
11759 If N is a positive number, save the N next articles.
11760 If N is a negative number, save the N previous articles.
11761 If N is nil and any articles have been marked with the process mark,
11762 save those articles instead."
11763   (interactive "P")
11764   (require 'gnus-art)
11765   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11766     (gnus-summary-save-article arg)))
11767
11768 (defun gnus-summary-save-article-file (&optional arg)
11769   "Append the current article to a file.
11770 If N is a positive number, save the N next articles.
11771 If N is a negative number, save the N previous articles.
11772 If N is nil and any articles have been marked with the process mark,
11773 save those articles instead."
11774   (interactive "P")
11775   (require 'gnus-art)
11776   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11777     (gnus-summary-save-article arg)))
11778
11779 (defun gnus-summary-write-article-file (&optional arg)
11780   "Write the current article to a file, deleting the previous file.
11781 If N is a positive number, save the N next articles.
11782 If N is a negative number, save the N previous articles.
11783 If N is nil and any articles have been marked with the process mark,
11784 save those articles instead."
11785   (interactive "P")
11786   (require 'gnus-art)
11787   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11788     (gnus-summary-save-article arg)))
11789
11790 (defun gnus-summary-save-article-body-file (&optional arg)
11791   "Append the current article body to a file.
11792 If N is a positive number, save the N next articles.
11793 If N is a negative number, save the N previous articles.
11794 If N is nil and any articles have been marked with the process mark,
11795 save those articles instead."
11796   (interactive "P")
11797   (require 'gnus-art)
11798   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11799     (gnus-summary-save-article arg)))
11800
11801 (defun gnus-summary-write-article-body-file (&optional arg)
11802   "Write the current article body to a file, deleting the previous file.
11803 If N is a positive number, save the N next articles.
11804 If N is a negative number, save the N previous articles.
11805 If N is nil and any articles have been marked with the process mark,
11806 save those articles instead."
11807   (interactive "P")
11808   (require 'gnus-art)
11809   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11810     (gnus-summary-save-article arg)))
11811
11812 (defun gnus-summary-muttprint (&optional arg)
11813   "Print the current article using Muttprint.
11814 If N is a positive number, save the N next articles.
11815 If N is a negative number, save the N previous articles.
11816 If N is nil and any articles have been marked with the process mark,
11817 save those articles instead."
11818   (interactive "P")
11819   (require 'gnus-art)
11820   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11821     (gnus-summary-save-article arg t)))
11822
11823 (defun gnus-summary-pipe-message (program)
11824   "Pipe the current article through PROGRAM."
11825   (interactive "sProgram: ")
11826   (gnus-summary-select-article)
11827   (let ((mail-header-separator ""))
11828     (gnus-eval-in-buffer-window gnus-article-buffer
11829       (save-restriction
11830         (widen)
11831         (let ((start (window-start))
11832               (inhibit-read-only t))
11833           (message-pipe-buffer-body program)
11834           (set-window-start (get-buffer-window (current-buffer)) start))))))
11835
11836 (defun gnus-get-split-value (methods)
11837   "Return a value based on the split METHODS."
11838   (let (split-name method result match)
11839     (when methods
11840       (with-current-buffer gnus-original-article-buffer
11841         (save-restriction
11842           (nnheader-narrow-to-headers)
11843           (while (and methods (not split-name))
11844             (goto-char (point-min))
11845             (setq method (pop methods))
11846             (setq match (car method))
11847             (when (cond
11848                    ((stringp match)
11849                     ;; Regular expression.
11850                     (ignore-errors
11851                       (re-search-forward match nil t)))
11852                    ((functionp match)
11853                     ;; Function.
11854                     (save-restriction
11855                       (widen)
11856                       (setq result (funcall match gnus-newsgroup-name))))
11857                    ((consp match)
11858                     ;; Form.
11859                     (save-restriction
11860                       (widen)
11861                       (setq result (eval match)))))
11862               (setq split-name (cdr method))
11863               (cond ((stringp result)
11864                      (push (expand-file-name
11865                             result gnus-article-save-directory)
11866                            split-name))
11867                     ((consp result)
11868                      (setq split-name (append result split-name)))))))))
11869     (nreverse split-name)))
11870
11871 (defun gnus-valid-move-group-p (group)
11872   (and (boundp group)
11873        (symbol-name group)
11874        (symbol-value group)
11875        (gnus-get-function (gnus-find-method-for-group
11876                            (symbol-name group)) 'request-accept-article t)))
11877
11878 (defun gnus-read-move-group-name (prompt default articles prefix)
11879   "Read a group name."
11880   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11881          (minibuffer-confirm-incomplete nil) ; XEmacs
11882          (prom
11883           (format "%s %s to"
11884                   prompt
11885                   (if (> (length articles) 1)
11886                       (format "these %d articles" (length articles))
11887                     "this article")))
11888          (to-newsgroup
11889           (let (active group)
11890             (when (or (null split-name) (= 1 (length split-name)))
11891               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11892               (mapatoms (lambda (symbol)
11893                           (setq group (symbol-name symbol))
11894                           (when (string-match "[^\000-\177]" group)
11895                             (setq group (gnus-group-decoded-name group)))
11896                           (set (intern group active) group))
11897                         gnus-active-hashtb))
11898             (cond
11899              ((null split-name)
11900               (gnus-completing-read-with-default
11901                default prom active 'gnus-valid-move-group-p nil prefix
11902                'gnus-group-history))
11903              ((= 1 (length split-name))
11904               (gnus-completing-read-with-default
11905                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11906                'gnus-group-history))
11907              (t
11908               (gnus-completing-read-with-default
11909                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11910                'gnus-group-history)))))
11911          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11912          encoded)
11913     (when to-newsgroup
11914       (if (or (string= to-newsgroup "")
11915               (string= to-newsgroup prefix))
11916           (setq to-newsgroup default))
11917       (unless to-newsgroup
11918         (error "No group name entered"))
11919       (setq encoded (mm-encode-coding-string
11920                      to-newsgroup
11921                      (gnus-group-name-charset to-method to-newsgroup)))
11922       (or (gnus-active encoded)
11923           (gnus-activate-group encoded nil nil to-method)
11924           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11925                                      to-newsgroup))
11926               (or (and (gnus-request-create-group encoded to-method)
11927                        (gnus-activate-group encoded nil nil to-method)
11928                        (gnus-subscribe-group encoded))
11929                   (error "Couldn't create group %s" to-newsgroup)))
11930           (error "No such group: %s" to-newsgroup))
11931       encoded)))
11932
11933 (defvar gnus-summary-save-parts-counter)
11934 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11935
11936 (defun gnus-summary-save-parts (type dir n &optional reverse)
11937   "Save parts matching TYPE to DIR.
11938 If REVERSE, save parts that do not match TYPE."
11939   (interactive
11940    (list (read-string "Save parts of type: "
11941                       (or (car gnus-summary-save-parts-type-history)
11942                           gnus-summary-save-parts-default-mime)
11943                       'gnus-summary-save-parts-type-history)
11944          (setq gnus-summary-save-parts-last-directory
11945                (read-file-name "Save to directory: "
11946                                gnus-summary-save-parts-last-directory
11947                                nil t))
11948          current-prefix-arg))
11949   (gnus-summary-iterate n
11950     (let ((gnus-display-mime-function nil)
11951           gnus-article-prepare-hook
11952           gnus-article-decode-hook
11953           gnus-display-mime-function
11954           gnus-break-pages
11955           (gnus-inhibit-treatment t))
11956       (gnus-summary-select-article))
11957     (with-current-buffer gnus-article-buffer
11958       (let ((handles (or gnus-article-mime-handles
11959                          (mm-dissect-buffer nil gnus-article-loose-mime)
11960                          (and gnus-article-emulate-mime
11961                               (mm-uu-dissect))))
11962             (gnus-summary-save-parts-counter 1))
11963         (when handles
11964           (gnus-summary-save-parts-1 type dir handles reverse)
11965           (unless gnus-article-mime-handles ;; Don't destroy this case.
11966             (mm-destroy-parts handles)))))))
11967
11968 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11969   (if (stringp (car handle))
11970       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11971               (cdr handle))
11972     (when (if reverse
11973               (not (string-match type (mm-handle-media-type handle)))
11974             (string-match type (mm-handle-media-type handle)))
11975       (let ((file (expand-file-name
11976                    (gnus-map-function
11977                     mm-file-name-rewrite-functions
11978                     (file-name-nondirectory
11979                      (or
11980                       (mail-content-type-get
11981                        (mm-handle-disposition handle) 'filename)
11982                       (mail-content-type-get
11983                        (mm-handle-type handle) 'name)
11984                       (format "%s.%d.%d" gnus-newsgroup-name
11985                               (cdr gnus-article-current)
11986                               gnus-summary-save-parts-counter))))
11987                    dir)))
11988         (incf gnus-summary-save-parts-counter)
11989         (unless (file-exists-p file)
11990           (mm-save-part-to-file handle file))))))
11991
11992 ;; Summary extract commands
11993
11994 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11995   (let ((inhibit-read-only t)
11996         (article (gnus-summary-article-number))
11997         after-article b e)
11998     (unless (gnus-summary-goto-subject article)
11999       (error "No such article: %d" article))
12000     (gnus-summary-position-point)
12001     ;; If all commands are to be bunched up on one line, we collect
12002     ;; them here.
12003     (unless gnus-view-pseudos-separately
12004       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12005             files action)
12006         (while ps
12007           (setq action (cdr (assq 'action (car ps))))
12008           (setq files (list (cdr (assq 'name (car ps)))))
12009           (while (and ps (cdr ps)
12010                       (string= (or action "1")
12011                                (or (cdr (assq 'action (cadr ps))) "2")))
12012             (push (cdr (assq 'name (cadr ps))) files)
12013             (setcdr ps (cddr ps)))
12014           (when files
12015             (when (not (string-match "%s" action))
12016               (push " " files))
12017             (push " " files)
12018             (when (assq 'execute (car ps))
12019               (setcdr (assq 'execute (car ps))
12020                       (funcall (if (string-match "%s" action)
12021                                    'format 'concat)
12022                                action
12023                                (mapconcat
12024                                 (lambda (f)
12025                                   (if (equal f " ")
12026                                       f
12027                                     (shell-quote-argument f)))
12028                                 files " ")))))
12029           (setq ps (cdr ps)))))
12030     (if (and gnus-view-pseudos (not not-view))
12031         (while pslist
12032           (when (assq 'execute (car pslist))
12033             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12034                                   (eq gnus-view-pseudos 'not-confirm)))
12035           (setq pslist (cdr pslist)))
12036       (save-excursion
12037         (while pslist
12038           (setq after-article (or (cdr (assq 'article (car pslist)))
12039                                   (gnus-summary-article-number)))
12040           (gnus-summary-goto-subject after-article)
12041           (forward-line 1)
12042           (setq b (point))
12043           (insert "    " (file-name-nondirectory
12044                           (cdr (assq 'name (car pslist))))
12045                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12046           (setq e (point))
12047           (forward-line -1)             ; back to `b'
12048           (gnus-add-text-properties
12049            b (1- e) (list 'gnus-number gnus-reffed-article-number
12050                           gnus-mouse-face-prop gnus-mouse-face))
12051           (gnus-data-enter
12052            after-article gnus-reffed-article-number
12053            gnus-unread-mark b (car pslist) 0 (- e b))
12054           (setq gnus-newsgroup-unreads
12055                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12056                                          gnus-reffed-article-number))
12057           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12058           (setq pslist (cdr pslist)))))))
12059
12060 (defun gnus-pseudos< (p1 p2)
12061   (let ((c1 (cdr (assq 'action p1)))
12062         (c2 (cdr (assq 'action p2))))
12063     (and c1 c2 (string< c1 c2))))
12064
12065 (defun gnus-request-pseudo-article (props)
12066   (cond ((assq 'execute props)
12067          (gnus-execute-command (cdr (assq 'execute props)))))
12068   (let ((gnus-current-article (gnus-summary-article-number)))
12069     (gnus-run-hooks 'gnus-mark-article-hook)))
12070
12071 (defun gnus-execute-command (command &optional automatic)
12072   (save-excursion
12073     (gnus-article-setup-buffer)
12074     (set-buffer gnus-article-buffer)
12075     (setq buffer-read-only nil)
12076     (let ((command (if automatic command
12077                      (read-string "Command: " (cons command 0)))))
12078       (erase-buffer)
12079       (insert "$ " command "\n\n")
12080       (if gnus-view-pseudo-asynchronously
12081           (start-process "gnus-execute" (current-buffer) shell-file-name
12082                          shell-command-switch command)
12083         (call-process shell-file-name nil t nil
12084                       shell-command-switch command)))))
12085
12086 ;; Summary kill commands.
12087
12088 (defun gnus-summary-edit-global-kill (article)
12089   "Edit the \"global\" kill file."
12090   (interactive (list (gnus-summary-article-number)))
12091   (gnus-group-edit-global-kill article))
12092
12093 (defun gnus-summary-edit-local-kill ()
12094   "Edit a local kill file applied to the current newsgroup."
12095   (interactive)
12096   (setq gnus-current-headers (gnus-summary-article-header))
12097   (gnus-group-edit-local-kill
12098    (gnus-summary-article-number) gnus-newsgroup-name))
12099
12100 ;;; Header reading.
12101
12102 (defun gnus-read-header (id &optional header)
12103   "Read the headers of article ID and enter them into the Gnus system."
12104   (let ((group gnus-newsgroup-name)
12105         (gnus-override-method
12106          (or
12107           gnus-override-method
12108           (and (gnus-news-group-p gnus-newsgroup-name)
12109                (car (gnus-refer-article-methods)))))
12110         where)
12111     ;; First we check to see whether the header in question is already
12112     ;; fetched.
12113     (if (stringp id)
12114         ;; This is a Message-ID.
12115         (setq header (or header (gnus-id-to-header id)))
12116       ;; This is an article number.
12117       (setq header (or header (gnus-summary-article-header id))))
12118     (if (and header
12119              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12120         ;; We have found the header.
12121         header
12122       ;; We have to really fetch the header to this article.
12123       (with-current-buffer nntp-server-buffer
12124         (when (setq where (gnus-request-head id group))
12125           (nnheader-fold-continuation-lines)
12126           (goto-char (point-max))
12127           (insert ".\n")
12128           (goto-char (point-min))
12129           (insert "211 ")
12130           (princ (cond
12131                   ((numberp id) id)
12132                   ((cdr where) (cdr where))
12133                   (header (mail-header-number header))
12134                   (t gnus-reffed-article-number))
12135                  (current-buffer))
12136           (insert " Article retrieved.\n"))
12137         (if (or (not where)
12138                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12139             ()                          ; Malformed head.
12140           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12141             (when (and (stringp id)
12142                        (or
12143                         (not (string= (gnus-group-real-name group)
12144                                       (car where)))
12145                         (not (gnus-server-equal gnus-override-method
12146                                                 (gnus-group-method group)))))
12147               ;; If we fetched by Message-ID and the article came from
12148               ;; a different group (or server), we fudge some bogus
12149               ;; article numbers for this article.
12150               (mail-header-set-number header gnus-reffed-article-number))
12151             (with-current-buffer gnus-summary-buffer
12152               (decf gnus-reffed-article-number)
12153               (gnus-remove-header (mail-header-number header))
12154               (push header gnus-newsgroup-headers)
12155               (setq gnus-current-headers header)
12156               (push (mail-header-number header) gnus-newsgroup-limit)))
12157           header)))))
12158
12159 (defun gnus-remove-header (number)
12160   "Remove header NUMBER from `gnus-newsgroup-headers'."
12161   (if (and gnus-newsgroup-headers
12162            (= number (mail-header-number (car gnus-newsgroup-headers))))
12163       (pop gnus-newsgroup-headers)
12164     (let ((headers gnus-newsgroup-headers))
12165       (while (and (cdr headers)
12166                   (not (= number (mail-header-number (cadr headers)))))
12167         (pop headers))
12168       (when (cdr headers)
12169         (setcdr headers (cddr headers))))))
12170
12171 ;;;
12172 ;;; summary highlights
12173 ;;;
12174
12175 (defun gnus-highlight-selected-summary ()
12176   "Highlight selected article in summary buffer."
12177   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12178   (when gnus-summary-selected-face
12179     (save-excursion
12180       (let* ((beg (point-at-bol))
12181              (end (point-at-eol))
12182              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12183              (from (if (get-text-property beg gnus-mouse-face-prop)
12184                        beg
12185                      (or (next-single-property-change
12186                           beg gnus-mouse-face-prop nil end)
12187                          beg)))
12188              (to
12189               (if (= from end)
12190                   (- from 2)
12191                 (or (next-single-property-change
12192                      from gnus-mouse-face-prop nil end)
12193                     end))))
12194         ;; If no mouse-face prop on line we will have to = from = end,
12195         ;; so we highlight the entire line instead.
12196         (when (= (+ to 2) from)
12197           (setq from beg)
12198           (setq to end))
12199         (if gnus-newsgroup-selected-overlay
12200             ;; Move old overlay.
12201             (gnus-move-overlay
12202              gnus-newsgroup-selected-overlay from to (current-buffer))
12203           ;; Create new overlay.
12204           (gnus-overlay-put
12205            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12206            'face gnus-summary-selected-face))))))
12207
12208 (defvar gnus-summary-highlight-line-cached nil)
12209 (defvar gnus-summary-highlight-line-trigger nil)
12210
12211 (defun gnus-summary-highlight-line-0 ()
12212   (if (and (eq gnus-summary-highlight-line-trigger
12213                gnus-summary-highlight)
12214            gnus-summary-highlight-line-cached)
12215       gnus-summary-highlight-line-cached
12216     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12217           gnus-summary-highlight-line-cached
12218           (let* ((cond (list 'cond))
12219                  (c cond)
12220                  (list gnus-summary-highlight))
12221             (while list
12222               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12223                               nil))
12224               (setq c (cdr c)
12225                     list (cdr list)))
12226             (gnus-byte-compile (list 'lambda nil cond))))))
12227
12228 (defun gnus-summary-highlight-line ()
12229   "Highlight current line according to `gnus-summary-highlight'."
12230   (let* ((beg (point-at-bol))
12231          (article (or (gnus-summary-article-number) gnus-current-article))
12232          (score (or (cdr (assq article
12233                                gnus-newsgroup-scored))
12234                     gnus-summary-default-score 0))
12235          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12236          (inhibit-read-only t)
12237          (default gnus-summary-default-score)
12238          (default-high gnus-summary-default-high-score)
12239          (default-low gnus-summary-default-low-score)
12240          (uncached (and gnus-summary-use-undownloaded-faces
12241                         (memq article gnus-newsgroup-undownloaded)
12242                         (not (memq article gnus-newsgroup-cached)))))
12243     (let ((face (funcall (gnus-summary-highlight-line-0))))
12244       (unless (eq face (get-text-property beg 'face))
12245         (gnus-put-text-property-excluding-characters-with-faces
12246          beg (point-at-eol) 'face
12247          (setq face (if (boundp face) (symbol-value face) face)))
12248         (when gnus-summary-highlight-line-function
12249           (funcall gnus-summary-highlight-line-function article face))))))
12250
12251 (defun gnus-update-read-articles (group unread &optional compute)
12252   "Update the list of read articles in GROUP.
12253 UNREAD is a sorted list."
12254   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12255         (info (gnus-get-info group))
12256         (prev 1)
12257         read)
12258     (if (or (not info) (not active))
12259         ;; There is no info on this group if it was, in fact,
12260         ;; killed.  Gnus stores no information on killed groups, so
12261         ;; there's nothing to be done.
12262         ;; One could store the information somewhere temporarily,
12263         ;; perhaps...  Hmmm...
12264         ()
12265       ;; Remove any negative articles numbers.
12266       (while (and unread (< (car unread) 0))
12267         (setq unread (cdr unread)))
12268       ;; Remove any expired article numbers
12269       (while (and unread (< (car unread) (car active)))
12270         (setq unread (cdr unread)))
12271       ;; Compute the ranges of read articles by looking at the list of
12272       ;; unread articles.
12273       (while unread
12274         (when (/= (car unread) prev)
12275           (push (if (= prev (1- (car unread))) prev
12276                   (cons prev (1- (car unread))))
12277                 read))
12278         (setq prev (1+ (car unread)))
12279         (setq unread (cdr unread)))
12280       (when (<= prev (cdr active))
12281         (push (cons prev (cdr active)) read))
12282       (setq read (if (> (length read) 1) (nreverse read) read))
12283       (if compute
12284           read
12285         (save-excursion
12286           (let (setmarkundo)
12287             ;; Propagate the read marks to the backend.
12288             (when (and gnus-propagate-marks
12289                        (gnus-check-backend-function 'request-set-mark group))
12290               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12291                     (add (gnus-remove-from-range read (gnus-info-read info))))
12292                 (when (or add del)
12293                   (unless (gnus-check-group group)
12294                     (error "Can't open server for %s" group))
12295                   (gnus-request-set-mark
12296                    group (delq nil (list (if add (list add 'add '(read)))
12297                                          (if del (list del 'del '(read))))))
12298                   (setq setmarkundo
12299                         `(gnus-request-set-mark
12300                           ,group
12301                           ',(delq nil (list
12302                                        (if del (list del 'add '(read)))
12303                                        (if add (list add 'del '(read))))))))))
12304             (set-buffer gnus-group-buffer)
12305             (gnus-undo-register
12306               `(progn
12307                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12308                  (gnus-info-set-read ',info ',(gnus-info-read info))
12309                  (gnus-get-unread-articles-in-group ',info
12310                                                     (gnus-active ,group))
12311                  (gnus-group-update-group ,group t)
12312                  ,setmarkundo))))
12313         ;; Enter this list into the group info.
12314         (gnus-info-set-read info read)
12315         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12316         (gnus-get-unread-articles-in-group info (gnus-active group))
12317         t))))
12318
12319 (defun gnus-offer-save-summaries ()
12320   "Offer to save all active summary buffers."
12321   (let (buffers)
12322     ;; Go through all buffers and find all summaries.
12323     (dolist (buffer (buffer-list))
12324       (when (and (setq buffer (buffer-name buffer))
12325                  (string-match "Summary" buffer)
12326                  (with-current-buffer buffer
12327                    ;; We check that this is, indeed, a summary buffer.
12328                    (and (eq major-mode 'gnus-summary-mode)
12329                         ;; Also make sure this isn't bogus.
12330                         gnus-newsgroup-prepared
12331                         ;; Also make sure that this isn't a
12332                         ;; dead summary buffer.
12333                         (not gnus-dead-summary-mode))))
12334         (push buffer buffers)))
12335     ;; Go through all these summary buffers and offer to save them.
12336     (when buffers
12337       (save-excursion
12338         (map-y-or-n-p
12339          "Update summary buffer %s? "
12340          (lambda (buf)
12341            (switch-to-buffer buf)
12342            (gnus-summary-exit))
12343          buffers)))))
12344
12345 (defun gnus-summary-setup-default-charset ()
12346   "Setup newsgroup default charset."
12347   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12348       (setq gnus-newsgroup-charset nil)
12349     (let* ((ignored-charsets
12350             (or gnus-newsgroup-ephemeral-ignored-charsets
12351                 (append
12352                  (and gnus-newsgroup-name
12353                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12354                  gnus-newsgroup-ignored-charsets))))
12355       (setq gnus-newsgroup-charset
12356             (or gnus-newsgroup-ephemeral-charset
12357                 (and gnus-newsgroup-name
12358                      (gnus-parameter-charset gnus-newsgroup-name))
12359                 gnus-default-charset))
12360       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12361            ignored-charsets))))
12362
12363 ;;;
12364 ;;; Mime Commands
12365 ;;;
12366
12367 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12368   "Display the current article buffer fully MIME-buttonized.
12369 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12370 treated as multipart/mixed."
12371   (interactive "P")
12372   (require 'gnus-art)
12373   (let ((gnus-unbuttonized-mime-types nil)
12374         (gnus-mime-display-multipart-as-mixed show-all-parts))
12375     (gnus-summary-show-article)))
12376
12377 (defun gnus-summary-repair-multipart (article)
12378   "Add a Content-Type header to a multipart article without one."
12379   (interactive (list (gnus-summary-article-number)))
12380   (gnus-with-article article
12381     (message-narrow-to-head)
12382     (message-remove-header "Mime-Version")
12383     (goto-char (point-max))
12384     (insert "Mime-Version: 1.0\n")
12385     (widen)
12386     (when (search-forward "\n--" nil t)
12387       (let ((separator (buffer-substring (point) (point-at-eol))))
12388         (message-narrow-to-head)
12389         (message-remove-header "Content-Type")
12390         (goto-char (point-max))
12391         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12392                         separator))
12393         (widen))))
12394   (let (gnus-mark-article-hook)
12395     (gnus-summary-select-article t t nil article)))
12396
12397 (defun gnus-summary-toggle-display-buttonized ()
12398   "Toggle the buttonizing of the article buffer."
12399   (interactive)
12400   (require 'gnus-art)
12401   (if (setq gnus-inhibit-mime-unbuttonizing
12402             (not gnus-inhibit-mime-unbuttonizing))
12403       (let ((gnus-unbuttonized-mime-types nil))
12404         (gnus-summary-show-article))
12405     (gnus-summary-show-article)))
12406
12407 ;;;
12408 ;;; Generic summary marking commands
12409 ;;;
12410
12411 (defvar gnus-summary-marking-alist
12412   '((read gnus-del-mark "d")
12413     (unread gnus-unread-mark "u")
12414     (ticked gnus-ticked-mark "!")
12415     (dormant gnus-dormant-mark "?")
12416     (expirable gnus-expirable-mark "e"))
12417   "An alist of names/marks/keystrokes.")
12418
12419 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12420 (defvar gnus-summary-mark-map)
12421
12422 (defun gnus-summary-make-all-marking-commands ()
12423   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12424   (dolist (elem gnus-summary-marking-alist)
12425     (apply 'gnus-summary-make-marking-command elem)))
12426
12427 (defun gnus-summary-make-marking-command (name mark keystroke)
12428   (let ((map (make-sparse-keymap)))
12429     (define-key gnus-summary-generic-mark-map keystroke map)
12430     (dolist (lway `((next "next" next nil "n")
12431                     (next-unread "next unread" next t "N")
12432                     (prev "previous" prev nil "p")
12433                     (prev-unread "previous unread" prev t "P")
12434                     (nomove "" nil nil ,keystroke)))
12435       (let ((func (gnus-summary-make-marking-command-1
12436                    mark (car lway) lway name)))
12437         (setq func (eval func))
12438         (define-key map (nth 4 lway) func)))))
12439
12440 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12441   `(defun ,(intern
12442             (format "gnus-summary-put-mark-as-%s%s"
12443                     name (if (eq way 'nomove)
12444                              ""
12445                            (concat "-" (symbol-name way)))))
12446      (n)
12447      ,(format
12448        "Mark the current article as %s%s.
12449 If N, the prefix, then repeat N times.
12450 If N is negative, move in reverse order.
12451 The difference between N and the actual number of articles marked is
12452 returned."
12453        name (cadr lway))
12454      (interactive "p")
12455      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12456
12457 (defun gnus-summary-generic-mark (n mark move unread)
12458   "Mark N articles with MARK."
12459   (unless (eq major-mode 'gnus-summary-mode)
12460     (error "This command can only be used in the summary buffer"))
12461   (gnus-summary-show-thread)
12462   (let ((nummove
12463          (cond
12464           ((eq move 'next) 1)
12465           ((eq move 'prev) -1)
12466           (t 0))))
12467     (if (zerop nummove)
12468         (setq n 1)
12469       (when (< n 0)
12470         (setq n (abs n)
12471               nummove (* -1 nummove))))
12472     (while (and (> n 0)
12473                 (gnus-summary-mark-article nil mark)
12474                 (zerop (gnus-summary-next-subject nummove unread t)))
12475       (setq n (1- n)))
12476     (when (/= 0 n)
12477       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12478     (gnus-summary-recenter)
12479     (gnus-summary-position-point)
12480     (gnus-set-mode-line 'summary)
12481     n))
12482
12483 (defun gnus-summary-insert-articles (articles)
12484   (when (setq articles
12485               (gnus-sorted-difference articles
12486                                       (mapcar (lambda (h)
12487                                                 (mail-header-number h))
12488                                               gnus-newsgroup-headers)))
12489     (setq gnus-newsgroup-headers
12490           (gnus-merge 'list
12491                       gnus-newsgroup-headers
12492                       (gnus-fetch-headers articles)
12493                       'gnus-article-sort-by-number))
12494     ;; Suppress duplicates?
12495     (when gnus-suppress-duplicates
12496       (gnus-dup-suppress-articles))
12497
12498     (if (and gnus-fetch-old-headers
12499              (eq gnus-headers-retrieved-by 'nov))
12500         ;; We might want to build some more threads first.
12501         (if (eq gnus-fetch-old-headers 'invisible)
12502             (gnus-build-all-threads)
12503           (gnus-build-old-threads))
12504       ;; Mark the inserted articles that are unread as unread.
12505       (setq gnus-newsgroup-unreads
12506             (gnus-sorted-nunion
12507              gnus-newsgroup-unreads
12508              (gnus-sorted-nintersection
12509               (gnus-list-of-unread-articles gnus-newsgroup-name)
12510               articles)))
12511       ;; Mark the inserted articles as selected so that the information
12512       ;; of the marks having been changed by a user may be updated when
12513       ;; exiting this group.  See `gnus-summary-update-info'.
12514       (dolist (art articles)
12515         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12516     ;; Let the Gnus agent mark articles as read.
12517     (when gnus-agent
12518       (gnus-agent-get-undownloaded-list))
12519     ;; Remove list identifiers from subject
12520     (when gnus-list-identifiers
12521       (gnus-summary-remove-list-identifiers))
12522     ;; First and last article in this newsgroup.
12523     (when gnus-newsgroup-headers
12524       (setq gnus-newsgroup-begin
12525             (mail-header-number (car gnus-newsgroup-headers))
12526             gnus-newsgroup-end
12527             (mail-header-number
12528              (gnus-last-element gnus-newsgroup-headers))))
12529     (when gnus-use-scoring
12530       (gnus-possibly-score-headers))))
12531
12532 (defun gnus-summary-insert-old-articles (&optional all)
12533   "Insert all old articles in this group.
12534 If ALL is non-nil, already read articles become readable.
12535 If ALL is a number, fetch this number of articles."
12536   (interactive "P")
12537   (prog1
12538       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12539             older len)
12540         (setq older
12541               ;; Some nntp servers lie about their active range.  When
12542               ;; this happens, the active range can be in the millions.
12543               ;; Use a compressed range to avoid creating a huge list.
12544               (gnus-range-difference (list gnus-newsgroup-active) old))
12545         (setq len (gnus-range-length older))
12546         (cond
12547          ((null older) nil)
12548          ((numberp all)
12549           (if (< all len)
12550               (let ((older-range (nreverse older)))
12551                 (setq older nil)
12552
12553                 (while (> all 0)
12554                   (let* ((r (pop older-range))
12555                          (min (if (numberp r) r (car r)))
12556                          (max (if (numberp r) r (cdr r))))
12557                     (while (and (<= min max)
12558                                 (> all 0))
12559                       (push max older)
12560                       (setq all (1- all)
12561                             max (1- max))))))
12562             (setq older (gnus-uncompress-range older))))
12563          (all
12564           (setq older (gnus-uncompress-range older)))
12565          (t
12566           (when (and (numberp gnus-large-newsgroup)
12567                    (> len gnus-large-newsgroup))
12568               (let* ((cursor-in-echo-area nil)
12569                      (initial (gnus-parameter-large-newsgroup-initial
12570                                gnus-newsgroup-name))
12571                      (input
12572                       (read-string
12573                        (format
12574                         "How many articles from %s (%s %d): "
12575                         (gnus-group-decoded-name gnus-newsgroup-name)
12576                         (if initial "max" "default")
12577                         len)
12578                        (if initial
12579                            (cons (number-to-string initial)
12580                                  0)))))
12581                 (unless (string-match "^[ \t]*$" input)
12582                   (setq all (string-to-number input))
12583                   (if (< all len)
12584                       (let ((older-range (nreverse older)))
12585                         (setq older nil)
12586
12587                         (while (> all 0)
12588                           (let* ((r (pop older-range))
12589                                  (min (if (numberp r) r (car r)))
12590                                  (max (if (numberp r) r (cdr r))))
12591                             (while (and (<= min max)
12592                                         (> all 0))
12593                               (push max older)
12594                               (setq all (1- all)
12595                                     max (1- max))))))))))
12596           (setq older (gnus-uncompress-range older))))
12597         (if (not older)
12598             (message "No old news.")
12599           (gnus-summary-insert-articles older)
12600           (gnus-summary-limit (gnus-sorted-nunion old older))))
12601     (gnus-summary-position-point)))
12602
12603 (defun gnus-summary-insert-new-articles ()
12604   "Insert all new articles in this group."
12605   (interactive)
12606   (prog1
12607       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12608             (old-active gnus-newsgroup-active)
12609             (nnmail-fetched-sources (list t))
12610             i new)
12611         (setq gnus-newsgroup-active
12612               (gnus-activate-group gnus-newsgroup-name 'scan))
12613         (setq i (cdr gnus-newsgroup-active))
12614         (while (> i (cdr old-active))
12615           (push i new)
12616           (decf i))
12617         (if (not new)
12618             (message "No gnus is bad news")
12619           (gnus-summary-insert-articles new)
12620           (setq gnus-newsgroup-unreads
12621                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12622           (gnus-summary-limit (gnus-sorted-nunion old new))))
12623     (gnus-summary-position-point)))
12624
12625 ;;; Bookmark support for Gnus.
12626 (declare-function bookmark-make-record-default
12627                   "bookmark" (&optional no-file no-context posn))
12628 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12629 (declare-function bookmark-default-handler "bookmark" (bmk))
12630 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12631 (defvar bookmark-yank-point)
12632 (defvar bookmark-current-buffer)
12633
12634 (defun gnus-summary-bookmark-make-record ()
12635   "Make a bookmark entry for a Gnus summary buffer."
12636   (let (pos buf)
12637     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12638       (save-restriction              ; FIXME is it necessary to widen?
12639         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12640       (setq buf "art") ; We are recording bookmark from article buffer.
12641       (setq bookmark-yank-point (point))
12642       (setq bookmark-current-buffer (current-buffer))
12643       (gnus-article-show-summary))      ; Go back in summary buffer.
12644     ;; We are now recording bookmark from summary buffer.
12645     (unless buf (setq buf "sum"))
12646     (let* ((subject (elt (gnus-summary-article-header) 1))
12647            (grp     (car gnus-article-current))
12648            (art     (cdr gnus-article-current))
12649            (head    (gnus-summary-article-header art))
12650            (id      (mail-header-id head)))
12651       `(,subject
12652         ,@(condition-case nil
12653               (bookmark-make-record-default 'no-file 'no-context pos)
12654             (wrong-number-of-arguments
12655              (bookmark-make-record-default 'point-only)))
12656         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12657         (group . ,grp) (article . ,art)
12658         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12659
12660 ;;;###autoload
12661 (defun gnus-summary-bookmark-jump (bookmark)
12662   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12663 BOOKMARK is a bookmark name or a bookmark record."
12664   (let ((group    (bookmark-prop-get bookmark 'group))
12665         (article  (bookmark-prop-get bookmark 'article))
12666         (id       (bookmark-prop-get bookmark 'message-id))
12667         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12668     (gnus-fetch-group group (list article))
12669     (gnus-summary-insert-cached-articles)
12670     (gnus-summary-goto-article id nil 'force)
12671     ;; FIXME we have to wait article buffer is ready (only large buffer)
12672     ;; Is there a better solution to know that?
12673     ;; If we don't wait `bookmark-default-handler' will have no chance
12674     ;; to set position. However there is no error, just wrong pos.
12675     (sit-for 1)
12676     (when (string= buf "Gnus-art")
12677       (other-window 1))
12678     (bookmark-default-handler
12679      `(""
12680        (buffer . ,(current-buffer))
12681        . ,(bookmark-get-bookmark-record bookmark)))))
12682
12683 (gnus-summary-make-all-marking-commands)
12684
12685 (gnus-ems-redefine)
12686
12687 (provide 'gnus-sum)
12688
12689 (run-hooks 'gnus-sum-load-hook)
12690
12691 ;; Local Variables:
12692 ;; coding: iso-8859-1
12693 ;; End:
12694
12695 ;;; gnus-sum.el ends here