(gnus-summary-scroll-up): Add more documentation.
[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 'unseen-or-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 "24.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 (defcustom gnus-auto-center-group t
455   "If non-nil, always center the group buffer."
456   :group 'gnus-summary-maneuvering
457   :type 'boolean)
458
459 (defcustom gnus-show-all-headers nil
460   "*If non-nil, don't hide any headers."
461   :group 'gnus-article-hiding
462   :group 'gnus-article-headers
463   :type 'boolean)
464
465 (defcustom gnus-summary-ignore-duplicates nil
466   "*If non-nil, ignore articles with identical Message-ID headers."
467   :group 'gnus-summary
468   :type 'boolean)
469
470 (defcustom gnus-single-article-buffer nil
471   "*If non-nil, display all articles in the same buffer.
472 If nil, each group will get its own article buffer."
473   :version "24.1"
474   :group 'gnus-article-various
475   :type 'boolean)
476
477 (defcustom gnus-break-pages t
478   "*If non-nil, do page breaking on articles.
479 The page delimiter is specified by the `gnus-page-delimiter'
480 variable."
481   :group 'gnus-article-various
482   :type 'boolean)
483
484 (defcustom gnus-move-split-methods nil
485   "*Variable used to suggest where articles are to be moved to.
486 It uses the same syntax as the `gnus-split-methods' variable.
487 However, whereas `gnus-split-methods' specifies file names as targets,
488 this variable specifies group names."
489   :group 'gnus-summary-mail
490   :type '(repeat (choice (list :value (fun) function)
491                          (cons :value ("" "") regexp (repeat string))
492                          (sexp :value nil))))
493
494 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
495   "Function used to compute default prefix for article move/copy/etc prompts.
496 The function should take one argument, a group name, and return a
497 string with the suggested prefix."
498   :group 'gnus-summary-mail
499   :type 'function)
500
501 ;; FIXME: Although the custom type is `character' for the following variables,
502 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
503
504 (defcustom gnus-unread-mark ?           ;Whitespace
505   "*Mark used for unread articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-ticked-mark ?!
510   "*Mark used for ticked articles."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-dormant-mark ??
515   "*Mark used for dormant articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-del-mark ?r
520   "*Mark used for del'd articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-read-mark ?R
525   "*Mark used for read articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-expirable-mark ?E
530   "*Mark used for expirable articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-killed-mark ?K
535   "*Mark used for killed articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-spam-mark ?$
540   "*Mark used for spam articles."
541   :version "22.1"
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-kill-file-mark ?X
546   "*Mark used for articles killed by kill files."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-low-score-mark ?Y
551   "*Mark used for articles with a low score."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-catchup-mark ?C
556   "*Mark used for articles that are caught up."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-replied-mark ?A
561   "*Mark used for articles that have been replied to."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-forwarded-mark ?F
566   "*Mark used for articles that have been forwarded."
567   :version "22.1"
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-recent-mark ?N
572   "*Mark used for articles that are recent."
573   :version "22.1"
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-cached-mark ?*
578   "*Mark used for articles that are in the cache."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-saved-mark ?S
583   "*Mark used for articles that have been saved."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-unseen-mark ?.
588   "*Mark used for articles that haven't been seen."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-no-mark ?               ;Whitespace
594   "*Mark used for articles that have no other secondary mark."
595   :version "22.1"
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-ancient-mark ?O
600   "*Mark used for ancient articles."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-sparse-mark ?Q
605   "*Mark used for sparsely reffed articles."
606   :group 'gnus-summary-marks
607   :type 'character)
608
609 (defcustom gnus-canceled-mark ?G
610   "*Mark used for canceled articles."
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-duplicate-mark ?M
615   "*Mark used for duplicate articles."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-undownloaded-mark ?-
620   "*Mark used for articles that weren't downloaded."
621   :version "22.1"
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-downloaded-mark ?+
626   "*Mark used for articles that were downloaded."
627   :group 'gnus-summary-marks
628   :type 'character)
629
630 (defcustom gnus-downloadable-mark ?%
631   "*Mark used for articles that are to be downloaded."
632   :group 'gnus-summary-marks
633   :type 'character)
634
635 (defcustom gnus-unsendable-mark ?=
636   "*Mark used for articles that won't be sent."
637   :group 'gnus-summary-marks
638   :type 'character)
639
640 (defcustom gnus-score-over-mark ?+
641   "*Score mark used for articles with high scores."
642   :group 'gnus-summary-marks
643   :type 'character)
644
645 (defcustom gnus-score-below-mark ?-
646   "*Score mark used for articles with low scores."
647   :group 'gnus-summary-marks
648   :type 'character)
649
650 (defcustom gnus-empty-thread-mark ?     ;Whitespace
651   "*There is no thread under the article."
652   :group 'gnus-summary-marks
653   :type 'character)
654
655 (defcustom gnus-not-empty-thread-mark ?=
656   "*There is a thread under the article."
657   :group 'gnus-summary-marks
658   :type 'character)
659
660 (defcustom gnus-view-pseudo-asynchronously nil
661   "*If non-nil, Gnus will view pseudo-articles asynchronously."
662   :group 'gnus-extract-view
663   :type 'boolean)
664
665 (defcustom gnus-auto-expirable-marks
666   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
667         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
668         gnus-duplicate-mark)
669   "*The list of marks converted into expiration if a group is auto-expirable."
670   :version "24.1"
671   :group 'gnus-summary
672   :type '(repeat character))
673
674 (defcustom gnus-inhibit-user-auto-expire t
675   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
676   :version "21.1"
677   :group 'gnus-summary
678   :type 'boolean)
679
680 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
681   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
682 If nil, the expirable marks will be unchanged except that the marks
683 will be removed when copying or moving articles to a group that has
684 not turned auto-expire on.  If non-nil, articles that have been read
685 will be marked as expirable when being copied or moved to a group in
686 which auto-expire is turned on."
687   :version "23.2"
688   :type 'boolean
689   :group 'gnus-summary-marks)
690
691 (defcustom gnus-view-pseudos nil
692   "*If `automatic', pseudo-articles will be viewed automatically.
693 If `not-confirm', pseudos will be viewed automatically, and the user
694 will not be asked to confirm the command."
695   :group 'gnus-extract-view
696   :type '(choice (const :tag "off" nil)
697                  (const automatic)
698                  (const not-confirm)))
699
700 (defcustom gnus-view-pseudos-separately t
701   "*If non-nil, one pseudo-article will be created for each file to be viewed.
702 If nil, all files that use the same viewing command will be given as a
703 list of parameters to that command."
704   :group 'gnus-extract-view
705   :type 'boolean)
706
707 (defcustom gnus-insert-pseudo-articles t
708   "*If non-nil, insert pseudo-articles when decoding articles."
709   :group 'gnus-extract-view
710   :type 'boolean)
711
712 (defcustom gnus-summary-dummy-line-format
713   "   %(:                             :%) %S\n"
714   "*The format specification for the dummy roots in the summary buffer.
715 It works along the same lines as a normal formatting string,
716 with some simple extensions.
717
718 %S  The subject
719
720 General format specifiers can also be used.
721 See `(gnus)Formatting Variables'."
722   :link '(custom-manual "(gnus)Formatting Variables")
723   :group 'gnus-threading
724   :type 'string)
725
726 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
727   "*The format specification for the summary mode line.
728 It works along the same lines as a normal formatting string,
729 with some simple extensions:
730
731 %G  Group name
732 %p  Unprefixed group name
733 %A  Current article number
734 %z  Current article score
735 %V  Gnus version
736 %U  Number of unread articles in the group
737 %e  Number of unselected articles in the group
738 %Z  A string with unread/unselected article counts
739 %g  Shortish group name
740 %S  Subject of the current article
741 %u  User-defined spec
742 %s  Current score file name
743 %d  Number of dormant articles
744 %r  Number of articles that have been marked as read in this session
745 %E  Number of articles expunged by the score files"
746   :group 'gnus-summary-format
747   :type 'string)
748
749 (defcustom gnus-list-identifiers nil
750   "Regexp that matches list identifiers to be removed from subject.
751 This can also be a list of regexps."
752   :version "21.1"
753   :group 'gnus-summary-format
754   :group 'gnus-article-hiding
755   :type '(choice (const :tag "none" nil)
756                  (regexp :value ".*")
757                  (repeat :value (".*") regexp)))
758
759 (defcustom gnus-summary-mark-below 0
760   "*Mark all articles with a score below this variable as read.
761 This variable is local to each summary buffer and usually set by the
762 score file."
763   :group 'gnus-score-default
764   :type 'integer)
765
766 (defun gnus-widget-reversible-match (widget value)
767   "Ignoring WIDGET, convert VALUE to internal form.
768 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
769   ;; (debug value)
770   (or (symbolp value)
771       (and (listp value)
772            (eq (length value) 2)
773            (eq (nth 0 value) 'not)
774            (symbolp (nth 1 value)))))
775
776 (defun gnus-widget-reversible-to-internal (widget value)
777   "Ignoring WIDGET, convert VALUE to internal form.
778 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
779 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
780   ;; (debug value)
781   (if (atom value)
782       (list value nil)
783     (list (nth 1 value) t)))
784
785 (defun gnus-widget-reversible-to-external (widget value)
786   "Ignoring WIDGET, convert VALUE to external form.
787 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
788 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
789   ;; (debug value)
790   (if (nth 1 value)
791       (list 'not (nth 0 value))
792     (nth 0 value)))
793
794 (define-widget 'gnus-widget-reversible 'group
795   "A `group' that convert values."
796   :match 'gnus-widget-reversible-match
797   :value-to-internal 'gnus-widget-reversible-to-internal
798   :value-to-external 'gnus-widget-reversible-to-external)
799
800 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
801   "*List of functions used for sorting articles in the summary buffer.
802
803 Each function takes two articles and returns non-nil if the first
804 article should be sorted before the other.  If you use more than one
805 function, the primary sort function should be the last.  You should
806 probably always include `gnus-article-sort-by-number' in the list of
807 sorting functions -- preferably first.  Also note that sorting by date
808 is often much slower than sorting by number, and the sorting order is
809 very similar.  (Sorting by date means sorting by the time the message
810 was sent, sorting by number means sorting by arrival time.)
811
812 Each item can also be a list `(not F)' where F is a function;
813 this reverses the sort order.
814
815 Ready-made functions include `gnus-article-sort-by-number',
816 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
817 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
818 and `gnus-article-sort-by-score'.
819
820 When threading is turned on, the variable `gnus-thread-sort-functions'
821 controls how articles are sorted."
822   :group 'gnus-summary-sort
823   :type '(repeat (gnus-widget-reversible
824                   (choice (function-item gnus-article-sort-by-number)
825                           (function-item gnus-article-sort-by-author)
826                           (function-item gnus-article-sort-by-subject)
827                           (function-item gnus-article-sort-by-date)
828                           (function-item gnus-article-sort-by-score)
829                           (function-item gnus-article-sort-by-random)
830                           (function :tag "other"))
831                   (boolean :tag "Reverse order"))))
832
833
834 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
835   "*List of functions used for sorting threads in the summary buffer.
836 By default, threads are sorted by article number.
837
838 Each function takes two threads and returns non-nil if the first
839 thread should be sorted before the other.  If you use more than one
840 function, the primary sort function should be the last.  You should
841 probably always include `gnus-thread-sort-by-number' in the list of
842 sorting functions -- preferably first.  Also note that sorting by date
843 is often much slower than sorting by number, and the sorting order is
844 very similar.  (Sorting by date means sorting by the time the message
845 was sent, sorting by number means sorting by arrival time.)
846
847 Each list item can also be a list `(not F)' where F is a
848 function; this specifies reversed sort order.
849
850 Ready-made functions include `gnus-thread-sort-by-number',
851 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
852 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
853 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
854 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
855 and `gnus-thread-sort-by-total-score' (see
856 `gnus-thread-score-function').
857
858 When threading is turned off, the variable
859 `gnus-article-sort-functions' controls how articles are sorted."
860   :group 'gnus-summary-sort
861   :type '(repeat
862           (gnus-widget-reversible
863            (choice (function-item gnus-thread-sort-by-number)
864                    (function-item gnus-thread-sort-by-author)
865                    (function-item gnus-thread-sort-by-recipient)
866                    (function-item gnus-thread-sort-by-subject)
867                    (function-item gnus-thread-sort-by-date)
868                    (function-item gnus-thread-sort-by-score)
869                    (function-item gnus-thread-sort-by-most-recent-number)
870                    (function-item gnus-thread-sort-by-most-recent-date)
871                    (function-item gnus-thread-sort-by-random)
872                    (function-item gnus-thread-sort-by-total-score)
873                    (function :tag "other"))
874            (boolean :tag "Reverse order"))))
875
876 (defcustom gnus-thread-score-function '+
877   "*Function used for calculating the total score of a thread.
878
879 The function is called with the scores of the article and each
880 subthread and should then return the score of the thread.
881
882 Some functions you can use are `+', `max', or `min'."
883   :group 'gnus-summary-sort
884   :type 'function)
885
886 (defcustom gnus-summary-expunge-below nil
887   "All articles that have a score less than this variable will be expunged.
888 This variable is local to the summary buffers."
889   :group 'gnus-score-default
890   :type '(choice (const :tag "off" nil)
891                  integer))
892
893 (defcustom gnus-thread-expunge-below nil
894   "All threads that have a total score less than this variable will be expunged.
895 See `gnus-thread-score-function' for en explanation of what a
896 \"thread score\" is.
897
898 This variable is local to the summary buffers."
899   :group 'gnus-threading
900   :group 'gnus-score-default
901   :type '(choice (const :tag "off" nil)
902                  integer))
903
904 (defcustom gnus-summary-mode-hook nil
905   "*A hook for Gnus summary mode.
906 This hook is run before any variables are set in the summary buffer."
907   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
908   :group 'gnus-summary-various
909   :type 'hook)
910
911 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
912 (when (featurep 'xemacs)
913   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
914   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
915   (add-hook 'gnus-summary-mode-hook
916             'gnus-xmas-switch-horizontal-scrollbar-off))
917
918 (defcustom gnus-summary-menu-hook nil
919   "*Hook run after the creation of the summary mode menu."
920   :group 'gnus-summary-visual
921   :type 'hook)
922
923 (defcustom gnus-summary-exit-hook nil
924   "*A hook called on exit from the summary buffer.
925 It will be called with point in the group buffer."
926   :group 'gnus-summary-exit
927   :type 'hook)
928
929 (defcustom gnus-summary-prepare-hook nil
930   "*A hook called after the summary buffer has been generated.
931 If you want to modify the summary buffer, you can use this hook."
932   :group 'gnus-summary-various
933   :type 'hook)
934
935 (defcustom gnus-summary-prepared-hook nil
936   "*A hook called as the last thing after the summary buffer has been generated."
937   :group 'gnus-summary-various
938   :type 'hook)
939
940 (defcustom gnus-summary-generate-hook nil
941   "*A hook run just before generating the summary buffer.
942 This hook is commonly used to customize threading variables and the
943 like."
944   :group 'gnus-summary-various
945   :type 'hook)
946
947 (defcustom gnus-select-group-hook nil
948   "*A hook called when a newsgroup is selected.
949
950 If you'd like to simplify subjects like the
951 `gnus-summary-next-same-subject' command does, you can use the
952 following hook:
953
954  (add-hook gnus-select-group-hook
955            (lambda ()
956              (mapcar (lambda (header)
957                        (mail-header-set-subject
958                         header
959                         (gnus-simplify-subject
960                          (mail-header-subject header) 're-only)))
961                      gnus-newsgroup-headers)))"
962   :group 'gnus-group-select
963   :type 'hook)
964
965 (defcustom gnus-select-article-hook nil
966   "*A hook called when an article is selected."
967   :group 'gnus-summary-choose
968   :options '(gnus-agent-fetch-selected-article)
969   :type 'hook)
970
971 (defcustom gnus-visual-mark-article-hook
972   (list 'gnus-highlight-selected-summary)
973   "*Hook run after selecting an article in the summary buffer.
974 It is meant to be used for highlighting the article in some way.  It
975 is not run if `gnus-visual' is nil."
976   :group 'gnus-summary-visual
977   :type 'hook)
978
979 (defcustom gnus-parse-headers-hook nil
980   "*A hook called before parsing the headers."
981   :group 'gnus-various
982   :type 'hook)
983
984 (defcustom gnus-exit-group-hook nil
985   "*A hook called when exiting summary mode.
986 This hook is not called from the non-updating exit commands like `Q'."
987   :group 'gnus-various
988   :type 'hook)
989
990 (defcustom gnus-summary-update-hook nil
991   "*A hook called when a summary line is changed.
992 The hook will not be called if `gnus-visual' is nil.
993
994 The default function `gnus-summary-highlight-line' will
995 highlight the line according to the `gnus-summary-highlight'
996 variable."
997   :group 'gnus-summary-visual
998   :type 'hook)
999
1000 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1001   "*A hook called when an article is selected for the first time.
1002 The hook is intended to mark an article as read (or unread)
1003 automatically when it is selected."
1004   :group 'gnus-summary-choose
1005   :type 'hook)
1006
1007 (defcustom gnus-group-no-more-groups-hook nil
1008   "*A hook run when returning to group mode having no more (unread) groups."
1009   :group 'gnus-group-select
1010   :type 'hook)
1011
1012 (defcustom gnus-ps-print-hook nil
1013   "*A hook run before ps-printing something from Gnus."
1014   :group 'gnus-summary
1015   :type 'hook)
1016
1017 (defcustom gnus-summary-article-move-hook nil
1018   "*A hook called after an article is moved, copied, respooled, or crossposted."
1019   :version "22.1"
1020   :group 'gnus-summary
1021   :type 'hook)
1022
1023 (defcustom gnus-summary-article-delete-hook nil
1024   "*A hook called after an article is deleted."
1025   :version "22.1"
1026   :group 'gnus-summary
1027   :type 'hook)
1028
1029 (defcustom gnus-summary-article-expire-hook nil
1030   "*A hook called after an article is expired."
1031   :version "22.1"
1032   :group 'gnus-summary
1033   :type 'hook)
1034
1035 (defcustom gnus-summary-display-arrow
1036   (and (fboundp 'display-graphic-p)
1037        (display-graphic-p))
1038   "*If non-nil, display an arrow highlighting the current article."
1039   :version "22.1"
1040   :group 'gnus-summary
1041   :type 'boolean)
1042
1043 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1044   "Face used for highlighting the current article in the summary buffer."
1045   :group 'gnus-summary-visual
1046   :type 'face)
1047
1048 (defvar gnus-tmp-downloaded nil)
1049
1050 (defcustom gnus-summary-highlight
1051   '(((eq mark gnus-canceled-mark)
1052      . gnus-summary-cancelled)
1053     ((and uncached (> score default-high))
1054      . gnus-summary-high-undownloaded)
1055     ((and uncached (< score default-low))
1056      . gnus-summary-low-undownloaded)
1057     (uncached
1058      . gnus-summary-normal-undownloaded)
1059     ((and (> score default-high)
1060           (or (eq mark gnus-dormant-mark)
1061               (eq mark gnus-ticked-mark)))
1062      . gnus-summary-high-ticked)
1063     ((and (< score default-low)
1064           (or (eq mark gnus-dormant-mark)
1065               (eq mark gnus-ticked-mark)))
1066      . gnus-summary-low-ticked)
1067     ((or (eq mark gnus-dormant-mark)
1068          (eq mark gnus-ticked-mark))
1069      . gnus-summary-normal-ticked)
1070     ((and (> score default-high) (eq mark gnus-ancient-mark))
1071      . gnus-summary-high-ancient)
1072     ((and (< score default-low) (eq mark gnus-ancient-mark))
1073      . gnus-summary-low-ancient)
1074     ((eq mark gnus-ancient-mark)
1075      . gnus-summary-normal-ancient)
1076     ((and (> score default-high) (eq mark gnus-unread-mark))
1077      . gnus-summary-high-unread)
1078     ((and (< score default-low) (eq mark gnus-unread-mark))
1079      . gnus-summary-low-unread)
1080     ((eq mark gnus-unread-mark)
1081      . gnus-summary-normal-unread)
1082     ((> score default-high)
1083      . gnus-summary-high-read)
1084     ((< score default-low)
1085      . gnus-summary-low-read)
1086     (t
1087      . gnus-summary-normal-read))
1088   "*Controls the highlighting of summary buffer lines.
1089
1090 A list of (FORM . FACE) pairs.  When deciding how a particular
1091 summary line should be displayed, each form is evaluated.  The content
1092 of the face field after the first true form is used.  You can change
1093 how those summary lines are displayed, by editing the face field.
1094
1095 You can use the following variables in the FORM field.
1096
1097 score:        The article's score.
1098 default:      The default article score.
1099 default-high: The default score for high scored articles.
1100 default-low:  The default score for low scored articles.
1101 below:        The score below which articles are automatically marked as read.
1102 mark:         The article's mark.
1103 uncached:     Non-nil if the article is uncached."
1104   :group 'gnus-summary-visual
1105   :type '(repeat (cons (sexp :tag "Form" nil)
1106                        face)))
1107 (put 'gnus-summary-highlight 'risky-local-variable t)
1108
1109 (defcustom gnus-alter-header-function nil
1110   "Function called to allow alteration of article header structures.
1111 The function is called with one parameter, the article header vector,
1112 which it may alter in any way."
1113   :type '(choice (const :tag "None" nil)
1114                  function)
1115   :group 'gnus-summary)
1116
1117 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1118   "Function used to decode a string with encoded words.")
1119
1120 (defvar gnus-decode-encoded-address-function
1121   'mail-decode-encoded-address-string
1122   "Function used to decode addresses with encoded words.")
1123
1124 (defcustom gnus-extra-headers '(To Newsgroups)
1125   "*Extra headers to parse."
1126   :version "21.1"
1127   :group 'gnus-summary
1128   :type '(repeat symbol))
1129
1130 (defcustom gnus-ignored-from-addresses
1131   (and user-mail-address
1132        (not (string= user-mail-address ""))
1133        (regexp-quote user-mail-address))
1134   "*From headers that may be suppressed in favor of To headers.
1135 This can be a regexp or a list of regexps."
1136   :version "21.1"
1137   :group 'gnus-summary
1138   :type '(choice regexp
1139                  (repeat :tag "Regexp List" regexp)))
1140
1141 (defsubst gnus-ignored-from-addresses ()
1142   (gmm-regexp-concat gnus-ignored-from-addresses))
1143
1144 (defcustom gnus-summary-to-prefix "-> "
1145   "*String prefixed to the To field in the summary line when
1146 using `gnus-ignored-from-addresses'."
1147   :version "22.1"
1148   :group 'gnus-summary
1149   :type 'string)
1150
1151 (defcustom gnus-summary-newsgroup-prefix "=> "
1152   "*String prefixed to the Newsgroup field in the summary
1153 line when using `gnus-ignored-from-addresses'."
1154   :version "22.1"
1155   :group 'gnus-summary
1156   :type 'string)
1157
1158 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1159   "List of charsets that should be ignored.
1160 When these charsets are used in the \"charset\" parameter, the
1161 default charset will be used instead."
1162   :version "21.1"
1163   :type '(repeat symbol)
1164   :group 'gnus-charset)
1165
1166 (defcustom gnus-newsgroup-maximum-articles nil
1167   "The maximum number of articles a newsgroup.
1168 If this is a number, old articles in a newsgroup exceeding this number
1169 are silently ignored.  If it is nil, no article is ignored.  Note that
1170 setting this variable to a number might prevent you from reading very
1171 old articles."
1172   :group 'gnus-group-select
1173   :version "22.2"
1174   :type '(choice (const :tag "No limit" nil)
1175                  integer))
1176
1177 (gnus-define-group-parameter
1178  ignored-charsets
1179  :type list
1180  :function-document
1181  "Return the ignored charsets of GROUP."
1182  :variable gnus-group-ignored-charsets-alist
1183  :variable-default
1184  '(("alt\\.chinese\\.text" iso-8859-1))
1185  :variable-document
1186  "Alist of regexps (to match group names) and charsets that should be ignored.
1187 When these charsets are used in the \"charset\" parameter, the
1188 default charset will be used instead."
1189  :variable-group gnus-charset
1190  :variable-type '(repeat (cons (regexp :tag "Group")
1191                                (repeat symbol)))
1192  :parameter-type '(choice :tag "Ignored charsets"
1193                           :value nil
1194                           (repeat (symbol)))
1195  :parameter-document       "\
1196 List of charsets that should be ignored.
1197
1198 When these charsets are used in the \"charset\" parameter, the
1199 default charset will be used instead.")
1200
1201 (defcustom gnus-group-highlight-words-alist nil
1202   "Alist of group regexps and highlight regexps.
1203 This variable uses the same syntax as `gnus-emphasis-alist'."
1204   :version "21.1"
1205   :type '(repeat (cons (regexp :tag "Group")
1206                        (repeat (list (regexp :tag "Highlight regexp")
1207                                      (number :tag "Group for entire word" 0)
1208                                      (number :tag "Group for displayed part" 0)
1209                                      (symbol :tag "Face"
1210                                              gnus-emphasis-highlight-words)))))
1211   :group 'gnus-summary-visual)
1212
1213 (defcustom gnus-summary-show-article-charset-alist
1214   nil
1215   "Alist of number and charset.
1216 The article will be shown with the charset corresponding to the
1217 numbered argument.
1218 For example: ((1 . cn-gb-2312) (2 . big5))."
1219   :version "21.1"
1220   :type '(repeat (cons (number :tag "Argument" 1)
1221                        (symbol :tag "Charset")))
1222   :group 'gnus-charset)
1223
1224 (defcustom gnus-preserve-marks t
1225   "Whether marks are preserved when moving, copying and respooling messages."
1226   :version "21.1"
1227   :type 'boolean
1228   :group 'gnus-summary-marks)
1229
1230 (defcustom gnus-propagate-marks t
1231   "If non-nil, do not propagate marks to the backends."
1232   :version "23.1" ;; No Gnus
1233   :type 'boolean
1234   :group 'gnus-summary-marks)
1235
1236 (defcustom gnus-alter-articles-to-read-function nil
1237   "Function to be called to alter the list of articles to be selected."
1238   :type '(choice (const nil) function)
1239   :group 'gnus-summary)
1240
1241 (defcustom gnus-orphan-score nil
1242   "*All orphans get this score added.  Set in the score file."
1243   :group 'gnus-score-default
1244   :type '(choice (const nil)
1245                  integer))
1246
1247 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1248   "*A regexp to match MIME parts when saving multiple parts of a
1249 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1250 This regexp will be used by default when prompting the user for which
1251 type of files to save."
1252   :group 'gnus-summary
1253   :type 'regexp)
1254
1255 (defcustom gnus-read-all-available-headers nil
1256   "Whether Gnus should parse all headers made available to it.
1257 This is mostly relevant for slow back ends where the user may
1258 wish to widen the summary buffer to include all headers
1259 that were fetched."
1260   :version "22.1"
1261   :group 'gnus-summary
1262   :type '(choice boolean regexp))
1263
1264 (defcustom gnus-summary-pipe-output-default-command nil
1265   "Command (and optional arguments) used to pipe article to subprocess.
1266 This will be used as the default command if it is non-nil.  The value
1267 will be updated if you modify it when executing the command
1268 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1269   :version "23.1" ;; No Gnus
1270   :group 'gnus-summary
1271   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1272
1273 (defcustom gnus-summary-muttprint-program "muttprint"
1274   "Command (and optional arguments) used to run Muttprint.
1275 The value will be updated if you modify it when executing the command
1276 `gnus-summary-muttprint'."
1277   :version "22.1"
1278   :group 'gnus-summary
1279   :type 'string)
1280
1281 (defcustom gnus-article-loose-mime t
1282   "If non-nil, don't require MIME-Version header.
1283 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1284 supply the MIME-Version header or deliberately strip it from the mail.
1285 If non-nil (the default), Gnus will treat some articles as MIME
1286 even if the MIME-Version header is missing."
1287   :version "22.1"
1288   :type 'boolean
1289   :group 'gnus-article-mime)
1290
1291 (defcustom gnus-article-emulate-mime t
1292   "If non-nil, use MIME emulation for uuencode and the like.
1293 This means that Gnus will search message bodies for text that look
1294 like uuencoded bits, yEncoded bits, and so on, and present that using
1295 the normal Gnus MIME machinery."
1296   :version "22.1"
1297   :type 'boolean
1298   :group 'gnus-article-mime)
1299
1300 ;;; Internal variables
1301
1302 (defvar gnus-summary-display-cache nil)
1303 (defvar gnus-article-mime-handles nil)
1304 (defvar gnus-article-decoded-p nil)
1305 (defvar gnus-article-charset nil)
1306 (defvar gnus-article-ignored-charsets nil)
1307 (defvar gnus-scores-exclude-files nil)
1308 (defvar gnus-page-broken nil)
1309
1310 (defvar gnus-original-article nil)
1311 (defvar gnus-article-internal-prepare-hook nil)
1312 (defvar gnus-newsgroup-process-stack nil)
1313
1314 (defvar gnus-thread-indent-array nil)
1315 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1316 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1317   "Function called to sort the articles within a thread after it has been gathered together.")
1318
1319 (defvar gnus-summary-save-parts-type-history nil)
1320 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1321
1322 ;; Avoid highlighting in kill files.
1323 (defvar gnus-summary-inhibit-highlight nil)
1324 (defvar gnus-newsgroup-selected-overlay nil)
1325 (defvar gnus-inhibit-limiting nil)
1326 (defvar gnus-newsgroup-adaptive-score-file nil)
1327 (defvar gnus-current-score-file nil)
1328 (defvar gnus-current-move-group nil)
1329 (defvar gnus-current-copy-group nil)
1330 (defvar gnus-current-crosspost-group nil)
1331 (defvar gnus-newsgroup-display nil)
1332
1333 (defvar gnus-newsgroup-dependencies nil)
1334 (defvar gnus-newsgroup-adaptive nil)
1335 (defvar gnus-summary-display-article-function nil)
1336 (defvar gnus-summary-highlight-line-function nil
1337   "Function called after highlighting a summary line.")
1338
1339 (defvar gnus-summary-line-format-alist
1340   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1341     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1342     (?s gnus-tmp-subject-or-nil ?s)
1343     (?n gnus-tmp-name ?s)
1344     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1345         ?s)
1346     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1347             gnus-tmp-from) ?s)
1348     (?F gnus-tmp-from ?s)
1349     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1350     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1351     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1352     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1353     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1354     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1355     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1356     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1357     (?L gnus-tmp-lines ?s)
1358     (?O gnus-tmp-downloaded ?c)
1359     (?I gnus-tmp-indentation ?s)
1360     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1361     (?R gnus-tmp-replied ?c)
1362     (?\[ gnus-tmp-opening-bracket ?c)
1363     (?\] gnus-tmp-closing-bracket ?c)
1364     (?\> (make-string gnus-tmp-level ? ) ?s)
1365     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1366     (?i gnus-tmp-score ?d)
1367     (?z gnus-tmp-score-char ?c)
1368     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1369     (?U gnus-tmp-unread ?c)
1370     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1371         ?s)
1372     (?t (gnus-summary-number-of-articles-in-thread
1373          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1374         ?d)
1375     (?e (gnus-summary-number-of-articles-in-thread
1376          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1377         ?c)
1378     (?u gnus-tmp-user-defined ?s)
1379     (?P (gnus-pick-line-number) ?d)
1380     (?B gnus-tmp-thread-tree-header-string ?s)
1381     (user-date (gnus-user-date
1382                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1383   "An alist of format specifications that can appear in summary lines.
1384 These are paired with what variables they correspond with, along with
1385 the type of the variable (string, integer, character, etc).")
1386
1387 (defvar gnus-summary-dummy-line-format-alist
1388   `((?S gnus-tmp-subject ?s)
1389     (?N gnus-tmp-number ?d)
1390     (?u gnus-tmp-user-defined ?s)))
1391
1392 (defvar gnus-summary-mode-line-format-alist
1393   `((?G gnus-tmp-group-name ?s)
1394     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1395     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1396     (?A gnus-tmp-article-number ?d)
1397     (?Z gnus-tmp-unread-and-unselected ?s)
1398     (?V gnus-version ?s)
1399     (?U gnus-tmp-unread-and-unticked ?d)
1400     (?S gnus-tmp-subject ?s)
1401     (?e gnus-tmp-unselected ?d)
1402     (?u gnus-tmp-user-defined ?s)
1403     (?d (length gnus-newsgroup-dormant) ?d)
1404     (?t (length gnus-newsgroup-marked) ?d)
1405     (?h (length gnus-newsgroup-spam-marked) ?d)
1406     (?r (length gnus-newsgroup-reads) ?d)
1407     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1408     (?E gnus-newsgroup-expunged-tally ?d)
1409     (?s (gnus-current-score-file-nondirectory) ?s)))
1410
1411 ;; This is here rather than in gnus-art for compilation reasons.
1412 (defvar gnus-article-mode-line-format-alist
1413   (nconc '((?w (gnus-article-wash-status) ?s)
1414            (?m (gnus-article-mime-part-status) ?s))
1415          gnus-summary-mode-line-format-alist))
1416
1417 (defvar gnus-last-search-regexp nil
1418   "Default regexp for article search command.")
1419
1420 (defvar gnus-last-shell-command nil
1421   "Default shell command on article.")
1422
1423 (defvar gnus-newsgroup-agentized nil
1424   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1425 (defvar gnus-newsgroup-begin nil)
1426 (defvar gnus-newsgroup-end nil)
1427 (defvar gnus-newsgroup-last-rmail nil)
1428 (defvar gnus-newsgroup-last-mail nil)
1429 (defvar gnus-newsgroup-last-folder nil)
1430 (defvar gnus-newsgroup-last-file nil)
1431 (defvar gnus-newsgroup-last-directory nil)
1432 (defvar gnus-newsgroup-auto-expire nil)
1433 (defvar gnus-newsgroup-active nil)
1434 (defvar gnus-newsgroup-highest nil)
1435
1436 (defvar gnus-newsgroup-data nil)
1437 (defvar gnus-newsgroup-data-reverse nil)
1438 (defvar gnus-newsgroup-limit nil)
1439 (defvar gnus-newsgroup-limits nil)
1440 (defvar gnus-summary-use-undownloaded-faces nil)
1441
1442 (defvar gnus-newsgroup-unreads nil
1443   "Sorted list of unread articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-unselected nil
1446   "Sorted list of unselected unread articles in the current newsgroup.")
1447
1448 (defvar gnus-newsgroup-reads nil
1449   "Alist of read articles and article marks in the current newsgroup.")
1450
1451 (defvar gnus-newsgroup-expunged-tally nil)
1452
1453 (defvar gnus-newsgroup-marked nil
1454   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1455
1456 (defvar gnus-newsgroup-spam-marked nil
1457   "List of ranges of articles that have been marked as spam.")
1458
1459 (defvar gnus-newsgroup-killed nil
1460   "List of ranges of articles that have been through the scoring process.")
1461
1462 (defvar gnus-newsgroup-cached nil
1463   "Sorted list of articles that come from the article cache.")
1464
1465 (defvar gnus-newsgroup-saved nil
1466   "List of articles that have been saved.")
1467
1468 (defvar gnus-newsgroup-kill-headers nil)
1469
1470 (defvar gnus-newsgroup-replied nil
1471   "List of articles that have been replied to in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-forwarded nil
1474   "List of articles that have been forwarded in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-recent nil
1477   "List of articles that have are recent in the current newsgroup.")
1478
1479 (defvar gnus-newsgroup-expirable nil
1480   "Sorted list of articles in the current newsgroup that can be expired.")
1481
1482 (defvar gnus-newsgroup-processable nil
1483   "List of articles in the current newsgroup that can be processed.")
1484
1485 (defvar gnus-newsgroup-downloadable nil
1486   "Sorted list of articles in the current newsgroup that can be processed.")
1487
1488 (defvar gnus-newsgroup-unfetched nil
1489   "Sorted list of articles in the current newsgroup whose headers have
1490 not been fetched into the agent.
1491
1492 This list will always be a subset of gnus-newsgroup-undownloaded.")
1493
1494 (defvar gnus-newsgroup-undownloaded nil
1495   "List of articles in the current newsgroup that haven't been downloaded.")
1496
1497 (defvar gnus-newsgroup-unsendable nil
1498   "List of articles in the current newsgroup that won't be sent.")
1499
1500 (defvar gnus-newsgroup-bookmarks nil
1501   "List of articles in the current newsgroup that have bookmarks.")
1502
1503 (defvar gnus-newsgroup-dormant nil
1504   "Sorted list of dormant articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-unseen nil
1507   "List of unseen articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-seen nil
1510   "Range of seen articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-articles nil
1513   "List of articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-scored nil
1516   "List of scored articles in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-headers nil
1519   "List of article headers in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-threads nil)
1522
1523 (defvar gnus-newsgroup-prepared nil
1524   "Whether the current group has been prepared properly.")
1525
1526 (defvar gnus-newsgroup-ancient nil
1527   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-sparse nil)
1530
1531 (defvar gnus-current-article nil)
1532 (defvar gnus-article-current nil)
1533 (defvar gnus-current-headers nil)
1534 (defvar gnus-have-all-headers nil)
1535 (defvar gnus-last-article nil)
1536 (defvar gnus-newsgroup-history nil)
1537 (defvar gnus-newsgroup-charset nil)
1538 (defvar gnus-newsgroup-ephemeral-charset nil)
1539 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1540
1541 (defvar gnus-article-before-search nil)
1542
1543 (defvar gnus-summary-local-variables
1544   '(gnus-newsgroup-name
1545
1546     ;; Marks lists
1547     gnus-newsgroup-unreads
1548     gnus-newsgroup-unselected
1549     gnus-newsgroup-marked
1550     gnus-newsgroup-spam-marked
1551     gnus-newsgroup-reads
1552     gnus-newsgroup-saved
1553     gnus-newsgroup-replied
1554     gnus-newsgroup-forwarded
1555     gnus-newsgroup-recent
1556     gnus-newsgroup-expirable
1557     gnus-newsgroup-killed
1558     gnus-newsgroup-unseen
1559     gnus-newsgroup-seen
1560     gnus-newsgroup-cached
1561     gnus-newsgroup-downloadable
1562     gnus-newsgroup-undownloaded
1563     gnus-newsgroup-unsendable
1564
1565     gnus-newsgroup-begin gnus-newsgroup-end
1566     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1567     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1568     gnus-newsgroup-last-directory
1569     gnus-newsgroup-auto-expire
1570     gnus-newsgroup-processable
1571     gnus-newsgroup-unfetched
1572     gnus-newsgroup-articles
1573     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1574     gnus-newsgroup-headers gnus-newsgroup-threads
1575     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1576     gnus-current-article gnus-current-headers gnus-have-all-headers
1577     gnus-last-article gnus-article-internal-prepare-hook
1578     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1579     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1580     gnus-thread-expunge-below
1581     gnus-score-alist gnus-current-score-file
1582     (gnus-summary-expunge-below . global)
1583     (gnus-summary-mark-below . global)
1584     (gnus-orphan-score . global)
1585     gnus-newsgroup-active gnus-scores-exclude-files
1586     gnus-newsgroup-highest
1587     gnus-newsgroup-history gnus-newsgroup-ancient
1588     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1589     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1590     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1591     (gnus-newsgroup-expunged-tally . 0)
1592     gnus-cache-removable-articles
1593     gnus-newsgroup-data gnus-newsgroup-data-reverse
1594     gnus-newsgroup-limit gnus-newsgroup-limits
1595     gnus-newsgroup-charset gnus-newsgroup-display
1596     gnus-summary-use-undownloaded-faces)
1597   "Variables that are buffer-local to the summary buffers.")
1598
1599 (defvar gnus-newsgroup-variables nil
1600   "A list of variables that have separate values in different newsgroups.
1601 A list of newsgroup (summary buffer) local variables, or cons of
1602 variables and their default expressions to be evalled (when the default
1603 values are not nil), that should be made global while the summary buffer
1604 is active.
1605
1606 Note: The default expressions will be evaluated (using function `eval')
1607 before assignment to the local variable rather than just assigned to it.
1608 If the default expression is the symbol `global', that symbol will not
1609 be evaluated but the global value of the local variable will be used
1610 instead.
1611
1612 These variables can be used to set variables in the group parameters
1613 while still allowing them to affect operations done in other buffers.
1614 For example:
1615
1616 \(setq gnus-newsgroup-variables
1617      '(message-use-followup-to
1618        (gnus-visible-headers .
1619          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1620 ")
1621
1622 (eval-when-compile
1623   ;; Bind features so that require will believe that gnus-sum has
1624   ;; already been loaded (avoids infinite recursion)
1625   (let ((features (cons 'gnus-sum features)))
1626     (require 'gnus-art)))
1627
1628 ;; MIME stuff.
1629
1630 (defvar gnus-decode-encoded-word-methods
1631   '(mail-decode-encoded-word-string)
1632   "List of methods used to decode encoded words.
1633
1634 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1635 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1636 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1637 whose names match REGEXP.
1638
1639 For example:
1640 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1641  mail-decode-encoded-word-string
1642  (\"chinese\" . rfc1843-decode-string))")
1643
1644 (defvar gnus-decode-encoded-word-methods-cache nil)
1645
1646 (defun gnus-multi-decode-encoded-word-string (string)
1647   "Apply the functions from `gnus-encoded-word-methods' that match."
1648   (unless (and gnus-decode-encoded-word-methods-cache
1649                (eq gnus-newsgroup-name
1650                    (car gnus-decode-encoded-word-methods-cache)))
1651     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1652     (dolist (method gnus-decode-encoded-word-methods)
1653       (if (symbolp method)
1654           (nconc gnus-decode-encoded-word-methods-cache (list method))
1655         (if (and gnus-newsgroup-name
1656                  (string-match (car method) gnus-newsgroup-name))
1657             (nconc gnus-decode-encoded-word-methods-cache
1658                    (list (cdr method)))))))
1659   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1660     (setq string (funcall method string))))
1661
1662 ;; Subject simplification.
1663
1664 (defun gnus-simplify-whitespace (str)
1665   "Remove excessive whitespace from STR."
1666   ;; Multiple spaces.
1667   (while (string-match "[ \t][ \t]+" str)
1668     (setq str (concat (substring str 0 (match-beginning 0))
1669                         " "
1670                         (substring str (match-end 0)))))
1671   ;; Leading spaces.
1672   (when (string-match "^[ \t]+" str)
1673     (setq str (substring str (match-end 0))))
1674   ;; Trailing spaces.
1675   (when (string-match "[ \t]+$" str)
1676     (setq str (substring str 0 (match-beginning 0))))
1677   str)
1678
1679 (defun gnus-simplify-all-whitespace (str)
1680   "Remove all whitespace from STR."
1681   (while (string-match "[ \t\n]+" str)
1682     (setq str (replace-match "" nil nil str)))
1683   str)
1684
1685 (defsubst gnus-simplify-subject-re (subject)
1686   "Remove \"Re:\" from subject lines."
1687   (if (string-match message-subject-re-regexp subject)
1688       (substring subject (match-end 0))
1689     subject))
1690
1691 (defun gnus-simplify-subject (subject &optional re-only)
1692   "Remove `Re:' and words in parentheses.
1693 If RE-ONLY is non-nil, strip leading `Re:'s only."
1694   (let ((case-fold-search t))           ;Ignore case.
1695     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1696     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1697       (setq subject (substring subject (match-end 0))))
1698     ;; Remove uninteresting prefixes.
1699     (when (and (not re-only)
1700                gnus-simplify-ignored-prefixes
1701                (string-match gnus-simplify-ignored-prefixes subject))
1702       (setq subject (substring subject (match-end 0))))
1703     ;; Remove words in parentheses from end.
1704     (unless re-only
1705       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1706         (setq subject (substring subject 0 (match-beginning 0)))))
1707     ;; Return subject string.
1708     subject))
1709
1710 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1711 ;; all whitespace.
1712 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1713   (goto-char (point-min))
1714   (while (re-search-forward regexp nil t)
1715     (replace-match (or newtext ""))))
1716
1717 (defun gnus-simplify-buffer-fuzzy ()
1718   "Simplify string in the buffer fuzzily.
1719 The string in the accessible portion of the current buffer is simplified.
1720 It is assumed to be a single-line subject.
1721 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1722 matter is removed.  Additional things can be deleted by setting
1723 `gnus-simplify-subject-fuzzy-regexp'."
1724   (let ((case-fold-search t)
1725         (modified-tick))
1726     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1727
1728     (while (not (eq modified-tick (buffer-modified-tick)))
1729       (setq modified-tick (buffer-modified-tick))
1730       (cond
1731        ((listp gnus-simplify-subject-fuzzy-regexp)
1732         (mapc 'gnus-simplify-buffer-fuzzy-step
1733               gnus-simplify-subject-fuzzy-regexp))
1734        (gnus-simplify-subject-fuzzy-regexp
1735         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1736       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1737       (gnus-simplify-buffer-fuzzy-step
1738        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1739       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1740
1741     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1742     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1743     (gnus-simplify-buffer-fuzzy-step " $")
1744     (gnus-simplify-buffer-fuzzy-step "^ +")))
1745
1746 (defun gnus-simplify-subject-fuzzy (subject)
1747   "Simplify a subject string fuzzily.
1748 See `gnus-simplify-buffer-fuzzy' for details."
1749   (save-excursion
1750     (gnus-set-work-buffer)
1751     (let ((case-fold-search t))
1752       ;; Remove uninteresting prefixes.
1753       (when (and gnus-simplify-ignored-prefixes
1754                  (string-match gnus-simplify-ignored-prefixes subject))
1755         (setq subject (substring subject (match-end 0))))
1756       (insert subject)
1757       (inline (gnus-simplify-buffer-fuzzy))
1758       (buffer-string))))
1759
1760 (defsubst gnus-simplify-subject-fully (subject)
1761   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1762   (cond
1763    (gnus-simplify-subject-functions
1764     (gnus-map-function gnus-simplify-subject-functions subject))
1765    ((null gnus-summary-gather-subject-limit)
1766     (gnus-simplify-subject-re subject))
1767    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1768     (gnus-simplify-subject-fuzzy subject))
1769    ((numberp gnus-summary-gather-subject-limit)
1770     (truncate-string-to-width (gnus-simplify-subject-re subject)
1771                               gnus-summary-gather-subject-limit))
1772    (t
1773     subject)))
1774
1775 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1776   "Check whether two subjects are equal.
1777 If optional argument SIMPLE-FIRST is t, first argument is already
1778 simplified."
1779   (cond
1780    ((null simple-first)
1781     (equal (gnus-simplify-subject-fully s1)
1782            (gnus-simplify-subject-fully s2)))
1783    (t
1784     (equal s1
1785            (gnus-simplify-subject-fully s2)))))
1786
1787 (defun gnus-summary-bubble-group ()
1788   "Increase the score of the current group.
1789 This is a handy function to add to `gnus-summary-exit-hook' to
1790 increase the score of each group you read."
1791   (gnus-group-add-score gnus-newsgroup-name))
1792
1793 \f
1794 ;;;
1795 ;;; Gnus summary mode
1796 ;;;
1797
1798 (put 'gnus-summary-mode 'mode-class 'special)
1799
1800 (defvar gnus-article-commands-menu)
1801
1802 ;; Non-orthogonal keys
1803
1804 (gnus-define-keys gnus-summary-mode-map
1805   " " gnus-summary-next-page
1806   "\177" gnus-summary-prev-page
1807   [delete] gnus-summary-prev-page
1808   [backspace] gnus-summary-prev-page
1809   "\r" gnus-summary-scroll-up
1810   "\M-\r" gnus-summary-scroll-down
1811   "n" gnus-summary-next-unread-article
1812   "p" gnus-summary-prev-unread-article
1813   "N" gnus-summary-next-article
1814   "P" gnus-summary-prev-article
1815   "\M-\C-n" gnus-summary-next-same-subject
1816   "\M-\C-p" gnus-summary-prev-same-subject
1817   "\M-n" gnus-summary-next-unread-subject
1818   "\M-p" gnus-summary-prev-unread-subject
1819   "." gnus-summary-first-unread-article
1820   "," gnus-summary-best-unread-article
1821   "\M-s" gnus-summary-search-article-forward
1822   "\M-r" gnus-summary-search-article-backward
1823   "\M-S" gnus-summary-repeat-search-article-forward
1824   "\M-R" gnus-summary-repeat-search-article-backward
1825   "<" gnus-summary-beginning-of-article
1826   ">" gnus-summary-end-of-article
1827   "j" gnus-summary-goto-article
1828   "^" gnus-summary-refer-parent-article
1829   "\M-^" gnus-summary-refer-article
1830   "u" gnus-summary-tick-article-forward
1831   "!" gnus-summary-tick-article-forward
1832   "U" gnus-summary-tick-article-backward
1833   "d" gnus-summary-mark-as-read-forward
1834   "D" gnus-summary-mark-as-read-backward
1835   "E" gnus-summary-mark-as-expirable
1836   "\M-u" gnus-summary-clear-mark-forward
1837   "\M-U" gnus-summary-clear-mark-backward
1838   "k" gnus-summary-kill-same-subject-and-select
1839   "\C-k" gnus-summary-kill-same-subject
1840   "\M-\C-k" gnus-summary-kill-thread
1841   "\M-\C-l" gnus-summary-lower-thread
1842   "e" gnus-summary-edit-article
1843   "#" gnus-summary-mark-as-processable
1844   "\M-#" gnus-summary-unmark-as-processable
1845   "\M-\C-t" gnus-summary-toggle-threads
1846   "\M-\C-s" gnus-summary-show-thread
1847   "\M-\C-h" gnus-summary-hide-thread
1848   "\M-\C-f" gnus-summary-next-thread
1849   "\M-\C-b" gnus-summary-prev-thread
1850   [(meta down)] gnus-summary-next-thread
1851   [(meta up)] gnus-summary-prev-thread
1852   "\M-\C-u" gnus-summary-up-thread
1853   "\M-\C-d" gnus-summary-down-thread
1854   "&" gnus-summary-execute-command
1855   "c" gnus-summary-catchup-and-exit
1856   "\C-w" gnus-summary-mark-region-as-read
1857   "\C-t" gnus-summary-toggle-truncation
1858   "?" gnus-summary-mark-as-dormant
1859   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1860   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1861   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1862   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1863   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1864   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1865   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1866   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1867   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1868   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1869   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1870   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1871   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1872   "=" gnus-summary-expand-window
1873   "\C-x\C-s" gnus-summary-reselect-current-group
1874   "\M-g" gnus-summary-rescan-group
1875   "\C-c\C-r" gnus-summary-caesar-message
1876   "f" gnus-summary-followup
1877   "F" gnus-summary-followup-with-original
1878   "C" gnus-summary-cancel-article
1879   "r" gnus-summary-reply
1880   "R" gnus-summary-reply-with-original
1881   "\C-c\C-f" gnus-summary-mail-forward
1882   "o" gnus-summary-save-article
1883   "\C-o" gnus-summary-save-article-mail
1884   "|" gnus-summary-pipe-output
1885   "\M-k" gnus-summary-edit-local-kill
1886   "\M-K" gnus-summary-edit-global-kill
1887   ;; "V" gnus-version
1888   "\C-c\C-d" gnus-summary-describe-group
1889   "q" gnus-summary-exit
1890   "Q" gnus-summary-exit-no-update
1891   "\C-c\C-i" gnus-info-find-node
1892   gnus-mouse-2 gnus-mouse-pick-article
1893   [follow-link] mouse-face
1894   "m" gnus-summary-mail-other-window
1895   "a" gnus-summary-post-news
1896   "x" gnus-summary-limit-to-unread
1897   "s" gnus-summary-isearch-article
1898   "t" gnus-summary-toggle-header
1899   "g" gnus-summary-show-article
1900   "l" gnus-summary-goto-last-article
1901   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1902   "\C-d" gnus-summary-enter-digest-group
1903   "\M-\C-d" gnus-summary-read-document
1904   "\M-\C-e" gnus-summary-edit-parameters
1905   "\M-\C-a" gnus-summary-customize-parameters
1906   "\C-c\C-b" gnus-bug
1907   "*" gnus-cache-enter-article
1908   "\M-*" gnus-cache-remove-article
1909   "\M-&" gnus-summary-universal-argument
1910   "\C-l" gnus-recenter
1911   "I" gnus-summary-increase-score
1912   "L" gnus-summary-lower-score
1913   "\M-i" gnus-symbolic-argument
1914   "h" gnus-summary-select-article-buffer
1915
1916   "b" gnus-article-view-part
1917   "\M-t" gnus-summary-toggle-display-buttonized
1918
1919   "V" gnus-summary-score-map
1920   "X" gnus-uu-extract-map
1921   "S" gnus-summary-send-map)
1922
1923 ;; Sort of orthogonal keymap
1924 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1925   "t" gnus-summary-tick-article-forward
1926   "!" gnus-summary-tick-article-forward
1927   "d" gnus-summary-mark-as-read-forward
1928   "r" gnus-summary-mark-as-read-forward
1929   "c" gnus-summary-clear-mark-forward
1930   " " gnus-summary-clear-mark-forward
1931   "e" gnus-summary-mark-as-expirable
1932   "x" gnus-summary-mark-as-expirable
1933   "?" gnus-summary-mark-as-dormant
1934   "b" gnus-summary-set-bookmark
1935   "B" gnus-summary-remove-bookmark
1936   "#" gnus-summary-mark-as-processable
1937   "\M-#" gnus-summary-unmark-as-processable
1938   "S" gnus-summary-limit-include-expunged
1939   "C" gnus-summary-catchup
1940   "H" gnus-summary-catchup-to-here
1941   "h" gnus-summary-catchup-from-here
1942   "\C-c" gnus-summary-catchup-all
1943   "k" gnus-summary-kill-same-subject-and-select
1944   "K" gnus-summary-kill-same-subject
1945   "P" gnus-uu-mark-map)
1946
1947 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1948   "c" gnus-summary-clear-above
1949   "u" gnus-summary-tick-above
1950   "m" gnus-summary-mark-above
1951   "k" gnus-summary-kill-below)
1952
1953 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1954   "/" gnus-summary-limit-to-subject
1955   "n" gnus-summary-limit-to-articles
1956   "b" gnus-summary-limit-to-bodies
1957   "h" gnus-summary-limit-to-headers
1958   "w" gnus-summary-pop-limit
1959   "s" gnus-summary-limit-to-subject
1960   "a" gnus-summary-limit-to-author
1961   "u" gnus-summary-limit-to-unread
1962   "m" gnus-summary-limit-to-marks
1963   "M" gnus-summary-limit-exclude-marks
1964   "v" gnus-summary-limit-to-score
1965   "*" gnus-summary-limit-include-cached
1966   "D" gnus-summary-limit-include-dormant
1967   "T" gnus-summary-limit-include-thread
1968   "d" gnus-summary-limit-exclude-dormant
1969   "t" gnus-summary-limit-to-age
1970   "." gnus-summary-limit-to-unseen
1971   "x" gnus-summary-limit-to-extra
1972   "p" gnus-summary-limit-to-display-predicate
1973   "E" gnus-summary-limit-include-expunged
1974   "c" gnus-summary-limit-exclude-childless-dormant
1975   "C" gnus-summary-limit-mark-excluded-as-read
1976   "o" gnus-summary-insert-old-articles
1977   "N" gnus-summary-insert-new-articles
1978   "S" gnus-summary-limit-to-singletons
1979   "r" gnus-summary-limit-to-replied
1980   "R" gnus-summary-limit-to-recipient
1981   "A" gnus-summary-limit-to-address)
1982
1983 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1984   "n" gnus-summary-next-unread-article
1985   "p" gnus-summary-prev-unread-article
1986   "N" gnus-summary-next-article
1987   "P" gnus-summary-prev-article
1988   "\C-n" gnus-summary-next-same-subject
1989   "\C-p" gnus-summary-prev-same-subject
1990   "\M-n" gnus-summary-next-unread-subject
1991   "\M-p" gnus-summary-prev-unread-subject
1992   "f" gnus-summary-first-unread-article
1993   "b" gnus-summary-best-unread-article
1994   "j" gnus-summary-goto-article
1995   "g" gnus-summary-goto-subject
1996   "l" gnus-summary-goto-last-article
1997   "o" gnus-summary-pop-article)
1998
1999 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2000   "k" gnus-summary-kill-thread
2001   "E" gnus-summary-expire-thread
2002   "l" gnus-summary-lower-thread
2003   "i" gnus-summary-raise-thread
2004   "T" gnus-summary-toggle-threads
2005   "t" gnus-summary-rethread-current
2006   "^" gnus-summary-reparent-thread
2007   "\M-^" gnus-summary-reparent-children
2008   "s" gnus-summary-show-thread
2009   "S" gnus-summary-show-all-threads
2010   "h" gnus-summary-hide-thread
2011   "H" gnus-summary-hide-all-threads
2012   "n" gnus-summary-next-thread
2013   "p" gnus-summary-prev-thread
2014   "u" gnus-summary-up-thread
2015   "o" gnus-summary-top-thread
2016   "d" gnus-summary-down-thread
2017   "#" gnus-uu-mark-thread
2018   "\M-#" gnus-uu-unmark-thread)
2019
2020 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2021   "g" gnus-summary-prepare
2022   "c" gnus-summary-insert-cached-articles
2023   "d" gnus-summary-insert-dormant-articles
2024   "t" gnus-summary-insert-ticked-articles)
2025
2026 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2027   "c" gnus-summary-catchup-and-exit
2028   "C" gnus-summary-catchup-all-and-exit
2029   "E" gnus-summary-exit-no-update
2030   "Q" gnus-summary-exit
2031   "Z" gnus-summary-exit
2032   "n" gnus-summary-catchup-and-goto-next-group
2033   "p" gnus-summary-catchup-and-goto-prev-group
2034   "R" gnus-summary-reselect-current-group
2035   "G" gnus-summary-rescan-group
2036   "N" gnus-summary-next-group
2037   "s" gnus-summary-save-newsrc
2038   "P" gnus-summary-prev-group)
2039
2040 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2041   " " gnus-summary-next-page
2042   "n" gnus-summary-next-page
2043   "\177" gnus-summary-prev-page
2044   [delete] gnus-summary-prev-page
2045   "p" gnus-summary-prev-page
2046   "\r" gnus-summary-scroll-up
2047   "\M-\r" gnus-summary-scroll-down
2048   "<" gnus-summary-beginning-of-article
2049   ">" gnus-summary-end-of-article
2050   "b" gnus-summary-beginning-of-article
2051   "e" gnus-summary-end-of-article
2052   "^" gnus-summary-refer-parent-article
2053   "r" gnus-summary-refer-parent-article
2054   "C" gnus-summary-show-complete-article
2055   "D" gnus-summary-enter-digest-group
2056   "R" gnus-summary-refer-references
2057   "T" gnus-summary-refer-thread
2058   "g" gnus-summary-show-article
2059   "s" gnus-summary-isearch-article
2060   "P" gnus-summary-print-article
2061   "S" gnus-sticky-article
2062   "M" gnus-mailing-list-insinuate
2063   "t" gnus-article-babel)
2064
2065 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2066   "b" gnus-article-add-buttons
2067   "B" gnus-article-add-buttons-to-head
2068   "o" gnus-article-treat-overstrike
2069   "e" gnus-article-emphasize
2070   "w" gnus-article-fill-cited-article
2071   "Q" gnus-article-fill-long-lines
2072   "L" gnus-article-toggle-truncate-lines
2073   "C" gnus-article-capitalize-sentences
2074   "c" gnus-article-remove-cr
2075   "q" gnus-article-de-quoted-unreadable
2076   "6" gnus-article-de-base64-unreadable
2077   "Z" gnus-article-decode-HZ
2078   "A" gnus-article-treat-ansi-sequences
2079   "h" gnus-article-wash-html
2080   "u" gnus-article-unsplit-urls
2081   "s" gnus-summary-force-verify-and-decrypt
2082   "f" gnus-article-display-x-face
2083   "l" gnus-summary-stop-page-breaking
2084   "r" gnus-summary-caesar-message
2085   "m" gnus-summary-morse-message
2086   "t" gnus-summary-toggle-header
2087   "g" gnus-treat-smiley
2088   "v" gnus-summary-verbose-headers
2089   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2090   "p" gnus-article-verify-x-pgp-sig
2091   "d" gnus-article-treat-dumbquotes
2092   "i" gnus-summary-idna-message)
2093
2094 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2095   ;; mnemonic: deuglif*Y*
2096   "u" gnus-article-outlook-unwrap-lines
2097   "a" gnus-article-outlook-repair-attribution
2098   "c" gnus-article-outlook-rearrange-citation
2099   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2100
2101 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2102   "a" gnus-article-hide
2103   "h" gnus-article-hide-headers
2104   "b" gnus-article-hide-boring-headers
2105   "s" gnus-article-hide-signature
2106   "c" gnus-article-hide-citation
2107   "C" gnus-article-hide-citation-in-followups
2108   "l" gnus-article-hide-list-identifiers
2109   "B" gnus-article-strip-banner
2110   "P" gnus-article-hide-pem
2111   "\C-c" gnus-article-hide-citation-maybe)
2112
2113 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2114   "a" gnus-article-highlight
2115   "h" gnus-article-highlight-headers
2116   "c" gnus-article-highlight-citation
2117   "s" gnus-article-highlight-signature)
2118
2119 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2120   "f" gnus-article-treat-fold-headers
2121   "u" gnus-article-treat-unfold-headers
2122   "n" gnus-article-treat-fold-newsgroups)
2123
2124 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2125   "x" gnus-article-display-x-face
2126   "d" gnus-article-display-face
2127   "s" gnus-treat-smiley
2128   "D" gnus-article-remove-images
2129   "W" gnus-html-show-images
2130   "f" gnus-treat-from-picon
2131   "m" gnus-treat-mail-picon
2132   "n" gnus-treat-newsgroups-picon
2133   "g" gnus-treat-from-gravatar
2134   "h" gnus-treat-mail-gravatar)
2135
2136 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2137   "w" gnus-article-decode-mime-words
2138   "c" gnus-article-decode-charset
2139   "v" gnus-mime-view-all-parts
2140   "b" gnus-article-view-part)
2141
2142 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2143   "z" gnus-article-date-ut
2144   "u" gnus-article-date-ut
2145   "l" gnus-article-date-local
2146   "p" gnus-article-date-english
2147   "e" gnus-article-date-lapsed
2148   "o" gnus-article-date-original
2149   "i" gnus-article-date-iso8601
2150   "s" gnus-article-date-user)
2151
2152 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2153   "t" gnus-article-remove-trailing-blank-lines
2154   "l" gnus-article-strip-leading-blank-lines
2155   "m" gnus-article-strip-multiple-blank-lines
2156   "a" gnus-article-strip-blank-lines
2157   "A" gnus-article-strip-all-blank-lines
2158   "s" gnus-article-strip-leading-space
2159   "e" gnus-article-strip-trailing-space
2160   "w" gnus-article-remove-leading-whitespace)
2161
2162 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2163   "v" gnus-version
2164   "d" gnus-summary-describe-group
2165   "h" gnus-summary-describe-briefly
2166   "i" gnus-info-find-node
2167   "C" gnus-group-fetch-control)
2168
2169 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2170   "e" gnus-summary-expire-articles
2171   "\M-\C-e" gnus-summary-expire-articles-now
2172   "\177" gnus-summary-delete-article
2173   [delete] gnus-summary-delete-article
2174   [backspace] gnus-summary-delete-article
2175   "m" gnus-summary-move-article
2176   "r" gnus-summary-respool-article
2177   "w" gnus-summary-edit-article
2178   "c" gnus-summary-copy-article
2179   "B" gnus-summary-crosspost-article
2180   "q" gnus-summary-respool-query
2181   "t" gnus-summary-respool-trace
2182   "i" gnus-summary-import-article
2183   "I" gnus-summary-create-article
2184   "p" gnus-summary-article-posted-p)
2185
2186 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2187   "o" gnus-summary-save-article
2188   "m" gnus-summary-save-article-mail
2189   "F" gnus-summary-write-article-file
2190   "r" gnus-summary-save-article-rmail
2191   "f" gnus-summary-save-article-file
2192   "b" gnus-summary-save-article-body-file
2193   "B" gnus-summary-write-article-body-file
2194   "h" gnus-summary-save-article-folder
2195   "v" gnus-summary-save-article-vm
2196   "p" gnus-summary-pipe-output
2197   "P" gnus-summary-muttprint)
2198
2199 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2200   "b" gnus-summary-display-buttonized
2201   "m" gnus-summary-repair-multipart
2202   "v" gnus-article-view-part
2203   "o" gnus-article-save-part
2204   "O" gnus-article-save-part-and-strip
2205   "r" gnus-article-replace-part
2206   "d" gnus-article-delete-part
2207   "t" gnus-article-view-part-as-type
2208   "j" gnus-article-jump-to-part
2209   "c" gnus-article-copy-part
2210   "C" gnus-article-view-part-as-charset
2211   "e" gnus-article-view-part-externally
2212   "H" gnus-article-browse-html-article
2213   "E" gnus-article-encrypt-body
2214   "i" gnus-article-inline-part
2215   "|" gnus-article-pipe-part)
2216
2217 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2218   "p" gnus-summary-mark-as-processable
2219   "u" gnus-summary-unmark-as-processable
2220   "U" gnus-summary-unmark-all-processable
2221   "v" gnus-uu-mark-over
2222   "s" gnus-uu-mark-series
2223   "r" gnus-uu-mark-region
2224   "g" gnus-uu-unmark-region
2225   "R" gnus-uu-mark-by-regexp
2226   "G" gnus-uu-unmark-by-regexp
2227   "t" gnus-uu-mark-thread
2228   "T" gnus-uu-unmark-thread
2229   "a" gnus-uu-mark-all
2230   "b" gnus-uu-mark-buffer
2231   "S" gnus-uu-mark-sparse
2232   "k" gnus-summary-kill-process-mark
2233   "y" gnus-summary-yank-process-mark
2234   "w" gnus-summary-save-process-mark
2235   "i" gnus-uu-invert-processable)
2236
2237 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2238   ;;"x" gnus-uu-extract-any
2239   "m" gnus-summary-save-parts
2240   "u" gnus-uu-decode-uu
2241   "U" gnus-uu-decode-uu-and-save
2242   "s" gnus-uu-decode-unshar
2243   "S" gnus-uu-decode-unshar-and-save
2244   "o" gnus-uu-decode-save
2245   "O" gnus-uu-decode-save
2246   "b" gnus-uu-decode-binhex
2247   "B" gnus-uu-decode-binhex
2248   "Y" gnus-uu-decode-yenc
2249   "p" gnus-uu-decode-postscript
2250   "P" gnus-uu-decode-postscript-and-save)
2251
2252 (gnus-define-keys
2253     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2254   "u" gnus-uu-decode-uu-view
2255   "U" gnus-uu-decode-uu-and-save-view
2256   "s" gnus-uu-decode-unshar-view
2257   "S" gnus-uu-decode-unshar-and-save-view
2258   "o" gnus-uu-decode-save-view
2259   "O" gnus-uu-decode-save-view
2260   "b" gnus-uu-decode-binhex-view
2261   "B" gnus-uu-decode-binhex-view
2262   "p" gnus-uu-decode-postscript-view
2263   "P" gnus-uu-decode-postscript-and-save-view)
2264
2265 (defvar gnus-article-post-menu nil)
2266
2267 (defconst gnus-summary-menu-maxlen 20)
2268
2269 (defun gnus-summary-menu-split (menu)
2270   ;; If we have lots of elements, divide them into groups of 20
2271   ;; and make a pane (or submenu) for each one.
2272   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2273       (let ((menu menu) sublists next
2274             (i 1))
2275         (while menu
2276           ;; Pull off the next gnus-summary-menu-maxlen elements
2277           ;; and make them the next element of sublist.
2278           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2279           (if next
2280               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2281                       nil))
2282           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2283                                              (aref (car (last menu)) 0)) menu)
2284                                sublists))
2285           (setq i (1+ i))
2286           (setq menu next))
2287         (nreverse sublists))
2288     ;; Few elements--put them all in one pane.
2289     menu))
2290
2291 (defun gnus-summary-make-menu-bar ()
2292   (gnus-turn-off-edit-menu 'summary)
2293
2294   (unless (boundp 'gnus-summary-misc-menu)
2295
2296     (easy-menu-define
2297       gnus-summary-kill-menu gnus-summary-mode-map ""
2298       (cons
2299        "Score"
2300        (nconc
2301         (list
2302          ["Customize" gnus-score-customize t])
2303         (gnus-make-score-map 'increase)
2304         (gnus-make-score-map 'lower)
2305         '(("Mark"
2306            ["Kill below" gnus-summary-kill-below t]
2307            ["Mark above" gnus-summary-mark-above t]
2308            ["Tick above" gnus-summary-tick-above t]
2309            ["Clear above" gnus-summary-clear-above t])
2310           ["Current score" gnus-summary-current-score t]
2311           ["Set score" gnus-summary-set-score t]
2312           ["Switch current score file..." gnus-score-change-score-file t]
2313           ["Set mark below..." gnus-score-set-mark-below t]
2314           ["Set expunge below..." gnus-score-set-expunge-below t]
2315           ["Edit current score file" gnus-score-edit-current-scores t]
2316           ["Edit score file..." gnus-score-edit-file t]
2317           ["Trace score" gnus-score-find-trace t]
2318           ["Find words" gnus-score-find-favourite-words t]
2319           ["Rescore buffer" gnus-summary-rescore t]
2320           ["Increase score..." gnus-summary-increase-score t]
2321           ["Lower score..." gnus-summary-lower-score t]))))
2322
2323     ;; Define both the Article menu in the summary buffer and the
2324     ;; equivalent Commands menu in the article buffer here for
2325     ;; consistency.
2326     (let ((innards
2327            `(("Hide"
2328               ["All" gnus-article-hide t]
2329               ["Headers" gnus-article-hide-headers t]
2330               ["Signature" gnus-article-hide-signature t]
2331               ["Citation" gnus-article-hide-citation t]
2332               ["List identifiers" gnus-article-hide-list-identifiers t]
2333               ["Banner" gnus-article-strip-banner t]
2334               ["Boring headers" gnus-article-hide-boring-headers t])
2335              ("Highlight"
2336               ["All" gnus-article-highlight t]
2337               ["Headers" gnus-article-highlight-headers t]
2338               ["Signature" gnus-article-highlight-signature t]
2339               ["Citation" gnus-article-highlight-citation t])
2340              ("MIME"
2341               ["Words" gnus-article-decode-mime-words t]
2342               ["Charset" gnus-article-decode-charset t]
2343               ["QP" gnus-article-de-quoted-unreadable t]
2344               ["Base64" gnus-article-de-base64-unreadable t]
2345               ["View MIME buttons" gnus-summary-display-buttonized t]
2346               ["View all" gnus-mime-view-all-parts t]
2347               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2348               ["Encrypt body" gnus-article-encrypt-body
2349                :active (not (gnus-group-read-only-p))
2350                ,@(if (featurep 'xemacs) nil
2351                    '(:help "Encrypt the message body on disk"))]
2352               ["Extract all parts..." gnus-summary-save-parts t]
2353               ("Multipart"
2354                ["Repair multipart" gnus-summary-repair-multipart t]
2355                ["Pipe part..." gnus-article-pipe-part t]
2356                ["Inline part" gnus-article-inline-part t]
2357                ["View part as type..." gnus-article-view-part-as-type t]
2358                ["Encrypt body" gnus-article-encrypt-body
2359                 :active (not (gnus-group-read-only-p))
2360                ,@(if (featurep 'xemacs) nil
2361                    '(:help "Encrypt the message body on disk"))]
2362                ["View part externally" gnus-article-view-part-externally t]
2363                ["View HTML parts in browser" gnus-article-browse-html-article t]
2364                ["View part with charset..." gnus-article-view-part-as-charset t]
2365                ["Copy part" gnus-article-copy-part t]
2366                ["Save part..." gnus-article-save-part t]
2367                ["View part" gnus-article-view-part t]))
2368              ("Date"
2369               ["Local" gnus-article-date-local t]
2370               ["ISO8601" gnus-article-date-iso8601 t]
2371               ["UT" gnus-article-date-ut t]
2372               ["Original" gnus-article-date-original t]
2373               ["Lapsed" gnus-article-date-lapsed t]
2374               ["User-defined" gnus-article-date-user t])
2375              ("Display"
2376               ["Remove images" gnus-article-remove-images t]
2377               ["Toggle smiley" gnus-treat-smiley t]
2378               ["Show X-Face" gnus-article-display-x-face t]
2379               ["Show picons in From" gnus-treat-from-picon t]
2380               ["Show picons in mail headers" gnus-treat-mail-picon t]
2381               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2382               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2383               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2384               ("View as different encoding"
2385                ,@(gnus-summary-menu-split
2386                   (mapcar
2387                    (lambda (cs)
2388                      ;; Since easymenu under Emacs doesn't allow
2389                      ;; lambda forms for menu commands, we should
2390                      ;; provide intern'ed function symbols.
2391                      (let ((command (intern (format "\
2392 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2393                        (fset command
2394                              `(lambda ()
2395                                 (interactive)
2396                                 (let ((gnus-summary-show-article-charset-alist
2397                                        '((1 . ,cs))))
2398                                   (gnus-summary-show-article 1))))
2399                        `[,(symbol-name cs) ,command t]))
2400                    (sort (if (fboundp 'coding-system-list)
2401                              (coding-system-list)
2402                            (mapcar 'car mm-mime-mule-charset-alist))
2403                          'string<)))))
2404              ("Washing"
2405               ("Remove Blanks"
2406                ["Leading" gnus-article-strip-leading-blank-lines t]
2407                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2408                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2409                ["All of the above" gnus-article-strip-blank-lines t]
2410                ["All" gnus-article-strip-all-blank-lines t]
2411                ["Leading space" gnus-article-strip-leading-space t]
2412                ["Trailing space" gnus-article-strip-trailing-space t]
2413                ["Leading space in headers"
2414                 gnus-article-remove-leading-whitespace t])
2415               ["Overstrike" gnus-article-treat-overstrike t]
2416               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2417               ["Emphasis" gnus-article-emphasize t]
2418               ["Word wrap" gnus-article-fill-cited-article t]
2419               ["Fill long lines" gnus-article-fill-long-lines t]
2420               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2421               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2422               ["Remove CR" gnus-article-remove-cr t]
2423               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2424               ["Base64" gnus-article-de-base64-unreadable t]
2425               ["Rot 13" gnus-summary-caesar-message
2426                ,@(if (featurep 'xemacs) '(t)
2427                    '(:help "\"Caesar rotate\" article by 13"))]
2428               ["De-IDNA" gnus-summary-idna-message t]
2429               ["Morse decode" gnus-summary-morse-message t]
2430               ["Unix pipe..." gnus-summary-pipe-message t]
2431               ["Add buttons" gnus-article-add-buttons t]
2432               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2433               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2434               ["Verbose header" gnus-summary-verbose-headers t]
2435               ["Toggle header" gnus-summary-toggle-header t]
2436               ["Unfold headers" gnus-article-treat-unfold-headers t]
2437               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2438               ["Html" gnus-article-wash-html t]
2439               ["Unsplit URLs" gnus-article-unsplit-urls t]
2440               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2441               ["Decode HZ" gnus-article-decode-HZ t]
2442               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2443               ("(Outlook) Deuglify"
2444                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2445                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2446                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2447                ["Full (Outlook) deuglify"
2448                 gnus-article-outlook-deuglify-article t])
2449               )
2450              ("Output"
2451               ["Save in default format..." gnus-summary-save-article
2452                ,@(if (featurep 'xemacs) '(t)
2453                    '(:help "Save article using default method"))]
2454               ["Save in file..." gnus-summary-save-article-file
2455                ,@(if (featurep 'xemacs) '(t)
2456                    '(:help "Save article in file"))]
2457               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2458               ["Save in MH folder..." gnus-summary-save-article-folder t]
2459               ["Save in VM folder..." gnus-summary-save-article-vm t]
2460               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2461               ["Save body in file..." gnus-summary-save-article-body-file t]
2462               ["Pipe through a filter..." gnus-summary-pipe-output t]
2463               ["Print with Muttprint..." gnus-summary-muttprint t]
2464               ["Print" gnus-summary-print-article
2465                ,@(if (featurep 'xemacs) '(t)
2466                    '(:help "Generate and print a PostScript image"))])
2467              ("Copy, move,... (Backend)"
2468               ,@(if (featurep 'xemacs) nil
2469                   '(:help "Copying, moving, expiring articles..."))
2470               ["Respool article..." gnus-summary-respool-article t]
2471               ["Move article..." gnus-summary-move-article
2472                (gnus-check-backend-function
2473                 'request-move-article gnus-newsgroup-name)]
2474               ["Copy article..." gnus-summary-copy-article t]
2475               ["Crosspost article..." gnus-summary-crosspost-article
2476                (gnus-check-backend-function
2477                 'request-replace-article gnus-newsgroup-name)]
2478               ["Import file..." gnus-summary-import-article
2479                (gnus-check-backend-function
2480                 'request-accept-article gnus-newsgroup-name)]
2481               ["Create article..." gnus-summary-create-article
2482                (gnus-check-backend-function
2483                 'request-accept-article gnus-newsgroup-name)]
2484               ["Check if posted" gnus-summary-article-posted-p t]
2485               ["Edit article" gnus-summary-edit-article
2486                (not (gnus-group-read-only-p))]
2487               ["Delete article" gnus-summary-delete-article
2488                (gnus-check-backend-function
2489                 'request-expire-articles gnus-newsgroup-name)]
2490               ["Query respool" gnus-summary-respool-query t]
2491               ["Trace respool" gnus-summary-respool-trace t]
2492               ["Delete expirable articles" gnus-summary-expire-articles-now
2493                (gnus-check-backend-function
2494                 'request-expire-articles gnus-newsgroup-name)])
2495              ("Extract"
2496               ["Uudecode" gnus-uu-decode-uu
2497                ,@(if (featurep 'xemacs) '(t)
2498                    '(:help "Decode uuencoded article(s)"))]
2499               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2500               ["Unshar" gnus-uu-decode-unshar t]
2501               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2502               ["Save" gnus-uu-decode-save t]
2503               ["Binhex" gnus-uu-decode-binhex t]
2504               ["Postscript" gnus-uu-decode-postscript t]
2505               ["All MIME parts" gnus-summary-save-parts t])
2506              ("Cache"
2507               ["Enter article" gnus-cache-enter-article t]
2508               ["Remove article" gnus-cache-remove-article t])
2509              ["Translate" gnus-article-babel t]
2510              ["Select article buffer" gnus-summary-select-article-buffer t]
2511              ["Make article buffer sticky" gnus-sticky-article t]
2512              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2513              ["Isearch article..." gnus-summary-isearch-article t]
2514              ["Beginning of the article" gnus-summary-beginning-of-article t]
2515              ["End of the article" gnus-summary-end-of-article t]
2516              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2517              ["Fetch referenced articles" gnus-summary-refer-references t]
2518              ["Fetch current thread" gnus-summary-refer-thread t]
2519              ["Fetch article with id..." gnus-summary-refer-article t]
2520              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2521              ["Redisplay" gnus-summary-show-article t]
2522              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2523       (easy-menu-define
2524         gnus-summary-article-menu gnus-summary-mode-map ""
2525         (cons "Article" innards))
2526
2527       (if (not (keymapp gnus-summary-article-menu))
2528           (easy-menu-define
2529             gnus-article-commands-menu gnus-article-mode-map ""
2530             (cons "Commands" innards))
2531         ;; in Emacs, don't share menu.
2532         (setq gnus-article-commands-menu
2533               (copy-keymap gnus-summary-article-menu))
2534         (define-key gnus-article-mode-map [menu-bar commands]
2535           (cons "Commands" gnus-article-commands-menu))))
2536
2537     (easy-menu-define
2538       gnus-summary-thread-menu gnus-summary-mode-map ""
2539       '("Threads"
2540         ["Find all messages in thread" gnus-summary-refer-thread t]
2541         ["Toggle threading" gnus-summary-toggle-threads t]
2542         ["Hide threads" gnus-summary-hide-all-threads t]
2543         ["Show threads" gnus-summary-show-all-threads t]
2544         ["Hide thread" gnus-summary-hide-thread t]
2545         ["Show thread" gnus-summary-show-thread t]
2546         ["Go to next thread" gnus-summary-next-thread t]
2547         ["Go to previous thread" gnus-summary-prev-thread t]
2548         ["Go down thread" gnus-summary-down-thread t]
2549         ["Go up thread" gnus-summary-up-thread t]
2550         ["Top of thread" gnus-summary-top-thread t]
2551         ["Mark thread as read" gnus-summary-kill-thread t]
2552         ["Mark thread as expired" gnus-summary-expire-thread t]
2553         ["Lower thread score" gnus-summary-lower-thread t]
2554         ["Raise thread score" gnus-summary-raise-thread t]
2555         ["Rethread current" gnus-summary-rethread-current t]))
2556
2557     (easy-menu-define
2558       gnus-summary-post-menu gnus-summary-mode-map ""
2559       `("Post"
2560         ["Send a message (mail or news)" gnus-summary-post-news
2561          ,@(if (featurep 'xemacs) '(t)
2562              '(:help "Compose a new message (mail or news)"))]
2563         ["Followup" gnus-summary-followup
2564          ,@(if (featurep 'xemacs) '(t)
2565              '(:help "Post followup to this article"))]
2566         ["Followup and yank" gnus-summary-followup-with-original
2567          ,@(if (featurep 'xemacs) '(t)
2568              '(:help "Post followup to this article, quoting its contents"))]
2569         ["Supersede article" gnus-summary-supersede-article t]
2570         ["Cancel article" gnus-summary-cancel-article
2571          ,@(if (featurep 'xemacs) '(t)
2572              '(:help "Cancel an article you posted"))]
2573         ["Reply" gnus-summary-reply t]
2574         ["Reply and yank" gnus-summary-reply-with-original t]
2575         ["Wide reply" gnus-summary-wide-reply t]
2576         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2577          ,@(if (featurep 'xemacs) '(t)
2578              '(:help "Mail a reply, quoting this article"))]
2579         ["Very wide reply" gnus-summary-very-wide-reply t]
2580         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2581          ,@(if (featurep 'xemacs) '(t)
2582              '(:help "Mail a very wide reply, quoting this article"))]
2583         ["Mail forward" gnus-summary-mail-forward t]
2584         ["Post forward" gnus-summary-post-forward t]
2585         ["Digest and mail" gnus-uu-digest-mail-forward t]
2586         ["Digest and post" gnus-uu-digest-post-forward t]
2587         ["Resend message" gnus-summary-resend-message t]
2588         ["Resend message edit" gnus-summary-resend-message-edit t]
2589         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2590         ["Send a mail" gnus-summary-mail-other-window t]
2591         ["Create a local message" gnus-summary-news-other-window t]
2592         ["Uuencode and post" gnus-uu-post-news
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Post a uuencoded article"))]
2595         ["Followup via news" gnus-summary-followup-to-mail t]
2596         ["Followup via news and yank"
2597          gnus-summary-followup-to-mail-with-original t]
2598         ["Strip signature on reply"
2599          (lambda ()
2600            (interactive)
2601            (if (not (memq message-cite-function
2602                           '(message-cite-original-without-signature
2603                             message-cite-original)))
2604                ;; Stupid workaround for XEmacs not honoring :visible.
2605                (message "Can't toggle this value of `message-cite-function'")
2606              (setq message-cite-function
2607                    (if (eq message-cite-function
2608                            'message-cite-original-without-signature)
2609                        'message-cite-original
2610                      'message-cite-original-without-signature))))
2611          ;; XEmacs barfs on :visible.
2612          ,@(if (featurep 'xemacs) nil
2613              '(:visible (memq message-cite-function
2614                               '(message-cite-original-without-signature
2615                                 message-cite-original))))
2616          :style toggle
2617          :selected (eq message-cite-function
2618                        'message-cite-original-without-signature)
2619          ,@(if (featurep 'xemacs) nil
2620              '(:help "Strip signature from cited article when replying."))]
2621         ;;("Draft"
2622         ;;["Send" gnus-summary-send-draft t]
2623         ;;["Send bounced" gnus-resend-bounced-mail t])
2624         ))
2625
2626     (cond
2627      ((not (keymapp gnus-summary-post-menu))
2628       (setq gnus-article-post-menu gnus-summary-post-menu))
2629      ((not gnus-article-post-menu)
2630       ;; Don't share post menu.
2631       (setq gnus-article-post-menu
2632             (copy-keymap gnus-summary-post-menu))))
2633     (define-key gnus-article-mode-map [menu-bar post]
2634       (cons "Post" gnus-article-post-menu))
2635
2636     (easy-menu-define
2637       gnus-summary-misc-menu gnus-summary-mode-map ""
2638       `("Gnus"
2639         ("Mark Read"
2640          ["Mark as read" gnus-summary-mark-as-read-forward t]
2641          ["Mark same subject and select"
2642           gnus-summary-kill-same-subject-and-select t]
2643          ["Mark same subject" gnus-summary-kill-same-subject t]
2644          ["Catchup" gnus-summary-catchup
2645           ,@(if (featurep 'xemacs) '(t)
2646               '(:help "Mark unread articles in this group as read"))]
2647          ["Catchup all" gnus-summary-catchup-all t]
2648          ["Catchup to here" gnus-summary-catchup-to-here t]
2649          ["Catchup from here" gnus-summary-catchup-from-here t]
2650          ["Catchup region" gnus-summary-mark-region-as-read
2651           (gnus-mark-active-p)]
2652          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2653         ("Mark Various"
2654          ["Tick" gnus-summary-tick-article-forward t]
2655          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2656          ["Remove marks" gnus-summary-clear-mark-forward t]
2657          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2658          ["Set bookmark" gnus-summary-set-bookmark t]
2659          ["Remove bookmark" gnus-summary-remove-bookmark t])
2660         ("Limit to"
2661          ["Marks..." gnus-summary-limit-to-marks t]
2662          ["Subject..." gnus-summary-limit-to-subject t]
2663          ["Author..." gnus-summary-limit-to-author t]
2664          ["Recipient..." gnus-summary-limit-to-recipient t]
2665          ["Address..." gnus-summary-limit-to-address t]
2666          ["Age..." gnus-summary-limit-to-age t]
2667          ["Extra..." gnus-summary-limit-to-extra t]
2668          ["Score..." gnus-summary-limit-to-score t]
2669          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2670          ["Unread" gnus-summary-limit-to-unread t]
2671          ["Unseen" gnus-summary-limit-to-unseen t]
2672          ["Singletons" gnus-summary-limit-to-singletons t]
2673          ["Replied" gnus-summary-limit-to-replied t]
2674          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2675          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2676          ["Pop limit" gnus-summary-pop-limit t]
2677          ["Show dormant" gnus-summary-limit-include-dormant t]
2678          ["Hide childless dormant"
2679           gnus-summary-limit-exclude-childless-dormant t]
2680          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2681          ["Hide marked" gnus-summary-limit-exclude-marks t]
2682          ["Show expunged" gnus-summary-limit-include-expunged t])
2683         ("Process Mark"
2684          ["Set mark" gnus-summary-mark-as-processable t]
2685          ["Remove mark" gnus-summary-unmark-as-processable t]
2686          ["Remove all marks" gnus-summary-unmark-all-processable t]
2687          ["Invert marks" gnus-uu-invert-processable t]
2688          ["Mark above" gnus-uu-mark-over t]
2689          ["Mark series" gnus-uu-mark-series t]
2690          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2691          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2692          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2693          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2694          ["Mark all" gnus-uu-mark-all t]
2695          ["Mark buffer" gnus-uu-mark-buffer t]
2696          ["Mark sparse" gnus-uu-mark-sparse t]
2697          ["Mark thread" gnus-uu-mark-thread t]
2698          ["Unmark thread" gnus-uu-unmark-thread t]
2699          ("Process Mark Sets"
2700           ["Kill" gnus-summary-kill-process-mark t]
2701           ["Yank" gnus-summary-yank-process-mark
2702            gnus-newsgroup-process-stack]
2703           ["Save" gnus-summary-save-process-mark t]
2704           ["Run command on marked..." gnus-summary-universal-argument t]))
2705         ("Registry Marks")
2706         ("Scroll article"
2707          ["Page forward" gnus-summary-next-page
2708           ,@(if (featurep 'xemacs) '(t)
2709               '(:help "Show next page of article"))]
2710          ["Page backward" gnus-summary-prev-page
2711           ,@(if (featurep 'xemacs) '(t)
2712               '(:help "Show previous page of article"))]
2713          ["Line forward" gnus-summary-scroll-up t])
2714         ("Move"
2715          ["Next unread article" gnus-summary-next-unread-article t]
2716          ["Previous unread article" gnus-summary-prev-unread-article t]
2717          ["Next article" gnus-summary-next-article t]
2718          ["Previous article" gnus-summary-prev-article t]
2719          ["Next unread subject" gnus-summary-next-unread-subject t]
2720          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2721          ["Next article same subject" gnus-summary-next-same-subject t]
2722          ["Previous article same subject" gnus-summary-prev-same-subject t]
2723          ["First unread article" gnus-summary-first-unread-article t]
2724          ["Best unread article" gnus-summary-best-unread-article t]
2725          ["Go to subject number..." gnus-summary-goto-subject t]
2726          ["Go to article number..." gnus-summary-goto-article t]
2727          ["Go to the last article" gnus-summary-goto-last-article t]
2728          ["Pop article off history" gnus-summary-pop-article t])
2729         ("Sort"
2730          ["Sort by number" gnus-summary-sort-by-number t]
2731          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2732          ["Sort by author" gnus-summary-sort-by-author t]
2733          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2734          ["Sort by subject" gnus-summary-sort-by-subject t]
2735          ["Sort by date" gnus-summary-sort-by-date t]
2736          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2737          ["Sort by score" gnus-summary-sort-by-score t]
2738          ["Sort by lines" gnus-summary-sort-by-lines t]
2739          ["Sort by characters" gnus-summary-sort-by-chars t]
2740          ["Randomize" gnus-summary-sort-by-random t]
2741          ["Original sort" gnus-summary-sort-by-original t])
2742         ("Help"
2743          ["Describe group" gnus-summary-describe-group t]
2744          ["Fetch control message" gnus-group-fetch-control
2745           ,@(if (featurep 'xemacs) nil
2746               '(:help "Display the archived control message for the current group"))]
2747          ["Read manual" gnus-info-find-node t])
2748         ("Modes"
2749          ["Pick and read" gnus-pick-mode t]
2750          ["Binary" gnus-binary-mode t])
2751         ("Regeneration"
2752          ["Regenerate" gnus-summary-prepare t]
2753          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2754          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2755          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2756          ["Toggle threading" gnus-summary-toggle-threads t])
2757         ["See old articles" gnus-summary-insert-old-articles t]
2758         ["See new articles" gnus-summary-insert-new-articles t]
2759         ["Filter articles..." gnus-summary-execute-command t]
2760         ["Run command on articles..." gnus-summary-universal-argument t]
2761         ["Search articles forward..." gnus-summary-search-article-forward t]
2762         ["Search articles backward..." gnus-summary-search-article-backward t]
2763         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2764         ["Expand window" gnus-summary-expand-window t]
2765         ["Expire expirable articles" gnus-summary-expire-articles
2766          (gnus-check-backend-function
2767           'request-expire-articles gnus-newsgroup-name)]
2768         ["Edit local kill file" gnus-summary-edit-local-kill t]
2769         ["Edit main kill file" gnus-summary-edit-global-kill t]
2770         ["Edit group parameters" gnus-summary-edit-parameters t]
2771         ["Customize group parameters" gnus-summary-customize-parameters t]
2772         ["Send a bug report" gnus-bug t]
2773         ("Exit"
2774          ["Catchup and exit" gnus-summary-catchup-and-exit
2775           ,@(if (featurep 'xemacs) '(t)
2776               '(:help "Mark unread articles in this group as read, then exit"))]
2777          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2778          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2779          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2780          ["Exit group" gnus-summary-exit
2781           ,@(if (featurep 'xemacs) '(t)
2782               '(:help "Exit current group, return to group selection mode"))]
2783          ["Exit group without updating" gnus-summary-exit-no-update t]
2784          ["Exit and goto next group" gnus-summary-next-group t]
2785          ["Exit and goto prev group" gnus-summary-prev-group t]
2786          ["Reselect group" gnus-summary-reselect-current-group t]
2787          ["Rescan group" gnus-summary-rescan-group t]
2788          ["Update dribble" gnus-summary-save-newsrc t])))
2789
2790     (gnus-run-hooks 'gnus-summary-menu-hook)))
2791
2792 (defvar gnus-summary-tool-bar-map nil)
2793
2794 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2795 ;; affect _new_ message buffers.  We might add a function that walks thru all
2796 ;; summary-mode buffers and force the update.
2797 (defun gnus-summary-tool-bar-update (&optional symbol value)
2798   "Update summary mode toolbar.
2799 Setter function for custom variables."
2800   (setq-default gnus-summary-tool-bar-map nil)
2801   (when symbol
2802     ;; When used as ":set" function:
2803     (set-default symbol value))
2804   (when (gnus-buffer-live-p gnus-summary-buffer)
2805     (with-current-buffer gnus-summary-buffer
2806       (gnus-summary-make-tool-bar))))
2807
2808 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2809                                      'gnus-summary-tool-bar-gnome
2810                                    'gnus-summary-tool-bar-retro)
2811   "Specifies the Gnus summary tool bar.
2812
2813 It can be either a list or a symbol refering to a list.  See
2814 `gmm-tool-bar-from-list' for the format of the list.  The
2815 default key map is `gnus-summary-mode-map'.
2816
2817 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2818 `gnus-summary-tool-bar-retro'."
2819   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2820                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2821                  (repeat :tag "User defined list" gmm-tool-bar-item)
2822                  (symbol))
2823   :version "23.1" ;; No Gnus
2824   :initialize 'custom-initialize-default
2825   :set 'gnus-summary-tool-bar-update
2826   :group 'gnus-summary)
2827
2828 (defcustom gnus-summary-tool-bar-gnome
2829   '((gnus-summary-post-news "mail/compose" nil)
2830     (gnus-summary-insert-new-articles "mail/inbox" nil
2831                                       :visible (or (not gnus-agent)
2832                                                    gnus-plugged))
2833     (gnus-summary-reply-with-original "mail/reply")
2834     (gnus-summary-reply "mail/reply" nil :visible nil)
2835     (gnus-summary-followup-with-original "mail/reply-all")
2836     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2837     (gnus-summary-mail-forward "mail/forward")
2838     (gnus-summary-save-article "mail/save")
2839     (gnus-summary-search-article-forward "search" nil :visible nil)
2840     (gnus-summary-print-article "print")
2841     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2842     ;; Some new commands that may need more suitable icons:
2843     (gnus-summary-save-newsrc "save" nil :visible nil)
2844     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2845     (gnus-summary-prev-article "left-arrow")
2846     (gnus-summary-next-article "right-arrow")
2847     (gnus-summary-next-page "next-page")
2848     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2849     ;;
2850     ;; Maybe some sort-by-... could be added:
2851     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2852     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2853     (gnus-summary-mark-as-expirable
2854      "delete" nil
2855      :visible (gnus-check-backend-function 'request-expire-articles
2856                                            gnus-newsgroup-name))
2857     (gnus-summary-mark-as-spam
2858      "mail/spam" t
2859      :visible (and (fboundp 'spam-group-ham-contents-p)
2860                    (spam-group-ham-contents-p gnus-newsgroup-name))
2861      :help "Mark as spam")
2862     (gnus-summary-mark-as-read-forward
2863      "mail/not-spam" nil
2864      :visible (and (fboundp 'spam-group-spam-contents-p)
2865                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2866     ;;
2867     (gnus-summary-exit "exit")
2868     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2869     (gnus-info-find-node "help"))
2870   "List of functions for the summary tool bar (GNOME style).
2871
2872 See `gmm-tool-bar-from-list' for the format of the list."
2873   :type '(repeat gmm-tool-bar-item)
2874   :version "23.1" ;; No Gnus
2875   :initialize 'custom-initialize-default
2876   :set 'gnus-summary-tool-bar-update
2877   :group 'gnus-summary)
2878
2879 (defcustom gnus-summary-tool-bar-retro
2880   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2881     (gnus-summary-next-unread-article "gnus/next-ur")
2882     (gnus-summary-post-news "gnus/post")
2883     (gnus-summary-followup-with-original "gnus/fuwo")
2884     (gnus-summary-followup "gnus/followup")
2885     (gnus-summary-reply-with-original "gnus/reply-wo")
2886     (gnus-summary-reply "gnus/reply")
2887     (gnus-summary-caesar-message "gnus/rot13")
2888     (gnus-uu-decode-uu "gnus/uu-decode")
2889     (gnus-summary-save-article-file "gnus/save-aif")
2890     (gnus-summary-save-article "gnus/save-art")
2891     (gnus-uu-post-news "gnus/uu-post")
2892     (gnus-summary-catchup "gnus/catchup")
2893     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2894     (gnus-summary-exit "gnus/exit-summ")
2895     ;; Some new command that may need more suitable icons:
2896     (gnus-summary-print-article "gnus/print" nil :visible nil)
2897     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2898     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2899     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2900     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2901     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2902     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2903     ;;
2904     (gnus-info-find-node "gnus/help" nil :visible nil))
2905   "List of functions for the summary tool bar (retro look).
2906
2907 See `gmm-tool-bar-from-list' for the format of the list."
2908   :type '(repeat gmm-tool-bar-item)
2909   :version "23.1" ;; No Gnus
2910   :initialize 'custom-initialize-default
2911   :set 'gnus-summary-tool-bar-update
2912   :group 'gnus-summary)
2913
2914 (defcustom gnus-summary-tool-bar-zap-list t
2915   "List of icon items from the global tool bar.
2916 These items are not displayed in the Gnus summary mode tool bar.
2917
2918 See `gmm-tool-bar-from-list' for the format of the list."
2919   :type 'gmm-tool-bar-zap-list
2920   :version "23.1" ;; No Gnus
2921   :initialize 'custom-initialize-default
2922   :set 'gnus-summary-tool-bar-update
2923   :group 'gnus-summary)
2924
2925 (defvar image-load-path)
2926 (defvar tool-bar-map)
2927
2928 (defun gnus-summary-make-tool-bar (&optional force)
2929   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2930 When FORCE, rebuild the tool bar."
2931   (when (and (not (featurep 'xemacs))
2932              (boundp 'tool-bar-mode)
2933              tool-bar-mode
2934              (or (not gnus-summary-tool-bar-map) force))
2935     (let* ((load-path
2936             (gmm-image-load-path-for-library "gnus"
2937                                              "mail/save.xpm"
2938                                              nil t))
2939            (image-load-path (cons (car load-path)
2940                                   (when (boundp 'image-load-path)
2941                                     image-load-path)))
2942            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2943                                         gnus-summary-tool-bar-zap-list
2944                                         'gnus-summary-mode-map)))
2945       (when map
2946         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2947         ;; uses it's value.
2948         (setq gnus-summary-tool-bar-map map))))
2949   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2950
2951 (defun gnus-score-set-default (var value)
2952   "A version of set that updates the GNU Emacs menu-bar."
2953   (set var value)
2954   ;; It is the message that forces the active status to be updated.
2955   (message ""))
2956
2957 (defun gnus-make-score-map (type)
2958   "Make a summary score map of type TYPE."
2959   (if t
2960       nil
2961     (let ((headers '(("author" "from" string)
2962                      ("subject" "subject" string)
2963                      ("article body" "body" string)
2964                      ("article head" "head" string)
2965                      ("xref" "xref" string)
2966                      ("extra header" "extra" string)
2967                      ("lines" "lines" number)
2968                      ("followups to author" "followup" string)))
2969           (types '((number ("less than" <)
2970                            ("greater than" >)
2971                            ("equal" =))
2972                    (string ("substring" s)
2973                            ("exact string" e)
2974                            ("fuzzy string" f)
2975                            ("regexp" r))))
2976           (perms '(("temporary" (current-time-string))
2977                    ("permanent" nil)
2978                    ("immediate" now)))
2979           header)
2980       (list
2981        (apply
2982         'nconc
2983         (list
2984          (if (eq type 'lower)
2985              "Lower score"
2986            "Increase score"))
2987         (let (outh)
2988           (while headers
2989             (setq header (car headers))
2990             (setq outh
2991                   (cons
2992                    (apply
2993                     'nconc
2994                     (list (car header))
2995                     (let ((ts (cdr (assoc (nth 2 header) types)))
2996                           outt)
2997                       (while ts
2998                         (setq outt
2999                               (cons
3000                                (apply
3001                                 'nconc
3002                                 (list (caar ts))
3003                                 (let ((ps perms)
3004                                       outp)
3005                                   (while ps
3006                                     (setq outp
3007                                           (cons
3008                                            (vector
3009                                             (caar ps)
3010                                             (list
3011                                              'gnus-summary-score-entry
3012                                              (nth 1 header)
3013                                              (if (or (string= (nth 1 header)
3014                                                               "head")
3015                                                      (string= (nth 1 header)
3016                                                               "body"))
3017                                                  ""
3018                                                (list 'gnus-summary-header
3019                                                      (nth 1 header)))
3020                                              (list 'quote (nth 1 (car ts)))
3021                                              (list 'gnus-score-delta-default
3022                                                    nil)
3023                                              (nth 1 (car ps))
3024                                              t)
3025                                             t)
3026                                            outp))
3027                                     (setq ps (cdr ps)))
3028                                   (list (nreverse outp))))
3029                                outt))
3030                         (setq ts (cdr ts)))
3031                       (list (nreverse outt))))
3032                    outh))
3033             (setq headers (cdr headers)))
3034           (list (nreverse outh))))))))
3035
3036
3037 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3038 (defvar bookmark-make-record-function)
3039 \f
3040
3041 (defun gnus-summary-mode (&optional group)
3042   "Major mode for reading articles.
3043
3044 All normal editing commands are switched off.
3045 \\<gnus-summary-mode-map>
3046 Each line in this buffer represents one article.  To read an
3047 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3048 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3049 respectively.
3050
3051 You can also post articles and send mail from this buffer.  To
3052 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3053 of an article, type `\\[gnus-summary-reply]'.
3054
3055 There are approx. one gazillion commands you can execute in this
3056 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3057
3058 The following commands are available:
3059
3060 \\{gnus-summary-mode-map}"
3061   (interactive)
3062   (kill-all-local-variables)
3063   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3064     (gnus-summary-make-local-variables))
3065   (gnus-summary-make-local-variables)
3066   (setq gnus-newsgroup-name group)
3067   (when (gnus-visual-p 'summary-menu 'menu)
3068     (gnus-summary-make-menu-bar)
3069     (gnus-summary-make-tool-bar))
3070   (gnus-make-thread-indent-array)
3071   (gnus-simplify-mode-line)
3072   (setq major-mode 'gnus-summary-mode)
3073   (setq mode-name "Summary")
3074   (use-local-map gnus-summary-mode-map)
3075   (buffer-disable-undo)
3076   (setq buffer-read-only t              ;Disable modification
3077         show-trailing-whitespace nil)
3078   (setq truncate-lines t)
3079   (add-to-invisibility-spec '(gnus-sum . t))
3080   (gnus-summary-set-display-table)
3081   (gnus-set-default-directory)
3082   (make-local-variable 'gnus-summary-line-format)
3083   (make-local-variable 'gnus-summary-line-format-spec)
3084   (make-local-variable 'gnus-summary-dummy-line-format)
3085   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3086   (make-local-variable 'gnus-summary-mark-positions)
3087   (gnus-make-local-hook 'pre-command-hook)
3088   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3089   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3090   (turn-on-gnus-mailing-list-mode)
3091   (mm-enable-multibyte)
3092   (set (make-local-variable 'bookmark-make-record-function)
3093        'gnus-summary-bookmark-make-record)
3094   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3095   (gnus-update-summary-mark-positions))
3096
3097 (defun gnus-summary-make-local-variables ()
3098   "Make all the local summary buffer variables."
3099   (let (global)
3100     (dolist (local gnus-summary-local-variables)
3101       (if (consp local)
3102           (progn
3103             (if (eq (cdr local) 'global)
3104                 ;; Copy the global value of the variable.
3105                 (setq global (symbol-value (car local)))
3106               ;; Use the value from the list.
3107               (setq global (eval (cdr local))))
3108             (set (make-local-variable (car local)) global))
3109         ;; Simple nil-valued local variable.
3110         (set (make-local-variable local) nil)))))
3111
3112 (defun gnus-summary-clear-local-variables ()
3113   (let ((locals gnus-summary-local-variables))
3114     (while locals
3115       (if (consp (car locals))
3116           (and (symbolp (caar locals))
3117                (set (caar locals) nil))
3118         (and (symbolp (car locals))
3119              (set (car locals) nil)))
3120       (setq locals (cdr locals)))))
3121
3122 ;; Summary data functions.
3123
3124 (defmacro gnus-data-number (data)
3125   `(car ,data))
3126
3127 (defmacro gnus-data-set-number (data number)
3128   `(setcar ,data ,number))
3129
3130 (defmacro gnus-data-mark (data)
3131   `(nth 1 ,data))
3132
3133 (defmacro gnus-data-set-mark (data mark)
3134   `(setcar (nthcdr 1 ,data) ,mark))
3135
3136 (defmacro gnus-data-pos (data)
3137   `(nth 2 ,data))
3138
3139 (defmacro gnus-data-set-pos (data pos)
3140   `(setcar (nthcdr 2 ,data) ,pos))
3141
3142 (defmacro gnus-data-header (data)
3143   `(nth 3 ,data))
3144
3145 (defmacro gnus-data-set-header (data header)
3146   `(setf (nth 3 ,data) ,header))
3147
3148 (defmacro gnus-data-level (data)
3149   `(nth 4 ,data))
3150
3151 (defmacro gnus-data-unread-p (data)
3152   `(= (nth 1 ,data) gnus-unread-mark))
3153
3154 (defmacro gnus-data-read-p (data)
3155   `(/= (nth 1 ,data) gnus-unread-mark))
3156
3157 (defmacro gnus-data-pseudo-p (data)
3158   `(consp (nth 3 ,data)))
3159
3160 (defmacro gnus-data-find (number)
3161   `(assq ,number gnus-newsgroup-data))
3162
3163 (defmacro gnus-data-find-list (number &optional data)
3164   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3165      (memq (assq ,number bdata)
3166            bdata)))
3167
3168 (defmacro gnus-data-make (number mark pos header level)
3169   `(list ,number ,mark ,pos ,header ,level))
3170
3171 (defun gnus-data-enter (after-article number mark pos header level offset)
3172   (let ((data (gnus-data-find-list after-article)))
3173     (unless data
3174       (error "No such article: %d" after-article))
3175     (setcdr data (cons (gnus-data-make number mark pos header level)
3176                        (cdr data)))
3177     (setq gnus-newsgroup-data-reverse nil)
3178     (gnus-data-update-list (cddr data) offset)))
3179
3180 (defun gnus-data-enter-list (after-article list &optional offset)
3181   (when list
3182     (let ((data (and after-article (gnus-data-find-list after-article)))
3183           (ilist list))
3184       (if (not (or data
3185                    after-article))
3186           (let ((odata gnus-newsgroup-data))
3187             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3188             (when offset
3189               (gnus-data-update-list odata offset)))
3190         ;; Find the last element in the list to be spliced into the main
3191         ;; list.
3192         (setq list (last list))
3193         (if (not data)
3194             (progn
3195               (setcdr list gnus-newsgroup-data)
3196               (setq gnus-newsgroup-data ilist)
3197               (when offset
3198                 (gnus-data-update-list (cdr list) offset)))
3199           (setcdr list (cdr data))
3200           (setcdr data ilist)
3201           (when offset
3202             (gnus-data-update-list (cdr list) offset))))
3203       (setq gnus-newsgroup-data-reverse nil))))
3204
3205 (defun gnus-data-remove (article &optional offset)
3206   (let ((data gnus-newsgroup-data))
3207     (if (= (gnus-data-number (car data)) article)
3208         (progn
3209           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3210                 gnus-newsgroup-data-reverse nil)
3211           (when offset
3212             (gnus-data-update-list gnus-newsgroup-data offset)))
3213       (while (cdr data)
3214         (when (= (gnus-data-number (cadr data)) article)
3215           (setcdr data (cddr data))
3216           (when offset
3217             (gnus-data-update-list (cdr data) offset))
3218           (setq data nil
3219                 gnus-newsgroup-data-reverse nil))
3220         (setq data (cdr data))))))
3221
3222 (defmacro gnus-data-list (backward)
3223   `(if ,backward
3224        (or gnus-newsgroup-data-reverse
3225            (setq gnus-newsgroup-data-reverse
3226                  (reverse gnus-newsgroup-data)))
3227      gnus-newsgroup-data))
3228
3229 (defun gnus-data-update-list (data offset)
3230   "Add OFFSET to the POS of all data entries in DATA."
3231   (setq gnus-newsgroup-data-reverse nil)
3232   (while data
3233     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3234     (setq data (cdr data))))
3235
3236 (defun gnus-summary-article-pseudo-p (article)
3237   "Say whether this article is a pseudo article or not."
3238   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3239
3240 (defmacro gnus-summary-article-sparse-p (article)
3241   "Say whether this article is a sparse article or not."
3242   `(memq ,article gnus-newsgroup-sparse))
3243
3244 (defmacro gnus-summary-article-ancient-p (article)
3245   "Say whether this article is a sparse article or not."
3246   `(memq ,article gnus-newsgroup-ancient))
3247
3248 (defun gnus-article-parent-p (number)
3249   "Say whether this article is a parent or not."
3250   (let ((data (gnus-data-find-list number)))
3251     (and (cdr data)              ; There has to be an article after...
3252          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3253             (gnus-data-level (nth 1 data))))))
3254
3255 (defun gnus-article-children (number)
3256   "Return a list of all children to NUMBER."
3257   (let* ((data (gnus-data-find-list number))
3258          (level (gnus-data-level (car data)))
3259          children)
3260     (setq data (cdr data))
3261     (while (and data
3262                 (= (gnus-data-level (car data)) (1+ level)))
3263       (push (gnus-data-number (car data)) children)
3264       (setq data (cdr data)))
3265     children))
3266
3267 (defmacro gnus-summary-skip-intangible ()
3268   "If the current article is intangible, then jump to a different article."
3269   '(let ((to (get-text-property (point) 'gnus-intangible)))
3270      (and to (gnus-summary-goto-subject to))))
3271
3272 (defmacro gnus-summary-article-intangible-p ()
3273   "Say whether this article is intangible or not."
3274   '(get-text-property (point) 'gnus-intangible))
3275
3276 (defun gnus-article-read-p (article)
3277   "Say whether ARTICLE is read or not."
3278   (not (or (memq article gnus-newsgroup-marked)
3279            (memq article gnus-newsgroup-spam-marked)
3280            (memq article gnus-newsgroup-unreads)
3281            (memq article gnus-newsgroup-unselected)
3282            (memq article gnus-newsgroup-dormant))))
3283
3284 ;; Some summary mode macros.
3285
3286 (defmacro gnus-summary-article-number ()
3287   "The article number of the article on the current line.
3288 If there isn't an article number here, then we return the current
3289 article number."
3290   '(progn
3291      (gnus-summary-skip-intangible)
3292      (or (get-text-property (point) 'gnus-number)
3293          (gnus-summary-last-subject))))
3294
3295 (defmacro gnus-summary-article-header (&optional number)
3296   "Return the header of article NUMBER."
3297   `(gnus-data-header (gnus-data-find
3298                       ,(or number '(gnus-summary-article-number)))))
3299
3300 (defmacro gnus-summary-thread-level (&optional number)
3301   "Return the level of thread that starts with article NUMBER."
3302   `(if (and (eq gnus-summary-make-false-root 'dummy)
3303             (get-text-property (point) 'gnus-intangible))
3304        0
3305      (gnus-data-level (gnus-data-find
3306                        ,(or number '(gnus-summary-article-number))))))
3307
3308 (defmacro gnus-summary-article-mark (&optional number)
3309   "Return the mark of article NUMBER."
3310   `(gnus-data-mark (gnus-data-find
3311                     ,(or number '(gnus-summary-article-number)))))
3312
3313 (defmacro gnus-summary-article-pos (&optional number)
3314   "Return the position of the line of article NUMBER."
3315   `(gnus-data-pos (gnus-data-find
3316                    ,(or number '(gnus-summary-article-number)))))
3317
3318 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3319 (defmacro gnus-summary-article-subject (&optional number)
3320   "Return current subject string or nil if nothing."
3321   `(let ((headers
3322           ,(if number
3323                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3324              '(gnus-data-header (assq (gnus-summary-article-number)
3325                                       gnus-newsgroup-data)))))
3326      (and headers
3327           (vectorp headers)
3328           (mail-header-subject headers))))
3329
3330 (defmacro gnus-summary-article-score (&optional number)
3331   "Return current article score."
3332   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3333                   gnus-newsgroup-scored))
3334        gnus-summary-default-score 0))
3335
3336 (defun gnus-summary-article-children (&optional number)
3337   "Return a list of article numbers that are children of article NUMBER."
3338   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3339          (level (gnus-data-level (car data)))
3340          l children)
3341     (while (and (setq data (cdr data))
3342                 (> (setq l (gnus-data-level (car data))) level))
3343       (and (= (1+ level) l)
3344            (push (gnus-data-number (car data))
3345                  children)))
3346     (nreverse children)))
3347
3348 (defun gnus-summary-article-parent (&optional number)
3349   "Return the article number of the parent of article NUMBER."
3350   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3351                                     (gnus-data-list t)))
3352          (level (gnus-data-level (car data))))
3353     (if (zerop level)
3354         ()                              ; This is a root.
3355       ;; We search until we find an article with a level less than
3356       ;; this one.  That function has to be the parent.
3357       (while (and (setq data (cdr data))
3358                   (not (< (gnus-data-level (car data)) level))))
3359       (and data (gnus-data-number (car data))))))
3360
3361 (defun gnus-unread-mark-p (mark)
3362   "Say whether MARK is the unread mark."
3363   (= mark gnus-unread-mark))
3364
3365 (defun gnus-read-mark-p (mark)
3366   "Say whether MARK is one of the marks that mark as read.
3367 This is all marks except unread, ticked, dormant, and expirable."
3368   (not (or (= mark gnus-unread-mark)
3369            (= mark gnus-ticked-mark)
3370            (= mark gnus-spam-mark)
3371            (= mark gnus-dormant-mark)
3372            (= mark gnus-expirable-mark))))
3373
3374 (defmacro gnus-article-mark (number)
3375   "Return the MARK of article NUMBER.
3376 This macro should only be used when computing the mark the \"first\"
3377 time; i.e., when generating the summary lines.  After that,
3378 `gnus-summary-article-mark' should be used to examine the
3379 marks of articles."
3380   `(cond
3381     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3382     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3383     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3384     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3385     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3386     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3387     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3388     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3389            gnus-ancient-mark))))
3390
3391 ;; Saving hidden threads.
3392
3393 (defmacro gnus-save-hidden-threads (&rest forms)
3394   "Save hidden threads, eval FORMS, and restore the hidden threads."
3395   (let ((config (make-symbol "config")))
3396     `(let ((,config (gnus-hidden-threads-configuration)))
3397        (unwind-protect
3398            (save-excursion
3399              ,@forms)
3400          (gnus-restore-hidden-threads-configuration ,config)))))
3401 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3402 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3403
3404 (defun gnus-data-compute-positions ()
3405   "Compute the positions of all articles."
3406   (setq gnus-newsgroup-data-reverse nil)
3407   (let ((data gnus-newsgroup-data))
3408     (save-excursion
3409       (gnus-save-hidden-threads
3410         (gnus-summary-show-all-threads)
3411         (goto-char (point-min))
3412         (while data
3413           (while (get-text-property (point) 'gnus-intangible)
3414             (forward-line 1))
3415           (gnus-data-set-pos (car data) (+ (point) 3))
3416           (setq data (cdr data))
3417           (forward-line 1))))))
3418
3419 (defun gnus-hidden-threads-configuration ()
3420   "Return the current hidden threads configuration."
3421   (save-excursion
3422     (let (config)
3423       (goto-char (point-min))
3424       (while (not (eobp))
3425         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3426           (push (save-excursion (forward-line 0) (point)) config))
3427         (forward-line 1))
3428       config)))
3429
3430 (defun gnus-restore-hidden-threads-configuration (config)
3431   "Restore hidden threads configuration from CONFIG."
3432   (save-excursion
3433     (let (point (inhibit-read-only t))
3434       (while (setq point (pop config))
3435         (goto-char point)
3436         (gnus-summary-hide-thread)))))
3437
3438 ;; Various summary mode internalish functions.
3439
3440 (defun gnus-mouse-pick-article (e)
3441   (interactive "e")
3442   (mouse-set-point e)
3443   (gnus-summary-next-page nil t))
3444
3445 (defun gnus-summary-set-display-table ()
3446   "Change the display table.
3447 Odd characters have a tendency to mess
3448 up nicely formatted displays - we make all possible glyphs
3449 display only a single character."
3450
3451   ;; We start from the standard display table, if any.
3452   (let ((table (or (copy-sequence standard-display-table)
3453                    (make-display-table)))
3454         (i 32))
3455     ;; Nix out all the control chars...
3456     (while (>= (setq i (1- i)) 0)
3457       (gnus-put-display-table i [??] table))
3458    ;; ... but not newline and cr, of course.  (cr is necessary for the
3459     ;; selective display).
3460     (gnus-put-display-table ?\n nil table)
3461     (gnus-put-display-table ?\r nil table)
3462     ;; We keep TAB as well.
3463     (gnus-put-display-table ?\t nil table)
3464     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3465     ;; Emacs 23 (unicode), that are not set already.
3466     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3467                  160
3468                256)))
3469       (while (>= (setq i (1- i)) 127)
3470         ;; Only modify if the entry is nil.
3471         (unless (gnus-get-display-table i table)
3472           (gnus-put-display-table i [??] table))))
3473     (setq buffer-display-table table)))
3474
3475 (defun gnus-summary-set-article-display-arrow (pos)
3476   "Update the overlay arrow to point to line at position POS."
3477   (when gnus-summary-display-arrow
3478     (make-local-variable 'overlay-arrow-position)
3479     (make-local-variable 'overlay-arrow-string)
3480     (save-excursion
3481       (goto-char pos)
3482       (beginning-of-line)
3483       (unless overlay-arrow-position
3484         (setq overlay-arrow-position (make-marker)))
3485       (setq overlay-arrow-string "=>"
3486             overlay-arrow-position (set-marker overlay-arrow-position
3487                                                (point)
3488                                                (current-buffer))))))
3489
3490 (defun gnus-summary-setup-buffer (group)
3491   "Initialize summary buffer."
3492   (let ((buffer (gnus-summary-buffer-name group))
3493         (dead-name (concat "*Dead Summary "
3494                            (gnus-group-decoded-name group) "*")))
3495     ;; If a dead summary buffer exists, we kill it.
3496     (when (gnus-buffer-live-p dead-name)
3497       (gnus-kill-buffer dead-name))
3498     (if (get-buffer buffer)
3499         (progn
3500           (set-buffer buffer)
3501           (setq gnus-summary-buffer (current-buffer))
3502           (not gnus-newsgroup-prepared))
3503       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3504       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3505       (gnus-summary-mode group)
3506       (when gnus-carpal
3507         (gnus-carpal-setup-buffer 'summary))
3508       (when (gnus-group-quit-config group)
3509         (set (make-local-variable 'gnus-single-article-buffer) nil))
3510       (make-local-variable 'gnus-article-buffer)
3511       (make-local-variable 'gnus-article-current)
3512       (make-local-variable 'gnus-original-article-buffer)
3513       (setq gnus-newsgroup-name group)
3514       ;; Set any local variables in the group parameters.
3515       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3516       t)))
3517
3518 (defun gnus-set-global-variables ()
3519   "Set the global equivalents of the buffer-local variables.
3520 They are set to the latest values they had.  These reflect the summary
3521 buffer that was in action when the last article was fetched."
3522   (when (eq major-mode 'gnus-summary-mode)
3523     (setq gnus-summary-buffer (current-buffer))
3524     (let ((name gnus-newsgroup-name)
3525           (marked gnus-newsgroup-marked)
3526           (spam gnus-newsgroup-spam-marked)
3527           (unread gnus-newsgroup-unreads)
3528           (headers gnus-current-headers)
3529           (data gnus-newsgroup-data)
3530           (summary gnus-summary-buffer)
3531           (article-buffer gnus-article-buffer)
3532           (original gnus-original-article-buffer)
3533           (gac gnus-article-current)
3534           (reffed gnus-reffed-article-number)
3535           (score-file gnus-current-score-file)
3536           (default-charset gnus-newsgroup-charset)
3537           vlist)
3538       (let ((locals gnus-newsgroup-variables))
3539         (while locals
3540           (if (consp (car locals))
3541               (push (eval (caar locals)) vlist)
3542             (push (eval (car locals)) vlist))
3543           (setq locals (cdr locals)))
3544         (setq vlist (nreverse vlist)))
3545       (with-current-buffer gnus-group-buffer
3546         (setq gnus-newsgroup-name name
3547               gnus-newsgroup-marked marked
3548               gnus-newsgroup-spam-marked spam
3549               gnus-newsgroup-unreads unread
3550               gnus-current-headers headers
3551               gnus-newsgroup-data data
3552               gnus-article-current gac
3553               gnus-summary-buffer summary
3554               gnus-article-buffer article-buffer
3555               gnus-original-article-buffer original
3556               gnus-reffed-article-number reffed
3557               gnus-current-score-file score-file
3558               gnus-newsgroup-charset default-charset)
3559         (let ((locals gnus-newsgroup-variables))
3560           (while locals
3561             (if (consp (car locals))
3562                 (set (caar locals) (pop vlist))
3563               (set (car locals) (pop vlist)))
3564             (setq locals (cdr locals))))
3565         ;; The article buffer also has local variables.
3566         (when (gnus-buffer-live-p gnus-article-buffer)
3567           (set-buffer gnus-article-buffer)
3568           (setq gnus-summary-buffer summary))))))
3569
3570 (defun gnus-summary-article-unread-p (article)
3571   "Say whether ARTICLE is unread or not."
3572   (memq article gnus-newsgroup-unreads))
3573
3574 (defun gnus-summary-first-article-p (&optional article)
3575   "Return whether ARTICLE is the first article in the buffer."
3576   (if (not (setq article (or article (gnus-summary-article-number))))
3577       nil
3578     (eq article (caar gnus-newsgroup-data))))
3579
3580 (defun gnus-summary-last-article-p (&optional article)
3581   "Return whether ARTICLE is the last article in the buffer."
3582   (if (not (setq article (or article (gnus-summary-article-number))))
3583       ;; All non-existent numbers are the last article.  :-)
3584       t
3585     (not (cdr (gnus-data-find-list article)))))
3586
3587 (defun gnus-make-thread-indent-array (&optional n)
3588   (when (or n
3589             (progn (setq n 200) nil)
3590             (null gnus-thread-indent-array)
3591             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3592     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3593           gnus-thread-indent-array-level gnus-thread-indent-level)
3594     (while (>= n 0)
3595       (aset gnus-thread-indent-array n
3596             (make-string (* n gnus-thread-indent-level) ? ))
3597       (setq n (1- n)))))
3598
3599 (defun gnus-update-summary-mark-positions ()
3600   "Compute where the summary marks are to go."
3601   (save-excursion
3602     (when (gnus-buffer-exists-p gnus-summary-buffer)
3603       (set-buffer gnus-summary-buffer))
3604     (let ((spec gnus-summary-line-format-spec)
3605           pos)
3606       (save-excursion
3607         (gnus-set-work-buffer)
3608         (let ((gnus-tmp-unread ?Z)
3609               (gnus-replied-mark ?Z)
3610               (gnus-score-below-mark ?Z)
3611               (gnus-score-over-mark ?Z)
3612               (gnus-undownloaded-mark ?Z)
3613               (gnus-summary-line-format-spec spec)
3614               (gnus-newsgroup-downloadable '(0))
3615               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3616               case-fold-search ignores)
3617           ;; Here, all marks are bound to Z.
3618           (gnus-summary-insert-line header
3619                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3620           (goto-char (point-min))
3621           ;; Memorize the positions of the same characters as dummy marks.
3622           (while (re-search-forward "[A-D]" nil t)
3623             (push (point) ignores))
3624           (erase-buffer)
3625           ;; We use A-D as dummy marks in order to know column positions
3626           ;; where marks should be inserted.
3627           (setq gnus-tmp-unread ?A
3628                 gnus-replied-mark ?B
3629                 gnus-score-below-mark ?C
3630                 gnus-score-over-mark ?C
3631                 gnus-undownloaded-mark ?D)
3632           (gnus-summary-insert-line header
3633                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3634           ;; Ignore characters which aren't dummy marks.
3635           (dolist (p ignores)
3636             (delete-region (goto-char (1- p)) p)
3637             (insert ?Z))
3638           (goto-char (point-min))
3639           (setq pos (list (cons 'unread
3640                                 (and (search-forward "A" nil t)
3641                                      (- (point) (point-min) 1)))))
3642           (goto-char (point-min))
3643           (push (cons 'replied (and (search-forward "B" nil t)
3644                                     (- (point) (point-min) 1)))
3645                 pos)
3646           (goto-char (point-min))
3647           (push (cons 'score (and (search-forward "C" nil t)
3648                                   (- (point) (point-min) 1)))
3649                 pos)
3650           (goto-char (point-min))
3651           (push (cons 'download (and (search-forward "D" nil t)
3652                                      (- (point) (point-min) 1)))
3653                 pos)))
3654       (setq gnus-summary-mark-positions pos))))
3655
3656 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3657   "Insert a dummy root in the summary buffer."
3658   (beginning-of-line)
3659   (gnus-add-text-properties
3660    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3661    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3662
3663 (defun gnus-summary-extract-address-component (from)
3664   (or (car (funcall gnus-extract-address-components from))
3665       from))
3666
3667 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3668   (let ((mail-parse-charset gnus-newsgroup-charset)
3669         (ignored-from-addresses (gnus-ignored-from-addresses))
3670         ; Is it really necessary to do this next part for each summary line?
3671         ; Luckily, doesn't seem to slow things down much.
3672         (mail-parse-ignored-charsets
3673          (with-current-buffer gnus-summary-buffer
3674            gnus-newsgroup-ignored-charsets)))
3675     (or
3676      (and ignored-from-addresses
3677           (string-match ignored-from-addresses gnus-tmp-from)
3678           (let ((extra-headers (mail-header-extra header))
3679                 to
3680                 newsgroups)
3681             (cond
3682              ((setq to (cdr (assq 'To extra-headers)))
3683               (concat gnus-summary-to-prefix
3684                       (inline
3685                         (gnus-summary-extract-address-component
3686                          (funcall gnus-decode-encoded-address-function to)))))
3687              ((setq newsgroups
3688                     (or
3689                      (cdr (assq 'Newsgroups extra-headers))
3690                      (and
3691                       (memq 'Newsgroups gnus-extra-headers)
3692                       (eq (car (gnus-find-method-for-group
3693                                 gnus-newsgroup-name)) 'nntp)
3694                       (gnus-group-real-name gnus-newsgroup-name))))
3695               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3696      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3697
3698 (defun gnus-summary-insert-line (gnus-tmp-header
3699                                  gnus-tmp-level gnus-tmp-current
3700                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3701                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3702                                  &optional gnus-tmp-dummy gnus-tmp-score
3703                                  gnus-tmp-process)
3704   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3705       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3706                                           gnus-tmp-level)))
3707   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3708          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3709          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3710          (gnus-tmp-score-char
3711           (if (or (null gnus-summary-default-score)
3712                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3713                       gnus-summary-zcore-fuzz))
3714               ?                         ;Whitespace
3715             (if (< gnus-tmp-score gnus-summary-default-score)
3716                 gnus-score-below-mark gnus-score-over-mark)))
3717          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3718          (gnus-tmp-replied
3719           (cond (gnus-tmp-process gnus-process-mark)
3720                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3721                  gnus-cached-mark)
3722                 (gnus-tmp-replied gnus-replied-mark)
3723                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3724                  gnus-forwarded-mark)
3725                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3726                  gnus-saved-mark)
3727                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3728                  gnus-recent-mark)
3729                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3730                  gnus-unseen-mark)
3731                 (t gnus-no-mark)))
3732          (gnus-tmp-downloaded
3733           (cond (undownloaded
3734                  gnus-undownloaded-mark)
3735                 (gnus-newsgroup-agentized
3736                  gnus-downloaded-mark)
3737                 (t
3738                  gnus-no-mark)))
3739          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3740          (gnus-tmp-name
3741           (cond
3742            ((string-match "<[^>]+> *$" gnus-tmp-from)
3743             (let ((beg (match-beginning 0)))
3744               (or (and (string-match "^\".+\"" gnus-tmp-from)
3745                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3746                   (substring gnus-tmp-from 0 beg))))
3747            ((string-match "(.+)" gnus-tmp-from)
3748             (substring gnus-tmp-from
3749                        (1+ (match-beginning 0)) (1- (match-end 0))))
3750            (t gnus-tmp-from)))
3751          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3752          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3753          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3754          (inhibit-read-only t))
3755     (when (string= gnus-tmp-name "")
3756       (setq gnus-tmp-name gnus-tmp-from))
3757     (unless (numberp gnus-tmp-lines)
3758       (setq gnus-tmp-lines -1))
3759     (if (= gnus-tmp-lines -1)
3760         (setq gnus-tmp-lines "?")
3761       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3762     (condition-case ()
3763         (gnus-put-text-property
3764          (point)
3765          (progn (eval gnus-summary-line-format-spec) (point))
3766          'gnus-number gnus-tmp-number)
3767       (error (gnus-message 5 "Error updating the summary line")))
3768     (when (gnus-visual-p 'summary-highlight 'highlight)
3769       (forward-line -1)
3770       (gnus-summary-highlight-line)
3771       (gnus-run-hooks 'gnus-summary-update-hook)
3772       (forward-line 1))))
3773
3774 (defun gnus-summary-update-line (&optional dont-update)
3775   "Update summary line after change."
3776   (when (and gnus-summary-default-score
3777              (not gnus-summary-inhibit-highlight))
3778     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3779            (article (gnus-summary-article-number))
3780            (score (gnus-summary-article-score article)))
3781       (unless dont-update
3782         (if (and gnus-summary-mark-below
3783                  (< (gnus-summary-article-score)
3784                     gnus-summary-mark-below))
3785             ;; This article has a low score, so we mark it as read.
3786             (when (memq article gnus-newsgroup-unreads)
3787               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3788           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3789             ;; This article was previously marked as read on account
3790             ;; of a low score, but now it has risen, so we mark it as
3791             ;; unread.
3792             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3793         (gnus-summary-update-mark
3794          (if (or (null gnus-summary-default-score)
3795                  (<= (abs (- score gnus-summary-default-score))
3796                      gnus-summary-zcore-fuzz))
3797              ?                          ;Whitespace
3798            (if (< score gnus-summary-default-score)
3799                gnus-score-below-mark gnus-score-over-mark))
3800          'score))
3801       ;; Do visual highlighting.
3802       (when (gnus-visual-p 'summary-highlight 'highlight)
3803         (gnus-summary-highlight-line)
3804         (gnus-run-hooks 'gnus-summary-update-hook)))))
3805
3806 (defvar gnus-tmp-new-adopts nil)
3807
3808 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3809   "Return the number of articles in THREAD.
3810 This may be 0 in some cases -- if none of the articles in
3811 the thread are to be displayed."
3812   (let* ((number
3813          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3814           (cond
3815            ((not (listp thread))
3816             1)
3817            ((and (consp thread) (cdr thread))
3818             (apply
3819              '+ 1 (mapcar
3820                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3821            ((null thread)
3822             1)
3823            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3824             1)
3825            (t 0))))
3826     (when (and level (zerop level) gnus-tmp-new-adopts)
3827       (incf number
3828             (apply '+ (mapcar
3829                        'gnus-summary-number-of-articles-in-thread
3830                        gnus-tmp-new-adopts))))
3831     (if char
3832         (if (> number 1) gnus-not-empty-thread-mark
3833           gnus-empty-thread-mark)
3834       number)))
3835
3836 (defsubst gnus-summary-line-message-size (head)
3837   "Return pretty-printed version of message size.
3838 This function is intended to be used in
3839 `gnus-summary-line-format-alist'."
3840   (let ((c (or (mail-header-chars head) -1)))
3841     (cond ((< c 0) "n/a")               ; chars not available
3842           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3843           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3844           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3845           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3846
3847
3848 (defun gnus-summary-set-local-parameters (group)
3849   "Go through the local params of GROUP and set all variable specs in that list."
3850   (let ((vars '(quit-config)))          ; Ignore quit-config.
3851     (dolist (elem (gnus-group-find-parameter group))
3852       (and (consp elem)                 ; Has to be a cons.
3853            (consp (cdr elem))           ; The cdr has to be a list.
3854            (symbolp (car elem))         ; Has to be a symbol in there.
3855            (not (memq (car elem) vars))
3856            (ignore-errors
3857              (push (car elem) vars)
3858              ;; Variables like `gnus-show-threads' that are globally
3859              ;; bound, if used as group parameters, need to get to be
3860              ;; buffer-local, whereas just parameters like `gcc-self',
3861              ;; `timestamp', etc. should not be bound as variables.
3862              (if (boundp (car elem))
3863                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3864                (eval (nth 1 elem))))))))
3865
3866 (defun gnus-summary-read-group (group &optional show-all no-article
3867                                       kill-buffer no-display backward
3868                                       select-articles)
3869   "Start reading news in newsgroup GROUP.
3870 If SHOW-ALL is non-nil, already read articles are also listed.
3871 If NO-ARTICLE is non-nil, no article is selected initially.
3872 If NO-DISPLAY, don't generate a summary buffer."
3873   (let (result)
3874     (while (and group
3875                 (null (setq result
3876                             (let ((gnus-auto-select-next nil))
3877                               (or (gnus-summary-read-group-1
3878                                    group show-all no-article
3879                                    kill-buffer no-display
3880                                    select-articles)
3881                                   (setq show-all nil
3882                                         select-articles nil)))))
3883                 (eq gnus-auto-select-next 'quietly))
3884       (set-buffer gnus-group-buffer)
3885       ;; The entry function called above goes to the next
3886       ;; group automatically, so we go two groups back
3887       ;; if we are searching for the previous group.
3888       (when backward
3889         (gnus-group-prev-unread-group 2))
3890       (if (not (equal group (gnus-group-group-name)))
3891           (setq group (gnus-group-group-name))
3892         (setq group nil)))
3893     result))
3894
3895 (defun gnus-summary-read-group-1 (group show-all no-article
3896                                         kill-buffer no-display
3897                                         &optional select-articles)
3898   ;; Killed foreign groups can't be entered.
3899   ;;  (when (and (not (gnus-group-native-p group))
3900   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3901   ;;    (error "Dead non-native groups can't be entered"))
3902   (gnus-message 5 "Retrieving newsgroup: %s..."
3903                 (gnus-group-decoded-name group))
3904   (let* ((new-group (gnus-summary-setup-buffer group))
3905          (quit-config (gnus-group-quit-config group))
3906          (did-select (and new-group (gnus-select-newsgroup
3907                                      group show-all select-articles))))
3908     (cond
3909      ;; This summary buffer exists already, so we just select it.
3910      ((not new-group)
3911       (gnus-set-global-variables)
3912       (when kill-buffer
3913         (gnus-kill-or-deaden-summary kill-buffer))
3914       (gnus-configure-windows 'summary 'force)
3915       (gnus-set-mode-line 'summary)
3916       (gnus-summary-position-point)
3917       (message "")
3918       t)
3919      ;; We couldn't select this group.
3920      ((null did-select)
3921       (when (and (eq major-mode 'gnus-summary-mode)
3922                  (not (equal (current-buffer) kill-buffer)))
3923         (kill-buffer (current-buffer))
3924         (if (not quit-config)
3925             (progn
3926               ;; Update the info -- marks might need to be removed,
3927               ;; for instance.
3928               (gnus-summary-update-info)
3929               (set-buffer gnus-group-buffer)
3930               (gnus-group-jump-to-group group)
3931               (gnus-group-next-unread-group 1))
3932           (gnus-handle-ephemeral-exit quit-config)))
3933       (let ((grpinfo (gnus-get-info group)))
3934         (if (null (gnus-info-read grpinfo))
3935             (gnus-message 3 "Group %s contains no messages"
3936                           (gnus-group-decoded-name group))
3937           (gnus-message 3 "Can't select group")))
3938       nil)
3939      ;; The user did a `C-g' while prompting for number of articles,
3940      ;; so we exit this group.
3941      ((eq did-select 'quit)
3942       (and (eq major-mode 'gnus-summary-mode)
3943            (not (equal (current-buffer) kill-buffer))
3944            (kill-buffer (current-buffer)))
3945       (when kill-buffer
3946         (gnus-kill-or-deaden-summary kill-buffer))
3947       (if (not quit-config)
3948           (progn
3949             (set-buffer gnus-group-buffer)
3950             (gnus-group-jump-to-group group)
3951             (gnus-configure-windows 'group 'force))
3952         (gnus-handle-ephemeral-exit quit-config))
3953       ;; Finally signal the quit.
3954       (signal 'quit nil))
3955      ;; The group was successfully selected.
3956      (t
3957       (gnus-set-global-variables)
3958       ;; Save the active value in effect when the group was entered.
3959       (setq gnus-newsgroup-active
3960             (gnus-copy-sequence
3961              (gnus-active gnus-newsgroup-name)))
3962       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3963       ;; You can change the summary buffer in some way with this hook.
3964       (gnus-run-hooks 'gnus-select-group-hook)
3965       (when (memq 'summary (gnus-update-format-specifications
3966                             nil 'summary 'summary-mode 'summary-dummy))
3967         ;; The format specification for the summary line was updated,
3968         ;; so we need to update the mark positions as well.
3969         (gnus-update-summary-mark-positions))
3970       ;; Do score processing.
3971       (when gnus-use-scoring
3972         (gnus-possibly-score-headers))
3973       ;; Check whether to fill in the gaps in the threads.
3974       (when gnus-build-sparse-threads
3975         (gnus-build-sparse-threads))
3976       ;; Find the initial limit.
3977       (if show-all
3978           (let ((gnus-newsgroup-dormant nil))
3979             (gnus-summary-initial-limit show-all))
3980         (gnus-summary-initial-limit show-all))
3981       ;; Generate the summary buffer.
3982       (unless no-display
3983         (gnus-summary-prepare))
3984       (when gnus-use-trees
3985         (gnus-tree-open group)
3986         (setq gnus-summary-highlight-line-function
3987               'gnus-tree-highlight-article))
3988       ;; If the summary buffer is empty, but there are some low-scored
3989       ;; articles or some excluded dormants, we include these in the
3990       ;; buffer.
3991       (when (and (zerop (buffer-size))
3992                  (not no-display))
3993         (cond (gnus-newsgroup-dormant
3994                (gnus-summary-limit-include-dormant))
3995               ((and gnus-newsgroup-scored show-all)
3996                (gnus-summary-limit-include-expunged t))))
3997       ;; Function `gnus-apply-kill-file' must be called in this hook.
3998       (gnus-run-hooks 'gnus-apply-kill-hook)
3999       (if (and (zerop (buffer-size))
4000                (not no-display))
4001           (progn
4002             ;; This newsgroup is empty.
4003             (gnus-summary-catchup-and-exit nil t)
4004             (gnus-message 6 "No unread news")
4005             (when kill-buffer
4006               (gnus-kill-or-deaden-summary kill-buffer))
4007             ;; Return nil from this function.
4008             nil)
4009         ;; Hide conversation thread subtrees.  We cannot do this in
4010         ;; gnus-summary-prepare-hook since kill processing may not
4011         ;; work with hidden articles.
4012         (gnus-summary-maybe-hide-threads)
4013         (when kill-buffer
4014           (gnus-kill-or-deaden-summary kill-buffer))
4015         (gnus-summary-auto-select-subject)
4016         ;; Show first unread article if requested.
4017         (if (and (not no-article)
4018                  (not no-display)
4019                  gnus-newsgroup-unreads
4020                  gnus-auto-select-first)
4021             (progn
4022               (gnus-configure-windows 'summary)
4023               (let ((art (gnus-summary-article-number)))
4024                 (unless (and (not gnus-plugged)
4025                              (or (memq art gnus-newsgroup-undownloaded)
4026                                  (memq art gnus-newsgroup-downloadable)))
4027                   (gnus-summary-goto-article art))))
4028           ;; Don't select any articles.
4029           (gnus-summary-position-point)
4030           (gnus-configure-windows 'summary 'force)
4031           (gnus-set-mode-line 'summary))
4032         (when (and gnus-auto-center-group
4033                    (get-buffer-window gnus-group-buffer t))
4034           ;; Gotta use windows, because recenter does weird stuff if
4035           ;; the current buffer ain't the displayed window.
4036           (let ((owin (selected-window)))
4037             (select-window (get-buffer-window gnus-group-buffer t))
4038             (when (gnus-group-goto-group group)
4039               (recenter))
4040             (select-window owin)))
4041         ;; Mark this buffer as "prepared".
4042         (setq gnus-newsgroup-prepared t)
4043         (gnus-run-hooks 'gnus-summary-prepared-hook)
4044         (unless (gnus-ephemeral-group-p group)
4045           (gnus-group-update-group group))
4046         t)))))
4047
4048 (defun gnus-summary-auto-select-subject ()
4049   "Select the subject line on initial group entry."
4050   (goto-char (point-min))
4051   (cond
4052    ((eq gnus-auto-select-subject 'best)
4053     (gnus-summary-best-unread-subject))
4054    ((eq gnus-auto-select-subject 'unread)
4055     (gnus-summary-first-unread-subject))
4056    ((eq gnus-auto-select-subject 'unseen)
4057     (gnus-summary-first-unseen-subject))
4058    ((eq gnus-auto-select-subject 'unseen-or-unread)
4059     (gnus-summary-first-unseen-or-unread-subject))
4060    ((eq gnus-auto-select-subject 'first)
4061     ;; Do nothing.
4062     )
4063    ((functionp gnus-auto-select-subject)
4064     (funcall gnus-auto-select-subject))))
4065
4066 (defun gnus-summary-prepare ()
4067   "Generate the summary buffer."
4068   (interactive)
4069   (let ((inhibit-read-only t))
4070     (erase-buffer)
4071     (setq gnus-newsgroup-data nil
4072           gnus-newsgroup-data-reverse nil)
4073     (gnus-run-hooks 'gnus-summary-generate-hook)
4074     ;; Generate the buffer, either with threads or without.
4075     (when gnus-newsgroup-headers
4076       (gnus-summary-prepare-threads
4077        (if gnus-show-threads
4078            (gnus-sort-gathered-threads
4079             (funcall gnus-summary-thread-gathering-function
4080                      (gnus-sort-threads
4081                       (gnus-cut-threads (gnus-make-threads)))))
4082          ;; Unthreaded display.
4083          (gnus-sort-articles gnus-newsgroup-headers))))
4084     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4085     ;; Call hooks for modifying summary buffer.
4086     (goto-char (point-min))
4087     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4088
4089 (defsubst gnus-general-simplify-subject (subject)
4090   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4091   (setq subject
4092         (cond
4093          ;; Truncate the subject.
4094          (gnus-simplify-subject-functions
4095           (gnus-map-function gnus-simplify-subject-functions subject))
4096          ((numberp gnus-summary-gather-subject-limit)
4097           (setq subject (gnus-simplify-subject-re subject))
4098           (if (> (length subject) gnus-summary-gather-subject-limit)
4099               (substring subject 0 gnus-summary-gather-subject-limit)
4100             subject))
4101          ;; Fuzzily simplify it.
4102          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4103           (gnus-simplify-subject-fuzzy subject))
4104          ;; Just remove the leading "Re:".
4105          (t
4106           (gnus-simplify-subject-re subject))))
4107
4108   (if (and gnus-summary-gather-exclude-subject
4109            (string-match gnus-summary-gather-exclude-subject subject))
4110       nil                         ; This article shouldn't be gathered
4111     subject))
4112
4113 (defun gnus-summary-simplify-subject-query ()
4114   "Query where the respool algorithm would put this article."
4115   (interactive)
4116   (gnus-summary-select-article)
4117   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4118
4119 (defun gnus-gather-threads-by-subject (threads)
4120   "Gather threads by looking at Subject headers."
4121   (if (not gnus-summary-make-false-root)
4122       threads
4123     (let ((hashtb (gnus-make-hashtable 1024))
4124           (prev threads)
4125           (result threads)
4126           subject hthread whole-subject)
4127       (while threads
4128         (setq subject (gnus-general-simplify-subject
4129                        (setq whole-subject (mail-header-subject
4130                                             (caar threads)))))
4131         (when subject
4132           (if (setq hthread (gnus-gethash subject hashtb))
4133               (progn
4134                 ;; We enter a dummy root into the thread, if we
4135                 ;; haven't done that already.
4136                 (unless (stringp (caar hthread))
4137                   (setcar hthread (list whole-subject (car hthread))))
4138                 ;; We add this new gathered thread to this gathered
4139                 ;; thread.
4140                 (setcdr (car hthread)
4141                         (nconc (cdar hthread) (list (car threads))))
4142                 ;; Remove it from the list of threads.
4143                 (setcdr prev (cdr threads))
4144                 (setq threads prev))
4145             ;; Enter this thread into the hash table.
4146             (gnus-sethash subject
4147                           (if gnus-summary-make-false-root-always
4148                               (progn
4149                                 ;; If you want a dummy root above all
4150                                 ;; threads...
4151                                 (setcar threads (list whole-subject
4152                                                       (car threads)))
4153                                 threads)
4154                             threads)
4155                           hashtb)))
4156         (setq prev threads)
4157         (setq threads (cdr threads)))
4158       result)))
4159
4160 (defun gnus-gather-threads-by-references (threads)
4161   "Gather threads by looking at References headers."
4162   (let ((idhashtb (gnus-make-hashtable 1024))
4163         (thhashtb (gnus-make-hashtable 1024))
4164         (prev threads)
4165         (result threads)
4166         ids references id gthread gid entered ref)
4167     (while threads
4168       (when (setq references (mail-header-references (caar threads)))
4169         (setq id (mail-header-id (caar threads))
4170               ids (inline (gnus-split-references references))
4171               entered nil)
4172         (while (setq ref (pop ids))
4173           (setq ids (delete ref ids))
4174           (if (not (setq gid (gnus-gethash ref idhashtb)))
4175               (progn
4176                 (gnus-sethash ref id idhashtb)
4177                 (gnus-sethash id threads thhashtb))
4178             (setq gthread (gnus-gethash gid thhashtb))
4179             (unless entered
4180               ;; We enter a dummy root into the thread, if we
4181               ;; haven't done that already.
4182               (unless (stringp (caar gthread))
4183                 (setcar gthread (list (mail-header-subject (caar gthread))
4184                                       (car gthread))))
4185               ;; We add this new gathered thread to this gathered
4186               ;; thread.
4187               (setcdr (car gthread)
4188                       (nconc (cdar gthread) (list (car threads)))))
4189             ;; Add it into the thread hash table.
4190             (gnus-sethash id gthread thhashtb)
4191             (setq entered t)
4192             ;; Remove it from the list of threads.
4193             (setcdr prev (cdr threads))
4194             (setq threads prev))))
4195       (setq prev threads)
4196       (setq threads (cdr threads)))
4197     result))
4198
4199 (defun gnus-sort-gathered-threads (threads)
4200   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4201   (let ((result threads))
4202     (while threads
4203       (when (stringp (caar threads))
4204         (setcdr (car threads)
4205                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4206       (setq threads (cdr threads)))
4207     result))
4208
4209 (defun gnus-thread-loop-p (root thread)
4210   "Say whether ROOT is in THREAD."
4211   (let ((stack (list thread))
4212         (infloop 0)
4213         th)
4214     (while (setq thread (pop stack))
4215       (setq th (cdr thread))
4216       (while (and th
4217                   (not (eq (caar th) root)))
4218         (pop th))
4219       (if th
4220           ;; We have found a loop.
4221           (let (ref-dep)
4222             (setcdr thread (delq (car th) (cdr thread)))
4223             (if (boundp (setq ref-dep (intern "none"
4224                                               gnus-newsgroup-dependencies)))
4225                 (setcdr (symbol-value ref-dep)
4226                         (nconc (cdr (symbol-value ref-dep))
4227                                (list (car th))))
4228               (set ref-dep (list nil (car th))))
4229             (setq infloop 1
4230                   stack nil))
4231         ;; Push all the subthreads onto the stack.
4232         (push (cdr thread) stack)))
4233     infloop))
4234
4235 (defun gnus-make-threads ()
4236   "Go through the dependency hashtb and find the roots.  Return all threads."
4237   (let (threads)
4238     (while (catch 'infloop
4239              (mapatoms
4240               (lambda (refs)
4241                 ;; Deal with self-referencing References loops.
4242                 (when (and (car (symbol-value refs))
4243                            (not (zerop
4244                                  (apply
4245                                   '+
4246                                   (mapcar
4247                                    (lambda (thread)
4248                                      (gnus-thread-loop-p
4249                                       (car (symbol-value refs)) thread))
4250                                    (cdr (symbol-value refs)))))))
4251                   (setq threads nil)
4252                   (throw 'infloop t))
4253                 (unless (car (symbol-value refs))
4254                   ;; These threads do not refer back to any other
4255                   ;; articles, so they're roots.
4256                   (setq threads (append (cdr (symbol-value refs)) threads))))
4257               gnus-newsgroup-dependencies)))
4258     threads))
4259
4260 ;; Build the thread tree.
4261 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4262   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4263
4264 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4265 if it was already present.
4266
4267 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4268 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4269 Message-IDs will be renamed to a unique Message-ID before being
4270 entered.
4271
4272 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4273   (let* ((id (mail-header-id header))
4274          (id-dep (and id (intern id dependencies)))
4275          parent-id ref ref-dep ref-header replaced)
4276     ;; Enter this `header' in the `dependencies' table.
4277     (cond
4278      ((not id-dep)
4279       (setq header nil))
4280      ;; The first two cases do the normal part: enter a new `header'
4281      ;; in the `dependencies' table.
4282      ((not (boundp id-dep))
4283       (set id-dep (list header)))
4284      ((null (car (symbol-value id-dep)))
4285       (setcar (symbol-value id-dep) header))
4286
4287      ;; From here the `header' was already present in the
4288      ;; `dependencies' table.
4289      (force-new
4290       ;; Overrides an existing entry;
4291       ;; just set the header part of the entry.
4292       (setcar (symbol-value id-dep) header)
4293       (setq replaced t))
4294
4295      ;; Renames the existing `header' to a unique Message-ID.
4296      ((not gnus-summary-ignore-duplicates)
4297       ;; An article with this Message-ID has already been seen.
4298       ;; We rename the Message-ID.
4299       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4300            (list header))
4301       (mail-header-set-id header id))
4302
4303      ;; The last case ignores an existing entry, except it adds any
4304      ;; additional Xrefs (in case the two articles came from different
4305      ;; servers.
4306      ;; Also sets `header' to `nil' meaning that the `dependencies'
4307      ;; table was *not* modified.
4308      (t
4309       (mail-header-set-xref
4310        (car (symbol-value id-dep))
4311        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4312                    "")
4313                (or (mail-header-xref header) "")))
4314       (setq header nil)))
4315
4316     (when (and header (not replaced))
4317       ;; First check that we are not creating a References loop.
4318       (setq parent-id (gnus-parent-id (mail-header-references header)))
4319       (setq ref parent-id)
4320       (while (and ref
4321                   (setq ref-dep (intern-soft ref dependencies))
4322                   (boundp ref-dep)
4323                   (setq ref-header (car (symbol-value ref-dep))))
4324         (if (string= id ref)
4325             ;; Yuk!  This is a reference loop.  Make the article be a
4326             ;; root article.
4327             (progn
4328               (mail-header-set-references (car (symbol-value id-dep)) "none")
4329               (setq ref nil)
4330               (setq parent-id nil))
4331           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4332       (setq ref-dep (intern (or parent-id "none") dependencies))
4333       (if (boundp ref-dep)
4334           (setcdr (symbol-value ref-dep)
4335                   (nconc (cdr (symbol-value ref-dep))
4336                          (list (symbol-value id-dep))))
4337         (set ref-dep (list nil (symbol-value id-dep)))))
4338     header))
4339
4340 (defun gnus-extract-message-id-from-in-reply-to (string)
4341   (if (string-match "<[^>]+>" string)
4342       (substring string (match-beginning 0) (match-end 0))
4343     nil))
4344
4345 (defun gnus-build-sparse-threads ()
4346   (let ((headers gnus-newsgroup-headers)
4347         (mail-parse-charset gnus-newsgroup-charset)
4348         (gnus-summary-ignore-duplicates t)
4349         header references generation relations
4350         subject child end new-child date)
4351     ;; First we create an alist of generations/relations, where
4352     ;; generations is how much we trust the relation, and the relation
4353     ;; is parent/child.
4354     (gnus-message 7 "Making sparse threads...")
4355     (save-excursion
4356       (nnheader-set-temp-buffer " *gnus sparse threads*")
4357       (while (setq header (pop headers))
4358         (when (and (setq references (mail-header-references header))
4359                    (not (string= references "")))
4360           (insert references)
4361           (setq child (mail-header-id header)
4362                 subject (mail-header-subject header)
4363                 date (mail-header-date header)
4364                 generation 0)
4365           (while (search-backward ">" nil t)
4366             (setq end (1+ (point)))
4367             (when (search-backward "<" nil t)
4368               (setq new-child (buffer-substring (point) end))
4369               (push (list (incf generation)
4370                           child (setq child new-child)
4371                           subject date)
4372                     relations)))
4373           (when child
4374             (push (list (1+ generation) child nil subject) relations))
4375           (erase-buffer)))
4376       (kill-buffer (current-buffer)))
4377     ;; Sort over trustworthiness.
4378     (dolist (relation (sort relations 'car-less-than-car))
4379       (when (gnus-dependencies-add-header
4380              (make-full-mail-header
4381               gnus-reffed-article-number
4382               (nth 3 relation) "" (or (nth 4 relation) "")
4383               (nth 1 relation)
4384               (or (nth 2 relation) "") 0 0 "")
4385              gnus-newsgroup-dependencies nil)
4386         (push gnus-reffed-article-number gnus-newsgroup-limit)
4387         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4388         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4389               gnus-newsgroup-reads)
4390         (decf gnus-reffed-article-number)))
4391     (gnus-message 7 "Making sparse threads...done")))
4392
4393 (defun gnus-build-old-threads ()
4394   ;; Look at all the articles that refer back to old articles, and
4395   ;; fetch the headers for the articles that aren't there.  This will
4396   ;; build complete threads - if the roots haven't been expired by the
4397   ;; server, that is.
4398   (let ((mail-parse-charset gnus-newsgroup-charset)
4399         id heads)
4400     (mapatoms
4401      (lambda (refs)
4402        (when (not (car (symbol-value refs)))
4403          (setq heads (cdr (symbol-value refs)))
4404          (while heads
4405            (if (memq (mail-header-number (caar heads))
4406                      gnus-newsgroup-dormant)
4407                (setq heads (cdr heads))
4408              (setq id (symbol-name refs))
4409              (while (and (setq id (gnus-build-get-header id))
4410                          (not (car (gnus-id-to-thread id)))))
4411              (setq heads nil)))))
4412      gnus-newsgroup-dependencies)))
4413
4414 (defsubst gnus-remove-odd-characters (string)
4415   "Translate STRING into something that doesn't contain weird characters."
4416   (mm-subst-char-in-string
4417    ?\r ?\-
4418    (mm-subst-char-in-string ?\n ?\- string t) t))
4419
4420 ;; This function has to be called with point after the article number
4421 ;; on the beginning of the line.
4422 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4423   (let ((eol (point-at-eol))
4424         (buffer (current-buffer))
4425         header references in-reply-to)
4426
4427     ;; overview: [num subject from date id refs chars lines misc]
4428     (unwind-protect
4429         (let (x)
4430           (narrow-to-region (point) eol)
4431           (unless (eobp)
4432             (forward-char))
4433
4434           (setq header
4435                 (make-full-mail-header
4436                  number                 ; number
4437                  (condition-case ()     ; subject
4438                      (gnus-remove-odd-characters
4439                       (funcall gnus-decode-encoded-word-function
4440                                (setq x (nnheader-nov-field))))
4441                    (error x))
4442                  (condition-case ()     ; from
4443                      (gnus-remove-odd-characters
4444                       (funcall gnus-decode-encoded-address-function
4445                                (setq x (nnheader-nov-field))))
4446                    (error x))
4447                  (nnheader-nov-field)   ; date
4448                  (nnheader-nov-read-message-id number)  ; id
4449                  (setq references (nnheader-nov-field)) ; refs
4450                  (nnheader-nov-read-integer) ; chars
4451                  (nnheader-nov-read-integer) ; lines
4452                  (unless (eobp)
4453                    (if (looking-at "Xref: ")
4454                        (goto-char (match-end 0)))
4455                    (nnheader-nov-field)) ; Xref
4456                  (nnheader-nov-parse-extra)))) ; extra
4457
4458       (widen))
4459
4460     (when (and (string= references "")
4461                (setq in-reply-to (mail-header-extra header))
4462                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4463       (mail-header-set-references
4464        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4465
4466     (when gnus-alter-header-function
4467       (funcall gnus-alter-header-function header))
4468     (gnus-dependencies-add-header header dependencies force-new)))
4469
4470 (defun gnus-build-get-header (id)
4471   "Look through the buffer of NOV lines and find the header to ID.
4472 Enter this line into the dependencies hash table, and return
4473 the id of the parent article (if any)."
4474   (let ((deps gnus-newsgroup-dependencies)
4475         found header)
4476     (prog1
4477         (with-current-buffer nntp-server-buffer
4478           (let ((case-fold-search nil))
4479             (goto-char (point-min))
4480             (while (and (not found)
4481                         (search-forward id nil t))
4482               (beginning-of-line)
4483               (setq found (looking-at
4484                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4485                                    (regexp-quote id))))
4486               (or found (beginning-of-line 2)))
4487             (when found
4488               (beginning-of-line)
4489               (and
4490                (setq header (gnus-nov-parse-line
4491                              (read (current-buffer)) deps))
4492                (gnus-parent-id (mail-header-references header))))))
4493       (when header
4494         (let ((number (mail-header-number header)))
4495           (push number gnus-newsgroup-limit)
4496           (push header gnus-newsgroup-headers)
4497           (if (memq number gnus-newsgroup-unselected)
4498               (progn
4499                 (setq gnus-newsgroup-unreads
4500                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4501                                                number))
4502                 (setq gnus-newsgroup-unselected
4503                       (delq number gnus-newsgroup-unselected)))
4504             (push number gnus-newsgroup-ancient)))))))
4505
4506 (defun gnus-build-all-threads ()
4507   "Read all the headers."
4508   (let ((gnus-summary-ignore-duplicates t)
4509         (mail-parse-charset gnus-newsgroup-charset)
4510         (dependencies gnus-newsgroup-dependencies)
4511         header article)
4512     (with-current-buffer nntp-server-buffer
4513       (let ((case-fold-search nil))
4514         (goto-char (point-min))
4515         (while (not (eobp))
4516           (ignore-errors
4517             (setq article (read (current-buffer))
4518                   header (gnus-nov-parse-line article dependencies)))
4519           (when header
4520             (with-current-buffer gnus-summary-buffer
4521               (push header gnus-newsgroup-headers)
4522               (if (memq (setq article (mail-header-number header))
4523                         gnus-newsgroup-unselected)
4524                   (progn
4525                     (setq gnus-newsgroup-unreads
4526                           (gnus-add-to-sorted-list
4527                            gnus-newsgroup-unreads article))
4528                     (setq gnus-newsgroup-unselected
4529                           (delq article gnus-newsgroup-unselected)))
4530                 (push article gnus-newsgroup-ancient)))
4531             (forward-line 1)))))))
4532
4533 (defun gnus-summary-update-article-line (article header)
4534   "Update the line for ARTICLE using HEADER."
4535   (let* ((id (mail-header-id header))
4536          (thread (gnus-id-to-thread id)))
4537     (unless thread
4538       (error "Article in no thread"))
4539     ;; Update the thread.
4540     (setcar thread header)
4541     (gnus-summary-goto-subject article)
4542     (let* ((datal (gnus-data-find-list article))
4543            (data (car datal))
4544            (inhibit-read-only t)
4545            (level (gnus-summary-thread-level)))
4546       (gnus-delete-line)
4547       (let ((inserted (- (point)
4548                          (progn
4549                            (gnus-summary-insert-line
4550                             header level nil
4551                             (memq article gnus-newsgroup-undownloaded)
4552                             (gnus-article-mark article)
4553                             (memq article gnus-newsgroup-replied)
4554                             (memq article gnus-newsgroup-expirable)
4555                             ;; Only insert the Subject string when it's different
4556                             ;; from the previous Subject string.
4557                             (if (and
4558                                  gnus-show-threads
4559                                  (gnus-subject-equal
4560                                   (condition-case ()
4561                                       (mail-header-subject
4562                                        (gnus-data-header
4563                                         (cadr
4564                                          (gnus-data-find-list
4565                                           article
4566                                           (gnus-data-list t)))))
4567                                     ;; Error on the side of excessive subjects.
4568                                     (error ""))
4569                                   (mail-header-subject header)))
4570                                 ""
4571                               (mail-header-subject header))
4572                             nil (cdr (assq article gnus-newsgroup-scored))
4573                             (memq article gnus-newsgroup-processable))
4574                            (point)))))
4575         (when (cdr datal)
4576           (gnus-data-update-list
4577            (cdr datal)
4578            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4579
4580 (defun gnus-summary-update-article (article &optional iheader)
4581   "Update ARTICLE in the summary buffer."
4582   (set-buffer gnus-summary-buffer)
4583   (let* ((header (gnus-summary-article-header article))
4584          (id (mail-header-id header))
4585          (data (gnus-data-find article))
4586          (thread (gnus-id-to-thread id))
4587          (references (mail-header-references header))
4588          (parent
4589           (gnus-id-to-thread
4590            (or (gnus-parent-id
4591                 (when (and references
4592                            (not (equal "" references)))
4593                   references))
4594                "none")))
4595          (inhibit-read-only t)
4596          (old (car thread)))
4597     (when thread
4598       (unless iheader
4599         (setcar thread nil)
4600         (when parent
4601           (delq thread parent)))
4602       (if (gnus-summary-insert-subject id header)
4603           ;; Set the (possibly) new article number in the data structure.
4604           (gnus-data-set-number data (gnus-id-to-article id))
4605         (setcar thread old)
4606         nil))))
4607
4608 (defun gnus-rebuild-thread (id &optional line)
4609   "Rebuild the thread containing ID.
4610 If LINE, insert the rebuilt thread starting on line LINE."
4611   (let ((inhibit-read-only t)
4612         old-pos current thread data)
4613     (if (not gnus-show-threads)
4614         (setq thread (list (car (gnus-id-to-thread id))))
4615       ;; Get the thread this article is part of.
4616       (setq thread (gnus-remove-thread id)))
4617     (setq old-pos (point-at-bol))
4618     (setq current (save-excursion
4619                     (and (re-search-backward "[\r\n]" nil t)
4620                          (gnus-summary-article-number))))
4621     ;; If this is a gathered thread, we have to go some re-gathering.
4622     (when (stringp (car thread))
4623       (let ((subject (car thread))
4624             roots thr)
4625         (setq thread (cdr thread))
4626         (while thread
4627           (unless (memq (setq thr (gnus-id-to-thread
4628                                    (gnus-root-id
4629                                     (mail-header-id (caar thread)))))
4630                         roots)
4631             (push thr roots))
4632           (setq thread (cdr thread)))
4633         ;; We now have all (unique) roots.
4634         (if (= (length roots) 1)
4635             ;; All the loose roots are now one solid root.
4636             (setq thread (car roots))
4637           (setq thread (cons subject (gnus-sort-threads roots))))))
4638     (let (threads)
4639       ;; We then insert this thread into the summary buffer.
4640       (when line
4641         (goto-char (point-min))
4642         (forward-line (1- line)))
4643       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4644         (if gnus-show-threads
4645             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4646           (gnus-summary-prepare-unthreaded thread))
4647         (setq data (nreverse gnus-newsgroup-data))
4648         (setq threads gnus-newsgroup-threads))
4649       ;; We splice the new data into the data structure.
4650       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4651       ;;!!! then we want to insert at the beginning of the buffer.
4652       ;;!!! That happens to be true with Gnus now, but that may
4653       ;;!!! change in the future.  Perhaps.
4654       (gnus-data-enter-list
4655        (if line nil current) data (- (point) old-pos))
4656       (setq gnus-newsgroup-threads
4657             (nconc threads gnus-newsgroup-threads))
4658       (gnus-data-compute-positions))))
4659
4660 (defun gnus-number-to-header (number)
4661   "Return the header for article NUMBER."
4662   (let ((headers gnus-newsgroup-headers))
4663     (while (and headers
4664                 (not (= number (mail-header-number (car headers)))))
4665       (pop headers))
4666     (when headers
4667       (car headers))))
4668
4669 (defun gnus-parent-headers (in-headers &optional generation)
4670   "Return the headers of the GENERATIONeth parent of HEADERS."
4671   (unless generation
4672     (setq generation 1))
4673   (let ((parent t)
4674         (headers in-headers)
4675         references)
4676     (while (and parent
4677                 (not (zerop generation))
4678                 (setq references (mail-header-references headers)))
4679       (setq headers (if (and references
4680                              (setq parent (gnus-parent-id references)))
4681                         (car (gnus-id-to-thread parent))
4682                       nil))
4683       (decf generation))
4684     (and (not (eq headers in-headers))
4685          headers)))
4686
4687 (defun gnus-id-to-thread (id)
4688   "Return the (sub-)thread where ID appears."
4689   (gnus-gethash id gnus-newsgroup-dependencies))
4690
4691 (defun gnus-id-to-article (id)
4692   "Return the article number of ID."
4693   (let ((thread (gnus-id-to-thread id)))
4694     (when (and thread
4695                (car thread))
4696       (mail-header-number (car thread)))))
4697
4698 (defun gnus-id-to-header (id)
4699   "Return the article headers of ID."
4700   (car (gnus-id-to-thread id)))
4701
4702 (defun gnus-article-displayed-root-p (article)
4703   "Say whether ARTICLE is a root(ish) article."
4704   (let ((level (gnus-summary-thread-level article))
4705         (refs (mail-header-references  (gnus-summary-article-header article)))
4706         particle)
4707     (cond
4708      ((null level) nil)
4709      ((zerop level) t)
4710      ((null refs) t)
4711      ((null (gnus-parent-id refs)) t)
4712      ((and (= 1 level)
4713            (null (setq particle (gnus-id-to-article
4714                                  (gnus-parent-id refs))))
4715            (null (gnus-summary-thread-level particle)))))))
4716
4717 (defun gnus-root-id (id)
4718   "Return the id of the root of the thread where ID appears."
4719   (let (last-id prev)
4720     (while (and id (setq prev (car (gnus-id-to-thread id))))
4721       (setq last-id id
4722             id (gnus-parent-id (mail-header-references prev))))
4723     last-id))
4724
4725 (defun gnus-articles-in-thread (thread)
4726   "Return the list of articles in THREAD."
4727   (cons (mail-header-number (car thread))
4728         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4729
4730 (defun gnus-remove-thread (id &optional dont-remove)
4731   "Remove the thread that has ID in it."
4732   (let (headers thread last-id)
4733     ;; First go up in this thread until we find the root.
4734     (setq last-id (gnus-root-id id)
4735           headers (message-flatten-list (gnus-id-to-thread last-id)))
4736     ;; We have now found the real root of this thread.  It might have
4737     ;; been gathered into some loose thread, so we have to search
4738     ;; through the threads to find the thread we wanted.
4739     (let ((threads gnus-newsgroup-threads)
4740           sub)
4741       (while threads
4742         (setq sub (car threads))
4743         (if (stringp (car sub))
4744             ;; This is a gathered thread, so we look at the roots
4745             ;; below it to find whether this article is in this
4746             ;; gathered root.
4747             (progn
4748               (setq sub (cdr sub))
4749               (while sub
4750                 (when (member (caar sub) headers)
4751                   (setq thread (car threads)
4752                         threads nil
4753                         sub nil))
4754                 (setq sub (cdr sub))))
4755           ;; It's an ordinary thread, so we check it.
4756           (when (eq (car sub) (car headers))
4757             (setq thread sub
4758                   threads nil)))
4759         (setq threads (cdr threads)))
4760       ;; If this article is in no thread, then it's a root.
4761       (if thread
4762           (unless dont-remove
4763             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4764         (setq thread (gnus-id-to-thread last-id)))
4765       (when thread
4766         (prog1
4767             thread                      ; We return this thread.
4768           (unless dont-remove
4769             (if (stringp (car thread))
4770                 (progn
4771                   ;; If we use dummy roots, then we have to remove the
4772                   ;; dummy root as well.
4773                   (when (eq gnus-summary-make-false-root 'dummy)
4774                     ;; We go to the dummy root by going to
4775                     ;; the first sub-"thread", and then one line up.
4776                     (gnus-summary-goto-article
4777                      (mail-header-number (caadr thread)))
4778                     (forward-line -1)
4779                     (gnus-delete-line)
4780                     (gnus-data-compute-positions))
4781                   (setq thread (cdr thread))
4782                   (while thread
4783                     (gnus-remove-thread-1 (car thread))
4784                     (setq thread (cdr thread))))
4785               (gnus-remove-thread-1 thread))))))))
4786
4787 (defun gnus-remove-thread-1 (thread)
4788   "Remove the thread THREAD recursively."
4789   (let ((number (mail-header-number (pop thread)))
4790         d)
4791     (setq thread (reverse thread))
4792     (while thread
4793       (gnus-remove-thread-1 (pop thread)))
4794     (when (setq d (gnus-data-find number))
4795       (goto-char (gnus-data-pos d))
4796       (gnus-summary-show-thread)
4797       (gnus-data-remove
4798        number
4799        (- (point-at-bol)
4800           (prog1
4801               (1+ (point-at-eol))
4802             (gnus-delete-line)))))))
4803
4804 (defun gnus-sort-threads-recursive (threads func)
4805   (sort (mapcar (lambda (thread)
4806                   (cons (car thread)
4807                         (and (cdr thread)
4808                              (gnus-sort-threads-recursive (cdr thread) func))))
4809                 threads) func))
4810
4811 (defun gnus-sort-threads-loop (threads func)
4812   (let* ((superthread (cons nil threads))
4813          (stack (list (cons superthread threads)))
4814          remaining-threads thread)
4815     (while stack
4816       (setq remaining-threads (cdr (car stack)))
4817       (if remaining-threads
4818           (progn (setq thread (car remaining-threads))
4819                  (setcdr (car stack) (cdr remaining-threads))
4820                  (if (cdr thread)
4821                      (push (cons thread (cdr thread)) stack)))
4822         (setq thread (caar stack))
4823         (setcdr thread (sort (cdr thread) func))
4824         (pop stack)))
4825     (cdr superthread)))
4826
4827 (defun gnus-sort-threads (threads)
4828   "Sort THREADS."
4829   (if (not gnus-thread-sort-functions)
4830       threads
4831     (gnus-message 8 "Sorting threads...")
4832     (prog1
4833         (condition-case nil
4834             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4835               (gnus-sort-threads-recursive
4836                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4837           ;; Even after binding max-lisp-eval-depth, the recursive
4838           ;; sorter might fail for very long threads.  In that case,
4839           ;; try using a (less well-tested) non-recursive sorter.
4840           (error (gnus-message 9 "Sorting threads with loop...")
4841                  (gnus-sort-threads-loop
4842                   threads (gnus-make-sort-function
4843                            gnus-thread-sort-functions))))
4844       (gnus-message 8 "Sorting threads...done"))))
4845
4846 (defun gnus-sort-articles (articles)
4847   "Sort ARTICLES."
4848   (when gnus-article-sort-functions
4849     (gnus-message 7 "Sorting articles...")
4850     (prog1
4851         (setq gnus-newsgroup-headers
4852               (sort articles (gnus-make-sort-function
4853                               gnus-article-sort-functions)))
4854       (gnus-message 7 "Sorting articles...done"))))
4855
4856 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4857 (defmacro gnus-thread-header (thread)
4858   "Return header of first article in THREAD.
4859 Note that THREAD must never, ever be anything else than a variable -
4860 using some other form will lead to serious barfage."
4861   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4862   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4863   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4864         (vector thread) 2))
4865
4866 (defsubst gnus-article-sort-by-number (h1 h2)
4867   "Sort articles by article number."
4868   (< (mail-header-number h1)
4869      (mail-header-number h2)))
4870
4871 (defun gnus-thread-sort-by-number (h1 h2)
4872   "Sort threads by root article number."
4873   (gnus-article-sort-by-number
4874    (gnus-thread-header h1) (gnus-thread-header h2)))
4875
4876 (defsubst gnus-article-sort-by-random (h1 h2)
4877   "Sort articles randomly."
4878   (zerop (random 2)))
4879
4880 (defun gnus-thread-sort-by-random (h1 h2)
4881   "Sort threads randomly."
4882   (gnus-article-sort-by-random
4883    (gnus-thread-header h1) (gnus-thread-header h2)))
4884
4885 (defsubst gnus-article-sort-by-lines (h1 h2)
4886   "Sort articles by article Lines header."
4887   (< (mail-header-lines h1)
4888      (mail-header-lines h2)))
4889
4890 (defun gnus-thread-sort-by-lines (h1 h2)
4891   "Sort threads by root article Lines header."
4892   (gnus-article-sort-by-lines
4893    (gnus-thread-header h1) (gnus-thread-header h2)))
4894
4895 (defsubst gnus-article-sort-by-chars (h1 h2)
4896   "Sort articles by octet length."
4897   (< (mail-header-chars h1)
4898      (mail-header-chars h2)))
4899
4900 (defun gnus-thread-sort-by-chars (h1 h2)
4901   "Sort threads by root article octet length."
4902   (gnus-article-sort-by-chars
4903    (gnus-thread-header h1) (gnus-thread-header h2)))
4904
4905 (defsubst gnus-article-sort-by-author (h1 h2)
4906   "Sort articles by root author."
4907   (gnus-string<
4908    (let ((extract (funcall
4909                    gnus-extract-address-components
4910                    (mail-header-from h1))))
4911      (or (car extract) (cadr extract) ""))
4912    (let ((extract (funcall
4913                    gnus-extract-address-components
4914                    (mail-header-from h2))))
4915      (or (car extract) (cadr extract) ""))))
4916
4917 (defun gnus-thread-sort-by-author (h1 h2)
4918   "Sort threads by root author."
4919   (gnus-article-sort-by-author
4920    (gnus-thread-header h1)  (gnus-thread-header h2)))
4921
4922 (defsubst gnus-article-sort-by-recipient (h1 h2)
4923   "Sort articles by recipient."
4924   (gnus-string<
4925    (let ((extract (funcall
4926                    gnus-extract-address-components
4927                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4928      (or (car extract) (cadr extract)))
4929    (let ((extract (funcall
4930                    gnus-extract-address-components
4931                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4932      (or (car extract) (cadr extract)))))
4933
4934 (defun gnus-thread-sort-by-recipient (h1 h2)
4935   "Sort threads by root recipient."
4936   (gnus-article-sort-by-recipient
4937    (gnus-thread-header h1) (gnus-thread-header h2)))
4938
4939 (defsubst gnus-article-sort-by-subject (h1 h2)
4940   "Sort articles by root subject."
4941   (gnus-string<
4942    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4943    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4944
4945 (defun gnus-thread-sort-by-subject (h1 h2)
4946   "Sort threads by root subject."
4947   (gnus-article-sort-by-subject
4948    (gnus-thread-header h1) (gnus-thread-header h2)))
4949
4950 (defsubst gnus-article-sort-by-date (h1 h2)
4951   "Sort articles by root article date."
4952   (time-less-p
4953    (gnus-date-get-time (mail-header-date h1))
4954    (gnus-date-get-time (mail-header-date h2))))
4955
4956 (defun gnus-thread-sort-by-date (h1 h2)
4957   "Sort threads by root article date."
4958   (gnus-article-sort-by-date
4959    (gnus-thread-header h1) (gnus-thread-header h2)))
4960
4961 (defsubst gnus-article-sort-by-score (h1 h2)
4962   "Sort articles by root article score.
4963 Unscored articles will be counted as having a score of zero."
4964   (> (or (cdr (assq (mail-header-number h1)
4965                     gnus-newsgroup-scored))
4966          gnus-summary-default-score 0)
4967      (or (cdr (assq (mail-header-number h2)
4968                     gnus-newsgroup-scored))
4969          gnus-summary-default-score 0)))
4970
4971 (defun gnus-thread-sort-by-score (h1 h2)
4972   "Sort threads by root article score."
4973   (gnus-article-sort-by-score
4974    (gnus-thread-header h1) (gnus-thread-header h2)))
4975
4976 (defun gnus-thread-sort-by-total-score (h1 h2)
4977   "Sort threads by the sum of all scores in the thread.
4978 Unscored articles will be counted as having a score of zero."
4979   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4980
4981 (defun gnus-thread-total-score (thread)
4982   ;; This function find the total score of THREAD.
4983   (cond
4984    ((null thread)
4985     0)
4986    ((consp thread)
4987     (if (stringp (car thread))
4988         (apply gnus-thread-score-function 0
4989                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4990       (gnus-thread-total-score-1 thread)))
4991    (t
4992     (gnus-thread-total-score-1 (list thread)))))
4993
4994 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4995   "Sort threads such that the thread with the most recently arrived article comes first."
4996   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4997
4998 (defun gnus-thread-highest-number (thread)
4999   "Return the highest article number in THREAD."
5000   (apply 'max (mapcar (lambda (header)
5001                         (mail-header-number header))
5002                       (message-flatten-list thread))))
5003
5004 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5005   "Sort threads such that the thread with the most recently dated article comes first."
5006   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5007
5008 ; Since this is called not only to sort the top-level threads, but
5009 ; also in recursive sorts to order the articles within a thread, each
5010 ; article will be processed many times.  Thus it speeds things up
5011 ; quite a bit to use gnus-date-get-time, which caches the time value.
5012 (defun gnus-thread-latest-date (thread)
5013   "Return the highest article date in THREAD."
5014   (apply 'max
5015          (mapcar (lambda (header) (gnus-float-time
5016                                    (gnus-date-get-time
5017                                     (mail-header-date header))))
5018                  (message-flatten-list thread))))
5019
5020 (defun gnus-thread-total-score-1 (root)
5021   ;; This function find the total score of the thread below ROOT.
5022   (setq root (car root))
5023   (apply gnus-thread-score-function
5024          (or (append
5025               (mapcar 'gnus-thread-total-score
5026                       (cdr (gnus-id-to-thread (mail-header-id root))))
5027               (when (> (mail-header-number root) 0)
5028                 (list (or (cdr (assq (mail-header-number root)
5029                                      gnus-newsgroup-scored))
5030                           gnus-summary-default-score 0))))
5031              (list gnus-summary-default-score)
5032              '(0))))
5033
5034 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5035 (defvar gnus-tmp-prev-subject nil)
5036 (defvar gnus-tmp-false-parent nil)
5037 (defvar gnus-tmp-root-expunged nil)
5038 (defvar gnus-tmp-dummy-line nil)
5039
5040 (defun gnus-extra-header (type &optional header)
5041   "Return the extra header of TYPE."
5042   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5043       ""))
5044
5045 (defvar gnus-tmp-thread-tree-header-string "")
5046
5047 (defcustom gnus-sum-thread-tree-root "> "
5048   "With %B spec, used for the root of a thread.
5049 If nil, use subject instead."
5050   :version "22.1"
5051   :type '(radio (const :format "%v  " nil) string)
5052   :group 'gnus-thread)
5053
5054 (defcustom gnus-sum-thread-tree-false-root "> "
5055   "With %B spec, used for a false root of a thread.
5056 If nil, use subject instead."
5057   :version "22.1"
5058   :type '(radio (const :format "%v  " nil) string)
5059   :group 'gnus-thread)
5060
5061 (defcustom gnus-sum-thread-tree-single-indent ""
5062   "With %B spec, used for a thread with just one message.
5063 If nil, use subject instead."
5064   :version "22.1"
5065   :type '(radio (const :format "%v  " nil) string)
5066   :group 'gnus-thread)
5067
5068 (defcustom gnus-sum-thread-tree-vertical "| "
5069   "With %B spec, used for drawing a vertical line."
5070   :version "22.1"
5071   :type 'string
5072   :group 'gnus-thread)
5073
5074 (defcustom gnus-sum-thread-tree-indent "  "
5075   "With %B spec, used for indenting."
5076   :version "22.1"
5077   :type 'string
5078   :group 'gnus-thread)
5079
5080 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5081   "With %B spec, used for a leaf with brothers."
5082   :version "22.1"
5083   :type 'string
5084   :group 'gnus-thread)
5085
5086 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5087   "With %B spec, used for a leaf without brothers."
5088   :version "22.1"
5089   :type 'string
5090   :group 'gnus-thread)
5091
5092 (defcustom gnus-summary-display-while-building nil
5093   "If non-nil, show and update the summary buffer as it's being built.
5094 If the value is t, update the buffer after every line is inserted.  If
5095 the value is an integer (N), update the display every N lines."
5096   :version "22.1"
5097   :group 'gnus-thread
5098   :type '(choice (const :tag "off" nil)
5099                  number
5100                  (const :tag "frequently" t)))
5101
5102 (defun gnus-summary-prepare-threads (threads)
5103   "Prepare summary buffer from THREADS and indentation LEVEL.
5104 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5105 or a straight list of headers."
5106   (gnus-message 7 "Generating summary...")
5107
5108   (setq gnus-newsgroup-threads threads)
5109   (beginning-of-line)
5110
5111   (let ((gnus-tmp-level 0)
5112         (default-score (or gnus-summary-default-score 0))
5113         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5114         (building-line-count gnus-summary-display-while-building)
5115         (building-count (integerp gnus-summary-display-while-building))
5116         thread number subject stack state gnus-tmp-gathered beg-match
5117         new-roots gnus-tmp-new-adopts thread-end simp-subject
5118         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5119         gnus-tmp-replied gnus-tmp-subject-or-nil
5120         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5121         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5122         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5123         tree-stack)
5124
5125     (setq gnus-tmp-prev-subject nil
5126           gnus-tmp-thread-tree-header-string "")
5127
5128     (if (vectorp (car threads))
5129         ;; If this is a straight (sic) list of headers, then a
5130         ;; threaded summary display isn't required, so we just create
5131         ;; an unthreaded one.
5132         (gnus-summary-prepare-unthreaded threads)
5133
5134       ;; Do the threaded display.
5135
5136       (if gnus-summary-display-while-building
5137           (switch-to-buffer (buffer-name)))
5138       (while (or threads stack gnus-tmp-new-adopts new-roots)
5139
5140         (if (and (= gnus-tmp-level 0)
5141                  (or (not stack)
5142                      (= (caar stack) 0))
5143                  (not gnus-tmp-false-parent)
5144                  (or gnus-tmp-new-adopts new-roots))
5145             (if gnus-tmp-new-adopts
5146                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5147                       thread (list (car gnus-tmp-new-adopts))
5148                       gnus-tmp-header (caar thread)
5149                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5150               (when new-roots
5151                 (setq thread (list (car new-roots))
5152                       gnus-tmp-header (caar thread)
5153                       new-roots (cdr new-roots))))
5154
5155           (if threads
5156               ;; If there are some threads, we do them before the
5157               ;; threads on the stack.
5158               (setq thread threads
5159                     gnus-tmp-header (caar thread))
5160             ;; There were no current threads, so we pop something off
5161             ;; the stack.
5162             (setq state (car stack)
5163                   gnus-tmp-level (car state)
5164                   tree-stack (cadr state)
5165                   thread (caddr state)
5166                   stack (cdr stack)
5167                   gnus-tmp-header (caar thread))))
5168
5169         (setq gnus-tmp-false-parent nil)
5170         (setq gnus-tmp-root-expunged nil)
5171         (setq thread-end nil)
5172
5173         (if (stringp gnus-tmp-header)
5174             ;; The header is a dummy root.
5175             (cond
5176              ((eq gnus-summary-make-false-root 'adopt)
5177               ;; We let the first article adopt the rest.
5178               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5179                                                (cddar thread)))
5180               (setq gnus-tmp-gathered
5181                     (nconc (mapcar
5182                             (lambda (h) (mail-header-number (car h)))
5183                             (cddar thread))
5184                            gnus-tmp-gathered))
5185               (setq thread (cons (list (caar thread)
5186                                        (cadar thread))
5187                                  (cdr thread)))
5188               (setq gnus-tmp-level -1
5189                     gnus-tmp-false-parent t))
5190              ((eq gnus-summary-make-false-root 'empty)
5191               ;; We print adopted articles with empty subject fields.
5192               (setq gnus-tmp-gathered
5193                     (nconc (mapcar
5194                             (lambda (h) (mail-header-number (car h)))
5195                             (cddar thread))
5196                            gnus-tmp-gathered))
5197               (setq gnus-tmp-level -1))
5198              ((eq gnus-summary-make-false-root 'dummy)
5199               ;; We remember that we probably want to output a dummy
5200               ;; root.
5201               (setq gnus-tmp-dummy-line gnus-tmp-header)
5202               (setq gnus-tmp-prev-subject gnus-tmp-header))
5203              (t
5204               ;; We do not make a root for the gathered
5205               ;; sub-threads at all.
5206               (setq gnus-tmp-level -1)))
5207
5208           (setq number (mail-header-number gnus-tmp-header)
5209                 subject (mail-header-subject gnus-tmp-header)
5210                 simp-subject (gnus-simplify-subject-fully subject))
5211
5212           (cond
5213            ;; If the thread has changed subject, we might want to make
5214            ;; this subthread into a root.
5215            ((and (null gnus-thread-ignore-subject)
5216                  (not (zerop gnus-tmp-level))
5217                  gnus-tmp-prev-subject
5218                  (not (string= gnus-tmp-prev-subject simp-subject)))
5219             (setq new-roots (nconc new-roots (list (car thread)))
5220                   thread-end t
5221                   gnus-tmp-header nil))
5222            ;; If the article lies outside the current limit,
5223            ;; then we do not display it.
5224            ((not (memq number gnus-newsgroup-limit))
5225             (setq gnus-tmp-gathered
5226                   (nconc (mapcar
5227                           (lambda (h) (mail-header-number (car h)))
5228                           (cdar thread))
5229                          gnus-tmp-gathered))
5230             (setq gnus-tmp-new-adopts (if (cdar thread)
5231                                           (append gnus-tmp-new-adopts
5232                                                   (cdar thread))
5233                                         gnus-tmp-new-adopts)
5234                   thread-end t
5235                   gnus-tmp-header nil)
5236             (when (zerop gnus-tmp-level)
5237               (setq gnus-tmp-root-expunged t)))
5238            ;; Perhaps this article is to be marked as read?
5239            ((and gnus-summary-mark-below
5240                  (< (or (cdr (assq number gnus-newsgroup-scored))
5241                         default-score)
5242                     gnus-summary-mark-below)
5243                  ;; Don't touch sparse articles.
5244                  (not (gnus-summary-article-sparse-p number))
5245                  (not (gnus-summary-article-ancient-p number)))
5246             (setq gnus-newsgroup-unreads
5247                   (delq number gnus-newsgroup-unreads))
5248             (if gnus-newsgroup-auto-expire
5249                 (setq gnus-newsgroup-expirable
5250                       (gnus-add-to-sorted-list
5251                        gnus-newsgroup-expirable number))
5252               (push (cons number gnus-low-score-mark)
5253                     gnus-newsgroup-reads))))
5254
5255           (when gnus-tmp-header
5256             ;; We may have an old dummy line to output before this
5257             ;; article.
5258             (when (and gnus-tmp-dummy-line
5259                        (gnus-subject-equal
5260                         gnus-tmp-dummy-line
5261                         (mail-header-subject gnus-tmp-header)))
5262               (gnus-summary-insert-dummy-line
5263                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5264               (setq gnus-tmp-dummy-line nil))
5265
5266             ;; Compute the mark.
5267             (setq gnus-tmp-unread (gnus-article-mark number))
5268
5269             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5270                                   gnus-tmp-header gnus-tmp-level)
5271                   gnus-newsgroup-data)
5272
5273             ;; Actually insert the line.
5274             (setq
5275              gnus-tmp-subject-or-nil
5276              (cond
5277               ((and gnus-thread-ignore-subject
5278                     gnus-tmp-prev-subject
5279                     (not (string= gnus-tmp-prev-subject simp-subject)))
5280                subject)
5281               ((zerop gnus-tmp-level)
5282                (if (and (eq gnus-summary-make-false-root 'empty)
5283                         (memq number gnus-tmp-gathered)
5284                         gnus-tmp-prev-subject
5285                         (string= gnus-tmp-prev-subject simp-subject))
5286                    gnus-summary-same-subject
5287                  subject))
5288               (t gnus-summary-same-subject)))
5289             (if (and (eq gnus-summary-make-false-root 'adopt)
5290                      (= gnus-tmp-level 1)
5291                      (memq number gnus-tmp-gathered))
5292                 (setq gnus-tmp-opening-bracket ?\<
5293                       gnus-tmp-closing-bracket ?\>)
5294               (setq gnus-tmp-opening-bracket ?\[
5295                     gnus-tmp-closing-bracket ?\]))
5296             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5297                 (gnus-make-thread-indent-array
5298                  (max (* 2 (length gnus-thread-indent-array))
5299                       gnus-tmp-level)))
5300             (setq
5301              gnus-tmp-indentation
5302              (aref gnus-thread-indent-array gnus-tmp-level)
5303              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5304              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5305                                 gnus-summary-default-score 0)
5306              gnus-tmp-score-char
5307              (if (or (null gnus-summary-default-score)
5308                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5309                          gnus-summary-zcore-fuzz))
5310                  ?                      ;Whitespace
5311                (if (< gnus-tmp-score gnus-summary-default-score)
5312                    gnus-score-below-mark gnus-score-over-mark))
5313              gnus-tmp-replied
5314              (cond ((memq number gnus-newsgroup-processable)
5315                     gnus-process-mark)
5316                    ((memq number gnus-newsgroup-cached)
5317                     gnus-cached-mark)
5318                    ((memq number gnus-newsgroup-replied)
5319                     gnus-replied-mark)
5320                    ((memq number gnus-newsgroup-forwarded)
5321                     gnus-forwarded-mark)
5322                    ((memq number gnus-newsgroup-saved)
5323                     gnus-saved-mark)
5324                    ((memq number gnus-newsgroup-recent)
5325                     gnus-recent-mark)
5326                    ((memq number gnus-newsgroup-unseen)
5327                     gnus-unseen-mark)
5328                    (t gnus-no-mark))
5329              gnus-tmp-downloaded
5330              (cond ((memq number gnus-newsgroup-undownloaded)
5331                     gnus-undownloaded-mark)
5332                    (gnus-newsgroup-agentized
5333                     gnus-downloaded-mark)
5334                    (t
5335                     gnus-no-mark))
5336              gnus-tmp-from (mail-header-from gnus-tmp-header)
5337              gnus-tmp-name
5338              (cond
5339               ((string-match "<[^>]+> *$" gnus-tmp-from)
5340                (setq beg-match (match-beginning 0))
5341                (or (and (string-match "^\".+\"" gnus-tmp-from)
5342                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5343                    (substring gnus-tmp-from 0 beg-match)))
5344               ((string-match "(.+)" gnus-tmp-from)
5345                (substring gnus-tmp-from
5346                           (1+ (match-beginning 0)) (1- (match-end 0))))
5347               (t gnus-tmp-from))
5348
5349              ;; Do the %B string
5350              gnus-tmp-thread-tree-header-string
5351              (cond
5352               ((not gnus-show-threads) "")
5353               ((zerop gnus-tmp-level)
5354                (cond ((cdar thread)
5355                       (or gnus-sum-thread-tree-root subject))
5356                      (gnus-tmp-new-adopts
5357                       (or gnus-sum-thread-tree-false-root subject))
5358                      (t
5359                       (or gnus-sum-thread-tree-single-indent subject))))
5360               (t
5361                (concat (apply 'concat
5362                               (mapcar (lambda (item)
5363                                         (if (= item 1)
5364                                             gnus-sum-thread-tree-vertical
5365                                           gnus-sum-thread-tree-indent))
5366                                       (cdr (reverse tree-stack))))
5367                        (if (nth 1 thread)
5368                            gnus-sum-thread-tree-leaf-with-other
5369                          gnus-sum-thread-tree-single-leaf)))))
5370             (when (string= gnus-tmp-name "")
5371               (setq gnus-tmp-name gnus-tmp-from))
5372             (unless (numberp gnus-tmp-lines)
5373               (setq gnus-tmp-lines -1))
5374             (if (= gnus-tmp-lines -1)
5375                 (setq gnus-tmp-lines "?")
5376               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5377             (gnus-put-text-property
5378              (point)
5379              (progn (eval gnus-summary-line-format-spec) (point))
5380              'gnus-number number)
5381             (when gnus-visual-p
5382               (forward-line -1)
5383               (gnus-summary-highlight-line)
5384               (when gnus-summary-update-hook
5385                 (gnus-run-hooks 'gnus-summary-update-hook))
5386               (forward-line 1))
5387
5388             (setq gnus-tmp-prev-subject simp-subject)))
5389
5390         (when (nth 1 thread)
5391           (push (list (max 0 gnus-tmp-level)
5392                       (copy-sequence tree-stack)
5393                       (nthcdr 1 thread))
5394                 stack))
5395         (push (if (nth 1 thread) 1 0) tree-stack)
5396         (incf gnus-tmp-level)
5397         (setq threads (if thread-end nil (cdar thread)))
5398         (if gnus-summary-display-while-building
5399             (if building-count
5400                 (progn
5401                   ;; use a set frequency
5402                   (setq building-line-count (1- building-line-count))
5403                   (when (= building-line-count 0)
5404                     (sit-for 0)
5405                     (setq building-line-count
5406                           gnus-summary-display-while-building)))
5407               ;; always
5408               (sit-for 0)))
5409         (unless threads
5410           (setq gnus-tmp-level 0)))))
5411   (gnus-message 7 "Generating summary...done"))
5412
5413 (defun gnus-summary-prepare-unthreaded (headers)
5414   "Generate an unthreaded summary buffer based on HEADERS."
5415   (let (header number mark)
5416
5417     (beginning-of-line)
5418
5419     (while headers
5420       ;; We may have to root out some bad articles...
5421       (when (memq (setq number (mail-header-number
5422                                 (setq header (pop headers))))
5423                   gnus-newsgroup-limit)
5424         ;; Mark article as read when it has a low score.
5425         (when (and gnus-summary-mark-below
5426                    (< (or (cdr (assq number gnus-newsgroup-scored))
5427                           gnus-summary-default-score 0)
5428                       gnus-summary-mark-below)
5429                    (not (gnus-summary-article-ancient-p number)))
5430           (setq gnus-newsgroup-unreads
5431                 (delq number gnus-newsgroup-unreads))
5432           (if gnus-newsgroup-auto-expire
5433               (push number gnus-newsgroup-expirable)
5434             (push (cons number gnus-low-score-mark)
5435                   gnus-newsgroup-reads)))
5436
5437         (setq mark (gnus-article-mark number))
5438         (push (gnus-data-make number mark (1+ (point)) header 0)
5439               gnus-newsgroup-data)
5440         (gnus-summary-insert-line
5441          header 0 number
5442          (memq number gnus-newsgroup-undownloaded)
5443          mark (memq number gnus-newsgroup-replied)
5444          (memq number gnus-newsgroup-expirable)
5445          (mail-header-subject header) nil
5446          (cdr (assq number gnus-newsgroup-scored))
5447          (memq number gnus-newsgroup-processable))))))
5448
5449 (defun gnus-summary-remove-list-identifiers ()
5450   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5451   (let ((regexp (if (consp gnus-list-identifiers)
5452                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5453                   gnus-list-identifiers))
5454         changed subject)
5455     (when regexp
5456       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5457       (dolist (header gnus-newsgroup-headers)
5458         (setq subject (mail-header-subject header)
5459               changed nil)
5460         (while (string-match regexp subject)
5461           (setq subject
5462                 (concat (substring subject 0 (match-beginning 1))
5463                         (substring subject (match-end 0)))
5464                 changed t))
5465         (when changed
5466           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5467             (setq subject
5468                   (concat (substring subject 0 (match-beginning 1))
5469                           (substring subject (match-end 1)))))
5470           (mail-header-set-subject header subject))))))
5471
5472 (defun gnus-fetch-headers (articles)
5473   "Fetch headers of ARTICLES."
5474   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5475     (gnus-message 5 "Fetching headers for %s..." name)
5476     (prog1
5477         (if (eq 'nov
5478                 (setq gnus-headers-retrieved-by
5479                       (gnus-retrieve-headers
5480                        articles gnus-newsgroup-name
5481                        ;; We might want to fetch old headers, but
5482                        ;; not if there is only 1 article.
5483                        (and (or (and
5484                                  (not (eq gnus-fetch-old-headers 'some))
5485                                  (not (numberp gnus-fetch-old-headers)))
5486                                 (> (length articles) 1))
5487                             gnus-fetch-old-headers))))
5488             (gnus-get-newsgroup-headers-xover
5489              articles nil nil gnus-newsgroup-name t)
5490           (gnus-get-newsgroup-headers))
5491       (gnus-message 5 "Fetching headers for %s...done" name))))
5492
5493 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5494   "Select newsgroup GROUP.
5495 If READ-ALL is non-nil, all articles in the group are selected.
5496 If SELECT-ARTICLES, only select those articles from GROUP."
5497   (let* ((entry (gnus-group-entry group))
5498          ;;!!! Dirty hack; should be removed.
5499          (gnus-summary-ignore-duplicates
5500           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5501               t
5502             gnus-summary-ignore-duplicates))
5503          (info (nth 2 entry))
5504          charset articles fetched-articles cached)
5505
5506     (unless (gnus-check-server
5507              (set (make-local-variable 'gnus-current-select-method)
5508                   (gnus-find-method-for-group group)))
5509       (error "Couldn't open server"))
5510     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5511
5512     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5513         (gnus-activate-group group)     ; Or we can activate it...
5514         (progn                          ; Or we bug out.
5515           (when (equal major-mode 'gnus-summary-mode)
5516             (gnus-kill-buffer (current-buffer)))
5517           (error
5518            "Couldn't activate group %s: %s"
5519            (mm-decode-coding-string group charset)
5520            (mm-decode-coding-string (gnus-status-message group) charset))))
5521
5522     (unless (gnus-request-group group t)
5523       (when (equal major-mode 'gnus-summary-mode)
5524         (gnus-kill-buffer (current-buffer)))
5525       (error "Couldn't request group %s: %s"
5526              (mm-decode-coding-string group charset)
5527              (mm-decode-coding-string (gnus-status-message group) charset)))
5528
5529     (when gnus-agent
5530       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5531
5532       (setq gnus-summary-use-undownloaded-faces
5533             (gnus-agent-find-parameter
5534              group
5535              'agent-enable-undownloaded-faces)))
5536
5537     (setq gnus-newsgroup-name group
5538           gnus-newsgroup-unselected nil
5539           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5540
5541     (let ((display (gnus-group-find-parameter group 'display)))
5542       (setq gnus-newsgroup-display
5543             (cond
5544              ((not (zerop (or (car-safe read-all) 0)))
5545               ;; The user entered the group with C-u SPC/RET, let's show
5546               ;; all articles.
5547               'gnus-not-ignore)
5548              ((eq display 'all)
5549               'gnus-not-ignore)
5550              ((arrayp display)
5551               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5552              ((numberp display)
5553               ;; The following is probably the "correct" solution, but
5554               ;; it makes Gnus fetch all headers and then limit the
5555               ;; articles (which is slow), so instead we hack the
5556               ;; select-articles parameter instead. -- Simon Josefsson
5557               ;; <jas@kth.se>
5558               ;;
5559               ;; (gnus-byte-compile
5560               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5561               ;;                         display)))))
5562               (setq select-articles
5563                     (gnus-uncompress-range
5564                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5565                              (if (> tmp 0)
5566                                  tmp
5567                                1))
5568                            (cdr (gnus-active group)))))
5569               nil)
5570              (t
5571               nil))))
5572
5573     (gnus-summary-setup-default-charset)
5574
5575     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5576     (when (gnus-virtual-group-p group)
5577       (setq cached gnus-newsgroup-cached))
5578
5579     (setq gnus-newsgroup-unreads
5580           (gnus-sorted-ndifference
5581            (gnus-sorted-ndifference gnus-newsgroup-unreads
5582                                     gnus-newsgroup-marked)
5583            gnus-newsgroup-dormant))
5584
5585     (setq gnus-newsgroup-processable nil)
5586
5587     (gnus-update-read-articles group gnus-newsgroup-unreads)
5588
5589     ;; Adjust and set lists of article marks.
5590     (when info
5591       (gnus-adjust-marked-articles info))
5592     (if (setq articles select-articles)
5593         (setq gnus-newsgroup-unselected
5594               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5595       (setq articles (gnus-articles-to-read group read-all)))
5596
5597     (cond
5598      ((null articles)
5599       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5600       'quit)
5601      ((eq articles 0) nil)
5602      (t
5603       ;; Init the dependencies hash table.
5604       (setq gnus-newsgroup-dependencies
5605             (gnus-make-hashtable (length articles)))
5606       (gnus-set-global-variables)
5607       ;; Retrieve the headers and read them in.
5608
5609       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5610
5611       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5612       (when cached
5613         (setq gnus-newsgroup-cached cached))
5614
5615       ;; Suppress duplicates?
5616       (when gnus-suppress-duplicates
5617         (gnus-dup-suppress-articles))
5618
5619       ;; Set the initial limit.
5620       (setq gnus-newsgroup-limit (copy-sequence articles))
5621       ;; Remove canceled articles from the list of unread articles.
5622       (setq fetched-articles
5623             (mapcar (lambda (headers) (mail-header-number headers))
5624                     gnus-newsgroup-headers))
5625       (setq gnus-newsgroup-articles fetched-articles)
5626       (setq gnus-newsgroup-unreads
5627             (gnus-sorted-nintersection
5628              gnus-newsgroup-unreads fetched-articles))
5629       (gnus-compute-unseen-list)
5630
5631       ;; Removed marked articles that do not exist.
5632       (gnus-update-missing-marks
5633        (gnus-sorted-difference articles fetched-articles))
5634       ;; We might want to build some more threads first.
5635       (when (and gnus-fetch-old-headers
5636                  (eq gnus-headers-retrieved-by 'nov))
5637         (if (eq gnus-fetch-old-headers 'invisible)
5638             (gnus-build-all-threads)
5639           (gnus-build-old-threads)))
5640       ;; Let the Gnus agent mark articles as read.
5641       (when gnus-agent
5642         (gnus-agent-get-undownloaded-list))
5643       ;; Remove list identifiers from subject
5644       (when gnus-list-identifiers
5645         (gnus-summary-remove-list-identifiers))
5646       ;; Check whether auto-expire is to be done in this group.
5647       (setq gnus-newsgroup-auto-expire
5648             (gnus-group-auto-expirable-p group))
5649       ;; Set up the article buffer now, if necessary.
5650       (unless (and gnus-single-article-buffer
5651                    (equal gnus-article-buffer "*Article*"))
5652         (gnus-article-setup-buffer))
5653       ;; First and last article in this newsgroup.
5654       (when gnus-newsgroup-headers
5655         (setq gnus-newsgroup-begin
5656               (mail-header-number (car gnus-newsgroup-headers))
5657               gnus-newsgroup-end
5658               (mail-header-number
5659                (gnus-last-element gnus-newsgroup-headers))))
5660       ;; GROUP is successfully selected.
5661       (or gnus-newsgroup-headers t)))))
5662
5663 (defun gnus-compute-unseen-list ()
5664   ;; The `seen' marks are treated specially.
5665   (if (not gnus-newsgroup-seen)
5666       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5667     (setq gnus-newsgroup-unseen
5668           (gnus-inverse-list-range-intersection
5669            gnus-newsgroup-articles gnus-newsgroup-seen))))
5670
5671 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5672
5673 (defun gnus-summary-display-make-predicate (display)
5674   (require 'gnus-agent)
5675   (when (= (length display) 1)
5676     (setq display (car display)))
5677   (unless gnus-summary-display-cache
5678     (dolist (elem (append '((unread . unread)
5679                             (read . read)
5680                             (unseen . unseen))
5681                           gnus-article-mark-lists))
5682       (push (cons (cdr elem)
5683                   (gnus-byte-compile
5684                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5685             gnus-summary-display-cache)))
5686   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5687         (gnus-category-predicate-cache gnus-summary-display-cache))
5688     (gnus-get-predicate display)))
5689
5690 ;; Uses the dynamically bound `number' variable.
5691 (defvar number)
5692 (defun gnus-article-marked-p (type &optional article)
5693   (let ((article (or article number)))
5694     (cond
5695      ((eq type 'tick)
5696       (memq article gnus-newsgroup-marked))
5697      ((eq type 'spam)
5698       (memq article gnus-newsgroup-spam-marked))
5699      ((eq type 'unsend)
5700       (memq article gnus-newsgroup-unsendable))
5701      ((eq type 'undownload)
5702       (memq article gnus-newsgroup-undownloaded))
5703      ((eq type 'download)
5704       (memq article gnus-newsgroup-downloadable))
5705      ((eq type 'unread)
5706       (memq article gnus-newsgroup-unreads))
5707      ((eq type 'read)
5708       (memq article gnus-newsgroup-reads))
5709      ((eq type 'dormant)
5710       (memq article gnus-newsgroup-dormant) )
5711      ((eq type 'expire)
5712       (memq article gnus-newsgroup-expirable))
5713      ((eq type 'reply)
5714       (memq article gnus-newsgroup-replied))
5715      ((eq type 'killed)
5716       (memq article gnus-newsgroup-killed))
5717      ((eq type 'bookmark)
5718       (assq article gnus-newsgroup-bookmarks))
5719      ((eq type 'score)
5720       (assq article gnus-newsgroup-scored))
5721      ((eq type 'save)
5722       (memq article gnus-newsgroup-saved))
5723      ((eq type 'cache)
5724       (memq article gnus-newsgroup-cached))
5725      ((eq type 'forward)
5726       (memq article gnus-newsgroup-forwarded))
5727      ((eq type 'seen)
5728       (not (memq article gnus-newsgroup-unseen)))
5729      ((eq type 'recent)
5730       (memq article gnus-newsgroup-recent))
5731      (t t))))
5732
5733 (defun gnus-articles-to-read (group &optional read-all)
5734   "Find out what articles the user wants to read."
5735   (let* ((articles
5736           ;; Select all articles if `read-all' is non-nil, or if there
5737           ;; are no unread articles.
5738           (if (or read-all
5739                   (and (zerop (length gnus-newsgroup-marked))
5740                        (zerop (length gnus-newsgroup-unreads)))
5741                   ;; Fetch all if the predicate is non-nil.
5742                   gnus-newsgroup-display)
5743               ;; We want to select the headers for all the articles in
5744               ;; the group, so we select either all the active
5745               ;; articles in the group, or (if that's nil), the
5746               ;; articles in the cache.
5747               (or
5748                (if gnus-newsgroup-maximum-articles
5749                    (let ((active (gnus-active group)))
5750                      (gnus-uncompress-range
5751                       (cons (max (car active)
5752                                  (- (cdr active)
5753                                     gnus-newsgroup-maximum-articles
5754                                     -1))
5755                             (cdr active))))
5756                  (gnus-uncompress-range (gnus-active group)))
5757                (gnus-cache-articles-in-group group))
5758             ;; Select only the "normal" subset of articles.
5759             (gnus-sorted-nunion
5760              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5761              gnus-newsgroup-unreads)))
5762          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5763          (scored (length scored-list))
5764          (number (length articles))
5765          (marked (+ (length gnus-newsgroup-marked)
5766                     (length gnus-newsgroup-dormant)))
5767          (select
5768           (cond
5769            ((numberp read-all)
5770             read-all)
5771            ((numberp gnus-newsgroup-display)
5772             gnus-newsgroup-display)
5773            (t
5774             (condition-case ()
5775                 (cond
5776                  ((and (or (<= scored marked) (= scored number))
5777                        (numberp gnus-large-newsgroup)
5778                        (> number gnus-large-newsgroup))
5779                   (let* ((cursor-in-echo-area nil)
5780                          (initial (gnus-parameter-large-newsgroup-initial
5781                                    gnus-newsgroup-name))
5782                          (input
5783                           (read-string
5784                            (format
5785                             "How many articles from %s (%s %d): "
5786                             (gnus-group-decoded-name gnus-newsgroup-name)
5787                             (if initial "max" "default")
5788                             number)
5789                            (if initial
5790                                (cons (number-to-string initial)
5791                                      0)))))
5792                     (if (string-match "^[ \t]*$" input) number input)))
5793                  ((and (> scored marked) (< scored number)
5794                        (> (- scored number) 20))
5795                   (let ((input
5796                          (read-string
5797                           (format "%s %s (%d scored, %d total): "
5798                                   "How many articles from"
5799                                   (gnus-group-decoded-name group)
5800                                   scored number))))
5801                     (if (string-match "^[ \t]*$" input)
5802                         number input)))
5803                  (t number))
5804               (quit
5805                (message "Quit getting the articles to read")
5806                nil))))))
5807     (setq select (if (stringp select) (string-to-number select) select))
5808     (if (or (null select) (zerop select))
5809         select
5810       (if (and (not (zerop scored)) (<= (abs select) scored))
5811           (progn
5812             (setq articles (sort scored-list '<))
5813             (setq number (length articles)))
5814         (setq articles (copy-sequence articles)))
5815
5816       (when (< (abs select) number)
5817         (if (< select 0)
5818             ;; Select the N oldest articles.
5819             (setcdr (nthcdr (1- (abs select)) articles) nil)
5820           ;; Select the N most recent articles.
5821           (setq articles (nthcdr (- number select) articles))))
5822       (setq gnus-newsgroup-unselected
5823             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5824       (when gnus-alter-articles-to-read-function
5825         (setq articles
5826               (sort
5827                (funcall gnus-alter-articles-to-read-function
5828                         gnus-newsgroup-name articles)
5829                '<)))
5830       articles)))
5831
5832 (defun gnus-killed-articles (killed articles)
5833   (let (out)
5834     (while articles
5835       (when (inline (gnus-member-of-range (car articles) killed))
5836         (push (car articles) out))
5837       (setq articles (cdr articles)))
5838     out))
5839
5840 (defun gnus-uncompress-marks (marks)
5841   "Uncompress the mark ranges in MARKS."
5842   (let ((uncompressed '(score bookmark))
5843         out)
5844     (while marks
5845       (if (memq (caar marks) uncompressed)
5846           (push (car marks) out)
5847         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5848       (setq marks (cdr marks)))
5849     out))
5850
5851 (defun gnus-article-mark-to-type (mark)
5852   "Return the type of MARK."
5853   (or (cadr (assq mark gnus-article-special-mark-lists))
5854       'list))
5855
5856 (defun gnus-article-unpropagatable-p (mark)
5857   "Return whether MARK should be propagated to back end."
5858   (memq mark gnus-article-unpropagated-mark-lists))
5859
5860 (defun gnus-adjust-marked-articles (info)
5861   "Set all article lists and remove all marks that are no longer valid."
5862   (let* ((marked-lists (gnus-info-marks info))
5863          (active (gnus-active (gnus-info-group info)))
5864          (min (car active))
5865          (max (cdr active))
5866          (types gnus-article-mark-lists)
5867          marks var articles article mark mark-type
5868          bgn end)
5869     ;; Hack to avoid adjusting marks for imap.
5870     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5871               'nnimap)
5872       (setq min 1))
5873
5874     (dolist (marks marked-lists)
5875       (setq mark (car marks)
5876             mark-type (gnus-article-mark-to-type mark)
5877             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5878
5879       ;; We set the variable according to the type of the marks list,
5880       ;; and then adjust the marks to a subset of the active articles.
5881       (cond
5882        ;; Adjust "simple" lists - compressed yet unsorted
5883        ((eq mark-type 'list)
5884         ;; Simultaneously uncompress and clip to active range
5885         ;; See gnus-uncompress-range for a description of possible marks
5886         (let (l lh)
5887           (if (not (cadr marks))
5888               (set var nil)
5889             (setq articles (if (numberp (cddr marks))
5890                                (list (cdr marks))
5891                              (cdr marks))
5892                   lh (cons nil nil)
5893                   l lh)
5894
5895             (while (setq article (pop articles))
5896               (cond ((consp article)
5897                      (setq bgn (max (car article) min)
5898                            end (min (cdr article) max))
5899                      (while (<= bgn end)
5900                        (setq l (setcdr l (cons bgn nil))
5901                              bgn (1+ bgn))))
5902                     ((and (<= min article)
5903                           (>= max article))
5904                      (setq l (setcdr l (cons article nil))))))
5905             (set var (cdr lh)))))
5906        ;; Adjust assocs.
5907        ((eq mark-type 'tuple)
5908         (set var (setq articles (cdr marks)))
5909         (when (not (listp (cdr (symbol-value var))))
5910           (set var (list (symbol-value var))))
5911         (when (not (listp (cdr articles)))
5912           (setq articles (list articles)))
5913         (while articles
5914           (when (or (not (consp (setq article (pop articles))))
5915                     (< (car article) min)
5916                     (> (car article) max))
5917             (set var (delq article (symbol-value var))))))
5918        ;; Adjust ranges (sloppily).
5919        ((eq mark-type 'range)
5920         (cond
5921          ((eq mark 'seen)
5922           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5923           ;; It should be (seen (NUM1 . NUM2)).
5924           (when (numberp (cddr marks))
5925             (setcdr marks (list (cdr marks))))
5926           (setq articles (cdr marks))
5927           (while (and articles
5928                       (or (and (consp (car articles))
5929                                (> min (cdar articles)))
5930                           (and (numberp (car articles))
5931                                (> min (car articles)))))
5932             (pop articles))
5933           (set var articles))))))))
5934
5935 (defun gnus-update-missing-marks (missing)
5936   "Go through the list of MISSING articles and remove them from the mark lists."
5937   (when missing
5938     (let (var m)
5939       ;; Go through all types.
5940       (dolist (elem gnus-article-mark-lists)
5941         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5942           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5943           (when (symbol-value var)
5944             ;; This list has articles.  So we delete all missing
5945             ;; articles from it.
5946             (setq m missing)
5947             (while m
5948               (set var (delq (pop m) (symbol-value var))))))))))
5949
5950 (defun gnus-update-marks ()
5951   "Enter the various lists of marked articles into the newsgroup info list."
5952   (let ((types gnus-article-mark-lists)
5953         (info (gnus-get-info gnus-newsgroup-name))
5954         type list newmarked symbol delta-marks)
5955     (when info
5956       ;; Add all marks lists to the list of marks lists.
5957       (while (setq type (pop types))
5958         (setq list (symbol-value
5959                     (setq symbol
5960                           (intern (format "gnus-newsgroup-%s" (car type))))))
5961
5962         (when list
5963           ;; Get rid of the entries of the articles that have the
5964           ;; default score.
5965           (when (and (eq (cdr type) 'score)
5966                      gnus-save-score
5967                      list)
5968             (let* ((arts list)
5969                    (prev (cons nil list))
5970                    (all prev))
5971               (while arts
5972                 (if (or (not (consp (car arts)))
5973                         (= (cdar arts) gnus-summary-default-score))
5974                     (setcdr prev (cdr arts))
5975                   (setq prev arts))
5976                 (setq arts (cdr arts)))
5977               (setq list (cdr all)))))
5978
5979         (when (eq (cdr type) 'seen)
5980           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5981
5982         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5983           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5984
5985         (when (and (gnus-check-backend-function
5986                     'request-set-mark gnus-newsgroup-name)
5987                    (not (gnus-article-unpropagatable-p (cdr type))))
5988           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5989                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5990                  (add (gnus-remove-from-range
5991                        (gnus-copy-sequence list) old)))
5992             (when add
5993               (push (list add 'add (list (cdr type))) delta-marks))
5994             (when del
5995               ;; Don't delete marks from outside the active range.  This
5996               ;; shouldn't happen, but is a sanity check.
5997               (setq del (gnus-sorted-range-intersection
5998                          (gnus-active gnus-newsgroup-name) del))
5999               (push (list del 'del (list (cdr type))) delta-marks))))
6000
6001         (when list
6002           (push (cons (cdr type) list) newmarked)))
6003
6004       (when delta-marks
6005         (unless (gnus-check-group gnus-newsgroup-name)
6006           (error "Can't open server for %s" gnus-newsgroup-name))
6007         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6008
6009       ;; Enter these new marks into the info of the group.
6010       (if (nthcdr 3 info)
6011           (setcar (nthcdr 3 info) newmarked)
6012         ;; Add the marks lists to the end of the info.
6013         (when newmarked
6014           (setcdr (nthcdr 2 info) (list newmarked))))
6015
6016       ;; Cut off the end of the info if there's nothing else there.
6017       (let ((i 5))
6018         (while (and (> i 2)
6019                     (not (nth i info)))
6020           (when (nthcdr (decf i) info)
6021             (setcdr (nthcdr i info) nil)))))))
6022
6023 (defun gnus-set-mode-line (where)
6024   "Set the mode line of the article or summary buffers.
6025 If WHERE is `summary', the summary mode line format will be used."
6026   ;; Is this mode line one we keep updated?
6027   (when (and (memq where gnus-updated-mode-lines)
6028              (symbol-value
6029               (intern (format "gnus-%s-mode-line-format-spec" where))))
6030     (let (mode-string)
6031       ;; We evaluate this in the summary buffer since these
6032       ;; variables are buffer-local to that buffer.
6033       (with-current-buffer gnus-summary-buffer
6034         ;; We bind all these variables that are used in the `eval' form
6035         ;; below.
6036         (let* ((mformat (symbol-value
6037                          (intern
6038                           (format "gnus-%s-mode-line-format-spec" where))))
6039                (gnus-tmp-group-name (gnus-mode-string-quote
6040                                      (gnus-group-decoded-name
6041                                       gnus-newsgroup-name)))
6042                (gnus-tmp-article-number (or gnus-current-article 0))
6043                (gnus-tmp-unread gnus-newsgroup-unreads)
6044                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6045                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6046                (gnus-tmp-unread-and-unselected
6047                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6048                             (zerop gnus-tmp-unselected))
6049                        "")
6050                       ((zerop gnus-tmp-unselected)
6051                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6052                       (t (format "{%d(+%d) more}"
6053                                  gnus-tmp-unread-and-unticked
6054                                  gnus-tmp-unselected))))
6055                (gnus-tmp-subject
6056                 (if (and gnus-current-headers
6057                          (vectorp gnus-current-headers))
6058                     (gnus-mode-string-quote
6059                      (mail-header-subject gnus-current-headers))
6060                   ""))
6061                bufname-length max-len
6062                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6063           (setq mode-string (eval mformat))
6064           (setq bufname-length (if (string-match "%b" mode-string)
6065                                    (- (length
6066                                        (buffer-name
6067                                         (if (eq where 'summary)
6068                                             nil
6069                                           (get-buffer gnus-article-buffer))))
6070                                       2)
6071                                  0))
6072           (setq max-len (max 4 (if gnus-mode-non-string-length
6073                                    (- (window-width)
6074                                       gnus-mode-non-string-length
6075                                       bufname-length)
6076                                  (length mode-string))))
6077           ;; We might have to chop a bit of the string off...
6078           (when (> (length mode-string) max-len)
6079             (setq mode-string
6080                   (concat (truncate-string-to-width mode-string (- max-len 3))
6081                           "...")))))
6082       ;; Update the mode line.
6083       (setq mode-line-buffer-identification
6084             (gnus-mode-line-buffer-identification (list mode-string)))
6085       (set-buffer-modified-p t))))
6086
6087 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6088   "Go through the HEADERS list and add all Xrefs to a hash table.
6089 The resulting hash table is returned, or nil if no Xrefs were found."
6090   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6091          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6092          (xref-hashtb (gnus-make-hashtable))
6093          start group entry number xrefs header)
6094     (while headers
6095       (setq header (pop headers))
6096       (when (and (setq xrefs (mail-header-xref header))
6097                  (not (memq (setq number (mail-header-number header))
6098                             unreads)))
6099         (setq start 0)
6100         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6101           (setq start (match-end 0))
6102           (setq group (if prefix
6103                           (concat prefix (substring xrefs (match-beginning 1)
6104                                                     (match-end 1)))
6105                         (substring xrefs (match-beginning 1) (match-end 1))))
6106           (setq number
6107                 (string-to-number (substring xrefs (match-beginning 2)
6108                                           (match-end 2))))
6109           (if (setq entry (gnus-gethash group xref-hashtb))
6110               (setcdr entry (cons number (cdr entry)))
6111             (gnus-sethash group (cons number nil) xref-hashtb)))))
6112     (and start xref-hashtb)))
6113
6114 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6115   "Look through all the headers and mark the Xrefs as read."
6116   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6117         name info xref-hashtb idlist method nth4)
6118     (with-current-buffer gnus-group-buffer
6119       (when (setq xref-hashtb
6120                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6121         (mapatoms
6122          (lambda (group)
6123            (unless (string= from-newsgroup (setq name (symbol-name group)))
6124              (setq idlist (symbol-value group))
6125              ;; Dead groups are not updated.
6126              (and (prog1
6127                       (setq info (gnus-get-info name))
6128                     (when (stringp (setq nth4 (gnus-info-method info)))
6129                       (setq nth4 (gnus-server-to-method nth4))))
6130                   ;; Only do the xrefs if the group has the same
6131                   ;; select method as the group we have just read.
6132                   (or (gnus-methods-equal-p
6133                        nth4 (gnus-find-method-for-group from-newsgroup))
6134                       virtual
6135                       (equal nth4 (setq method (gnus-find-method-for-group
6136                                                 from-newsgroup)))
6137                       (and (equal (car nth4) (car method))
6138                            (equal (nth 1 nth4) (nth 1 method))))
6139                   gnus-use-cross-reference
6140                   (or (not (eq gnus-use-cross-reference t))
6141                       virtual
6142                       ;; Only do cross-references on subscribed
6143                       ;; groups, if that is what is wanted.
6144                       (<= (gnus-info-level info) gnus-level-subscribed))
6145                   (gnus-group-make-articles-read name idlist))))
6146          xref-hashtb)))))
6147
6148 (defun gnus-compute-read-articles (group articles)
6149   (let* ((entry (gnus-group-entry group))
6150          (info (nth 2 entry))
6151          (active (gnus-active group))
6152          ninfo)
6153     (when entry
6154       ;; First peel off all invalid article numbers.
6155       (when active
6156         (let ((ids articles)
6157               id first)
6158           (while (setq id (pop ids))
6159             (when (and first (> id (cdr active)))
6160               ;; We'll end up in this situation in one particular
6161               ;; obscure situation.  If you re-scan a group and get
6162               ;; a new article that is cross-posted to a different
6163               ;; group that has not been re-scanned, you might get
6164               ;; crossposted article that has a higher number than
6165               ;; Gnus believes possible.  So we re-activate this
6166               ;; group as well.  This might mean doing the
6167               ;; crossposting thingy will *increase* the number
6168               ;; of articles in some groups.  Tsk, tsk.
6169               (setq active (or (gnus-activate-group group) active)))
6170             (when (or (> id (cdr active))
6171                       (< id (car active)))
6172               (setq articles (delq id articles))))))
6173       ;; If the read list is nil, we init it.
6174       (if (and active
6175                (null (gnus-info-read info))
6176                (> (car active) 1))
6177           (setq ninfo (cons 1 (1- (car active))))
6178         (setq ninfo (gnus-info-read info)))
6179       ;; Then we add the read articles to the range.
6180       (gnus-add-to-range
6181        ninfo (setq articles (sort articles '<))))))
6182
6183 (defun gnus-group-make-articles-read (group articles)
6184   "Update the info of GROUP to say that ARTICLES are read."
6185   (let* ((num 0)
6186          (entry (gnus-group-entry group))
6187          (info (nth 2 entry))
6188          (active (gnus-active group))
6189          range)
6190     (when entry
6191       (setq range (gnus-compute-read-articles group articles))
6192       (with-current-buffer gnus-group-buffer
6193         (gnus-undo-register
6194           `(progn
6195              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6196              (gnus-info-set-read ',info ',(gnus-info-read info))
6197              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6198              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6199              (gnus-group-update-group ,group t))))
6200       ;; Add the read articles to the range.
6201       (gnus-info-set-read info range)
6202       (gnus-request-set-mark group (list (list range 'add '(read))))
6203       ;; Then we have to re-compute how many unread
6204       ;; articles there are in this group.
6205       (when active
6206         (cond
6207          ((not range)
6208           (setq num (- (1+ (cdr active)) (car active))))
6209          ((not (listp (cdr range)))
6210           (setq num (- (cdr active) (- (1+ (cdr range))
6211                                        (car range)))))
6212          (t
6213           (while range
6214             (if (numberp (car range))
6215                 (setq num (1+ num))
6216               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6217             (setq range (cdr range)))
6218           (setq num (- (cdr active) num))))
6219         ;; Update the number of unread articles.
6220         (setcar entry num)
6221         ;; Update the group buffer.
6222         (unless (gnus-ephemeral-group-p group)
6223           (gnus-group-update-group group t))))))
6224
6225 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6226   (let ((cur nntp-server-buffer)
6227         (dependencies
6228          (or dependencies
6229              (with-current-buffer gnus-summary-buffer
6230                gnus-newsgroup-dependencies)))
6231         headers id end ref number
6232         (mail-parse-charset gnus-newsgroup-charset)
6233         (mail-parse-ignored-charsets
6234          (save-current-buffer (condition-case nil
6235                                   (set-buffer gnus-summary-buffer)
6236                                 (error))
6237                               gnus-newsgroup-ignored-charsets)))
6238     (with-current-buffer nntp-server-buffer
6239       ;; Translate all TAB characters into SPACE characters.
6240       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6241       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6242       (ietf-drums-unfold-fws)
6243       (gnus-run-hooks 'gnus-parse-headers-hook)
6244       (let ((case-fold-search t)
6245             in-reply-to header p lines chars)
6246         (goto-char (point-min))
6247         ;; Search to the beginning of the next header.  Error messages
6248         ;; do not begin with 2 or 3.
6249         (while (re-search-forward "^[23][0-9]+ " nil t)
6250           (setq id nil
6251                 ref nil)
6252           ;; This implementation of this function, with nine
6253           ;; search-forwards instead of the one re-search-forward and
6254           ;; a case (which basically was the old function) is actually
6255           ;; about twice as fast, even though it looks messier.  You
6256           ;; can't have everything, I guess.  Speed and elegance
6257           ;; doesn't always go hand in hand.
6258           (setq
6259            header
6260            (vector
6261             ;; Number.
6262             (prog1
6263                 (setq number (read cur))
6264               (end-of-line)
6265               (setq p (point))
6266               (narrow-to-region (point)
6267                                 (or (and (search-forward "\n.\n" nil t)
6268                                          (- (point) 2))
6269                                     (point))))
6270             ;; Subject.
6271             (progn
6272               (goto-char p)
6273               (if (search-forward "\nsubject:" nil t)
6274                   (funcall gnus-decode-encoded-word-function
6275                            (nnheader-header-value))
6276                 "(none)"))
6277             ;; From.
6278             (progn
6279               (goto-char p)
6280               (if (search-forward "\nfrom:" nil t)
6281                   (funcall gnus-decode-encoded-address-function
6282                            (nnheader-header-value))
6283                 "(nobody)"))
6284             ;; Date.
6285             (progn
6286               (goto-char p)
6287               (if (search-forward "\ndate:" nil t)
6288                   (nnheader-header-value) ""))
6289             ;; Message-ID.
6290             (progn
6291               (goto-char p)
6292               (setq id (if (re-search-forward
6293                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6294                            ;; We do it this way to make sure the Message-ID
6295                            ;; is (somewhat) syntactically valid.
6296                            (buffer-substring (match-beginning 1)
6297                                              (match-end 1))
6298                          ;; If there was no message-id, we just fake one
6299                          ;; to make subsequent routines simpler.
6300                          (nnheader-generate-fake-message-id number))))
6301             ;; References.
6302             (progn
6303               (goto-char p)
6304               (if (search-forward "\nreferences:" nil t)
6305                   (progn
6306                     (setq end (point))
6307                     (prog1
6308                         (nnheader-header-value)
6309                       (setq ref
6310                             (buffer-substring
6311                              (progn
6312                                (end-of-line)
6313                                (search-backward ">" end t)
6314                                (1+ (point)))
6315                              (progn
6316                                (search-backward "<" end t)
6317                                (point))))))
6318                 ;; Get the references from the in-reply-to header if there
6319                 ;; were no references and the in-reply-to header looks
6320                 ;; promising.
6321                 (if (and (search-forward "\nin-reply-to:" nil t)
6322                          (setq in-reply-to (nnheader-header-value))
6323                          (string-match "<[^>]+>" in-reply-to))
6324                     (let (ref2)
6325                       (setq ref (substring in-reply-to (match-beginning 0)
6326                                            (match-end 0)))
6327                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6328                         (setq ref2 (substring in-reply-to (match-beginning 0)
6329                                               (match-end 0)))
6330                         (when (> (length ref2) (length ref))
6331                           (setq ref ref2)))
6332                       ref)
6333                   (setq ref nil))))
6334             ;; Chars.
6335             (progn
6336               (goto-char p)
6337               (if (search-forward "\nchars: " nil t)
6338                   (if (numberp (setq chars (ignore-errors (read cur))))
6339                       chars -1)
6340                 -1))
6341             ;; Lines.
6342             (progn
6343               (goto-char p)
6344               (if (search-forward "\nlines: " nil t)
6345                   (if (numberp (setq lines (ignore-errors (read cur))))
6346                       lines -1)
6347                 -1))
6348             ;; Xref.
6349             (progn
6350               (goto-char p)
6351               (and (search-forward "\nxref:" nil t)
6352                    (nnheader-header-value)))
6353             ;; Extra.
6354             (when gnus-extra-headers
6355               (let ((extra gnus-extra-headers)
6356                     out)
6357                 (while extra
6358                   (goto-char p)
6359                   (when (search-forward
6360                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6361                     (push (cons (car extra) (nnheader-header-value))
6362                           out))
6363                   (pop extra))
6364                 out))))
6365           (when (equal id ref)
6366             (setq ref nil))
6367
6368           (when gnus-alter-header-function
6369             (funcall gnus-alter-header-function header)
6370             (setq id (mail-header-id header)
6371                   ref (gnus-parent-id (mail-header-references header))))
6372
6373           (when (setq header
6374                       (gnus-dependencies-add-header
6375                        header dependencies force-new))
6376             (push header headers))
6377           (goto-char (point-max))
6378           (widen))
6379         (nreverse headers)))))
6380
6381 ;; Goes through the xover lines and returns a list of vectors
6382 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6383                                                   force-new dependencies
6384                                                   group also-fetch-heads)
6385   "Parse the news overview data in the server buffer.
6386 Return a list of headers that match SEQUENCE (see
6387 `nntp-retrieve-headers')."
6388   ;; Get the Xref when the users reads the articles since most/some
6389   ;; NNTP servers do not include Xrefs when using XOVER.
6390   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6391   (let ((mail-parse-charset gnus-newsgroup-charset)
6392         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6393         (cur nntp-server-buffer)
6394         (dependencies (or dependencies gnus-newsgroup-dependencies))
6395         (allp (cond
6396                ((eq gnus-read-all-available-headers t)
6397                 t)
6398                ((and (stringp gnus-read-all-available-headers)
6399                      group)
6400                 (string-match gnus-read-all-available-headers group))
6401                (t
6402                 nil)))
6403         number headers header)
6404     (with-current-buffer nntp-server-buffer
6405       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6406       ;; Allow the user to mangle the headers before parsing them.
6407       (gnus-run-hooks 'gnus-parse-headers-hook)
6408       (goto-char (point-min))
6409       (gnus-parse-without-error
6410         (while (and (or sequence allp)
6411                     (not (eobp)))
6412           (setq number (read cur))
6413           (when (not allp)
6414             (while (and sequence
6415                         (< (car sequence) number))
6416               (setq sequence (cdr sequence))))
6417           (when (and (or allp
6418                          (and sequence
6419                               (eq number (car sequence))))
6420                      (progn
6421                        (setq sequence (cdr sequence))
6422                        (setq header (inline
6423                                       (gnus-nov-parse-line
6424                                        number dependencies force-new)))))
6425             (push header headers))
6426           (forward-line 1)))
6427       ;; A common bug in inn is that if you have posted an article and
6428       ;; then retrieves the active file, it will answer correctly --
6429       ;; the new article is included.  However, a NOV entry for the
6430       ;; article may not have been generated yet, so this may fail.
6431       ;; We work around this problem by retrieving the last few
6432       ;; headers using HEAD.
6433       (if (or (not also-fetch-heads)
6434               (not sequence))
6435           ;; We (probably) got all the headers.
6436           (nreverse headers)
6437         (let ((gnus-nov-is-evil t))
6438           (nconc
6439            (nreverse headers)
6440            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6441              (gnus-get-newsgroup-headers))))))))
6442
6443 (defun gnus-article-get-xrefs ()
6444   "Fill in the Xref value in `gnus-current-headers', if necessary.
6445 This is meant to be called in `gnus-article-internal-prepare-hook'."
6446   (let ((headers (with-current-buffer gnus-summary-buffer
6447                    gnus-current-headers)))
6448     (or (not gnus-use-cross-reference)
6449         (not headers)
6450         (and (mail-header-xref headers)
6451              (not (string= (mail-header-xref headers) "")))
6452         (let ((case-fold-search t)
6453               xref)
6454           (save-restriction
6455             (nnheader-narrow-to-headers)
6456             (goto-char (point-min))
6457             (when (or (and (not (eobp))
6458                            (eq (downcase (char-after)) ?x)
6459                            (looking-at "Xref:"))
6460                       (search-forward "\nXref:" nil t))
6461               (goto-char (1+ (match-end 0)))
6462               (setq xref (buffer-substring (point) (point-at-eol)))
6463               (mail-header-set-xref headers xref)))))))
6464
6465 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6466   "Find article ID and insert the summary line for that article.
6467 OLD-HEADER can either be a header or a line number to insert
6468 the subject line on."
6469   (let* ((line (and (numberp old-header) old-header))
6470          (old-header (and (vectorp old-header) old-header))
6471          (header (cond ((and old-header use-old-header)
6472                         old-header)
6473                        ((and (numberp id)
6474                              (gnus-number-to-header id))
6475                         (gnus-number-to-header id))
6476                        (t
6477                         (gnus-read-header id))))
6478          (number (and (numberp id) id))
6479          d)
6480     (when header
6481       ;; Rebuild the thread that this article is part of and go to the
6482       ;; article we have fetched.
6483       (when (and (not gnus-show-threads)
6484                  old-header)
6485         (when (and number
6486                    (setq d (gnus-data-find (mail-header-number old-header))))
6487           (goto-char (gnus-data-pos d))
6488           (gnus-data-remove
6489            number
6490            (- (point-at-bol)
6491               (prog1
6492                   (1+ (point-at-eol))
6493                 (gnus-delete-line))))))
6494       ;; Remove list identifiers from subject.
6495       (when gnus-list-identifiers
6496         (let ((gnus-newsgroup-headers (list header)))
6497           (gnus-summary-remove-list-identifiers)))
6498       (when old-header
6499         (mail-header-set-number header (mail-header-number old-header)))
6500       (setq gnus-newsgroup-sparse
6501             (delq (setq number (mail-header-number header))
6502                   gnus-newsgroup-sparse))
6503       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6504       (push number gnus-newsgroup-limit)
6505       (gnus-rebuild-thread (mail-header-id header) line)
6506       (gnus-summary-goto-subject number nil t))
6507     (when (and (numberp number)
6508                (> number 0))
6509       ;; We have to update the boundaries even if we can't fetch the
6510       ;; article if ID is a number -- so that the next `P' or `N'
6511       ;; command will fetch the previous (or next) article even
6512       ;; if the one we tried to fetch this time has been canceled.
6513       (when (> number gnus-newsgroup-end)
6514         (setq gnus-newsgroup-end number))
6515       (when (< number gnus-newsgroup-begin)
6516         (setq gnus-newsgroup-begin number))
6517       (setq gnus-newsgroup-unselected
6518             (delq number gnus-newsgroup-unselected)))
6519     ;; Report back a success?
6520     (and header (mail-header-number header))))
6521
6522 ;;; Process/prefix in the summary buffer
6523
6524 (defun gnus-summary-work-articles (n)
6525   "Return a list of articles to be worked upon.
6526 The prefix argument, the list of process marked articles, and the
6527 current article will be taken into consideration."
6528   (with-current-buffer gnus-summary-buffer
6529     (cond
6530      (n
6531       ;; A numerical prefix has been given.
6532       (setq n (prefix-numeric-value n))
6533       (let ((backward (< n 0))
6534             (n (abs (prefix-numeric-value n)))
6535             articles article)
6536         (save-excursion
6537           (while
6538               (and (> n 0)
6539                    (push (setq article (gnus-summary-article-number))
6540                          articles)
6541                    (if backward
6542                        (gnus-summary-find-prev nil article)
6543                      (gnus-summary-find-next nil article)))
6544             (decf n)))
6545         (nreverse articles)))
6546      ((and (gnus-region-active-p) (mark))
6547       (message "region active")
6548       ;; Work on the region between point and mark.
6549       (let ((max (max (point) (mark)))
6550             articles article)
6551         (save-excursion
6552           (goto-char (min (point) (mark)))
6553           (while
6554               (and
6555                (push (setq article (gnus-summary-article-number)) articles)
6556                (gnus-summary-find-next nil article)
6557                (< (point) max)))
6558           (nreverse articles))))
6559      (gnus-newsgroup-processable
6560       ;; There are process-marked articles present.
6561       ;; Save current state.
6562       (gnus-summary-save-process-mark)
6563       ;; Return the list.
6564       (reverse gnus-newsgroup-processable))
6565      (t
6566       ;; Just return the current article.
6567       (list (gnus-summary-article-number))))))
6568
6569 (defmacro gnus-summary-iterate (arg &rest forms)
6570   "Iterate over the process/prefixed articles and do FORMS.
6571 ARG is the interactive prefix given to the command.  FORMS will be
6572 executed with point over the summary line of the articles."
6573   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6574     `(let ((,articles (gnus-summary-work-articles ,arg)))
6575        (while ,articles
6576          (gnus-summary-goto-subject (car ,articles))
6577          ,@forms
6578          (pop ,articles)))))
6579
6580 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6581 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6582
6583 (defun gnus-summary-save-process-mark ()
6584   "Push the current set of process marked articles on the stack."
6585   (interactive)
6586   (push (copy-sequence gnus-newsgroup-processable)
6587         gnus-newsgroup-process-stack))
6588
6589 (defun gnus-summary-kill-process-mark ()
6590   "Push the current set of process marked articles on the stack and unmark."
6591   (interactive)
6592   (gnus-summary-save-process-mark)
6593   (gnus-summary-unmark-all-processable))
6594
6595 (defun gnus-summary-yank-process-mark ()
6596   "Pop the last process mark state off the stack and restore it."
6597   (interactive)
6598   (unless gnus-newsgroup-process-stack
6599     (error "Empty mark stack"))
6600   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6601
6602 (defun gnus-summary-process-mark-set (set)
6603   "Make SET into the current process marked articles."
6604   (gnus-summary-unmark-all-processable)
6605   (mapc 'gnus-summary-set-process-mark set))
6606
6607 ;;; Searching and stuff
6608
6609 (defun gnus-summary-search-group (&optional backward use-level)
6610   "Search for next unread newsgroup.
6611 If optional argument BACKWARD is non-nil, search backward instead."
6612   (with-current-buffer gnus-group-buffer
6613     (when (gnus-group-search-forward
6614            backward nil (if use-level (gnus-group-group-level) nil))
6615       (gnus-group-group-name))))
6616
6617 (defun gnus-summary-best-group (&optional exclude-group)
6618   "Find the name of the best unread group.
6619 If EXCLUDE-GROUP, do not go to this group."
6620   (with-current-buffer gnus-group-buffer
6621     (save-excursion
6622       (gnus-group-best-unread-group exclude-group))))
6623
6624 (defun gnus-summary-find-next (&optional unread article backward)
6625   (if backward
6626       (gnus-summary-find-prev unread article)
6627     (let* ((dummy (gnus-summary-article-intangible-p))
6628            (article (or article (gnus-summary-article-number)))
6629            (data (gnus-data-find-list article))
6630            result)
6631       (when (and (not dummy)
6632                  (or (not gnus-summary-check-current)
6633                      (not unread)
6634                      (not (gnus-data-unread-p (car data)))))
6635         (setq data (cdr data)))
6636       (when (setq result
6637                   (if unread
6638                       (progn
6639                         (while data
6640                           (unless (memq (gnus-data-number (car data))
6641                                         (cond
6642                                          ((eq gnus-auto-goto-ignores
6643                                               'always-undownloaded)
6644                                           gnus-newsgroup-undownloaded)
6645                                          (gnus-plugged
6646                                           nil)
6647                                          ((eq gnus-auto-goto-ignores
6648                                               'unfetched)
6649                                           gnus-newsgroup-unfetched)
6650                                          ((eq gnus-auto-goto-ignores
6651                                               'undownloaded)
6652                                           gnus-newsgroup-undownloaded)))
6653                             (when (gnus-data-unread-p (car data))
6654                               (setq result (car data)
6655                                     data nil)))
6656                           (setq data (cdr data)))
6657                         result)
6658                     (car data)))
6659         (goto-char (gnus-data-pos result))
6660         (gnus-data-number result)))))
6661
6662 (defun gnus-summary-find-prev (&optional unread article)
6663   (let* ((eobp (eobp))
6664          (article (or article (gnus-summary-article-number)))
6665          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6666          result)
6667     (when (and (not eobp)
6668                (or (not gnus-summary-check-current)
6669                    (not unread)
6670                    (not (gnus-data-unread-p (car data)))))
6671       (setq data (cdr data)))
6672     (when (setq result
6673                 (if unread
6674                     (progn
6675                       (while data
6676                         (unless (memq (gnus-data-number (car data))
6677                                       (cond
6678                                        ((eq gnus-auto-goto-ignores
6679                                             'always-undownloaded)
6680                                         gnus-newsgroup-undownloaded)
6681                                        (gnus-plugged
6682                                         nil)
6683                                        ((eq gnus-auto-goto-ignores
6684                                             'unfetched)
6685                                         gnus-newsgroup-unfetched)
6686                                        ((eq gnus-auto-goto-ignores
6687                                             'undownloaded)
6688                                         gnus-newsgroup-undownloaded)))
6689                           (when (gnus-data-unread-p (car data))
6690                             (setq result (car data)
6691                                   data nil)))
6692                         (setq data (cdr data)))
6693                       result)
6694                   (car data)))
6695       (goto-char (gnus-data-pos result))
6696       (gnus-data-number result))))
6697
6698 (defun gnus-summary-find-subject (subject &optional unread backward article)
6699   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6700          (article (or article (gnus-summary-article-number)))
6701          (articles (gnus-data-list backward))
6702          (arts (gnus-data-find-list article articles))
6703          result)
6704     (when (or (not gnus-summary-check-current)
6705               (not unread)
6706               (not (gnus-data-unread-p (car arts))))
6707       (setq arts (cdr arts)))
6708     (while arts
6709       (and (or (not unread)
6710                (gnus-data-unread-p (car arts)))
6711            (vectorp (gnus-data-header (car arts)))
6712            (gnus-subject-equal
6713             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6714            (setq result (car arts)
6715                  arts nil))
6716       (setq arts (cdr arts)))
6717     (and result
6718          (goto-char (gnus-data-pos result))
6719          (gnus-data-number result))))
6720
6721 (defun gnus-summary-search-forward (&optional unread subject backward)
6722   "Search forward for an article.
6723 If UNREAD, look for unread articles.  If SUBJECT, look for
6724 articles with that subject.  If BACKWARD, search backward instead."
6725   (cond (subject (gnus-summary-find-subject subject unread backward))
6726         (backward (gnus-summary-find-prev unread))
6727         (t (gnus-summary-find-next unread))))
6728
6729 (defun gnus-recenter (&optional n)
6730   "Center point in window and redisplay frame.
6731 Also do horizontal recentering."
6732   (interactive "P")
6733   (when (and gnus-auto-center-summary
6734              (not (eq gnus-auto-center-summary 'vertical)))
6735     (gnus-horizontal-recenter))
6736   (if (fboundp 'recenter-top-bottom)
6737       (recenter-top-bottom n)
6738     (recenter n)))
6739
6740 (put 'gnus-recenter 'isearch-scroll t)
6741
6742 (defun gnus-forward-line-ignore-invisible (n)
6743   "Move N lines forward (backward if N is negative).
6744 Like forward-line, but skip over (and don't count) invisible lines."
6745   (let (done)
6746     (while (and (> n 0) (not done))
6747       ;; If the following character is currently invisible,
6748       ;; skip all characters with that same `invisible' property value.
6749       (while (gnus-invisible-p (point))
6750         (goto-char (gnus-next-char-property-change (point))))
6751       (forward-line 1)
6752       (if (eobp)
6753           (setq done t)
6754         (setq n (1- n))))
6755     (while (and (< n 0) (not done))
6756       (forward-line -1)
6757       (if (bobp) (setq done t)
6758         (setq n (1+ n))
6759         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6760           (goto-char (gnus-previous-char-property-change (point))))))))
6761
6762 (defun gnus-summary-recenter ()
6763   "Center point in the summary window.
6764 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6765 displayed, no centering will be performed."
6766   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6767   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6768   (interactive)
6769   ;; The user has to want it.
6770   (when gnus-auto-center-summary
6771     (let* ((top (cond ((< (window-height) 4) 0)
6772                       ((< (window-height) 7) 1)
6773                       (t (if (numberp gnus-auto-center-summary)
6774                              gnus-auto-center-summary
6775                            (/ (1- (window-height)) 2)))))
6776            (height (1- (window-height)))
6777            (bottom (save-excursion
6778                      (goto-char (point-max))
6779                      (gnus-forward-line-ignore-invisible (- height))
6780                      (point)))
6781            (window (get-buffer-window (current-buffer))))
6782       (when (get-buffer-window gnus-article-buffer)
6783         ;; Only do recentering when the article buffer is displayed,
6784         ;; Set the window start to either `bottom', which is the biggest
6785         ;; possible valid number, or the second line from the top,
6786         ;; whichever is the least.
6787         (let ((top-pos (save-excursion
6788                          (gnus-forward-line-ignore-invisible (- top))
6789                          (point))))
6790           (if (> bottom top-pos)
6791               ;; Keep the second line from the top visible
6792               (set-window-start window top-pos)
6793             ;; Try to keep the bottom line visible; if it's partially
6794             ;; obscured, either scroll one more line to make it fully
6795             ;; visible, or revert to using TOP-POS.
6796             (save-excursion
6797               (goto-char (point-max))
6798               (gnus-forward-line-ignore-invisible -1)
6799               (let ((last-line-start (point)))
6800                 (goto-char bottom)
6801                 (set-window-start window (point) t)
6802                 (when (not (pos-visible-in-window-p last-line-start window))
6803                   (gnus-forward-line-ignore-invisible 1)
6804                   (set-window-start window (min (point) top-pos) t)))))))
6805       ;; Do horizontal recentering while we're at it.
6806       (when (and (get-buffer-window (current-buffer) t)
6807                  (not (eq gnus-auto-center-summary 'vertical)))
6808         (let ((selected (selected-window)))
6809           (select-window (get-buffer-window (current-buffer) t))
6810           (gnus-summary-position-point)
6811           (gnus-horizontal-recenter)
6812           (select-window selected))))))
6813
6814 (defun gnus-summary-jump-to-group (newsgroup)
6815   "Move point to NEWSGROUP in group mode buffer."
6816   ;; Keep update point of group mode buffer if visible.
6817   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6818       (save-window-excursion
6819         ;; Take care of tree window mode.
6820         (when (get-buffer-window gnus-group-buffer)
6821           (pop-to-buffer gnus-group-buffer))
6822         (gnus-group-jump-to-group newsgroup))
6823     (save-excursion
6824       ;; Take care of tree window mode.
6825       (if (get-buffer-window gnus-group-buffer 0)
6826           (pop-to-buffer gnus-group-buffer)
6827         (set-buffer gnus-group-buffer))
6828       (gnus-group-jump-to-group newsgroup))))
6829
6830 ;; This function returns a list of article numbers based on the
6831 ;; difference between the ranges of read articles in this group and
6832 ;; the range of active articles.
6833 (defun gnus-list-of-unread-articles (group)
6834   (let* ((read (gnus-info-read (gnus-get-info group)))
6835          (active (or (gnus-active group) (gnus-activate-group group)))
6836          (last (or (cdr active)
6837                    (error "Group %s couldn't be activated " group)))
6838          (bottom (if gnus-newsgroup-maximum-articles
6839                      (max (car active)
6840                           (- last gnus-newsgroup-maximum-articles -1))
6841                    (car active)))
6842          first nlast unread)
6843     ;; If none are read, then all are unread.
6844     (if (not read)
6845         (setq first bottom)
6846       ;; If the range of read articles is a single range, then the
6847       ;; first unread article is the article after the last read
6848       ;; article.  Sounds logical, doesn't it?
6849       (if (and (not (listp (cdr read)))
6850                (or (< (car read) bottom)
6851                    (progn (setq read (list read))
6852                           nil)))
6853           (setq first (max bottom (1+ (cdr read))))
6854         ;; `read' is a list of ranges.
6855         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6856                                   (caar read)))
6857                   1)
6858           (setq first bottom))
6859         (while read
6860           (when first
6861             (while (< first nlast)
6862               (setq unread (cons first unread)
6863                     first (1+ first))))
6864           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6865           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6866           (setq read (cdr read)))))
6867     ;; And add the last unread articles.
6868     (while (<= first last)
6869       (setq unread (cons first unread)
6870             first (1+ first)))
6871     ;; Return the list of unread articles.
6872     (delq 0 (nreverse unread))))
6873
6874 (defun gnus-list-of-read-articles (group)
6875   "Return a list of unread, unticked and non-dormant articles."
6876   (let* ((info (gnus-get-info group))
6877          (marked (gnus-info-marks info))
6878          (active (gnus-active group)))
6879     (and info active
6880          (gnus-list-range-difference
6881           (gnus-list-range-difference
6882            (gnus-sorted-complement
6883             (gnus-uncompress-range
6884              (if gnus-newsgroup-maximum-articles
6885                  (cons (max (car active)
6886                             (- (cdr active)
6887                                gnus-newsgroup-maximum-articles
6888                                -1))
6889                        (cdr active))
6890                active))
6891             (gnus-list-of-unread-articles group))
6892            (cdr (assq 'dormant marked)))
6893           (cdr (assq 'tick marked))))))
6894
6895 ;; This function returns a sequence of article numbers based on the
6896 ;; difference between the ranges of read articles in this group and
6897 ;; the range of active articles.
6898 (defun gnus-sequence-of-unread-articles (group)
6899   (let* ((read (gnus-info-read (gnus-get-info group)))
6900          (active (or (gnus-active group) (gnus-activate-group group)))
6901          (last (cdr active))
6902          (bottom (if gnus-newsgroup-maximum-articles
6903                      (max (car active)
6904                           (- last gnus-newsgroup-maximum-articles -1))
6905                    (car active)))
6906          first nlast unread)
6907     ;; If none are read, then all are unread.
6908     (if (not read)
6909         (setq first bottom)
6910       ;; If the range of read articles is a single range, then the
6911       ;; first unread article is the article after the last read
6912       ;; article.  Sounds logical, doesn't it?
6913       (if (and (not (listp (cdr read)))
6914                (or (< (car read) bottom)
6915                    (progn (setq read (list read))
6916                           nil)))
6917           (setq first (max bottom (1+ (cdr read))))
6918         ;; `read' is a list of ranges.
6919         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6920                                   (caar read)))
6921                   1)
6922           (setq first bottom))
6923         (while read
6924           (when first
6925             (push (cons first nlast) unread))
6926           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6927           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6928           (setq read (cdr read)))))
6929     ;; And add the last unread articles.
6930     (cond ((not (and first last))
6931            nil)
6932           ((< first last)
6933            (push (cons first last) unread))
6934           ((= first last)
6935            (push first unread)))
6936     ;; Return the sequence of unread articles.
6937     (delq 0 (nreverse unread))))
6938
6939 ;; Various summary commands
6940
6941 (defun gnus-summary-select-article-buffer ()
6942   "Reconfigure windows to show article buffer."
6943   (interactive)
6944   (if (not (gnus-buffer-live-p gnus-article-buffer))
6945       (error "There is no article buffer for this summary buffer")
6946     (gnus-configure-windows 'article)
6947     (select-window (get-buffer-window gnus-article-buffer))))
6948
6949 (defun gnus-summary-universal-argument (arg)
6950   "Perform any operation on all articles that are process/prefixed."
6951   (interactive "P")
6952   (let ((articles (gnus-summary-work-articles arg))
6953         func article)
6954     (if (eq
6955          (setq
6956           func
6957           (key-binding
6958            (read-key-sequence
6959             (substitute-command-keys
6960              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6961          'undefined)
6962         (gnus-error 1 "Undefined key")
6963       (save-excursion
6964         (while articles
6965           (gnus-summary-goto-subject (setq article (pop articles)))
6966           (let (gnus-newsgroup-processable)
6967             (command-execute func))
6968           (gnus-summary-remove-process-mark article)))))
6969   (gnus-summary-position-point))
6970
6971 (defun gnus-summary-toggle-truncation (&optional arg)
6972   "Toggle truncation of summary lines.
6973 With ARG, turn line truncation on if ARG is positive."
6974   (interactive "P")
6975   (setq truncate-lines
6976         (if (null arg) (not truncate-lines)
6977           (> (prefix-numeric-value arg) 0)))
6978   (redraw-display))
6979
6980 (defun gnus-summary-find-for-reselect ()
6981   "Return the number of an article to stay on across a reselect.
6982 The current article is considered, then following articles, then previous
6983 articles.  An article is sought which is not cancelled and isn't a temporary
6984 insertion from another group.  If there's no such then return a dummy 0."
6985   (let (found)
6986     (dolist (rev '(nil t))
6987       (unless found      ; don't demand the reverse list if we don't need it
6988         (let ((data (gnus-data-find-list
6989                      (gnus-summary-article-number) (gnus-data-list rev))))
6990           (while (and data (not found))
6991             (if (and (< 0 (gnus-data-number (car data)))
6992                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6993                 (setq found (gnus-data-number (car data))))
6994             (setq data (cdr data))))))
6995     (or found 0)))
6996
6997 (defun gnus-summary-reselect-current-group (&optional all rescan)
6998   "Exit and then reselect the current newsgroup.
6999 The prefix argument ALL means to select all articles."
7000   (interactive "P")
7001   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7002     (error "Ephemeral groups can't be reselected"))
7003   (let ((current-subject (gnus-summary-find-for-reselect))
7004         (group gnus-newsgroup-name))
7005     (setq gnus-newsgroup-begin nil)
7006     (gnus-summary-exit nil 'leave-hidden)
7007     ;; We have to adjust the point of group mode buffer because
7008     ;; point was moved to the next unread newsgroup by exiting.
7009     (gnus-summary-jump-to-group group)
7010     (when rescan
7011       (save-excursion
7012         (gnus-group-get-new-news-this-group 1)))
7013     (gnus-group-read-group all t)
7014     (gnus-summary-goto-subject current-subject nil t)))
7015
7016 (defun gnus-summary-rescan-group (&optional all)
7017   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7018   (interactive "P")
7019   (gnus-summary-reselect-current-group all t))
7020
7021 (defun gnus-summary-update-info (&optional non-destructive)
7022   (save-excursion
7023     (let ((group gnus-newsgroup-name))
7024       (when group
7025         (when gnus-newsgroup-kill-headers
7026           (setq gnus-newsgroup-killed
7027                 (gnus-compress-sequence
7028                  (gnus-sorted-union
7029                   (gnus-list-range-intersection
7030                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7031                   gnus-newsgroup-unreads)
7032                  t)))
7033         (unless (listp (cdr gnus-newsgroup-killed))
7034           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7035         (let ((headers gnus-newsgroup-headers))
7036           ;; Set the new ranges of read articles.
7037           (with-current-buffer gnus-group-buffer
7038             (gnus-undo-force-boundary))
7039           (gnus-update-read-articles
7040            group (gnus-sorted-union
7041                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7042           ;; Set the current article marks.
7043           (let ((gnus-newsgroup-scored
7044                  (if (and (not gnus-save-score)
7045                           (not non-destructive))
7046                      nil
7047                    gnus-newsgroup-scored)))
7048             (save-excursion
7049               (gnus-update-marks)))
7050           ;; Do the cross-ref thing.
7051           (when gnus-use-cross-reference
7052             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7053           ;; Do not switch windows but change the buffer to work.
7054           (set-buffer gnus-group-buffer)
7055           (unless (gnus-ephemeral-group-p group)
7056             (gnus-group-update-group group)))))))
7057
7058 (defun gnus-summary-save-newsrc (&optional force)
7059   "Save the current number of read/marked articles in the dribble buffer.
7060 The dribble buffer will then be saved.
7061 If FORCE (the prefix), also save the .newsrc file(s)."
7062   (interactive "P")
7063   (gnus-summary-update-info t)
7064   (if force
7065       (gnus-save-newsrc-file)
7066     (gnus-dribble-save)))
7067
7068 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7069
7070 (defun gnus-summary-exit (&optional temporary leave-hidden)
7071   "Exit reading current newsgroup, and then return to group selection mode.
7072 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7073   (interactive)
7074   (gnus-set-global-variables)
7075   (when (gnus-buffer-live-p gnus-article-buffer)
7076     (with-current-buffer gnus-article-buffer
7077       (mm-destroy-parts gnus-article-mime-handles)
7078       ;; Set it to nil for safety reason.
7079       (setq gnus-article-mime-handle-alist nil)
7080       (setq gnus-article-mime-handles nil)))
7081   (gnus-kill-save-kill-buffer)
7082   (gnus-async-halt-prefetch)
7083   (let* ((group gnus-newsgroup-name)
7084          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7085          (gnus-group-is-exiting-p t)
7086          (mode major-mode)
7087          (group-point nil)
7088          (buf (current-buffer)))
7089     (unless quit-config
7090       ;; Do adaptive scoring, and possibly save score files.
7091       (when gnus-newsgroup-adaptive
7092         (gnus-score-adaptive))
7093       (when gnus-use-scoring
7094         (gnus-score-save)))
7095     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7096     (when gnus-use-cache
7097       (gnus-cache-possibly-remove-articles)
7098       (gnus-cache-save-buffers))
7099     (gnus-async-prefetch-remove-group group)
7100     (when gnus-suppress-duplicates
7101       (gnus-dup-enter-articles))
7102     (when gnus-use-trees
7103       (gnus-tree-close group))
7104     (when gnus-use-cache
7105       (gnus-cache-write-active))
7106     ;; Remove entries for this group.
7107     (nnmail-purge-split-history (gnus-group-real-name group))
7108     ;; Make all changes in this group permanent.
7109     (unless quit-config
7110       (gnus-run-hooks 'gnus-exit-group-hook)
7111       (gnus-summary-update-info))
7112     (gnus-close-group group)
7113     ;; Make sure where we were, and go to next newsgroup.
7114     (set-buffer gnus-group-buffer)
7115     (unless quit-config
7116       (gnus-group-jump-to-group group))
7117     (gnus-run-hooks 'gnus-summary-exit-hook)
7118     (unless (or quit-config
7119                 (not gnus-summary-next-group-on-exit)
7120                 ;; If this group has disappeared from the summary
7121                 ;; buffer, don't skip forwards.
7122                 (not (string= group (gnus-group-group-name))))
7123       (gnus-group-next-unread-group 1))
7124     (setq group-point (point))
7125     (if temporary
7126         nil                             ;Nothing to do.
7127       (set-buffer buf)
7128       (if (not gnus-kill-summary-on-exit)
7129           (progn
7130             (gnus-deaden-summary)
7131             (setq mode nil))
7132         ;; We set all buffer-local variables to nil.  It is unclear why
7133         ;; this is needed, but if we don't, buffer-local variables are
7134         ;; not garbage-collected, it seems.  This would the lead to en
7135         ;; ever-growing Emacs.
7136         (gnus-summary-clear-local-variables)
7137         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7138           (gnus-summary-clear-local-variables))
7139         (when (get-buffer gnus-article-buffer)
7140           (bury-buffer gnus-article-buffer))
7141         ;; Return to group mode buffer.
7142         (when (eq mode 'gnus-summary-mode)
7143           (gnus-kill-buffer buf)))
7144
7145       ;; If we have several article buffers, we kill them at exit.
7146       (unless gnus-single-article-buffer
7147         (when (gnus-buffer-live-p gnus-article-buffer)
7148           (with-current-buffer gnus-article-buffer
7149             ;; Don't kill sticky article buffers
7150             (unless (eq major-mode 'gnus-sticky-article-mode)
7151               (gnus-kill-buffer gnus-article-buffer)
7152               (setq gnus-article-current nil))))
7153         (gnus-kill-buffer gnus-original-article-buffer))
7154
7155       (setq gnus-current-select-method gnus-select-method)
7156       (set-buffer gnus-group-buffer)
7157       (if quit-config
7158           (gnus-handle-ephemeral-exit quit-config)
7159         (goto-char group-point)
7160         ;; If gnus-group-buffer is already displayed, make sure we also move
7161         ;; the cursor in the window that displays it.
7162         (let ((win (get-buffer-window (current-buffer) 0)))
7163           (if win (set-window-point win (point))))
7164         (unless leave-hidden
7165           (gnus-configure-windows 'group 'force)))
7166       ;; Clear the current group name.
7167       (unless quit-config
7168         (setq gnus-newsgroup-name nil)))))
7169
7170 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7171 (defun gnus-summary-exit-no-update (&optional no-questions)
7172   "Quit reading current newsgroup without updating read article info."
7173   (interactive)
7174   (let* ((group gnus-newsgroup-name)
7175          (gnus-group-is-exiting-p t)
7176          (gnus-group-is-exiting-without-update-p t)
7177          (quit-config (gnus-group-quit-config group)))
7178     (when (or no-questions
7179               gnus-expert-user
7180               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7181       (gnus-async-halt-prefetch)
7182       (run-hooks 'gnus-summary-prepare-exit-hook)
7183       (when (gnus-buffer-live-p gnus-article-buffer)
7184         (with-current-buffer gnus-article-buffer
7185           (mm-destroy-parts gnus-article-mime-handles)
7186           ;; Set it to nil for safety reason.
7187           (setq gnus-article-mime-handle-alist nil)
7188           (setq gnus-article-mime-handles nil)))
7189       ;; If we have several article buffers, we kill them at exit.
7190       (unless gnus-single-article-buffer
7191         (gnus-kill-buffer gnus-article-buffer)
7192         (gnus-kill-buffer gnus-original-article-buffer)
7193         (setq gnus-article-current nil))
7194       (if (not gnus-kill-summary-on-exit)
7195           (gnus-deaden-summary)
7196         (gnus-close-group group)
7197         (gnus-summary-clear-local-variables)
7198         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7199           (gnus-summary-clear-local-variables))
7200         (gnus-kill-buffer gnus-summary-buffer))
7201       (unless gnus-single-article-buffer
7202         (setq gnus-article-current nil))
7203       (when gnus-use-trees
7204         (gnus-tree-close group))
7205       (gnus-async-prefetch-remove-group group)
7206       (when (get-buffer gnus-article-buffer)
7207         (bury-buffer gnus-article-buffer))
7208       ;; Return to the group buffer.
7209       (gnus-configure-windows 'group 'force)
7210       ;; Clear the current group name.
7211       (setq gnus-newsgroup-name nil)
7212       (unless (gnus-ephemeral-group-p group)
7213         (gnus-group-update-group group))
7214       (when (equal (gnus-group-group-name) group)
7215         (gnus-group-next-unread-group 1))
7216       (when quit-config
7217         (gnus-handle-ephemeral-exit quit-config)))))
7218
7219 (defun gnus-handle-ephemeral-exit (quit-config)
7220   "Handle movement when leaving an ephemeral group.
7221 The state which existed when entering the ephemeral is reset."
7222   (if (not (buffer-name (car quit-config)))
7223       (gnus-configure-windows 'group 'force)
7224     (set-buffer (car quit-config))
7225     (cond ((eq major-mode 'gnus-summary-mode)
7226            (gnus-set-global-variables))
7227           ((eq major-mode 'gnus-article-mode)
7228            (save-current-buffer
7229              ;; The `gnus-summary-buffer' variable may point
7230              ;; to the old summary buffer when using a single
7231              ;; article buffer.
7232              (unless (gnus-buffer-live-p gnus-summary-buffer)
7233                (set-buffer gnus-group-buffer))
7234              (set-buffer gnus-summary-buffer)
7235              (gnus-set-global-variables))))
7236     (if (or (eq (cdr quit-config) 'article)
7237             (eq (cdr quit-config) 'pick))
7238         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7239             (gnus-configure-windows 'pick 'force)
7240           (gnus-configure-windows (cdr quit-config) 'force))
7241       (gnus-configure-windows (cdr quit-config) 'force))
7242     (when (eq major-mode 'gnus-summary-mode)
7243       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7244                                                      next-unread-noselect))
7245           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7246                                   'next-noselect)
7247                               (gnus-summary-next-subject 1 nil t))
7248                              ((eq gnus-auto-select-on-ephemeral-exit
7249                                   'next-unread-noselect)
7250                               (gnus-summary-next-subject 1 t t))))
7251             ;; Hide the article buffer which displays the article different
7252             ;; from the one that the cursor points to in the summary buffer.
7253             (gnus-configure-windows 'summary 'force))
7254         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7255                (gnus-summary-next-subject 1))
7256               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7257                (gnus-summary-next-subject 1 t))))
7258       (gnus-summary-recenter)
7259       (gnus-summary-position-point))))
7260
7261 ;;; Dead summaries.
7262
7263 (defvar gnus-dead-summary-mode-map
7264   (let ((map (make-keymap)))
7265     (suppress-keymap map)
7266     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7267     (dolist (key '("\C-d" "\r" "\177" [delete]))
7268       (define-key map key 'gnus-summary-wake-up-the-dead))
7269     (dolist (key '("q" "Q"))
7270       (define-key map key 'bury-buffer))
7271     map))
7272
7273 (define-minor-mode gnus-dead-summary-mode
7274   "Minor mode for Gnus summary buffers."
7275   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7276   (unless (derived-mode-p 'gnus-summary-mode)
7277     (setq gnus-dead-summary-mode nil)))
7278
7279 (defun gnus-deaden-summary ()
7280   "Make the current summary buffer into a dead summary buffer."
7281   ;; Kill any previous dead summary buffer.
7282   (when (and gnus-dead-summary
7283              (buffer-name gnus-dead-summary))
7284     (with-current-buffer gnus-dead-summary
7285       (when gnus-dead-summary-mode
7286         (kill-buffer (current-buffer)))))
7287   ;; Make this the current dead summary.
7288   (setq gnus-dead-summary (current-buffer))
7289   (gnus-dead-summary-mode 1)
7290   (let ((name (buffer-name)))
7291     (when (string-match "Summary" name)
7292       (rename-buffer
7293        (concat (substring name 0 (match-beginning 0)) "Dead "
7294                (substring name (match-beginning 0)))
7295        t)
7296       (bury-buffer))))
7297
7298 (defun gnus-kill-or-deaden-summary (buffer)
7299   "Kill or deaden the summary BUFFER."
7300   (save-excursion
7301     (when (and (buffer-name buffer)
7302                (not gnus-single-article-buffer))
7303       (with-current-buffer buffer
7304         (gnus-kill-buffer gnus-article-buffer)
7305         (gnus-kill-buffer gnus-original-article-buffer)))
7306     (cond
7307      ;; Kill the buffer.
7308      (gnus-kill-summary-on-exit
7309       (when (and gnus-use-trees
7310                  (gnus-buffer-exists-p buffer))
7311         (with-current-buffer buffer
7312           (gnus-tree-close gnus-newsgroup-name)))
7313       (gnus-kill-buffer buffer))
7314      ;; Deaden the buffer.
7315      ((gnus-buffer-exists-p buffer)
7316       (with-current-buffer buffer
7317         (gnus-deaden-summary))))))
7318
7319 (defun gnus-summary-wake-up-the-dead (&rest args)
7320   "Wake up the dead summary buffer."
7321   (interactive)
7322   (gnus-dead-summary-mode -1)
7323   (let ((name (buffer-name)))
7324     (when (string-match "Dead " name)
7325       (rename-buffer
7326        (concat (substring name 0 (match-beginning 0))
7327                (substring name (match-end 0)))
7328        t)))
7329   (gnus-message 3 "This dead summary is now alive again"))
7330
7331 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7332 (defun gnus-summary-describe-group (&optional force)
7333   "Describe the current newsgroup."
7334   (interactive "P")
7335   (gnus-group-describe-group force gnus-newsgroup-name))
7336
7337 (defun gnus-summary-describe-briefly ()
7338   "Describe summary mode commands briefly."
7339   (interactive)
7340   (gnus-message 6 "%s" (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info   \\[gnus-summary-describe-briefly]:This help")))
7341
7342 ;; Walking around group mode buffer from summary mode.
7343
7344 (defun gnus-summary-next-group (&optional no-article target-group backward)
7345   "Exit current newsgroup and then select next unread newsgroup.
7346 If prefix argument NO-ARTICLE is non-nil, no article is selected
7347 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7348 previous group instead."
7349   (interactive "P")
7350   ;; Stop pre-fetching.
7351   (gnus-async-halt-prefetch)
7352   (let ((current-group gnus-newsgroup-name)
7353         (current-buffer (current-buffer))
7354         entered)
7355     ;; First we semi-exit this group to update Xrefs and all variables.
7356     ;; We can't do a real exit, because the window conf must remain
7357     ;; the same in case the user is prompted for info, and we don't
7358     ;; want the window conf to change before that...
7359     (gnus-summary-exit t)
7360     (while (not entered)
7361       ;; Then we find what group we are supposed to enter.
7362       (set-buffer gnus-group-buffer)
7363       (gnus-group-jump-to-group current-group)
7364       (setq target-group
7365             (or target-group
7366                 (if (eq gnus-keep-same-level 'best)
7367                     (gnus-summary-best-group gnus-newsgroup-name)
7368                   (gnus-summary-search-group backward gnus-keep-same-level))))
7369       (if (not target-group)
7370           ;; There are no further groups, so we return to the group
7371           ;; buffer.
7372           (progn
7373             (gnus-message 5 "Returning to the group buffer")
7374             (setq entered t)
7375             (when (gnus-buffer-live-p current-buffer)
7376               (set-buffer current-buffer)
7377               (gnus-summary-exit))
7378             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7379         ;; We try to enter the target group.
7380         (gnus-group-jump-to-group target-group)
7381         (let ((unreads (gnus-group-group-unread)))
7382           (if (and (or (eq t unreads)
7383                        (and unreads (not (zerop unreads))))
7384                    (gnus-summary-read-group
7385                     target-group nil no-article
7386                     (and (buffer-name current-buffer) current-buffer)
7387                     nil backward))
7388               (setq entered t)
7389             (setq current-group target-group
7390                   target-group nil)))))))
7391
7392 (defun gnus-summary-prev-group (&optional no-article)
7393   "Exit current newsgroup and then select previous unread newsgroup.
7394 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7395   (interactive "P")
7396   (gnus-summary-next-group no-article nil t))
7397
7398 ;; Walking around summary lines.
7399
7400 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7401   "Go to the first subject satisfying any non-nil constraint.
7402 If UNREAD is non-nil, the article should be unread.
7403 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7404 If UNSEEN is non-nil, the article should be unseen as well as unread.
7405 Returns the article selected or nil if there are no matching articles."
7406   (interactive "P")
7407   (cond
7408    ;; Empty summary.
7409    ((null gnus-newsgroup-data)
7410     (gnus-message 3 "No articles in the group")
7411     nil)
7412    ;; Pick the first article.
7413    ((not (or unread undownloaded unseen))
7414     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7415     (gnus-data-number (car gnus-newsgroup-data)))
7416    ;; Find the first unread article.
7417    (t
7418     (let ((data gnus-newsgroup-data))
7419       (while (and data
7420                   (let ((num (gnus-data-number (car data))))
7421                     (or (memq num gnus-newsgroup-unfetched)
7422                         (not (or (and unread
7423                                       (memq num gnus-newsgroup-unreads))
7424                                  (and undownloaded
7425                                       (memq num gnus-newsgroup-undownloaded))
7426                                  (and unseen
7427                                       (memq num gnus-newsgroup-unseen)
7428                                       (memq num gnus-newsgroup-unreads)))))))
7429         (setq data (cdr data)))
7430       (prog1
7431           (if data
7432               (progn
7433                 (goto-char (gnus-data-pos (car data)))
7434                 (gnus-data-number (car data)))
7435             (gnus-message 3 "No more%s articles"
7436                           (let* ((r (when unread " unread"))
7437                                  (d (when undownloaded " undownloaded"))
7438                                  (s (when unseen " unseen"))
7439                                  (l (delq nil (list r d s))))
7440                             (cond ((= 3 (length l))
7441                                    (concat r "," d ", or" s))
7442                                   ((= 2 (length l))
7443                                    (concat (car l) ", or" (cadr l)))
7444                                   ((= 1 (length l))
7445                                    (car l))
7446                                   (t
7447                                    ""))))
7448             nil
7449             )
7450         (gnus-summary-position-point))))))
7451
7452 (defun gnus-summary-next-subject (n &optional unread dont-display)
7453   "Go to next N'th summary line.
7454 If N is negative, go to the previous N'th subject line.
7455 If UNREAD is non-nil, only unread articles are selected.
7456 The difference between N and the actual number of steps taken is
7457 returned."
7458   (interactive "p")
7459   (let ((backward (< n 0))
7460         (n (abs n)))
7461     (while (and (> n 0)
7462                 (if backward
7463                     (gnus-summary-find-prev unread)
7464                   (gnus-summary-find-next unread)))
7465       (unless (zerop (setq n (1- n)))
7466         (gnus-summary-show-thread)))
7467     (when (/= 0 n)
7468       (gnus-message 7 "No more%s articles"
7469                     (if unread " unread" "")))
7470     (unless dont-display
7471       (gnus-summary-recenter)
7472       (gnus-summary-position-point))
7473     n))
7474
7475 (defun gnus-summary-next-unread-subject (n)
7476   "Go to next N'th unread summary line."
7477   (interactive "p")
7478   (gnus-summary-next-subject n t))
7479
7480 (defun gnus-summary-prev-subject (n &optional unread)
7481   "Go to previous N'th summary line.
7482 If optional argument UNREAD is non-nil, only unread article is selected."
7483   (interactive "p")
7484   (gnus-summary-next-subject (- n) unread))
7485
7486 (defun gnus-summary-prev-unread-subject (n)
7487   "Go to previous N'th unread summary line."
7488   (interactive "p")
7489   (gnus-summary-next-subject (- n) t))
7490
7491 (defun gnus-summary-goto-subjects (articles)
7492   "Insert the subject header for ARTICLES in the current buffer."
7493   (save-excursion
7494     (dolist (article articles)
7495       (gnus-summary-goto-subject article t)))
7496   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7497   (gnus-summary-position-point))
7498
7499 (defun gnus-summary-goto-subject (article &optional force silent)
7500   "Go to the subject line of ARTICLE.
7501 If FORCE, also allow jumping to articles not currently shown."
7502   (interactive "nArticle number: ")
7503   (unless (numberp article)
7504     (error "Article %s is not a number" article))
7505   (let ((b (point))
7506         (data (gnus-data-find article)))
7507     ;; We read in the article if we have to.
7508     (and (not data)
7509          force
7510          (gnus-summary-insert-subject
7511           article
7512           (if (or (numberp force) (vectorp force)) force)
7513           t)
7514          (setq data (gnus-data-find article)))
7515     (goto-char b)
7516     (if (not data)
7517         (progn
7518           (unless silent
7519             (gnus-message 3 "Can't find article %d" article))
7520           nil)
7521       (let ((pt (gnus-data-pos data)))
7522         (goto-char pt)
7523         (gnus-summary-set-article-display-arrow pt))
7524       (gnus-summary-position-point)
7525       article)))
7526
7527 ;; Walking around summary lines with displaying articles.
7528
7529 (defun gnus-summary-expand-window (&optional arg)
7530   "Make the summary buffer take up the entire Emacs frame.
7531 Given a prefix, will force an `article' buffer configuration."
7532   (interactive "P")
7533   (if arg
7534       (gnus-configure-windows 'article 'force)
7535     (gnus-configure-windows 'summary 'force)))
7536
7537 (defun gnus-summary-display-article (article &optional all-header)
7538   "Display ARTICLE in article buffer."
7539   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7540                (with-current-buffer gnus-article-buffer
7541                  (eq major-mode 'gnus-article-mode)))
7542     (gnus-article-setup-buffer))
7543   (gnus-set-global-variables)
7544   (with-current-buffer gnus-article-buffer
7545     (setq gnus-article-charset gnus-newsgroup-charset)
7546     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7547     (mm-enable-multibyte))
7548   (if (null article)
7549       nil
7550     (prog1
7551         (if gnus-summary-display-article-function
7552             (funcall gnus-summary-display-article-function article all-header)
7553           (gnus-article-prepare article all-header))
7554       (gnus-run-hooks 'gnus-select-article-hook)
7555       (when (and gnus-current-article
7556                  (not (zerop gnus-current-article)))
7557         (gnus-summary-goto-subject gnus-current-article))
7558       (gnus-summary-recenter)
7559       (when (and gnus-use-trees gnus-show-threads)
7560         (gnus-possibly-generate-tree article)
7561         (gnus-highlight-selected-tree article))
7562       ;; Successfully display article.
7563       (gnus-article-set-window-start
7564        (cdr (assq article gnus-newsgroup-bookmarks))))))
7565
7566 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7567   "Select the current article.
7568 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7569 non-nil, the article will be re-fetched even if it already present in
7570 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7571 be displayed."
7572   ;; Make sure we are in the summary buffer to work around bbdb bug.
7573   (unless (eq major-mode 'gnus-summary-mode)
7574     (set-buffer gnus-summary-buffer))
7575   (let ((article (or article (gnus-summary-article-number)))
7576         (all-headers (not (not all-headers))) ;Must be t or nil.
7577         gnus-summary-display-article-function)
7578     (and (not pseudo)
7579          (gnus-summary-article-pseudo-p article)
7580          (error "This is a pseudo-article"))
7581     (with-current-buffer gnus-summary-buffer
7582       (if (or (and gnus-single-article-buffer
7583                    (or (null gnus-current-article)
7584                        (null gnus-article-current)
7585                        (null (get-buffer gnus-article-buffer))
7586                        (not (eq article (cdr gnus-article-current)))
7587                        (not (equal (car gnus-article-current)
7588                                    gnus-newsgroup-name))))
7589               (and (not gnus-single-article-buffer)
7590                    (or (null gnus-current-article)
7591                        (not (eq gnus-current-article article))))
7592               force)
7593           ;; The requested article is different from the current article.
7594           (progn
7595             (gnus-summary-display-article article all-headers)
7596             (when (gnus-buffer-live-p gnus-article-buffer)
7597               (with-current-buffer gnus-article-buffer
7598                 (if (not gnus-article-decoded-p) ;; a local variable
7599                     (mm-disable-multibyte))))
7600             (gnus-article-set-window-start
7601              (cdr (assq article gnus-newsgroup-bookmarks)))
7602             article)
7603         'old))))
7604
7605 (defun gnus-summary-force-verify-and-decrypt ()
7606   "Display buttons for signed/encrypted parts and verify/decrypt them."
7607   (interactive)
7608   (let ((mm-verify-option 'known)
7609         (mm-decrypt-option 'known)
7610         (gnus-article-emulate-mime t)
7611         (gnus-buttonized-mime-types (append (list "multipart/signed"
7612                                                   "multipart/encrypted")
7613                                             gnus-buttonized-mime-types)))
7614     (gnus-summary-select-article nil 'force)))
7615
7616 (defun gnus-summary-set-current-mark (&optional current-mark)
7617   "Obsolete function."
7618   nil)
7619
7620 (defun gnus-summary-next-article (&optional unread subject backward push)
7621   "Select the next article.
7622 If UNREAD, only unread articles are selected.
7623 If SUBJECT, only articles with SUBJECT are selected.
7624 If BACKWARD, the previous article is selected instead of the next."
7625   (interactive "P")
7626   ;; Make sure we are in the summary buffer.
7627   (unless (eq major-mode 'gnus-summary-mode)
7628     (set-buffer gnus-summary-buffer))
7629   (cond
7630    ;; Is there such an article?
7631    ((and (gnus-summary-search-forward unread subject backward)
7632          (or (gnus-summary-display-article (gnus-summary-article-number))
7633              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7634     (gnus-summary-position-point))
7635    ;; If not, we try the first unread, if that is wanted.
7636    ((and subject
7637          gnus-auto-select-same
7638          (gnus-summary-first-unread-article))
7639     (gnus-summary-position-point)
7640     (gnus-message 6 "Wrapped"))
7641    ;; Try to get next/previous article not displayed in this group.
7642    ((and gnus-auto-extend-newsgroup
7643          (not unread) (not subject))
7644     (gnus-summary-goto-article
7645      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7646      nil (count-lines (point-min) (point))))
7647    ;; Go to next/previous group.
7648    (t
7649     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7650       (gnus-summary-jump-to-group gnus-newsgroup-name))
7651     (let ((cmd (if (featurep 'xemacs)
7652                    last-command-char
7653                  last-command-event))
7654           (point
7655            (with-current-buffer gnus-group-buffer
7656              (point)))
7657           (group
7658            (if (eq gnus-keep-same-level 'best)
7659                (gnus-summary-best-group gnus-newsgroup-name)
7660              (gnus-summary-search-group backward gnus-keep-same-level))))
7661       ;; For some reason, the group window gets selected.  We change
7662       ;; it back.
7663       (select-window (get-buffer-window (current-buffer)))
7664       ;; Select next unread newsgroup automagically.
7665       (cond
7666        ((or (not gnus-auto-select-next)
7667             (not cmd))
7668         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7669        ((or (eq gnus-auto-select-next 'quietly)
7670             (and (eq gnus-auto-select-next 'slightly-quietly)
7671                  push)
7672             (and (eq gnus-auto-select-next 'almost-quietly)
7673                  (gnus-summary-last-article-p)))
7674         ;; Select quietly.
7675         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7676             (gnus-summary-exit)
7677           (gnus-message 7 "No more%s articles (%s)..."
7678                         (if unread " unread" "")
7679                         (if group (concat "selecting " group)
7680                           "exiting"))
7681           (gnus-summary-next-group nil group backward)))
7682        (t
7683         (when (gnus-key-press-event-p last-input-event)
7684           (gnus-summary-walk-group-buffer
7685            gnus-newsgroup-name cmd unread backward point))))))))
7686
7687 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7688   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7689                       (?\C-p (gnus-group-prev-unread-group 1))))
7690         (cursor-in-echo-area t)
7691         keve key group ended prompt)
7692     (with-current-buffer gnus-group-buffer
7693       (goto-char start)
7694       (setq group
7695             (if (eq gnus-keep-same-level 'best)
7696                 (gnus-summary-best-group gnus-newsgroup-name)
7697               (gnus-summary-search-group backward gnus-keep-same-level))))
7698     (while (not ended)
7699       (setq prompt
7700             (format
7701              "No more%s articles%s " (if unread " unread" "")
7702              (if (and group
7703                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7704                  (format " (Type %s for %s [%s])"
7705                          (single-key-description cmd)
7706                          (gnus-group-decoded-name group)
7707                          (gnus-group-unread group))
7708                (format " (Type %s to exit %s)"
7709                        (single-key-description cmd)
7710                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7711       ;; Confirm auto selection.
7712       (setq key (car (setq keve (gnus-read-event-char prompt)))
7713             ended t)
7714       (cond
7715        ((assq key keystrokes)
7716         (let ((obuf (current-buffer)))
7717           (switch-to-buffer gnus-group-buffer)
7718           (when group
7719             (gnus-group-jump-to-group group))
7720           (eval (cadr (assq key keystrokes)))
7721           (setq group (gnus-group-group-name))
7722           (switch-to-buffer obuf))
7723         (setq ended nil))
7724        ((equal key cmd)
7725         (if (or (not group)
7726                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7727             (gnus-summary-exit)
7728           (gnus-summary-next-group nil group backward)))
7729        (t
7730         (push (cdr keve) unread-command-events))))))
7731
7732 (defun gnus-summary-next-unread-article ()
7733   "Select unread article after current one."
7734   (interactive)
7735   (gnus-summary-next-article
7736    (or (not (eq gnus-summary-goto-unread 'never))
7737        (gnus-summary-last-article-p (gnus-summary-article-number)))
7738    (and gnus-auto-select-same
7739         (gnus-summary-article-subject))))
7740
7741 (defun gnus-summary-prev-article (&optional unread subject)
7742   "Select the article before the current one.
7743 If UNREAD is non-nil, only unread articles are selected."
7744   (interactive "P")
7745   (gnus-summary-next-article unread subject t))
7746
7747 (defun gnus-summary-prev-unread-article ()
7748   "Select unread article before current one."
7749   (interactive)
7750   (gnus-summary-prev-article
7751    (or (not (eq gnus-summary-goto-unread 'never))
7752        (gnus-summary-first-article-p (gnus-summary-article-number)))
7753    (and gnus-auto-select-same
7754         (gnus-summary-article-subject))))
7755
7756 (defun gnus-summary-next-page (&optional lines circular stop)
7757   "Show next page of the selected article.
7758 If at the end of the current article, select the next article.
7759 LINES says how many lines should be scrolled up.
7760
7761 If CIRCULAR is non-nil, go to the start of the article instead of
7762 selecting the next article when reaching the end of the current
7763 article.
7764
7765 If STOP is non-nil, just stop when reaching the end of the message.
7766
7767 Also see the variable `gnus-article-skip-boring'."
7768   (interactive "P")
7769   (setq gnus-summary-buffer (current-buffer))
7770   (gnus-set-global-variables)
7771   (let ((article (gnus-summary-article-number))
7772         (article-window (get-buffer-window gnus-article-buffer t))
7773         endp)
7774     ;; If the buffer is empty, we have no article.
7775     (unless article
7776       (error "No article to select"))
7777     (gnus-configure-windows 'article)
7778     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7779         (if (and (eq gnus-summary-goto-unread 'never)
7780                  (not (gnus-summary-last-article-p article)))
7781             (gnus-summary-next-article)
7782           (gnus-summary-next-unread-article))
7783       (if (or (null gnus-current-article)
7784               (null gnus-article-current)
7785               (/= article (cdr gnus-article-current))
7786               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7787           ;; Selected subject is different from current article's.
7788           (gnus-summary-display-article article)
7789         (when article-window
7790           (gnus-eval-in-buffer-window gnus-article-buffer
7791             (setq endp (or (gnus-article-next-page lines)
7792                            (gnus-article-only-boring-p))))
7793           (when endp
7794             (cond ((or stop gnus-summary-stop-at-end-of-message)
7795                    (gnus-message 3 "End of message"))
7796                   (circular
7797                    (gnus-summary-beginning-of-article))
7798                   (lines
7799                    (gnus-message 3 "End of message"))
7800                   ((null lines)
7801                    (if (and (eq gnus-summary-goto-unread 'never)
7802                             (not (gnus-summary-last-article-p article)))
7803                        (gnus-summary-next-article)
7804                      (gnus-summary-next-unread-article))))))))
7805     (gnus-summary-recenter)
7806     (gnus-summary-position-point)))
7807
7808 (defun gnus-summary-prev-page (&optional lines move)
7809   "Show previous page of selected article.
7810 Argument LINES specifies lines to be scrolled down.
7811 If MOVE, move to the previous unread article if point is at
7812 the beginning of the buffer."
7813   (interactive "P")
7814   (let ((article (gnus-summary-article-number))
7815         (article-window (get-buffer-window gnus-article-buffer t))
7816         endp)
7817     (gnus-configure-windows 'article)
7818     (if (or (null gnus-current-article)
7819             (null gnus-article-current)
7820             (/= article (cdr gnus-article-current))
7821             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7822         ;; Selected subject is different from current article's.
7823         (gnus-summary-display-article article)
7824       (gnus-summary-recenter)
7825       (when article-window
7826         (gnus-eval-in-buffer-window gnus-article-buffer
7827           (setq endp (gnus-article-prev-page lines)))
7828         (when (and move endp)
7829           (cond (lines
7830                  (gnus-message 3 "Beginning of message"))
7831                 ((null lines)
7832                  (if (and (eq gnus-summary-goto-unread 'never)
7833                           (not (gnus-summary-first-article-p article)))
7834                      (gnus-summary-prev-article)
7835                    (gnus-summary-prev-unread-article))))))))
7836   (gnus-summary-position-point))
7837
7838 (defun gnus-summary-prev-page-or-article (&optional lines)
7839   "Show previous page of selected article.
7840 Argument LINES specifies lines to be scrolled down.
7841 If at the beginning of the article, go to the next article."
7842   (interactive "P")
7843   (gnus-summary-prev-page lines t))
7844
7845 (defun gnus-summary-scroll-up (lines)
7846   "Scroll up (or down) one line current article.
7847 Argument LINES specifies lines to be scrolled up (or down if negative).
7848 If no article is selected, then the current article will be selected first."
7849   (interactive "p")
7850   (gnus-configure-windows 'article)
7851   (gnus-summary-show-thread)
7852   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7853     (gnus-eval-in-buffer-window gnus-article-buffer
7854       (cond ((> lines 0)
7855              (when (gnus-article-next-page lines)
7856                (gnus-message 3 "End of message")))
7857             ((< lines 0)
7858              (gnus-article-prev-page (- lines))))))
7859   (gnus-summary-recenter)
7860   (gnus-summary-position-point))
7861
7862 (defun gnus-summary-scroll-down (lines)
7863   "Scroll down (or up) one line current article.
7864 Argument LINES specifies lines to be scrolled down (or up if negative).
7865 If no article is selected, then the current article will be selected first."
7866   (interactive "p")
7867   (gnus-summary-scroll-up (- lines)))
7868
7869 (defun gnus-summary-next-same-subject ()
7870   "Select next article which has the same subject as current one."
7871   (interactive)
7872   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7873
7874 (defun gnus-summary-prev-same-subject ()
7875   "Select previous article which has the same subject as current one."
7876   (interactive)
7877   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7878
7879 (defun gnus-summary-next-unread-same-subject ()
7880   "Select next unread article which has the same subject as current one."
7881   (interactive)
7882   (gnus-summary-next-article t (gnus-summary-article-subject)))
7883
7884 (defun gnus-summary-prev-unread-same-subject ()
7885   "Select previous unread article which has the same subject as current one."
7886   (interactive)
7887   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7888
7889 (defun gnus-summary-first-unread-article ()
7890   "Select the first unread article.
7891 Return nil if there are no unread articles."
7892   (interactive)
7893   (prog1
7894       (when (gnus-summary-first-subject t)
7895         (gnus-summary-show-thread)
7896         (gnus-summary-first-subject t)
7897         (gnus-summary-display-article (gnus-summary-article-number)))
7898     (gnus-summary-position-point)))
7899
7900 (defun gnus-summary-first-unread-subject ()
7901   "Place the point on the subject line of the first unread article.
7902 Return nil if there are no unread articles."
7903   (interactive)
7904   (prog1
7905       (when (gnus-summary-first-subject t)
7906         (gnus-summary-show-thread)
7907         (gnus-summary-first-subject t))
7908     (gnus-summary-position-point)))
7909
7910 (defun gnus-summary-first-unseen-subject ()
7911   "Place the point on the subject line of the first unseen article.
7912 Return nil if there are no unseen articles."
7913   (interactive)
7914   (prog1
7915       (when (gnus-summary-first-subject nil nil t)
7916         (gnus-summary-show-thread)
7917         (gnus-summary-first-subject nil nil t))
7918     (gnus-summary-position-point)))
7919
7920 (defun gnus-summary-first-unseen-or-unread-subject ()
7921   "Place the point on the subject line of the first unseen and unread article.
7922 If all article have been seen, on the subject line of the first unread
7923 article."
7924   (interactive)
7925   (prog1
7926       (unless (when (gnus-summary-first-subject nil nil t)
7927                 (gnus-summary-show-thread)
7928                 (gnus-summary-first-subject nil nil t))
7929         (when (gnus-summary-first-subject t)
7930           (gnus-summary-show-thread)
7931           (gnus-summary-first-subject t)))
7932     (gnus-summary-position-point)))
7933
7934 (defun gnus-summary-first-article ()
7935   "Select the first article.
7936 Return nil if there are no articles."
7937   (interactive)
7938   (prog1
7939       (when (gnus-summary-first-subject)
7940         (gnus-summary-show-thread)
7941         (gnus-summary-first-subject)
7942         (gnus-summary-display-article (gnus-summary-article-number)))
7943     (gnus-summary-position-point)))
7944
7945 (defun gnus-summary-best-unread-article (&optional arg)
7946   "Select the unread article with the highest score.
7947 If given a prefix argument, select the next unread article that has a
7948 score higher than the default score."
7949   (interactive "P")
7950   (let ((article (if arg
7951                      (gnus-summary-better-unread-subject)
7952                    (gnus-summary-best-unread-subject))))
7953     (if article
7954         (gnus-summary-goto-article article)
7955       (error "No unread articles"))))
7956
7957 (defun gnus-summary-best-unread-subject ()
7958   "Select the unread subject with the highest score."
7959   (interactive)
7960   (let ((best -1000000)
7961         (data gnus-newsgroup-data)
7962         article score)
7963     (while data
7964       (and (gnus-data-unread-p (car data))
7965            (> (setq score
7966                     (gnus-summary-article-score (gnus-data-number (car data))))
7967               best)
7968            (setq best score
7969                  article (gnus-data-number (car data))))
7970       (setq data (cdr data)))
7971     (when article
7972       (gnus-summary-goto-subject article))
7973     (gnus-summary-position-point)
7974     article))
7975
7976 (defun gnus-summary-better-unread-subject ()
7977   "Select the first unread subject that has a score over the default score."
7978   (interactive)
7979   (let ((data gnus-newsgroup-data)
7980         article score)
7981     (while (and (setq article (gnus-data-number (car data)))
7982                 (or (gnus-data-read-p (car data))
7983                     (not (> (gnus-summary-article-score article)
7984                             gnus-summary-default-score))))
7985       (setq data (cdr data)))
7986     (when article
7987       (gnus-summary-goto-subject article))
7988     (gnus-summary-position-point)
7989     article))
7990
7991 (defun gnus-summary-last-subject ()
7992   "Go to the last displayed subject line in the group."
7993   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7994     (when article
7995       (gnus-summary-goto-subject article))))
7996
7997 (defun gnus-summary-goto-article (article &optional all-headers force)
7998   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7999 If ALL-HEADERS is non-nil, no header lines are hidden.
8000 If FORCE, go to the article even if it isn't displayed.  If FORCE
8001 is a number, it is the line the article is to be displayed on."
8002   (interactive
8003    (list
8004     (gnus-completing-read
8005      "Article number or Message-ID"
8006      (mapcar 'int-to-string gnus-newsgroup-limit))
8007     current-prefix-arg
8008     t))
8009   (prog1
8010       (if (and (stringp article)
8011                (string-match "@\\|%40" article))
8012           (gnus-summary-refer-article article)
8013         (when (stringp article)
8014           (setq article (string-to-number article)))
8015         (if (gnus-summary-goto-subject article force)
8016             (gnus-summary-display-article article all-headers)
8017           (gnus-message 4 "Couldn't go to article %s" article) nil))
8018     (gnus-summary-position-point)))
8019
8020 (defun gnus-summary-goto-last-article ()
8021   "Go to the previously read article."
8022   (interactive)
8023   (prog1
8024       (when gnus-last-article
8025         (gnus-summary-goto-article gnus-last-article nil t))
8026     (gnus-summary-position-point)))
8027
8028 (defun gnus-summary-pop-article (number)
8029   "Pop one article off the history and go to the previous.
8030 NUMBER articles will be popped off."
8031   (interactive "p")
8032   (let (to)
8033     (setq gnus-newsgroup-history
8034           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8035     (if to
8036         (gnus-summary-goto-article (car to) nil t)
8037       (error "Article history empty")))
8038   (gnus-summary-position-point))
8039
8040 ;; Summary commands and functions for limiting the summary buffer.
8041
8042 (defun gnus-summary-limit-to-articles (n)
8043   "Limit the summary buffer to the next N articles.
8044 If not given a prefix, use the process marked articles instead."
8045   (interactive "P")
8046   (prog1
8047       (let ((articles (gnus-summary-work-articles n)))
8048         (setq gnus-newsgroup-processable nil)
8049         (gnus-summary-limit articles))
8050     (gnus-summary-position-point)))
8051
8052 (defun gnus-summary-pop-limit (&optional total)
8053   "Restore the previous limit.
8054 If given a prefix, remove all limits."
8055   (interactive "P")
8056   (when total
8057     (setq gnus-newsgroup-limits
8058           (list (mapcar (lambda (h) (mail-header-number h))
8059                         gnus-newsgroup-headers))))
8060   (unless gnus-newsgroup-limits
8061     (error "No limit to pop"))
8062   (prog1
8063       (gnus-summary-limit nil 'pop)
8064     (gnus-summary-position-point)))
8065
8066 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8067   "Limit the summary buffer to articles that have subjects that match a regexp.
8068 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8069   (interactive
8070    (list (read-string (if current-prefix-arg
8071                           "Exclude subject (regexp): "
8072                         "Limit to subject (regexp): "))
8073          nil current-prefix-arg))
8074   (unless header
8075     (setq header "subject"))
8076   (when (not (equal "" subject))
8077     (prog1
8078         (let ((articles (gnus-summary-find-matching
8079                          (or header "subject") subject 'all nil nil
8080                          not-matching)))
8081           (unless articles
8082             (error "Found no matches for \"%s\"" subject))
8083           (gnus-summary-limit articles))
8084       (gnus-summary-position-point))))
8085
8086 (defun gnus-summary-limit-to-author (from &optional not-matching)
8087   "Limit the summary buffer to articles that have authors that match a regexp.
8088 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8089   (interactive
8090    (list (read-string (if current-prefix-arg
8091                           "Exclude author (regexp): "
8092                         "Limit to author (regexp): "))
8093          current-prefix-arg))
8094   (gnus-summary-limit-to-subject from "from" not-matching))
8095
8096 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8097   "Limit the summary buffer to articles with the given RECIPIENT.
8098
8099 If NOT-MATCHING, exclude RECIPIENT.
8100
8101 To and Cc headers are checked.  You need to include them in
8102 `nnmail-extra-headers'."
8103   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8104   (interactive
8105    (list (read-string (format "%s recipient (regexp): "
8106                               (if current-prefix-arg "Exclude" "Limit to")))
8107          current-prefix-arg))
8108   (when (not (equal "" recipient))
8109     (prog1 (let* ((to
8110                    (if (memq 'To nnmail-extra-headers)
8111                        (gnus-summary-find-matching
8112                         (cons 'extra 'To) recipient 'all nil nil
8113                         not-matching)
8114                      (gnus-message
8115                       1 "`To' isn't present in `nnmail-extra-headers'")
8116                      (sit-for 1)
8117                      nil))
8118                   (cc
8119                    (if (memq 'Cc nnmail-extra-headers)
8120                        (gnus-summary-find-matching
8121                         (cons 'extra 'Cc) recipient 'all nil nil
8122                         not-matching)
8123                      (gnus-message
8124                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8125                      (sit-for 1)
8126                      nil))
8127                   (articles
8128                    (if not-matching
8129                        ;; We need the numbers that are in both lists:
8130                        (mapcar (lambda (a)
8131                                  (and (memq a to) a))
8132                                cc)
8133                      (nconc to cc))))
8134              (unless articles
8135                (error "Found no matches for \"%s\"" recipient))
8136              (gnus-summary-limit articles))
8137       (gnus-summary-position-point))))
8138
8139 (defun gnus-summary-limit-to-address (address &optional not-matching)
8140   "Limit the summary buffer to articles with the given ADDRESS.
8141
8142 If NOT-MATCHING, exclude ADDRESS.
8143
8144 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8145 in `nnmail-extra-headers'."
8146   (interactive
8147    (list (read-string (format "%s address (regexp): "
8148                               (if current-prefix-arg "Exclude" "Limit to")))
8149          current-prefix-arg))
8150   (when (not (equal "" address))
8151     (prog1 (let* ((to
8152                    (if (memq 'To nnmail-extra-headers)
8153                        (gnus-summary-find-matching
8154                         (cons 'extra 'To) address 'all nil nil
8155                         not-matching)
8156                      (gnus-message
8157                       1 "`To' isn't present in `nnmail-extra-headers'")
8158                      (sit-for 1)
8159                      t))
8160                   (cc
8161                    (if (memq 'Cc nnmail-extra-headers)
8162                        (gnus-summary-find-matching
8163                         (cons 'extra 'Cc) address 'all nil nil
8164                         not-matching)
8165                      (gnus-message
8166                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8167                      (sit-for 1)
8168                      t))
8169                   (from
8170                    (gnus-summary-find-matching "from" address
8171                                                'all nil nil not-matching))
8172                   (articles
8173                    (if not-matching
8174                        ;; We need the numbers that are in all lists:
8175                        (if (eq cc t)
8176                            (if (eq to t)
8177                                from
8178                              (mapcar (lambda (a) (car (memq a from))) to))
8179                          (if (eq to t)
8180                              (mapcar (lambda (a) (car (memq a from))) cc)
8181                            (mapcar (lambda (a) (car (memq a from)))
8182                                    (mapcar (lambda (a) (car (memq a to)))
8183                                            cc))))
8184                      (nconc (if (eq to t) nil to)
8185                             (if (eq cc t) nil cc)
8186                             from))))
8187              (unless articles
8188                (error "Found no matches for \"%s\"" address))
8189              (gnus-summary-limit articles))
8190       (gnus-summary-position-point))))
8191
8192 (defun gnus-summary-limit-strange-charsets-predicate (header)
8193   (when (fboundp 'char-charset)
8194     (let ((string (concat (mail-header-subject header)
8195                           (mail-header-from header)))
8196           charset found)
8197       (dotimes (i (1- (length string)))
8198         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8199         (when (string-match "unicode\\|big\\|japanese" charset)
8200           (setq found t)))
8201       found)))
8202
8203 (defun gnus-summary-limit-to-predicate (predicate)
8204   "Limit to articles where PREDICATE returns non-nil.
8205 PREDICATE will be called with the header structures of the
8206 articles."
8207   (let ((articles nil)
8208         (case-fold-search t))
8209     (dolist (header gnus-newsgroup-headers)
8210       (when (funcall predicate header)
8211         (push (mail-header-number header) articles)))
8212     (gnus-summary-limit (nreverse articles))))
8213
8214 (defun gnus-summary-limit-to-age (age &optional younger-p)
8215   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8216 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8217 articles that are younger than AGE days."
8218   (interactive
8219    (let ((younger current-prefix-arg)
8220          (days-got nil)
8221          days)
8222      (while (not days-got)
8223        (setq days (if younger
8224                       (read-string "Limit to articles younger than (in days, older when negative): ")
8225                     (read-string
8226                      "Limit to articles older than (in days, younger when negative): ")))
8227        (when (> (length days) 0)
8228          (setq days (read days)))
8229        (if (numberp days)
8230            (progn
8231              (setq days-got t)
8232              (when (< days 0)
8233                (setq younger (not younger))
8234                (setq days (* days -1))))
8235          (message "Please enter a number.")
8236          (sleep-for 1)))
8237      (list days younger)))
8238   (prog1
8239       (let ((data gnus-newsgroup-data)
8240             (cutoff (days-to-time age))
8241             articles d date is-younger)
8242         (while (setq d (pop data))
8243           (when (and (vectorp (gnus-data-header d))
8244                      (setq date (mail-header-date (gnus-data-header d))))
8245             (setq is-younger (time-less-p
8246                               (time-since (gnus-date-get-time date))
8247                               cutoff))
8248             (when (if younger-p
8249                       is-younger
8250                     (not is-younger))
8251               (push (gnus-data-number d) articles))))
8252         (gnus-summary-limit (nreverse articles)))
8253     (gnus-summary-position-point)))
8254
8255 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8256   "Limit the summary buffer to articles that match an 'extra' header."
8257   (interactive
8258    (let ((header
8259           (intern
8260            (gnus-completing-read
8261             (if current-prefix-arg
8262                 "Exclude extra header"
8263               "Limit extra header")
8264             (mapcar 'symbol-name gnus-extra-headers)
8265             t nil nil
8266             (symbol-name (car gnus-extra-headers))))))
8267      (list header
8268            (read-string (format "%s header %s (regexp): "
8269                                 (if current-prefix-arg "Exclude" "Limit to")
8270                                 header))
8271            current-prefix-arg)))
8272   (when (not (equal "" regexp))
8273     (prog1
8274         (let ((articles (gnus-summary-find-matching
8275                          (cons 'extra header) regexp 'all nil nil
8276                          not-matching)))
8277           (unless articles
8278             (error "Found no matches for \"%s\"" regexp))
8279           (gnus-summary-limit articles))
8280       (gnus-summary-position-point))))
8281
8282 (defun gnus-summary-limit-to-display-predicate ()
8283   "Limit the summary buffer to the predicated in the `display' group parameter."
8284   (interactive)
8285   (unless gnus-newsgroup-display
8286     (error "There is no `display' group parameter"))
8287   (let (articles)
8288     (dolist (number gnus-newsgroup-articles)
8289       (when (funcall gnus-newsgroup-display)
8290         (push number articles)))
8291     (gnus-summary-limit articles))
8292   (gnus-summary-position-point))
8293
8294 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8295 (make-obsolete
8296  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8297
8298 (defun gnus-summary-limit-to-unread (&optional all)
8299   "Limit the summary buffer to articles that are not marked as read.
8300 If ALL is non-nil, limit strictly to unread articles."
8301   (interactive "P")
8302   (if all
8303       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8304     (gnus-summary-limit-to-marks
8305      ;; Concat all the marks that say that an article is read and have
8306      ;; those removed.
8307      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8308            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8309            gnus-low-score-mark gnus-expirable-mark
8310            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8311            gnus-duplicate-mark)
8312      'reverse)))
8313
8314 (defun gnus-summary-limit-to-headers (match &optional reverse)
8315   "Limit the summary buffer to articles that have headers that match MATCH.
8316 If REVERSE (the prefix), limit to articles that don't match."
8317   (interactive "sMatch headers (regexp): \nP")
8318   (gnus-summary-limit-to-bodies match reverse t))
8319
8320 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8321   "Limit the summary buffer to articles that have bodies that match MATCH.
8322 If REVERSE (the prefix), limit to articles that don't match."
8323   (interactive "sMatch body (regexp): \nP")
8324   (let ((articles nil)
8325         (gnus-select-article-hook nil)  ;Disable hook.
8326         (gnus-article-prepare-hook nil)
8327         (gnus-use-article-prefetch nil)
8328         (gnus-keep-backlog nil)
8329         (gnus-break-pages nil)
8330         (gnus-summary-display-arrow nil)
8331         (gnus-updated-mode-lines nil)
8332         (gnus-auto-center-summary nil)
8333         (gnus-display-mime-function nil))
8334     (dolist (data gnus-newsgroup-data)
8335       (let (gnus-mark-article-hook)
8336         (gnus-summary-select-article t t nil (gnus-data-number data)))
8337       (with-current-buffer gnus-article-buffer
8338         (article-goto-body)
8339         (let* ((case-fold-search t)
8340                (found (if headersp
8341                           (re-search-backward match nil t)
8342                         (re-search-forward match nil t))))
8343           (when (or (and found
8344                          (not reverse))
8345                     (and (not found)
8346                          reverse))
8347             (push (gnus-data-number data) articles)))))
8348     (if (not articles)
8349         (message "No messages matched")
8350       (gnus-summary-limit articles)))
8351   (gnus-summary-position-point))
8352
8353 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8354   "Limit the summary buffer to articles that aren't part on any thread.
8355 If THREADSP (the prefix), limit to articles that are in threads."
8356   (interactive "P")
8357   (let ((articles nil)
8358         thread-articles
8359         threads)
8360     (dolist (thread gnus-newsgroup-threads)
8361       (if (stringp (car thread))
8362           (dolist (thread (cdr thread))
8363             (push thread threads))
8364         (push thread threads)))
8365     (dolist (thread threads)
8366       (setq thread-articles (gnus-articles-in-thread thread))
8367       (when (or (and threadsp
8368                      (> (length thread-articles) 1))
8369                 (and (not threadsp)
8370                      (= (length thread-articles) 1)))
8371         (setq articles (nconc thread-articles articles))))
8372     (if (not articles)
8373         (message "No messages matched")
8374       (gnus-summary-limit articles))
8375     (gnus-summary-position-point)))
8376
8377 (defun gnus-summary-limit-to-replied (&optional unreplied)
8378   "Limit the summary buffer to replied articles.
8379 If UNREPLIED (the prefix), limit to unreplied articles."
8380   (interactive "P")
8381   (if unreplied
8382       (gnus-summary-limit
8383        (gnus-set-difference gnus-newsgroup-articles
8384         gnus-newsgroup-replied))
8385     (gnus-summary-limit gnus-newsgroup-replied))
8386   (gnus-summary-position-point))
8387
8388 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8389 (make-obsolete 'gnus-summary-delete-marked-with
8390                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8391
8392 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8393   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8394 If REVERSE, limit the summary buffer to articles that are marked
8395 with MARKS.  MARKS can either be a string of marks or a list of marks.
8396 Returns how many articles were removed."
8397   (interactive "sMarks: ")
8398   (gnus-summary-limit-to-marks marks t))
8399
8400 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8401   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8402 If REVERSE (the prefix), limit the summary buffer to articles that are
8403 not marked with MARKS.  MARKS can either be a string of marks or a
8404 list of marks.
8405 Returns how many articles were removed."
8406   (interactive "sMarks: \nP")
8407   (prog1
8408       (let ((data gnus-newsgroup-data)
8409             (marks (if (listp marks) marks
8410                      (append marks nil))) ; Transform to list.
8411             articles)
8412         (while data
8413           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8414                   (memq (gnus-data-mark (car data)) marks))
8415             (push (gnus-data-number (car data)) articles))
8416           (setq data (cdr data)))
8417         (gnus-summary-limit articles))
8418     (gnus-summary-position-point)))
8419
8420 (defun gnus-summary-limit-to-score (score)
8421   "Limit to articles with score at or above SCORE."
8422   (interactive "NLimit to articles with score of at least: ")
8423   (let ((data gnus-newsgroup-data)
8424         articles)
8425     (while data
8426       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8427                 score)
8428         (push (gnus-data-number (car data)) articles))
8429       (setq data (cdr data)))
8430     (prog1
8431         (gnus-summary-limit articles)
8432       (gnus-summary-position-point))))
8433
8434 (defun gnus-summary-limit-to-unseen ()
8435   "Limit to unseen articles."
8436   (interactive)
8437   (prog1
8438       (gnus-summary-limit gnus-newsgroup-unseen)
8439     (gnus-summary-position-point)))
8440
8441 (defun gnus-summary-limit-include-thread (id)
8442   "Display all the hidden articles that is in the thread with ID in it.
8443 When called interactively, ID is the Message-ID of the current
8444 article."
8445   (interactive (list (mail-header-id (gnus-summary-article-header))))
8446   (let ((articles (gnus-articles-in-thread
8447                    (gnus-id-to-thread (gnus-root-id id)))))
8448     (prog1
8449         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8450       (gnus-summary-limit-include-matching-articles
8451        "subject"
8452        (regexp-quote (gnus-simplify-subject-re
8453                       (mail-header-subject (gnus-id-to-header id)))))
8454       (gnus-summary-position-point))))
8455
8456 (defun gnus-summary-limit-include-matching-articles (header regexp)
8457   "Display all the hidden articles that have HEADERs that match REGEXP."
8458   (interactive (list (read-string "Match on header: ")
8459                      (read-string "Regexp: ")))
8460   (let ((articles (gnus-find-matching-articles header regexp)))
8461     (prog1
8462         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8463       (gnus-summary-position-point))))
8464
8465 (defun gnus-summary-insert-dormant-articles ()
8466   "Insert all the dormant articles for this group into the current buffer."
8467   (interactive)
8468   (let ((gnus-verbose (max 6 gnus-verbose)))
8469     (if (not gnus-newsgroup-dormant)
8470         (gnus-message 3 "No dormant articles for this group")
8471       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8472
8473 (defun gnus-summary-insert-ticked-articles ()
8474   "Insert ticked articles for this group into the current buffer."
8475   (interactive)
8476   (let ((gnus-verbose (max 6 gnus-verbose)))
8477     (if (not gnus-newsgroup-marked)
8478         (gnus-message 3 "No ticked articles for this group")
8479       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8480
8481 (defun gnus-summary-limit-include-dormant ()
8482   "Display all the hidden articles that are marked as dormant.
8483 Note that this command only works on a subset of the articles currently
8484 fetched for this group."
8485   (interactive)
8486   (unless gnus-newsgroup-dormant
8487     (error "There are no dormant articles in this group"))
8488   (prog1
8489       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8490     (gnus-summary-position-point)))
8491
8492 (defun gnus-summary-limit-exclude-dormant ()
8493   "Hide all dormant articles."
8494   (interactive)
8495   (prog1
8496       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8497     (gnus-summary-position-point)))
8498
8499 (defun gnus-summary-limit-exclude-childless-dormant ()
8500   "Hide all dormant articles that have no children."
8501   (interactive)
8502   (let ((data (gnus-data-list t))
8503         articles d children)
8504     ;; Find all articles that are either not dormant or have
8505     ;; children.
8506     (while (setq d (pop data))
8507       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8508                 (and (setq children
8509                            (gnus-article-children (gnus-data-number d)))
8510                      (let (found)
8511                        (while children
8512                          (when (memq (car children) articles)
8513                            (setq children nil
8514                                  found t))
8515                          (pop children))
8516                        found)))
8517         (push (gnus-data-number d) articles)))
8518     ;; Do the limiting.
8519     (prog1
8520         (gnus-summary-limit articles)
8521       (gnus-summary-position-point))))
8522
8523 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8524   "Mark all unread excluded articles as read.
8525 If ALL, mark even excluded ticked and dormants as read."
8526   (interactive "P")
8527   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8528   (let ((articles (gnus-sorted-ndifference
8529                    (sort
8530                     (mapcar (lambda (h) (mail-header-number h))
8531                             gnus-newsgroup-headers)
8532                     '<)
8533                    gnus-newsgroup-limit))
8534         article)
8535     (setq gnus-newsgroup-unreads
8536           (gnus-sorted-intersection gnus-newsgroup-unreads
8537                                     gnus-newsgroup-limit))
8538     (if all
8539         (setq gnus-newsgroup-dormant nil
8540               gnus-newsgroup-marked nil
8541               gnus-newsgroup-reads
8542               (nconc
8543                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8544                gnus-newsgroup-reads))
8545       (while (setq article (pop articles))
8546         (unless (or (memq article gnus-newsgroup-dormant)
8547                     (memq article gnus-newsgroup-marked))
8548           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8549
8550 (defun gnus-summary-limit (articles &optional pop)
8551   (if pop
8552       ;; We pop the previous limit off the stack and use that.
8553       (setq articles (car gnus-newsgroup-limits)
8554             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8555     ;; We use the new limit, so we push the old limit on the stack.
8556     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8557   ;; Set the limit.
8558   (setq gnus-newsgroup-limit articles)
8559   (let ((total (length gnus-newsgroup-data))
8560         (data (gnus-data-find-list (gnus-summary-article-number)))
8561         (gnus-summary-mark-below nil)   ; Inhibit this.
8562         found)
8563     ;; This will do all the work of generating the new summary buffer
8564     ;; according to the new limit.
8565     (gnus-summary-prepare)
8566     ;; Hide any threads, possibly.
8567     (gnus-summary-maybe-hide-threads)
8568     ;; Try to return to the article you were at, or one in the
8569     ;; neighborhood.
8570     (when data
8571       ;; We try to find some article after the current one.
8572       (while data
8573         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8574           (setq data nil
8575                 found t))
8576         (setq data (cdr data))))
8577     (unless found
8578       ;; If there is no data, that means that we were after the last
8579       ;; article.  The same goes when we can't find any articles
8580       ;; after the current one.
8581       (goto-char (point-max))
8582       (gnus-summary-find-prev))
8583     (gnus-set-mode-line 'summary)
8584     ;; We return how many articles were removed from the summary
8585     ;; buffer as a result of the new limit.
8586     (- total (length gnus-newsgroup-data))))
8587
8588 (defsubst gnus-invisible-cut-children (threads)
8589   (let ((num 0))
8590     (while threads
8591       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8592         (incf num))
8593       (pop threads))
8594     (< num 2)))
8595
8596 (defsubst gnus-cut-thread (thread)
8597   "Go forwards in the thread until we find an article that we want to display."
8598   (when (or (eq gnus-fetch-old-headers 'some)
8599             (eq gnus-fetch-old-headers 'invisible)
8600             (numberp gnus-fetch-old-headers)
8601             (eq gnus-build-sparse-threads 'some)
8602             (eq gnus-build-sparse-threads 'more))
8603     ;; Deal with old-fetched headers and sparse threads.
8604     (while (and
8605             thread
8606             (or
8607              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8608              (gnus-summary-article-ancient-p
8609               (mail-header-number (car thread))))
8610             (if (or (<= (length (cdr thread)) 1)
8611                     (eq gnus-fetch-old-headers 'invisible))
8612                 (setq gnus-newsgroup-limit
8613                       (delq (mail-header-number (car thread))
8614                             gnus-newsgroup-limit)
8615                       thread (cadr thread))
8616               (when (gnus-invisible-cut-children (cdr thread))
8617                 (let ((th (cdr thread)))
8618                   (while th
8619                     (if (memq (mail-header-number (caar th))
8620                               gnus-newsgroup-limit)
8621                         (setq thread (car th)
8622                               th nil)
8623                       (setq th (cdr th))))))))))
8624   thread)
8625
8626 (defun gnus-cut-threads (threads)
8627   "Cut off all uninteresting articles from the beginning of THREADS."
8628   (when (or (eq gnus-fetch-old-headers 'some)
8629             (eq gnus-fetch-old-headers 'invisible)
8630             (numberp gnus-fetch-old-headers)
8631             (eq gnus-build-sparse-threads 'some)
8632             (eq gnus-build-sparse-threads 'more))
8633     (let ((th threads))
8634       (while th
8635         (setcar th (gnus-cut-thread (car th)))
8636         (setq th (cdr th)))))
8637   ;; Remove nixed out threads.
8638   (delq nil threads))
8639
8640 (defun gnus-summary-initial-limit (&optional show-if-empty)
8641   "Figure out what the initial limit is supposed to be on group entry.
8642 This entails weeding out unwanted dormants, low-scored articles,
8643 fetch-old-headers verbiage, and so on."
8644   ;; Most groups have nothing to remove.
8645   (unless (or gnus-inhibit-limiting
8646               (and (null gnus-newsgroup-dormant)
8647                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8648                    (not (eq gnus-fetch-old-headers 'some))
8649                    (not (numberp gnus-fetch-old-headers))
8650                    (not (eq gnus-fetch-old-headers 'invisible))
8651                    (null gnus-summary-expunge-below)
8652                    (not (eq gnus-build-sparse-threads 'some))
8653                    (not (eq gnus-build-sparse-threads 'more))
8654                    (null gnus-thread-expunge-below)))
8655     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8656     (setq gnus-newsgroup-limit nil)
8657     (mapatoms
8658      (lambda (node)
8659        (unless (car (symbol-value node))
8660          ;; These threads have no parents -- they are roots.
8661          (let ((nodes (cdr (symbol-value node)))
8662                thread)
8663            (while nodes
8664              (if (and gnus-thread-expunge-below
8665                       (< (gnus-thread-total-score (car nodes))
8666                          gnus-thread-expunge-below))
8667                  (gnus-expunge-thread (pop nodes))
8668                (setq thread (pop nodes))
8669                (gnus-summary-limit-children thread))))))
8670      gnus-newsgroup-dependencies)
8671     ;; If this limitation resulted in an empty group, we might
8672     ;; pop the previous limit and use it instead.
8673     (when (and (not gnus-newsgroup-limit)
8674                show-if-empty)
8675       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8676     gnus-newsgroup-limit))
8677
8678 (defun gnus-summary-limit-children (thread)
8679   "Return 1 if this subthread is visible and 0 if it is not."
8680   ;; First we get the number of visible children to this thread.  This
8681   ;; is done by recursing down the thread using this function, so this
8682   ;; will really go down to a leaf article first, before slowly
8683   ;; working its way up towards the root.
8684   (when thread
8685     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8686            (children
8687            (if (cdr thread)
8688                (apply '+ (mapcar 'gnus-summary-limit-children
8689                                  (cdr thread)))
8690              0))
8691           (number (mail-header-number (car thread)))
8692           score)
8693       (if (and
8694            (not (memq number gnus-newsgroup-marked))
8695            (or
8696             ;; If this article is dormant and has absolutely no visible
8697             ;; children, then this article isn't visible.
8698             (and (memq number gnus-newsgroup-dormant)
8699                  (zerop children))
8700             ;; If this is "fetch-old-headered" and there is no
8701             ;; visible children, then we don't want this article.
8702             (and (or (eq gnus-fetch-old-headers 'some)
8703                      (numberp gnus-fetch-old-headers))
8704                  (gnus-summary-article-ancient-p number)
8705                  (zerop children))
8706             ;; If this is "fetch-old-headered" and `invisible', then
8707             ;; we don't want this article.
8708             (and (eq gnus-fetch-old-headers 'invisible)
8709                  (gnus-summary-article-ancient-p number))
8710             ;; If this is a sparsely inserted article with no children,
8711             ;; we don't want it.
8712             (and (eq gnus-build-sparse-threads 'some)
8713                  (gnus-summary-article-sparse-p number)
8714                  (zerop children))
8715             ;; If we use expunging, and this article is really
8716             ;; low-scored, then we don't want this article.
8717             (when (and gnus-summary-expunge-below
8718                        (< (setq score
8719                                 (or (cdr (assq number gnus-newsgroup-scored))
8720                                     gnus-summary-default-score))
8721                           gnus-summary-expunge-below))
8722               ;; We increase the expunge-tally here, but that has
8723               ;; nothing to do with the limits, really.
8724               (incf gnus-newsgroup-expunged-tally)
8725               ;; We also mark as read here, if that's wanted.
8726               (when (and gnus-summary-mark-below
8727                          (< score gnus-summary-mark-below))
8728                 (setq gnus-newsgroup-unreads
8729                       (delq number gnus-newsgroup-unreads))
8730                 (if gnus-newsgroup-auto-expire
8731                     (push number gnus-newsgroup-expirable)
8732                   (push (cons number gnus-low-score-mark)
8733                         gnus-newsgroup-reads)))
8734               t)
8735             ;; Do the `display' group parameter.
8736             (and gnus-newsgroup-display
8737                  (not (funcall gnus-newsgroup-display)))))
8738           ;; Nope, invisible article.
8739           0
8740         ;; Ok, this article is to be visible, so we add it to the limit
8741         ;; and return 1.
8742         (push number gnus-newsgroup-limit)
8743         1))))
8744
8745 (defun gnus-expunge-thread (thread)
8746   "Mark all articles in THREAD as read."
8747   (let* ((number (mail-header-number (car thread))))
8748     (incf gnus-newsgroup-expunged-tally)
8749     ;; We also mark as read here, if that's wanted.
8750     (setq gnus-newsgroup-unreads
8751           (delq number gnus-newsgroup-unreads))
8752     (if gnus-newsgroup-auto-expire
8753         (push number gnus-newsgroup-expirable)
8754       (push (cons number gnus-low-score-mark)
8755             gnus-newsgroup-reads)))
8756   ;; Go recursively through all subthreads.
8757   (mapcar 'gnus-expunge-thread (cdr thread)))
8758
8759 ;; Summary article oriented commands
8760
8761 (defun gnus-summary-refer-parent-article (n)
8762   "Refer parent article N times.
8763 If N is negative, go to ancestor -N instead.
8764 The difference between N and the number of articles fetched is returned."
8765   (interactive "p")
8766   (let ((skip 1)
8767         error header ref)
8768     (when (not (natnump n))
8769       (setq skip (abs n)
8770             n 1))
8771     (while (and (> n 0)
8772                 (not error))
8773       (setq header (gnus-summary-article-header))
8774       (if (and (eq (mail-header-number header)
8775                    (cdr gnus-article-current))
8776                (equal gnus-newsgroup-name
8777                       (car gnus-article-current)))
8778           ;; If we try to find the parent of the currently
8779           ;; displayed article, then we take a look at the actual
8780           ;; References header, since this is slightly more
8781           ;; reliable than the References field we got from the
8782           ;; server.
8783           (with-current-buffer gnus-original-article-buffer
8784             (nnheader-narrow-to-headers)
8785             (unless (setq ref (message-fetch-field "references"))
8786               (when (setq ref (message-fetch-field "in-reply-to"))
8787                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8788             (widen))
8789         (setq ref
8790               ;; It's not the current article, so we take a bet on
8791               ;; the value we got from the server.
8792               (mail-header-references header)))
8793       (if (and ref
8794                (not (equal ref "")))
8795           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8796             (gnus-message 1 "Couldn't find parent"))
8797         (gnus-message 1 "No references in article %d"
8798                       (gnus-summary-article-number))
8799         (setq error t))
8800       (decf n))
8801     (gnus-summary-position-point)
8802     n))
8803
8804 (defun gnus-summary-refer-references ()
8805   "Fetch all articles mentioned in the References header.
8806 Return the number of articles fetched."
8807   (interactive)
8808   (let ((ref (mail-header-references (gnus-summary-article-header)))
8809         (current (gnus-summary-article-number))
8810         (n 0))
8811     (if (or (not ref)
8812             (equal ref ""))
8813         (error "No References in the current article")
8814       ;; For each Message-ID in the References header...
8815       (while (string-match "<[^>]*>" ref)
8816         (incf n)
8817         ;; ... fetch that article.
8818         (gnus-summary-refer-article
8819          (prog1 (match-string 0 ref)
8820            (setq ref (substring ref (match-end 0))))))
8821       (gnus-summary-goto-subject current)
8822       (gnus-summary-position-point)
8823       n)))
8824
8825 (defun gnus-summary-refer-thread (&optional limit)
8826   "Fetch all articles in the current thread.
8827 If LIMIT (the numerical prefix), fetch that many old headers instead
8828 of what's specified by the `gnus-refer-thread-limit' variable."
8829   (interactive "P")
8830   (let ((id (mail-header-id (gnus-summary-article-header)))
8831         (limit (if limit (prefix-numeric-value limit)
8832                  gnus-refer-thread-limit)))
8833     (unless (eq gnus-fetch-old-headers 'invisible)
8834       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8835       ;; Retrieve the headers and read them in.
8836       (if (eq (if (numberp limit)
8837                   (gnus-retrieve-headers
8838                    (list (min
8839                           (+ (mail-header-number
8840                               (gnus-summary-article-header))
8841                              limit)
8842                           gnus-newsgroup-end))
8843                    gnus-newsgroup-name (* limit 2))
8844                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8845                 ;; headers.
8846                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8847                                        gnus-newsgroup-name limit))
8848               'nov)
8849           (gnus-build-all-threads)
8850         (error "Can't fetch thread from back ends that don't support NOV"))
8851       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8852     (gnus-summary-limit-include-thread id)))
8853
8854 (defun gnus-summary-refer-article (message-id)
8855   "Fetch an article specified by MESSAGE-ID."
8856   (interactive "sMessage-ID: ")
8857   (when (and (stringp message-id)
8858              (not (zerop (length message-id))))
8859     (setq message-id (gnus-replace-in-string message-id " " ""))
8860     ;; Construct the correct Message-ID if necessary.
8861     ;; Suggested by tale@pawl.rpi.edu.
8862     (unless (string-match "^<" message-id)
8863       (setq message-id (concat "<" message-id)))
8864     (unless (string-match ">$" message-id)
8865       (setq message-id (concat message-id ">")))
8866     ;; People often post MIDs from URLs, so unhex it:
8867     (unless (string-match "@" message-id)
8868       (setq message-id (gnus-url-unhex-string message-id)))
8869     (let* ((header (gnus-id-to-header message-id))
8870            (sparse (and header
8871                         (gnus-summary-article-sparse-p
8872                          (mail-header-number header))
8873                         (memq (mail-header-number header)
8874                               gnus-newsgroup-limit)))
8875            number)
8876       (cond
8877        ;; If the article is present in the buffer we just go to it.
8878        ((and header
8879              (or (not (gnus-summary-article-sparse-p
8880                        (mail-header-number header)))
8881                  sparse))
8882         (prog1
8883             (gnus-summary-goto-article
8884              (mail-header-number header) nil t)
8885           (when sparse
8886             (gnus-summary-update-article (mail-header-number header)))))
8887        (t
8888         ;; We fetch the article.
8889         (catch 'found
8890           (dolist (gnus-override-method (gnus-refer-article-methods))
8891             (when (and (gnus-check-server gnus-override-method)
8892                        ;; Fetch the header,
8893                        (setq number (gnus-summary-insert-subject message-id)))
8894               ;; and display the article.
8895               (gnus-summary-select-article nil nil nil number)
8896               (throw 'found t)))
8897           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8898
8899 (defun gnus-refer-article-methods ()
8900   "Return a list of referable methods."
8901   (cond
8902    ;; No method, so we default to current and native.
8903    ((null gnus-refer-article-method)
8904     (list gnus-current-select-method gnus-select-method))
8905    ;; Current.
8906    ((eq 'current gnus-refer-article-method)
8907     (list gnus-current-select-method))
8908    ;; List of select methods.
8909    ((not (and (symbolp (car gnus-refer-article-method))
8910               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8911     (let (out)
8912       (dolist (method gnus-refer-article-method)
8913         (push (if (eq 'current method)
8914                   gnus-current-select-method
8915                 method)
8916               out))
8917       (nreverse out)))
8918    ;; One single select method.
8919    (t
8920     (list gnus-refer-article-method))))
8921
8922 (defun gnus-summary-edit-parameters ()
8923   "Edit the group parameters of the current group."
8924   (interactive)
8925   (gnus-group-edit-group gnus-newsgroup-name 'params))
8926
8927 (defun gnus-summary-customize-parameters ()
8928   "Customize the group parameters of the current group."
8929   (interactive)
8930   (gnus-group-customize gnus-newsgroup-name))
8931
8932 (defun gnus-summary-enter-digest-group (&optional force)
8933   "Enter an nndoc group based on the current article.
8934 If FORCE, force a digest interpretation.  If not, try
8935 to guess what the document format is."
8936   (interactive "P")
8937   (let ((conf gnus-current-window-configuration))
8938     (save-window-excursion
8939       (save-excursion
8940         (let (gnus-article-prepare-hook
8941               gnus-display-mime-function
8942               gnus-break-pages)
8943           (gnus-summary-select-article))))
8944     (setq gnus-current-window-configuration conf)
8945     (let* ((name (format "%s-%d"
8946                          (gnus-group-prefixed-name
8947                           gnus-newsgroup-name (list 'nndoc ""))
8948                          (with-current-buffer gnus-summary-buffer
8949                            gnus-current-article)))
8950            (ogroup gnus-newsgroup-name)
8951            (params (append (gnus-info-params (gnus-get-info ogroup))
8952                            (list (cons 'to-group ogroup))
8953                            (list (cons 'parent-group ogroup))
8954                            (list (cons 'save-article-group ogroup))))
8955            (case-fold-search t)
8956            (buf (current-buffer))
8957            dig to-address)
8958       (with-current-buffer gnus-original-article-buffer
8959         ;; Have the digest group inherit the main mail address of
8960         ;; the parent article.
8961         (when (setq to-address (or (gnus-fetch-field "reply-to")
8962                                    (gnus-fetch-field "from")))
8963           (setq params
8964                 (append
8965                  (list (cons 'to-address
8966                              (funcall gnus-decode-encoded-address-function
8967                                       to-address))))))
8968         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8969         (insert-buffer-substring gnus-original-article-buffer)
8970         ;; Remove lines that may lead nndoc to misinterpret the
8971         ;; document type.
8972         (narrow-to-region
8973          (goto-char (point-min))
8974          (or (search-forward "\n\n" nil t) (point)))
8975         (goto-char (point-min))
8976         (delete-matching-lines "^Path:\\|^From ")
8977         (widen))
8978       (unwind-protect
8979           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8980                     (gnus-newsgroup-ephemeral-ignored-charsets
8981                      gnus-newsgroup-ignored-charsets))
8982                 (gnus-group-read-ephemeral-group
8983                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8984                               (nndoc-article-type
8985                                ,(if force 'mbox 'guess)))
8986                  t nil nil nil
8987                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8988                                                         "ADAPT")))))
8989               ;; Make all postings to this group go to the parent group.
8990               (nconc (gnus-info-params (gnus-get-info name))
8991                      params)
8992             ;; Couldn't select this doc group.
8993             (switch-to-buffer buf)
8994             (gnus-set-global-variables)
8995             (gnus-configure-windows 'summary)
8996             (gnus-message 3 "Article couldn't be entered?"))
8997         (kill-buffer dig)))))
8998
8999 (defun gnus-summary-read-document (n)
9000   "Open a new group based on the current article(s).
9001 This will allow you to read digests and other similar
9002 documents as newsgroups.
9003 Obeys the standard process/prefix convention."
9004   (interactive "P")
9005   (let* ((ogroup gnus-newsgroup-name)
9006          (params (append (gnus-info-params (gnus-get-info ogroup))
9007                          (list (cons 'to-group ogroup))))
9008          group egroup groups vgroup)
9009     (dolist (article (gnus-summary-work-articles n))
9010       (setq group (format "%s-%d" gnus-newsgroup-name article))
9011       (gnus-summary-remove-process-mark article)
9012       (when (gnus-summary-display-article article)
9013         (save-excursion ;;What for?
9014           (with-temp-buffer
9015             (insert-buffer-substring gnus-original-article-buffer)
9016             ;; Remove some headers that may lead nndoc to make
9017             ;; the wrong guess.
9018             (message-narrow-to-head)
9019             (goto-char (point-min))
9020             (delete-matching-lines "^Path:\\|^From ")
9021             (widen)
9022             (if (setq egroup
9023                       (gnus-group-read-ephemeral-group
9024                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9025                                      (nndoc-article-type guess))
9026                        t nil t))
9027                 (progn
9028                   ;; Make all postings to this group go to the parent group.
9029                   (nconc (gnus-info-params (gnus-get-info egroup))
9030                          params)
9031                   (push egroup groups))
9032               ;; Couldn't select this doc group.
9033               (gnus-error 3 "Article couldn't be entered"))))))
9034     ;; Now we have selected all the documents.
9035     (cond
9036      ((not groups)
9037       (error "None of the articles could be interpreted as documents"))
9038      ((gnus-group-read-ephemeral-group
9039        (setq vgroup (format
9040                      "nnvirtual:%s-%s" gnus-newsgroup-name
9041                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9042        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9043        t
9044        (cons (current-buffer) 'summary)))
9045      (t
9046       (error "Couldn't select virtual nndoc group")))))
9047
9048 (defun gnus-summary-isearch-article (&optional regexp-p)
9049   "Do incremental search forward on the current article.
9050 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9051   (interactive "P")
9052   (gnus-summary-select-article)
9053   (gnus-configure-windows 'article)
9054   (gnus-eval-in-buffer-window gnus-article-buffer
9055     (save-restriction
9056       (widen)
9057       (isearch-forward regexp-p))))
9058
9059 (defun gnus-summary-repeat-search-article-forward ()
9060   "Repeat the previous search forwards."
9061   (interactive)
9062   (unless gnus-last-search-regexp
9063     (error "No previous search"))
9064   (gnus-summary-search-article-forward gnus-last-search-regexp))
9065
9066 (defun gnus-summary-repeat-search-article-backward ()
9067   "Repeat the previous search backwards."
9068   (interactive)
9069   (unless gnus-last-search-regexp
9070     (error "No previous search"))
9071   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9072
9073 (defun gnus-summary-search-article-forward (regexp &optional backward)
9074   "Search for an article containing REGEXP forward.
9075 If BACKWARD, search backward instead."
9076   (interactive
9077    (list (read-string
9078           (format "Search article %s (regexp%s): "
9079                   (if current-prefix-arg "backward" "forward")
9080                   (if gnus-last-search-regexp
9081                       (concat ", default " gnus-last-search-regexp)
9082                     "")))
9083          current-prefix-arg))
9084   (if (string-equal regexp "")
9085       (setq regexp (or gnus-last-search-regexp ""))
9086     (setq gnus-last-search-regexp regexp)
9087     (setq gnus-article-before-search gnus-current-article))
9088   ;; Intentionally set gnus-last-article.
9089   (setq gnus-last-article gnus-article-before-search)
9090   (let ((gnus-last-article gnus-last-article))
9091     (if (gnus-summary-search-article regexp backward)
9092         (gnus-summary-show-thread)
9093       (signal 'search-failed (list regexp)))))
9094
9095 (defun gnus-summary-search-article-backward (regexp)
9096   "Search for an article containing REGEXP backward."
9097   (interactive
9098    (list (read-string
9099           (format "Search article backward (regexp%s): "
9100                   (if gnus-last-search-regexp
9101                       (concat ", default " gnus-last-search-regexp)
9102                     "")))))
9103   (gnus-summary-search-article-forward regexp 'backward))
9104
9105 (defun gnus-summary-search-article (regexp &optional backward)
9106   "Search for an article containing REGEXP.
9107 Optional argument BACKWARD means do search for backward.
9108 `gnus-select-article-hook' is not called during the search."
9109   ;; We have to require this here to make sure that the following
9110   ;; dynamic binding isn't shadowed by autoloading.
9111   (require 'gnus-async)
9112   (require 'gnus-art)
9113   (let ((gnus-select-article-hook nil)  ;Disable hook.
9114         (gnus-article-prepare-hook nil)
9115         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9116         (gnus-use-article-prefetch nil)
9117         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9118         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9119         (gnus-visual nil)
9120         (gnus-keep-backlog nil)
9121         (gnus-break-pages nil)
9122         (gnus-summary-display-arrow nil)
9123         (gnus-updated-mode-lines nil)
9124         (gnus-auto-center-summary nil)
9125         (sum (current-buffer))
9126         (gnus-display-mime-function nil)
9127         (found nil)
9128         point)
9129     (gnus-save-hidden-threads
9130       (gnus-summary-select-article)
9131       (set-buffer gnus-article-buffer)
9132       (goto-char (window-point (get-buffer-window (current-buffer))))
9133       (when backward
9134         (forward-line -1))
9135       (while (not found)
9136         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9137         (if (if backward
9138                 (re-search-backward regexp nil t)
9139               (re-search-forward regexp nil t))
9140             ;; We found the regexp.
9141             (progn
9142               (setq found 'found)
9143               (beginning-of-line)
9144               (set-window-start
9145                (get-buffer-window (current-buffer))
9146                (point))
9147               (forward-line 1)
9148               (set-window-point
9149                (get-buffer-window (current-buffer))
9150                (point))
9151               (set-buffer sum)
9152               (setq point (point)))
9153           ;; We didn't find it, so we go to the next article.
9154           (set-buffer sum)
9155           (setq found 'not)
9156           (while (eq found 'not)
9157             (if (not (if backward (gnus-summary-find-prev)
9158                        (gnus-summary-find-next)))
9159                 ;; No more articles.
9160                 (setq found t)
9161               ;; Select the next article and adjust point.
9162               (unless (gnus-summary-article-sparse-p
9163                        (gnus-summary-article-number))
9164                 (setq found nil)
9165                 (gnus-summary-select-article)
9166                 (set-buffer gnus-article-buffer)
9167                 (widen)
9168                 (goto-char (if backward (point-max) (point-min))))))))
9169       (gnus-message 7 ""))
9170     ;; Return whether we found the regexp.
9171     (when (eq found 'found)
9172       (goto-char point)
9173       (gnus-summary-show-thread)
9174       (gnus-summary-goto-subject gnus-current-article)
9175       (gnus-summary-position-point)
9176       t)))
9177
9178 (defun gnus-find-matching-articles (header regexp)
9179   "Return a list of all articles that match REGEXP on HEADER.
9180 This search includes all articles in the current group that Gnus has
9181 fetched headers for, whether they are displayed or not."
9182   (let ((articles nil)
9183         ;; Can't eta-reduce because it's a macro.
9184         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9185         (case-fold-search t))
9186     (dolist (header gnus-newsgroup-headers)
9187       (when (string-match regexp (funcall func header))
9188         (push (mail-header-number header) articles)))
9189     (nreverse articles)))
9190
9191 (defun gnus-summary-find-matching (header regexp &optional backward unread
9192                                           not-case-fold not-matching)
9193   "Return a list of all articles that match REGEXP on HEADER.
9194 The search stars on the current article and goes forwards unless
9195 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9196 If UNREAD is non-nil, only unread articles will
9197 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9198 in the comparisons. If NOT-MATCHING, return a list of all articles that
9199 not match REGEXP on HEADER."
9200   (let ((case-fold-search (not not-case-fold))
9201         articles d func)
9202     (if (consp header)
9203         (if (eq (car header) 'extra)
9204             (setq func
9205                   `(lambda (h)
9206                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9207                          "")))
9208           (error "%s is an invalid header" header))
9209       (unless (fboundp (intern (concat "mail-header-" header)))
9210         (error "%s is not a valid header" header))
9211       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9212     (dolist (d (if (eq backward 'all)
9213                    gnus-newsgroup-data
9214                  (gnus-data-find-list
9215                   (gnus-summary-article-number)
9216                   (gnus-data-list backward))))
9217       (when (and (or (not unread)       ; We want all articles...
9218                      (gnus-data-unread-p d)) ; Or just unreads.
9219                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9220                  (if not-matching
9221                      (not (string-match
9222                            regexp
9223                            (funcall func (gnus-data-header d))))
9224                    (string-match regexp
9225                                  (funcall func (gnus-data-header d)))))
9226         (push (gnus-data-number d) articles))) ; Success!
9227     (nreverse articles)))
9228
9229 (defun gnus-summary-execute-command (header regexp command &optional backward)
9230   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9231 If HEADER is an empty string (or nil), the match is done on the entire
9232 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9233   (interactive
9234    (list (let ((completion-ignore-case t))
9235            (gnus-completing-read
9236             "Header name"
9237             (mapcar 'symbol-name
9238                     (append
9239                      '(Number Subject From Lines Date
9240                        Message-ID Xref References Body)
9241                      gnus-extra-headers))
9242             'require-match))
9243          (read-string "Regexp: ")
9244          (read-key-sequence "Command: ")
9245          current-prefix-arg))
9246   (when (equal header "Body")
9247     (setq header ""))
9248   ;; Hidden thread subtrees must be searched as well.
9249   (gnus-summary-show-all-threads)
9250   ;; We don't want to change current point nor window configuration.
9251   (save-excursion
9252     (save-window-excursion
9253       (let (gnus-visual
9254             gnus-treat-strip-trailing-blank-lines
9255             gnus-treat-strip-leading-blank-lines
9256             gnus-treat-strip-multiple-blank-lines
9257             gnus-treat-hide-boring-headers
9258             gnus-treat-fold-newsgroups
9259             gnus-article-prepare-hook)
9260         (gnus-message 6 "Executing %s..." (key-description command))
9261         ;; We'd like to execute COMMAND interactively so as to give arguments.
9262         (gnus-execute header regexp
9263                       `(call-interactively ',(key-binding command))
9264                       backward)
9265         (gnus-message 6 "Executing %s...done" (key-description command))))))
9266
9267 (defun gnus-summary-beginning-of-article ()
9268   "Scroll the article back to the beginning."
9269   (interactive)
9270   (gnus-summary-select-article)
9271   (gnus-configure-windows 'article)
9272   (gnus-eval-in-buffer-window gnus-article-buffer
9273     (widen)
9274     (goto-char (point-min))
9275     (when gnus-break-pages
9276       (gnus-narrow-to-page))))
9277
9278 (defun gnus-summary-end-of-article ()
9279   "Scroll to the end of the article."
9280   (interactive)
9281   (gnus-summary-select-article)
9282   (gnus-configure-windows 'article)
9283   (gnus-eval-in-buffer-window gnus-article-buffer
9284     (widen)
9285     (goto-char (point-max))
9286     (recenter -3)
9287     (when gnus-break-pages
9288       (gnus-narrow-to-page))))
9289
9290 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9291   "Truncate to LEN and quote all \"(\"'s in STRING."
9292   (gnus-replace-in-string (if (and len (> (length string) len))
9293                               (substring string 0 len)
9294                             string)
9295                           "[()]" "\\\\\\&"))
9296
9297 (defun gnus-summary-print-article (&optional filename n)
9298   "Generate and print a PostScript image of the process-marked (mail) articles.
9299
9300 If used interactively, print the current article if none are
9301 process-marked.  With prefix arg, prompt the user for the name of the
9302 file to save in.
9303
9304 When used from Lisp, accept two optional args FILENAME and N.  N means
9305 to print the next N articles.  If N is negative, print the N previous
9306 articles.  If N is nil and articles have been marked with the process
9307 mark, print these instead.
9308
9309 If the optional first argument FILENAME is nil, send the image to the
9310 printer.  If FILENAME is a string, save the PostScript image in a file with
9311 that name.  If FILENAME is a number, prompt the user for the name of the file
9312 to save in."
9313   (interactive (list (ps-print-preprint current-prefix-arg)))
9314   (dolist (article (gnus-summary-work-articles n))
9315     (gnus-summary-select-article nil nil 'pseudo article)
9316     (gnus-eval-in-buffer-window gnus-article-buffer
9317       (gnus-print-buffer))
9318     (gnus-summary-remove-process-mark article))
9319   (ps-despool filename))
9320
9321 (defun gnus-print-buffer ()
9322   (let ((buffer (generate-new-buffer " *print*")))
9323     (unwind-protect
9324         (progn
9325           (copy-to-buffer buffer (point-min) (point-max))
9326           (set-buffer buffer)
9327           (gnus-remove-text-with-property 'gnus-decoration)
9328           (when (gnus-visual-p 'article-highlight 'highlight)
9329             ;; Copy-to-buffer doesn't copy overlay.  So redo
9330             ;; highlight.
9331             (let ((gnus-article-buffer buffer))
9332               (gnus-article-highlight-citation t)
9333               (gnus-article-highlight-signature)
9334               (gnus-article-emphasize)
9335               (gnus-article-delete-invisible-text)))
9336           (let ((ps-left-header
9337                  (list
9338                   (concat "("
9339                           (gnus-summary-print-truncate-and-quote
9340                            (mail-header-subject gnus-current-headers)
9341                            66) ")")
9342                   (concat "("
9343                           (gnus-summary-print-truncate-and-quote
9344                            (mail-header-from gnus-current-headers)
9345                            45) ")")))
9346                 (ps-right-header
9347                  (list
9348                   "/pagenumberstring load"
9349                   (concat "("
9350                           (mail-header-date gnus-current-headers) ")"))))
9351             (gnus-run-hooks 'gnus-ps-print-hook)
9352             (save-excursion
9353               (if ps-print-color-p
9354                   (ps-spool-buffer-with-faces)
9355                 (ps-spool-buffer)))))
9356       (kill-buffer buffer))))
9357
9358 (defun gnus-summary-show-complete-article ()
9359   "Show a complete version of the current article.
9360 This is only useful if you're looking at a partial version of the
9361 article currently."
9362   (interactive)
9363   (let ((gnus-keep-backlog nil)
9364         (gnus-use-cache nil)
9365         (gnus-agent nil)
9366         (variable (intern
9367                    (format "%s-fetch-partial-articles"
9368                            (car (gnus-find-method-for-group
9369                                  gnus-newsgroup-name)))
9370                    obarray))
9371         old-val)
9372     (unwind-protect
9373         (progn
9374           (setq old-val (symbol-value variable))
9375           (set variable nil)
9376           (gnus-flush-original-article-buffer)
9377           (gnus-summary-show-article))
9378       (set variable old-val))))
9379
9380 (defun gnus-summary-show-article (&optional arg)
9381   "Force redisplaying of the current article.
9382 If ARG (the prefix) is a number, show the article with the charset
9383 defined in `gnus-summary-show-article-charset-alist', or the charset
9384 input.
9385 If ARG (the prefix) is non-nil and not a number, show the raw article
9386 without any article massaging functions being run.  Normally, the key
9387 strokes are `C-u g'."
9388   (interactive "P")
9389   (cond
9390    ((numberp arg)
9391     (gnus-summary-show-article t)
9392     (let ((gnus-newsgroup-charset
9393            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9394                (mm-read-coding-system
9395                 "View as charset: " ;; actually it is coding system.
9396                 (with-current-buffer gnus-article-buffer
9397                   (mm-detect-coding-region (point) (point-max))))))
9398           (gnus-newsgroup-ignored-charsets 'gnus-all))
9399       (gnus-summary-select-article nil 'force)
9400       (let ((deps gnus-newsgroup-dependencies)
9401             head header lines)
9402         (with-current-buffer gnus-original-article-buffer
9403           (save-restriction
9404             (message-narrow-to-head)
9405             (setq head (buffer-string))
9406             (goto-char (point-min))
9407             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9408               (goto-char (point-max))
9409               (widen)
9410               (setq lines (1- (count-lines (point) (point-max))))))
9411           (with-temp-buffer
9412             (insert (format "211 %d Article retrieved.\n"
9413                             (cdr gnus-article-current)))
9414             (insert head)
9415             (if lines (insert (format "Lines: %d\n" lines)))
9416             (insert ".\n")
9417             (let ((nntp-server-buffer (current-buffer)))
9418               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9419         (gnus-data-set-header
9420          (gnus-data-find (cdr gnus-article-current))
9421          header)
9422         (gnus-summary-update-article-line
9423          (cdr gnus-article-current) header)
9424         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9425           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9426    ((not arg)
9427     ;; Select the article the normal way.
9428     (gnus-summary-select-article nil 'force))
9429    (t
9430     ;; We have to require this here to make sure that the following
9431     ;; dynamic binding isn't shadowed by autoloading.
9432     (require 'gnus-async)
9433     (require 'gnus-art)
9434     ;; Bind the article treatment functions to nil.
9435     (let ((gnus-have-all-headers t)
9436           gnus-article-prepare-hook
9437           gnus-article-decode-hook
9438           gnus-display-mime-function
9439           gnus-break-pages)
9440       ;; Destroy any MIME parts.
9441       (when (gnus-buffer-live-p gnus-article-buffer)
9442         (with-current-buffer gnus-article-buffer
9443           (mm-destroy-parts gnus-article-mime-handles)
9444           ;; Set it to nil for safety reason.
9445           (setq gnus-article-mime-handle-alist nil)
9446           (setq gnus-article-mime-handles nil)))
9447       (gnus-summary-select-article nil 'force))))
9448   (gnus-summary-goto-subject gnus-current-article)
9449   (gnus-summary-position-point))
9450
9451 (defun gnus-summary-show-raw-article ()
9452   "Show the raw article without any article massaging functions being run."
9453   (interactive)
9454   (gnus-summary-show-article t))
9455
9456 (defun gnus-summary-verbose-headers (&optional arg)
9457   "Toggle permanent full header display.
9458 If ARG is a positive number, turn header display on.
9459 If ARG is a negative number, turn header display off."
9460   (interactive "P")
9461   (setq gnus-show-all-headers
9462         (cond ((or (not (numberp arg))
9463                    (zerop arg))
9464                (not gnus-show-all-headers))
9465               ((natnump arg)
9466                t)))
9467   (gnus-summary-show-article))
9468
9469 (defun gnus-summary-toggle-header (&optional arg)
9470   "Show the headers if they are hidden, or hide them if they are shown.
9471 If ARG is a positive number, show the entire header.
9472 If ARG is a negative number, hide the unwanted header lines."
9473   (interactive "P")
9474   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9475                      (get-buffer-window gnus-article-buffer t))))
9476     (with-current-buffer gnus-article-buffer
9477       (widen)
9478       (article-narrow-to-head)
9479       (let* ((inhibit-read-only t)
9480              (inhibit-point-motion-hooks t)
9481              (hidden (if (numberp arg)
9482                          (>= arg 0)
9483                        (or (not (looking-at "[^ \t\n]+:"))
9484                            (gnus-article-hidden-text-p 'headers))))
9485              s e)
9486         (delete-region (point-min) (point-max))
9487         (with-current-buffer gnus-original-article-buffer
9488           (goto-char (setq s (point-min)))
9489           (setq e (if (search-forward "\n\n" nil t)
9490                       (1- (point))
9491                     (point-max))))
9492         (insert-buffer-substring gnus-original-article-buffer s e)
9493         (run-hooks 'gnus-article-decode-hook)
9494         (if hidden
9495             (let ((gnus-treat-hide-headers nil)
9496                   (gnus-treat-hide-boring-headers nil))
9497               (gnus-delete-wash-type 'headers)
9498               (gnus-treat-article 'head))
9499           (gnus-treat-article 'head))
9500         (widen)
9501         (if window
9502             (set-window-start window (goto-char (point-min))))
9503         (if gnus-break-pages
9504             (gnus-narrow-to-page)
9505           (when (gnus-visual-p 'page-marker)
9506             (let ((inhibit-read-only t))
9507               (gnus-remove-text-with-property 'gnus-prev)
9508               (gnus-remove-text-with-property 'gnus-next))))
9509         (gnus-set-mode-line 'article)))))
9510
9511 (defun gnus-summary-show-all-headers ()
9512   "Make all header lines visible."
9513   (interactive)
9514   (gnus-summary-toggle-header 1))
9515
9516 (defun gnus-summary-caesar-message (&optional arg)
9517   "Caesar rotate the current article by 13.
9518 With a non-numerical prefix, also rotate headers.  A numerical
9519 prefix specifies how many places to rotate each letter forward."
9520   (interactive "P")
9521   (gnus-summary-select-article)
9522   (let ((mail-header-separator ""))
9523     (gnus-eval-in-buffer-window gnus-article-buffer
9524       (save-restriction
9525         (widen)
9526         (let ((start (window-start))
9527               (inhibit-read-only t))
9528           (if (equal arg '(4))
9529               (message-caesar-buffer-body nil t)
9530             (message-caesar-buffer-body arg))
9531           (set-window-start (get-buffer-window (current-buffer)) start)))))
9532   ;; Create buttons and stuff...
9533   (gnus-treat-article nil))
9534
9535 (declare-function idna-to-unicode "ext:idna" (str))
9536
9537 (defun gnus-summary-idna-message (&optional arg)
9538   "Decode IDNA encoded domain names in the current articles.
9539 IDNA encoded domain names looks like `xn--bar'.  If a string
9540 remain unencoded after running this function, it is likely an
9541 invalid IDNA string (`xn--bar' is invalid).
9542
9543 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9544 installed for this command to work."
9545   (interactive "P")
9546   (if (not (and (condition-case nil (require 'idna)
9547                   (file-error))
9548                 (mm-coding-system-p 'utf-8)
9549                 (executable-find (symbol-value 'idna-program))))
9550       (gnus-message
9551        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9552     (gnus-summary-select-article)
9553     (let ((mail-header-separator ""))
9554       (gnus-eval-in-buffer-window gnus-article-buffer
9555         (save-restriction
9556           (widen)
9557           (let ((start (window-start))
9558                 buffer-read-only)
9559             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9560               (replace-match (idna-to-unicode (match-string 1))))
9561             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9562
9563 (defun gnus-summary-morse-message (&optional arg)
9564   "Morse decode the current article."
9565   (interactive "P")
9566   (gnus-summary-select-article)
9567   (let ((mail-header-separator ""))
9568     (gnus-eval-in-buffer-window gnus-article-buffer
9569       (save-excursion
9570         (save-restriction
9571           (widen)
9572           (let ((pos (window-start))
9573                 (inhibit-read-only t))
9574             (goto-char (point-min))
9575             (when (message-goto-body)
9576               (gnus-narrow-to-body))
9577             (goto-char (point-min))
9578             (while (search-forward "·" (point-max) t)
9579               (replace-match "."))
9580             (unmorse-region (point-min) (point-max))
9581             (widen)
9582             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9583
9584 (defun gnus-summary-stop-page-breaking ()
9585   "Stop page breaking in the current article."
9586   (interactive)
9587   (gnus-summary-select-article)
9588   (gnus-eval-in-buffer-window gnus-article-buffer
9589     (widen)
9590     (when (gnus-visual-p 'page-marker)
9591       (let ((inhibit-read-only t))
9592         (gnus-remove-text-with-property 'gnus-prev)
9593         (gnus-remove-text-with-property 'gnus-next))
9594       (setq gnus-page-broken nil))))
9595
9596 (defun gnus-summary-move-article (&optional n to-newsgroup
9597                                             select-method action)
9598   "Move the current article to a different newsgroup.
9599 If N is a positive number, move the N next articles.
9600 If N is a negative number, move the N previous articles.
9601 If N is nil and any articles have been marked with the process mark,
9602 move those articles instead.
9603 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9604 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9605 re-spool using this method.
9606
9607 When called interactively with TO-NEWSGROUP being nil, the value of
9608 the variable `gnus-move-split-methods' is used for finding a default
9609 for the target newsgroup.
9610
9611 For this function to work, both the current newsgroup and the
9612 newsgroup that you want to move to have to support the `request-move'
9613 and `request-accept' functions.
9614
9615 ACTION can be either `move' (the default), `crosspost' or `copy'."
9616   (interactive "P")
9617   (unless action
9618     (setq action 'move))
9619   ;; Check whether the source group supports the required functions.
9620   (cond ((and (eq action 'move)
9621               (not (gnus-check-backend-function
9622                     'request-move-article gnus-newsgroup-name)))
9623          (error "The current group does not support article moving"))
9624         ((and (eq action 'crosspost)
9625               (not (gnus-check-backend-function
9626                     'request-replace-article gnus-newsgroup-name)))
9627          (error "The current group does not support article editing")))
9628   (let ((articles (gnus-summary-work-articles n))
9629         (prefix (if (gnus-check-backend-function
9630                      'request-move-article gnus-newsgroup-name)
9631                     (funcall gnus-move-group-prefix-function
9632                              gnus-newsgroup-name)
9633                   ""))
9634         (names '((move "Move" "Moving")
9635                  (copy "Copy" "Copying")
9636                  (crosspost "Crosspost" "Crossposting")))
9637         (copy-buf (save-excursion
9638                     (nnheader-set-temp-buffer " *copy article*")))
9639         art-group to-method new-xref article to-groups
9640         articles-to-update-marks encoded)
9641     (unless (assq action names)
9642       (error "Unknown action %s" action))
9643     ;; Read the newsgroup name.
9644     (when (and (not to-newsgroup)
9645                (not select-method))
9646       (if (and gnus-move-split-methods
9647                (not
9648                 (and (memq gnus-current-article articles)
9649                      (gnus-buffer-live-p gnus-original-article-buffer))))
9650           ;; When `gnus-move-split-methods' is non-nil, we have to
9651           ;; select an article to give `gnus-read-move-group-name' an
9652           ;; opportunity to suggest an appropriate default.  However,
9653           ;; we needn't render or mark the article.
9654           (let ((gnus-display-mime-function nil)
9655                 (gnus-article-prepare-hook nil)
9656                 (gnus-mark-article-hook nil))
9657             (gnus-summary-select-article nil nil nil (car articles))))
9658       (setq to-newsgroup (gnus-read-move-group-name
9659                           (cadr (assq action names))
9660                           (symbol-value
9661                            (intern (format "gnus-current-%s-group" action)))
9662                           articles prefix)
9663             encoded to-newsgroup
9664             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9665       (set (intern (format "gnus-current-%s-group" action))
9666            (mm-decode-coding-string
9667             to-newsgroup
9668             (gnus-group-name-charset to-method to-newsgroup))))
9669     (unless to-method
9670       (setq to-method (or select-method
9671                           (gnus-server-to-method
9672                            (gnus-group-method to-newsgroup)))))
9673     (setq to-newsgroup
9674           (or encoded
9675               (and to-newsgroup
9676                    (mm-encode-coding-string
9677                     to-newsgroup
9678                     (gnus-group-name-charset to-method to-newsgroup)))))
9679     ;; Check the method we are to move this article to...
9680     (unless (gnus-check-backend-function
9681              'request-accept-article (car to-method))
9682       (error "%s does not support article copying" (car to-method)))
9683     (unless (gnus-check-server to-method)
9684       (error "Can't open server %s" (car to-method)))
9685     (gnus-message 6 "%s to %s: %s..."
9686                   (caddr (assq action names))
9687                   (or (car select-method)
9688                       (gnus-group-decoded-name to-newsgroup))
9689                   articles)
9690     (while articles
9691       (setq article (pop articles))
9692       (setq
9693        art-group
9694        (cond
9695         ;; Move the article.
9696         ((eq action 'move)
9697          ;; Remove this article from future suppression.
9698          (gnus-dup-unsuppress-article article)
9699          (let* ((from-method (gnus-find-method-for-group
9700                               gnus-newsgroup-name))
9701                 (to-method (or select-method
9702                                (gnus-find-method-for-group to-newsgroup)))
9703                 (move-is-internal (gnus-server-equal from-method to-method)))
9704            (gnus-request-move-article
9705             article                     ; Article to move
9706             gnus-newsgroup-name         ; From newsgroup
9707             (nth 1 (gnus-find-method-for-group
9708                     gnus-newsgroup-name)) ; Server
9709             (list 'gnus-request-accept-article
9710                   to-newsgroup (list 'quote select-method)
9711                   (not articles) t)     ; Accept form
9712             (not articles)              ; Only save nov last time
9713             (and move-is-internal
9714                  to-newsgroup           ; Not respooling
9715                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9716         ;; Copy the article.
9717         ((eq action 'copy)
9718          (with-current-buffer copy-buf
9719            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9720              (save-restriction
9721                (nnheader-narrow-to-headers)
9722                (dolist (hdr gnus-copy-article-ignored-headers)
9723                  (message-remove-header hdr t)))
9724              (gnus-request-accept-article
9725               to-newsgroup select-method (not articles) t))))
9726         ;; Crosspost the article.
9727         ((eq action 'crosspost)
9728          (let ((xref (message-tokenize-header
9729                       (mail-header-xref (gnus-summary-article-header article))
9730                       " ")))
9731            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9732                                   ":" (number-to-string article)))
9733            (unless xref
9734              (setq xref (list (system-name))))
9735            (setq new-xref
9736                  (concat
9737                   (mapconcat 'identity
9738                              (delete "Xref:" (delete new-xref xref))
9739                              " ")
9740                   " " new-xref))
9741            (with-current-buffer copy-buf
9742              ;; First put the article in the destination group.
9743              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9744              (when (consp (setq art-group
9745                                 (gnus-request-accept-article
9746                                  to-newsgroup select-method (not articles) t)))
9747                (setq new-xref (concat new-xref " " (car art-group)
9748                                       ":"
9749                                       (number-to-string (cdr art-group))))
9750                ;; Now we have the new Xrefs header, so we insert
9751                ;; it and replace the new article.
9752                (nnheader-replace-header "Xref" new-xref)
9753                (gnus-request-replace-article
9754                 (cdr art-group) to-newsgroup (current-buffer) t)
9755                art-group))))))
9756       (cond
9757        ((not art-group)
9758         (gnus-message 1 "Couldn't %s article %s: %s"
9759                       (cadr (assq action names)) article
9760                       (nnheader-get-report (car to-method))))
9761        ((eq art-group 'junk)
9762         (when (eq action 'move)
9763           (gnus-summary-mark-article article gnus-canceled-mark)
9764           (gnus-message 4 "Deleted article %s" article)
9765           ;; run the delete hook
9766           (run-hook-with-args 'gnus-summary-article-delete-hook
9767                               action
9768                               (gnus-data-header
9769                                (assoc article (gnus-data-list nil)))
9770                               gnus-newsgroup-name nil
9771                               select-method)))
9772        (t
9773         (let* ((pto-group (gnus-group-prefixed-name
9774                            (car art-group) to-method))
9775                (info (gnus-get-info pto-group))
9776                (to-group (gnus-info-group info))
9777                to-marks)
9778           ;; Update the group that has been moved to.
9779           (when (and info
9780                      (memq action '(move copy)))
9781             (unless (member to-group to-groups)
9782               (push to-group to-groups))
9783
9784             (unless (memq article gnus-newsgroup-unreads)
9785               (push 'read to-marks)
9786               (gnus-info-set-read
9787                info (gnus-add-to-range (gnus-info-read info)
9788                                        (list (cdr art-group)))))
9789
9790             ;; See whether the article is to be put in the cache.
9791             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9792                    (marks (if expirable
9793                               gnus-article-mark-lists
9794                             (delete '(expirable . expire)
9795                                     (copy-sequence gnus-article-mark-lists))))
9796                    (to-article (cdr art-group)))
9797
9798               ;; Enter the article into the cache in the new group,
9799               ;; if that is required.
9800               (when gnus-use-cache
9801                 (gnus-cache-possibly-enter-article
9802                  to-group to-article
9803                  (memq article gnus-newsgroup-marked)
9804                  (memq article gnus-newsgroup-dormant)
9805                  (memq article gnus-newsgroup-unreads)))
9806
9807               (when gnus-preserve-marks
9808                 ;; Copy any marks over to the new group.
9809                 (when (and (equal to-group gnus-newsgroup-name)
9810                            (not (memq article gnus-newsgroup-unreads)))
9811                   ;; Mark this article as read in this group.
9812                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9813                   ;; Increase the active status of this group.
9814                   (setcdr (gnus-active to-group) to-article)
9815                   (setcdr gnus-newsgroup-active to-article))
9816
9817                 (while marks
9818                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9819                     (when (memq article (symbol-value
9820                                          (intern (format "gnus-newsgroup-%s"
9821                                                          (caar marks)))))
9822                       (push (cdar marks) to-marks)
9823                       ;; If the other group is the same as this group,
9824                       ;; then we have to add the mark to the list.
9825                       (when (equal to-group gnus-newsgroup-name)
9826                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9827                              (cons to-article
9828                                    (symbol-value
9829                                     (intern (format "gnus-newsgroup-%s"
9830                                                     (caar marks)))))))
9831                       ;; Copy the marks to other group.
9832                       (gnus-add-marked-articles
9833                        to-group (cdar marks) (list to-article) info)))
9834                   (setq marks (cdr marks)))
9835
9836                 (when (and expirable
9837                            gnus-mark-copied-or-moved-articles-as-expirable
9838                            (not (memq 'expire to-marks)))
9839                   ;; Mark this article as expirable.
9840                   (push 'expire to-marks)
9841                   (when (equal to-group gnus-newsgroup-name)
9842                     (push to-article gnus-newsgroup-expirable))
9843                   ;; Copy the expirable mark to other group.
9844                   (gnus-add-marked-articles
9845                    to-group 'expire (list to-article) info))
9846
9847                 (when to-marks
9848                   (gnus-request-set-mark
9849                    to-group (list (list (list to-article) 'add to-marks)))))
9850
9851               (gnus-dribble-enter
9852                (concat "(gnus-group-set-info '"
9853                        (gnus-prin1-to-string (gnus-get-info to-group))
9854                        ")"))))
9855
9856           ;; Update the Xref header in this article to point to
9857           ;; the new crossposted article we have just created.
9858           (when (eq action 'crosspost)
9859             (with-current-buffer copy-buf
9860               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9861               (nnheader-replace-header "Xref" new-xref)
9862               (gnus-request-replace-article
9863                article gnus-newsgroup-name (current-buffer) t)))
9864
9865           ;; run the move/copy/crosspost/respool hook
9866           (run-hook-with-args 'gnus-summary-article-move-hook
9867                               action
9868                               (gnus-data-header
9869                                (assoc article (gnus-data-list nil)))
9870                               gnus-newsgroup-name
9871                               to-newsgroup
9872                               select-method))
9873
9874         ;;;!!!Why is this necessary?
9875         (set-buffer gnus-summary-buffer)
9876
9877         (when (eq action 'move)
9878           (save-excursion
9879             (gnus-summary-goto-subject article)
9880             (gnus-summary-mark-article article gnus-canceled-mark)))))
9881       (push article articles-to-update-marks))
9882
9883     (save-excursion
9884       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9885     ;; Re-activate all groups that have been moved to.
9886     (with-current-buffer gnus-group-buffer
9887       (let ((gnus-group-marked to-groups))
9888         (gnus-group-get-new-news-this-group nil t)))
9889
9890     (gnus-kill-buffer copy-buf)
9891     (gnus-summary-position-point)
9892     (gnus-set-mode-line 'summary)))
9893
9894 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9895   "Copy the current article to some other group.
9896 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9897 When called interactively, if TO-NEWSGROUP is nil, use the value of
9898 the variable `gnus-move-split-methods' for finding a default target
9899 newsgroup.
9900 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9901 re-spool using this method."
9902   (interactive "P")
9903   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9904
9905 (defun gnus-summary-crosspost-article (&optional n)
9906   "Crosspost the current article to some other group."
9907   (interactive "P")
9908   (gnus-summary-move-article n nil nil 'crosspost))
9909
9910 (defcustom gnus-summary-respool-default-method nil
9911   "Default method type for respooling an article.
9912 If nil, use to the current newsgroup method."
9913   :type 'symbol
9914   :group 'gnus-summary-mail)
9915
9916 (defun gnus-summary-respool-article (&optional n method)
9917   "Respool the current article.
9918 The article will be squeezed through the mail spooling process again,
9919 which means that it will be put in some mail newsgroup or other
9920 depending on `nnmail-split-methods'.
9921 If N is a positive number, respool the N next articles.
9922 If N is a negative number, respool the N previous articles.
9923 If N is nil and any articles have been marked with the process mark,
9924 respool those articles instead.
9925
9926 Respooling can be done both from mail groups and \"real\" newsgroups.
9927 In the former case, the articles in question will be moved from the
9928 current group into whatever groups they are destined to.  In the
9929 latter case, they will be copied into the relevant groups."
9930   (interactive
9931    (list current-prefix-arg
9932          (let* ((methods (gnus-methods-using 'respool))
9933                 (methname
9934                  (symbol-name (or gnus-summary-respool-default-method
9935                                   (car (gnus-find-method-for-group
9936                                         gnus-newsgroup-name)))))
9937                 (method
9938                  (gnus-completing-read
9939                   "Backend to use when respooling"
9940                   methods t nil 'gnus-mail-method-history methname))
9941                 ms)
9942            (cond
9943             ((zerop (length (setq ms (gnus-servers-using-backend
9944                                       (intern method)))))
9945              (list (intern method) ""))
9946             ((= 1 (length ms))
9947              (car ms))
9948             (t
9949              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9950                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
9951                            ms-alist))))))))
9952   (unless method
9953     (error "No method given for respooling"))
9954   (if (assoc (symbol-name
9955               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9956              (gnus-methods-using 'respool))
9957       (gnus-summary-move-article n nil method)
9958     (gnus-summary-copy-article n nil method)))
9959
9960 (defun gnus-summary-import-article (file &optional edit)
9961   "Import an arbitrary file into a mail newsgroup."
9962   (interactive "fImport file: \nP")
9963   (let ((group gnus-newsgroup-name)
9964         (now (current-time))
9965         atts lines group-art)
9966     (unless (gnus-check-backend-function 'request-accept-article group)
9967       (error "%s does not support article importing" group))
9968     (or (file-readable-p file)
9969         (not (file-regular-p file))
9970         (error "Can't read %s" file))
9971     (with-current-buffer (gnus-get-buffer-create " *import file*")
9972       (erase-buffer)
9973       (nnheader-insert-file-contents file)
9974       (goto-char (point-min))
9975       (if (nnheader-article-p)
9976           (save-restriction
9977             (goto-char (point-min))
9978             (search-forward "\n\n" nil t)
9979             (narrow-to-region (point-min) (1- (point)))
9980             (goto-char (point-min))
9981             (unless (re-search-forward "^date:" nil t)
9982               (goto-char (point-max))
9983               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9984        ;; This doesn't look like an article, so we fudge some headers.
9985         (setq atts (file-attributes file)
9986               lines (count-lines (point-min) (point-max)))
9987         (insert "From: " (read-string "From: ") "\n"
9988                 "Subject: " (read-string "Subject: ") "\n"
9989                 "Date: " (message-make-date (nth 5 atts)) "\n"
9990                 "Message-ID: " (message-make-message-id) "\n"
9991                 "Lines: " (int-to-string lines) "\n"
9992                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9993       (setq group-art (gnus-request-accept-article group nil t))
9994       (kill-buffer (current-buffer)))
9995     (setq gnus-newsgroup-active (gnus-activate-group group))
9996     (forward-line 1)
9997     (gnus-summary-goto-article (cdr group-art) nil t)
9998     (when edit
9999       (gnus-summary-edit-article))))
10000
10001 (defun gnus-summary-create-article ()
10002   "Create an article in a mail newsgroup."
10003   (interactive)
10004   (let ((group gnus-newsgroup-name)
10005         (now (current-time))
10006         group-art)
10007     (unless (gnus-check-backend-function 'request-accept-article group)
10008       (error "%s does not support article importing" group))
10009     (with-current-buffer (gnus-get-buffer-create " *import file*")
10010       (erase-buffer)
10011       (goto-char (point-min))
10012       ;; This doesn't look like an article, so we fudge some headers.
10013       (insert "From: " (read-string "From: ") "\n"
10014               "Subject: " (read-string "Subject: ") "\n"
10015               "Date: " (message-make-date now) "\n"
10016               "Message-ID: " (message-make-message-id) "\n")
10017       (setq group-art (gnus-request-accept-article group nil t))
10018       (kill-buffer (current-buffer)))
10019     (setq gnus-newsgroup-active (gnus-activate-group group))
10020     (forward-line 1)
10021     (gnus-summary-goto-article (cdr group-art) nil t)
10022     (gnus-summary-edit-article)))
10023
10024 (defun gnus-summary-article-posted-p ()
10025   "Say whether the current (mail) article is available from news as well.
10026 This will be the case if the article has both been mailed and posted."
10027   (interactive)
10028   (let ((id (mail-header-references (gnus-summary-article-header)))
10029         (gnus-override-method (car (gnus-refer-article-methods))))
10030     (if (gnus-request-head id "")
10031         (gnus-message 2 "The current message was found on %s"
10032                       gnus-override-method)
10033       (gnus-message 2 "The current message couldn't be found on %s"
10034                     gnus-override-method)
10035       nil)))
10036
10037 (defun gnus-summary-expire-articles (&optional now)
10038   "Expire all articles that are marked as expirable in the current group."
10039   (interactive)
10040   (when (and (not gnus-group-is-exiting-without-update-p)
10041              (gnus-check-backend-function
10042               'request-expire-articles gnus-newsgroup-name))
10043     ;; This backend supports expiry.
10044     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10045            (expirable (if total
10046                           (progn
10047                             ;; We need to update the info for
10048                             ;; this group for `gnus-list-of-read-articles'
10049                             ;; to give us the right answer.
10050                             (gnus-run-hooks 'gnus-exit-group-hook)
10051                             (gnus-summary-update-info)
10052                             (gnus-list-of-read-articles gnus-newsgroup-name))
10053                         (setq gnus-newsgroup-expirable
10054                               (sort gnus-newsgroup-expirable '<))))
10055            (expiry-wait (if now 'immediate
10056                           (gnus-group-find-parameter
10057                            gnus-newsgroup-name 'expiry-wait)))
10058            (nnmail-expiry-target
10059             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10060                 nnmail-expiry-target))
10061            es)
10062       (when expirable
10063         ;; There are expirable articles in this group, so we run them
10064         ;; through the expiry process.
10065         (gnus-message 6 "Expiring articles...")
10066         (unless (gnus-check-group gnus-newsgroup-name)
10067           (error "Can't open server for %s" gnus-newsgroup-name))
10068         ;; The list of articles that weren't expired is returned.
10069         (save-excursion
10070           (if expiry-wait
10071               (let ((nnmail-expiry-wait-function nil)
10072                     (nnmail-expiry-wait expiry-wait))
10073                 (setq es (gnus-request-expire-articles
10074                           expirable gnus-newsgroup-name)))
10075             (setq es (gnus-request-expire-articles
10076                       expirable gnus-newsgroup-name)))
10077           (unless total
10078             (setq gnus-newsgroup-expirable es))
10079           ;; We go through the old list of expirable, and mark all
10080           ;; really expired articles as nonexistent.
10081           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10082             (let ((gnus-use-cache nil))
10083               (dolist (article expirable)
10084                 (when (and (not (memq article es))
10085                            (gnus-data-find article))
10086                   (gnus-summary-mark-article article gnus-canceled-mark)
10087                   (run-hook-with-args 'gnus-summary-article-expire-hook
10088                                       'delete
10089                                       (gnus-data-header
10090                                        (assoc article (gnus-data-list nil)))
10091                                       gnus-newsgroup-name
10092                                       nil
10093                                       nil))))))
10094         (gnus-message 6 "Expiring articles...done")))))
10095
10096 (defun gnus-summary-expire-articles-now ()
10097   "Expunge all expirable articles in the current group.
10098 This means that *all* articles that are marked as expirable will be
10099 deleted forever, right now."
10100   (interactive)
10101   (or gnus-expert-user
10102       (gnus-yes-or-no-p
10103        "Are you really, really sure you want to delete all expirable messages? ")
10104       (error "Phew!"))
10105   (gnus-summary-expire-articles t))
10106
10107 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10108 (defun gnus-summary-delete-article (&optional n)
10109   "Delete the N next (mail) articles.
10110 This command actually deletes articles.  This is not a marking
10111 command.  The article will disappear forever from your life, never to
10112 return.
10113
10114 If N is negative, delete backwards.
10115 If N is nil and articles have been marked with the process mark,
10116 delete these instead.
10117
10118 If `gnus-novice-user' is non-nil you will be asked for
10119 confirmation before the articles are deleted."
10120   (interactive "P")
10121   (unless (gnus-check-backend-function 'request-expire-articles
10122                                        gnus-newsgroup-name)
10123     (error "The current newsgroup does not support article deletion"))
10124   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10125     (error "Couldn't open server"))
10126   ;; Compute the list of articles to delete.
10127   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10128         (nnmail-expiry-target 'delete)
10129         not-deleted)
10130     (if (and gnus-novice-user
10131              (not (gnus-yes-or-no-p
10132                    (format "Do you really want to delete %s forever? "
10133                            (if (> (length articles) 1)
10134                                (format "these %s articles" (length articles))
10135                              "this article")))))
10136         ()
10137       ;; Delete the articles.
10138       (setq not-deleted (gnus-request-expire-articles
10139                          articles gnus-newsgroup-name 'force))
10140       (save-excursion
10141         (while articles
10142           (gnus-summary-remove-process-mark (car articles))
10143           ;; The backend might not have been able to delete the article
10144           ;; after all.
10145           (unless (memq (car articles) not-deleted)
10146             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10147           (let* ((article (car articles))
10148                  (ghead  (gnus-data-header
10149                           (assoc article (gnus-data-list nil)))))
10150             (run-hook-with-args 'gnus-summary-article-delete-hook
10151                                 'delete ghead gnus-newsgroup-name nil
10152                                 nil))
10153           (setq articles (cdr articles))))
10154       (when not-deleted
10155         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10156     (gnus-summary-position-point)
10157     (gnus-set-mode-line 'summary)
10158     not-deleted))
10159
10160 (defun gnus-summary-edit-article (&optional arg)
10161   "Edit the current article.
10162 This will have permanent effect only in mail groups.
10163 If ARG is nil, edit the decoded articles.
10164 If ARG is 1, edit the raw articles.
10165 If ARG is 2, edit the raw articles even in read-only groups.
10166 If ARG is 3, edit the articles with the current handles.
10167 Otherwise, allow editing of articles even in read-only
10168 groups."
10169   (interactive "P")
10170   (let (force raw current-handles)
10171     (cond
10172      ((null arg))
10173      ((eq arg 1)
10174       (setq raw t))
10175      ((eq arg 2)
10176       (setq raw t
10177             force t))
10178      ((eq arg 3)
10179       (setq current-handles
10180             (and (gnus-buffer-live-p gnus-article-buffer)
10181                  (with-current-buffer gnus-article-buffer
10182                    (prog1
10183                        gnus-article-mime-handles
10184                      (setq gnus-article-mime-handles nil))))))
10185      (t
10186       (setq force t)))
10187     (when (and raw (not force)
10188                (member gnus-newsgroup-name '("nndraft:delayed"
10189                                              "nndraft:drafts"
10190                                              "nndraft:queue")))
10191       (error "Can't edit the raw article in group %s"
10192              gnus-newsgroup-name))
10193     (with-current-buffer gnus-summary-buffer
10194       (let ((mail-parse-charset gnus-newsgroup-charset)
10195             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10196         (gnus-set-global-variables)
10197         (when (and (not force)
10198                    (gnus-group-read-only-p))
10199           (error "The current newsgroup does not support article editing"))
10200         (gnus-summary-show-article t)
10201         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10202           (with-current-buffer gnus-article-buffer
10203             (mm-enable-multibyte)))
10204         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10205             (setq raw t))
10206         (gnus-article-edit-article
10207          (if raw 'ignore
10208            `(lambda ()
10209               (let ((mbl mml-buffer-list))
10210                 (setq mml-buffer-list nil)
10211                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10212                   (mime-to-mml ,'current-handles))
10213                 (let ((mbl1 mml-buffer-list))
10214                   (setq mml-buffer-list mbl)
10215                   (set (make-local-variable 'mml-buffer-list) mbl1))
10216                 (gnus-make-local-hook 'kill-buffer-hook)
10217                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10218          `(lambda (no-highlight)
10219             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10220                   (message-options message-options)
10221                   (message-options-set-recipient)
10222                   (mail-parse-ignored-charsets
10223                    ',gnus-newsgroup-ignored-charsets)
10224                   (rfc2047-header-encoding-alist
10225                    ',(let ((charset (gnus-group-name-charset
10226                                      (gnus-find-method-for-group
10227                                       gnus-newsgroup-name)
10228                                      gnus-newsgroup-name)))
10229                        (append (list (cons "Newsgroups" charset)
10230                                      (cons "Followup-To" charset)
10231                                      (cons "Xref" charset))
10232                                rfc2047-header-encoding-alist))))
10233               ,(if (not raw) '(progn
10234                                 (mml-to-mime)
10235                                 (mml-destroy-buffers)
10236                                 (remove-hook 'kill-buffer-hook
10237                                              'mml-destroy-buffers t)
10238                                 (kill-local-variable 'mml-buffer-list)))
10239               (gnus-summary-edit-article-done
10240                ,(or (mail-header-references gnus-current-headers) "")
10241                ,(gnus-group-read-only-p)
10242                ,gnus-summary-buffer no-highlight))))))))
10243
10244 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10245
10246 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10247                                                  no-highlight)
10248   "Make edits to the current article permanent."
10249   (interactive)
10250   (save-excursion
10251    ;; The buffer restriction contains the entire article if it exists.
10252     (when (article-goto-body)
10253       (let ((lines (count-lines (point) (point-max)))
10254             (length (- (point-max) (point)))
10255             (case-fold-search t)
10256             (body (copy-marker (point))))
10257         (goto-char (point-min))
10258         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10259           (delete-region (match-beginning 1) (match-end 1))
10260           (insert (number-to-string length)))
10261         (goto-char (point-min))
10262         (when (re-search-forward
10263                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10264           (delete-region (match-beginning 1) (match-end 1))
10265           (insert (number-to-string length)))
10266         (goto-char (point-min))
10267         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10268           (delete-region (match-beginning 1) (match-end 1))
10269           (insert (number-to-string lines))))))
10270   ;; Replace the article.
10271   (let ((buf (current-buffer)))
10272     (with-temp-buffer
10273       (insert-buffer-substring buf)
10274
10275       (if (and (not read-only)
10276                (not (gnus-request-replace-article
10277                      (cdr gnus-article-current) (car gnus-article-current)
10278                      (current-buffer) t)))
10279           (error "Couldn't replace article")
10280         ;; Update the summary buffer.
10281         (if (and references
10282                  (equal (message-tokenize-header references " ")
10283                         (message-tokenize-header
10284                          (or (message-fetch-field "references") "") " ")))
10285             ;; We only have to update this line.
10286             (save-excursion
10287               (save-restriction
10288                 (message-narrow-to-head)
10289                 (let ((head (buffer-substring-no-properties
10290                              (point-min) (point-max)))
10291                       header)
10292                   (with-temp-buffer
10293                     (insert (format "211 %d Article retrieved.\n"
10294                                     (cdr gnus-article-current)))
10295                     (insert head)
10296                     (insert ".\n")
10297                     (let ((nntp-server-buffer (current-buffer)))
10298                       (setq header (car (gnus-get-newsgroup-headers
10299                                          nil t))))
10300                     (with-current-buffer gnus-summary-buffer
10301                       (gnus-data-set-header
10302                        (gnus-data-find (cdr gnus-article-current))
10303                        header)
10304                       (gnus-summary-update-article-line
10305                        (cdr gnus-article-current) header)
10306                       (if (gnus-summary-goto-subject
10307                            (cdr gnus-article-current) nil t)
10308                           (gnus-summary-update-secondary-mark
10309                            (cdr gnus-article-current))))))))
10310           ;; Update threads.
10311           (set-buffer (or buffer gnus-summary-buffer))
10312           (gnus-summary-update-article (cdr gnus-article-current))
10313           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10314               (gnus-summary-update-secondary-mark
10315                (cdr gnus-article-current))))
10316         ;; Prettify the article buffer again.
10317         (unless no-highlight
10318           (with-current-buffer gnus-article-buffer
10319             ;;;!!! Fix this -- article should be rehighlighted.
10320             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10321             (set-buffer gnus-original-article-buffer)
10322             (gnus-request-article
10323              (cdr gnus-article-current)
10324              (car gnus-article-current) (current-buffer))))
10325         ;; Prettify the summary buffer line.
10326         (when (gnus-visual-p 'summary-highlight 'highlight)
10327           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10328
10329 (defun gnus-summary-edit-wash (key)
10330   "Perform editing command KEY in the article buffer."
10331   (interactive
10332    (list
10333     (progn
10334       (message "%s" (concat (this-command-keys) "- "))
10335       (read-char))))
10336   (message "")
10337   (gnus-summary-edit-article)
10338   (execute-kbd-macro (concat (this-command-keys) key))
10339   (gnus-article-edit-done))
10340
10341 ;;; Respooling
10342
10343 (defun gnus-summary-respool-query (&optional silent trace)
10344   "Query where the respool algorithm would put this article."
10345   (interactive)
10346   (let (gnus-mark-article-hook)
10347     (gnus-summary-select-article)
10348     (with-current-buffer gnus-original-article-buffer
10349       (let ((groups (nnmail-article-group 'identity trace)))
10350         (unless silent
10351           (if groups
10352               (message "This message would go to %s"
10353                        (mapconcat 'car groups ", "))
10354             (message "This message would go to no groups"))
10355           groups)))))
10356
10357 (defun gnus-summary-respool-trace ()
10358   "Trace where the respool algorithm would put this article.
10359 Display a buffer showing all fancy splitting patterns which matched."
10360   (interactive)
10361   (gnus-summary-respool-query nil t))
10362
10363 ;; Summary marking commands.
10364
10365 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10366   "Mark articles which has the same subject as read, and then select the next.
10367 If UNMARK is positive, remove any kind of mark.
10368 If UNMARK is negative, tick articles."
10369   (interactive "P")
10370   (when unmark
10371     (setq unmark (prefix-numeric-value unmark)))
10372   (let ((count
10373          (gnus-summary-mark-same-subject
10374           (gnus-summary-article-subject) unmark)))
10375     ;; Select next unread article.  If auto-select-same mode, should
10376     ;; select the first unread article.
10377     (gnus-summary-next-article t (and gnus-auto-select-same
10378                                       (gnus-summary-article-subject)))
10379     (gnus-message 7 "%d article%s marked as %s"
10380                   count (if (= count 1) " is" "s are")
10381                   (if unmark "unread" "read"))))
10382
10383 (defun gnus-summary-kill-same-subject (&optional unmark)
10384   "Mark articles which has the same subject as read.
10385 If UNMARK is positive, remove any kind of mark.
10386 If UNMARK is negative, tick articles."
10387   (interactive "P")
10388   (when unmark
10389     (setq unmark (prefix-numeric-value unmark)))
10390   (let ((count
10391          (gnus-summary-mark-same-subject
10392           (gnus-summary-article-subject) unmark)))
10393     ;; If marked as read, go to next unread subject.
10394     (when (null unmark)
10395       ;; Go to next unread subject.
10396       (gnus-summary-next-subject 1 t))
10397     (gnus-message 7 "%d articles are marked as %s"
10398                   count (if unmark "unread" "read"))))
10399
10400 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10401   "Mark articles with same SUBJECT as read, and return marked number.
10402 If optional argument UNMARK is positive, remove any kinds of marks.
10403 If optional argument UNMARK is negative, mark articles as unread instead."
10404   (let ((count 1))
10405     (save-excursion
10406       (cond
10407        ((null unmark)                   ; Mark as read.
10408         (while (and
10409                 (progn
10410                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10411                   (gnus-summary-show-thread) t)
10412                 (gnus-summary-find-subject subject))
10413           (setq count (1+ count))))
10414        ((> unmark 0)                    ; Tick.
10415         (while (and
10416                 (progn
10417                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10418                   (gnus-summary-show-thread) t)
10419                 (gnus-summary-find-subject subject))
10420           (setq count (1+ count))))
10421        (t                               ; Mark as unread.
10422         (while (and
10423                 (progn
10424                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10425                   (gnus-summary-show-thread) t)
10426                 (gnus-summary-find-subject subject))
10427           (setq count (1+ count)))))
10428       (gnus-set-mode-line 'summary)
10429       ;; Return the number of marked articles.
10430       count)))
10431
10432 (defun gnus-summary-mark-as-processable (n &optional unmark)
10433   "Set the process mark on the next N articles.
10434 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10435 the process mark instead.  The difference between N and the actual
10436 number of articles marked is returned."
10437   (interactive "P")
10438   (if (and (null n) (gnus-region-active-p))
10439       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10440     (setq n (prefix-numeric-value n))
10441     (let ((backward (< n 0))
10442           (n (abs n)))
10443       (while (and
10444               (> n 0)
10445               (if unmark
10446                   (gnus-summary-remove-process-mark
10447                    (gnus-summary-article-number))
10448                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10449               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10450         (setq n (1- n)))
10451       (when (/= 0 n)
10452         (gnus-message 7 "No more articles"))
10453       (gnus-summary-recenter)
10454       (gnus-summary-position-point)
10455       n)))
10456
10457 (defun gnus-summary-unmark-as-processable (n)
10458   "Remove the process mark from the next N articles.
10459 If N is negative, unmark backward instead.  The difference between N and
10460 the actual number of articles unmarked is returned."
10461   (interactive "P")
10462   (gnus-summary-mark-as-processable n t))
10463
10464 (defun gnus-summary-unmark-all-processable ()
10465   "Remove the process mark from all articles."
10466   (interactive)
10467   (save-excursion
10468     (while gnus-newsgroup-processable
10469       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10470   (gnus-summary-position-point))
10471
10472 (defun gnus-summary-add-mark (article type)
10473   "Mark ARTICLE with a mark of TYPE."
10474   (let ((vtype (car (assq type gnus-article-mark-lists)))
10475         var)
10476     (if (not vtype)
10477         (error "No such mark type: %s" type)
10478       (setq var (intern (format "gnus-newsgroup-%s" type)))
10479       (set var (cons article (symbol-value var)))
10480       (if (memq type '(processable cached replied forwarded recent saved))
10481           (gnus-summary-update-secondary-mark article)
10482         ;;; !!! This is bogus.  We should find out what primary
10483         ;;; !!! mark we want to set.
10484         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10485
10486 (defun gnus-summary-mark-as-expirable (n)
10487   "Mark N articles forward as expirable.
10488 If N is negative, mark backward instead.  The difference between N and
10489 the actual number of articles marked is returned."
10490   (interactive "p")
10491   (gnus-summary-mark-forward n gnus-expirable-mark))
10492
10493 (defun gnus-summary-mark-as-spam (n)
10494   "Mark N articles forward as spam.
10495 If N is negative, mark backward instead.  The difference between N and
10496 the actual number of articles marked is returned."
10497   (interactive "p")
10498   (gnus-summary-mark-forward n gnus-spam-mark))
10499
10500 (defun gnus-summary-mark-article-as-replied (article)
10501   "Mark ARTICLE as replied to and update the summary line.
10502 ARTICLE can also be a list of articles."
10503   (interactive (list (gnus-summary-article-number)))
10504   (let ((articles (if (listp article) article (list article))))
10505     (dolist (article articles)
10506       (unless (numberp article)
10507         (error "%s is not a number" article))
10508       (push article gnus-newsgroup-replied)
10509       (let ((inhibit-read-only t))
10510         (when (gnus-summary-goto-subject article nil t)
10511           (gnus-summary-update-secondary-mark article))))))
10512
10513 (defun gnus-summary-mark-article-as-forwarded (article)
10514   "Mark ARTICLE as forwarded and update the summary line.
10515 ARTICLE can also be a list of articles."
10516   (let ((articles (if (listp article) article (list article))))
10517     (dolist (article articles)
10518       (push article gnus-newsgroup-forwarded)
10519       (let ((inhibit-read-only t))
10520         (when (gnus-summary-goto-subject article nil t)
10521           (gnus-summary-update-secondary-mark article))))))
10522
10523 (defun gnus-summary-set-bookmark (article)
10524   "Set a bookmark in current article."
10525   (interactive (list (gnus-summary-article-number)))
10526   (when (or (not (get-buffer gnus-article-buffer))
10527             (not gnus-current-article)
10528             (not gnus-article-current)
10529             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10530     (error "No current article selected"))
10531   ;; Remove old bookmark, if one exists.
10532   (gnus-pull article gnus-newsgroup-bookmarks)
10533   ;; Set the new bookmark, which is on the form
10534   ;; (article-number . line-number-in-body).
10535   (push
10536    (cons article
10537          (with-current-buffer gnus-article-buffer
10538            (count-lines
10539             (min (point)
10540                  (save-excursion
10541                    (article-goto-body)
10542                    (point)))
10543             (point))))
10544    gnus-newsgroup-bookmarks)
10545   (gnus-message 6 "A bookmark has been added to the current article."))
10546
10547 (defun gnus-summary-remove-bookmark (article)
10548   "Remove the bookmark from the current article."
10549   (interactive (list (gnus-summary-article-number)))
10550   ;; Remove old bookmark, if one exists.
10551   (if (not (assq article gnus-newsgroup-bookmarks))
10552       (gnus-message 6 "No bookmark in current article.")
10553     (gnus-pull article gnus-newsgroup-bookmarks)
10554     (gnus-message 6 "Removed bookmark.")))
10555
10556 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10557 (defun gnus-summary-mark-as-dormant (n)
10558   "Mark N articles forward as dormant.
10559 If N is negative, mark backward instead.  The difference between N and
10560 the actual number of articles marked is returned."
10561   (interactive "p")
10562   (gnus-summary-mark-forward n gnus-dormant-mark))
10563
10564 (defun gnus-summary-set-process-mark (article)
10565   "Set the process mark on ARTICLE and update the summary line."
10566   (setq gnus-newsgroup-processable
10567         (cons article
10568               (delq article gnus-newsgroup-processable)))
10569   (when (gnus-summary-goto-subject article)
10570     (gnus-summary-show-thread)
10571     (gnus-summary-goto-subject article)
10572     (gnus-summary-update-secondary-mark article)))
10573
10574 (defun gnus-summary-remove-process-mark (&rest articles)
10575   "Remove the process mark from ARTICLES and update the summary line."
10576   (dolist (article articles)
10577     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10578     (when (gnus-summary-goto-subject article)
10579       (gnus-summary-show-thread)
10580       (gnus-summary-goto-subject article)
10581       (gnus-summary-update-secondary-mark article)))
10582   t)
10583
10584 (defun gnus-summary-set-saved-mark (article)
10585   "Set the process mark on ARTICLE and update the summary line."
10586   (push article gnus-newsgroup-saved)
10587   (when (gnus-summary-goto-subject article)
10588     (gnus-summary-update-secondary-mark article)))
10589
10590 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10591   "Mark N articles as read forwards.
10592 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10593 The difference between N and the actual number of articles marked is
10594 returned.
10595 If NO-EXPIRE, auto-expiry will be inhibited."
10596   (interactive "p")
10597   (gnus-summary-show-thread)
10598   (let ((backward (< n 0))
10599         (gnus-summary-goto-unread
10600          (and gnus-summary-goto-unread
10601               (not (eq gnus-summary-goto-unread 'never))
10602               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10603                                     gnus-ticked-mark gnus-dormant-mark)))))
10604         (n (abs n))
10605         (mark (or mark gnus-del-mark)))
10606     (while (and (> n 0)
10607                 (gnus-summary-mark-article nil mark no-expire)
10608                 (zerop (gnus-summary-next-subject
10609                         (if backward -1 1)
10610                         (and gnus-summary-goto-unread
10611                              (not (eq gnus-summary-goto-unread 'never)))
10612                         t)))
10613       (setq n (1- n)))
10614     (when (/= 0 n)
10615       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10616     (gnus-summary-recenter)
10617     (gnus-summary-position-point)
10618     (gnus-set-mode-line 'summary)
10619     n))
10620
10621 (defun gnus-summary-mark-article-as-read (mark)
10622   "Mark the current article quickly as read with MARK."
10623   (let ((article (gnus-summary-article-number)))
10624     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10625     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10626     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10627     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10628     (push (cons article mark) gnus-newsgroup-reads)
10629     ;; Possibly remove from cache, if that is used.
10630     (when gnus-use-cache
10631       (gnus-cache-enter-remove-article article))
10632     ;; Allow the backend to change the mark.
10633     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10634     ;; Check for auto-expiry.
10635     (when (and gnus-newsgroup-auto-expire
10636                (memq mark gnus-auto-expirable-marks))
10637       (setq mark gnus-expirable-mark)
10638       ;; Let the backend know about the mark change.
10639       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10640       (push article gnus-newsgroup-expirable))
10641     ;; Set the mark in the buffer.
10642     (gnus-summary-update-mark mark 'unread)
10643     t))
10644
10645 (defun gnus-summary-mark-article-as-unread (mark)
10646   "Mark the current article quickly as unread with MARK."
10647   (let* ((article (gnus-summary-article-number))
10648          (old-mark (gnus-summary-article-mark article)))
10649     ;; Allow the backend to change the mark.
10650     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10651     (if (eq mark old-mark)
10652         t
10653       (if (<= article 0)
10654           (progn
10655             (gnus-error 1 "Can't mark negative article numbers")
10656             nil)
10657         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10658         (setq gnus-newsgroup-spam-marked
10659               (delq article gnus-newsgroup-spam-marked))
10660         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10661         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10662         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10663         (cond ((= mark gnus-ticked-mark)
10664                (setq gnus-newsgroup-marked
10665                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10666                                               article)))
10667               ((= mark gnus-spam-mark)
10668                (setq gnus-newsgroup-spam-marked
10669                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10670                                               article)))
10671               ((= mark gnus-dormant-mark)
10672                (setq gnus-newsgroup-dormant
10673                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10674                                               article)))
10675               (t
10676                (setq gnus-newsgroup-unreads
10677                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10678                                               article))))
10679         (gnus-pull article gnus-newsgroup-reads)
10680
10681         ;; See whether the article is to be put in the cache.
10682         (and gnus-use-cache
10683              (vectorp (gnus-summary-article-header article))
10684              (save-excursion
10685                (gnus-cache-possibly-enter-article
10686                 gnus-newsgroup-name article
10687                 (= mark gnus-ticked-mark)
10688                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10689
10690         ;; Fix the mark.
10691         (gnus-summary-update-mark mark 'unread)
10692         t))))
10693
10694 (defun gnus-summary-mark-article (&optional article mark no-expire)
10695   "Mark ARTICLE with MARK.  MARK can be any character.
10696 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10697 `??' (dormant) and `?E' (expirable).
10698 If MARK is nil, then the default character `?r' is used.
10699 If ARTICLE is nil, then the article on the current line will be
10700 marked.
10701 If NO-EXPIRE, auto-expiry will be inhibited."
10702   ;; The mark might be a string.
10703   (when (stringp mark)
10704     (setq mark (aref mark 0)))
10705   ;; If no mark is given, then we check auto-expiring.
10706   (when (null mark)
10707     (setq mark gnus-del-mark))
10708   (when (and (not no-expire)
10709              gnus-newsgroup-auto-expire
10710              (memq mark gnus-auto-expirable-marks))
10711     (setq mark gnus-expirable-mark))
10712   (let ((article (or article (gnus-summary-article-number)))
10713         (old-mark (gnus-summary-article-mark article)))
10714     ;; Allow the backend to change the mark.
10715     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10716     (if (eq mark old-mark)
10717         t
10718       (unless article
10719         (error "No article on current line"))
10720       (if (not (if (or (= mark gnus-unread-mark)
10721                        (= mark gnus-ticked-mark)
10722                        (= mark gnus-spam-mark)
10723                        (= mark gnus-dormant-mark))
10724                    (gnus-mark-article-as-unread article mark)
10725                  (gnus-mark-article-as-read article mark)))
10726           t
10727         ;; See whether the article is to be put in the cache.
10728         (and gnus-use-cache
10729              (not (= mark gnus-canceled-mark))
10730              (vectorp (gnus-summary-article-header article))
10731              (save-excursion
10732                (gnus-cache-possibly-enter-article
10733                 gnus-newsgroup-name article
10734                 (= mark gnus-ticked-mark)
10735                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10736
10737         (when (gnus-summary-goto-subject article nil t)
10738           (let ((inhibit-read-only t))
10739             (gnus-summary-show-thread)
10740             ;; Fix the mark.
10741             (gnus-summary-update-mark mark 'unread)
10742             t))))))
10743
10744 (defun gnus-summary-update-secondary-mark (article)
10745   "Update the secondary (read, process, cache) mark."
10746   (gnus-summary-update-mark
10747    (cond ((memq article gnus-newsgroup-processable)
10748           gnus-process-mark)
10749          ((memq article gnus-newsgroup-cached)
10750           gnus-cached-mark)
10751          ((memq article gnus-newsgroup-replied)
10752           gnus-replied-mark)
10753          ((memq article gnus-newsgroup-forwarded)
10754           gnus-forwarded-mark)
10755          ((memq article gnus-newsgroup-saved)
10756           gnus-saved-mark)
10757          ((memq article gnus-newsgroup-recent)
10758           gnus-recent-mark)
10759          ((memq article gnus-newsgroup-unseen)
10760           gnus-unseen-mark)
10761          (t gnus-no-mark))
10762    'replied)
10763   (when (gnus-visual-p 'summary-highlight 'highlight)
10764     (gnus-summary-highlight-line)
10765     (gnus-run-hooks 'gnus-summary-update-hook))
10766   t)
10767
10768 (defun gnus-summary-update-download-mark (article)
10769   "Update the download mark."
10770   (gnus-summary-update-mark
10771    (cond ((memq article gnus-newsgroup-undownloaded)
10772           gnus-undownloaded-mark)
10773          (gnus-newsgroup-agentized
10774           gnus-downloaded-mark)
10775          (t
10776           gnus-no-mark))
10777    'download)
10778   (gnus-summary-update-line t)
10779   t)
10780
10781 (defun gnus-summary-update-mark (mark type)
10782   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10783         (inhibit-read-only t))
10784     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10785     (when forward
10786       (when (looking-at "\r")
10787         (incf forward))
10788       (when (<= (+ forward (point)) (point-max))
10789         ;; Go to the right position on the line.
10790         (goto-char (+ forward (point)))
10791         ;; Replace the old mark with the new mark.
10792         (let ((to-insert
10793                (mm-subst-char-in-string
10794                 (char-after) mark
10795                 (buffer-substring (point) (1+ (point))))))
10796           (delete-region (point) (1+ (point)))
10797           (insert to-insert))
10798         ;; Optionally update the marks by some user rule.
10799         (when (eq type 'unread)
10800           (gnus-data-set-mark
10801            (gnus-data-find (gnus-summary-article-number)) mark)
10802           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10803
10804 (defun gnus-mark-article-as-read (article &optional mark)
10805   "Enter ARTICLE in the pertinent lists and remove it from others."
10806   ;; Make the article expirable.
10807   (let ((mark (or mark gnus-del-mark)))
10808     (setq gnus-newsgroup-expirable
10809           (if (= mark gnus-expirable-mark)
10810               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10811             (delq article gnus-newsgroup-expirable)))
10812     ;; Remove from unread and marked lists.
10813     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10814     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10815     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10816     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10817     (push (cons article mark) gnus-newsgroup-reads)
10818     ;; Possibly remove from cache, if that is used.
10819     (when gnus-use-cache
10820       (gnus-cache-enter-remove-article article))
10821     t))
10822
10823 (defun gnus-mark-article-as-unread (article &optional mark)
10824   "Enter ARTICLE in the pertinent lists and remove it from others."
10825   (let ((mark (or mark gnus-ticked-mark)))
10826     (if (<= article 0)
10827         (progn
10828           (gnus-error 1 "Can't mark negative article numbers")
10829           nil)
10830       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10831             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10832             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10833             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10834             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10835
10836       ;; Unsuppress duplicates?
10837       (when gnus-suppress-duplicates
10838         (gnus-dup-unsuppress-article article))
10839
10840       (cond ((= mark gnus-ticked-mark)
10841              (setq gnus-newsgroup-marked
10842                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10843             ((= mark gnus-spam-mark)
10844              (setq gnus-newsgroup-spam-marked
10845                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10846                                             article)))
10847             ((= mark gnus-dormant-mark)
10848              (setq gnus-newsgroup-dormant
10849                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10850             (t
10851              (setq gnus-newsgroup-unreads
10852                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10853       (gnus-pull article gnus-newsgroup-reads)
10854       t)))
10855
10856 (defalias 'gnus-summary-mark-as-unread-forward
10857   'gnus-summary-tick-article-forward)
10858 (make-obsolete 'gnus-summary-mark-as-unread-forward
10859                'gnus-summary-tick-article-forward "Emacs 20.4")
10860 (defun gnus-summary-tick-article-forward (n)
10861   "Tick N articles forwards.
10862 If N is negative, tick backwards instead.
10863 The difference between N and the number of articles ticked is returned."
10864   (interactive "p")
10865   (gnus-summary-mark-forward n gnus-ticked-mark))
10866
10867 (defalias 'gnus-summary-mark-as-unread-backward
10868   'gnus-summary-tick-article-backward)
10869 (make-obsolete 'gnus-summary-mark-as-unread-backward
10870                'gnus-summary-tick-article-backward "Emacs 20.4")
10871 (defun gnus-summary-tick-article-backward (n)
10872   "Tick N articles backwards.
10873 The difference between N and the number of articles ticked is returned."
10874   (interactive "p")
10875   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10876
10877 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10878 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10879 (defun gnus-summary-tick-article (&optional article clear-mark)
10880   "Mark current article as unread.
10881 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10882 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10883   (interactive)
10884   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10885                                        gnus-ticked-mark)))
10886
10887 (defun gnus-summary-mark-as-read-forward (n)
10888   "Mark N articles as read forwards.
10889 If N is negative, mark backwards instead.
10890 The difference between N and the actual number of articles marked is
10891 returned."
10892   (interactive "p")
10893   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10894
10895 (defun gnus-summary-mark-as-read-backward (n)
10896   "Mark the N articles as read backwards.
10897 The difference between N and the actual number of articles marked is
10898 returned."
10899   (interactive "p")
10900   (gnus-summary-mark-forward
10901    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10902
10903 (defun gnus-summary-mark-as-read (&optional article mark)
10904   "Mark current article as read.
10905 ARTICLE specifies the article to be marked as read.
10906 MARK specifies a string to be inserted at the beginning of the line."
10907   (gnus-summary-mark-article article mark))
10908
10909 (defun gnus-summary-clear-mark-forward (n)
10910   "Clear marks from N articles forward.
10911 If N is negative, clear backward instead.
10912 The difference between N and the number of marks cleared is returned."
10913   (interactive "p")
10914   (gnus-summary-mark-forward n gnus-unread-mark))
10915
10916 (defun gnus-summary-clear-mark-backward (n)
10917   "Clear marks from N articles backward.
10918 The difference between N and the number of marks cleared is returned."
10919   (interactive "p")
10920   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10921
10922 (defun gnus-summary-mark-unread-as-read ()
10923   "Intended to be used by `gnus-mark-article-hook'."
10924   (when (memq gnus-current-article gnus-newsgroup-unreads)
10925     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10926
10927 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10928   "Intended to be used by `gnus-mark-article-hook'."
10929   (let ((mark (gnus-summary-article-mark)))
10930     (when (or (gnus-unread-mark-p mark)
10931               (gnus-read-mark-p mark))
10932       (gnus-summary-mark-article gnus-current-article
10933                                  (or new-mark gnus-read-mark)))))
10934
10935 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10936   "Intended to be used by `gnus-mark-article-hook'."
10937   (let ((mark (gnus-summary-article-mark)))
10938     (when (or (gnus-unread-mark-p mark)
10939               (gnus-read-mark-p mark))
10940       (gnus-summary-mark-article (gnus-summary-article-number)
10941                                  (or new-mark gnus-read-mark)))))
10942
10943 (defun gnus-summary-mark-unread-as-ticked ()
10944   "Intended to be used by `gnus-mark-article-hook'."
10945   (when (memq gnus-current-article gnus-newsgroup-unreads)
10946     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10947
10948 (defun gnus-summary-mark-region-as-read (point mark all)
10949   "Mark all unread articles between point and mark as read.
10950 If given a prefix, mark all articles between point and mark as read,
10951 even ticked and dormant ones."
10952   (interactive "r\nP")
10953   (save-excursion
10954     (let (article)
10955       (goto-char point)
10956       (beginning-of-line)
10957       (while (and
10958               (< (point) mark)
10959               (progn
10960                 (when (or all
10961                           (memq (setq article (gnus-summary-article-number))
10962                                 gnus-newsgroup-unreads))
10963                   (gnus-summary-mark-article article gnus-del-mark))
10964                 t)
10965               (gnus-summary-find-next))))))
10966
10967 (defun gnus-summary-mark-below (score mark)
10968   "Mark articles with score less than SCORE with MARK."
10969   (interactive "P\ncMark: ")
10970   (setq score (if score
10971                   (prefix-numeric-value score)
10972                 (or gnus-summary-default-score 0)))
10973   (with-current-buffer gnus-summary-buffer
10974     (goto-char (point-min))
10975     (while
10976         (progn
10977           (and (< (gnus-summary-article-score) score)
10978                (gnus-summary-mark-article nil mark))
10979           (gnus-summary-find-next)))))
10980
10981 (defun gnus-summary-kill-below (&optional score)
10982   "Mark articles with score below SCORE as read."
10983   (interactive "P")
10984   (gnus-summary-mark-below score gnus-killed-mark))
10985
10986 (defun gnus-summary-clear-above (&optional score)
10987   "Clear all marks from articles with score above SCORE."
10988   (interactive "P")
10989   (gnus-summary-mark-above score gnus-unread-mark))
10990
10991 (defun gnus-summary-tick-above (&optional score)
10992   "Tick all articles with score above SCORE."
10993   (interactive "P")
10994   (gnus-summary-mark-above score gnus-ticked-mark))
10995
10996 (defun gnus-summary-mark-above (score mark)
10997   "Mark articles with score over SCORE with MARK."
10998   (interactive "P\ncMark: ")
10999   (setq score (if score
11000                   (prefix-numeric-value score)
11001                 (or gnus-summary-default-score 0)))
11002   (with-current-buffer gnus-summary-buffer
11003     (goto-char (point-min))
11004     (while (and (progn
11005                   (when (> (gnus-summary-article-score) score)
11006                     (gnus-summary-mark-article nil mark))
11007                   t)
11008                 (gnus-summary-find-next)))))
11009
11010 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11011 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11012 (defun gnus-summary-limit-include-expunged (&optional no-error)
11013   "Display all the hidden articles that were expunged for low scores."
11014   (interactive)
11015   (let ((inhibit-read-only t))
11016     (let ((scored gnus-newsgroup-scored)
11017           headers h)
11018       (while scored
11019         (unless (gnus-summary-article-header (caar scored))
11020           (and (setq h (gnus-number-to-header (caar scored)))
11021                (< (cdar scored) gnus-summary-expunge-below)
11022                (push h headers)))
11023         (setq scored (cdr scored)))
11024       (if (not headers)
11025           (when (not no-error)
11026             (error "No expunged articles hidden"))
11027         (goto-char (point-min))
11028         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11029         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11030         (dolist (x headers)
11031           (push (mail-header-number x) gnus-newsgroup-limit))
11032         (gnus-summary-prepare-unthreaded (nreverse headers))
11033         (goto-char (point-min))
11034         (gnus-summary-position-point)
11035         t))))
11036
11037 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11038   "Mark all unread articles in this newsgroup as read.
11039 If prefix argument ALL is non-nil, ticked and dormant articles will
11040 also be marked as read.
11041 If QUIETLY is non-nil, no questions will be asked.
11042
11043 If TO-HERE is non-nil, it should be a point in the buffer.  All
11044 articles before (after, if REVERSE is set) this point will be marked
11045 as read.
11046
11047 Note that this function will only catch up the unread article
11048 in the current summary buffer limitation.
11049
11050 The number of articles marked as read is returned."
11051   (interactive "P")
11052   (prog1
11053       (save-excursion
11054         (when (or quietly
11055                   (not gnus-interactive-catchup) ;Without confirmation?
11056                   gnus-expert-user
11057                   (gnus-y-or-n-p
11058                    (if all
11059                        "Mark absolutely all articles as read? "
11060                      "Mark all unread articles as read? ")))
11061           (if (and not-mark
11062                    (not gnus-newsgroup-adaptive)
11063                    (not gnus-newsgroup-auto-expire)
11064                    (not gnus-suppress-duplicates)
11065                    (or (not gnus-use-cache)
11066                        (eq gnus-use-cache 'passive)))
11067               (progn
11068                 (when all
11069                   (setq gnus-newsgroup-marked nil
11070                         gnus-newsgroup-spam-marked nil
11071                         gnus-newsgroup-dormant nil))
11072                 (setq gnus-newsgroup-unreads
11073                       (gnus-sorted-nunion
11074                        (gnus-sorted-intersection gnus-newsgroup-unreads
11075                                                  gnus-newsgroup-downloadable)
11076                        (gnus-sorted-difference gnus-newsgroup-unfetched
11077                                                gnus-newsgroup-cached))))
11078             ;; We actually mark all articles as canceled, which we
11079             ;; have to do when using auto-expiry or adaptive scoring.
11080             (gnus-summary-show-all-threads)
11081             (if (and to-here reverse)
11082                 (progn
11083                   (goto-char to-here)
11084                   (gnus-summary-mark-current-read-and-unread-as-read
11085                    gnus-catchup-mark)
11086                   (while (gnus-summary-find-next (not all))
11087                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11088               (when (gnus-summary-first-subject (not all))
11089                 (while (and
11090                         (if to-here (< (point) to-here) t)
11091                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11092                         (gnus-summary-find-next (not all))))))
11093             (gnus-set-mode-line 'summary))
11094           t))
11095     (gnus-summary-position-point)))
11096
11097 (defun gnus-summary-catchup-to-here (&optional all)
11098   "Mark all unticked articles before the current one as read.
11099 If ALL is non-nil, also mark ticked and dormant articles as read."
11100   (interactive "P")
11101   (save-excursion
11102     (gnus-save-hidden-threads
11103       (let ((beg (point)))
11104         ;; We check that there are unread articles.
11105         (when (or all (gnus-summary-find-prev))
11106           (gnus-summary-catchup all t beg)))))
11107   (gnus-summary-position-point))
11108
11109 (defun gnus-summary-catchup-from-here (&optional all)
11110   "Mark all unticked articles after (and including) the current one as read.
11111 If ALL is non-nil, also mark ticked and dormant articles as read."
11112   (interactive "P")
11113   (save-excursion
11114     (gnus-save-hidden-threads
11115       (let ((beg (point)))
11116         ;; We check that there are unread articles.
11117         (when (or all (gnus-summary-find-next))
11118           (gnus-summary-catchup all t beg nil t)))))
11119   (gnus-summary-position-point))
11120
11121 (defun gnus-summary-catchup-all (&optional quietly)
11122   "Mark all articles in this newsgroup as read.
11123 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11124 instead, which marks only unread articles as read."
11125   (interactive "P")
11126   (gnus-summary-catchup t quietly))
11127
11128 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11129   "Mark all unread articles in this group as read, then exit.
11130 If prefix argument ALL is non-nil, all articles are marked as read.
11131 If QUIETLY is non-nil, no questions will be asked."
11132   (interactive "P")
11133   (when (gnus-summary-catchup all quietly nil 'fast)
11134     ;; Select next newsgroup or exit.
11135     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11136              (eq gnus-auto-select-next 'quietly))
11137         (gnus-summary-next-group nil)
11138       (gnus-summary-exit))))
11139
11140 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11141   "Mark all articles in this newsgroup as read, and then exit.
11142 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11143 instead, which marks only unread articles as read."
11144   (interactive "P")
11145   (gnus-summary-catchup-and-exit t quietly))
11146
11147 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11148   "Mark all articles in this group as read and select the next group.
11149 If given a prefix, mark all articles, unread as well as ticked, as
11150 read."
11151   (interactive "P")
11152   (save-excursion
11153     (gnus-summary-catchup all))
11154   (gnus-summary-next-group))
11155
11156 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11157   "Mark all articles in this group as read and select the previous group.
11158 If given a prefix, mark all articles, unread as well as ticked, as
11159 read."
11160   (interactive "P")
11161   (save-excursion
11162     (gnus-summary-catchup all))
11163   (gnus-summary-next-group nil nil t))
11164
11165 ;;;
11166 ;;; with article
11167 ;;;
11168
11169 (defmacro gnus-with-article (article &rest forms)
11170   "Select ARTICLE and perform FORMS in the original article buffer.
11171 Then replace the article with the result."
11172   `(progn
11173      ;; We don't want the article to be marked as read.
11174      (let (gnus-mark-article-hook)
11175        (gnus-summary-select-article t t nil ,article))
11176      (set-buffer gnus-original-article-buffer)
11177      ,@forms
11178      (if (not (gnus-check-backend-function
11179                'request-replace-article (car gnus-article-current)))
11180          (gnus-message 5 "Read-only group; not replacing")
11181        (unless (gnus-request-replace-article
11182                 ,article (car gnus-article-current)
11183                 (current-buffer) t)
11184          (error "Couldn't replace article")))
11185      ;; The cache and backlog have to be flushed somewhat.
11186      (when gnus-keep-backlog
11187        (gnus-backlog-remove-article
11188         (car gnus-article-current) (cdr gnus-article-current)))
11189      (when gnus-use-cache
11190        (gnus-cache-update-article
11191         (car gnus-article-current) (cdr gnus-article-current)))))
11192
11193 (put 'gnus-with-article 'lisp-indent-function 1)
11194 (put 'gnus-with-article 'edebug-form-spec '(form body))
11195
11196 ;; Thread-based commands.
11197
11198 (defun gnus-summary-articles-in-thread (&optional article)
11199   "Return a list of all articles in the current thread.
11200 If ARTICLE is non-nil, return all articles in the thread that starts
11201 with that article."
11202   (let* ((article (or article (gnus-summary-article-number)))
11203          (data (gnus-data-find-list article))
11204          (top-level (gnus-data-level (car data)))
11205          (top-subject
11206           (cond ((null gnus-thread-operation-ignore-subject)
11207                  (gnus-simplify-subject-re
11208                   (mail-header-subject (gnus-data-header (car data)))))
11209                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11210                  (gnus-simplify-subject-fuzzy
11211                   (mail-header-subject (gnus-data-header (car data)))))
11212                 (t nil)))
11213          (end-point (save-excursion
11214                       (if (gnus-summary-go-to-next-thread)
11215                           (point) (point-max))))
11216          articles)
11217     (while (and data
11218                 (< (gnus-data-pos (car data)) end-point))
11219       (when (or (not top-subject)
11220                 (string= top-subject
11221                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11222                              (gnus-simplify-subject-fuzzy
11223                               (mail-header-subject
11224                                (gnus-data-header (car data))))
11225                            (gnus-simplify-subject-re
11226                             (mail-header-subject
11227                              (gnus-data-header (car data)))))))
11228         (push (gnus-data-number (car data)) articles))
11229       (unless (and (setq data (cdr data))
11230                    (> (gnus-data-level (car data)) top-level))
11231         (setq data nil)))
11232     ;; Return the list of articles.
11233     (nreverse articles)))
11234
11235 (defun gnus-summary-rethread-current ()
11236   "Rethread the thread the current article is part of."
11237   (interactive)
11238   (let* ((gnus-show-threads t)
11239          (article (gnus-summary-article-number))
11240          (id (mail-header-id (gnus-summary-article-header)))
11241          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11242     (unless id
11243       (error "No article on the current line"))
11244     (gnus-rebuild-thread id)
11245     (gnus-summary-goto-subject article)))
11246
11247 (defun gnus-summary-reparent-thread ()
11248   "Make the current article child of the marked (or previous) article.
11249
11250 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11251 is non-nil or the Subject: of both articles are the same."
11252   (interactive)
11253   (unless (not (gnus-group-read-only-p))
11254     (error "The current newsgroup does not support article editing"))
11255   (unless (<= (length gnus-newsgroup-processable) 1)
11256     (error "No more than one article may be marked"))
11257   (let ((child (gnus-summary-article-number))
11258         ;; First grab the marked article, otherwise one line up.
11259         (parent (if (not (null gnus-newsgroup-processable))
11260                     (car gnus-newsgroup-processable)
11261                   (save-excursion
11262                     (if (eq (forward-line -1) 0)
11263                         (gnus-summary-article-number)
11264                       (error "Beginning of summary buffer"))))))
11265     (gnus-summary-reparent-children parent (list child))))
11266
11267 (defun gnus-summary-reparent-children (parent children)
11268   "Make PARENT the parent of CHILDREN.
11269 When called interactively, PARENT is the current article and CHILDREN
11270 are the process-marked articles."
11271   (interactive
11272    (list (gnus-summary-article-number)
11273          (gnus-summary-work-articles nil)))
11274   (dolist (child children)
11275     (save-window-excursion
11276       (let ((gnus-article-buffer " *reparent*"))
11277         (unless (not (eq parent child))
11278           (error "An article may not be self-referential"))
11279         (let ((message-id (mail-header-id
11280                            (gnus-summary-article-header parent))))
11281           (unless (and message-id (not (equal message-id "")))
11282             (error "No message-id in desired parent"))
11283           (gnus-with-article child
11284             (save-restriction
11285               (goto-char (point-min))
11286               (message-narrow-to-head)
11287               (if (re-search-forward "^References: " nil t)
11288                   (progn
11289                     (re-search-forward "^[^ \t]" nil t)
11290                     (forward-line -1)
11291                     (end-of-line)
11292                     (insert " " message-id))
11293                 (insert "References: " message-id "\n"))))
11294           (set-buffer gnus-summary-buffer)
11295           (gnus-summary-unmark-all-processable)
11296           (gnus-summary-update-article child)
11297           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11298             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11299           (gnus-summary-rethread-current)
11300           (gnus-message 3 "Article %d is now the child of article %d"
11301                         child parent))))))
11302
11303 (defun gnus-summary-toggle-threads (&optional arg)
11304   "Toggle showing conversation threads.
11305 If ARG is positive number, turn showing conversation threads on."
11306   (interactive "P")
11307   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11308     (setq gnus-show-threads
11309           (if (null arg) (not gnus-show-threads)
11310             (> (prefix-numeric-value arg) 0)))
11311     (gnus-summary-prepare)
11312     (gnus-summary-goto-subject current)
11313     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11314     (gnus-summary-position-point)))
11315
11316 (eval-and-compile
11317   (if (fboundp 'remove-overlays)
11318       (defalias 'gnus-remove-overlays 'remove-overlays)
11319     (defun gnus-remove-overlays (beg end name val)
11320       "Clear BEG and END of overlays whose property NAME has value VAL.
11321 For compatibility with XEmacs."
11322       (dolist (ov (gnus-overlays-in beg end))
11323         (when (eq (gnus-overlay-get ov name) val)
11324           (gnus-delete-overlay ov))))))
11325
11326 (defun gnus-summary-show-all-threads ()
11327   "Show all threads."
11328   (interactive)
11329   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11330   (gnus-summary-position-point))
11331
11332 (defsubst gnus-summary--inv (p)
11333   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11334
11335 (defun gnus-summary-show-thread ()
11336   "Show thread subtrees.
11337 Returns nil if no thread was there to be shown."
11338   (interactive)
11339   (let* ((orig (point))
11340          (end (point-at-eol))
11341          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11342          ;; Leave point at bol
11343          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11344          (eoi (when end
11345                 (if (fboundp 'next-single-char-property-change)
11346                     (or (next-single-char-property-change end 'invisible)
11347                         (point-max))
11348                   (while (progn
11349                            (end-of-line 2)
11350                            (and (not (eobp))
11351                                 (eq (get-char-property (point) 'invisible)
11352                                     'gnus-sum))))
11353                   (point)))))
11354     (when eoi
11355       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11356       (goto-char orig)
11357       (gnus-summary-position-point)
11358       eoi)))
11359
11360 (defun gnus-summary-maybe-hide-threads ()
11361   "If requested, hide the threads that should be hidden."
11362   (when (and gnus-show-threads
11363              gnus-thread-hide-subtree)
11364     (gnus-summary-hide-all-threads
11365      (if (or (consp gnus-thread-hide-subtree)
11366              (functionp gnus-thread-hide-subtree))
11367          (gnus-make-predicate gnus-thread-hide-subtree)
11368        nil))))
11369
11370 ;;; Hiding predicates.
11371
11372 (defun gnus-article-unread-p (header)
11373   (memq (mail-header-number header) gnus-newsgroup-unreads))
11374
11375 (defun gnus-article-unseen-p (header)
11376   (memq (mail-header-number header) gnus-newsgroup-unseen))
11377
11378 (defun gnus-map-articles (predicate articles)
11379   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11380   (apply 'gnus-or (mapcar predicate
11381                           (mapcar (lambda (number)
11382                                     (gnus-summary-article-header number))
11383                                   articles))))
11384
11385 (defun gnus-summary-hide-all-threads (&optional predicate)
11386   "Hide all thread subtrees.
11387 If PREDICATE is supplied, threads that satisfy this predicate
11388 will not be hidden."
11389   (interactive)
11390   (save-excursion
11391     (goto-char (point-min))
11392     (let ((end nil))
11393       (while (not end)
11394         (when (or (not predicate)
11395                   (gnus-map-articles
11396                    predicate (gnus-summary-article-children)))
11397             (gnus-summary-hide-thread))
11398         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11399   (gnus-summary-position-point))
11400
11401 (defun gnus-summary-hide-thread ()
11402   "Hide thread subtrees.
11403 If PREDICATE is supplied, threads that satisfy this predicate
11404 will not be hidden.
11405 Returns nil if no threads were there to be hidden."
11406   (interactive)
11407   (let ((start (point))
11408         (starteol (line-end-position))
11409         (article (gnus-summary-article-number)))
11410     (goto-char start)
11411     ;; Go forward until either the buffer ends or the subthread ends.
11412     (when (and (not (eobp))
11413                (or (zerop (gnus-summary-next-thread 1 t))
11414                    (goto-char (point-max))))
11415       (if (and (> (point) start)
11416                ;; FIXME: this should actually search for a non-invisible \n.
11417                (search-backward "\n" start t))
11418           (progn
11419             (when (> (point) starteol)
11420               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11421               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11422                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11423                 (gnus-overlay-put ol 'evaporate t)))
11424             (gnus-summary-goto-subject article))
11425         (goto-char start)
11426         nil))))
11427
11428 (defun gnus-summary-go-to-next-thread (&optional previous)
11429   "Go to the same level (or less) next thread.
11430 If PREVIOUS is non-nil, go to previous thread instead.
11431 Return the article number moved to, or nil if moving was impossible."
11432   (let ((level (gnus-summary-thread-level))
11433         (way (if previous -1 1))
11434         (beg (point)))
11435     (forward-line way)
11436     (while (and (not (eobp))
11437                 (< level (gnus-summary-thread-level)))
11438       (forward-line way))
11439     (if (eobp)
11440         (progn
11441           (goto-char beg)
11442           nil)
11443       (setq beg (point))
11444       (prog1
11445           (gnus-summary-article-number)
11446         (goto-char beg)))))
11447
11448 (defun gnus-summary-next-thread (n &optional silent)
11449   "Go to the same level next N'th thread.
11450 If N is negative, search backward instead.
11451 Returns the difference between N and the number of skips actually
11452 done.
11453
11454 If SILENT, don't output messages."
11455   (interactive "p")
11456   (let ((backward (< n 0))
11457         (n (abs n)))
11458     (while (and (> n 0)
11459                 (gnus-summary-go-to-next-thread backward))
11460       (decf n))
11461     (unless silent
11462       (gnus-summary-position-point))
11463     (when (and (not silent) (/= 0 n))
11464       (gnus-message 7 "No more threads"))
11465     n))
11466
11467 (defun gnus-summary-prev-thread (n)
11468   "Go to the same level previous N'th thread.
11469 Returns the difference between N and the number of skips actually
11470 done."
11471   (interactive "p")
11472   (gnus-summary-next-thread (- n)))
11473
11474 (defun gnus-summary-go-down-thread ()
11475   "Go down one level in the current thread."
11476   (let ((children (gnus-summary-article-children)))
11477     (when children
11478       (gnus-summary-goto-subject (car children)))))
11479
11480 (defun gnus-summary-go-up-thread ()
11481   "Go up one level in the current thread."
11482   (let ((parent (gnus-summary-article-parent)))
11483     (when parent
11484       (gnus-summary-goto-subject parent))))
11485
11486 (defun gnus-summary-down-thread (n)
11487   "Go down thread N steps.
11488 If N is negative, go up instead.
11489 Returns the difference between N and how many steps down that were
11490 taken."
11491   (interactive "p")
11492   (let ((up (< n 0))
11493         (n (abs n)))
11494     (while (and (> n 0)
11495                 (if up (gnus-summary-go-up-thread)
11496                   (gnus-summary-go-down-thread)))
11497       (setq n (1- n)))
11498     (gnus-summary-position-point)
11499     (when (/= 0 n)
11500       (gnus-message 7 "Can't go further"))
11501     n))
11502
11503 (defun gnus-summary-up-thread (n)
11504   "Go up thread N steps.
11505 If N is negative, go down instead.
11506 Returns the difference between N and how many steps down that were
11507 taken."
11508   (interactive "p")
11509   (gnus-summary-down-thread (- n)))
11510
11511 (defun gnus-summary-top-thread ()
11512   "Go to the top of the thread."
11513   (interactive)
11514   (while (gnus-summary-go-up-thread))
11515   (gnus-summary-article-number))
11516
11517 (defun gnus-summary-expire-thread ()
11518   "Mark articles under current thread as expired."
11519   (interactive)
11520   (gnus-summary-kill-thread 0))
11521
11522 (defun gnus-summary-kill-thread (&optional unmark)
11523   "Mark articles under current thread as read.
11524 If the prefix argument is positive, remove any kinds of marks.
11525 If the prefix argument is zero, mark thread as expired.
11526 If the prefix argument is negative, tick articles instead."
11527   (interactive "P")
11528   (when unmark
11529     (setq unmark (prefix-numeric-value unmark)))
11530   (let ((articles (gnus-summary-articles-in-thread))
11531         (hide (or (null unmark) (= unmark 0))))
11532     (save-excursion
11533       ;; Expand the thread.
11534       (gnus-summary-show-thread)
11535       ;; Mark all the articles.
11536       (while articles
11537         (gnus-summary-goto-subject (car articles))
11538         (cond ((null unmark)
11539                (gnus-summary-mark-article-as-read gnus-killed-mark))
11540               ((> unmark 0)
11541                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11542               ((= unmark 0)
11543                (gnus-summary-mark-article nil gnus-expirable-mark))
11544               (t
11545                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11546         (setq articles (cdr articles))))
11547     ;; Hide killed subtrees when hide is true.
11548     (and hide
11549          gnus-thread-hide-killed
11550          (gnus-summary-hide-thread))
11551     ;; If hide is t, go to next unread subject.
11552     (when hide
11553       ;; Go to next unread subject.
11554       (gnus-summary-next-subject 1 t)))
11555   (gnus-set-mode-line 'summary))
11556
11557 ;; Summary sorting commands
11558
11559 (defun gnus-summary-sort-by-number (&optional reverse)
11560   "Sort the summary buffer by article number.
11561 Argument REVERSE means reverse order."
11562   (interactive "P")
11563   (gnus-summary-sort 'number reverse))
11564
11565 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11566   "Sort the summary buffer by most recent article number.
11567 Argument REVERSE means reverse order."
11568   (interactive "P")
11569   (gnus-summary-sort 'most-recent-number reverse))
11570
11571 (defun gnus-summary-sort-by-random (&optional reverse)
11572   "Randomize the order in the summary buffer.
11573 Argument REVERSE means to randomize in reverse order."
11574   (interactive "P")
11575   (gnus-summary-sort 'random reverse))
11576
11577 (defun gnus-summary-sort-by-author (&optional reverse)
11578   "Sort the summary buffer by author name alphabetically.
11579 If `case-fold-search' is non-nil, case of letters is ignored.
11580 Argument REVERSE means reverse order."
11581   (interactive "P")
11582   (gnus-summary-sort 'author reverse))
11583
11584 (defun gnus-summary-sort-by-recipient (&optional reverse)
11585   "Sort the summary buffer by recipient name alphabetically.
11586 If `case-fold-search' is non-nil, case of letters is ignored.
11587 Argument REVERSE means reverse order."
11588   (interactive "P")
11589   (gnus-summary-sort 'recipient reverse))
11590
11591 (defun gnus-summary-sort-by-subject (&optional reverse)
11592   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11593 If `case-fold-search' is non-nil, case of letters is ignored.
11594 Argument REVERSE means reverse order."
11595   (interactive "P")
11596   (gnus-summary-sort 'subject reverse))
11597
11598 (defun gnus-summary-sort-by-date (&optional reverse)
11599   "Sort the summary buffer by date.
11600 Argument REVERSE means reverse order."
11601   (interactive "P")
11602   (gnus-summary-sort 'date reverse))
11603
11604 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11605   "Sort the summary buffer by most recent date.
11606 Argument REVERSE means reverse order."
11607   (interactive "P")
11608   (gnus-summary-sort 'most-recent-date reverse))
11609
11610 (defun gnus-summary-sort-by-score (&optional reverse)
11611   "Sort the summary buffer by score.
11612 Argument REVERSE means reverse order."
11613   (interactive "P")
11614   (gnus-summary-sort 'score reverse))
11615
11616 (defun gnus-summary-sort-by-lines (&optional reverse)
11617   "Sort the summary buffer by the number of lines.
11618 Argument REVERSE means reverse order."
11619   (interactive "P")
11620   (gnus-summary-sort 'lines reverse))
11621
11622 (defun gnus-summary-sort-by-chars (&optional reverse)
11623   "Sort the summary buffer by article length.
11624 Argument REVERSE means reverse order."
11625   (interactive "P")
11626   (gnus-summary-sort 'chars reverse))
11627
11628 (defun gnus-summary-sort-by-original (&optional reverse)
11629   "Sort the summary buffer using the default sorting method.
11630 Argument REVERSE means reverse order."
11631   (interactive "P")
11632   (let* ((inhibit-read-only t)
11633          (gnus-summary-prepare-hook nil))
11634     ;; We do the sorting by regenerating the threads.
11635     (gnus-summary-prepare)
11636     ;; Hide subthreads if needed.
11637     (gnus-summary-maybe-hide-threads)))
11638
11639 (defun gnus-summary-sort (predicate reverse)
11640   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11641   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11642          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11643          (gnus-thread-sort-functions
11644           (if (not reverse)
11645               thread
11646             `(lambda (t1 t2)
11647                (,thread t2 t1))))
11648          (gnus-sort-gathered-threads-function
11649           gnus-thread-sort-functions)
11650          (gnus-article-sort-functions
11651           (if (not reverse)
11652               article
11653             `(lambda (t1 t2)
11654                (,article t2 t1))))
11655          (inhibit-read-only t)
11656          (gnus-summary-prepare-hook nil))
11657     ;; We do the sorting by regenerating the threads.
11658     (gnus-summary-prepare)
11659     ;; Hide subthreads if needed.
11660     (gnus-summary-maybe-hide-threads)))
11661
11662 ;; Summary saving commands.
11663
11664 (defun gnus-summary-save-article (&optional n not-saved)
11665   "Save the current article using the default saver function.
11666 If N is a positive number, save the N next articles.
11667 If N is a negative number, save the N previous articles.
11668 If N is nil and any articles have been marked with the process mark,
11669 save those articles instead.
11670 The variable `gnus-default-article-saver' specifies the saver function.
11671
11672 If the optional second argument NOT-SAVED is non-nil, articles saved
11673 will not be marked as saved."
11674   (interactive "P")
11675   (require 'gnus-art)
11676   (let* ((articles (gnus-summary-work-articles n))
11677          (save-buffer (save-excursion
11678                         (nnheader-set-temp-buffer " *Gnus Save*")))
11679          (num (length articles))
11680          ;; Whether to save decoded articles or raw articles.
11681          (decode (when gnus-article-save-coding-system
11682                    (get gnus-default-article-saver :decode)))
11683          ;; When saving many articles in a single file, use the other
11684          ;; function to save articles other than the first one.
11685          (saver2 (get gnus-default-article-saver :function))
11686          (gnus-prompt-before-saving (if saver2
11687                                         t
11688                                       gnus-prompt-before-saving))
11689          (gnus-default-article-saver gnus-default-article-saver)
11690          header file)
11691     (dolist (article articles)
11692       (setq header (gnus-summary-article-header article))
11693       (if (not (vectorp header))
11694           ;; This is a pseudo-article.
11695           (if (assq 'name header)
11696               (gnus-copy-file (cdr (assq 'name header)))
11697             (gnus-message 1 "Article %d is unsaveable" article))
11698         ;; This is a real article.
11699         (save-window-excursion
11700           (gnus-summary-select-article decode decode nil article)
11701           (gnus-summary-goto-subject article))
11702         (with-current-buffer save-buffer
11703           (erase-buffer)
11704           (insert-buffer-substring (if decode
11705                                        gnus-article-buffer
11706                                      gnus-original-article-buffer)))
11707         (setq file (gnus-article-save save-buffer file num))
11708         (gnus-summary-remove-process-mark article)
11709         (unless not-saved
11710           (gnus-summary-set-saved-mark article)))
11711       (when saver2
11712         (setq gnus-default-article-saver saver2
11713               saver2 nil)))
11714     (gnus-kill-buffer save-buffer)
11715     (gnus-summary-position-point)
11716     (gnus-set-mode-line 'summary)
11717     n))
11718
11719 (defun gnus-summary-pipe-output (&optional n sym)
11720   "Pipe the current article to a subprocess.
11721 If N is a positive number, pipe the N next articles.
11722 If N is a negative number, pipe the N previous articles.
11723 If N is nil and any articles have been marked with the process mark,
11724 pipe those articles instead.
11725 The default command to which articles are piped is specified by the
11726 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11727 will be prompted for the command.
11728
11729 The properties `:decode' and `:headers' that are put to the function
11730 symbol `gnus-summary-save-in-pipe' control whether this function
11731 decodes articles and what headers to keep (see the doc string for the
11732 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11733 is neither omitted nor the symbol `r', force including all headers
11734 regardless of the `:headers' property.  If it is the symbol `r',
11735 articles that are not decoded and include all headers will be piped
11736 no matter what the properties `:decode' and `:headers' are."
11737   (interactive (gnus-interactive "P\ny"))
11738   (require 'gnus-art)
11739   (let* ((articles (gnus-summary-work-articles n))
11740          (result-buffer "*Shell Command Output*")
11741          (all-headers (not (memq sym '(nil r))))
11742          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11743          (raw (eq sym 'r))
11744          (headers (get 'gnus-summary-save-in-pipe :headers))
11745          command result)
11746     (unless (numberp (car articles))
11747       (error "No article to pipe"))
11748     (setq command (gnus-read-shell-command
11749                    (concat "Shell command on "
11750                            (if (cdr articles)
11751                                (format "these %d articles" (length articles))
11752                              "this article")
11753                            ": ")
11754                    gnus-summary-pipe-output-default-command))
11755     (when (string-equal command "")
11756       (error "A command is required"))
11757     (when all-headers
11758       (put 'gnus-summary-save-in-pipe :headers nil))
11759     (unwind-protect
11760         (while articles
11761           (gnus-summary-goto-subject (pop articles))
11762           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11763           (when (and (get-buffer result-buffer)
11764                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11765             (setq result (concat result (with-current-buffer result-buffer
11766                                           (buffer-string))))))
11767       (put 'gnus-summary-save-in-pipe :headers headers))
11768     (unless (zerop (length result))
11769       (if (with-current-buffer (get-buffer-create result-buffer)
11770             (erase-buffer)
11771             (insert result)
11772             (prog1
11773                 (and (= (count-lines (point-min) (point)) 1)
11774                      (progn
11775                        (end-of-line 0)
11776                        (<= (current-column)
11777                            (window-width (minibuffer-window)))))
11778               (goto-char (point-min))))
11779           (message "%s" (substring result 0 -1))
11780         (message nil)
11781         (gnus-configure-windows 'pipe)))))
11782
11783 (defun gnus-summary-save-article-mail (&optional arg)
11784   "Append the current article to a Unix mail box file.
11785 If N is a positive number, save the N next articles.
11786 If N is a negative number, save the N previous articles.
11787 If N is nil and any articles have been marked with the process mark,
11788 save those articles instead."
11789   (interactive "P")
11790   (require 'gnus-art)
11791   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11792     (gnus-summary-save-article arg)))
11793
11794 (defun gnus-summary-save-article-rmail (&optional arg)
11795   "Append the current article to an rmail file.
11796 If N is a positive number, save the N next articles.
11797 If N is a negative number, save the N previous articles.
11798 If N is nil and any articles have been marked with the process mark,
11799 save those articles instead."
11800   (interactive "P")
11801   (require 'gnus-art)
11802   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11803     (gnus-summary-save-article arg)))
11804
11805 (defun gnus-summary-save-article-file (&optional arg)
11806   "Append the current article to a file.
11807 If N is a positive number, save the N next articles.
11808 If N is a negative number, save the N previous articles.
11809 If N is nil and any articles have been marked with the process mark,
11810 save those articles instead."
11811   (interactive "P")
11812   (require 'gnus-art)
11813   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11814     (gnus-summary-save-article arg)))
11815
11816 (defun gnus-summary-write-article-file (&optional arg)
11817   "Write the current article to a file, deleting the previous file.
11818 If N is a positive number, save the N next articles.
11819 If N is a negative number, save the N previous articles.
11820 If N is nil and any articles have been marked with the process mark,
11821 save those articles instead."
11822   (interactive "P")
11823   (require 'gnus-art)
11824   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11825     (gnus-summary-save-article arg)))
11826
11827 (defun gnus-summary-save-article-body-file (&optional arg)
11828   "Append the current article body to a file.
11829 If N is a positive number, save the N next articles.
11830 If N is a negative number, save the N previous articles.
11831 If N is nil and any articles have been marked with the process mark,
11832 save those articles instead."
11833   (interactive "P")
11834   (require 'gnus-art)
11835   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11836     (gnus-summary-save-article arg)))
11837
11838 (defun gnus-summary-write-article-body-file (&optional arg)
11839   "Write the current article body to a file, deleting the previous file.
11840 If N is a positive number, save the N next articles.
11841 If N is a negative number, save the N previous articles.
11842 If N is nil and any articles have been marked with the process mark,
11843 save those articles instead."
11844   (interactive "P")
11845   (require 'gnus-art)
11846   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11847     (gnus-summary-save-article arg)))
11848
11849 (defun gnus-summary-muttprint (&optional arg)
11850   "Print the current article using Muttprint.
11851 If N is a positive number, save the N next articles.
11852 If N is a negative number, save the N previous articles.
11853 If N is nil and any articles have been marked with the process mark,
11854 save those articles instead."
11855   (interactive "P")
11856   (require 'gnus-art)
11857   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11858     (gnus-summary-save-article arg t)))
11859
11860 (defun gnus-summary-pipe-message (program)
11861   "Pipe the current article through PROGRAM."
11862   (interactive "sProgram: ")
11863   (gnus-summary-select-article)
11864   (let ((mail-header-separator ""))
11865     (gnus-eval-in-buffer-window gnus-article-buffer
11866       (save-restriction
11867         (widen)
11868         (let ((start (window-start))
11869               (inhibit-read-only t))
11870           (message-pipe-buffer-body program)
11871           (set-window-start (get-buffer-window (current-buffer)) start))))))
11872
11873 (defun gnus-get-split-value (methods)
11874   "Return a value based on the split METHODS."
11875   (let (split-name method result match)
11876     (when methods
11877       (with-current-buffer gnus-original-article-buffer
11878         (save-restriction
11879           (nnheader-narrow-to-headers)
11880           (while (and methods (not split-name))
11881             (goto-char (point-min))
11882             (setq method (pop methods))
11883             (setq match (car method))
11884             (when (cond
11885                    ((stringp match)
11886                     ;; Regular expression.
11887                     (ignore-errors
11888                       (re-search-forward match nil t)))
11889                    ((functionp match)
11890                     ;; Function.
11891                     (save-restriction
11892                       (widen)
11893                       (setq result (funcall match gnus-newsgroup-name))))
11894                    ((consp match)
11895                     ;; Form.
11896                     (save-restriction
11897                       (widen)
11898                       (setq result (eval match)))))
11899               (setq split-name (cdr method))
11900               (cond ((stringp result)
11901                      (push (expand-file-name
11902                             result gnus-article-save-directory)
11903                            split-name))
11904                     ((consp result)
11905                      (setq split-name (append result split-name)))))))))
11906     (nreverse split-name)))
11907
11908 (defun gnus-valid-move-group-p (group)
11909   (and (symbolp group)
11910        (boundp group)
11911        (symbol-name group)
11912        (symbol-value group)
11913        (gnus-get-function (gnus-find-method-for-group
11914                            (symbol-name group)) 'request-accept-article t)))
11915
11916 (defun gnus-read-move-group-name (prompt default articles prefix)
11917   "Read a group name."
11918   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11919          (minibuffer-confirm-incomplete nil) ; XEmacs
11920          (prom
11921           (format "%s %s to"
11922                   prompt
11923                   (if (> (length articles) 1)
11924                       (format "these %d articles" (length articles))
11925                     "this article")))
11926          (to-newsgroup
11927           (cond
11928            ((null split-name)
11929             (gnus-group-completing-read
11930              prom
11931              (remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb)
11932              nil prefix nil default))
11933            ((= 1 (length split-name))
11934             (gnus-group-completing-read
11935              prom (remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb)
11936              nil prefix 'gnus-group-history (car split-name)))
11937            (t
11938             (gnus-completing-read
11939              prom (nreverse split-name) nil nil 'gnus-group-history))))
11940          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11941          encoded)
11942     (when to-newsgroup
11943       (if (or (string= to-newsgroup "")
11944               (string= to-newsgroup prefix))
11945           (setq to-newsgroup default))
11946       (unless to-newsgroup
11947         (error "No group name entered"))
11948       (setq encoded (mm-encode-coding-string
11949                      to-newsgroup
11950                      (gnus-group-name-charset to-method to-newsgroup)))
11951       (or (gnus-active encoded)
11952           (gnus-activate-group encoded nil nil to-method)
11953           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11954                                      to-newsgroup))
11955               (or (and (gnus-request-create-group encoded to-method)
11956                        (gnus-activate-group encoded nil nil to-method)
11957                        (gnus-subscribe-group encoded))
11958                   (error "Couldn't create group %s" to-newsgroup)))
11959           (error "No such group: %s" to-newsgroup))
11960       encoded)))
11961
11962 (defvar gnus-summary-save-parts-counter)
11963 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11964
11965 (defun gnus-summary-save-parts (type dir n &optional reverse)
11966   "Save parts matching TYPE to DIR.
11967 If REVERSE, save parts that do not match TYPE."
11968   (interactive
11969    (list (read-string "Save parts of type: "
11970                       (or (car gnus-summary-save-parts-type-history)
11971                           gnus-summary-save-parts-default-mime)
11972                       'gnus-summary-save-parts-type-history)
11973          (setq gnus-summary-save-parts-last-directory
11974                (read-file-name "Save to directory: "
11975                                gnus-summary-save-parts-last-directory
11976                                nil t))
11977          current-prefix-arg))
11978   (gnus-summary-iterate n
11979     (let ((gnus-display-mime-function nil)
11980           gnus-article-prepare-hook
11981           gnus-article-decode-hook
11982           gnus-display-mime-function
11983           gnus-break-pages
11984           (gnus-inhibit-treatment t))
11985       (gnus-summary-select-article))
11986     (with-current-buffer gnus-article-buffer
11987       (let ((handles (or gnus-article-mime-handles
11988                          (mm-dissect-buffer nil gnus-article-loose-mime)
11989                          (and gnus-article-emulate-mime
11990                               (mm-uu-dissect))))
11991             (gnus-summary-save-parts-counter 1))
11992         (when handles
11993           (gnus-summary-save-parts-1 type dir handles reverse)
11994           (unless gnus-article-mime-handles ;; Don't destroy this case.
11995             (mm-destroy-parts handles)))))))
11996
11997 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11998   (if (stringp (car handle))
11999       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12000               (cdr handle))
12001     (when (if reverse
12002               (not (string-match type (mm-handle-media-type handle)))
12003             (string-match type (mm-handle-media-type handle)))
12004       (let ((file (expand-file-name
12005                    (gnus-map-function
12006                     mm-file-name-rewrite-functions
12007                     (file-name-nondirectory
12008                      (or
12009                       (mail-content-type-get
12010                        (mm-handle-disposition handle) 'filename)
12011                       (mail-content-type-get
12012                        (mm-handle-type handle) 'name)
12013                       (format "%s.%d.%d" gnus-newsgroup-name
12014                               (cdr gnus-article-current)
12015                               gnus-summary-save-parts-counter))))
12016                    dir)))
12017         (incf gnus-summary-save-parts-counter)
12018         (unless (file-exists-p file)
12019           (mm-save-part-to-file handle file))))))
12020
12021 ;; Summary extract commands
12022
12023 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12024   (let ((inhibit-read-only t)
12025         (article (gnus-summary-article-number))
12026         after-article b e)
12027     (unless (gnus-summary-goto-subject article)
12028       (error "No such article: %d" article))
12029     (gnus-summary-position-point)
12030     ;; If all commands are to be bunched up on one line, we collect
12031     ;; them here.
12032     (unless gnus-view-pseudos-separately
12033       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12034             files action)
12035         (while ps
12036           (setq action (cdr (assq 'action (car ps))))
12037           (setq files (list (cdr (assq 'name (car ps)))))
12038           (while (and ps (cdr ps)
12039                       (string= (or action "1")
12040                                (or (cdr (assq 'action (cadr ps))) "2")))
12041             (push (cdr (assq 'name (cadr ps))) files)
12042             (setcdr ps (cddr ps)))
12043           (when files
12044             (when (not (string-match "%s" action))
12045               (push " " files))
12046             (push " " files)
12047             (when (assq 'execute (car ps))
12048               (setcdr (assq 'execute (car ps))
12049                       (funcall (if (string-match "%s" action)
12050                                    'format 'concat)
12051                                action
12052                                (mapconcat
12053                                 (lambda (f)
12054                                   (if (equal f " ")
12055                                       f
12056                                     (shell-quote-argument f)))
12057                                 files " ")))))
12058           (setq ps (cdr ps)))))
12059     (if (and gnus-view-pseudos (not not-view))
12060         (while pslist
12061           (when (assq 'execute (car pslist))
12062             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12063                                   (eq gnus-view-pseudos 'not-confirm)))
12064           (setq pslist (cdr pslist)))
12065       (save-excursion
12066         (while pslist
12067           (setq after-article (or (cdr (assq 'article (car pslist)))
12068                                   (gnus-summary-article-number)))
12069           (gnus-summary-goto-subject after-article)
12070           (forward-line 1)
12071           (setq b (point))
12072           (insert "    " (file-name-nondirectory
12073                           (cdr (assq 'name (car pslist))))
12074                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12075           (setq e (point))
12076           (forward-line -1)             ; back to `b'
12077           (gnus-add-text-properties
12078            b (1- e) (list 'gnus-number gnus-reffed-article-number
12079                           gnus-mouse-face-prop gnus-mouse-face))
12080           (gnus-data-enter
12081            after-article gnus-reffed-article-number
12082            gnus-unread-mark b (car pslist) 0 (- e b))
12083           (setq gnus-newsgroup-unreads
12084                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12085                                          gnus-reffed-article-number))
12086           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12087           (setq pslist (cdr pslist)))))))
12088
12089 (defun gnus-pseudos< (p1 p2)
12090   (let ((c1 (cdr (assq 'action p1)))
12091         (c2 (cdr (assq 'action p2))))
12092     (and c1 c2 (string< c1 c2))))
12093
12094 (defun gnus-request-pseudo-article (props)
12095   (cond ((assq 'execute props)
12096          (gnus-execute-command (cdr (assq 'execute props)))))
12097   (let ((gnus-current-article (gnus-summary-article-number)))
12098     (gnus-run-hooks 'gnus-mark-article-hook)))
12099
12100 (defun gnus-execute-command (command &optional automatic)
12101   (save-excursion
12102     (gnus-article-setup-buffer)
12103     (set-buffer gnus-article-buffer)
12104     (setq buffer-read-only nil)
12105     (let ((command (if automatic command
12106                      (read-string "Command: " (cons command 0)))))
12107       (erase-buffer)
12108       (insert "$ " command "\n\n")
12109       (if gnus-view-pseudo-asynchronously
12110           (start-process "gnus-execute" (current-buffer) shell-file-name
12111                          shell-command-switch command)
12112         (call-process shell-file-name nil t nil
12113                       shell-command-switch command)))))
12114
12115 ;; Summary kill commands.
12116
12117 (defun gnus-summary-edit-global-kill (article)
12118   "Edit the \"global\" kill file."
12119   (interactive (list (gnus-summary-article-number)))
12120   (gnus-group-edit-global-kill article))
12121
12122 (defun gnus-summary-edit-local-kill ()
12123   "Edit a local kill file applied to the current newsgroup."
12124   (interactive)
12125   (setq gnus-current-headers (gnus-summary-article-header))
12126   (gnus-group-edit-local-kill
12127    (gnus-summary-article-number) gnus-newsgroup-name))
12128
12129 ;;; Header reading.
12130
12131 (defun gnus-read-header (id &optional header)
12132   "Read the headers of article ID and enter them into the Gnus system."
12133   (let ((group gnus-newsgroup-name)
12134         (gnus-override-method
12135          (or
12136           gnus-override-method
12137           (and (gnus-news-group-p gnus-newsgroup-name)
12138                (car (gnus-refer-article-methods)))))
12139         where)
12140     ;; First we check to see whether the header in question is already
12141     ;; fetched.
12142     (if (stringp id)
12143         ;; This is a Message-ID.
12144         (setq header (or header (gnus-id-to-header id)))
12145       ;; This is an article number.
12146       (setq header (or header (gnus-summary-article-header id))))
12147     (if (and header
12148              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12149         ;; We have found the header.
12150         header
12151       ;; We have to really fetch the header to this article.
12152       (with-current-buffer nntp-server-buffer
12153         (when (setq where (gnus-request-head id group))
12154           (nnheader-fold-continuation-lines)
12155           (goto-char (point-max))
12156           (insert ".\n")
12157           (goto-char (point-min))
12158           (insert "211 ")
12159           (princ (cond
12160                   ((numberp id) id)
12161                   ((cdr where) (cdr where))
12162                   (header (mail-header-number header))
12163                   (t gnus-reffed-article-number))
12164                  (current-buffer))
12165           (insert " Article retrieved.\n"))
12166         (if (or (not where)
12167                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12168             ()                          ; Malformed head.
12169           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12170             (when (and (stringp id)
12171                        (or
12172                         (not (string= (gnus-group-real-name group)
12173                                       (car where)))
12174                         (not (gnus-server-equal gnus-override-method
12175                                                 (gnus-group-method group)))))
12176               ;; If we fetched by Message-ID and the article came from
12177               ;; a different group (or server), we fudge some bogus
12178               ;; article numbers for this article.
12179               (mail-header-set-number header gnus-reffed-article-number))
12180             (with-current-buffer gnus-summary-buffer
12181               (decf gnus-reffed-article-number)
12182               (gnus-remove-header (mail-header-number header))
12183               (push header gnus-newsgroup-headers)
12184               (setq gnus-current-headers header)
12185               (push (mail-header-number header) gnus-newsgroup-limit)))
12186           header)))))
12187
12188 (defun gnus-remove-header (number)
12189   "Remove header NUMBER from `gnus-newsgroup-headers'."
12190   (if (and gnus-newsgroup-headers
12191            (= number (mail-header-number (car gnus-newsgroup-headers))))
12192       (pop gnus-newsgroup-headers)
12193     (let ((headers gnus-newsgroup-headers))
12194       (while (and (cdr headers)
12195                   (not (= number (mail-header-number (cadr headers)))))
12196         (pop headers))
12197       (when (cdr headers)
12198         (setcdr headers (cddr headers))))))
12199
12200 ;;;
12201 ;;; summary highlights
12202 ;;;
12203
12204 (defun gnus-highlight-selected-summary ()
12205   "Highlight selected article in summary buffer."
12206   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12207   (when gnus-summary-selected-face
12208     (save-excursion
12209       (let* ((beg (point-at-bol))
12210              (end (point-at-eol))
12211              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12212              (from (if (get-text-property beg gnus-mouse-face-prop)
12213                        beg
12214                      (or (next-single-property-change
12215                           beg gnus-mouse-face-prop nil end)
12216                          beg)))
12217              (to
12218               (if (= from end)
12219                   (- from 2)
12220                 (or (next-single-property-change
12221                      from gnus-mouse-face-prop nil end)
12222                     end))))
12223         ;; If no mouse-face prop on line we will have to = from = end,
12224         ;; so we highlight the entire line instead.
12225         (when (= (+ to 2) from)
12226           (setq from beg)
12227           (setq to end))
12228         (if gnus-newsgroup-selected-overlay
12229             ;; Move old overlay.
12230             (gnus-move-overlay
12231              gnus-newsgroup-selected-overlay from to (current-buffer))
12232           ;; Create new overlay.
12233           (gnus-overlay-put
12234            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12235            'face gnus-summary-selected-face))))))
12236
12237 (defvar gnus-summary-highlight-line-cached nil)
12238 (defvar gnus-summary-highlight-line-trigger nil)
12239
12240 (defun gnus-summary-highlight-line-0 ()
12241   (if (and (eq gnus-summary-highlight-line-trigger
12242                gnus-summary-highlight)
12243            gnus-summary-highlight-line-cached)
12244       gnus-summary-highlight-line-cached
12245     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12246           gnus-summary-highlight-line-cached
12247           (let* ((cond (list 'cond))
12248                  (c cond)
12249                  (list gnus-summary-highlight))
12250             (while list
12251               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12252                               nil))
12253               (setq c (cdr c)
12254                     list (cdr list)))
12255             (gnus-byte-compile (list 'lambda nil cond))))))
12256
12257 (defun gnus-summary-highlight-line ()
12258   "Highlight current line according to `gnus-summary-highlight'."
12259   (let* ((beg (point-at-bol))
12260          (article (or (gnus-summary-article-number) gnus-current-article))
12261          (score (or (cdr (assq article
12262                                gnus-newsgroup-scored))
12263                     gnus-summary-default-score 0))
12264          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12265          (inhibit-read-only t)
12266          (default gnus-summary-default-score)
12267          (default-high gnus-summary-default-high-score)
12268          (default-low gnus-summary-default-low-score)
12269          (uncached (and gnus-summary-use-undownloaded-faces
12270                         (memq article gnus-newsgroup-undownloaded)
12271                         (not (memq article gnus-newsgroup-cached)))))
12272     (let ((face (funcall (gnus-summary-highlight-line-0))))
12273       (unless (eq face (get-text-property beg 'face))
12274         (gnus-put-text-property-excluding-characters-with-faces
12275          beg (point-at-eol) 'face
12276          (setq face (if (boundp face) (symbol-value face) face)))
12277         (when gnus-summary-highlight-line-function
12278           (funcall gnus-summary-highlight-line-function article face))))))
12279
12280 (defun gnus-update-read-articles (group unread &optional compute)
12281   "Update the list of read articles in GROUP.
12282 UNREAD is a sorted list."
12283   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12284         (info (gnus-get-info group))
12285         (prev 1)
12286         read)
12287     (if (or (not info) (not active))
12288         ;; There is no info on this group if it was, in fact,
12289         ;; killed.  Gnus stores no information on killed groups, so
12290         ;; there's nothing to be done.
12291         ;; One could store the information somewhere temporarily,
12292         ;; perhaps...  Hmmm...
12293         ()
12294       ;; Remove any negative articles numbers.
12295       (while (and unread (< (car unread) 0))
12296         (setq unread (cdr unread)))
12297       ;; Remove any expired article numbers
12298       (while (and unread (< (car unread) (car active)))
12299         (setq unread (cdr unread)))
12300       ;; Compute the ranges of read articles by looking at the list of
12301       ;; unread articles.
12302       (while unread
12303         (when (/= (car unread) prev)
12304           (push (if (= prev (1- (car unread))) prev
12305                   (cons prev (1- (car unread))))
12306                 read))
12307         (setq prev (1+ (car unread)))
12308         (setq unread (cdr unread)))
12309       (when (<= prev (cdr active))
12310         (push (cons prev (cdr active)) read))
12311       (setq read (if (> (length read) 1) (nreverse read) read))
12312       (if compute
12313           read
12314         (save-excursion
12315           (let (setmarkundo)
12316             ;; Propagate the read marks to the backend.
12317             (when (and gnus-propagate-marks
12318                        (gnus-check-backend-function 'request-set-mark group))
12319               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12320                     (add (gnus-remove-from-range read (gnus-info-read info))))
12321                 (when (or add del)
12322                   (unless (gnus-check-group group)
12323                     (error "Can't open server for %s" group))
12324                   (gnus-request-set-mark
12325                    group (delq nil (list (if add (list add 'add '(read)))
12326                                          (if del (list del 'del '(read))))))
12327                   (setq setmarkundo
12328                         `(gnus-request-set-mark
12329                           ,group
12330                           ',(delq nil (list
12331                                        (if del (list del 'add '(read)))
12332                                        (if add (list add 'del '(read))))))))))
12333             (set-buffer gnus-group-buffer)
12334             (gnus-undo-register
12335               `(progn
12336                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12337                  (gnus-info-set-read ',info ',(gnus-info-read info))
12338                  (gnus-get-unread-articles-in-group ',info
12339                                                     (gnus-active ,group))
12340                  (gnus-group-update-group ,group t)
12341                  ,setmarkundo))))
12342         ;; Enter this list into the group info.
12343         (gnus-info-set-read info read)
12344         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12345         (gnus-get-unread-articles-in-group info (gnus-active group))
12346         t))))
12347
12348 (defun gnus-offer-save-summaries ()
12349   "Offer to save all active summary buffers."
12350   (let (buffers)
12351     ;; Go through all buffers and find all summaries.
12352     (dolist (buffer (buffer-list))
12353       (when (and (setq buffer (buffer-name buffer))
12354                  (string-match "Summary" buffer)
12355                  (with-current-buffer buffer
12356                    ;; We check that this is, indeed, a summary buffer.
12357                    (and (eq major-mode 'gnus-summary-mode)
12358                         ;; Also make sure this isn't bogus.
12359                         gnus-newsgroup-prepared
12360                         ;; Also make sure that this isn't a
12361                         ;; dead summary buffer.
12362                         (not gnus-dead-summary-mode))))
12363         (push buffer buffers)))
12364     ;; Go through all these summary buffers and offer to save them.
12365     (when buffers
12366       (save-excursion
12367         (map-y-or-n-p
12368          "Update summary buffer %s? "
12369          (lambda (buf)
12370            (switch-to-buffer buf)
12371            (gnus-summary-exit))
12372          buffers)))))
12373
12374 (defun gnus-summary-setup-default-charset ()
12375   "Setup newsgroup default charset."
12376   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12377       (setq gnus-newsgroup-charset nil)
12378     (let* ((ignored-charsets
12379             (or gnus-newsgroup-ephemeral-ignored-charsets
12380                 (append
12381                  (and gnus-newsgroup-name
12382                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12383                  gnus-newsgroup-ignored-charsets))))
12384       (setq gnus-newsgroup-charset
12385             (or gnus-newsgroup-ephemeral-charset
12386                 (and gnus-newsgroup-name
12387                      (gnus-parameter-charset gnus-newsgroup-name))
12388                 gnus-default-charset))
12389       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12390            ignored-charsets))))
12391
12392 ;;;
12393 ;;; Mime Commands
12394 ;;;
12395
12396 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12397   "Display the current article buffer fully MIME-buttonized.
12398 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12399 treated as multipart/mixed."
12400   (interactive "P")
12401   (require 'gnus-art)
12402   (let ((gnus-unbuttonized-mime-types nil)
12403         (gnus-mime-display-multipart-as-mixed show-all-parts))
12404     (gnus-summary-show-article)))
12405
12406 (defun gnus-summary-repair-multipart (article)
12407   "Add a Content-Type header to a multipart article without one."
12408   (interactive (list (gnus-summary-article-number)))
12409   (gnus-with-article article
12410     (message-narrow-to-head)
12411     (message-remove-header "Mime-Version")
12412     (goto-char (point-max))
12413     (insert "Mime-Version: 1.0\n")
12414     (widen)
12415     (when (search-forward "\n--" nil t)
12416       (let ((separator (buffer-substring (point) (point-at-eol))))
12417         (message-narrow-to-head)
12418         (message-remove-header "Content-Type")
12419         (goto-char (point-max))
12420         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12421                         separator))
12422         (widen))))
12423   (let (gnus-mark-article-hook)
12424     (gnus-summary-select-article t t nil article)))
12425
12426 (defun gnus-summary-toggle-display-buttonized ()
12427   "Toggle the buttonizing of the article buffer."
12428   (interactive)
12429   (require 'gnus-art)
12430   (if (setq gnus-inhibit-mime-unbuttonizing
12431             (not gnus-inhibit-mime-unbuttonizing))
12432       (let ((gnus-unbuttonized-mime-types nil))
12433         (gnus-summary-show-article))
12434     (gnus-summary-show-article)))
12435
12436 ;;;
12437 ;;; Generic summary marking commands
12438 ;;;
12439
12440 (defvar gnus-summary-marking-alist
12441   '((read gnus-del-mark "d")
12442     (unread gnus-unread-mark "u")
12443     (ticked gnus-ticked-mark "!")
12444     (dormant gnus-dormant-mark "?")
12445     (expirable gnus-expirable-mark "e"))
12446   "An alist of names/marks/keystrokes.")
12447
12448 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12449 (defvar gnus-summary-mark-map)
12450
12451 (defun gnus-summary-make-all-marking-commands ()
12452   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12453   (dolist (elem gnus-summary-marking-alist)
12454     (apply 'gnus-summary-make-marking-command elem)))
12455
12456 (defun gnus-summary-make-marking-command (name mark keystroke)
12457   (let ((map (make-sparse-keymap)))
12458     (define-key gnus-summary-generic-mark-map keystroke map)
12459     (dolist (lway `((next "next" next nil "n")
12460                     (next-unread "next unread" next t "N")
12461                     (prev "previous" prev nil "p")
12462                     (prev-unread "previous unread" prev t "P")
12463                     (nomove "" nil nil ,keystroke)))
12464       (let ((func (gnus-summary-make-marking-command-1
12465                    mark (car lway) lway name)))
12466         (setq func (eval func))
12467         (define-key map (nth 4 lway) func)))))
12468
12469 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12470   `(defun ,(intern
12471             (format "gnus-summary-put-mark-as-%s%s"
12472                     name (if (eq way 'nomove)
12473                              ""
12474                            (concat "-" (symbol-name way)))))
12475      (n)
12476      ,(format
12477        "Mark the current article as %s%s.
12478 If N, the prefix, then repeat N times.
12479 If N is negative, move in reverse order.
12480 The difference between N and the actual number of articles marked is
12481 returned."
12482        name (cadr lway))
12483      (interactive "p")
12484      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12485
12486 (defun gnus-summary-generic-mark (n mark move unread)
12487   "Mark N articles with MARK."
12488   (unless (eq major-mode 'gnus-summary-mode)
12489     (error "This command can only be used in the summary buffer"))
12490   (gnus-summary-show-thread)
12491   (let ((nummove
12492          (cond
12493           ((eq move 'next) 1)
12494           ((eq move 'prev) -1)
12495           (t 0))))
12496     (if (zerop nummove)
12497         (setq n 1)
12498       (when (< n 0)
12499         (setq n (abs n)
12500               nummove (* -1 nummove))))
12501     (while (and (> n 0)
12502                 (gnus-summary-mark-article nil mark)
12503                 (zerop (gnus-summary-next-subject nummove unread t)))
12504       (setq n (1- n)))
12505     (when (/= 0 n)
12506       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12507     (gnus-summary-recenter)
12508     (gnus-summary-position-point)
12509     (gnus-set-mode-line 'summary)
12510     n))
12511
12512 (defun gnus-summary-insert-articles (articles)
12513   (when (setq articles
12514               (gnus-sorted-difference articles
12515                                       (mapcar (lambda (h)
12516                                                 (mail-header-number h))
12517                                               gnus-newsgroup-headers)))
12518     (setq gnus-newsgroup-headers
12519           (gnus-merge 'list
12520                       gnus-newsgroup-headers
12521                       (gnus-fetch-headers articles)
12522                       'gnus-article-sort-by-number))
12523     ;; Suppress duplicates?
12524     (when gnus-suppress-duplicates
12525       (gnus-dup-suppress-articles))
12526
12527     (if (and gnus-fetch-old-headers
12528              (eq gnus-headers-retrieved-by 'nov))
12529         ;; We might want to build some more threads first.
12530         (if (eq gnus-fetch-old-headers 'invisible)
12531             (gnus-build-all-threads)
12532           (gnus-build-old-threads))
12533       ;; Mark the inserted articles that are unread as unread.
12534       (setq gnus-newsgroup-unreads
12535             (gnus-sorted-nunion
12536              gnus-newsgroup-unreads
12537              (gnus-sorted-nintersection
12538               (gnus-list-of-unread-articles gnus-newsgroup-name)
12539               articles)))
12540       ;; Mark the inserted articles as selected so that the information
12541       ;; of the marks having been changed by a user may be updated when
12542       ;; exiting this group.  See `gnus-summary-update-info'.
12543       (dolist (art articles)
12544         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12545     ;; Let the Gnus agent mark articles as read.
12546     (when gnus-agent
12547       (gnus-agent-get-undownloaded-list))
12548     ;; Remove list identifiers from subject
12549     (when gnus-list-identifiers
12550       (gnus-summary-remove-list-identifiers))
12551     ;; First and last article in this newsgroup.
12552     (when gnus-newsgroup-headers
12553       (setq gnus-newsgroup-begin
12554             (mail-header-number (car gnus-newsgroup-headers))
12555             gnus-newsgroup-end
12556             (mail-header-number
12557              (gnus-last-element gnus-newsgroup-headers))))
12558     (when gnus-use-scoring
12559       (gnus-possibly-score-headers))))
12560
12561 (defun gnus-summary-insert-old-articles (&optional all)
12562   "Insert all old articles in this group.
12563 If ALL is non-nil, already read articles become readable.
12564 If ALL is a number, fetch this number of articles."
12565   (interactive "P")
12566   (prog1
12567       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12568             older len)
12569         (setq older
12570               ;; Some nntp servers lie about their active range.  When
12571               ;; this happens, the active range can be in the millions.
12572               ;; Use a compressed range to avoid creating a huge list.
12573               (gnus-range-difference (list gnus-newsgroup-active) old))
12574         (setq len (gnus-range-length older))
12575         (cond
12576          ((null older) nil)
12577          ((numberp all)
12578           (if (< all len)
12579               (let ((older-range (nreverse older)))
12580                 (setq older nil)
12581
12582                 (while (> all 0)
12583                   (let* ((r (pop older-range))
12584                          (min (if (numberp r) r (car r)))
12585                          (max (if (numberp r) r (cdr r))))
12586                     (while (and (<= min max)
12587                                 (> all 0))
12588                       (push max older)
12589                       (setq all (1- all)
12590                             max (1- max))))))
12591             (setq older (gnus-uncompress-range older))))
12592          (all
12593           (setq older (gnus-uncompress-range older)))
12594          (t
12595           (when (and (numberp gnus-large-newsgroup)
12596                    (> len gnus-large-newsgroup))
12597               (let* ((cursor-in-echo-area nil)
12598                      (initial (gnus-parameter-large-newsgroup-initial
12599                                gnus-newsgroup-name))
12600                      (input
12601                       (read-string
12602                        (format
12603                         "How many articles from %s (%s %d): "
12604                         (gnus-group-decoded-name gnus-newsgroup-name)
12605                         (if initial "max" "default")
12606                         len)
12607                        (if initial
12608                            (cons (number-to-string initial)
12609                                  0)))))
12610                 (unless (string-match "^[ \t]*$" input)
12611                   (setq all (string-to-number input))
12612                   (if (< all len)
12613                       (let ((older-range (nreverse older)))
12614                         (setq older nil)
12615
12616                         (while (> all 0)
12617                           (let* ((r (pop older-range))
12618                                  (min (if (numberp r) r (car r)))
12619                                  (max (if (numberp r) r (cdr r))))
12620                             (while (and (<= min max)
12621                                         (> all 0))
12622                               (push max older)
12623                               (setq all (1- all)
12624                                     max (1- max))))))))))
12625           (setq older (gnus-uncompress-range older))))
12626         (if (not older)
12627             (message "No old news.")
12628           (gnus-summary-insert-articles older)
12629           (gnus-summary-limit (gnus-sorted-nunion old older))))
12630     (gnus-summary-position-point)))
12631
12632 (defun gnus-summary-insert-new-articles ()
12633   "Insert all new articles in this group."
12634   (interactive)
12635   (prog1
12636       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12637             (old-high gnus-newsgroup-highest)
12638             (nnmail-fetched-sources (list t))
12639             i new)
12640         (setq gnus-newsgroup-active
12641               (gnus-copy-sequence
12642                (gnus-activate-group gnus-newsgroup-name 'scan)))
12643         (setq i (cdr gnus-newsgroup-active)
12644               gnus-newsgroup-highest i)
12645         (while (> i old-high)
12646           (push i new)
12647           (decf i))
12648         (if (not new)
12649             (message "No gnus is bad news")
12650           (gnus-summary-insert-articles new)
12651           (setq gnus-newsgroup-unreads
12652                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12653           (gnus-summary-limit (gnus-sorted-nunion old new))))
12654     (gnus-summary-position-point)))
12655
12656 ;;; Bookmark support for Gnus.
12657 (declare-function bookmark-make-record-default
12658                   "bookmark" (&optional no-file no-context posn))
12659 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12660 (declare-function bookmark-default-handler "bookmark" (bmk))
12661 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12662 (defvar bookmark-yank-point)
12663 (defvar bookmark-current-buffer)
12664
12665 (defun gnus-summary-bookmark-make-record ()
12666   "Make a bookmark entry for a Gnus summary buffer."
12667   (let (pos buf)
12668     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12669       (save-restriction              ; FIXME is it necessary to widen?
12670         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12671       (setq buf "art") ; We are recording bookmark from article buffer.
12672       (setq bookmark-yank-point (point))
12673       (setq bookmark-current-buffer (current-buffer))
12674       (gnus-article-show-summary))      ; Go back in summary buffer.
12675     ;; We are now recording bookmark from summary buffer.
12676     (unless buf (setq buf "sum"))
12677     (let* ((subject (elt (gnus-summary-article-header) 1))
12678            (grp     (car gnus-article-current))
12679            (art     (cdr gnus-article-current))
12680            (head    (gnus-summary-article-header art))
12681            (id      (mail-header-id head)))
12682       `(,subject
12683         ,@(condition-case nil
12684               (bookmark-make-record-default 'no-file 'no-context pos)
12685             (wrong-number-of-arguments
12686              (bookmark-make-record-default 'point-only)))
12687         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12688         (group . ,grp) (article . ,art)
12689         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12690
12691 ;;;###autoload
12692 (defun gnus-summary-bookmark-jump (bookmark)
12693   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12694 BOOKMARK is a bookmark name or a bookmark record."
12695   (let ((group    (bookmark-prop-get bookmark 'group))
12696         (article  (bookmark-prop-get bookmark 'article))
12697         (id       (bookmark-prop-get bookmark 'message-id))
12698         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12699     (gnus-fetch-group group (list article))
12700     (gnus-summary-insert-cached-articles)
12701     (gnus-summary-goto-article id nil 'force)
12702     ;; FIXME we have to wait article buffer is ready (only large buffer)
12703     ;; Is there a better solution to know that?
12704     ;; If we don't wait `bookmark-default-handler' will have no chance
12705     ;; to set position. However there is no error, just wrong pos.
12706     (sit-for 1)
12707     (when (string= buf "Gnus-art")
12708       (other-window 1))
12709     (bookmark-default-handler
12710      `(""
12711        (buffer . ,(current-buffer))
12712        . ,(bookmark-get-bookmark-record bookmark)))))
12713
12714 (gnus-summary-make-all-marking-commands)
12715
12716 (gnus-ems-redefine)
12717
12718 (provide 'gnus-sum)
12719
12720 (run-hooks 'gnus-sum-load-hook)
12721
12722 ;; Local Variables:
12723 ;; coding: iso-8859-1
12724 ;; End:
12725
12726 ;;; gnus-sum.el ends here