c50d8aabf7b1615ecd402c5a4c6b5aefe256fce1
[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 and XEmacs.
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-widen-article-window nil
478   "If non-nil, selecting the article buffer will display only the article buffer."
479   :version "24.1"
480   :group 'gnus-article-various
481   :type 'boolean)
482
483 (defcustom gnus-break-pages t
484   "*If non-nil, do page breaking on articles.
485 The page delimiter is specified by the `gnus-page-delimiter'
486 variable."
487   :group 'gnus-article-various
488   :type 'boolean)
489
490 (defcustom gnus-move-split-methods nil
491   "*Variable used to suggest where articles are to be moved to.
492 It uses the same syntax as the `gnus-split-methods' variable.
493 However, whereas `gnus-split-methods' specifies file names as targets,
494 this variable specifies group names."
495   :group 'gnus-summary-mail
496   :type '(repeat (choice (list :value (fun) function)
497                          (cons :value ("" "") regexp (repeat string))
498                          (sexp :value nil))))
499
500 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
501   "Function used to compute default prefix for article move/copy/etc prompts.
502 The function should take one argument, a group name, and return a
503 string with the suggested prefix."
504   :group 'gnus-summary-mail
505   :type 'function)
506
507 ;; FIXME: Although the custom type is `character' for the following variables,
508 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
509
510 (defcustom gnus-unread-mark ?           ;Whitespace
511   "*Mark used for unread articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-ticked-mark ?!
516   "*Mark used for ticked articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-dormant-mark ??
521   "*Mark used for dormant articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-del-mark ?r
526   "*Mark used for del'd articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-read-mark ?R
531   "*Mark used for read articles."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-expirable-mark ?E
536   "*Mark used for expirable articles."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-killed-mark ?K
541   "*Mark used for killed articles."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-spam-mark ?$
546   "*Mark used for spam articles."
547   :version "22.1"
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-kill-file-mark ?X
552   "*Mark used for articles killed by kill files."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-low-score-mark ?Y
557   "*Mark used for articles with a low score."
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-catchup-mark ?C
562   "*Mark used for articles that are caught up."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-replied-mark ?A
567   "*Mark used for articles that have been replied to."
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-forwarded-mark ?F
572   "*Mark used for articles that have been forwarded."
573   :version "22.1"
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-recent-mark ?N
578   "*Mark used for articles that are recent."
579   :version "22.1"
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-cached-mark ?*
584   "*Mark used for articles that are in the cache."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-saved-mark ?S
589   "*Mark used for articles that have been saved."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-unseen-mark ?.
594   "*Mark used for articles that haven't been seen."
595   :version "22.1"
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-no-mark ?               ;Whitespace
600   "*Mark used for articles that have no other secondary mark."
601   :version "22.1"
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-ancient-mark ?O
606   "*Mark used for ancient articles."
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-sparse-mark ?Q
611   "*Mark used for sparsely reffed articles."
612   :group 'gnus-summary-marks
613   :type 'character)
614
615 (defcustom gnus-canceled-mark ?G
616   "*Mark used for canceled articles."
617   :group 'gnus-summary-marks
618   :type 'character)
619
620 (defcustom gnus-duplicate-mark ?M
621   "*Mark used for duplicate articles."
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-undownloaded-mark ?-
626   "*Mark used for articles that weren't downloaded."
627   :version "22.1"
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-downloaded-mark ?+
632   "*Mark used for articles that were downloaded."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-downloadable-mark ?%
637   "*Mark used for articles that are to be downloaded."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-unsendable-mark ?=
642   "*Mark used for articles that won't be sent."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-score-over-mark ?+
647   "*Score mark used for articles with high scores."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-score-below-mark ?-
652   "*Score mark used for articles with low scores."
653   :group 'gnus-summary-marks
654   :type 'character)
655
656 (defcustom gnus-empty-thread-mark ?     ;Whitespace
657   "*There is no thread under the article."
658   :group 'gnus-summary-marks
659   :type 'character)
660
661 (defcustom gnus-not-empty-thread-mark ?=
662   "*There is a thread under the article."
663   :group 'gnus-summary-marks
664   :type 'character)
665
666 (defcustom gnus-view-pseudo-asynchronously nil
667   "*If non-nil, Gnus will view pseudo-articles asynchronously."
668   :group 'gnus-extract-view
669   :type 'boolean)
670
671 (defcustom gnus-auto-expirable-marks
672   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
673         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
674         gnus-duplicate-mark)
675   "*The list of marks converted into expiration if a group is auto-expirable."
676   :version "24.1"
677   :group 'gnus-summary
678   :type '(repeat character))
679
680 (defcustom gnus-inhibit-user-auto-expire t
681   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
682   :version "21.1"
683   :group 'gnus-summary
684   :type 'boolean)
685
686 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
687   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
688 If nil, the expirable marks will be unchanged except that the marks
689 will be removed when copying or moving articles to a group that has
690 not turned auto-expire on.  If non-nil, articles that have been read
691 will be marked as expirable when being copied or moved to a group in
692 which auto-expire is turned on."
693   :version "23.2"
694   :type 'boolean
695   :group 'gnus-summary-marks)
696
697 (defcustom gnus-view-pseudos nil
698   "*If `automatic', pseudo-articles will be viewed automatically.
699 If `not-confirm', pseudos will be viewed automatically, and the user
700 will not be asked to confirm the command."
701   :group 'gnus-extract-view
702   :type '(choice (const :tag "off" nil)
703                  (const automatic)
704                  (const not-confirm)))
705
706 (defcustom gnus-view-pseudos-separately t
707   "*If non-nil, one pseudo-article will be created for each file to be viewed.
708 If nil, all files that use the same viewing command will be given as a
709 list of parameters to that command."
710   :group 'gnus-extract-view
711   :type 'boolean)
712
713 (defcustom gnus-insert-pseudo-articles t
714   "*If non-nil, insert pseudo-articles when decoding articles."
715   :group 'gnus-extract-view
716   :type 'boolean)
717
718 (defcustom gnus-summary-dummy-line-format
719   "   %(:                             :%) %S\n"
720   "*The format specification for the dummy roots in the summary buffer.
721 It works along the same lines as a normal formatting string,
722 with some simple extensions.
723
724 %S  The subject
725
726 General format specifiers can also be used.
727 See `(gnus)Formatting Variables'."
728   :link '(custom-manual "(gnus)Formatting Variables")
729   :group 'gnus-threading
730   :type 'string)
731
732 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
733   "*The format specification for the summary mode line.
734 It works along the same lines as a normal formatting string,
735 with some simple extensions:
736
737 %G  Group name
738 %p  Unprefixed group name
739 %A  Current article number
740 %z  Current article score
741 %V  Gnus version
742 %U  Number of unread articles in the group
743 %e  Number of unselected articles in the group
744 %Z  A string with unread/unselected article counts
745 %g  Shortish group name
746 %S  Subject of the current article
747 %u  User-defined spec
748 %s  Current score file name
749 %d  Number of dormant articles
750 %r  Number of articles that have been marked as read in this session
751 %E  Number of articles expunged by the score files"
752   :group 'gnus-summary-format
753   :type 'string)
754
755 (defcustom gnus-list-identifiers nil
756   "Regexp that matches list identifiers to be removed from subject.
757 This can also be a list of regexps."
758   :version "21.1"
759   :group 'gnus-summary-format
760   :group 'gnus-article-hiding
761   :type '(choice (const :tag "none" nil)
762                  (regexp :value ".*")
763                  (repeat :value (".*") regexp)))
764
765 (defcustom gnus-summary-mark-below 0
766   "*Mark all articles with a score below this variable as read.
767 This variable is local to each summary buffer and usually set by the
768 score file."
769   :group 'gnus-score-default
770   :type 'integer)
771
772 (defun gnus-widget-reversible-match (widget value)
773   "Ignoring WIDGET, convert VALUE to internal form.
774 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
775   ;; (debug value)
776   (or (symbolp value)
777       (and (listp value)
778            (eq (length value) 2)
779            (eq (nth 0 value) 'not)
780            (symbolp (nth 1 value)))))
781
782 (defun gnus-widget-reversible-to-internal (widget value)
783   "Ignoring WIDGET, convert VALUE to internal form.
784 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
785 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
786   ;; (debug value)
787   (if (atom value)
788       (list value nil)
789     (list (nth 1 value) t)))
790
791 (defun gnus-widget-reversible-to-external (widget value)
792   "Ignoring WIDGET, convert VALUE to external form.
793 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
794 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
795   ;; (debug value)
796   (if (nth 1 value)
797       (list 'not (nth 0 value))
798     (nth 0 value)))
799
800 (define-widget 'gnus-widget-reversible 'group
801   "A `group' that convert values."
802   :match 'gnus-widget-reversible-match
803   :value-to-internal 'gnus-widget-reversible-to-internal
804   :value-to-external 'gnus-widget-reversible-to-external)
805
806 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
807   "*List of functions used for sorting articles in the summary buffer.
808
809 Each function takes two articles and returns non-nil if the first
810 article should be sorted before the other.  If you use more than one
811 function, the primary sort function should be the last.  You should
812 probably always include `gnus-article-sort-by-number' in the list of
813 sorting functions -- preferably first.  Also note that sorting by date
814 is often much slower than sorting by number, and the sorting order is
815 very similar.  (Sorting by date means sorting by the time the message
816 was sent, sorting by number means sorting by arrival time.)
817
818 Each item can also be a list `(not F)' where F is a function;
819 this reverses the sort order.
820
821 Ready-made functions include `gnus-article-sort-by-number',
822 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
823 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
824 and `gnus-article-sort-by-score'.
825
826 When threading is turned on, the variable `gnus-thread-sort-functions'
827 controls how articles are sorted."
828   :group 'gnus-summary-sort
829   :type '(repeat (gnus-widget-reversible
830                   (choice (function-item gnus-article-sort-by-number)
831                           (function-item gnus-article-sort-by-author)
832                           (function-item gnus-article-sort-by-subject)
833                           (function-item gnus-article-sort-by-date)
834                           (function-item gnus-article-sort-by-score)
835                           (function-item gnus-article-sort-by-random)
836                           (function :tag "other"))
837                   (boolean :tag "Reverse order"))))
838
839
840 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
841   "*List of functions used for sorting threads in the summary buffer.
842 By default, threads are sorted by article number.
843
844 Each function takes two threads and returns non-nil if the first
845 thread should be sorted before the other.  If you use more than one
846 function, the primary sort function should be the last.  You should
847 probably always include `gnus-thread-sort-by-number' in the list of
848 sorting functions -- preferably first.  Also note that sorting by date
849 is often much slower than sorting by number, and the sorting order is
850 very similar.  (Sorting by date means sorting by the time the message
851 was sent, sorting by number means sorting by arrival time.)
852
853 Each list item can also be a list `(not F)' where F is a
854 function; this specifies reversed sort order.
855
856 Ready-made functions include `gnus-thread-sort-by-number',
857 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
858 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
859 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
860 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
861 and `gnus-thread-sort-by-total-score' (see
862 `gnus-thread-score-function').
863
864 When threading is turned off, the variable
865 `gnus-article-sort-functions' controls how articles are sorted."
866   :group 'gnus-summary-sort
867   :type '(repeat
868           (gnus-widget-reversible
869            (choice (function-item gnus-thread-sort-by-number)
870                    (function-item gnus-thread-sort-by-author)
871                    (function-item gnus-thread-sort-by-recipient)
872                    (function-item gnus-thread-sort-by-subject)
873                    (function-item gnus-thread-sort-by-date)
874                    (function-item gnus-thread-sort-by-score)
875                    (function-item gnus-thread-sort-by-most-recent-number)
876                    (function-item gnus-thread-sort-by-most-recent-date)
877                    (function-item gnus-thread-sort-by-random)
878                    (function-item gnus-thread-sort-by-total-score)
879                    (function :tag "other"))
880            (boolean :tag "Reverse order"))))
881
882 (defcustom gnus-thread-score-function '+
883   "*Function used for calculating the total score of a thread.
884
885 The function is called with the scores of the article and each
886 subthread and should then return the score of the thread.
887
888 Some functions you can use are `+', `max', or `min'."
889   :group 'gnus-summary-sort
890   :type 'function)
891
892 (defcustom gnus-summary-expunge-below nil
893   "All articles that have a score less than this variable will be expunged.
894 This variable is local to the summary buffers."
895   :group 'gnus-score-default
896   :type '(choice (const :tag "off" nil)
897                  integer))
898
899 (defcustom gnus-thread-expunge-below nil
900   "All threads that have a total score less than this variable will be expunged.
901 See `gnus-thread-score-function' for en explanation of what a
902 \"thread score\" is.
903
904 This variable is local to the summary buffers."
905   :group 'gnus-threading
906   :group 'gnus-score-default
907   :type '(choice (const :tag "off" nil)
908                  integer))
909
910 (defcustom gnus-summary-mode-hook nil
911   "*A hook for Gnus summary mode.
912 This hook is run before any variables are set in the summary buffer."
913   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
914   :group 'gnus-summary-various
915   :type 'hook)
916
917 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
918 (when (featurep 'xemacs)
919   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
920   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
921   (add-hook 'gnus-summary-mode-hook
922             'gnus-xmas-switch-horizontal-scrollbar-off))
923
924 (defcustom gnus-summary-menu-hook nil
925   "*Hook run after the creation of the summary mode menu."
926   :group 'gnus-summary-visual
927   :type 'hook)
928
929 (defcustom gnus-summary-exit-hook nil
930   "*A hook called on exit from the summary buffer.
931 It will be called with point in the group buffer."
932   :group 'gnus-summary-exit
933   :type 'hook)
934
935 (defcustom gnus-summary-prepare-hook nil
936   "*A hook called after the summary buffer has been generated.
937 If you want to modify the summary buffer, you can use this hook."
938   :group 'gnus-summary-various
939   :type 'hook)
940
941 (defcustom gnus-summary-prepared-hook nil
942   "*A hook called as the last thing after the summary buffer has been generated."
943   :group 'gnus-summary-various
944   :type 'hook)
945
946 (defcustom gnus-summary-generate-hook nil
947   "*A hook run just before generating the summary buffer.
948 This hook is commonly used to customize threading variables and the
949 like."
950   :group 'gnus-summary-various
951   :type 'hook)
952
953 (defcustom gnus-select-group-hook nil
954   "*A hook called when a newsgroup is selected.
955
956 If you'd like to simplify subjects like the
957 `gnus-summary-next-same-subject' command does, you can use the
958 following hook:
959
960  (add-hook gnus-select-group-hook
961            (lambda ()
962              (mapcar (lambda (header)
963                        (mail-header-set-subject
964                         header
965                         (gnus-simplify-subject
966                          (mail-header-subject header) 're-only)))
967                      gnus-newsgroup-headers)))"
968   :group 'gnus-group-select
969   :type 'hook)
970
971 (defcustom gnus-select-article-hook nil
972   "*A hook called when an article is selected."
973   :group 'gnus-summary-choose
974   :options '(gnus-agent-fetch-selected-article)
975   :type 'hook)
976
977 (defcustom gnus-visual-mark-article-hook
978   (list 'gnus-highlight-selected-summary)
979   "*Hook run after selecting an article in the summary buffer.
980 It is meant to be used for highlighting the article in some way.  It
981 is not run if `gnus-visual' is nil."
982   :group 'gnus-summary-visual
983   :type 'hook)
984
985 (defcustom gnus-parse-headers-hook nil
986   "*A hook called before parsing the headers."
987   :group 'gnus-various
988   :type 'hook)
989
990 (defcustom gnus-exit-group-hook nil
991   "*A hook called when exiting summary mode.
992 This hook is not called from the non-updating exit commands like `Q'."
993   :group 'gnus-various
994   :type 'hook)
995
996 (defcustom gnus-summary-update-hook nil
997   "*A hook called when a summary line is changed.
998 The hook will not be called if `gnus-visual' is nil.
999
1000 The default function `gnus-summary-highlight-line' will
1001 highlight the line according to the `gnus-summary-highlight'
1002 variable."
1003   :group 'gnus-summary-visual
1004   :type 'hook)
1005
1006 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1007   "*A hook called when an article is selected for the first time.
1008 The hook is intended to mark an article as read (or unread)
1009 automatically when it is selected."
1010   :group 'gnus-summary-choose
1011   :type 'hook)
1012
1013 (defcustom gnus-group-no-more-groups-hook nil
1014   "*A hook run when returning to group mode having no more (unread) groups."
1015   :group 'gnus-group-select
1016   :type 'hook)
1017
1018 (defcustom gnus-ps-print-hook nil
1019   "*A hook run before ps-printing something from Gnus."
1020   :group 'gnus-summary
1021   :type 'hook)
1022
1023 (defcustom gnus-summary-article-move-hook nil
1024   "*A hook called after an article is moved, copied, respooled, or crossposted."
1025   :version "22.1"
1026   :group 'gnus-summary
1027   :type 'hook)
1028
1029 (defcustom gnus-summary-article-delete-hook nil
1030   "*A hook called after an article is deleted."
1031   :version "22.1"
1032   :group 'gnus-summary
1033   :type 'hook)
1034
1035 (defcustom gnus-summary-article-expire-hook nil
1036   "*A hook called after an article is expired."
1037   :version "22.1"
1038   :group 'gnus-summary
1039   :type 'hook)
1040
1041 (defcustom gnus-summary-display-arrow
1042   (and (fboundp 'display-graphic-p)
1043        (display-graphic-p))
1044   "*If non-nil, display an arrow highlighting the current article."
1045   :version "22.1"
1046   :group 'gnus-summary
1047   :type 'boolean)
1048
1049 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1050   "Face used for highlighting the current article in the summary buffer."
1051   :group 'gnus-summary-visual
1052   :type 'face)
1053
1054 (defvar gnus-tmp-downloaded nil)
1055
1056 (defcustom gnus-summary-highlight
1057   '(((eq mark gnus-canceled-mark)
1058      . gnus-summary-cancelled)
1059     ((and uncached (> score default-high))
1060      . gnus-summary-high-undownloaded)
1061     ((and uncached (< score default-low))
1062      . gnus-summary-low-undownloaded)
1063     (uncached
1064      . gnus-summary-normal-undownloaded)
1065     ((and (> score default-high)
1066           (or (eq mark gnus-dormant-mark)
1067               (eq mark gnus-ticked-mark)))
1068      . gnus-summary-high-ticked)
1069     ((and (< score default-low)
1070           (or (eq mark gnus-dormant-mark)
1071               (eq mark gnus-ticked-mark)))
1072      . gnus-summary-low-ticked)
1073     ((or (eq mark gnus-dormant-mark)
1074          (eq mark gnus-ticked-mark))
1075      . gnus-summary-normal-ticked)
1076     ((and (> score default-high) (eq mark gnus-ancient-mark))
1077      . gnus-summary-high-ancient)
1078     ((and (< score default-low) (eq mark gnus-ancient-mark))
1079      . gnus-summary-low-ancient)
1080     ((eq mark gnus-ancient-mark)
1081      . gnus-summary-normal-ancient)
1082     ((and (> score default-high) (eq mark gnus-unread-mark))
1083      . gnus-summary-high-unread)
1084     ((and (< score default-low) (eq mark gnus-unread-mark))
1085      . gnus-summary-low-unread)
1086     ((eq mark gnus-unread-mark)
1087      . gnus-summary-normal-unread)
1088     ((> score default-high)
1089      . gnus-summary-high-read)
1090     ((< score default-low)
1091      . gnus-summary-low-read)
1092     (t
1093      . gnus-summary-normal-read))
1094   "*Controls the highlighting of summary buffer lines.
1095
1096 A list of (FORM . FACE) pairs.  When deciding how a particular
1097 summary line should be displayed, each form is evaluated.  The content
1098 of the face field after the first true form is used.  You can change
1099 how those summary lines are displayed, by editing the face field.
1100
1101 You can use the following variables in the FORM field.
1102
1103 score:        The article's score.
1104 default:      The default article score.
1105 default-high: The default score for high scored articles.
1106 default-low:  The default score for low scored articles.
1107 below:        The score below which articles are automatically marked as read.
1108 mark:         The article's mark.
1109 uncached:     Non-nil if the article is uncached."
1110   :group 'gnus-summary-visual
1111   :type '(repeat (cons (sexp :tag "Form" nil)
1112                        face)))
1113 (put 'gnus-summary-highlight 'risky-local-variable t)
1114
1115 (defcustom gnus-alter-header-function nil
1116   "Function called to allow alteration of article header structures.
1117 The function is called with one parameter, the article header vector,
1118 which it may alter in any way."
1119   :type '(choice (const :tag "None" nil)
1120                  function)
1121   :group 'gnus-summary)
1122
1123 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1124   "Function used to decode a string with encoded words.")
1125
1126 (defvar gnus-decode-encoded-address-function
1127   'mail-decode-encoded-address-string
1128   "Function used to decode addresses with encoded words.")
1129
1130 (defcustom gnus-extra-headers '(To Newsgroups)
1131   "*Extra headers to parse."
1132   :version "21.1"
1133   :group 'gnus-summary
1134   :type '(repeat symbol))
1135
1136 (defcustom gnus-ignored-from-addresses
1137   (and user-mail-address
1138        (not (string= user-mail-address ""))
1139        (regexp-quote user-mail-address))
1140   "*From headers that may be suppressed in favor of To headers.
1141 This can be a regexp or a list of regexps."
1142   :version "21.1"
1143   :group 'gnus-summary
1144   :type '(choice regexp
1145                  (repeat :tag "Regexp List" regexp)))
1146
1147 (defsubst gnus-ignored-from-addresses ()
1148   (gmm-regexp-concat gnus-ignored-from-addresses))
1149
1150 (defcustom gnus-summary-to-prefix "-> "
1151   "*String prefixed to the To field in the summary line when
1152 using `gnus-ignored-from-addresses'."
1153   :version "22.1"
1154   :group 'gnus-summary
1155   :type 'string)
1156
1157 (defcustom gnus-summary-newsgroup-prefix "=> "
1158   "*String prefixed to the Newsgroup field in the summary
1159 line when using `gnus-ignored-from-addresses'."
1160   :version "22.1"
1161   :group 'gnus-summary
1162   :type 'string)
1163
1164 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1165   "List of charsets that should be ignored.
1166 When these charsets are used in the \"charset\" parameter, the
1167 default charset will be used instead."
1168   :version "21.1"
1169   :type '(repeat symbol)
1170   :group 'gnus-charset)
1171
1172 (defcustom gnus-newsgroup-maximum-articles nil
1173   "The maximum number of articles a newsgroup.
1174 If this is a number, old articles in a newsgroup exceeding this number
1175 are silently ignored.  If it is nil, no article is ignored.  Note that
1176 setting this variable to a number might prevent you from reading very
1177 old articles."
1178   :group 'gnus-group-select
1179   :version "22.2"
1180   :type '(choice (const :tag "No limit" nil)
1181                  integer))
1182
1183 (gnus-define-group-parameter
1184  ignored-charsets
1185  :type list
1186  :function-document
1187  "Return the ignored charsets of GROUP."
1188  :variable gnus-group-ignored-charsets-alist
1189  :variable-default
1190  '(("alt\\.chinese\\.text" iso-8859-1))
1191  :variable-document
1192  "Alist of regexps (to match group names) and charsets that should be ignored.
1193 When these charsets are used in the \"charset\" parameter, the
1194 default charset will be used instead."
1195  :variable-group gnus-charset
1196  :variable-type '(repeat (cons (regexp :tag "Group")
1197                                (repeat symbol)))
1198  :parameter-type '(choice :tag "Ignored charsets"
1199                           :value nil
1200                           (repeat (symbol)))
1201  :parameter-document       "\
1202 List of charsets that should be ignored.
1203
1204 When these charsets are used in the \"charset\" parameter, the
1205 default charset will be used instead.")
1206
1207 (defcustom gnus-group-highlight-words-alist nil
1208   "Alist of group regexps and highlight regexps.
1209 This variable uses the same syntax as `gnus-emphasis-alist'."
1210   :version "21.1"
1211   :type '(repeat (cons (regexp :tag "Group")
1212                        (repeat (list (regexp :tag "Highlight regexp")
1213                                      (number :tag "Group for entire word" 0)
1214                                      (number :tag "Group for displayed part" 0)
1215                                      (symbol :tag "Face"
1216                                              gnus-emphasis-highlight-words)))))
1217   :group 'gnus-summary-visual)
1218
1219 (defcustom gnus-summary-show-article-charset-alist
1220   nil
1221   "Alist of number and charset.
1222 The article will be shown with the charset corresponding to the
1223 numbered argument.
1224 For example: ((1 . cn-gb-2312) (2 . big5))."
1225   :version "21.1"
1226   :type '(repeat (cons (number :tag "Argument" 1)
1227                        (symbol :tag "Charset")))
1228   :group 'gnus-charset)
1229
1230 (defcustom gnus-preserve-marks t
1231   "Whether marks are preserved when moving, copying and respooling messages."
1232   :version "21.1"
1233   :type 'boolean
1234   :group 'gnus-summary-marks)
1235
1236 (defcustom gnus-propagate-marks t
1237   "If non-nil, do not propagate marks to the backends."
1238   :version "23.1" ;; No Gnus
1239   :type 'boolean
1240   :group 'gnus-summary-marks)
1241
1242 (defcustom gnus-alter-articles-to-read-function nil
1243   "Function to be called to alter the list of articles to be selected."
1244   :type '(choice (const nil) function)
1245   :group 'gnus-summary)
1246
1247 (defcustom gnus-orphan-score nil
1248   "*All orphans get this score added.  Set in the score file."
1249   :group 'gnus-score-default
1250   :type '(choice (const nil)
1251                  integer))
1252
1253 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1254   "*A regexp to match MIME parts when saving multiple parts of a
1255 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1256 This regexp will be used by default when prompting the user for which
1257 type of files to save."
1258   :group 'gnus-summary
1259   :type 'regexp)
1260
1261 (defcustom gnus-read-all-available-headers nil
1262   "Whether Gnus should parse all headers made available to it.
1263 This is mostly relevant for slow back ends where the user may
1264 wish to widen the summary buffer to include all headers
1265 that were fetched."
1266   :version "22.1"
1267   :group 'gnus-summary
1268   :type '(choice boolean regexp))
1269
1270 (defcustom gnus-summary-pipe-output-default-command nil
1271   "Command (and optional arguments) used to pipe article to subprocess.
1272 This will be used as the default command if it is non-nil.  The value
1273 will be updated if you modify it when executing the command
1274 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1275   :version "23.1" ;; No Gnus
1276   :group 'gnus-summary
1277   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1278
1279 (defcustom gnus-summary-muttprint-program "muttprint"
1280   "Command (and optional arguments) used to run Muttprint.
1281 The value will be updated if you modify it when executing the command
1282 `gnus-summary-muttprint'."
1283   :version "22.1"
1284   :group 'gnus-summary
1285   :type 'string)
1286
1287 (defcustom gnus-article-loose-mime t
1288   "If non-nil, don't require MIME-Version header.
1289 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1290 supply the MIME-Version header or deliberately strip it from the mail.
1291 If non-nil (the default), Gnus will treat some articles as MIME
1292 even if the MIME-Version header is missing."
1293   :version "22.1"
1294   :type 'boolean
1295   :group 'gnus-article-mime)
1296
1297 (defcustom gnus-article-emulate-mime t
1298   "If non-nil, use MIME emulation for uuencode and the like.
1299 This means that Gnus will search message bodies for text that look
1300 like uuencoded bits, yEncoded bits, and so on, and present that using
1301 the normal Gnus MIME machinery."
1302   :version "22.1"
1303   :type 'boolean
1304   :group 'gnus-article-mime)
1305
1306 ;;; Internal variables
1307
1308 (defvar gnus-summary-display-cache nil)
1309 (defvar gnus-article-mime-handles nil)
1310 (defvar gnus-article-decoded-p nil)
1311 (defvar gnus-article-charset nil)
1312 (defvar gnus-article-ignored-charsets nil)
1313 (defvar gnus-scores-exclude-files nil)
1314 (defvar gnus-page-broken nil)
1315
1316 (defvar gnus-original-article nil)
1317 (defvar gnus-article-internal-prepare-hook nil)
1318 (defvar gnus-newsgroup-process-stack nil)
1319
1320 (defvar gnus-thread-indent-array nil)
1321 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1322 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1323   "Function called to sort the articles within a thread after it has been gathered together.")
1324
1325 (defvar gnus-summary-save-parts-type-history nil)
1326 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1327
1328 ;; Avoid highlighting in kill files.
1329 (defvar gnus-summary-inhibit-highlight nil)
1330 (defvar gnus-newsgroup-selected-overlay nil)
1331 (defvar gnus-inhibit-limiting nil)
1332 (defvar gnus-newsgroup-adaptive-score-file nil)
1333 (defvar gnus-current-score-file nil)
1334 (defvar gnus-current-move-group nil)
1335 (defvar gnus-current-copy-group nil)
1336 (defvar gnus-current-crosspost-group nil)
1337 (defvar gnus-newsgroup-display nil)
1338
1339 (defvar gnus-newsgroup-dependencies nil)
1340 (defvar gnus-newsgroup-adaptive nil)
1341 (defvar gnus-summary-display-article-function nil)
1342 (defvar gnus-summary-highlight-line-function nil
1343   "Function called after highlighting a summary line.")
1344
1345 (defvar gnus-summary-line-format-alist
1346   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1347     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1348     (?s gnus-tmp-subject-or-nil ?s)
1349     (?n gnus-tmp-name ?s)
1350     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1351         ?s)
1352     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1353             gnus-tmp-from) ?s)
1354     (?F gnus-tmp-from ?s)
1355     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1356     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1357     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1358     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1359     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1360     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1361     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1362     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1363     (?L gnus-tmp-lines ?s)
1364     (?O gnus-tmp-downloaded ?c)
1365     (?I gnus-tmp-indentation ?s)
1366     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1367     (?R gnus-tmp-replied ?c)
1368     (?\[ gnus-tmp-opening-bracket ?c)
1369     (?\] gnus-tmp-closing-bracket ?c)
1370     (?\> (make-string gnus-tmp-level ? ) ?s)
1371     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1372     (?i gnus-tmp-score ?d)
1373     (?z gnus-tmp-score-char ?c)
1374     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1375     (?U gnus-tmp-unread ?c)
1376     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1377         ?s)
1378     (?t (gnus-summary-number-of-articles-in-thread
1379          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1380         ?d)
1381     (?e (gnus-summary-number-of-articles-in-thread
1382          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1383         ?c)
1384     (?u gnus-tmp-user-defined ?s)
1385     (?P (gnus-pick-line-number) ?d)
1386     (?B gnus-tmp-thread-tree-header-string ?s)
1387     (user-date (gnus-user-date
1388                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1389   "An alist of format specifications that can appear in summary lines.
1390 These are paired with what variables they correspond with, along with
1391 the type of the variable (string, integer, character, etc).")
1392
1393 (defvar gnus-summary-dummy-line-format-alist
1394   `((?S gnus-tmp-subject ?s)
1395     (?N gnus-tmp-number ?d)
1396     (?u gnus-tmp-user-defined ?s)))
1397
1398 (defvar gnus-summary-mode-line-format-alist
1399   `((?G gnus-tmp-group-name ?s)
1400     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1401     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1402     (?A gnus-tmp-article-number ?d)
1403     (?Z gnus-tmp-unread-and-unselected ?s)
1404     (?V gnus-version ?s)
1405     (?U gnus-tmp-unread-and-unticked ?d)
1406     (?S gnus-tmp-subject ?s)
1407     (?e gnus-tmp-unselected ?d)
1408     (?u gnus-tmp-user-defined ?s)
1409     (?d (length gnus-newsgroup-dormant) ?d)
1410     (?t (length gnus-newsgroup-marked) ?d)
1411     (?h (length gnus-newsgroup-spam-marked) ?d)
1412     (?r (length gnus-newsgroup-reads) ?d)
1413     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1414     (?E gnus-newsgroup-expunged-tally ?d)
1415     (?s (gnus-current-score-file-nondirectory) ?s)))
1416
1417 ;; This is here rather than in gnus-art for compilation reasons.
1418 (defvar gnus-article-mode-line-format-alist
1419   (nconc '((?w (gnus-article-wash-status) ?s)
1420            (?m (gnus-article-mime-part-status) ?s))
1421          gnus-summary-mode-line-format-alist))
1422
1423 (defvar gnus-last-search-regexp nil
1424   "Default regexp for article search command.")
1425
1426 (defvar gnus-last-shell-command nil
1427   "Default shell command on article.")
1428
1429 (defvar gnus-newsgroup-agentized nil
1430   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1431 (defvar gnus-newsgroup-begin nil)
1432 (defvar gnus-newsgroup-end nil)
1433 (defvar gnus-newsgroup-last-rmail nil)
1434 (defvar gnus-newsgroup-last-mail nil)
1435 (defvar gnus-newsgroup-last-folder nil)
1436 (defvar gnus-newsgroup-last-file nil)
1437 (defvar gnus-newsgroup-last-directory nil)
1438 (defvar gnus-newsgroup-auto-expire nil)
1439 (defvar gnus-newsgroup-active nil)
1440 (defvar gnus-newsgroup-highest nil)
1441
1442 (defvar gnus-newsgroup-data nil)
1443 (defvar gnus-newsgroup-data-reverse nil)
1444 (defvar gnus-newsgroup-limit nil)
1445 (defvar gnus-newsgroup-limits nil)
1446 (defvar gnus-summary-use-undownloaded-faces nil)
1447
1448 (defvar gnus-newsgroup-unreads nil
1449   "Sorted list of unread articles in the current newsgroup.")
1450
1451 (defvar gnus-newsgroup-unselected nil
1452   "Sorted list of unselected unread articles in the current newsgroup.")
1453
1454 (defvar gnus-newsgroup-reads nil
1455   "Alist of read articles and article marks in the current newsgroup.")
1456
1457 (defvar gnus-newsgroup-expunged-tally nil)
1458
1459 (defvar gnus-newsgroup-marked nil
1460   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1461
1462 (defvar gnus-newsgroup-spam-marked nil
1463   "List of ranges of articles that have been marked as spam.")
1464
1465 (defvar gnus-newsgroup-killed nil
1466   "List of ranges of articles that have been through the scoring process.")
1467
1468 (defvar gnus-newsgroup-cached nil
1469   "Sorted list of articles that come from the article cache.")
1470
1471 (defvar gnus-newsgroup-saved nil
1472   "List of articles that have been saved.")
1473
1474 (defvar gnus-newsgroup-kill-headers nil)
1475
1476 (defvar gnus-newsgroup-replied nil
1477   "List of articles that have been replied to in the current newsgroup.")
1478
1479 (defvar gnus-newsgroup-forwarded nil
1480   "List of articles that have been forwarded in the current newsgroup.")
1481
1482 (defvar gnus-newsgroup-recent nil
1483   "List of articles that have are recent in the current newsgroup.")
1484
1485 (defvar gnus-newsgroup-expirable nil
1486   "Sorted list of articles in the current newsgroup that can be expired.")
1487
1488 (defvar gnus-newsgroup-processable nil
1489   "List of articles in the current newsgroup that can be processed.")
1490
1491 (defvar gnus-newsgroup-downloadable nil
1492   "Sorted list of articles in the current newsgroup that can be processed.")
1493
1494 (defvar gnus-newsgroup-unfetched nil
1495   "Sorted list of articles in the current newsgroup whose headers have
1496 not been fetched into the agent.
1497
1498 This list will always be a subset of gnus-newsgroup-undownloaded.")
1499
1500 (defvar gnus-newsgroup-undownloaded nil
1501   "List of articles in the current newsgroup that haven't been downloaded.")
1502
1503 (defvar gnus-newsgroup-unsendable nil
1504   "List of articles in the current newsgroup that won't be sent.")
1505
1506 (defvar gnus-newsgroup-bookmarks nil
1507   "List of articles in the current newsgroup that have bookmarks.")
1508
1509 (defvar gnus-newsgroup-dormant nil
1510   "Sorted list of dormant articles in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-unseen nil
1513   "List of unseen articles in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-seen nil
1516   "Range of seen articles in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-articles nil
1519   "List of articles in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-scored nil
1522   "List of scored articles in the current newsgroup.")
1523
1524 (defvar gnus-newsgroup-headers nil
1525   "List of article headers in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-threads nil)
1528
1529 (defvar gnus-newsgroup-prepared nil
1530   "Whether the current group has been prepared properly.")
1531
1532 (defvar gnus-newsgroup-ancient nil
1533   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-sparse nil)
1536
1537 (defvar gnus-current-article nil)
1538 (defvar gnus-article-current nil)
1539 (defvar gnus-current-headers nil)
1540 (defvar gnus-have-all-headers nil)
1541 (defvar gnus-last-article nil)
1542 (defvar gnus-newsgroup-history nil)
1543 (defvar gnus-newsgroup-charset nil)
1544 (defvar gnus-newsgroup-ephemeral-charset nil)
1545 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1546
1547 (defvar gnus-article-before-search nil)
1548
1549 (defvar gnus-summary-local-variables
1550   '(gnus-newsgroup-name
1551
1552     ;; Marks lists
1553     gnus-newsgroup-unreads
1554     gnus-newsgroup-unselected
1555     gnus-newsgroup-marked
1556     gnus-newsgroup-spam-marked
1557     gnus-newsgroup-reads
1558     gnus-newsgroup-saved
1559     gnus-newsgroup-replied
1560     gnus-newsgroup-forwarded
1561     gnus-newsgroup-recent
1562     gnus-newsgroup-expirable
1563     gnus-newsgroup-killed
1564     gnus-newsgroup-unseen
1565     gnus-newsgroup-seen
1566     gnus-newsgroup-cached
1567     gnus-newsgroup-downloadable
1568     gnus-newsgroup-undownloaded
1569     gnus-newsgroup-unsendable
1570
1571     gnus-newsgroup-begin gnus-newsgroup-end
1572     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1573     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1574     gnus-newsgroup-last-directory
1575     gnus-newsgroup-auto-expire
1576     gnus-newsgroup-processable
1577     gnus-newsgroup-unfetched
1578     gnus-newsgroup-articles
1579     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1580     gnus-newsgroup-headers gnus-newsgroup-threads
1581     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1582     gnus-current-article gnus-current-headers gnus-have-all-headers
1583     gnus-last-article gnus-article-internal-prepare-hook
1584     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1585     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1586     gnus-thread-expunge-below
1587     gnus-score-alist gnus-current-score-file
1588     (gnus-summary-expunge-below . global)
1589     (gnus-summary-mark-below . global)
1590     (gnus-orphan-score . global)
1591     gnus-newsgroup-active gnus-scores-exclude-files
1592     gnus-newsgroup-highest
1593     gnus-newsgroup-history gnus-newsgroup-ancient
1594     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1595     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1596     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1597     (gnus-newsgroup-expunged-tally . 0)
1598     gnus-cache-removable-articles
1599     gnus-newsgroup-data gnus-newsgroup-data-reverse
1600     gnus-newsgroup-limit gnus-newsgroup-limits
1601     gnus-newsgroup-charset gnus-newsgroup-display
1602     gnus-summary-use-undownloaded-faces)
1603   "Variables that are buffer-local to the summary buffers.")
1604
1605 (defvar gnus-newsgroup-variables nil
1606   "A list of variables that have separate values in different newsgroups.
1607 A list of newsgroup (summary buffer) local variables, or cons of
1608 variables and their default expressions to be evalled (when the default
1609 values are not nil), that should be made global while the summary buffer
1610 is active.
1611
1612 Note: The default expressions will be evaluated (using function `eval')
1613 before assignment to the local variable rather than just assigned to it.
1614 If the default expression is the symbol `global', that symbol will not
1615 be evaluated but the global value of the local variable will be used
1616 instead.
1617
1618 These variables can be used to set variables in the group parameters
1619 while still allowing them to affect operations done in other buffers.
1620 For example:
1621
1622 \(setq gnus-newsgroup-variables
1623      '(message-use-followup-to
1624        (gnus-visible-headers .
1625          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1626 ")
1627
1628 (eval-when-compile
1629   ;; Bind features so that require will believe that gnus-sum has
1630   ;; already been loaded (avoids infinite recursion)
1631   (let ((features (cons 'gnus-sum features)))
1632     (require 'gnus-art)))
1633
1634 ;; MIME stuff.
1635
1636 (defvar gnus-decode-encoded-word-methods
1637   '(mail-decode-encoded-word-string)
1638   "List of methods used to decode encoded words.
1639
1640 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1641 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1642 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1643 whose names match REGEXP.
1644
1645 For example:
1646 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1647  mail-decode-encoded-word-string
1648  (\"chinese\" . rfc1843-decode-string))")
1649
1650 (defvar gnus-decode-encoded-word-methods-cache nil)
1651
1652 (defun gnus-multi-decode-encoded-word-string (string)
1653   "Apply the functions from `gnus-encoded-word-methods' that match."
1654   (unless (and gnus-decode-encoded-word-methods-cache
1655                (eq gnus-newsgroup-name
1656                    (car gnus-decode-encoded-word-methods-cache)))
1657     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1658     (dolist (method gnus-decode-encoded-word-methods)
1659       (if (symbolp method)
1660           (nconc gnus-decode-encoded-word-methods-cache (list method))
1661         (if (and gnus-newsgroup-name
1662                  (string-match (car method) gnus-newsgroup-name))
1663             (nconc gnus-decode-encoded-word-methods-cache
1664                    (list (cdr method)))))))
1665   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1666     (setq string (funcall method string))))
1667
1668 ;; Subject simplification.
1669
1670 (defun gnus-simplify-whitespace (str)
1671   "Remove excessive whitespace from STR."
1672   ;; Multiple spaces.
1673   (while (string-match "[ \t][ \t]+" str)
1674     (setq str (concat (substring str 0 (match-beginning 0))
1675                         " "
1676                         (substring str (match-end 0)))))
1677   ;; Leading spaces.
1678   (when (string-match "^[ \t]+" str)
1679     (setq str (substring str (match-end 0))))
1680   ;; Trailing spaces.
1681   (when (string-match "[ \t]+$" str)
1682     (setq str (substring str 0 (match-beginning 0))))
1683   str)
1684
1685 (defun gnus-simplify-all-whitespace (str)
1686   "Remove all whitespace from STR."
1687   (while (string-match "[ \t\n]+" str)
1688     (setq str (replace-match "" nil nil str)))
1689   str)
1690
1691 (defsubst gnus-simplify-subject-re (subject)
1692   "Remove \"Re:\" from subject lines."
1693   (if (string-match message-subject-re-regexp subject)
1694       (substring subject (match-end 0))
1695     subject))
1696
1697 (defun gnus-simplify-subject (subject &optional re-only)
1698   "Remove `Re:' and words in parentheses.
1699 If RE-ONLY is non-nil, strip leading `Re:'s only."
1700   (let ((case-fold-search t))           ;Ignore case.
1701     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1702     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1703       (setq subject (substring subject (match-end 0))))
1704     ;; Remove uninteresting prefixes.
1705     (when (and (not re-only)
1706                gnus-simplify-ignored-prefixes
1707                (string-match gnus-simplify-ignored-prefixes subject))
1708       (setq subject (substring subject (match-end 0))))
1709     ;; Remove words in parentheses from end.
1710     (unless re-only
1711       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1712         (setq subject (substring subject 0 (match-beginning 0)))))
1713     ;; Return subject string.
1714     subject))
1715
1716 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1717 ;; all whitespace.
1718 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1719   (goto-char (point-min))
1720   (while (re-search-forward regexp nil t)
1721     (replace-match (or newtext ""))))
1722
1723 (defun gnus-simplify-buffer-fuzzy ()
1724   "Simplify string in the buffer fuzzily.
1725 The string in the accessible portion of the current buffer is simplified.
1726 It is assumed to be a single-line subject.
1727 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1728 matter is removed.  Additional things can be deleted by setting
1729 `gnus-simplify-subject-fuzzy-regexp'."
1730   (let ((case-fold-search t)
1731         (modified-tick))
1732     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1733
1734     (while (not (eq modified-tick (buffer-modified-tick)))
1735       (setq modified-tick (buffer-modified-tick))
1736       (cond
1737        ((listp gnus-simplify-subject-fuzzy-regexp)
1738         (mapc 'gnus-simplify-buffer-fuzzy-step
1739               gnus-simplify-subject-fuzzy-regexp))
1740        (gnus-simplify-subject-fuzzy-regexp
1741         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1742       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1743       (gnus-simplify-buffer-fuzzy-step
1744        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1745       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1746
1747     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1748     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1749     (gnus-simplify-buffer-fuzzy-step " $")
1750     (gnus-simplify-buffer-fuzzy-step "^ +")))
1751
1752 (defun gnus-simplify-subject-fuzzy (subject)
1753   "Simplify a subject string fuzzily.
1754 See `gnus-simplify-buffer-fuzzy' for details."
1755   (save-excursion
1756     (gnus-set-work-buffer)
1757     (let ((case-fold-search t))
1758       ;; Remove uninteresting prefixes.
1759       (when (and gnus-simplify-ignored-prefixes
1760                  (string-match gnus-simplify-ignored-prefixes subject))
1761         (setq subject (substring subject (match-end 0))))
1762       (insert subject)
1763       (inline (gnus-simplify-buffer-fuzzy))
1764       (buffer-string))))
1765
1766 (defsubst gnus-simplify-subject-fully (subject)
1767   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1768   (cond
1769    (gnus-simplify-subject-functions
1770     (gnus-map-function gnus-simplify-subject-functions subject))
1771    ((null gnus-summary-gather-subject-limit)
1772     (gnus-simplify-subject-re subject))
1773    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1774     (gnus-simplify-subject-fuzzy subject))
1775    ((numberp gnus-summary-gather-subject-limit)
1776     (truncate-string-to-width (gnus-simplify-subject-re subject)
1777                               gnus-summary-gather-subject-limit))
1778    (t
1779     subject)))
1780
1781 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1782   "Check whether two subjects are equal.
1783 If optional argument SIMPLE-FIRST is t, first argument is already
1784 simplified."
1785   (cond
1786    ((null simple-first)
1787     (equal (gnus-simplify-subject-fully s1)
1788            (gnus-simplify-subject-fully s2)))
1789    (t
1790     (equal s1
1791            (gnus-simplify-subject-fully s2)))))
1792
1793 (defun gnus-summary-bubble-group ()
1794   "Increase the score of the current group.
1795 This is a handy function to add to `gnus-summary-exit-hook' to
1796 increase the score of each group you read."
1797   (gnus-group-add-score gnus-newsgroup-name))
1798
1799 \f
1800 ;;;
1801 ;;; Gnus summary mode
1802 ;;;
1803
1804 (put 'gnus-summary-mode 'mode-class 'special)
1805
1806 (defvar gnus-article-commands-menu)
1807
1808 ;; Non-orthogonal keys
1809
1810 (gnus-define-keys gnus-summary-mode-map
1811   " " gnus-summary-next-page
1812   "\177" gnus-summary-prev-page
1813   [delete] gnus-summary-prev-page
1814   [backspace] gnus-summary-prev-page
1815   "\r" gnus-summary-scroll-up
1816   "\M-\r" gnus-summary-scroll-down
1817   "n" gnus-summary-next-unread-article
1818   "p" gnus-summary-prev-unread-article
1819   "N" gnus-summary-next-article
1820   "P" gnus-summary-prev-article
1821   "\M-\C-n" gnus-summary-next-same-subject
1822   "\M-\C-p" gnus-summary-prev-same-subject
1823   "\M-n" gnus-summary-next-unread-subject
1824   "\M-p" gnus-summary-prev-unread-subject
1825   "." gnus-summary-first-unread-article
1826   "," gnus-summary-best-unread-article
1827   "\M-s" gnus-summary-search-article-forward
1828   "\M-r" gnus-summary-search-article-backward
1829   "\M-S" gnus-summary-repeat-search-article-forward
1830   "\M-R" gnus-summary-repeat-search-article-backward
1831   "<" gnus-summary-beginning-of-article
1832   ">" gnus-summary-end-of-article
1833   "j" gnus-summary-goto-article
1834   "^" gnus-summary-refer-parent-article
1835   "\M-^" gnus-summary-refer-article
1836   "u" gnus-summary-tick-article-forward
1837   "!" gnus-summary-tick-article-forward
1838   "U" gnus-summary-tick-article-backward
1839   "d" gnus-summary-mark-as-read-forward
1840   "D" gnus-summary-mark-as-read-backward
1841   "E" gnus-summary-mark-as-expirable
1842   "\M-u" gnus-summary-clear-mark-forward
1843   "\M-U" gnus-summary-clear-mark-backward
1844   "k" gnus-summary-kill-same-subject-and-select
1845   "\C-k" gnus-summary-kill-same-subject
1846   "\M-\C-k" gnus-summary-kill-thread
1847   "\M-\C-l" gnus-summary-lower-thread
1848   "e" gnus-summary-edit-article
1849   "#" gnus-summary-mark-as-processable
1850   "\M-#" gnus-summary-unmark-as-processable
1851   "\M-\C-t" gnus-summary-toggle-threads
1852   "\M-\C-s" gnus-summary-show-thread
1853   "\M-\C-h" gnus-summary-hide-thread
1854   "\M-\C-f" gnus-summary-next-thread
1855   "\M-\C-b" gnus-summary-prev-thread
1856   [(meta down)] gnus-summary-next-thread
1857   [(meta up)] gnus-summary-prev-thread
1858   "\M-\C-u" gnus-summary-up-thread
1859   "\M-\C-d" gnus-summary-down-thread
1860   "&" gnus-summary-execute-command
1861   "c" gnus-summary-catchup-and-exit
1862   "\C-w" gnus-summary-mark-region-as-read
1863   "\C-t" gnus-summary-toggle-truncation
1864   "?" gnus-summary-mark-as-dormant
1865   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1866   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1867   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1868   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1869   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1870   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1871   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1872   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1873   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1874   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1875   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1876   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1877   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1878   "=" gnus-summary-expand-window
1879   "\C-x\C-s" gnus-summary-reselect-current-group
1880   "\M-g" gnus-summary-rescan-group
1881   "\C-c\C-r" gnus-summary-caesar-message
1882   "f" gnus-summary-followup
1883   "F" gnus-summary-followup-with-original
1884   "C" gnus-summary-cancel-article
1885   "r" gnus-summary-reply
1886   "R" gnus-summary-reply-with-original
1887   "\C-c\C-f" gnus-summary-mail-forward
1888   "o" gnus-summary-save-article
1889   "\C-o" gnus-summary-save-article-mail
1890   "|" gnus-summary-pipe-output
1891   "\M-k" gnus-summary-edit-local-kill
1892   "\M-K" gnus-summary-edit-global-kill
1893   ;; "V" gnus-version
1894   "\C-c\C-d" gnus-summary-describe-group
1895   "q" gnus-summary-exit
1896   "Q" gnus-summary-exit-no-update
1897   "\C-c\C-i" gnus-info-find-node
1898   gnus-mouse-2 gnus-mouse-pick-article
1899   [follow-link] mouse-face
1900   "m" gnus-summary-mail-other-window
1901   "a" gnus-summary-post-news
1902   "x" gnus-summary-limit-to-unread
1903   "s" gnus-summary-isearch-article
1904   "t" gnus-summary-toggle-header
1905   "g" gnus-summary-show-article
1906   "l" gnus-summary-goto-last-article
1907   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1908   "\C-d" gnus-summary-enter-digest-group
1909   "\M-\C-d" gnus-summary-read-document
1910   "\M-\C-e" gnus-summary-edit-parameters
1911   "\M-\C-a" gnus-summary-customize-parameters
1912   "\C-c\C-b" gnus-bug
1913   "*" gnus-cache-enter-article
1914   "\M-*" gnus-cache-remove-article
1915   "\M-&" gnus-summary-universal-argument
1916   "\C-l" gnus-recenter
1917   "I" gnus-summary-increase-score
1918   "L" gnus-summary-lower-score
1919   "\M-i" gnus-symbolic-argument
1920   "h" gnus-summary-select-article-buffer
1921
1922   "b" gnus-article-view-part
1923   "\M-t" gnus-summary-toggle-display-buttonized
1924
1925   "V" gnus-summary-score-map
1926   "X" gnus-uu-extract-map
1927   "S" gnus-summary-send-map)
1928
1929 ;; Sort of orthogonal keymap
1930 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1931   "t" gnus-summary-tick-article-forward
1932   "!" gnus-summary-tick-article-forward
1933   "d" gnus-summary-mark-as-read-forward
1934   "r" gnus-summary-mark-as-read-forward
1935   "c" gnus-summary-clear-mark-forward
1936   " " gnus-summary-clear-mark-forward
1937   "e" gnus-summary-mark-as-expirable
1938   "x" gnus-summary-mark-as-expirable
1939   "?" gnus-summary-mark-as-dormant
1940   "b" gnus-summary-set-bookmark
1941   "B" gnus-summary-remove-bookmark
1942   "#" gnus-summary-mark-as-processable
1943   "\M-#" gnus-summary-unmark-as-processable
1944   "S" gnus-summary-limit-include-expunged
1945   "C" gnus-summary-catchup
1946   "H" gnus-summary-catchup-to-here
1947   "h" gnus-summary-catchup-from-here
1948   "\C-c" gnus-summary-catchup-all
1949   "k" gnus-summary-kill-same-subject-and-select
1950   "K" gnus-summary-kill-same-subject
1951   "P" gnus-uu-mark-map)
1952
1953 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1954   "c" gnus-summary-clear-above
1955   "u" gnus-summary-tick-above
1956   "m" gnus-summary-mark-above
1957   "k" gnus-summary-kill-below)
1958
1959 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1960   "/" gnus-summary-limit-to-subject
1961   "n" gnus-summary-limit-to-articles
1962   "b" gnus-summary-limit-to-bodies
1963   "h" gnus-summary-limit-to-headers
1964   "w" gnus-summary-pop-limit
1965   "s" gnus-summary-limit-to-subject
1966   "a" gnus-summary-limit-to-author
1967   "u" gnus-summary-limit-to-unread
1968   "m" gnus-summary-limit-to-marks
1969   "M" gnus-summary-limit-exclude-marks
1970   "v" gnus-summary-limit-to-score
1971   "*" gnus-summary-limit-include-cached
1972   "D" gnus-summary-limit-include-dormant
1973   "T" gnus-summary-limit-include-thread
1974   "d" gnus-summary-limit-exclude-dormant
1975   "t" gnus-summary-limit-to-age
1976   "." gnus-summary-limit-to-unseen
1977   "x" gnus-summary-limit-to-extra
1978   "p" gnus-summary-limit-to-display-predicate
1979   "E" gnus-summary-limit-include-expunged
1980   "c" gnus-summary-limit-exclude-childless-dormant
1981   "C" gnus-summary-limit-mark-excluded-as-read
1982   "o" gnus-summary-insert-old-articles
1983   "N" gnus-summary-insert-new-articles
1984   "S" gnus-summary-limit-to-singletons
1985   "r" gnus-summary-limit-to-replied
1986   "R" gnus-summary-limit-to-recipient
1987   "A" gnus-summary-limit-to-address)
1988
1989 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1990   "n" gnus-summary-next-unread-article
1991   "p" gnus-summary-prev-unread-article
1992   "N" gnus-summary-next-article
1993   "P" gnus-summary-prev-article
1994   "\C-n" gnus-summary-next-same-subject
1995   "\C-p" gnus-summary-prev-same-subject
1996   "\M-n" gnus-summary-next-unread-subject
1997   "\M-p" gnus-summary-prev-unread-subject
1998   "f" gnus-summary-first-unread-article
1999   "b" gnus-summary-best-unread-article
2000   "j" gnus-summary-goto-article
2001   "g" gnus-summary-goto-subject
2002   "l" gnus-summary-goto-last-article
2003   "o" gnus-summary-pop-article)
2004
2005 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2006   "k" gnus-summary-kill-thread
2007   "E" gnus-summary-expire-thread
2008   "l" gnus-summary-lower-thread
2009   "i" gnus-summary-raise-thread
2010   "T" gnus-summary-toggle-threads
2011   "t" gnus-summary-rethread-current
2012   "^" gnus-summary-reparent-thread
2013   "\M-^" gnus-summary-reparent-children
2014   "s" gnus-summary-show-thread
2015   "S" gnus-summary-show-all-threads
2016   "h" gnus-summary-hide-thread
2017   "H" gnus-summary-hide-all-threads
2018   "n" gnus-summary-next-thread
2019   "p" gnus-summary-prev-thread
2020   "u" gnus-summary-up-thread
2021   "o" gnus-summary-top-thread
2022   "d" gnus-summary-down-thread
2023   "#" gnus-uu-mark-thread
2024   "\M-#" gnus-uu-unmark-thread)
2025
2026 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2027   "g" gnus-summary-prepare
2028   "c" gnus-summary-insert-cached-articles
2029   "d" gnus-summary-insert-dormant-articles
2030   "t" gnus-summary-insert-ticked-articles)
2031
2032 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2033   "c" gnus-summary-catchup-and-exit
2034   "C" gnus-summary-catchup-all-and-exit
2035   "E" gnus-summary-exit-no-update
2036   "Q" gnus-summary-exit
2037   "Z" gnus-summary-exit
2038   "n" gnus-summary-catchup-and-goto-next-group
2039   "p" gnus-summary-catchup-and-goto-prev-group
2040   "R" gnus-summary-reselect-current-group
2041   "G" gnus-summary-rescan-group
2042   "N" gnus-summary-next-group
2043   "s" gnus-summary-save-newsrc
2044   "P" gnus-summary-prev-group)
2045
2046 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2047   " " gnus-summary-next-page
2048   "n" gnus-summary-next-page
2049   "\177" gnus-summary-prev-page
2050   [delete] gnus-summary-prev-page
2051   "p" gnus-summary-prev-page
2052   "\r" gnus-summary-scroll-up
2053   "\M-\r" gnus-summary-scroll-down
2054   "<" gnus-summary-beginning-of-article
2055   ">" gnus-summary-end-of-article
2056   "b" gnus-summary-beginning-of-article
2057   "e" gnus-summary-end-of-article
2058   "^" gnus-summary-refer-parent-article
2059   "r" gnus-summary-refer-parent-article
2060   "C" gnus-summary-show-complete-article
2061   "D" gnus-summary-enter-digest-group
2062   "R" gnus-summary-refer-references
2063   "T" gnus-summary-refer-thread
2064   "g" gnus-summary-show-article
2065   "s" gnus-summary-isearch-article
2066   "P" gnus-summary-print-article
2067   "S" gnus-sticky-article
2068   "M" gnus-mailing-list-insinuate
2069   "t" gnus-article-babel)
2070
2071 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2072   "b" gnus-article-add-buttons
2073   "B" gnus-article-add-buttons-to-head
2074   "o" gnus-article-treat-overstrike
2075   "e" gnus-article-emphasize
2076   "w" gnus-article-fill-cited-article
2077   "Q" gnus-article-fill-long-lines
2078   "L" gnus-article-toggle-truncate-lines
2079   "C" gnus-article-capitalize-sentences
2080   "c" gnus-article-remove-cr
2081   "q" gnus-article-de-quoted-unreadable
2082   "6" gnus-article-de-base64-unreadable
2083   "Z" gnus-article-decode-HZ
2084   "A" gnus-article-treat-ansi-sequences
2085   "h" gnus-article-wash-html
2086   "u" gnus-article-unsplit-urls
2087   "s" gnus-summary-force-verify-and-decrypt
2088   "f" gnus-article-display-x-face
2089   "l" gnus-summary-stop-page-breaking
2090   "r" gnus-summary-caesar-message
2091   "m" gnus-summary-morse-message
2092   "t" gnus-summary-toggle-header
2093   "g" gnus-treat-smiley
2094   "v" gnus-summary-verbose-headers
2095   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2096   "p" gnus-article-verify-x-pgp-sig
2097   "d" gnus-article-treat-dumbquotes
2098   "i" gnus-summary-idna-message)
2099
2100 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2101   ;; mnemonic: deuglif*Y*
2102   "u" gnus-article-outlook-unwrap-lines
2103   "a" gnus-article-outlook-repair-attribution
2104   "c" gnus-article-outlook-rearrange-citation
2105   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2106
2107 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2108   "a" gnus-article-hide
2109   "h" gnus-article-hide-headers
2110   "b" gnus-article-hide-boring-headers
2111   "s" gnus-article-hide-signature
2112   "c" gnus-article-hide-citation
2113   "C" gnus-article-hide-citation-in-followups
2114   "l" gnus-article-hide-list-identifiers
2115   "B" gnus-article-strip-banner
2116   "P" gnus-article-hide-pem
2117   "\C-c" gnus-article-hide-citation-maybe)
2118
2119 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2120   "a" gnus-article-highlight
2121   "h" gnus-article-highlight-headers
2122   "c" gnus-article-highlight-citation
2123   "s" gnus-article-highlight-signature)
2124
2125 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2126   "f" gnus-article-treat-fold-headers
2127   "u" gnus-article-treat-unfold-headers
2128   "n" gnus-article-treat-fold-newsgroups)
2129
2130 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2131   "x" gnus-article-display-x-face
2132   "d" gnus-article-display-face
2133   "s" gnus-treat-smiley
2134   "D" gnus-article-remove-images
2135   "W" gnus-html-show-images
2136   "f" gnus-treat-from-picon
2137   "m" gnus-treat-mail-picon
2138   "n" gnus-treat-newsgroups-picon
2139   "g" gnus-treat-from-gravatar
2140   "h" gnus-treat-mail-gravatar)
2141
2142 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2143   "w" gnus-article-decode-mime-words
2144   "c" gnus-article-decode-charset
2145   "v" gnus-mime-view-all-parts
2146   "b" gnus-article-view-part)
2147
2148 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2149   "z" gnus-article-date-ut
2150   "u" gnus-article-date-ut
2151   "l" gnus-article-date-local
2152   "p" gnus-article-date-english
2153   "e" gnus-article-date-lapsed
2154   "o" gnus-article-date-original
2155   "i" gnus-article-date-iso8601
2156   "s" gnus-article-date-user)
2157
2158 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2159   "t" gnus-article-remove-trailing-blank-lines
2160   "l" gnus-article-strip-leading-blank-lines
2161   "m" gnus-article-strip-multiple-blank-lines
2162   "a" gnus-article-strip-blank-lines
2163   "A" gnus-article-strip-all-blank-lines
2164   "s" gnus-article-strip-leading-space
2165   "e" gnus-article-strip-trailing-space
2166   "w" gnus-article-remove-leading-whitespace)
2167
2168 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2169   "v" gnus-version
2170   "d" gnus-summary-describe-group
2171   "h" gnus-summary-describe-briefly
2172   "i" gnus-info-find-node
2173   "C" gnus-group-fetch-control)
2174
2175 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2176   "e" gnus-summary-expire-articles
2177   "\M-\C-e" gnus-summary-expire-articles-now
2178   "\177" gnus-summary-delete-article
2179   [delete] gnus-summary-delete-article
2180   [backspace] gnus-summary-delete-article
2181   "m" gnus-summary-move-article
2182   "r" gnus-summary-respool-article
2183   "w" gnus-summary-edit-article
2184   "c" gnus-summary-copy-article
2185   "B" gnus-summary-crosspost-article
2186   "q" gnus-summary-respool-query
2187   "t" gnus-summary-respool-trace
2188   "i" gnus-summary-import-article
2189   "I" gnus-summary-create-article
2190   "p" gnus-summary-article-posted-p)
2191
2192 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2193   "o" gnus-summary-save-article
2194   "m" gnus-summary-save-article-mail
2195   "F" gnus-summary-write-article-file
2196   "r" gnus-summary-save-article-rmail
2197   "f" gnus-summary-save-article-file
2198   "b" gnus-summary-save-article-body-file
2199   "B" gnus-summary-write-article-body-file
2200   "h" gnus-summary-save-article-folder
2201   "v" gnus-summary-save-article-vm
2202   "p" gnus-summary-pipe-output
2203   "P" gnus-summary-muttprint)
2204
2205 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2206   "b" gnus-summary-display-buttonized
2207   "m" gnus-summary-repair-multipart
2208   "v" gnus-article-view-part
2209   "o" gnus-article-save-part
2210   "O" gnus-article-save-part-and-strip
2211   "r" gnus-article-replace-part
2212   "d" gnus-article-delete-part
2213   "t" gnus-article-view-part-as-type
2214   "j" gnus-article-jump-to-part
2215   "c" gnus-article-copy-part
2216   "C" gnus-article-view-part-as-charset
2217   "e" gnus-article-view-part-externally
2218   "H" gnus-article-browse-html-article
2219   "E" gnus-article-encrypt-body
2220   "i" gnus-article-inline-part
2221   "|" gnus-article-pipe-part)
2222
2223 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2224   "p" gnus-summary-mark-as-processable
2225   "u" gnus-summary-unmark-as-processable
2226   "U" gnus-summary-unmark-all-processable
2227   "v" gnus-uu-mark-over
2228   "s" gnus-uu-mark-series
2229   "r" gnus-uu-mark-region
2230   "g" gnus-uu-unmark-region
2231   "R" gnus-uu-mark-by-regexp
2232   "G" gnus-uu-unmark-by-regexp
2233   "t" gnus-uu-mark-thread
2234   "T" gnus-uu-unmark-thread
2235   "a" gnus-uu-mark-all
2236   "b" gnus-uu-mark-buffer
2237   "S" gnus-uu-mark-sparse
2238   "k" gnus-summary-kill-process-mark
2239   "y" gnus-summary-yank-process-mark
2240   "w" gnus-summary-save-process-mark
2241   "i" gnus-uu-invert-processable)
2242
2243 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2244   ;;"x" gnus-uu-extract-any
2245   "m" gnus-summary-save-parts
2246   "u" gnus-uu-decode-uu
2247   "U" gnus-uu-decode-uu-and-save
2248   "s" gnus-uu-decode-unshar
2249   "S" gnus-uu-decode-unshar-and-save
2250   "o" gnus-uu-decode-save
2251   "O" gnus-uu-decode-save
2252   "b" gnus-uu-decode-binhex
2253   "B" gnus-uu-decode-binhex
2254   "Y" gnus-uu-decode-yenc
2255   "p" gnus-uu-decode-postscript
2256   "P" gnus-uu-decode-postscript-and-save)
2257
2258 (gnus-define-keys
2259     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2260   "u" gnus-uu-decode-uu-view
2261   "U" gnus-uu-decode-uu-and-save-view
2262   "s" gnus-uu-decode-unshar-view
2263   "S" gnus-uu-decode-unshar-and-save-view
2264   "o" gnus-uu-decode-save-view
2265   "O" gnus-uu-decode-save-view
2266   "b" gnus-uu-decode-binhex-view
2267   "B" gnus-uu-decode-binhex-view
2268   "p" gnus-uu-decode-postscript-view
2269   "P" gnus-uu-decode-postscript-and-save-view)
2270
2271 (defvar gnus-article-post-menu nil)
2272
2273 (defconst gnus-summary-menu-maxlen 20)
2274
2275 (defun gnus-summary-menu-split (menu)
2276   ;; If we have lots of elements, divide them into groups of 20
2277   ;; and make a pane (or submenu) for each one.
2278   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2279       (let ((menu menu) sublists next
2280             (i 1))
2281         (while menu
2282           ;; Pull off the next gnus-summary-menu-maxlen elements
2283           ;; and make them the next element of sublist.
2284           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2285           (if next
2286               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2287                       nil))
2288           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2289                                              (aref (car (last menu)) 0)) menu)
2290                                sublists))
2291           (setq i (1+ i))
2292           (setq menu next))
2293         (nreverse sublists))
2294     ;; Few elements--put them all in one pane.
2295     menu))
2296
2297 (defun gnus-summary-make-menu-bar ()
2298   (gnus-turn-off-edit-menu 'summary)
2299
2300   (unless (boundp 'gnus-summary-misc-menu)
2301
2302     (easy-menu-define
2303       gnus-summary-kill-menu gnus-summary-mode-map ""
2304       (cons
2305        "Score"
2306        (nconc
2307         (list
2308          ["Customize" gnus-score-customize t])
2309         (gnus-make-score-map 'increase)
2310         (gnus-make-score-map 'lower)
2311         '(("Mark"
2312            ["Kill below" gnus-summary-kill-below t]
2313            ["Mark above" gnus-summary-mark-above t]
2314            ["Tick above" gnus-summary-tick-above t]
2315            ["Clear above" gnus-summary-clear-above t])
2316           ["Current score" gnus-summary-current-score t]
2317           ["Set score" gnus-summary-set-score t]
2318           ["Switch current score file..." gnus-score-change-score-file t]
2319           ["Set mark below..." gnus-score-set-mark-below t]
2320           ["Set expunge below..." gnus-score-set-expunge-below t]
2321           ["Edit current score file" gnus-score-edit-current-scores t]
2322           ["Edit score file..." gnus-score-edit-file t]
2323           ["Trace score" gnus-score-find-trace t]
2324           ["Find words" gnus-score-find-favourite-words t]
2325           ["Rescore buffer" gnus-summary-rescore t]
2326           ["Increase score..." gnus-summary-increase-score t]
2327           ["Lower score..." gnus-summary-lower-score t]))))
2328
2329     ;; Define both the Article menu in the summary buffer and the
2330     ;; equivalent Commands menu in the article buffer here for
2331     ;; consistency.
2332     (let ((innards
2333            `(("Hide"
2334               ["All" gnus-article-hide t]
2335               ["Headers" gnus-article-hide-headers t]
2336               ["Signature" gnus-article-hide-signature t]
2337               ["Citation" gnus-article-hide-citation t]
2338               ["List identifiers" gnus-article-hide-list-identifiers t]
2339               ["Banner" gnus-article-strip-banner t]
2340               ["Boring headers" gnus-article-hide-boring-headers t])
2341              ("Highlight"
2342               ["All" gnus-article-highlight t]
2343               ["Headers" gnus-article-highlight-headers t]
2344               ["Signature" gnus-article-highlight-signature t]
2345               ["Citation" gnus-article-highlight-citation t])
2346              ("MIME"
2347               ["Words" gnus-article-decode-mime-words t]
2348               ["Charset" gnus-article-decode-charset t]
2349               ["QP" gnus-article-de-quoted-unreadable t]
2350               ["Base64" gnus-article-de-base64-unreadable t]
2351               ["View MIME buttons" gnus-summary-display-buttonized t]
2352               ["View all" gnus-mime-view-all-parts t]
2353               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2354               ["Encrypt body" gnus-article-encrypt-body
2355                :active (not (gnus-group-read-only-p))
2356                ,@(if (featurep 'xemacs) nil
2357                    '(:help "Encrypt the message body on disk"))]
2358               ["Extract all parts..." gnus-summary-save-parts t]
2359               ("Multipart"
2360                ["Repair multipart" gnus-summary-repair-multipart t]
2361                ["Pipe part..." gnus-article-pipe-part t]
2362                ["Inline part" gnus-article-inline-part t]
2363                ["View part as type..." gnus-article-view-part-as-type t]
2364                ["Encrypt body" gnus-article-encrypt-body
2365                 :active (not (gnus-group-read-only-p))
2366                ,@(if (featurep 'xemacs) nil
2367                    '(:help "Encrypt the message body on disk"))]
2368                ["View part externally" gnus-article-view-part-externally t]
2369                ["View HTML parts in browser" gnus-article-browse-html-article t]
2370                ["View part with charset..." gnus-article-view-part-as-charset t]
2371                ["Copy part" gnus-article-copy-part t]
2372                ["Save part..." gnus-article-save-part t]
2373                ["View part" gnus-article-view-part t]))
2374              ("Date"
2375               ["Local" gnus-article-date-local t]
2376               ["ISO8601" gnus-article-date-iso8601 t]
2377               ["UT" gnus-article-date-ut t]
2378               ["Original" gnus-article-date-original t]
2379               ["Lapsed" gnus-article-date-lapsed t]
2380               ["User-defined" gnus-article-date-user t])
2381              ("Display"
2382               ["Remove images" gnus-article-remove-images t]
2383               ["Toggle smiley" gnus-treat-smiley t]
2384               ["Show X-Face" gnus-article-display-x-face t]
2385               ["Show picons in From" gnus-treat-from-picon t]
2386               ["Show picons in mail headers" gnus-treat-mail-picon t]
2387               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2388               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2389               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2390               ("View as different encoding"
2391                ,@(gnus-summary-menu-split
2392                   (mapcar
2393                    (lambda (cs)
2394                      ;; Since easymenu under Emacs doesn't allow
2395                      ;; lambda forms for menu commands, we should
2396                      ;; provide intern'ed function symbols.
2397                      (let ((command (intern (format "\
2398 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2399                        (fset command
2400                              `(lambda ()
2401                                 (interactive)
2402                                 (let ((gnus-summary-show-article-charset-alist
2403                                        '((1 . ,cs))))
2404                                   (gnus-summary-show-article 1))))
2405                        `[,(symbol-name cs) ,command t]))
2406                    (sort (if (fboundp 'coding-system-list)
2407                              (coding-system-list)
2408                            (mapcar 'car mm-mime-mule-charset-alist))
2409                          'string<)))))
2410              ("Washing"
2411               ("Remove Blanks"
2412                ["Leading" gnus-article-strip-leading-blank-lines t]
2413                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2414                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2415                ["All of the above" gnus-article-strip-blank-lines t]
2416                ["All" gnus-article-strip-all-blank-lines t]
2417                ["Leading space" gnus-article-strip-leading-space t]
2418                ["Trailing space" gnus-article-strip-trailing-space t]
2419                ["Leading space in headers"
2420                 gnus-article-remove-leading-whitespace t])
2421               ["Overstrike" gnus-article-treat-overstrike t]
2422               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2423               ["Emphasis" gnus-article-emphasize t]
2424               ["Word wrap" gnus-article-fill-cited-article t]
2425               ["Fill long lines" gnus-article-fill-long-lines t]
2426               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2427               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2428               ["Remove CR" gnus-article-remove-cr t]
2429               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2430               ["Base64" gnus-article-de-base64-unreadable t]
2431               ["Rot 13" gnus-summary-caesar-message
2432                ,@(if (featurep 'xemacs) '(t)
2433                    '(:help "\"Caesar rotate\" article by 13"))]
2434               ["De-IDNA" gnus-summary-idna-message t]
2435               ["Morse decode" gnus-summary-morse-message t]
2436               ["Unix pipe..." gnus-summary-pipe-message t]
2437               ["Add buttons" gnus-article-add-buttons t]
2438               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2439               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2440               ["Verbose header" gnus-summary-verbose-headers t]
2441               ["Toggle header" gnus-summary-toggle-header t]
2442               ["Unfold headers" gnus-article-treat-unfold-headers t]
2443               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2444               ["Html" gnus-article-wash-html t]
2445               ["Unsplit URLs" gnus-article-unsplit-urls t]
2446               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2447               ["Decode HZ" gnus-article-decode-HZ t]
2448               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2449               ("(Outlook) Deuglify"
2450                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2451                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2452                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2453                ["Full (Outlook) deuglify"
2454                 gnus-article-outlook-deuglify-article t])
2455               )
2456              ("Output"
2457               ["Save in default format..." gnus-summary-save-article
2458                ,@(if (featurep 'xemacs) '(t)
2459                    '(:help "Save article using default method"))]
2460               ["Save in file..." gnus-summary-save-article-file
2461                ,@(if (featurep 'xemacs) '(t)
2462                    '(:help "Save article in file"))]
2463               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2464               ["Save in MH folder..." gnus-summary-save-article-folder t]
2465               ["Save in VM folder..." gnus-summary-save-article-vm t]
2466               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2467               ["Save body in file..." gnus-summary-save-article-body-file t]
2468               ["Pipe through a filter..." gnus-summary-pipe-output t]
2469               ["Print with Muttprint..." gnus-summary-muttprint t]
2470               ["Print" gnus-summary-print-article
2471                ,@(if (featurep 'xemacs) '(t)
2472                    '(:help "Generate and print a PostScript image"))])
2473              ("Copy, move,... (Backend)"
2474               ,@(if (featurep 'xemacs) nil
2475                   '(:help "Copying, moving, expiring articles..."))
2476               ["Respool article..." gnus-summary-respool-article t]
2477               ["Move article..." gnus-summary-move-article
2478                (gnus-check-backend-function
2479                 'request-move-article gnus-newsgroup-name)]
2480               ["Copy article..." gnus-summary-copy-article t]
2481               ["Crosspost article..." gnus-summary-crosspost-article
2482                (gnus-check-backend-function
2483                 'request-replace-article gnus-newsgroup-name)]
2484               ["Import file..." gnus-summary-import-article
2485                (gnus-check-backend-function
2486                 'request-accept-article gnus-newsgroup-name)]
2487               ["Create article..." gnus-summary-create-article
2488                (gnus-check-backend-function
2489                 'request-accept-article gnus-newsgroup-name)]
2490               ["Check if posted" gnus-summary-article-posted-p t]
2491               ["Edit article" gnus-summary-edit-article
2492                (not (gnus-group-read-only-p))]
2493               ["Delete article" gnus-summary-delete-article
2494                (gnus-check-backend-function
2495                 'request-expire-articles gnus-newsgroup-name)]
2496               ["Query respool" gnus-summary-respool-query t]
2497               ["Trace respool" gnus-summary-respool-trace t]
2498               ["Delete expirable articles" gnus-summary-expire-articles-now
2499                (gnus-check-backend-function
2500                 'request-expire-articles gnus-newsgroup-name)])
2501              ("Extract"
2502               ["Uudecode" gnus-uu-decode-uu
2503                ,@(if (featurep 'xemacs) '(t)
2504                    '(:help "Decode uuencoded article(s)"))]
2505               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2506               ["Unshar" gnus-uu-decode-unshar t]
2507               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2508               ["Save" gnus-uu-decode-save t]
2509               ["Binhex" gnus-uu-decode-binhex t]
2510               ["Postscript" gnus-uu-decode-postscript t]
2511               ["All MIME parts" gnus-summary-save-parts t])
2512              ("Cache"
2513               ["Enter article" gnus-cache-enter-article t]
2514               ["Remove article" gnus-cache-remove-article t])
2515              ["Translate" gnus-article-babel t]
2516              ["Select article buffer" gnus-summary-select-article-buffer t]
2517              ["Make article buffer sticky" gnus-sticky-article t]
2518              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2519              ["Isearch article..." gnus-summary-isearch-article t]
2520              ["Beginning of the article" gnus-summary-beginning-of-article t]
2521              ["End of the article" gnus-summary-end-of-article t]
2522              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2523              ["Fetch referenced articles" gnus-summary-refer-references t]
2524              ["Fetch current thread" gnus-summary-refer-thread t]
2525              ["Fetch article with id..." gnus-summary-refer-article t]
2526              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2527              ["Redisplay" gnus-summary-show-article t]
2528              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2529       (easy-menu-define
2530         gnus-summary-article-menu gnus-summary-mode-map ""
2531         (cons "Article" innards))
2532
2533       (if (not (keymapp gnus-summary-article-menu))
2534           (easy-menu-define
2535             gnus-article-commands-menu gnus-article-mode-map ""
2536             (cons "Commands" innards))
2537         ;; in Emacs, don't share menu.
2538         (setq gnus-article-commands-menu
2539               (copy-keymap gnus-summary-article-menu))
2540         (define-key gnus-article-mode-map [menu-bar commands]
2541           (cons "Commands" gnus-article-commands-menu))))
2542
2543     (easy-menu-define
2544       gnus-summary-thread-menu gnus-summary-mode-map ""
2545       '("Threads"
2546         ["Find all messages in thread" gnus-summary-refer-thread t]
2547         ["Toggle threading" gnus-summary-toggle-threads t]
2548         ["Hide threads" gnus-summary-hide-all-threads t]
2549         ["Show threads" gnus-summary-show-all-threads t]
2550         ["Hide thread" gnus-summary-hide-thread t]
2551         ["Show thread" gnus-summary-show-thread t]
2552         ["Go to next thread" gnus-summary-next-thread t]
2553         ["Go to previous thread" gnus-summary-prev-thread t]
2554         ["Go down thread" gnus-summary-down-thread t]
2555         ["Go up thread" gnus-summary-up-thread t]
2556         ["Top of thread" gnus-summary-top-thread t]
2557         ["Mark thread as read" gnus-summary-kill-thread t]
2558         ["Mark thread as expired" gnus-summary-expire-thread t]
2559         ["Lower thread score" gnus-summary-lower-thread t]
2560         ["Raise thread score" gnus-summary-raise-thread t]
2561         ["Rethread current" gnus-summary-rethread-current t]))
2562
2563     (easy-menu-define
2564       gnus-summary-post-menu gnus-summary-mode-map ""
2565       `("Post"
2566         ["Send a message (mail or news)" gnus-summary-post-news
2567          ,@(if (featurep 'xemacs) '(t)
2568              '(:help "Compose a new message (mail or news)"))]
2569         ["Followup" gnus-summary-followup
2570          ,@(if (featurep 'xemacs) '(t)
2571              '(:help "Post followup to this article"))]
2572         ["Followup and yank" gnus-summary-followup-with-original
2573          ,@(if (featurep 'xemacs) '(t)
2574              '(:help "Post followup to this article, quoting its contents"))]
2575         ["Supersede article" gnus-summary-supersede-article t]
2576         ["Cancel article" gnus-summary-cancel-article
2577          ,@(if (featurep 'xemacs) '(t)
2578              '(:help "Cancel an article you posted"))]
2579         ["Reply" gnus-summary-reply t]
2580         ["Reply and yank" gnus-summary-reply-with-original t]
2581         ["Wide reply" gnus-summary-wide-reply t]
2582         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2583          ,@(if (featurep 'xemacs) '(t)
2584              '(:help "Mail a reply, quoting this article"))]
2585         ["Very wide reply" gnus-summary-very-wide-reply t]
2586         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2587          ,@(if (featurep 'xemacs) '(t)
2588              '(:help "Mail a very wide reply, quoting this article"))]
2589         ["Mail forward" gnus-summary-mail-forward t]
2590         ["Post forward" gnus-summary-post-forward t]
2591         ["Digest and mail" gnus-uu-digest-mail-forward t]
2592         ["Digest and post" gnus-uu-digest-post-forward t]
2593         ["Resend message" gnus-summary-resend-message t]
2594         ["Resend message edit" gnus-summary-resend-message-edit t]
2595         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2596         ["Send a mail" gnus-summary-mail-other-window t]
2597         ["Create a local message" gnus-summary-news-other-window t]
2598         ["Uuencode and post" gnus-uu-post-news
2599          ,@(if (featurep 'xemacs) '(t)
2600              '(:help "Post a uuencoded article"))]
2601         ["Followup via news" gnus-summary-followup-to-mail t]
2602         ["Followup via news and yank"
2603          gnus-summary-followup-to-mail-with-original t]
2604         ["Strip signature on reply"
2605          (lambda ()
2606            (interactive)
2607            (if (not (memq message-cite-function
2608                           '(message-cite-original-without-signature
2609                             message-cite-original)))
2610                ;; Stupid workaround for XEmacs not honoring :visible.
2611                (message "Can't toggle this value of `message-cite-function'")
2612              (setq message-cite-function
2613                    (if (eq message-cite-function
2614                            'message-cite-original-without-signature)
2615                        'message-cite-original
2616                      'message-cite-original-without-signature))))
2617          ;; XEmacs barfs on :visible.
2618          ,@(if (featurep 'xemacs) nil
2619              '(:visible (memq message-cite-function
2620                               '(message-cite-original-without-signature
2621                                 message-cite-original))))
2622          :style toggle
2623          :selected (eq message-cite-function
2624                        'message-cite-original-without-signature)
2625          ,@(if (featurep 'xemacs) nil
2626              '(:help "Strip signature from cited article when replying."))]
2627         ;;("Draft"
2628         ;;["Send" gnus-summary-send-draft t]
2629         ;;["Send bounced" gnus-resend-bounced-mail t])
2630         ))
2631
2632     (cond
2633      ((not (keymapp gnus-summary-post-menu))
2634       (setq gnus-article-post-menu gnus-summary-post-menu))
2635      ((not gnus-article-post-menu)
2636       ;; Don't share post menu.
2637       (setq gnus-article-post-menu
2638             (copy-keymap gnus-summary-post-menu))))
2639     (define-key gnus-article-mode-map [menu-bar post]
2640       (cons "Post" gnus-article-post-menu))
2641
2642     (easy-menu-define
2643       gnus-summary-misc-menu gnus-summary-mode-map ""
2644       `("Gnus"
2645         ("Mark Read"
2646          ["Mark as read" gnus-summary-mark-as-read-forward t]
2647          ["Mark same subject and select"
2648           gnus-summary-kill-same-subject-and-select t]
2649          ["Mark same subject" gnus-summary-kill-same-subject t]
2650          ["Catchup" gnus-summary-catchup
2651           ,@(if (featurep 'xemacs) '(t)
2652               '(:help "Mark unread articles in this group as read"))]
2653          ["Catchup all" gnus-summary-catchup-all t]
2654          ["Catchup to here" gnus-summary-catchup-to-here t]
2655          ["Catchup from here" gnus-summary-catchup-from-here t]
2656          ["Catchup region" gnus-summary-mark-region-as-read
2657           (gnus-mark-active-p)]
2658          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2659         ("Mark Various"
2660          ["Tick" gnus-summary-tick-article-forward t]
2661          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2662          ["Remove marks" gnus-summary-clear-mark-forward t]
2663          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2664          ["Set bookmark" gnus-summary-set-bookmark t]
2665          ["Remove bookmark" gnus-summary-remove-bookmark t])
2666         ("Limit to"
2667          ["Marks..." gnus-summary-limit-to-marks t]
2668          ["Subject..." gnus-summary-limit-to-subject t]
2669          ["Author..." gnus-summary-limit-to-author t]
2670          ["Recipient..." gnus-summary-limit-to-recipient t]
2671          ["Address..." gnus-summary-limit-to-address t]
2672          ["Age..." gnus-summary-limit-to-age t]
2673          ["Extra..." gnus-summary-limit-to-extra t]
2674          ["Score..." gnus-summary-limit-to-score t]
2675          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2676          ["Unread" gnus-summary-limit-to-unread t]
2677          ["Unseen" gnus-summary-limit-to-unseen t]
2678          ["Singletons" gnus-summary-limit-to-singletons t]
2679          ["Replied" gnus-summary-limit-to-replied t]
2680          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2681          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2682          ["Pop limit" gnus-summary-pop-limit t]
2683          ["Show dormant" gnus-summary-limit-include-dormant t]
2684          ["Hide childless dormant"
2685           gnus-summary-limit-exclude-childless-dormant t]
2686          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2687          ["Hide marked" gnus-summary-limit-exclude-marks t]
2688          ["Show expunged" gnus-summary-limit-include-expunged t])
2689         ("Process Mark"
2690          ["Set mark" gnus-summary-mark-as-processable t]
2691          ["Remove mark" gnus-summary-unmark-as-processable t]
2692          ["Remove all marks" gnus-summary-unmark-all-processable t]
2693          ["Invert marks" gnus-uu-invert-processable t]
2694          ["Mark above" gnus-uu-mark-over t]
2695          ["Mark series" gnus-uu-mark-series t]
2696          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2697          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2698          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2699          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2700          ["Mark all" gnus-uu-mark-all t]
2701          ["Mark buffer" gnus-uu-mark-buffer t]
2702          ["Mark sparse" gnus-uu-mark-sparse t]
2703          ["Mark thread" gnus-uu-mark-thread t]
2704          ["Unmark thread" gnus-uu-unmark-thread t]
2705          ("Process Mark Sets"
2706           ["Kill" gnus-summary-kill-process-mark t]
2707           ["Yank" gnus-summary-yank-process-mark
2708            gnus-newsgroup-process-stack]
2709           ["Save" gnus-summary-save-process-mark t]
2710           ["Run command on marked..." gnus-summary-universal-argument t]))
2711         ("Registry Marks")
2712         ("Scroll article"
2713          ["Page forward" gnus-summary-next-page
2714           ,@(if (featurep 'xemacs) '(t)
2715               '(:help "Show next page of article"))]
2716          ["Page backward" gnus-summary-prev-page
2717           ,@(if (featurep 'xemacs) '(t)
2718               '(:help "Show previous page of article"))]
2719          ["Line forward" gnus-summary-scroll-up t])
2720         ("Move"
2721          ["Next unread article" gnus-summary-next-unread-article t]
2722          ["Previous unread article" gnus-summary-prev-unread-article t]
2723          ["Next article" gnus-summary-next-article t]
2724          ["Previous article" gnus-summary-prev-article t]
2725          ["Next unread subject" gnus-summary-next-unread-subject t]
2726          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2727          ["Next article same subject" gnus-summary-next-same-subject t]
2728          ["Previous article same subject" gnus-summary-prev-same-subject t]
2729          ["First unread article" gnus-summary-first-unread-article t]
2730          ["Best unread article" gnus-summary-best-unread-article t]
2731          ["Go to subject number..." gnus-summary-goto-subject t]
2732          ["Go to article number..." gnus-summary-goto-article t]
2733          ["Go to the last article" gnus-summary-goto-last-article t]
2734          ["Pop article off history" gnus-summary-pop-article t])
2735         ("Sort"
2736          ["Sort by number" gnus-summary-sort-by-number t]
2737          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2738          ["Sort by author" gnus-summary-sort-by-author t]
2739          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2740          ["Sort by subject" gnus-summary-sort-by-subject t]
2741          ["Sort by date" gnus-summary-sort-by-date t]
2742          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2743          ["Sort by score" gnus-summary-sort-by-score t]
2744          ["Sort by lines" gnus-summary-sort-by-lines t]
2745          ["Sort by characters" gnus-summary-sort-by-chars t]
2746          ["Randomize" gnus-summary-sort-by-random t]
2747          ["Original sort" gnus-summary-sort-by-original t])
2748         ("Help"
2749          ["Describe group" gnus-summary-describe-group t]
2750          ["Fetch control message" gnus-group-fetch-control
2751           ,@(if (featurep 'xemacs) nil
2752               '(:help "Display the archived control message for the current group"))]
2753          ["Read manual" gnus-info-find-node t])
2754         ("Modes"
2755          ["Pick and read" gnus-pick-mode t]
2756          ["Binary" gnus-binary-mode t])
2757         ("Regeneration"
2758          ["Regenerate" gnus-summary-prepare t]
2759          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2760          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2761          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2762          ["Toggle threading" gnus-summary-toggle-threads t])
2763         ["See old articles" gnus-summary-insert-old-articles t]
2764         ["See new articles" gnus-summary-insert-new-articles t]
2765         ["Filter articles..." gnus-summary-execute-command t]
2766         ["Run command on articles..." gnus-summary-universal-argument t]
2767         ["Search articles forward..." gnus-summary-search-article-forward t]
2768         ["Search articles backward..." gnus-summary-search-article-backward t]
2769         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2770         ["Expand window" gnus-summary-expand-window t]
2771         ["Expire expirable articles" gnus-summary-expire-articles
2772          (gnus-check-backend-function
2773           'request-expire-articles gnus-newsgroup-name)]
2774         ["Edit local kill file" gnus-summary-edit-local-kill t]
2775         ["Edit main kill file" gnus-summary-edit-global-kill t]
2776         ["Edit group parameters" gnus-summary-edit-parameters t]
2777         ["Customize group parameters" gnus-summary-customize-parameters t]
2778         ["Send a bug report" gnus-bug t]
2779         ("Exit"
2780          ["Catchup and exit" gnus-summary-catchup-and-exit
2781           ,@(if (featurep 'xemacs) '(t)
2782               '(:help "Mark unread articles in this group as read, then exit"))]
2783          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2784          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2785          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2786          ["Exit group" gnus-summary-exit
2787           ,@(if (featurep 'xemacs) '(t)
2788               '(:help "Exit current group, return to group selection mode"))]
2789          ["Exit group without updating" gnus-summary-exit-no-update t]
2790          ["Exit and goto next group" gnus-summary-next-group t]
2791          ["Exit and goto prev group" gnus-summary-prev-group t]
2792          ["Reselect group" gnus-summary-reselect-current-group t]
2793          ["Rescan group" gnus-summary-rescan-group t]
2794          ["Update dribble" gnus-summary-save-newsrc t])))
2795
2796     (gnus-run-hooks 'gnus-summary-menu-hook)))
2797
2798 (defvar gnus-summary-tool-bar-map nil)
2799
2800 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2801 ;; affect _new_ message buffers.  We might add a function that walks thru all
2802 ;; summary-mode buffers and force the update.
2803 (defun gnus-summary-tool-bar-update (&optional symbol value)
2804   "Update summary mode toolbar.
2805 Setter function for custom variables."
2806   (setq-default gnus-summary-tool-bar-map nil)
2807   (when symbol
2808     ;; When used as ":set" function:
2809     (set-default symbol value))
2810   (when (gnus-buffer-live-p gnus-summary-buffer)
2811     (with-current-buffer gnus-summary-buffer
2812       (gnus-summary-make-tool-bar))))
2813
2814 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2815                                      'gnus-summary-tool-bar-gnome
2816                                    'gnus-summary-tool-bar-retro)
2817   "Specifies the Gnus summary tool bar.
2818
2819 It can be either a list or a symbol refering to a list.  See
2820 `gmm-tool-bar-from-list' for the format of the list.  The
2821 default key map is `gnus-summary-mode-map'.
2822
2823 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2824 `gnus-summary-tool-bar-retro'."
2825   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2826                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2827                  (repeat :tag "User defined list" gmm-tool-bar-item)
2828                  (symbol))
2829   :version "23.1" ;; No Gnus
2830   :initialize 'custom-initialize-default
2831   :set 'gnus-summary-tool-bar-update
2832   :group 'gnus-summary)
2833
2834 (defcustom gnus-summary-tool-bar-gnome
2835   '((gnus-summary-post-news "mail/compose" nil)
2836     (gnus-summary-insert-new-articles "mail/inbox" nil
2837                                       :visible (or (not gnus-agent)
2838                                                    gnus-plugged))
2839     (gnus-summary-reply-with-original "mail/reply")
2840     (gnus-summary-reply "mail/reply" nil :visible nil)
2841     (gnus-summary-followup-with-original "mail/reply-all")
2842     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2843     (gnus-summary-mail-forward "mail/forward")
2844     (gnus-summary-save-article "mail/save")
2845     (gnus-summary-search-article-forward "search" nil :visible nil)
2846     (gnus-summary-print-article "print")
2847     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2848     ;; Some new commands that may need more suitable icons:
2849     (gnus-summary-save-newsrc "save" nil :visible nil)
2850     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2851     (gnus-summary-prev-article "left-arrow")
2852     (gnus-summary-next-article "right-arrow")
2853     (gnus-summary-next-page "next-page")
2854     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2855     ;;
2856     ;; Maybe some sort-by-... could be added:
2857     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2858     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2859     (gnus-summary-mark-as-expirable
2860      "delete" nil
2861      :visible (gnus-check-backend-function 'request-expire-articles
2862                                            gnus-newsgroup-name))
2863     (gnus-summary-mark-as-spam
2864      "mail/spam" t
2865      :visible (and (fboundp 'spam-group-ham-contents-p)
2866                    (spam-group-ham-contents-p gnus-newsgroup-name))
2867      :help "Mark as spam")
2868     (gnus-summary-mark-as-read-forward
2869      "mail/not-spam" nil
2870      :visible (and (fboundp 'spam-group-spam-contents-p)
2871                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2872     ;;
2873     (gnus-summary-exit "exit")
2874     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2875     (gnus-info-find-node "help"))
2876   "List of functions for the summary tool bar (GNOME style).
2877
2878 See `gmm-tool-bar-from-list' for the format of the list."
2879   :type '(repeat gmm-tool-bar-item)
2880   :version "23.1" ;; No Gnus
2881   :initialize 'custom-initialize-default
2882   :set 'gnus-summary-tool-bar-update
2883   :group 'gnus-summary)
2884
2885 (defcustom gnus-summary-tool-bar-retro
2886   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2887     (gnus-summary-next-unread-article "gnus/next-ur")
2888     (gnus-summary-post-news "gnus/post")
2889     (gnus-summary-followup-with-original "gnus/fuwo")
2890     (gnus-summary-followup "gnus/followup")
2891     (gnus-summary-reply-with-original "gnus/reply-wo")
2892     (gnus-summary-reply "gnus/reply")
2893     (gnus-summary-caesar-message "gnus/rot13")
2894     (gnus-uu-decode-uu "gnus/uu-decode")
2895     (gnus-summary-save-article-file "gnus/save-aif")
2896     (gnus-summary-save-article "gnus/save-art")
2897     (gnus-uu-post-news "gnus/uu-post")
2898     (gnus-summary-catchup "gnus/catchup")
2899     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2900     (gnus-summary-exit "gnus/exit-summ")
2901     ;; Some new command that may need more suitable icons:
2902     (gnus-summary-print-article "gnus/print" nil :visible nil)
2903     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2904     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2905     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2906     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2907     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2908     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2909     ;;
2910     (gnus-info-find-node "gnus/help" nil :visible nil))
2911   "List of functions for the summary tool bar (retro look).
2912
2913 See `gmm-tool-bar-from-list' for the format of the list."
2914   :type '(repeat gmm-tool-bar-item)
2915   :version "23.1" ;; No Gnus
2916   :initialize 'custom-initialize-default
2917   :set 'gnus-summary-tool-bar-update
2918   :group 'gnus-summary)
2919
2920 (defcustom gnus-summary-tool-bar-zap-list t
2921   "List of icon items from the global tool bar.
2922 These items are not displayed in the Gnus summary mode tool bar.
2923
2924 See `gmm-tool-bar-from-list' for the format of the list."
2925   :type 'gmm-tool-bar-zap-list
2926   :version "23.1" ;; No Gnus
2927   :initialize 'custom-initialize-default
2928   :set 'gnus-summary-tool-bar-update
2929   :group 'gnus-summary)
2930
2931 (defvar image-load-path)
2932 (defvar tool-bar-map)
2933
2934 (defun gnus-summary-make-tool-bar (&optional force)
2935   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2936 When FORCE, rebuild the tool bar."
2937   (when (and (not (featurep 'xemacs))
2938              (boundp 'tool-bar-mode)
2939              tool-bar-mode
2940              (or (not gnus-summary-tool-bar-map) force))
2941     (let* ((load-path
2942             (gmm-image-load-path-for-library "gnus"
2943                                              "mail/save.xpm"
2944                                              nil t))
2945            (image-load-path (cons (car load-path)
2946                                   (when (boundp 'image-load-path)
2947                                     image-load-path)))
2948            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2949                                         gnus-summary-tool-bar-zap-list
2950                                         'gnus-summary-mode-map)))
2951       (when map
2952         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2953         ;; uses it's value.
2954         (setq gnus-summary-tool-bar-map map))))
2955   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2956
2957 (defun gnus-score-set-default (var value)
2958   "A version of set that updates the GNU Emacs menu-bar."
2959   (set var value)
2960   ;; It is the message that forces the active status to be updated.
2961   (message ""))
2962
2963 (defun gnus-make-score-map (type)
2964   "Make a summary score map of type TYPE."
2965   (if t
2966       nil
2967     (let ((headers '(("author" "from" string)
2968                      ("subject" "subject" string)
2969                      ("article body" "body" string)
2970                      ("article head" "head" string)
2971                      ("xref" "xref" string)
2972                      ("extra header" "extra" string)
2973                      ("lines" "lines" number)
2974                      ("followups to author" "followup" string)))
2975           (types '((number ("less than" <)
2976                            ("greater than" >)
2977                            ("equal" =))
2978                    (string ("substring" s)
2979                            ("exact string" e)
2980                            ("fuzzy string" f)
2981                            ("regexp" r))))
2982           (perms '(("temporary" (current-time-string))
2983                    ("permanent" nil)
2984                    ("immediate" now)))
2985           header)
2986       (list
2987        (apply
2988         'nconc
2989         (list
2990          (if (eq type 'lower)
2991              "Lower score"
2992            "Increase score"))
2993         (let (outh)
2994           (while headers
2995             (setq header (car headers))
2996             (setq outh
2997                   (cons
2998                    (apply
2999                     'nconc
3000                     (list (car header))
3001                     (let ((ts (cdr (assoc (nth 2 header) types)))
3002                           outt)
3003                       (while ts
3004                         (setq outt
3005                               (cons
3006                                (apply
3007                                 'nconc
3008                                 (list (caar ts))
3009                                 (let ((ps perms)
3010                                       outp)
3011                                   (while ps
3012                                     (setq outp
3013                                           (cons
3014                                            (vector
3015                                             (caar ps)
3016                                             (list
3017                                              'gnus-summary-score-entry
3018                                              (nth 1 header)
3019                                              (if (or (string= (nth 1 header)
3020                                                               "head")
3021                                                      (string= (nth 1 header)
3022                                                               "body"))
3023                                                  ""
3024                                                (list 'gnus-summary-header
3025                                                      (nth 1 header)))
3026                                              (list 'quote (nth 1 (car ts)))
3027                                              (list 'gnus-score-delta-default
3028                                                    nil)
3029                                              (nth 1 (car ps))
3030                                              t)
3031                                             t)
3032                                            outp))
3033                                     (setq ps (cdr ps)))
3034                                   (list (nreverse outp))))
3035                                outt))
3036                         (setq ts (cdr ts)))
3037                       (list (nreverse outt))))
3038                    outh))
3039             (setq headers (cdr headers)))
3040           (list (nreverse outh))))))))
3041
3042
3043 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3044 (defvar bookmark-make-record-function)
3045 \f
3046
3047 (defun gnus-summary-mode (&optional group)
3048   "Major mode for reading articles.
3049
3050 All normal editing commands are switched off.
3051 \\<gnus-summary-mode-map>
3052 Each line in this buffer represents one article.  To read an
3053 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3054 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3055 respectively.
3056
3057 You can also post articles and send mail from this buffer.  To
3058 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3059 of an article, type `\\[gnus-summary-reply]'.
3060
3061 There are approx. one gazillion commands you can execute in this
3062 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3063
3064 The following commands are available:
3065
3066 \\{gnus-summary-mode-map}"
3067   (interactive)
3068   (kill-all-local-variables)
3069   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3070     (gnus-summary-make-local-variables))
3071   (gnus-summary-make-local-variables)
3072   (setq gnus-newsgroup-name group)
3073   (when (gnus-visual-p 'summary-menu 'menu)
3074     (gnus-summary-make-menu-bar)
3075     (gnus-summary-make-tool-bar))
3076   (gnus-make-thread-indent-array)
3077   (gnus-simplify-mode-line)
3078   (setq major-mode 'gnus-summary-mode)
3079   (setq mode-name "Summary")
3080   (use-local-map gnus-summary-mode-map)
3081   (buffer-disable-undo)
3082   (setq buffer-read-only t              ;Disable modification
3083         show-trailing-whitespace nil)
3084   (setq truncate-lines t)
3085   (add-to-invisibility-spec '(gnus-sum . t))
3086   (gnus-summary-set-display-table)
3087   (gnus-set-default-directory)
3088   (make-local-variable 'gnus-summary-line-format)
3089   (make-local-variable 'gnus-summary-line-format-spec)
3090   (make-local-variable 'gnus-summary-dummy-line-format)
3091   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3092   (make-local-variable 'gnus-summary-mark-positions)
3093   (gnus-make-local-hook 'pre-command-hook)
3094   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3095   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3096   (turn-on-gnus-mailing-list-mode)
3097   (mm-enable-multibyte)
3098   (set (make-local-variable 'bookmark-make-record-function)
3099        'gnus-summary-bookmark-make-record)
3100   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3101   (gnus-update-summary-mark-positions))
3102
3103 (defun gnus-summary-make-local-variables ()
3104   "Make all the local summary buffer variables."
3105   (let (global)
3106     (dolist (local gnus-summary-local-variables)
3107       (if (consp local)
3108           (progn
3109             (if (eq (cdr local) 'global)
3110                 ;; Copy the global value of the variable.
3111                 (setq global (symbol-value (car local)))
3112               ;; Use the value from the list.
3113               (setq global (eval (cdr local))))
3114             (set (make-local-variable (car local)) global))
3115         ;; Simple nil-valued local variable.
3116         (set (make-local-variable local) nil)))))
3117
3118 ;; Summary data functions.
3119
3120 (defmacro gnus-data-number (data)
3121   `(car ,data))
3122
3123 (defmacro gnus-data-set-number (data number)
3124   `(setcar ,data ,number))
3125
3126 (defmacro gnus-data-mark (data)
3127   `(nth 1 ,data))
3128
3129 (defmacro gnus-data-set-mark (data mark)
3130   `(setcar (nthcdr 1 ,data) ,mark))
3131
3132 (defmacro gnus-data-pos (data)
3133   `(nth 2 ,data))
3134
3135 (defmacro gnus-data-set-pos (data pos)
3136   `(setcar (nthcdr 2 ,data) ,pos))
3137
3138 (defmacro gnus-data-header (data)
3139   `(nth 3 ,data))
3140
3141 (defmacro gnus-data-set-header (data header)
3142   `(setf (nth 3 ,data) ,header))
3143
3144 (defmacro gnus-data-level (data)
3145   `(nth 4 ,data))
3146
3147 (defmacro gnus-data-unread-p (data)
3148   `(= (nth 1 ,data) gnus-unread-mark))
3149
3150 (defmacro gnus-data-read-p (data)
3151   `(/= (nth 1 ,data) gnus-unread-mark))
3152
3153 (defmacro gnus-data-pseudo-p (data)
3154   `(consp (nth 3 ,data)))
3155
3156 (defmacro gnus-data-find (number)
3157   `(assq ,number gnus-newsgroup-data))
3158
3159 (defmacro gnus-data-find-list (number &optional data)
3160   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3161      (memq (assq ,number bdata)
3162            bdata)))
3163
3164 (defmacro gnus-data-make (number mark pos header level)
3165   `(list ,number ,mark ,pos ,header ,level))
3166
3167 (defun gnus-data-enter (after-article number mark pos header level offset)
3168   (let ((data (gnus-data-find-list after-article)))
3169     (unless data
3170       (error "No such article: %d" after-article))
3171     (setcdr data (cons (gnus-data-make number mark pos header level)
3172                        (cdr data)))
3173     (setq gnus-newsgroup-data-reverse nil)
3174     (gnus-data-update-list (cddr data) offset)))
3175
3176 (defun gnus-data-enter-list (after-article list &optional offset)
3177   (when list
3178     (let ((data (and after-article (gnus-data-find-list after-article)))
3179           (ilist list))
3180       (if (not (or data
3181                    after-article))
3182           (let ((odata gnus-newsgroup-data))
3183             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3184             (when offset
3185               (gnus-data-update-list odata offset)))
3186         ;; Find the last element in the list to be spliced into the main
3187         ;; list.
3188         (setq list (last list))
3189         (if (not data)
3190             (progn
3191               (setcdr list gnus-newsgroup-data)
3192               (setq gnus-newsgroup-data ilist)
3193               (when offset
3194                 (gnus-data-update-list (cdr list) offset)))
3195           (setcdr list (cdr data))
3196           (setcdr data ilist)
3197           (when offset
3198             (gnus-data-update-list (cdr list) offset))))
3199       (setq gnus-newsgroup-data-reverse nil))))
3200
3201 (defun gnus-data-remove (article &optional offset)
3202   (let ((data gnus-newsgroup-data))
3203     (if (= (gnus-data-number (car data)) article)
3204         (progn
3205           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3206                 gnus-newsgroup-data-reverse nil)
3207           (when offset
3208             (gnus-data-update-list gnus-newsgroup-data offset)))
3209       (while (cdr data)
3210         (when (= (gnus-data-number (cadr data)) article)
3211           (setcdr data (cddr data))
3212           (when offset
3213             (gnus-data-update-list (cdr data) offset))
3214           (setq data nil
3215                 gnus-newsgroup-data-reverse nil))
3216         (setq data (cdr data))))))
3217
3218 (defmacro gnus-data-list (backward)
3219   `(if ,backward
3220        (or gnus-newsgroup-data-reverse
3221            (setq gnus-newsgroup-data-reverse
3222                  (reverse gnus-newsgroup-data)))
3223      gnus-newsgroup-data))
3224
3225 (defun gnus-data-update-list (data offset)
3226   "Add OFFSET to the POS of all data entries in DATA."
3227   (setq gnus-newsgroup-data-reverse nil)
3228   (while data
3229     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3230     (setq data (cdr data))))
3231
3232 (defun gnus-summary-article-pseudo-p (article)
3233   "Say whether this article is a pseudo article or not."
3234   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3235
3236 (defmacro gnus-summary-article-sparse-p (article)
3237   "Say whether this article is a sparse article or not."
3238   `(memq ,article gnus-newsgroup-sparse))
3239
3240 (defmacro gnus-summary-article-ancient-p (article)
3241   "Say whether this article is a sparse article or not."
3242   `(memq ,article gnus-newsgroup-ancient))
3243
3244 (defun gnus-article-parent-p (number)
3245   "Say whether this article is a parent or not."
3246   (let ((data (gnus-data-find-list number)))
3247     (and (cdr data)              ; There has to be an article after...
3248          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3249             (gnus-data-level (nth 1 data))))))
3250
3251 (defun gnus-article-children (number)
3252   "Return a list of all children to NUMBER."
3253   (let* ((data (gnus-data-find-list number))
3254          (level (gnus-data-level (car data)))
3255          children)
3256     (setq data (cdr data))
3257     (while (and data
3258                 (= (gnus-data-level (car data)) (1+ level)))
3259       (push (gnus-data-number (car data)) children)
3260       (setq data (cdr data)))
3261     children))
3262
3263 (defmacro gnus-summary-skip-intangible ()
3264   "If the current article is intangible, then jump to a different article."
3265   '(let ((to (get-text-property (point) 'gnus-intangible)))
3266      (and to (gnus-summary-goto-subject to))))
3267
3268 (defmacro gnus-summary-article-intangible-p ()
3269   "Say whether this article is intangible or not."
3270   '(get-text-property (point) 'gnus-intangible))
3271
3272 (defun gnus-article-read-p (article)
3273   "Say whether ARTICLE is read or not."
3274   (not (or (memq article gnus-newsgroup-marked)
3275            (memq article gnus-newsgroup-spam-marked)
3276            (memq article gnus-newsgroup-unreads)
3277            (memq article gnus-newsgroup-unselected)
3278            (memq article gnus-newsgroup-dormant))))
3279
3280 ;; Some summary mode macros.
3281
3282 (defmacro gnus-summary-article-number ()
3283   "The article number of the article on the current line.
3284 If there isn't an article number here, then we return the current
3285 article number."
3286   '(progn
3287      (gnus-summary-skip-intangible)
3288      (or (get-text-property (point) 'gnus-number)
3289          (gnus-summary-last-subject))))
3290
3291 (defmacro gnus-summary-article-header (&optional number)
3292   "Return the header of article NUMBER."
3293   `(gnus-data-header (gnus-data-find
3294                       ,(or number '(gnus-summary-article-number)))))
3295
3296 (defmacro gnus-summary-thread-level (&optional number)
3297   "Return the level of thread that starts with article NUMBER."
3298   `(if (and (eq gnus-summary-make-false-root 'dummy)
3299             (get-text-property (point) 'gnus-intangible))
3300        0
3301      (gnus-data-level (gnus-data-find
3302                        ,(or number '(gnus-summary-article-number))))))
3303
3304 (defmacro gnus-summary-article-mark (&optional number)
3305   "Return the mark of article NUMBER."
3306   `(gnus-data-mark (gnus-data-find
3307                     ,(or number '(gnus-summary-article-number)))))
3308
3309 (defmacro gnus-summary-article-pos (&optional number)
3310   "Return the position of the line of article NUMBER."
3311   `(gnus-data-pos (gnus-data-find
3312                    ,(or number '(gnus-summary-article-number)))))
3313
3314 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3315 (defmacro gnus-summary-article-subject (&optional number)
3316   "Return current subject string or nil if nothing."
3317   `(let ((headers
3318           ,(if number
3319                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3320              '(gnus-data-header (assq (gnus-summary-article-number)
3321                                       gnus-newsgroup-data)))))
3322      (and headers
3323           (vectorp headers)
3324           (mail-header-subject headers))))
3325
3326 (defmacro gnus-summary-article-score (&optional number)
3327   "Return current article score."
3328   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3329                   gnus-newsgroup-scored))
3330        gnus-summary-default-score 0))
3331
3332 (defun gnus-summary-article-children (&optional number)
3333   "Return a list of article numbers that are children of article NUMBER."
3334   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3335          (level (gnus-data-level (car data)))
3336          l children)
3337     (while (and (setq data (cdr data))
3338                 (> (setq l (gnus-data-level (car data))) level))
3339       (and (= (1+ level) l)
3340            (push (gnus-data-number (car data))
3341                  children)))
3342     (nreverse children)))
3343
3344 (defun gnus-summary-article-parent (&optional number)
3345   "Return the article number of the parent of article NUMBER."
3346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3347                                     (gnus-data-list t)))
3348          (level (gnus-data-level (car data))))
3349     (if (zerop level)
3350         ()                              ; This is a root.
3351       ;; We search until we find an article with a level less than
3352       ;; this one.  That function has to be the parent.
3353       (while (and (setq data (cdr data))
3354                   (not (< (gnus-data-level (car data)) level))))
3355       (and data (gnus-data-number (car data))))))
3356
3357 (defun gnus-unread-mark-p (mark)
3358   "Say whether MARK is the unread mark."
3359   (= mark gnus-unread-mark))
3360
3361 (defun gnus-read-mark-p (mark)
3362   "Say whether MARK is one of the marks that mark as read.
3363 This is all marks except unread, ticked, dormant, and expirable."
3364   (not (or (= mark gnus-unread-mark)
3365            (= mark gnus-ticked-mark)
3366            (= mark gnus-spam-mark)
3367            (= mark gnus-dormant-mark)
3368            (= mark gnus-expirable-mark))))
3369
3370 (defmacro gnus-article-mark (number)
3371   "Return the MARK of article NUMBER.
3372 This macro should only be used when computing the mark the \"first\"
3373 time; i.e., when generating the summary lines.  After that,
3374 `gnus-summary-article-mark' should be used to examine the
3375 marks of articles."
3376   `(cond
3377     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3378     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3379     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3380     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3381     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3382     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3383     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3384     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3385            gnus-ancient-mark))))
3386
3387 ;; Saving hidden threads.
3388
3389 (defmacro gnus-save-hidden-threads (&rest forms)
3390   "Save hidden threads, eval FORMS, and restore the hidden threads."
3391   (let ((config (make-symbol "config")))
3392     `(let ((,config (gnus-hidden-threads-configuration)))
3393        (unwind-protect
3394            (save-excursion
3395              ,@forms)
3396          (gnus-restore-hidden-threads-configuration ,config)))))
3397 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3398 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3399
3400 (defun gnus-data-compute-positions ()
3401   "Compute the positions of all articles."
3402   (setq gnus-newsgroup-data-reverse nil)
3403   (let ((data gnus-newsgroup-data))
3404     (save-excursion
3405       (gnus-save-hidden-threads
3406         (gnus-summary-show-all-threads)
3407         (goto-char (point-min))
3408         (while data
3409           (while (get-text-property (point) 'gnus-intangible)
3410             (forward-line 1))
3411           (gnus-data-set-pos (car data) (+ (point) 3))
3412           (setq data (cdr data))
3413           (forward-line 1))))))
3414
3415 (defun gnus-hidden-threads-configuration ()
3416   "Return the current hidden threads configuration."
3417   (save-excursion
3418     (let (config)
3419       (goto-char (point-min))
3420       (while (not (eobp))
3421         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3422           (push (save-excursion (forward-line 0) (point)) config))
3423         (forward-line 1))
3424       config)))
3425
3426 (defun gnus-restore-hidden-threads-configuration (config)
3427   "Restore hidden threads configuration from CONFIG."
3428   (save-excursion
3429     (let (point (inhibit-read-only t))
3430       (while (setq point (pop config))
3431         (goto-char point)
3432         (gnus-summary-hide-thread)))))
3433
3434 ;; Various summary mode internalish functions.
3435
3436 (defun gnus-mouse-pick-article (e)
3437   (interactive "e")
3438   (mouse-set-point e)
3439   (gnus-summary-next-page nil t))
3440
3441 (defun gnus-summary-set-display-table ()
3442   "Change the display table.
3443 Odd characters have a tendency to mess
3444 up nicely formatted displays - we make all possible glyphs
3445 display only a single character."
3446
3447   ;; We start from the standard display table, if any.
3448   (let ((table (or (copy-sequence standard-display-table)
3449                    (make-display-table)))
3450         (i 32))
3451     ;; Nix out all the control chars...
3452     (while (>= (setq i (1- i)) 0)
3453       (gnus-put-display-table i [??] table))
3454    ;; ... but not newline and cr, of course.  (cr is necessary for the
3455     ;; selective display).
3456     (gnus-put-display-table ?\n nil table)
3457     (gnus-put-display-table ?\r nil table)
3458     ;; We keep TAB as well.
3459     (gnus-put-display-table ?\t nil table)
3460     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3461     ;; Emacs 23 (unicode), that are not set already.
3462     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3463                  160
3464                256)))
3465       (while (>= (setq i (1- i)) 127)
3466         ;; Only modify if the entry is nil.
3467         (unless (gnus-get-display-table i table)
3468           (gnus-put-display-table i [??] table))))
3469     (setq buffer-display-table table)))
3470
3471 (defun gnus-summary-set-article-display-arrow (pos)
3472   "Update the overlay arrow to point to line at position POS."
3473   (when gnus-summary-display-arrow
3474     (make-local-variable 'overlay-arrow-position)
3475     (make-local-variable 'overlay-arrow-string)
3476     (save-excursion
3477       (goto-char pos)
3478       (beginning-of-line)
3479       (unless overlay-arrow-position
3480         (setq overlay-arrow-position (make-marker)))
3481       (setq overlay-arrow-string "=>"
3482             overlay-arrow-position (set-marker overlay-arrow-position
3483                                                (point)
3484                                                (current-buffer))))))
3485
3486 (defun gnus-summary-setup-buffer (group)
3487   "Initialize summary buffer."
3488   (let ((buffer (gnus-summary-buffer-name group))
3489         (dead-name (concat "*Dead Summary "
3490                            (gnus-group-decoded-name group) "*")))
3491     ;; If a dead summary buffer exists, we kill it.
3492     (when (gnus-buffer-live-p dead-name)
3493       (gnus-kill-buffer dead-name))
3494     (if (get-buffer buffer)
3495         (progn
3496           (set-buffer buffer)
3497           (setq gnus-summary-buffer (current-buffer))
3498           (not gnus-newsgroup-prepared))
3499       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3500       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3501       (gnus-summary-mode group)
3502       (when (gnus-group-quit-config group)
3503         (set (make-local-variable 'gnus-single-article-buffer) nil))
3504       (make-local-variable 'gnus-article-buffer)
3505       (make-local-variable 'gnus-article-current)
3506       (make-local-variable 'gnus-original-article-buffer)
3507       (setq gnus-newsgroup-name group)
3508       ;; Set any local variables in the group parameters.
3509       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3510       t)))
3511
3512 (defun gnus-set-global-variables ()
3513   "Set the global equivalents of the buffer-local variables.
3514 They are set to the latest values they had.  These reflect the summary
3515 buffer that was in action when the last article was fetched."
3516   (when (eq major-mode 'gnus-summary-mode)
3517     (setq gnus-summary-buffer (current-buffer))
3518     (let ((name gnus-newsgroup-name)
3519           (marked gnus-newsgroup-marked)
3520           (spam gnus-newsgroup-spam-marked)
3521           (unread gnus-newsgroup-unreads)
3522           (headers gnus-current-headers)
3523           (data gnus-newsgroup-data)
3524           (summary gnus-summary-buffer)
3525           (article-buffer gnus-article-buffer)
3526           (original gnus-original-article-buffer)
3527           (gac gnus-article-current)
3528           (reffed gnus-reffed-article-number)
3529           (score-file gnus-current-score-file)
3530           (default-charset gnus-newsgroup-charset)
3531           vlist)
3532       (let ((locals gnus-newsgroup-variables))
3533         (while locals
3534           (if (consp (car locals))
3535               (push (eval (caar locals)) vlist)
3536             (push (eval (car locals)) vlist))
3537           (setq locals (cdr locals)))
3538         (setq vlist (nreverse vlist)))
3539       (with-current-buffer gnus-group-buffer
3540         (setq gnus-newsgroup-name name
3541               gnus-newsgroup-marked marked
3542               gnus-newsgroup-spam-marked spam
3543               gnus-newsgroup-unreads unread
3544               gnus-current-headers headers
3545               gnus-newsgroup-data data
3546               gnus-article-current gac
3547               gnus-summary-buffer summary
3548               gnus-article-buffer article-buffer
3549               gnus-original-article-buffer original
3550               gnus-reffed-article-number reffed
3551               gnus-current-score-file score-file
3552               gnus-newsgroup-charset default-charset)
3553         (let ((locals gnus-newsgroup-variables))
3554           (while locals
3555             (if (consp (car locals))
3556                 (set (caar locals) (pop vlist))
3557               (set (car locals) (pop vlist)))
3558             (setq locals (cdr locals))))
3559         ;; The article buffer also has local variables.
3560         (when (gnus-buffer-live-p gnus-article-buffer)
3561           (set-buffer gnus-article-buffer)
3562           (setq gnus-summary-buffer summary))))))
3563
3564 (defun gnus-summary-article-unread-p (article)
3565   "Say whether ARTICLE is unread or not."
3566   (memq article gnus-newsgroup-unreads))
3567
3568 (defun gnus-summary-first-article-p (&optional article)
3569   "Return whether ARTICLE is the first article in the buffer."
3570   (if (not (setq article (or article (gnus-summary-article-number))))
3571       nil
3572     (eq article (caar gnus-newsgroup-data))))
3573
3574 (defun gnus-summary-last-article-p (&optional article)
3575   "Return whether ARTICLE is the last article in the buffer."
3576   (if (not (setq article (or article (gnus-summary-article-number))))
3577       ;; All non-existent numbers are the last article.  :-)
3578       t
3579     (not (cdr (gnus-data-find-list article)))))
3580
3581 (defun gnus-make-thread-indent-array (&optional n)
3582   (when (or n
3583             (progn (setq n 200) nil)
3584             (null gnus-thread-indent-array)
3585             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3586     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3587           gnus-thread-indent-array-level gnus-thread-indent-level)
3588     (while (>= n 0)
3589       (aset gnus-thread-indent-array n
3590             (make-string (* n gnus-thread-indent-level) ? ))
3591       (setq n (1- n)))))
3592
3593 (defun gnus-update-summary-mark-positions ()
3594   "Compute where the summary marks are to go."
3595   (save-excursion
3596     (when (gnus-buffer-exists-p gnus-summary-buffer)
3597       (set-buffer gnus-summary-buffer))
3598     (let ((spec gnus-summary-line-format-spec)
3599           pos)
3600       (save-excursion
3601         (gnus-set-work-buffer)
3602         (let ((gnus-tmp-unread ?Z)
3603               (gnus-replied-mark ?Z)
3604               (gnus-score-below-mark ?Z)
3605               (gnus-score-over-mark ?Z)
3606               (gnus-undownloaded-mark ?Z)
3607               (gnus-summary-line-format-spec spec)
3608               (gnus-newsgroup-downloadable '(0))
3609               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3610               case-fold-search ignores)
3611           ;; Here, all marks are bound to Z.
3612           (gnus-summary-insert-line header
3613                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3614           (goto-char (point-min))
3615           ;; Memorize the positions of the same characters as dummy marks.
3616           (while (re-search-forward "[A-D]" nil t)
3617             (push (point) ignores))
3618           (erase-buffer)
3619           ;; We use A-D as dummy marks in order to know column positions
3620           ;; where marks should be inserted.
3621           (setq gnus-tmp-unread ?A
3622                 gnus-replied-mark ?B
3623                 gnus-score-below-mark ?C
3624                 gnus-score-over-mark ?C
3625                 gnus-undownloaded-mark ?D)
3626           (gnus-summary-insert-line header
3627                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3628           ;; Ignore characters which aren't dummy marks.
3629           (dolist (p ignores)
3630             (delete-region (goto-char (1- p)) p)
3631             (insert ?Z))
3632           (goto-char (point-min))
3633           (setq pos (list (cons 'unread
3634                                 (and (search-forward "A" nil t)
3635                                      (- (point) (point-min) 1)))))
3636           (goto-char (point-min))
3637           (push (cons 'replied (and (search-forward "B" nil t)
3638                                     (- (point) (point-min) 1)))
3639                 pos)
3640           (goto-char (point-min))
3641           (push (cons 'score (and (search-forward "C" nil t)
3642                                   (- (point) (point-min) 1)))
3643                 pos)
3644           (goto-char (point-min))
3645           (push (cons 'download (and (search-forward "D" nil t)
3646                                      (- (point) (point-min) 1)))
3647                 pos)))
3648       (setq gnus-summary-mark-positions pos))))
3649
3650 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3651   "Insert a dummy root in the summary buffer."
3652   (beginning-of-line)
3653   (gnus-add-text-properties
3654    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3655    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3656
3657 (defun gnus-summary-extract-address-component (from)
3658   (or (car (funcall gnus-extract-address-components from))
3659       from))
3660
3661 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3662   (let ((mail-parse-charset gnus-newsgroup-charset)
3663         (ignored-from-addresses (gnus-ignored-from-addresses))
3664         ; Is it really necessary to do this next part for each summary line?
3665         ; Luckily, doesn't seem to slow things down much.
3666         (mail-parse-ignored-charsets
3667          (with-current-buffer gnus-summary-buffer
3668            gnus-newsgroup-ignored-charsets)))
3669     (or
3670      (and ignored-from-addresses
3671           (string-match ignored-from-addresses gnus-tmp-from)
3672           (let ((extra-headers (mail-header-extra header))
3673                 to
3674                 newsgroups)
3675             (cond
3676              ((setq to (cdr (assq 'To extra-headers)))
3677               (concat gnus-summary-to-prefix
3678                       (inline
3679                         (gnus-summary-extract-address-component
3680                          (funcall gnus-decode-encoded-address-function to)))))
3681              ((setq newsgroups
3682                     (or
3683                      (cdr (assq 'Newsgroups extra-headers))
3684                      (and
3685                       (memq 'Newsgroups gnus-extra-headers)
3686                       (eq (car (gnus-find-method-for-group
3687                                 gnus-newsgroup-name)) 'nntp)
3688                       (gnus-group-real-name gnus-newsgroup-name))))
3689               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3690      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3691
3692 (defun gnus-summary-insert-line (gnus-tmp-header
3693                                  gnus-tmp-level gnus-tmp-current
3694                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3695                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3696                                  &optional gnus-tmp-dummy gnus-tmp-score
3697                                  gnus-tmp-process)
3698   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3699       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3700                                           gnus-tmp-level)))
3701   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3702          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3703          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3704          (gnus-tmp-score-char
3705           (if (or (null gnus-summary-default-score)
3706                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3707                       gnus-summary-zcore-fuzz))
3708               ?                         ;Whitespace
3709             (if (< gnus-tmp-score gnus-summary-default-score)
3710                 gnus-score-below-mark gnus-score-over-mark)))
3711          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3712          (gnus-tmp-replied
3713           (cond (gnus-tmp-process gnus-process-mark)
3714                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3715                  gnus-cached-mark)
3716                 (gnus-tmp-replied gnus-replied-mark)
3717                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3718                  gnus-forwarded-mark)
3719                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3720                  gnus-saved-mark)
3721                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3722                  gnus-recent-mark)
3723                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3724                  gnus-unseen-mark)
3725                 (t gnus-no-mark)))
3726          (gnus-tmp-downloaded
3727           (cond (undownloaded
3728                  gnus-undownloaded-mark)
3729                 (gnus-newsgroup-agentized
3730                  gnus-downloaded-mark)
3731                 (t
3732                  gnus-no-mark)))
3733          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3734          (gnus-tmp-name
3735           (cond
3736            ((string-match "<[^>]+> *$" gnus-tmp-from)
3737             (let ((beg (match-beginning 0)))
3738               (or (and (string-match "^\".+\"" gnus-tmp-from)
3739                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3740                   (substring gnus-tmp-from 0 beg))))
3741            ((string-match "(.+)" gnus-tmp-from)
3742             (substring gnus-tmp-from
3743                        (1+ (match-beginning 0)) (1- (match-end 0))))
3744            (t gnus-tmp-from)))
3745          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3746          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3747          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3748          (inhibit-read-only t))
3749     (when (string= gnus-tmp-name "")
3750       (setq gnus-tmp-name gnus-tmp-from))
3751     (unless (numberp gnus-tmp-lines)
3752       (setq gnus-tmp-lines -1))
3753     (if (= gnus-tmp-lines -1)
3754         (setq gnus-tmp-lines "?")
3755       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3756     (condition-case ()
3757         (gnus-put-text-property
3758          (point)
3759          (progn (eval gnus-summary-line-format-spec) (point))
3760          'gnus-number gnus-tmp-number)
3761       (error (gnus-message 5 "Error updating the summary line")))
3762     (when (gnus-visual-p 'summary-highlight 'highlight)
3763       (forward-line -1)
3764       (gnus-summary-highlight-line)
3765       (gnus-run-hooks 'gnus-summary-update-hook)
3766       (forward-line 1))))
3767
3768 (defun gnus-summary-update-line (&optional dont-update)
3769   "Update summary line after change."
3770   (when (and gnus-summary-default-score
3771              (not gnus-summary-inhibit-highlight))
3772     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3773            (article (gnus-summary-article-number))
3774            (score (gnus-summary-article-score article)))
3775       (unless dont-update
3776         (if (and gnus-summary-mark-below
3777                  (< (gnus-summary-article-score)
3778                     gnus-summary-mark-below))
3779             ;; This article has a low score, so we mark it as read.
3780             (when (memq article gnus-newsgroup-unreads)
3781               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3782           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3783             ;; This article was previously marked as read on account
3784             ;; of a low score, but now it has risen, so we mark it as
3785             ;; unread.
3786             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3787         (gnus-summary-update-mark
3788          (if (or (null gnus-summary-default-score)
3789                  (<= (abs (- score gnus-summary-default-score))
3790                      gnus-summary-zcore-fuzz))
3791              ?                          ;Whitespace
3792            (if (< score gnus-summary-default-score)
3793                gnus-score-below-mark gnus-score-over-mark))
3794          'score))
3795       ;; Do visual highlighting.
3796       (when (gnus-visual-p 'summary-highlight 'highlight)
3797         (gnus-summary-highlight-line)
3798         (gnus-run-hooks 'gnus-summary-update-hook)))))
3799
3800 (defvar gnus-tmp-new-adopts nil)
3801
3802 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3803   "Return the number of articles in THREAD.
3804 This may be 0 in some cases -- if none of the articles in
3805 the thread are to be displayed."
3806   (let* ((number
3807          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3808           (cond
3809            ((not (listp thread))
3810             1)
3811            ((and (consp thread) (cdr thread))
3812             (apply
3813              '+ 1 (mapcar
3814                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3815            ((null thread)
3816             1)
3817            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3818             1)
3819            (t 0))))
3820     (when (and level (zerop level) gnus-tmp-new-adopts)
3821       (incf number
3822             (apply '+ (mapcar
3823                        'gnus-summary-number-of-articles-in-thread
3824                        gnus-tmp-new-adopts))))
3825     (if char
3826         (if (> number 1) gnus-not-empty-thread-mark
3827           gnus-empty-thread-mark)
3828       number)))
3829
3830 (defsubst gnus-summary-line-message-size (head)
3831   "Return pretty-printed version of message size.
3832 This function is intended to be used in
3833 `gnus-summary-line-format-alist'."
3834   (let ((c (or (mail-header-chars head) -1)))
3835     (cond ((< c 0) "n/a")               ; chars not available
3836           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3837           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3838           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3839           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3840
3841
3842 (defun gnus-summary-set-local-parameters (group)
3843   "Go through the local params of GROUP and set all variable specs in that list."
3844   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3845                                         ; really variables.
3846     (dolist (elem (gnus-group-find-parameter group))
3847       (and (consp elem)                 ; Has to be a cons.
3848            (consp (cdr elem))           ; The cdr has to be a list.
3849            (symbolp (car elem))         ; Has to be a symbol in there.
3850            (not (memq (car elem) vars))
3851            (ignore-errors
3852              (push (car elem) vars)
3853              ;; Variables like `gnus-show-threads' that are globally
3854              ;; bound, if used as group parameters, need to get to be
3855              ;; buffer-local, whereas just parameters like `gcc-self',
3856              ;; `timestamp', etc. should not be bound as variables.
3857              (if (boundp (car elem))
3858                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3859                (eval (nth 1 elem))))))))
3860
3861 (defun gnus-summary-read-group (group &optional show-all no-article
3862                                       kill-buffer no-display backward
3863                                       select-articles)
3864   "Start reading news in newsgroup GROUP.
3865 If SHOW-ALL is non-nil, already read articles are also listed.
3866 If NO-ARTICLE is non-nil, no article is selected initially.
3867 If NO-DISPLAY, don't generate a summary buffer."
3868   (let (result)
3869     (while (and group
3870                 (null (setq result
3871                             (let ((gnus-auto-select-next nil))
3872                               (or (gnus-summary-read-group-1
3873                                    group show-all no-article
3874                                    kill-buffer no-display
3875                                    select-articles)
3876                                   (setq show-all nil
3877                                         select-articles nil)))))
3878                 (eq gnus-auto-select-next 'quietly))
3879       (set-buffer gnus-group-buffer)
3880       ;; The entry function called above goes to the next
3881       ;; group automatically, so we go two groups back
3882       ;; if we are searching for the previous group.
3883       (when backward
3884         (gnus-group-prev-unread-group 2))
3885       (if (not (equal group (gnus-group-group-name)))
3886           (setq group (gnus-group-group-name))
3887         (setq group nil)))
3888     result))
3889
3890 (defun gnus-summary-read-group-1 (group show-all no-article
3891                                         kill-buffer no-display
3892                                         &optional select-articles)
3893   ;; Killed foreign groups can't be entered.
3894   ;;  (when (and (not (gnus-group-native-p group))
3895   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3896   ;;    (error "Dead non-native groups can't be entered"))
3897   (gnus-message 5 "Retrieving newsgroup: %s..."
3898                 (gnus-group-decoded-name group))
3899   (let* ((new-group (gnus-summary-setup-buffer group))
3900          (quit-config (gnus-group-quit-config group))
3901          (did-select (and new-group (gnus-select-newsgroup
3902                                      group show-all select-articles))))
3903     (cond
3904      ;; This summary buffer exists already, so we just select it.
3905      ((not new-group)
3906       (gnus-set-global-variables)
3907       (when kill-buffer
3908         (gnus-kill-or-deaden-summary kill-buffer))
3909       (gnus-configure-windows 'summary 'force)
3910       (gnus-set-mode-line 'summary)
3911       (gnus-summary-position-point)
3912       (message "")
3913       t)
3914      ;; We couldn't select this group.
3915      ((null did-select)
3916       (when (and (eq major-mode 'gnus-summary-mode)
3917                  (not (equal (current-buffer) kill-buffer)))
3918         (kill-buffer (current-buffer))
3919         (if (not quit-config)
3920             (progn
3921               ;; Update the info -- marks might need to be removed,
3922               ;; for instance.
3923               (gnus-summary-update-info)
3924               (set-buffer gnus-group-buffer)
3925               (gnus-group-jump-to-group group)
3926               (gnus-group-next-unread-group 1))
3927           (gnus-handle-ephemeral-exit quit-config)))
3928       (let ((grpinfo (gnus-get-info group)))
3929         (if (null (gnus-info-read grpinfo))
3930             (gnus-message 3 "Group %s contains no messages"
3931                           (gnus-group-decoded-name group))
3932           (gnus-message 3 "Can't select group")))
3933       nil)
3934      ;; The user did a `C-g' while prompting for number of articles,
3935      ;; so we exit this group.
3936      ((eq did-select 'quit)
3937       (and (eq major-mode 'gnus-summary-mode)
3938            (not (equal (current-buffer) kill-buffer))
3939            (kill-buffer (current-buffer)))
3940       (when kill-buffer
3941         (gnus-kill-or-deaden-summary kill-buffer))
3942       (if (not quit-config)
3943           (progn
3944             (set-buffer gnus-group-buffer)
3945             (gnus-group-jump-to-group group)
3946             (gnus-configure-windows 'group 'force))
3947         (gnus-handle-ephemeral-exit quit-config))
3948       ;; Finally signal the quit.
3949       (signal 'quit nil))
3950      ;; The group was successfully selected.
3951      (t
3952       (gnus-set-global-variables)
3953       ;; Save the active value in effect when the group was entered.
3954       (setq gnus-newsgroup-active
3955             (gnus-copy-sequence
3956              (gnus-active gnus-newsgroup-name)))
3957       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3958       ;; You can change the summary buffer in some way with this hook.
3959       (gnus-run-hooks 'gnus-select-group-hook)
3960       (when (memq 'summary (gnus-update-format-specifications
3961                             nil 'summary 'summary-mode 'summary-dummy))
3962         ;; The format specification for the summary line was updated,
3963         ;; so we need to update the mark positions as well.
3964         (gnus-update-summary-mark-positions))
3965       ;; Do score processing.
3966       (when gnus-use-scoring
3967         (gnus-possibly-score-headers))
3968       ;; Check whether to fill in the gaps in the threads.
3969       (when gnus-build-sparse-threads
3970         (gnus-build-sparse-threads))
3971       ;; Find the initial limit.
3972       (if show-all
3973           (let ((gnus-newsgroup-dormant nil))
3974             (gnus-summary-initial-limit show-all))
3975         (gnus-summary-initial-limit show-all))
3976       ;; Generate the summary buffer.
3977       (unless no-display
3978         (gnus-summary-prepare))
3979       (when gnus-use-trees
3980         (gnus-tree-open group)
3981         (setq gnus-summary-highlight-line-function
3982               'gnus-tree-highlight-article))
3983       ;; If the summary buffer is empty, but there are some low-scored
3984       ;; articles or some excluded dormants, we include these in the
3985       ;; buffer.
3986       (when (and (zerop (buffer-size))
3987                  (not no-display))
3988         (cond (gnus-newsgroup-dormant
3989                (gnus-summary-limit-include-dormant))
3990               ((and gnus-newsgroup-scored show-all)
3991                (gnus-summary-limit-include-expunged t))))
3992       ;; Function `gnus-apply-kill-file' must be called in this hook.
3993       (gnus-run-hooks 'gnus-apply-kill-hook)
3994       (if (and (zerop (buffer-size))
3995                (not no-display))
3996           (progn
3997             ;; This newsgroup is empty.
3998             (gnus-summary-catchup-and-exit nil t)
3999             (gnus-message 6 "No unread news")
4000             (when kill-buffer
4001               (gnus-kill-or-deaden-summary kill-buffer))
4002             ;; Return nil from this function.
4003             nil)
4004         ;; Hide conversation thread subtrees.  We cannot do this in
4005         ;; gnus-summary-prepare-hook since kill processing may not
4006         ;; work with hidden articles.
4007         (gnus-summary-maybe-hide-threads)
4008         (when kill-buffer
4009           (gnus-kill-or-deaden-summary kill-buffer))
4010         (gnus-summary-auto-select-subject)
4011         ;; Show first unread article if requested.
4012         (if (and (not no-article)
4013                  (not no-display)
4014                  gnus-newsgroup-unreads
4015                  gnus-auto-select-first)
4016             (progn
4017               (gnus-configure-windows 'summary)
4018               (let ((art (gnus-summary-article-number)))
4019                 (unless (and (not gnus-plugged)
4020                              (or (memq art gnus-newsgroup-undownloaded)
4021                                  (memq art gnus-newsgroup-downloadable)))
4022                   (gnus-summary-goto-article art))))
4023           ;; Don't select any articles.
4024           (gnus-summary-position-point)
4025           (gnus-configure-windows 'summary 'force)
4026           (gnus-set-mode-line 'summary))
4027         (when (and gnus-auto-center-group
4028                    (get-buffer-window gnus-group-buffer t))
4029           ;; Gotta use windows, because recenter does weird stuff if
4030           ;; the current buffer ain't the displayed window.
4031           (let ((owin (selected-window)))
4032             (select-window (get-buffer-window gnus-group-buffer t))
4033             (when (gnus-group-goto-group group)
4034               (recenter))
4035             (select-window owin)))
4036         ;; Mark this buffer as "prepared".
4037         (setq gnus-newsgroup-prepared t)
4038         (gnus-run-hooks 'gnus-summary-prepared-hook)
4039         (unless (gnus-ephemeral-group-p group)
4040           (gnus-group-update-group group))
4041         t)))))
4042
4043 (defun gnus-summary-auto-select-subject ()
4044   "Select the subject line on initial group entry."
4045   (goto-char (point-min))
4046   (cond
4047    ((eq gnus-auto-select-subject 'best)
4048     (gnus-summary-best-unread-subject))
4049    ((eq gnus-auto-select-subject 'unread)
4050     (gnus-summary-first-unread-subject))
4051    ((eq gnus-auto-select-subject 'unseen)
4052     (gnus-summary-first-unseen-subject))
4053    ((eq gnus-auto-select-subject 'unseen-or-unread)
4054     (gnus-summary-first-unseen-or-unread-subject))
4055    ((eq gnus-auto-select-subject 'first)
4056     ;; Do nothing.
4057     )
4058    ((functionp gnus-auto-select-subject)
4059     (funcall gnus-auto-select-subject))))
4060
4061 (defun gnus-summary-prepare ()
4062   "Generate the summary buffer."
4063   (interactive)
4064   (let ((inhibit-read-only t))
4065     (erase-buffer)
4066     (setq gnus-newsgroup-data nil
4067           gnus-newsgroup-data-reverse nil)
4068     (gnus-run-hooks 'gnus-summary-generate-hook)
4069     ;; Generate the buffer, either with threads or without.
4070     (when gnus-newsgroup-headers
4071       (gnus-summary-prepare-threads
4072        (if gnus-show-threads
4073            (gnus-sort-gathered-threads
4074             (funcall gnus-summary-thread-gathering-function
4075                      (gnus-sort-threads
4076                       (gnus-cut-threads (gnus-make-threads)))))
4077          ;; Unthreaded display.
4078          (gnus-sort-articles gnus-newsgroup-headers))))
4079     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4080     ;; Call hooks for modifying summary buffer.
4081     (goto-char (point-min))
4082     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4083
4084 (defsubst gnus-general-simplify-subject (subject)
4085   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4086   (setq subject
4087         (cond
4088          ;; Truncate the subject.
4089          (gnus-simplify-subject-functions
4090           (gnus-map-function gnus-simplify-subject-functions subject))
4091          ((numberp gnus-summary-gather-subject-limit)
4092           (setq subject (gnus-simplify-subject-re subject))
4093           (if (> (length subject) gnus-summary-gather-subject-limit)
4094               (substring subject 0 gnus-summary-gather-subject-limit)
4095             subject))
4096          ;; Fuzzily simplify it.
4097          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4098           (gnus-simplify-subject-fuzzy subject))
4099          ;; Just remove the leading "Re:".
4100          (t
4101           (gnus-simplify-subject-re subject))))
4102
4103   (if (and gnus-summary-gather-exclude-subject
4104            (string-match gnus-summary-gather-exclude-subject subject))
4105       nil                         ; This article shouldn't be gathered
4106     subject))
4107
4108 (defun gnus-summary-simplify-subject-query ()
4109   "Query where the respool algorithm would put this article."
4110   (interactive)
4111   (gnus-summary-select-article)
4112   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4113
4114 (defun gnus-gather-threads-by-subject (threads)
4115   "Gather threads by looking at Subject headers."
4116   (if (not gnus-summary-make-false-root)
4117       threads
4118     (let ((hashtb (gnus-make-hashtable 1024))
4119           (prev threads)
4120           (result threads)
4121           subject hthread whole-subject)
4122       (while threads
4123         (setq subject (gnus-general-simplify-subject
4124                        (setq whole-subject (mail-header-subject
4125                                             (caar threads)))))
4126         (when subject
4127           (if (setq hthread (gnus-gethash subject hashtb))
4128               (progn
4129                 ;; We enter a dummy root into the thread, if we
4130                 ;; haven't done that already.
4131                 (unless (stringp (caar hthread))
4132                   (setcar hthread (list whole-subject (car hthread))))
4133                 ;; We add this new gathered thread to this gathered
4134                 ;; thread.
4135                 (setcdr (car hthread)
4136                         (nconc (cdar hthread) (list (car threads))))
4137                 ;; Remove it from the list of threads.
4138                 (setcdr prev (cdr threads))
4139                 (setq threads prev))
4140             ;; Enter this thread into the hash table.
4141             (gnus-sethash subject
4142                           (if gnus-summary-make-false-root-always
4143                               (progn
4144                                 ;; If you want a dummy root above all
4145                                 ;; threads...
4146                                 (setcar threads (list whole-subject
4147                                                       (car threads)))
4148                                 threads)
4149                             threads)
4150                           hashtb)))
4151         (setq prev threads)
4152         (setq threads (cdr threads)))
4153       result)))
4154
4155 (defun gnus-gather-threads-by-references (threads)
4156   "Gather threads by looking at References headers."
4157   (let ((idhashtb (gnus-make-hashtable 1024))
4158         (thhashtb (gnus-make-hashtable 1024))
4159         (prev threads)
4160         (result threads)
4161         ids references id gthread gid entered ref)
4162     (while threads
4163       (when (setq references (mail-header-references (caar threads)))
4164         (setq id (mail-header-id (caar threads))
4165               ids (inline (gnus-split-references references))
4166               entered nil)
4167         (while (setq ref (pop ids))
4168           (setq ids (delete ref ids))
4169           (if (not (setq gid (gnus-gethash ref idhashtb)))
4170               (progn
4171                 (gnus-sethash ref id idhashtb)
4172                 (gnus-sethash id threads thhashtb))
4173             (setq gthread (gnus-gethash gid thhashtb))
4174             (unless entered
4175               ;; We enter a dummy root into the thread, if we
4176               ;; haven't done that already.
4177               (unless (stringp (caar gthread))
4178                 (setcar gthread (list (mail-header-subject (caar gthread))
4179                                       (car gthread))))
4180               ;; We add this new gathered thread to this gathered
4181               ;; thread.
4182               (setcdr (car gthread)
4183                       (nconc (cdar gthread) (list (car threads)))))
4184             ;; Add it into the thread hash table.
4185             (gnus-sethash id gthread thhashtb)
4186             (setq entered t)
4187             ;; Remove it from the list of threads.
4188             (setcdr prev (cdr threads))
4189             (setq threads prev))))
4190       (setq prev threads)
4191       (setq threads (cdr threads)))
4192     result))
4193
4194 (defun gnus-sort-gathered-threads (threads)
4195   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4196   (let ((result threads))
4197     (while threads
4198       (when (stringp (caar threads))
4199         (setcdr (car threads)
4200                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4201       (setq threads (cdr threads)))
4202     result))
4203
4204 (defun gnus-thread-loop-p (root thread)
4205   "Say whether ROOT is in THREAD."
4206   (let ((stack (list thread))
4207         (infloop 0)
4208         th)
4209     (while (setq thread (pop stack))
4210       (setq th (cdr thread))
4211       (while (and th
4212                   (not (eq (caar th) root)))
4213         (pop th))
4214       (if th
4215           ;; We have found a loop.
4216           (let (ref-dep)
4217             (setcdr thread (delq (car th) (cdr thread)))
4218             (if (boundp (setq ref-dep (intern "none"
4219                                               gnus-newsgroup-dependencies)))
4220                 (setcdr (symbol-value ref-dep)
4221                         (nconc (cdr (symbol-value ref-dep))
4222                                (list (car th))))
4223               (set ref-dep (list nil (car th))))
4224             (setq infloop 1
4225                   stack nil))
4226         ;; Push all the subthreads onto the stack.
4227         (push (cdr thread) stack)))
4228     infloop))
4229
4230 (defun gnus-make-threads ()
4231   "Go through the dependency hashtb and find the roots.  Return all threads."
4232   (let (threads)
4233     (while (catch 'infloop
4234              (mapatoms
4235               (lambda (refs)
4236                 ;; Deal with self-referencing References loops.
4237                 (when (and (car (symbol-value refs))
4238                            (not (zerop
4239                                  (apply
4240                                   '+
4241                                   (mapcar
4242                                    (lambda (thread)
4243                                      (gnus-thread-loop-p
4244                                       (car (symbol-value refs)) thread))
4245                                    (cdr (symbol-value refs)))))))
4246                   (setq threads nil)
4247                   (throw 'infloop t))
4248                 (unless (car (symbol-value refs))
4249                   ;; These threads do not refer back to any other
4250                   ;; articles, so they're roots.
4251                   (setq threads (append (cdr (symbol-value refs)) threads))))
4252               gnus-newsgroup-dependencies)))
4253     threads))
4254
4255 ;; Build the thread tree.
4256 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4257   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4258
4259 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4260 if it was already present.
4261
4262 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4263 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4264 Message-IDs will be renamed to a unique Message-ID before being
4265 entered.
4266
4267 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4268   (let* ((id (mail-header-id header))
4269          (id-dep (and id (intern id dependencies)))
4270          parent-id ref ref-dep ref-header replaced)
4271     ;; Enter this `header' in the `dependencies' table.
4272     (cond
4273      ((not id-dep)
4274       (setq header nil))
4275      ;; The first two cases do the normal part: enter a new `header'
4276      ;; in the `dependencies' table.
4277      ((not (boundp id-dep))
4278       (set id-dep (list header)))
4279      ((null (car (symbol-value id-dep)))
4280       (setcar (symbol-value id-dep) header))
4281
4282      ;; From here the `header' was already present in the
4283      ;; `dependencies' table.
4284      (force-new
4285       ;; Overrides an existing entry;
4286       ;; just set the header part of the entry.
4287       (setcar (symbol-value id-dep) header)
4288       (setq replaced t))
4289
4290      ;; Renames the existing `header' to a unique Message-ID.
4291      ((not gnus-summary-ignore-duplicates)
4292       ;; An article with this Message-ID has already been seen.
4293       ;; We rename the Message-ID.
4294       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4295            (list header))
4296       (mail-header-set-id header id))
4297
4298      ;; The last case ignores an existing entry, except it adds any
4299      ;; additional Xrefs (in case the two articles came from different
4300      ;; servers.
4301      ;; Also sets `header' to `nil' meaning that the `dependencies'
4302      ;; table was *not* modified.
4303      (t
4304       (mail-header-set-xref
4305        (car (symbol-value id-dep))
4306        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4307                    "")
4308                (or (mail-header-xref header) "")))
4309       (setq header nil)))
4310
4311     (when (and header (not replaced))
4312       ;; First check that we are not creating a References loop.
4313       (setq parent-id (gnus-parent-id (mail-header-references header)))
4314       (setq ref parent-id)
4315       (while (and ref
4316                   (setq ref-dep (intern-soft ref dependencies))
4317                   (boundp ref-dep)
4318                   (setq ref-header (car (symbol-value ref-dep))))
4319         (if (string= id ref)
4320             ;; Yuk!  This is a reference loop.  Make the article be a
4321             ;; root article.
4322             (progn
4323               (mail-header-set-references (car (symbol-value id-dep)) "none")
4324               (setq ref nil)
4325               (setq parent-id nil))
4326           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4327       (setq ref-dep (intern (or parent-id "none") dependencies))
4328       (if (boundp ref-dep)
4329           (setcdr (symbol-value ref-dep)
4330                   (nconc (cdr (symbol-value ref-dep))
4331                          (list (symbol-value id-dep))))
4332         (set ref-dep (list nil (symbol-value id-dep)))))
4333     header))
4334
4335 (defun gnus-extract-message-id-from-in-reply-to (string)
4336   (if (string-match "<[^>]+>" string)
4337       (substring string (match-beginning 0) (match-end 0))
4338     nil))
4339
4340 (defun gnus-build-sparse-threads ()
4341   (let ((headers gnus-newsgroup-headers)
4342         (mail-parse-charset gnus-newsgroup-charset)
4343         (gnus-summary-ignore-duplicates t)
4344         header references generation relations
4345         subject child end new-child date)
4346     ;; First we create an alist of generations/relations, where
4347     ;; generations is how much we trust the relation, and the relation
4348     ;; is parent/child.
4349     (gnus-message 7 "Making sparse threads...")
4350     (save-excursion
4351       (nnheader-set-temp-buffer " *gnus sparse threads*")
4352       (while (setq header (pop headers))
4353         (when (and (setq references (mail-header-references header))
4354                    (not (string= references "")))
4355           (insert references)
4356           (setq child (mail-header-id header)
4357                 subject (mail-header-subject header)
4358                 date (mail-header-date header)
4359                 generation 0)
4360           (while (search-backward ">" nil t)
4361             (setq end (1+ (point)))
4362             (when (search-backward "<" nil t)
4363               (setq new-child (buffer-substring (point) end))
4364               (push (list (incf generation)
4365                           child (setq child new-child)
4366                           subject date)
4367                     relations)))
4368           (when child
4369             (push (list (1+ generation) child nil subject) relations))
4370           (erase-buffer)))
4371       (kill-buffer (current-buffer)))
4372     ;; Sort over trustworthiness.
4373     (dolist (relation (sort relations 'car-less-than-car))
4374       (when (gnus-dependencies-add-header
4375              (make-full-mail-header
4376               gnus-reffed-article-number
4377               (nth 3 relation) "" (or (nth 4 relation) "")
4378               (nth 1 relation)
4379               (or (nth 2 relation) "") 0 0 "")
4380              gnus-newsgroup-dependencies nil)
4381         (push gnus-reffed-article-number gnus-newsgroup-limit)
4382         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4383         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4384               gnus-newsgroup-reads)
4385         (decf gnus-reffed-article-number)))
4386     (gnus-message 7 "Making sparse threads...done")))
4387
4388 (defun gnus-build-old-threads ()
4389   ;; Look at all the articles that refer back to old articles, and
4390   ;; fetch the headers for the articles that aren't there.  This will
4391   ;; build complete threads - if the roots haven't been expired by the
4392   ;; server, that is.
4393   (let ((mail-parse-charset gnus-newsgroup-charset)
4394         id heads)
4395     (mapatoms
4396      (lambda (refs)
4397        (when (not (car (symbol-value refs)))
4398          (setq heads (cdr (symbol-value refs)))
4399          (while heads
4400            (if (memq (mail-header-number (caar heads))
4401                      gnus-newsgroup-dormant)
4402                (setq heads (cdr heads))
4403              (setq id (symbol-name refs))
4404              (while (and (setq id (gnus-build-get-header id))
4405                          (not (car (gnus-id-to-thread id)))))
4406              (setq heads nil)))))
4407      gnus-newsgroup-dependencies)))
4408
4409 (defsubst gnus-remove-odd-characters (string)
4410   "Translate STRING into something that doesn't contain weird characters."
4411   (mm-subst-char-in-string
4412    ?\r ?\-
4413    (mm-subst-char-in-string ?\n ?\- string t) t))
4414
4415 ;; This function has to be called with point after the article number
4416 ;; on the beginning of the line.
4417 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4418   (let ((eol (point-at-eol))
4419         (buffer (current-buffer))
4420         header references in-reply-to)
4421
4422     ;; overview: [num subject from date id refs chars lines misc]
4423     (unwind-protect
4424         (let (x)
4425           (narrow-to-region (point) eol)
4426           (unless (eobp)
4427             (forward-char))
4428
4429           (setq header
4430                 (make-full-mail-header
4431                  number                 ; number
4432                  (condition-case ()     ; subject
4433                      (gnus-remove-odd-characters
4434                       (funcall gnus-decode-encoded-word-function
4435                                (setq x (nnheader-nov-field))))
4436                    (error x))
4437                  (condition-case ()     ; from
4438                      (gnus-remove-odd-characters
4439                       (funcall gnus-decode-encoded-address-function
4440                                (setq x (nnheader-nov-field))))
4441                    (error x))
4442                  (nnheader-nov-field)   ; date
4443                  (nnheader-nov-read-message-id number)  ; id
4444                  (setq references (nnheader-nov-field)) ; refs
4445                  (nnheader-nov-read-integer) ; chars
4446                  (nnheader-nov-read-integer) ; lines
4447                  (unless (eobp)
4448                    (if (looking-at "Xref: ")
4449                        (goto-char (match-end 0)))
4450                    (nnheader-nov-field)) ; Xref
4451                  (nnheader-nov-parse-extra)))) ; extra
4452
4453       (widen))
4454
4455     (when (and (string= references "")
4456                (setq in-reply-to (mail-header-extra header))
4457                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4458       (mail-header-set-references
4459        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4460
4461     (when gnus-alter-header-function
4462       (funcall gnus-alter-header-function header))
4463     (gnus-dependencies-add-header header dependencies force-new)))
4464
4465 (defun gnus-build-get-header (id)
4466   "Look through the buffer of NOV lines and find the header to ID.
4467 Enter this line into the dependencies hash table, and return
4468 the id of the parent article (if any)."
4469   (let ((deps gnus-newsgroup-dependencies)
4470         found header)
4471     (prog1
4472         (with-current-buffer nntp-server-buffer
4473           (let ((case-fold-search nil))
4474             (goto-char (point-min))
4475             (while (and (not found)
4476                         (search-forward id nil t))
4477               (beginning-of-line)
4478               (setq found (looking-at
4479                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4480                                    (regexp-quote id))))
4481               (or found (beginning-of-line 2)))
4482             (when found
4483               (beginning-of-line)
4484               (and
4485                (setq header (gnus-nov-parse-line
4486                              (read (current-buffer)) deps))
4487                (gnus-parent-id (mail-header-references header))))))
4488       (when header
4489         (let ((number (mail-header-number header)))
4490           (push number gnus-newsgroup-limit)
4491           (push header gnus-newsgroup-headers)
4492           (if (memq number gnus-newsgroup-unselected)
4493               (progn
4494                 (setq gnus-newsgroup-unreads
4495                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4496                                                number))
4497                 (setq gnus-newsgroup-unselected
4498                       (delq number gnus-newsgroup-unselected)))
4499             (push number gnus-newsgroup-ancient)))))))
4500
4501 (defun gnus-build-all-threads ()
4502   "Read all the headers."
4503   (let ((gnus-summary-ignore-duplicates t)
4504         (mail-parse-charset gnus-newsgroup-charset)
4505         (dependencies gnus-newsgroup-dependencies)
4506         header article)
4507     (with-current-buffer nntp-server-buffer
4508       (let ((case-fold-search nil))
4509         (goto-char (point-min))
4510         (while (not (eobp))
4511           (ignore-errors
4512             (setq article (read (current-buffer))
4513                   header (gnus-nov-parse-line article dependencies)))
4514           (when header
4515             (with-current-buffer gnus-summary-buffer
4516               (push header gnus-newsgroup-headers)
4517               (if (memq (setq article (mail-header-number header))
4518                         gnus-newsgroup-unselected)
4519                   (progn
4520                     (setq gnus-newsgroup-unreads
4521                           (gnus-add-to-sorted-list
4522                            gnus-newsgroup-unreads article))
4523                     (setq gnus-newsgroup-unselected
4524                           (delq article gnus-newsgroup-unselected)))
4525                 (push article gnus-newsgroup-ancient)))
4526             (forward-line 1)))))))
4527
4528 (defun gnus-summary-update-article-line (article header)
4529   "Update the line for ARTICLE using HEADER."
4530   (let* ((id (mail-header-id header))
4531          (thread (gnus-id-to-thread id)))
4532     (unless thread
4533       (error "Article in no thread"))
4534     ;; Update the thread.
4535     (setcar thread header)
4536     (gnus-summary-goto-subject article)
4537     (let* ((datal (gnus-data-find-list article))
4538            (data (car datal))
4539            (inhibit-read-only t)
4540            (level (gnus-summary-thread-level)))
4541       (gnus-delete-line)
4542       (let ((inserted (- (point)
4543                          (progn
4544                            (gnus-summary-insert-line
4545                             header level nil
4546                             (memq article gnus-newsgroup-undownloaded)
4547                             (gnus-article-mark article)
4548                             (memq article gnus-newsgroup-replied)
4549                             (memq article gnus-newsgroup-expirable)
4550                             ;; Only insert the Subject string when it's different
4551                             ;; from the previous Subject string.
4552                             (if (and
4553                                  gnus-show-threads
4554                                  (gnus-subject-equal
4555                                   (condition-case ()
4556                                       (mail-header-subject
4557                                        (gnus-data-header
4558                                         (cadr
4559                                          (gnus-data-find-list
4560                                           article
4561                                           (gnus-data-list t)))))
4562                                     ;; Error on the side of excessive subjects.
4563                                     (error ""))
4564                                   (mail-header-subject header)))
4565                                 ""
4566                               (mail-header-subject header))
4567                             nil (cdr (assq article gnus-newsgroup-scored))
4568                             (memq article gnus-newsgroup-processable))
4569                            (point)))))
4570         (when (cdr datal)
4571           (gnus-data-update-list
4572            (cdr datal)
4573            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4574
4575 (defun gnus-summary-update-article (article &optional iheader)
4576   "Update ARTICLE in the summary buffer."
4577   (set-buffer gnus-summary-buffer)
4578   (let* ((header (gnus-summary-article-header article))
4579          (id (mail-header-id header))
4580          (data (gnus-data-find article))
4581          (thread (gnus-id-to-thread id))
4582          (references (mail-header-references header))
4583          (parent
4584           (gnus-id-to-thread
4585            (or (gnus-parent-id
4586                 (when (and references
4587                            (not (equal "" references)))
4588                   references))
4589                "none")))
4590          (inhibit-read-only t)
4591          (old (car thread)))
4592     (when thread
4593       (unless iheader
4594         (setcar thread nil)
4595         (when parent
4596           (delq thread parent)))
4597       (if (gnus-summary-insert-subject id header)
4598           ;; Set the (possibly) new article number in the data structure.
4599           (gnus-data-set-number data (gnus-id-to-article id))
4600         (setcar thread old)
4601         nil))))
4602
4603 (defun gnus-rebuild-thread (id &optional line)
4604   "Rebuild the thread containing ID.
4605 If LINE, insert the rebuilt thread starting on line LINE."
4606   (let ((inhibit-read-only t)
4607         old-pos current thread data)
4608     (if (not gnus-show-threads)
4609         (setq thread (list (car (gnus-id-to-thread id))))
4610       ;; Get the thread this article is part of.
4611       (setq thread (gnus-remove-thread id)))
4612     (setq old-pos (point-at-bol))
4613     (setq current (save-excursion
4614                     (and (re-search-backward "[\r\n]" nil t)
4615                          (gnus-summary-article-number))))
4616     ;; If this is a gathered thread, we have to go some re-gathering.
4617     (when (stringp (car thread))
4618       (let ((subject (car thread))
4619             roots thr)
4620         (setq thread (cdr thread))
4621         (while thread
4622           (unless (memq (setq thr (gnus-id-to-thread
4623                                    (gnus-root-id
4624                                     (mail-header-id (caar thread)))))
4625                         roots)
4626             (push thr roots))
4627           (setq thread (cdr thread)))
4628         ;; We now have all (unique) roots.
4629         (if (= (length roots) 1)
4630             ;; All the loose roots are now one solid root.
4631             (setq thread (car roots))
4632           (setq thread (cons subject (gnus-sort-threads roots))))))
4633     (let (threads)
4634       ;; We then insert this thread into the summary buffer.
4635       (when line
4636         (goto-char (point-min))
4637         (forward-line (1- line)))
4638       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4639         (if gnus-show-threads
4640             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4641           (gnus-summary-prepare-unthreaded thread))
4642         (setq data (nreverse gnus-newsgroup-data))
4643         (setq threads gnus-newsgroup-threads))
4644       ;; We splice the new data into the data structure.
4645       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4646       ;;!!! then we want to insert at the beginning of the buffer.
4647       ;;!!! That happens to be true with Gnus now, but that may
4648       ;;!!! change in the future.  Perhaps.
4649       (gnus-data-enter-list
4650        (if line nil current) data (- (point) old-pos))
4651       (setq gnus-newsgroup-threads
4652             (nconc threads gnus-newsgroup-threads))
4653       (gnus-data-compute-positions))))
4654
4655 (defun gnus-number-to-header (number)
4656   "Return the header for article NUMBER."
4657   (let ((headers gnus-newsgroup-headers))
4658     (while (and headers
4659                 (not (= number (mail-header-number (car headers)))))
4660       (pop headers))
4661     (when headers
4662       (car headers))))
4663
4664 (defun gnus-parent-headers (in-headers &optional generation)
4665   "Return the headers of the GENERATIONeth parent of HEADERS."
4666   (unless generation
4667     (setq generation 1))
4668   (let ((parent t)
4669         (headers in-headers)
4670         references)
4671     (while (and parent
4672                 (not (zerop generation))
4673                 (setq references (mail-header-references headers)))
4674       (setq headers (if (and references
4675                              (setq parent (gnus-parent-id references)))
4676                         (car (gnus-id-to-thread parent))
4677                       nil))
4678       (decf generation))
4679     (and (not (eq headers in-headers))
4680          headers)))
4681
4682 (defun gnus-id-to-thread (id)
4683   "Return the (sub-)thread where ID appears."
4684   (gnus-gethash id gnus-newsgroup-dependencies))
4685
4686 (defun gnus-id-to-article (id)
4687   "Return the article number of ID."
4688   (let ((thread (gnus-id-to-thread id)))
4689     (when (and thread
4690                (car thread))
4691       (mail-header-number (car thread)))))
4692
4693 (defun gnus-id-to-header (id)
4694   "Return the article headers of ID."
4695   (car (gnus-id-to-thread id)))
4696
4697 (defun gnus-article-displayed-root-p (article)
4698   "Say whether ARTICLE is a root(ish) article."
4699   (let ((level (gnus-summary-thread-level article))
4700         (refs (mail-header-references  (gnus-summary-article-header article)))
4701         particle)
4702     (cond
4703      ((null level) nil)
4704      ((zerop level) t)
4705      ((null refs) t)
4706      ((null (gnus-parent-id refs)) t)
4707      ((and (= 1 level)
4708            (null (setq particle (gnus-id-to-article
4709                                  (gnus-parent-id refs))))
4710            (null (gnus-summary-thread-level particle)))))))
4711
4712 (defun gnus-root-id (id)
4713   "Return the id of the root of the thread where ID appears."
4714   (let (last-id prev)
4715     (while (and id (setq prev (car (gnus-id-to-thread id))))
4716       (setq last-id id
4717             id (gnus-parent-id (mail-header-references prev))))
4718     last-id))
4719
4720 (defun gnus-articles-in-thread (thread)
4721   "Return the list of articles in THREAD."
4722   (cons (mail-header-number (car thread))
4723         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4724
4725 (defun gnus-remove-thread (id &optional dont-remove)
4726   "Remove the thread that has ID in it."
4727   (let (headers thread last-id)
4728     ;; First go up in this thread until we find the root.
4729     (setq last-id (gnus-root-id id)
4730           headers (message-flatten-list (gnus-id-to-thread last-id)))
4731     ;; We have now found the real root of this thread.  It might have
4732     ;; been gathered into some loose thread, so we have to search
4733     ;; through the threads to find the thread we wanted.
4734     (let ((threads gnus-newsgroup-threads)
4735           sub)
4736       (while threads
4737         (setq sub (car threads))
4738         (if (stringp (car sub))
4739             ;; This is a gathered thread, so we look at the roots
4740             ;; below it to find whether this article is in this
4741             ;; gathered root.
4742             (progn
4743               (setq sub (cdr sub))
4744               (while sub
4745                 (when (member (caar sub) headers)
4746                   (setq thread (car threads)
4747                         threads nil
4748                         sub nil))
4749                 (setq sub (cdr sub))))
4750           ;; It's an ordinary thread, so we check it.
4751           (when (eq (car sub) (car headers))
4752             (setq thread sub
4753                   threads nil)))
4754         (setq threads (cdr threads)))
4755       ;; If this article is in no thread, then it's a root.
4756       (if thread
4757           (unless dont-remove
4758             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4759         (setq thread (gnus-id-to-thread last-id)))
4760       (when thread
4761         (prog1
4762             thread                      ; We return this thread.
4763           (unless dont-remove
4764             (if (stringp (car thread))
4765                 (progn
4766                   ;; If we use dummy roots, then we have to remove the
4767                   ;; dummy root as well.
4768                   (when (eq gnus-summary-make-false-root 'dummy)
4769                     ;; We go to the dummy root by going to
4770                     ;; the first sub-"thread", and then one line up.
4771                     (gnus-summary-goto-article
4772                      (mail-header-number (caadr thread)))
4773                     (forward-line -1)
4774                     (gnus-delete-line)
4775                     (gnus-data-compute-positions))
4776                   (setq thread (cdr thread))
4777                   (while thread
4778                     (gnus-remove-thread-1 (car thread))
4779                     (setq thread (cdr thread))))
4780               (gnus-remove-thread-1 thread))))))))
4781
4782 (defun gnus-remove-thread-1 (thread)
4783   "Remove the thread THREAD recursively."
4784   (let ((number (mail-header-number (pop thread)))
4785         d)
4786     (setq thread (reverse thread))
4787     (while thread
4788       (gnus-remove-thread-1 (pop thread)))
4789     (when (setq d (gnus-data-find number))
4790       (goto-char (gnus-data-pos d))
4791       (gnus-summary-show-thread)
4792       (gnus-data-remove
4793        number
4794        (- (point-at-bol)
4795           (prog1
4796               (1+ (point-at-eol))
4797             (gnus-delete-line)))))))
4798
4799 (defun gnus-sort-threads-recursive (threads func)
4800   (sort (mapcar (lambda (thread)
4801                   (cons (car thread)
4802                         (and (cdr thread)
4803                              (gnus-sort-threads-recursive (cdr thread) func))))
4804                 threads) func))
4805
4806 (defun gnus-sort-threads-loop (threads func)
4807   (let* ((superthread (cons nil threads))
4808          (stack (list (cons superthread threads)))
4809          remaining-threads thread)
4810     (while stack
4811       (setq remaining-threads (cdr (car stack)))
4812       (if remaining-threads
4813           (progn (setq thread (car remaining-threads))
4814                  (setcdr (car stack) (cdr remaining-threads))
4815                  (if (cdr thread)
4816                      (push (cons thread (cdr thread)) stack)))
4817         (setq thread (caar stack))
4818         (setcdr thread (sort (cdr thread) func))
4819         (pop stack)))
4820     (cdr superthread)))
4821
4822 (defun gnus-sort-threads (threads)
4823   "Sort THREADS."
4824   (if (not gnus-thread-sort-functions)
4825       threads
4826     (gnus-message 8 "Sorting threads...")
4827     (prog1
4828         (condition-case nil
4829             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4830               (gnus-sort-threads-recursive
4831                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4832           ;; Even after binding max-lisp-eval-depth, the recursive
4833           ;; sorter might fail for very long threads.  In that case,
4834           ;; try using a (less well-tested) non-recursive sorter.
4835           (error (gnus-message 9 "Sorting threads with loop...")
4836                  (gnus-sort-threads-loop
4837                   threads (gnus-make-sort-function
4838                            gnus-thread-sort-functions))))
4839       (gnus-message 8 "Sorting threads...done"))))
4840
4841 (defun gnus-sort-articles (articles)
4842   "Sort ARTICLES."
4843   (when gnus-article-sort-functions
4844     (gnus-message 7 "Sorting articles...")
4845     (prog1
4846         (setq gnus-newsgroup-headers
4847               (sort articles (gnus-make-sort-function
4848                               gnus-article-sort-functions)))
4849       (gnus-message 7 "Sorting articles...done"))))
4850
4851 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4852 (defmacro gnus-thread-header (thread)
4853   "Return header of first article in THREAD.
4854 Note that THREAD must never, ever be anything else than a variable -
4855 using some other form will lead to serious barfage."
4856   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4857   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4858   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4859         (vector thread) 2))
4860
4861 (defsubst gnus-article-sort-by-number (h1 h2)
4862   "Sort articles by article number."
4863   (< (mail-header-number h1)
4864      (mail-header-number h2)))
4865
4866 (defun gnus-thread-sort-by-number (h1 h2)
4867   "Sort threads by root article number."
4868   (gnus-article-sort-by-number
4869    (gnus-thread-header h1) (gnus-thread-header h2)))
4870
4871 (defsubst gnus-article-sort-by-random (h1 h2)
4872   "Sort articles randomly."
4873   (zerop (random 2)))
4874
4875 (defun gnus-thread-sort-by-random (h1 h2)
4876   "Sort threads randomly."
4877   (gnus-article-sort-by-random
4878    (gnus-thread-header h1) (gnus-thread-header h2)))
4879
4880 (defsubst gnus-article-sort-by-lines (h1 h2)
4881   "Sort articles by article Lines header."
4882   (< (mail-header-lines h1)
4883      (mail-header-lines h2)))
4884
4885 (defun gnus-thread-sort-by-lines (h1 h2)
4886   "Sort threads by root article Lines header."
4887   (gnus-article-sort-by-lines
4888    (gnus-thread-header h1) (gnus-thread-header h2)))
4889
4890 (defsubst gnus-article-sort-by-chars (h1 h2)
4891   "Sort articles by octet length."
4892   (< (mail-header-chars h1)
4893      (mail-header-chars h2)))
4894
4895 (defun gnus-thread-sort-by-chars (h1 h2)
4896   "Sort threads by root article octet length."
4897   (gnus-article-sort-by-chars
4898    (gnus-thread-header h1) (gnus-thread-header h2)))
4899
4900 (defsubst gnus-article-sort-by-author (h1 h2)
4901   "Sort articles by root author."
4902   (gnus-string<
4903    (let ((extract (funcall
4904                    gnus-extract-address-components
4905                    (mail-header-from h1))))
4906      (or (car extract) (cadr extract) ""))
4907    (let ((extract (funcall
4908                    gnus-extract-address-components
4909                    (mail-header-from h2))))
4910      (or (car extract) (cadr extract) ""))))
4911
4912 (defun gnus-thread-sort-by-author (h1 h2)
4913   "Sort threads by root author."
4914   (gnus-article-sort-by-author
4915    (gnus-thread-header h1)  (gnus-thread-header h2)))
4916
4917 (defsubst gnus-article-sort-by-recipient (h1 h2)
4918   "Sort articles by recipient."
4919   (gnus-string<
4920    (let ((extract (funcall
4921                    gnus-extract-address-components
4922                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4923      (or (car extract) (cadr extract)))
4924    (let ((extract (funcall
4925                    gnus-extract-address-components
4926                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4927      (or (car extract) (cadr extract)))))
4928
4929 (defun gnus-thread-sort-by-recipient (h1 h2)
4930   "Sort threads by root recipient."
4931   (gnus-article-sort-by-recipient
4932    (gnus-thread-header h1) (gnus-thread-header h2)))
4933
4934 (defsubst gnus-article-sort-by-subject (h1 h2)
4935   "Sort articles by root subject."
4936   (gnus-string<
4937    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4938    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4939
4940 (defun gnus-thread-sort-by-subject (h1 h2)
4941   "Sort threads by root subject."
4942   (gnus-article-sort-by-subject
4943    (gnus-thread-header h1) (gnus-thread-header h2)))
4944
4945 (defsubst gnus-article-sort-by-date (h1 h2)
4946   "Sort articles by root article date."
4947   (time-less-p
4948    (gnus-date-get-time (mail-header-date h1))
4949    (gnus-date-get-time (mail-header-date h2))))
4950
4951 (defun gnus-thread-sort-by-date (h1 h2)
4952   "Sort threads by root article date."
4953   (gnus-article-sort-by-date
4954    (gnus-thread-header h1) (gnus-thread-header h2)))
4955
4956 (defsubst gnus-article-sort-by-score (h1 h2)
4957   "Sort articles by root article score.
4958 Unscored articles will be counted as having a score of zero."
4959   (> (or (cdr (assq (mail-header-number h1)
4960                     gnus-newsgroup-scored))
4961          gnus-summary-default-score 0)
4962      (or (cdr (assq (mail-header-number h2)
4963                     gnus-newsgroup-scored))
4964          gnus-summary-default-score 0)))
4965
4966 (defun gnus-thread-sort-by-score (h1 h2)
4967   "Sort threads by root article score."
4968   (gnus-article-sort-by-score
4969    (gnus-thread-header h1) (gnus-thread-header h2)))
4970
4971 (defun gnus-thread-sort-by-total-score (h1 h2)
4972   "Sort threads by the sum of all scores in the thread.
4973 Unscored articles will be counted as having a score of zero."
4974   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4975
4976 (defun gnus-thread-total-score (thread)
4977   ;; This function find the total score of THREAD.
4978   (cond
4979    ((null thread)
4980     0)
4981    ((consp thread)
4982     (if (stringp (car thread))
4983         (apply gnus-thread-score-function 0
4984                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4985       (gnus-thread-total-score-1 thread)))
4986    (t
4987     (gnus-thread-total-score-1 (list thread)))))
4988
4989 (defun gnus-article-sort-by-most-recent-number (h1 h2)
4990   "Sort articles by number."
4991   (gnus-article-sort-by-number h1 h2))
4992
4993 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4994   "Sort threads such that the thread with the most recently arrived article comes first."
4995   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4996
4997 (defun gnus-thread-highest-number (thread)
4998   "Return the highest article number in THREAD."
4999   (apply 'max (mapcar (lambda (header)
5000                         (mail-header-number header))
5001                       (message-flatten-list thread))))
5002
5003 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5004   "Sort articles by number."
5005   (gnus-article-sort-by-date h1 h2))
5006
5007 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5008   "Sort threads such that the thread with the most recently dated article comes first."
5009   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5010
5011 ; Since this is called not only to sort the top-level threads, but
5012 ; also in recursive sorts to order the articles within a thread, each
5013 ; article will be processed many times.  Thus it speeds things up
5014 ; quite a bit to use gnus-date-get-time, which caches the time value.
5015 (defun gnus-thread-latest-date (thread)
5016   "Return the highest article date in THREAD."
5017   (apply 'max
5018          (mapcar (lambda (header) (gnus-float-time
5019                                    (gnus-date-get-time
5020                                     (mail-header-date header))))
5021                  (message-flatten-list thread))))
5022
5023 (defun gnus-thread-total-score-1 (root)
5024   ;; This function find the total score of the thread below ROOT.
5025   (setq root (car root))
5026   (apply gnus-thread-score-function
5027          (or (append
5028               (mapcar 'gnus-thread-total-score
5029                       (cdr (gnus-id-to-thread (mail-header-id root))))
5030               (when (> (mail-header-number root) 0)
5031                 (list (or (cdr (assq (mail-header-number root)
5032                                      gnus-newsgroup-scored))
5033                           gnus-summary-default-score 0))))
5034              (list gnus-summary-default-score)
5035              '(0))))
5036
5037 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5038 (defvar gnus-tmp-prev-subject nil)
5039 (defvar gnus-tmp-false-parent nil)
5040 (defvar gnus-tmp-root-expunged nil)
5041 (defvar gnus-tmp-dummy-line nil)
5042
5043 (defun gnus-extra-header (type &optional header)
5044   "Return the extra header of TYPE."
5045   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5046       ""))
5047
5048 (defvar gnus-tmp-thread-tree-header-string "")
5049
5050 (defcustom gnus-sum-thread-tree-root "> "
5051   "With %B spec, used for the root of a thread.
5052 If nil, use subject instead."
5053   :version "22.1"
5054   :type '(radio (const :format "%v  " nil) string)
5055   :group 'gnus-thread)
5056
5057 (defcustom gnus-sum-thread-tree-false-root "> "
5058   "With %B spec, used for a false root of a thread.
5059 If nil, use subject instead."
5060   :version "22.1"
5061   :type '(radio (const :format "%v  " nil) string)
5062   :group 'gnus-thread)
5063
5064 (defcustom gnus-sum-thread-tree-single-indent ""
5065   "With %B spec, used for a thread with just one message.
5066 If nil, use subject instead."
5067   :version "22.1"
5068   :type '(radio (const :format "%v  " nil) string)
5069   :group 'gnus-thread)
5070
5071 (defcustom gnus-sum-thread-tree-vertical "| "
5072   "With %B spec, used for drawing a vertical line."
5073   :version "22.1"
5074   :type 'string
5075   :group 'gnus-thread)
5076
5077 (defcustom gnus-sum-thread-tree-indent "  "
5078   "With %B spec, used for indenting."
5079   :version "22.1"
5080   :type 'string
5081   :group 'gnus-thread)
5082
5083 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5084   "With %B spec, used for a leaf with brothers."
5085   :version "22.1"
5086   :type 'string
5087   :group 'gnus-thread)
5088
5089 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5090   "With %B spec, used for a leaf without brothers."
5091   :version "22.1"
5092   :type 'string
5093   :group 'gnus-thread)
5094
5095 (defcustom gnus-summary-display-while-building nil
5096   "If non-nil, show and update the summary buffer as it's being built.
5097 If the value is t, update the buffer after every line is inserted.  If
5098 the value is an integer (N), update the display every N lines."
5099   :version "22.1"
5100   :group 'gnus-thread
5101   :type '(choice (const :tag "off" nil)
5102                  number
5103                  (const :tag "frequently" t)))
5104
5105 (defun gnus-summary-prepare-threads (threads)
5106   "Prepare summary buffer from THREADS and indentation LEVEL.
5107 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5108 or a straight list of headers."
5109   (gnus-message 7 "Generating summary...")
5110
5111   (setq gnus-newsgroup-threads threads)
5112   (beginning-of-line)
5113
5114   (let ((gnus-tmp-level 0)
5115         (default-score (or gnus-summary-default-score 0))
5116         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5117         (building-line-count gnus-summary-display-while-building)
5118         (building-count (integerp gnus-summary-display-while-building))
5119         thread number subject stack state gnus-tmp-gathered beg-match
5120         new-roots gnus-tmp-new-adopts thread-end simp-subject
5121         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5122         gnus-tmp-replied gnus-tmp-subject-or-nil
5123         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5124         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5125         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5126         tree-stack)
5127
5128     (setq gnus-tmp-prev-subject nil
5129           gnus-tmp-thread-tree-header-string "")
5130
5131     (if (vectorp (car threads))
5132         ;; If this is a straight (sic) list of headers, then a
5133         ;; threaded summary display isn't required, so we just create
5134         ;; an unthreaded one.
5135         (gnus-summary-prepare-unthreaded threads)
5136
5137       ;; Do the threaded display.
5138
5139       (if gnus-summary-display-while-building
5140           (switch-to-buffer (buffer-name)))
5141       (while (or threads stack gnus-tmp-new-adopts new-roots)
5142
5143         (if (and (= gnus-tmp-level 0)
5144                  (or (not stack)
5145                      (= (caar stack) 0))
5146                  (not gnus-tmp-false-parent)
5147                  (or gnus-tmp-new-adopts new-roots))
5148             (if gnus-tmp-new-adopts
5149                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5150                       thread (list (car gnus-tmp-new-adopts))
5151                       gnus-tmp-header (caar thread)
5152                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5153               (when new-roots
5154                 (setq thread (list (car new-roots))
5155                       gnus-tmp-header (caar thread)
5156                       new-roots (cdr new-roots))))
5157
5158           (if threads
5159               ;; If there are some threads, we do them before the
5160               ;; threads on the stack.
5161               (setq thread threads
5162                     gnus-tmp-header (caar thread))
5163             ;; There were no current threads, so we pop something off
5164             ;; the stack.
5165             (setq state (car stack)
5166                   gnus-tmp-level (car state)
5167                   tree-stack (cadr state)
5168                   thread (caddr state)
5169                   stack (cdr stack)
5170                   gnus-tmp-header (caar thread))))
5171
5172         (setq gnus-tmp-false-parent nil)
5173         (setq gnus-tmp-root-expunged nil)
5174         (setq thread-end nil)
5175
5176         (if (stringp gnus-tmp-header)
5177             ;; The header is a dummy root.
5178             (cond
5179              ((eq gnus-summary-make-false-root 'adopt)
5180               ;; We let the first article adopt the rest.
5181               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5182                                                (cddar thread)))
5183               (setq gnus-tmp-gathered
5184                     (nconc (mapcar
5185                             (lambda (h) (mail-header-number (car h)))
5186                             (cddar thread))
5187                            gnus-tmp-gathered))
5188               (setq thread (cons (list (caar thread)
5189                                        (cadar thread))
5190                                  (cdr thread)))
5191               (setq gnus-tmp-level -1
5192                     gnus-tmp-false-parent t))
5193              ((eq gnus-summary-make-false-root 'empty)
5194               ;; We print adopted articles with empty subject fields.
5195               (setq gnus-tmp-gathered
5196                     (nconc (mapcar
5197                             (lambda (h) (mail-header-number (car h)))
5198                             (cddar thread))
5199                            gnus-tmp-gathered))
5200               (setq gnus-tmp-level -1))
5201              ((eq gnus-summary-make-false-root 'dummy)
5202               ;; We remember that we probably want to output a dummy
5203               ;; root.
5204               (setq gnus-tmp-dummy-line gnus-tmp-header)
5205               (setq gnus-tmp-prev-subject gnus-tmp-header))
5206              (t
5207               ;; We do not make a root for the gathered
5208               ;; sub-threads at all.
5209               (setq gnus-tmp-level -1)))
5210
5211           (setq number (mail-header-number gnus-tmp-header)
5212                 subject (mail-header-subject gnus-tmp-header)
5213                 simp-subject (gnus-simplify-subject-fully subject))
5214
5215           (cond
5216            ;; If the thread has changed subject, we might want to make
5217            ;; this subthread into a root.
5218            ((and (null gnus-thread-ignore-subject)
5219                  (not (zerop gnus-tmp-level))
5220                  gnus-tmp-prev-subject
5221                  (not (string= gnus-tmp-prev-subject simp-subject)))
5222             (setq new-roots (nconc new-roots (list (car thread)))
5223                   thread-end t
5224                   gnus-tmp-header nil))
5225            ;; If the article lies outside the current limit,
5226            ;; then we do not display it.
5227            ((not (memq number gnus-newsgroup-limit))
5228             (setq gnus-tmp-gathered
5229                   (nconc (mapcar
5230                           (lambda (h) (mail-header-number (car h)))
5231                           (cdar thread))
5232                          gnus-tmp-gathered))
5233             (setq gnus-tmp-new-adopts (if (cdar thread)
5234                                           (append gnus-tmp-new-adopts
5235                                                   (cdar thread))
5236                                         gnus-tmp-new-adopts)
5237                   thread-end t
5238                   gnus-tmp-header nil)
5239             (when (zerop gnus-tmp-level)
5240               (setq gnus-tmp-root-expunged t)))
5241            ;; Perhaps this article is to be marked as read?
5242            ((and gnus-summary-mark-below
5243                  (< (or (cdr (assq number gnus-newsgroup-scored))
5244                         default-score)
5245                     gnus-summary-mark-below)
5246                  ;; Don't touch sparse articles.
5247                  (not (gnus-summary-article-sparse-p number))
5248                  (not (gnus-summary-article-ancient-p number)))
5249             (setq gnus-newsgroup-unreads
5250                   (delq number gnus-newsgroup-unreads))
5251             (if gnus-newsgroup-auto-expire
5252                 (setq gnus-newsgroup-expirable
5253                       (gnus-add-to-sorted-list
5254                        gnus-newsgroup-expirable number))
5255               (push (cons number gnus-low-score-mark)
5256                     gnus-newsgroup-reads))))
5257
5258           (when gnus-tmp-header
5259             ;; We may have an old dummy line to output before this
5260             ;; article.
5261             (when (and gnus-tmp-dummy-line
5262                        (gnus-subject-equal
5263                         gnus-tmp-dummy-line
5264                         (mail-header-subject gnus-tmp-header)))
5265               (gnus-summary-insert-dummy-line
5266                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5267               (setq gnus-tmp-dummy-line nil))
5268
5269             ;; Compute the mark.
5270             (setq gnus-tmp-unread (gnus-article-mark number))
5271
5272             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5273                                   gnus-tmp-header gnus-tmp-level)
5274                   gnus-newsgroup-data)
5275
5276             ;; Actually insert the line.
5277             (setq
5278              gnus-tmp-subject-or-nil
5279              (cond
5280               ((and gnus-thread-ignore-subject
5281                     gnus-tmp-prev-subject
5282                     (not (string= gnus-tmp-prev-subject simp-subject)))
5283                subject)
5284               ((zerop gnus-tmp-level)
5285                (if (and (eq gnus-summary-make-false-root 'empty)
5286                         (memq number gnus-tmp-gathered)
5287                         gnus-tmp-prev-subject
5288                         (string= gnus-tmp-prev-subject simp-subject))
5289                    gnus-summary-same-subject
5290                  subject))
5291               (t gnus-summary-same-subject)))
5292             (if (and (eq gnus-summary-make-false-root 'adopt)
5293                      (= gnus-tmp-level 1)
5294                      (memq number gnus-tmp-gathered))
5295                 (setq gnus-tmp-opening-bracket ?\<
5296                       gnus-tmp-closing-bracket ?\>)
5297               (setq gnus-tmp-opening-bracket ?\[
5298                     gnus-tmp-closing-bracket ?\]))
5299             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5300                 (gnus-make-thread-indent-array
5301                  (max (* 2 (length gnus-thread-indent-array))
5302                       gnus-tmp-level)))
5303             (setq
5304              gnus-tmp-indentation
5305              (aref gnus-thread-indent-array gnus-tmp-level)
5306              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5307              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5308                                 gnus-summary-default-score 0)
5309              gnus-tmp-score-char
5310              (if (or (null gnus-summary-default-score)
5311                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5312                          gnus-summary-zcore-fuzz))
5313                  ?                      ;Whitespace
5314                (if (< gnus-tmp-score gnus-summary-default-score)
5315                    gnus-score-below-mark gnus-score-over-mark))
5316              gnus-tmp-replied
5317              (cond ((memq number gnus-newsgroup-processable)
5318                     gnus-process-mark)
5319                    ((memq number gnus-newsgroup-cached)
5320                     gnus-cached-mark)
5321                    ((memq number gnus-newsgroup-replied)
5322                     gnus-replied-mark)
5323                    ((memq number gnus-newsgroup-forwarded)
5324                     gnus-forwarded-mark)
5325                    ((memq number gnus-newsgroup-saved)
5326                     gnus-saved-mark)
5327                    ((memq number gnus-newsgroup-recent)
5328                     gnus-recent-mark)
5329                    ((memq number gnus-newsgroup-unseen)
5330                     gnus-unseen-mark)
5331                    (t gnus-no-mark))
5332              gnus-tmp-downloaded
5333              (cond ((memq number gnus-newsgroup-undownloaded)
5334                     gnus-undownloaded-mark)
5335                    (gnus-newsgroup-agentized
5336                     gnus-downloaded-mark)
5337                    (t
5338                     gnus-no-mark))
5339              gnus-tmp-from (mail-header-from gnus-tmp-header)
5340              gnus-tmp-name
5341              (cond
5342               ((string-match "<[^>]+> *$" gnus-tmp-from)
5343                (setq beg-match (match-beginning 0))
5344                (or (and (string-match "^\".+\"" gnus-tmp-from)
5345                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5346                    (substring gnus-tmp-from 0 beg-match)))
5347               ((string-match "(.+)" gnus-tmp-from)
5348                (substring gnus-tmp-from
5349                           (1+ (match-beginning 0)) (1- (match-end 0))))
5350               (t gnus-tmp-from))
5351
5352              ;; Do the %B string
5353              gnus-tmp-thread-tree-header-string
5354              (cond
5355               ((not gnus-show-threads) "")
5356               ((zerop gnus-tmp-level)
5357                (cond ((cdar thread)
5358                       (or gnus-sum-thread-tree-root subject))
5359                      (gnus-tmp-new-adopts
5360                       (or gnus-sum-thread-tree-false-root subject))
5361                      (t
5362                       (or gnus-sum-thread-tree-single-indent subject))))
5363               (t
5364                (concat (apply 'concat
5365                               (mapcar (lambda (item)
5366                                         (if (= item 1)
5367                                             gnus-sum-thread-tree-vertical
5368                                           gnus-sum-thread-tree-indent))
5369                                       (cdr (reverse tree-stack))))
5370                        (if (nth 1 thread)
5371                            gnus-sum-thread-tree-leaf-with-other
5372                          gnus-sum-thread-tree-single-leaf)))))
5373             (when (string= gnus-tmp-name "")
5374               (setq gnus-tmp-name gnus-tmp-from))
5375             (unless (numberp gnus-tmp-lines)
5376               (setq gnus-tmp-lines -1))
5377             (if (= gnus-tmp-lines -1)
5378                 (setq gnus-tmp-lines "?")
5379               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5380             (gnus-put-text-property
5381              (point)
5382              (progn (eval gnus-summary-line-format-spec) (point))
5383              'gnus-number number)
5384             (when gnus-visual-p
5385               (forward-line -1)
5386               (gnus-summary-highlight-line)
5387               (when gnus-summary-update-hook
5388                 (gnus-run-hooks 'gnus-summary-update-hook))
5389               (forward-line 1))
5390
5391             (setq gnus-tmp-prev-subject simp-subject)))
5392
5393         (when (nth 1 thread)
5394           (push (list (max 0 gnus-tmp-level)
5395                       (copy-sequence tree-stack)
5396                       (nthcdr 1 thread))
5397                 stack))
5398         (push (if (nth 1 thread) 1 0) tree-stack)
5399         (incf gnus-tmp-level)
5400         (setq threads (if thread-end nil (cdar thread)))
5401         (if gnus-summary-display-while-building
5402             (if building-count
5403                 (progn
5404                   ;; use a set frequency
5405                   (setq building-line-count (1- building-line-count))
5406                   (when (= building-line-count 0)
5407                     (sit-for 0)
5408                     (setq building-line-count
5409                           gnus-summary-display-while-building)))
5410               ;; always
5411               (sit-for 0)))
5412         (unless threads
5413           (setq gnus-tmp-level 0)))))
5414   (gnus-message 7 "Generating summary...done"))
5415
5416 (defun gnus-summary-prepare-unthreaded (headers)
5417   "Generate an unthreaded summary buffer based on HEADERS."
5418   (let (header number mark)
5419
5420     (beginning-of-line)
5421
5422     (while headers
5423       ;; We may have to root out some bad articles...
5424       (when (memq (setq number (mail-header-number
5425                                 (setq header (pop headers))))
5426                   gnus-newsgroup-limit)
5427         ;; Mark article as read when it has a low score.
5428         (when (and gnus-summary-mark-below
5429                    (< (or (cdr (assq number gnus-newsgroup-scored))
5430                           gnus-summary-default-score 0)
5431                       gnus-summary-mark-below)
5432                    (not (gnus-summary-article-ancient-p number)))
5433           (setq gnus-newsgroup-unreads
5434                 (delq number gnus-newsgroup-unreads))
5435           (if gnus-newsgroup-auto-expire
5436               (push number gnus-newsgroup-expirable)
5437             (push (cons number gnus-low-score-mark)
5438                   gnus-newsgroup-reads)))
5439
5440         (setq mark (gnus-article-mark number))
5441         (push (gnus-data-make number mark (1+ (point)) header 0)
5442               gnus-newsgroup-data)
5443         (gnus-summary-insert-line
5444          header 0 number
5445          (memq number gnus-newsgroup-undownloaded)
5446          mark (memq number gnus-newsgroup-replied)
5447          (memq number gnus-newsgroup-expirable)
5448          (mail-header-subject header) nil
5449          (cdr (assq number gnus-newsgroup-scored))
5450          (memq number gnus-newsgroup-processable))))))
5451
5452 (defun gnus-summary-remove-list-identifiers ()
5453   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5454   (let ((regexp (if (consp gnus-list-identifiers)
5455                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5456                   gnus-list-identifiers))
5457         changed subject)
5458     (when regexp
5459       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5460       (dolist (header gnus-newsgroup-headers)
5461         (setq subject (mail-header-subject header)
5462               changed nil)
5463         (while (string-match regexp subject)
5464           (setq subject
5465                 (concat (substring subject 0 (match-beginning 1))
5466                         (substring subject (match-end 0)))
5467                 changed t))
5468         (when changed
5469           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5470             (setq subject
5471                   (concat (substring subject 0 (match-beginning 1))
5472                           (substring subject (match-end 1)))))
5473           (mail-header-set-subject header subject))))))
5474
5475 (defun gnus-fetch-headers (articles)
5476   "Fetch headers of ARTICLES."
5477   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5478     (gnus-message 5 "Fetching headers for %s..." name)
5479     (prog1
5480         (if (eq 'nov
5481                 (setq gnus-headers-retrieved-by
5482                       (gnus-retrieve-headers
5483                        articles gnus-newsgroup-name
5484                        ;; We might want to fetch old headers, but
5485                        ;; not if there is only 1 article.
5486                        (and (or (and
5487                                  (not (eq gnus-fetch-old-headers 'some))
5488                                  (not (numberp gnus-fetch-old-headers)))
5489                                 (> (length articles) 1))
5490                             gnus-fetch-old-headers))))
5491             (gnus-get-newsgroup-headers-xover
5492              articles nil nil gnus-newsgroup-name t)
5493           (gnus-get-newsgroup-headers))
5494       (gnus-message 5 "Fetching headers for %s...done" name))))
5495
5496 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5497   "Select newsgroup GROUP.
5498 If READ-ALL is non-nil, all articles in the group are selected.
5499 If SELECT-ARTICLES, only select those articles from GROUP."
5500   (let* ((entry (gnus-group-entry group))
5501          ;;!!! Dirty hack; should be removed.
5502          (gnus-summary-ignore-duplicates
5503           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5504               t
5505             gnus-summary-ignore-duplicates))
5506          (info (nth 2 entry))
5507          charset articles fetched-articles cached)
5508
5509     (unless (gnus-check-server
5510              (set (make-local-variable 'gnus-current-select-method)
5511                   (gnus-find-method-for-group group)))
5512       (error "Couldn't open server"))
5513     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5514
5515     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5516         (gnus-activate-group group)     ; Or we can activate it...
5517         (progn                          ; Or we bug out.
5518           (when (equal major-mode 'gnus-summary-mode)
5519             (gnus-kill-buffer (current-buffer)))
5520           (error
5521            "Couldn't activate group %s: %s"
5522            (mm-decode-coding-string group charset)
5523            (mm-decode-coding-string (gnus-status-message group) charset))))
5524
5525     (unless (gnus-request-group group t)
5526       (when (equal major-mode 'gnus-summary-mode)
5527         (gnus-kill-buffer (current-buffer)))
5528       (error "Couldn't request group %s: %s"
5529              (mm-decode-coding-string group charset)
5530              (mm-decode-coding-string (gnus-status-message group) charset)))
5531
5532     (when gnus-agent
5533       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5534
5535       (setq gnus-summary-use-undownloaded-faces
5536             (gnus-agent-find-parameter
5537              group
5538              'agent-enable-undownloaded-faces)))
5539
5540     (setq gnus-newsgroup-name group
5541           gnus-newsgroup-unselected nil
5542           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5543
5544     (let ((display (gnus-group-find-parameter group 'display)))
5545       (setq gnus-newsgroup-display
5546             (cond
5547              ((not (zerop (or (car-safe read-all) 0)))
5548               ;; The user entered the group with C-u SPC/RET, let's show
5549               ;; all articles.
5550               'gnus-not-ignore)
5551              ((eq display 'all)
5552               'gnus-not-ignore)
5553              ((arrayp display)
5554               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5555              ((numberp display)
5556               ;; The following is probably the "correct" solution, but
5557               ;; it makes Gnus fetch all headers and then limit the
5558               ;; articles (which is slow), so instead we hack the
5559               ;; select-articles parameter instead. -- Simon Josefsson
5560               ;; <jas@kth.se>
5561               ;;
5562               ;; (gnus-byte-compile
5563               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5564               ;;                         display)))))
5565               (setq select-articles
5566                     (gnus-uncompress-range
5567                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5568                              (if (> tmp 0)
5569                                  tmp
5570                                1))
5571                            (cdr (gnus-active group)))))
5572               nil)
5573              (t
5574               nil))))
5575
5576     (gnus-summary-setup-default-charset)
5577
5578     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5579     (when (gnus-virtual-group-p group)
5580       (setq cached gnus-newsgroup-cached))
5581
5582     (setq gnus-newsgroup-unreads
5583           (gnus-sorted-ndifference
5584            (gnus-sorted-ndifference gnus-newsgroup-unreads
5585                                     gnus-newsgroup-marked)
5586            gnus-newsgroup-dormant))
5587
5588     (setq gnus-newsgroup-processable nil)
5589
5590     (gnus-update-read-articles group gnus-newsgroup-unreads)
5591
5592     ;; Adjust and set lists of article marks.
5593     (when info
5594       (gnus-adjust-marked-articles info))
5595     (if (setq articles select-articles)
5596         (setq gnus-newsgroup-unselected
5597               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5598       (setq articles (gnus-articles-to-read group read-all)))
5599
5600     (cond
5601      ((null articles)
5602       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5603       'quit)
5604      ((eq articles 0) nil)
5605      (t
5606       ;; Init the dependencies hash table.
5607       (setq gnus-newsgroup-dependencies
5608             (gnus-make-hashtable (length articles)))
5609       (gnus-set-global-variables)
5610       ;; Retrieve the headers and read them in.
5611
5612       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5613
5614       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5615       (when cached
5616         (setq gnus-newsgroup-cached cached))
5617
5618       ;; Suppress duplicates?
5619       (when gnus-suppress-duplicates
5620         (gnus-dup-suppress-articles))
5621
5622       ;; Set the initial limit.
5623       (setq gnus-newsgroup-limit (copy-sequence articles))
5624       ;; Remove canceled articles from the list of unread articles.
5625       (setq fetched-articles
5626             (mapcar (lambda (headers) (mail-header-number headers))
5627                     gnus-newsgroup-headers))
5628       (setq gnus-newsgroup-articles fetched-articles)
5629       (setq gnus-newsgroup-unreads
5630             (gnus-sorted-nintersection
5631              gnus-newsgroup-unreads fetched-articles))
5632       (gnus-compute-unseen-list)
5633
5634       ;; Removed marked articles that do not exist.
5635       (gnus-update-missing-marks
5636        (gnus-sorted-difference articles fetched-articles))
5637       ;; We might want to build some more threads first.
5638       (when (and gnus-fetch-old-headers
5639                  (eq gnus-headers-retrieved-by 'nov))
5640         (if (eq gnus-fetch-old-headers 'invisible)
5641             (gnus-build-all-threads)
5642           (gnus-build-old-threads)))
5643       ;; Let the Gnus agent mark articles as read.
5644       (when gnus-agent
5645         (gnus-agent-get-undownloaded-list))
5646       ;; Remove list identifiers from subject
5647       (when gnus-list-identifiers
5648         (gnus-summary-remove-list-identifiers))
5649       ;; Check whether auto-expire is to be done in this group.
5650       (setq gnus-newsgroup-auto-expire
5651             (gnus-group-auto-expirable-p group))
5652       ;; Set up the article buffer now, if necessary.
5653       (unless (and gnus-single-article-buffer
5654                    (equal gnus-article-buffer "*Article*"))
5655         (gnus-article-setup-buffer))
5656       ;; First and last article in this newsgroup.
5657       (when gnus-newsgroup-headers
5658         (setq gnus-newsgroup-begin
5659               (mail-header-number (car gnus-newsgroup-headers))
5660               gnus-newsgroup-end
5661               (mail-header-number
5662                (gnus-last-element gnus-newsgroup-headers))))
5663       ;; GROUP is successfully selected.
5664       (or gnus-newsgroup-headers t)))))
5665
5666 (defun gnus-compute-unseen-list ()
5667   ;; The `seen' marks are treated specially.
5668   (if (not gnus-newsgroup-seen)
5669       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5670     (setq gnus-newsgroup-unseen
5671           (gnus-inverse-list-range-intersection
5672            gnus-newsgroup-articles gnus-newsgroup-seen))))
5673
5674 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5675
5676 (defun gnus-summary-display-make-predicate (display)
5677   (require 'gnus-agent)
5678   (when (= (length display) 1)
5679     (setq display (car display)))
5680   (unless gnus-summary-display-cache
5681     (dolist (elem (append '((unread . unread)
5682                             (read . read)
5683                             (unseen . unseen))
5684                           gnus-article-mark-lists))
5685       (push (cons (cdr elem)
5686                   (gnus-byte-compile    ;Why bother?
5687                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5688             gnus-summary-display-cache)))
5689   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5690         (gnus-category-predicate-cache gnus-summary-display-cache))
5691     (gnus-get-predicate display)))
5692
5693 ;; Uses the dynamically bound `gnus-number' variable.
5694 (defvar gnus-number)
5695 (defun gnus-article-marked-p (type &optional article)
5696   (let ((article (or article gnus-number)))
5697     (cond
5698      ((eq type 'tick)
5699       (memq article gnus-newsgroup-marked))
5700      ((eq type 'spam)
5701       (memq article gnus-newsgroup-spam-marked))
5702      ((eq type 'unsend)
5703       (memq article gnus-newsgroup-unsendable))
5704      ((eq type 'undownload)
5705       (memq article gnus-newsgroup-undownloaded))
5706      ((eq type 'download)
5707       (memq article gnus-newsgroup-downloadable))
5708      ((eq type 'unread)
5709       (memq article gnus-newsgroup-unreads))
5710      ((eq type 'read)
5711       (memq article gnus-newsgroup-reads))
5712      ((eq type 'dormant)
5713       (memq article gnus-newsgroup-dormant) )
5714      ((eq type 'expire)
5715       (memq article gnus-newsgroup-expirable))
5716      ((eq type 'reply)
5717       (memq article gnus-newsgroup-replied))
5718      ((eq type 'killed)
5719       (memq article gnus-newsgroup-killed))
5720      ((eq type 'bookmark)
5721       (assq article gnus-newsgroup-bookmarks))
5722      ((eq type 'score)
5723       (assq article gnus-newsgroup-scored))
5724      ((eq type 'save)
5725       (memq article gnus-newsgroup-saved))
5726      ((eq type 'cache)
5727       (memq article gnus-newsgroup-cached))
5728      ((eq type 'forward)
5729       (memq article gnus-newsgroup-forwarded))
5730      ((eq type 'seen)
5731       (not (memq article gnus-newsgroup-unseen)))
5732      ((eq type 'recent)
5733       (memq article gnus-newsgroup-recent))
5734      (t t))))
5735
5736 (defun gnus-articles-to-read (group &optional read-all)
5737   "Find out what articles the user wants to read."
5738   (let* ((articles
5739           ;; Select all articles if `read-all' is non-nil, or if there
5740           ;; are no unread articles.
5741           (if (or read-all
5742                   (and (zerop (length gnus-newsgroup-marked))
5743                        (zerop (length gnus-newsgroup-unreads)))
5744                   ;; Fetch all if the predicate is non-nil.
5745                   gnus-newsgroup-display)
5746               ;; We want to select the headers for all the articles in
5747               ;; the group, so we select either all the active
5748               ;; articles in the group, or (if that's nil), the
5749               ;; articles in the cache.
5750               (or
5751                (if gnus-newsgroup-maximum-articles
5752                    (let ((active (gnus-active group)))
5753                      (gnus-uncompress-range
5754                       (cons (max (car active)
5755                                  (- (cdr active)
5756                                     gnus-newsgroup-maximum-articles
5757                                     -1))
5758                             (cdr active))))
5759                  (gnus-uncompress-range (gnus-active group)))
5760                (gnus-cache-articles-in-group group))
5761             ;; Select only the "normal" subset of articles.
5762             (gnus-sorted-nunion
5763              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5764              gnus-newsgroup-unreads)))
5765          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5766          (scored (length scored-list))
5767          (number (length articles))
5768          (marked (+ (length gnus-newsgroup-marked)
5769                     (length gnus-newsgroup-dormant)))
5770          (select
5771           (cond
5772            ((numberp read-all)
5773             read-all)
5774            ((numberp gnus-newsgroup-display)
5775             gnus-newsgroup-display)
5776            (t
5777             (condition-case ()
5778                 (cond
5779                  ((and (or (<= scored marked) (= scored number))
5780                        (numberp gnus-large-newsgroup)
5781                        (> number gnus-large-newsgroup))
5782                   (let* ((cursor-in-echo-area nil)
5783                          (initial (gnus-parameter-large-newsgroup-initial
5784                                    gnus-newsgroup-name))
5785                          (input
5786                           (read-string
5787                            (format
5788                             "How many articles from %s (%s %d): "
5789                             (gnus-group-decoded-name gnus-newsgroup-name)
5790                             (if initial "max" "default")
5791                             number)
5792                            (if initial
5793                                (cons (number-to-string initial)
5794                                      0)))))
5795                     (if (string-match "^[ \t]*$" input) number input)))
5796                  ((and (> scored marked) (< scored number)
5797                        (> (- scored number) 20))
5798                   (let ((input
5799                          (read-string
5800                           (format "%s %s (%d scored, %d total): "
5801                                   "How many articles from"
5802                                   (gnus-group-decoded-name group)
5803                                   scored number))))
5804                     (if (string-match "^[ \t]*$" input)
5805                         number input)))
5806                  (t number))
5807               (quit
5808                (message "Quit getting the articles to read")
5809                nil))))))
5810     (setq select (if (stringp select) (string-to-number select) select))
5811     (if (or (null select) (zerop select))
5812         select
5813       (if (and (not (zerop scored)) (<= (abs select) scored))
5814           (progn
5815             (setq articles (sort scored-list '<))
5816             (setq number (length articles)))
5817         (setq articles (copy-sequence articles)))
5818
5819       (when (< (abs select) number)
5820         (if (< select 0)
5821             ;; Select the N oldest articles.
5822             (setcdr (nthcdr (1- (abs select)) articles) nil)
5823           ;; Select the N most recent articles.
5824           (setq articles (nthcdr (- number select) articles))))
5825       (setq gnus-newsgroup-unselected
5826             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5827       (when gnus-alter-articles-to-read-function
5828         (setq articles
5829               (sort
5830                (funcall gnus-alter-articles-to-read-function
5831                         gnus-newsgroup-name articles)
5832                '<)))
5833       articles)))
5834
5835 (defun gnus-killed-articles (killed articles)
5836   (let (out)
5837     (while articles
5838       (when (inline (gnus-member-of-range (car articles) killed))
5839         (push (car articles) out))
5840       (setq articles (cdr articles)))
5841     out))
5842
5843 (defun gnus-uncompress-marks (marks)
5844   "Uncompress the mark ranges in MARKS."
5845   (let ((uncompressed '(score bookmark))
5846         out)
5847     (while marks
5848       (if (memq (caar marks) uncompressed)
5849           (push (car marks) out)
5850         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5851       (setq marks (cdr marks)))
5852     out))
5853
5854 (defun gnus-article-mark-to-type (mark)
5855   "Return the type of MARK."
5856   (or (cadr (assq mark gnus-article-special-mark-lists))
5857       'list))
5858
5859 (defun gnus-article-unpropagatable-p (mark)
5860   "Return whether MARK should be propagated to back end."
5861   (memq mark gnus-article-unpropagated-mark-lists))
5862
5863 (defun gnus-adjust-marked-articles (info)
5864   "Set all article lists and remove all marks that are no longer valid."
5865   (let* ((marked-lists (gnus-info-marks info))
5866          (active (gnus-active (gnus-info-group info)))
5867          (min (car active))
5868          (max (cdr active))
5869          (types gnus-article-mark-lists)
5870          marks var articles article mark mark-type
5871          bgn end)
5872     ;; Hack to avoid adjusting marks for imap.
5873     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5874               'nnimap)
5875       (setq min 1))
5876
5877     (dolist (marks marked-lists)
5878       (setq mark (car marks)
5879             mark-type (gnus-article-mark-to-type mark)
5880             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5881
5882       ;; We set the variable according to the type of the marks list,
5883       ;; and then adjust the marks to a subset of the active articles.
5884       (cond
5885        ;; Adjust "simple" lists - compressed yet unsorted
5886        ((eq mark-type 'list)
5887         ;; Simultaneously uncompress and clip to active range
5888         ;; See gnus-uncompress-range for a description of possible marks
5889         (let (l lh)
5890           (if (not (cadr marks))
5891               (set var nil)
5892             (setq articles (if (numberp (cddr marks))
5893                                (list (cdr marks))
5894                              (cdr marks))
5895                   lh (cons nil nil)
5896                   l lh)
5897
5898             (while (setq article (pop articles))
5899               (cond ((consp article)
5900                      (setq bgn (max (car article) min)
5901                            end (min (cdr article) max))
5902                      (while (<= bgn end)
5903                        (setq l (setcdr l (cons bgn nil))
5904                              bgn (1+ bgn))))
5905                     ((and (<= min article)
5906                           (>= max article))
5907                      (setq l (setcdr l (cons article nil))))))
5908             (set var (cdr lh)))))
5909        ;; Adjust assocs.
5910        ((eq mark-type 'tuple)
5911         (set var (setq articles (cdr marks)))
5912         (when (not (listp (cdr (symbol-value var))))
5913           (set var (list (symbol-value var))))
5914         (when (not (listp (cdr articles)))
5915           (setq articles (list articles)))
5916         (while articles
5917           (when (or (not (consp (setq article (pop articles))))
5918                     (< (car article) min)
5919                     (> (car article) max))
5920             (set var (delq article (symbol-value var))))))
5921        ;; Adjust ranges (sloppily).
5922        ((eq mark-type 'range)
5923         (cond
5924          ((eq mark 'seen)
5925           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5926           ;; It should be (seen (NUM1 . NUM2)).
5927           (when (numberp (cddr marks))
5928             (setcdr marks (list (cdr marks))))
5929           (setq articles (cdr marks))
5930           (while (and articles
5931                       (or (and (consp (car articles))
5932                                (> min (cdar articles)))
5933                           (and (numberp (car articles))
5934                                (> min (car articles)))))
5935             (pop articles))
5936           (set var articles))))))))
5937
5938 (defun gnus-update-missing-marks (missing)
5939   "Go through the list of MISSING articles and remove them from the mark lists."
5940   (when missing
5941     (let (var m)
5942       ;; Go through all types.
5943       (dolist (elem gnus-article-mark-lists)
5944         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5945           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5946           (when (symbol-value var)
5947             ;; This list has articles.  So we delete all missing
5948             ;; articles from it.
5949             (setq m missing)
5950             (while m
5951               (set var (delq (pop m) (symbol-value var))))))))))
5952
5953 (defun gnus-update-marks ()
5954   "Enter the various lists of marked articles into the newsgroup info list."
5955   (let ((types gnus-article-mark-lists)
5956         (info (gnus-get-info gnus-newsgroup-name))
5957         type list newmarked symbol delta-marks)
5958     (when info
5959       ;; Add all marks lists to the list of marks lists.
5960       (while (setq type (pop types))
5961         (setq list (symbol-value
5962                     (setq symbol
5963                           (intern (format "gnus-newsgroup-%s" (car type))))))
5964
5965         (when list
5966           ;; Get rid of the entries of the articles that have the
5967           ;; default score.
5968           (when (and (eq (cdr type) 'score)
5969                      gnus-save-score
5970                      list)
5971             (let* ((arts list)
5972                    (prev (cons nil list))
5973                    (all prev))
5974               (while arts
5975                 (if (or (not (consp (car arts)))
5976                         (= (cdar arts) gnus-summary-default-score))
5977                     (setcdr prev (cdr arts))
5978                   (setq prev arts))
5979                 (setq arts (cdr arts)))
5980               (setq list (cdr all)))))
5981
5982         (when (eq (cdr type) 'seen)
5983           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5984
5985         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5986           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5987
5988         (when (and (gnus-check-backend-function
5989                     'request-set-mark gnus-newsgroup-name)
5990                    (not (gnus-article-unpropagatable-p (cdr type))))
5991           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5992                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5993                  (add (gnus-remove-from-range
5994                        (gnus-copy-sequence list) old)))
5995             (when add
5996               (push (list add 'add (list (cdr type))) delta-marks))
5997             (when del
5998               ;; Don't delete marks from outside the active range.  This
5999               ;; shouldn't happen, but is a sanity check.
6000               (setq del (gnus-sorted-range-intersection
6001                          (gnus-active gnus-newsgroup-name) del))
6002               (push (list del 'del (list (cdr type))) delta-marks))))
6003
6004         (when list
6005           (push (cons (cdr type) list) newmarked)))
6006
6007       (when delta-marks
6008         (unless (gnus-check-group gnus-newsgroup-name)
6009           (error "Can't open server for %s" gnus-newsgroup-name))
6010         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6011
6012       ;; Enter these new marks into the info of the group.
6013       (if (nthcdr 3 info)
6014           (setcar (nthcdr 3 info) newmarked)
6015         ;; Add the marks lists to the end of the info.
6016         (when newmarked
6017           (setcdr (nthcdr 2 info) (list newmarked))))
6018
6019       ;; Cut off the end of the info if there's nothing else there.
6020       (let ((i 5))
6021         (while (and (> i 2)
6022                     (not (nth i info)))
6023           (when (nthcdr (decf i) info)
6024             (setcdr (nthcdr i info) nil)))))))
6025
6026 (defun gnus-set-mode-line (where)
6027   "Set the mode line of the article or summary buffers.
6028 If WHERE is `summary', the summary mode line format will be used."
6029   ;; Is this mode line one we keep updated?
6030   (when (and (memq where gnus-updated-mode-lines)
6031              (symbol-value
6032               (intern (format "gnus-%s-mode-line-format-spec" where))))
6033     (let (mode-string)
6034       ;; We evaluate this in the summary buffer since these
6035       ;; variables are buffer-local to that buffer.
6036       (with-current-buffer gnus-summary-buffer
6037         ;; We bind all these variables that are used in the `eval' form
6038         ;; below.
6039         (let* ((mformat (symbol-value
6040                          (intern
6041                           (format "gnus-%s-mode-line-format-spec" where))))
6042                (gnus-tmp-group-name (gnus-mode-string-quote
6043                                      (gnus-group-decoded-name
6044                                       gnus-newsgroup-name)))
6045                (gnus-tmp-article-number (or gnus-current-article 0))
6046                (gnus-tmp-unread gnus-newsgroup-unreads)
6047                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6048                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6049                (gnus-tmp-unread-and-unselected
6050                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6051                             (zerop gnus-tmp-unselected))
6052                        "")
6053                       ((zerop gnus-tmp-unselected)
6054                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6055                       (t (format "{%d(+%d) more}"
6056                                  gnus-tmp-unread-and-unticked
6057                                  gnus-tmp-unselected))))
6058                (gnus-tmp-subject
6059                 (if (and gnus-current-headers
6060                          (vectorp gnus-current-headers))
6061                     (gnus-mode-string-quote
6062                      (mail-header-subject gnus-current-headers))
6063                   ""))
6064                bufname-length max-len
6065                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6066           (setq mode-string (eval mformat))
6067           (setq bufname-length (if (string-match "%b" mode-string)
6068                                    (- (length
6069                                        (buffer-name
6070                                         (if (eq where 'summary)
6071                                             nil
6072                                           (get-buffer gnus-article-buffer))))
6073                                       2)
6074                                  0))
6075           (setq max-len (max 4 (if gnus-mode-non-string-length
6076                                    (- (window-width)
6077                                       gnus-mode-non-string-length
6078                                       bufname-length)
6079                                  (length mode-string))))
6080           ;; We might have to chop a bit of the string off...
6081           (when (> (length mode-string) max-len)
6082             (setq mode-string
6083                   (concat (truncate-string-to-width mode-string (- max-len 3))
6084                           "...")))))
6085       ;; Update the mode line.
6086       (setq mode-line-buffer-identification
6087             (gnus-mode-line-buffer-identification (list mode-string)))
6088       (set-buffer-modified-p t))))
6089
6090 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6091   "Go through the HEADERS list and add all Xrefs to a hash table.
6092 The resulting hash table is returned, or nil if no Xrefs were found."
6093   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6094          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6095          (xref-hashtb (gnus-make-hashtable))
6096          start group entry number xrefs header)
6097     (while headers
6098       (setq header (pop headers))
6099       (when (and (setq xrefs (mail-header-xref header))
6100                  (not (memq (setq number (mail-header-number header))
6101                             unreads)))
6102         (setq start 0)
6103         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6104           (setq start (match-end 0))
6105           (setq group (if prefix
6106                           (concat prefix (substring xrefs (match-beginning 1)
6107                                                     (match-end 1)))
6108                         (substring xrefs (match-beginning 1) (match-end 1))))
6109           (setq number
6110                 (string-to-number (substring xrefs (match-beginning 2)
6111                                           (match-end 2))))
6112           (if (setq entry (gnus-gethash group xref-hashtb))
6113               (setcdr entry (cons number (cdr entry)))
6114             (gnus-sethash group (cons number nil) xref-hashtb)))))
6115     (and start xref-hashtb)))
6116
6117 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6118   "Look through all the headers and mark the Xrefs as read."
6119   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6120         name info xref-hashtb idlist method nth4)
6121     (with-current-buffer gnus-group-buffer
6122       (when (setq xref-hashtb
6123                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6124         (mapatoms
6125          (lambda (group)
6126            (unless (string= from-newsgroup (setq name (symbol-name group)))
6127              (setq idlist (symbol-value group))
6128              ;; Dead groups are not updated.
6129              (and (prog1
6130                       (setq info (gnus-get-info name))
6131                     (when (stringp (setq nth4 (gnus-info-method info)))
6132                       (setq nth4 (gnus-server-to-method nth4))))
6133                   ;; Only do the xrefs if the group has the same
6134                   ;; select method as the group we have just read.
6135                   (or (gnus-methods-equal-p
6136                        nth4 (gnus-find-method-for-group from-newsgroup))
6137                       virtual
6138                       (equal nth4 (setq method (gnus-find-method-for-group
6139                                                 from-newsgroup)))
6140                       (and (equal (car nth4) (car method))
6141                            (equal (nth 1 nth4) (nth 1 method))))
6142                   gnus-use-cross-reference
6143                   (or (not (eq gnus-use-cross-reference t))
6144                       virtual
6145                       ;; Only do cross-references on subscribed
6146                       ;; groups, if that is what is wanted.
6147                       (<= (gnus-info-level info) gnus-level-subscribed))
6148                   (gnus-group-make-articles-read name idlist))))
6149          xref-hashtb)))))
6150
6151 (defun gnus-compute-read-articles (group articles)
6152   (let* ((entry (gnus-group-entry group))
6153          (info (nth 2 entry))
6154          (active (gnus-active group))
6155          ninfo)
6156     (when entry
6157       ;; First peel off all invalid article numbers.
6158       (when active
6159         (let ((ids articles)
6160               id first)
6161           (while (setq id (pop ids))
6162             (when (and first (> id (cdr active)))
6163               ;; We'll end up in this situation in one particular
6164               ;; obscure situation.  If you re-scan a group and get
6165               ;; a new article that is cross-posted to a different
6166               ;; group that has not been re-scanned, you might get
6167               ;; crossposted article that has a higher number than
6168               ;; Gnus believes possible.  So we re-activate this
6169               ;; group as well.  This might mean doing the
6170               ;; crossposting thingy will *increase* the number
6171               ;; of articles in some groups.  Tsk, tsk.
6172               (setq active (or (gnus-activate-group group) active)))
6173             (when (or (> id (cdr active))
6174                       (< id (car active)))
6175               (setq articles (delq id articles))))))
6176       ;; If the read list is nil, we init it.
6177       (if (and active
6178                (null (gnus-info-read info))
6179                (> (car active) 1))
6180           (setq ninfo (cons 1 (1- (car active))))
6181         (setq ninfo (gnus-info-read info)))
6182       ;; Then we add the read articles to the range.
6183       (gnus-add-to-range
6184        ninfo (setq articles (sort articles '<))))))
6185
6186 (defun gnus-group-make-articles-read (group articles)
6187   "Update the info of GROUP to say that ARTICLES are read."
6188   (let* ((num 0)
6189          (entry (gnus-group-entry group))
6190          (info (nth 2 entry))
6191          (active (gnus-active group))
6192          range)
6193     (when entry
6194       (setq range (gnus-compute-read-articles group articles))
6195       (with-current-buffer gnus-group-buffer
6196         (gnus-undo-register
6197           `(progn
6198              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6199              (gnus-info-set-read ',info ',(gnus-info-read info))
6200              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6201              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6202              (gnus-group-update-group ,group t))))
6203       ;; Add the read articles to the range.
6204       (gnus-info-set-read info range)
6205       (gnus-request-set-mark group (list (list range 'add '(read))))
6206       ;; Then we have to re-compute how many unread
6207       ;; articles there are in this group.
6208       (when active
6209         (cond
6210          ((not range)
6211           (setq num (- (1+ (cdr active)) (car active))))
6212          ((not (listp (cdr range)))
6213           (setq num (- (cdr active) (- (1+ (cdr range))
6214                                        (car range)))))
6215          (t
6216           (while range
6217             (if (numberp (car range))
6218                 (setq num (1+ num))
6219               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6220             (setq range (cdr range)))
6221           (setq num (- (cdr active) num))))
6222         ;; Update the number of unread articles.
6223         (setcar entry num)
6224         ;; Update the group buffer.
6225         (unless (gnus-ephemeral-group-p group)
6226           (gnus-group-update-group group t))))))
6227
6228 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6229   (let ((cur nntp-server-buffer)
6230         (dependencies
6231          (or dependencies
6232              (with-current-buffer gnus-summary-buffer
6233                gnus-newsgroup-dependencies)))
6234         headers id end ref number
6235         (mail-parse-charset gnus-newsgroup-charset)
6236         (mail-parse-ignored-charsets
6237          (save-current-buffer (condition-case nil
6238                                   (set-buffer gnus-summary-buffer)
6239                                 (error))
6240                               gnus-newsgroup-ignored-charsets)))
6241     (with-current-buffer nntp-server-buffer
6242       ;; Translate all TAB characters into SPACE characters.
6243       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6244       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6245       (ietf-drums-unfold-fws)
6246       (gnus-run-hooks 'gnus-parse-headers-hook)
6247       (let ((case-fold-search t)
6248             in-reply-to header p lines chars)
6249         (goto-char (point-min))
6250         ;; Search to the beginning of the next header.  Error messages
6251         ;; do not begin with 2 or 3.
6252         (while (re-search-forward "^[23][0-9]+ " nil t)
6253           (setq id nil
6254                 ref nil)
6255           ;; This implementation of this function, with nine
6256           ;; search-forwards instead of the one re-search-forward and
6257           ;; a case (which basically was the old function) is actually
6258           ;; about twice as fast, even though it looks messier.  You
6259           ;; can't have everything, I guess.  Speed and elegance
6260           ;; doesn't always go hand in hand.
6261           (setq
6262            header
6263            (vector
6264             ;; Number.
6265             (prog1
6266                 (setq number (read cur))
6267               (end-of-line)
6268               (setq p (point))
6269               (narrow-to-region (point)
6270                                 (or (and (search-forward "\n.\n" nil t)
6271                                          (- (point) 2))
6272                                     (point))))
6273             ;; Subject.
6274             (progn
6275               (goto-char p)
6276               (if (search-forward "\nsubject:" nil t)
6277                   (funcall gnus-decode-encoded-word-function
6278                            (nnheader-header-value))
6279                 "(none)"))
6280             ;; From.
6281             (progn
6282               (goto-char p)
6283               (if (search-forward "\nfrom:" nil t)
6284                   (funcall gnus-decode-encoded-address-function
6285                            (nnheader-header-value))
6286                 "(nobody)"))
6287             ;; Date.
6288             (progn
6289               (goto-char p)
6290               (if (search-forward "\ndate:" nil t)
6291                   (nnheader-header-value) ""))
6292             ;; Message-ID.
6293             (progn
6294               (goto-char p)
6295               (setq id (if (re-search-forward
6296                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6297                            ;; We do it this way to make sure the Message-ID
6298                            ;; is (somewhat) syntactically valid.
6299                            (buffer-substring (match-beginning 1)
6300                                              (match-end 1))
6301                          ;; If there was no message-id, we just fake one
6302                          ;; to make subsequent routines simpler.
6303                          (nnheader-generate-fake-message-id number))))
6304             ;; References.
6305             (progn
6306               (goto-char p)
6307               (if (search-forward "\nreferences:" nil t)
6308                   (progn
6309                     (setq end (point))
6310                     (prog1
6311                         (nnheader-header-value)
6312                       (setq ref
6313                             (buffer-substring
6314                              (progn
6315                                (end-of-line)
6316                                (search-backward ">" end t)
6317                                (1+ (point)))
6318                              (progn
6319                                (search-backward "<" end t)
6320                                (point))))))
6321                 ;; Get the references from the in-reply-to header if there
6322                 ;; were no references and the in-reply-to header looks
6323                 ;; promising.
6324                 (if (and (search-forward "\nin-reply-to:" nil t)
6325                          (setq in-reply-to (nnheader-header-value))
6326                          (string-match "<[^>]+>" in-reply-to))
6327                     (let (ref2)
6328                       (setq ref (substring in-reply-to (match-beginning 0)
6329                                            (match-end 0)))
6330                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6331                         (setq ref2 (substring in-reply-to (match-beginning 0)
6332                                               (match-end 0)))
6333                         (when (> (length ref2) (length ref))
6334                           (setq ref ref2)))
6335                       ref)
6336                   (setq ref nil))))
6337             ;; Chars.
6338             (progn
6339               (goto-char p)
6340               (if (search-forward "\nchars: " nil t)
6341                   (if (numberp (setq chars (ignore-errors (read cur))))
6342                       chars -1)
6343                 -1))
6344             ;; Lines.
6345             (progn
6346               (goto-char p)
6347               (if (search-forward "\nlines: " nil t)
6348                   (if (numberp (setq lines (ignore-errors (read cur))))
6349                       lines -1)
6350                 -1))
6351             ;; Xref.
6352             (progn
6353               (goto-char p)
6354               (and (search-forward "\nxref:" nil t)
6355                    (nnheader-header-value)))
6356             ;; Extra.
6357             (when gnus-extra-headers
6358               (let ((extra gnus-extra-headers)
6359                     out)
6360                 (while extra
6361                   (goto-char p)
6362                   (when (search-forward
6363                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6364                     (push (cons (car extra) (nnheader-header-value))
6365                           out))
6366                   (pop extra))
6367                 out))))
6368           (when (equal id ref)
6369             (setq ref nil))
6370
6371           (when gnus-alter-header-function
6372             (funcall gnus-alter-header-function header)
6373             (setq id (mail-header-id header)
6374                   ref (gnus-parent-id (mail-header-references header))))
6375
6376           (when (setq header
6377                       (gnus-dependencies-add-header
6378                        header dependencies force-new))
6379             (push header headers))
6380           (goto-char (point-max))
6381           (widen))
6382         (nreverse headers)))))
6383
6384 ;; Goes through the xover lines and returns a list of vectors
6385 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6386                                                   force-new dependencies
6387                                                   group also-fetch-heads)
6388   "Parse the news overview data in the server buffer.
6389 Return a list of headers that match SEQUENCE (see
6390 `nntp-retrieve-headers')."
6391   ;; Get the Xref when the users reads the articles since most/some
6392   ;; NNTP servers do not include Xrefs when using XOVER.
6393   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6394   (let ((mail-parse-charset gnus-newsgroup-charset)
6395         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6396         (cur nntp-server-buffer)
6397         (dependencies (or dependencies gnus-newsgroup-dependencies))
6398         (allp (cond
6399                ((eq gnus-read-all-available-headers t)
6400                 t)
6401                ((and (stringp gnus-read-all-available-headers)
6402                      group)
6403                 (string-match gnus-read-all-available-headers group))
6404                (t
6405                 nil)))
6406         number headers header)
6407     (with-current-buffer nntp-server-buffer
6408       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6409       ;; Allow the user to mangle the headers before parsing them.
6410       (gnus-run-hooks 'gnus-parse-headers-hook)
6411       (goto-char (point-min))
6412       (gnus-parse-without-error
6413         (while (and (or sequence allp)
6414                     (not (eobp)))
6415           (setq number (read cur))
6416           (when (not allp)
6417             (while (and sequence
6418                         (< (car sequence) number))
6419               (setq sequence (cdr sequence))))
6420           (when (and (or allp
6421                          (and sequence
6422                               (eq number (car sequence))))
6423                      (progn
6424                        (setq sequence (cdr sequence))
6425                        (setq header (inline
6426                                       (gnus-nov-parse-line
6427                                        number dependencies force-new)))))
6428             (push header headers))
6429           (forward-line 1)))
6430       ;; A common bug in inn is that if you have posted an article and
6431       ;; then retrieves the active file, it will answer correctly --
6432       ;; the new article is included.  However, a NOV entry for the
6433       ;; article may not have been generated yet, so this may fail.
6434       ;; We work around this problem by retrieving the last few
6435       ;; headers using HEAD.
6436       (if (or (not also-fetch-heads)
6437               (not sequence))
6438           ;; We (probably) got all the headers.
6439           (nreverse headers)
6440         (let ((gnus-nov-is-evil t))
6441           (nconc
6442            (nreverse headers)
6443            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6444              (gnus-get-newsgroup-headers))))))))
6445
6446 (defun gnus-article-get-xrefs ()
6447   "Fill in the Xref value in `gnus-current-headers', if necessary.
6448 This is meant to be called in `gnus-article-internal-prepare-hook'."
6449   (let ((headers (with-current-buffer gnus-summary-buffer
6450                    gnus-current-headers)))
6451     (or (not gnus-use-cross-reference)
6452         (not headers)
6453         (and (mail-header-xref headers)
6454              (not (string= (mail-header-xref headers) "")))
6455         (let ((case-fold-search t)
6456               xref)
6457           (save-restriction
6458             (nnheader-narrow-to-headers)
6459             (goto-char (point-min))
6460             (when (or (and (not (eobp))
6461                            (eq (downcase (char-after)) ?x)
6462                            (looking-at "Xref:"))
6463                       (search-forward "\nXref:" nil t))
6464               (goto-char (1+ (match-end 0)))
6465               (setq xref (buffer-substring (point) (point-at-eol)))
6466               (mail-header-set-xref headers xref)))))))
6467
6468 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6469   "Find article ID and insert the summary line for that article.
6470 OLD-HEADER can either be a header or a line number to insert
6471 the subject line on."
6472   (let* ((line (and (numberp old-header) old-header))
6473          (old-header (and (vectorp old-header) old-header))
6474          (header (cond ((and old-header use-old-header)
6475                         old-header)
6476                        ((and (numberp id)
6477                              (gnus-number-to-header id))
6478                         (gnus-number-to-header id))
6479                        (t
6480                         (gnus-read-header id))))
6481          (number (and (numberp id) id))
6482          d)
6483     (when header
6484       ;; Rebuild the thread that this article is part of and go to the
6485       ;; article we have fetched.
6486       (when (and (not gnus-show-threads)
6487                  old-header)
6488         (when (and number
6489                    (setq d (gnus-data-find (mail-header-number old-header))))
6490           (goto-char (gnus-data-pos d))
6491           (gnus-data-remove
6492            number
6493            (- (point-at-bol)
6494               (prog1
6495                   (1+ (point-at-eol))
6496                 (gnus-delete-line))))))
6497       ;; Remove list identifiers from subject.
6498       (when gnus-list-identifiers
6499         (let ((gnus-newsgroup-headers (list header)))
6500           (gnus-summary-remove-list-identifiers)))
6501       (when old-header
6502         (mail-header-set-number header (mail-header-number old-header)))
6503       (setq gnus-newsgroup-sparse
6504             (delq (setq number (mail-header-number header))
6505                   gnus-newsgroup-sparse))
6506       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6507       (push number gnus-newsgroup-limit)
6508       (gnus-rebuild-thread (mail-header-id header) line)
6509       (gnus-summary-goto-subject number nil t))
6510     (when (and (numberp number)
6511                (> number 0))
6512       ;; We have to update the boundaries even if we can't fetch the
6513       ;; article if ID is a number -- so that the next `P' or `N'
6514       ;; command will fetch the previous (or next) article even
6515       ;; if the one we tried to fetch this time has been canceled.
6516       (when (> number gnus-newsgroup-end)
6517         (setq gnus-newsgroup-end number))
6518       (when (< number gnus-newsgroup-begin)
6519         (setq gnus-newsgroup-begin number))
6520       (setq gnus-newsgroup-unselected
6521             (delq number gnus-newsgroup-unselected)))
6522     ;; Report back a success?
6523     (and header (mail-header-number header))))
6524
6525 ;;; Process/prefix in the summary buffer
6526
6527 (defun gnus-summary-work-articles (n)
6528   "Return a list of articles to be worked upon.
6529 The prefix argument, the list of process marked articles, and the
6530 current article will be taken into consideration."
6531   (with-current-buffer gnus-summary-buffer
6532     (cond
6533      (n
6534       ;; A numerical prefix has been given.
6535       (setq n (prefix-numeric-value n))
6536       (let ((backward (< n 0))
6537             (n (abs (prefix-numeric-value n)))
6538             articles article)
6539         (save-excursion
6540           (while
6541               (and (> n 0)
6542                    (push (setq article (gnus-summary-article-number))
6543                          articles)
6544                    (if backward
6545                        (gnus-summary-find-prev nil article)
6546                      (gnus-summary-find-next nil article)))
6547             (decf n)))
6548         (nreverse articles)))
6549      ((and (gnus-region-active-p) (mark))
6550       (message "region active")
6551       ;; Work on the region between point and mark.
6552       (let ((max (max (point) (mark)))
6553             articles article)
6554         (save-excursion
6555           (goto-char (min (point) (mark)))
6556           (while
6557               (and
6558                (push (setq article (gnus-summary-article-number)) articles)
6559                (gnus-summary-find-next nil article)
6560                (< (point) max)))
6561           (nreverse articles))))
6562      (gnus-newsgroup-processable
6563       ;; There are process-marked articles present.
6564       ;; Save current state.
6565       (gnus-summary-save-process-mark)
6566       ;; Return the list.
6567       (reverse gnus-newsgroup-processable))
6568      (t
6569       ;; Just return the current article.
6570       (list (gnus-summary-article-number))))))
6571
6572 (defmacro gnus-summary-iterate (arg &rest forms)
6573   "Iterate over the process/prefixed articles and do FORMS.
6574 ARG is the interactive prefix given to the command.  FORMS will be
6575 executed with point over the summary line of the articles."
6576   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6577     `(let ((,articles (gnus-summary-work-articles ,arg)))
6578        (while ,articles
6579          (gnus-summary-goto-subject (car ,articles))
6580          ,@forms
6581          (pop ,articles)))))
6582
6583 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6584 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6585
6586 (defun gnus-summary-save-process-mark ()
6587   "Push the current set of process marked articles on the stack."
6588   (interactive)
6589   (push (copy-sequence gnus-newsgroup-processable)
6590         gnus-newsgroup-process-stack))
6591
6592 (defun gnus-summary-kill-process-mark ()
6593   "Push the current set of process marked articles on the stack and unmark."
6594   (interactive)
6595   (gnus-summary-save-process-mark)
6596   (gnus-summary-unmark-all-processable))
6597
6598 (defun gnus-summary-yank-process-mark ()
6599   "Pop the last process mark state off the stack and restore it."
6600   (interactive)
6601   (unless gnus-newsgroup-process-stack
6602     (error "Empty mark stack"))
6603   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6604
6605 (defun gnus-summary-process-mark-set (set)
6606   "Make SET into the current process marked articles."
6607   (gnus-summary-unmark-all-processable)
6608   (mapc 'gnus-summary-set-process-mark set))
6609
6610 ;;; Searching and stuff
6611
6612 (defun gnus-summary-search-group (&optional backward use-level)
6613   "Search for next unread newsgroup.
6614 If optional argument BACKWARD is non-nil, search backward instead."
6615   (with-current-buffer gnus-group-buffer
6616     (when (gnus-group-search-forward
6617            backward nil (if use-level (gnus-group-group-level) nil))
6618       (gnus-group-group-name))))
6619
6620 (defun gnus-summary-best-group (&optional exclude-group)
6621   "Find the name of the best unread group.
6622 If EXCLUDE-GROUP, do not go to this group."
6623   (with-current-buffer gnus-group-buffer
6624     (save-excursion
6625       (gnus-group-best-unread-group exclude-group))))
6626
6627 (defun gnus-summary-find-next (&optional unread article backward)
6628   (if backward
6629       (gnus-summary-find-prev unread article)
6630     (let* ((dummy (gnus-summary-article-intangible-p))
6631            (article (or article (gnus-summary-article-number)))
6632            (data (gnus-data-find-list article))
6633            result)
6634       (when (and (not dummy)
6635                  (or (not gnus-summary-check-current)
6636                      (not unread)
6637                      (not (gnus-data-unread-p (car data)))))
6638         (setq data (cdr data)))
6639       (when (setq result
6640                   (if unread
6641                       (progn
6642                         (while data
6643                           (unless (memq (gnus-data-number (car data))
6644                                         (cond
6645                                          ((eq gnus-auto-goto-ignores
6646                                               'always-undownloaded)
6647                                           gnus-newsgroup-undownloaded)
6648                                          (gnus-plugged
6649                                           nil)
6650                                          ((eq gnus-auto-goto-ignores
6651                                               'unfetched)
6652                                           gnus-newsgroup-unfetched)
6653                                          ((eq gnus-auto-goto-ignores
6654                                               'undownloaded)
6655                                           gnus-newsgroup-undownloaded)))
6656                             (when (gnus-data-unread-p (car data))
6657                               (setq result (car data)
6658                                     data nil)))
6659                           (setq data (cdr data)))
6660                         result)
6661                     (car data)))
6662         (goto-char (gnus-data-pos result))
6663         (gnus-data-number result)))))
6664
6665 (defun gnus-summary-find-prev (&optional unread article)
6666   (let* ((eobp (eobp))
6667          (article (or article (gnus-summary-article-number)))
6668          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6669          result)
6670     (when (and (not eobp)
6671                (or (not gnus-summary-check-current)
6672                    (not unread)
6673                    (not (gnus-data-unread-p (car data)))))
6674       (setq data (cdr data)))
6675     (when (setq result
6676                 (if unread
6677                     (progn
6678                       (while data
6679                         (unless (memq (gnus-data-number (car data))
6680                                       (cond
6681                                        ((eq gnus-auto-goto-ignores
6682                                             'always-undownloaded)
6683                                         gnus-newsgroup-undownloaded)
6684                                        (gnus-plugged
6685                                         nil)
6686                                        ((eq gnus-auto-goto-ignores
6687                                             'unfetched)
6688                                         gnus-newsgroup-unfetched)
6689                                        ((eq gnus-auto-goto-ignores
6690                                             'undownloaded)
6691                                         gnus-newsgroup-undownloaded)))
6692                           (when (gnus-data-unread-p (car data))
6693                             (setq result (car data)
6694                                   data nil)))
6695                         (setq data (cdr data)))
6696                       result)
6697                   (car data)))
6698       (goto-char (gnus-data-pos result))
6699       (gnus-data-number result))))
6700
6701 (defun gnus-summary-find-subject (subject &optional unread backward article)
6702   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6703          (article (or article (gnus-summary-article-number)))
6704          (articles (gnus-data-list backward))
6705          (arts (gnus-data-find-list article articles))
6706          result)
6707     (when (or (not gnus-summary-check-current)
6708               (not unread)
6709               (not (gnus-data-unread-p (car arts))))
6710       (setq arts (cdr arts)))
6711     (while arts
6712       (and (or (not unread)
6713                (gnus-data-unread-p (car arts)))
6714            (vectorp (gnus-data-header (car arts)))
6715            (gnus-subject-equal
6716             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6717            (setq result (car arts)
6718                  arts nil))
6719       (setq arts (cdr arts)))
6720     (and result
6721          (goto-char (gnus-data-pos result))
6722          (gnus-data-number result))))
6723
6724 (defun gnus-summary-search-forward (&optional unread subject backward)
6725   "Search forward for an article.
6726 If UNREAD, look for unread articles.  If SUBJECT, look for
6727 articles with that subject.  If BACKWARD, search backward instead."
6728   (cond (subject (gnus-summary-find-subject subject unread backward))
6729         (backward (gnus-summary-find-prev unread))
6730         (t (gnus-summary-find-next unread))))
6731
6732 (defun gnus-recenter (&optional n)
6733   "Center point in window and redisplay frame.
6734 Also do horizontal recentering."
6735   (interactive "P")
6736   (when (and gnus-auto-center-summary
6737              (not (eq gnus-auto-center-summary 'vertical)))
6738     (gnus-horizontal-recenter))
6739   (if (fboundp 'recenter-top-bottom)
6740       (recenter-top-bottom n)
6741     (recenter n)))
6742
6743 (put 'gnus-recenter 'isearch-scroll t)
6744
6745 (defun gnus-forward-line-ignore-invisible (n)
6746   "Move N lines forward (backward if N is negative).
6747 Like forward-line, but skip over (and don't count) invisible lines."
6748   (let (done)
6749     (while (and (> n 0) (not done))
6750       ;; If the following character is currently invisible,
6751       ;; skip all characters with that same `invisible' property value.
6752       (while (gnus-invisible-p (point))
6753         (goto-char (gnus-next-char-property-change (point))))
6754       (forward-line 1)
6755       (if (eobp)
6756           (setq done t)
6757         (setq n (1- n))))
6758     (while (and (< n 0) (not done))
6759       (forward-line -1)
6760       (if (bobp) (setq done t)
6761         (setq n (1+ n))
6762         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6763           (goto-char (gnus-previous-char-property-change (point))))))))
6764
6765 (defun gnus-summary-recenter ()
6766   "Center point in the summary window.
6767 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6768 displayed, no centering will be performed."
6769   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6770   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6771   (interactive)
6772   ;; The user has to want it.
6773   (when gnus-auto-center-summary
6774     (let* ((top (cond ((< (window-height) 4) 0)
6775                       ((< (window-height) 7) 1)
6776                       (t (if (numberp gnus-auto-center-summary)
6777                              gnus-auto-center-summary
6778                            (/ (1- (window-height)) 2)))))
6779            (height (1- (window-height)))
6780            (bottom (save-excursion
6781                      (goto-char (point-max))
6782                      (gnus-forward-line-ignore-invisible (- height))
6783                      (point)))
6784            (window (get-buffer-window (current-buffer))))
6785       (when (get-buffer-window gnus-article-buffer)
6786         ;; Only do recentering when the article buffer is displayed,
6787         ;; Set the window start to either `bottom', which is the biggest
6788         ;; possible valid number, or the second line from the top,
6789         ;; whichever is the least.
6790         (let ((top-pos (save-excursion
6791                          (gnus-forward-line-ignore-invisible (- top))
6792                          (point))))
6793           (if (> bottom top-pos)
6794               ;; Keep the second line from the top visible
6795               (set-window-start window top-pos)
6796             ;; Try to keep the bottom line visible; if it's partially
6797             ;; obscured, either scroll one more line to make it fully
6798             ;; visible, or revert to using TOP-POS.
6799             (save-excursion
6800               (goto-char (point-max))
6801               (gnus-forward-line-ignore-invisible -1)
6802               (let ((last-line-start (point)))
6803                 (goto-char bottom)
6804                 (set-window-start window (point) t)
6805                 (when (not (pos-visible-in-window-p last-line-start window))
6806                   (gnus-forward-line-ignore-invisible 1)
6807                   (set-window-start window (min (point) top-pos) t)))))))
6808       ;; Do horizontal recentering while we're at it.
6809       (when (and (get-buffer-window (current-buffer) t)
6810                  (not (eq gnus-auto-center-summary 'vertical)))
6811         (let ((selected (selected-window)))
6812           (select-window (get-buffer-window (current-buffer) t))
6813           (gnus-summary-position-point)
6814           (gnus-horizontal-recenter)
6815           (select-window selected))))))
6816
6817 (defun gnus-summary-jump-to-group (newsgroup)
6818   "Move point to NEWSGROUP in group mode buffer."
6819   ;; Keep update point of group mode buffer if visible.
6820   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6821       (save-window-excursion
6822         ;; Take care of tree window mode.
6823         (when (get-buffer-window gnus-group-buffer)
6824           (pop-to-buffer gnus-group-buffer))
6825         (gnus-group-jump-to-group newsgroup))
6826     (save-excursion
6827       ;; Take care of tree window mode.
6828       (if (get-buffer-window gnus-group-buffer 0)
6829           (pop-to-buffer gnus-group-buffer)
6830         (set-buffer gnus-group-buffer))
6831       (gnus-group-jump-to-group newsgroup))))
6832
6833 ;; This function returns a list of article numbers based on the
6834 ;; difference between the ranges of read articles in this group and
6835 ;; the range of active articles.
6836 (defun gnus-list-of-unread-articles (group)
6837   (let* ((read (gnus-info-read (gnus-get-info group)))
6838          (active (or (gnus-active group) (gnus-activate-group group)))
6839          (last (or (cdr active)
6840                    (error "Group %s couldn't be activated " group)))
6841          (bottom (if gnus-newsgroup-maximum-articles
6842                      (max (car active)
6843                           (- last gnus-newsgroup-maximum-articles -1))
6844                    (car active)))
6845          first nlast unread)
6846     ;; If none are read, then all are unread.
6847     (if (not read)
6848         (setq first bottom)
6849       ;; If the range of read articles is a single range, then the
6850       ;; first unread article is the article after the last read
6851       ;; article.  Sounds logical, doesn't it?
6852       (if (and (not (listp (cdr read)))
6853                (or (< (car read) bottom)
6854                    (progn (setq read (list read))
6855                           nil)))
6856           (setq first (max bottom (1+ (cdr read))))
6857         ;; `read' is a list of ranges.
6858         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6859                                   (caar read)))
6860                   1)
6861           (setq first bottom))
6862         (while read
6863           (when first
6864             (while (< first nlast)
6865               (setq unread (cons first unread)
6866                     first (1+ first))))
6867           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6868           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6869           (setq read (cdr read)))))
6870     ;; And add the last unread articles.
6871     (while (<= first last)
6872       (setq unread (cons first unread)
6873             first (1+ first)))
6874     ;; Return the list of unread articles.
6875     (delq 0 (nreverse unread))))
6876
6877 (defun gnus-list-of-read-articles (group)
6878   "Return a list of unread, unticked and non-dormant articles."
6879   (let* ((info (gnus-get-info group))
6880          (marked (gnus-info-marks info))
6881          (active (gnus-active group)))
6882     (and info active
6883          (gnus-list-range-difference
6884           (gnus-list-range-difference
6885            (gnus-sorted-complement
6886             (gnus-uncompress-range
6887              (if gnus-newsgroup-maximum-articles
6888                  (cons (max (car active)
6889                             (- (cdr active)
6890                                gnus-newsgroup-maximum-articles
6891                                -1))
6892                        (cdr active))
6893                active))
6894             (gnus-list-of-unread-articles group))
6895            (cdr (assq 'dormant marked)))
6896           (cdr (assq 'tick marked))))))
6897
6898 ;; This function returns a sequence of article numbers based on the
6899 ;; difference between the ranges of read articles in this group and
6900 ;; the range of active articles.
6901 (defun gnus-sequence-of-unread-articles (group)
6902   (let* ((read (gnus-info-read (gnus-get-info group)))
6903          (active (or (gnus-active group) (gnus-activate-group group)))
6904          (last (cdr active))
6905          (bottom (if gnus-newsgroup-maximum-articles
6906                      (max (car active)
6907                           (- last gnus-newsgroup-maximum-articles -1))
6908                    (car active)))
6909          first nlast unread)
6910     ;; If none are read, then all are unread.
6911     (if (not read)
6912         (setq first bottom)
6913       ;; If the range of read articles is a single range, then the
6914       ;; first unread article is the article after the last read
6915       ;; article.  Sounds logical, doesn't it?
6916       (if (and (not (listp (cdr read)))
6917                (or (< (car read) bottom)
6918                    (progn (setq read (list read))
6919                           nil)))
6920           (setq first (max bottom (1+ (cdr read))))
6921         ;; `read' is a list of ranges.
6922         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6923                                   (caar read)))
6924                   1)
6925           (setq first bottom))
6926         (while read
6927           (when first
6928             (push (cons first nlast) unread))
6929           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6930           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6931           (setq read (cdr read)))))
6932     ;; And add the last unread articles.
6933     (cond ((not (and first last))
6934            nil)
6935           ((< first last)
6936            (push (cons first last) unread))
6937           ((= first last)
6938            (push first unread)))
6939     ;; Return the sequence of unread articles.
6940     (delq 0 (nreverse unread))))
6941
6942 ;; Various summary commands
6943
6944 (defun gnus-summary-select-article-buffer ()
6945   "Reconfigure windows to show the article buffer."
6946   (interactive)
6947   (if (not (gnus-buffer-live-p gnus-article-buffer))
6948       (error "There is no article buffer for this summary buffer")
6949     (unless (get-buffer-window gnus-article-buffer)
6950       (gnus-summary-show-article))
6951     (gnus-configure-windows
6952      (if gnus-widen-article-window
6953          'only-article
6954        'article)
6955      t)
6956     (select-window (get-buffer-window gnus-article-buffer))))
6957
6958 (defun gnus-summary-universal-argument (arg)
6959   "Perform any operation on all articles that are process/prefixed."
6960   (interactive "P")
6961   (let ((articles (gnus-summary-work-articles arg))
6962         func article)
6963     (if (eq
6964          (setq
6965           func
6966           (key-binding
6967            (read-key-sequence
6968             (substitute-command-keys
6969              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6970          'undefined)
6971         (gnus-error 1 "Undefined key")
6972       (save-excursion
6973         (while articles
6974           (gnus-summary-goto-subject (setq article (pop articles)))
6975           (let (gnus-newsgroup-processable)
6976             (command-execute func))
6977           (gnus-summary-remove-process-mark article)))))
6978   (gnus-summary-position-point))
6979
6980 (defun gnus-summary-toggle-truncation (&optional arg)
6981   "Toggle truncation of summary lines.
6982 With ARG, turn line truncation on if ARG is positive."
6983   (interactive "P")
6984   (setq truncate-lines
6985         (if (null arg) (not truncate-lines)
6986           (> (prefix-numeric-value arg) 0)))
6987   (redraw-display))
6988
6989 (defun gnus-summary-find-for-reselect ()
6990   "Return the number of an article to stay on across a reselect.
6991 The current article is considered, then following articles, then previous
6992 articles.  An article is sought which is not cancelled and isn't a temporary
6993 insertion from another group.  If there's no such then return a dummy 0."
6994   (let (found)
6995     (dolist (rev '(nil t))
6996       (unless found      ; don't demand the reverse list if we don't need it
6997         (let ((data (gnus-data-find-list
6998                      (gnus-summary-article-number) (gnus-data-list rev))))
6999           (while (and data (not found))
7000             (if (and (< 0 (gnus-data-number (car data)))
7001                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7002                 (setq found (gnus-data-number (car data))))
7003             (setq data (cdr data))))))
7004     (or found 0)))
7005
7006 (defun gnus-summary-reselect-current-group (&optional all rescan)
7007   "Exit and then reselect the current newsgroup.
7008 The prefix argument ALL means to select all articles."
7009   (interactive "P")
7010   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7011     (error "Ephemeral groups can't be reselected"))
7012   (let ((current-subject (gnus-summary-find-for-reselect))
7013         (group gnus-newsgroup-name))
7014     (setq gnus-newsgroup-begin nil)
7015     (gnus-summary-exit nil 'leave-hidden)
7016     ;; We have to adjust the point of group mode buffer because
7017     ;; point was moved to the next unread newsgroup by exiting.
7018     (gnus-summary-jump-to-group group)
7019     (when rescan
7020       (save-excursion
7021         (gnus-group-get-new-news-this-group 1)))
7022     (gnus-group-read-group all t)
7023     (gnus-summary-goto-subject current-subject nil t)))
7024
7025 (defun gnus-summary-rescan-group (&optional all)
7026   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7027   (interactive "P")
7028   (gnus-summary-reselect-current-group all t))
7029
7030 (defun gnus-summary-update-info (&optional non-destructive)
7031   (save-excursion
7032     (let ((group gnus-newsgroup-name))
7033       (when group
7034         (when gnus-newsgroup-kill-headers
7035           (setq gnus-newsgroup-killed
7036                 (gnus-compress-sequence
7037                  (gnus-sorted-union
7038                   (gnus-list-range-intersection
7039                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7040                   gnus-newsgroup-unreads)
7041                  t)))
7042         (unless (listp (cdr gnus-newsgroup-killed))
7043           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7044         (let ((headers gnus-newsgroup-headers))
7045           ;; Set the new ranges of read articles.
7046           (with-current-buffer gnus-group-buffer
7047             (gnus-undo-force-boundary))
7048           (gnus-update-read-articles
7049            group (gnus-sorted-union
7050                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7051           ;; Set the current article marks.
7052           (let ((gnus-newsgroup-scored
7053                  (if (and (not gnus-save-score)
7054                           (not non-destructive))
7055                      nil
7056                    gnus-newsgroup-scored)))
7057             (save-excursion
7058               (gnus-update-marks)))
7059           ;; Do the cross-ref thing.
7060           (when gnus-use-cross-reference
7061             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7062           ;; Do not switch windows but change the buffer to work.
7063           (set-buffer gnus-group-buffer)
7064           (unless (gnus-ephemeral-group-p group)
7065             (gnus-group-update-group group)))))))
7066
7067 (defun gnus-summary-save-newsrc (&optional force)
7068   "Save the current number of read/marked articles in the dribble buffer.
7069 The dribble buffer will then be saved.
7070 If FORCE (the prefix), also save the .newsrc file(s)."
7071   (interactive "P")
7072   (gnus-summary-update-info t)
7073   (if force
7074       (gnus-save-newsrc-file)
7075     (gnus-dribble-save)))
7076
7077 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7078
7079 (defun gnus-summary-exit (&optional temporary leave-hidden)
7080   "Exit reading current newsgroup, and then return to group selection mode.
7081 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7082   (interactive)
7083   (gnus-set-global-variables)
7084   (when (gnus-buffer-live-p gnus-article-buffer)
7085     (with-current-buffer gnus-article-buffer
7086       (mm-destroy-parts gnus-article-mime-handles)
7087       ;; Set it to nil for safety reason.
7088       (setq gnus-article-mime-handle-alist nil)
7089       (setq gnus-article-mime-handles nil)))
7090   (gnus-kill-save-kill-buffer)
7091   (gnus-async-halt-prefetch)
7092   (let* ((group gnus-newsgroup-name)
7093          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7094          (gnus-group-is-exiting-p t)
7095          (mode major-mode)
7096          (group-point nil)
7097          (buf (current-buffer)))
7098     (unless quit-config
7099       ;; Do adaptive scoring, and possibly save score files.
7100       (when gnus-newsgroup-adaptive
7101         (gnus-score-adaptive))
7102       (when gnus-use-scoring
7103         (gnus-score-save)))
7104     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7105     (when gnus-use-cache
7106       (gnus-cache-possibly-remove-articles)
7107       (gnus-cache-save-buffers))
7108     (gnus-async-prefetch-remove-group group)
7109     (when gnus-suppress-duplicates
7110       (gnus-dup-enter-articles))
7111     (when gnus-use-trees
7112       (gnus-tree-close group))
7113     (when gnus-use-cache
7114       (gnus-cache-write-active))
7115     ;; Remove entries for this group.
7116     (nnmail-purge-split-history (gnus-group-real-name group))
7117     ;; Make all changes in this group permanent.
7118     (unless quit-config
7119       (gnus-run-hooks 'gnus-exit-group-hook)
7120       (gnus-summary-update-info))
7121     (gnus-close-group group)
7122     ;; Make sure where we were, and go to next newsgroup.
7123     (set-buffer gnus-group-buffer)
7124     (unless quit-config
7125       (gnus-group-jump-to-group group))
7126     (gnus-run-hooks 'gnus-summary-exit-hook)
7127     (unless (or quit-config
7128                 (not gnus-summary-next-group-on-exit)
7129                 ;; If this group has disappeared from the summary
7130                 ;; buffer, don't skip forwards.
7131                 (not (string= group (gnus-group-group-name))))
7132       (gnus-group-next-unread-group 1))
7133     (setq group-point (point))
7134     (if temporary
7135         nil                             ;Nothing to do.
7136       (set-buffer buf)
7137       (if (not gnus-kill-summary-on-exit)
7138           (progn
7139             (gnus-deaden-summary)
7140             (setq mode nil))
7141         (when (get-buffer gnus-article-buffer)
7142           (bury-buffer gnus-article-buffer))
7143         ;; Return to group mode buffer.
7144         (when (eq mode 'gnus-summary-mode)
7145           (gnus-kill-buffer buf)))
7146
7147       ;; If we have several article buffers, we kill them at exit.
7148       (unless gnus-single-article-buffer
7149         (when (gnus-buffer-live-p gnus-article-buffer)
7150           (with-current-buffer gnus-article-buffer
7151             ;; Don't kill sticky article buffers
7152             (unless (eq major-mode 'gnus-sticky-article-mode)
7153               (gnus-kill-buffer gnus-article-buffer)
7154               (setq gnus-article-current nil))))
7155         (gnus-kill-buffer gnus-original-article-buffer))
7156
7157       (setq gnus-current-select-method gnus-select-method)
7158       (set-buffer gnus-group-buffer)
7159       (if quit-config
7160           (gnus-handle-ephemeral-exit quit-config)
7161         (goto-char group-point)
7162         ;; If gnus-group-buffer is already displayed, make sure we also move
7163         ;; the cursor in the window that displays it.
7164         (let ((win (get-buffer-window (current-buffer) 0)))
7165           (if win (set-window-point win (point))))
7166         (unless leave-hidden
7167           (gnus-configure-windows 'group 'force)))
7168       ;; Clear the current group name.
7169       (unless quit-config
7170         (setq gnus-newsgroup-name nil)))))
7171
7172 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7173 (defun gnus-summary-exit-no-update (&optional no-questions)
7174   "Quit reading current newsgroup without updating read article info."
7175   (interactive)
7176   (let* ((group gnus-newsgroup-name)
7177          (gnus-group-is-exiting-p t)
7178          (gnus-group-is-exiting-without-update-p t)
7179          (quit-config (gnus-group-quit-config group)))
7180     (when (or no-questions
7181               gnus-expert-user
7182               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7183       (gnus-async-halt-prefetch)
7184       (run-hooks 'gnus-summary-prepare-exit-hook)
7185       (when (gnus-buffer-live-p gnus-article-buffer)
7186         (with-current-buffer gnus-article-buffer
7187           (mm-destroy-parts gnus-article-mime-handles)
7188           ;; Set it to nil for safety reason.
7189           (setq gnus-article-mime-handle-alist nil)
7190           (setq gnus-article-mime-handles nil)))
7191       ;; If we have several article buffers, we kill them at exit.
7192       (unless gnus-single-article-buffer
7193         (gnus-kill-buffer gnus-article-buffer)
7194         (gnus-kill-buffer gnus-original-article-buffer)
7195         (setq gnus-article-current nil))
7196       (if (not gnus-kill-summary-on-exit)
7197           (gnus-deaden-summary)
7198         (gnus-close-group group)
7199         (gnus-kill-buffer gnus-summary-buffer))
7200       (unless gnus-single-article-buffer
7201         (setq gnus-article-current nil))
7202       (when gnus-use-trees
7203         (gnus-tree-close group))
7204       (gnus-async-prefetch-remove-group group)
7205       (when (get-buffer gnus-article-buffer)
7206         (bury-buffer gnus-article-buffer))
7207       ;; Return to the group buffer.
7208       (gnus-configure-windows 'group 'force)
7209       ;; Clear the current group name.
7210       (setq gnus-newsgroup-name nil)
7211       (unless (gnus-ephemeral-group-p group)
7212         (gnus-group-update-group group))
7213       (when (equal (gnus-group-group-name) group)
7214         (gnus-group-next-unread-group 1))
7215       (when quit-config
7216         (gnus-handle-ephemeral-exit quit-config)))))
7217
7218 (defun gnus-handle-ephemeral-exit (quit-config)
7219   "Handle movement when leaving an ephemeral group.
7220 The state which existed when entering the ephemeral is reset."
7221   (if (not (buffer-name (car quit-config)))
7222       (gnus-configure-windows 'group 'force)
7223     (set-buffer (car quit-config))
7224     (cond ((eq major-mode 'gnus-summary-mode)
7225            (gnus-set-global-variables))
7226           ((eq major-mode 'gnus-article-mode)
7227            (save-current-buffer
7228              ;; The `gnus-summary-buffer' variable may point
7229              ;; to the old summary buffer when using a single
7230              ;; article buffer.
7231              (unless (gnus-buffer-live-p gnus-summary-buffer)
7232                (set-buffer gnus-group-buffer))
7233              (set-buffer gnus-summary-buffer)
7234              (gnus-set-global-variables))))
7235     (if (or (eq (cdr quit-config) 'article)
7236             (eq (cdr quit-config) 'pick))
7237         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7238             (gnus-configure-windows 'pick 'force)
7239           (gnus-configure-windows (cdr quit-config) 'force))
7240       (gnus-configure-windows (cdr quit-config) 'force))
7241     (when (eq major-mode 'gnus-summary-mode)
7242       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7243                                                      next-unread-noselect))
7244           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7245                                   'next-noselect)
7246                               (gnus-summary-next-subject 1 nil t))
7247                              ((eq gnus-auto-select-on-ephemeral-exit
7248                                   'next-unread-noselect)
7249                               (gnus-summary-next-subject 1 t t))))
7250             ;; Hide the article buffer which displays the article different
7251             ;; from the one that the cursor points to in the summary buffer.
7252             (gnus-configure-windows 'summary 'force))
7253         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7254                (gnus-summary-next-subject 1))
7255               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7256                (gnus-summary-next-subject 1 t))))
7257       (gnus-summary-recenter)
7258       (gnus-summary-position-point))))
7259
7260 ;;; Dead summaries.
7261
7262 (defvar gnus-dead-summary-mode-map
7263   (let ((map (make-keymap)))
7264     (suppress-keymap map)
7265     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7266     (dolist (key '("\C-d" "\r" "\177" [delete]))
7267       (define-key map key 'gnus-summary-wake-up-the-dead))
7268     (dolist (key '("q" "Q"))
7269       (define-key map key 'bury-buffer))
7270     map))
7271
7272 (define-minor-mode gnus-dead-summary-mode
7273   "Minor mode for Gnus summary buffers."
7274   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7275   (unless (derived-mode-p 'gnus-summary-mode)
7276     (setq gnus-dead-summary-mode nil)))
7277
7278 (defun gnus-deaden-summary ()
7279   "Make the current summary buffer into a dead summary buffer."
7280   ;; Kill any previous dead summary buffer.
7281   (when (and gnus-dead-summary
7282              (buffer-name gnus-dead-summary))
7283     (with-current-buffer gnus-dead-summary
7284       (when gnus-dead-summary-mode
7285         (kill-buffer (current-buffer)))))
7286   ;; Make this the current dead summary.
7287   (setq gnus-dead-summary (current-buffer))
7288   (gnus-dead-summary-mode 1)
7289   (let ((name (buffer-name)))
7290     (when (string-match "Summary" name)
7291       (rename-buffer
7292        (concat (substring name 0 (match-beginning 0)) "Dead "
7293                (substring name (match-beginning 0)))
7294        t)
7295       (bury-buffer))))
7296
7297 (defun gnus-kill-or-deaden-summary (buffer)
7298   "Kill or deaden the summary BUFFER."
7299   (save-excursion
7300     (when (and (buffer-name buffer)
7301                (not gnus-single-article-buffer))
7302       (with-current-buffer buffer
7303         (gnus-kill-buffer gnus-article-buffer)
7304         (gnus-kill-buffer gnus-original-article-buffer)))
7305     (cond
7306      ;; Kill the buffer.
7307      (gnus-kill-summary-on-exit
7308       (when (and gnus-use-trees
7309                  (gnus-buffer-exists-p buffer))
7310         (with-current-buffer buffer
7311           (gnus-tree-close gnus-newsgroup-name)))
7312       (gnus-kill-buffer buffer))
7313      ;; Deaden the buffer.
7314      ((gnus-buffer-exists-p buffer)
7315       (with-current-buffer buffer
7316         (gnus-deaden-summary))))))
7317
7318 (defun gnus-summary-wake-up-the-dead (&rest args)
7319   "Wake up the dead summary buffer."
7320   (interactive)
7321   (gnus-dead-summary-mode -1)
7322   (let ((name (buffer-name)))
7323     (when (string-match "Dead " name)
7324       (rename-buffer
7325        (concat (substring name 0 (match-beginning 0))
7326                (substring name (match-end 0)))
7327        t)))
7328   (gnus-message 3 "This dead summary is now alive again"))
7329
7330 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7331 (defun gnus-summary-describe-group (&optional force)
7332   "Describe the current newsgroup."
7333   (interactive "P")
7334   (gnus-group-describe-group force gnus-newsgroup-name))
7335
7336 (defun gnus-summary-describe-briefly ()
7337   "Describe summary mode commands briefly."
7338   (interactive)
7339   (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")))
7340
7341 ;; Walking around group mode buffer from summary mode.
7342
7343 (defun gnus-summary-next-group (&optional no-article target-group backward)
7344   "Exit current newsgroup and then select next unread newsgroup.
7345 If prefix argument NO-ARTICLE is non-nil, no article is selected
7346 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7347 previous group instead."
7348   (interactive "P")
7349   ;; Stop pre-fetching.
7350   (gnus-async-halt-prefetch)
7351   (let ((current-group gnus-newsgroup-name)
7352         (current-buffer (current-buffer))
7353         entered)
7354     ;; First we semi-exit this group to update Xrefs and all variables.
7355     ;; We can't do a real exit, because the window conf must remain
7356     ;; the same in case the user is prompted for info, and we don't
7357     ;; want the window conf to change before that...
7358     (gnus-summary-exit t)
7359     (while (not entered)
7360       ;; Then we find what group we are supposed to enter.
7361       (set-buffer gnus-group-buffer)
7362       (gnus-group-jump-to-group current-group)
7363       (setq target-group
7364             (or target-group
7365                 (if (eq gnus-keep-same-level 'best)
7366                     (gnus-summary-best-group gnus-newsgroup-name)
7367                   (gnus-summary-search-group backward gnus-keep-same-level))))
7368       (if (not target-group)
7369           ;; There are no further groups, so we return to the group
7370           ;; buffer.
7371           (progn
7372             (gnus-message 5 "Returning to the group buffer")
7373             (setq entered t)
7374             (when (gnus-buffer-live-p current-buffer)
7375               (set-buffer current-buffer)
7376               (gnus-summary-exit))
7377             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7378         ;; We try to enter the target group.
7379         (gnus-group-jump-to-group target-group)
7380         (let ((unreads (gnus-group-group-unread)))
7381           (if (and (or (eq t unreads)
7382                        (and unreads (not (zerop unreads))))
7383                    (gnus-summary-read-group
7384                     target-group nil no-article
7385                     (and (buffer-name current-buffer) current-buffer)
7386                     nil backward))
7387               (setq entered t)
7388             (setq current-group target-group
7389                   target-group nil)))))))
7390
7391 (defun gnus-summary-prev-group (&optional no-article)
7392   "Exit current newsgroup and then select previous unread newsgroup.
7393 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7394   (interactive "P")
7395   (gnus-summary-next-group no-article nil t))
7396
7397 ;; Walking around summary lines.
7398
7399 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7400   "Go to the first subject satisfying any non-nil constraint.
7401 If UNREAD is non-nil, the article should be unread.
7402 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7403 If UNSEEN is non-nil, the article should be unseen as well as unread.
7404 Returns the article selected or nil if there are no matching articles."
7405   (interactive "P")
7406   (cond
7407    ;; Empty summary.
7408    ((null gnus-newsgroup-data)
7409     (gnus-message 3 "No articles in the group")
7410     nil)
7411    ;; Pick the first article.
7412    ((not (or unread undownloaded unseen))
7413     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7414     (gnus-data-number (car gnus-newsgroup-data)))
7415    ;; Find the first unread article.
7416    (t
7417     (let ((data gnus-newsgroup-data))
7418       (while (and data
7419                   (let ((num (gnus-data-number (car data))))
7420                     (or (memq num gnus-newsgroup-unfetched)
7421                         (not (or (and unread
7422                                       (memq num gnus-newsgroup-unreads))
7423                                  (and undownloaded
7424                                       (memq num gnus-newsgroup-undownloaded))
7425                                  (and unseen
7426                                       (memq num gnus-newsgroup-unseen)
7427                                       (memq num gnus-newsgroup-unreads)))))))
7428         (setq data (cdr data)))
7429       (prog1
7430           (if data
7431               (progn
7432                 (goto-char (gnus-data-pos (car data)))
7433                 (gnus-data-number (car data)))
7434             (gnus-message 3 "No more%s articles"
7435                           (let* ((r (when unread " unread"))
7436                                  (d (when undownloaded " undownloaded"))
7437                                  (s (when unseen " unseen"))
7438                                  (l (delq nil (list r d s))))
7439                             (cond ((= 3 (length l))
7440                                    (concat r "," d ", or" s))
7441                                   ((= 2 (length l))
7442                                    (concat (car l) ", or" (cadr l)))
7443                                   ((= 1 (length l))
7444                                    (car l))
7445                                   (t
7446                                    ""))))
7447             nil
7448             )
7449         (gnus-summary-position-point))))))
7450
7451 (defun gnus-summary-next-subject (n &optional unread dont-display)
7452   "Go to next N'th summary line.
7453 If N is negative, go to the previous N'th subject line.
7454 If UNREAD is non-nil, only unread articles are selected.
7455 The difference between N and the actual number of steps taken is
7456 returned."
7457   (interactive "p")
7458   (let ((backward (< n 0))
7459         (n (abs n)))
7460     (while (and (> n 0)
7461                 (if backward
7462                     (gnus-summary-find-prev unread)
7463                   (gnus-summary-find-next unread)))
7464       (unless (zerop (setq n (1- n)))
7465         (gnus-summary-show-thread)))
7466     (when (/= 0 n)
7467       (gnus-message 7 "No more%s articles"
7468                     (if unread " unread" "")))
7469     (unless dont-display
7470       (gnus-summary-recenter)
7471       (gnus-summary-position-point))
7472     n))
7473
7474 (defun gnus-summary-next-unread-subject (n)
7475   "Go to next N'th unread summary line."
7476   (interactive "p")
7477   (gnus-summary-next-subject n t))
7478
7479 (defun gnus-summary-prev-subject (n &optional unread)
7480   "Go to previous N'th summary line.
7481 If optional argument UNREAD is non-nil, only unread article is selected."
7482   (interactive "p")
7483   (gnus-summary-next-subject (- n) unread))
7484
7485 (defun gnus-summary-prev-unread-subject (n)
7486   "Go to previous N'th unread summary line."
7487   (interactive "p")
7488   (gnus-summary-next-subject (- n) t))
7489
7490 (defun gnus-summary-goto-subjects (articles)
7491   "Insert the subject header for ARTICLES in the current buffer."
7492   (save-excursion
7493     (dolist (article articles)
7494       (gnus-summary-goto-subject article t)))
7495   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7496   (gnus-summary-position-point))
7497
7498 (defun gnus-summary-goto-subject (article &optional force silent)
7499   "Go to the subject line of ARTICLE.
7500 If FORCE, also allow jumping to articles not currently shown."
7501   (interactive "nArticle number: ")
7502   (unless (numberp article)
7503     (error "Article %s is not a number" article))
7504   (let ((b (point))
7505         (data (gnus-data-find article)))
7506     ;; We read in the article if we have to.
7507     (and (not data)
7508          force
7509          (gnus-summary-insert-subject
7510           article
7511           (if (or (numberp force) (vectorp force)) force)
7512           t)
7513          (setq data (gnus-data-find article)))
7514     (goto-char b)
7515     (if (not data)
7516         (progn
7517           (unless silent
7518             (gnus-message 3 "Can't find article %d" article))
7519           nil)
7520       (let ((pt (gnus-data-pos data)))
7521         (goto-char pt)
7522         (gnus-summary-set-article-display-arrow pt))
7523       (gnus-summary-position-point)
7524       article)))
7525
7526 ;; Walking around summary lines with displaying articles.
7527
7528 (defun gnus-summary-expand-window (&optional arg)
7529   "Make the summary buffer take up the entire Emacs frame.
7530 Given a prefix, will force an `article' buffer configuration."
7531   (interactive "P")
7532   (if arg
7533       (gnus-configure-windows 'article 'force)
7534     (gnus-configure-windows 'summary 'force)))
7535
7536 (defun gnus-summary-display-article (article &optional all-header)
7537   "Display ARTICLE in article buffer."
7538   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7539                (with-current-buffer gnus-article-buffer
7540                  (eq major-mode 'gnus-article-mode)))
7541     (gnus-article-setup-buffer))
7542   (gnus-set-global-variables)
7543   (with-current-buffer gnus-article-buffer
7544     (setq gnus-article-charset gnus-newsgroup-charset)
7545     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7546     (mm-enable-multibyte))
7547   (if (null article)
7548       nil
7549     (prog1
7550         (if gnus-summary-display-article-function
7551             (funcall gnus-summary-display-article-function article all-header)
7552           (gnus-article-prepare article all-header))
7553       (gnus-run-hooks 'gnus-select-article-hook)
7554       (when (and gnus-current-article
7555                  (not (zerop gnus-current-article)))
7556         (gnus-summary-goto-subject gnus-current-article))
7557       (gnus-summary-recenter)
7558       (when (and gnus-use-trees gnus-show-threads)
7559         (gnus-possibly-generate-tree article)
7560         (gnus-highlight-selected-tree article))
7561       ;; Successfully display article.
7562       (gnus-article-set-window-start
7563        (cdr (assq article gnus-newsgroup-bookmarks))))))
7564
7565 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7566   "Select the current article.
7567 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7568 non-nil, the article will be re-fetched even if it already present in
7569 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7570 be displayed."
7571   ;; Make sure we are in the summary buffer to work around bbdb bug.
7572   (unless (eq major-mode 'gnus-summary-mode)
7573     (set-buffer gnus-summary-buffer))
7574   (let ((article (or article (gnus-summary-article-number)))
7575         (all-headers (not (not all-headers))) ;Must be t or nil.
7576         gnus-summary-display-article-function)
7577     (and (not pseudo)
7578          (gnus-summary-article-pseudo-p article)
7579          (error "This is a pseudo-article"))
7580     (with-current-buffer gnus-summary-buffer
7581       (if (or (and gnus-single-article-buffer
7582                    (or (null gnus-current-article)
7583                        (null gnus-article-current)
7584                        (null (get-buffer gnus-article-buffer))
7585                        (not (eq article (cdr gnus-article-current)))
7586                        (not (equal (car gnus-article-current)
7587                                    gnus-newsgroup-name))))
7588               (and (not gnus-single-article-buffer)
7589                    (or (null gnus-current-article)
7590                        (not (eq gnus-current-article article))))
7591               force)
7592           ;; The requested article is different from the current article.
7593           (progn
7594             (gnus-summary-display-article article all-headers)
7595             (when (gnus-buffer-live-p gnus-article-buffer)
7596               (with-current-buffer gnus-article-buffer
7597                 (if (not gnus-article-decoded-p) ;; a local variable
7598                     (mm-disable-multibyte))))
7599             (gnus-article-set-window-start
7600              (cdr (assq article gnus-newsgroup-bookmarks)))
7601             article)
7602         'old))))
7603
7604 (defun gnus-summary-force-verify-and-decrypt ()
7605   "Display buttons for signed/encrypted parts and verify/decrypt them."
7606   (interactive)
7607   (let ((mm-verify-option 'known)
7608         (mm-decrypt-option 'known)
7609         (gnus-article-emulate-mime t)
7610         (gnus-buttonized-mime-types (append (list "multipart/signed"
7611                                                   "multipart/encrypted")
7612                                             gnus-buttonized-mime-types)))
7613     (gnus-summary-select-article nil 'force)))
7614
7615 (defun gnus-summary-set-current-mark (&optional current-mark)
7616   "Obsolete function."
7617   nil)
7618
7619 (defun gnus-summary-next-article (&optional unread subject backward push)
7620   "Select the next article.
7621 If UNREAD, only unread articles are selected.
7622 If SUBJECT, only articles with SUBJECT are selected.
7623 If BACKWARD, the previous article is selected instead of the next."
7624   (interactive "P")
7625   ;; Make sure we are in the summary buffer.
7626   (unless (eq major-mode 'gnus-summary-mode)
7627     (set-buffer gnus-summary-buffer))
7628   (cond
7629    ;; Is there such an article?
7630    ((and (gnus-summary-search-forward unread subject backward)
7631          (or (gnus-summary-display-article (gnus-summary-article-number))
7632              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7633     (gnus-summary-position-point))
7634    ;; If not, we try the first unread, if that is wanted.
7635    ((and subject
7636          gnus-auto-select-same
7637          (gnus-summary-first-unread-article))
7638     (gnus-summary-position-point)
7639     (gnus-message 6 "Wrapped"))
7640    ;; Try to get next/previous article not displayed in this group.
7641    ((and gnus-auto-extend-newsgroup
7642          (not unread) (not subject))
7643     (gnus-summary-goto-article
7644      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7645      nil (count-lines (point-min) (point))))
7646    ;; Go to next/previous group.
7647    (t
7648     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7649       (gnus-summary-jump-to-group gnus-newsgroup-name))
7650     (let ((cmd (if (featurep 'xemacs)
7651                    last-command-char
7652                  last-command-event))
7653           (point
7654            (with-current-buffer gnus-group-buffer
7655              (point)))
7656           (group
7657            (if (eq gnus-keep-same-level 'best)
7658                (gnus-summary-best-group gnus-newsgroup-name)
7659              (gnus-summary-search-group backward gnus-keep-same-level))))
7660       ;; For some reason, the group window gets selected.  We change
7661       ;; it back.
7662       (select-window (get-buffer-window (current-buffer)))
7663       ;; Select next unread newsgroup automagically.
7664       (cond
7665        ((or (not gnus-auto-select-next)
7666             (not cmd))
7667         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7668        ((or (eq gnus-auto-select-next 'quietly)
7669             (and (eq gnus-auto-select-next 'slightly-quietly)
7670                  push)
7671             (and (eq gnus-auto-select-next 'almost-quietly)
7672                  (gnus-summary-last-article-p)))
7673         ;; Select quietly.
7674         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7675             (gnus-summary-exit)
7676           (gnus-message 7 "No more%s articles (%s)..."
7677                         (if unread " unread" "")
7678                         (if group (concat "selecting " group)
7679                           "exiting"))
7680           (gnus-summary-next-group nil group backward)))
7681        (t
7682         (when (gnus-key-press-event-p last-input-event)
7683           (gnus-summary-walk-group-buffer
7684            gnus-newsgroup-name cmd unread backward point))))))))
7685
7686 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7687   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7688                       (?\C-p (gnus-group-prev-unread-group 1))))
7689         (cursor-in-echo-area t)
7690         keve key group ended prompt)
7691     (with-current-buffer gnus-group-buffer
7692       (goto-char start)
7693       (setq group
7694             (if (eq gnus-keep-same-level 'best)
7695                 (gnus-summary-best-group gnus-newsgroup-name)
7696               (gnus-summary-search-group backward gnus-keep-same-level))))
7697     (while (not ended)
7698       (setq prompt
7699             (format
7700              "No more%s articles%s " (if unread " unread" "")
7701              (if (and group
7702                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7703                  (format " (Type %s for %s [%s])"
7704                          (single-key-description cmd)
7705                          (gnus-group-decoded-name group)
7706                          (gnus-group-unread group))
7707                (format " (Type %s to exit %s)"
7708                        (single-key-description cmd)
7709                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7710       ;; Confirm auto selection.
7711       (setq key (car (setq keve (gnus-read-event-char prompt)))
7712             ended t)
7713       (cond
7714        ((assq key keystrokes)
7715         (let ((obuf (current-buffer)))
7716           (switch-to-buffer gnus-group-buffer)
7717           (when group
7718             (gnus-group-jump-to-group group))
7719           (eval (cadr (assq key keystrokes)))
7720           (setq group (gnus-group-group-name))
7721           (switch-to-buffer obuf))
7722         (setq ended nil))
7723        ((equal key cmd)
7724         (if (or (not group)
7725                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7726             (gnus-summary-exit)
7727           (gnus-summary-next-group nil group backward)))
7728        (t
7729         (push (cdr keve) unread-command-events))))))
7730
7731 (defun gnus-summary-next-unread-article ()
7732   "Select unread article after current one."
7733   (interactive)
7734   (gnus-summary-next-article
7735    (or (not (eq gnus-summary-goto-unread 'never))
7736        (gnus-summary-last-article-p (gnus-summary-article-number)))
7737    (and gnus-auto-select-same
7738         (gnus-summary-article-subject))))
7739
7740 (defun gnus-summary-prev-article (&optional unread subject)
7741   "Select the article before the current one.
7742 If UNREAD is non-nil, only unread articles are selected."
7743   (interactive "P")
7744   (gnus-summary-next-article unread subject t))
7745
7746 (defun gnus-summary-prev-unread-article ()
7747   "Select unread article before current one."
7748   (interactive)
7749   (gnus-summary-prev-article
7750    (or (not (eq gnus-summary-goto-unread 'never))
7751        (gnus-summary-first-article-p (gnus-summary-article-number)))
7752    (and gnus-auto-select-same
7753         (gnus-summary-article-subject))))
7754
7755 (defun gnus-summary-next-page (&optional lines circular stop)
7756   "Show next page of the selected article.
7757 If at the end of the current article, select the next article.
7758 LINES says how many lines should be scrolled up.
7759
7760 If CIRCULAR is non-nil, go to the start of the article instead of
7761 selecting the next article when reaching the end of the current
7762 article.
7763
7764 If STOP is non-nil, just stop when reaching the end of the message.
7765
7766 Also see the variable `gnus-article-skip-boring'."
7767   (interactive "P")
7768   (setq gnus-summary-buffer (current-buffer))
7769   (gnus-set-global-variables)
7770   (let ((article (gnus-summary-article-number))
7771         (article-window (get-buffer-window gnus-article-buffer t))
7772         endp)
7773     ;; If the buffer is empty, we have no article.
7774     (unless article
7775       (error "No article to select"))
7776     (gnus-configure-windows 'article)
7777     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7778         (if (and (eq gnus-summary-goto-unread 'never)
7779                  (not (gnus-summary-last-article-p article)))
7780             (gnus-summary-next-article)
7781           (gnus-summary-next-unread-article))
7782       (if (or (null gnus-current-article)
7783               (null gnus-article-current)
7784               (/= article (cdr gnus-article-current))
7785               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7786           ;; Selected subject is different from current article's.
7787           (gnus-summary-display-article article)
7788         (when article-window
7789           (gnus-eval-in-buffer-window gnus-article-buffer
7790             (setq endp (or (gnus-article-next-page lines)
7791                            (gnus-article-only-boring-p))))
7792           (when endp
7793             (cond ((or stop gnus-summary-stop-at-end-of-message)
7794                    (gnus-message 3 "End of message"))
7795                   (circular
7796                    (gnus-summary-beginning-of-article))
7797                   (lines
7798                    (gnus-message 3 "End of message"))
7799                   ((null lines)
7800                    (if (and (eq gnus-summary-goto-unread 'never)
7801                             (not (gnus-summary-last-article-p article)))
7802                        (gnus-summary-next-article)
7803                      (gnus-summary-next-unread-article))))))))
7804     (gnus-summary-recenter)
7805     (gnus-summary-position-point)))
7806
7807 (defun gnus-summary-prev-page (&optional lines move)
7808   "Show previous page of selected article.
7809 Argument LINES specifies lines to be scrolled down.
7810 If MOVE, move to the previous unread article if point is at
7811 the beginning of the buffer."
7812   (interactive "P")
7813   (let ((article (gnus-summary-article-number))
7814         (article-window (get-buffer-window gnus-article-buffer t))
7815         endp)
7816     (gnus-configure-windows 'article)
7817     (if (or (null gnus-current-article)
7818             (null gnus-article-current)
7819             (/= article (cdr gnus-article-current))
7820             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7821         ;; Selected subject is different from current article's.
7822         (gnus-summary-display-article article)
7823       (gnus-summary-recenter)
7824       (when article-window
7825         (gnus-eval-in-buffer-window gnus-article-buffer
7826           (setq endp (gnus-article-prev-page lines)))
7827         (when (and move endp)
7828           (cond (lines
7829                  (gnus-message 3 "Beginning of message"))
7830                 ((null lines)
7831                  (if (and (eq gnus-summary-goto-unread 'never)
7832                           (not (gnus-summary-first-article-p article)))
7833                      (gnus-summary-prev-article)
7834                    (gnus-summary-prev-unread-article))))))))
7835   (gnus-summary-position-point))
7836
7837 (defun gnus-summary-prev-page-or-article (&optional lines)
7838   "Show previous page of selected article.
7839 Argument LINES specifies lines to be scrolled down.
7840 If at the beginning of the article, go to the next article."
7841   (interactive "P")
7842   (gnus-summary-prev-page lines t))
7843
7844 (defun gnus-summary-scroll-up (lines)
7845   "Scroll up (or down) one line current article.
7846 Argument LINES specifies lines to be scrolled up (or down if negative).
7847 If no article is selected, then the current article will be selected first."
7848   (interactive "p")
7849   (gnus-configure-windows 'article)
7850   (gnus-summary-show-thread)
7851   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7852     (gnus-eval-in-buffer-window gnus-article-buffer
7853       (cond ((> lines 0)
7854              (when (gnus-article-next-page lines)
7855                (gnus-message 3 "End of message")))
7856             ((< lines 0)
7857              (gnus-article-prev-page (- lines))))))
7858   (gnus-summary-recenter)
7859   (gnus-summary-position-point))
7860
7861 (defun gnus-summary-scroll-down (lines)
7862   "Scroll down (or up) one line current article.
7863 Argument LINES specifies lines to be scrolled down (or up if negative).
7864 If no article is selected, then the current article will be selected first."
7865   (interactive "p")
7866   (gnus-summary-scroll-up (- lines)))
7867
7868 (defun gnus-summary-next-same-subject ()
7869   "Select next article which has the same subject as current one."
7870   (interactive)
7871   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7872
7873 (defun gnus-summary-prev-same-subject ()
7874   "Select previous article which has the same subject as current one."
7875   (interactive)
7876   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7877
7878 (defun gnus-summary-next-unread-same-subject ()
7879   "Select next unread article which has the same subject as current one."
7880   (interactive)
7881   (gnus-summary-next-article t (gnus-summary-article-subject)))
7882
7883 (defun gnus-summary-prev-unread-same-subject ()
7884   "Select previous unread article which has the same subject as current one."
7885   (interactive)
7886   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7887
7888 (defun gnus-summary-first-unread-article ()
7889   "Select the first unread article.
7890 Return nil if there are no unread articles."
7891   (interactive)
7892   (prog1
7893       (when (gnus-summary-first-subject t)
7894         (gnus-summary-show-thread)
7895         (gnus-summary-first-subject t)
7896         (gnus-summary-display-article (gnus-summary-article-number)))
7897     (gnus-summary-position-point)))
7898
7899 (defun gnus-summary-first-unread-subject ()
7900   "Place the point on the subject line of the first unread article.
7901 Return nil if there are no unread articles."
7902   (interactive)
7903   (prog1
7904       (when (gnus-summary-first-subject t)
7905         (gnus-summary-show-thread)
7906         (gnus-summary-first-subject t))
7907     (gnus-summary-position-point)))
7908
7909 (defun gnus-summary-first-unseen-subject ()
7910   "Place the point on the subject line of the first unseen article.
7911 Return nil if there are no unseen articles."
7912   (interactive)
7913   (prog1
7914       (when (gnus-summary-first-subject nil nil t)
7915         (gnus-summary-show-thread)
7916         (gnus-summary-first-subject nil nil t))
7917     (gnus-summary-position-point)))
7918
7919 (defun gnus-summary-first-unseen-or-unread-subject ()
7920   "Place the point on the subject line of the first unseen and unread article.
7921 If all article have been seen, on the subject line of the first unread
7922 article."
7923   (interactive)
7924   (prog1
7925       (unless (when (gnus-summary-first-subject nil nil t)
7926                 (gnus-summary-show-thread)
7927                 (gnus-summary-first-subject nil nil t))
7928         (when (gnus-summary-first-subject t)
7929           (gnus-summary-show-thread)
7930           (gnus-summary-first-subject t)))
7931     (gnus-summary-position-point)))
7932
7933 (defun gnus-summary-first-article ()
7934   "Select the first article.
7935 Return nil if there are no articles."
7936   (interactive)
7937   (prog1
7938       (when (gnus-summary-first-subject)
7939         (gnus-summary-show-thread)
7940         (gnus-summary-first-subject)
7941         (gnus-summary-display-article (gnus-summary-article-number)))
7942     (gnus-summary-position-point)))
7943
7944 (defun gnus-summary-best-unread-article (&optional arg)
7945   "Select the unread article with the highest score.
7946 If given a prefix argument, select the next unread article that has a
7947 score higher than the default score."
7948   (interactive "P")
7949   (let ((article (if arg
7950                      (gnus-summary-better-unread-subject)
7951                    (gnus-summary-best-unread-subject))))
7952     (if article
7953         (gnus-summary-goto-article article)
7954       (error "No unread articles"))))
7955
7956 (defun gnus-summary-best-unread-subject ()
7957   "Select the unread subject with the highest score."
7958   (interactive)
7959   (let ((best -1000000)
7960         (data gnus-newsgroup-data)
7961         article score)
7962     (while data
7963       (and (gnus-data-unread-p (car data))
7964            (> (setq score
7965                     (gnus-summary-article-score (gnus-data-number (car data))))
7966               best)
7967            (setq best score
7968                  article (gnus-data-number (car data))))
7969       (setq data (cdr data)))
7970     (when article
7971       (gnus-summary-goto-subject article))
7972     (gnus-summary-position-point)
7973     article))
7974
7975 (defun gnus-summary-better-unread-subject ()
7976   "Select the first unread subject that has a score over the default score."
7977   (interactive)
7978   (let ((data gnus-newsgroup-data)
7979         article score)
7980     (while (and (setq article (gnus-data-number (car data)))
7981                 (or (gnus-data-read-p (car data))
7982                     (not (> (gnus-summary-article-score article)
7983                             gnus-summary-default-score))))
7984       (setq data (cdr data)))
7985     (when article
7986       (gnus-summary-goto-subject article))
7987     (gnus-summary-position-point)
7988     article))
7989
7990 (defun gnus-summary-last-subject ()
7991   "Go to the last displayed subject line in the group."
7992   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7993     (when article
7994       (gnus-summary-goto-subject article))))
7995
7996 (defun gnus-summary-goto-article (article &optional all-headers force)
7997   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7998 If ALL-HEADERS is non-nil, no header lines are hidden.
7999 If FORCE, go to the article even if it isn't displayed.  If FORCE
8000 is a number, it is the line the article is to be displayed on."
8001   (interactive
8002    (list
8003     (gnus-completing-read
8004      "Article number or Message-ID"
8005      (mapcar 'int-to-string gnus-newsgroup-limit))
8006     current-prefix-arg
8007     t))
8008   (prog1
8009       (if (and (stringp article)
8010                (string-match "@\\|%40" article))
8011           (gnus-summary-refer-article article)
8012         (when (stringp article)
8013           (setq article (string-to-number article)))
8014         (if (gnus-summary-goto-subject article force)
8015             (gnus-summary-display-article article all-headers)
8016           (gnus-message 4 "Couldn't go to article %s" article) nil))
8017     (gnus-summary-position-point)))
8018
8019 (defun gnus-summary-goto-last-article ()
8020   "Go to the previously read article."
8021   (interactive)
8022   (prog1
8023       (when gnus-last-article
8024         (gnus-summary-goto-article gnus-last-article nil t))
8025     (gnus-summary-position-point)))
8026
8027 (defun gnus-summary-pop-article (number)
8028   "Pop one article off the history and go to the previous.
8029 NUMBER articles will be popped off."
8030   (interactive "p")
8031   (let (to)
8032     (setq gnus-newsgroup-history
8033           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8034     (if to
8035         (gnus-summary-goto-article (car to) nil t)
8036       (error "Article history empty")))
8037   (gnus-summary-position-point))
8038
8039 ;; Summary commands and functions for limiting the summary buffer.
8040
8041 (defun gnus-summary-limit-to-articles (n)
8042   "Limit the summary buffer to the next N articles.
8043 If not given a prefix, use the process marked articles instead."
8044   (interactive "P")
8045   (prog1
8046       (let ((articles (gnus-summary-work-articles n)))
8047         (setq gnus-newsgroup-processable nil)
8048         (gnus-summary-limit articles))
8049     (gnus-summary-position-point)))
8050
8051 (defun gnus-summary-pop-limit (&optional total)
8052   "Restore the previous limit.
8053 If given a prefix, remove all limits."
8054   (interactive "P")
8055   (when total
8056     (setq gnus-newsgroup-limits
8057           (list (mapcar (lambda (h) (mail-header-number h))
8058                         gnus-newsgroup-headers))))
8059   (unless gnus-newsgroup-limits
8060     (error "No limit to pop"))
8061   (prog1
8062       (gnus-summary-limit nil 'pop)
8063     (gnus-summary-position-point)))
8064
8065 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8066   "Limit the summary buffer to articles that have subjects that match a regexp.
8067 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8068   (interactive
8069    (list (read-string (if current-prefix-arg
8070                           "Exclude subject (regexp): "
8071                         "Limit to subject (regexp): "))
8072          nil current-prefix-arg))
8073   (unless header
8074     (setq header "subject"))
8075   (when (not (equal "" subject))
8076     (prog1
8077         (let ((articles (gnus-summary-find-matching
8078                          (or header "subject") subject 'all nil nil
8079                          not-matching)))
8080           (unless articles
8081             (error "Found no matches for \"%s\"" subject))
8082           (gnus-summary-limit articles))
8083       (gnus-summary-position-point))))
8084
8085 (defun gnus-summary-limit-to-author (from &optional not-matching)
8086   "Limit the summary buffer to articles that have authors that match a regexp.
8087 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8088   (interactive
8089    (list (read-string (if current-prefix-arg
8090                           "Exclude author (regexp): "
8091                         "Limit to author (regexp): "))
8092          current-prefix-arg))
8093   (gnus-summary-limit-to-subject from "from" not-matching))
8094
8095 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8096   "Limit the summary buffer to articles with the given RECIPIENT.
8097
8098 If NOT-MATCHING, exclude RECIPIENT.
8099
8100 To and Cc headers are checked.  You need to include them in
8101 `nnmail-extra-headers'."
8102   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8103   (interactive
8104    (list (read-string (format "%s recipient (regexp): "
8105                               (if current-prefix-arg "Exclude" "Limit to")))
8106          current-prefix-arg))
8107   (when (not (equal "" recipient))
8108     (prog1 (let* ((to
8109                    (if (memq 'To nnmail-extra-headers)
8110                        (gnus-summary-find-matching
8111                         (cons 'extra 'To) recipient 'all nil nil
8112                         not-matching)
8113                      (gnus-message
8114                       1 "`To' isn't present in `nnmail-extra-headers'")
8115                      (sit-for 1)
8116                      nil))
8117                   (cc
8118                    (if (memq 'Cc nnmail-extra-headers)
8119                        (gnus-summary-find-matching
8120                         (cons 'extra 'Cc) recipient 'all nil nil
8121                         not-matching)
8122                      (gnus-message
8123                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8124                      (sit-for 1)
8125                      nil))
8126                   (articles
8127                    (if not-matching
8128                        ;; We need the numbers that are in both lists:
8129                        (mapcar (lambda (a)
8130                                  (and (memq a to) a))
8131                                cc)
8132                      (nconc to cc))))
8133              (unless articles
8134                (error "Found no matches for \"%s\"" recipient))
8135              (gnus-summary-limit articles))
8136       (gnus-summary-position-point))))
8137
8138 (defun gnus-summary-limit-to-address (address &optional not-matching)
8139   "Limit the summary buffer to articles with the given ADDRESS.
8140
8141 If NOT-MATCHING, exclude ADDRESS.
8142
8143 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8144 in `nnmail-extra-headers'."
8145   (interactive
8146    (list (read-string (format "%s address (regexp): "
8147                               (if current-prefix-arg "Exclude" "Limit to")))
8148          current-prefix-arg))
8149   (when (not (equal "" address))
8150     (prog1 (let* ((to
8151                    (if (memq 'To nnmail-extra-headers)
8152                        (gnus-summary-find-matching
8153                         (cons 'extra 'To) address 'all nil nil
8154                         not-matching)
8155                      (gnus-message
8156                       1 "`To' isn't present in `nnmail-extra-headers'")
8157                      (sit-for 1)
8158                      t))
8159                   (cc
8160                    (if (memq 'Cc nnmail-extra-headers)
8161                        (gnus-summary-find-matching
8162                         (cons 'extra 'Cc) address 'all nil nil
8163                         not-matching)
8164                      (gnus-message
8165                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8166                      (sit-for 1)
8167                      t))
8168                   (from
8169                    (gnus-summary-find-matching "from" address
8170                                                'all nil nil not-matching))
8171                   (articles
8172                    (if not-matching
8173                        ;; We need the numbers that are in all lists:
8174                        (if (eq cc t)
8175                            (if (eq to t)
8176                                from
8177                              (mapcar (lambda (a) (car (memq a from))) to))
8178                          (if (eq to t)
8179                              (mapcar (lambda (a) (car (memq a from))) cc)
8180                            (mapcar (lambda (a) (car (memq a from)))
8181                                    (mapcar (lambda (a) (car (memq a to)))
8182                                            cc))))
8183                      (nconc (if (eq to t) nil to)
8184                             (if (eq cc t) nil cc)
8185                             from))))
8186              (unless articles
8187                (error "Found no matches for \"%s\"" address))
8188              (gnus-summary-limit articles))
8189       (gnus-summary-position-point))))
8190
8191 (defun gnus-summary-limit-strange-charsets-predicate (header)
8192   (when (fboundp 'char-charset)
8193     (let ((string (concat (mail-header-subject header)
8194                           (mail-header-from header)))
8195           charset found)
8196       (dotimes (i (1- (length string)))
8197         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8198         (when (string-match "unicode\\|big\\|japanese" charset)
8199           (setq found t)))
8200       found)))
8201
8202 (defun gnus-summary-limit-to-predicate (predicate)
8203   "Limit to articles where PREDICATE returns non-nil.
8204 PREDICATE will be called with the header structures of the
8205 articles."
8206   (let ((articles nil)
8207         (case-fold-search t))
8208     (dolist (header gnus-newsgroup-headers)
8209       (when (funcall predicate header)
8210         (push (mail-header-number header) articles)))
8211     (gnus-summary-limit (nreverse articles))))
8212
8213 (defun gnus-summary-limit-to-age (age &optional younger-p)
8214   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8215 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8216 articles that are younger than AGE days."
8217   (interactive
8218    (let ((younger current-prefix-arg)
8219          (days-got nil)
8220          days)
8221      (while (not days-got)
8222        (setq days (if younger
8223                       (read-string "Limit to articles younger than (in days, older when negative): ")
8224                     (read-string
8225                      "Limit to articles older than (in days, younger when negative): ")))
8226        (when (> (length days) 0)
8227          (setq days (read days)))
8228        (if (numberp days)
8229            (progn
8230              (setq days-got t)
8231              (when (< days 0)
8232                (setq younger (not younger))
8233                (setq days (* days -1))))
8234          (message "Please enter a number.")
8235          (sleep-for 1)))
8236      (list days younger)))
8237   (prog1
8238       (let ((data gnus-newsgroup-data)
8239             (cutoff (days-to-time age))
8240             articles d date is-younger)
8241         (while (setq d (pop data))
8242           (when (and (vectorp (gnus-data-header d))
8243                      (setq date (mail-header-date (gnus-data-header d))))
8244             (setq is-younger (time-less-p
8245                               (time-since (gnus-date-get-time date))
8246                               cutoff))
8247             (when (if younger-p
8248                       is-younger
8249                     (not is-younger))
8250               (push (gnus-data-number d) articles))))
8251         (gnus-summary-limit (nreverse articles)))
8252     (gnus-summary-position-point)))
8253
8254 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8255   "Limit the summary buffer to articles that match an 'extra' header."
8256   (interactive
8257    (let ((header
8258           (intern
8259            (gnus-completing-read
8260             (if current-prefix-arg
8261                 "Exclude extra header"
8262               "Limit extra header")
8263             (mapcar 'symbol-name gnus-extra-headers)
8264             t nil nil
8265             (symbol-name (car gnus-extra-headers))))))
8266      (list header
8267            (read-string (format "%s header %s (regexp): "
8268                                 (if current-prefix-arg "Exclude" "Limit to")
8269                                 header))
8270            current-prefix-arg)))
8271   (when (not (equal "" regexp))
8272     (prog1
8273         (let ((articles (gnus-summary-find-matching
8274                          (cons 'extra header) regexp 'all nil nil
8275                          not-matching)))
8276           (unless articles
8277             (error "Found no matches for \"%s\"" regexp))
8278           (gnus-summary-limit articles))
8279       (gnus-summary-position-point))))
8280
8281 (defun gnus-summary-limit-to-display-predicate ()
8282   "Limit the summary buffer to the predicated in the `display' group parameter."
8283   (interactive)
8284   (unless gnus-newsgroup-display
8285     (error "There is no `display' group parameter"))
8286   (let (articles)
8287     (dolist (gnus-number gnus-newsgroup-articles)
8288       (when (funcall gnus-newsgroup-display)
8289         (push gnus-number articles)))
8290     (gnus-summary-limit articles))
8291   (gnus-summary-position-point))
8292
8293 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8294 (make-obsolete
8295  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8296
8297 (defun gnus-summary-limit-to-unread (&optional all)
8298   "Limit the summary buffer to articles that are not marked as read.
8299 If ALL is non-nil, limit strictly to unread articles."
8300   (interactive "P")
8301   (if all
8302       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8303     (gnus-summary-limit-to-marks
8304      ;; Concat all the marks that say that an article is read and have
8305      ;; those removed.
8306      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8307            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8308            gnus-low-score-mark gnus-expirable-mark
8309            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8310            gnus-duplicate-mark)
8311      'reverse)))
8312
8313 (defun gnus-summary-limit-to-headers (match &optional reverse)
8314   "Limit the summary buffer to articles that have headers that match MATCH.
8315 If REVERSE (the prefix), limit to articles that don't match."
8316   (interactive "sMatch headers (regexp): \nP")
8317   (gnus-summary-limit-to-bodies match reverse t))
8318
8319 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8320   "Limit the summary buffer to articles that have bodies that match MATCH.
8321 If REVERSE (the prefix), limit to articles that don't match."
8322   (interactive "sMatch body (regexp): \nP")
8323   (let ((articles nil)
8324         (gnus-select-article-hook nil)  ;Disable hook.
8325         (gnus-article-prepare-hook nil)
8326         (gnus-use-article-prefetch nil)
8327         (gnus-keep-backlog nil)
8328         (gnus-break-pages nil)
8329         (gnus-summary-display-arrow nil)
8330         (gnus-updated-mode-lines nil)
8331         (gnus-auto-center-summary nil)
8332         (gnus-display-mime-function nil))
8333     (dolist (data gnus-newsgroup-data)
8334       (let (gnus-mark-article-hook)
8335         (gnus-summary-select-article t t nil (gnus-data-number data)))
8336       (with-current-buffer gnus-article-buffer
8337         (article-goto-body)
8338         (let* ((case-fold-search t)
8339                (found (if headersp
8340                           (re-search-backward match nil t)
8341                         (re-search-forward match nil t))))
8342           (when (or (and found
8343                          (not reverse))
8344                     (and (not found)
8345                          reverse))
8346             (push (gnus-data-number data) articles)))))
8347     (if (not articles)
8348         (message "No messages matched")
8349       (gnus-summary-limit articles)))
8350   (gnus-summary-position-point))
8351
8352 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8353   "Limit the summary buffer to articles that aren't part on any thread.
8354 If THREADSP (the prefix), limit to articles that are in threads."
8355   (interactive "P")
8356   (let ((articles nil)
8357         thread-articles
8358         threads)
8359     (dolist (thread gnus-newsgroup-threads)
8360       (if (stringp (car thread))
8361           (dolist (thread (cdr thread))
8362             (push thread threads))
8363         (push thread threads)))
8364     (dolist (thread threads)
8365       (setq thread-articles (gnus-articles-in-thread thread))
8366       (when (or (and threadsp
8367                      (> (length thread-articles) 1))
8368                 (and (not threadsp)
8369                      (= (length thread-articles) 1)))
8370         (setq articles (nconc thread-articles articles))))
8371     (if (not articles)
8372         (message "No messages matched")
8373       (gnus-summary-limit articles))
8374     (gnus-summary-position-point)))
8375
8376 (defun gnus-summary-limit-to-replied (&optional unreplied)
8377   "Limit the summary buffer to replied articles.
8378 If UNREPLIED (the prefix), limit to unreplied articles."
8379   (interactive "P")
8380   (if unreplied
8381       (gnus-summary-limit
8382        (gnus-set-difference gnus-newsgroup-articles
8383         gnus-newsgroup-replied))
8384     (gnus-summary-limit gnus-newsgroup-replied))
8385   (gnus-summary-position-point))
8386
8387 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8388 (make-obsolete 'gnus-summary-delete-marked-with
8389                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8390
8391 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8392   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8393 If REVERSE, limit the summary buffer to articles that are marked
8394 with MARKS.  MARKS can either be a string of marks or a list of marks.
8395 Returns how many articles were removed."
8396   (interactive "sMarks: ")
8397   (gnus-summary-limit-to-marks marks t))
8398
8399 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8400   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8401 If REVERSE (the prefix), limit the summary buffer to articles that are
8402 not marked with MARKS.  MARKS can either be a string of marks or a
8403 list of marks.
8404 Returns how many articles were removed."
8405   (interactive "sMarks: \nP")
8406   (prog1
8407       (let ((data gnus-newsgroup-data)
8408             (marks (if (listp marks) marks
8409                      (append marks nil))) ; Transform to list.
8410             articles)
8411         (while data
8412           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8413                   (memq (gnus-data-mark (car data)) marks))
8414             (push (gnus-data-number (car data)) articles))
8415           (setq data (cdr data)))
8416         (gnus-summary-limit articles))
8417     (gnus-summary-position-point)))
8418
8419 (defun gnus-summary-limit-to-score (score)
8420   "Limit to articles with score at or above SCORE."
8421   (interactive "NLimit to articles with score of at least: ")
8422   (let ((data gnus-newsgroup-data)
8423         articles)
8424     (while data
8425       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8426                 score)
8427         (push (gnus-data-number (car data)) articles))
8428       (setq data (cdr data)))
8429     (prog1
8430         (gnus-summary-limit articles)
8431       (gnus-summary-position-point))))
8432
8433 (defun gnus-summary-limit-to-unseen ()
8434   "Limit to unseen articles."
8435   (interactive)
8436   (prog1
8437       (gnus-summary-limit gnus-newsgroup-unseen)
8438     (gnus-summary-position-point)))
8439
8440 (defun gnus-summary-limit-include-thread (id)
8441   "Display all the hidden articles that is in the thread with ID in it.
8442 When called interactively, ID is the Message-ID of the current
8443 article."
8444   (interactive (list (mail-header-id (gnus-summary-article-header))))
8445   (let ((articles (gnus-articles-in-thread
8446                    (gnus-id-to-thread (gnus-root-id id)))))
8447     (prog1
8448         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8449       (gnus-summary-limit-include-matching-articles
8450        "subject"
8451        (regexp-quote (gnus-simplify-subject-re
8452                       (mail-header-subject (gnus-id-to-header id)))))
8453       (gnus-summary-position-point))))
8454
8455 (defun gnus-summary-limit-include-matching-articles (header regexp)
8456   "Display all the hidden articles that have HEADERs that match REGEXP."
8457   (interactive (list (read-string "Match on header: ")
8458                      (read-string "Regexp: ")))
8459   (let ((articles (gnus-find-matching-articles header regexp)))
8460     (prog1
8461         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8462       (gnus-summary-position-point))))
8463
8464 (defun gnus-summary-insert-dormant-articles ()
8465   "Insert all the dormant articles for this group into the current buffer."
8466   (interactive)
8467   (let ((gnus-verbose (max 6 gnus-verbose)))
8468     (if (not gnus-newsgroup-dormant)
8469         (gnus-message 3 "No dormant articles for this group")
8470       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8471
8472 (defun gnus-summary-insert-ticked-articles ()
8473   "Insert ticked articles for this group into the current buffer."
8474   (interactive)
8475   (let ((gnus-verbose (max 6 gnus-verbose)))
8476     (if (not gnus-newsgroup-marked)
8477         (gnus-message 3 "No ticked articles for this group")
8478       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8479
8480 (defun gnus-summary-limit-include-dormant ()
8481   "Display all the hidden articles that are marked as dormant.
8482 Note that this command only works on a subset of the articles currently
8483 fetched for this group."
8484   (interactive)
8485   (unless gnus-newsgroup-dormant
8486     (error "There are no dormant articles in this group"))
8487   (prog1
8488       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8489     (gnus-summary-position-point)))
8490
8491 (defun gnus-summary-limit-exclude-dormant ()
8492   "Hide all dormant articles."
8493   (interactive)
8494   (prog1
8495       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8496     (gnus-summary-position-point)))
8497
8498 (defun gnus-summary-limit-exclude-childless-dormant ()
8499   "Hide all dormant articles that have no children."
8500   (interactive)
8501   (let ((data (gnus-data-list t))
8502         articles d children)
8503     ;; Find all articles that are either not dormant or have
8504     ;; children.
8505     (while (setq d (pop data))
8506       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8507                 (and (setq children
8508                            (gnus-article-children (gnus-data-number d)))
8509                      (let (found)
8510                        (while children
8511                          (when (memq (car children) articles)
8512                            (setq children nil
8513                                  found t))
8514                          (pop children))
8515                        found)))
8516         (push (gnus-data-number d) articles)))
8517     ;; Do the limiting.
8518     (prog1
8519         (gnus-summary-limit articles)
8520       (gnus-summary-position-point))))
8521
8522 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8523   "Mark all unread excluded articles as read.
8524 If ALL, mark even excluded ticked and dormants as read."
8525   (interactive "P")
8526   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8527   (let ((articles (gnus-sorted-ndifference
8528                    (sort
8529                     (mapcar (lambda (h) (mail-header-number h))
8530                             gnus-newsgroup-headers)
8531                     '<)
8532                    gnus-newsgroup-limit))
8533         article)
8534     (setq gnus-newsgroup-unreads
8535           (gnus-sorted-intersection gnus-newsgroup-unreads
8536                                     gnus-newsgroup-limit))
8537     (if all
8538         (setq gnus-newsgroup-dormant nil
8539               gnus-newsgroup-marked nil
8540               gnus-newsgroup-reads
8541               (nconc
8542                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8543                gnus-newsgroup-reads))
8544       (while (setq article (pop articles))
8545         (unless (or (memq article gnus-newsgroup-dormant)
8546                     (memq article gnus-newsgroup-marked))
8547           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8548
8549 (defun gnus-summary-limit (articles &optional pop)
8550   (if pop
8551       ;; We pop the previous limit off the stack and use that.
8552       (setq articles (car gnus-newsgroup-limits)
8553             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8554     ;; We use the new limit, so we push the old limit on the stack.
8555     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8556   ;; Set the limit.
8557   (setq gnus-newsgroup-limit articles)
8558   (let ((total (length gnus-newsgroup-data))
8559         (data (gnus-data-find-list (gnus-summary-article-number)))
8560         (gnus-summary-mark-below nil)   ; Inhibit this.
8561         found)
8562     ;; This will do all the work of generating the new summary buffer
8563     ;; according to the new limit.
8564     (gnus-summary-prepare)
8565     ;; Hide any threads, possibly.
8566     (gnus-summary-maybe-hide-threads)
8567     ;; Try to return to the article you were at, or one in the
8568     ;; neighborhood.
8569     (when data
8570       ;; We try to find some article after the current one.
8571       (while data
8572         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8573           (setq data nil
8574                 found t))
8575         (setq data (cdr data))))
8576     (unless found
8577       ;; If there is no data, that means that we were after the last
8578       ;; article.  The same goes when we can't find any articles
8579       ;; after the current one.
8580       (goto-char (point-max))
8581       (gnus-summary-find-prev))
8582     (gnus-set-mode-line 'summary)
8583     ;; We return how many articles were removed from the summary
8584     ;; buffer as a result of the new limit.
8585     (- total (length gnus-newsgroup-data))))
8586
8587 (defsubst gnus-invisible-cut-children (threads)
8588   (let ((num 0))
8589     (while threads
8590       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8591         (incf num))
8592       (pop threads))
8593     (< num 2)))
8594
8595 (defsubst gnus-cut-thread (thread)
8596   "Go forwards in the thread until we find an article that we want to display."
8597   (when (or (eq gnus-fetch-old-headers 'some)
8598             (eq gnus-fetch-old-headers 'invisible)
8599             (numberp gnus-fetch-old-headers)
8600             (eq gnus-build-sparse-threads 'some)
8601             (eq gnus-build-sparse-threads 'more))
8602     ;; Deal with old-fetched headers and sparse threads.
8603     (while (and
8604             thread
8605             (or
8606              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8607              (gnus-summary-article-ancient-p
8608               (mail-header-number (car thread))))
8609             (if (or (<= (length (cdr thread)) 1)
8610                     (eq gnus-fetch-old-headers 'invisible))
8611                 (setq gnus-newsgroup-limit
8612                       (delq (mail-header-number (car thread))
8613                             gnus-newsgroup-limit)
8614                       thread (cadr thread))
8615               (when (gnus-invisible-cut-children (cdr thread))
8616                 (let ((th (cdr thread)))
8617                   (while th
8618                     (if (memq (mail-header-number (caar th))
8619                               gnus-newsgroup-limit)
8620                         (setq thread (car th)
8621                               th nil)
8622                       (setq th (cdr th))))))))))
8623   thread)
8624
8625 (defun gnus-cut-threads (threads)
8626   "Cut off all uninteresting articles from the beginning of THREADS."
8627   (when (or (eq gnus-fetch-old-headers 'some)
8628             (eq gnus-fetch-old-headers 'invisible)
8629             (numberp gnus-fetch-old-headers)
8630             (eq gnus-build-sparse-threads 'some)
8631             (eq gnus-build-sparse-threads 'more))
8632     (let ((th threads))
8633       (while th
8634         (setcar th (gnus-cut-thread (car th)))
8635         (setq th (cdr th)))))
8636   ;; Remove nixed out threads.
8637   (delq nil threads))
8638
8639 (defun gnus-summary-initial-limit (&optional show-if-empty)
8640   "Figure out what the initial limit is supposed to be on group entry.
8641 This entails weeding out unwanted dormants, low-scored articles,
8642 fetch-old-headers verbiage, and so on."
8643   ;; Most groups have nothing to remove.
8644   (unless (or gnus-inhibit-limiting
8645               (and (null gnus-newsgroup-dormant)
8646                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8647                    (not (eq gnus-fetch-old-headers 'some))
8648                    (not (numberp gnus-fetch-old-headers))
8649                    (not (eq gnus-fetch-old-headers 'invisible))
8650                    (null gnus-summary-expunge-below)
8651                    (not (eq gnus-build-sparse-threads 'some))
8652                    (not (eq gnus-build-sparse-threads 'more))
8653                    (null gnus-thread-expunge-below)))
8654     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8655     (setq gnus-newsgroup-limit nil)
8656     (mapatoms
8657      (lambda (node)
8658        (unless (car (symbol-value node))
8659          ;; These threads have no parents -- they are roots.
8660          (let ((nodes (cdr (symbol-value node)))
8661                thread)
8662            (while nodes
8663              (if (and gnus-thread-expunge-below
8664                       (< (gnus-thread-total-score (car nodes))
8665                          gnus-thread-expunge-below))
8666                  (gnus-expunge-thread (pop nodes))
8667                (setq thread (pop nodes))
8668                (gnus-summary-limit-children thread))))))
8669      gnus-newsgroup-dependencies)
8670     ;; If this limitation resulted in an empty group, we might
8671     ;; pop the previous limit and use it instead.
8672     (when (and (not gnus-newsgroup-limit)
8673                show-if-empty)
8674       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8675     gnus-newsgroup-limit))
8676
8677 (defun gnus-summary-limit-children (thread)
8678   "Return 1 if this subthread is visible and 0 if it is not."
8679   ;; First we get the number of visible children to this thread.  This
8680   ;; is done by recursing down the thread using this function, so this
8681   ;; will really go down to a leaf article first, before slowly
8682   ;; working its way up towards the root.
8683   (when thread
8684     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8685            (children
8686            (if (cdr thread)
8687                (apply '+ (mapcar 'gnus-summary-limit-children
8688                                  (cdr thread)))
8689              0))
8690            (number (mail-header-number (car thread)))
8691            score)
8692       (if (and
8693            (not (memq number gnus-newsgroup-marked))
8694            (or
8695             ;; If this article is dormant and has absolutely no visible
8696             ;; children, then this article isn't visible.
8697             (and (memq number gnus-newsgroup-dormant)
8698                  (zerop children))
8699             ;; If this is "fetch-old-headered" and there is no
8700             ;; visible children, then we don't want this article.
8701             (and (or (eq gnus-fetch-old-headers 'some)
8702                      (numberp gnus-fetch-old-headers))
8703                  (gnus-summary-article-ancient-p number)
8704                  (zerop children))
8705             ;; If this is "fetch-old-headered" and `invisible', then
8706             ;; we don't want this article.
8707             (and (eq gnus-fetch-old-headers 'invisible)
8708                  (gnus-summary-article-ancient-p number))
8709             ;; If this is a sparsely inserted article with no children,
8710             ;; we don't want it.
8711             (and (eq gnus-build-sparse-threads 'some)
8712                  (gnus-summary-article-sparse-p number)
8713                  (zerop children))
8714             ;; If we use expunging, and this article is really
8715             ;; low-scored, then we don't want this article.
8716             (when (and gnus-summary-expunge-below
8717                        (< (setq score
8718                                 (or (cdr (assq number gnus-newsgroup-scored))
8719                                     gnus-summary-default-score))
8720                           gnus-summary-expunge-below))
8721               ;; We increase the expunge-tally here, but that has
8722               ;; nothing to do with the limits, really.
8723               (incf gnus-newsgroup-expunged-tally)
8724               ;; We also mark as read here, if that's wanted.
8725               (when (and gnus-summary-mark-below
8726                          (< score gnus-summary-mark-below))
8727                 (setq gnus-newsgroup-unreads
8728                       (delq number gnus-newsgroup-unreads))
8729                 (if gnus-newsgroup-auto-expire
8730                     (push number gnus-newsgroup-expirable)
8731                   (push (cons number gnus-low-score-mark)
8732                         gnus-newsgroup-reads)))
8733               t)
8734             ;; Do the `display' group parameter.
8735             (and gnus-newsgroup-display
8736                  (let ((gnus-number number))
8737                    (not (funcall gnus-newsgroup-display))))))
8738           ;; Nope, invisible article.
8739           0
8740         ;; Ok, this article is to be visible, so we add it to the limit
8741         ;; and return 1.
8742         (push number gnus-newsgroup-limit)
8743         1))))
8744
8745 (defun gnus-expunge-thread (thread)
8746   "Mark all articles in THREAD as read."
8747   (let* ((number (mail-header-number (car thread))))
8748     (incf gnus-newsgroup-expunged-tally)
8749     ;; We also mark as read here, if that's wanted.
8750     (setq gnus-newsgroup-unreads
8751           (delq number gnus-newsgroup-unreads))
8752     (if gnus-newsgroup-auto-expire
8753         (push number gnus-newsgroup-expirable)
8754       (push (cons number gnus-low-score-mark)
8755             gnus-newsgroup-reads)))
8756   ;; Go recursively through all subthreads.
8757   (mapcar 'gnus-expunge-thread (cdr thread)))
8758
8759 ;; Summary article oriented commands
8760
8761 (defun gnus-summary-refer-parent-article (n)
8762   "Refer parent article N times.
8763 If N is negative, go to ancestor -N instead.
8764 The difference between N and the number of articles fetched is returned."
8765   (interactive "p")
8766   (let ((skip 1)
8767         error header ref)
8768     (when (not (natnump n))
8769       (setq skip (abs n)
8770             n 1))
8771     (while (and (> n 0)
8772                 (not error))
8773       (setq header (gnus-summary-article-header))
8774       (if (and (eq (mail-header-number header)
8775                    (cdr gnus-article-current))
8776                (equal gnus-newsgroup-name
8777                       (car gnus-article-current)))
8778           ;; If we try to find the parent of the currently
8779           ;; displayed article, then we take a look at the actual
8780           ;; References header, since this is slightly more
8781           ;; reliable than the References field we got from the
8782           ;; server.
8783           (with-current-buffer gnus-original-article-buffer
8784             (nnheader-narrow-to-headers)
8785             (unless (setq ref (message-fetch-field "references"))
8786               (when (setq ref (message-fetch-field "in-reply-to"))
8787                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8788             (widen))
8789         (setq ref
8790               ;; It's not the current article, so we take a bet on
8791               ;; the value we got from the server.
8792               (mail-header-references header)))
8793       (if (and ref
8794                (not (equal ref "")))
8795           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8796             (gnus-message 1 "Couldn't find parent"))
8797         (gnus-message 1 "No references in article %d"
8798                       (gnus-summary-article-number))
8799         (setq error t))
8800       (decf n))
8801     (gnus-summary-position-point)
8802     n))
8803
8804 (defun gnus-summary-refer-references ()
8805   "Fetch all articles mentioned in the References header.
8806 Return the number of articles fetched."
8807   (interactive)
8808   (let ((ref (mail-header-references (gnus-summary-article-header)))
8809         (current (gnus-summary-article-number))
8810         (n 0))
8811     (if (or (not ref)
8812             (equal ref ""))
8813         (error "No References in the current article")
8814       ;; For each Message-ID in the References header...
8815       (while (string-match "<[^>]*>" ref)
8816         (incf n)
8817         ;; ... fetch that article.
8818         (gnus-summary-refer-article
8819          (prog1 (match-string 0 ref)
8820            (setq ref (substring ref (match-end 0))))))
8821       (gnus-summary-goto-subject current)
8822       (gnus-summary-position-point)
8823       n)))
8824
8825 (defun gnus-summary-refer-thread (&optional limit)
8826   "Fetch all articles in the current thread.
8827 If no backend-specific 'request-thread function is available
8828 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8829 fetch what's specified by the `gnus-refer-thread-limit'
8830 variable."
8831   (interactive "P")
8832   (let ((id (mail-header-id (gnus-summary-article-header)))
8833         (gnus-summary-ignore-duplicates t)
8834         (limit (if limit (prefix-numeric-value limit)
8835                  gnus-refer-thread-limit)))
8836     (if  (gnus-check-backend-function 'request-thread gnus-newsgroup-name)
8837         (gnus-request-thread id)
8838       (unless (eq gnus-fetch-old-headers 'invisible)
8839         (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8840         ;;      Retrieve the headers and read them in.
8841         (if (numberp limit)
8842             (gnus-retrieve-headers
8843              (list (min
8844                     (+ (mail-header-number
8845                         (gnus-summary-article-header))
8846                        limit)
8847                     gnus-newsgroup-end))
8848              gnus-newsgroup-name (* limit 2))
8849           ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8850           ;; headers.
8851           (gnus-retrieve-headers (list gnus-newsgroup-end)
8852                                  gnus-newsgroup-name limit)
8853           (gnus-message 5 "Fetching headers for %s...done"
8854                         gnus-newsgroup-name))))
8855     (when (eq gnus-headers-retrieved-by 'nov)
8856       (gnus-build-all-threads))
8857     (gnus-summary-limit-include-thread id)))
8858
8859 (defun gnus-summary-refer-article (message-id)
8860   "Fetch an article specified by MESSAGE-ID."
8861   (interactive "sMessage-ID: ")
8862   (when (and (stringp message-id)
8863              (not (zerop (length message-id))))
8864     (setq message-id (gnus-replace-in-string message-id " " ""))
8865     ;; Construct the correct Message-ID if necessary.
8866     ;; Suggested by tale@pawl.rpi.edu.
8867     (unless (string-match "^<" message-id)
8868       (setq message-id (concat "<" message-id)))
8869     (unless (string-match ">$" message-id)
8870       (setq message-id (concat message-id ">")))
8871     ;; People often post MIDs from URLs, so unhex it:
8872     (unless (string-match "@" message-id)
8873       (setq message-id (gnus-url-unhex-string message-id)))
8874     (let* ((header (gnus-id-to-header message-id))
8875            (sparse (and header
8876                         (gnus-summary-article-sparse-p
8877                          (mail-header-number header))
8878                         (memq (mail-header-number header)
8879                               gnus-newsgroup-limit)))
8880            number)
8881       (cond
8882        ;; If the article is present in the buffer we just go to it.
8883        ((and header
8884              (or (not (gnus-summary-article-sparse-p
8885                        (mail-header-number header)))
8886                  sparse))
8887         (prog1
8888             (gnus-summary-goto-article
8889              (mail-header-number header) nil t)
8890           (when sparse
8891             (gnus-summary-update-article (mail-header-number header)))))
8892        (t
8893         ;; We fetch the article.
8894         (catch 'found
8895           (dolist (gnus-override-method (gnus-refer-article-methods))
8896             (when (and (gnus-check-server gnus-override-method)
8897                        ;; Fetch the header,
8898                        (setq number (gnus-summary-insert-subject message-id)))
8899               ;; and display the article.
8900               (gnus-summary-select-article nil nil nil number)
8901               (throw 'found t)))
8902           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8903
8904 (defun gnus-refer-article-methods ()
8905   "Return a list of referable methods."
8906   (cond
8907    ;; No method, so we default to current and native.
8908    ((null gnus-refer-article-method)
8909     (list gnus-current-select-method gnus-select-method))
8910    ;; Current.
8911    ((eq 'current gnus-refer-article-method)
8912     (list gnus-current-select-method))
8913    ;; List of select methods.
8914    ((not (and (symbolp (car gnus-refer-article-method))
8915               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8916     (let (out)
8917       (dolist (method gnus-refer-article-method)
8918         (push (if (eq 'current method)
8919                   gnus-current-select-method
8920                 method)
8921               out))
8922       (nreverse out)))
8923    ;; One single select method.
8924    (t
8925     (list gnus-refer-article-method))))
8926
8927 (defun gnus-summary-edit-parameters ()
8928   "Edit the group parameters of the current group."
8929   (interactive)
8930   (gnus-group-edit-group gnus-newsgroup-name 'params))
8931
8932 (defun gnus-summary-customize-parameters ()
8933   "Customize the group parameters of the current group."
8934   (interactive)
8935   (gnus-group-customize gnus-newsgroup-name))
8936
8937 (defun gnus-summary-enter-digest-group (&optional force)
8938   "Enter an nndoc group based on the current article.
8939 If FORCE, force a digest interpretation.  If not, try
8940 to guess what the document format is."
8941   (interactive "P")
8942   (let ((conf gnus-current-window-configuration))
8943     (save-window-excursion
8944       (save-excursion
8945         (let (gnus-article-prepare-hook
8946               gnus-display-mime-function
8947               gnus-break-pages)
8948           (gnus-summary-select-article))))
8949     (setq gnus-current-window-configuration conf)
8950     (let* ((name (format "%s-%d"
8951                          (gnus-group-prefixed-name
8952                           gnus-newsgroup-name (list 'nndoc ""))
8953                          (with-current-buffer gnus-summary-buffer
8954                            gnus-current-article)))
8955            (ogroup gnus-newsgroup-name)
8956            (params (append (gnus-info-params (gnus-get-info ogroup))
8957                            (list (cons 'to-group ogroup))
8958                            (list (cons 'parent-group ogroup))
8959                            (list (cons 'save-article-group ogroup))))
8960            (case-fold-search t)
8961            (buf (current-buffer))
8962            dig to-address)
8963       (with-current-buffer gnus-original-article-buffer
8964         ;; Have the digest group inherit the main mail address of
8965         ;; the parent article.
8966         (when (setq to-address (or (gnus-fetch-field "reply-to")
8967                                    (gnus-fetch-field "from")))
8968           (setq params
8969                 (append
8970                  (list (cons 'to-address
8971                              (funcall gnus-decode-encoded-address-function
8972                                       to-address))))))
8973         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8974         (insert-buffer-substring gnus-original-article-buffer)
8975         ;; Remove lines that may lead nndoc to misinterpret the
8976         ;; document type.
8977         (narrow-to-region
8978          (goto-char (point-min))
8979          (or (search-forward "\n\n" nil t) (point)))
8980         (goto-char (point-min))
8981         (delete-matching-lines "^Path:\\|^From ")
8982         (widen))
8983       (unwind-protect
8984           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8985                     (gnus-newsgroup-ephemeral-ignored-charsets
8986                      gnus-newsgroup-ignored-charsets))
8987                 (gnus-group-read-ephemeral-group
8988                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8989                               (nndoc-article-type
8990                                ,(if force 'mbox 'guess)))
8991                  t nil nil nil
8992                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8993                                                         "ADAPT")))))
8994               ;; Make all postings to this group go to the parent group.
8995               (nconc (gnus-info-params (gnus-get-info name))
8996                      params)
8997             ;; Couldn't select this doc group.
8998             (switch-to-buffer buf)
8999             (gnus-set-global-variables)
9000             (gnus-configure-windows 'summary)
9001             (gnus-message 3 "Article couldn't be entered?"))
9002         (kill-buffer dig)))))
9003
9004 (defun gnus-summary-read-document (n)
9005   "Open a new group based on the current article(s).
9006 This will allow you to read digests and other similar
9007 documents as newsgroups.
9008 Obeys the standard process/prefix convention."
9009   (interactive "P")
9010   (let* ((ogroup gnus-newsgroup-name)
9011          (params (append (gnus-info-params (gnus-get-info ogroup))
9012                          (list (cons 'to-group ogroup))))
9013          group egroup groups vgroup)
9014     (dolist (article (gnus-summary-work-articles n))
9015       (setq group (format "%s-%d" gnus-newsgroup-name article))
9016       (gnus-summary-remove-process-mark article)
9017       (when (gnus-summary-display-article article)
9018         (save-excursion ;;What for?
9019           (with-temp-buffer
9020             (insert-buffer-substring gnus-original-article-buffer)
9021             ;; Remove some headers that may lead nndoc to make
9022             ;; the wrong guess.
9023             (message-narrow-to-head)
9024             (goto-char (point-min))
9025             (delete-matching-lines "^Path:\\|^From ")
9026             (widen)
9027             (if (setq egroup
9028                       (gnus-group-read-ephemeral-group
9029                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9030                                      (nndoc-article-type guess))
9031                        t nil t))
9032                 (progn
9033                   ;; Make all postings to this group go to the parent group.
9034                   (nconc (gnus-info-params (gnus-get-info egroup))
9035                          params)
9036                   (push egroup groups))
9037               ;; Couldn't select this doc group.
9038               (gnus-error 3 "Article couldn't be entered"))))))
9039     ;; Now we have selected all the documents.
9040     (cond
9041      ((not groups)
9042       (error "None of the articles could be interpreted as documents"))
9043      ((gnus-group-read-ephemeral-group
9044        (setq vgroup (format
9045                      "nnvirtual:%s-%s" gnus-newsgroup-name
9046                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9047        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9048        t
9049        (cons (current-buffer) 'summary)))
9050      (t
9051       (error "Couldn't select virtual nndoc group")))))
9052
9053 (defun gnus-summary-isearch-article (&optional regexp-p)
9054   "Do incremental search forward on the current article.
9055 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9056   (interactive "P")
9057   (gnus-summary-select-article)
9058   (gnus-configure-windows 'article)
9059   (gnus-eval-in-buffer-window gnus-article-buffer
9060     (save-restriction
9061       (widen)
9062       (isearch-forward regexp-p))))
9063
9064 (defun gnus-summary-repeat-search-article-forward ()
9065   "Repeat the previous search forwards."
9066   (interactive)
9067   (unless gnus-last-search-regexp
9068     (error "No previous search"))
9069   (gnus-summary-search-article-forward gnus-last-search-regexp))
9070
9071 (defun gnus-summary-repeat-search-article-backward ()
9072   "Repeat the previous search backwards."
9073   (interactive)
9074   (unless gnus-last-search-regexp
9075     (error "No previous search"))
9076   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9077
9078 (defun gnus-summary-search-article-forward (regexp &optional backward)
9079   "Search for an article containing REGEXP forward.
9080 If BACKWARD, search backward instead."
9081   (interactive
9082    (list (read-string
9083           (format "Search article %s (regexp%s): "
9084                   (if current-prefix-arg "backward" "forward")
9085                   (if gnus-last-search-regexp
9086                       (concat ", default " gnus-last-search-regexp)
9087                     "")))
9088          current-prefix-arg))
9089   (if (string-equal regexp "")
9090       (setq regexp (or gnus-last-search-regexp ""))
9091     (setq gnus-last-search-regexp regexp)
9092     (setq gnus-article-before-search gnus-current-article))
9093   ;; Intentionally set gnus-last-article.
9094   (setq gnus-last-article gnus-article-before-search)
9095   (let ((gnus-last-article gnus-last-article))
9096     (if (gnus-summary-search-article regexp backward)
9097         (gnus-summary-show-thread)
9098       (signal 'search-failed (list regexp)))))
9099
9100 (defun gnus-summary-search-article-backward (regexp)
9101   "Search for an article containing REGEXP backward."
9102   (interactive
9103    (list (read-string
9104           (format "Search article backward (regexp%s): "
9105                   (if gnus-last-search-regexp
9106                       (concat ", default " gnus-last-search-regexp)
9107                     "")))))
9108   (gnus-summary-search-article-forward regexp 'backward))
9109
9110 (defun gnus-summary-search-article (regexp &optional backward)
9111   "Search for an article containing REGEXP.
9112 Optional argument BACKWARD means do search for backward.
9113 `gnus-select-article-hook' is not called during the search."
9114   ;; We have to require this here to make sure that the following
9115   ;; dynamic binding isn't shadowed by autoloading.
9116   (require 'gnus-async)
9117   (require 'gnus-art)
9118   (let ((gnus-select-article-hook nil)  ;Disable hook.
9119         (gnus-article-prepare-hook nil)
9120         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9121         (gnus-use-article-prefetch nil)
9122         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9123         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9124         (gnus-visual nil)
9125         (gnus-keep-backlog nil)
9126         (gnus-break-pages nil)
9127         (gnus-summary-display-arrow nil)
9128         (gnus-updated-mode-lines nil)
9129         (gnus-auto-center-summary nil)
9130         (sum (current-buffer))
9131         (gnus-display-mime-function nil)
9132         (found nil)
9133         point)
9134     (gnus-save-hidden-threads
9135       (gnus-summary-select-article)
9136       (set-buffer gnus-article-buffer)
9137       (goto-char (window-point (get-buffer-window (current-buffer))))
9138       (when backward
9139         (forward-line -1))
9140       (while (not found)
9141         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9142         (if (if backward
9143                 (re-search-backward regexp nil t)
9144               (re-search-forward regexp nil t))
9145             ;; We found the regexp.
9146             (progn
9147               (setq found 'found)
9148               (beginning-of-line)
9149               (set-window-start
9150                (get-buffer-window (current-buffer))
9151                (point))
9152               (forward-line 1)
9153               (set-window-point
9154                (get-buffer-window (current-buffer))
9155                (point))
9156               (set-buffer sum)
9157               (setq point (point)))
9158           ;; We didn't find it, so we go to the next article.
9159           (set-buffer sum)
9160           (setq found 'not)
9161           (while (eq found 'not)
9162             (if (not (if backward (gnus-summary-find-prev)
9163                        (gnus-summary-find-next)))
9164                 ;; No more articles.
9165                 (setq found t)
9166               ;; Select the next article and adjust point.
9167               (unless (gnus-summary-article-sparse-p
9168                        (gnus-summary-article-number))
9169                 (setq found nil)
9170                 (gnus-summary-select-article)
9171                 (set-buffer gnus-article-buffer)
9172                 (widen)
9173                 (goto-char (if backward (point-max) (point-min))))))))
9174       (gnus-message 7 ""))
9175     ;; Return whether we found the regexp.
9176     (when (eq found 'found)
9177       (goto-char point)
9178       (gnus-summary-show-thread)
9179       (gnus-summary-goto-subject gnus-current-article)
9180       (gnus-summary-position-point)
9181       t)))
9182
9183 (defun gnus-find-matching-articles (header regexp)
9184   "Return a list of all articles that match REGEXP on HEADER.
9185 This search includes all articles in the current group that Gnus has
9186 fetched headers for, whether they are displayed or not."
9187   (let ((articles nil)
9188         ;; Can't eta-reduce because it's a macro.
9189         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9190         (case-fold-search t))
9191     (dolist (header gnus-newsgroup-headers)
9192       (when (string-match regexp (funcall func header))
9193         (push (mail-header-number header) articles)))
9194     (nreverse articles)))
9195
9196 (defun gnus-summary-find-matching (header regexp &optional backward unread
9197                                           not-case-fold not-matching)
9198   "Return a list of all articles that match REGEXP on HEADER.
9199 The search stars on the current article and goes forwards unless
9200 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9201 If UNREAD is non-nil, only unread articles will
9202 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9203 in the comparisons. If NOT-MATCHING, return a list of all articles that
9204 not match REGEXP on HEADER."
9205   (let ((case-fold-search (not not-case-fold))
9206         articles d func)
9207     (if (consp header)
9208         (if (eq (car header) 'extra)
9209             (setq func
9210                   `(lambda (h)
9211                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9212                          "")))
9213           (error "%s is an invalid header" header))
9214       (unless (fboundp (intern (concat "mail-header-" header)))
9215         (error "%s is not a valid header" header))
9216       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9217     (dolist (d (if (eq backward 'all)
9218                    gnus-newsgroup-data
9219                  (gnus-data-find-list
9220                   (gnus-summary-article-number)
9221                   (gnus-data-list backward))))
9222       (when (and (or (not unread)       ; We want all articles...
9223                      (gnus-data-unread-p d)) ; Or just unreads.
9224                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9225                  (if not-matching
9226                      (not (string-match
9227                            regexp
9228                            (funcall func (gnus-data-header d))))
9229                    (string-match regexp
9230                                  (funcall func (gnus-data-header d)))))
9231         (push (gnus-data-number d) articles))) ; Success!
9232     (nreverse articles)))
9233
9234 (defun gnus-summary-execute-command (header regexp command &optional backward)
9235   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9236 If HEADER is an empty string (or nil), the match is done on the entire
9237 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9238   (interactive
9239    (list (let ((completion-ignore-case t))
9240            (gnus-completing-read
9241             "Header name"
9242             (mapcar 'symbol-name
9243                     (append
9244                      '(Number Subject From Lines Date
9245                        Message-ID Xref References Body)
9246                      gnus-extra-headers))
9247             'require-match))
9248          (read-string "Regexp: ")
9249          (read-key-sequence "Command: ")
9250          current-prefix-arg))
9251   (when (equal header "Body")
9252     (setq header ""))
9253   ;; Hidden thread subtrees must be searched as well.
9254   (gnus-summary-show-all-threads)
9255   ;; We don't want to change current point nor window configuration.
9256   (save-excursion
9257     (save-window-excursion
9258       (let (gnus-visual
9259             gnus-treat-strip-trailing-blank-lines
9260             gnus-treat-strip-leading-blank-lines
9261             gnus-treat-strip-multiple-blank-lines
9262             gnus-treat-hide-boring-headers
9263             gnus-treat-fold-newsgroups
9264             gnus-article-prepare-hook)
9265         (gnus-message 6 "Executing %s..." (key-description command))
9266         ;; We'd like to execute COMMAND interactively so as to give arguments.
9267         (gnus-execute header regexp
9268                       `(call-interactively ',(key-binding command))
9269                       backward)
9270         (gnus-message 6 "Executing %s...done" (key-description command))))))
9271
9272 (defun gnus-summary-beginning-of-article ()
9273   "Scroll the article back to the beginning."
9274   (interactive)
9275   (gnus-summary-select-article)
9276   (gnus-configure-windows 'article)
9277   (gnus-eval-in-buffer-window gnus-article-buffer
9278     (widen)
9279     (goto-char (point-min))
9280     (when gnus-break-pages
9281       (gnus-narrow-to-page))))
9282
9283 (defun gnus-summary-end-of-article ()
9284   "Scroll to the end of the article."
9285   (interactive)
9286   (gnus-summary-select-article)
9287   (gnus-configure-windows 'article)
9288   (gnus-eval-in-buffer-window gnus-article-buffer
9289     (widen)
9290     (goto-char (point-max))
9291     (recenter -3)
9292     (when gnus-break-pages
9293       (gnus-narrow-to-page))))
9294
9295 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9296   "Truncate to LEN and quote all \"(\"'s in STRING."
9297   (gnus-replace-in-string (if (and len (> (length string) len))
9298                               (substring string 0 len)
9299                             string)
9300                           "[()]" "\\\\\\&"))
9301
9302 (defun gnus-summary-print-article (&optional filename n)
9303   "Generate and print a PostScript image of the process-marked (mail) articles.
9304
9305 If used interactively, print the current article if none are
9306 process-marked.  With prefix arg, prompt the user for the name of the
9307 file to save in.
9308
9309 When used from Lisp, accept two optional args FILENAME and N.  N means
9310 to print the next N articles.  If N is negative, print the N previous
9311 articles.  If N is nil and articles have been marked with the process
9312 mark, print these instead.
9313
9314 If the optional first argument FILENAME is nil, send the image to the
9315 printer.  If FILENAME is a string, save the PostScript image in a file with
9316 that name.  If FILENAME is a number, prompt the user for the name of the file
9317 to save in."
9318   (interactive (list (ps-print-preprint current-prefix-arg)))
9319   (dolist (article (gnus-summary-work-articles n))
9320     (gnus-summary-select-article nil nil 'pseudo article)
9321     (gnus-eval-in-buffer-window gnus-article-buffer
9322       (gnus-print-buffer))
9323     (gnus-summary-remove-process-mark article))
9324   (ps-despool filename))
9325
9326 (defun gnus-print-buffer ()
9327   (let ((buffer (generate-new-buffer " *print*")))
9328     (unwind-protect
9329         (progn
9330           (copy-to-buffer buffer (point-min) (point-max))
9331           (set-buffer buffer)
9332           (gnus-remove-text-with-property 'gnus-decoration)
9333           (when (gnus-visual-p 'article-highlight 'highlight)
9334             ;; Copy-to-buffer doesn't copy overlay.  So redo
9335             ;; highlight.
9336             (let ((gnus-article-buffer buffer))
9337               (gnus-article-highlight-citation t)
9338               (gnus-article-highlight-signature)
9339               (gnus-article-emphasize)
9340               (gnus-article-delete-invisible-text)))
9341           (let ((ps-left-header
9342                  (list
9343                   (concat "("
9344                           (gnus-summary-print-truncate-and-quote
9345                            (mail-header-subject gnus-current-headers)
9346                            66) ")")
9347                   (concat "("
9348                           (gnus-summary-print-truncate-and-quote
9349                            (mail-header-from gnus-current-headers)
9350                            45) ")")))
9351                 (ps-right-header
9352                  (list
9353                   "/pagenumberstring load"
9354                   (concat "("
9355                           (mail-header-date gnus-current-headers) ")"))))
9356             (gnus-run-hooks 'gnus-ps-print-hook)
9357             (save-excursion
9358               (if ps-print-color-p
9359                   (ps-spool-buffer-with-faces)
9360                 (ps-spool-buffer)))))
9361       (kill-buffer buffer))))
9362
9363 (defun gnus-summary-show-complete-article ()
9364   "Show a complete version of the current article.
9365 This is only useful if you're looking at a partial version of the
9366 article currently."
9367   (interactive)
9368   (let ((gnus-keep-backlog nil)
9369         (gnus-use-cache nil)
9370         (gnus-agent nil)
9371         (variable (intern
9372                    (format "%s-fetch-partial-articles"
9373                            (car (gnus-find-method-for-group
9374                                  gnus-newsgroup-name)))
9375                    obarray))
9376         old-val)
9377     (unwind-protect
9378         (progn
9379           (setq old-val (symbol-value variable))
9380           (set variable nil)
9381           (gnus-flush-original-article-buffer)
9382           (gnus-summary-show-article))
9383       (set variable old-val))))
9384
9385 (defun gnus-summary-show-article (&optional arg)
9386   "Force redisplaying of the current article.
9387 If ARG (the prefix) is a number, show the article with the charset
9388 defined in `gnus-summary-show-article-charset-alist', or the charset
9389 input.
9390 If ARG (the prefix) is non-nil and not a number, show the raw article
9391 without any article massaging functions being run.  Normally, the key
9392 strokes are `C-u g'."
9393   (interactive "P")
9394   (cond
9395    ((numberp arg)
9396     (gnus-summary-show-article t)
9397     (let ((gnus-newsgroup-charset
9398            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9399                (mm-read-coding-system
9400                 "View as charset: " ;; actually it is coding system.
9401                 (with-current-buffer gnus-article-buffer
9402                   (mm-detect-coding-region (point) (point-max))))))
9403           (gnus-newsgroup-ignored-charsets 'gnus-all))
9404       (gnus-summary-select-article nil 'force)
9405       (let ((deps gnus-newsgroup-dependencies)
9406             head header lines)
9407         (with-current-buffer gnus-original-article-buffer
9408           (save-restriction
9409             (message-narrow-to-head)
9410             (setq head (buffer-string))
9411             (goto-char (point-min))
9412             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9413               (goto-char (point-max))
9414               (widen)
9415               (setq lines (1- (count-lines (point) (point-max))))))
9416           (with-temp-buffer
9417             (insert (format "211 %d Article retrieved.\n"
9418                             (cdr gnus-article-current)))
9419             (insert head)
9420             (if lines (insert (format "Lines: %d\n" lines)))
9421             (insert ".\n")
9422             (let ((nntp-server-buffer (current-buffer)))
9423               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9424         (gnus-data-set-header
9425          (gnus-data-find (cdr gnus-article-current))
9426          header)
9427         (gnus-summary-update-article-line
9428          (cdr gnus-article-current) header)
9429         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9430           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9431    ((not arg)
9432     ;; Select the article the normal way.
9433     (gnus-summary-select-article nil 'force))
9434    (t
9435     ;; We have to require this here to make sure that the following
9436     ;; dynamic binding isn't shadowed by autoloading.
9437     (require 'gnus-async)
9438     (require 'gnus-art)
9439     ;; Bind the article treatment functions to nil.
9440     (let ((gnus-have-all-headers t)
9441           gnus-article-prepare-hook
9442           gnus-article-decode-hook
9443           gnus-display-mime-function
9444           gnus-break-pages)
9445       ;; Destroy any MIME parts.
9446       (when (gnus-buffer-live-p gnus-article-buffer)
9447         (with-current-buffer gnus-article-buffer
9448           (mm-destroy-parts gnus-article-mime-handles)
9449           ;; Set it to nil for safety reason.
9450           (setq gnus-article-mime-handle-alist nil)
9451           (setq gnus-article-mime-handles nil)))
9452       (gnus-summary-select-article nil 'force))))
9453   (gnus-summary-goto-subject gnus-current-article)
9454   (gnus-summary-position-point))
9455
9456 (defun gnus-summary-show-raw-article ()
9457   "Show the raw article without any article massaging functions being run."
9458   (interactive)
9459   (gnus-summary-show-article t))
9460
9461 (defun gnus-summary-verbose-headers (&optional arg)
9462   "Toggle permanent full header display.
9463 If ARG is a positive number, turn header display on.
9464 If ARG is a negative number, turn header display off."
9465   (interactive "P")
9466   (setq gnus-show-all-headers
9467         (cond ((or (not (numberp arg))
9468                    (zerop arg))
9469                (not gnus-show-all-headers))
9470               ((natnump arg)
9471                t)))
9472   (gnus-summary-show-article))
9473
9474 (defun gnus-summary-toggle-header (&optional arg)
9475   "Show the headers if they are hidden, or hide them if they are shown.
9476 If ARG is a positive number, show the entire header.
9477 If ARG is a negative number, hide the unwanted header lines."
9478   (interactive "P")
9479   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9480                      (get-buffer-window gnus-article-buffer t))))
9481     (with-current-buffer gnus-article-buffer
9482       (widen)
9483       (article-narrow-to-head)
9484       (let* ((inhibit-read-only t)
9485              (inhibit-point-motion-hooks t)
9486              (hidden (if (numberp arg)
9487                          (>= arg 0)
9488                        (or (not (looking-at "[^ \t\n]+:"))
9489                            (gnus-article-hidden-text-p 'headers))))
9490              s e)
9491         (delete-region (point-min) (point-max))
9492         (with-current-buffer gnus-original-article-buffer
9493           (goto-char (setq s (point-min)))
9494           (setq e (if (search-forward "\n\n" nil t)
9495                       (1- (point))
9496                     (point-max))))
9497         (insert-buffer-substring gnus-original-article-buffer s e)
9498         (run-hooks 'gnus-article-decode-hook)
9499         (if hidden
9500             (let ((gnus-treat-hide-headers nil)
9501                   (gnus-treat-hide-boring-headers nil))
9502               (gnus-delete-wash-type 'headers)
9503               (gnus-treat-article 'head))
9504           (gnus-treat-article 'head))
9505         (widen)
9506         (if window
9507             (set-window-start window (goto-char (point-min))))
9508         (if gnus-break-pages
9509             (gnus-narrow-to-page)
9510           (when (gnus-visual-p 'page-marker)
9511             (let ((inhibit-read-only t))
9512               (gnus-remove-text-with-property 'gnus-prev)
9513               (gnus-remove-text-with-property 'gnus-next))))
9514         (gnus-set-mode-line 'article)))))
9515
9516 (defun gnus-summary-show-all-headers ()
9517   "Make all header lines visible."
9518   (interactive)
9519   (gnus-summary-toggle-header 1))
9520
9521 (defun gnus-summary-caesar-message (&optional arg)
9522   "Caesar rotate the current article by 13.
9523 With a non-numerical prefix, also rotate headers.  A numerical
9524 prefix specifies how many places to rotate each letter forward."
9525   (interactive "P")
9526   (gnus-summary-select-article)
9527   (let ((mail-header-separator ""))
9528     (gnus-eval-in-buffer-window gnus-article-buffer
9529       (save-restriction
9530         (widen)
9531         (let ((start (window-start))
9532               (inhibit-read-only t))
9533           (if (equal arg '(4))
9534               (message-caesar-buffer-body nil t)
9535             (message-caesar-buffer-body arg))
9536           (set-window-start (get-buffer-window (current-buffer)) start)))))
9537   ;; Create buttons and stuff...
9538   (gnus-treat-article nil))
9539
9540 (declare-function idna-to-unicode "ext:idna" (str))
9541
9542 (defun gnus-summary-idna-message (&optional arg)
9543   "Decode IDNA encoded domain names in the current articles.
9544 IDNA encoded domain names looks like `xn--bar'.  If a string
9545 remain unencoded after running this function, it is likely an
9546 invalid IDNA string (`xn--bar' is invalid).
9547
9548 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9549 installed for this command to work."
9550   (interactive "P")
9551   (if (not (and (condition-case nil (require 'idna)
9552                   (file-error))
9553                 (mm-coding-system-p 'utf-8)
9554                 (executable-find (symbol-value 'idna-program))))
9555       (gnus-message
9556        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9557     (gnus-summary-select-article)
9558     (let ((mail-header-separator ""))
9559       (gnus-eval-in-buffer-window gnus-article-buffer
9560         (save-restriction
9561           (widen)
9562           (let ((start (window-start))
9563                 buffer-read-only)
9564             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9565               (replace-match (idna-to-unicode (match-string 1))))
9566             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9567
9568 (defun gnus-summary-morse-message (&optional arg)
9569   "Morse decode the current article."
9570   (interactive "P")
9571   (gnus-summary-select-article)
9572   (let ((mail-header-separator ""))
9573     (gnus-eval-in-buffer-window gnus-article-buffer
9574       (save-excursion
9575         (save-restriction
9576           (widen)
9577           (let ((pos (window-start))
9578                 (inhibit-read-only t))
9579             (goto-char (point-min))
9580             (when (message-goto-body)
9581               (gnus-narrow-to-body))
9582             (goto-char (point-min))
9583             (while (search-forward "·" (point-max) t)
9584               (replace-match "."))
9585             (unmorse-region (point-min) (point-max))
9586             (widen)
9587             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9588
9589 (defun gnus-summary-stop-page-breaking ()
9590   "Stop page breaking in the current article."
9591   (interactive)
9592   (gnus-summary-select-article)
9593   (gnus-eval-in-buffer-window gnus-article-buffer
9594     (widen)
9595     (when (gnus-visual-p 'page-marker)
9596       (let ((inhibit-read-only t))
9597         (gnus-remove-text-with-property 'gnus-prev)
9598         (gnus-remove-text-with-property 'gnus-next))
9599       (setq gnus-page-broken nil))))
9600
9601 (defun gnus-summary-move-article (&optional n to-newsgroup
9602                                             select-method action)
9603   "Move the current article to a different newsgroup.
9604 If N is a positive number, move the N next articles.
9605 If N is a negative number, move the N previous articles.
9606 If N is nil and any articles have been marked with the process mark,
9607 move those articles instead.
9608 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9609 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9610 re-spool using this method.
9611
9612 When called interactively with TO-NEWSGROUP being nil, the value of
9613 the variable `gnus-move-split-methods' is used for finding a default
9614 for the target newsgroup.
9615
9616 For this function to work, both the current newsgroup and the
9617 newsgroup that you want to move to have to support the `request-move'
9618 and `request-accept' functions.
9619
9620 ACTION can be either `move' (the default), `crosspost' or `copy'."
9621   (interactive "P")
9622   (unless action
9623     (setq action 'move))
9624   ;; Check whether the source group supports the required functions.
9625   (cond ((and (eq action 'move)
9626               (not (gnus-check-backend-function
9627                     'request-move-article gnus-newsgroup-name)))
9628          (error "The current group does not support article moving"))
9629         ((and (eq action 'crosspost)
9630               (not (gnus-check-backend-function
9631                     'request-replace-article gnus-newsgroup-name)))
9632          (error "The current group does not support article editing")))
9633   (let ((articles (gnus-summary-work-articles n))
9634         (prefix (if (gnus-check-backend-function
9635                      'request-move-article gnus-newsgroup-name)
9636                     (funcall gnus-move-group-prefix-function
9637                              gnus-newsgroup-name)
9638                   ""))
9639         (names '((move "Move" "Moving")
9640                  (copy "Copy" "Copying")
9641                  (crosspost "Crosspost" "Crossposting")))
9642         (copy-buf (save-excursion
9643                     (nnheader-set-temp-buffer " *copy article*")))
9644         art-group to-method new-xref article to-groups
9645         articles-to-update-marks encoded)
9646     (unless (assq action names)
9647       (error "Unknown action %s" action))
9648     ;; Read the newsgroup name.
9649     (when (and (not to-newsgroup)
9650                (not select-method))
9651       (if (and gnus-move-split-methods
9652                (not
9653                 (and (memq gnus-current-article articles)
9654                      (gnus-buffer-live-p gnus-original-article-buffer))))
9655           ;; When `gnus-move-split-methods' is non-nil, we have to
9656           ;; select an article to give `gnus-read-move-group-name' an
9657           ;; opportunity to suggest an appropriate default.  However,
9658           ;; we needn't render or mark the article.
9659           (let ((gnus-display-mime-function nil)
9660                 (gnus-article-prepare-hook nil)
9661                 (gnus-mark-article-hook nil))
9662             (gnus-summary-select-article nil nil nil (car articles))))
9663       (setq to-newsgroup (gnus-read-move-group-name
9664                           (cadr (assq action names))
9665                           (symbol-value
9666                            (intern (format "gnus-current-%s-group" action)))
9667                           articles prefix)
9668             encoded to-newsgroup
9669             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9670       (set (intern (format "gnus-current-%s-group" action))
9671            (mm-decode-coding-string
9672             to-newsgroup
9673             (gnus-group-name-charset to-method to-newsgroup))))
9674     (unless to-method
9675       (setq to-method (or select-method
9676                           (gnus-server-to-method
9677                            (gnus-group-method to-newsgroup)))))
9678     (setq to-newsgroup
9679           (or encoded
9680               (and to-newsgroup
9681                    (mm-encode-coding-string
9682                     to-newsgroup
9683                     (gnus-group-name-charset to-method to-newsgroup)))))
9684     ;; Check the method we are to move this article to...
9685     (unless (gnus-check-backend-function
9686              'request-accept-article (car to-method))
9687       (error "%s does not support article copying" (car to-method)))
9688     (unless (gnus-check-server to-method)
9689       (error "Can't open server %s" (car to-method)))
9690     (gnus-message 6 "%s to %s: %s..."
9691                   (caddr (assq action names))
9692                   (or (car select-method)
9693                       (gnus-group-decoded-name to-newsgroup))
9694                   articles)
9695     (while articles
9696       (setq article (pop articles))
9697       (setq
9698        art-group
9699        (cond
9700         ;; Move the article.
9701         ((eq action 'move)
9702          ;; Remove this article from future suppression.
9703          (gnus-dup-unsuppress-article article)
9704          (let* ((from-method (gnus-find-method-for-group
9705                               gnus-newsgroup-name))
9706                 (to-method (or select-method
9707                                (gnus-find-method-for-group to-newsgroup)))
9708                 (move-is-internal (gnus-server-equal from-method to-method)))
9709            (gnus-request-move-article
9710             article                     ; Article to move
9711             gnus-newsgroup-name         ; From newsgroup
9712             (nth 1 (gnus-find-method-for-group
9713                     gnus-newsgroup-name)) ; Server
9714             (list 'gnus-request-accept-article
9715                   to-newsgroup (list 'quote select-method)
9716                   (not articles) t)     ; Accept form
9717             (not articles)              ; Only save nov last time
9718             (and move-is-internal
9719                  to-newsgroup           ; Not respooling
9720                  (gnus-group-real-name to-newsgroup))))) ; Is this move internal?
9721         ;; Copy the article.
9722         ((eq action 'copy)
9723          (with-current-buffer copy-buf
9724            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9725              (save-restriction
9726                (nnheader-narrow-to-headers)
9727                (dolist (hdr gnus-copy-article-ignored-headers)
9728                  (message-remove-header hdr t)))
9729              (gnus-request-accept-article
9730               to-newsgroup select-method (not articles) t))))
9731         ;; Crosspost the article.
9732         ((eq action 'crosspost)
9733          (let ((xref (message-tokenize-header
9734                       (mail-header-xref (gnus-summary-article-header article))
9735                       " ")))
9736            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9737                                   ":" (number-to-string article)))
9738            (unless xref
9739              (setq xref (list (system-name))))
9740            (setq new-xref
9741                  (concat
9742                   (mapconcat 'identity
9743                              (delete "Xref:" (delete new-xref xref))
9744                              " ")
9745                   " " new-xref))
9746            (with-current-buffer copy-buf
9747              ;; First put the article in the destination group.
9748              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9749              (when (consp (setq art-group
9750                                 (gnus-request-accept-article
9751                                  to-newsgroup select-method (not articles) t)))
9752                (setq new-xref (concat new-xref " " (car art-group)
9753                                       ":"
9754                                       (number-to-string (cdr art-group))))
9755                ;; Now we have the new Xrefs header, so we insert
9756                ;; it and replace the new article.
9757                (nnheader-replace-header "Xref" new-xref)
9758                (gnus-request-replace-article
9759                 (cdr art-group) to-newsgroup (current-buffer) t)
9760                art-group))))))
9761       (cond
9762        ((not art-group)
9763         (gnus-message 1 "Couldn't %s article %s: %s"
9764                       (cadr (assq action names)) article
9765                       (nnheader-get-report (car to-method))))
9766        ((eq art-group 'junk)
9767         (when (eq action 'move)
9768           (gnus-summary-mark-article article gnus-canceled-mark)
9769           (gnus-message 4 "Deleted article %s" article)
9770           ;; run the delete hook
9771           (run-hook-with-args 'gnus-summary-article-delete-hook
9772                               action
9773                               (gnus-data-header
9774                                (assoc article (gnus-data-list nil)))
9775                               gnus-newsgroup-name nil
9776                               select-method)))
9777        (t
9778         (let* ((pto-group (gnus-group-prefixed-name
9779                            (car art-group) to-method))
9780                (info (gnus-get-info pto-group))
9781                (to-group (gnus-info-group info))
9782                to-marks)
9783           ;; Update the group that has been moved to.
9784           (when (and info
9785                      (memq action '(move copy)))
9786             (unless (member to-group to-groups)
9787               (push to-group to-groups))
9788
9789             (unless (memq article gnus-newsgroup-unreads)
9790               (push 'read to-marks)
9791               (gnus-info-set-read
9792                info (gnus-add-to-range (gnus-info-read info)
9793                                        (list (cdr art-group)))))
9794
9795             ;; See whether the article is to be put in the cache.
9796             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9797                    (marks (if expirable
9798                               gnus-article-mark-lists
9799                             (delete '(expirable . expire)
9800                                     (copy-sequence gnus-article-mark-lists))))
9801                    (to-article (cdr art-group)))
9802
9803               ;; Enter the article into the cache in the new group,
9804               ;; if that is required.
9805               (when gnus-use-cache
9806                 (gnus-cache-possibly-enter-article
9807                  to-group to-article
9808                  (memq article gnus-newsgroup-marked)
9809                  (memq article gnus-newsgroup-dormant)
9810                  (memq article gnus-newsgroup-unreads)))
9811
9812               (when gnus-preserve-marks
9813                 ;; Copy any marks over to the new group.
9814                 (when (and (equal to-group gnus-newsgroup-name)
9815                            (not (memq article gnus-newsgroup-unreads)))
9816                   ;; Mark this article as read in this group.
9817                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9818                   ;; Increase the active status of this group.
9819                   (setcdr (gnus-active to-group) to-article)
9820                   (setcdr gnus-newsgroup-active to-article))
9821
9822                 (while marks
9823                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9824                     (when (memq article (symbol-value
9825                                          (intern (format "gnus-newsgroup-%s"
9826                                                          (caar marks)))))
9827                       (push (cdar marks) to-marks)
9828                       ;; If the other group is the same as this group,
9829                       ;; then we have to add the mark to the list.
9830                       (when (equal to-group gnus-newsgroup-name)
9831                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9832                              (cons to-article
9833                                    (symbol-value
9834                                     (intern (format "gnus-newsgroup-%s"
9835                                                     (caar marks)))))))
9836                       ;; Copy the marks to other group.
9837                       (gnus-add-marked-articles
9838                        to-group (cdar marks) (list to-article) info)))
9839                   (setq marks (cdr marks)))
9840
9841                 (when (and expirable
9842                            gnus-mark-copied-or-moved-articles-as-expirable
9843                            (not (memq 'expire to-marks)))
9844                   ;; Mark this article as expirable.
9845                   (push 'expire to-marks)
9846                   (when (equal to-group gnus-newsgroup-name)
9847                     (push to-article gnus-newsgroup-expirable))
9848                   ;; Copy the expirable mark to other group.
9849                   (gnus-add-marked-articles
9850                    to-group 'expire (list to-article) info))
9851
9852                 (when to-marks
9853                   (gnus-request-set-mark
9854                    to-group (list (list (list to-article) 'add to-marks)))))
9855
9856               (gnus-dribble-enter
9857                (concat "(gnus-group-set-info '"
9858                        (gnus-prin1-to-string (gnus-get-info to-group))
9859                        ")"))))
9860
9861           ;; Update the Xref header in this article to point to
9862           ;; the new crossposted article we have just created.
9863           (when (eq action 'crosspost)
9864             (with-current-buffer copy-buf
9865               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9866               (nnheader-replace-header "Xref" new-xref)
9867               (gnus-request-replace-article
9868                article gnus-newsgroup-name (current-buffer) t)))
9869
9870           ;; run the move/copy/crosspost/respool hook
9871           (run-hook-with-args 'gnus-summary-article-move-hook
9872                               action
9873                               (gnus-data-header
9874                                (assoc article (gnus-data-list nil)))
9875                               gnus-newsgroup-name
9876                               to-newsgroup
9877                               select-method))
9878
9879         ;;;!!!Why is this necessary?
9880         (set-buffer gnus-summary-buffer)
9881
9882         (when (eq action 'move)
9883           (save-excursion
9884             (gnus-summary-goto-subject article)
9885             (gnus-summary-mark-article article gnus-canceled-mark)))))
9886       (push article articles-to-update-marks))
9887
9888     (save-excursion
9889       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9890     ;; Re-activate all groups that have been moved to.
9891     (with-current-buffer gnus-group-buffer
9892       (let ((gnus-group-marked to-groups))
9893         (gnus-group-get-new-news-this-group nil t)))
9894
9895     (gnus-kill-buffer copy-buf)
9896     (gnus-summary-position-point)
9897     (gnus-set-mode-line 'summary)))
9898
9899 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9900   "Copy the current article to some other group.
9901 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9902 When called interactively, if TO-NEWSGROUP is nil, use the value of
9903 the variable `gnus-move-split-methods' for finding a default target
9904 newsgroup.
9905 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9906 re-spool using this method."
9907   (interactive "P")
9908   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9909
9910 (defun gnus-summary-crosspost-article (&optional n)
9911   "Crosspost the current article to some other group."
9912   (interactive "P")
9913   (gnus-summary-move-article n nil nil 'crosspost))
9914
9915 (defcustom gnus-summary-respool-default-method nil
9916   "Default method type for respooling an article.
9917 If nil, use to the current newsgroup method."
9918   :type 'symbol
9919   :group 'gnus-summary-mail)
9920
9921 (defun gnus-summary-respool-article (&optional n method)
9922   "Respool the current article.
9923 The article will be squeezed through the mail spooling process again,
9924 which means that it will be put in some mail newsgroup or other
9925 depending on `nnmail-split-methods'.
9926 If N is a positive number, respool the N next articles.
9927 If N is a negative number, respool the N previous articles.
9928 If N is nil and any articles have been marked with the process mark,
9929 respool those articles instead.
9930
9931 Respooling can be done both from mail groups and \"real\" newsgroups.
9932 In the former case, the articles in question will be moved from the
9933 current group into whatever groups they are destined to.  In the
9934 latter case, they will be copied into the relevant groups."
9935   (interactive
9936    (list current-prefix-arg
9937          (let* ((methods (gnus-methods-using 'respool))
9938                 (methname
9939                  (symbol-name (or gnus-summary-respool-default-method
9940                                   (car (gnus-find-method-for-group
9941                                         gnus-newsgroup-name)))))
9942                 (method
9943                  (gnus-completing-read
9944                   "Backend to use when respooling"
9945                   methods t nil 'gnus-mail-method-history methname))
9946                 ms)
9947            (cond
9948             ((zerop (length (setq ms (gnus-servers-using-backend
9949                                       (intern method)))))
9950              (list (intern method) ""))
9951             ((= 1 (length ms))
9952              (car ms))
9953             (t
9954              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9955                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
9956                            ms-alist))))))))
9957   (unless method
9958     (error "No method given for respooling"))
9959   (if (assoc (symbol-name
9960               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9961              (gnus-methods-using 'respool))
9962       (gnus-summary-move-article n nil method)
9963     (gnus-summary-copy-article n nil method)))
9964
9965 (defun gnus-summary-import-article (file &optional edit)
9966   "Import an arbitrary file into a mail newsgroup."
9967   (interactive "fImport file: \nP")
9968   (let ((group gnus-newsgroup-name)
9969         (now (current-time))
9970         atts lines group-art)
9971     (unless (gnus-check-backend-function 'request-accept-article group)
9972       (error "%s does not support article importing" group))
9973     (or (file-readable-p file)
9974         (not (file-regular-p file))
9975         (error "Can't read %s" file))
9976     (with-current-buffer (gnus-get-buffer-create " *import file*")
9977       (erase-buffer)
9978       (nnheader-insert-file-contents file)
9979       (goto-char (point-min))
9980       (if (nnheader-article-p)
9981           (save-restriction
9982             (goto-char (point-min))
9983             (search-forward "\n\n" nil t)
9984             (narrow-to-region (point-min) (1- (point)))
9985             (goto-char (point-min))
9986             (unless (re-search-forward "^date:" nil t)
9987               (goto-char (point-max))
9988               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9989        ;; This doesn't look like an article, so we fudge some headers.
9990         (setq atts (file-attributes file)
9991               lines (count-lines (point-min) (point-max)))
9992         (insert "From: " (read-string "From: ") "\n"
9993                 "Subject: " (read-string "Subject: ") "\n"
9994                 "Date: " (message-make-date (nth 5 atts)) "\n"
9995                 "Message-ID: " (message-make-message-id) "\n"
9996                 "Lines: " (int-to-string lines) "\n"
9997                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9998       (setq group-art (gnus-request-accept-article group nil t))
9999       (kill-buffer (current-buffer)))
10000     (setq gnus-newsgroup-active (gnus-activate-group group))
10001     (forward-line 1)
10002     (gnus-summary-goto-article (cdr group-art) nil t)
10003     (when edit
10004       (gnus-summary-edit-article))))
10005
10006 (defun gnus-summary-create-article ()
10007   "Create an article in a mail newsgroup."
10008   (interactive)
10009   (let ((group gnus-newsgroup-name)
10010         (now (current-time))
10011         group-art)
10012     (unless (gnus-check-backend-function 'request-accept-article group)
10013       (error "%s does not support article importing" group))
10014     (with-current-buffer (gnus-get-buffer-create " *import file*")
10015       (erase-buffer)
10016       (goto-char (point-min))
10017       ;; This doesn't look like an article, so we fudge some headers.
10018       (insert "From: " (read-string "From: ") "\n"
10019               "Subject: " (read-string "Subject: ") "\n"
10020               "Date: " (message-make-date now) "\n"
10021               "Message-ID: " (message-make-message-id) "\n")
10022       (setq group-art (gnus-request-accept-article group nil t))
10023       (kill-buffer (current-buffer)))
10024     (setq gnus-newsgroup-active (gnus-activate-group group))
10025     (forward-line 1)
10026     (gnus-summary-goto-article (cdr group-art) nil t)
10027     (gnus-summary-edit-article)))
10028
10029 (defun gnus-summary-article-posted-p ()
10030   "Say whether the current (mail) article is available from news as well.
10031 This will be the case if the article has both been mailed and posted."
10032   (interactive)
10033   (let ((id (mail-header-references (gnus-summary-article-header)))
10034         (gnus-override-method (car (gnus-refer-article-methods))))
10035     (if (gnus-request-head id "")
10036         (gnus-message 2 "The current message was found on %s"
10037                       gnus-override-method)
10038       (gnus-message 2 "The current message couldn't be found on %s"
10039                     gnus-override-method)
10040       nil)))
10041
10042 (defun gnus-summary-expire-articles (&optional now)
10043   "Expire all articles that are marked as expirable in the current group."
10044   (interactive)
10045   (when (and (not gnus-group-is-exiting-without-update-p)
10046              (gnus-check-backend-function
10047               'request-expire-articles gnus-newsgroup-name))
10048     ;; This backend supports expiry.
10049     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10050            (expirable (if total
10051                           (progn
10052                             ;; We need to update the info for
10053                             ;; this group for `gnus-list-of-read-articles'
10054                             ;; to give us the right answer.
10055                             (gnus-run-hooks 'gnus-exit-group-hook)
10056                             (gnus-summary-update-info)
10057                             (gnus-list-of-read-articles gnus-newsgroup-name))
10058                         (setq gnus-newsgroup-expirable
10059                               (sort gnus-newsgroup-expirable '<))))
10060            (expiry-wait (if now 'immediate
10061                           (gnus-group-find-parameter
10062                            gnus-newsgroup-name 'expiry-wait)))
10063            (nnmail-expiry-target
10064             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10065                 nnmail-expiry-target))
10066            es)
10067       (when expirable
10068         ;; There are expirable articles in this group, so we run them
10069         ;; through the expiry process.
10070         (gnus-message 6 "Expiring articles...")
10071         (unless (gnus-check-group gnus-newsgroup-name)
10072           (error "Can't open server for %s" gnus-newsgroup-name))
10073         ;; The list of articles that weren't expired is returned.
10074         (save-excursion
10075           (if expiry-wait
10076               (let ((nnmail-expiry-wait-function nil)
10077                     (nnmail-expiry-wait expiry-wait))
10078                 (setq es (gnus-request-expire-articles
10079                           expirable gnus-newsgroup-name)))
10080             (setq es (gnus-request-expire-articles
10081                       expirable gnus-newsgroup-name)))
10082           (unless total
10083             (setq gnus-newsgroup-expirable es))
10084           ;; We go through the old list of expirable, and mark all
10085           ;; really expired articles as nonexistent.
10086           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10087             (let ((gnus-use-cache nil))
10088               (dolist (article expirable)
10089                 (when (and (not (memq article es))
10090                            (gnus-data-find article))
10091                   (gnus-summary-mark-article article gnus-canceled-mark)
10092                   (run-hook-with-args 'gnus-summary-article-expire-hook
10093                                       'delete
10094                                       (gnus-data-header
10095                                        (assoc article (gnus-data-list nil)))
10096                                       gnus-newsgroup-name
10097                                       nil
10098                                       nil))))))
10099         (gnus-message 6 "Expiring articles...done")))))
10100
10101 (defun gnus-summary-expire-articles-now ()
10102   "Expunge all expirable articles in the current group.
10103 This means that *all* articles that are marked as expirable will be
10104 deleted forever, right now."
10105   (interactive)
10106   (or gnus-expert-user
10107       (gnus-yes-or-no-p
10108        "Are you really, really sure you want to delete all expirable messages? ")
10109       (error "Phew!"))
10110   (gnus-summary-expire-articles t))
10111
10112 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10113 (defun gnus-summary-delete-article (&optional n)
10114   "Delete the N next (mail) articles.
10115 This command actually deletes articles.  This is not a marking
10116 command.  The article will disappear forever from your life, never to
10117 return.
10118
10119 If N is negative, delete backwards.
10120 If N is nil and articles have been marked with the process mark,
10121 delete these instead.
10122
10123 If `gnus-novice-user' is non-nil you will be asked for
10124 confirmation before the articles are deleted."
10125   (interactive "P")
10126   (unless (gnus-check-backend-function 'request-expire-articles
10127                                        gnus-newsgroup-name)
10128     (error "The current newsgroup does not support article deletion"))
10129   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10130     (error "Couldn't open server"))
10131   ;; Compute the list of articles to delete.
10132   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10133         (nnmail-expiry-target 'delete)
10134         not-deleted)
10135     (if (and gnus-novice-user
10136              (not (gnus-yes-or-no-p
10137                    (format "Do you really want to delete %s forever? "
10138                            (if (> (length articles) 1)
10139                                (format "these %s articles" (length articles))
10140                              "this article")))))
10141         ()
10142       ;; Delete the articles.
10143       (setq not-deleted (gnus-request-expire-articles
10144                          articles gnus-newsgroup-name 'force))
10145       (save-excursion
10146         (while articles
10147           (gnus-summary-remove-process-mark (car articles))
10148           ;; The backend might not have been able to delete the article
10149           ;; after all.
10150           (unless (memq (car articles) not-deleted)
10151             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10152           (let* ((article (car articles))
10153                  (ghead  (gnus-data-header
10154                           (assoc article (gnus-data-list nil)))))
10155             (run-hook-with-args 'gnus-summary-article-delete-hook
10156                                 'delete ghead gnus-newsgroup-name nil
10157                                 nil))
10158           (setq articles (cdr articles))))
10159       (when not-deleted
10160         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10161     (gnus-summary-position-point)
10162     (gnus-set-mode-line 'summary)
10163     not-deleted))
10164
10165 (defun gnus-summary-edit-article (&optional arg)
10166   "Edit the current article.
10167 This will have permanent effect only in mail groups.
10168 If ARG is nil, edit the decoded articles.
10169 If ARG is 1, edit the raw articles.
10170 If ARG is 2, edit the raw articles even in read-only groups.
10171 If ARG is 3, edit the articles with the current handles.
10172 Otherwise, allow editing of articles even in read-only
10173 groups."
10174   (interactive "P")
10175   (let (force raw current-handles)
10176     (cond
10177      ((null arg))
10178      ((eq arg 1)
10179       (setq raw t))
10180      ((eq arg 2)
10181       (setq raw t
10182             force t))
10183      ((eq arg 3)
10184       (setq current-handles
10185             (and (gnus-buffer-live-p gnus-article-buffer)
10186                  (with-current-buffer gnus-article-buffer
10187                    (prog1
10188                        gnus-article-mime-handles
10189                      (setq gnus-article-mime-handles nil))))))
10190      (t
10191       (setq force t)))
10192     (when (and raw (not force)
10193                (member gnus-newsgroup-name '("nndraft:delayed"
10194                                              "nndraft:drafts"
10195                                              "nndraft:queue")))
10196       (error "Can't edit the raw article in group %s"
10197              gnus-newsgroup-name))
10198     (with-current-buffer gnus-summary-buffer
10199       (let ((mail-parse-charset gnus-newsgroup-charset)
10200             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10201         (gnus-set-global-variables)
10202         (when (and (not force)
10203                    (gnus-group-read-only-p))
10204           (error "The current newsgroup does not support article editing"))
10205         (gnus-summary-show-article t)
10206         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10207           (with-current-buffer gnus-article-buffer
10208             (mm-enable-multibyte)))
10209         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10210             (setq raw t))
10211         (gnus-article-edit-article
10212          (if raw 'ignore
10213            `(lambda ()
10214               (let ((mbl mml-buffer-list))
10215                 (setq mml-buffer-list nil)
10216                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10217                   (mime-to-mml ,'current-handles))
10218                 (let ((mbl1 mml-buffer-list))
10219                   (setq mml-buffer-list mbl)
10220                   (set (make-local-variable 'mml-buffer-list) mbl1))
10221                 (gnus-make-local-hook 'kill-buffer-hook)
10222                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10223          `(lambda (no-highlight)
10224             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10225                   (message-options message-options)
10226                   (message-options-set-recipient)
10227                   (mail-parse-ignored-charsets
10228                    ',gnus-newsgroup-ignored-charsets)
10229                   (rfc2047-header-encoding-alist
10230                    ',(let ((charset (gnus-group-name-charset
10231                                      (gnus-find-method-for-group
10232                                       gnus-newsgroup-name)
10233                                      gnus-newsgroup-name)))
10234                        (append (list (cons "Newsgroups" charset)
10235                                      (cons "Followup-To" charset)
10236                                      (cons "Xref" charset))
10237                                rfc2047-header-encoding-alist))))
10238               ,(if (not raw) '(progn
10239                                 (mml-to-mime)
10240                                 (mml-destroy-buffers)
10241                                 (remove-hook 'kill-buffer-hook
10242                                              'mml-destroy-buffers t)
10243                                 (kill-local-variable 'mml-buffer-list)))
10244               (gnus-summary-edit-article-done
10245                ,(or (mail-header-references gnus-current-headers) "")
10246                ,(gnus-group-read-only-p)
10247                ,gnus-summary-buffer no-highlight))))))))
10248
10249 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10250
10251 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10252                                                  no-highlight)
10253   "Make edits to the current article permanent."
10254   (interactive)
10255   (save-excursion
10256    ;; The buffer restriction contains the entire article if it exists.
10257     (when (article-goto-body)
10258       (let ((lines (count-lines (point) (point-max)))
10259             (length (- (point-max) (point)))
10260             (case-fold-search t)
10261             (body (copy-marker (point))))
10262         (goto-char (point-min))
10263         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10264           (delete-region (match-beginning 1) (match-end 1))
10265           (insert (number-to-string length)))
10266         (goto-char (point-min))
10267         (when (re-search-forward
10268                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10269           (delete-region (match-beginning 1) (match-end 1))
10270           (insert (number-to-string length)))
10271         (goto-char (point-min))
10272         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10273           (delete-region (match-beginning 1) (match-end 1))
10274           (insert (number-to-string lines))))))
10275   ;; Replace the article.
10276   (let ((buf (current-buffer)))
10277     (with-temp-buffer
10278       (insert-buffer-substring buf)
10279
10280       (if (and (not read-only)
10281                (not (gnus-request-replace-article
10282                      (cdr gnus-article-current) (car gnus-article-current)
10283                      (current-buffer) t)))
10284           (error "Couldn't replace article")
10285         ;; Update the summary buffer.
10286         (if (and references
10287                  (equal (message-tokenize-header references " ")
10288                         (message-tokenize-header
10289                          (or (message-fetch-field "references") "") " ")))
10290             ;; We only have to update this line.
10291             (save-excursion
10292               (save-restriction
10293                 (message-narrow-to-head)
10294                 (let ((head (buffer-substring-no-properties
10295                              (point-min) (point-max)))
10296                       header)
10297                   (with-temp-buffer
10298                     (insert (format "211 %d Article retrieved.\n"
10299                                     (cdr gnus-article-current)))
10300                     (insert head)
10301                     (insert ".\n")
10302                     (let ((nntp-server-buffer (current-buffer)))
10303                       (setq header (car (gnus-get-newsgroup-headers
10304                                          nil t))))
10305                     (with-current-buffer gnus-summary-buffer
10306                       (gnus-data-set-header
10307                        (gnus-data-find (cdr gnus-article-current))
10308                        header)
10309                       (gnus-summary-update-article-line
10310                        (cdr gnus-article-current) header)
10311                       (if (gnus-summary-goto-subject
10312                            (cdr gnus-article-current) nil t)
10313                           (gnus-summary-update-secondary-mark
10314                            (cdr gnus-article-current))))))))
10315           ;; Update threads.
10316           (set-buffer (or buffer gnus-summary-buffer))
10317           (gnus-summary-update-article (cdr gnus-article-current))
10318           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10319               (gnus-summary-update-secondary-mark
10320                (cdr gnus-article-current))))
10321         ;; Prettify the article buffer again.
10322         (unless no-highlight
10323           (with-current-buffer gnus-article-buffer
10324             ;;;!!! Fix this -- article should be rehighlighted.
10325             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10326             (set-buffer gnus-original-article-buffer)
10327             (gnus-request-article
10328              (cdr gnus-article-current)
10329              (car gnus-article-current) (current-buffer))))
10330         ;; Prettify the summary buffer line.
10331         (when (gnus-visual-p 'summary-highlight 'highlight)
10332           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10333
10334 (defun gnus-summary-edit-wash (key)
10335   "Perform editing command KEY in the article buffer."
10336   (interactive
10337    (list
10338     (progn
10339       (message "%s" (concat (this-command-keys) "- "))
10340       (read-char))))
10341   (message "")
10342   (gnus-summary-edit-article)
10343   (execute-kbd-macro (concat (this-command-keys) key))
10344   (gnus-article-edit-done))
10345
10346 ;;; Respooling
10347
10348 (defun gnus-summary-respool-query (&optional silent trace)
10349   "Query where the respool algorithm would put this article."
10350   (interactive)
10351   (let (gnus-mark-article-hook)
10352     (gnus-summary-select-article)
10353     (with-current-buffer gnus-original-article-buffer
10354       (let ((groups (nnmail-article-group 'identity trace)))
10355         (unless silent
10356           (if groups
10357               (message "This message would go to %s"
10358                        (mapconcat 'car groups ", "))
10359             (message "This message would go to no groups"))
10360           groups)))))
10361
10362 (defun gnus-summary-respool-trace ()
10363   "Trace where the respool algorithm would put this article.
10364 Display a buffer showing all fancy splitting patterns which matched."
10365   (interactive)
10366   (gnus-summary-respool-query nil t))
10367
10368 ;; Summary marking commands.
10369
10370 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10371   "Mark articles which has the same subject as read, and then select the next.
10372 If UNMARK is positive, remove any kind of mark.
10373 If UNMARK is negative, tick articles."
10374   (interactive "P")
10375   (when unmark
10376     (setq unmark (prefix-numeric-value unmark)))
10377   (let ((count
10378          (gnus-summary-mark-same-subject
10379           (gnus-summary-article-subject) unmark)))
10380     ;; Select next unread article.  If auto-select-same mode, should
10381     ;; select the first unread article.
10382     (gnus-summary-next-article t (and gnus-auto-select-same
10383                                       (gnus-summary-article-subject)))
10384     (gnus-message 7 "%d article%s marked as %s"
10385                   count (if (= count 1) " is" "s are")
10386                   (if unmark "unread" "read"))))
10387
10388 (defun gnus-summary-kill-same-subject (&optional unmark)
10389   "Mark articles which has the same subject as read.
10390 If UNMARK is positive, remove any kind of mark.
10391 If UNMARK is negative, tick articles."
10392   (interactive "P")
10393   (when unmark
10394     (setq unmark (prefix-numeric-value unmark)))
10395   (let ((count
10396          (gnus-summary-mark-same-subject
10397           (gnus-summary-article-subject) unmark)))
10398     ;; If marked as read, go to next unread subject.
10399     (when (null unmark)
10400       ;; Go to next unread subject.
10401       (gnus-summary-next-subject 1 t))
10402     (gnus-message 7 "%d articles are marked as %s"
10403                   count (if unmark "unread" "read"))))
10404
10405 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10406   "Mark articles with same SUBJECT as read, and return marked number.
10407 If optional argument UNMARK is positive, remove any kinds of marks.
10408 If optional argument UNMARK is negative, mark articles as unread instead."
10409   (let ((count 1))
10410     (save-excursion
10411       (cond
10412        ((null unmark)                   ; Mark as read.
10413         (while (and
10414                 (progn
10415                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10416                   (gnus-summary-show-thread) t)
10417                 (gnus-summary-find-subject subject))
10418           (setq count (1+ count))))
10419        ((> unmark 0)                    ; Tick.
10420         (while (and
10421                 (progn
10422                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10423                   (gnus-summary-show-thread) t)
10424                 (gnus-summary-find-subject subject))
10425           (setq count (1+ count))))
10426        (t                               ; Mark as unread.
10427         (while (and
10428                 (progn
10429                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10430                   (gnus-summary-show-thread) t)
10431                 (gnus-summary-find-subject subject))
10432           (setq count (1+ count)))))
10433       (gnus-set-mode-line 'summary)
10434       ;; Return the number of marked articles.
10435       count)))
10436
10437 (defun gnus-summary-mark-as-processable (n &optional unmark)
10438   "Set the process mark on the next N articles.
10439 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10440 the process mark instead.  The difference between N and the actual
10441 number of articles marked is returned."
10442   (interactive "P")
10443   (if (and (null n) (gnus-region-active-p))
10444       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10445     (setq n (prefix-numeric-value n))
10446     (let ((backward (< n 0))
10447           (n (abs n)))
10448       (while (and
10449               (> n 0)
10450               (if unmark
10451                   (gnus-summary-remove-process-mark
10452                    (gnus-summary-article-number))
10453                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10454               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10455         (setq n (1- n)))
10456       (when (/= 0 n)
10457         (gnus-message 7 "No more articles"))
10458       (gnus-summary-recenter)
10459       (gnus-summary-position-point)
10460       n)))
10461
10462 (defun gnus-summary-unmark-as-processable (n)
10463   "Remove the process mark from the next N articles.
10464 If N is negative, unmark backward instead.  The difference between N and
10465 the actual number of articles unmarked is returned."
10466   (interactive "P")
10467   (gnus-summary-mark-as-processable n t))
10468
10469 (defun gnus-summary-unmark-all-processable ()
10470   "Remove the process mark from all articles."
10471   (interactive)
10472   (save-excursion
10473     (while gnus-newsgroup-processable
10474       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10475   (gnus-summary-position-point))
10476
10477 (defun gnus-summary-add-mark (article type)
10478   "Mark ARTICLE with a mark of TYPE."
10479   (let ((vtype (car (assq type gnus-article-mark-lists)))
10480         var)
10481     (if (not vtype)
10482         (error "No such mark type: %s" type)
10483       (setq var (intern (format "gnus-newsgroup-%s" type)))
10484       (set var (cons article (symbol-value var)))
10485       (if (memq type '(processable cached replied forwarded recent saved))
10486           (gnus-summary-update-secondary-mark article)
10487         ;;; !!! This is bogus.  We should find out what primary
10488         ;;; !!! mark we want to set.
10489         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10490
10491 (defun gnus-summary-mark-as-expirable (n)
10492   "Mark N articles forward as expirable.
10493 If N is negative, mark backward instead.  The difference between N and
10494 the actual number of articles marked is returned."
10495   (interactive "p")
10496   (gnus-summary-mark-forward n gnus-expirable-mark))
10497
10498 (defun gnus-summary-mark-as-spam (n)
10499   "Mark N articles forward as spam.
10500 If N is negative, mark backward instead.  The difference between N and
10501 the actual number of articles marked is returned."
10502   (interactive "p")
10503   (gnus-summary-mark-forward n gnus-spam-mark))
10504
10505 (defun gnus-summary-mark-article-as-replied (article)
10506   "Mark ARTICLE as replied to and update the summary line.
10507 ARTICLE can also be a list of articles."
10508   (interactive (list (gnus-summary-article-number)))
10509   (let ((articles (if (listp article) article (list article))))
10510     (dolist (article articles)
10511       (unless (numberp article)
10512         (error "%s is not a number" article))
10513       (push article gnus-newsgroup-replied)
10514       (let ((inhibit-read-only t))
10515         (when (gnus-summary-goto-subject article nil t)
10516           (gnus-summary-update-secondary-mark article))))))
10517
10518 (defun gnus-summary-mark-article-as-forwarded (article)
10519   "Mark ARTICLE as forwarded and update the summary line.
10520 ARTICLE can also be a list of articles."
10521   (let ((articles (if (listp article) article (list article))))
10522     (dolist (article articles)
10523       (push article gnus-newsgroup-forwarded)
10524       (let ((inhibit-read-only t))
10525         (when (gnus-summary-goto-subject article nil t)
10526           (gnus-summary-update-secondary-mark article))))))
10527
10528 (defun gnus-summary-set-bookmark (article)
10529   "Set a bookmark in current article."
10530   (interactive (list (gnus-summary-article-number)))
10531   (when (or (not (get-buffer gnus-article-buffer))
10532             (not gnus-current-article)
10533             (not gnus-article-current)
10534             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10535     (error "No current article selected"))
10536   ;; Remove old bookmark, if one exists.
10537   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10538   ;; Set the new bookmark, which is on the form
10539   ;; (article-number . line-number-in-body).
10540   (push
10541    (cons article
10542          (with-current-buffer gnus-article-buffer
10543            (count-lines
10544             (min (point)
10545                  (save-excursion
10546                    (article-goto-body)
10547                    (point)))
10548             (point))))
10549    gnus-newsgroup-bookmarks)
10550   (gnus-message 6 "A bookmark has been added to the current article."))
10551
10552 (defun gnus-summary-remove-bookmark (article)
10553   "Remove the bookmark from the current article."
10554   (interactive (list (gnus-summary-article-number)))
10555   ;; Remove old bookmark, if one exists.
10556   (if (not (assq article gnus-newsgroup-bookmarks))
10557       (gnus-message 6 "No bookmark in current article.")
10558     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10559     (gnus-message 6 "Removed bookmark.")))
10560
10561 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10562 (defun gnus-summary-mark-as-dormant (n)
10563   "Mark N articles forward as dormant.
10564 If N is negative, mark backward instead.  The difference between N and
10565 the actual number of articles marked is returned."
10566   (interactive "p")
10567   (gnus-summary-mark-forward n gnus-dormant-mark))
10568
10569 (defun gnus-summary-set-process-mark (article)
10570   "Set the process mark on ARTICLE and update the summary line."
10571   (setq gnus-newsgroup-processable
10572         (cons article
10573               (delq article gnus-newsgroup-processable)))
10574   (when (gnus-summary-goto-subject article)
10575     (gnus-summary-show-thread)
10576     (gnus-summary-goto-subject article)
10577     (gnus-summary-update-secondary-mark article)))
10578
10579 (defun gnus-summary-remove-process-mark (&rest articles)
10580   "Remove the process mark from ARTICLES and update the summary line."
10581   (dolist (article articles)
10582     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10583     (when (gnus-summary-goto-subject article)
10584       (gnus-summary-show-thread)
10585       (gnus-summary-goto-subject article)
10586       (gnus-summary-update-secondary-mark article)))
10587   t)
10588
10589 (defun gnus-summary-set-saved-mark (article)
10590   "Set the process mark on ARTICLE and update the summary line."
10591   (push article gnus-newsgroup-saved)
10592   (when (gnus-summary-goto-subject article)
10593     (gnus-summary-update-secondary-mark article)))
10594
10595 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10596   "Mark N articles as read forwards.
10597 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10598 The difference between N and the actual number of articles marked is
10599 returned.
10600 If NO-EXPIRE, auto-expiry will be inhibited."
10601   (interactive "p")
10602   (gnus-summary-show-thread)
10603   (let ((backward (< n 0))
10604         (gnus-summary-goto-unread
10605          (and gnus-summary-goto-unread
10606               (not (eq gnus-summary-goto-unread 'never))
10607               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10608                                     gnus-ticked-mark gnus-dormant-mark)))))
10609         (n (abs n))
10610         (mark (or mark gnus-del-mark)))
10611     (while (and (> n 0)
10612                 (gnus-summary-mark-article nil mark no-expire)
10613                 (zerop (gnus-summary-next-subject
10614                         (if backward -1 1)
10615                         (and gnus-summary-goto-unread
10616                              (not (eq gnus-summary-goto-unread 'never)))
10617                         t)))
10618       (setq n (1- n)))
10619     (when (/= 0 n)
10620       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10621     (gnus-summary-recenter)
10622     (gnus-summary-position-point)
10623     (gnus-set-mode-line 'summary)
10624     n))
10625
10626 (defun gnus-summary-mark-article-as-read (mark)
10627   "Mark the current article quickly as read with MARK."
10628   (let ((article (gnus-summary-article-number)))
10629     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10630     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10631     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10632     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10633     (push (cons article mark) gnus-newsgroup-reads)
10634     ;; Possibly remove from cache, if that is used.
10635     (when gnus-use-cache
10636       (gnus-cache-enter-remove-article article))
10637     ;; Allow the backend to change the mark.
10638     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10639     ;; Check for auto-expiry.
10640     (when (and gnus-newsgroup-auto-expire
10641                (memq mark gnus-auto-expirable-marks))
10642       (setq mark gnus-expirable-mark)
10643       ;; Let the backend know about the mark change.
10644       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10645       (push article gnus-newsgroup-expirable))
10646     ;; Set the mark in the buffer.
10647     (gnus-summary-update-mark mark 'unread)
10648     t))
10649
10650 (defun gnus-summary-mark-article-as-unread (mark)
10651   "Mark the current article quickly as unread with MARK."
10652   (let* ((article (gnus-summary-article-number))
10653          (old-mark (gnus-summary-article-mark article)))
10654     ;; Allow the backend to change the mark.
10655     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10656     (if (eq mark old-mark)
10657         t
10658       (if (<= article 0)
10659           (progn
10660             (gnus-error 1 "Can't mark negative article numbers")
10661             nil)
10662         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10663         (setq gnus-newsgroup-spam-marked
10664               (delq article gnus-newsgroup-spam-marked))
10665         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10666         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10667         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10668         (cond ((= mark gnus-ticked-mark)
10669                (setq gnus-newsgroup-marked
10670                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10671                                               article)))
10672               ((= mark gnus-spam-mark)
10673                (setq gnus-newsgroup-spam-marked
10674                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10675                                               article)))
10676               ((= mark gnus-dormant-mark)
10677                (setq gnus-newsgroup-dormant
10678                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10679                                               article)))
10680               (t
10681                (setq gnus-newsgroup-unreads
10682                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10683                                               article))))
10684         (gnus-alist-pull article gnus-newsgroup-reads)
10685
10686         ;; See whether the article is to be put in the cache.
10687         (and gnus-use-cache
10688              (vectorp (gnus-summary-article-header article))
10689              (save-excursion
10690                (gnus-cache-possibly-enter-article
10691                 gnus-newsgroup-name article
10692                 (= mark gnus-ticked-mark)
10693                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10694
10695         ;; Fix the mark.
10696         (gnus-summary-update-mark mark 'unread)
10697         t))))
10698
10699 (defun gnus-summary-mark-article (&optional article mark no-expire)
10700   "Mark ARTICLE with MARK.  MARK can be any character.
10701 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10702 `??' (dormant) and `?E' (expirable).
10703 If MARK is nil, then the default character `?r' is used.
10704 If ARTICLE is nil, then the article on the current line will be
10705 marked.
10706 If NO-EXPIRE, auto-expiry will be inhibited."
10707   ;; The mark might be a string.
10708   (when (stringp mark)
10709     (setq mark (aref mark 0)))
10710   ;; If no mark is given, then we check auto-expiring.
10711   (when (null mark)
10712     (setq mark gnus-del-mark))
10713   (when (and (not no-expire)
10714              gnus-newsgroup-auto-expire
10715              (memq mark gnus-auto-expirable-marks))
10716     (setq mark gnus-expirable-mark))
10717   (let ((article (or article (gnus-summary-article-number)))
10718         (old-mark (gnus-summary-article-mark article)))
10719     ;; Allow the backend to change the mark.
10720     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10721     (if (eq mark old-mark)
10722         t
10723       (unless article
10724         (error "No article on current line"))
10725       (if (not (if (or (= mark gnus-unread-mark)
10726                        (= mark gnus-ticked-mark)
10727                        (= mark gnus-spam-mark)
10728                        (= mark gnus-dormant-mark))
10729                    (gnus-mark-article-as-unread article mark)
10730                  (gnus-mark-article-as-read article mark)))
10731           t
10732         ;; See whether the article is to be put in the cache.
10733         (and gnus-use-cache
10734              (not (= mark gnus-canceled-mark))
10735              (vectorp (gnus-summary-article-header article))
10736              (save-excursion
10737                (gnus-cache-possibly-enter-article
10738                 gnus-newsgroup-name article
10739                 (= mark gnus-ticked-mark)
10740                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10741
10742         (when (gnus-summary-goto-subject article nil t)
10743           (let ((inhibit-read-only t))
10744             (gnus-summary-show-thread)
10745             ;; Fix the mark.
10746             (gnus-summary-update-mark mark 'unread)
10747             t))))))
10748
10749 (defun gnus-summary-update-secondary-mark (article)
10750   "Update the secondary (read, process, cache) mark."
10751   (gnus-summary-update-mark
10752    (cond ((memq article gnus-newsgroup-processable)
10753           gnus-process-mark)
10754          ((memq article gnus-newsgroup-cached)
10755           gnus-cached-mark)
10756          ((memq article gnus-newsgroup-replied)
10757           gnus-replied-mark)
10758          ((memq article gnus-newsgroup-forwarded)
10759           gnus-forwarded-mark)
10760          ((memq article gnus-newsgroup-saved)
10761           gnus-saved-mark)
10762          ((memq article gnus-newsgroup-recent)
10763           gnus-recent-mark)
10764          ((memq article gnus-newsgroup-unseen)
10765           gnus-unseen-mark)
10766          (t gnus-no-mark))
10767    'replied)
10768   (when (gnus-visual-p 'summary-highlight 'highlight)
10769     (gnus-summary-highlight-line)
10770     (gnus-run-hooks 'gnus-summary-update-hook))
10771   t)
10772
10773 (defun gnus-summary-update-download-mark (article)
10774   "Update the download mark."
10775   (gnus-summary-update-mark
10776    (cond ((memq article gnus-newsgroup-undownloaded)
10777           gnus-undownloaded-mark)
10778          (gnus-newsgroup-agentized
10779           gnus-downloaded-mark)
10780          (t
10781           gnus-no-mark))
10782    'download)
10783   (gnus-summary-update-line t)
10784   t)
10785
10786 (defun gnus-summary-update-mark (mark type)
10787   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10788         (inhibit-read-only t))
10789     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10790     (when forward
10791       (when (looking-at "\r")
10792         (incf forward))
10793       (when (<= (+ forward (point)) (point-max))
10794         ;; Go to the right position on the line.
10795         (goto-char (+ forward (point)))
10796         ;; Replace the old mark with the new mark.
10797         (let ((to-insert
10798                (mm-subst-char-in-string
10799                 (char-after) mark
10800                 (buffer-substring (point) (1+ (point))))))
10801           (delete-region (point) (1+ (point)))
10802           (insert to-insert))
10803         ;; Optionally update the marks by some user rule.
10804         (when (eq type 'unread)
10805           (gnus-data-set-mark
10806            (gnus-data-find (gnus-summary-article-number)) mark)
10807           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10808
10809 (defun gnus-mark-article-as-read (article &optional mark)
10810   "Enter ARTICLE in the pertinent lists and remove it from others."
10811   ;; Make the article expirable.
10812   (let ((mark (or mark gnus-del-mark)))
10813     (setq gnus-newsgroup-expirable
10814           (if (= mark gnus-expirable-mark)
10815               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10816             (delq article gnus-newsgroup-expirable)))
10817     ;; Remove from unread and marked lists.
10818     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10819     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10820     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10821     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10822     (push (cons article mark) gnus-newsgroup-reads)
10823     ;; Possibly remove from cache, if that is used.
10824     (when gnus-use-cache
10825       (gnus-cache-enter-remove-article article))
10826     t))
10827
10828 (defun gnus-mark-article-as-unread (article &optional mark)
10829   "Enter ARTICLE in the pertinent lists and remove it from others."
10830   (let ((mark (or mark gnus-ticked-mark)))
10831     (if (<= article 0)
10832         (progn
10833           (gnus-error 1 "Can't mark negative article numbers")
10834           nil)
10835       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10836             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10837             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10838             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10839             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10840
10841       ;; Unsuppress duplicates?
10842       (when gnus-suppress-duplicates
10843         (gnus-dup-unsuppress-article article))
10844
10845       (cond ((= mark gnus-ticked-mark)
10846              (setq gnus-newsgroup-marked
10847                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10848             ((= mark gnus-spam-mark)
10849              (setq gnus-newsgroup-spam-marked
10850                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10851                                             article)))
10852             ((= mark gnus-dormant-mark)
10853              (setq gnus-newsgroup-dormant
10854                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10855             (t
10856              (setq gnus-newsgroup-unreads
10857                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10858       (gnus-alist-pull article gnus-newsgroup-reads)
10859       t)))
10860
10861 (defun gnus-summary-tick-article-forward (n)
10862   "Tick N articles forwards.
10863 If N is negative, tick backwards instead.
10864 The difference between N and the number of articles ticked is returned."
10865   (interactive "p")
10866   (gnus-summary-mark-forward n gnus-ticked-mark))
10867
10868 (defun gnus-summary-tick-article-backward (n)
10869   "Tick N articles backwards.
10870 The difference between N and the number of articles ticked is returned."
10871   (interactive "p")
10872   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10873
10874 (defun gnus-summary-tick-article (&optional article clear-mark)
10875   "Mark current article as unread.
10876 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10877 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10878   (interactive)
10879   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10880                                        gnus-ticked-mark)))
10881
10882 (defun gnus-summary-mark-as-read-forward (n)
10883   "Mark N articles as read forwards.
10884 If N is negative, mark backwards instead.
10885 The difference between N and the actual number of articles marked is
10886 returned."
10887   (interactive "p")
10888   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10889
10890 (defun gnus-summary-mark-as-read-backward (n)
10891   "Mark the N articles as read backwards.
10892 The difference between N and the actual number of articles marked is
10893 returned."
10894   (interactive "p")
10895   (gnus-summary-mark-forward
10896    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10897
10898 (defun gnus-summary-mark-as-read (&optional article mark)
10899   "Mark current article as read.
10900 ARTICLE specifies the article to be marked as read.
10901 MARK specifies a string to be inserted at the beginning of the line."
10902   (gnus-summary-mark-article article mark))
10903
10904 (defun gnus-summary-clear-mark-forward (n)
10905   "Clear marks from N articles forward.
10906 If N is negative, clear backward instead.
10907 The difference between N and the number of marks cleared is returned."
10908   (interactive "p")
10909   (gnus-summary-mark-forward n gnus-unread-mark))
10910
10911 (defun gnus-summary-clear-mark-backward (n)
10912   "Clear marks from N articles backward.
10913 The difference between N and the number of marks cleared is returned."
10914   (interactive "p")
10915   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10916
10917 (defun gnus-summary-mark-unread-as-read ()
10918   "Intended to be used by `gnus-mark-article-hook'."
10919   (when (memq gnus-current-article gnus-newsgroup-unreads)
10920     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10921
10922 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10923   "Intended to be used by `gnus-mark-article-hook'."
10924   (let ((mark (gnus-summary-article-mark)))
10925     (when (or (gnus-unread-mark-p mark)
10926               (gnus-read-mark-p mark))
10927       (gnus-summary-mark-article gnus-current-article
10928                                  (or new-mark gnus-read-mark)))))
10929
10930 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10931   "Intended to be used by `gnus-mark-article-hook'."
10932   (let ((mark (gnus-summary-article-mark)))
10933     (when (or (gnus-unread-mark-p mark)
10934               (gnus-read-mark-p mark))
10935       (gnus-summary-mark-article (gnus-summary-article-number)
10936                                  (or new-mark gnus-read-mark)))))
10937
10938 (defun gnus-summary-mark-unread-as-ticked ()
10939   "Intended to be used by `gnus-mark-article-hook'."
10940   (when (memq gnus-current-article gnus-newsgroup-unreads)
10941     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10942
10943 (defun gnus-summary-mark-region-as-read (point mark all)
10944   "Mark all unread articles between point and mark as read.
10945 If given a prefix, mark all articles between point and mark as read,
10946 even ticked and dormant ones."
10947   (interactive "r\nP")
10948   (save-excursion
10949     (let (article)
10950       (goto-char point)
10951       (beginning-of-line)
10952       (while (and
10953               (< (point) mark)
10954               (progn
10955                 (when (or all
10956                           (memq (setq article (gnus-summary-article-number))
10957                                 gnus-newsgroup-unreads))
10958                   (gnus-summary-mark-article article gnus-del-mark))
10959                 t)
10960               (gnus-summary-find-next))))))
10961
10962 (defun gnus-summary-mark-below (score mark)
10963   "Mark articles with score less than SCORE with MARK."
10964   (interactive "P\ncMark: ")
10965   (setq score (if score
10966                   (prefix-numeric-value score)
10967                 (or gnus-summary-default-score 0)))
10968   (with-current-buffer gnus-summary-buffer
10969     (goto-char (point-min))
10970     (while
10971         (progn
10972           (and (< (gnus-summary-article-score) score)
10973                (gnus-summary-mark-article nil mark))
10974           (gnus-summary-find-next)))))
10975
10976 (defun gnus-summary-kill-below (&optional score)
10977   "Mark articles with score below SCORE as read."
10978   (interactive "P")
10979   (gnus-summary-mark-below score gnus-killed-mark))
10980
10981 (defun gnus-summary-clear-above (&optional score)
10982   "Clear all marks from articles with score above SCORE."
10983   (interactive "P")
10984   (gnus-summary-mark-above score gnus-unread-mark))
10985
10986 (defun gnus-summary-tick-above (&optional score)
10987   "Tick all articles with score above SCORE."
10988   (interactive "P")
10989   (gnus-summary-mark-above score gnus-ticked-mark))
10990
10991 (defun gnus-summary-mark-above (score mark)
10992   "Mark articles with score over SCORE with MARK."
10993   (interactive "P\ncMark: ")
10994   (setq score (if score
10995                   (prefix-numeric-value score)
10996                 (or gnus-summary-default-score 0)))
10997   (with-current-buffer gnus-summary-buffer
10998     (goto-char (point-min))
10999     (while (and (progn
11000                   (when (> (gnus-summary-article-score) score)
11001                     (gnus-summary-mark-article nil mark))
11002                   t)
11003                 (gnus-summary-find-next)))))
11004
11005 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11006 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11007 (defun gnus-summary-limit-include-expunged (&optional no-error)
11008   "Display all the hidden articles that were expunged for low scores."
11009   (interactive)
11010   (let ((inhibit-read-only t))
11011     (let ((scored gnus-newsgroup-scored)
11012           headers h)
11013       (while scored
11014         (unless (gnus-summary-article-header (caar scored))
11015           (and (setq h (gnus-number-to-header (caar scored)))
11016                (< (cdar scored) gnus-summary-expunge-below)
11017                (push h headers)))
11018         (setq scored (cdr scored)))
11019       (if (not headers)
11020           (when (not no-error)
11021             (error "No expunged articles hidden"))
11022         (goto-char (point-min))
11023         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11024         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11025         (dolist (x headers)
11026           (push (mail-header-number x) gnus-newsgroup-limit))
11027         (gnus-summary-prepare-unthreaded (nreverse headers))
11028         (goto-char (point-min))
11029         (gnus-summary-position-point)
11030         t))))
11031
11032 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11033   "Mark all unread articles in this newsgroup as read.
11034 If prefix argument ALL is non-nil, ticked and dormant articles will
11035 also be marked as read.
11036 If QUIETLY is non-nil, no questions will be asked.
11037
11038 If TO-HERE is non-nil, it should be a point in the buffer.  All
11039 articles before (after, if REVERSE is set) this point will be marked
11040 as read.
11041
11042 Note that this function will only catch up the unread article
11043 in the current summary buffer limitation.
11044
11045 The number of articles marked as read is returned."
11046   (interactive "P")
11047   (prog1
11048       (save-excursion
11049         (when (or quietly
11050                   (not gnus-interactive-catchup) ;Without confirmation?
11051                   gnus-expert-user
11052                   (gnus-y-or-n-p
11053                    (if all
11054                        "Mark absolutely all articles as read? "
11055                      "Mark all unread articles as read? ")))
11056           (if (and not-mark
11057                    (not gnus-newsgroup-adaptive)
11058                    (not gnus-newsgroup-auto-expire)
11059                    (not gnus-suppress-duplicates)
11060                    (or (not gnus-use-cache)
11061                        (eq gnus-use-cache 'passive)))
11062               (progn
11063                 (when all
11064                   (setq gnus-newsgroup-marked nil
11065                         gnus-newsgroup-spam-marked nil
11066                         gnus-newsgroup-dormant nil))
11067                 (setq gnus-newsgroup-unreads
11068                       (gnus-sorted-nunion
11069                        (gnus-sorted-intersection gnus-newsgroup-unreads
11070                                                  gnus-newsgroup-downloadable)
11071                        (gnus-sorted-difference gnus-newsgroup-unfetched
11072                                                gnus-newsgroup-cached))))
11073             ;; We actually mark all articles as canceled, which we
11074             ;; have to do when using auto-expiry or adaptive scoring.
11075             (gnus-summary-show-all-threads)
11076             (if (and to-here reverse)
11077                 (progn
11078                   (goto-char to-here)
11079                   (gnus-summary-mark-current-read-and-unread-as-read
11080                    gnus-catchup-mark)
11081                   (while (gnus-summary-find-next (not all))
11082                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11083               (when (gnus-summary-first-subject (not all))
11084                 (while (and
11085                         (if to-here (< (point) to-here) t)
11086                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11087                         (gnus-summary-find-next (not all))))))
11088             (gnus-set-mode-line 'summary))
11089           t))
11090     (gnus-summary-position-point)))
11091
11092 (defun gnus-summary-catchup-to-here (&optional all)
11093   "Mark all unticked articles before the current one as read.
11094 If ALL is non-nil, also mark ticked and dormant articles as read."
11095   (interactive "P")
11096   (save-excursion
11097     (gnus-save-hidden-threads
11098       (let ((beg (point)))
11099         ;; We check that there are unread articles.
11100         (when (or all (gnus-summary-find-prev))
11101           (gnus-summary-catchup all t beg)))))
11102   (gnus-summary-position-point))
11103
11104 (defun gnus-summary-catchup-from-here (&optional all)
11105   "Mark all unticked articles after (and including) the current one as read.
11106 If ALL is non-nil, also mark ticked and dormant articles as read."
11107   (interactive "P")
11108   (save-excursion
11109     (gnus-save-hidden-threads
11110       (let ((beg (point)))
11111         ;; We check that there are unread articles.
11112         (when (or all (gnus-summary-find-next))
11113           (gnus-summary-catchup all t beg nil t)))))
11114   (gnus-summary-position-point))
11115
11116 (defun gnus-summary-catchup-all (&optional quietly)
11117   "Mark all articles in this newsgroup as read.
11118 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11119 instead, which marks only unread articles as read."
11120   (interactive "P")
11121   (gnus-summary-catchup t quietly))
11122
11123 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11124   "Mark all unread articles in this group as read, then exit.
11125 If prefix argument ALL is non-nil, all articles are marked as read.
11126 If QUIETLY is non-nil, no questions will be asked."
11127   (interactive "P")
11128   (when (gnus-summary-catchup all quietly nil 'fast)
11129     ;; Select next newsgroup or exit.
11130     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11131              (eq gnus-auto-select-next 'quietly))
11132         (gnus-summary-next-group nil)
11133       (gnus-summary-exit))))
11134
11135 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11136   "Mark all articles in this newsgroup as read, and then exit.
11137 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11138 instead, which marks only unread articles as read."
11139   (interactive "P")
11140   (gnus-summary-catchup-and-exit t quietly))
11141
11142 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11143   "Mark all articles in this group as read and select the next group.
11144 If given a prefix, mark all articles, unread as well as ticked, as
11145 read."
11146   (interactive "P")
11147   (save-excursion
11148     (gnus-summary-catchup all))
11149   (gnus-summary-next-group))
11150
11151 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11152   "Mark all articles in this group as read and select the previous group.
11153 If given a prefix, mark all articles, unread as well as ticked, as
11154 read."
11155   (interactive "P")
11156   (save-excursion
11157     (gnus-summary-catchup all))
11158   (gnus-summary-next-group nil nil t))
11159
11160 ;;;
11161 ;;; with article
11162 ;;;
11163
11164 (defmacro gnus-with-article (article &rest forms)
11165   "Select ARTICLE and perform FORMS in the original article buffer.
11166 Then replace the article with the result."
11167   `(progn
11168      ;; We don't want the article to be marked as read.
11169      (let (gnus-mark-article-hook)
11170        (gnus-summary-select-article t t nil ,article))
11171      (set-buffer gnus-original-article-buffer)
11172      ,@forms
11173      (if (not (gnus-check-backend-function
11174                'request-replace-article (car gnus-article-current)))
11175          (gnus-message 5 "Read-only group; not replacing")
11176        (unless (gnus-request-replace-article
11177                 ,article (car gnus-article-current)
11178                 (current-buffer) t)
11179          (error "Couldn't replace article")))
11180      ;; The cache and backlog have to be flushed somewhat.
11181      (when gnus-keep-backlog
11182        (gnus-backlog-remove-article
11183         (car gnus-article-current) (cdr gnus-article-current)))
11184      (when gnus-use-cache
11185        (gnus-cache-update-article
11186         (car gnus-article-current) (cdr gnus-article-current)))))
11187
11188 (put 'gnus-with-article 'lisp-indent-function 1)
11189 (put 'gnus-with-article 'edebug-form-spec '(form body))
11190
11191 ;; Thread-based commands.
11192
11193 (defun gnus-summary-articles-in-thread (&optional article)
11194   "Return a list of all articles in the current thread.
11195 If ARTICLE is non-nil, return all articles in the thread that starts
11196 with that article."
11197   (let* ((article (or article (gnus-summary-article-number)))
11198          (data (gnus-data-find-list article))
11199          (top-level (gnus-data-level (car data)))
11200          (top-subject
11201           (cond ((null gnus-thread-operation-ignore-subject)
11202                  (gnus-simplify-subject-re
11203                   (mail-header-subject (gnus-data-header (car data)))))
11204                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11205                  (gnus-simplify-subject-fuzzy
11206                   (mail-header-subject (gnus-data-header (car data)))))
11207                 (t nil)))
11208          (end-point (save-excursion
11209                       (if (gnus-summary-go-to-next-thread)
11210                           (point) (point-max))))
11211          articles)
11212     (while (and data
11213                 (< (gnus-data-pos (car data)) end-point))
11214       (when (or (not top-subject)
11215                 (string= top-subject
11216                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11217                              (gnus-simplify-subject-fuzzy
11218                               (mail-header-subject
11219                                (gnus-data-header (car data))))
11220                            (gnus-simplify-subject-re
11221                             (mail-header-subject
11222                              (gnus-data-header (car data)))))))
11223         (push (gnus-data-number (car data)) articles))
11224       (unless (and (setq data (cdr data))
11225                    (> (gnus-data-level (car data)) top-level))
11226         (setq data nil)))
11227     ;; Return the list of articles.
11228     (nreverse articles)))
11229
11230 (defun gnus-summary-rethread-current ()
11231   "Rethread the thread the current article is part of."
11232   (interactive)
11233   (let* ((gnus-show-threads t)
11234          (article (gnus-summary-article-number))
11235          (id (mail-header-id (gnus-summary-article-header)))
11236          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11237     (unless id
11238       (error "No article on the current line"))
11239     (gnus-rebuild-thread id)
11240     (gnus-summary-goto-subject article)))
11241
11242 (defun gnus-summary-reparent-thread ()
11243   "Make the current article child of the marked (or previous) article.
11244
11245 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11246 is non-nil or the Subject: of both articles are the same."
11247   (interactive)
11248   (unless (not (gnus-group-read-only-p))
11249     (error "The current newsgroup does not support article editing"))
11250   (unless (<= (length gnus-newsgroup-processable) 1)
11251     (error "No more than one article may be marked"))
11252   (let ((child (gnus-summary-article-number))
11253         ;; First grab the marked article, otherwise one line up.
11254         (parent (if (not (null gnus-newsgroup-processable))
11255                     (car gnus-newsgroup-processable)
11256                   (save-excursion
11257                     (if (eq (forward-line -1) 0)
11258                         (gnus-summary-article-number)
11259                       (error "Beginning of summary buffer"))))))
11260     (gnus-summary-reparent-children parent (list child))))
11261
11262 (defun gnus-summary-reparent-children (parent children)
11263   "Make PARENT the parent of CHILDREN.
11264 When called interactively, PARENT is the current article and CHILDREN
11265 are the process-marked articles."
11266   (interactive
11267    (list (gnus-summary-article-number)
11268          (gnus-summary-work-articles nil)))
11269   (dolist (child children)
11270     (save-window-excursion
11271       (let ((gnus-article-buffer " *reparent*"))
11272         (unless (not (eq parent child))
11273           (error "An article may not be self-referential"))
11274         (let ((message-id (mail-header-id
11275                            (gnus-summary-article-header parent))))
11276           (unless (and message-id (not (equal message-id "")))
11277             (error "No message-id in desired parent"))
11278           (gnus-with-article child
11279             (save-restriction
11280               (goto-char (point-min))
11281               (message-narrow-to-head)
11282               (if (re-search-forward "^References: " nil t)
11283                   (progn
11284                     (re-search-forward "^[^ \t]" nil t)
11285                     (forward-line -1)
11286                     (end-of-line)
11287                     (insert " " message-id))
11288                 (insert "References: " message-id "\n"))))
11289           (set-buffer gnus-summary-buffer)
11290           (gnus-summary-unmark-all-processable)
11291           (gnus-summary-update-article child)
11292           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11293             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11294           (gnus-summary-rethread-current)
11295           (gnus-message 3 "Article %d is now the child of article %d"
11296                         child parent))))))
11297
11298 (defun gnus-summary-toggle-threads (&optional arg)
11299   "Toggle showing conversation threads.
11300 If ARG is positive number, turn showing conversation threads on."
11301   (interactive "P")
11302   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11303     (setq gnus-show-threads
11304           (if (null arg) (not gnus-show-threads)
11305             (> (prefix-numeric-value arg) 0)))
11306     (gnus-summary-prepare)
11307     (gnus-summary-goto-subject current)
11308     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11309     (gnus-summary-position-point)))
11310
11311 (eval-and-compile
11312   (if (fboundp 'remove-overlays)
11313       (defalias 'gnus-remove-overlays 'remove-overlays)
11314     (defun gnus-remove-overlays (beg end name val)
11315       "Clear BEG and END of overlays whose property NAME has value VAL.
11316 For compatibility with XEmacs."
11317       (dolist (ov (gnus-overlays-in beg end))
11318         (when (eq (gnus-overlay-get ov name) val)
11319           (gnus-delete-overlay ov))))))
11320
11321 (defun gnus-summary-show-all-threads ()
11322   "Show all threads."
11323   (interactive)
11324   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11325   (gnus-summary-position-point))
11326
11327 (defsubst gnus-summary--inv (p)
11328   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11329
11330 (defun gnus-summary-show-thread ()
11331   "Show thread subtrees.
11332 Returns nil if no thread was there to be shown."
11333   (interactive)
11334   (let* ((orig (point))
11335          (end (point-at-eol))
11336          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11337          ;; Leave point at bol
11338          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11339          (eoi (when end
11340                 (if (fboundp 'next-single-char-property-change)
11341                     (or (next-single-char-property-change end 'invisible)
11342                         (point-max))
11343                   (while (progn
11344                            (end-of-line 2)
11345                            (and (not (eobp))
11346                                 (eq (get-char-property (point) 'invisible)
11347                                     'gnus-sum))))
11348                   (point)))))
11349     (when eoi
11350       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11351       (goto-char orig)
11352       (gnus-summary-position-point)
11353       eoi)))
11354
11355 (defun gnus-summary-maybe-hide-threads ()
11356   "If requested, hide the threads that should be hidden."
11357   (when (and gnus-show-threads
11358              gnus-thread-hide-subtree)
11359     (gnus-summary-hide-all-threads
11360      (if (or (consp gnus-thread-hide-subtree)
11361              (functionp gnus-thread-hide-subtree))
11362          (gnus-make-predicate gnus-thread-hide-subtree)
11363        nil))))
11364
11365 ;;; Hiding predicates.
11366
11367 (defun gnus-article-unread-p (header)
11368   (memq (mail-header-number header) gnus-newsgroup-unreads))
11369
11370 (defun gnus-article-unseen-p (header)
11371   (memq (mail-header-number header) gnus-newsgroup-unseen))
11372
11373 (defun gnus-map-articles (predicate articles)
11374   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11375   (apply 'gnus-or (mapcar predicate
11376                           (mapcar (lambda (number)
11377                                     (gnus-summary-article-header number))
11378                                   articles))))
11379
11380 (defun gnus-summary-hide-all-threads (&optional predicate)
11381   "Hide all thread subtrees.
11382 If PREDICATE is supplied, threads that satisfy this predicate
11383 will not be hidden."
11384   (interactive)
11385   (save-excursion
11386     (goto-char (point-min))
11387     (let ((end nil))
11388       (while (not end)
11389         (when (or (not predicate)
11390                   (gnus-map-articles
11391                    predicate (gnus-summary-article-children)))
11392             (gnus-summary-hide-thread))
11393         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11394   (gnus-summary-position-point))
11395
11396 (defun gnus-summary-hide-thread ()
11397   "Hide thread subtrees.
11398 If PREDICATE is supplied, threads that satisfy this predicate
11399 will not be hidden.
11400 Returns nil if no threads were there to be hidden."
11401   (interactive)
11402   (let ((start (point))
11403         (starteol (line-end-position))
11404         (article (gnus-summary-article-number)))
11405     (goto-char start)
11406     ;; Go forward until either the buffer ends or the subthread ends.
11407     (when (and (not (eobp))
11408                (or (zerop (gnus-summary-next-thread 1 t))
11409                    (goto-char (point-max))))
11410       (if (and (> (point) start)
11411                ;; FIXME: this should actually search for a non-invisible \n.
11412                (search-backward "\n" start t))
11413           (progn
11414             (when (> (point) starteol)
11415               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11416               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11417                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11418                 (gnus-overlay-put ol 'evaporate t)))
11419             (gnus-summary-goto-subject article))
11420         (goto-char start)
11421         nil))))
11422
11423 (defun gnus-summary-go-to-next-thread (&optional previous)
11424   "Go to the same level (or less) next thread.
11425 If PREVIOUS is non-nil, go to previous thread instead.
11426 Return the article number moved to, or nil if moving was impossible."
11427   (let ((level (gnus-summary-thread-level))
11428         (way (if previous -1 1))
11429         (beg (point)))
11430     (forward-line way)
11431     (while (and (not (eobp))
11432                 (< level (gnus-summary-thread-level)))
11433       (forward-line way))
11434     (if (eobp)
11435         (progn
11436           (goto-char beg)
11437           nil)
11438       (setq beg (point))
11439       (prog1
11440           (gnus-summary-article-number)
11441         (goto-char beg)))))
11442
11443 (defun gnus-summary-next-thread (n &optional silent)
11444   "Go to the same level next N'th thread.
11445 If N is negative, search backward instead.
11446 Returns the difference between N and the number of skips actually
11447 done.
11448
11449 If SILENT, don't output messages."
11450   (interactive "p")
11451   (let ((backward (< n 0))
11452         (n (abs n)))
11453     (while (and (> n 0)
11454                 (gnus-summary-go-to-next-thread backward))
11455       (decf n))
11456     (unless silent
11457       (gnus-summary-position-point))
11458     (when (and (not silent) (/= 0 n))
11459       (gnus-message 7 "No more threads"))
11460     n))
11461
11462 (defun gnus-summary-prev-thread (n)
11463   "Go to the same level previous N'th thread.
11464 Returns the difference between N and the number of skips actually
11465 done."
11466   (interactive "p")
11467   (gnus-summary-next-thread (- n)))
11468
11469 (defun gnus-summary-go-down-thread ()
11470   "Go down one level in the current thread."
11471   (let ((children (gnus-summary-article-children)))
11472     (when children
11473       (gnus-summary-goto-subject (car children)))))
11474
11475 (defun gnus-summary-go-up-thread ()
11476   "Go up one level in the current thread."
11477   (let ((parent (gnus-summary-article-parent)))
11478     (when parent
11479       (gnus-summary-goto-subject parent))))
11480
11481 (defun gnus-summary-down-thread (n)
11482   "Go down thread N steps.
11483 If N is negative, go up instead.
11484 Returns the difference between N and how many steps down that were
11485 taken."
11486   (interactive "p")
11487   (let ((up (< n 0))
11488         (n (abs n)))
11489     (while (and (> n 0)
11490                 (if up (gnus-summary-go-up-thread)
11491                   (gnus-summary-go-down-thread)))
11492       (setq n (1- n)))
11493     (gnus-summary-position-point)
11494     (when (/= 0 n)
11495       (gnus-message 7 "Can't go further"))
11496     n))
11497
11498 (defun gnus-summary-up-thread (n)
11499   "Go up thread N steps.
11500 If N is negative, go down instead.
11501 Returns the difference between N and how many steps down that were
11502 taken."
11503   (interactive "p")
11504   (gnus-summary-down-thread (- n)))
11505
11506 (defun gnus-summary-top-thread ()
11507   "Go to the top of the thread."
11508   (interactive)
11509   (while (gnus-summary-go-up-thread))
11510   (gnus-summary-article-number))
11511
11512 (defun gnus-summary-expire-thread ()
11513   "Mark articles under current thread as expired."
11514   (interactive)
11515   (gnus-summary-kill-thread 0))
11516
11517 (defun gnus-summary-kill-thread (&optional unmark)
11518   "Mark articles under current thread as read.
11519 If the prefix argument is positive, remove any kinds of marks.
11520 If the prefix argument is zero, mark thread as expired.
11521 If the prefix argument is negative, tick articles instead."
11522   (interactive "P")
11523   (when unmark
11524     (setq unmark (prefix-numeric-value unmark)))
11525   (let ((articles (gnus-summary-articles-in-thread))
11526         (hide (or (null unmark) (= unmark 0))))
11527     (save-excursion
11528       ;; Expand the thread.
11529       (gnus-summary-show-thread)
11530       ;; Mark all the articles.
11531       (while articles
11532         (gnus-summary-goto-subject (car articles))
11533         (cond ((null unmark)
11534                (gnus-summary-mark-article-as-read gnus-killed-mark))
11535               ((> unmark 0)
11536                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11537               ((= unmark 0)
11538                (gnus-summary-mark-article nil gnus-expirable-mark))
11539               (t
11540                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11541         (setq articles (cdr articles))))
11542     ;; Hide killed subtrees when hide is true.
11543     (and hide
11544          gnus-thread-hide-killed
11545          (gnus-summary-hide-thread))
11546     ;; If hide is t, go to next unread subject.
11547     (when hide
11548       ;; Go to next unread subject.
11549       (gnus-summary-next-subject 1 t)))
11550   (gnus-set-mode-line 'summary))
11551
11552 ;; Summary sorting commands
11553
11554 (defun gnus-summary-sort-by-number (&optional reverse)
11555   "Sort the summary buffer by article number.
11556 Argument REVERSE means reverse order."
11557   (interactive "P")
11558   (gnus-summary-sort 'number reverse))
11559
11560 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11561   "Sort the summary buffer by most recent article number.
11562 Argument REVERSE means reverse order."
11563   (interactive "P")
11564   (gnus-summary-sort 'most-recent-number reverse))
11565
11566 (defun gnus-summary-sort-by-random (&optional reverse)
11567   "Randomize the order in the summary buffer.
11568 Argument REVERSE means to randomize in reverse order."
11569   (interactive "P")
11570   (gnus-summary-sort 'random reverse))
11571
11572 (defun gnus-summary-sort-by-author (&optional reverse)
11573   "Sort the summary buffer by author name alphabetically.
11574 If `case-fold-search' is non-nil, case of letters is ignored.
11575 Argument REVERSE means reverse order."
11576   (interactive "P")
11577   (gnus-summary-sort 'author reverse))
11578
11579 (defun gnus-summary-sort-by-recipient (&optional reverse)
11580   "Sort the summary buffer by recipient name alphabetically.
11581 If `case-fold-search' is non-nil, case of letters is ignored.
11582 Argument REVERSE means reverse order."
11583   (interactive "P")
11584   (gnus-summary-sort 'recipient reverse))
11585
11586 (defun gnus-summary-sort-by-subject (&optional reverse)
11587   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11588 If `case-fold-search' is non-nil, case of letters is ignored.
11589 Argument REVERSE means reverse order."
11590   (interactive "P")
11591   (gnus-summary-sort 'subject reverse))
11592
11593 (defun gnus-summary-sort-by-date (&optional reverse)
11594   "Sort the summary buffer by date.
11595 Argument REVERSE means reverse order."
11596   (interactive "P")
11597   (gnus-summary-sort 'date reverse))
11598
11599 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11600   "Sort the summary buffer by most recent date.
11601 Argument REVERSE means reverse order."
11602   (interactive "P")
11603   (gnus-summary-sort 'most-recent-date reverse))
11604
11605 (defun gnus-summary-sort-by-score (&optional reverse)
11606   "Sort the summary buffer by score.
11607 Argument REVERSE means reverse order."
11608   (interactive "P")
11609   (gnus-summary-sort 'score reverse))
11610
11611 (defun gnus-summary-sort-by-lines (&optional reverse)
11612   "Sort the summary buffer by the number of lines.
11613 Argument REVERSE means reverse order."
11614   (interactive "P")
11615   (gnus-summary-sort 'lines reverse))
11616
11617 (defun gnus-summary-sort-by-chars (&optional reverse)
11618   "Sort the summary buffer by article length.
11619 Argument REVERSE means reverse order."
11620   (interactive "P")
11621   (gnus-summary-sort 'chars reverse))
11622
11623 (defun gnus-summary-sort-by-original (&optional reverse)
11624   "Sort the summary buffer using the default sorting method.
11625 Argument REVERSE means reverse order."
11626   (interactive "P")
11627   (let* ((inhibit-read-only t)
11628          (gnus-summary-prepare-hook nil))
11629     ;; We do the sorting by regenerating the threads.
11630     (gnus-summary-prepare)
11631     ;; Hide subthreads if needed.
11632     (gnus-summary-maybe-hide-threads)))
11633
11634 (defun gnus-summary-sort (predicate reverse)
11635   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11636   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11637          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11638          (gnus-thread-sort-functions
11639           (if (not reverse)
11640               thread
11641             `(lambda (t1 t2)
11642                (,thread t2 t1))))
11643          (gnus-sort-gathered-threads-function
11644           gnus-thread-sort-functions)
11645          (gnus-article-sort-functions
11646           (if (not reverse)
11647               article
11648             `(lambda (t1 t2)
11649                (,article t2 t1))))
11650          (inhibit-read-only t)
11651          (gnus-summary-prepare-hook nil))
11652     ;; We do the sorting by regenerating the threads.
11653     (gnus-summary-prepare)
11654     ;; Hide subthreads if needed.
11655     (gnus-summary-maybe-hide-threads)))
11656
11657 ;; Summary saving commands.
11658
11659 (defun gnus-summary-save-article (&optional n not-saved)
11660   "Save the current article using the default saver function.
11661 If N is a positive number, save the N next articles.
11662 If N is a negative number, save the N previous articles.
11663 If N is nil and any articles have been marked with the process mark,
11664 save those articles instead.
11665 The variable `gnus-default-article-saver' specifies the saver function.
11666
11667 If the optional second argument NOT-SAVED is non-nil, articles saved
11668 will not be marked as saved."
11669   (interactive "P")
11670   (require 'gnus-art)
11671   (let* ((articles (gnus-summary-work-articles n))
11672          (save-buffer (save-excursion
11673                         (nnheader-set-temp-buffer " *Gnus Save*")))
11674          (num (length articles))
11675          ;; Whether to save decoded articles or raw articles.
11676          (decode (when gnus-article-save-coding-system
11677                    (get gnus-default-article-saver :decode)))
11678          ;; When saving many articles in a single file, use the other
11679          ;; function to save articles other than the first one.
11680          (saver2 (get gnus-default-article-saver :function))
11681          (gnus-prompt-before-saving (if saver2
11682                                         t
11683                                       gnus-prompt-before-saving))
11684          (gnus-default-article-saver gnus-default-article-saver)
11685          header file)
11686     (dolist (article articles)
11687       (setq header (gnus-summary-article-header article))
11688       (if (not (vectorp header))
11689           ;; This is a pseudo-article.
11690           (if (assq 'name header)
11691               (gnus-copy-file (cdr (assq 'name header)))
11692             (gnus-message 1 "Article %d is unsaveable" article))
11693         ;; This is a real article.
11694         (save-window-excursion
11695           (gnus-summary-select-article decode decode nil article)
11696           (gnus-summary-goto-subject article))
11697         (with-current-buffer save-buffer
11698           (erase-buffer)
11699           (insert-buffer-substring (if decode
11700                                        gnus-article-buffer
11701                                      gnus-original-article-buffer)))
11702         (setq file (gnus-article-save save-buffer file num))
11703         (gnus-summary-remove-process-mark article)
11704         (unless not-saved
11705           (gnus-summary-set-saved-mark article)))
11706       (when saver2
11707         (setq gnus-default-article-saver saver2
11708               saver2 nil)))
11709     (gnus-kill-buffer save-buffer)
11710     (gnus-summary-position-point)
11711     (gnus-set-mode-line 'summary)
11712     n))
11713
11714 (defun gnus-summary-pipe-output (&optional n sym)
11715   "Pipe the current article to a subprocess.
11716 If N is a positive number, pipe the N next articles.
11717 If N is a negative number, pipe the N previous articles.
11718 If N is nil and any articles have been marked with the process mark,
11719 pipe those articles instead.
11720 The default command to which articles are piped is specified by the
11721 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11722 will be prompted for the command.
11723
11724 The properties `:decode' and `:headers' that are put to the function
11725 symbol `gnus-summary-save-in-pipe' control whether this function
11726 decodes articles and what headers to keep (see the doc string for the
11727 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11728 is neither omitted nor the symbol `r', force including all headers
11729 regardless of the `:headers' property.  If it is the symbol `r',
11730 articles that are not decoded and include all headers will be piped
11731 no matter what the properties `:decode' and `:headers' are."
11732   (interactive (gnus-interactive "P\ny"))
11733   (require 'gnus-art)
11734   (let* ((articles (gnus-summary-work-articles n))
11735          (result-buffer "*Shell Command Output*")
11736          (all-headers (not (memq sym '(nil r))))
11737          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11738          (raw (eq sym 'r))
11739          (headers (get 'gnus-summary-save-in-pipe :headers))
11740          command result)
11741     (unless (numberp (car articles))
11742       (error "No article to pipe"))
11743     (setq command (gnus-read-shell-command
11744                    (concat "Shell command on "
11745                            (if (cdr articles)
11746                                (format "these %d articles" (length articles))
11747                              "this article")
11748                            ": ")
11749                    gnus-summary-pipe-output-default-command))
11750     (when (string-equal command "")
11751       (error "A command is required"))
11752     (when all-headers
11753       (put 'gnus-summary-save-in-pipe :headers nil))
11754     (unwind-protect
11755         (while articles
11756           (gnus-summary-goto-subject (pop articles))
11757           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11758           (when (and (get-buffer result-buffer)
11759                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11760             (setq result (concat result (with-current-buffer result-buffer
11761                                           (buffer-string))))))
11762       (put 'gnus-summary-save-in-pipe :headers headers))
11763     (unless (zerop (length result))
11764       (if (with-current-buffer (get-buffer-create result-buffer)
11765             (erase-buffer)
11766             (insert result)
11767             (prog1
11768                 (and (= (count-lines (point-min) (point)) 1)
11769                      (progn
11770                        (end-of-line 0)
11771                        (<= (current-column)
11772                            (window-width (minibuffer-window)))))
11773               (goto-char (point-min))))
11774           (message "%s" (substring result 0 -1))
11775         (message nil)
11776         (gnus-configure-windows 'pipe)))))
11777
11778 (defun gnus-summary-save-article-mail (&optional arg)
11779   "Append the current article to a Unix mail box file.
11780 If N is a positive number, save the N next articles.
11781 If N is a negative number, save the N previous articles.
11782 If N is nil and any articles have been marked with the process mark,
11783 save those articles instead."
11784   (interactive "P")
11785   (require 'gnus-art)
11786   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11787     (gnus-summary-save-article arg)))
11788
11789 (defun gnus-summary-save-article-rmail (&optional arg)
11790   "Append the current article to an rmail file.
11791 If N is a positive number, save the N next articles.
11792 If N is a negative number, save the N previous articles.
11793 If N is nil and any articles have been marked with the process mark,
11794 save those articles instead."
11795   (interactive "P")
11796   (require 'gnus-art)
11797   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11798     (gnus-summary-save-article arg)))
11799
11800 (defun gnus-summary-save-article-file (&optional arg)
11801   "Append the current article to a file.
11802 If N is a positive number, save the N next articles.
11803 If N is a negative number, save the N previous articles.
11804 If N is nil and any articles have been marked with the process mark,
11805 save those articles instead."
11806   (interactive "P")
11807   (require 'gnus-art)
11808   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11809     (gnus-summary-save-article arg)))
11810
11811 (defun gnus-summary-write-article-file (&optional arg)
11812   "Write the current article to a file, deleting the previous file.
11813 If N is a positive number, save the N next articles.
11814 If N is a negative number, save the N previous articles.
11815 If N is nil and any articles have been marked with the process mark,
11816 save those articles instead."
11817   (interactive "P")
11818   (require 'gnus-art)
11819   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11820     (gnus-summary-save-article arg)))
11821
11822 (defun gnus-summary-save-article-body-file (&optional arg)
11823   "Append the current article body to a file.
11824 If N is a positive number, save the N next articles.
11825 If N is a negative number, save the N previous articles.
11826 If N is nil and any articles have been marked with the process mark,
11827 save those articles instead."
11828   (interactive "P")
11829   (require 'gnus-art)
11830   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11831     (gnus-summary-save-article arg)))
11832
11833 (defun gnus-summary-write-article-body-file (&optional arg)
11834   "Write the current article body to a file, deleting the previous file.
11835 If N is a positive number, save the N next articles.
11836 If N is a negative number, save the N previous articles.
11837 If N is nil and any articles have been marked with the process mark,
11838 save those articles instead."
11839   (interactive "P")
11840   (require 'gnus-art)
11841   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11842     (gnus-summary-save-article arg)))
11843
11844 (defun gnus-summary-muttprint (&optional arg)
11845   "Print the current article using Muttprint.
11846 If N is a positive number, save the N next articles.
11847 If N is a negative number, save the N previous articles.
11848 If N is nil and any articles have been marked with the process mark,
11849 save those articles instead."
11850   (interactive "P")
11851   (require 'gnus-art)
11852   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11853     (gnus-summary-save-article arg t)))
11854
11855 (defun gnus-summary-pipe-message (program)
11856   "Pipe the current article through PROGRAM."
11857   (interactive "sProgram: ")
11858   (gnus-summary-select-article)
11859   (let ((mail-header-separator ""))
11860     (gnus-eval-in-buffer-window gnus-article-buffer
11861       (save-restriction
11862         (widen)
11863         (let ((start (window-start))
11864               (inhibit-read-only t))
11865           (message-pipe-buffer-body program)
11866           (set-window-start (get-buffer-window (current-buffer)) start))))))
11867
11868 (defun gnus-get-split-value (methods)
11869   "Return a value based on the split METHODS."
11870   (let (split-name method result match)
11871     (when methods
11872       (with-current-buffer gnus-original-article-buffer
11873         (save-restriction
11874           (nnheader-narrow-to-headers)
11875           (while (and methods (not split-name))
11876             (goto-char (point-min))
11877             (setq method (pop methods))
11878             (setq match (car method))
11879             (when (cond
11880                    ((stringp match)
11881                     ;; Regular expression.
11882                     (ignore-errors
11883                       (re-search-forward match nil t)))
11884                    ((functionp match)
11885                     ;; Function.
11886                     (save-restriction
11887                       (widen)
11888                       (setq result (funcall match gnus-newsgroup-name))))
11889                    ((consp match)
11890                     ;; Form.
11891                     (save-restriction
11892                       (widen)
11893                       (setq result (eval match)))))
11894               (setq split-name (cdr method))
11895               (cond ((stringp result)
11896                      (push (expand-file-name
11897                             result gnus-article-save-directory)
11898                            split-name))
11899                     ((consp result)
11900                      (setq split-name (append result split-name)))))))))
11901     (nreverse split-name)))
11902
11903 (defun gnus-valid-move-group-p (group)
11904   (and (symbolp group)
11905        (boundp group)
11906        (symbol-name group)
11907        (symbol-value group)
11908        (gnus-get-function (gnus-find-method-for-group
11909                            (symbol-name group)) 'request-accept-article t)))
11910
11911 (defun gnus-read-move-group-name (prompt default articles prefix)
11912   "Read a group name."
11913   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11914          (minibuffer-confirm-incomplete nil) ; XEmacs
11915          (prom
11916           (format "%s %s to"
11917                   prompt
11918                   (if (> (length articles) 1)
11919                       (format "these %d articles" (length articles))
11920                     "this article")))
11921          (to-newsgroup
11922           (cond
11923            ((null split-name)
11924             (gnus-group-completing-read
11925              prom
11926              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11927              nil prefix nil default))
11928            ((= 1 (length split-name))
11929             (gnus-group-completing-read
11930              prom
11931              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11932              nil prefix 'gnus-group-history (car split-name)))
11933            (t
11934             (gnus-completing-read
11935              prom (nreverse split-name) nil nil 'gnus-group-history))))
11936          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11937          encoded)
11938     (when to-newsgroup
11939       (if (or (string= to-newsgroup "")
11940               (string= to-newsgroup prefix))
11941           (setq to-newsgroup default))
11942       (unless to-newsgroup
11943         (error "No group name entered"))
11944       (setq encoded (mm-encode-coding-string
11945                      to-newsgroup
11946                      (gnus-group-name-charset to-method to-newsgroup)))
11947       (or (gnus-active encoded)
11948           (gnus-activate-group encoded nil nil to-method)
11949           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11950                                      to-newsgroup))
11951               (or (and (gnus-request-create-group encoded to-method)
11952                        (gnus-activate-group encoded nil nil to-method)
11953                        (gnus-subscribe-group encoded))
11954                   (error "Couldn't create group %s" to-newsgroup)))
11955           (error "No such group: %s" to-newsgroup))
11956       encoded)))
11957
11958 (defvar gnus-summary-save-parts-counter)
11959 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11960
11961 (defun gnus-summary-save-parts (type dir n &optional reverse)
11962   "Save parts matching TYPE to DIR.
11963 If REVERSE, save parts that do not match TYPE."
11964   (interactive
11965    (list (read-string "Save parts of type: "
11966                       (or (car gnus-summary-save-parts-type-history)
11967                           gnus-summary-save-parts-default-mime)
11968                       'gnus-summary-save-parts-type-history)
11969          (setq gnus-summary-save-parts-last-directory
11970                (read-file-name "Save to directory: "
11971                                gnus-summary-save-parts-last-directory
11972                                nil t))
11973          current-prefix-arg))
11974   (gnus-summary-iterate n
11975     (let ((gnus-display-mime-function nil)
11976           gnus-article-prepare-hook
11977           gnus-article-decode-hook
11978           gnus-display-mime-function
11979           gnus-break-pages
11980           (gnus-inhibit-treatment t))
11981       (gnus-summary-select-article))
11982     (with-current-buffer gnus-article-buffer
11983       (let ((handles (or gnus-article-mime-handles
11984                          (mm-dissect-buffer nil gnus-article-loose-mime)
11985                          (and gnus-article-emulate-mime
11986                               (mm-uu-dissect))))
11987             (gnus-summary-save-parts-counter 1))
11988         (when handles
11989           (gnus-summary-save-parts-1 type dir handles reverse)
11990           (unless gnus-article-mime-handles ;; Don't destroy this case.
11991             (mm-destroy-parts handles)))))))
11992
11993 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11994   (if (stringp (car handle))
11995       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11996               (cdr handle))
11997     (when (if reverse
11998               (not (string-match type (mm-handle-media-type handle)))
11999             (string-match type (mm-handle-media-type handle)))
12000       (let ((file (expand-file-name
12001                    (gnus-map-function
12002                     mm-file-name-rewrite-functions
12003                     (file-name-nondirectory
12004                      (or
12005                       (mail-content-type-get
12006                        (mm-handle-disposition handle) 'filename)
12007                       (mail-content-type-get
12008                        (mm-handle-type handle) 'name)
12009                       (format "%s.%d.%d" gnus-newsgroup-name
12010                               (cdr gnus-article-current)
12011                               gnus-summary-save-parts-counter))))
12012                    dir)))
12013         (incf gnus-summary-save-parts-counter)
12014         (unless (file-exists-p file)
12015           (mm-save-part-to-file handle file))))))
12016
12017 ;; Summary extract commands
12018
12019 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12020   (let ((inhibit-read-only t)
12021         (article (gnus-summary-article-number))
12022         after-article b e)
12023     (unless (gnus-summary-goto-subject article)
12024       (error "No such article: %d" article))
12025     (gnus-summary-position-point)
12026     ;; If all commands are to be bunched up on one line, we collect
12027     ;; them here.
12028     (unless gnus-view-pseudos-separately
12029       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12030             files action)
12031         (while ps
12032           (setq action (cdr (assq 'action (car ps))))
12033           (setq files (list (cdr (assq 'name (car ps)))))
12034           (while (and ps (cdr ps)
12035                       (string= (or action "1")
12036                                (or (cdr (assq 'action (cadr ps))) "2")))
12037             (push (cdr (assq 'name (cadr ps))) files)
12038             (setcdr ps (cddr ps)))
12039           (when files
12040             (when (not (string-match "%s" action))
12041               (push " " files))
12042             (push " " files)
12043             (when (assq 'execute (car ps))
12044               (setcdr (assq 'execute (car ps))
12045                       (funcall (if (string-match "%s" action)
12046                                    'format 'concat)
12047                                action
12048                                (mapconcat
12049                                 (lambda (f)
12050                                   (if (equal f " ")
12051                                       f
12052                                     (shell-quote-argument f)))
12053                                 files " ")))))
12054           (setq ps (cdr ps)))))
12055     (if (and gnus-view-pseudos (not not-view))
12056         (while pslist
12057           (when (assq 'execute (car pslist))
12058             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12059                                   (eq gnus-view-pseudos 'not-confirm)))
12060           (setq pslist (cdr pslist)))
12061       (save-excursion
12062         (while pslist
12063           (setq after-article (or (cdr (assq 'article (car pslist)))
12064                                   (gnus-summary-article-number)))
12065           (gnus-summary-goto-subject after-article)
12066           (forward-line 1)
12067           (setq b (point))
12068           (insert "    " (file-name-nondirectory
12069                           (cdr (assq 'name (car pslist))))
12070                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12071           (setq e (point))
12072           (forward-line -1)             ; back to `b'
12073           (gnus-add-text-properties
12074            b (1- e) (list 'gnus-number gnus-reffed-article-number
12075                           gnus-mouse-face-prop gnus-mouse-face))
12076           (gnus-data-enter
12077            after-article gnus-reffed-article-number
12078            gnus-unread-mark b (car pslist) 0 (- e b))
12079           (setq gnus-newsgroup-unreads
12080                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12081                                          gnus-reffed-article-number))
12082           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12083           (setq pslist (cdr pslist)))))))
12084
12085 (defun gnus-pseudos< (p1 p2)
12086   (let ((c1 (cdr (assq 'action p1)))
12087         (c2 (cdr (assq 'action p2))))
12088     (and c1 c2 (string< c1 c2))))
12089
12090 (defun gnus-request-pseudo-article (props)
12091   (cond ((assq 'execute props)
12092          (gnus-execute-command (cdr (assq 'execute props)))))
12093   (let ((gnus-current-article (gnus-summary-article-number)))
12094     (gnus-run-hooks 'gnus-mark-article-hook)))
12095
12096 (defun gnus-execute-command (command &optional automatic)
12097   (save-excursion
12098     (gnus-article-setup-buffer)
12099     (set-buffer gnus-article-buffer)
12100     (setq buffer-read-only nil)
12101     (let ((command (if automatic command
12102                      (read-string "Command: " (cons command 0)))))
12103       (erase-buffer)
12104       (insert "$ " command "\n\n")
12105       (if gnus-view-pseudo-asynchronously
12106           (start-process "gnus-execute" (current-buffer) shell-file-name
12107                          shell-command-switch command)
12108         (call-process shell-file-name nil t nil
12109                       shell-command-switch command)))))
12110
12111 ;; Summary kill commands.
12112
12113 (defun gnus-summary-edit-global-kill (article)
12114   "Edit the \"global\" kill file."
12115   (interactive (list (gnus-summary-article-number)))
12116   (gnus-group-edit-global-kill article))
12117
12118 (defun gnus-summary-edit-local-kill ()
12119   "Edit a local kill file applied to the current newsgroup."
12120   (interactive)
12121   (setq gnus-current-headers (gnus-summary-article-header))
12122   (gnus-group-edit-local-kill
12123    (gnus-summary-article-number) gnus-newsgroup-name))
12124
12125 ;;; Header reading.
12126
12127 (defun gnus-read-header (id &optional header)
12128   "Read the headers of article ID and enter them into the Gnus system."
12129   (let ((group gnus-newsgroup-name)
12130         (gnus-override-method
12131          (or
12132           gnus-override-method
12133           (and (gnus-news-group-p gnus-newsgroup-name)
12134                (car (gnus-refer-article-methods)))))
12135         where)
12136     ;; First we check to see whether the header in question is already
12137     ;; fetched.
12138     (if (stringp id)
12139         ;; This is a Message-ID.
12140         (setq header (or header (gnus-id-to-header id)))
12141       ;; This is an article number.
12142       (setq header (or header (gnus-summary-article-header id))))
12143     (if (and header
12144              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12145         ;; We have found the header.
12146         header
12147       ;; We have to really fetch the header to this article.
12148       (with-current-buffer nntp-server-buffer
12149         (when (setq where (gnus-request-head id group))
12150           (nnheader-fold-continuation-lines)
12151           (goto-char (point-max))
12152           (insert ".\n")
12153           (goto-char (point-min))
12154           (insert "211 ")
12155           (princ (cond
12156                   ((numberp id) id)
12157                   ((cdr where) (cdr where))
12158                   (header (mail-header-number header))
12159                   (t gnus-reffed-article-number))
12160                  (current-buffer))
12161           (insert " Article retrieved.\n"))
12162         (if (or (not where)
12163                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12164             ()                          ; Malformed head.
12165           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12166             (when (and (stringp id)
12167                        (or
12168                         (not (string= (gnus-group-real-name group)
12169                                       (car where)))
12170                         (not (gnus-server-equal gnus-override-method
12171                                                 (gnus-group-method group)))))
12172               ;; If we fetched by Message-ID and the article came from
12173               ;; a different group (or server), we fudge some bogus
12174               ;; article numbers for this article.
12175               (mail-header-set-number header gnus-reffed-article-number))
12176             (with-current-buffer gnus-summary-buffer
12177               (decf gnus-reffed-article-number)
12178               (gnus-remove-header (mail-header-number header))
12179               (push header gnus-newsgroup-headers)
12180               (setq gnus-current-headers header)
12181               (push (mail-header-number header) gnus-newsgroup-limit)))
12182           header)))))
12183
12184 (defun gnus-remove-header (number)
12185   "Remove header NUMBER from `gnus-newsgroup-headers'."
12186   (if (and gnus-newsgroup-headers
12187            (= number (mail-header-number (car gnus-newsgroup-headers))))
12188       (pop gnus-newsgroup-headers)
12189     (let ((headers gnus-newsgroup-headers))
12190       (while (and (cdr headers)
12191                   (not (= number (mail-header-number (cadr headers)))))
12192         (pop headers))
12193       (when (cdr headers)
12194         (setcdr headers (cddr headers))))))
12195
12196 ;;;
12197 ;;; summary highlights
12198 ;;;
12199
12200 (defun gnus-highlight-selected-summary ()
12201   "Highlight selected article in summary buffer."
12202   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12203   (when gnus-summary-selected-face
12204     (save-excursion
12205       (let* ((beg (point-at-bol))
12206              (end (point-at-eol))
12207              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12208              (from (if (get-text-property beg gnus-mouse-face-prop)
12209                        beg
12210                      (or (next-single-property-change
12211                           beg gnus-mouse-face-prop nil end)
12212                          beg)))
12213              (to
12214               (if (= from end)
12215                   (- from 2)
12216                 (or (next-single-property-change
12217                      from gnus-mouse-face-prop nil end)
12218                     end))))
12219         ;; If no mouse-face prop on line we will have to = from = end,
12220         ;; so we highlight the entire line instead.
12221         (when (= (+ to 2) from)
12222           (setq from beg)
12223           (setq to end))
12224         (if gnus-newsgroup-selected-overlay
12225             ;; Move old overlay.
12226             (gnus-move-overlay
12227              gnus-newsgroup-selected-overlay from to (current-buffer))
12228           ;; Create new overlay.
12229           (gnus-overlay-put
12230            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12231            'face gnus-summary-selected-face))))))
12232
12233 (defvar gnus-summary-highlight-line-cached nil)
12234 (defvar gnus-summary-highlight-line-trigger nil)
12235
12236 (defun gnus-summary-highlight-line-0 ()
12237   (if (and (eq gnus-summary-highlight-line-trigger
12238                gnus-summary-highlight)
12239            gnus-summary-highlight-line-cached)
12240       gnus-summary-highlight-line-cached
12241     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12242           gnus-summary-highlight-line-cached
12243           (let* ((cond (list 'cond))
12244                  (c cond)
12245                  (list gnus-summary-highlight))
12246             (while list
12247               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12248                               nil))
12249               (setq c (cdr c)
12250                     list (cdr list)))
12251             (gnus-byte-compile (list 'lambda nil cond))))))
12252
12253 (defun gnus-summary-highlight-line ()
12254   "Highlight current line according to `gnus-summary-highlight'."
12255   (let* ((beg (point-at-bol))
12256          (article (or (gnus-summary-article-number) gnus-current-article))
12257          (score (or (cdr (assq article
12258                                gnus-newsgroup-scored))
12259                     gnus-summary-default-score 0))
12260          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12261          (inhibit-read-only t)
12262          (default gnus-summary-default-score)
12263          (default-high gnus-summary-default-high-score)
12264          (default-low gnus-summary-default-low-score)
12265          (uncached (and gnus-summary-use-undownloaded-faces
12266                         (memq article gnus-newsgroup-undownloaded)
12267                         (not (memq article gnus-newsgroup-cached)))))
12268     (let ((face (funcall (gnus-summary-highlight-line-0))))
12269       (unless (eq face (get-text-property beg 'face))
12270         (gnus-put-text-property-excluding-characters-with-faces
12271          beg (point-at-eol) 'face
12272          (setq face (if (boundp face) (symbol-value face) face)))
12273         (when gnus-summary-highlight-line-function
12274           (funcall gnus-summary-highlight-line-function article face))))))
12275
12276 (defun gnus-update-read-articles (group unread &optional compute)
12277   "Update the list of read articles in GROUP.
12278 UNREAD is a sorted list."
12279   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12280         (info (gnus-get-info group))
12281         (prev 1)
12282         read)
12283     (if (or (not info) (not active))
12284         ;; There is no info on this group if it was, in fact,
12285         ;; killed.  Gnus stores no information on killed groups, so
12286         ;; there's nothing to be done.
12287         ;; One could store the information somewhere temporarily,
12288         ;; perhaps...  Hmmm...
12289         ()
12290       ;; Remove any negative articles numbers.
12291       (while (and unread (< (car unread) 0))
12292         (setq unread (cdr unread)))
12293       ;; Remove any expired article numbers
12294       (while (and unread (< (car unread) (car active)))
12295         (setq unread (cdr unread)))
12296       ;; Compute the ranges of read articles by looking at the list of
12297       ;; unread articles.
12298       (while unread
12299         (when (/= (car unread) prev)
12300           (push (if (= prev (1- (car unread))) prev
12301                   (cons prev (1- (car unread))))
12302                 read))
12303         (setq prev (1+ (car unread)))
12304         (setq unread (cdr unread)))
12305       (when (<= prev (cdr active))
12306         (push (cons prev (cdr active)) read))
12307       (setq read (if (> (length read) 1) (nreverse read) read))
12308       (if compute
12309           read
12310         (save-excursion
12311           (let (setmarkundo)
12312             ;; Propagate the read marks to the backend.
12313             (when (and gnus-propagate-marks
12314                        (gnus-check-backend-function 'request-set-mark group))
12315               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12316                     (add (gnus-remove-from-range read (gnus-info-read info))))
12317                 (when (or add del)
12318                   (unless (gnus-check-group group)
12319                     (error "Can't open server for %s" group))
12320                   (gnus-request-set-mark
12321                    group (delq nil (list (if add (list add 'add '(read)))
12322                                          (if del (list del 'del '(read))))))
12323                   (setq setmarkundo
12324                         `(gnus-request-set-mark
12325                           ,group
12326                           ',(delq nil (list
12327                                        (if del (list del 'add '(read)))
12328                                        (if add (list add 'del '(read))))))))))
12329             (set-buffer gnus-group-buffer)
12330             (gnus-undo-register
12331               `(progn
12332                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12333                  (gnus-info-set-read ',info ',(gnus-info-read info))
12334                  (gnus-get-unread-articles-in-group ',info
12335                                                     (gnus-active ,group))
12336                  (gnus-group-update-group ,group t)
12337                  ,setmarkundo))))
12338         ;; Enter this list into the group info.
12339         (gnus-info-set-read info read)
12340         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12341         (gnus-get-unread-articles-in-group info (gnus-active group))
12342         t))))
12343
12344 (defun gnus-offer-save-summaries ()
12345   "Offer to save all active summary buffers."
12346   (let (buffers)
12347     ;; Go through all buffers and find all summaries.
12348     (dolist (buffer (buffer-list))
12349       (when (and (setq buffer (buffer-name buffer))
12350                  (string-match "Summary" buffer)
12351                  (with-current-buffer buffer
12352                    ;; We check that this is, indeed, a summary buffer.
12353                    (and (eq major-mode 'gnus-summary-mode)
12354                         ;; Also make sure this isn't bogus.
12355                         gnus-newsgroup-prepared
12356                         ;; Also make sure that this isn't a
12357                         ;; dead summary buffer.
12358                         (not gnus-dead-summary-mode))))
12359         (push buffer buffers)))
12360     ;; Go through all these summary buffers and offer to save them.
12361     (when buffers
12362       (save-excursion
12363         (map-y-or-n-p
12364          "Update summary buffer %s? "
12365          (lambda (buf)
12366            (switch-to-buffer buf)
12367            (gnus-summary-exit))
12368          buffers)))))
12369
12370 (defun gnus-summary-setup-default-charset ()
12371   "Setup newsgroup default charset."
12372   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12373       (setq gnus-newsgroup-charset nil)
12374     (let* ((ignored-charsets
12375             (or gnus-newsgroup-ephemeral-ignored-charsets
12376                 (append
12377                  (and gnus-newsgroup-name
12378                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12379                  gnus-newsgroup-ignored-charsets))))
12380       (setq gnus-newsgroup-charset
12381             (or gnus-newsgroup-ephemeral-charset
12382                 (and gnus-newsgroup-name
12383                      (gnus-parameter-charset gnus-newsgroup-name))
12384                 gnus-default-charset))
12385       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12386            ignored-charsets))))
12387
12388 ;;;
12389 ;;; Mime Commands
12390 ;;;
12391
12392 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12393   "Display the current article buffer fully MIME-buttonized.
12394 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12395 treated as multipart/mixed."
12396   (interactive "P")
12397   (require 'gnus-art)
12398   (let ((gnus-unbuttonized-mime-types nil)
12399         (gnus-mime-display-multipart-as-mixed show-all-parts))
12400     (gnus-summary-show-article)))
12401
12402 (defun gnus-summary-repair-multipart (article)
12403   "Add a Content-Type header to a multipart article without one."
12404   (interactive (list (gnus-summary-article-number)))
12405   (gnus-with-article article
12406     (message-narrow-to-head)
12407     (message-remove-header "Mime-Version")
12408     (goto-char (point-max))
12409     (insert "Mime-Version: 1.0\n")
12410     (widen)
12411     (when (search-forward "\n--" nil t)
12412       (let ((separator (buffer-substring (point) (point-at-eol))))
12413         (message-narrow-to-head)
12414         (message-remove-header "Content-Type")
12415         (goto-char (point-max))
12416         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12417                         separator))
12418         (widen))))
12419   (let (gnus-mark-article-hook)
12420     (gnus-summary-select-article t t nil article)))
12421
12422 (defun gnus-summary-toggle-display-buttonized ()
12423   "Toggle the buttonizing of the article buffer."
12424   (interactive)
12425   (require 'gnus-art)
12426   (if (setq gnus-inhibit-mime-unbuttonizing
12427             (not gnus-inhibit-mime-unbuttonizing))
12428       (let ((gnus-unbuttonized-mime-types nil))
12429         (gnus-summary-show-article))
12430     (gnus-summary-show-article)))
12431
12432 ;;;
12433 ;;; Generic summary marking commands
12434 ;;;
12435
12436 (defvar gnus-summary-marking-alist
12437   '((read gnus-del-mark "d")
12438     (unread gnus-unread-mark "u")
12439     (ticked gnus-ticked-mark "!")
12440     (dormant gnus-dormant-mark "?")
12441     (expirable gnus-expirable-mark "e"))
12442   "An alist of names/marks/keystrokes.")
12443
12444 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12445 (defvar gnus-summary-mark-map)
12446
12447 (defun gnus-summary-make-all-marking-commands ()
12448   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12449   (dolist (elem gnus-summary-marking-alist)
12450     (apply 'gnus-summary-make-marking-command elem)))
12451
12452 (defun gnus-summary-make-marking-command (name mark keystroke)
12453   (let ((map (make-sparse-keymap)))
12454     (define-key gnus-summary-generic-mark-map keystroke map)
12455     (dolist (lway `((next "next" next nil "n")
12456                     (next-unread "next unread" next t "N")
12457                     (prev "previous" prev nil "p")
12458                     (prev-unread "previous unread" prev t "P")
12459                     (nomove "" nil nil ,keystroke)))
12460       (let ((func (gnus-summary-make-marking-command-1
12461                    mark (car lway) lway name)))
12462         (setq func (eval func))
12463         (define-key map (nth 4 lway) func)))))
12464
12465 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12466   `(defun ,(intern
12467             (format "gnus-summary-put-mark-as-%s%s"
12468                     name (if (eq way 'nomove)
12469                              ""
12470                            (concat "-" (symbol-name way)))))
12471      (n)
12472      ,(format
12473        "Mark the current article as %s%s.
12474 If N, the prefix, then repeat N times.
12475 If N is negative, move in reverse order.
12476 The difference between N and the actual number of articles marked is
12477 returned."
12478        name (cadr lway))
12479      (interactive "p")
12480      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12481
12482 (defun gnus-summary-generic-mark (n mark move unread)
12483   "Mark N articles with MARK."
12484   (unless (eq major-mode 'gnus-summary-mode)
12485     (error "This command can only be used in the summary buffer"))
12486   (gnus-summary-show-thread)
12487   (let ((nummove
12488          (cond
12489           ((eq move 'next) 1)
12490           ((eq move 'prev) -1)
12491           (t 0))))
12492     (if (zerop nummove)
12493         (setq n 1)
12494       (when (< n 0)
12495         (setq n (abs n)
12496               nummove (* -1 nummove))))
12497     (while (and (> n 0)
12498                 (gnus-summary-mark-article nil mark)
12499                 (zerop (gnus-summary-next-subject nummove unread t)))
12500       (setq n (1- n)))
12501     (when (/= 0 n)
12502       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12503     (gnus-summary-recenter)
12504     (gnus-summary-position-point)
12505     (gnus-set-mode-line 'summary)
12506     n))
12507
12508 (defun gnus-summary-insert-articles (articles)
12509   (when (setq articles
12510               (gnus-sorted-difference articles
12511                                       (mapcar (lambda (h)
12512                                                 (mail-header-number h))
12513                                               gnus-newsgroup-headers)))
12514     (setq gnus-newsgroup-headers
12515           (gnus-merge 'list
12516                       gnus-newsgroup-headers
12517                       (gnus-fetch-headers articles)
12518                       'gnus-article-sort-by-number))
12519     ;; Suppress duplicates?
12520     (when gnus-suppress-duplicates
12521       (gnus-dup-suppress-articles))
12522
12523     (if (and gnus-fetch-old-headers
12524              (eq gnus-headers-retrieved-by 'nov))
12525         ;; We might want to build some more threads first.
12526         (if (eq gnus-fetch-old-headers 'invisible)
12527             (gnus-build-all-threads)
12528           (gnus-build-old-threads))
12529       ;; Mark the inserted articles that are unread as unread.
12530       (setq gnus-newsgroup-unreads
12531             (gnus-sorted-nunion
12532              gnus-newsgroup-unreads
12533              (gnus-sorted-nintersection
12534               (gnus-list-of-unread-articles gnus-newsgroup-name)
12535               articles)))
12536       ;; Mark the inserted articles as selected so that the information
12537       ;; of the marks having been changed by a user may be updated when
12538       ;; exiting this group.  See `gnus-summary-update-info'.
12539       (dolist (art articles)
12540         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12541     ;; Let the Gnus agent mark articles as read.
12542     (when gnus-agent
12543       (gnus-agent-get-undownloaded-list))
12544     ;; Remove list identifiers from subject
12545     (when gnus-list-identifiers
12546       (gnus-summary-remove-list-identifiers))
12547     ;; First and last article in this newsgroup.
12548     (when gnus-newsgroup-headers
12549       (setq gnus-newsgroup-begin
12550             (mail-header-number (car gnus-newsgroup-headers))
12551             gnus-newsgroup-end
12552             (mail-header-number
12553              (gnus-last-element gnus-newsgroup-headers))))
12554     (when gnus-use-scoring
12555       (gnus-possibly-score-headers))))
12556
12557 (defun gnus-summary-insert-old-articles (&optional all)
12558   "Insert all old articles in this group.
12559 If ALL is non-nil, already read articles become readable.
12560 If ALL is a number, fetch this number of articles."
12561   (interactive "P")
12562   (prog1
12563       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12564             older len)
12565         (setq older
12566               ;; Some nntp servers lie about their active range.  When
12567               ;; this happens, the active range can be in the millions.
12568               ;; Use a compressed range to avoid creating a huge list.
12569               (gnus-range-difference (list gnus-newsgroup-active) old))
12570         (setq len (gnus-range-length older))
12571         (cond
12572          ((null older) nil)
12573          ((numberp all)
12574           (if (< all len)
12575               (let ((older-range (nreverse older)))
12576                 (setq older nil)
12577
12578                 (while (> all 0)
12579                   (let* ((r (pop older-range))
12580                          (min (if (numberp r) r (car r)))
12581                          (max (if (numberp r) r (cdr r))))
12582                     (while (and (<= min max)
12583                                 (> all 0))
12584                       (push max older)
12585                       (setq all (1- all)
12586                             max (1- max))))))
12587             (setq older (gnus-uncompress-range older))))
12588          (all
12589           (setq older (gnus-uncompress-range older)))
12590          (t
12591           (when (and (numberp gnus-large-newsgroup)
12592                    (> len gnus-large-newsgroup))
12593               (let* ((cursor-in-echo-area nil)
12594                      (initial (gnus-parameter-large-newsgroup-initial
12595                                gnus-newsgroup-name))
12596                      (input
12597                       (read-string
12598                        (format
12599                         "How many articles from %s (%s %d): "
12600                         (gnus-group-decoded-name gnus-newsgroup-name)
12601                         (if initial "max" "default")
12602                         len)
12603                        (if initial
12604                            (cons (number-to-string initial)
12605                                  0)))))
12606                 (unless (string-match "^[ \t]*$" input)
12607                   (setq all (string-to-number input))
12608                   (if (< all len)
12609                       (let ((older-range (nreverse older)))
12610                         (setq older nil)
12611
12612                         (while (> all 0)
12613                           (let* ((r (pop older-range))
12614                                  (min (if (numberp r) r (car r)))
12615                                  (max (if (numberp r) r (cdr r))))
12616                             (while (and (<= min max)
12617                                         (> all 0))
12618                               (push max older)
12619                               (setq all (1- all)
12620                                     max (1- max))))))))))
12621           (setq older (gnus-uncompress-range older))))
12622         (if (not older)
12623             (message "No old news.")
12624           (gnus-summary-insert-articles older)
12625           (gnus-summary-limit (gnus-sorted-nunion old older))))
12626     (gnus-summary-position-point)))
12627
12628 (defun gnus-summary-insert-new-articles ()
12629   "Insert all new articles in this group."
12630   (interactive)
12631   (prog1
12632       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12633             (old-high gnus-newsgroup-highest)
12634             (nnmail-fetched-sources (list t))
12635             i new)
12636         (setq gnus-newsgroup-active
12637               (gnus-copy-sequence
12638                (gnus-activate-group gnus-newsgroup-name 'scan)))
12639         (setq i (cdr gnus-newsgroup-active)
12640               gnus-newsgroup-highest i)
12641         (while (> i old-high)
12642           (push i new)
12643           (decf i))
12644         (if (not new)
12645             (message "No gnus is bad news")
12646           (gnus-summary-insert-articles new)
12647           (setq gnus-newsgroup-unreads
12648                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12649           (gnus-summary-limit (gnus-sorted-nunion old new))))
12650     (gnus-summary-position-point)))
12651
12652 ;;; Bookmark support for Gnus.
12653 (declare-function bookmark-make-record-default
12654                   "bookmark" (&optional no-file no-context posn))
12655 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12656 (declare-function bookmark-default-handler "bookmark" (bmk))
12657 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12658 (defvar bookmark-yank-point)
12659 (defvar bookmark-current-buffer)
12660
12661 (defun gnus-summary-bookmark-make-record ()
12662   "Make a bookmark entry for a Gnus summary buffer."
12663   (let (pos buf)
12664     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12665       (save-restriction              ; FIXME is it necessary to widen?
12666         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12667       (setq buf "art") ; We are recording bookmark from article buffer.
12668       (setq bookmark-yank-point (point))
12669       (setq bookmark-current-buffer (current-buffer))
12670       (gnus-article-show-summary))      ; Go back in summary buffer.
12671     ;; We are now recording bookmark from summary buffer.
12672     (unless buf (setq buf "sum"))
12673     (let* ((subject (elt (gnus-summary-article-header) 1))
12674            (grp     (car gnus-article-current))
12675            (art     (cdr gnus-article-current))
12676            (head    (gnus-summary-article-header art))
12677            (id      (mail-header-id head)))
12678       `(,subject
12679         ,@(condition-case nil
12680               (bookmark-make-record-default 'no-file 'no-context pos)
12681             (wrong-number-of-arguments
12682              (bookmark-make-record-default 'point-only)))
12683         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12684         (group . ,grp) (article . ,art)
12685         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12686
12687 ;;;###autoload
12688 (defun gnus-summary-bookmark-jump (bookmark)
12689   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12690 BOOKMARK is a bookmark name or a bookmark record."
12691   (let ((group    (bookmark-prop-get bookmark 'group))
12692         (article  (bookmark-prop-get bookmark 'article))
12693         (id       (bookmark-prop-get bookmark 'message-id))
12694         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12695     (gnus-fetch-group group (list article))
12696     (gnus-summary-insert-cached-articles)
12697     (gnus-summary-goto-article id nil 'force)
12698     ;; FIXME we have to wait article buffer is ready (only large buffer)
12699     ;; Is there a better solution to know that?
12700     ;; If we don't wait `bookmark-default-handler' will have no chance
12701     ;; to set position. However there is no error, just wrong pos.
12702     (sit-for 1)
12703     (when (string= buf "Gnus-art")
12704       (other-window 1))
12705     (bookmark-default-handler
12706      `(""
12707        (buffer . ,(current-buffer))
12708        . ,(bookmark-get-bookmark-record bookmark)))))
12709
12710 (gnus-summary-make-all-marking-commands)
12711
12712 (gnus-ems-redefine)
12713
12714 (provide 'gnus-sum)
12715
12716 (run-hooks 'gnus-sum-load-hook)
12717
12718 ;; Local Variables:
12719 ;; coding: iso-8859-1
12720 ;; End:
12721
12722 ;;; gnus-sum.el ends here