If the article buffer isn't shown, then select the current article first instead...
[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 ;; Summary data functions.
3113
3114 (defmacro gnus-data-number (data)
3115   `(car ,data))
3116
3117 (defmacro gnus-data-set-number (data number)
3118   `(setcar ,data ,number))
3119
3120 (defmacro gnus-data-mark (data)
3121   `(nth 1 ,data))
3122
3123 (defmacro gnus-data-set-mark (data mark)
3124   `(setcar (nthcdr 1 ,data) ,mark))
3125
3126 (defmacro gnus-data-pos (data)
3127   `(nth 2 ,data))
3128
3129 (defmacro gnus-data-set-pos (data pos)
3130   `(setcar (nthcdr 2 ,data) ,pos))
3131
3132 (defmacro gnus-data-header (data)
3133   `(nth 3 ,data))
3134
3135 (defmacro gnus-data-set-header (data header)
3136   `(setf (nth 3 ,data) ,header))
3137
3138 (defmacro gnus-data-level (data)
3139   `(nth 4 ,data))
3140
3141 (defmacro gnus-data-unread-p (data)
3142   `(= (nth 1 ,data) gnus-unread-mark))
3143
3144 (defmacro gnus-data-read-p (data)
3145   `(/= (nth 1 ,data) gnus-unread-mark))
3146
3147 (defmacro gnus-data-pseudo-p (data)
3148   `(consp (nth 3 ,data)))
3149
3150 (defmacro gnus-data-find (number)
3151   `(assq ,number gnus-newsgroup-data))
3152
3153 (defmacro gnus-data-find-list (number &optional data)
3154   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3155      (memq (assq ,number bdata)
3156            bdata)))
3157
3158 (defmacro gnus-data-make (number mark pos header level)
3159   `(list ,number ,mark ,pos ,header ,level))
3160
3161 (defun gnus-data-enter (after-article number mark pos header level offset)
3162   (let ((data (gnus-data-find-list after-article)))
3163     (unless data
3164       (error "No such article: %d" after-article))
3165     (setcdr data (cons (gnus-data-make number mark pos header level)
3166                        (cdr data)))
3167     (setq gnus-newsgroup-data-reverse nil)
3168     (gnus-data-update-list (cddr data) offset)))
3169
3170 (defun gnus-data-enter-list (after-article list &optional offset)
3171   (when list
3172     (let ((data (and after-article (gnus-data-find-list after-article)))
3173           (ilist list))
3174       (if (not (or data
3175                    after-article))
3176           (let ((odata gnus-newsgroup-data))
3177             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3178             (when offset
3179               (gnus-data-update-list odata offset)))
3180         ;; Find the last element in the list to be spliced into the main
3181         ;; list.
3182         (setq list (last list))
3183         (if (not data)
3184             (progn
3185               (setcdr list gnus-newsgroup-data)
3186               (setq gnus-newsgroup-data ilist)
3187               (when offset
3188                 (gnus-data-update-list (cdr list) offset)))
3189           (setcdr list (cdr data))
3190           (setcdr data ilist)
3191           (when offset
3192             (gnus-data-update-list (cdr list) offset))))
3193       (setq gnus-newsgroup-data-reverse nil))))
3194
3195 (defun gnus-data-remove (article &optional offset)
3196   (let ((data gnus-newsgroup-data))
3197     (if (= (gnus-data-number (car data)) article)
3198         (progn
3199           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3200                 gnus-newsgroup-data-reverse nil)
3201           (when offset
3202             (gnus-data-update-list gnus-newsgroup-data offset)))
3203       (while (cdr data)
3204         (when (= (gnus-data-number (cadr data)) article)
3205           (setcdr data (cddr data))
3206           (when offset
3207             (gnus-data-update-list (cdr data) offset))
3208           (setq data nil
3209                 gnus-newsgroup-data-reverse nil))
3210         (setq data (cdr data))))))
3211
3212 (defmacro gnus-data-list (backward)
3213   `(if ,backward
3214        (or gnus-newsgroup-data-reverse
3215            (setq gnus-newsgroup-data-reverse
3216                  (reverse gnus-newsgroup-data)))
3217      gnus-newsgroup-data))
3218
3219 (defun gnus-data-update-list (data offset)
3220   "Add OFFSET to the POS of all data entries in DATA."
3221   (setq gnus-newsgroup-data-reverse nil)
3222   (while data
3223     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3224     (setq data (cdr data))))
3225
3226 (defun gnus-summary-article-pseudo-p (article)
3227   "Say whether this article is a pseudo article or not."
3228   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3229
3230 (defmacro gnus-summary-article-sparse-p (article)
3231   "Say whether this article is a sparse article or not."
3232   `(memq ,article gnus-newsgroup-sparse))
3233
3234 (defmacro gnus-summary-article-ancient-p (article)
3235   "Say whether this article is a sparse article or not."
3236   `(memq ,article gnus-newsgroup-ancient))
3237
3238 (defun gnus-article-parent-p (number)
3239   "Say whether this article is a parent or not."
3240   (let ((data (gnus-data-find-list number)))
3241     (and (cdr data)              ; There has to be an article after...
3242          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3243             (gnus-data-level (nth 1 data))))))
3244
3245 (defun gnus-article-children (number)
3246   "Return a list of all children to NUMBER."
3247   (let* ((data (gnus-data-find-list number))
3248          (level (gnus-data-level (car data)))
3249          children)
3250     (setq data (cdr data))
3251     (while (and data
3252                 (= (gnus-data-level (car data)) (1+ level)))
3253       (push (gnus-data-number (car data)) children)
3254       (setq data (cdr data)))
3255     children))
3256
3257 (defmacro gnus-summary-skip-intangible ()
3258   "If the current article is intangible, then jump to a different article."
3259   '(let ((to (get-text-property (point) 'gnus-intangible)))
3260      (and to (gnus-summary-goto-subject to))))
3261
3262 (defmacro gnus-summary-article-intangible-p ()
3263   "Say whether this article is intangible or not."
3264   '(get-text-property (point) 'gnus-intangible))
3265
3266 (defun gnus-article-read-p (article)
3267   "Say whether ARTICLE is read or not."
3268   (not (or (memq article gnus-newsgroup-marked)
3269            (memq article gnus-newsgroup-spam-marked)
3270            (memq article gnus-newsgroup-unreads)
3271            (memq article gnus-newsgroup-unselected)
3272            (memq article gnus-newsgroup-dormant))))
3273
3274 ;; Some summary mode macros.
3275
3276 (defmacro gnus-summary-article-number ()
3277   "The article number of the article on the current line.
3278 If there isn't an article number here, then we return the current
3279 article number."
3280   '(progn
3281      (gnus-summary-skip-intangible)
3282      (or (get-text-property (point) 'gnus-number)
3283          (gnus-summary-last-subject))))
3284
3285 (defmacro gnus-summary-article-header (&optional number)
3286   "Return the header of article NUMBER."
3287   `(gnus-data-header (gnus-data-find
3288                       ,(or number '(gnus-summary-article-number)))))
3289
3290 (defmacro gnus-summary-thread-level (&optional number)
3291   "Return the level of thread that starts with article NUMBER."
3292   `(if (and (eq gnus-summary-make-false-root 'dummy)
3293             (get-text-property (point) 'gnus-intangible))
3294        0
3295      (gnus-data-level (gnus-data-find
3296                        ,(or number '(gnus-summary-article-number))))))
3297
3298 (defmacro gnus-summary-article-mark (&optional number)
3299   "Return the mark of article NUMBER."
3300   `(gnus-data-mark (gnus-data-find
3301                     ,(or number '(gnus-summary-article-number)))))
3302
3303 (defmacro gnus-summary-article-pos (&optional number)
3304   "Return the position of the line of article NUMBER."
3305   `(gnus-data-pos (gnus-data-find
3306                    ,(or number '(gnus-summary-article-number)))))
3307
3308 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3309 (defmacro gnus-summary-article-subject (&optional number)
3310   "Return current subject string or nil if nothing."
3311   `(let ((headers
3312           ,(if number
3313                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3314              '(gnus-data-header (assq (gnus-summary-article-number)
3315                                       gnus-newsgroup-data)))))
3316      (and headers
3317           (vectorp headers)
3318           (mail-header-subject headers))))
3319
3320 (defmacro gnus-summary-article-score (&optional number)
3321   "Return current article score."
3322   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3323                   gnus-newsgroup-scored))
3324        gnus-summary-default-score 0))
3325
3326 (defun gnus-summary-article-children (&optional number)
3327   "Return a list of article numbers that are children of article NUMBER."
3328   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3329          (level (gnus-data-level (car data)))
3330          l children)
3331     (while (and (setq data (cdr data))
3332                 (> (setq l (gnus-data-level (car data))) level))
3333       (and (= (1+ level) l)
3334            (push (gnus-data-number (car data))
3335                  children)))
3336     (nreverse children)))
3337
3338 (defun gnus-summary-article-parent (&optional number)
3339   "Return the article number of the parent of article NUMBER."
3340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3341                                     (gnus-data-list t)))
3342          (level (gnus-data-level (car data))))
3343     (if (zerop level)
3344         ()                              ; This is a root.
3345       ;; We search until we find an article with a level less than
3346       ;; this one.  That function has to be the parent.
3347       (while (and (setq data (cdr data))
3348                   (not (< (gnus-data-level (car data)) level))))
3349       (and data (gnus-data-number (car data))))))
3350
3351 (defun gnus-unread-mark-p (mark)
3352   "Say whether MARK is the unread mark."
3353   (= mark gnus-unread-mark))
3354
3355 (defun gnus-read-mark-p (mark)
3356   "Say whether MARK is one of the marks that mark as read.
3357 This is all marks except unread, ticked, dormant, and expirable."
3358   (not (or (= mark gnus-unread-mark)
3359            (= mark gnus-ticked-mark)
3360            (= mark gnus-spam-mark)
3361            (= mark gnus-dormant-mark)
3362            (= mark gnus-expirable-mark))))
3363
3364 (defmacro gnus-article-mark (number)
3365   "Return the MARK of article NUMBER.
3366 This macro should only be used when computing the mark the \"first\"
3367 time; i.e., when generating the summary lines.  After that,
3368 `gnus-summary-article-mark' should be used to examine the
3369 marks of articles."
3370   `(cond
3371     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3372     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3373     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3374     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3375     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3376     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3377     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3378     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3379            gnus-ancient-mark))))
3380
3381 ;; Saving hidden threads.
3382
3383 (defmacro gnus-save-hidden-threads (&rest forms)
3384   "Save hidden threads, eval FORMS, and restore the hidden threads."
3385   (let ((config (make-symbol "config")))
3386     `(let ((,config (gnus-hidden-threads-configuration)))
3387        (unwind-protect
3388            (save-excursion
3389              ,@forms)
3390          (gnus-restore-hidden-threads-configuration ,config)))))
3391 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3392 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3393
3394 (defun gnus-data-compute-positions ()
3395   "Compute the positions of all articles."
3396   (setq gnus-newsgroup-data-reverse nil)
3397   (let ((data gnus-newsgroup-data))
3398     (save-excursion
3399       (gnus-save-hidden-threads
3400         (gnus-summary-show-all-threads)
3401         (goto-char (point-min))
3402         (while data
3403           (while (get-text-property (point) 'gnus-intangible)
3404             (forward-line 1))
3405           (gnus-data-set-pos (car data) (+ (point) 3))
3406           (setq data (cdr data))
3407           (forward-line 1))))))
3408
3409 (defun gnus-hidden-threads-configuration ()
3410   "Return the current hidden threads configuration."
3411   (save-excursion
3412     (let (config)
3413       (goto-char (point-min))
3414       (while (not (eobp))
3415         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3416           (push (save-excursion (forward-line 0) (point)) config))
3417         (forward-line 1))
3418       config)))
3419
3420 (defun gnus-restore-hidden-threads-configuration (config)
3421   "Restore hidden threads configuration from CONFIG."
3422   (save-excursion
3423     (let (point (inhibit-read-only t))
3424       (while (setq point (pop config))
3425         (goto-char point)
3426         (gnus-summary-hide-thread)))))
3427
3428 ;; Various summary mode internalish functions.
3429
3430 (defun gnus-mouse-pick-article (e)
3431   (interactive "e")
3432   (mouse-set-point e)
3433   (gnus-summary-next-page nil t))
3434
3435 (defun gnus-summary-set-display-table ()
3436   "Change the display table.
3437 Odd characters have a tendency to mess
3438 up nicely formatted displays - we make all possible glyphs
3439 display only a single character."
3440
3441   ;; We start from the standard display table, if any.
3442   (let ((table (or (copy-sequence standard-display-table)
3443                    (make-display-table)))
3444         (i 32))
3445     ;; Nix out all the control chars...
3446     (while (>= (setq i (1- i)) 0)
3447       (gnus-put-display-table i [??] table))
3448    ;; ... but not newline and cr, of course.  (cr is necessary for the
3449     ;; selective display).
3450     (gnus-put-display-table ?\n nil table)
3451     (gnus-put-display-table ?\r nil table)
3452     ;; We keep TAB as well.
3453     (gnus-put-display-table ?\t nil table)
3454     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3455     ;; Emacs 23 (unicode), that are not set already.
3456     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3457                  160
3458                256)))
3459       (while (>= (setq i (1- i)) 127)
3460         ;; Only modify if the entry is nil.
3461         (unless (gnus-get-display-table i table)
3462           (gnus-put-display-table i [??] table))))
3463     (setq buffer-display-table table)))
3464
3465 (defun gnus-summary-set-article-display-arrow (pos)
3466   "Update the overlay arrow to point to line at position POS."
3467   (when gnus-summary-display-arrow
3468     (make-local-variable 'overlay-arrow-position)
3469     (make-local-variable 'overlay-arrow-string)
3470     (save-excursion
3471       (goto-char pos)
3472       (beginning-of-line)
3473       (unless overlay-arrow-position
3474         (setq overlay-arrow-position (make-marker)))
3475       (setq overlay-arrow-string "=>"
3476             overlay-arrow-position (set-marker overlay-arrow-position
3477                                                (point)
3478                                                (current-buffer))))))
3479
3480 (defun gnus-summary-setup-buffer (group)
3481   "Initialize summary buffer."
3482   (let ((buffer (gnus-summary-buffer-name group))
3483         (dead-name (concat "*Dead Summary "
3484                            (gnus-group-decoded-name group) "*")))
3485     ;; If a dead summary buffer exists, we kill it.
3486     (when (gnus-buffer-live-p dead-name)
3487       (gnus-kill-buffer dead-name))
3488     (if (get-buffer buffer)
3489         (progn
3490           (set-buffer buffer)
3491           (setq gnus-summary-buffer (current-buffer))
3492           (not gnus-newsgroup-prepared))
3493       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3494       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3495       (gnus-summary-mode group)
3496       (when gnus-carpal
3497         (gnus-carpal-setup-buffer 'summary))
3498       (when (gnus-group-quit-config group)
3499         (set (make-local-variable 'gnus-single-article-buffer) nil))
3500       (make-local-variable 'gnus-article-buffer)
3501       (make-local-variable 'gnus-article-current)
3502       (make-local-variable 'gnus-original-article-buffer)
3503       (setq gnus-newsgroup-name group)
3504       ;; Set any local variables in the group parameters.
3505       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3506       t)))
3507
3508 (defun gnus-set-global-variables ()
3509   "Set the global equivalents of the buffer-local variables.
3510 They are set to the latest values they had.  These reflect the summary
3511 buffer that was in action when the last article was fetched."
3512   (when (eq major-mode 'gnus-summary-mode)
3513     (setq gnus-summary-buffer (current-buffer))
3514     (let ((name gnus-newsgroup-name)
3515           (marked gnus-newsgroup-marked)
3516           (spam gnus-newsgroup-spam-marked)
3517           (unread gnus-newsgroup-unreads)
3518           (headers gnus-current-headers)
3519           (data gnus-newsgroup-data)
3520           (summary gnus-summary-buffer)
3521           (article-buffer gnus-article-buffer)
3522           (original gnus-original-article-buffer)
3523           (gac gnus-article-current)
3524           (reffed gnus-reffed-article-number)
3525           (score-file gnus-current-score-file)
3526           (default-charset gnus-newsgroup-charset)
3527           vlist)
3528       (let ((locals gnus-newsgroup-variables))
3529         (while locals
3530           (if (consp (car locals))
3531               (push (eval (caar locals)) vlist)
3532             (push (eval (car locals)) vlist))
3533           (setq locals (cdr locals)))
3534         (setq vlist (nreverse vlist)))
3535       (with-current-buffer gnus-group-buffer
3536         (setq gnus-newsgroup-name name
3537               gnus-newsgroup-marked marked
3538               gnus-newsgroup-spam-marked spam
3539               gnus-newsgroup-unreads unread
3540               gnus-current-headers headers
3541               gnus-newsgroup-data data
3542               gnus-article-current gac
3543               gnus-summary-buffer summary
3544               gnus-article-buffer article-buffer
3545               gnus-original-article-buffer original
3546               gnus-reffed-article-number reffed
3547               gnus-current-score-file score-file
3548               gnus-newsgroup-charset default-charset)
3549         (let ((locals gnus-newsgroup-variables))
3550           (while locals
3551             (if (consp (car locals))
3552                 (set (caar locals) (pop vlist))
3553               (set (car locals) (pop vlist)))
3554             (setq locals (cdr locals))))
3555         ;; The article buffer also has local variables.
3556         (when (gnus-buffer-live-p gnus-article-buffer)
3557           (set-buffer gnus-article-buffer)
3558           (setq gnus-summary-buffer summary))))))
3559
3560 (defun gnus-summary-article-unread-p (article)
3561   "Say whether ARTICLE is unread or not."
3562   (memq article gnus-newsgroup-unreads))
3563
3564 (defun gnus-summary-first-article-p (&optional article)
3565   "Return whether ARTICLE is the first article in the buffer."
3566   (if (not (setq article (or article (gnus-summary-article-number))))
3567       nil
3568     (eq article (caar gnus-newsgroup-data))))
3569
3570 (defun gnus-summary-last-article-p (&optional article)
3571   "Return whether ARTICLE is the last article in the buffer."
3572   (if (not (setq article (or article (gnus-summary-article-number))))
3573       ;; All non-existent numbers are the last article.  :-)
3574       t
3575     (not (cdr (gnus-data-find-list article)))))
3576
3577 (defun gnus-make-thread-indent-array (&optional n)
3578   (when (or n
3579             (progn (setq n 200) nil)
3580             (null gnus-thread-indent-array)
3581             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3582     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3583           gnus-thread-indent-array-level gnus-thread-indent-level)
3584     (while (>= n 0)
3585       (aset gnus-thread-indent-array n
3586             (make-string (* n gnus-thread-indent-level) ? ))
3587       (setq n (1- n)))))
3588
3589 (defun gnus-update-summary-mark-positions ()
3590   "Compute where the summary marks are to go."
3591   (save-excursion
3592     (when (gnus-buffer-exists-p gnus-summary-buffer)
3593       (set-buffer gnus-summary-buffer))
3594     (let ((spec gnus-summary-line-format-spec)
3595           pos)
3596       (save-excursion
3597         (gnus-set-work-buffer)
3598         (let ((gnus-tmp-unread ?Z)
3599               (gnus-replied-mark ?Z)
3600               (gnus-score-below-mark ?Z)
3601               (gnus-score-over-mark ?Z)
3602               (gnus-undownloaded-mark ?Z)
3603               (gnus-summary-line-format-spec spec)
3604               (gnus-newsgroup-downloadable '(0))
3605               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3606               case-fold-search ignores)
3607           ;; Here, all marks are bound to Z.
3608           (gnus-summary-insert-line header
3609                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3610           (goto-char (point-min))
3611           ;; Memorize the positions of the same characters as dummy marks.
3612           (while (re-search-forward "[A-D]" nil t)
3613             (push (point) ignores))
3614           (erase-buffer)
3615           ;; We use A-D as dummy marks in order to know column positions
3616           ;; where marks should be inserted.
3617           (setq gnus-tmp-unread ?A
3618                 gnus-replied-mark ?B
3619                 gnus-score-below-mark ?C
3620                 gnus-score-over-mark ?C
3621                 gnus-undownloaded-mark ?D)
3622           (gnus-summary-insert-line header
3623                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3624           ;; Ignore characters which aren't dummy marks.
3625           (dolist (p ignores)
3626             (delete-region (goto-char (1- p)) p)
3627             (insert ?Z))
3628           (goto-char (point-min))
3629           (setq pos (list (cons 'unread
3630                                 (and (search-forward "A" nil t)
3631                                      (- (point) (point-min) 1)))))
3632           (goto-char (point-min))
3633           (push (cons 'replied (and (search-forward "B" nil t)
3634                                     (- (point) (point-min) 1)))
3635                 pos)
3636           (goto-char (point-min))
3637           (push (cons 'score (and (search-forward "C" nil t)
3638                                   (- (point) (point-min) 1)))
3639                 pos)
3640           (goto-char (point-min))
3641           (push (cons 'download (and (search-forward "D" nil t)
3642                                      (- (point) (point-min) 1)))
3643                 pos)))
3644       (setq gnus-summary-mark-positions pos))))
3645
3646 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3647   "Insert a dummy root in the summary buffer."
3648   (beginning-of-line)
3649   (gnus-add-text-properties
3650    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3651    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3652
3653 (defun gnus-summary-extract-address-component (from)
3654   (or (car (funcall gnus-extract-address-components from))
3655       from))
3656
3657 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3658   (let ((mail-parse-charset gnus-newsgroup-charset)
3659         (ignored-from-addresses (gnus-ignored-from-addresses))
3660         ; Is it really necessary to do this next part for each summary line?
3661         ; Luckily, doesn't seem to slow things down much.
3662         (mail-parse-ignored-charsets
3663          (with-current-buffer gnus-summary-buffer
3664            gnus-newsgroup-ignored-charsets)))
3665     (or
3666      (and ignored-from-addresses
3667           (string-match ignored-from-addresses gnus-tmp-from)
3668           (let ((extra-headers (mail-header-extra header))
3669                 to
3670                 newsgroups)
3671             (cond
3672              ((setq to (cdr (assq 'To extra-headers)))
3673               (concat gnus-summary-to-prefix
3674                       (inline
3675                         (gnus-summary-extract-address-component
3676                          (funcall gnus-decode-encoded-address-function to)))))
3677              ((setq newsgroups
3678                     (or
3679                      (cdr (assq 'Newsgroups extra-headers))
3680                      (and
3681                       (memq 'Newsgroups gnus-extra-headers)
3682                       (eq (car (gnus-find-method-for-group
3683                                 gnus-newsgroup-name)) 'nntp)
3684                       (gnus-group-real-name gnus-newsgroup-name))))
3685               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3686      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3687
3688 (defun gnus-summary-insert-line (gnus-tmp-header
3689                                  gnus-tmp-level gnus-tmp-current
3690                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3691                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3692                                  &optional gnus-tmp-dummy gnus-tmp-score
3693                                  gnus-tmp-process)
3694   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3695       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3696                                           gnus-tmp-level)))
3697   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3698          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3699          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3700          (gnus-tmp-score-char
3701           (if (or (null gnus-summary-default-score)
3702                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3703                       gnus-summary-zcore-fuzz))
3704               ?                         ;Whitespace
3705             (if (< gnus-tmp-score gnus-summary-default-score)
3706                 gnus-score-below-mark gnus-score-over-mark)))
3707          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3708          (gnus-tmp-replied
3709           (cond (gnus-tmp-process gnus-process-mark)
3710                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3711                  gnus-cached-mark)
3712                 (gnus-tmp-replied gnus-replied-mark)
3713                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3714                  gnus-forwarded-mark)
3715                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3716                  gnus-saved-mark)
3717                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3718                  gnus-recent-mark)
3719                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3720                  gnus-unseen-mark)
3721                 (t gnus-no-mark)))
3722          (gnus-tmp-downloaded
3723           (cond (undownloaded
3724                  gnus-undownloaded-mark)
3725                 (gnus-newsgroup-agentized
3726                  gnus-downloaded-mark)
3727                 (t
3728                  gnus-no-mark)))
3729          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3730          (gnus-tmp-name
3731           (cond
3732            ((string-match "<[^>]+> *$" gnus-tmp-from)
3733             (let ((beg (match-beginning 0)))
3734               (or (and (string-match "^\".+\"" gnus-tmp-from)
3735                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3736                   (substring gnus-tmp-from 0 beg))))
3737            ((string-match "(.+)" gnus-tmp-from)
3738             (substring gnus-tmp-from
3739                        (1+ (match-beginning 0)) (1- (match-end 0))))
3740            (t gnus-tmp-from)))
3741          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3742          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3743          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3744          (inhibit-read-only t))
3745     (when (string= gnus-tmp-name "")
3746       (setq gnus-tmp-name gnus-tmp-from))
3747     (unless (numberp gnus-tmp-lines)
3748       (setq gnus-tmp-lines -1))
3749     (if (= gnus-tmp-lines -1)
3750         (setq gnus-tmp-lines "?")
3751       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3752     (condition-case ()
3753         (gnus-put-text-property
3754          (point)
3755          (progn (eval gnus-summary-line-format-spec) (point))
3756          'gnus-number gnus-tmp-number)
3757       (error (gnus-message 5 "Error updating the summary line")))
3758     (when (gnus-visual-p 'summary-highlight 'highlight)
3759       (forward-line -1)
3760       (gnus-summary-highlight-line)
3761       (gnus-run-hooks 'gnus-summary-update-hook)
3762       (forward-line 1))))
3763
3764 (defun gnus-summary-update-line (&optional dont-update)
3765   "Update summary line after change."
3766   (when (and gnus-summary-default-score
3767              (not gnus-summary-inhibit-highlight))
3768     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3769            (article (gnus-summary-article-number))
3770            (score (gnus-summary-article-score article)))
3771       (unless dont-update
3772         (if (and gnus-summary-mark-below
3773                  (< (gnus-summary-article-score)
3774                     gnus-summary-mark-below))
3775             ;; This article has a low score, so we mark it as read.
3776             (when (memq article gnus-newsgroup-unreads)
3777               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3778           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3779             ;; This article was previously marked as read on account
3780             ;; of a low score, but now it has risen, so we mark it as
3781             ;; unread.
3782             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3783         (gnus-summary-update-mark
3784          (if (or (null gnus-summary-default-score)
3785                  (<= (abs (- score gnus-summary-default-score))
3786                      gnus-summary-zcore-fuzz))
3787              ?                          ;Whitespace
3788            (if (< score gnus-summary-default-score)
3789                gnus-score-below-mark gnus-score-over-mark))
3790          'score))
3791       ;; Do visual highlighting.
3792       (when (gnus-visual-p 'summary-highlight 'highlight)
3793         (gnus-summary-highlight-line)
3794         (gnus-run-hooks 'gnus-summary-update-hook)))))
3795
3796 (defvar gnus-tmp-new-adopts nil)
3797
3798 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3799   "Return the number of articles in THREAD.
3800 This may be 0 in some cases -- if none of the articles in
3801 the thread are to be displayed."
3802   (let* ((number
3803          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3804           (cond
3805            ((not (listp thread))
3806             1)
3807            ((and (consp thread) (cdr thread))
3808             (apply
3809              '+ 1 (mapcar
3810                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3811            ((null thread)
3812             1)
3813            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3814             1)
3815            (t 0))))
3816     (when (and level (zerop level) gnus-tmp-new-adopts)
3817       (incf number
3818             (apply '+ (mapcar
3819                        'gnus-summary-number-of-articles-in-thread
3820                        gnus-tmp-new-adopts))))
3821     (if char
3822         (if (> number 1) gnus-not-empty-thread-mark
3823           gnus-empty-thread-mark)
3824       number)))
3825
3826 (defsubst gnus-summary-line-message-size (head)
3827   "Return pretty-printed version of message size.
3828 This function is intended to be used in
3829 `gnus-summary-line-format-alist'."
3830   (let ((c (or (mail-header-chars head) -1)))
3831     (cond ((< c 0) "n/a")               ; chars not available
3832           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3833           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3834           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3835           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3836
3837
3838 (defun gnus-summary-set-local-parameters (group)
3839   "Go through the local params of GROUP and set all variable specs in that list."
3840   (let ((vars '(quit-config)))          ; Ignore quit-config.
3841     (dolist (elem (gnus-group-find-parameter group))
3842       (and (consp elem)                 ; Has to be a cons.
3843            (consp (cdr elem))           ; The cdr has to be a list.
3844            (symbolp (car elem))         ; Has to be a symbol in there.
3845            (not (memq (car elem) vars))
3846            (ignore-errors
3847              (push (car elem) vars)
3848              ;; Variables like `gnus-show-threads' that are globally
3849              ;; bound, if used as group parameters, need to get to be
3850              ;; buffer-local, whereas just parameters like `gcc-self',
3851              ;; `timestamp', etc. should not be bound as variables.
3852              (if (boundp (car elem))
3853                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3854                (eval (nth 1 elem))))))))
3855
3856 (defun gnus-summary-read-group (group &optional show-all no-article
3857                                       kill-buffer no-display backward
3858                                       select-articles)
3859   "Start reading news in newsgroup GROUP.
3860 If SHOW-ALL is non-nil, already read articles are also listed.
3861 If NO-ARTICLE is non-nil, no article is selected initially.
3862 If NO-DISPLAY, don't generate a summary buffer."
3863   (let (result)
3864     (while (and group
3865                 (null (setq result
3866                             (let ((gnus-auto-select-next nil))
3867                               (or (gnus-summary-read-group-1
3868                                    group show-all no-article
3869                                    kill-buffer no-display
3870                                    select-articles)
3871                                   (setq show-all nil
3872                                         select-articles nil)))))
3873                 (eq gnus-auto-select-next 'quietly))
3874       (set-buffer gnus-group-buffer)
3875       ;; The entry function called above goes to the next
3876       ;; group automatically, so we go two groups back
3877       ;; if we are searching for the previous group.
3878       (when backward
3879         (gnus-group-prev-unread-group 2))
3880       (if (not (equal group (gnus-group-group-name)))
3881           (setq group (gnus-group-group-name))
3882         (setq group nil)))
3883     result))
3884
3885 (defun gnus-summary-read-group-1 (group show-all no-article
3886                                         kill-buffer no-display
3887                                         &optional select-articles)
3888   ;; Killed foreign groups can't be entered.
3889   ;;  (when (and (not (gnus-group-native-p group))
3890   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3891   ;;    (error "Dead non-native groups can't be entered"))
3892   (gnus-message 5 "Retrieving newsgroup: %s..."
3893                 (gnus-group-decoded-name group))
3894   (let* ((new-group (gnus-summary-setup-buffer group))
3895          (quit-config (gnus-group-quit-config group))
3896          (did-select (and new-group (gnus-select-newsgroup
3897                                      group show-all select-articles))))
3898     (cond
3899      ;; This summary buffer exists already, so we just select it.
3900      ((not new-group)
3901       (gnus-set-global-variables)
3902       (when kill-buffer
3903         (gnus-kill-or-deaden-summary kill-buffer))
3904       (gnus-configure-windows 'summary 'force)
3905       (gnus-set-mode-line 'summary)
3906       (gnus-summary-position-point)
3907       (message "")
3908       t)
3909      ;; We couldn't select this group.
3910      ((null did-select)
3911       (when (and (eq major-mode 'gnus-summary-mode)
3912                  (not (equal (current-buffer) kill-buffer)))
3913         (kill-buffer (current-buffer))
3914         (if (not quit-config)
3915             (progn
3916               ;; Update the info -- marks might need to be removed,
3917               ;; for instance.
3918               (gnus-summary-update-info)
3919               (set-buffer gnus-group-buffer)
3920               (gnus-group-jump-to-group group)
3921               (gnus-group-next-unread-group 1))
3922           (gnus-handle-ephemeral-exit quit-config)))
3923       (let ((grpinfo (gnus-get-info group)))
3924         (if (null (gnus-info-read grpinfo))
3925             (gnus-message 3 "Group %s contains no messages"
3926                           (gnus-group-decoded-name group))
3927           (gnus-message 3 "Can't select group")))
3928       nil)
3929      ;; The user did a `C-g' while prompting for number of articles,
3930      ;; so we exit this group.
3931      ((eq did-select 'quit)
3932       (and (eq major-mode 'gnus-summary-mode)
3933            (not (equal (current-buffer) kill-buffer))
3934            (kill-buffer (current-buffer)))
3935       (when kill-buffer
3936         (gnus-kill-or-deaden-summary kill-buffer))
3937       (if (not quit-config)
3938           (progn
3939             (set-buffer gnus-group-buffer)
3940             (gnus-group-jump-to-group group)
3941             (gnus-configure-windows 'group 'force))
3942         (gnus-handle-ephemeral-exit quit-config))
3943       ;; Finally signal the quit.
3944       (signal 'quit nil))
3945      ;; The group was successfully selected.
3946      (t
3947       (gnus-set-global-variables)
3948       ;; Save the active value in effect when the group was entered.
3949       (setq gnus-newsgroup-active
3950             (gnus-copy-sequence
3951              (gnus-active gnus-newsgroup-name)))
3952       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3953       ;; You can change the summary buffer in some way with this hook.
3954       (gnus-run-hooks 'gnus-select-group-hook)
3955       (when (memq 'summary (gnus-update-format-specifications
3956                             nil 'summary 'summary-mode 'summary-dummy))
3957         ;; The format specification for the summary line was updated,
3958         ;; so we need to update the mark positions as well.
3959         (gnus-update-summary-mark-positions))
3960       ;; Do score processing.
3961       (when gnus-use-scoring
3962         (gnus-possibly-score-headers))
3963       ;; Check whether to fill in the gaps in the threads.
3964       (when gnus-build-sparse-threads
3965         (gnus-build-sparse-threads))
3966       ;; Find the initial limit.
3967       (if show-all
3968           (let ((gnus-newsgroup-dormant nil))
3969             (gnus-summary-initial-limit show-all))
3970         (gnus-summary-initial-limit show-all))
3971       ;; Generate the summary buffer.
3972       (unless no-display
3973         (gnus-summary-prepare))
3974       (when gnus-use-trees
3975         (gnus-tree-open group)
3976         (setq gnus-summary-highlight-line-function
3977               'gnus-tree-highlight-article))
3978       ;; If the summary buffer is empty, but there are some low-scored
3979       ;; articles or some excluded dormants, we include these in the
3980       ;; buffer.
3981       (when (and (zerop (buffer-size))
3982                  (not no-display))
3983         (cond (gnus-newsgroup-dormant
3984                (gnus-summary-limit-include-dormant))
3985               ((and gnus-newsgroup-scored show-all)
3986                (gnus-summary-limit-include-expunged t))))
3987       ;; Function `gnus-apply-kill-file' must be called in this hook.
3988       (gnus-run-hooks 'gnus-apply-kill-hook)
3989       (if (and (zerop (buffer-size))
3990                (not no-display))
3991           (progn
3992             ;; This newsgroup is empty.
3993             (gnus-summary-catchup-and-exit nil t)
3994             (gnus-message 6 "No unread news")
3995             (when kill-buffer
3996               (gnus-kill-or-deaden-summary kill-buffer))
3997             ;; Return nil from this function.
3998             nil)
3999         ;; Hide conversation thread subtrees.  We cannot do this in
4000         ;; gnus-summary-prepare-hook since kill processing may not
4001         ;; work with hidden articles.
4002         (gnus-summary-maybe-hide-threads)
4003         (when kill-buffer
4004           (gnus-kill-or-deaden-summary kill-buffer))
4005         (gnus-summary-auto-select-subject)
4006         ;; Show first unread article if requested.
4007         (if (and (not no-article)
4008                  (not no-display)
4009                  gnus-newsgroup-unreads
4010                  gnus-auto-select-first)
4011             (progn
4012               (gnus-configure-windows 'summary)
4013               (let ((art (gnus-summary-article-number)))
4014                 (unless (and (not gnus-plugged)
4015                              (or (memq art gnus-newsgroup-undownloaded)
4016                                  (memq art gnus-newsgroup-downloadable)))
4017                   (gnus-summary-goto-article art))))
4018           ;; Don't select any articles.
4019           (gnus-summary-position-point)
4020           (gnus-configure-windows 'summary 'force)
4021           (gnus-set-mode-line 'summary))
4022         (when (and gnus-auto-center-group
4023                    (get-buffer-window gnus-group-buffer t))
4024           ;; Gotta use windows, because recenter does weird stuff if
4025           ;; the current buffer ain't the displayed window.
4026           (let ((owin (selected-window)))
4027             (select-window (get-buffer-window gnus-group-buffer t))
4028             (when (gnus-group-goto-group group)
4029               (recenter))
4030             (select-window owin)))
4031         ;; Mark this buffer as "prepared".
4032         (setq gnus-newsgroup-prepared t)
4033         (gnus-run-hooks 'gnus-summary-prepared-hook)
4034         (unless (gnus-ephemeral-group-p group)
4035           (gnus-group-update-group group))
4036         t)))))
4037
4038 (defun gnus-summary-auto-select-subject ()
4039   "Select the subject line on initial group entry."
4040   (goto-char (point-min))
4041   (cond
4042    ((eq gnus-auto-select-subject 'best)
4043     (gnus-summary-best-unread-subject))
4044    ((eq gnus-auto-select-subject 'unread)
4045     (gnus-summary-first-unread-subject))
4046    ((eq gnus-auto-select-subject 'unseen)
4047     (gnus-summary-first-unseen-subject))
4048    ((eq gnus-auto-select-subject 'unseen-or-unread)
4049     (gnus-summary-first-unseen-or-unread-subject))
4050    ((eq gnus-auto-select-subject 'first)
4051     ;; Do nothing.
4052     )
4053    ((functionp gnus-auto-select-subject)
4054     (funcall gnus-auto-select-subject))))
4055
4056 (defun gnus-summary-prepare ()
4057   "Generate the summary buffer."
4058   (interactive)
4059   (let ((inhibit-read-only t))
4060     (erase-buffer)
4061     (setq gnus-newsgroup-data nil
4062           gnus-newsgroup-data-reverse nil)
4063     (gnus-run-hooks 'gnus-summary-generate-hook)
4064     ;; Generate the buffer, either with threads or without.
4065     (when gnus-newsgroup-headers
4066       (gnus-summary-prepare-threads
4067        (if gnus-show-threads
4068            (gnus-sort-gathered-threads
4069             (funcall gnus-summary-thread-gathering-function
4070                      (gnus-sort-threads
4071                       (gnus-cut-threads (gnus-make-threads)))))
4072          ;; Unthreaded display.
4073          (gnus-sort-articles gnus-newsgroup-headers))))
4074     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4075     ;; Call hooks for modifying summary buffer.
4076     (goto-char (point-min))
4077     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4078
4079 (defsubst gnus-general-simplify-subject (subject)
4080   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4081   (setq subject
4082         (cond
4083          ;; Truncate the subject.
4084          (gnus-simplify-subject-functions
4085           (gnus-map-function gnus-simplify-subject-functions subject))
4086          ((numberp gnus-summary-gather-subject-limit)
4087           (setq subject (gnus-simplify-subject-re subject))
4088           (if (> (length subject) gnus-summary-gather-subject-limit)
4089               (substring subject 0 gnus-summary-gather-subject-limit)
4090             subject))
4091          ;; Fuzzily simplify it.
4092          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4093           (gnus-simplify-subject-fuzzy subject))
4094          ;; Just remove the leading "Re:".
4095          (t
4096           (gnus-simplify-subject-re subject))))
4097
4098   (if (and gnus-summary-gather-exclude-subject
4099            (string-match gnus-summary-gather-exclude-subject subject))
4100       nil                         ; This article shouldn't be gathered
4101     subject))
4102
4103 (defun gnus-summary-simplify-subject-query ()
4104   "Query where the respool algorithm would put this article."
4105   (interactive)
4106   (gnus-summary-select-article)
4107   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4108
4109 (defun gnus-gather-threads-by-subject (threads)
4110   "Gather threads by looking at Subject headers."
4111   (if (not gnus-summary-make-false-root)
4112       threads
4113     (let ((hashtb (gnus-make-hashtable 1024))
4114           (prev threads)
4115           (result threads)
4116           subject hthread whole-subject)
4117       (while threads
4118         (setq subject (gnus-general-simplify-subject
4119                        (setq whole-subject (mail-header-subject
4120                                             (caar threads)))))
4121         (when subject
4122           (if (setq hthread (gnus-gethash subject hashtb))
4123               (progn
4124                 ;; We enter a dummy root into the thread, if we
4125                 ;; haven't done that already.
4126                 (unless (stringp (caar hthread))
4127                   (setcar hthread (list whole-subject (car hthread))))
4128                 ;; We add this new gathered thread to this gathered
4129                 ;; thread.
4130                 (setcdr (car hthread)
4131                         (nconc (cdar hthread) (list (car threads))))
4132                 ;; Remove it from the list of threads.
4133                 (setcdr prev (cdr threads))
4134                 (setq threads prev))
4135             ;; Enter this thread into the hash table.
4136             (gnus-sethash subject
4137                           (if gnus-summary-make-false-root-always
4138                               (progn
4139                                 ;; If you want a dummy root above all
4140                                 ;; threads...
4141                                 (setcar threads (list whole-subject
4142                                                       (car threads)))
4143                                 threads)
4144                             threads)
4145                           hashtb)))
4146         (setq prev threads)
4147         (setq threads (cdr threads)))
4148       result)))
4149
4150 (defun gnus-gather-threads-by-references (threads)
4151   "Gather threads by looking at References headers."
4152   (let ((idhashtb (gnus-make-hashtable 1024))
4153         (thhashtb (gnus-make-hashtable 1024))
4154         (prev threads)
4155         (result threads)
4156         ids references id gthread gid entered ref)
4157     (while threads
4158       (when (setq references (mail-header-references (caar threads)))
4159         (setq id (mail-header-id (caar threads))
4160               ids (inline (gnus-split-references references))
4161               entered nil)
4162         (while (setq ref (pop ids))
4163           (setq ids (delete ref ids))
4164           (if (not (setq gid (gnus-gethash ref idhashtb)))
4165               (progn
4166                 (gnus-sethash ref id idhashtb)
4167                 (gnus-sethash id threads thhashtb))
4168             (setq gthread (gnus-gethash gid thhashtb))
4169             (unless entered
4170               ;; We enter a dummy root into the thread, if we
4171               ;; haven't done that already.
4172               (unless (stringp (caar gthread))
4173                 (setcar gthread (list (mail-header-subject (caar gthread))
4174                                       (car gthread))))
4175               ;; We add this new gathered thread to this gathered
4176               ;; thread.
4177               (setcdr (car gthread)
4178                       (nconc (cdar gthread) (list (car threads)))))
4179             ;; Add it into the thread hash table.
4180             (gnus-sethash id gthread thhashtb)
4181             (setq entered t)
4182             ;; Remove it from the list of threads.
4183             (setcdr prev (cdr threads))
4184             (setq threads prev))))
4185       (setq prev threads)
4186       (setq threads (cdr threads)))
4187     result))
4188
4189 (defun gnus-sort-gathered-threads (threads)
4190   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4191   (let ((result threads))
4192     (while threads
4193       (when (stringp (caar threads))
4194         (setcdr (car threads)
4195                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4196       (setq threads (cdr threads)))
4197     result))
4198
4199 (defun gnus-thread-loop-p (root thread)
4200   "Say whether ROOT is in THREAD."
4201   (let ((stack (list thread))
4202         (infloop 0)
4203         th)
4204     (while (setq thread (pop stack))
4205       (setq th (cdr thread))
4206       (while (and th
4207                   (not (eq (caar th) root)))
4208         (pop th))
4209       (if th
4210           ;; We have found a loop.
4211           (let (ref-dep)
4212             (setcdr thread (delq (car th) (cdr thread)))
4213             (if (boundp (setq ref-dep (intern "none"
4214                                               gnus-newsgroup-dependencies)))
4215                 (setcdr (symbol-value ref-dep)
4216                         (nconc (cdr (symbol-value ref-dep))
4217                                (list (car th))))
4218               (set ref-dep (list nil (car th))))
4219             (setq infloop 1
4220                   stack nil))
4221         ;; Push all the subthreads onto the stack.
4222         (push (cdr thread) stack)))
4223     infloop))
4224
4225 (defun gnus-make-threads ()
4226   "Go through the dependency hashtb and find the roots.  Return all threads."
4227   (let (threads)
4228     (while (catch 'infloop
4229              (mapatoms
4230               (lambda (refs)
4231                 ;; Deal with self-referencing References loops.
4232                 (when (and (car (symbol-value refs))
4233                            (not (zerop
4234                                  (apply
4235                                   '+
4236                                   (mapcar
4237                                    (lambda (thread)
4238                                      (gnus-thread-loop-p
4239                                       (car (symbol-value refs)) thread))
4240                                    (cdr (symbol-value refs)))))))
4241                   (setq threads nil)
4242                   (throw 'infloop t))
4243                 (unless (car (symbol-value refs))
4244                   ;; These threads do not refer back to any other
4245                   ;; articles, so they're roots.
4246                   (setq threads (append (cdr (symbol-value refs)) threads))))
4247               gnus-newsgroup-dependencies)))
4248     threads))
4249
4250 ;; Build the thread tree.
4251 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4252   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4253
4254 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4255 if it was already present.
4256
4257 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4258 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4259 Message-IDs will be renamed to a unique Message-ID before being
4260 entered.
4261
4262 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4263   (let* ((id (mail-header-id header))
4264          (id-dep (and id (intern id dependencies)))
4265          parent-id ref ref-dep ref-header replaced)
4266     ;; Enter this `header' in the `dependencies' table.
4267     (cond
4268      ((not id-dep)
4269       (setq header nil))
4270      ;; The first two cases do the normal part: enter a new `header'
4271      ;; in the `dependencies' table.
4272      ((not (boundp id-dep))
4273       (set id-dep (list header)))
4274      ((null (car (symbol-value id-dep)))
4275       (setcar (symbol-value id-dep) header))
4276
4277      ;; From here the `header' was already present in the
4278      ;; `dependencies' table.
4279      (force-new
4280       ;; Overrides an existing entry;
4281       ;; just set the header part of the entry.
4282       (setcar (symbol-value id-dep) header)
4283       (setq replaced t))
4284
4285      ;; Renames the existing `header' to a unique Message-ID.
4286      ((not gnus-summary-ignore-duplicates)
4287       ;; An article with this Message-ID has already been seen.
4288       ;; We rename the Message-ID.
4289       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4290            (list header))
4291       (mail-header-set-id header id))
4292
4293      ;; The last case ignores an existing entry, except it adds any
4294      ;; additional Xrefs (in case the two articles came from different
4295      ;; servers.
4296      ;; Also sets `header' to `nil' meaning that the `dependencies'
4297      ;; table was *not* modified.
4298      (t
4299       (mail-header-set-xref
4300        (car (symbol-value id-dep))
4301        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4302                    "")
4303                (or (mail-header-xref header) "")))
4304       (setq header nil)))
4305
4306     (when (and header (not replaced))
4307       ;; First check that we are not creating a References loop.
4308       (setq parent-id (gnus-parent-id (mail-header-references header)))
4309       (setq ref parent-id)
4310       (while (and ref
4311                   (setq ref-dep (intern-soft ref dependencies))
4312                   (boundp ref-dep)
4313                   (setq ref-header (car (symbol-value ref-dep))))
4314         (if (string= id ref)
4315             ;; Yuk!  This is a reference loop.  Make the article be a
4316             ;; root article.
4317             (progn
4318               (mail-header-set-references (car (symbol-value id-dep)) "none")
4319               (setq ref nil)
4320               (setq parent-id nil))
4321           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4322       (setq ref-dep (intern (or parent-id "none") dependencies))
4323       (if (boundp ref-dep)
4324           (setcdr (symbol-value ref-dep)
4325                   (nconc (cdr (symbol-value ref-dep))
4326                          (list (symbol-value id-dep))))
4327         (set ref-dep (list nil (symbol-value id-dep)))))
4328     header))
4329
4330 (defun gnus-extract-message-id-from-in-reply-to (string)
4331   (if (string-match "<[^>]+>" string)
4332       (substring string (match-beginning 0) (match-end 0))
4333     nil))
4334
4335 (defun gnus-build-sparse-threads ()
4336   (let ((headers gnus-newsgroup-headers)
4337         (mail-parse-charset gnus-newsgroup-charset)
4338         (gnus-summary-ignore-duplicates t)
4339         header references generation relations
4340         subject child end new-child date)
4341     ;; First we create an alist of generations/relations, where
4342     ;; generations is how much we trust the relation, and the relation
4343     ;; is parent/child.
4344     (gnus-message 7 "Making sparse threads...")
4345     (save-excursion
4346       (nnheader-set-temp-buffer " *gnus sparse threads*")
4347       (while (setq header (pop headers))
4348         (when (and (setq references (mail-header-references header))
4349                    (not (string= references "")))
4350           (insert references)
4351           (setq child (mail-header-id header)
4352                 subject (mail-header-subject header)
4353                 date (mail-header-date header)
4354                 generation 0)
4355           (while (search-backward ">" nil t)
4356             (setq end (1+ (point)))
4357             (when (search-backward "<" nil t)
4358               (setq new-child (buffer-substring (point) end))
4359               (push (list (incf generation)
4360                           child (setq child new-child)
4361                           subject date)
4362                     relations)))
4363           (when child
4364             (push (list (1+ generation) child nil subject) relations))
4365           (erase-buffer)))
4366       (kill-buffer (current-buffer)))
4367     ;; Sort over trustworthiness.
4368     (dolist (relation (sort relations 'car-less-than-car))
4369       (when (gnus-dependencies-add-header
4370              (make-full-mail-header
4371               gnus-reffed-article-number
4372               (nth 3 relation) "" (or (nth 4 relation) "")
4373               (nth 1 relation)
4374               (or (nth 2 relation) "") 0 0 "")
4375              gnus-newsgroup-dependencies nil)
4376         (push gnus-reffed-article-number gnus-newsgroup-limit)
4377         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4378         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4379               gnus-newsgroup-reads)
4380         (decf gnus-reffed-article-number)))
4381     (gnus-message 7 "Making sparse threads...done")))
4382
4383 (defun gnus-build-old-threads ()
4384   ;; Look at all the articles that refer back to old articles, and
4385   ;; fetch the headers for the articles that aren't there.  This will
4386   ;; build complete threads - if the roots haven't been expired by the
4387   ;; server, that is.
4388   (let ((mail-parse-charset gnus-newsgroup-charset)
4389         id heads)
4390     (mapatoms
4391      (lambda (refs)
4392        (when (not (car (symbol-value refs)))
4393          (setq heads (cdr (symbol-value refs)))
4394          (while heads
4395            (if (memq (mail-header-number (caar heads))
4396                      gnus-newsgroup-dormant)
4397                (setq heads (cdr heads))
4398              (setq id (symbol-name refs))
4399              (while (and (setq id (gnus-build-get-header id))
4400                          (not (car (gnus-id-to-thread id)))))
4401              (setq heads nil)))))
4402      gnus-newsgroup-dependencies)))
4403
4404 (defsubst gnus-remove-odd-characters (string)
4405   "Translate STRING into something that doesn't contain weird characters."
4406   (mm-subst-char-in-string
4407    ?\r ?\-
4408    (mm-subst-char-in-string ?\n ?\- string t) t))
4409
4410 ;; This function has to be called with point after the article number
4411 ;; on the beginning of the line.
4412 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4413   (let ((eol (point-at-eol))
4414         (buffer (current-buffer))
4415         header references in-reply-to)
4416
4417     ;; overview: [num subject from date id refs chars lines misc]
4418     (unwind-protect
4419         (let (x)
4420           (narrow-to-region (point) eol)
4421           (unless (eobp)
4422             (forward-char))
4423
4424           (setq header
4425                 (make-full-mail-header
4426                  number                 ; number
4427                  (condition-case ()     ; subject
4428                      (gnus-remove-odd-characters
4429                       (funcall gnus-decode-encoded-word-function
4430                                (setq x (nnheader-nov-field))))
4431                    (error x))
4432                  (condition-case ()     ; from
4433                      (gnus-remove-odd-characters
4434                       (funcall gnus-decode-encoded-address-function
4435                                (setq x (nnheader-nov-field))))
4436                    (error x))
4437                  (nnheader-nov-field)   ; date
4438                  (nnheader-nov-read-message-id number)  ; id
4439                  (setq references (nnheader-nov-field)) ; refs
4440                  (nnheader-nov-read-integer) ; chars
4441                  (nnheader-nov-read-integer) ; lines
4442                  (unless (eobp)
4443                    (if (looking-at "Xref: ")
4444                        (goto-char (match-end 0)))
4445                    (nnheader-nov-field)) ; Xref
4446                  (nnheader-nov-parse-extra)))) ; extra
4447
4448       (widen))
4449
4450     (when (and (string= references "")
4451                (setq in-reply-to (mail-header-extra header))
4452                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4453       (mail-header-set-references
4454        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4455
4456     (when gnus-alter-header-function
4457       (funcall gnus-alter-header-function header))
4458     (gnus-dependencies-add-header header dependencies force-new)))
4459
4460 (defun gnus-build-get-header (id)
4461   "Look through the buffer of NOV lines and find the header to ID.
4462 Enter this line into the dependencies hash table, and return
4463 the id of the parent article (if any)."
4464   (let ((deps gnus-newsgroup-dependencies)
4465         found header)
4466     (prog1
4467         (with-current-buffer nntp-server-buffer
4468           (let ((case-fold-search nil))
4469             (goto-char (point-min))
4470             (while (and (not found)
4471                         (search-forward id nil t))
4472               (beginning-of-line)
4473               (setq found (looking-at
4474                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4475                                    (regexp-quote id))))
4476               (or found (beginning-of-line 2)))
4477             (when found
4478               (beginning-of-line)
4479               (and
4480                (setq header (gnus-nov-parse-line
4481                              (read (current-buffer)) deps))
4482                (gnus-parent-id (mail-header-references header))))))
4483       (when header
4484         (let ((number (mail-header-number header)))
4485           (push number gnus-newsgroup-limit)
4486           (push header gnus-newsgroup-headers)
4487           (if (memq number gnus-newsgroup-unselected)
4488               (progn
4489                 (setq gnus-newsgroup-unreads
4490                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4491                                                number))
4492                 (setq gnus-newsgroup-unselected
4493                       (delq number gnus-newsgroup-unselected)))
4494             (push number gnus-newsgroup-ancient)))))))
4495
4496 (defun gnus-build-all-threads ()
4497   "Read all the headers."
4498   (let ((gnus-summary-ignore-duplicates t)
4499         (mail-parse-charset gnus-newsgroup-charset)
4500         (dependencies gnus-newsgroup-dependencies)
4501         header article)
4502     (with-current-buffer nntp-server-buffer
4503       (let ((case-fold-search nil))
4504         (goto-char (point-min))
4505         (while (not (eobp))
4506           (ignore-errors
4507             (setq article (read (current-buffer))
4508                   header (gnus-nov-parse-line article dependencies)))
4509           (when header
4510             (with-current-buffer gnus-summary-buffer
4511               (push header gnus-newsgroup-headers)
4512               (if (memq (setq article (mail-header-number header))
4513                         gnus-newsgroup-unselected)
4514                   (progn
4515                     (setq gnus-newsgroup-unreads
4516                           (gnus-add-to-sorted-list
4517                            gnus-newsgroup-unreads article))
4518                     (setq gnus-newsgroup-unselected
4519                           (delq article gnus-newsgroup-unselected)))
4520                 (push article gnus-newsgroup-ancient)))
4521             (forward-line 1)))))))
4522
4523 (defun gnus-summary-update-article-line (article header)
4524   "Update the line for ARTICLE using HEADER."
4525   (let* ((id (mail-header-id header))
4526          (thread (gnus-id-to-thread id)))
4527     (unless thread
4528       (error "Article in no thread"))
4529     ;; Update the thread.
4530     (setcar thread header)
4531     (gnus-summary-goto-subject article)
4532     (let* ((datal (gnus-data-find-list article))
4533            (data (car datal))
4534            (inhibit-read-only t)
4535            (level (gnus-summary-thread-level)))
4536       (gnus-delete-line)
4537       (let ((inserted (- (point)
4538                          (progn
4539                            (gnus-summary-insert-line
4540                             header level nil
4541                             (memq article gnus-newsgroup-undownloaded)
4542                             (gnus-article-mark article)
4543                             (memq article gnus-newsgroup-replied)
4544                             (memq article gnus-newsgroup-expirable)
4545                             ;; Only insert the Subject string when it's different
4546                             ;; from the previous Subject string.
4547                             (if (and
4548                                  gnus-show-threads
4549                                  (gnus-subject-equal
4550                                   (condition-case ()
4551                                       (mail-header-subject
4552                                        (gnus-data-header
4553                                         (cadr
4554                                          (gnus-data-find-list
4555                                           article
4556                                           (gnus-data-list t)))))
4557                                     ;; Error on the side of excessive subjects.
4558                                     (error ""))
4559                                   (mail-header-subject header)))
4560                                 ""
4561                               (mail-header-subject header))
4562                             nil (cdr (assq article gnus-newsgroup-scored))
4563                             (memq article gnus-newsgroup-processable))
4564                            (point)))))
4565         (when (cdr datal)
4566           (gnus-data-update-list
4567            (cdr datal)
4568            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4569
4570 (defun gnus-summary-update-article (article &optional iheader)
4571   "Update ARTICLE in the summary buffer."
4572   (set-buffer gnus-summary-buffer)
4573   (let* ((header (gnus-summary-article-header article))
4574          (id (mail-header-id header))
4575          (data (gnus-data-find article))
4576          (thread (gnus-id-to-thread id))
4577          (references (mail-header-references header))
4578          (parent
4579           (gnus-id-to-thread
4580            (or (gnus-parent-id
4581                 (when (and references
4582                            (not (equal "" references)))
4583                   references))
4584                "none")))
4585          (inhibit-read-only t)
4586          (old (car thread)))
4587     (when thread
4588       (unless iheader
4589         (setcar thread nil)
4590         (when parent
4591           (delq thread parent)))
4592       (if (gnus-summary-insert-subject id header)
4593           ;; Set the (possibly) new article number in the data structure.
4594           (gnus-data-set-number data (gnus-id-to-article id))
4595         (setcar thread old)
4596         nil))))
4597
4598 (defun gnus-rebuild-thread (id &optional line)
4599   "Rebuild the thread containing ID.
4600 If LINE, insert the rebuilt thread starting on line LINE."
4601   (let ((inhibit-read-only t)
4602         old-pos current thread data)
4603     (if (not gnus-show-threads)
4604         (setq thread (list (car (gnus-id-to-thread id))))
4605       ;; Get the thread this article is part of.
4606       (setq thread (gnus-remove-thread id)))
4607     (setq old-pos (point-at-bol))
4608     (setq current (save-excursion
4609                     (and (re-search-backward "[\r\n]" nil t)
4610                          (gnus-summary-article-number))))
4611     ;; If this is a gathered thread, we have to go some re-gathering.
4612     (when (stringp (car thread))
4613       (let ((subject (car thread))
4614             roots thr)
4615         (setq thread (cdr thread))
4616         (while thread
4617           (unless (memq (setq thr (gnus-id-to-thread
4618                                    (gnus-root-id
4619                                     (mail-header-id (caar thread)))))
4620                         roots)
4621             (push thr roots))
4622           (setq thread (cdr thread)))
4623         ;; We now have all (unique) roots.
4624         (if (= (length roots) 1)
4625             ;; All the loose roots are now one solid root.
4626             (setq thread (car roots))
4627           (setq thread (cons subject (gnus-sort-threads roots))))))
4628     (let (threads)
4629       ;; We then insert this thread into the summary buffer.
4630       (when line
4631         (goto-char (point-min))
4632         (forward-line (1- line)))
4633       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4634         (if gnus-show-threads
4635             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4636           (gnus-summary-prepare-unthreaded thread))
4637         (setq data (nreverse gnus-newsgroup-data))
4638         (setq threads gnus-newsgroup-threads))
4639       ;; We splice the new data into the data structure.
4640       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4641       ;;!!! then we want to insert at the beginning of the buffer.
4642       ;;!!! That happens to be true with Gnus now, but that may
4643       ;;!!! change in the future.  Perhaps.
4644       (gnus-data-enter-list
4645        (if line nil current) data (- (point) old-pos))
4646       (setq gnus-newsgroup-threads
4647             (nconc threads gnus-newsgroup-threads))
4648       (gnus-data-compute-positions))))
4649
4650 (defun gnus-number-to-header (number)
4651   "Return the header for article NUMBER."
4652   (let ((headers gnus-newsgroup-headers))
4653     (while (and headers
4654                 (not (= number (mail-header-number (car headers)))))
4655       (pop headers))
4656     (when headers
4657       (car headers))))
4658
4659 (defun gnus-parent-headers (in-headers &optional generation)
4660   "Return the headers of the GENERATIONeth parent of HEADERS."
4661   (unless generation
4662     (setq generation 1))
4663   (let ((parent t)
4664         (headers in-headers)
4665         references)
4666     (while (and parent
4667                 (not (zerop generation))
4668                 (setq references (mail-header-references headers)))
4669       (setq headers (if (and references
4670                              (setq parent (gnus-parent-id references)))
4671                         (car (gnus-id-to-thread parent))
4672                       nil))
4673       (decf generation))
4674     (and (not (eq headers in-headers))
4675          headers)))
4676
4677 (defun gnus-id-to-thread (id)
4678   "Return the (sub-)thread where ID appears."
4679   (gnus-gethash id gnus-newsgroup-dependencies))
4680
4681 (defun gnus-id-to-article (id)
4682   "Return the article number of ID."
4683   (let ((thread (gnus-id-to-thread id)))
4684     (when (and thread
4685                (car thread))
4686       (mail-header-number (car thread)))))
4687
4688 (defun gnus-id-to-header (id)
4689   "Return the article headers of ID."
4690   (car (gnus-id-to-thread id)))
4691
4692 (defun gnus-article-displayed-root-p (article)
4693   "Say whether ARTICLE is a root(ish) article."
4694   (let ((level (gnus-summary-thread-level article))
4695         (refs (mail-header-references  (gnus-summary-article-header article)))
4696         particle)
4697     (cond
4698      ((null level) nil)
4699      ((zerop level) t)
4700      ((null refs) t)
4701      ((null (gnus-parent-id refs)) t)
4702      ((and (= 1 level)
4703            (null (setq particle (gnus-id-to-article
4704                                  (gnus-parent-id refs))))
4705            (null (gnus-summary-thread-level particle)))))))
4706
4707 (defun gnus-root-id (id)
4708   "Return the id of the root of the thread where ID appears."
4709   (let (last-id prev)
4710     (while (and id (setq prev (car (gnus-id-to-thread id))))
4711       (setq last-id id
4712             id (gnus-parent-id (mail-header-references prev))))
4713     last-id))
4714
4715 (defun gnus-articles-in-thread (thread)
4716   "Return the list of articles in THREAD."
4717   (cons (mail-header-number (car thread))
4718         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4719
4720 (defun gnus-remove-thread (id &optional dont-remove)
4721   "Remove the thread that has ID in it."
4722   (let (headers thread last-id)
4723     ;; First go up in this thread until we find the root.
4724     (setq last-id (gnus-root-id id)
4725           headers (message-flatten-list (gnus-id-to-thread last-id)))
4726     ;; We have now found the real root of this thread.  It might have
4727     ;; been gathered into some loose thread, so we have to search
4728     ;; through the threads to find the thread we wanted.
4729     (let ((threads gnus-newsgroup-threads)
4730           sub)
4731       (while threads
4732         (setq sub (car threads))
4733         (if (stringp (car sub))
4734             ;; This is a gathered thread, so we look at the roots
4735             ;; below it to find whether this article is in this
4736             ;; gathered root.
4737             (progn
4738               (setq sub (cdr sub))
4739               (while sub
4740                 (when (member (caar sub) headers)
4741                   (setq thread (car threads)
4742                         threads nil
4743                         sub nil))
4744                 (setq sub (cdr sub))))
4745           ;; It's an ordinary thread, so we check it.
4746           (when (eq (car sub) (car headers))
4747             (setq thread sub
4748                   threads nil)))
4749         (setq threads (cdr threads)))
4750       ;; If this article is in no thread, then it's a root.
4751       (if thread
4752           (unless dont-remove
4753             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4754         (setq thread (gnus-id-to-thread last-id)))
4755       (when thread
4756         (prog1
4757             thread                      ; We return this thread.
4758           (unless dont-remove
4759             (if (stringp (car thread))
4760                 (progn
4761                   ;; If we use dummy roots, then we have to remove the
4762                   ;; dummy root as well.
4763                   (when (eq gnus-summary-make-false-root 'dummy)
4764                     ;; We go to the dummy root by going to
4765                     ;; the first sub-"thread", and then one line up.
4766                     (gnus-summary-goto-article
4767                      (mail-header-number (caadr thread)))
4768                     (forward-line -1)
4769                     (gnus-delete-line)
4770                     (gnus-data-compute-positions))
4771                   (setq thread (cdr thread))
4772                   (while thread
4773                     (gnus-remove-thread-1 (car thread))
4774                     (setq thread (cdr thread))))
4775               (gnus-remove-thread-1 thread))))))))
4776
4777 (defun gnus-remove-thread-1 (thread)
4778   "Remove the thread THREAD recursively."
4779   (let ((number (mail-header-number (pop thread)))
4780         d)
4781     (setq thread (reverse thread))
4782     (while thread
4783       (gnus-remove-thread-1 (pop thread)))
4784     (when (setq d (gnus-data-find number))
4785       (goto-char (gnus-data-pos d))
4786       (gnus-summary-show-thread)
4787       (gnus-data-remove
4788        number
4789        (- (point-at-bol)
4790           (prog1
4791               (1+ (point-at-eol))
4792             (gnus-delete-line)))))))
4793
4794 (defun gnus-sort-threads-recursive (threads func)
4795   (sort (mapcar (lambda (thread)
4796                   (cons (car thread)
4797                         (and (cdr thread)
4798                              (gnus-sort-threads-recursive (cdr thread) func))))
4799                 threads) func))
4800
4801 (defun gnus-sort-threads-loop (threads func)
4802   (let* ((superthread (cons nil threads))
4803          (stack (list (cons superthread threads)))
4804          remaining-threads thread)
4805     (while stack
4806       (setq remaining-threads (cdr (car stack)))
4807       (if remaining-threads
4808           (progn (setq thread (car remaining-threads))
4809                  (setcdr (car stack) (cdr remaining-threads))
4810                  (if (cdr thread)
4811                      (push (cons thread (cdr thread)) stack)))
4812         (setq thread (caar stack))
4813         (setcdr thread (sort (cdr thread) func))
4814         (pop stack)))
4815     (cdr superthread)))
4816
4817 (defun gnus-sort-threads (threads)
4818   "Sort THREADS."
4819   (if (not gnus-thread-sort-functions)
4820       threads
4821     (gnus-message 8 "Sorting threads...")
4822     (prog1
4823         (condition-case nil
4824             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4825               (gnus-sort-threads-recursive
4826                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4827           ;; Even after binding max-lisp-eval-depth, the recursive
4828           ;; sorter might fail for very long threads.  In that case,
4829           ;; try using a (less well-tested) non-recursive sorter.
4830           (error (gnus-message 9 "Sorting threads with loop...")
4831                  (gnus-sort-threads-loop
4832                   threads (gnus-make-sort-function
4833                            gnus-thread-sort-functions))))
4834       (gnus-message 8 "Sorting threads...done"))))
4835
4836 (defun gnus-sort-articles (articles)
4837   "Sort ARTICLES."
4838   (when gnus-article-sort-functions
4839     (gnus-message 7 "Sorting articles...")
4840     (prog1
4841         (setq gnus-newsgroup-headers
4842               (sort articles (gnus-make-sort-function
4843                               gnus-article-sort-functions)))
4844       (gnus-message 7 "Sorting articles...done"))))
4845
4846 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4847 (defmacro gnus-thread-header (thread)
4848   "Return header of first article in THREAD.
4849 Note that THREAD must never, ever be anything else than a variable -
4850 using some other form will lead to serious barfage."
4851   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4852   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4853   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4854         (vector thread) 2))
4855
4856 (defsubst gnus-article-sort-by-number (h1 h2)
4857   "Sort articles by article number."
4858   (< (mail-header-number h1)
4859      (mail-header-number h2)))
4860
4861 (defun gnus-thread-sort-by-number (h1 h2)
4862   "Sort threads by root article number."
4863   (gnus-article-sort-by-number
4864    (gnus-thread-header h1) (gnus-thread-header h2)))
4865
4866 (defsubst gnus-article-sort-by-random (h1 h2)
4867   "Sort articles randomly."
4868   (zerop (random 2)))
4869
4870 (defun gnus-thread-sort-by-random (h1 h2)
4871   "Sort threads randomly."
4872   (gnus-article-sort-by-random
4873    (gnus-thread-header h1) (gnus-thread-header h2)))
4874
4875 (defsubst gnus-article-sort-by-lines (h1 h2)
4876   "Sort articles by article Lines header."
4877   (< (mail-header-lines h1)
4878      (mail-header-lines h2)))
4879
4880 (defun gnus-thread-sort-by-lines (h1 h2)
4881   "Sort threads by root article Lines header."
4882   (gnus-article-sort-by-lines
4883    (gnus-thread-header h1) (gnus-thread-header h2)))
4884
4885 (defsubst gnus-article-sort-by-chars (h1 h2)
4886   "Sort articles by octet length."
4887   (< (mail-header-chars h1)
4888      (mail-header-chars h2)))
4889
4890 (defun gnus-thread-sort-by-chars (h1 h2)
4891   "Sort threads by root article octet length."
4892   (gnus-article-sort-by-chars
4893    (gnus-thread-header h1) (gnus-thread-header h2)))
4894
4895 (defsubst gnus-article-sort-by-author (h1 h2)
4896   "Sort articles by root author."
4897   (gnus-string<
4898    (let ((extract (funcall
4899                    gnus-extract-address-components
4900                    (mail-header-from h1))))
4901      (or (car extract) (cadr extract) ""))
4902    (let ((extract (funcall
4903                    gnus-extract-address-components
4904                    (mail-header-from h2))))
4905      (or (car extract) (cadr extract) ""))))
4906
4907 (defun gnus-thread-sort-by-author (h1 h2)
4908   "Sort threads by root author."
4909   (gnus-article-sort-by-author
4910    (gnus-thread-header h1)  (gnus-thread-header h2)))
4911
4912 (defsubst gnus-article-sort-by-recipient (h1 h2)
4913   "Sort articles by recipient."
4914   (gnus-string<
4915    (let ((extract (funcall
4916                    gnus-extract-address-components
4917                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4918      (or (car extract) (cadr extract)))
4919    (let ((extract (funcall
4920                    gnus-extract-address-components
4921                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4922      (or (car extract) (cadr extract)))))
4923
4924 (defun gnus-thread-sort-by-recipient (h1 h2)
4925   "Sort threads by root recipient."
4926   (gnus-article-sort-by-recipient
4927    (gnus-thread-header h1) (gnus-thread-header h2)))
4928
4929 (defsubst gnus-article-sort-by-subject (h1 h2)
4930   "Sort articles by root subject."
4931   (gnus-string<
4932    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4933    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4934
4935 (defun gnus-thread-sort-by-subject (h1 h2)
4936   "Sort threads by root subject."
4937   (gnus-article-sort-by-subject
4938    (gnus-thread-header h1) (gnus-thread-header h2)))
4939
4940 (defsubst gnus-article-sort-by-date (h1 h2)
4941   "Sort articles by root article date."
4942   (time-less-p
4943    (gnus-date-get-time (mail-header-date h1))
4944    (gnus-date-get-time (mail-header-date h2))))
4945
4946 (defun gnus-thread-sort-by-date (h1 h2)
4947   "Sort threads by root article date."
4948   (gnus-article-sort-by-date
4949    (gnus-thread-header h1) (gnus-thread-header h2)))
4950
4951 (defsubst gnus-article-sort-by-score (h1 h2)
4952   "Sort articles by root article score.
4953 Unscored articles will be counted as having a score of zero."
4954   (> (or (cdr (assq (mail-header-number h1)
4955                     gnus-newsgroup-scored))
4956          gnus-summary-default-score 0)
4957      (or (cdr (assq (mail-header-number h2)
4958                     gnus-newsgroup-scored))
4959          gnus-summary-default-score 0)))
4960
4961 (defun gnus-thread-sort-by-score (h1 h2)
4962   "Sort threads by root article score."
4963   (gnus-article-sort-by-score
4964    (gnus-thread-header h1) (gnus-thread-header h2)))
4965
4966 (defun gnus-thread-sort-by-total-score (h1 h2)
4967   "Sort threads by the sum of all scores in the thread.
4968 Unscored articles will be counted as having a score of zero."
4969   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4970
4971 (defun gnus-thread-total-score (thread)
4972   ;; This function find the total score of THREAD.
4973   (cond
4974    ((null thread)
4975     0)
4976    ((consp thread)
4977     (if (stringp (car thread))
4978         (apply gnus-thread-score-function 0
4979                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4980       (gnus-thread-total-score-1 thread)))
4981    (t
4982     (gnus-thread-total-score-1 (list thread)))))
4983
4984 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4985   "Sort threads such that the thread with the most recently arrived article comes first."
4986   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4987
4988 (defun gnus-thread-highest-number (thread)
4989   "Return the highest article number in THREAD."
4990   (apply 'max (mapcar (lambda (header)
4991                         (mail-header-number header))
4992                       (message-flatten-list thread))))
4993
4994 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4995   "Sort threads such that the thread with the most recently dated article comes first."
4996   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4997
4998 ; Since this is called not only to sort the top-level threads, but
4999 ; also in recursive sorts to order the articles within a thread, each
5000 ; article will be processed many times.  Thus it speeds things up
5001 ; quite a bit to use gnus-date-get-time, which caches the time value.
5002 (defun gnus-thread-latest-date (thread)
5003   "Return the highest article date in THREAD."
5004   (apply 'max
5005          (mapcar (lambda (header) (gnus-float-time
5006                                    (gnus-date-get-time
5007                                     (mail-header-date header))))
5008                  (message-flatten-list thread))))
5009
5010 (defun gnus-thread-total-score-1 (root)
5011   ;; This function find the total score of the thread below ROOT.
5012   (setq root (car root))
5013   (apply gnus-thread-score-function
5014          (or (append
5015               (mapcar 'gnus-thread-total-score
5016                       (cdr (gnus-id-to-thread (mail-header-id root))))
5017               (when (> (mail-header-number root) 0)
5018                 (list (or (cdr (assq (mail-header-number root)
5019                                      gnus-newsgroup-scored))
5020                           gnus-summary-default-score 0))))
5021              (list gnus-summary-default-score)
5022              '(0))))
5023
5024 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5025 (defvar gnus-tmp-prev-subject nil)
5026 (defvar gnus-tmp-false-parent nil)
5027 (defvar gnus-tmp-root-expunged nil)
5028 (defvar gnus-tmp-dummy-line nil)
5029
5030 (defun gnus-extra-header (type &optional header)
5031   "Return the extra header of TYPE."
5032   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5033       ""))
5034
5035 (defvar gnus-tmp-thread-tree-header-string "")
5036
5037 (defcustom gnus-sum-thread-tree-root "> "
5038   "With %B spec, used for the root of a thread.
5039 If nil, use subject instead."
5040   :version "22.1"
5041   :type '(radio (const :format "%v  " nil) string)
5042   :group 'gnus-thread)
5043
5044 (defcustom gnus-sum-thread-tree-false-root "> "
5045   "With %B spec, used for a false root of a thread.
5046 If nil, use subject instead."
5047   :version "22.1"
5048   :type '(radio (const :format "%v  " nil) string)
5049   :group 'gnus-thread)
5050
5051 (defcustom gnus-sum-thread-tree-single-indent ""
5052   "With %B spec, used for a thread with just one message.
5053 If nil, use subject instead."
5054   :version "22.1"
5055   :type '(radio (const :format "%v  " nil) string)
5056   :group 'gnus-thread)
5057
5058 (defcustom gnus-sum-thread-tree-vertical "| "
5059   "With %B spec, used for drawing a vertical line."
5060   :version "22.1"
5061   :type 'string
5062   :group 'gnus-thread)
5063
5064 (defcustom gnus-sum-thread-tree-indent "  "
5065   "With %B spec, used for indenting."
5066   :version "22.1"
5067   :type 'string
5068   :group 'gnus-thread)
5069
5070 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5071   "With %B spec, used for a leaf with brothers."
5072   :version "22.1"
5073   :type 'string
5074   :group 'gnus-thread)
5075
5076 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5077   "With %B spec, used for a leaf without brothers."
5078   :version "22.1"
5079   :type 'string
5080   :group 'gnus-thread)
5081
5082 (defcustom gnus-summary-display-while-building nil
5083   "If non-nil, show and update the summary buffer as it's being built.
5084 If the value is t, update the buffer after every line is inserted.  If
5085 the value is an integer (N), update the display every N lines."
5086   :version "22.1"
5087   :group 'gnus-thread
5088   :type '(choice (const :tag "off" nil)
5089                  number
5090                  (const :tag "frequently" t)))
5091
5092 (defun gnus-summary-prepare-threads (threads)
5093   "Prepare summary buffer from THREADS and indentation LEVEL.
5094 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5095 or a straight list of headers."
5096   (gnus-message 7 "Generating summary...")
5097
5098   (setq gnus-newsgroup-threads threads)
5099   (beginning-of-line)
5100
5101   (let ((gnus-tmp-level 0)
5102         (default-score (or gnus-summary-default-score 0))
5103         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5104         (building-line-count gnus-summary-display-while-building)
5105         (building-count (integerp gnus-summary-display-while-building))
5106         thread number subject stack state gnus-tmp-gathered beg-match
5107         new-roots gnus-tmp-new-adopts thread-end simp-subject
5108         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5109         gnus-tmp-replied gnus-tmp-subject-or-nil
5110         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5111         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5112         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5113         tree-stack)
5114
5115     (setq gnus-tmp-prev-subject nil
5116           gnus-tmp-thread-tree-header-string "")
5117
5118     (if (vectorp (car threads))
5119         ;; If this is a straight (sic) list of headers, then a
5120         ;; threaded summary display isn't required, so we just create
5121         ;; an unthreaded one.
5122         (gnus-summary-prepare-unthreaded threads)
5123
5124       ;; Do the threaded display.
5125
5126       (if gnus-summary-display-while-building
5127           (switch-to-buffer (buffer-name)))
5128       (while (or threads stack gnus-tmp-new-adopts new-roots)
5129
5130         (if (and (= gnus-tmp-level 0)
5131                  (or (not stack)
5132                      (= (caar stack) 0))
5133                  (not gnus-tmp-false-parent)
5134                  (or gnus-tmp-new-adopts new-roots))
5135             (if gnus-tmp-new-adopts
5136                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5137                       thread (list (car gnus-tmp-new-adopts))
5138                       gnus-tmp-header (caar thread)
5139                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5140               (when new-roots
5141                 (setq thread (list (car new-roots))
5142                       gnus-tmp-header (caar thread)
5143                       new-roots (cdr new-roots))))
5144
5145           (if threads
5146               ;; If there are some threads, we do them before the
5147               ;; threads on the stack.
5148               (setq thread threads
5149                     gnus-tmp-header (caar thread))
5150             ;; There were no current threads, so we pop something off
5151             ;; the stack.
5152             (setq state (car stack)
5153                   gnus-tmp-level (car state)
5154                   tree-stack (cadr state)
5155                   thread (caddr state)
5156                   stack (cdr stack)
5157                   gnus-tmp-header (caar thread))))
5158
5159         (setq gnus-tmp-false-parent nil)
5160         (setq gnus-tmp-root-expunged nil)
5161         (setq thread-end nil)
5162
5163         (if (stringp gnus-tmp-header)
5164             ;; The header is a dummy root.
5165             (cond
5166              ((eq gnus-summary-make-false-root 'adopt)
5167               ;; We let the first article adopt the rest.
5168               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5169                                                (cddar thread)))
5170               (setq gnus-tmp-gathered
5171                     (nconc (mapcar
5172                             (lambda (h) (mail-header-number (car h)))
5173                             (cddar thread))
5174                            gnus-tmp-gathered))
5175               (setq thread (cons (list (caar thread)
5176                                        (cadar thread))
5177                                  (cdr thread)))
5178               (setq gnus-tmp-level -1
5179                     gnus-tmp-false-parent t))
5180              ((eq gnus-summary-make-false-root 'empty)
5181               ;; We print adopted articles with empty subject fields.
5182               (setq gnus-tmp-gathered
5183                     (nconc (mapcar
5184                             (lambda (h) (mail-header-number (car h)))
5185                             (cddar thread))
5186                            gnus-tmp-gathered))
5187               (setq gnus-tmp-level -1))
5188              ((eq gnus-summary-make-false-root 'dummy)
5189               ;; We remember that we probably want to output a dummy
5190               ;; root.
5191               (setq gnus-tmp-dummy-line gnus-tmp-header)
5192               (setq gnus-tmp-prev-subject gnus-tmp-header))
5193              (t
5194               ;; We do not make a root for the gathered
5195               ;; sub-threads at all.
5196               (setq gnus-tmp-level -1)))
5197
5198           (setq number (mail-header-number gnus-tmp-header)
5199                 subject (mail-header-subject gnus-tmp-header)
5200                 simp-subject (gnus-simplify-subject-fully subject))
5201
5202           (cond
5203            ;; If the thread has changed subject, we might want to make
5204            ;; this subthread into a root.
5205            ((and (null gnus-thread-ignore-subject)
5206                  (not (zerop gnus-tmp-level))
5207                  gnus-tmp-prev-subject
5208                  (not (string= gnus-tmp-prev-subject simp-subject)))
5209             (setq new-roots (nconc new-roots (list (car thread)))
5210                   thread-end t
5211                   gnus-tmp-header nil))
5212            ;; If the article lies outside the current limit,
5213            ;; then we do not display it.
5214            ((not (memq number gnus-newsgroup-limit))
5215             (setq gnus-tmp-gathered
5216                   (nconc (mapcar
5217                           (lambda (h) (mail-header-number (car h)))
5218                           (cdar thread))
5219                          gnus-tmp-gathered))
5220             (setq gnus-tmp-new-adopts (if (cdar thread)
5221                                           (append gnus-tmp-new-adopts
5222                                                   (cdar thread))
5223                                         gnus-tmp-new-adopts)
5224                   thread-end t
5225                   gnus-tmp-header nil)
5226             (when (zerop gnus-tmp-level)
5227               (setq gnus-tmp-root-expunged t)))
5228            ;; Perhaps this article is to be marked as read?
5229            ((and gnus-summary-mark-below
5230                  (< (or (cdr (assq number gnus-newsgroup-scored))
5231                         default-score)
5232                     gnus-summary-mark-below)
5233                  ;; Don't touch sparse articles.
5234                  (not (gnus-summary-article-sparse-p number))
5235                  (not (gnus-summary-article-ancient-p number)))
5236             (setq gnus-newsgroup-unreads
5237                   (delq number gnus-newsgroup-unreads))
5238             (if gnus-newsgroup-auto-expire
5239                 (setq gnus-newsgroup-expirable
5240                       (gnus-add-to-sorted-list
5241                        gnus-newsgroup-expirable number))
5242               (push (cons number gnus-low-score-mark)
5243                     gnus-newsgroup-reads))))
5244
5245           (when gnus-tmp-header
5246             ;; We may have an old dummy line to output before this
5247             ;; article.
5248             (when (and gnus-tmp-dummy-line
5249                        (gnus-subject-equal
5250                         gnus-tmp-dummy-line
5251                         (mail-header-subject gnus-tmp-header)))
5252               (gnus-summary-insert-dummy-line
5253                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5254               (setq gnus-tmp-dummy-line nil))
5255
5256             ;; Compute the mark.
5257             (setq gnus-tmp-unread (gnus-article-mark number))
5258
5259             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5260                                   gnus-tmp-header gnus-tmp-level)
5261                   gnus-newsgroup-data)
5262
5263             ;; Actually insert the line.
5264             (setq
5265              gnus-tmp-subject-or-nil
5266              (cond
5267               ((and gnus-thread-ignore-subject
5268                     gnus-tmp-prev-subject
5269                     (not (string= gnus-tmp-prev-subject simp-subject)))
5270                subject)
5271               ((zerop gnus-tmp-level)
5272                (if (and (eq gnus-summary-make-false-root 'empty)
5273                         (memq number gnus-tmp-gathered)
5274                         gnus-tmp-prev-subject
5275                         (string= gnus-tmp-prev-subject simp-subject))
5276                    gnus-summary-same-subject
5277                  subject))
5278               (t gnus-summary-same-subject)))
5279             (if (and (eq gnus-summary-make-false-root 'adopt)
5280                      (= gnus-tmp-level 1)
5281                      (memq number gnus-tmp-gathered))
5282                 (setq gnus-tmp-opening-bracket ?\<
5283                       gnus-tmp-closing-bracket ?\>)
5284               (setq gnus-tmp-opening-bracket ?\[
5285                     gnus-tmp-closing-bracket ?\]))
5286             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5287                 (gnus-make-thread-indent-array
5288                  (max (* 2 (length gnus-thread-indent-array))
5289                       gnus-tmp-level)))
5290             (setq
5291              gnus-tmp-indentation
5292              (aref gnus-thread-indent-array gnus-tmp-level)
5293              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5294              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5295                                 gnus-summary-default-score 0)
5296              gnus-tmp-score-char
5297              (if (or (null gnus-summary-default-score)
5298                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5299                          gnus-summary-zcore-fuzz))
5300                  ?                      ;Whitespace
5301                (if (< gnus-tmp-score gnus-summary-default-score)
5302                    gnus-score-below-mark gnus-score-over-mark))
5303              gnus-tmp-replied
5304              (cond ((memq number gnus-newsgroup-processable)
5305                     gnus-process-mark)
5306                    ((memq number gnus-newsgroup-cached)
5307                     gnus-cached-mark)
5308                    ((memq number gnus-newsgroup-replied)
5309                     gnus-replied-mark)
5310                    ((memq number gnus-newsgroup-forwarded)
5311                     gnus-forwarded-mark)
5312                    ((memq number gnus-newsgroup-saved)
5313                     gnus-saved-mark)
5314                    ((memq number gnus-newsgroup-recent)
5315                     gnus-recent-mark)
5316                    ((memq number gnus-newsgroup-unseen)
5317                     gnus-unseen-mark)
5318                    (t gnus-no-mark))
5319              gnus-tmp-downloaded
5320              (cond ((memq number gnus-newsgroup-undownloaded)
5321                     gnus-undownloaded-mark)
5322                    (gnus-newsgroup-agentized
5323                     gnus-downloaded-mark)
5324                    (t
5325                     gnus-no-mark))
5326              gnus-tmp-from (mail-header-from gnus-tmp-header)
5327              gnus-tmp-name
5328              (cond
5329               ((string-match "<[^>]+> *$" gnus-tmp-from)
5330                (setq beg-match (match-beginning 0))
5331                (or (and (string-match "^\".+\"" gnus-tmp-from)
5332                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5333                    (substring gnus-tmp-from 0 beg-match)))
5334               ((string-match "(.+)" gnus-tmp-from)
5335                (substring gnus-tmp-from
5336                           (1+ (match-beginning 0)) (1- (match-end 0))))
5337               (t gnus-tmp-from))
5338
5339              ;; Do the %B string
5340              gnus-tmp-thread-tree-header-string
5341              (cond
5342               ((not gnus-show-threads) "")
5343               ((zerop gnus-tmp-level)
5344                (cond ((cdar thread)
5345                       (or gnus-sum-thread-tree-root subject))
5346                      (gnus-tmp-new-adopts
5347                       (or gnus-sum-thread-tree-false-root subject))
5348                      (t
5349                       (or gnus-sum-thread-tree-single-indent subject))))
5350               (t
5351                (concat (apply 'concat
5352                               (mapcar (lambda (item)
5353                                         (if (= item 1)
5354                                             gnus-sum-thread-tree-vertical
5355                                           gnus-sum-thread-tree-indent))
5356                                       (cdr (reverse tree-stack))))
5357                        (if (nth 1 thread)
5358                            gnus-sum-thread-tree-leaf-with-other
5359                          gnus-sum-thread-tree-single-leaf)))))
5360             (when (string= gnus-tmp-name "")
5361               (setq gnus-tmp-name gnus-tmp-from))
5362             (unless (numberp gnus-tmp-lines)
5363               (setq gnus-tmp-lines -1))
5364             (if (= gnus-tmp-lines -1)
5365                 (setq gnus-tmp-lines "?")
5366               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5367             (gnus-put-text-property
5368              (point)
5369              (progn (eval gnus-summary-line-format-spec) (point))
5370              'gnus-number number)
5371             (when gnus-visual-p
5372               (forward-line -1)
5373               (gnus-summary-highlight-line)
5374               (when gnus-summary-update-hook
5375                 (gnus-run-hooks 'gnus-summary-update-hook))
5376               (forward-line 1))
5377
5378             (setq gnus-tmp-prev-subject simp-subject)))
5379
5380         (when (nth 1 thread)
5381           (push (list (max 0 gnus-tmp-level)
5382                       (copy-sequence tree-stack)
5383                       (nthcdr 1 thread))
5384                 stack))
5385         (push (if (nth 1 thread) 1 0) tree-stack)
5386         (incf gnus-tmp-level)
5387         (setq threads (if thread-end nil (cdar thread)))
5388         (if gnus-summary-display-while-building
5389             (if building-count
5390                 (progn
5391                   ;; use a set frequency
5392                   (setq building-line-count (1- building-line-count))
5393                   (when (= building-line-count 0)
5394                     (sit-for 0)
5395                     (setq building-line-count
5396                           gnus-summary-display-while-building)))
5397               ;; always
5398               (sit-for 0)))
5399         (unless threads
5400           (setq gnus-tmp-level 0)))))
5401   (gnus-message 7 "Generating summary...done"))
5402
5403 (defun gnus-summary-prepare-unthreaded (headers)
5404   "Generate an unthreaded summary buffer based on HEADERS."
5405   (let (header number mark)
5406
5407     (beginning-of-line)
5408
5409     (while headers
5410       ;; We may have to root out some bad articles...
5411       (when (memq (setq number (mail-header-number
5412                                 (setq header (pop headers))))
5413                   gnus-newsgroup-limit)
5414         ;; Mark article as read when it has a low score.
5415         (when (and gnus-summary-mark-below
5416                    (< (or (cdr (assq number gnus-newsgroup-scored))
5417                           gnus-summary-default-score 0)
5418                       gnus-summary-mark-below)
5419                    (not (gnus-summary-article-ancient-p number)))
5420           (setq gnus-newsgroup-unreads
5421                 (delq number gnus-newsgroup-unreads))
5422           (if gnus-newsgroup-auto-expire
5423               (push number gnus-newsgroup-expirable)
5424             (push (cons number gnus-low-score-mark)
5425                   gnus-newsgroup-reads)))
5426
5427         (setq mark (gnus-article-mark number))
5428         (push (gnus-data-make number mark (1+ (point)) header 0)
5429               gnus-newsgroup-data)
5430         (gnus-summary-insert-line
5431          header 0 number
5432          (memq number gnus-newsgroup-undownloaded)
5433          mark (memq number gnus-newsgroup-replied)
5434          (memq number gnus-newsgroup-expirable)
5435          (mail-header-subject header) nil
5436          (cdr (assq number gnus-newsgroup-scored))
5437          (memq number gnus-newsgroup-processable))))))
5438
5439 (defun gnus-summary-remove-list-identifiers ()
5440   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5441   (let ((regexp (if (consp gnus-list-identifiers)
5442                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5443                   gnus-list-identifiers))
5444         changed subject)
5445     (when regexp
5446       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5447       (dolist (header gnus-newsgroup-headers)
5448         (setq subject (mail-header-subject header)
5449               changed nil)
5450         (while (string-match regexp subject)
5451           (setq subject
5452                 (concat (substring subject 0 (match-beginning 1))
5453                         (substring subject (match-end 0)))
5454                 changed t))
5455         (when changed
5456           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5457             (setq subject
5458                   (concat (substring subject 0 (match-beginning 1))
5459                           (substring subject (match-end 1)))))
5460           (mail-header-set-subject header subject))))))
5461
5462 (defun gnus-fetch-headers (articles)
5463   "Fetch headers of ARTICLES."
5464   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5465     (gnus-message 5 "Fetching headers for %s..." name)
5466     (prog1
5467         (if (eq 'nov
5468                 (setq gnus-headers-retrieved-by
5469                       (gnus-retrieve-headers
5470                        articles gnus-newsgroup-name
5471                        ;; We might want to fetch old headers, but
5472                        ;; not if there is only 1 article.
5473                        (and (or (and
5474                                  (not (eq gnus-fetch-old-headers 'some))
5475                                  (not (numberp gnus-fetch-old-headers)))
5476                                 (> (length articles) 1))
5477                             gnus-fetch-old-headers))))
5478             (gnus-get-newsgroup-headers-xover
5479              articles nil nil gnus-newsgroup-name t)
5480           (gnus-get-newsgroup-headers))
5481       (gnus-message 5 "Fetching headers for %s...done" name))))
5482
5483 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5484   "Select newsgroup GROUP.
5485 If READ-ALL is non-nil, all articles in the group are selected.
5486 If SELECT-ARTICLES, only select those articles from GROUP."
5487   (let* ((entry (gnus-group-entry group))
5488          ;;!!! Dirty hack; should be removed.
5489          (gnus-summary-ignore-duplicates
5490           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5491               t
5492             gnus-summary-ignore-duplicates))
5493          (info (nth 2 entry))
5494          charset articles fetched-articles cached)
5495
5496     (unless (gnus-check-server
5497              (set (make-local-variable 'gnus-current-select-method)
5498                   (gnus-find-method-for-group group)))
5499       (error "Couldn't open server"))
5500     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5501
5502     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5503         (gnus-activate-group group)     ; Or we can activate it...
5504         (progn                          ; Or we bug out.
5505           (when (equal major-mode 'gnus-summary-mode)
5506             (gnus-kill-buffer (current-buffer)))
5507           (error
5508            "Couldn't activate group %s: %s"
5509            (mm-decode-coding-string group charset)
5510            (mm-decode-coding-string (gnus-status-message group) charset))))
5511
5512     (unless (gnus-request-group group t)
5513       (when (equal major-mode 'gnus-summary-mode)
5514         (gnus-kill-buffer (current-buffer)))
5515       (error "Couldn't request group %s: %s"
5516              (mm-decode-coding-string group charset)
5517              (mm-decode-coding-string (gnus-status-message group) charset)))
5518
5519     (when gnus-agent
5520       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5521
5522       (setq gnus-summary-use-undownloaded-faces
5523             (gnus-agent-find-parameter
5524              group
5525              'agent-enable-undownloaded-faces)))
5526
5527     (setq gnus-newsgroup-name group
5528           gnus-newsgroup-unselected nil
5529           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5530
5531     (let ((display (gnus-group-find-parameter group 'display)))
5532       (setq gnus-newsgroup-display
5533             (cond
5534              ((not (zerop (or (car-safe read-all) 0)))
5535               ;; The user entered the group with C-u SPC/RET, let's show
5536               ;; all articles.
5537               'gnus-not-ignore)
5538              ((eq display 'all)
5539               'gnus-not-ignore)
5540              ((arrayp display)
5541               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5542              ((numberp display)
5543               ;; The following is probably the "correct" solution, but
5544               ;; it makes Gnus fetch all headers and then limit the
5545               ;; articles (which is slow), so instead we hack the
5546               ;; select-articles parameter instead. -- Simon Josefsson
5547               ;; <jas@kth.se>
5548               ;;
5549               ;; (gnus-byte-compile
5550               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5551               ;;                         display)))))
5552               (setq select-articles
5553                     (gnus-uncompress-range
5554                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5555                              (if (> tmp 0)
5556                                  tmp
5557                                1))
5558                            (cdr (gnus-active group)))))
5559               nil)
5560              (t
5561               nil))))
5562
5563     (gnus-summary-setup-default-charset)
5564
5565     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5566     (when (gnus-virtual-group-p group)
5567       (setq cached gnus-newsgroup-cached))
5568
5569     (setq gnus-newsgroup-unreads
5570           (gnus-sorted-ndifference
5571            (gnus-sorted-ndifference gnus-newsgroup-unreads
5572                                     gnus-newsgroup-marked)
5573            gnus-newsgroup-dormant))
5574
5575     (setq gnus-newsgroup-processable nil)
5576
5577     (gnus-update-read-articles group gnus-newsgroup-unreads)
5578
5579     ;; Adjust and set lists of article marks.
5580     (when info
5581       (gnus-adjust-marked-articles info))
5582     (if (setq articles select-articles)
5583         (setq gnus-newsgroup-unselected
5584               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5585       (setq articles (gnus-articles-to-read group read-all)))
5586
5587     (cond
5588      ((null articles)
5589       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5590       'quit)
5591      ((eq articles 0) nil)
5592      (t
5593       ;; Init the dependencies hash table.
5594       (setq gnus-newsgroup-dependencies
5595             (gnus-make-hashtable (length articles)))
5596       (gnus-set-global-variables)
5597       ;; Retrieve the headers and read them in.
5598
5599       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5600
5601       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5602       (when cached
5603         (setq gnus-newsgroup-cached cached))
5604
5605       ;; Suppress duplicates?
5606       (when gnus-suppress-duplicates
5607         (gnus-dup-suppress-articles))
5608
5609       ;; Set the initial limit.
5610       (setq gnus-newsgroup-limit (copy-sequence articles))
5611       ;; Remove canceled articles from the list of unread articles.
5612       (setq fetched-articles
5613             (mapcar (lambda (headers) (mail-header-number headers))
5614                     gnus-newsgroup-headers))
5615       (setq gnus-newsgroup-articles fetched-articles)
5616       (setq gnus-newsgroup-unreads
5617             (gnus-sorted-nintersection
5618              gnus-newsgroup-unreads fetched-articles))
5619       (gnus-compute-unseen-list)
5620
5621       ;; Removed marked articles that do not exist.
5622       (gnus-update-missing-marks
5623        (gnus-sorted-difference articles fetched-articles))
5624       ;; We might want to build some more threads first.
5625       (when (and gnus-fetch-old-headers
5626                  (eq gnus-headers-retrieved-by 'nov))
5627         (if (eq gnus-fetch-old-headers 'invisible)
5628             (gnus-build-all-threads)
5629           (gnus-build-old-threads)))
5630       ;; Let the Gnus agent mark articles as read.
5631       (when gnus-agent
5632         (gnus-agent-get-undownloaded-list))
5633       ;; Remove list identifiers from subject
5634       (when gnus-list-identifiers
5635         (gnus-summary-remove-list-identifiers))
5636       ;; Check whether auto-expire is to be done in this group.
5637       (setq gnus-newsgroup-auto-expire
5638             (gnus-group-auto-expirable-p group))
5639       ;; Set up the article buffer now, if necessary.
5640       (unless (and gnus-single-article-buffer
5641                    (equal gnus-article-buffer "*Article*"))
5642         (gnus-article-setup-buffer))
5643       ;; First and last article in this newsgroup.
5644       (when gnus-newsgroup-headers
5645         (setq gnus-newsgroup-begin
5646               (mail-header-number (car gnus-newsgroup-headers))
5647               gnus-newsgroup-end
5648               (mail-header-number
5649                (gnus-last-element gnus-newsgroup-headers))))
5650       ;; GROUP is successfully selected.
5651       (or gnus-newsgroup-headers t)))))
5652
5653 (defun gnus-compute-unseen-list ()
5654   ;; The `seen' marks are treated specially.
5655   (if (not gnus-newsgroup-seen)
5656       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5657     (setq gnus-newsgroup-unseen
5658           (gnus-inverse-list-range-intersection
5659            gnus-newsgroup-articles gnus-newsgroup-seen))))
5660
5661 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5662
5663 (defun gnus-summary-display-make-predicate (display)
5664   (require 'gnus-agent)
5665   (when (= (length display) 1)
5666     (setq display (car display)))
5667   (unless gnus-summary-display-cache
5668     (dolist (elem (append '((unread . unread)
5669                             (read . read)
5670                             (unseen . unseen))
5671                           gnus-article-mark-lists))
5672       (push (cons (cdr elem)
5673                   (gnus-byte-compile
5674                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5675             gnus-summary-display-cache)))
5676   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5677         (gnus-category-predicate-cache gnus-summary-display-cache))
5678     (gnus-get-predicate display)))
5679
5680 ;; Uses the dynamically bound `number' variable.
5681 (defvar number)
5682 (defun gnus-article-marked-p (type &optional article)
5683   (let ((article (or article number)))
5684     (cond
5685      ((eq type 'tick)
5686       (memq article gnus-newsgroup-marked))
5687      ((eq type 'spam)
5688       (memq article gnus-newsgroup-spam-marked))
5689      ((eq type 'unsend)
5690       (memq article gnus-newsgroup-unsendable))
5691      ((eq type 'undownload)
5692       (memq article gnus-newsgroup-undownloaded))
5693      ((eq type 'download)
5694       (memq article gnus-newsgroup-downloadable))
5695      ((eq type 'unread)
5696       (memq article gnus-newsgroup-unreads))
5697      ((eq type 'read)
5698       (memq article gnus-newsgroup-reads))
5699      ((eq type 'dormant)
5700       (memq article gnus-newsgroup-dormant) )
5701      ((eq type 'expire)
5702       (memq article gnus-newsgroup-expirable))
5703      ((eq type 'reply)
5704       (memq article gnus-newsgroup-replied))
5705      ((eq type 'killed)
5706       (memq article gnus-newsgroup-killed))
5707      ((eq type 'bookmark)
5708       (assq article gnus-newsgroup-bookmarks))
5709      ((eq type 'score)
5710       (assq article gnus-newsgroup-scored))
5711      ((eq type 'save)
5712       (memq article gnus-newsgroup-saved))
5713      ((eq type 'cache)
5714       (memq article gnus-newsgroup-cached))
5715      ((eq type 'forward)
5716       (memq article gnus-newsgroup-forwarded))
5717      ((eq type 'seen)
5718       (not (memq article gnus-newsgroup-unseen)))
5719      ((eq type 'recent)
5720       (memq article gnus-newsgroup-recent))
5721      (t t))))
5722
5723 (defun gnus-articles-to-read (group &optional read-all)
5724   "Find out what articles the user wants to read."
5725   (let* ((articles
5726           ;; Select all articles if `read-all' is non-nil, or if there
5727           ;; are no unread articles.
5728           (if (or read-all
5729                   (and (zerop (length gnus-newsgroup-marked))
5730                        (zerop (length gnus-newsgroup-unreads)))
5731                   ;; Fetch all if the predicate is non-nil.
5732                   gnus-newsgroup-display)
5733               ;; We want to select the headers for all the articles in
5734               ;; the group, so we select either all the active
5735               ;; articles in the group, or (if that's nil), the
5736               ;; articles in the cache.
5737               (or
5738                (if gnus-newsgroup-maximum-articles
5739                    (let ((active (gnus-active group)))
5740                      (gnus-uncompress-range
5741                       (cons (max (car active)
5742                                  (- (cdr active)
5743                                     gnus-newsgroup-maximum-articles
5744                                     -1))
5745                             (cdr active))))
5746                  (gnus-uncompress-range (gnus-active group)))
5747                (gnus-cache-articles-in-group group))
5748             ;; Select only the "normal" subset of articles.
5749             (gnus-sorted-nunion
5750              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5751              gnus-newsgroup-unreads)))
5752          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5753          (scored (length scored-list))
5754          (number (length articles))
5755          (marked (+ (length gnus-newsgroup-marked)
5756                     (length gnus-newsgroup-dormant)))
5757          (select
5758           (cond
5759            ((numberp read-all)
5760             read-all)
5761            ((numberp gnus-newsgroup-display)
5762             gnus-newsgroup-display)
5763            (t
5764             (condition-case ()
5765                 (cond
5766                  ((and (or (<= scored marked) (= scored number))
5767                        (numberp gnus-large-newsgroup)
5768                        (> number gnus-large-newsgroup))
5769                   (let* ((cursor-in-echo-area nil)
5770                          (initial (gnus-parameter-large-newsgroup-initial
5771                                    gnus-newsgroup-name))
5772                          (input
5773                           (read-string
5774                            (format
5775                             "How many articles from %s (%s %d): "
5776                             (gnus-group-decoded-name gnus-newsgroup-name)
5777                             (if initial "max" "default")
5778                             number)
5779                            (if initial
5780                                (cons (number-to-string initial)
5781                                      0)))))
5782                     (if (string-match "^[ \t]*$" input) number input)))
5783                  ((and (> scored marked) (< scored number)
5784                        (> (- scored number) 20))
5785                   (let ((input
5786                          (read-string
5787                           (format "%s %s (%d scored, %d total): "
5788                                   "How many articles from"
5789                                   (gnus-group-decoded-name group)
5790                                   scored number))))
5791                     (if (string-match "^[ \t]*$" input)
5792                         number input)))
5793                  (t number))
5794               (quit
5795                (message "Quit getting the articles to read")
5796                nil))))))
5797     (setq select (if (stringp select) (string-to-number select) select))
5798     (if (or (null select) (zerop select))
5799         select
5800       (if (and (not (zerop scored)) (<= (abs select) scored))
5801           (progn
5802             (setq articles (sort scored-list '<))
5803             (setq number (length articles)))
5804         (setq articles (copy-sequence articles)))
5805
5806       (when (< (abs select) number)
5807         (if (< select 0)
5808             ;; Select the N oldest articles.
5809             (setcdr (nthcdr (1- (abs select)) articles) nil)
5810           ;; Select the N most recent articles.
5811           (setq articles (nthcdr (- number select) articles))))
5812       (setq gnus-newsgroup-unselected
5813             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5814       (when gnus-alter-articles-to-read-function
5815         (setq articles
5816               (sort
5817                (funcall gnus-alter-articles-to-read-function
5818                         gnus-newsgroup-name articles)
5819                '<)))
5820       articles)))
5821
5822 (defun gnus-killed-articles (killed articles)
5823   (let (out)
5824     (while articles
5825       (when (inline (gnus-member-of-range (car articles) killed))
5826         (push (car articles) out))
5827       (setq articles (cdr articles)))
5828     out))
5829
5830 (defun gnus-uncompress-marks (marks)
5831   "Uncompress the mark ranges in MARKS."
5832   (let ((uncompressed '(score bookmark))
5833         out)
5834     (while marks
5835       (if (memq (caar marks) uncompressed)
5836           (push (car marks) out)
5837         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5838       (setq marks (cdr marks)))
5839     out))
5840
5841 (defun gnus-article-mark-to-type (mark)
5842   "Return the type of MARK."
5843   (or (cadr (assq mark gnus-article-special-mark-lists))
5844       'list))
5845
5846 (defun gnus-article-unpropagatable-p (mark)
5847   "Return whether MARK should be propagated to back end."
5848   (memq mark gnus-article-unpropagated-mark-lists))
5849
5850 (defun gnus-adjust-marked-articles (info)
5851   "Set all article lists and remove all marks that are no longer valid."
5852   (let* ((marked-lists (gnus-info-marks info))
5853          (active (gnus-active (gnus-info-group info)))
5854          (min (car active))
5855          (max (cdr active))
5856          (types gnus-article-mark-lists)
5857          marks var articles article mark mark-type
5858          bgn end)
5859     ;; Hack to avoid adjusting marks for imap.
5860     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5861               'nnimap)
5862       (setq min 1))
5863
5864     (dolist (marks marked-lists)
5865       (setq mark (car marks)
5866             mark-type (gnus-article-mark-to-type mark)
5867             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5868
5869       ;; We set the variable according to the type of the marks list,
5870       ;; and then adjust the marks to a subset of the active articles.
5871       (cond
5872        ;; Adjust "simple" lists - compressed yet unsorted
5873        ((eq mark-type 'list)
5874         ;; Simultaneously uncompress and clip to active range
5875         ;; See gnus-uncompress-range for a description of possible marks
5876         (let (l lh)
5877           (if (not (cadr marks))
5878               (set var nil)
5879             (setq articles (if (numberp (cddr marks))
5880                                (list (cdr marks))
5881                              (cdr marks))
5882                   lh (cons nil nil)
5883                   l lh)
5884
5885             (while (setq article (pop articles))
5886               (cond ((consp article)
5887                      (setq bgn (max (car article) min)
5888                            end (min (cdr article) max))
5889                      (while (<= bgn end)
5890                        (setq l (setcdr l (cons bgn nil))
5891                              bgn (1+ bgn))))
5892                     ((and (<= min article)
5893                           (>= max article))
5894                      (setq l (setcdr l (cons article nil))))))
5895             (set var (cdr lh)))))
5896        ;; Adjust assocs.
5897        ((eq mark-type 'tuple)
5898         (set var (setq articles (cdr marks)))
5899         (when (not (listp (cdr (symbol-value var))))
5900           (set var (list (symbol-value var))))
5901         (when (not (listp (cdr articles)))
5902           (setq articles (list articles)))
5903         (while articles
5904           (when (or (not (consp (setq article (pop articles))))
5905                     (< (car article) min)
5906                     (> (car article) max))
5907             (set var (delq article (symbol-value var))))))
5908        ;; Adjust ranges (sloppily).
5909        ((eq mark-type 'range)
5910         (cond
5911          ((eq mark 'seen)
5912           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5913           ;; It should be (seen (NUM1 . NUM2)).
5914           (when (numberp (cddr marks))
5915             (setcdr marks (list (cdr marks))))
5916           (setq articles (cdr marks))
5917           (while (and articles
5918                       (or (and (consp (car articles))
5919                                (> min (cdar articles)))
5920                           (and (numberp (car articles))
5921                                (> min (car articles)))))
5922             (pop articles))
5923           (set var articles))))))))
5924
5925 (defun gnus-update-missing-marks (missing)
5926   "Go through the list of MISSING articles and remove them from the mark lists."
5927   (when missing
5928     (let (var m)
5929       ;; Go through all types.
5930       (dolist (elem gnus-article-mark-lists)
5931         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5932           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5933           (when (symbol-value var)
5934             ;; This list has articles.  So we delete all missing
5935             ;; articles from it.
5936             (setq m missing)
5937             (while m
5938               (set var (delq (pop m) (symbol-value var))))))))))
5939
5940 (defun gnus-update-marks ()
5941   "Enter the various lists of marked articles into the newsgroup info list."
5942   (let ((types gnus-article-mark-lists)
5943         (info (gnus-get-info gnus-newsgroup-name))
5944         type list newmarked symbol delta-marks)
5945     (when info
5946       ;; Add all marks lists to the list of marks lists.
5947       (while (setq type (pop types))
5948         (setq list (symbol-value
5949                     (setq symbol
5950                           (intern (format "gnus-newsgroup-%s" (car type))))))
5951
5952         (when list
5953           ;; Get rid of the entries of the articles that have the
5954           ;; default score.
5955           (when (and (eq (cdr type) 'score)
5956                      gnus-save-score
5957                      list)
5958             (let* ((arts list)
5959                    (prev (cons nil list))
5960                    (all prev))
5961               (while arts
5962                 (if (or (not (consp (car arts)))
5963                         (= (cdar arts) gnus-summary-default-score))
5964                     (setcdr prev (cdr arts))
5965                   (setq prev arts))
5966                 (setq arts (cdr arts)))
5967               (setq list (cdr all)))))
5968
5969         (when (eq (cdr type) 'seen)
5970           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5971
5972         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5973           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5974
5975         (when (and (gnus-check-backend-function
5976                     'request-set-mark gnus-newsgroup-name)
5977                    (not (gnus-article-unpropagatable-p (cdr type))))
5978           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5979                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5980                  (add (gnus-remove-from-range
5981                        (gnus-copy-sequence list) old)))
5982             (when add
5983               (push (list add 'add (list (cdr type))) delta-marks))
5984             (when del
5985               ;; Don't delete marks from outside the active range.  This
5986               ;; shouldn't happen, but is a sanity check.
5987               (setq del (gnus-sorted-range-intersection
5988                          (gnus-active gnus-newsgroup-name) del))
5989               (push (list del 'del (list (cdr type))) delta-marks))))
5990
5991         (when list
5992           (push (cons (cdr type) list) newmarked)))
5993
5994       (when delta-marks
5995         (unless (gnus-check-group gnus-newsgroup-name)
5996           (error "Can't open server for %s" gnus-newsgroup-name))
5997         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5998
5999       ;; Enter these new marks into the info of the group.
6000       (if (nthcdr 3 info)
6001           (setcar (nthcdr 3 info) newmarked)
6002         ;; Add the marks lists to the end of the info.
6003         (when newmarked
6004           (setcdr (nthcdr 2 info) (list newmarked))))
6005
6006       ;; Cut off the end of the info if there's nothing else there.
6007       (let ((i 5))
6008         (while (and (> i 2)
6009                     (not (nth i info)))
6010           (when (nthcdr (decf i) info)
6011             (setcdr (nthcdr i info) nil)))))))
6012
6013 (defun gnus-set-mode-line (where)
6014   "Set the mode line of the article or summary buffers.
6015 If WHERE is `summary', the summary mode line format will be used."
6016   ;; Is this mode line one we keep updated?
6017   (when (and (memq where gnus-updated-mode-lines)
6018              (symbol-value
6019               (intern (format "gnus-%s-mode-line-format-spec" where))))
6020     (let (mode-string)
6021       ;; We evaluate this in the summary buffer since these
6022       ;; variables are buffer-local to that buffer.
6023       (with-current-buffer gnus-summary-buffer
6024         ;; We bind all these variables that are used in the `eval' form
6025         ;; below.
6026         (let* ((mformat (symbol-value
6027                          (intern
6028                           (format "gnus-%s-mode-line-format-spec" where))))
6029                (gnus-tmp-group-name (gnus-mode-string-quote
6030                                      (gnus-group-decoded-name
6031                                       gnus-newsgroup-name)))
6032                (gnus-tmp-article-number (or gnus-current-article 0))
6033                (gnus-tmp-unread gnus-newsgroup-unreads)
6034                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6035                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6036                (gnus-tmp-unread-and-unselected
6037                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6038                             (zerop gnus-tmp-unselected))
6039                        "")
6040                       ((zerop gnus-tmp-unselected)
6041                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6042                       (t (format "{%d(+%d) more}"
6043                                  gnus-tmp-unread-and-unticked
6044                                  gnus-tmp-unselected))))
6045                (gnus-tmp-subject
6046                 (if (and gnus-current-headers
6047                          (vectorp gnus-current-headers))
6048                     (gnus-mode-string-quote
6049                      (mail-header-subject gnus-current-headers))
6050                   ""))
6051                bufname-length max-len
6052                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6053           (setq mode-string (eval mformat))
6054           (setq bufname-length (if (string-match "%b" mode-string)
6055                                    (- (length
6056                                        (buffer-name
6057                                         (if (eq where 'summary)
6058                                             nil
6059                                           (get-buffer gnus-article-buffer))))
6060                                       2)
6061                                  0))
6062           (setq max-len (max 4 (if gnus-mode-non-string-length
6063                                    (- (window-width)
6064                                       gnus-mode-non-string-length
6065                                       bufname-length)
6066                                  (length mode-string))))
6067           ;; We might have to chop a bit of the string off...
6068           (when (> (length mode-string) max-len)
6069             (setq mode-string
6070                   (concat (truncate-string-to-width mode-string (- max-len 3))
6071                           "...")))))
6072       ;; Update the mode line.
6073       (setq mode-line-buffer-identification
6074             (gnus-mode-line-buffer-identification (list mode-string)))
6075       (set-buffer-modified-p t))))
6076
6077 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6078   "Go through the HEADERS list and add all Xrefs to a hash table.
6079 The resulting hash table is returned, or nil if no Xrefs were found."
6080   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6081          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6082          (xref-hashtb (gnus-make-hashtable))
6083          start group entry number xrefs header)
6084     (while headers
6085       (setq header (pop headers))
6086       (when (and (setq xrefs (mail-header-xref header))
6087                  (not (memq (setq number (mail-header-number header))
6088                             unreads)))
6089         (setq start 0)
6090         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6091           (setq start (match-end 0))
6092           (setq group (if prefix
6093                           (concat prefix (substring xrefs (match-beginning 1)
6094                                                     (match-end 1)))
6095                         (substring xrefs (match-beginning 1) (match-end 1))))
6096           (setq number
6097                 (string-to-number (substring xrefs (match-beginning 2)
6098                                           (match-end 2))))
6099           (if (setq entry (gnus-gethash group xref-hashtb))
6100               (setcdr entry (cons number (cdr entry)))
6101             (gnus-sethash group (cons number nil) xref-hashtb)))))
6102     (and start xref-hashtb)))
6103
6104 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6105   "Look through all the headers and mark the Xrefs as read."
6106   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6107         name info xref-hashtb idlist method nth4)
6108     (with-current-buffer gnus-group-buffer
6109       (when (setq xref-hashtb
6110                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6111         (mapatoms
6112          (lambda (group)
6113            (unless (string= from-newsgroup (setq name (symbol-name group)))
6114              (setq idlist (symbol-value group))
6115              ;; Dead groups are not updated.
6116              (and (prog1
6117                       (setq info (gnus-get-info name))
6118                     (when (stringp (setq nth4 (gnus-info-method info)))
6119                       (setq nth4 (gnus-server-to-method nth4))))
6120                   ;; Only do the xrefs if the group has the same
6121                   ;; select method as the group we have just read.
6122                   (or (gnus-methods-equal-p
6123                        nth4 (gnus-find-method-for-group from-newsgroup))
6124                       virtual
6125                       (equal nth4 (setq method (gnus-find-method-for-group
6126                                                 from-newsgroup)))
6127                       (and (equal (car nth4) (car method))
6128                            (equal (nth 1 nth4) (nth 1 method))))
6129                   gnus-use-cross-reference
6130                   (or (not (eq gnus-use-cross-reference t))
6131                       virtual
6132                       ;; Only do cross-references on subscribed
6133                       ;; groups, if that is what is wanted.
6134                       (<= (gnus-info-level info) gnus-level-subscribed))
6135                   (gnus-group-make-articles-read name idlist))))
6136          xref-hashtb)))))
6137
6138 (defun gnus-compute-read-articles (group articles)
6139   (let* ((entry (gnus-group-entry group))
6140          (info (nth 2 entry))
6141          (active (gnus-active group))
6142          ninfo)
6143     (when entry
6144       ;; First peel off all invalid article numbers.
6145       (when active
6146         (let ((ids articles)
6147               id first)
6148           (while (setq id (pop ids))
6149             (when (and first (> id (cdr active)))
6150               ;; We'll end up in this situation in one particular
6151               ;; obscure situation.  If you re-scan a group and get
6152               ;; a new article that is cross-posted to a different
6153               ;; group that has not been re-scanned, you might get
6154               ;; crossposted article that has a higher number than
6155               ;; Gnus believes possible.  So we re-activate this
6156               ;; group as well.  This might mean doing the
6157               ;; crossposting thingy will *increase* the number
6158               ;; of articles in some groups.  Tsk, tsk.
6159               (setq active (or (gnus-activate-group group) active)))
6160             (when (or (> id (cdr active))
6161                       (< id (car active)))
6162               (setq articles (delq id articles))))))
6163       ;; If the read list is nil, we init it.
6164       (if (and active
6165                (null (gnus-info-read info))
6166                (> (car active) 1))
6167           (setq ninfo (cons 1 (1- (car active))))
6168         (setq ninfo (gnus-info-read info)))
6169       ;; Then we add the read articles to the range.
6170       (gnus-add-to-range
6171        ninfo (setq articles (sort articles '<))))))
6172
6173 (defun gnus-group-make-articles-read (group articles)
6174   "Update the info of GROUP to say that ARTICLES are read."
6175   (let* ((num 0)
6176          (entry (gnus-group-entry group))
6177          (info (nth 2 entry))
6178          (active (gnus-active group))
6179          range)
6180     (when entry
6181       (setq range (gnus-compute-read-articles group articles))
6182       (with-current-buffer gnus-group-buffer
6183         (gnus-undo-register
6184           `(progn
6185              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6186              (gnus-info-set-read ',info ',(gnus-info-read info))
6187              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6188              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6189              (gnus-group-update-group ,group t))))
6190       ;; Add the read articles to the range.
6191       (gnus-info-set-read info range)
6192       (gnus-request-set-mark group (list (list range 'add '(read))))
6193       ;; Then we have to re-compute how many unread
6194       ;; articles there are in this group.
6195       (when active
6196         (cond
6197          ((not range)
6198           (setq num (- (1+ (cdr active)) (car active))))
6199          ((not (listp (cdr range)))
6200           (setq num (- (cdr active) (- (1+ (cdr range))
6201                                        (car range)))))
6202          (t
6203           (while range
6204             (if (numberp (car range))
6205                 (setq num (1+ num))
6206               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6207             (setq range (cdr range)))
6208           (setq num (- (cdr active) num))))
6209         ;; Update the number of unread articles.
6210         (setcar entry num)
6211         ;; Update the group buffer.
6212         (unless (gnus-ephemeral-group-p group)
6213           (gnus-group-update-group group t))))))
6214
6215 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6216   (let ((cur nntp-server-buffer)
6217         (dependencies
6218          (or dependencies
6219              (with-current-buffer gnus-summary-buffer
6220                gnus-newsgroup-dependencies)))
6221         headers id end ref number
6222         (mail-parse-charset gnus-newsgroup-charset)
6223         (mail-parse-ignored-charsets
6224          (save-current-buffer (condition-case nil
6225                                   (set-buffer gnus-summary-buffer)
6226                                 (error))
6227                               gnus-newsgroup-ignored-charsets)))
6228     (with-current-buffer nntp-server-buffer
6229       ;; Translate all TAB characters into SPACE characters.
6230       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6231       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6232       (ietf-drums-unfold-fws)
6233       (gnus-run-hooks 'gnus-parse-headers-hook)
6234       (let ((case-fold-search t)
6235             in-reply-to header p lines chars)
6236         (goto-char (point-min))
6237         ;; Search to the beginning of the next header.  Error messages
6238         ;; do not begin with 2 or 3.
6239         (while (re-search-forward "^[23][0-9]+ " nil t)
6240           (setq id nil
6241                 ref nil)
6242           ;; This implementation of this function, with nine
6243           ;; search-forwards instead of the one re-search-forward and
6244           ;; a case (which basically was the old function) is actually
6245           ;; about twice as fast, even though it looks messier.  You
6246           ;; can't have everything, I guess.  Speed and elegance
6247           ;; doesn't always go hand in hand.
6248           (setq
6249            header
6250            (vector
6251             ;; Number.
6252             (prog1
6253                 (setq number (read cur))
6254               (end-of-line)
6255               (setq p (point))
6256               (narrow-to-region (point)
6257                                 (or (and (search-forward "\n.\n" nil t)
6258                                          (- (point) 2))
6259                                     (point))))
6260             ;; Subject.
6261             (progn
6262               (goto-char p)
6263               (if (search-forward "\nsubject:" nil t)
6264                   (funcall gnus-decode-encoded-word-function
6265                            (nnheader-header-value))
6266                 "(none)"))
6267             ;; From.
6268             (progn
6269               (goto-char p)
6270               (if (search-forward "\nfrom:" nil t)
6271                   (funcall gnus-decode-encoded-address-function
6272                            (nnheader-header-value))
6273                 "(nobody)"))
6274             ;; Date.
6275             (progn
6276               (goto-char p)
6277               (if (search-forward "\ndate:" nil t)
6278                   (nnheader-header-value) ""))
6279             ;; Message-ID.
6280             (progn
6281               (goto-char p)
6282               (setq id (if (re-search-forward
6283                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6284                            ;; We do it this way to make sure the Message-ID
6285                            ;; is (somewhat) syntactically valid.
6286                            (buffer-substring (match-beginning 1)
6287                                              (match-end 1))
6288                          ;; If there was no message-id, we just fake one
6289                          ;; to make subsequent routines simpler.
6290                          (nnheader-generate-fake-message-id number))))
6291             ;; References.
6292             (progn
6293               (goto-char p)
6294               (if (search-forward "\nreferences:" nil t)
6295                   (progn
6296                     (setq end (point))
6297                     (prog1
6298                         (nnheader-header-value)
6299                       (setq ref
6300                             (buffer-substring
6301                              (progn
6302                                (end-of-line)
6303                                (search-backward ">" end t)
6304                                (1+ (point)))
6305                              (progn
6306                                (search-backward "<" end t)
6307                                (point))))))
6308                 ;; Get the references from the in-reply-to header if there
6309                 ;; were no references and the in-reply-to header looks
6310                 ;; promising.
6311                 (if (and (search-forward "\nin-reply-to:" nil t)
6312                          (setq in-reply-to (nnheader-header-value))
6313                          (string-match "<[^>]+>" in-reply-to))
6314                     (let (ref2)
6315                       (setq ref (substring in-reply-to (match-beginning 0)
6316                                            (match-end 0)))
6317                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6318                         (setq ref2 (substring in-reply-to (match-beginning 0)
6319                                               (match-end 0)))
6320                         (when (> (length ref2) (length ref))
6321                           (setq ref ref2)))
6322                       ref)
6323                   (setq ref nil))))
6324             ;; Chars.
6325             (progn
6326               (goto-char p)
6327               (if (search-forward "\nchars: " nil t)
6328                   (if (numberp (setq chars (ignore-errors (read cur))))
6329                       chars -1)
6330                 -1))
6331             ;; Lines.
6332             (progn
6333               (goto-char p)
6334               (if (search-forward "\nlines: " nil t)
6335                   (if (numberp (setq lines (ignore-errors (read cur))))
6336                       lines -1)
6337                 -1))
6338             ;; Xref.
6339             (progn
6340               (goto-char p)
6341               (and (search-forward "\nxref:" nil t)
6342                    (nnheader-header-value)))
6343             ;; Extra.
6344             (when gnus-extra-headers
6345               (let ((extra gnus-extra-headers)
6346                     out)
6347                 (while extra
6348                   (goto-char p)
6349                   (when (search-forward
6350                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6351                     (push (cons (car extra) (nnheader-header-value))
6352                           out))
6353                   (pop extra))
6354                 out))))
6355           (when (equal id ref)
6356             (setq ref nil))
6357
6358           (when gnus-alter-header-function
6359             (funcall gnus-alter-header-function header)
6360             (setq id (mail-header-id header)
6361                   ref (gnus-parent-id (mail-header-references header))))
6362
6363           (when (setq header
6364                       (gnus-dependencies-add-header
6365                        header dependencies force-new))
6366             (push header headers))
6367           (goto-char (point-max))
6368           (widen))
6369         (nreverse headers)))))
6370
6371 ;; Goes through the xover lines and returns a list of vectors
6372 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6373                                                   force-new dependencies
6374                                                   group also-fetch-heads)
6375   "Parse the news overview data in the server buffer.
6376 Return a list of headers that match SEQUENCE (see
6377 `nntp-retrieve-headers')."
6378   ;; Get the Xref when the users reads the articles since most/some
6379   ;; NNTP servers do not include Xrefs when using XOVER.
6380   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6381   (let ((mail-parse-charset gnus-newsgroup-charset)
6382         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6383         (cur nntp-server-buffer)
6384         (dependencies (or dependencies gnus-newsgroup-dependencies))
6385         (allp (cond
6386                ((eq gnus-read-all-available-headers t)
6387                 t)
6388                ((and (stringp gnus-read-all-available-headers)
6389                      group)
6390                 (string-match gnus-read-all-available-headers group))
6391                (t
6392                 nil)))
6393         number headers header)
6394     (with-current-buffer nntp-server-buffer
6395       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6396       ;; Allow the user to mangle the headers before parsing them.
6397       (gnus-run-hooks 'gnus-parse-headers-hook)
6398       (goto-char (point-min))
6399       (gnus-parse-without-error
6400         (while (and (or sequence allp)
6401                     (not (eobp)))
6402           (setq number (read cur))
6403           (when (not allp)
6404             (while (and sequence
6405                         (< (car sequence) number))
6406               (setq sequence (cdr sequence))))
6407           (when (and (or allp
6408                          (and sequence
6409                               (eq number (car sequence))))
6410                      (progn
6411                        (setq sequence (cdr sequence))
6412                        (setq header (inline
6413                                       (gnus-nov-parse-line
6414                                        number dependencies force-new)))))
6415             (push header headers))
6416           (forward-line 1)))
6417       ;; A common bug in inn is that if you have posted an article and
6418       ;; then retrieves the active file, it will answer correctly --
6419       ;; the new article is included.  However, a NOV entry for the
6420       ;; article may not have been generated yet, so this may fail.
6421       ;; We work around this problem by retrieving the last few
6422       ;; headers using HEAD.
6423       (if (or (not also-fetch-heads)
6424               (not sequence))
6425           ;; We (probably) got all the headers.
6426           (nreverse headers)
6427         (let ((gnus-nov-is-evil t))
6428           (nconc
6429            (nreverse headers)
6430            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6431              (gnus-get-newsgroup-headers))))))))
6432
6433 (defun gnus-article-get-xrefs ()
6434   "Fill in the Xref value in `gnus-current-headers', if necessary.
6435 This is meant to be called in `gnus-article-internal-prepare-hook'."
6436   (let ((headers (with-current-buffer gnus-summary-buffer
6437                    gnus-current-headers)))
6438     (or (not gnus-use-cross-reference)
6439         (not headers)
6440         (and (mail-header-xref headers)
6441              (not (string= (mail-header-xref headers) "")))
6442         (let ((case-fold-search t)
6443               xref)
6444           (save-restriction
6445             (nnheader-narrow-to-headers)
6446             (goto-char (point-min))
6447             (when (or (and (not (eobp))
6448                            (eq (downcase (char-after)) ?x)
6449                            (looking-at "Xref:"))
6450                       (search-forward "\nXref:" nil t))
6451               (goto-char (1+ (match-end 0)))
6452               (setq xref (buffer-substring (point) (point-at-eol)))
6453               (mail-header-set-xref headers xref)))))))
6454
6455 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6456   "Find article ID and insert the summary line for that article.
6457 OLD-HEADER can either be a header or a line number to insert
6458 the subject line on."
6459   (let* ((line (and (numberp old-header) old-header))
6460          (old-header (and (vectorp old-header) old-header))
6461          (header (cond ((and old-header use-old-header)
6462                         old-header)
6463                        ((and (numberp id)
6464                              (gnus-number-to-header id))
6465                         (gnus-number-to-header id))
6466                        (t
6467                         (gnus-read-header id))))
6468          (number (and (numberp id) id))
6469          d)
6470     (when header
6471       ;; Rebuild the thread that this article is part of and go to the
6472       ;; article we have fetched.
6473       (when (and (not gnus-show-threads)
6474                  old-header)
6475         (when (and number
6476                    (setq d (gnus-data-find (mail-header-number old-header))))
6477           (goto-char (gnus-data-pos d))
6478           (gnus-data-remove
6479            number
6480            (- (point-at-bol)
6481               (prog1
6482                   (1+ (point-at-eol))
6483                 (gnus-delete-line))))))
6484       ;; Remove list identifiers from subject.
6485       (when gnus-list-identifiers
6486         (let ((gnus-newsgroup-headers (list header)))
6487           (gnus-summary-remove-list-identifiers)))
6488       (when old-header
6489         (mail-header-set-number header (mail-header-number old-header)))
6490       (setq gnus-newsgroup-sparse
6491             (delq (setq number (mail-header-number header))
6492                   gnus-newsgroup-sparse))
6493       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6494       (push number gnus-newsgroup-limit)
6495       (gnus-rebuild-thread (mail-header-id header) line)
6496       (gnus-summary-goto-subject number nil t))
6497     (when (and (numberp number)
6498                (> number 0))
6499       ;; We have to update the boundaries even if we can't fetch the
6500       ;; article if ID is a number -- so that the next `P' or `N'
6501       ;; command will fetch the previous (or next) article even
6502       ;; if the one we tried to fetch this time has been canceled.
6503       (when (> number gnus-newsgroup-end)
6504         (setq gnus-newsgroup-end number))
6505       (when (< number gnus-newsgroup-begin)
6506         (setq gnus-newsgroup-begin number))
6507       (setq gnus-newsgroup-unselected
6508             (delq number gnus-newsgroup-unselected)))
6509     ;; Report back a success?
6510     (and header (mail-header-number header))))
6511
6512 ;;; Process/prefix in the summary buffer
6513
6514 (defun gnus-summary-work-articles (n)
6515   "Return a list of articles to be worked upon.
6516 The prefix argument, the list of process marked articles, and the
6517 current article will be taken into consideration."
6518   (with-current-buffer gnus-summary-buffer
6519     (cond
6520      (n
6521       ;; A numerical prefix has been given.
6522       (setq n (prefix-numeric-value n))
6523       (let ((backward (< n 0))
6524             (n (abs (prefix-numeric-value n)))
6525             articles article)
6526         (save-excursion
6527           (while
6528               (and (> n 0)
6529                    (push (setq article (gnus-summary-article-number))
6530                          articles)
6531                    (if backward
6532                        (gnus-summary-find-prev nil article)
6533                      (gnus-summary-find-next nil article)))
6534             (decf n)))
6535         (nreverse articles)))
6536      ((and (gnus-region-active-p) (mark))
6537       (message "region active")
6538       ;; Work on the region between point and mark.
6539       (let ((max (max (point) (mark)))
6540             articles article)
6541         (save-excursion
6542           (goto-char (min (point) (mark)))
6543           (while
6544               (and
6545                (push (setq article (gnus-summary-article-number)) articles)
6546                (gnus-summary-find-next nil article)
6547                (< (point) max)))
6548           (nreverse articles))))
6549      (gnus-newsgroup-processable
6550       ;; There are process-marked articles present.
6551       ;; Save current state.
6552       (gnus-summary-save-process-mark)
6553       ;; Return the list.
6554       (reverse gnus-newsgroup-processable))
6555      (t
6556       ;; Just return the current article.
6557       (list (gnus-summary-article-number))))))
6558
6559 (defmacro gnus-summary-iterate (arg &rest forms)
6560   "Iterate over the process/prefixed articles and do FORMS.
6561 ARG is the interactive prefix given to the command.  FORMS will be
6562 executed with point over the summary line of the articles."
6563   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6564     `(let ((,articles (gnus-summary-work-articles ,arg)))
6565        (while ,articles
6566          (gnus-summary-goto-subject (car ,articles))
6567          ,@forms
6568          (pop ,articles)))))
6569
6570 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6571 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6572
6573 (defun gnus-summary-save-process-mark ()
6574   "Push the current set of process marked articles on the stack."
6575   (interactive)
6576   (push (copy-sequence gnus-newsgroup-processable)
6577         gnus-newsgroup-process-stack))
6578
6579 (defun gnus-summary-kill-process-mark ()
6580   "Push the current set of process marked articles on the stack and unmark."
6581   (interactive)
6582   (gnus-summary-save-process-mark)
6583   (gnus-summary-unmark-all-processable))
6584
6585 (defun gnus-summary-yank-process-mark ()
6586   "Pop the last process mark state off the stack and restore it."
6587   (interactive)
6588   (unless gnus-newsgroup-process-stack
6589     (error "Empty mark stack"))
6590   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6591
6592 (defun gnus-summary-process-mark-set (set)
6593   "Make SET into the current process marked articles."
6594   (gnus-summary-unmark-all-processable)
6595   (mapc 'gnus-summary-set-process-mark set))
6596
6597 ;;; Searching and stuff
6598
6599 (defun gnus-summary-search-group (&optional backward use-level)
6600   "Search for next unread newsgroup.
6601 If optional argument BACKWARD is non-nil, search backward instead."
6602   (with-current-buffer gnus-group-buffer
6603     (when (gnus-group-search-forward
6604            backward nil (if use-level (gnus-group-group-level) nil))
6605       (gnus-group-group-name))))
6606
6607 (defun gnus-summary-best-group (&optional exclude-group)
6608   "Find the name of the best unread group.
6609 If EXCLUDE-GROUP, do not go to this group."
6610   (with-current-buffer gnus-group-buffer
6611     (save-excursion
6612       (gnus-group-best-unread-group exclude-group))))
6613
6614 (defun gnus-summary-find-next (&optional unread article backward)
6615   (if backward
6616       (gnus-summary-find-prev unread article)
6617     (let* ((dummy (gnus-summary-article-intangible-p))
6618            (article (or article (gnus-summary-article-number)))
6619            (data (gnus-data-find-list article))
6620            result)
6621       (when (and (not dummy)
6622                  (or (not gnus-summary-check-current)
6623                      (not unread)
6624                      (not (gnus-data-unread-p (car data)))))
6625         (setq data (cdr data)))
6626       (when (setq result
6627                   (if unread
6628                       (progn
6629                         (while data
6630                           (unless (memq (gnus-data-number (car data))
6631                                         (cond
6632                                          ((eq gnus-auto-goto-ignores
6633                                               'always-undownloaded)
6634                                           gnus-newsgroup-undownloaded)
6635                                          (gnus-plugged
6636                                           nil)
6637                                          ((eq gnus-auto-goto-ignores
6638                                               'unfetched)
6639                                           gnus-newsgroup-unfetched)
6640                                          ((eq gnus-auto-goto-ignores
6641                                               'undownloaded)
6642                                           gnus-newsgroup-undownloaded)))
6643                             (when (gnus-data-unread-p (car data))
6644                               (setq result (car data)
6645                                     data nil)))
6646                           (setq data (cdr data)))
6647                         result)
6648                     (car data)))
6649         (goto-char (gnus-data-pos result))
6650         (gnus-data-number result)))))
6651
6652 (defun gnus-summary-find-prev (&optional unread article)
6653   (let* ((eobp (eobp))
6654          (article (or article (gnus-summary-article-number)))
6655          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6656          result)
6657     (when (and (not eobp)
6658                (or (not gnus-summary-check-current)
6659                    (not unread)
6660                    (not (gnus-data-unread-p (car data)))))
6661       (setq data (cdr data)))
6662     (when (setq result
6663                 (if unread
6664                     (progn
6665                       (while data
6666                         (unless (memq (gnus-data-number (car data))
6667                                       (cond
6668                                        ((eq gnus-auto-goto-ignores
6669                                             'always-undownloaded)
6670                                         gnus-newsgroup-undownloaded)
6671                                        (gnus-plugged
6672                                         nil)
6673                                        ((eq gnus-auto-goto-ignores
6674                                             'unfetched)
6675                                         gnus-newsgroup-unfetched)
6676                                        ((eq gnus-auto-goto-ignores
6677                                             'undownloaded)
6678                                         gnus-newsgroup-undownloaded)))
6679                           (when (gnus-data-unread-p (car data))
6680                             (setq result (car data)
6681                                   data nil)))
6682                         (setq data (cdr data)))
6683                       result)
6684                   (car data)))
6685       (goto-char (gnus-data-pos result))
6686       (gnus-data-number result))))
6687
6688 (defun gnus-summary-find-subject (subject &optional unread backward article)
6689   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6690          (article (or article (gnus-summary-article-number)))
6691          (articles (gnus-data-list backward))
6692          (arts (gnus-data-find-list article articles))
6693          result)
6694     (when (or (not gnus-summary-check-current)
6695               (not unread)
6696               (not (gnus-data-unread-p (car arts))))
6697       (setq arts (cdr arts)))
6698     (while arts
6699       (and (or (not unread)
6700                (gnus-data-unread-p (car arts)))
6701            (vectorp (gnus-data-header (car arts)))
6702            (gnus-subject-equal
6703             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6704            (setq result (car arts)
6705                  arts nil))
6706       (setq arts (cdr arts)))
6707     (and result
6708          (goto-char (gnus-data-pos result))
6709          (gnus-data-number result))))
6710
6711 (defun gnus-summary-search-forward (&optional unread subject backward)
6712   "Search forward for an article.
6713 If UNREAD, look for unread articles.  If SUBJECT, look for
6714 articles with that subject.  If BACKWARD, search backward instead."
6715   (cond (subject (gnus-summary-find-subject subject unread backward))
6716         (backward (gnus-summary-find-prev unread))
6717         (t (gnus-summary-find-next unread))))
6718
6719 (defun gnus-recenter (&optional n)
6720   "Center point in window and redisplay frame.
6721 Also do horizontal recentering."
6722   (interactive "P")
6723   (when (and gnus-auto-center-summary
6724              (not (eq gnus-auto-center-summary 'vertical)))
6725     (gnus-horizontal-recenter))
6726   (if (fboundp 'recenter-top-bottom)
6727       (recenter-top-bottom n)
6728     (recenter n)))
6729
6730 (put 'gnus-recenter 'isearch-scroll t)
6731
6732 (defun gnus-forward-line-ignore-invisible (n)
6733   "Move N lines forward (backward if N is negative).
6734 Like forward-line, but skip over (and don't count) invisible lines."
6735   (let (done)
6736     (while (and (> n 0) (not done))
6737       ;; If the following character is currently invisible,
6738       ;; skip all characters with that same `invisible' property value.
6739       (while (gnus-invisible-p (point))
6740         (goto-char (gnus-next-char-property-change (point))))
6741       (forward-line 1)
6742       (if (eobp)
6743           (setq done t)
6744         (setq n (1- n))))
6745     (while (and (< n 0) (not done))
6746       (forward-line -1)
6747       (if (bobp) (setq done t)
6748         (setq n (1+ n))
6749         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6750           (goto-char (gnus-previous-char-property-change (point))))))))
6751
6752 (defun gnus-summary-recenter ()
6753   "Center point in the summary window.
6754 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6755 displayed, no centering will be performed."
6756   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6757   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6758   (interactive)
6759   ;; The user has to want it.
6760   (when gnus-auto-center-summary
6761     (let* ((top (cond ((< (window-height) 4) 0)
6762                       ((< (window-height) 7) 1)
6763                       (t (if (numberp gnus-auto-center-summary)
6764                              gnus-auto-center-summary
6765                            (/ (1- (window-height)) 2)))))
6766            (height (1- (window-height)))
6767            (bottom (save-excursion
6768                      (goto-char (point-max))
6769                      (gnus-forward-line-ignore-invisible (- height))
6770                      (point)))
6771            (window (get-buffer-window (current-buffer))))
6772       (when (get-buffer-window gnus-article-buffer)
6773         ;; Only do recentering when the article buffer is displayed,
6774         ;; Set the window start to either `bottom', which is the biggest
6775         ;; possible valid number, or the second line from the top,
6776         ;; whichever is the least.
6777         (let ((top-pos (save-excursion
6778                          (gnus-forward-line-ignore-invisible (- top))
6779                          (point))))
6780           (if (> bottom top-pos)
6781               ;; Keep the second line from the top visible
6782               (set-window-start window top-pos)
6783             ;; Try to keep the bottom line visible; if it's partially
6784             ;; obscured, either scroll one more line to make it fully
6785             ;; visible, or revert to using TOP-POS.
6786             (save-excursion
6787               (goto-char (point-max))
6788               (gnus-forward-line-ignore-invisible -1)
6789               (let ((last-line-start (point)))
6790                 (goto-char bottom)
6791                 (set-window-start window (point) t)
6792                 (when (not (pos-visible-in-window-p last-line-start window))
6793                   (gnus-forward-line-ignore-invisible 1)
6794                   (set-window-start window (min (point) top-pos) t)))))))
6795       ;; Do horizontal recentering while we're at it.
6796       (when (and (get-buffer-window (current-buffer) t)
6797                  (not (eq gnus-auto-center-summary 'vertical)))
6798         (let ((selected (selected-window)))
6799           (select-window (get-buffer-window (current-buffer) t))
6800           (gnus-summary-position-point)
6801           (gnus-horizontal-recenter)
6802           (select-window selected))))))
6803
6804 (defun gnus-summary-jump-to-group (newsgroup)
6805   "Move point to NEWSGROUP in group mode buffer."
6806   ;; Keep update point of group mode buffer if visible.
6807   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6808       (save-window-excursion
6809         ;; Take care of tree window mode.
6810         (when (get-buffer-window gnus-group-buffer)
6811           (pop-to-buffer gnus-group-buffer))
6812         (gnus-group-jump-to-group newsgroup))
6813     (save-excursion
6814       ;; Take care of tree window mode.
6815       (if (get-buffer-window gnus-group-buffer 0)
6816           (pop-to-buffer gnus-group-buffer)
6817         (set-buffer gnus-group-buffer))
6818       (gnus-group-jump-to-group newsgroup))))
6819
6820 ;; This function returns a list of article numbers based on the
6821 ;; difference between the ranges of read articles in this group and
6822 ;; the range of active articles.
6823 (defun gnus-list-of-unread-articles (group)
6824   (let* ((read (gnus-info-read (gnus-get-info group)))
6825          (active (or (gnus-active group) (gnus-activate-group group)))
6826          (last (or (cdr active)
6827                    (error "Group %s couldn't be activated " group)))
6828          (bottom (if gnus-newsgroup-maximum-articles
6829                      (max (car active)
6830                           (- last gnus-newsgroup-maximum-articles -1))
6831                    (car active)))
6832          first nlast unread)
6833     ;; If none are read, then all are unread.
6834     (if (not read)
6835         (setq first bottom)
6836       ;; If the range of read articles is a single range, then the
6837       ;; first unread article is the article after the last read
6838       ;; article.  Sounds logical, doesn't it?
6839       (if (and (not (listp (cdr read)))
6840                (or (< (car read) bottom)
6841                    (progn (setq read (list read))
6842                           nil)))
6843           (setq first (max bottom (1+ (cdr read))))
6844         ;; `read' is a list of ranges.
6845         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6846                                   (caar read)))
6847                   1)
6848           (setq first bottom))
6849         (while read
6850           (when first
6851             (while (< first nlast)
6852               (setq unread (cons first unread)
6853                     first (1+ first))))
6854           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6855           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6856           (setq read (cdr read)))))
6857     ;; And add the last unread articles.
6858     (while (<= first last)
6859       (setq unread (cons first unread)
6860             first (1+ first)))
6861     ;; Return the list of unread articles.
6862     (delq 0 (nreverse unread))))
6863
6864 (defun gnus-list-of-read-articles (group)
6865   "Return a list of unread, unticked and non-dormant articles."
6866   (let* ((info (gnus-get-info group))
6867          (marked (gnus-info-marks info))
6868          (active (gnus-active group)))
6869     (and info active
6870          (gnus-list-range-difference
6871           (gnus-list-range-difference
6872            (gnus-sorted-complement
6873             (gnus-uncompress-range
6874              (if gnus-newsgroup-maximum-articles
6875                  (cons (max (car active)
6876                             (- (cdr active)
6877                                gnus-newsgroup-maximum-articles
6878                                -1))
6879                        (cdr active))
6880                active))
6881             (gnus-list-of-unread-articles group))
6882            (cdr (assq 'dormant marked)))
6883           (cdr (assq 'tick marked))))))
6884
6885 ;; This function returns a sequence of article numbers based on the
6886 ;; difference between the ranges of read articles in this group and
6887 ;; the range of active articles.
6888 (defun gnus-sequence-of-unread-articles (group)
6889   (let* ((read (gnus-info-read (gnus-get-info group)))
6890          (active (or (gnus-active group) (gnus-activate-group group)))
6891          (last (cdr active))
6892          (bottom (if gnus-newsgroup-maximum-articles
6893                      (max (car active)
6894                           (- last gnus-newsgroup-maximum-articles -1))
6895                    (car active)))
6896          first nlast unread)
6897     ;; If none are read, then all are unread.
6898     (if (not read)
6899         (setq first bottom)
6900       ;; If the range of read articles is a single range, then the
6901       ;; first unread article is the article after the last read
6902       ;; article.  Sounds logical, doesn't it?
6903       (if (and (not (listp (cdr read)))
6904                (or (< (car read) bottom)
6905                    (progn (setq read (list read))
6906                           nil)))
6907           (setq first (max bottom (1+ (cdr read))))
6908         ;; `read' is a list of ranges.
6909         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6910                                   (caar read)))
6911                   1)
6912           (setq first bottom))
6913         (while read
6914           (when first
6915             (push (cons first nlast) unread))
6916           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6917           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6918           (setq read (cdr read)))))
6919     ;; And add the last unread articles.
6920     (cond ((not (and first last))
6921            nil)
6922           ((< first last)
6923            (push (cons first last) unread))
6924           ((= first last)
6925            (push first unread)))
6926     ;; Return the sequence of unread articles.
6927     (delq 0 (nreverse unread))))
6928
6929 ;; Various summary commands
6930
6931 (defun gnus-summary-select-article-buffer ()
6932   "Reconfigure windows to show the article buffer."
6933   (interactive)
6934   (if (not (gnus-buffer-live-p gnus-article-buffer))
6935       (error "There is no article buffer for this summary buffer")
6936     (unless (get-buffer-window gnus-article-buffer)
6937       (gnus-summary-show-article))
6938     (select-window (get-buffer-window gnus-article-buffer))
6939     (gnus-configure-windows 'only-article t)))
6940
6941 (defun gnus-summary-universal-argument (arg)
6942   "Perform any operation on all articles that are process/prefixed."
6943   (interactive "P")
6944   (let ((articles (gnus-summary-work-articles arg))
6945         func article)
6946     (if (eq
6947          (setq
6948           func
6949           (key-binding
6950            (read-key-sequence
6951             (substitute-command-keys
6952              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6953          'undefined)
6954         (gnus-error 1 "Undefined key")
6955       (save-excursion
6956         (while articles
6957           (gnus-summary-goto-subject (setq article (pop articles)))
6958           (let (gnus-newsgroup-processable)
6959             (command-execute func))
6960           (gnus-summary-remove-process-mark article)))))
6961   (gnus-summary-position-point))
6962
6963 (defun gnus-summary-toggle-truncation (&optional arg)
6964   "Toggle truncation of summary lines.
6965 With ARG, turn line truncation on if ARG is positive."
6966   (interactive "P")
6967   (setq truncate-lines
6968         (if (null arg) (not truncate-lines)
6969           (> (prefix-numeric-value arg) 0)))
6970   (redraw-display))
6971
6972 (defun gnus-summary-find-for-reselect ()
6973   "Return the number of an article to stay on across a reselect.
6974 The current article is considered, then following articles, then previous
6975 articles.  An article is sought which is not cancelled and isn't a temporary
6976 insertion from another group.  If there's no such then return a dummy 0."
6977   (let (found)
6978     (dolist (rev '(nil t))
6979       (unless found      ; don't demand the reverse list if we don't need it
6980         (let ((data (gnus-data-find-list
6981                      (gnus-summary-article-number) (gnus-data-list rev))))
6982           (while (and data (not found))
6983             (if (and (< 0 (gnus-data-number (car data)))
6984                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6985                 (setq found (gnus-data-number (car data))))
6986             (setq data (cdr data))))))
6987     (or found 0)))
6988
6989 (defun gnus-summary-reselect-current-group (&optional all rescan)
6990   "Exit and then reselect the current newsgroup.
6991 The prefix argument ALL means to select all articles."
6992   (interactive "P")
6993   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6994     (error "Ephemeral groups can't be reselected"))
6995   (let ((current-subject (gnus-summary-find-for-reselect))
6996         (group gnus-newsgroup-name))
6997     (setq gnus-newsgroup-begin nil)
6998     (gnus-summary-exit nil 'leave-hidden)
6999     ;; We have to adjust the point of group mode buffer because
7000     ;; point was moved to the next unread newsgroup by exiting.
7001     (gnus-summary-jump-to-group group)
7002     (when rescan
7003       (save-excursion
7004         (gnus-group-get-new-news-this-group 1)))
7005     (gnus-group-read-group all t)
7006     (gnus-summary-goto-subject current-subject nil t)))
7007
7008 (defun gnus-summary-rescan-group (&optional all)
7009   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7010   (interactive "P")
7011   (gnus-summary-reselect-current-group all t))
7012
7013 (defun gnus-summary-update-info (&optional non-destructive)
7014   (save-excursion
7015     (let ((group gnus-newsgroup-name))
7016       (when group
7017         (when gnus-newsgroup-kill-headers
7018           (setq gnus-newsgroup-killed
7019                 (gnus-compress-sequence
7020                  (gnus-sorted-union
7021                   (gnus-list-range-intersection
7022                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7023                   gnus-newsgroup-unreads)
7024                  t)))
7025         (unless (listp (cdr gnus-newsgroup-killed))
7026           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7027         (let ((headers gnus-newsgroup-headers))
7028           ;; Set the new ranges of read articles.
7029           (with-current-buffer gnus-group-buffer
7030             (gnus-undo-force-boundary))
7031           (gnus-update-read-articles
7032            group (gnus-sorted-union
7033                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7034           ;; Set the current article marks.
7035           (let ((gnus-newsgroup-scored
7036                  (if (and (not gnus-save-score)
7037                           (not non-destructive))
7038                      nil
7039                    gnus-newsgroup-scored)))
7040             (save-excursion
7041               (gnus-update-marks)))
7042           ;; Do the cross-ref thing.
7043           (when gnus-use-cross-reference
7044             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7045           ;; Do not switch windows but change the buffer to work.
7046           (set-buffer gnus-group-buffer)
7047           (unless (gnus-ephemeral-group-p group)
7048             (gnus-group-update-group group)))))))
7049
7050 (defun gnus-summary-save-newsrc (&optional force)
7051   "Save the current number of read/marked articles in the dribble buffer.
7052 The dribble buffer will then be saved.
7053 If FORCE (the prefix), also save the .newsrc file(s)."
7054   (interactive "P")
7055   (gnus-summary-update-info t)
7056   (if force
7057       (gnus-save-newsrc-file)
7058     (gnus-dribble-save)))
7059
7060 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7061
7062 (defun gnus-summary-exit (&optional temporary leave-hidden)
7063   "Exit reading current newsgroup, and then return to group selection mode.
7064 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7065   (interactive)
7066   (gnus-set-global-variables)
7067   (when (gnus-buffer-live-p gnus-article-buffer)
7068     (with-current-buffer gnus-article-buffer
7069       (mm-destroy-parts gnus-article-mime-handles)
7070       ;; Set it to nil for safety reason.
7071       (setq gnus-article-mime-handle-alist nil)
7072       (setq gnus-article-mime-handles nil)))
7073   (gnus-kill-save-kill-buffer)
7074   (gnus-async-halt-prefetch)
7075   (let* ((group gnus-newsgroup-name)
7076          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7077          (gnus-group-is-exiting-p t)
7078          (mode major-mode)
7079          (group-point nil)
7080          (buf (current-buffer)))
7081     (unless quit-config
7082       ;; Do adaptive scoring, and possibly save score files.
7083       (when gnus-newsgroup-adaptive
7084         (gnus-score-adaptive))
7085       (when gnus-use-scoring
7086         (gnus-score-save)))
7087     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7088     (when gnus-use-cache
7089       (gnus-cache-possibly-remove-articles)
7090       (gnus-cache-save-buffers))
7091     (gnus-async-prefetch-remove-group group)
7092     (when gnus-suppress-duplicates
7093       (gnus-dup-enter-articles))
7094     (when gnus-use-trees
7095       (gnus-tree-close group))
7096     (when gnus-use-cache
7097       (gnus-cache-write-active))
7098     ;; Remove entries for this group.
7099     (nnmail-purge-split-history (gnus-group-real-name group))
7100     ;; Make all changes in this group permanent.
7101     (unless quit-config
7102       (gnus-run-hooks 'gnus-exit-group-hook)
7103       (gnus-summary-update-info))
7104     (gnus-close-group group)
7105     ;; Make sure where we were, and go to next newsgroup.
7106     (set-buffer gnus-group-buffer)
7107     (unless quit-config
7108       (gnus-group-jump-to-group group))
7109     (gnus-run-hooks 'gnus-summary-exit-hook)
7110     (unless (or quit-config
7111                 (not gnus-summary-next-group-on-exit)
7112                 ;; If this group has disappeared from the summary
7113                 ;; buffer, don't skip forwards.
7114                 (not (string= group (gnus-group-group-name))))
7115       (gnus-group-next-unread-group 1))
7116     (setq group-point (point))
7117     (if temporary
7118         nil                             ;Nothing to do.
7119       (set-buffer buf)
7120       (if (not gnus-kill-summary-on-exit)
7121           (progn
7122             (gnus-deaden-summary)
7123             (setq mode nil))
7124         (when (get-buffer gnus-article-buffer)
7125           (bury-buffer gnus-article-buffer))
7126         ;; Return to group mode buffer.
7127         (when (eq mode 'gnus-summary-mode)
7128           (gnus-kill-buffer buf)))
7129
7130       ;; If we have several article buffers, we kill them at exit.
7131       (unless gnus-single-article-buffer
7132         (when (gnus-buffer-live-p gnus-article-buffer)
7133           (with-current-buffer gnus-article-buffer
7134             ;; Don't kill sticky article buffers
7135             (unless (eq major-mode 'gnus-sticky-article-mode)
7136               (gnus-kill-buffer gnus-article-buffer)
7137               (setq gnus-article-current nil))))
7138         (gnus-kill-buffer gnus-original-article-buffer))
7139
7140       (setq gnus-current-select-method gnus-select-method)
7141       (set-buffer gnus-group-buffer)
7142       (if quit-config
7143           (gnus-handle-ephemeral-exit quit-config)
7144         (goto-char group-point)
7145         ;; If gnus-group-buffer is already displayed, make sure we also move
7146         ;; the cursor in the window that displays it.
7147         (let ((win (get-buffer-window (current-buffer) 0)))
7148           (if win (set-window-point win (point))))
7149         (unless leave-hidden
7150           (gnus-configure-windows 'group 'force)))
7151       ;; Clear the current group name.
7152       (unless quit-config
7153         (setq gnus-newsgroup-name nil)))))
7154
7155 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7156 (defun gnus-summary-exit-no-update (&optional no-questions)
7157   "Quit reading current newsgroup without updating read article info."
7158   (interactive)
7159   (let* ((group gnus-newsgroup-name)
7160          (gnus-group-is-exiting-p t)
7161          (gnus-group-is-exiting-without-update-p t)
7162          (quit-config (gnus-group-quit-config group)))
7163     (when (or no-questions
7164               gnus-expert-user
7165               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7166       (gnus-async-halt-prefetch)
7167       (run-hooks 'gnus-summary-prepare-exit-hook)
7168       (when (gnus-buffer-live-p gnus-article-buffer)
7169         (with-current-buffer gnus-article-buffer
7170           (mm-destroy-parts gnus-article-mime-handles)
7171           ;; Set it to nil for safety reason.
7172           (setq gnus-article-mime-handle-alist nil)
7173           (setq gnus-article-mime-handles nil)))
7174       ;; If we have several article buffers, we kill them at exit.
7175       (unless gnus-single-article-buffer
7176         (gnus-kill-buffer gnus-article-buffer)
7177         (gnus-kill-buffer gnus-original-article-buffer)
7178         (setq gnus-article-current nil))
7179       (if (not gnus-kill-summary-on-exit)
7180           (gnus-deaden-summary)
7181         (gnus-close-group group)
7182         (gnus-kill-buffer gnus-summary-buffer))
7183       (unless gnus-single-article-buffer
7184         (setq gnus-article-current nil))
7185       (when gnus-use-trees
7186         (gnus-tree-close group))
7187       (gnus-async-prefetch-remove-group group)
7188       (when (get-buffer gnus-article-buffer)
7189         (bury-buffer gnus-article-buffer))
7190       ;; Return to the group buffer.
7191       (gnus-configure-windows 'group 'force)
7192       ;; Clear the current group name.
7193       (setq gnus-newsgroup-name nil)
7194       (unless (gnus-ephemeral-group-p group)
7195         (gnus-group-update-group group))
7196       (when (equal (gnus-group-group-name) group)
7197         (gnus-group-next-unread-group 1))
7198       (when quit-config
7199         (gnus-handle-ephemeral-exit quit-config)))))
7200
7201 (defun gnus-handle-ephemeral-exit (quit-config)
7202   "Handle movement when leaving an ephemeral group.
7203 The state which existed when entering the ephemeral is reset."
7204   (if (not (buffer-name (car quit-config)))
7205       (gnus-configure-windows 'group 'force)
7206     (set-buffer (car quit-config))
7207     (cond ((eq major-mode 'gnus-summary-mode)
7208            (gnus-set-global-variables))
7209           ((eq major-mode 'gnus-article-mode)
7210            (save-current-buffer
7211              ;; The `gnus-summary-buffer' variable may point
7212              ;; to the old summary buffer when using a single
7213              ;; article buffer.
7214              (unless (gnus-buffer-live-p gnus-summary-buffer)
7215                (set-buffer gnus-group-buffer))
7216              (set-buffer gnus-summary-buffer)
7217              (gnus-set-global-variables))))
7218     (if (or (eq (cdr quit-config) 'article)
7219             (eq (cdr quit-config) 'pick))
7220         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7221             (gnus-configure-windows 'pick 'force)
7222           (gnus-configure-windows (cdr quit-config) 'force))
7223       (gnus-configure-windows (cdr quit-config) 'force))
7224     (when (eq major-mode 'gnus-summary-mode)
7225       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7226                                                      next-unread-noselect))
7227           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7228                                   'next-noselect)
7229                               (gnus-summary-next-subject 1 nil t))
7230                              ((eq gnus-auto-select-on-ephemeral-exit
7231                                   'next-unread-noselect)
7232                               (gnus-summary-next-subject 1 t t))))
7233             ;; Hide the article buffer which displays the article different
7234             ;; from the one that the cursor points to in the summary buffer.
7235             (gnus-configure-windows 'summary 'force))
7236         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7237                (gnus-summary-next-subject 1))
7238               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7239                (gnus-summary-next-subject 1 t))))
7240       (gnus-summary-recenter)
7241       (gnus-summary-position-point))))
7242
7243 ;;; Dead summaries.
7244
7245 (defvar gnus-dead-summary-mode-map
7246   (let ((map (make-keymap)))
7247     (suppress-keymap map)
7248     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7249     (dolist (key '("\C-d" "\r" "\177" [delete]))
7250       (define-key map key 'gnus-summary-wake-up-the-dead))
7251     (dolist (key '("q" "Q"))
7252       (define-key map key 'bury-buffer))
7253     map))
7254
7255 (define-minor-mode gnus-dead-summary-mode
7256   "Minor mode for Gnus summary buffers."
7257   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7258   (unless (derived-mode-p 'gnus-summary-mode)
7259     (setq gnus-dead-summary-mode nil)))
7260
7261 (defun gnus-deaden-summary ()
7262   "Make the current summary buffer into a dead summary buffer."
7263   ;; Kill any previous dead summary buffer.
7264   (when (and gnus-dead-summary
7265              (buffer-name gnus-dead-summary))
7266     (with-current-buffer gnus-dead-summary
7267       (when gnus-dead-summary-mode
7268         (kill-buffer (current-buffer)))))
7269   ;; Make this the current dead summary.
7270   (setq gnus-dead-summary (current-buffer))
7271   (gnus-dead-summary-mode 1)
7272   (let ((name (buffer-name)))
7273     (when (string-match "Summary" name)
7274       (rename-buffer
7275        (concat (substring name 0 (match-beginning 0)) "Dead "
7276                (substring name (match-beginning 0)))
7277        t)
7278       (bury-buffer))))
7279
7280 (defun gnus-kill-or-deaden-summary (buffer)
7281   "Kill or deaden the summary BUFFER."
7282   (save-excursion
7283     (when (and (buffer-name buffer)
7284                (not gnus-single-article-buffer))
7285       (with-current-buffer buffer
7286         (gnus-kill-buffer gnus-article-buffer)
7287         (gnus-kill-buffer gnus-original-article-buffer)))
7288     (cond
7289      ;; Kill the buffer.
7290      (gnus-kill-summary-on-exit
7291       (when (and gnus-use-trees
7292                  (gnus-buffer-exists-p buffer))
7293         (with-current-buffer buffer
7294           (gnus-tree-close gnus-newsgroup-name)))
7295       (gnus-kill-buffer buffer))
7296      ;; Deaden the buffer.
7297      ((gnus-buffer-exists-p buffer)
7298       (with-current-buffer buffer
7299         (gnus-deaden-summary))))))
7300
7301 (defun gnus-summary-wake-up-the-dead (&rest args)
7302   "Wake up the dead summary buffer."
7303   (interactive)
7304   (gnus-dead-summary-mode -1)
7305   (let ((name (buffer-name)))
7306     (when (string-match "Dead " name)
7307       (rename-buffer
7308        (concat (substring name 0 (match-beginning 0))
7309                (substring name (match-end 0)))
7310        t)))
7311   (gnus-message 3 "This dead summary is now alive again"))
7312
7313 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7314 (defun gnus-summary-describe-group (&optional force)
7315   "Describe the current newsgroup."
7316   (interactive "P")
7317   (gnus-group-describe-group force gnus-newsgroup-name))
7318
7319 (defun gnus-summary-describe-briefly ()
7320   "Describe summary mode commands briefly."
7321   (interactive)
7322   (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")))
7323
7324 ;; Walking around group mode buffer from summary mode.
7325
7326 (defun gnus-summary-next-group (&optional no-article target-group backward)
7327   "Exit current newsgroup and then select next unread newsgroup.
7328 If prefix argument NO-ARTICLE is non-nil, no article is selected
7329 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7330 previous group instead."
7331   (interactive "P")
7332   ;; Stop pre-fetching.
7333   (gnus-async-halt-prefetch)
7334   (let ((current-group gnus-newsgroup-name)
7335         (current-buffer (current-buffer))
7336         entered)
7337     ;; First we semi-exit this group to update Xrefs and all variables.
7338     ;; We can't do a real exit, because the window conf must remain
7339     ;; the same in case the user is prompted for info, and we don't
7340     ;; want the window conf to change before that...
7341     (gnus-summary-exit t)
7342     (while (not entered)
7343       ;; Then we find what group we are supposed to enter.
7344       (set-buffer gnus-group-buffer)
7345       (gnus-group-jump-to-group current-group)
7346       (setq target-group
7347             (or target-group
7348                 (if (eq gnus-keep-same-level 'best)
7349                     (gnus-summary-best-group gnus-newsgroup-name)
7350                   (gnus-summary-search-group backward gnus-keep-same-level))))
7351       (if (not target-group)
7352           ;; There are no further groups, so we return to the group
7353           ;; buffer.
7354           (progn
7355             (gnus-message 5 "Returning to the group buffer")
7356             (setq entered t)
7357             (when (gnus-buffer-live-p current-buffer)
7358               (set-buffer current-buffer)
7359               (gnus-summary-exit))
7360             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7361         ;; We try to enter the target group.
7362         (gnus-group-jump-to-group target-group)
7363         (let ((unreads (gnus-group-group-unread)))
7364           (if (and (or (eq t unreads)
7365                        (and unreads (not (zerop unreads))))
7366                    (gnus-summary-read-group
7367                     target-group nil no-article
7368                     (and (buffer-name current-buffer) current-buffer)
7369                     nil backward))
7370               (setq entered t)
7371             (setq current-group target-group
7372                   target-group nil)))))))
7373
7374 (defun gnus-summary-prev-group (&optional no-article)
7375   "Exit current newsgroup and then select previous unread newsgroup.
7376 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7377   (interactive "P")
7378   (gnus-summary-next-group no-article nil t))
7379
7380 ;; Walking around summary lines.
7381
7382 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7383   "Go to the first subject satisfying any non-nil constraint.
7384 If UNREAD is non-nil, the article should be unread.
7385 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7386 If UNSEEN is non-nil, the article should be unseen as well as unread.
7387 Returns the article selected or nil if there are no matching articles."
7388   (interactive "P")
7389   (cond
7390    ;; Empty summary.
7391    ((null gnus-newsgroup-data)
7392     (gnus-message 3 "No articles in the group")
7393     nil)
7394    ;; Pick the first article.
7395    ((not (or unread undownloaded unseen))
7396     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7397     (gnus-data-number (car gnus-newsgroup-data)))
7398    ;; Find the first unread article.
7399    (t
7400     (let ((data gnus-newsgroup-data))
7401       (while (and data
7402                   (let ((num (gnus-data-number (car data))))
7403                     (or (memq num gnus-newsgroup-unfetched)
7404                         (not (or (and unread
7405                                       (memq num gnus-newsgroup-unreads))
7406                                  (and undownloaded
7407                                       (memq num gnus-newsgroup-undownloaded))
7408                                  (and unseen
7409                                       (memq num gnus-newsgroup-unseen)
7410                                       (memq num gnus-newsgroup-unreads)))))))
7411         (setq data (cdr data)))
7412       (prog1
7413           (if data
7414               (progn
7415                 (goto-char (gnus-data-pos (car data)))
7416                 (gnus-data-number (car data)))
7417             (gnus-message 3 "No more%s articles"
7418                           (let* ((r (when unread " unread"))
7419                                  (d (when undownloaded " undownloaded"))
7420                                  (s (when unseen " unseen"))
7421                                  (l (delq nil (list r d s))))
7422                             (cond ((= 3 (length l))
7423                                    (concat r "," d ", or" s))
7424                                   ((= 2 (length l))
7425                                    (concat (car l) ", or" (cadr l)))
7426                                   ((= 1 (length l))
7427                                    (car l))
7428                                   (t
7429                                    ""))))
7430             nil
7431             )
7432         (gnus-summary-position-point))))))
7433
7434 (defun gnus-summary-next-subject (n &optional unread dont-display)
7435   "Go to next N'th summary line.
7436 If N is negative, go to the previous N'th subject line.
7437 If UNREAD is non-nil, only unread articles are selected.
7438 The difference between N and the actual number of steps taken is
7439 returned."
7440   (interactive "p")
7441   (let ((backward (< n 0))
7442         (n (abs n)))
7443     (while (and (> n 0)
7444                 (if backward
7445                     (gnus-summary-find-prev unread)
7446                   (gnus-summary-find-next unread)))
7447       (unless (zerop (setq n (1- n)))
7448         (gnus-summary-show-thread)))
7449     (when (/= 0 n)
7450       (gnus-message 7 "No more%s articles"
7451                     (if unread " unread" "")))
7452     (unless dont-display
7453       (gnus-summary-recenter)
7454       (gnus-summary-position-point))
7455     n))
7456
7457 (defun gnus-summary-next-unread-subject (n)
7458   "Go to next N'th unread summary line."
7459   (interactive "p")
7460   (gnus-summary-next-subject n t))
7461
7462 (defun gnus-summary-prev-subject (n &optional unread)
7463   "Go to previous N'th summary line.
7464 If optional argument UNREAD is non-nil, only unread article is selected."
7465   (interactive "p")
7466   (gnus-summary-next-subject (- n) unread))
7467
7468 (defun gnus-summary-prev-unread-subject (n)
7469   "Go to previous N'th unread summary line."
7470   (interactive "p")
7471   (gnus-summary-next-subject (- n) t))
7472
7473 (defun gnus-summary-goto-subjects (articles)
7474   "Insert the subject header for ARTICLES in the current buffer."
7475   (save-excursion
7476     (dolist (article articles)
7477       (gnus-summary-goto-subject article t)))
7478   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7479   (gnus-summary-position-point))
7480
7481 (defun gnus-summary-goto-subject (article &optional force silent)
7482   "Go to the subject line of ARTICLE.
7483 If FORCE, also allow jumping to articles not currently shown."
7484   (interactive "nArticle number: ")
7485   (unless (numberp article)
7486     (error "Article %s is not a number" article))
7487   (let ((b (point))
7488         (data (gnus-data-find article)))
7489     ;; We read in the article if we have to.
7490     (and (not data)
7491          force
7492          (gnus-summary-insert-subject
7493           article
7494           (if (or (numberp force) (vectorp force)) force)
7495           t)
7496          (setq data (gnus-data-find article)))
7497     (goto-char b)
7498     (if (not data)
7499         (progn
7500           (unless silent
7501             (gnus-message 3 "Can't find article %d" article))
7502           nil)
7503       (let ((pt (gnus-data-pos data)))
7504         (goto-char pt)
7505         (gnus-summary-set-article-display-arrow pt))
7506       (gnus-summary-position-point)
7507       article)))
7508
7509 ;; Walking around summary lines with displaying articles.
7510
7511 (defun gnus-summary-expand-window (&optional arg)
7512   "Make the summary buffer take up the entire Emacs frame.
7513 Given a prefix, will force an `article' buffer configuration."
7514   (interactive "P")
7515   (if arg
7516       (gnus-configure-windows 'article 'force)
7517     (gnus-configure-windows 'summary 'force)))
7518
7519 (defun gnus-summary-display-article (article &optional all-header)
7520   "Display ARTICLE in article buffer."
7521   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7522                (with-current-buffer gnus-article-buffer
7523                  (eq major-mode 'gnus-article-mode)))
7524     (gnus-article-setup-buffer))
7525   (gnus-set-global-variables)
7526   (with-current-buffer gnus-article-buffer
7527     (setq gnus-article-charset gnus-newsgroup-charset)
7528     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7529     (mm-enable-multibyte))
7530   (if (null article)
7531       nil
7532     (prog1
7533         (if gnus-summary-display-article-function
7534             (funcall gnus-summary-display-article-function article all-header)
7535           (gnus-article-prepare article all-header))
7536       (gnus-run-hooks 'gnus-select-article-hook)
7537       (when (and gnus-current-article
7538                  (not (zerop gnus-current-article)))
7539         (gnus-summary-goto-subject gnus-current-article))
7540       (gnus-summary-recenter)
7541       (when (and gnus-use-trees gnus-show-threads)
7542         (gnus-possibly-generate-tree article)
7543         (gnus-highlight-selected-tree article))
7544       ;; Successfully display article.
7545       (gnus-article-set-window-start
7546        (cdr (assq article gnus-newsgroup-bookmarks))))))
7547
7548 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7549   "Select the current article.
7550 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7551 non-nil, the article will be re-fetched even if it already present in
7552 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7553 be displayed."
7554   ;; Make sure we are in the summary buffer to work around bbdb bug.
7555   (unless (eq major-mode 'gnus-summary-mode)
7556     (set-buffer gnus-summary-buffer))
7557   (let ((article (or article (gnus-summary-article-number)))
7558         (all-headers (not (not all-headers))) ;Must be t or nil.
7559         gnus-summary-display-article-function)
7560     (and (not pseudo)
7561          (gnus-summary-article-pseudo-p article)
7562          (error "This is a pseudo-article"))
7563     (with-current-buffer gnus-summary-buffer
7564       (if (or (and gnus-single-article-buffer
7565                    (or (null gnus-current-article)
7566                        (null gnus-article-current)
7567                        (null (get-buffer gnus-article-buffer))
7568                        (not (eq article (cdr gnus-article-current)))
7569                        (not (equal (car gnus-article-current)
7570                                    gnus-newsgroup-name))))
7571               (and (not gnus-single-article-buffer)
7572                    (or (null gnus-current-article)
7573                        (not (eq gnus-current-article article))))
7574               force)
7575           ;; The requested article is different from the current article.
7576           (progn
7577             (gnus-summary-display-article article all-headers)
7578             (when (gnus-buffer-live-p gnus-article-buffer)
7579               (with-current-buffer gnus-article-buffer
7580                 (if (not gnus-article-decoded-p) ;; a local variable
7581                     (mm-disable-multibyte))))
7582             (gnus-article-set-window-start
7583              (cdr (assq article gnus-newsgroup-bookmarks)))
7584             article)
7585         'old))))
7586
7587 (defun gnus-summary-force-verify-and-decrypt ()
7588   "Display buttons for signed/encrypted parts and verify/decrypt them."
7589   (interactive)
7590   (let ((mm-verify-option 'known)
7591         (mm-decrypt-option 'known)
7592         (gnus-article-emulate-mime t)
7593         (gnus-buttonized-mime-types (append (list "multipart/signed"
7594                                                   "multipart/encrypted")
7595                                             gnus-buttonized-mime-types)))
7596     (gnus-summary-select-article nil 'force)))
7597
7598 (defun gnus-summary-set-current-mark (&optional current-mark)
7599   "Obsolete function."
7600   nil)
7601
7602 (defun gnus-summary-next-article (&optional unread subject backward push)
7603   "Select the next article.
7604 If UNREAD, only unread articles are selected.
7605 If SUBJECT, only articles with SUBJECT are selected.
7606 If BACKWARD, the previous article is selected instead of the next."
7607   (interactive "P")
7608   ;; Make sure we are in the summary buffer.
7609   (unless (eq major-mode 'gnus-summary-mode)
7610     (set-buffer gnus-summary-buffer))
7611   (cond
7612    ;; Is there such an article?
7613    ((and (gnus-summary-search-forward unread subject backward)
7614          (or (gnus-summary-display-article (gnus-summary-article-number))
7615              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7616     (gnus-summary-position-point))
7617    ;; If not, we try the first unread, if that is wanted.
7618    ((and subject
7619          gnus-auto-select-same
7620          (gnus-summary-first-unread-article))
7621     (gnus-summary-position-point)
7622     (gnus-message 6 "Wrapped"))
7623    ;; Try to get next/previous article not displayed in this group.
7624    ((and gnus-auto-extend-newsgroup
7625          (not unread) (not subject))
7626     (gnus-summary-goto-article
7627      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7628      nil (count-lines (point-min) (point))))
7629    ;; Go to next/previous group.
7630    (t
7631     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7632       (gnus-summary-jump-to-group gnus-newsgroup-name))
7633     (let ((cmd (if (featurep 'xemacs)
7634                    last-command-char
7635                  last-command-event))
7636           (point
7637            (with-current-buffer gnus-group-buffer
7638              (point)))
7639           (group
7640            (if (eq gnus-keep-same-level 'best)
7641                (gnus-summary-best-group gnus-newsgroup-name)
7642              (gnus-summary-search-group backward gnus-keep-same-level))))
7643       ;; For some reason, the group window gets selected.  We change
7644       ;; it back.
7645       (select-window (get-buffer-window (current-buffer)))
7646       ;; Select next unread newsgroup automagically.
7647       (cond
7648        ((or (not gnus-auto-select-next)
7649             (not cmd))
7650         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7651        ((or (eq gnus-auto-select-next 'quietly)
7652             (and (eq gnus-auto-select-next 'slightly-quietly)
7653                  push)
7654             (and (eq gnus-auto-select-next 'almost-quietly)
7655                  (gnus-summary-last-article-p)))
7656         ;; Select quietly.
7657         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7658             (gnus-summary-exit)
7659           (gnus-message 7 "No more%s articles (%s)..."
7660                         (if unread " unread" "")
7661                         (if group (concat "selecting " group)
7662                           "exiting"))
7663           (gnus-summary-next-group nil group backward)))
7664        (t
7665         (when (gnus-key-press-event-p last-input-event)
7666           (gnus-summary-walk-group-buffer
7667            gnus-newsgroup-name cmd unread backward point))))))))
7668
7669 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7670   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7671                       (?\C-p (gnus-group-prev-unread-group 1))))
7672         (cursor-in-echo-area t)
7673         keve key group ended prompt)
7674     (with-current-buffer gnus-group-buffer
7675       (goto-char start)
7676       (setq group
7677             (if (eq gnus-keep-same-level 'best)
7678                 (gnus-summary-best-group gnus-newsgroup-name)
7679               (gnus-summary-search-group backward gnus-keep-same-level))))
7680     (while (not ended)
7681       (setq prompt
7682             (format
7683              "No more%s articles%s " (if unread " unread" "")
7684              (if (and group
7685                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7686                  (format " (Type %s for %s [%s])"
7687                          (single-key-description cmd)
7688                          (gnus-group-decoded-name group)
7689                          (gnus-group-unread group))
7690                (format " (Type %s to exit %s)"
7691                        (single-key-description cmd)
7692                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7693       ;; Confirm auto selection.
7694       (setq key (car (setq keve (gnus-read-event-char prompt)))
7695             ended t)
7696       (cond
7697        ((assq key keystrokes)
7698         (let ((obuf (current-buffer)))
7699           (switch-to-buffer gnus-group-buffer)
7700           (when group
7701             (gnus-group-jump-to-group group))
7702           (eval (cadr (assq key keystrokes)))
7703           (setq group (gnus-group-group-name))
7704           (switch-to-buffer obuf))
7705         (setq ended nil))
7706        ((equal key cmd)
7707         (if (or (not group)
7708                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7709             (gnus-summary-exit)
7710           (gnus-summary-next-group nil group backward)))
7711        (t
7712         (push (cdr keve) unread-command-events))))))
7713
7714 (defun gnus-summary-next-unread-article ()
7715   "Select unread article after current one."
7716   (interactive)
7717   (gnus-summary-next-article
7718    (or (not (eq gnus-summary-goto-unread 'never))
7719        (gnus-summary-last-article-p (gnus-summary-article-number)))
7720    (and gnus-auto-select-same
7721         (gnus-summary-article-subject))))
7722
7723 (defun gnus-summary-prev-article (&optional unread subject)
7724   "Select the article before the current one.
7725 If UNREAD is non-nil, only unread articles are selected."
7726   (interactive "P")
7727   (gnus-summary-next-article unread subject t))
7728
7729 (defun gnus-summary-prev-unread-article ()
7730   "Select unread article before current one."
7731   (interactive)
7732   (gnus-summary-prev-article
7733    (or (not (eq gnus-summary-goto-unread 'never))
7734        (gnus-summary-first-article-p (gnus-summary-article-number)))
7735    (and gnus-auto-select-same
7736         (gnus-summary-article-subject))))
7737
7738 (defun gnus-summary-next-page (&optional lines circular stop)
7739   "Show next page of the selected article.
7740 If at the end of the current article, select the next article.
7741 LINES says how many lines should be scrolled up.
7742
7743 If CIRCULAR is non-nil, go to the start of the article instead of
7744 selecting the next article when reaching the end of the current
7745 article.
7746
7747 If STOP is non-nil, just stop when reaching the end of the message.
7748
7749 Also see the variable `gnus-article-skip-boring'."
7750   (interactive "P")
7751   (setq gnus-summary-buffer (current-buffer))
7752   (gnus-set-global-variables)
7753   (let ((article (gnus-summary-article-number))
7754         (article-window (get-buffer-window gnus-article-buffer t))
7755         endp)
7756     ;; If the buffer is empty, we have no article.
7757     (unless article
7758       (error "No article to select"))
7759     (gnus-configure-windows 'article)
7760     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7761         (if (and (eq gnus-summary-goto-unread 'never)
7762                  (not (gnus-summary-last-article-p article)))
7763             (gnus-summary-next-article)
7764           (gnus-summary-next-unread-article))
7765       (if (or (null gnus-current-article)
7766               (null gnus-article-current)
7767               (/= article (cdr gnus-article-current))
7768               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7769           ;; Selected subject is different from current article's.
7770           (gnus-summary-display-article article)
7771         (when article-window
7772           (gnus-eval-in-buffer-window gnus-article-buffer
7773             (setq endp (or (gnus-article-next-page lines)
7774                            (gnus-article-only-boring-p))))
7775           (when endp
7776             (cond ((or stop gnus-summary-stop-at-end-of-message)
7777                    (gnus-message 3 "End of message"))
7778                   (circular
7779                    (gnus-summary-beginning-of-article))
7780                   (lines
7781                    (gnus-message 3 "End of message"))
7782                   ((null lines)
7783                    (if (and (eq gnus-summary-goto-unread 'never)
7784                             (not (gnus-summary-last-article-p article)))
7785                        (gnus-summary-next-article)
7786                      (gnus-summary-next-unread-article))))))))
7787     (gnus-summary-recenter)
7788     (gnus-summary-position-point)))
7789
7790 (defun gnus-summary-prev-page (&optional lines move)
7791   "Show previous page of selected article.
7792 Argument LINES specifies lines to be scrolled down.
7793 If MOVE, move to the previous unread article if point is at
7794 the beginning of the buffer."
7795   (interactive "P")
7796   (let ((article (gnus-summary-article-number))
7797         (article-window (get-buffer-window gnus-article-buffer t))
7798         endp)
7799     (gnus-configure-windows 'article)
7800     (if (or (null gnus-current-article)
7801             (null gnus-article-current)
7802             (/= article (cdr gnus-article-current))
7803             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7804         ;; Selected subject is different from current article's.
7805         (gnus-summary-display-article article)
7806       (gnus-summary-recenter)
7807       (when article-window
7808         (gnus-eval-in-buffer-window gnus-article-buffer
7809           (setq endp (gnus-article-prev-page lines)))
7810         (when (and move endp)
7811           (cond (lines
7812                  (gnus-message 3 "Beginning of message"))
7813                 ((null lines)
7814                  (if (and (eq gnus-summary-goto-unread 'never)
7815                           (not (gnus-summary-first-article-p article)))
7816                      (gnus-summary-prev-article)
7817                    (gnus-summary-prev-unread-article))))))))
7818   (gnus-summary-position-point))
7819
7820 (defun gnus-summary-prev-page-or-article (&optional lines)
7821   "Show previous page of selected article.
7822 Argument LINES specifies lines to be scrolled down.
7823 If at the beginning of the article, go to the next article."
7824   (interactive "P")
7825   (gnus-summary-prev-page lines t))
7826
7827 (defun gnus-summary-scroll-up (lines)
7828   "Scroll up (or down) one line current article.
7829 Argument LINES specifies lines to be scrolled up (or down if negative).
7830 If no article is selected, then the current article will be selected first."
7831   (interactive "p")
7832   (gnus-configure-windows 'article)
7833   (gnus-summary-show-thread)
7834   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7835     (gnus-eval-in-buffer-window gnus-article-buffer
7836       (cond ((> lines 0)
7837              (when (gnus-article-next-page lines)
7838                (gnus-message 3 "End of message")))
7839             ((< lines 0)
7840              (gnus-article-prev-page (- lines))))))
7841   (gnus-summary-recenter)
7842   (gnus-summary-position-point))
7843
7844 (defun gnus-summary-scroll-down (lines)
7845   "Scroll down (or up) one line current article.
7846 Argument LINES specifies lines to be scrolled down (or up if negative).
7847 If no article is selected, then the current article will be selected first."
7848   (interactive "p")
7849   (gnus-summary-scroll-up (- lines)))
7850
7851 (defun gnus-summary-next-same-subject ()
7852   "Select next article which has the same subject as current one."
7853   (interactive)
7854   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7855
7856 (defun gnus-summary-prev-same-subject ()
7857   "Select previous article which has the same subject as current one."
7858   (interactive)
7859   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7860
7861 (defun gnus-summary-next-unread-same-subject ()
7862   "Select next unread article which has the same subject as current one."
7863   (interactive)
7864   (gnus-summary-next-article t (gnus-summary-article-subject)))
7865
7866 (defun gnus-summary-prev-unread-same-subject ()
7867   "Select previous unread article which has the same subject as current one."
7868   (interactive)
7869   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7870
7871 (defun gnus-summary-first-unread-article ()
7872   "Select the first unread article.
7873 Return nil if there are no unread articles."
7874   (interactive)
7875   (prog1
7876       (when (gnus-summary-first-subject t)
7877         (gnus-summary-show-thread)
7878         (gnus-summary-first-subject t)
7879         (gnus-summary-display-article (gnus-summary-article-number)))
7880     (gnus-summary-position-point)))
7881
7882 (defun gnus-summary-first-unread-subject ()
7883   "Place the point on the subject line of the first unread article.
7884 Return nil if there are no unread articles."
7885   (interactive)
7886   (prog1
7887       (when (gnus-summary-first-subject t)
7888         (gnus-summary-show-thread)
7889         (gnus-summary-first-subject t))
7890     (gnus-summary-position-point)))
7891
7892 (defun gnus-summary-first-unseen-subject ()
7893   "Place the point on the subject line of the first unseen article.
7894 Return nil if there are no unseen articles."
7895   (interactive)
7896   (prog1
7897       (when (gnus-summary-first-subject nil nil t)
7898         (gnus-summary-show-thread)
7899         (gnus-summary-first-subject nil nil t))
7900     (gnus-summary-position-point)))
7901
7902 (defun gnus-summary-first-unseen-or-unread-subject ()
7903   "Place the point on the subject line of the first unseen and unread article.
7904 If all article have been seen, on the subject line of the first unread
7905 article."
7906   (interactive)
7907   (prog1
7908       (unless (when (gnus-summary-first-subject nil nil t)
7909                 (gnus-summary-show-thread)
7910                 (gnus-summary-first-subject nil nil t))
7911         (when (gnus-summary-first-subject t)
7912           (gnus-summary-show-thread)
7913           (gnus-summary-first-subject t)))
7914     (gnus-summary-position-point)))
7915
7916 (defun gnus-summary-first-article ()
7917   "Select the first article.
7918 Return nil if there are no articles."
7919   (interactive)
7920   (prog1
7921       (when (gnus-summary-first-subject)
7922         (gnus-summary-show-thread)
7923         (gnus-summary-first-subject)
7924         (gnus-summary-display-article (gnus-summary-article-number)))
7925     (gnus-summary-position-point)))
7926
7927 (defun gnus-summary-best-unread-article (&optional arg)
7928   "Select the unread article with the highest score.
7929 If given a prefix argument, select the next unread article that has a
7930 score higher than the default score."
7931   (interactive "P")
7932   (let ((article (if arg
7933                      (gnus-summary-better-unread-subject)
7934                    (gnus-summary-best-unread-subject))))
7935     (if article
7936         (gnus-summary-goto-article article)
7937       (error "No unread articles"))))
7938
7939 (defun gnus-summary-best-unread-subject ()
7940   "Select the unread subject with the highest score."
7941   (interactive)
7942   (let ((best -1000000)
7943         (data gnus-newsgroup-data)
7944         article score)
7945     (while data
7946       (and (gnus-data-unread-p (car data))
7947            (> (setq score
7948                     (gnus-summary-article-score (gnus-data-number (car data))))
7949               best)
7950            (setq best score
7951                  article (gnus-data-number (car data))))
7952       (setq data (cdr data)))
7953     (when article
7954       (gnus-summary-goto-subject article))
7955     (gnus-summary-position-point)
7956     article))
7957
7958 (defun gnus-summary-better-unread-subject ()
7959   "Select the first unread subject that has a score over the default score."
7960   (interactive)
7961   (let ((data gnus-newsgroup-data)
7962         article score)
7963     (while (and (setq article (gnus-data-number (car data)))
7964                 (or (gnus-data-read-p (car data))
7965                     (not (> (gnus-summary-article-score article)
7966                             gnus-summary-default-score))))
7967       (setq data (cdr data)))
7968     (when article
7969       (gnus-summary-goto-subject article))
7970     (gnus-summary-position-point)
7971     article))
7972
7973 (defun gnus-summary-last-subject ()
7974   "Go to the last displayed subject line in the group."
7975   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7976     (when article
7977       (gnus-summary-goto-subject article))))
7978
7979 (defun gnus-summary-goto-article (article &optional all-headers force)
7980   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7981 If ALL-HEADERS is non-nil, no header lines are hidden.
7982 If FORCE, go to the article even if it isn't displayed.  If FORCE
7983 is a number, it is the line the article is to be displayed on."
7984   (interactive
7985    (list
7986     (gnus-completing-read
7987      "Article number or Message-ID"
7988      (mapcar 'int-to-string gnus-newsgroup-limit))
7989     current-prefix-arg
7990     t))
7991   (prog1
7992       (if (and (stringp article)
7993                (string-match "@\\|%40" article))
7994           (gnus-summary-refer-article article)
7995         (when (stringp article)
7996           (setq article (string-to-number article)))
7997         (if (gnus-summary-goto-subject article force)
7998             (gnus-summary-display-article article all-headers)
7999           (gnus-message 4 "Couldn't go to article %s" article) nil))
8000     (gnus-summary-position-point)))
8001
8002 (defun gnus-summary-goto-last-article ()
8003   "Go to the previously read article."
8004   (interactive)
8005   (prog1
8006       (when gnus-last-article
8007         (gnus-summary-goto-article gnus-last-article nil t))
8008     (gnus-summary-position-point)))
8009
8010 (defun gnus-summary-pop-article (number)
8011   "Pop one article off the history and go to the previous.
8012 NUMBER articles will be popped off."
8013   (interactive "p")
8014   (let (to)
8015     (setq gnus-newsgroup-history
8016           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8017     (if to
8018         (gnus-summary-goto-article (car to) nil t)
8019       (error "Article history empty")))
8020   (gnus-summary-position-point))
8021
8022 ;; Summary commands and functions for limiting the summary buffer.
8023
8024 (defun gnus-summary-limit-to-articles (n)
8025   "Limit the summary buffer to the next N articles.
8026 If not given a prefix, use the process marked articles instead."
8027   (interactive "P")
8028   (prog1
8029       (let ((articles (gnus-summary-work-articles n)))
8030         (setq gnus-newsgroup-processable nil)
8031         (gnus-summary-limit articles))
8032     (gnus-summary-position-point)))
8033
8034 (defun gnus-summary-pop-limit (&optional total)
8035   "Restore the previous limit.
8036 If given a prefix, remove all limits."
8037   (interactive "P")
8038   (when total
8039     (setq gnus-newsgroup-limits
8040           (list (mapcar (lambda (h) (mail-header-number h))
8041                         gnus-newsgroup-headers))))
8042   (unless gnus-newsgroup-limits
8043     (error "No limit to pop"))
8044   (prog1
8045       (gnus-summary-limit nil 'pop)
8046     (gnus-summary-position-point)))
8047
8048 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8049   "Limit the summary buffer to articles that have subjects that match a regexp.
8050 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8051   (interactive
8052    (list (read-string (if current-prefix-arg
8053                           "Exclude subject (regexp): "
8054                         "Limit to subject (regexp): "))
8055          nil current-prefix-arg))
8056   (unless header
8057     (setq header "subject"))
8058   (when (not (equal "" subject))
8059     (prog1
8060         (let ((articles (gnus-summary-find-matching
8061                          (or header "subject") subject 'all nil nil
8062                          not-matching)))
8063           (unless articles
8064             (error "Found no matches for \"%s\"" subject))
8065           (gnus-summary-limit articles))
8066       (gnus-summary-position-point))))
8067
8068 (defun gnus-summary-limit-to-author (from &optional not-matching)
8069   "Limit the summary buffer to articles that have authors that match a regexp.
8070 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8071   (interactive
8072    (list (read-string (if current-prefix-arg
8073                           "Exclude author (regexp): "
8074                         "Limit to author (regexp): "))
8075          current-prefix-arg))
8076   (gnus-summary-limit-to-subject from "from" not-matching))
8077
8078 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8079   "Limit the summary buffer to articles with the given RECIPIENT.
8080
8081 If NOT-MATCHING, exclude RECIPIENT.
8082
8083 To and Cc headers are checked.  You need to include them in
8084 `nnmail-extra-headers'."
8085   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8086   (interactive
8087    (list (read-string (format "%s recipient (regexp): "
8088                               (if current-prefix-arg "Exclude" "Limit to")))
8089          current-prefix-arg))
8090   (when (not (equal "" recipient))
8091     (prog1 (let* ((to
8092                    (if (memq 'To nnmail-extra-headers)
8093                        (gnus-summary-find-matching
8094                         (cons 'extra 'To) recipient 'all nil nil
8095                         not-matching)
8096                      (gnus-message
8097                       1 "`To' isn't present in `nnmail-extra-headers'")
8098                      (sit-for 1)
8099                      nil))
8100                   (cc
8101                    (if (memq 'Cc nnmail-extra-headers)
8102                        (gnus-summary-find-matching
8103                         (cons 'extra 'Cc) recipient 'all nil nil
8104                         not-matching)
8105                      (gnus-message
8106                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8107                      (sit-for 1)
8108                      nil))
8109                   (articles
8110                    (if not-matching
8111                        ;; We need the numbers that are in both lists:
8112                        (mapcar (lambda (a)
8113                                  (and (memq a to) a))
8114                                cc)
8115                      (nconc to cc))))
8116              (unless articles
8117                (error "Found no matches for \"%s\"" recipient))
8118              (gnus-summary-limit articles))
8119       (gnus-summary-position-point))))
8120
8121 (defun gnus-summary-limit-to-address (address &optional not-matching)
8122   "Limit the summary buffer to articles with the given ADDRESS.
8123
8124 If NOT-MATCHING, exclude ADDRESS.
8125
8126 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8127 in `nnmail-extra-headers'."
8128   (interactive
8129    (list (read-string (format "%s address (regexp): "
8130                               (if current-prefix-arg "Exclude" "Limit to")))
8131          current-prefix-arg))
8132   (when (not (equal "" address))
8133     (prog1 (let* ((to
8134                    (if (memq 'To nnmail-extra-headers)
8135                        (gnus-summary-find-matching
8136                         (cons 'extra 'To) address 'all nil nil
8137                         not-matching)
8138                      (gnus-message
8139                       1 "`To' isn't present in `nnmail-extra-headers'")
8140                      (sit-for 1)
8141                      t))
8142                   (cc
8143                    (if (memq 'Cc nnmail-extra-headers)
8144                        (gnus-summary-find-matching
8145                         (cons 'extra 'Cc) address 'all nil nil
8146                         not-matching)
8147                      (gnus-message
8148                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8149                      (sit-for 1)
8150                      t))
8151                   (from
8152                    (gnus-summary-find-matching "from" address
8153                                                'all nil nil not-matching))
8154                   (articles
8155                    (if not-matching
8156                        ;; We need the numbers that are in all lists:
8157                        (if (eq cc t)
8158                            (if (eq to t)
8159                                from
8160                              (mapcar (lambda (a) (car (memq a from))) to))
8161                          (if (eq to t)
8162                              (mapcar (lambda (a) (car (memq a from))) cc)
8163                            (mapcar (lambda (a) (car (memq a from)))
8164                                    (mapcar (lambda (a) (car (memq a to)))
8165                                            cc))))
8166                      (nconc (if (eq to t) nil to)
8167                             (if (eq cc t) nil cc)
8168                             from))))
8169              (unless articles
8170                (error "Found no matches for \"%s\"" address))
8171              (gnus-summary-limit articles))
8172       (gnus-summary-position-point))))
8173
8174 (defun gnus-summary-limit-strange-charsets-predicate (header)
8175   (when (fboundp 'char-charset)
8176     (let ((string (concat (mail-header-subject header)
8177                           (mail-header-from header)))
8178           charset found)
8179       (dotimes (i (1- (length string)))
8180         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8181         (when (string-match "unicode\\|big\\|japanese" charset)
8182           (setq found t)))
8183       found)))
8184
8185 (defun gnus-summary-limit-to-predicate (predicate)
8186   "Limit to articles where PREDICATE returns non-nil.
8187 PREDICATE will be called with the header structures of the
8188 articles."
8189   (let ((articles nil)
8190         (case-fold-search t))
8191     (dolist (header gnus-newsgroup-headers)
8192       (when (funcall predicate header)
8193         (push (mail-header-number header) articles)))
8194     (gnus-summary-limit (nreverse articles))))
8195
8196 (defun gnus-summary-limit-to-age (age &optional younger-p)
8197   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8198 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8199 articles that are younger than AGE days."
8200   (interactive
8201    (let ((younger current-prefix-arg)
8202          (days-got nil)
8203          days)
8204      (while (not days-got)
8205        (setq days (if younger
8206                       (read-string "Limit to articles younger than (in days, older when negative): ")
8207                     (read-string
8208                      "Limit to articles older than (in days, younger when negative): ")))
8209        (when (> (length days) 0)
8210          (setq days (read days)))
8211        (if (numberp days)
8212            (progn
8213              (setq days-got t)
8214              (when (< days 0)
8215                (setq younger (not younger))
8216                (setq days (* days -1))))
8217          (message "Please enter a number.")
8218          (sleep-for 1)))
8219      (list days younger)))
8220   (prog1
8221       (let ((data gnus-newsgroup-data)
8222             (cutoff (days-to-time age))
8223             articles d date is-younger)
8224         (while (setq d (pop data))
8225           (when (and (vectorp (gnus-data-header d))
8226                      (setq date (mail-header-date (gnus-data-header d))))
8227             (setq is-younger (time-less-p
8228                               (time-since (gnus-date-get-time date))
8229                               cutoff))
8230             (when (if younger-p
8231                       is-younger
8232                     (not is-younger))
8233               (push (gnus-data-number d) articles))))
8234         (gnus-summary-limit (nreverse articles)))
8235     (gnus-summary-position-point)))
8236
8237 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8238   "Limit the summary buffer to articles that match an 'extra' header."
8239   (interactive
8240    (let ((header
8241           (intern
8242            (gnus-completing-read
8243             (if current-prefix-arg
8244                 "Exclude extra header"
8245               "Limit extra header")
8246             (mapcar 'symbol-name gnus-extra-headers)
8247             t nil nil
8248             (symbol-name (car gnus-extra-headers))))))
8249      (list header
8250            (read-string (format "%s header %s (regexp): "
8251                                 (if current-prefix-arg "Exclude" "Limit to")
8252                                 header))
8253            current-prefix-arg)))
8254   (when (not (equal "" regexp))
8255     (prog1
8256         (let ((articles (gnus-summary-find-matching
8257                          (cons 'extra header) regexp 'all nil nil
8258                          not-matching)))
8259           (unless articles
8260             (error "Found no matches for \"%s\"" regexp))
8261           (gnus-summary-limit articles))
8262       (gnus-summary-position-point))))
8263
8264 (defun gnus-summary-limit-to-display-predicate ()
8265   "Limit the summary buffer to the predicated in the `display' group parameter."
8266   (interactive)
8267   (unless gnus-newsgroup-display
8268     (error "There is no `display' group parameter"))
8269   (let (articles)
8270     (dolist (number gnus-newsgroup-articles)
8271       (when (funcall gnus-newsgroup-display)
8272         (push number articles)))
8273     (gnus-summary-limit articles))
8274   (gnus-summary-position-point))
8275
8276 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8277 (make-obsolete
8278  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8279
8280 (defun gnus-summary-limit-to-unread (&optional all)
8281   "Limit the summary buffer to articles that are not marked as read.
8282 If ALL is non-nil, limit strictly to unread articles."
8283   (interactive "P")
8284   (if all
8285       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8286     (gnus-summary-limit-to-marks
8287      ;; Concat all the marks that say that an article is read and have
8288      ;; those removed.
8289      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8290            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8291            gnus-low-score-mark gnus-expirable-mark
8292            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8293            gnus-duplicate-mark)
8294      'reverse)))
8295
8296 (defun gnus-summary-limit-to-headers (match &optional reverse)
8297   "Limit the summary buffer to articles that have headers that match MATCH.
8298 If REVERSE (the prefix), limit to articles that don't match."
8299   (interactive "sMatch headers (regexp): \nP")
8300   (gnus-summary-limit-to-bodies match reverse t))
8301
8302 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8303   "Limit the summary buffer to articles that have bodies that match MATCH.
8304 If REVERSE (the prefix), limit to articles that don't match."
8305   (interactive "sMatch body (regexp): \nP")
8306   (let ((articles nil)
8307         (gnus-select-article-hook nil)  ;Disable hook.
8308         (gnus-article-prepare-hook nil)
8309         (gnus-use-article-prefetch nil)
8310         (gnus-keep-backlog nil)
8311         (gnus-break-pages nil)
8312         (gnus-summary-display-arrow nil)
8313         (gnus-updated-mode-lines nil)
8314         (gnus-auto-center-summary nil)
8315         (gnus-display-mime-function nil))
8316     (dolist (data gnus-newsgroup-data)
8317       (let (gnus-mark-article-hook)
8318         (gnus-summary-select-article t t nil (gnus-data-number data)))
8319       (with-current-buffer gnus-article-buffer
8320         (article-goto-body)
8321         (let* ((case-fold-search t)
8322                (found (if headersp
8323                           (re-search-backward match nil t)
8324                         (re-search-forward match nil t))))
8325           (when (or (and found
8326                          (not reverse))
8327                     (and (not found)
8328                          reverse))
8329             (push (gnus-data-number data) articles)))))
8330     (if (not articles)
8331         (message "No messages matched")
8332       (gnus-summary-limit articles)))
8333   (gnus-summary-position-point))
8334
8335 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8336   "Limit the summary buffer to articles that aren't part on any thread.
8337 If THREADSP (the prefix), limit to articles that are in threads."
8338   (interactive "P")
8339   (let ((articles nil)
8340         thread-articles
8341         threads)
8342     (dolist (thread gnus-newsgroup-threads)
8343       (if (stringp (car thread))
8344           (dolist (thread (cdr thread))
8345             (push thread threads))
8346         (push thread threads)))
8347     (dolist (thread threads)
8348       (setq thread-articles (gnus-articles-in-thread thread))
8349       (when (or (and threadsp
8350                      (> (length thread-articles) 1))
8351                 (and (not threadsp)
8352                      (= (length thread-articles) 1)))
8353         (setq articles (nconc thread-articles articles))))
8354     (if (not articles)
8355         (message "No messages matched")
8356       (gnus-summary-limit articles))
8357     (gnus-summary-position-point)))
8358
8359 (defun gnus-summary-limit-to-replied (&optional unreplied)
8360   "Limit the summary buffer to replied articles.
8361 If UNREPLIED (the prefix), limit to unreplied articles."
8362   (interactive "P")
8363   (if unreplied
8364       (gnus-summary-limit
8365        (gnus-set-difference gnus-newsgroup-articles
8366         gnus-newsgroup-replied))
8367     (gnus-summary-limit gnus-newsgroup-replied))
8368   (gnus-summary-position-point))
8369
8370 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8371 (make-obsolete 'gnus-summary-delete-marked-with
8372                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8373
8374 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8375   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8376 If REVERSE, limit the summary buffer to articles that are marked
8377 with MARKS.  MARKS can either be a string of marks or a list of marks.
8378 Returns how many articles were removed."
8379   (interactive "sMarks: ")
8380   (gnus-summary-limit-to-marks marks t))
8381
8382 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8383   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8384 If REVERSE (the prefix), limit the summary buffer to articles that are
8385 not marked with MARKS.  MARKS can either be a string of marks or a
8386 list of marks.
8387 Returns how many articles were removed."
8388   (interactive "sMarks: \nP")
8389   (prog1
8390       (let ((data gnus-newsgroup-data)
8391             (marks (if (listp marks) marks
8392                      (append marks nil))) ; Transform to list.
8393             articles)
8394         (while data
8395           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8396                   (memq (gnus-data-mark (car data)) marks))
8397             (push (gnus-data-number (car data)) articles))
8398           (setq data (cdr data)))
8399         (gnus-summary-limit articles))
8400     (gnus-summary-position-point)))
8401
8402 (defun gnus-summary-limit-to-score (score)
8403   "Limit to articles with score at or above SCORE."
8404   (interactive "NLimit to articles with score of at least: ")
8405   (let ((data gnus-newsgroup-data)
8406         articles)
8407     (while data
8408       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8409                 score)
8410         (push (gnus-data-number (car data)) articles))
8411       (setq data (cdr data)))
8412     (prog1
8413         (gnus-summary-limit articles)
8414       (gnus-summary-position-point))))
8415
8416 (defun gnus-summary-limit-to-unseen ()
8417   "Limit to unseen articles."
8418   (interactive)
8419   (prog1
8420       (gnus-summary-limit gnus-newsgroup-unseen)
8421     (gnus-summary-position-point)))
8422
8423 (defun gnus-summary-limit-include-thread (id)
8424   "Display all the hidden articles that is in the thread with ID in it.
8425 When called interactively, ID is the Message-ID of the current
8426 article."
8427   (interactive (list (mail-header-id (gnus-summary-article-header))))
8428   (let ((articles (gnus-articles-in-thread
8429                    (gnus-id-to-thread (gnus-root-id id)))))
8430     (prog1
8431         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8432       (gnus-summary-limit-include-matching-articles
8433        "subject"
8434        (regexp-quote (gnus-simplify-subject-re
8435                       (mail-header-subject (gnus-id-to-header id)))))
8436       (gnus-summary-position-point))))
8437
8438 (defun gnus-summary-limit-include-matching-articles (header regexp)
8439   "Display all the hidden articles that have HEADERs that match REGEXP."
8440   (interactive (list (read-string "Match on header: ")
8441                      (read-string "Regexp: ")))
8442   (let ((articles (gnus-find-matching-articles header regexp)))
8443     (prog1
8444         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8445       (gnus-summary-position-point))))
8446
8447 (defun gnus-summary-insert-dormant-articles ()
8448   "Insert all the dormant articles for this group into the current buffer."
8449   (interactive)
8450   (let ((gnus-verbose (max 6 gnus-verbose)))
8451     (if (not gnus-newsgroup-dormant)
8452         (gnus-message 3 "No dormant articles for this group")
8453       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8454
8455 (defun gnus-summary-insert-ticked-articles ()
8456   "Insert ticked articles for this group into the current buffer."
8457   (interactive)
8458   (let ((gnus-verbose (max 6 gnus-verbose)))
8459     (if (not gnus-newsgroup-marked)
8460         (gnus-message 3 "No ticked articles for this group")
8461       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8462
8463 (defun gnus-summary-limit-include-dormant ()
8464   "Display all the hidden articles that are marked as dormant.
8465 Note that this command only works on a subset of the articles currently
8466 fetched for this group."
8467   (interactive)
8468   (unless gnus-newsgroup-dormant
8469     (error "There are no dormant articles in this group"))
8470   (prog1
8471       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8472     (gnus-summary-position-point)))
8473
8474 (defun gnus-summary-limit-exclude-dormant ()
8475   "Hide all dormant articles."
8476   (interactive)
8477   (prog1
8478       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8479     (gnus-summary-position-point)))
8480
8481 (defun gnus-summary-limit-exclude-childless-dormant ()
8482   "Hide all dormant articles that have no children."
8483   (interactive)
8484   (let ((data (gnus-data-list t))
8485         articles d children)
8486     ;; Find all articles that are either not dormant or have
8487     ;; children.
8488     (while (setq d (pop data))
8489       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8490                 (and (setq children
8491                            (gnus-article-children (gnus-data-number d)))
8492                      (let (found)
8493                        (while children
8494                          (when (memq (car children) articles)
8495                            (setq children nil
8496                                  found t))
8497                          (pop children))
8498                        found)))
8499         (push (gnus-data-number d) articles)))
8500     ;; Do the limiting.
8501     (prog1
8502         (gnus-summary-limit articles)
8503       (gnus-summary-position-point))))
8504
8505 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8506   "Mark all unread excluded articles as read.
8507 If ALL, mark even excluded ticked and dormants as read."
8508   (interactive "P")
8509   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8510   (let ((articles (gnus-sorted-ndifference
8511                    (sort
8512                     (mapcar (lambda (h) (mail-header-number h))
8513                             gnus-newsgroup-headers)
8514                     '<)
8515                    gnus-newsgroup-limit))
8516         article)
8517     (setq gnus-newsgroup-unreads
8518           (gnus-sorted-intersection gnus-newsgroup-unreads
8519                                     gnus-newsgroup-limit))
8520     (if all
8521         (setq gnus-newsgroup-dormant nil
8522               gnus-newsgroup-marked nil
8523               gnus-newsgroup-reads
8524               (nconc
8525                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8526                gnus-newsgroup-reads))
8527       (while (setq article (pop articles))
8528         (unless (or (memq article gnus-newsgroup-dormant)
8529                     (memq article gnus-newsgroup-marked))
8530           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8531
8532 (defun gnus-summary-limit (articles &optional pop)
8533   (if pop
8534       ;; We pop the previous limit off the stack and use that.
8535       (setq articles (car gnus-newsgroup-limits)
8536             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8537     ;; We use the new limit, so we push the old limit on the stack.
8538     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8539   ;; Set the limit.
8540   (setq gnus-newsgroup-limit articles)
8541   (let ((total (length gnus-newsgroup-data))
8542         (data (gnus-data-find-list (gnus-summary-article-number)))
8543         (gnus-summary-mark-below nil)   ; Inhibit this.
8544         found)
8545     ;; This will do all the work of generating the new summary buffer
8546     ;; according to the new limit.
8547     (gnus-summary-prepare)
8548     ;; Hide any threads, possibly.
8549     (gnus-summary-maybe-hide-threads)
8550     ;; Try to return to the article you were at, or one in the
8551     ;; neighborhood.
8552     (when data
8553       ;; We try to find some article after the current one.
8554       (while data
8555         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8556           (setq data nil
8557                 found t))
8558         (setq data (cdr data))))
8559     (unless found
8560       ;; If there is no data, that means that we were after the last
8561       ;; article.  The same goes when we can't find any articles
8562       ;; after the current one.
8563       (goto-char (point-max))
8564       (gnus-summary-find-prev))
8565     (gnus-set-mode-line 'summary)
8566     ;; We return how many articles were removed from the summary
8567     ;; buffer as a result of the new limit.
8568     (- total (length gnus-newsgroup-data))))
8569
8570 (defsubst gnus-invisible-cut-children (threads)
8571   (let ((num 0))
8572     (while threads
8573       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8574         (incf num))
8575       (pop threads))
8576     (< num 2)))
8577
8578 (defsubst gnus-cut-thread (thread)
8579   "Go forwards in the thread until we find an article that we want to display."
8580   (when (or (eq gnus-fetch-old-headers 'some)
8581             (eq gnus-fetch-old-headers 'invisible)
8582             (numberp gnus-fetch-old-headers)
8583             (eq gnus-build-sparse-threads 'some)
8584             (eq gnus-build-sparse-threads 'more))
8585     ;; Deal with old-fetched headers and sparse threads.
8586     (while (and
8587             thread
8588             (or
8589              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8590              (gnus-summary-article-ancient-p
8591               (mail-header-number (car thread))))
8592             (if (or (<= (length (cdr thread)) 1)
8593                     (eq gnus-fetch-old-headers 'invisible))
8594                 (setq gnus-newsgroup-limit
8595                       (delq (mail-header-number (car thread))
8596                             gnus-newsgroup-limit)
8597                       thread (cadr thread))
8598               (when (gnus-invisible-cut-children (cdr thread))
8599                 (let ((th (cdr thread)))
8600                   (while th
8601                     (if (memq (mail-header-number (caar th))
8602                               gnus-newsgroup-limit)
8603                         (setq thread (car th)
8604                               th nil)
8605                       (setq th (cdr th))))))))))
8606   thread)
8607
8608 (defun gnus-cut-threads (threads)
8609   "Cut off all uninteresting articles from the beginning of THREADS."
8610   (when (or (eq gnus-fetch-old-headers 'some)
8611             (eq gnus-fetch-old-headers 'invisible)
8612             (numberp gnus-fetch-old-headers)
8613             (eq gnus-build-sparse-threads 'some)
8614             (eq gnus-build-sparse-threads 'more))
8615     (let ((th threads))
8616       (while th
8617         (setcar th (gnus-cut-thread (car th)))
8618         (setq th (cdr th)))))
8619   ;; Remove nixed out threads.
8620   (delq nil threads))
8621
8622 (defun gnus-summary-initial-limit (&optional show-if-empty)
8623   "Figure out what the initial limit is supposed to be on group entry.
8624 This entails weeding out unwanted dormants, low-scored articles,
8625 fetch-old-headers verbiage, and so on."
8626   ;; Most groups have nothing to remove.
8627   (unless (or gnus-inhibit-limiting
8628               (and (null gnus-newsgroup-dormant)
8629                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8630                    (not (eq gnus-fetch-old-headers 'some))
8631                    (not (numberp gnus-fetch-old-headers))
8632                    (not (eq gnus-fetch-old-headers 'invisible))
8633                    (null gnus-summary-expunge-below)
8634                    (not (eq gnus-build-sparse-threads 'some))
8635                    (not (eq gnus-build-sparse-threads 'more))
8636                    (null gnus-thread-expunge-below)))
8637     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8638     (setq gnus-newsgroup-limit nil)
8639     (mapatoms
8640      (lambda (node)
8641        (unless (car (symbol-value node))
8642          ;; These threads have no parents -- they are roots.
8643          (let ((nodes (cdr (symbol-value node)))
8644                thread)
8645            (while nodes
8646              (if (and gnus-thread-expunge-below
8647                       (< (gnus-thread-total-score (car nodes))
8648                          gnus-thread-expunge-below))
8649                  (gnus-expunge-thread (pop nodes))
8650                (setq thread (pop nodes))
8651                (gnus-summary-limit-children thread))))))
8652      gnus-newsgroup-dependencies)
8653     ;; If this limitation resulted in an empty group, we might
8654     ;; pop the previous limit and use it instead.
8655     (when (and (not gnus-newsgroup-limit)
8656                show-if-empty)
8657       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8658     gnus-newsgroup-limit))
8659
8660 (defun gnus-summary-limit-children (thread)
8661   "Return 1 if this subthread is visible and 0 if it is not."
8662   ;; First we get the number of visible children to this thread.  This
8663   ;; is done by recursing down the thread using this function, so this
8664   ;; will really go down to a leaf article first, before slowly
8665   ;; working its way up towards the root.
8666   (when thread
8667     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8668            (children
8669            (if (cdr thread)
8670                (apply '+ (mapcar 'gnus-summary-limit-children
8671                                  (cdr thread)))
8672              0))
8673           (number (mail-header-number (car thread)))
8674           score)
8675       (if (and
8676            (not (memq number gnus-newsgroup-marked))
8677            (or
8678             ;; If this article is dormant and has absolutely no visible
8679             ;; children, then this article isn't visible.
8680             (and (memq number gnus-newsgroup-dormant)
8681                  (zerop children))
8682             ;; If this is "fetch-old-headered" and there is no
8683             ;; visible children, then we don't want this article.
8684             (and (or (eq gnus-fetch-old-headers 'some)
8685                      (numberp gnus-fetch-old-headers))
8686                  (gnus-summary-article-ancient-p number)
8687                  (zerop children))
8688             ;; If this is "fetch-old-headered" and `invisible', then
8689             ;; we don't want this article.
8690             (and (eq gnus-fetch-old-headers 'invisible)
8691                  (gnus-summary-article-ancient-p number))
8692             ;; If this is a sparsely inserted article with no children,
8693             ;; we don't want it.
8694             (and (eq gnus-build-sparse-threads 'some)
8695                  (gnus-summary-article-sparse-p number)
8696                  (zerop children))
8697             ;; If we use expunging, and this article is really
8698             ;; low-scored, then we don't want this article.
8699             (when (and gnus-summary-expunge-below
8700                        (< (setq score
8701                                 (or (cdr (assq number gnus-newsgroup-scored))
8702                                     gnus-summary-default-score))
8703                           gnus-summary-expunge-below))
8704               ;; We increase the expunge-tally here, but that has
8705               ;; nothing to do with the limits, really.
8706               (incf gnus-newsgroup-expunged-tally)
8707               ;; We also mark as read here, if that's wanted.
8708               (when (and gnus-summary-mark-below
8709                          (< score gnus-summary-mark-below))
8710                 (setq gnus-newsgroup-unreads
8711                       (delq number gnus-newsgroup-unreads))
8712                 (if gnus-newsgroup-auto-expire
8713                     (push number gnus-newsgroup-expirable)
8714                   (push (cons number gnus-low-score-mark)
8715                         gnus-newsgroup-reads)))
8716               t)
8717             ;; Do the `display' group parameter.
8718             (and gnus-newsgroup-display
8719                  (not (funcall gnus-newsgroup-display)))))
8720           ;; Nope, invisible article.
8721           0
8722         ;; Ok, this article is to be visible, so we add it to the limit
8723         ;; and return 1.
8724         (push number gnus-newsgroup-limit)
8725         1))))
8726
8727 (defun gnus-expunge-thread (thread)
8728   "Mark all articles in THREAD as read."
8729   (let* ((number (mail-header-number (car thread))))
8730     (incf gnus-newsgroup-expunged-tally)
8731     ;; We also mark as read here, if that's wanted.
8732     (setq gnus-newsgroup-unreads
8733           (delq number gnus-newsgroup-unreads))
8734     (if gnus-newsgroup-auto-expire
8735         (push number gnus-newsgroup-expirable)
8736       (push (cons number gnus-low-score-mark)
8737             gnus-newsgroup-reads)))
8738   ;; Go recursively through all subthreads.
8739   (mapcar 'gnus-expunge-thread (cdr thread)))
8740
8741 ;; Summary article oriented commands
8742
8743 (defun gnus-summary-refer-parent-article (n)
8744   "Refer parent article N times.
8745 If N is negative, go to ancestor -N instead.
8746 The difference between N and the number of articles fetched is returned."
8747   (interactive "p")
8748   (let ((skip 1)
8749         error header ref)
8750     (when (not (natnump n))
8751       (setq skip (abs n)
8752             n 1))
8753     (while (and (> n 0)
8754                 (not error))
8755       (setq header (gnus-summary-article-header))
8756       (if (and (eq (mail-header-number header)
8757                    (cdr gnus-article-current))
8758                (equal gnus-newsgroup-name
8759                       (car gnus-article-current)))
8760           ;; If we try to find the parent of the currently
8761           ;; displayed article, then we take a look at the actual
8762           ;; References header, since this is slightly more
8763           ;; reliable than the References field we got from the
8764           ;; server.
8765           (with-current-buffer gnus-original-article-buffer
8766             (nnheader-narrow-to-headers)
8767             (unless (setq ref (message-fetch-field "references"))
8768               (when (setq ref (message-fetch-field "in-reply-to"))
8769                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8770             (widen))
8771         (setq ref
8772               ;; It's not the current article, so we take a bet on
8773               ;; the value we got from the server.
8774               (mail-header-references header)))
8775       (if (and ref
8776                (not (equal ref "")))
8777           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8778             (gnus-message 1 "Couldn't find parent"))
8779         (gnus-message 1 "No references in article %d"
8780                       (gnus-summary-article-number))
8781         (setq error t))
8782       (decf n))
8783     (gnus-summary-position-point)
8784     n))
8785
8786 (defun gnus-summary-refer-references ()
8787   "Fetch all articles mentioned in the References header.
8788 Return the number of articles fetched."
8789   (interactive)
8790   (let ((ref (mail-header-references (gnus-summary-article-header)))
8791         (current (gnus-summary-article-number))
8792         (n 0))
8793     (if (or (not ref)
8794             (equal ref ""))
8795         (error "No References in the current article")
8796       ;; For each Message-ID in the References header...
8797       (while (string-match "<[^>]*>" ref)
8798         (incf n)
8799         ;; ... fetch that article.
8800         (gnus-summary-refer-article
8801          (prog1 (match-string 0 ref)
8802            (setq ref (substring ref (match-end 0))))))
8803       (gnus-summary-goto-subject current)
8804       (gnus-summary-position-point)
8805       n)))
8806
8807 (defun gnus-summary-refer-thread (&optional limit)
8808   "Fetch all articles in the current thread.
8809 If LIMIT (the numerical prefix), fetch that many old headers instead
8810 of what's specified by the `gnus-refer-thread-limit' variable."
8811   (interactive "P")
8812   (let ((id (mail-header-id (gnus-summary-article-header)))
8813         (limit (if limit (prefix-numeric-value limit)
8814                  gnus-refer-thread-limit)))
8815     (unless (eq gnus-fetch-old-headers 'invisible)
8816       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8817       ;; Retrieve the headers and read them in.
8818       (if (eq (if (numberp limit)
8819                   (gnus-retrieve-headers
8820                    (list (min
8821                           (+ (mail-header-number
8822                               (gnus-summary-article-header))
8823                              limit)
8824                           gnus-newsgroup-end))
8825                    gnus-newsgroup-name (* limit 2))
8826                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8827                 ;; headers.
8828                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8829                                        gnus-newsgroup-name limit))
8830               'nov)
8831           (gnus-build-all-threads)
8832         (error "Can't fetch thread from back ends that don't support NOV"))
8833       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8834     (gnus-summary-limit-include-thread id)))
8835
8836 (defun gnus-summary-refer-article (message-id)
8837   "Fetch an article specified by MESSAGE-ID."
8838   (interactive "sMessage-ID: ")
8839   (when (and (stringp message-id)
8840              (not (zerop (length message-id))))
8841     (setq message-id (gnus-replace-in-string message-id " " ""))
8842     ;; Construct the correct Message-ID if necessary.
8843     ;; Suggested by tale@pawl.rpi.edu.
8844     (unless (string-match "^<" message-id)
8845       (setq message-id (concat "<" message-id)))
8846     (unless (string-match ">$" message-id)
8847       (setq message-id (concat message-id ">")))
8848     ;; People often post MIDs from URLs, so unhex it:
8849     (unless (string-match "@" message-id)
8850       (setq message-id (gnus-url-unhex-string message-id)))
8851     (let* ((header (gnus-id-to-header message-id))
8852            (sparse (and header
8853                         (gnus-summary-article-sparse-p
8854                          (mail-header-number header))
8855                         (memq (mail-header-number header)
8856                               gnus-newsgroup-limit)))
8857            number)
8858       (cond
8859        ;; If the article is present in the buffer we just go to it.
8860        ((and header
8861              (or (not (gnus-summary-article-sparse-p
8862                        (mail-header-number header)))
8863                  sparse))
8864         (prog1
8865             (gnus-summary-goto-article
8866              (mail-header-number header) nil t)
8867           (when sparse
8868             (gnus-summary-update-article (mail-header-number header)))))
8869        (t
8870         ;; We fetch the article.
8871         (catch 'found
8872           (dolist (gnus-override-method (gnus-refer-article-methods))
8873             (when (and (gnus-check-server gnus-override-method)
8874                        ;; Fetch the header,
8875                        (setq number (gnus-summary-insert-subject message-id)))
8876               ;; and display the article.
8877               (gnus-summary-select-article nil nil nil number)
8878               (throw 'found t)))
8879           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8880
8881 (defun gnus-refer-article-methods ()
8882   "Return a list of referable methods."
8883   (cond
8884    ;; No method, so we default to current and native.
8885    ((null gnus-refer-article-method)
8886     (list gnus-current-select-method gnus-select-method))
8887    ;; Current.
8888    ((eq 'current gnus-refer-article-method)
8889     (list gnus-current-select-method))
8890    ;; List of select methods.
8891    ((not (and (symbolp (car gnus-refer-article-method))
8892               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8893     (let (out)
8894       (dolist (method gnus-refer-article-method)
8895         (push (if (eq 'current method)
8896                   gnus-current-select-method
8897                 method)
8898               out))
8899       (nreverse out)))
8900    ;; One single select method.
8901    (t
8902     (list gnus-refer-article-method))))
8903
8904 (defun gnus-summary-edit-parameters ()
8905   "Edit the group parameters of the current group."
8906   (interactive)
8907   (gnus-group-edit-group gnus-newsgroup-name 'params))
8908
8909 (defun gnus-summary-customize-parameters ()
8910   "Customize the group parameters of the current group."
8911   (interactive)
8912   (gnus-group-customize gnus-newsgroup-name))
8913
8914 (defun gnus-summary-enter-digest-group (&optional force)
8915   "Enter an nndoc group based on the current article.
8916 If FORCE, force a digest interpretation.  If not, try
8917 to guess what the document format is."
8918   (interactive "P")
8919   (let ((conf gnus-current-window-configuration))
8920     (save-window-excursion
8921       (save-excursion
8922         (let (gnus-article-prepare-hook
8923               gnus-display-mime-function
8924               gnus-break-pages)
8925           (gnus-summary-select-article))))
8926     (setq gnus-current-window-configuration conf)
8927     (let* ((name (format "%s-%d"
8928                          (gnus-group-prefixed-name
8929                           gnus-newsgroup-name (list 'nndoc ""))
8930                          (with-current-buffer gnus-summary-buffer
8931                            gnus-current-article)))
8932            (ogroup gnus-newsgroup-name)
8933            (params (append (gnus-info-params (gnus-get-info ogroup))
8934                            (list (cons 'to-group ogroup))
8935                            (list (cons 'parent-group ogroup))
8936                            (list (cons 'save-article-group ogroup))))
8937            (case-fold-search t)
8938            (buf (current-buffer))
8939            dig to-address)
8940       (with-current-buffer gnus-original-article-buffer
8941         ;; Have the digest group inherit the main mail address of
8942         ;; the parent article.
8943         (when (setq to-address (or (gnus-fetch-field "reply-to")
8944                                    (gnus-fetch-field "from")))
8945           (setq params
8946                 (append
8947                  (list (cons 'to-address
8948                              (funcall gnus-decode-encoded-address-function
8949                                       to-address))))))
8950         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8951         (insert-buffer-substring gnus-original-article-buffer)
8952         ;; Remove lines that may lead nndoc to misinterpret the
8953         ;; document type.
8954         (narrow-to-region
8955          (goto-char (point-min))
8956          (or (search-forward "\n\n" nil t) (point)))
8957         (goto-char (point-min))
8958         (delete-matching-lines "^Path:\\|^From ")
8959         (widen))
8960       (unwind-protect
8961           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8962                     (gnus-newsgroup-ephemeral-ignored-charsets
8963                      gnus-newsgroup-ignored-charsets))
8964                 (gnus-group-read-ephemeral-group
8965                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8966                               (nndoc-article-type
8967                                ,(if force 'mbox 'guess)))
8968                  t nil nil nil
8969                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8970                                                         "ADAPT")))))
8971               ;; Make all postings to this group go to the parent group.
8972               (nconc (gnus-info-params (gnus-get-info name))
8973                      params)
8974             ;; Couldn't select this doc group.
8975             (switch-to-buffer buf)
8976             (gnus-set-global-variables)
8977             (gnus-configure-windows 'summary)
8978             (gnus-message 3 "Article couldn't be entered?"))
8979         (kill-buffer dig)))))
8980
8981 (defun gnus-summary-read-document (n)
8982   "Open a new group based on the current article(s).
8983 This will allow you to read digests and other similar
8984 documents as newsgroups.
8985 Obeys the standard process/prefix convention."
8986   (interactive "P")
8987   (let* ((ogroup gnus-newsgroup-name)
8988          (params (append (gnus-info-params (gnus-get-info ogroup))
8989                          (list (cons 'to-group ogroup))))
8990          group egroup groups vgroup)
8991     (dolist (article (gnus-summary-work-articles n))
8992       (setq group (format "%s-%d" gnus-newsgroup-name article))
8993       (gnus-summary-remove-process-mark article)
8994       (when (gnus-summary-display-article article)
8995         (save-excursion ;;What for?
8996           (with-temp-buffer
8997             (insert-buffer-substring gnus-original-article-buffer)
8998             ;; Remove some headers that may lead nndoc to make
8999             ;; the wrong guess.
9000             (message-narrow-to-head)
9001             (goto-char (point-min))
9002             (delete-matching-lines "^Path:\\|^From ")
9003             (widen)
9004             (if (setq egroup
9005                       (gnus-group-read-ephemeral-group
9006                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9007                                      (nndoc-article-type guess))
9008                        t nil t))
9009                 (progn
9010                   ;; Make all postings to this group go to the parent group.
9011                   (nconc (gnus-info-params (gnus-get-info egroup))
9012                          params)
9013                   (push egroup groups))
9014               ;; Couldn't select this doc group.
9015               (gnus-error 3 "Article couldn't be entered"))))))
9016     ;; Now we have selected all the documents.
9017     (cond
9018      ((not groups)
9019       (error "None of the articles could be interpreted as documents"))
9020      ((gnus-group-read-ephemeral-group
9021        (setq vgroup (format
9022                      "nnvirtual:%s-%s" gnus-newsgroup-name
9023                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9024        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9025        t
9026        (cons (current-buffer) 'summary)))
9027      (t
9028       (error "Couldn't select virtual nndoc group")))))
9029
9030 (defun gnus-summary-isearch-article (&optional regexp-p)
9031   "Do incremental search forward on the current article.
9032 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9033   (interactive "P")
9034   (gnus-summary-select-article)
9035   (gnus-configure-windows 'article)
9036   (gnus-eval-in-buffer-window gnus-article-buffer
9037     (save-restriction
9038       (widen)
9039       (isearch-forward regexp-p))))
9040
9041 (defun gnus-summary-repeat-search-article-forward ()
9042   "Repeat the previous search forwards."
9043   (interactive)
9044   (unless gnus-last-search-regexp
9045     (error "No previous search"))
9046   (gnus-summary-search-article-forward gnus-last-search-regexp))
9047
9048 (defun gnus-summary-repeat-search-article-backward ()
9049   "Repeat the previous search backwards."
9050   (interactive)
9051   (unless gnus-last-search-regexp
9052     (error "No previous search"))
9053   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9054
9055 (defun gnus-summary-search-article-forward (regexp &optional backward)
9056   "Search for an article containing REGEXP forward.
9057 If BACKWARD, search backward instead."
9058   (interactive
9059    (list (read-string
9060           (format "Search article %s (regexp%s): "
9061                   (if current-prefix-arg "backward" "forward")
9062                   (if gnus-last-search-regexp
9063                       (concat ", default " gnus-last-search-regexp)
9064                     "")))
9065          current-prefix-arg))
9066   (if (string-equal regexp "")
9067       (setq regexp (or gnus-last-search-regexp ""))
9068     (setq gnus-last-search-regexp regexp)
9069     (setq gnus-article-before-search gnus-current-article))
9070   ;; Intentionally set gnus-last-article.
9071   (setq gnus-last-article gnus-article-before-search)
9072   (let ((gnus-last-article gnus-last-article))
9073     (if (gnus-summary-search-article regexp backward)
9074         (gnus-summary-show-thread)
9075       (signal 'search-failed (list regexp)))))
9076
9077 (defun gnus-summary-search-article-backward (regexp)
9078   "Search for an article containing REGEXP backward."
9079   (interactive
9080    (list (read-string
9081           (format "Search article backward (regexp%s): "
9082                   (if gnus-last-search-regexp
9083                       (concat ", default " gnus-last-search-regexp)
9084                     "")))))
9085   (gnus-summary-search-article-forward regexp 'backward))
9086
9087 (defun gnus-summary-search-article (regexp &optional backward)
9088   "Search for an article containing REGEXP.
9089 Optional argument BACKWARD means do search for backward.
9090 `gnus-select-article-hook' is not called during the search."
9091   ;; We have to require this here to make sure that the following
9092   ;; dynamic binding isn't shadowed by autoloading.
9093   (require 'gnus-async)
9094   (require 'gnus-art)
9095   (let ((gnus-select-article-hook nil)  ;Disable hook.
9096         (gnus-article-prepare-hook nil)
9097         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9098         (gnus-use-article-prefetch nil)
9099         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9100         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9101         (gnus-visual nil)
9102         (gnus-keep-backlog nil)
9103         (gnus-break-pages nil)
9104         (gnus-summary-display-arrow nil)
9105         (gnus-updated-mode-lines nil)
9106         (gnus-auto-center-summary nil)
9107         (sum (current-buffer))
9108         (gnus-display-mime-function nil)
9109         (found nil)
9110         point)
9111     (gnus-save-hidden-threads
9112       (gnus-summary-select-article)
9113       (set-buffer gnus-article-buffer)
9114       (goto-char (window-point (get-buffer-window (current-buffer))))
9115       (when backward
9116         (forward-line -1))
9117       (while (not found)
9118         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9119         (if (if backward
9120                 (re-search-backward regexp nil t)
9121               (re-search-forward regexp nil t))
9122             ;; We found the regexp.
9123             (progn
9124               (setq found 'found)
9125               (beginning-of-line)
9126               (set-window-start
9127                (get-buffer-window (current-buffer))
9128                (point))
9129               (forward-line 1)
9130               (set-window-point
9131                (get-buffer-window (current-buffer))
9132                (point))
9133               (set-buffer sum)
9134               (setq point (point)))
9135           ;; We didn't find it, so we go to the next article.
9136           (set-buffer sum)
9137           (setq found 'not)
9138           (while (eq found 'not)
9139             (if (not (if backward (gnus-summary-find-prev)
9140                        (gnus-summary-find-next)))
9141                 ;; No more articles.
9142                 (setq found t)
9143               ;; Select the next article and adjust point.
9144               (unless (gnus-summary-article-sparse-p
9145                        (gnus-summary-article-number))
9146                 (setq found nil)
9147                 (gnus-summary-select-article)
9148                 (set-buffer gnus-article-buffer)
9149                 (widen)
9150                 (goto-char (if backward (point-max) (point-min))))))))
9151       (gnus-message 7 ""))
9152     ;; Return whether we found the regexp.
9153     (when (eq found 'found)
9154       (goto-char point)
9155       (gnus-summary-show-thread)
9156       (gnus-summary-goto-subject gnus-current-article)
9157       (gnus-summary-position-point)
9158       t)))
9159
9160 (defun gnus-find-matching-articles (header regexp)
9161   "Return a list of all articles that match REGEXP on HEADER.
9162 This search includes all articles in the current group that Gnus has
9163 fetched headers for, whether they are displayed or not."
9164   (let ((articles nil)
9165         ;; Can't eta-reduce because it's a macro.
9166         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9167         (case-fold-search t))
9168     (dolist (header gnus-newsgroup-headers)
9169       (when (string-match regexp (funcall func header))
9170         (push (mail-header-number header) articles)))
9171     (nreverse articles)))
9172
9173 (defun gnus-summary-find-matching (header regexp &optional backward unread
9174                                           not-case-fold not-matching)
9175   "Return a list of all articles that match REGEXP on HEADER.
9176 The search stars on the current article and goes forwards unless
9177 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9178 If UNREAD is non-nil, only unread articles will
9179 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9180 in the comparisons. If NOT-MATCHING, return a list of all articles that
9181 not match REGEXP on HEADER."
9182   (let ((case-fold-search (not not-case-fold))
9183         articles d func)
9184     (if (consp header)
9185         (if (eq (car header) 'extra)
9186             (setq func
9187                   `(lambda (h)
9188                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9189                          "")))
9190           (error "%s is an invalid header" header))
9191       (unless (fboundp (intern (concat "mail-header-" header)))
9192         (error "%s is not a valid header" header))
9193       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9194     (dolist (d (if (eq backward 'all)
9195                    gnus-newsgroup-data
9196                  (gnus-data-find-list
9197                   (gnus-summary-article-number)
9198                   (gnus-data-list backward))))
9199       (when (and (or (not unread)       ; We want all articles...
9200                      (gnus-data-unread-p d)) ; Or just unreads.
9201                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9202                  (if not-matching
9203                      (not (string-match
9204                            regexp
9205                            (funcall func (gnus-data-header d))))
9206                    (string-match regexp
9207                                  (funcall func (gnus-data-header d)))))
9208         (push (gnus-data-number d) articles))) ; Success!
9209     (nreverse articles)))
9210
9211 (defun gnus-summary-execute-command (header regexp command &optional backward)
9212   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9213 If HEADER is an empty string (or nil), the match is done on the entire
9214 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9215   (interactive
9216    (list (let ((completion-ignore-case t))
9217            (gnus-completing-read
9218             "Header name"
9219             (mapcar 'symbol-name
9220                     (append
9221                      '(Number Subject From Lines Date
9222                        Message-ID Xref References Body)
9223                      gnus-extra-headers))
9224             'require-match))
9225          (read-string "Regexp: ")
9226          (read-key-sequence "Command: ")
9227          current-prefix-arg))
9228   (when (equal header "Body")
9229     (setq header ""))
9230   ;; Hidden thread subtrees must be searched as well.
9231   (gnus-summary-show-all-threads)
9232   ;; We don't want to change current point nor window configuration.
9233   (save-excursion
9234     (save-window-excursion
9235       (let (gnus-visual
9236             gnus-treat-strip-trailing-blank-lines
9237             gnus-treat-strip-leading-blank-lines
9238             gnus-treat-strip-multiple-blank-lines
9239             gnus-treat-hide-boring-headers
9240             gnus-treat-fold-newsgroups
9241             gnus-article-prepare-hook)
9242         (gnus-message 6 "Executing %s..." (key-description command))
9243         ;; We'd like to execute COMMAND interactively so as to give arguments.
9244         (gnus-execute header regexp
9245                       `(call-interactively ',(key-binding command))
9246                       backward)
9247         (gnus-message 6 "Executing %s...done" (key-description command))))))
9248
9249 (defun gnus-summary-beginning-of-article ()
9250   "Scroll the article back to the beginning."
9251   (interactive)
9252   (gnus-summary-select-article)
9253   (gnus-configure-windows 'article)
9254   (gnus-eval-in-buffer-window gnus-article-buffer
9255     (widen)
9256     (goto-char (point-min))
9257     (when gnus-break-pages
9258       (gnus-narrow-to-page))))
9259
9260 (defun gnus-summary-end-of-article ()
9261   "Scroll to the end of the article."
9262   (interactive)
9263   (gnus-summary-select-article)
9264   (gnus-configure-windows 'article)
9265   (gnus-eval-in-buffer-window gnus-article-buffer
9266     (widen)
9267     (goto-char (point-max))
9268     (recenter -3)
9269     (when gnus-break-pages
9270       (gnus-narrow-to-page))))
9271
9272 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9273   "Truncate to LEN and quote all \"(\"'s in STRING."
9274   (gnus-replace-in-string (if (and len (> (length string) len))
9275                               (substring string 0 len)
9276                             string)
9277                           "[()]" "\\\\\\&"))
9278
9279 (defun gnus-summary-print-article (&optional filename n)
9280   "Generate and print a PostScript image of the process-marked (mail) articles.
9281
9282 If used interactively, print the current article if none are
9283 process-marked.  With prefix arg, prompt the user for the name of the
9284 file to save in.
9285
9286 When used from Lisp, accept two optional args FILENAME and N.  N means
9287 to print the next N articles.  If N is negative, print the N previous
9288 articles.  If N is nil and articles have been marked with the process
9289 mark, print these instead.
9290
9291 If the optional first argument FILENAME is nil, send the image to the
9292 printer.  If FILENAME is a string, save the PostScript image in a file with
9293 that name.  If FILENAME is a number, prompt the user for the name of the file
9294 to save in."
9295   (interactive (list (ps-print-preprint current-prefix-arg)))
9296   (dolist (article (gnus-summary-work-articles n))
9297     (gnus-summary-select-article nil nil 'pseudo article)
9298     (gnus-eval-in-buffer-window gnus-article-buffer
9299       (gnus-print-buffer))
9300     (gnus-summary-remove-process-mark article))
9301   (ps-despool filename))
9302
9303 (defun gnus-print-buffer ()
9304   (let ((buffer (generate-new-buffer " *print*")))
9305     (unwind-protect
9306         (progn
9307           (copy-to-buffer buffer (point-min) (point-max))
9308           (set-buffer buffer)
9309           (gnus-remove-text-with-property 'gnus-decoration)
9310           (when (gnus-visual-p 'article-highlight 'highlight)
9311             ;; Copy-to-buffer doesn't copy overlay.  So redo
9312             ;; highlight.
9313             (let ((gnus-article-buffer buffer))
9314               (gnus-article-highlight-citation t)
9315               (gnus-article-highlight-signature)
9316               (gnus-article-emphasize)
9317               (gnus-article-delete-invisible-text)))
9318           (let ((ps-left-header
9319                  (list
9320                   (concat "("
9321                           (gnus-summary-print-truncate-and-quote
9322                            (mail-header-subject gnus-current-headers)
9323                            66) ")")
9324                   (concat "("
9325                           (gnus-summary-print-truncate-and-quote
9326                            (mail-header-from gnus-current-headers)
9327                            45) ")")))
9328                 (ps-right-header
9329                  (list
9330                   "/pagenumberstring load"
9331                   (concat "("
9332                           (mail-header-date gnus-current-headers) ")"))))
9333             (gnus-run-hooks 'gnus-ps-print-hook)
9334             (save-excursion
9335               (if ps-print-color-p
9336                   (ps-spool-buffer-with-faces)
9337                 (ps-spool-buffer)))))
9338       (kill-buffer buffer))))
9339
9340 (defun gnus-summary-show-complete-article ()
9341   "Show a complete version of the current article.
9342 This is only useful if you're looking at a partial version of the
9343 article currently."
9344   (interactive)
9345   (let ((gnus-keep-backlog nil)
9346         (gnus-use-cache nil)
9347         (gnus-agent nil)
9348         (variable (intern
9349                    (format "%s-fetch-partial-articles"
9350                            (car (gnus-find-method-for-group
9351                                  gnus-newsgroup-name)))
9352                    obarray))
9353         old-val)
9354     (unwind-protect
9355         (progn
9356           (setq old-val (symbol-value variable))
9357           (set variable nil)
9358           (gnus-flush-original-article-buffer)
9359           (gnus-summary-show-article))
9360       (set variable old-val))))
9361
9362 (defun gnus-summary-show-article (&optional arg)
9363   "Force redisplaying of the current article.
9364 If ARG (the prefix) is a number, show the article with the charset
9365 defined in `gnus-summary-show-article-charset-alist', or the charset
9366 input.
9367 If ARG (the prefix) is non-nil and not a number, show the raw article
9368 without any article massaging functions being run.  Normally, the key
9369 strokes are `C-u g'."
9370   (interactive "P")
9371   (cond
9372    ((numberp arg)
9373     (gnus-summary-show-article t)
9374     (let ((gnus-newsgroup-charset
9375            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9376                (mm-read-coding-system
9377                 "View as charset: " ;; actually it is coding system.
9378                 (with-current-buffer gnus-article-buffer
9379                   (mm-detect-coding-region (point) (point-max))))))
9380           (gnus-newsgroup-ignored-charsets 'gnus-all))
9381       (gnus-summary-select-article nil 'force)
9382       (let ((deps gnus-newsgroup-dependencies)
9383             head header lines)
9384         (with-current-buffer gnus-original-article-buffer
9385           (save-restriction
9386             (message-narrow-to-head)
9387             (setq head (buffer-string))
9388             (goto-char (point-min))
9389             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9390               (goto-char (point-max))
9391               (widen)
9392               (setq lines (1- (count-lines (point) (point-max))))))
9393           (with-temp-buffer
9394             (insert (format "211 %d Article retrieved.\n"
9395                             (cdr gnus-article-current)))
9396             (insert head)
9397             (if lines (insert (format "Lines: %d\n" lines)))
9398             (insert ".\n")
9399             (let ((nntp-server-buffer (current-buffer)))
9400               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9401         (gnus-data-set-header
9402          (gnus-data-find (cdr gnus-article-current))
9403          header)
9404         (gnus-summary-update-article-line
9405          (cdr gnus-article-current) header)
9406         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9407           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9408    ((not arg)
9409     ;; Select the article the normal way.
9410     (gnus-summary-select-article nil 'force))
9411    (t
9412     ;; We have to require this here to make sure that the following
9413     ;; dynamic binding isn't shadowed by autoloading.
9414     (require 'gnus-async)
9415     (require 'gnus-art)
9416     ;; Bind the article treatment functions to nil.
9417     (let ((gnus-have-all-headers t)
9418           gnus-article-prepare-hook
9419           gnus-article-decode-hook
9420           gnus-display-mime-function
9421           gnus-break-pages)
9422       ;; Destroy any MIME parts.
9423       (when (gnus-buffer-live-p gnus-article-buffer)
9424         (with-current-buffer gnus-article-buffer
9425           (mm-destroy-parts gnus-article-mime-handles)
9426           ;; Set it to nil for safety reason.
9427           (setq gnus-article-mime-handle-alist nil)
9428           (setq gnus-article-mime-handles nil)))
9429       (gnus-summary-select-article nil 'force))))
9430   (gnus-summary-goto-subject gnus-current-article)
9431   (gnus-summary-position-point))
9432
9433 (defun gnus-summary-show-raw-article ()
9434   "Show the raw article without any article massaging functions being run."
9435   (interactive)
9436   (gnus-summary-show-article t))
9437
9438 (defun gnus-summary-verbose-headers (&optional arg)
9439   "Toggle permanent full header display.
9440 If ARG is a positive number, turn header display on.
9441 If ARG is a negative number, turn header display off."
9442   (interactive "P")
9443   (setq gnus-show-all-headers
9444         (cond ((or (not (numberp arg))
9445                    (zerop arg))
9446                (not gnus-show-all-headers))
9447               ((natnump arg)
9448                t)))
9449   (gnus-summary-show-article))
9450
9451 (defun gnus-summary-toggle-header (&optional arg)
9452   "Show the headers if they are hidden, or hide them if they are shown.
9453 If ARG is a positive number, show the entire header.
9454 If ARG is a negative number, hide the unwanted header lines."
9455   (interactive "P")
9456   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9457                      (get-buffer-window gnus-article-buffer t))))
9458     (with-current-buffer gnus-article-buffer
9459       (widen)
9460       (article-narrow-to-head)
9461       (let* ((inhibit-read-only t)
9462              (inhibit-point-motion-hooks t)
9463              (hidden (if (numberp arg)
9464                          (>= arg 0)
9465                        (or (not (looking-at "[^ \t\n]+:"))
9466                            (gnus-article-hidden-text-p 'headers))))
9467              s e)
9468         (delete-region (point-min) (point-max))
9469         (with-current-buffer gnus-original-article-buffer
9470           (goto-char (setq s (point-min)))
9471           (setq e (if (search-forward "\n\n" nil t)
9472                       (1- (point))
9473                     (point-max))))
9474         (insert-buffer-substring gnus-original-article-buffer s e)
9475         (run-hooks 'gnus-article-decode-hook)
9476         (if hidden
9477             (let ((gnus-treat-hide-headers nil)
9478                   (gnus-treat-hide-boring-headers nil))
9479               (gnus-delete-wash-type 'headers)
9480               (gnus-treat-article 'head))
9481           (gnus-treat-article 'head))
9482         (widen)
9483         (if window
9484             (set-window-start window (goto-char (point-min))))
9485         (if gnus-break-pages
9486             (gnus-narrow-to-page)
9487           (when (gnus-visual-p 'page-marker)
9488             (let ((inhibit-read-only t))
9489               (gnus-remove-text-with-property 'gnus-prev)
9490               (gnus-remove-text-with-property 'gnus-next))))
9491         (gnus-set-mode-line 'article)))))
9492
9493 (defun gnus-summary-show-all-headers ()
9494   "Make all header lines visible."
9495   (interactive)
9496   (gnus-summary-toggle-header 1))
9497
9498 (defun gnus-summary-caesar-message (&optional arg)
9499   "Caesar rotate the current article by 13.
9500 With a non-numerical prefix, also rotate headers.  A numerical
9501 prefix specifies how many places to rotate each letter forward."
9502   (interactive "P")
9503   (gnus-summary-select-article)
9504   (let ((mail-header-separator ""))
9505     (gnus-eval-in-buffer-window gnus-article-buffer
9506       (save-restriction
9507         (widen)
9508         (let ((start (window-start))
9509               (inhibit-read-only t))
9510           (if (equal arg '(4))
9511               (message-caesar-buffer-body nil t)
9512             (message-caesar-buffer-body arg))
9513           (set-window-start (get-buffer-window (current-buffer)) start)))))
9514   ;; Create buttons and stuff...
9515   (gnus-treat-article nil))
9516
9517 (declare-function idna-to-unicode "ext:idna" (str))
9518
9519 (defun gnus-summary-idna-message (&optional arg)
9520   "Decode IDNA encoded domain names in the current articles.
9521 IDNA encoded domain names looks like `xn--bar'.  If a string
9522 remain unencoded after running this function, it is likely an
9523 invalid IDNA string (`xn--bar' is invalid).
9524
9525 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9526 installed for this command to work."
9527   (interactive "P")
9528   (if (not (and (condition-case nil (require 'idna)
9529                   (file-error))
9530                 (mm-coding-system-p 'utf-8)
9531                 (executable-find (symbol-value 'idna-program))))
9532       (gnus-message
9533        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9534     (gnus-summary-select-article)
9535     (let ((mail-header-separator ""))
9536       (gnus-eval-in-buffer-window gnus-article-buffer
9537         (save-restriction
9538           (widen)
9539           (let ((start (window-start))
9540                 buffer-read-only)
9541             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9542               (replace-match (idna-to-unicode (match-string 1))))
9543             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9544
9545 (defun gnus-summary-morse-message (&optional arg)
9546   "Morse decode the current article."
9547   (interactive "P")
9548   (gnus-summary-select-article)
9549   (let ((mail-header-separator ""))
9550     (gnus-eval-in-buffer-window gnus-article-buffer
9551       (save-excursion
9552         (save-restriction
9553           (widen)
9554           (let ((pos (window-start))
9555                 (inhibit-read-only t))
9556             (goto-char (point-min))
9557             (when (message-goto-body)
9558               (gnus-narrow-to-body))
9559             (goto-char (point-min))
9560             (while (search-forward "·" (point-max) t)
9561               (replace-match "."))
9562             (unmorse-region (point-min) (point-max))
9563             (widen)
9564             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9565
9566 (defun gnus-summary-stop-page-breaking ()
9567   "Stop page breaking in the current article."
9568   (interactive)
9569   (gnus-summary-select-article)
9570   (gnus-eval-in-buffer-window gnus-article-buffer
9571     (widen)
9572     (when (gnus-visual-p 'page-marker)
9573       (let ((inhibit-read-only t))
9574         (gnus-remove-text-with-property 'gnus-prev)
9575         (gnus-remove-text-with-property 'gnus-next))
9576       (setq gnus-page-broken nil))))
9577
9578 (defun gnus-summary-move-article (&optional n to-newsgroup
9579                                             select-method action)
9580   "Move the current article to a different newsgroup.
9581 If N is a positive number, move the N next articles.
9582 If N is a negative number, move the N previous articles.
9583 If N is nil and any articles have been marked with the process mark,
9584 move those articles instead.
9585 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9586 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9587 re-spool using this method.
9588
9589 When called interactively with TO-NEWSGROUP being nil, the value of
9590 the variable `gnus-move-split-methods' is used for finding a default
9591 for the target newsgroup.
9592
9593 For this function to work, both the current newsgroup and the
9594 newsgroup that you want to move to have to support the `request-move'
9595 and `request-accept' functions.
9596
9597 ACTION can be either `move' (the default), `crosspost' or `copy'."
9598   (interactive "P")
9599   (unless action
9600     (setq action 'move))
9601   ;; Check whether the source group supports the required functions.
9602   (cond ((and (eq action 'move)
9603               (not (gnus-check-backend-function
9604                     'request-move-article gnus-newsgroup-name)))
9605          (error "The current group does not support article moving"))
9606         ((and (eq action 'crosspost)
9607               (not (gnus-check-backend-function
9608                     'request-replace-article gnus-newsgroup-name)))
9609          (error "The current group does not support article editing")))
9610   (let ((articles (gnus-summary-work-articles n))
9611         (prefix (if (gnus-check-backend-function
9612                      'request-move-article gnus-newsgroup-name)
9613                     (funcall gnus-move-group-prefix-function
9614                              gnus-newsgroup-name)
9615                   ""))
9616         (names '((move "Move" "Moving")
9617                  (copy "Copy" "Copying")
9618                  (crosspost "Crosspost" "Crossposting")))
9619         (copy-buf (save-excursion
9620                     (nnheader-set-temp-buffer " *copy article*")))
9621         art-group to-method new-xref article to-groups
9622         articles-to-update-marks encoded)
9623     (unless (assq action names)
9624       (error "Unknown action %s" action))
9625     ;; Read the newsgroup name.
9626     (when (and (not to-newsgroup)
9627                (not select-method))
9628       (if (and gnus-move-split-methods
9629                (not
9630                 (and (memq gnus-current-article articles)
9631                      (gnus-buffer-live-p gnus-original-article-buffer))))
9632           ;; When `gnus-move-split-methods' is non-nil, we have to
9633           ;; select an article to give `gnus-read-move-group-name' an
9634           ;; opportunity to suggest an appropriate default.  However,
9635           ;; we needn't render or mark the article.
9636           (let ((gnus-display-mime-function nil)
9637                 (gnus-article-prepare-hook nil)
9638                 (gnus-mark-article-hook nil))
9639             (gnus-summary-select-article nil nil nil (car articles))))
9640       (setq to-newsgroup (gnus-read-move-group-name
9641                           (cadr (assq action names))
9642                           (symbol-value
9643                            (intern (format "gnus-current-%s-group" action)))
9644                           articles prefix)
9645             encoded to-newsgroup
9646             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9647       (set (intern (format "gnus-current-%s-group" action))
9648            (mm-decode-coding-string
9649             to-newsgroup
9650             (gnus-group-name-charset to-method to-newsgroup))))
9651     (unless to-method
9652       (setq to-method (or select-method
9653                           (gnus-server-to-method
9654                            (gnus-group-method to-newsgroup)))))
9655     (setq to-newsgroup
9656           (or encoded
9657               (and to-newsgroup
9658                    (mm-encode-coding-string
9659                     to-newsgroup
9660                     (gnus-group-name-charset to-method to-newsgroup)))))
9661     ;; Check the method we are to move this article to...
9662     (unless (gnus-check-backend-function
9663              'request-accept-article (car to-method))
9664       (error "%s does not support article copying" (car to-method)))
9665     (unless (gnus-check-server to-method)
9666       (error "Can't open server %s" (car to-method)))
9667     (gnus-message 6 "%s to %s: %s..."
9668                   (caddr (assq action names))
9669                   (or (car select-method)
9670                       (gnus-group-decoded-name to-newsgroup))
9671                   articles)
9672     (while articles
9673       (setq article (pop articles))
9674       (setq
9675        art-group
9676        (cond
9677         ;; Move the article.
9678         ((eq action 'move)
9679          ;; Remove this article from future suppression.
9680          (gnus-dup-unsuppress-article article)
9681          (let* ((from-method (gnus-find-method-for-group
9682                               gnus-newsgroup-name))
9683                 (to-method (or select-method
9684                                (gnus-find-method-for-group to-newsgroup)))
9685                 (move-is-internal (gnus-server-equal from-method to-method)))
9686            (gnus-request-move-article
9687             article                     ; Article to move
9688             gnus-newsgroup-name         ; From newsgroup
9689             (nth 1 (gnus-find-method-for-group
9690                     gnus-newsgroup-name)) ; Server
9691             (list 'gnus-request-accept-article
9692                   to-newsgroup (list 'quote select-method)
9693                   (not articles) t)     ; Accept form
9694             (not articles)              ; Only save nov last time
9695             (and move-is-internal
9696                  to-newsgroup           ; Not respooling
9697                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9698         ;; Copy the article.
9699         ((eq action 'copy)
9700          (with-current-buffer copy-buf
9701            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9702              (save-restriction
9703                (nnheader-narrow-to-headers)
9704                (dolist (hdr gnus-copy-article-ignored-headers)
9705                  (message-remove-header hdr t)))
9706              (gnus-request-accept-article
9707               to-newsgroup select-method (not articles) t))))
9708         ;; Crosspost the article.
9709         ((eq action 'crosspost)
9710          (let ((xref (message-tokenize-header
9711                       (mail-header-xref (gnus-summary-article-header article))
9712                       " ")))
9713            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9714                                   ":" (number-to-string article)))
9715            (unless xref
9716              (setq xref (list (system-name))))
9717            (setq new-xref
9718                  (concat
9719                   (mapconcat 'identity
9720                              (delete "Xref:" (delete new-xref xref))
9721                              " ")
9722                   " " new-xref))
9723            (with-current-buffer copy-buf
9724              ;; First put the article in the destination group.
9725              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9726              (when (consp (setq art-group
9727                                 (gnus-request-accept-article
9728                                  to-newsgroup select-method (not articles) t)))
9729                (setq new-xref (concat new-xref " " (car art-group)
9730                                       ":"
9731                                       (number-to-string (cdr art-group))))
9732                ;; Now we have the new Xrefs header, so we insert
9733                ;; it and replace the new article.
9734                (nnheader-replace-header "Xref" new-xref)
9735                (gnus-request-replace-article
9736                 (cdr art-group) to-newsgroup (current-buffer) t)
9737                art-group))))))
9738       (cond
9739        ((not art-group)
9740         (gnus-message 1 "Couldn't %s article %s: %s"
9741                       (cadr (assq action names)) article
9742                       (nnheader-get-report (car to-method))))
9743        ((eq art-group 'junk)
9744         (when (eq action 'move)
9745           (gnus-summary-mark-article article gnus-canceled-mark)
9746           (gnus-message 4 "Deleted article %s" article)
9747           ;; run the delete hook
9748           (run-hook-with-args 'gnus-summary-article-delete-hook
9749                               action
9750                               (gnus-data-header
9751                                (assoc article (gnus-data-list nil)))
9752                               gnus-newsgroup-name nil
9753                               select-method)))
9754        (t
9755         (let* ((pto-group (gnus-group-prefixed-name
9756                            (car art-group) to-method))
9757                (info (gnus-get-info pto-group))
9758                (to-group (gnus-info-group info))
9759                to-marks)
9760           ;; Update the group that has been moved to.
9761           (when (and info
9762                      (memq action '(move copy)))
9763             (unless (member to-group to-groups)
9764               (push to-group to-groups))
9765
9766             (unless (memq article gnus-newsgroup-unreads)
9767               (push 'read to-marks)
9768               (gnus-info-set-read
9769                info (gnus-add-to-range (gnus-info-read info)
9770                                        (list (cdr art-group)))))
9771
9772             ;; See whether the article is to be put in the cache.
9773             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9774                    (marks (if expirable
9775                               gnus-article-mark-lists
9776                             (delete '(expirable . expire)
9777                                     (copy-sequence gnus-article-mark-lists))))
9778                    (to-article (cdr art-group)))
9779
9780               ;; Enter the article into the cache in the new group,
9781               ;; if that is required.
9782               (when gnus-use-cache
9783                 (gnus-cache-possibly-enter-article
9784                  to-group to-article
9785                  (memq article gnus-newsgroup-marked)
9786                  (memq article gnus-newsgroup-dormant)
9787                  (memq article gnus-newsgroup-unreads)))
9788
9789               (when gnus-preserve-marks
9790                 ;; Copy any marks over to the new group.
9791                 (when (and (equal to-group gnus-newsgroup-name)
9792                            (not (memq article gnus-newsgroup-unreads)))
9793                   ;; Mark this article as read in this group.
9794                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9795                   ;; Increase the active status of this group.
9796                   (setcdr (gnus-active to-group) to-article)
9797                   (setcdr gnus-newsgroup-active to-article))
9798
9799                 (while marks
9800                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9801                     (when (memq article (symbol-value
9802                                          (intern (format "gnus-newsgroup-%s"
9803                                                          (caar marks)))))
9804                       (push (cdar marks) to-marks)
9805                       ;; If the other group is the same as this group,
9806                       ;; then we have to add the mark to the list.
9807                       (when (equal to-group gnus-newsgroup-name)
9808                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9809                              (cons to-article
9810                                    (symbol-value
9811                                     (intern (format "gnus-newsgroup-%s"
9812                                                     (caar marks)))))))
9813                       ;; Copy the marks to other group.
9814                       (gnus-add-marked-articles
9815                        to-group (cdar marks) (list to-article) info)))
9816                   (setq marks (cdr marks)))
9817
9818                 (when (and expirable
9819                            gnus-mark-copied-or-moved-articles-as-expirable
9820                            (not (memq 'expire to-marks)))
9821                   ;; Mark this article as expirable.
9822                   (push 'expire to-marks)
9823                   (when (equal to-group gnus-newsgroup-name)
9824                     (push to-article gnus-newsgroup-expirable))
9825                   ;; Copy the expirable mark to other group.
9826                   (gnus-add-marked-articles
9827                    to-group 'expire (list to-article) info))
9828
9829                 (when to-marks
9830                   (gnus-request-set-mark
9831                    to-group (list (list (list to-article) 'add to-marks)))))
9832
9833               (gnus-dribble-enter
9834                (concat "(gnus-group-set-info '"
9835                        (gnus-prin1-to-string (gnus-get-info to-group))
9836                        ")"))))
9837
9838           ;; Update the Xref header in this article to point to
9839           ;; the new crossposted article we have just created.
9840           (when (eq action 'crosspost)
9841             (with-current-buffer copy-buf
9842               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9843               (nnheader-replace-header "Xref" new-xref)
9844               (gnus-request-replace-article
9845                article gnus-newsgroup-name (current-buffer) t)))
9846
9847           ;; run the move/copy/crosspost/respool hook
9848           (run-hook-with-args 'gnus-summary-article-move-hook
9849                               action
9850                               (gnus-data-header
9851                                (assoc article (gnus-data-list nil)))
9852                               gnus-newsgroup-name
9853                               to-newsgroup
9854                               select-method))
9855
9856         ;;;!!!Why is this necessary?
9857         (set-buffer gnus-summary-buffer)
9858
9859         (when (eq action 'move)
9860           (save-excursion
9861             (gnus-summary-goto-subject article)
9862             (gnus-summary-mark-article article gnus-canceled-mark)))))
9863       (push article articles-to-update-marks))
9864
9865     (save-excursion
9866       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9867     ;; Re-activate all groups that have been moved to.
9868     (with-current-buffer gnus-group-buffer
9869       (let ((gnus-group-marked to-groups))
9870         (gnus-group-get-new-news-this-group nil t)))
9871
9872     (gnus-kill-buffer copy-buf)
9873     (gnus-summary-position-point)
9874     (gnus-set-mode-line 'summary)))
9875
9876 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9877   "Copy the current article to some other group.
9878 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9879 When called interactively, if TO-NEWSGROUP is nil, use the value of
9880 the variable `gnus-move-split-methods' for finding a default target
9881 newsgroup.
9882 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9883 re-spool using this method."
9884   (interactive "P")
9885   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9886
9887 (defun gnus-summary-crosspost-article (&optional n)
9888   "Crosspost the current article to some other group."
9889   (interactive "P")
9890   (gnus-summary-move-article n nil nil 'crosspost))
9891
9892 (defcustom gnus-summary-respool-default-method nil
9893   "Default method type for respooling an article.
9894 If nil, use to the current newsgroup method."
9895   :type 'symbol
9896   :group 'gnus-summary-mail)
9897
9898 (defun gnus-summary-respool-article (&optional n method)
9899   "Respool the current article.
9900 The article will be squeezed through the mail spooling process again,
9901 which means that it will be put in some mail newsgroup or other
9902 depending on `nnmail-split-methods'.
9903 If N is a positive number, respool the N next articles.
9904 If N is a negative number, respool the N previous articles.
9905 If N is nil and any articles have been marked with the process mark,
9906 respool those articles instead.
9907
9908 Respooling can be done both from mail groups and \"real\" newsgroups.
9909 In the former case, the articles in question will be moved from the
9910 current group into whatever groups they are destined to.  In the
9911 latter case, they will be copied into the relevant groups."
9912   (interactive
9913    (list current-prefix-arg
9914          (let* ((methods (gnus-methods-using 'respool))
9915                 (methname
9916                  (symbol-name (or gnus-summary-respool-default-method
9917                                   (car (gnus-find-method-for-group
9918                                         gnus-newsgroup-name)))))
9919                 (method
9920                  (gnus-completing-read
9921                   "Backend to use when respooling"
9922                   methods t nil 'gnus-mail-method-history methname))
9923                 ms)
9924            (cond
9925             ((zerop (length (setq ms (gnus-servers-using-backend
9926                                       (intern method)))))
9927              (list (intern method) ""))
9928             ((= 1 (length ms))
9929              (car ms))
9930             (t
9931              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9932                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
9933                            ms-alist))))))))
9934   (unless method
9935     (error "No method given for respooling"))
9936   (if (assoc (symbol-name
9937               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9938              (gnus-methods-using 'respool))
9939       (gnus-summary-move-article n nil method)
9940     (gnus-summary-copy-article n nil method)))
9941
9942 (defun gnus-summary-import-article (file &optional edit)
9943   "Import an arbitrary file into a mail newsgroup."
9944   (interactive "fImport file: \nP")
9945   (let ((group gnus-newsgroup-name)
9946         (now (current-time))
9947         atts lines group-art)
9948     (unless (gnus-check-backend-function 'request-accept-article group)
9949       (error "%s does not support article importing" group))
9950     (or (file-readable-p file)
9951         (not (file-regular-p file))
9952         (error "Can't read %s" file))
9953     (with-current-buffer (gnus-get-buffer-create " *import file*")
9954       (erase-buffer)
9955       (nnheader-insert-file-contents file)
9956       (goto-char (point-min))
9957       (if (nnheader-article-p)
9958           (save-restriction
9959             (goto-char (point-min))
9960             (search-forward "\n\n" nil t)
9961             (narrow-to-region (point-min) (1- (point)))
9962             (goto-char (point-min))
9963             (unless (re-search-forward "^date:" nil t)
9964               (goto-char (point-max))
9965               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9966        ;; This doesn't look like an article, so we fudge some headers.
9967         (setq atts (file-attributes file)
9968               lines (count-lines (point-min) (point-max)))
9969         (insert "From: " (read-string "From: ") "\n"
9970                 "Subject: " (read-string "Subject: ") "\n"
9971                 "Date: " (message-make-date (nth 5 atts)) "\n"
9972                 "Message-ID: " (message-make-message-id) "\n"
9973                 "Lines: " (int-to-string lines) "\n"
9974                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9975       (setq group-art (gnus-request-accept-article group nil t))
9976       (kill-buffer (current-buffer)))
9977     (setq gnus-newsgroup-active (gnus-activate-group group))
9978     (forward-line 1)
9979     (gnus-summary-goto-article (cdr group-art) nil t)
9980     (when edit
9981       (gnus-summary-edit-article))))
9982
9983 (defun gnus-summary-create-article ()
9984   "Create an article in a mail newsgroup."
9985   (interactive)
9986   (let ((group gnus-newsgroup-name)
9987         (now (current-time))
9988         group-art)
9989     (unless (gnus-check-backend-function 'request-accept-article group)
9990       (error "%s does not support article importing" group))
9991     (with-current-buffer (gnus-get-buffer-create " *import file*")
9992       (erase-buffer)
9993       (goto-char (point-min))
9994       ;; This doesn't look like an article, so we fudge some headers.
9995       (insert "From: " (read-string "From: ") "\n"
9996               "Subject: " (read-string "Subject: ") "\n"
9997               "Date: " (message-make-date now) "\n"
9998               "Message-ID: " (message-make-message-id) "\n")
9999       (setq group-art (gnus-request-accept-article group nil t))
10000       (kill-buffer (current-buffer)))
10001     (setq gnus-newsgroup-active (gnus-activate-group group))
10002     (forward-line 1)
10003     (gnus-summary-goto-article (cdr group-art) nil t)
10004     (gnus-summary-edit-article)))
10005
10006 (defun gnus-summary-article-posted-p ()
10007   "Say whether the current (mail) article is available from news as well.
10008 This will be the case if the article has both been mailed and posted."
10009   (interactive)
10010   (let ((id (mail-header-references (gnus-summary-article-header)))
10011         (gnus-override-method (car (gnus-refer-article-methods))))
10012     (if (gnus-request-head id "")
10013         (gnus-message 2 "The current message was found on %s"
10014                       gnus-override-method)
10015       (gnus-message 2 "The current message couldn't be found on %s"
10016                     gnus-override-method)
10017       nil)))
10018
10019 (defun gnus-summary-expire-articles (&optional now)
10020   "Expire all articles that are marked as expirable in the current group."
10021   (interactive)
10022   (when (and (not gnus-group-is-exiting-without-update-p)
10023              (gnus-check-backend-function
10024               'request-expire-articles gnus-newsgroup-name))
10025     ;; This backend supports expiry.
10026     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10027            (expirable (if total
10028                           (progn
10029                             ;; We need to update the info for
10030                             ;; this group for `gnus-list-of-read-articles'
10031                             ;; to give us the right answer.
10032                             (gnus-run-hooks 'gnus-exit-group-hook)
10033                             (gnus-summary-update-info)
10034                             (gnus-list-of-read-articles gnus-newsgroup-name))
10035                         (setq gnus-newsgroup-expirable
10036                               (sort gnus-newsgroup-expirable '<))))
10037            (expiry-wait (if now 'immediate
10038                           (gnus-group-find-parameter
10039                            gnus-newsgroup-name 'expiry-wait)))
10040            (nnmail-expiry-target
10041             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10042                 nnmail-expiry-target))
10043            es)
10044       (when expirable
10045         ;; There are expirable articles in this group, so we run them
10046         ;; through the expiry process.
10047         (gnus-message 6 "Expiring articles...")
10048         (unless (gnus-check-group gnus-newsgroup-name)
10049           (error "Can't open server for %s" gnus-newsgroup-name))
10050         ;; The list of articles that weren't expired is returned.
10051         (save-excursion
10052           (if expiry-wait
10053               (let ((nnmail-expiry-wait-function nil)
10054                     (nnmail-expiry-wait expiry-wait))
10055                 (setq es (gnus-request-expire-articles
10056                           expirable gnus-newsgroup-name)))
10057             (setq es (gnus-request-expire-articles
10058                       expirable gnus-newsgroup-name)))
10059           (unless total
10060             (setq gnus-newsgroup-expirable es))
10061           ;; We go through the old list of expirable, and mark all
10062           ;; really expired articles as nonexistent.
10063           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10064             (let ((gnus-use-cache nil))
10065               (dolist (article expirable)
10066                 (when (and (not (memq article es))
10067                            (gnus-data-find article))
10068                   (gnus-summary-mark-article article gnus-canceled-mark)
10069                   (run-hook-with-args 'gnus-summary-article-expire-hook
10070                                       'delete
10071                                       (gnus-data-header
10072                                        (assoc article (gnus-data-list nil)))
10073                                       gnus-newsgroup-name
10074                                       nil
10075                                       nil))))))
10076         (gnus-message 6 "Expiring articles...done")))))
10077
10078 (defun gnus-summary-expire-articles-now ()
10079   "Expunge all expirable articles in the current group.
10080 This means that *all* articles that are marked as expirable will be
10081 deleted forever, right now."
10082   (interactive)
10083   (or gnus-expert-user
10084       (gnus-yes-or-no-p
10085        "Are you really, really sure you want to delete all expirable messages? ")
10086       (error "Phew!"))
10087   (gnus-summary-expire-articles t))
10088
10089 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10090 (defun gnus-summary-delete-article (&optional n)
10091   "Delete the N next (mail) articles.
10092 This command actually deletes articles.  This is not a marking
10093 command.  The article will disappear forever from your life, never to
10094 return.
10095
10096 If N is negative, delete backwards.
10097 If N is nil and articles have been marked with the process mark,
10098 delete these instead.
10099
10100 If `gnus-novice-user' is non-nil you will be asked for
10101 confirmation before the articles are deleted."
10102   (interactive "P")
10103   (unless (gnus-check-backend-function 'request-expire-articles
10104                                        gnus-newsgroup-name)
10105     (error "The current newsgroup does not support article deletion"))
10106   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10107     (error "Couldn't open server"))
10108   ;; Compute the list of articles to delete.
10109   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10110         (nnmail-expiry-target 'delete)
10111         not-deleted)
10112     (if (and gnus-novice-user
10113              (not (gnus-yes-or-no-p
10114                    (format "Do you really want to delete %s forever? "
10115                            (if (> (length articles) 1)
10116                                (format "these %s articles" (length articles))
10117                              "this article")))))
10118         ()
10119       ;; Delete the articles.
10120       (setq not-deleted (gnus-request-expire-articles
10121                          articles gnus-newsgroup-name 'force))
10122       (save-excursion
10123         (while articles
10124           (gnus-summary-remove-process-mark (car articles))
10125           ;; The backend might not have been able to delete the article
10126           ;; after all.
10127           (unless (memq (car articles) not-deleted)
10128             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10129           (let* ((article (car articles))
10130                  (ghead  (gnus-data-header
10131                           (assoc article (gnus-data-list nil)))))
10132             (run-hook-with-args 'gnus-summary-article-delete-hook
10133                                 'delete ghead gnus-newsgroup-name nil
10134                                 nil))
10135           (setq articles (cdr articles))))
10136       (when not-deleted
10137         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10138     (gnus-summary-position-point)
10139     (gnus-set-mode-line 'summary)
10140     not-deleted))
10141
10142 (defun gnus-summary-edit-article (&optional arg)
10143   "Edit the current article.
10144 This will have permanent effect only in mail groups.
10145 If ARG is nil, edit the decoded articles.
10146 If ARG is 1, edit the raw articles.
10147 If ARG is 2, edit the raw articles even in read-only groups.
10148 If ARG is 3, edit the articles with the current handles.
10149 Otherwise, allow editing of articles even in read-only
10150 groups."
10151   (interactive "P")
10152   (let (force raw current-handles)
10153     (cond
10154      ((null arg))
10155      ((eq arg 1)
10156       (setq raw t))
10157      ((eq arg 2)
10158       (setq raw t
10159             force t))
10160      ((eq arg 3)
10161       (setq current-handles
10162             (and (gnus-buffer-live-p gnus-article-buffer)
10163                  (with-current-buffer gnus-article-buffer
10164                    (prog1
10165                        gnus-article-mime-handles
10166                      (setq gnus-article-mime-handles nil))))))
10167      (t
10168       (setq force t)))
10169     (when (and raw (not force)
10170                (member gnus-newsgroup-name '("nndraft:delayed"
10171                                              "nndraft:drafts"
10172                                              "nndraft:queue")))
10173       (error "Can't edit the raw article in group %s"
10174              gnus-newsgroup-name))
10175     (with-current-buffer gnus-summary-buffer
10176       (let ((mail-parse-charset gnus-newsgroup-charset)
10177             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10178         (gnus-set-global-variables)
10179         (when (and (not force)
10180                    (gnus-group-read-only-p))
10181           (error "The current newsgroup does not support article editing"))
10182         (gnus-summary-show-article t)
10183         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10184           (with-current-buffer gnus-article-buffer
10185             (mm-enable-multibyte)))
10186         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10187             (setq raw t))
10188         (gnus-article-edit-article
10189          (if raw 'ignore
10190            `(lambda ()
10191               (let ((mbl mml-buffer-list))
10192                 (setq mml-buffer-list nil)
10193                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10194                   (mime-to-mml ,'current-handles))
10195                 (let ((mbl1 mml-buffer-list))
10196                   (setq mml-buffer-list mbl)
10197                   (set (make-local-variable 'mml-buffer-list) mbl1))
10198                 (gnus-make-local-hook 'kill-buffer-hook)
10199                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10200          `(lambda (no-highlight)
10201             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10202                   (message-options message-options)
10203                   (message-options-set-recipient)
10204                   (mail-parse-ignored-charsets
10205                    ',gnus-newsgroup-ignored-charsets)
10206                   (rfc2047-header-encoding-alist
10207                    ',(let ((charset (gnus-group-name-charset
10208                                      (gnus-find-method-for-group
10209                                       gnus-newsgroup-name)
10210                                      gnus-newsgroup-name)))
10211                        (append (list (cons "Newsgroups" charset)
10212                                      (cons "Followup-To" charset)
10213                                      (cons "Xref" charset))
10214                                rfc2047-header-encoding-alist))))
10215               ,(if (not raw) '(progn
10216                                 (mml-to-mime)
10217                                 (mml-destroy-buffers)
10218                                 (remove-hook 'kill-buffer-hook
10219                                              'mml-destroy-buffers t)
10220                                 (kill-local-variable 'mml-buffer-list)))
10221               (gnus-summary-edit-article-done
10222                ,(or (mail-header-references gnus-current-headers) "")
10223                ,(gnus-group-read-only-p)
10224                ,gnus-summary-buffer no-highlight))))))))
10225
10226 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10227
10228 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10229                                                  no-highlight)
10230   "Make edits to the current article permanent."
10231   (interactive)
10232   (save-excursion
10233    ;; The buffer restriction contains the entire article if it exists.
10234     (when (article-goto-body)
10235       (let ((lines (count-lines (point) (point-max)))
10236             (length (- (point-max) (point)))
10237             (case-fold-search t)
10238             (body (copy-marker (point))))
10239         (goto-char (point-min))
10240         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10241           (delete-region (match-beginning 1) (match-end 1))
10242           (insert (number-to-string length)))
10243         (goto-char (point-min))
10244         (when (re-search-forward
10245                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10246           (delete-region (match-beginning 1) (match-end 1))
10247           (insert (number-to-string length)))
10248         (goto-char (point-min))
10249         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10250           (delete-region (match-beginning 1) (match-end 1))
10251           (insert (number-to-string lines))))))
10252   ;; Replace the article.
10253   (let ((buf (current-buffer)))
10254     (with-temp-buffer
10255       (insert-buffer-substring buf)
10256
10257       (if (and (not read-only)
10258                (not (gnus-request-replace-article
10259                      (cdr gnus-article-current) (car gnus-article-current)
10260                      (current-buffer) t)))
10261           (error "Couldn't replace article")
10262         ;; Update the summary buffer.
10263         (if (and references
10264                  (equal (message-tokenize-header references " ")
10265                         (message-tokenize-header
10266                          (or (message-fetch-field "references") "") " ")))
10267             ;; We only have to update this line.
10268             (save-excursion
10269               (save-restriction
10270                 (message-narrow-to-head)
10271                 (let ((head (buffer-substring-no-properties
10272                              (point-min) (point-max)))
10273                       header)
10274                   (with-temp-buffer
10275                     (insert (format "211 %d Article retrieved.\n"
10276                                     (cdr gnus-article-current)))
10277                     (insert head)
10278                     (insert ".\n")
10279                     (let ((nntp-server-buffer (current-buffer)))
10280                       (setq header (car (gnus-get-newsgroup-headers
10281                                          nil t))))
10282                     (with-current-buffer gnus-summary-buffer
10283                       (gnus-data-set-header
10284                        (gnus-data-find (cdr gnus-article-current))
10285                        header)
10286                       (gnus-summary-update-article-line
10287                        (cdr gnus-article-current) header)
10288                       (if (gnus-summary-goto-subject
10289                            (cdr gnus-article-current) nil t)
10290                           (gnus-summary-update-secondary-mark
10291                            (cdr gnus-article-current))))))))
10292           ;; Update threads.
10293           (set-buffer (or buffer gnus-summary-buffer))
10294           (gnus-summary-update-article (cdr gnus-article-current))
10295           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10296               (gnus-summary-update-secondary-mark
10297                (cdr gnus-article-current))))
10298         ;; Prettify the article buffer again.
10299         (unless no-highlight
10300           (with-current-buffer gnus-article-buffer
10301             ;;;!!! Fix this -- article should be rehighlighted.
10302             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10303             (set-buffer gnus-original-article-buffer)
10304             (gnus-request-article
10305              (cdr gnus-article-current)
10306              (car gnus-article-current) (current-buffer))))
10307         ;; Prettify the summary buffer line.
10308         (when (gnus-visual-p 'summary-highlight 'highlight)
10309           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10310
10311 (defun gnus-summary-edit-wash (key)
10312   "Perform editing command KEY in the article buffer."
10313   (interactive
10314    (list
10315     (progn
10316       (message "%s" (concat (this-command-keys) "- "))
10317       (read-char))))
10318   (message "")
10319   (gnus-summary-edit-article)
10320   (execute-kbd-macro (concat (this-command-keys) key))
10321   (gnus-article-edit-done))
10322
10323 ;;; Respooling
10324
10325 (defun gnus-summary-respool-query (&optional silent trace)
10326   "Query where the respool algorithm would put this article."
10327   (interactive)
10328   (let (gnus-mark-article-hook)
10329     (gnus-summary-select-article)
10330     (with-current-buffer gnus-original-article-buffer
10331       (let ((groups (nnmail-article-group 'identity trace)))
10332         (unless silent
10333           (if groups
10334               (message "This message would go to %s"
10335                        (mapconcat 'car groups ", "))
10336             (message "This message would go to no groups"))
10337           groups)))))
10338
10339 (defun gnus-summary-respool-trace ()
10340   "Trace where the respool algorithm would put this article.
10341 Display a buffer showing all fancy splitting patterns which matched."
10342   (interactive)
10343   (gnus-summary-respool-query nil t))
10344
10345 ;; Summary marking commands.
10346
10347 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10348   "Mark articles which has the same subject as read, and then select the next.
10349 If UNMARK is positive, remove any kind of mark.
10350 If UNMARK is negative, tick articles."
10351   (interactive "P")
10352   (when unmark
10353     (setq unmark (prefix-numeric-value unmark)))
10354   (let ((count
10355          (gnus-summary-mark-same-subject
10356           (gnus-summary-article-subject) unmark)))
10357     ;; Select next unread article.  If auto-select-same mode, should
10358     ;; select the first unread article.
10359     (gnus-summary-next-article t (and gnus-auto-select-same
10360                                       (gnus-summary-article-subject)))
10361     (gnus-message 7 "%d article%s marked as %s"
10362                   count (if (= count 1) " is" "s are")
10363                   (if unmark "unread" "read"))))
10364
10365 (defun gnus-summary-kill-same-subject (&optional unmark)
10366   "Mark articles which has the same subject as read.
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     ;; If marked as read, go to next unread subject.
10376     (when (null unmark)
10377       ;; Go to next unread subject.
10378       (gnus-summary-next-subject 1 t))
10379     (gnus-message 7 "%d articles are marked as %s"
10380                   count (if unmark "unread" "read"))))
10381
10382 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10383   "Mark articles with same SUBJECT as read, and return marked number.
10384 If optional argument UNMARK is positive, remove any kinds of marks.
10385 If optional argument UNMARK is negative, mark articles as unread instead."
10386   (let ((count 1))
10387     (save-excursion
10388       (cond
10389        ((null unmark)                   ; Mark as read.
10390         (while (and
10391                 (progn
10392                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10393                   (gnus-summary-show-thread) t)
10394                 (gnus-summary-find-subject subject))
10395           (setq count (1+ count))))
10396        ((> unmark 0)                    ; Tick.
10397         (while (and
10398                 (progn
10399                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10400                   (gnus-summary-show-thread) t)
10401                 (gnus-summary-find-subject subject))
10402           (setq count (1+ count))))
10403        (t                               ; Mark as unread.
10404         (while (and
10405                 (progn
10406                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10407                   (gnus-summary-show-thread) t)
10408                 (gnus-summary-find-subject subject))
10409           (setq count (1+ count)))))
10410       (gnus-set-mode-line 'summary)
10411       ;; Return the number of marked articles.
10412       count)))
10413
10414 (defun gnus-summary-mark-as-processable (n &optional unmark)
10415   "Set the process mark on the next N articles.
10416 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10417 the process mark instead.  The difference between N and the actual
10418 number of articles marked is returned."
10419   (interactive "P")
10420   (if (and (null n) (gnus-region-active-p))
10421       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10422     (setq n (prefix-numeric-value n))
10423     (let ((backward (< n 0))
10424           (n (abs n)))
10425       (while (and
10426               (> n 0)
10427               (if unmark
10428                   (gnus-summary-remove-process-mark
10429                    (gnus-summary-article-number))
10430                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10431               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10432         (setq n (1- n)))
10433       (when (/= 0 n)
10434         (gnus-message 7 "No more articles"))
10435       (gnus-summary-recenter)
10436       (gnus-summary-position-point)
10437       n)))
10438
10439 (defun gnus-summary-unmark-as-processable (n)
10440   "Remove the process mark from the next N articles.
10441 If N is negative, unmark backward instead.  The difference between N and
10442 the actual number of articles unmarked is returned."
10443   (interactive "P")
10444   (gnus-summary-mark-as-processable n t))
10445
10446 (defun gnus-summary-unmark-all-processable ()
10447   "Remove the process mark from all articles."
10448   (interactive)
10449   (save-excursion
10450     (while gnus-newsgroup-processable
10451       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10452   (gnus-summary-position-point))
10453
10454 (defun gnus-summary-add-mark (article type)
10455   "Mark ARTICLE with a mark of TYPE."
10456   (let ((vtype (car (assq type gnus-article-mark-lists)))
10457         var)
10458     (if (not vtype)
10459         (error "No such mark type: %s" type)
10460       (setq var (intern (format "gnus-newsgroup-%s" type)))
10461       (set var (cons article (symbol-value var)))
10462       (if (memq type '(processable cached replied forwarded recent saved))
10463           (gnus-summary-update-secondary-mark article)
10464         ;;; !!! This is bogus.  We should find out what primary
10465         ;;; !!! mark we want to set.
10466         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10467
10468 (defun gnus-summary-mark-as-expirable (n)
10469   "Mark N articles forward as expirable.
10470 If N is negative, mark backward instead.  The difference between N and
10471 the actual number of articles marked is returned."
10472   (interactive "p")
10473   (gnus-summary-mark-forward n gnus-expirable-mark))
10474
10475 (defun gnus-summary-mark-as-spam (n)
10476   "Mark N articles forward as spam.
10477 If N is negative, mark backward instead.  The difference between N and
10478 the actual number of articles marked is returned."
10479   (interactive "p")
10480   (gnus-summary-mark-forward n gnus-spam-mark))
10481
10482 (defun gnus-summary-mark-article-as-replied (article)
10483   "Mark ARTICLE as replied to and update the summary line.
10484 ARTICLE can also be a list of articles."
10485   (interactive (list (gnus-summary-article-number)))
10486   (let ((articles (if (listp article) article (list article))))
10487     (dolist (article articles)
10488       (unless (numberp article)
10489         (error "%s is not a number" article))
10490       (push article gnus-newsgroup-replied)
10491       (let ((inhibit-read-only t))
10492         (when (gnus-summary-goto-subject article nil t)
10493           (gnus-summary-update-secondary-mark article))))))
10494
10495 (defun gnus-summary-mark-article-as-forwarded (article)
10496   "Mark ARTICLE as forwarded and update the summary line.
10497 ARTICLE can also be a list of articles."
10498   (let ((articles (if (listp article) article (list article))))
10499     (dolist (article articles)
10500       (push article gnus-newsgroup-forwarded)
10501       (let ((inhibit-read-only t))
10502         (when (gnus-summary-goto-subject article nil t)
10503           (gnus-summary-update-secondary-mark article))))))
10504
10505 (defun gnus-summary-set-bookmark (article)
10506   "Set a bookmark in current article."
10507   (interactive (list (gnus-summary-article-number)))
10508   (when (or (not (get-buffer gnus-article-buffer))
10509             (not gnus-current-article)
10510             (not gnus-article-current)
10511             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10512     (error "No current article selected"))
10513   ;; Remove old bookmark, if one exists.
10514   (gnus-pull article gnus-newsgroup-bookmarks)
10515   ;; Set the new bookmark, which is on the form
10516   ;; (article-number . line-number-in-body).
10517   (push
10518    (cons article
10519          (with-current-buffer gnus-article-buffer
10520            (count-lines
10521             (min (point)
10522                  (save-excursion
10523                    (article-goto-body)
10524                    (point)))
10525             (point))))
10526    gnus-newsgroup-bookmarks)
10527   (gnus-message 6 "A bookmark has been added to the current article."))
10528
10529 (defun gnus-summary-remove-bookmark (article)
10530   "Remove the bookmark from the current article."
10531   (interactive (list (gnus-summary-article-number)))
10532   ;; Remove old bookmark, if one exists.
10533   (if (not (assq article gnus-newsgroup-bookmarks))
10534       (gnus-message 6 "No bookmark in current article.")
10535     (gnus-pull article gnus-newsgroup-bookmarks)
10536     (gnus-message 6 "Removed bookmark.")))
10537
10538 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10539 (defun gnus-summary-mark-as-dormant (n)
10540   "Mark N articles forward as dormant.
10541 If N is negative, mark backward instead.  The difference between N and
10542 the actual number of articles marked is returned."
10543   (interactive "p")
10544   (gnus-summary-mark-forward n gnus-dormant-mark))
10545
10546 (defun gnus-summary-set-process-mark (article)
10547   "Set the process mark on ARTICLE and update the summary line."
10548   (setq gnus-newsgroup-processable
10549         (cons article
10550               (delq article gnus-newsgroup-processable)))
10551   (when (gnus-summary-goto-subject article)
10552     (gnus-summary-show-thread)
10553     (gnus-summary-goto-subject article)
10554     (gnus-summary-update-secondary-mark article)))
10555
10556 (defun gnus-summary-remove-process-mark (&rest articles)
10557   "Remove the process mark from ARTICLES and update the summary line."
10558   (dolist (article articles)
10559     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10560     (when (gnus-summary-goto-subject article)
10561       (gnus-summary-show-thread)
10562       (gnus-summary-goto-subject article)
10563       (gnus-summary-update-secondary-mark article)))
10564   t)
10565
10566 (defun gnus-summary-set-saved-mark (article)
10567   "Set the process mark on ARTICLE and update the summary line."
10568   (push article gnus-newsgroup-saved)
10569   (when (gnus-summary-goto-subject article)
10570     (gnus-summary-update-secondary-mark article)))
10571
10572 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10573   "Mark N articles as read forwards.
10574 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10575 The difference between N and the actual number of articles marked is
10576 returned.
10577 If NO-EXPIRE, auto-expiry will be inhibited."
10578   (interactive "p")
10579   (gnus-summary-show-thread)
10580   (let ((backward (< n 0))
10581         (gnus-summary-goto-unread
10582          (and gnus-summary-goto-unread
10583               (not (eq gnus-summary-goto-unread 'never))
10584               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10585                                     gnus-ticked-mark gnus-dormant-mark)))))
10586         (n (abs n))
10587         (mark (or mark gnus-del-mark)))
10588     (while (and (> n 0)
10589                 (gnus-summary-mark-article nil mark no-expire)
10590                 (zerop (gnus-summary-next-subject
10591                         (if backward -1 1)
10592                         (and gnus-summary-goto-unread
10593                              (not (eq gnus-summary-goto-unread 'never)))
10594                         t)))
10595       (setq n (1- n)))
10596     (when (/= 0 n)
10597       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10598     (gnus-summary-recenter)
10599     (gnus-summary-position-point)
10600     (gnus-set-mode-line 'summary)
10601     n))
10602
10603 (defun gnus-summary-mark-article-as-read (mark)
10604   "Mark the current article quickly as read with MARK."
10605   (let ((article (gnus-summary-article-number)))
10606     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10607     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10608     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10609     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10610     (push (cons article mark) gnus-newsgroup-reads)
10611     ;; Possibly remove from cache, if that is used.
10612     (when gnus-use-cache
10613       (gnus-cache-enter-remove-article article))
10614     ;; Allow the backend to change the mark.
10615     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10616     ;; Check for auto-expiry.
10617     (when (and gnus-newsgroup-auto-expire
10618                (memq mark gnus-auto-expirable-marks))
10619       (setq mark gnus-expirable-mark)
10620       ;; Let the backend know about the mark change.
10621       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10622       (push article gnus-newsgroup-expirable))
10623     ;; Set the mark in the buffer.
10624     (gnus-summary-update-mark mark 'unread)
10625     t))
10626
10627 (defun gnus-summary-mark-article-as-unread (mark)
10628   "Mark the current article quickly as unread with MARK."
10629   (let* ((article (gnus-summary-article-number))
10630          (old-mark (gnus-summary-article-mark article)))
10631     ;; Allow the backend to change the mark.
10632     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10633     (if (eq mark old-mark)
10634         t
10635       (if (<= article 0)
10636           (progn
10637             (gnus-error 1 "Can't mark negative article numbers")
10638             nil)
10639         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10640         (setq gnus-newsgroup-spam-marked
10641               (delq article gnus-newsgroup-spam-marked))
10642         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10643         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10644         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10645         (cond ((= mark gnus-ticked-mark)
10646                (setq gnus-newsgroup-marked
10647                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10648                                               article)))
10649               ((= mark gnus-spam-mark)
10650                (setq gnus-newsgroup-spam-marked
10651                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10652                                               article)))
10653               ((= mark gnus-dormant-mark)
10654                (setq gnus-newsgroup-dormant
10655                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10656                                               article)))
10657               (t
10658                (setq gnus-newsgroup-unreads
10659                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10660                                               article))))
10661         (gnus-pull article gnus-newsgroup-reads)
10662
10663         ;; See whether the article is to be put in the cache.
10664         (and gnus-use-cache
10665              (vectorp (gnus-summary-article-header article))
10666              (save-excursion
10667                (gnus-cache-possibly-enter-article
10668                 gnus-newsgroup-name article
10669                 (= mark gnus-ticked-mark)
10670                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10671
10672         ;; Fix the mark.
10673         (gnus-summary-update-mark mark 'unread)
10674         t))))
10675
10676 (defun gnus-summary-mark-article (&optional article mark no-expire)
10677   "Mark ARTICLE with MARK.  MARK can be any character.
10678 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10679 `??' (dormant) and `?E' (expirable).
10680 If MARK is nil, then the default character `?r' is used.
10681 If ARTICLE is nil, then the article on the current line will be
10682 marked.
10683 If NO-EXPIRE, auto-expiry will be inhibited."
10684   ;; The mark might be a string.
10685   (when (stringp mark)
10686     (setq mark (aref mark 0)))
10687   ;; If no mark is given, then we check auto-expiring.
10688   (when (null mark)
10689     (setq mark gnus-del-mark))
10690   (when (and (not no-expire)
10691              gnus-newsgroup-auto-expire
10692              (memq mark gnus-auto-expirable-marks))
10693     (setq mark gnus-expirable-mark))
10694   (let ((article (or article (gnus-summary-article-number)))
10695         (old-mark (gnus-summary-article-mark article)))
10696     ;; Allow the backend to change the mark.
10697     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10698     (if (eq mark old-mark)
10699         t
10700       (unless article
10701         (error "No article on current line"))
10702       (if (not (if (or (= mark gnus-unread-mark)
10703                        (= mark gnus-ticked-mark)
10704                        (= mark gnus-spam-mark)
10705                        (= mark gnus-dormant-mark))
10706                    (gnus-mark-article-as-unread article mark)
10707                  (gnus-mark-article-as-read article mark)))
10708           t
10709         ;; See whether the article is to be put in the cache.
10710         (and gnus-use-cache
10711              (not (= mark gnus-canceled-mark))
10712              (vectorp (gnus-summary-article-header article))
10713              (save-excursion
10714                (gnus-cache-possibly-enter-article
10715                 gnus-newsgroup-name article
10716                 (= mark gnus-ticked-mark)
10717                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10718
10719         (when (gnus-summary-goto-subject article nil t)
10720           (let ((inhibit-read-only t))
10721             (gnus-summary-show-thread)
10722             ;; Fix the mark.
10723             (gnus-summary-update-mark mark 'unread)
10724             t))))))
10725
10726 (defun gnus-summary-update-secondary-mark (article)
10727   "Update the secondary (read, process, cache) mark."
10728   (gnus-summary-update-mark
10729    (cond ((memq article gnus-newsgroup-processable)
10730           gnus-process-mark)
10731          ((memq article gnus-newsgroup-cached)
10732           gnus-cached-mark)
10733          ((memq article gnus-newsgroup-replied)
10734           gnus-replied-mark)
10735          ((memq article gnus-newsgroup-forwarded)
10736           gnus-forwarded-mark)
10737          ((memq article gnus-newsgroup-saved)
10738           gnus-saved-mark)
10739          ((memq article gnus-newsgroup-recent)
10740           gnus-recent-mark)
10741          ((memq article gnus-newsgroup-unseen)
10742           gnus-unseen-mark)
10743          (t gnus-no-mark))
10744    'replied)
10745   (when (gnus-visual-p 'summary-highlight 'highlight)
10746     (gnus-summary-highlight-line)
10747     (gnus-run-hooks 'gnus-summary-update-hook))
10748   t)
10749
10750 (defun gnus-summary-update-download-mark (article)
10751   "Update the download mark."
10752   (gnus-summary-update-mark
10753    (cond ((memq article gnus-newsgroup-undownloaded)
10754           gnus-undownloaded-mark)
10755          (gnus-newsgroup-agentized
10756           gnus-downloaded-mark)
10757          (t
10758           gnus-no-mark))
10759    'download)
10760   (gnus-summary-update-line t)
10761   t)
10762
10763 (defun gnus-summary-update-mark (mark type)
10764   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10765         (inhibit-read-only t))
10766     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10767     (when forward
10768       (when (looking-at "\r")
10769         (incf forward))
10770       (when (<= (+ forward (point)) (point-max))
10771         ;; Go to the right position on the line.
10772         (goto-char (+ forward (point)))
10773         ;; Replace the old mark with the new mark.
10774         (let ((to-insert
10775                (mm-subst-char-in-string
10776                 (char-after) mark
10777                 (buffer-substring (point) (1+ (point))))))
10778           (delete-region (point) (1+ (point)))
10779           (insert to-insert))
10780         ;; Optionally update the marks by some user rule.
10781         (when (eq type 'unread)
10782           (gnus-data-set-mark
10783            (gnus-data-find (gnus-summary-article-number)) mark)
10784           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10785
10786 (defun gnus-mark-article-as-read (article &optional mark)
10787   "Enter ARTICLE in the pertinent lists and remove it from others."
10788   ;; Make the article expirable.
10789   (let ((mark (or mark gnus-del-mark)))
10790     (setq gnus-newsgroup-expirable
10791           (if (= mark gnus-expirable-mark)
10792               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10793             (delq article gnus-newsgroup-expirable)))
10794     ;; Remove from unread and marked lists.
10795     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10796     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10797     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10798     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10799     (push (cons article mark) gnus-newsgroup-reads)
10800     ;; Possibly remove from cache, if that is used.
10801     (when gnus-use-cache
10802       (gnus-cache-enter-remove-article article))
10803     t))
10804
10805 (defun gnus-mark-article-as-unread (article &optional mark)
10806   "Enter ARTICLE in the pertinent lists and remove it from others."
10807   (let ((mark (or mark gnus-ticked-mark)))
10808     (if (<= article 0)
10809         (progn
10810           (gnus-error 1 "Can't mark negative article numbers")
10811           nil)
10812       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10813             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10814             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10815             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10816             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10817
10818       ;; Unsuppress duplicates?
10819       (when gnus-suppress-duplicates
10820         (gnus-dup-unsuppress-article article))
10821
10822       (cond ((= mark gnus-ticked-mark)
10823              (setq gnus-newsgroup-marked
10824                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10825             ((= mark gnus-spam-mark)
10826              (setq gnus-newsgroup-spam-marked
10827                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10828                                             article)))
10829             ((= mark gnus-dormant-mark)
10830              (setq gnus-newsgroup-dormant
10831                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10832             (t
10833              (setq gnus-newsgroup-unreads
10834                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10835       (gnus-pull article gnus-newsgroup-reads)
10836       t)))
10837
10838 (defalias 'gnus-summary-mark-as-unread-forward
10839   'gnus-summary-tick-article-forward)
10840 (make-obsolete 'gnus-summary-mark-as-unread-forward
10841                'gnus-summary-tick-article-forward "Emacs 20.4")
10842 (defun gnus-summary-tick-article-forward (n)
10843   "Tick N articles forwards.
10844 If N is negative, tick backwards instead.
10845 The difference between N and the number of articles ticked is returned."
10846   (interactive "p")
10847   (gnus-summary-mark-forward n gnus-ticked-mark))
10848
10849 (defalias 'gnus-summary-mark-as-unread-backward
10850   'gnus-summary-tick-article-backward)
10851 (make-obsolete 'gnus-summary-mark-as-unread-backward
10852                'gnus-summary-tick-article-backward "Emacs 20.4")
10853 (defun gnus-summary-tick-article-backward (n)
10854   "Tick N articles backwards.
10855 The difference between N and the number of articles ticked is returned."
10856   (interactive "p")
10857   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10858
10859 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10860 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10861 (defun gnus-summary-tick-article (&optional article clear-mark)
10862   "Mark current article as unread.
10863 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10864 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10865   (interactive)
10866   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10867                                        gnus-ticked-mark)))
10868
10869 (defun gnus-summary-mark-as-read-forward (n)
10870   "Mark N articles as read forwards.
10871 If N is negative, mark backwards instead.
10872 The difference between N and the actual number of articles marked is
10873 returned."
10874   (interactive "p")
10875   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10876
10877 (defun gnus-summary-mark-as-read-backward (n)
10878   "Mark the N articles as read backwards.
10879 The difference between N and the actual number of articles marked is
10880 returned."
10881   (interactive "p")
10882   (gnus-summary-mark-forward
10883    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10884
10885 (defun gnus-summary-mark-as-read (&optional article mark)
10886   "Mark current article as read.
10887 ARTICLE specifies the article to be marked as read.
10888 MARK specifies a string to be inserted at the beginning of the line."
10889   (gnus-summary-mark-article article mark))
10890
10891 (defun gnus-summary-clear-mark-forward (n)
10892   "Clear marks from N articles forward.
10893 If N is negative, clear backward instead.
10894 The difference between N and the number of marks cleared is returned."
10895   (interactive "p")
10896   (gnus-summary-mark-forward n gnus-unread-mark))
10897
10898 (defun gnus-summary-clear-mark-backward (n)
10899   "Clear marks from N articles backward.
10900 The difference between N and the number of marks cleared is returned."
10901   (interactive "p")
10902   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10903
10904 (defun gnus-summary-mark-unread-as-read ()
10905   "Intended to be used by `gnus-mark-article-hook'."
10906   (when (memq gnus-current-article gnus-newsgroup-unreads)
10907     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10908
10909 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10910   "Intended to be used by `gnus-mark-article-hook'."
10911   (let ((mark (gnus-summary-article-mark)))
10912     (when (or (gnus-unread-mark-p mark)
10913               (gnus-read-mark-p mark))
10914       (gnus-summary-mark-article gnus-current-article
10915                                  (or new-mark gnus-read-mark)))))
10916
10917 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10918   "Intended to be used by `gnus-mark-article-hook'."
10919   (let ((mark (gnus-summary-article-mark)))
10920     (when (or (gnus-unread-mark-p mark)
10921               (gnus-read-mark-p mark))
10922       (gnus-summary-mark-article (gnus-summary-article-number)
10923                                  (or new-mark gnus-read-mark)))))
10924
10925 (defun gnus-summary-mark-unread-as-ticked ()
10926   "Intended to be used by `gnus-mark-article-hook'."
10927   (when (memq gnus-current-article gnus-newsgroup-unreads)
10928     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10929
10930 (defun gnus-summary-mark-region-as-read (point mark all)
10931   "Mark all unread articles between point and mark as read.
10932 If given a prefix, mark all articles between point and mark as read,
10933 even ticked and dormant ones."
10934   (interactive "r\nP")
10935   (save-excursion
10936     (let (article)
10937       (goto-char point)
10938       (beginning-of-line)
10939       (while (and
10940               (< (point) mark)
10941               (progn
10942                 (when (or all
10943                           (memq (setq article (gnus-summary-article-number))
10944                                 gnus-newsgroup-unreads))
10945                   (gnus-summary-mark-article article gnus-del-mark))
10946                 t)
10947               (gnus-summary-find-next))))))
10948
10949 (defun gnus-summary-mark-below (score mark)
10950   "Mark articles with score less than SCORE with MARK."
10951   (interactive "P\ncMark: ")
10952   (setq score (if score
10953                   (prefix-numeric-value score)
10954                 (or gnus-summary-default-score 0)))
10955   (with-current-buffer gnus-summary-buffer
10956     (goto-char (point-min))
10957     (while
10958         (progn
10959           (and (< (gnus-summary-article-score) score)
10960                (gnus-summary-mark-article nil mark))
10961           (gnus-summary-find-next)))))
10962
10963 (defun gnus-summary-kill-below (&optional score)
10964   "Mark articles with score below SCORE as read."
10965   (interactive "P")
10966   (gnus-summary-mark-below score gnus-killed-mark))
10967
10968 (defun gnus-summary-clear-above (&optional score)
10969   "Clear all marks from articles with score above SCORE."
10970   (interactive "P")
10971   (gnus-summary-mark-above score gnus-unread-mark))
10972
10973 (defun gnus-summary-tick-above (&optional score)
10974   "Tick all articles with score above SCORE."
10975   (interactive "P")
10976   (gnus-summary-mark-above score gnus-ticked-mark))
10977
10978 (defun gnus-summary-mark-above (score mark)
10979   "Mark articles with score over SCORE with MARK."
10980   (interactive "P\ncMark: ")
10981   (setq score (if score
10982                   (prefix-numeric-value score)
10983                 (or gnus-summary-default-score 0)))
10984   (with-current-buffer gnus-summary-buffer
10985     (goto-char (point-min))
10986     (while (and (progn
10987                   (when (> (gnus-summary-article-score) score)
10988                     (gnus-summary-mark-article nil mark))
10989                   t)
10990                 (gnus-summary-find-next)))))
10991
10992 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10993 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10994 (defun gnus-summary-limit-include-expunged (&optional no-error)
10995   "Display all the hidden articles that were expunged for low scores."
10996   (interactive)
10997   (let ((inhibit-read-only t))
10998     (let ((scored gnus-newsgroup-scored)
10999           headers h)
11000       (while scored
11001         (unless (gnus-summary-article-header (caar scored))
11002           (and (setq h (gnus-number-to-header (caar scored)))
11003                (< (cdar scored) gnus-summary-expunge-below)
11004                (push h headers)))
11005         (setq scored (cdr scored)))
11006       (if (not headers)
11007           (when (not no-error)
11008             (error "No expunged articles hidden"))
11009         (goto-char (point-min))
11010         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11011         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11012         (dolist (x headers)
11013           (push (mail-header-number x) gnus-newsgroup-limit))
11014         (gnus-summary-prepare-unthreaded (nreverse headers))
11015         (goto-char (point-min))
11016         (gnus-summary-position-point)
11017         t))))
11018
11019 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11020   "Mark all unread articles in this newsgroup as read.
11021 If prefix argument ALL is non-nil, ticked and dormant articles will
11022 also be marked as read.
11023 If QUIETLY is non-nil, no questions will be asked.
11024
11025 If TO-HERE is non-nil, it should be a point in the buffer.  All
11026 articles before (after, if REVERSE is set) this point will be marked
11027 as read.
11028
11029 Note that this function will only catch up the unread article
11030 in the current summary buffer limitation.
11031
11032 The number of articles marked as read is returned."
11033   (interactive "P")
11034   (prog1
11035       (save-excursion
11036         (when (or quietly
11037                   (not gnus-interactive-catchup) ;Without confirmation?
11038                   gnus-expert-user
11039                   (gnus-y-or-n-p
11040                    (if all
11041                        "Mark absolutely all articles as read? "
11042                      "Mark all unread articles as read? ")))
11043           (if (and not-mark
11044                    (not gnus-newsgroup-adaptive)
11045                    (not gnus-newsgroup-auto-expire)
11046                    (not gnus-suppress-duplicates)
11047                    (or (not gnus-use-cache)
11048                        (eq gnus-use-cache 'passive)))
11049               (progn
11050                 (when all
11051                   (setq gnus-newsgroup-marked nil
11052                         gnus-newsgroup-spam-marked nil
11053                         gnus-newsgroup-dormant nil))
11054                 (setq gnus-newsgroup-unreads
11055                       (gnus-sorted-nunion
11056                        (gnus-sorted-intersection gnus-newsgroup-unreads
11057                                                  gnus-newsgroup-downloadable)
11058                        (gnus-sorted-difference gnus-newsgroup-unfetched
11059                                                gnus-newsgroup-cached))))
11060             ;; We actually mark all articles as canceled, which we
11061             ;; have to do when using auto-expiry or adaptive scoring.
11062             (gnus-summary-show-all-threads)
11063             (if (and to-here reverse)
11064                 (progn
11065                   (goto-char to-here)
11066                   (gnus-summary-mark-current-read-and-unread-as-read
11067                    gnus-catchup-mark)
11068                   (while (gnus-summary-find-next (not all))
11069                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11070               (when (gnus-summary-first-subject (not all))
11071                 (while (and
11072                         (if to-here (< (point) to-here) t)
11073                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11074                         (gnus-summary-find-next (not all))))))
11075             (gnus-set-mode-line 'summary))
11076           t))
11077     (gnus-summary-position-point)))
11078
11079 (defun gnus-summary-catchup-to-here (&optional all)
11080   "Mark all unticked articles before the current one as read.
11081 If ALL is non-nil, also mark ticked and dormant articles as read."
11082   (interactive "P")
11083   (save-excursion
11084     (gnus-save-hidden-threads
11085       (let ((beg (point)))
11086         ;; We check that there are unread articles.
11087         (when (or all (gnus-summary-find-prev))
11088           (gnus-summary-catchup all t beg)))))
11089   (gnus-summary-position-point))
11090
11091 (defun gnus-summary-catchup-from-here (&optional all)
11092   "Mark all unticked articles after (and including) the current one as read.
11093 If ALL is non-nil, also mark ticked and dormant articles as read."
11094   (interactive "P")
11095   (save-excursion
11096     (gnus-save-hidden-threads
11097       (let ((beg (point)))
11098         ;; We check that there are unread articles.
11099         (when (or all (gnus-summary-find-next))
11100           (gnus-summary-catchup all t beg nil t)))))
11101   (gnus-summary-position-point))
11102
11103 (defun gnus-summary-catchup-all (&optional quietly)
11104   "Mark all articles in this newsgroup as read.
11105 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11106 instead, which marks only unread articles as read."
11107   (interactive "P")
11108   (gnus-summary-catchup t quietly))
11109
11110 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11111   "Mark all unread articles in this group as read, then exit.
11112 If prefix argument ALL is non-nil, all articles are marked as read.
11113 If QUIETLY is non-nil, no questions will be asked."
11114   (interactive "P")
11115   (when (gnus-summary-catchup all quietly nil 'fast)
11116     ;; Select next newsgroup or exit.
11117     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11118              (eq gnus-auto-select-next 'quietly))
11119         (gnus-summary-next-group nil)
11120       (gnus-summary-exit))))
11121
11122 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11123   "Mark all articles in this newsgroup as read, and then exit.
11124 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11125 instead, which marks only unread articles as read."
11126   (interactive "P")
11127   (gnus-summary-catchup-and-exit t quietly))
11128
11129 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11130   "Mark all articles in this group as read and select the next group.
11131 If given a prefix, mark all articles, unread as well as ticked, as
11132 read."
11133   (interactive "P")
11134   (save-excursion
11135     (gnus-summary-catchup all))
11136   (gnus-summary-next-group))
11137
11138 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11139   "Mark all articles in this group as read and select the previous group.
11140 If given a prefix, mark all articles, unread as well as ticked, as
11141 read."
11142   (interactive "P")
11143   (save-excursion
11144     (gnus-summary-catchup all))
11145   (gnus-summary-next-group nil nil t))
11146
11147 ;;;
11148 ;;; with article
11149 ;;;
11150
11151 (defmacro gnus-with-article (article &rest forms)
11152   "Select ARTICLE and perform FORMS in the original article buffer.
11153 Then replace the article with the result."
11154   `(progn
11155      ;; We don't want the article to be marked as read.
11156      (let (gnus-mark-article-hook)
11157        (gnus-summary-select-article t t nil ,article))
11158      (set-buffer gnus-original-article-buffer)
11159      ,@forms
11160      (if (not (gnus-check-backend-function
11161                'request-replace-article (car gnus-article-current)))
11162          (gnus-message 5 "Read-only group; not replacing")
11163        (unless (gnus-request-replace-article
11164                 ,article (car gnus-article-current)
11165                 (current-buffer) t)
11166          (error "Couldn't replace article")))
11167      ;; The cache and backlog have to be flushed somewhat.
11168      (when gnus-keep-backlog
11169        (gnus-backlog-remove-article
11170         (car gnus-article-current) (cdr gnus-article-current)))
11171      (when gnus-use-cache
11172        (gnus-cache-update-article
11173         (car gnus-article-current) (cdr gnus-article-current)))))
11174
11175 (put 'gnus-with-article 'lisp-indent-function 1)
11176 (put 'gnus-with-article 'edebug-form-spec '(form body))
11177
11178 ;; Thread-based commands.
11179
11180 (defun gnus-summary-articles-in-thread (&optional article)
11181   "Return a list of all articles in the current thread.
11182 If ARTICLE is non-nil, return all articles in the thread that starts
11183 with that article."
11184   (let* ((article (or article (gnus-summary-article-number)))
11185          (data (gnus-data-find-list article))
11186          (top-level (gnus-data-level (car data)))
11187          (top-subject
11188           (cond ((null gnus-thread-operation-ignore-subject)
11189                  (gnus-simplify-subject-re
11190                   (mail-header-subject (gnus-data-header (car data)))))
11191                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11192                  (gnus-simplify-subject-fuzzy
11193                   (mail-header-subject (gnus-data-header (car data)))))
11194                 (t nil)))
11195          (end-point (save-excursion
11196                       (if (gnus-summary-go-to-next-thread)
11197                           (point) (point-max))))
11198          articles)
11199     (while (and data
11200                 (< (gnus-data-pos (car data)) end-point))
11201       (when (or (not top-subject)
11202                 (string= top-subject
11203                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11204                              (gnus-simplify-subject-fuzzy
11205                               (mail-header-subject
11206                                (gnus-data-header (car data))))
11207                            (gnus-simplify-subject-re
11208                             (mail-header-subject
11209                              (gnus-data-header (car data)))))))
11210         (push (gnus-data-number (car data)) articles))
11211       (unless (and (setq data (cdr data))
11212                    (> (gnus-data-level (car data)) top-level))
11213         (setq data nil)))
11214     ;; Return the list of articles.
11215     (nreverse articles)))
11216
11217 (defun gnus-summary-rethread-current ()
11218   "Rethread the thread the current article is part of."
11219   (interactive)
11220   (let* ((gnus-show-threads t)
11221          (article (gnus-summary-article-number))
11222          (id (mail-header-id (gnus-summary-article-header)))
11223          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11224     (unless id
11225       (error "No article on the current line"))
11226     (gnus-rebuild-thread id)
11227     (gnus-summary-goto-subject article)))
11228
11229 (defun gnus-summary-reparent-thread ()
11230   "Make the current article child of the marked (or previous) article.
11231
11232 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11233 is non-nil or the Subject: of both articles are the same."
11234   (interactive)
11235   (unless (not (gnus-group-read-only-p))
11236     (error "The current newsgroup does not support article editing"))
11237   (unless (<= (length gnus-newsgroup-processable) 1)
11238     (error "No more than one article may be marked"))
11239   (let ((child (gnus-summary-article-number))
11240         ;; First grab the marked article, otherwise one line up.
11241         (parent (if (not (null gnus-newsgroup-processable))
11242                     (car gnus-newsgroup-processable)
11243                   (save-excursion
11244                     (if (eq (forward-line -1) 0)
11245                         (gnus-summary-article-number)
11246                       (error "Beginning of summary buffer"))))))
11247     (gnus-summary-reparent-children parent (list child))))
11248
11249 (defun gnus-summary-reparent-children (parent children)
11250   "Make PARENT the parent of CHILDREN.
11251 When called interactively, PARENT is the current article and CHILDREN
11252 are the process-marked articles."
11253   (interactive
11254    (list (gnus-summary-article-number)
11255          (gnus-summary-work-articles nil)))
11256   (dolist (child children)
11257     (save-window-excursion
11258       (let ((gnus-article-buffer " *reparent*"))
11259         (unless (not (eq parent child))
11260           (error "An article may not be self-referential"))
11261         (let ((message-id (mail-header-id
11262                            (gnus-summary-article-header parent))))
11263           (unless (and message-id (not (equal message-id "")))
11264             (error "No message-id in desired parent"))
11265           (gnus-with-article child
11266             (save-restriction
11267               (goto-char (point-min))
11268               (message-narrow-to-head)
11269               (if (re-search-forward "^References: " nil t)
11270                   (progn
11271                     (re-search-forward "^[^ \t]" nil t)
11272                     (forward-line -1)
11273                     (end-of-line)
11274                     (insert " " message-id))
11275                 (insert "References: " message-id "\n"))))
11276           (set-buffer gnus-summary-buffer)
11277           (gnus-summary-unmark-all-processable)
11278           (gnus-summary-update-article child)
11279           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11280             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11281           (gnus-summary-rethread-current)
11282           (gnus-message 3 "Article %d is now the child of article %d"
11283                         child parent))))))
11284
11285 (defun gnus-summary-toggle-threads (&optional arg)
11286   "Toggle showing conversation threads.
11287 If ARG is positive number, turn showing conversation threads on."
11288   (interactive "P")
11289   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11290     (setq gnus-show-threads
11291           (if (null arg) (not gnus-show-threads)
11292             (> (prefix-numeric-value arg) 0)))
11293     (gnus-summary-prepare)
11294     (gnus-summary-goto-subject current)
11295     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11296     (gnus-summary-position-point)))
11297
11298 (eval-and-compile
11299   (if (fboundp 'remove-overlays)
11300       (defalias 'gnus-remove-overlays 'remove-overlays)
11301     (defun gnus-remove-overlays (beg end name val)
11302       "Clear BEG and END of overlays whose property NAME has value VAL.
11303 For compatibility with XEmacs."
11304       (dolist (ov (gnus-overlays-in beg end))
11305         (when (eq (gnus-overlay-get ov name) val)
11306           (gnus-delete-overlay ov))))))
11307
11308 (defun gnus-summary-show-all-threads ()
11309   "Show all threads."
11310   (interactive)
11311   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11312   (gnus-summary-position-point))
11313
11314 (defsubst gnus-summary--inv (p)
11315   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11316
11317 (defun gnus-summary-show-thread ()
11318   "Show thread subtrees.
11319 Returns nil if no thread was there to be shown."
11320   (interactive)
11321   (let* ((orig (point))
11322          (end (point-at-eol))
11323          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11324          ;; Leave point at bol
11325          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11326          (eoi (when end
11327                 (if (fboundp 'next-single-char-property-change)
11328                     (or (next-single-char-property-change end 'invisible)
11329                         (point-max))
11330                   (while (progn
11331                            (end-of-line 2)
11332                            (and (not (eobp))
11333                                 (eq (get-char-property (point) 'invisible)
11334                                     'gnus-sum))))
11335                   (point)))))
11336     (when eoi
11337       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11338       (goto-char orig)
11339       (gnus-summary-position-point)
11340       eoi)))
11341
11342 (defun gnus-summary-maybe-hide-threads ()
11343   "If requested, hide the threads that should be hidden."
11344   (when (and gnus-show-threads
11345              gnus-thread-hide-subtree)
11346     (gnus-summary-hide-all-threads
11347      (if (or (consp gnus-thread-hide-subtree)
11348              (functionp gnus-thread-hide-subtree))
11349          (gnus-make-predicate gnus-thread-hide-subtree)
11350        nil))))
11351
11352 ;;; Hiding predicates.
11353
11354 (defun gnus-article-unread-p (header)
11355   (memq (mail-header-number header) gnus-newsgroup-unreads))
11356
11357 (defun gnus-article-unseen-p (header)
11358   (memq (mail-header-number header) gnus-newsgroup-unseen))
11359
11360 (defun gnus-map-articles (predicate articles)
11361   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11362   (apply 'gnus-or (mapcar predicate
11363                           (mapcar (lambda (number)
11364                                     (gnus-summary-article-header number))
11365                                   articles))))
11366
11367 (defun gnus-summary-hide-all-threads (&optional predicate)
11368   "Hide all thread subtrees.
11369 If PREDICATE is supplied, threads that satisfy this predicate
11370 will not be hidden."
11371   (interactive)
11372   (save-excursion
11373     (goto-char (point-min))
11374     (let ((end nil))
11375       (while (not end)
11376         (when (or (not predicate)
11377                   (gnus-map-articles
11378                    predicate (gnus-summary-article-children)))
11379             (gnus-summary-hide-thread))
11380         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11381   (gnus-summary-position-point))
11382
11383 (defun gnus-summary-hide-thread ()
11384   "Hide thread subtrees.
11385 If PREDICATE is supplied, threads that satisfy this predicate
11386 will not be hidden.
11387 Returns nil if no threads were there to be hidden."
11388   (interactive)
11389   (let ((start (point))
11390         (starteol (line-end-position))
11391         (article (gnus-summary-article-number)))
11392     (goto-char start)
11393     ;; Go forward until either the buffer ends or the subthread ends.
11394     (when (and (not (eobp))
11395                (or (zerop (gnus-summary-next-thread 1 t))
11396                    (goto-char (point-max))))
11397       (if (and (> (point) start)
11398                ;; FIXME: this should actually search for a non-invisible \n.
11399                (search-backward "\n" start t))
11400           (progn
11401             (when (> (point) starteol)
11402               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11403               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11404                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11405                 (gnus-overlay-put ol 'evaporate t)))
11406             (gnus-summary-goto-subject article))
11407         (goto-char start)
11408         nil))))
11409
11410 (defun gnus-summary-go-to-next-thread (&optional previous)
11411   "Go to the same level (or less) next thread.
11412 If PREVIOUS is non-nil, go to previous thread instead.
11413 Return the article number moved to, or nil if moving was impossible."
11414   (let ((level (gnus-summary-thread-level))
11415         (way (if previous -1 1))
11416         (beg (point)))
11417     (forward-line way)
11418     (while (and (not (eobp))
11419                 (< level (gnus-summary-thread-level)))
11420       (forward-line way))
11421     (if (eobp)
11422         (progn
11423           (goto-char beg)
11424           nil)
11425       (setq beg (point))
11426       (prog1
11427           (gnus-summary-article-number)
11428         (goto-char beg)))))
11429
11430 (defun gnus-summary-next-thread (n &optional silent)
11431   "Go to the same level next N'th thread.
11432 If N is negative, search backward instead.
11433 Returns the difference between N and the number of skips actually
11434 done.
11435
11436 If SILENT, don't output messages."
11437   (interactive "p")
11438   (let ((backward (< n 0))
11439         (n (abs n)))
11440     (while (and (> n 0)
11441                 (gnus-summary-go-to-next-thread backward))
11442       (decf n))
11443     (unless silent
11444       (gnus-summary-position-point))
11445     (when (and (not silent) (/= 0 n))
11446       (gnus-message 7 "No more threads"))
11447     n))
11448
11449 (defun gnus-summary-prev-thread (n)
11450   "Go to the same level previous N'th thread.
11451 Returns the difference between N and the number of skips actually
11452 done."
11453   (interactive "p")
11454   (gnus-summary-next-thread (- n)))
11455
11456 (defun gnus-summary-go-down-thread ()
11457   "Go down one level in the current thread."
11458   (let ((children (gnus-summary-article-children)))
11459     (when children
11460       (gnus-summary-goto-subject (car children)))))
11461
11462 (defun gnus-summary-go-up-thread ()
11463   "Go up one level in the current thread."
11464   (let ((parent (gnus-summary-article-parent)))
11465     (when parent
11466       (gnus-summary-goto-subject parent))))
11467
11468 (defun gnus-summary-down-thread (n)
11469   "Go down thread N steps.
11470 If N is negative, go up instead.
11471 Returns the difference between N and how many steps down that were
11472 taken."
11473   (interactive "p")
11474   (let ((up (< n 0))
11475         (n (abs n)))
11476     (while (and (> n 0)
11477                 (if up (gnus-summary-go-up-thread)
11478                   (gnus-summary-go-down-thread)))
11479       (setq n (1- n)))
11480     (gnus-summary-position-point)
11481     (when (/= 0 n)
11482       (gnus-message 7 "Can't go further"))
11483     n))
11484
11485 (defun gnus-summary-up-thread (n)
11486   "Go up thread N steps.
11487 If N is negative, go down instead.
11488 Returns the difference between N and how many steps down that were
11489 taken."
11490   (interactive "p")
11491   (gnus-summary-down-thread (- n)))
11492
11493 (defun gnus-summary-top-thread ()
11494   "Go to the top of the thread."
11495   (interactive)
11496   (while (gnus-summary-go-up-thread))
11497   (gnus-summary-article-number))
11498
11499 (defun gnus-summary-expire-thread ()
11500   "Mark articles under current thread as expired."
11501   (interactive)
11502   (gnus-summary-kill-thread 0))
11503
11504 (defun gnus-summary-kill-thread (&optional unmark)
11505   "Mark articles under current thread as read.
11506 If the prefix argument is positive, remove any kinds of marks.
11507 If the prefix argument is zero, mark thread as expired.
11508 If the prefix argument is negative, tick articles instead."
11509   (interactive "P")
11510   (when unmark
11511     (setq unmark (prefix-numeric-value unmark)))
11512   (let ((articles (gnus-summary-articles-in-thread))
11513         (hide (or (null unmark) (= unmark 0))))
11514     (save-excursion
11515       ;; Expand the thread.
11516       (gnus-summary-show-thread)
11517       ;; Mark all the articles.
11518       (while articles
11519         (gnus-summary-goto-subject (car articles))
11520         (cond ((null unmark)
11521                (gnus-summary-mark-article-as-read gnus-killed-mark))
11522               ((> unmark 0)
11523                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11524               ((= unmark 0)
11525                (gnus-summary-mark-article nil gnus-expirable-mark))
11526               (t
11527                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11528         (setq articles (cdr articles))))
11529     ;; Hide killed subtrees when hide is true.
11530     (and hide
11531          gnus-thread-hide-killed
11532          (gnus-summary-hide-thread))
11533     ;; If hide is t, go to next unread subject.
11534     (when hide
11535       ;; Go to next unread subject.
11536       (gnus-summary-next-subject 1 t)))
11537   (gnus-set-mode-line 'summary))
11538
11539 ;; Summary sorting commands
11540
11541 (defun gnus-summary-sort-by-number (&optional reverse)
11542   "Sort the summary buffer by article number.
11543 Argument REVERSE means reverse order."
11544   (interactive "P")
11545   (gnus-summary-sort 'number reverse))
11546
11547 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11548   "Sort the summary buffer by most recent article number.
11549 Argument REVERSE means reverse order."
11550   (interactive "P")
11551   (gnus-summary-sort 'most-recent-number reverse))
11552
11553 (defun gnus-summary-sort-by-random (&optional reverse)
11554   "Randomize the order in the summary buffer.
11555 Argument REVERSE means to randomize in reverse order."
11556   (interactive "P")
11557   (gnus-summary-sort 'random reverse))
11558
11559 (defun gnus-summary-sort-by-author (&optional reverse)
11560   "Sort the summary buffer by author name alphabetically.
11561 If `case-fold-search' is non-nil, case of letters is ignored.
11562 Argument REVERSE means reverse order."
11563   (interactive "P")
11564   (gnus-summary-sort 'author reverse))
11565
11566 (defun gnus-summary-sort-by-recipient (&optional reverse)
11567   "Sort the summary buffer by recipient name alphabetically.
11568 If `case-fold-search' is non-nil, case of letters is ignored.
11569 Argument REVERSE means reverse order."
11570   (interactive "P")
11571   (gnus-summary-sort 'recipient reverse))
11572
11573 (defun gnus-summary-sort-by-subject (&optional reverse)
11574   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11575 If `case-fold-search' is non-nil, case of letters is ignored.
11576 Argument REVERSE means reverse order."
11577   (interactive "P")
11578   (gnus-summary-sort 'subject reverse))
11579
11580 (defun gnus-summary-sort-by-date (&optional reverse)
11581   "Sort the summary buffer by date.
11582 Argument REVERSE means reverse order."
11583   (interactive "P")
11584   (gnus-summary-sort 'date reverse))
11585
11586 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11587   "Sort the summary buffer by most recent date.
11588 Argument REVERSE means reverse order."
11589   (interactive "P")
11590   (gnus-summary-sort 'most-recent-date reverse))
11591
11592 (defun gnus-summary-sort-by-score (&optional reverse)
11593   "Sort the summary buffer by score.
11594 Argument REVERSE means reverse order."
11595   (interactive "P")
11596   (gnus-summary-sort 'score reverse))
11597
11598 (defun gnus-summary-sort-by-lines (&optional reverse)
11599   "Sort the summary buffer by the number of lines.
11600 Argument REVERSE means reverse order."
11601   (interactive "P")
11602   (gnus-summary-sort 'lines reverse))
11603
11604 (defun gnus-summary-sort-by-chars (&optional reverse)
11605   "Sort the summary buffer by article length.
11606 Argument REVERSE means reverse order."
11607   (interactive "P")
11608   (gnus-summary-sort 'chars reverse))
11609
11610 (defun gnus-summary-sort-by-original (&optional reverse)
11611   "Sort the summary buffer using the default sorting method.
11612 Argument REVERSE means reverse order."
11613   (interactive "P")
11614   (let* ((inhibit-read-only t)
11615          (gnus-summary-prepare-hook nil))
11616     ;; We do the sorting by regenerating the threads.
11617     (gnus-summary-prepare)
11618     ;; Hide subthreads if needed.
11619     (gnus-summary-maybe-hide-threads)))
11620
11621 (defun gnus-summary-sort (predicate reverse)
11622   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11623   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11624          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11625          (gnus-thread-sort-functions
11626           (if (not reverse)
11627               thread
11628             `(lambda (t1 t2)
11629                (,thread t2 t1))))
11630          (gnus-sort-gathered-threads-function
11631           gnus-thread-sort-functions)
11632          (gnus-article-sort-functions
11633           (if (not reverse)
11634               article
11635             `(lambda (t1 t2)
11636                (,article t2 t1))))
11637          (inhibit-read-only t)
11638          (gnus-summary-prepare-hook nil))
11639     ;; We do the sorting by regenerating the threads.
11640     (gnus-summary-prepare)
11641     ;; Hide subthreads if needed.
11642     (gnus-summary-maybe-hide-threads)))
11643
11644 ;; Summary saving commands.
11645
11646 (defun gnus-summary-save-article (&optional n not-saved)
11647   "Save the current article using the default saver function.
11648 If N is a positive number, save the N next articles.
11649 If N is a negative number, save the N previous articles.
11650 If N is nil and any articles have been marked with the process mark,
11651 save those articles instead.
11652 The variable `gnus-default-article-saver' specifies the saver function.
11653
11654 If the optional second argument NOT-SAVED is non-nil, articles saved
11655 will not be marked as saved."
11656   (interactive "P")
11657   (require 'gnus-art)
11658   (let* ((articles (gnus-summary-work-articles n))
11659          (save-buffer (save-excursion
11660                         (nnheader-set-temp-buffer " *Gnus Save*")))
11661          (num (length articles))
11662          ;; Whether to save decoded articles or raw articles.
11663          (decode (when gnus-article-save-coding-system
11664                    (get gnus-default-article-saver :decode)))
11665          ;; When saving many articles in a single file, use the other
11666          ;; function to save articles other than the first one.
11667          (saver2 (get gnus-default-article-saver :function))
11668          (gnus-prompt-before-saving (if saver2
11669                                         t
11670                                       gnus-prompt-before-saving))
11671          (gnus-default-article-saver gnus-default-article-saver)
11672          header file)
11673     (dolist (article articles)
11674       (setq header (gnus-summary-article-header article))
11675       (if (not (vectorp header))
11676           ;; This is a pseudo-article.
11677           (if (assq 'name header)
11678               (gnus-copy-file (cdr (assq 'name header)))
11679             (gnus-message 1 "Article %d is unsaveable" article))
11680         ;; This is a real article.
11681         (save-window-excursion
11682           (gnus-summary-select-article decode decode nil article)
11683           (gnus-summary-goto-subject article))
11684         (with-current-buffer save-buffer
11685           (erase-buffer)
11686           (insert-buffer-substring (if decode
11687                                        gnus-article-buffer
11688                                      gnus-original-article-buffer)))
11689         (setq file (gnus-article-save save-buffer file num))
11690         (gnus-summary-remove-process-mark article)
11691         (unless not-saved
11692           (gnus-summary-set-saved-mark article)))
11693       (when saver2
11694         (setq gnus-default-article-saver saver2
11695               saver2 nil)))
11696     (gnus-kill-buffer save-buffer)
11697     (gnus-summary-position-point)
11698     (gnus-set-mode-line 'summary)
11699     n))
11700
11701 (defun gnus-summary-pipe-output (&optional n sym)
11702   "Pipe the current article to a subprocess.
11703 If N is a positive number, pipe the N next articles.
11704 If N is a negative number, pipe the N previous articles.
11705 If N is nil and any articles have been marked with the process mark,
11706 pipe those articles instead.
11707 The default command to which articles are piped is specified by the
11708 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11709 will be prompted for the command.
11710
11711 The properties `:decode' and `:headers' that are put to the function
11712 symbol `gnus-summary-save-in-pipe' control whether this function
11713 decodes articles and what headers to keep (see the doc string for the
11714 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11715 is neither omitted nor the symbol `r', force including all headers
11716 regardless of the `:headers' property.  If it is the symbol `r',
11717 articles that are not decoded and include all headers will be piped
11718 no matter what the properties `:decode' and `:headers' are."
11719   (interactive (gnus-interactive "P\ny"))
11720   (require 'gnus-art)
11721   (let* ((articles (gnus-summary-work-articles n))
11722          (result-buffer "*Shell Command Output*")
11723          (all-headers (not (memq sym '(nil r))))
11724          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11725          (raw (eq sym 'r))
11726          (headers (get 'gnus-summary-save-in-pipe :headers))
11727          command result)
11728     (unless (numberp (car articles))
11729       (error "No article to pipe"))
11730     (setq command (gnus-read-shell-command
11731                    (concat "Shell command on "
11732                            (if (cdr articles)
11733                                (format "these %d articles" (length articles))
11734                              "this article")
11735                            ": ")
11736                    gnus-summary-pipe-output-default-command))
11737     (when (string-equal command "")
11738       (error "A command is required"))
11739     (when all-headers
11740       (put 'gnus-summary-save-in-pipe :headers nil))
11741     (unwind-protect
11742         (while articles
11743           (gnus-summary-goto-subject (pop articles))
11744           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11745           (when (and (get-buffer result-buffer)
11746                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11747             (setq result (concat result (with-current-buffer result-buffer
11748                                           (buffer-string))))))
11749       (put 'gnus-summary-save-in-pipe :headers headers))
11750     (unless (zerop (length result))
11751       (if (with-current-buffer (get-buffer-create result-buffer)
11752             (erase-buffer)
11753             (insert result)
11754             (prog1
11755                 (and (= (count-lines (point-min) (point)) 1)
11756                      (progn
11757                        (end-of-line 0)
11758                        (<= (current-column)
11759                            (window-width (minibuffer-window)))))
11760               (goto-char (point-min))))
11761           (message "%s" (substring result 0 -1))
11762         (message nil)
11763         (gnus-configure-windows 'pipe)))))
11764
11765 (defun gnus-summary-save-article-mail (&optional arg)
11766   "Append the current article to a Unix mail box file.
11767 If N is a positive number, save the N next articles.
11768 If N is a negative number, save the N previous articles.
11769 If N is nil and any articles have been marked with the process mark,
11770 save those articles instead."
11771   (interactive "P")
11772   (require 'gnus-art)
11773   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11774     (gnus-summary-save-article arg)))
11775
11776 (defun gnus-summary-save-article-rmail (&optional arg)
11777   "Append the current article to an rmail file.
11778 If N is a positive number, save the N next articles.
11779 If N is a negative number, save the N previous articles.
11780 If N is nil and any articles have been marked with the process mark,
11781 save those articles instead."
11782   (interactive "P")
11783   (require 'gnus-art)
11784   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11785     (gnus-summary-save-article arg)))
11786
11787 (defun gnus-summary-save-article-file (&optional arg)
11788   "Append the current article to a file.
11789 If N is a positive number, save the N next articles.
11790 If N is a negative number, save the N previous articles.
11791 If N is nil and any articles have been marked with the process mark,
11792 save those articles instead."
11793   (interactive "P")
11794   (require 'gnus-art)
11795   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11796     (gnus-summary-save-article arg)))
11797
11798 (defun gnus-summary-write-article-file (&optional arg)
11799   "Write the current article to a file, deleting the previous file.
11800 If N is a positive number, save the N next articles.
11801 If N is a negative number, save the N previous articles.
11802 If N is nil and any articles have been marked with the process mark,
11803 save those articles instead."
11804   (interactive "P")
11805   (require 'gnus-art)
11806   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11807     (gnus-summary-save-article arg)))
11808
11809 (defun gnus-summary-save-article-body-file (&optional arg)
11810   "Append the current article body to a file.
11811 If N is a positive number, save the N next articles.
11812 If N is a negative number, save the N previous articles.
11813 If N is nil and any articles have been marked with the process mark,
11814 save those articles instead."
11815   (interactive "P")
11816   (require 'gnus-art)
11817   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11818     (gnus-summary-save-article arg)))
11819
11820 (defun gnus-summary-write-article-body-file (&optional arg)
11821   "Write the current article body to a file, deleting the previous file.
11822 If N is a positive number, save the N next articles.
11823 If N is a negative number, save the N previous articles.
11824 If N is nil and any articles have been marked with the process mark,
11825 save those articles instead."
11826   (interactive "P")
11827   (require 'gnus-art)
11828   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11829     (gnus-summary-save-article arg)))
11830
11831 (defun gnus-summary-muttprint (&optional arg)
11832   "Print the current article using Muttprint.
11833 If N is a positive number, save the N next articles.
11834 If N is a negative number, save the N previous articles.
11835 If N is nil and any articles have been marked with the process mark,
11836 save those articles instead."
11837   (interactive "P")
11838   (require 'gnus-art)
11839   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11840     (gnus-summary-save-article arg t)))
11841
11842 (defun gnus-summary-pipe-message (program)
11843   "Pipe the current article through PROGRAM."
11844   (interactive "sProgram: ")
11845   (gnus-summary-select-article)
11846   (let ((mail-header-separator ""))
11847     (gnus-eval-in-buffer-window gnus-article-buffer
11848       (save-restriction
11849         (widen)
11850         (let ((start (window-start))
11851               (inhibit-read-only t))
11852           (message-pipe-buffer-body program)
11853           (set-window-start (get-buffer-window (current-buffer)) start))))))
11854
11855 (defun gnus-get-split-value (methods)
11856   "Return a value based on the split METHODS."
11857   (let (split-name method result match)
11858     (when methods
11859       (with-current-buffer gnus-original-article-buffer
11860         (save-restriction
11861           (nnheader-narrow-to-headers)
11862           (while (and methods (not split-name))
11863             (goto-char (point-min))
11864             (setq method (pop methods))
11865             (setq match (car method))
11866             (when (cond
11867                    ((stringp match)
11868                     ;; Regular expression.
11869                     (ignore-errors
11870                       (re-search-forward match nil t)))
11871                    ((functionp match)
11872                     ;; Function.
11873                     (save-restriction
11874                       (widen)
11875                       (setq result (funcall match gnus-newsgroup-name))))
11876                    ((consp match)
11877                     ;; Form.
11878                     (save-restriction
11879                       (widen)
11880                       (setq result (eval match)))))
11881               (setq split-name (cdr method))
11882               (cond ((stringp result)
11883                      (push (expand-file-name
11884                             result gnus-article-save-directory)
11885                            split-name))
11886                     ((consp result)
11887                      (setq split-name (append result split-name)))))))))
11888     (nreverse split-name)))
11889
11890 (defun gnus-valid-move-group-p (group)
11891   (and (symbolp group)
11892        (boundp group)
11893        (symbol-name group)
11894        (symbol-value group)
11895        (gnus-get-function (gnus-find-method-for-group
11896                            (symbol-name group)) 'request-accept-article t)))
11897
11898 (defun gnus-read-move-group-name (prompt default articles prefix)
11899   "Read a group name."
11900   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11901          (minibuffer-confirm-incomplete nil) ; XEmacs
11902          (prom
11903           (format "%s %s to"
11904                   prompt
11905                   (if (> (length articles) 1)
11906                       (format "these %d articles" (length articles))
11907                     "this article")))
11908          (to-newsgroup
11909           (cond
11910            ((null split-name)
11911             (gnus-group-completing-read
11912              prom
11913              (remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb)
11914              nil prefix nil default))
11915            ((= 1 (length split-name))
11916             (gnus-group-completing-read
11917              prom (remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb)
11918              nil prefix 'gnus-group-history (car split-name)))
11919            (t
11920             (gnus-completing-read
11921              prom (nreverse split-name) nil nil 'gnus-group-history))))
11922          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11923          encoded)
11924     (when to-newsgroup
11925       (if (or (string= to-newsgroup "")
11926               (string= to-newsgroup prefix))
11927           (setq to-newsgroup default))
11928       (unless to-newsgroup
11929         (error "No group name entered"))
11930       (setq encoded (mm-encode-coding-string
11931                      to-newsgroup
11932                      (gnus-group-name-charset to-method to-newsgroup)))
11933       (or (gnus-active encoded)
11934           (gnus-activate-group encoded nil nil to-method)
11935           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11936                                      to-newsgroup))
11937               (or (and (gnus-request-create-group encoded to-method)
11938                        (gnus-activate-group encoded nil nil to-method)
11939                        (gnus-subscribe-group encoded))
11940                   (error "Couldn't create group %s" to-newsgroup)))
11941           (error "No such group: %s" to-newsgroup))
11942       encoded)))
11943
11944 (defvar gnus-summary-save-parts-counter)
11945 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11946
11947 (defun gnus-summary-save-parts (type dir n &optional reverse)
11948   "Save parts matching TYPE to DIR.
11949 If REVERSE, save parts that do not match TYPE."
11950   (interactive
11951    (list (read-string "Save parts of type: "
11952                       (or (car gnus-summary-save-parts-type-history)
11953                           gnus-summary-save-parts-default-mime)
11954                       'gnus-summary-save-parts-type-history)
11955          (setq gnus-summary-save-parts-last-directory
11956                (read-file-name "Save to directory: "
11957                                gnus-summary-save-parts-last-directory
11958                                nil t))
11959          current-prefix-arg))
11960   (gnus-summary-iterate n
11961     (let ((gnus-display-mime-function nil)
11962           gnus-article-prepare-hook
11963           gnus-article-decode-hook
11964           gnus-display-mime-function
11965           gnus-break-pages
11966           (gnus-inhibit-treatment t))
11967       (gnus-summary-select-article))
11968     (with-current-buffer gnus-article-buffer
11969       (let ((handles (or gnus-article-mime-handles
11970                          (mm-dissect-buffer nil gnus-article-loose-mime)
11971                          (and gnus-article-emulate-mime
11972                               (mm-uu-dissect))))
11973             (gnus-summary-save-parts-counter 1))
11974         (when handles
11975           (gnus-summary-save-parts-1 type dir handles reverse)
11976           (unless gnus-article-mime-handles ;; Don't destroy this case.
11977             (mm-destroy-parts handles)))))))
11978
11979 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11980   (if (stringp (car handle))
11981       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11982               (cdr handle))
11983     (when (if reverse
11984               (not (string-match type (mm-handle-media-type handle)))
11985             (string-match type (mm-handle-media-type handle)))
11986       (let ((file (expand-file-name
11987                    (gnus-map-function
11988                     mm-file-name-rewrite-functions
11989                     (file-name-nondirectory
11990                      (or
11991                       (mail-content-type-get
11992                        (mm-handle-disposition handle) 'filename)
11993                       (mail-content-type-get
11994                        (mm-handle-type handle) 'name)
11995                       (format "%s.%d.%d" gnus-newsgroup-name
11996                               (cdr gnus-article-current)
11997                               gnus-summary-save-parts-counter))))
11998                    dir)))
11999         (incf gnus-summary-save-parts-counter)
12000         (unless (file-exists-p file)
12001           (mm-save-part-to-file handle file))))))
12002
12003 ;; Summary extract commands
12004
12005 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12006   (let ((inhibit-read-only t)
12007         (article (gnus-summary-article-number))
12008         after-article b e)
12009     (unless (gnus-summary-goto-subject article)
12010       (error "No such article: %d" article))
12011     (gnus-summary-position-point)
12012     ;; If all commands are to be bunched up on one line, we collect
12013     ;; them here.
12014     (unless gnus-view-pseudos-separately
12015       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12016             files action)
12017         (while ps
12018           (setq action (cdr (assq 'action (car ps))))
12019           (setq files (list (cdr (assq 'name (car ps)))))
12020           (while (and ps (cdr ps)
12021                       (string= (or action "1")
12022                                (or (cdr (assq 'action (cadr ps))) "2")))
12023             (push (cdr (assq 'name (cadr ps))) files)
12024             (setcdr ps (cddr ps)))
12025           (when files
12026             (when (not (string-match "%s" action))
12027               (push " " files))
12028             (push " " files)
12029             (when (assq 'execute (car ps))
12030               (setcdr (assq 'execute (car ps))
12031                       (funcall (if (string-match "%s" action)
12032                                    'format 'concat)
12033                                action
12034                                (mapconcat
12035                                 (lambda (f)
12036                                   (if (equal f " ")
12037                                       f
12038                                     (shell-quote-argument f)))
12039                                 files " ")))))
12040           (setq ps (cdr ps)))))
12041     (if (and gnus-view-pseudos (not not-view))
12042         (while pslist
12043           (when (assq 'execute (car pslist))
12044             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12045                                   (eq gnus-view-pseudos 'not-confirm)))
12046           (setq pslist (cdr pslist)))
12047       (save-excursion
12048         (while pslist
12049           (setq after-article (or (cdr (assq 'article (car pslist)))
12050                                   (gnus-summary-article-number)))
12051           (gnus-summary-goto-subject after-article)
12052           (forward-line 1)
12053           (setq b (point))
12054           (insert "    " (file-name-nondirectory
12055                           (cdr (assq 'name (car pslist))))
12056                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12057           (setq e (point))
12058           (forward-line -1)             ; back to `b'
12059           (gnus-add-text-properties
12060            b (1- e) (list 'gnus-number gnus-reffed-article-number
12061                           gnus-mouse-face-prop gnus-mouse-face))
12062           (gnus-data-enter
12063            after-article gnus-reffed-article-number
12064            gnus-unread-mark b (car pslist) 0 (- e b))
12065           (setq gnus-newsgroup-unreads
12066                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12067                                          gnus-reffed-article-number))
12068           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12069           (setq pslist (cdr pslist)))))))
12070
12071 (defun gnus-pseudos< (p1 p2)
12072   (let ((c1 (cdr (assq 'action p1)))
12073         (c2 (cdr (assq 'action p2))))
12074     (and c1 c2 (string< c1 c2))))
12075
12076 (defun gnus-request-pseudo-article (props)
12077   (cond ((assq 'execute props)
12078          (gnus-execute-command (cdr (assq 'execute props)))))
12079   (let ((gnus-current-article (gnus-summary-article-number)))
12080     (gnus-run-hooks 'gnus-mark-article-hook)))
12081
12082 (defun gnus-execute-command (command &optional automatic)
12083   (save-excursion
12084     (gnus-article-setup-buffer)
12085     (set-buffer gnus-article-buffer)
12086     (setq buffer-read-only nil)
12087     (let ((command (if automatic command
12088                      (read-string "Command: " (cons command 0)))))
12089       (erase-buffer)
12090       (insert "$ " command "\n\n")
12091       (if gnus-view-pseudo-asynchronously
12092           (start-process "gnus-execute" (current-buffer) shell-file-name
12093                          shell-command-switch command)
12094         (call-process shell-file-name nil t nil
12095                       shell-command-switch command)))))
12096
12097 ;; Summary kill commands.
12098
12099 (defun gnus-summary-edit-global-kill (article)
12100   "Edit the \"global\" kill file."
12101   (interactive (list (gnus-summary-article-number)))
12102   (gnus-group-edit-global-kill article))
12103
12104 (defun gnus-summary-edit-local-kill ()
12105   "Edit a local kill file applied to the current newsgroup."
12106   (interactive)
12107   (setq gnus-current-headers (gnus-summary-article-header))
12108   (gnus-group-edit-local-kill
12109    (gnus-summary-article-number) gnus-newsgroup-name))
12110
12111 ;;; Header reading.
12112
12113 (defun gnus-read-header (id &optional header)
12114   "Read the headers of article ID and enter them into the Gnus system."
12115   (let ((group gnus-newsgroup-name)
12116         (gnus-override-method
12117          (or
12118           gnus-override-method
12119           (and (gnus-news-group-p gnus-newsgroup-name)
12120                (car (gnus-refer-article-methods)))))
12121         where)
12122     ;; First we check to see whether the header in question is already
12123     ;; fetched.
12124     (if (stringp id)
12125         ;; This is a Message-ID.
12126         (setq header (or header (gnus-id-to-header id)))
12127       ;; This is an article number.
12128       (setq header (or header (gnus-summary-article-header id))))
12129     (if (and header
12130              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12131         ;; We have found the header.
12132         header
12133       ;; We have to really fetch the header to this article.
12134       (with-current-buffer nntp-server-buffer
12135         (when (setq where (gnus-request-head id group))
12136           (nnheader-fold-continuation-lines)
12137           (goto-char (point-max))
12138           (insert ".\n")
12139           (goto-char (point-min))
12140           (insert "211 ")
12141           (princ (cond
12142                   ((numberp id) id)
12143                   ((cdr where) (cdr where))
12144                   (header (mail-header-number header))
12145                   (t gnus-reffed-article-number))
12146                  (current-buffer))
12147           (insert " Article retrieved.\n"))
12148         (if (or (not where)
12149                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12150             ()                          ; Malformed head.
12151           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12152             (when (and (stringp id)
12153                        (or
12154                         (not (string= (gnus-group-real-name group)
12155                                       (car where)))
12156                         (not (gnus-server-equal gnus-override-method
12157                                                 (gnus-group-method group)))))
12158               ;; If we fetched by Message-ID and the article came from
12159               ;; a different group (or server), we fudge some bogus
12160               ;; article numbers for this article.
12161               (mail-header-set-number header gnus-reffed-article-number))
12162             (with-current-buffer gnus-summary-buffer
12163               (decf gnus-reffed-article-number)
12164               (gnus-remove-header (mail-header-number header))
12165               (push header gnus-newsgroup-headers)
12166               (setq gnus-current-headers header)
12167               (push (mail-header-number header) gnus-newsgroup-limit)))
12168           header)))))
12169
12170 (defun gnus-remove-header (number)
12171   "Remove header NUMBER from `gnus-newsgroup-headers'."
12172   (if (and gnus-newsgroup-headers
12173            (= number (mail-header-number (car gnus-newsgroup-headers))))
12174       (pop gnus-newsgroup-headers)
12175     (let ((headers gnus-newsgroup-headers))
12176       (while (and (cdr headers)
12177                   (not (= number (mail-header-number (cadr headers)))))
12178         (pop headers))
12179       (when (cdr headers)
12180         (setcdr headers (cddr headers))))))
12181
12182 ;;;
12183 ;;; summary highlights
12184 ;;;
12185
12186 (defun gnus-highlight-selected-summary ()
12187   "Highlight selected article in summary buffer."
12188   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12189   (when gnus-summary-selected-face
12190     (save-excursion
12191       (let* ((beg (point-at-bol))
12192              (end (point-at-eol))
12193              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12194              (from (if (get-text-property beg gnus-mouse-face-prop)
12195                        beg
12196                      (or (next-single-property-change
12197                           beg gnus-mouse-face-prop nil end)
12198                          beg)))
12199              (to
12200               (if (= from end)
12201                   (- from 2)
12202                 (or (next-single-property-change
12203                      from gnus-mouse-face-prop nil end)
12204                     end))))
12205         ;; If no mouse-face prop on line we will have to = from = end,
12206         ;; so we highlight the entire line instead.
12207         (when (= (+ to 2) from)
12208           (setq from beg)
12209           (setq to end))
12210         (if gnus-newsgroup-selected-overlay
12211             ;; Move old overlay.
12212             (gnus-move-overlay
12213              gnus-newsgroup-selected-overlay from to (current-buffer))
12214           ;; Create new overlay.
12215           (gnus-overlay-put
12216            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12217            'face gnus-summary-selected-face))))))
12218
12219 (defvar gnus-summary-highlight-line-cached nil)
12220 (defvar gnus-summary-highlight-line-trigger nil)
12221
12222 (defun gnus-summary-highlight-line-0 ()
12223   (if (and (eq gnus-summary-highlight-line-trigger
12224                gnus-summary-highlight)
12225            gnus-summary-highlight-line-cached)
12226       gnus-summary-highlight-line-cached
12227     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12228           gnus-summary-highlight-line-cached
12229           (let* ((cond (list 'cond))
12230                  (c cond)
12231                  (list gnus-summary-highlight))
12232             (while list
12233               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12234                               nil))
12235               (setq c (cdr c)
12236                     list (cdr list)))
12237             (gnus-byte-compile (list 'lambda nil cond))))))
12238
12239 (defun gnus-summary-highlight-line ()
12240   "Highlight current line according to `gnus-summary-highlight'."
12241   (let* ((beg (point-at-bol))
12242          (article (or (gnus-summary-article-number) gnus-current-article))
12243          (score (or (cdr (assq article
12244                                gnus-newsgroup-scored))
12245                     gnus-summary-default-score 0))
12246          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12247          (inhibit-read-only t)
12248          (default gnus-summary-default-score)
12249          (default-high gnus-summary-default-high-score)
12250          (default-low gnus-summary-default-low-score)
12251          (uncached (and gnus-summary-use-undownloaded-faces
12252                         (memq article gnus-newsgroup-undownloaded)
12253                         (not (memq article gnus-newsgroup-cached)))))
12254     (let ((face (funcall (gnus-summary-highlight-line-0))))
12255       (unless (eq face (get-text-property beg 'face))
12256         (gnus-put-text-property-excluding-characters-with-faces
12257          beg (point-at-eol) 'face
12258          (setq face (if (boundp face) (symbol-value face) face)))
12259         (when gnus-summary-highlight-line-function
12260           (funcall gnus-summary-highlight-line-function article face))))))
12261
12262 (defun gnus-update-read-articles (group unread &optional compute)
12263   "Update the list of read articles in GROUP.
12264 UNREAD is a sorted list."
12265   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12266         (info (gnus-get-info group))
12267         (prev 1)
12268         read)
12269     (if (or (not info) (not active))
12270         ;; There is no info on this group if it was, in fact,
12271         ;; killed.  Gnus stores no information on killed groups, so
12272         ;; there's nothing to be done.
12273         ;; One could store the information somewhere temporarily,
12274         ;; perhaps...  Hmmm...
12275         ()
12276       ;; Remove any negative articles numbers.
12277       (while (and unread (< (car unread) 0))
12278         (setq unread (cdr unread)))
12279       ;; Remove any expired article numbers
12280       (while (and unread (< (car unread) (car active)))
12281         (setq unread (cdr unread)))
12282       ;; Compute the ranges of read articles by looking at the list of
12283       ;; unread articles.
12284       (while unread
12285         (when (/= (car unread) prev)
12286           (push (if (= prev (1- (car unread))) prev
12287                   (cons prev (1- (car unread))))
12288                 read))
12289         (setq prev (1+ (car unread)))
12290         (setq unread (cdr unread)))
12291       (when (<= prev (cdr active))
12292         (push (cons prev (cdr active)) read))
12293       (setq read (if (> (length read) 1) (nreverse read) read))
12294       (if compute
12295           read
12296         (save-excursion
12297           (let (setmarkundo)
12298             ;; Propagate the read marks to the backend.
12299             (when (and gnus-propagate-marks
12300                        (gnus-check-backend-function 'request-set-mark group))
12301               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12302                     (add (gnus-remove-from-range read (gnus-info-read info))))
12303                 (when (or add del)
12304                   (unless (gnus-check-group group)
12305                     (error "Can't open server for %s" group))
12306                   (gnus-request-set-mark
12307                    group (delq nil (list (if add (list add 'add '(read)))
12308                                          (if del (list del 'del '(read))))))
12309                   (setq setmarkundo
12310                         `(gnus-request-set-mark
12311                           ,group
12312                           ',(delq nil (list
12313                                        (if del (list del 'add '(read)))
12314                                        (if add (list add 'del '(read))))))))))
12315             (set-buffer gnus-group-buffer)
12316             (gnus-undo-register
12317               `(progn
12318                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12319                  (gnus-info-set-read ',info ',(gnus-info-read info))
12320                  (gnus-get-unread-articles-in-group ',info
12321                                                     (gnus-active ,group))
12322                  (gnus-group-update-group ,group t)
12323                  ,setmarkundo))))
12324         ;; Enter this list into the group info.
12325         (gnus-info-set-read info read)
12326         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12327         (gnus-get-unread-articles-in-group info (gnus-active group))
12328         t))))
12329
12330 (defun gnus-offer-save-summaries ()
12331   "Offer to save all active summary buffers."
12332   (let (buffers)
12333     ;; Go through all buffers and find all summaries.
12334     (dolist (buffer (buffer-list))
12335       (when (and (setq buffer (buffer-name buffer))
12336                  (string-match "Summary" buffer)
12337                  (with-current-buffer buffer
12338                    ;; We check that this is, indeed, a summary buffer.
12339                    (and (eq major-mode 'gnus-summary-mode)
12340                         ;; Also make sure this isn't bogus.
12341                         gnus-newsgroup-prepared
12342                         ;; Also make sure that this isn't a
12343                         ;; dead summary buffer.
12344                         (not gnus-dead-summary-mode))))
12345         (push buffer buffers)))
12346     ;; Go through all these summary buffers and offer to save them.
12347     (when buffers
12348       (save-excursion
12349         (map-y-or-n-p
12350          "Update summary buffer %s? "
12351          (lambda (buf)
12352            (switch-to-buffer buf)
12353            (gnus-summary-exit))
12354          buffers)))))
12355
12356 (defun gnus-summary-setup-default-charset ()
12357   "Setup newsgroup default charset."
12358   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12359       (setq gnus-newsgroup-charset nil)
12360     (let* ((ignored-charsets
12361             (or gnus-newsgroup-ephemeral-ignored-charsets
12362                 (append
12363                  (and gnus-newsgroup-name
12364                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12365                  gnus-newsgroup-ignored-charsets))))
12366       (setq gnus-newsgroup-charset
12367             (or gnus-newsgroup-ephemeral-charset
12368                 (and gnus-newsgroup-name
12369                      (gnus-parameter-charset gnus-newsgroup-name))
12370                 gnus-default-charset))
12371       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12372            ignored-charsets))))
12373
12374 ;;;
12375 ;;; Mime Commands
12376 ;;;
12377
12378 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12379   "Display the current article buffer fully MIME-buttonized.
12380 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12381 treated as multipart/mixed."
12382   (interactive "P")
12383   (require 'gnus-art)
12384   (let ((gnus-unbuttonized-mime-types nil)
12385         (gnus-mime-display-multipart-as-mixed show-all-parts))
12386     (gnus-summary-show-article)))
12387
12388 (defun gnus-summary-repair-multipart (article)
12389   "Add a Content-Type header to a multipart article without one."
12390   (interactive (list (gnus-summary-article-number)))
12391   (gnus-with-article article
12392     (message-narrow-to-head)
12393     (message-remove-header "Mime-Version")
12394     (goto-char (point-max))
12395     (insert "Mime-Version: 1.0\n")
12396     (widen)
12397     (when (search-forward "\n--" nil t)
12398       (let ((separator (buffer-substring (point) (point-at-eol))))
12399         (message-narrow-to-head)
12400         (message-remove-header "Content-Type")
12401         (goto-char (point-max))
12402         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12403                         separator))
12404         (widen))))
12405   (let (gnus-mark-article-hook)
12406     (gnus-summary-select-article t t nil article)))
12407
12408 (defun gnus-summary-toggle-display-buttonized ()
12409   "Toggle the buttonizing of the article buffer."
12410   (interactive)
12411   (require 'gnus-art)
12412   (if (setq gnus-inhibit-mime-unbuttonizing
12413             (not gnus-inhibit-mime-unbuttonizing))
12414       (let ((gnus-unbuttonized-mime-types nil))
12415         (gnus-summary-show-article))
12416     (gnus-summary-show-article)))
12417
12418 ;;;
12419 ;;; Generic summary marking commands
12420 ;;;
12421
12422 (defvar gnus-summary-marking-alist
12423   '((read gnus-del-mark "d")
12424     (unread gnus-unread-mark "u")
12425     (ticked gnus-ticked-mark "!")
12426     (dormant gnus-dormant-mark "?")
12427     (expirable gnus-expirable-mark "e"))
12428   "An alist of names/marks/keystrokes.")
12429
12430 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12431 (defvar gnus-summary-mark-map)
12432
12433 (defun gnus-summary-make-all-marking-commands ()
12434   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12435   (dolist (elem gnus-summary-marking-alist)
12436     (apply 'gnus-summary-make-marking-command elem)))
12437
12438 (defun gnus-summary-make-marking-command (name mark keystroke)
12439   (let ((map (make-sparse-keymap)))
12440     (define-key gnus-summary-generic-mark-map keystroke map)
12441     (dolist (lway `((next "next" next nil "n")
12442                     (next-unread "next unread" next t "N")
12443                     (prev "previous" prev nil "p")
12444                     (prev-unread "previous unread" prev t "P")
12445                     (nomove "" nil nil ,keystroke)))
12446       (let ((func (gnus-summary-make-marking-command-1
12447                    mark (car lway) lway name)))
12448         (setq func (eval func))
12449         (define-key map (nth 4 lway) func)))))
12450
12451 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12452   `(defun ,(intern
12453             (format "gnus-summary-put-mark-as-%s%s"
12454                     name (if (eq way 'nomove)
12455                              ""
12456                            (concat "-" (symbol-name way)))))
12457      (n)
12458      ,(format
12459        "Mark the current article as %s%s.
12460 If N, the prefix, then repeat N times.
12461 If N is negative, move in reverse order.
12462 The difference between N and the actual number of articles marked is
12463 returned."
12464        name (cadr lway))
12465      (interactive "p")
12466      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12467
12468 (defun gnus-summary-generic-mark (n mark move unread)
12469   "Mark N articles with MARK."
12470   (unless (eq major-mode 'gnus-summary-mode)
12471     (error "This command can only be used in the summary buffer"))
12472   (gnus-summary-show-thread)
12473   (let ((nummove
12474          (cond
12475           ((eq move 'next) 1)
12476           ((eq move 'prev) -1)
12477           (t 0))))
12478     (if (zerop nummove)
12479         (setq n 1)
12480       (when (< n 0)
12481         (setq n (abs n)
12482               nummove (* -1 nummove))))
12483     (while (and (> n 0)
12484                 (gnus-summary-mark-article nil mark)
12485                 (zerop (gnus-summary-next-subject nummove unread t)))
12486       (setq n (1- n)))
12487     (when (/= 0 n)
12488       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12489     (gnus-summary-recenter)
12490     (gnus-summary-position-point)
12491     (gnus-set-mode-line 'summary)
12492     n))
12493
12494 (defun gnus-summary-insert-articles (articles)
12495   (when (setq articles
12496               (gnus-sorted-difference articles
12497                                       (mapcar (lambda (h)
12498                                                 (mail-header-number h))
12499                                               gnus-newsgroup-headers)))
12500     (setq gnus-newsgroup-headers
12501           (gnus-merge 'list
12502                       gnus-newsgroup-headers
12503                       (gnus-fetch-headers articles)
12504                       'gnus-article-sort-by-number))
12505     ;; Suppress duplicates?
12506     (when gnus-suppress-duplicates
12507       (gnus-dup-suppress-articles))
12508
12509     (if (and gnus-fetch-old-headers
12510              (eq gnus-headers-retrieved-by 'nov))
12511         ;; We might want to build some more threads first.
12512         (if (eq gnus-fetch-old-headers 'invisible)
12513             (gnus-build-all-threads)
12514           (gnus-build-old-threads))
12515       ;; Mark the inserted articles that are unread as unread.
12516       (setq gnus-newsgroup-unreads
12517             (gnus-sorted-nunion
12518              gnus-newsgroup-unreads
12519              (gnus-sorted-nintersection
12520               (gnus-list-of-unread-articles gnus-newsgroup-name)
12521               articles)))
12522       ;; Mark the inserted articles as selected so that the information
12523       ;; of the marks having been changed by a user may be updated when
12524       ;; exiting this group.  See `gnus-summary-update-info'.
12525       (dolist (art articles)
12526         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12527     ;; Let the Gnus agent mark articles as read.
12528     (when gnus-agent
12529       (gnus-agent-get-undownloaded-list))
12530     ;; Remove list identifiers from subject
12531     (when gnus-list-identifiers
12532       (gnus-summary-remove-list-identifiers))
12533     ;; First and last article in this newsgroup.
12534     (when gnus-newsgroup-headers
12535       (setq gnus-newsgroup-begin
12536             (mail-header-number (car gnus-newsgroup-headers))
12537             gnus-newsgroup-end
12538             (mail-header-number
12539              (gnus-last-element gnus-newsgroup-headers))))
12540     (when gnus-use-scoring
12541       (gnus-possibly-score-headers))))
12542
12543 (defun gnus-summary-insert-old-articles (&optional all)
12544   "Insert all old articles in this group.
12545 If ALL is non-nil, already read articles become readable.
12546 If ALL is a number, fetch this number of articles."
12547   (interactive "P")
12548   (prog1
12549       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12550             older len)
12551         (setq older
12552               ;; Some nntp servers lie about their active range.  When
12553               ;; this happens, the active range can be in the millions.
12554               ;; Use a compressed range to avoid creating a huge list.
12555               (gnus-range-difference (list gnus-newsgroup-active) old))
12556         (setq len (gnus-range-length older))
12557         (cond
12558          ((null older) nil)
12559          ((numberp all)
12560           (if (< all len)
12561               (let ((older-range (nreverse older)))
12562                 (setq older nil)
12563
12564                 (while (> all 0)
12565                   (let* ((r (pop older-range))
12566                          (min (if (numberp r) r (car r)))
12567                          (max (if (numberp r) r (cdr r))))
12568                     (while (and (<= min max)
12569                                 (> all 0))
12570                       (push max older)
12571                       (setq all (1- all)
12572                             max (1- max))))))
12573             (setq older (gnus-uncompress-range older))))
12574          (all
12575           (setq older (gnus-uncompress-range older)))
12576          (t
12577           (when (and (numberp gnus-large-newsgroup)
12578                    (> len gnus-large-newsgroup))
12579               (let* ((cursor-in-echo-area nil)
12580                      (initial (gnus-parameter-large-newsgroup-initial
12581                                gnus-newsgroup-name))
12582                      (input
12583                       (read-string
12584                        (format
12585                         "How many articles from %s (%s %d): "
12586                         (gnus-group-decoded-name gnus-newsgroup-name)
12587                         (if initial "max" "default")
12588                         len)
12589                        (if initial
12590                            (cons (number-to-string initial)
12591                                  0)))))
12592                 (unless (string-match "^[ \t]*$" input)
12593                   (setq all (string-to-number input))
12594                   (if (< all len)
12595                       (let ((older-range (nreverse older)))
12596                         (setq older nil)
12597
12598                         (while (> all 0)
12599                           (let* ((r (pop older-range))
12600                                  (min (if (numberp r) r (car r)))
12601                                  (max (if (numberp r) r (cdr r))))
12602                             (while (and (<= min max)
12603                                         (> all 0))
12604                               (push max older)
12605                               (setq all (1- all)
12606                                     max (1- max))))))))))
12607           (setq older (gnus-uncompress-range older))))
12608         (if (not older)
12609             (message "No old news.")
12610           (gnus-summary-insert-articles older)
12611           (gnus-summary-limit (gnus-sorted-nunion old older))))
12612     (gnus-summary-position-point)))
12613
12614 (defun gnus-summary-insert-new-articles ()
12615   "Insert all new articles in this group."
12616   (interactive)
12617   (prog1
12618       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12619             (old-high gnus-newsgroup-highest)
12620             (nnmail-fetched-sources (list t))
12621             i new)
12622         (setq gnus-newsgroup-active
12623               (gnus-copy-sequence
12624                (gnus-activate-group gnus-newsgroup-name 'scan)))
12625         (setq i (cdr gnus-newsgroup-active)
12626               gnus-newsgroup-highest i)
12627         (while (> i old-high)
12628           (push i new)
12629           (decf i))
12630         (if (not new)
12631             (message "No gnus is bad news")
12632           (gnus-summary-insert-articles new)
12633           (setq gnus-newsgroup-unreads
12634                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12635           (gnus-summary-limit (gnus-sorted-nunion old new))))
12636     (gnus-summary-position-point)))
12637
12638 ;;; Bookmark support for Gnus.
12639 (declare-function bookmark-make-record-default
12640                   "bookmark" (&optional no-file no-context posn))
12641 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12642 (declare-function bookmark-default-handler "bookmark" (bmk))
12643 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12644 (defvar bookmark-yank-point)
12645 (defvar bookmark-current-buffer)
12646
12647 (defun gnus-summary-bookmark-make-record ()
12648   "Make a bookmark entry for a Gnus summary buffer."
12649   (let (pos buf)
12650     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12651       (save-restriction              ; FIXME is it necessary to widen?
12652         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12653       (setq buf "art") ; We are recording bookmark from article buffer.
12654       (setq bookmark-yank-point (point))
12655       (setq bookmark-current-buffer (current-buffer))
12656       (gnus-article-show-summary))      ; Go back in summary buffer.
12657     ;; We are now recording bookmark from summary buffer.
12658     (unless buf (setq buf "sum"))
12659     (let* ((subject (elt (gnus-summary-article-header) 1))
12660            (grp     (car gnus-article-current))
12661            (art     (cdr gnus-article-current))
12662            (head    (gnus-summary-article-header art))
12663            (id      (mail-header-id head)))
12664       `(,subject
12665         ,@(condition-case nil
12666               (bookmark-make-record-default 'no-file 'no-context pos)
12667             (wrong-number-of-arguments
12668              (bookmark-make-record-default 'point-only)))
12669         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12670         (group . ,grp) (article . ,art)
12671         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12672
12673 ;;;###autoload
12674 (defun gnus-summary-bookmark-jump (bookmark)
12675   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12676 BOOKMARK is a bookmark name or a bookmark record."
12677   (let ((group    (bookmark-prop-get bookmark 'group))
12678         (article  (bookmark-prop-get bookmark 'article))
12679         (id       (bookmark-prop-get bookmark 'message-id))
12680         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12681     (gnus-fetch-group group (list article))
12682     (gnus-summary-insert-cached-articles)
12683     (gnus-summary-goto-article id nil 'force)
12684     ;; FIXME we have to wait article buffer is ready (only large buffer)
12685     ;; Is there a better solution to know that?
12686     ;; If we don't wait `bookmark-default-handler' will have no chance
12687     ;; to set position. However there is no error, just wrong pos.
12688     (sit-for 1)
12689     (when (string= buf "Gnus-art")
12690       (other-window 1))
12691     (bookmark-default-handler
12692      `(""
12693        (buffer . ,(current-buffer))
12694        . ,(bookmark-get-bookmark-record bookmark)))))
12695
12696 (gnus-summary-make-all-marking-commands)
12697
12698 (gnus-ems-redefine)
12699
12700 (provide 'gnus-sum)
12701
12702 (run-hooks 'gnus-sum-load-hook)
12703
12704 ;; Local Variables:
12705 ;; coding: iso-8859-1
12706 ;; End:
12707
12708 ;;; gnus-sum.el ends here