(gnus-summary-delete-article): Don't alter the registry on failed delete.
[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-article-original-subject nil)
1314 (defvar gnus-scores-exclude-files nil)
1315 (defvar gnus-page-broken nil)
1316
1317 (defvar gnus-original-article nil)
1318 (defvar gnus-article-internal-prepare-hook nil)
1319 (defvar gnus-newsgroup-process-stack nil)
1320
1321 (defvar gnus-thread-indent-array nil)
1322 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1323 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1324   "Function called to sort the articles within a thread after it has been gathered together.")
1325
1326 (defvar gnus-summary-save-parts-type-history nil)
1327 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1328
1329 ;; Avoid highlighting in kill files.
1330 (defvar gnus-summary-inhibit-highlight nil)
1331 (defvar gnus-newsgroup-selected-overlay nil)
1332 (defvar gnus-inhibit-limiting nil)
1333 (defvar gnus-newsgroup-adaptive-score-file nil)
1334 (defvar gnus-current-score-file nil)
1335 (defvar gnus-current-move-group nil)
1336 (defvar gnus-current-copy-group nil)
1337 (defvar gnus-current-crosspost-group nil)
1338 (defvar gnus-newsgroup-display nil)
1339 (defvar gnus-newsgroup-original-name nil)
1340
1341 (defvar gnus-newsgroup-dependencies nil)
1342 (defvar gnus-newsgroup-adaptive nil)
1343 (defvar gnus-summary-display-article-function nil)
1344 (defvar gnus-summary-highlight-line-function nil
1345   "Function called after highlighting a summary line.")
1346
1347 (defvar gnus-summary-line-format-alist
1348   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1349     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1350     (?s gnus-tmp-subject-or-nil ?s)
1351     (?n gnus-tmp-name ?s)
1352     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1353         ?s)
1354     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1355             gnus-tmp-from) ?s)
1356     (?F gnus-tmp-from ?s)
1357     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1358     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1359     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1360     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1361     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1362     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1363     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1364     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1365     (?L gnus-tmp-lines ?s)
1366     (?O gnus-tmp-downloaded ?c)
1367     (?I gnus-tmp-indentation ?s)
1368     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1369     (?R gnus-tmp-replied ?c)
1370     (?\[ gnus-tmp-opening-bracket ?c)
1371     (?\] gnus-tmp-closing-bracket ?c)
1372     (?\> (make-string gnus-tmp-level ? ) ?s)
1373     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1374     (?i gnus-tmp-score ?d)
1375     (?z gnus-tmp-score-char ?c)
1376     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1377     (?U gnus-tmp-unread ?c)
1378     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1379         ?s)
1380     (?t (gnus-summary-number-of-articles-in-thread
1381          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1382         ?d)
1383     (?e (gnus-summary-number-of-articles-in-thread
1384          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1385         ?c)
1386     (?u gnus-tmp-user-defined ?s)
1387     (?P (gnus-pick-line-number) ?d)
1388     (?B gnus-tmp-thread-tree-header-string ?s)
1389     (user-date (gnus-user-date
1390                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1391   "An alist of format specifications that can appear in summary lines.
1392 These are paired with what variables they correspond with, along with
1393 the type of the variable (string, integer, character, etc).")
1394
1395 (defvar gnus-summary-dummy-line-format-alist
1396   `((?S gnus-tmp-subject ?s)
1397     (?N gnus-tmp-number ?d)
1398     (?u gnus-tmp-user-defined ?s)))
1399
1400 (defvar gnus-summary-mode-line-format-alist
1401   `((?G gnus-tmp-group-name ?s)
1402     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1403     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1404     (?A gnus-tmp-article-number ?d)
1405     (?Z gnus-tmp-unread-and-unselected ?s)
1406     (?V gnus-version ?s)
1407     (?U gnus-tmp-unread-and-unticked ?d)
1408     (?S gnus-tmp-subject ?s)
1409     (?e gnus-tmp-unselected ?d)
1410     (?u gnus-tmp-user-defined ?s)
1411     (?d (length gnus-newsgroup-dormant) ?d)
1412     (?t (length gnus-newsgroup-marked) ?d)
1413     (?h (length gnus-newsgroup-spam-marked) ?d)
1414     (?r (length gnus-newsgroup-reads) ?d)
1415     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1416     (?E gnus-newsgroup-expunged-tally ?d)
1417     (?s (gnus-current-score-file-nondirectory) ?s)))
1418
1419 ;; This is here rather than in gnus-art for compilation reasons.
1420 (defvar gnus-article-mode-line-format-alist
1421   (nconc '((?w (gnus-article-wash-status) ?s)
1422            (?m (gnus-article-mime-part-status) ?s))
1423          gnus-summary-mode-line-format-alist))
1424
1425 (defvar gnus-last-search-regexp nil
1426   "Default regexp for article search command.")
1427
1428 (defvar gnus-last-shell-command nil
1429   "Default shell command on article.")
1430
1431 (defvar gnus-newsgroup-agentized nil
1432   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1433 (defvar gnus-newsgroup-begin nil)
1434 (defvar gnus-newsgroup-end nil)
1435 (defvar gnus-newsgroup-last-rmail nil)
1436 (defvar gnus-newsgroup-last-mail nil)
1437 (defvar gnus-newsgroup-last-folder nil)
1438 (defvar gnus-newsgroup-last-file nil)
1439 (defvar gnus-newsgroup-last-directory nil)
1440 (defvar gnus-newsgroup-auto-expire nil)
1441 (defvar gnus-newsgroup-active nil)
1442 (defvar gnus-newsgroup-highest nil)
1443
1444 (defvar gnus-newsgroup-data nil)
1445 (defvar gnus-newsgroup-data-reverse nil)
1446 (defvar gnus-newsgroup-limit nil)
1447 (defvar gnus-newsgroup-limits nil)
1448 (defvar gnus-summary-use-undownloaded-faces nil)
1449
1450 (defvar gnus-newsgroup-unreads nil
1451   "Sorted list of unread articles in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-unselected nil
1454   "Sorted list of unselected unread articles in the current newsgroup.")
1455
1456 (defvar gnus-newsgroup-reads nil
1457   "Alist of read articles and article marks in the current newsgroup.")
1458
1459 (defvar gnus-newsgroup-expunged-tally nil)
1460
1461 (defvar gnus-newsgroup-marked nil
1462   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1463
1464 (defvar gnus-newsgroup-spam-marked nil
1465   "List of ranges of articles that have been marked as spam.")
1466
1467 (defvar gnus-newsgroup-killed nil
1468   "List of ranges of articles that have been through the scoring process.")
1469
1470 (defvar gnus-newsgroup-cached nil
1471   "Sorted list of articles that come from the article cache.")
1472
1473 (defvar gnus-newsgroup-saved nil
1474   "List of articles that have been saved.")
1475
1476 (defvar gnus-newsgroup-kill-headers nil)
1477
1478 (defvar gnus-newsgroup-replied nil
1479   "List of articles that have been replied to in the current newsgroup.")
1480
1481 (defvar gnus-newsgroup-forwarded nil
1482   "List of articles that have been forwarded in the current newsgroup.")
1483
1484 (defvar gnus-newsgroup-recent nil
1485   "List of articles that have are recent in the current newsgroup.")
1486
1487 (defvar gnus-newsgroup-expirable nil
1488   "Sorted list of articles in the current newsgroup that can be expired.")
1489
1490 (defvar gnus-newsgroup-processable nil
1491   "List of articles in the current newsgroup that can be processed.")
1492
1493 (defvar gnus-newsgroup-downloadable nil
1494   "Sorted list of articles in the current newsgroup that can be processed.")
1495
1496 (defvar gnus-newsgroup-unfetched nil
1497   "Sorted list of articles in the current newsgroup whose headers have
1498 not been fetched into the agent.
1499
1500 This list will always be a subset of gnus-newsgroup-undownloaded.")
1501
1502 (defvar gnus-newsgroup-undownloaded nil
1503   "List of articles in the current newsgroup that haven't been downloaded.")
1504
1505 (defvar gnus-newsgroup-unsendable nil
1506   "List of articles in the current newsgroup that won't be sent.")
1507
1508 (defvar gnus-newsgroup-bookmarks nil
1509   "List of articles in the current newsgroup that have bookmarks.")
1510
1511 (defvar gnus-newsgroup-dormant nil
1512   "Sorted list of dormant articles in the current newsgroup.")
1513
1514 (defvar gnus-newsgroup-unseen nil
1515   "List of unseen articles in the current newsgroup.")
1516
1517 (defvar gnus-newsgroup-seen nil
1518   "Range of seen articles in the current newsgroup.")
1519
1520 (defvar gnus-newsgroup-articles nil
1521   "List of articles in the current newsgroup.")
1522
1523 (defvar gnus-newsgroup-scored nil
1524   "List of scored articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-headers nil
1527   "List of article headers in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-threads nil)
1530
1531 (defvar gnus-newsgroup-prepared nil
1532   "Whether the current group has been prepared properly.")
1533
1534 (defvar gnus-newsgroup-ancient nil
1535   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1536
1537 (defvar gnus-newsgroup-sparse nil)
1538
1539 (defvar gnus-current-article nil)
1540 (defvar gnus-article-current nil)
1541 (defvar gnus-current-headers nil)
1542 (defvar gnus-have-all-headers nil)
1543 (defvar gnus-last-article nil)
1544 (defvar gnus-newsgroup-history nil)
1545 (defvar gnus-newsgroup-charset nil)
1546 (defvar gnus-newsgroup-ephemeral-charset nil)
1547 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1548
1549 (defvar gnus-article-before-search nil)
1550
1551 (defvar gnus-summary-local-variables
1552   '(gnus-newsgroup-name
1553
1554     ;; Marks lists
1555     gnus-newsgroup-unreads
1556     gnus-newsgroup-unselected
1557     gnus-newsgroup-marked
1558     gnus-newsgroup-spam-marked
1559     gnus-newsgroup-reads
1560     gnus-newsgroup-saved
1561     gnus-newsgroup-replied
1562     gnus-newsgroup-forwarded
1563     gnus-newsgroup-recent
1564     gnus-newsgroup-expirable
1565     gnus-newsgroup-killed
1566     gnus-newsgroup-unseen
1567     gnus-newsgroup-seen
1568     gnus-newsgroup-cached
1569     gnus-newsgroup-downloadable
1570     gnus-newsgroup-undownloaded
1571     gnus-newsgroup-unsendable
1572
1573     gnus-newsgroup-begin gnus-newsgroup-end
1574     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1575     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1576     gnus-newsgroup-last-directory
1577     gnus-newsgroup-auto-expire
1578     gnus-newsgroup-processable
1579     gnus-newsgroup-unfetched
1580     gnus-newsgroup-articles
1581     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1582     gnus-newsgroup-headers gnus-newsgroup-threads
1583     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1584     gnus-current-article gnus-current-headers gnus-have-all-headers
1585     gnus-last-article gnus-article-internal-prepare-hook
1586     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1587     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1588     gnus-thread-expunge-below
1589     gnus-score-alist gnus-current-score-file
1590     (gnus-summary-expunge-below . global)
1591     (gnus-summary-mark-below . global)
1592     (gnus-orphan-score . global)
1593     gnus-newsgroup-active gnus-scores-exclude-files
1594     gnus-newsgroup-highest
1595     gnus-newsgroup-history gnus-newsgroup-ancient
1596     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1597     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1598     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1599     (gnus-newsgroup-expunged-tally . 0)
1600     gnus-cache-removable-articles
1601     gnus-newsgroup-data gnus-newsgroup-data-reverse
1602     gnus-newsgroup-limit gnus-newsgroup-limits
1603     gnus-newsgroup-charset gnus-newsgroup-display
1604     gnus-summary-use-undownloaded-faces)
1605   "Variables that are buffer-local to the summary buffers.")
1606
1607 (defvar gnus-newsgroup-variables nil
1608   "A list of variables that have separate values in different newsgroups.
1609 A list of newsgroup (summary buffer) local variables, or cons of
1610 variables and their default expressions to be evalled (when the default
1611 values are not nil), that should be made global while the summary buffer
1612 is active.
1613
1614 Note: The default expressions will be evaluated (using function `eval')
1615 before assignment to the local variable rather than just assigned to it.
1616 If the default expression is the symbol `global', that symbol will not
1617 be evaluated but the global value of the local variable will be used
1618 instead.
1619
1620 These variables can be used to set variables in the group parameters
1621 while still allowing them to affect operations done in other buffers.
1622 For example:
1623
1624 \(setq gnus-newsgroup-variables
1625      '(message-use-followup-to
1626        (gnus-visible-headers .
1627          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1628 ")
1629
1630 (eval-when-compile
1631   ;; Bind features so that require will believe that gnus-sum has
1632   ;; already been loaded (avoids infinite recursion)
1633   (let ((features (cons 'gnus-sum features)))
1634     (require 'gnus-art)))
1635
1636 ;; MIME stuff.
1637
1638 (defvar gnus-decode-encoded-word-methods
1639   '(mail-decode-encoded-word-string)
1640   "List of methods used to decode encoded words.
1641
1642 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1643 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1644 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1645 whose names match REGEXP.
1646
1647 For example:
1648 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1649  mail-decode-encoded-word-string
1650  (\"chinese\" . rfc1843-decode-string))")
1651
1652 (defvar gnus-decode-encoded-word-methods-cache nil)
1653
1654 (defun gnus-multi-decode-encoded-word-string (string)
1655   "Apply the functions from `gnus-encoded-word-methods' that match."
1656   (unless (and gnus-decode-encoded-word-methods-cache
1657                (eq gnus-newsgroup-name
1658                    (car gnus-decode-encoded-word-methods-cache)))
1659     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1660     (dolist (method gnus-decode-encoded-word-methods)
1661       (if (symbolp method)
1662           (nconc gnus-decode-encoded-word-methods-cache (list method))
1663         (if (and gnus-newsgroup-name
1664                  (string-match (car method) gnus-newsgroup-name))
1665             (nconc gnus-decode-encoded-word-methods-cache
1666                    (list (cdr method)))))))
1667   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1668     (setq string (funcall method string))))
1669
1670 ;; Subject simplification.
1671
1672 (defun gnus-simplify-whitespace (str)
1673   "Remove excessive whitespace from STR."
1674   ;; Multiple spaces.
1675   (while (string-match "[ \t][ \t]+" str)
1676     (setq str (concat (substring str 0 (match-beginning 0))
1677                         " "
1678                         (substring str (match-end 0)))))
1679   ;; Leading spaces.
1680   (when (string-match "^[ \t]+" str)
1681     (setq str (substring str (match-end 0))))
1682   ;; Trailing spaces.
1683   (when (string-match "[ \t]+$" str)
1684     (setq str (substring str 0 (match-beginning 0))))
1685   str)
1686
1687 (defun gnus-simplify-all-whitespace (str)
1688   "Remove all whitespace from STR."
1689   (while (string-match "[ \t\n]+" str)
1690     (setq str (replace-match "" nil nil str)))
1691   str)
1692
1693 (defsubst gnus-simplify-subject-re (subject)
1694   "Remove \"Re:\" from subject lines."
1695   (if (string-match message-subject-re-regexp subject)
1696       (substring subject (match-end 0))
1697     subject))
1698
1699 (defun gnus-simplify-subject (subject &optional re-only)
1700   "Remove `Re:' and words in parentheses.
1701 If RE-ONLY is non-nil, strip leading `Re:'s only."
1702   (let ((case-fold-search t))           ;Ignore case.
1703     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1704     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1705       (setq subject (substring subject (match-end 0))))
1706     ;; Remove uninteresting prefixes.
1707     (when (and (not re-only)
1708                gnus-simplify-ignored-prefixes
1709                (string-match gnus-simplify-ignored-prefixes subject))
1710       (setq subject (substring subject (match-end 0))))
1711     ;; Remove words in parentheses from end.
1712     (unless re-only
1713       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1714         (setq subject (substring subject 0 (match-beginning 0)))))
1715     ;; Return subject string.
1716     subject))
1717
1718 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1719 ;; all whitespace.
1720 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1721   (goto-char (point-min))
1722   (while (re-search-forward regexp nil t)
1723     (replace-match (or newtext ""))))
1724
1725 (defun gnus-simplify-buffer-fuzzy ()
1726   "Simplify string in the buffer fuzzily.
1727 The string in the accessible portion of the current buffer is simplified.
1728 It is assumed to be a single-line subject.
1729 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1730 matter is removed.  Additional things can be deleted by setting
1731 `gnus-simplify-subject-fuzzy-regexp'."
1732   (let ((case-fold-search t)
1733         (modified-tick))
1734     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1735
1736     (while (not (eq modified-tick (buffer-modified-tick)))
1737       (setq modified-tick (buffer-modified-tick))
1738       (cond
1739        ((listp gnus-simplify-subject-fuzzy-regexp)
1740         (mapc 'gnus-simplify-buffer-fuzzy-step
1741               gnus-simplify-subject-fuzzy-regexp))
1742        (gnus-simplify-subject-fuzzy-regexp
1743         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1744       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1745       (gnus-simplify-buffer-fuzzy-step
1746        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1747       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1748
1749     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1750     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1751     (gnus-simplify-buffer-fuzzy-step " $")
1752     (gnus-simplify-buffer-fuzzy-step "^ +")))
1753
1754 (defun gnus-simplify-subject-fuzzy (subject)
1755   "Simplify a subject string fuzzily.
1756 See `gnus-simplify-buffer-fuzzy' for details."
1757   (save-excursion
1758     (gnus-set-work-buffer)
1759     (let ((case-fold-search t))
1760       ;; Remove uninteresting prefixes.
1761       (when (and gnus-simplify-ignored-prefixes
1762                  (string-match gnus-simplify-ignored-prefixes subject))
1763         (setq subject (substring subject (match-end 0))))
1764       (insert subject)
1765       (inline (gnus-simplify-buffer-fuzzy))
1766       (buffer-string))))
1767
1768 (defsubst gnus-simplify-subject-fully (subject)
1769   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1770   (cond
1771    (gnus-simplify-subject-functions
1772     (gnus-map-function gnus-simplify-subject-functions subject))
1773    ((null gnus-summary-gather-subject-limit)
1774     (gnus-simplify-subject-re subject))
1775    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1776     (gnus-simplify-subject-fuzzy subject))
1777    ((numberp gnus-summary-gather-subject-limit)
1778     (truncate-string-to-width (gnus-simplify-subject-re subject)
1779                               gnus-summary-gather-subject-limit))
1780    (t
1781     subject)))
1782
1783 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1784   "Check whether two subjects are equal.
1785 If optional argument SIMPLE-FIRST is t, first argument is already
1786 simplified."
1787   (cond
1788    ((null simple-first)
1789     (equal (gnus-simplify-subject-fully s1)
1790            (gnus-simplify-subject-fully s2)))
1791    (t
1792     (equal s1
1793            (gnus-simplify-subject-fully s2)))))
1794
1795 (defun gnus-summary-bubble-group ()
1796   "Increase the score of the current group.
1797 This is a handy function to add to `gnus-summary-exit-hook' to
1798 increase the score of each group you read."
1799   (gnus-group-add-score gnus-newsgroup-name))
1800
1801 \f
1802 ;;;
1803 ;;; Gnus summary mode
1804 ;;;
1805
1806 (put 'gnus-summary-mode 'mode-class 'special)
1807
1808 (defvar gnus-article-commands-menu)
1809
1810 ;; Non-orthogonal keys
1811
1812 (gnus-define-keys gnus-summary-mode-map
1813   " " gnus-summary-next-page
1814   "\177" gnus-summary-prev-page
1815   [delete] gnus-summary-prev-page
1816   [backspace] gnus-summary-prev-page
1817   "\r" gnus-summary-scroll-up
1818   "\M-\r" gnus-summary-scroll-down
1819   "n" gnus-summary-next-unread-article
1820   "p" gnus-summary-prev-unread-article
1821   "N" gnus-summary-next-article
1822   "P" gnus-summary-prev-article
1823   "\M-\C-n" gnus-summary-next-same-subject
1824   "\M-\C-p" gnus-summary-prev-same-subject
1825   "\M-n" gnus-summary-next-unread-subject
1826   "\M-p" gnus-summary-prev-unread-subject
1827   "." gnus-summary-first-unread-article
1828   "," gnus-summary-best-unread-article
1829   "\M-s" gnus-summary-search-article-forward
1830   "\M-r" gnus-summary-search-article-backward
1831   "\M-S" gnus-summary-repeat-search-article-forward
1832   "\M-R" gnus-summary-repeat-search-article-backward
1833   "<" gnus-summary-beginning-of-article
1834   ">" gnus-summary-end-of-article
1835   "j" gnus-summary-goto-article
1836   "^" gnus-summary-refer-parent-article
1837   "\M-^" gnus-summary-refer-article
1838   "u" gnus-summary-tick-article-forward
1839   "!" gnus-summary-tick-article-forward
1840   "U" gnus-summary-tick-article-backward
1841   "d" gnus-summary-mark-as-read-forward
1842   "D" gnus-summary-mark-as-read-backward
1843   "E" gnus-summary-mark-as-expirable
1844   "\M-u" gnus-summary-clear-mark-forward
1845   "\M-U" gnus-summary-clear-mark-backward
1846   "k" gnus-summary-kill-same-subject-and-select
1847   "\C-k" gnus-summary-kill-same-subject
1848   "\M-\C-k" gnus-summary-kill-thread
1849   "\M-\C-l" gnus-summary-lower-thread
1850   "e" gnus-summary-edit-article
1851   "#" gnus-summary-mark-as-processable
1852   "\M-#" gnus-summary-unmark-as-processable
1853   "\M-\C-t" gnus-summary-toggle-threads
1854   "\M-\C-s" gnus-summary-show-thread
1855   "\M-\C-h" gnus-summary-hide-thread
1856   "\M-\C-f" gnus-summary-next-thread
1857   "\M-\C-b" gnus-summary-prev-thread
1858   [(meta down)] gnus-summary-next-thread
1859   [(meta up)] gnus-summary-prev-thread
1860   "\M-\C-u" gnus-summary-up-thread
1861   "\M-\C-d" gnus-summary-down-thread
1862   "&" gnus-summary-execute-command
1863   "c" gnus-summary-catchup-and-exit
1864   "\C-w" gnus-summary-mark-region-as-read
1865   "\C-t" gnus-summary-toggle-truncation
1866   "?" gnus-summary-mark-as-dormant
1867   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1868   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1869   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1870   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1871   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1872   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1873   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1874   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1875   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1876   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1877   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1878   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1879   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1880   "=" gnus-summary-expand-window
1881   "\C-x\C-s" gnus-summary-reselect-current-group
1882   "\M-g" gnus-summary-rescan-group
1883   "\C-c\C-r" gnus-summary-caesar-message
1884   "f" gnus-summary-followup
1885   "F" gnus-summary-followup-with-original
1886   "C" gnus-summary-cancel-article
1887   "r" gnus-summary-reply
1888   "R" gnus-summary-reply-with-original
1889   "\C-c\C-f" gnus-summary-mail-forward
1890   "o" gnus-summary-save-article
1891   "\C-o" gnus-summary-save-article-mail
1892   "|" gnus-summary-pipe-output
1893   "\M-k" gnus-summary-edit-local-kill
1894   "\M-K" gnus-summary-edit-global-kill
1895   ;; "V" gnus-version
1896   "\C-c\C-d" gnus-summary-describe-group
1897   "q" gnus-summary-exit
1898   "Q" gnus-summary-exit-no-update
1899   "\C-c\C-i" gnus-info-find-node
1900   gnus-mouse-2 gnus-mouse-pick-article
1901   [follow-link] mouse-face
1902   "m" gnus-summary-mail-other-window
1903   "a" gnus-summary-post-news
1904   "x" gnus-summary-limit-to-unread
1905   "s" gnus-summary-isearch-article
1906   [tab] gnus-summary-widget-forward
1907   "t" gnus-summary-toggle-header
1908   "g" gnus-summary-show-article
1909   "l" gnus-summary-goto-last-article
1910   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1911   "\C-d" gnus-summary-enter-digest-group
1912   "\M-\C-d" gnus-summary-read-document
1913   "\M-\C-e" gnus-summary-edit-parameters
1914   "\M-\C-a" gnus-summary-customize-parameters
1915   "\C-c\C-b" gnus-bug
1916   "*" gnus-cache-enter-article
1917   "\M-*" gnus-cache-remove-article
1918   "\M-&" gnus-summary-universal-argument
1919   "\C-l" gnus-recenter
1920   "I" gnus-summary-increase-score
1921   "L" gnus-summary-lower-score
1922   "\M-i" gnus-symbolic-argument
1923   "h" gnus-summary-select-article-buffer
1924
1925   "b" gnus-article-view-part
1926   "\M-t" gnus-summary-toggle-display-buttonized
1927
1928   "V" gnus-summary-score-map
1929   "X" gnus-uu-extract-map
1930   "S" gnus-summary-send-map)
1931
1932 ;; Sort of orthogonal keymap
1933 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1934   "t" gnus-summary-tick-article-forward
1935   "!" gnus-summary-tick-article-forward
1936   "d" gnus-summary-mark-as-read-forward
1937   "r" gnus-summary-mark-as-read-forward
1938   "c" gnus-summary-clear-mark-forward
1939   " " gnus-summary-clear-mark-forward
1940   "e" gnus-summary-mark-as-expirable
1941   "x" gnus-summary-mark-as-expirable
1942   "?" gnus-summary-mark-as-dormant
1943   "b" gnus-summary-set-bookmark
1944   "B" gnus-summary-remove-bookmark
1945   "#" gnus-summary-mark-as-processable
1946   "\M-#" gnus-summary-unmark-as-processable
1947   "S" gnus-summary-limit-include-expunged
1948   "C" gnus-summary-catchup
1949   "H" gnus-summary-catchup-to-here
1950   "h" gnus-summary-catchup-from-here
1951   "\C-c" gnus-summary-catchup-all
1952   "k" gnus-summary-kill-same-subject-and-select
1953   "K" gnus-summary-kill-same-subject
1954   "P" gnus-uu-mark-map)
1955
1956 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1957   "c" gnus-summary-clear-above
1958   "u" gnus-summary-tick-above
1959   "m" gnus-summary-mark-above
1960   "k" gnus-summary-kill-below)
1961
1962 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1963   "/" gnus-summary-limit-to-subject
1964   "n" gnus-summary-limit-to-articles
1965   "b" gnus-summary-limit-to-bodies
1966   "h" gnus-summary-limit-to-headers
1967   "w" gnus-summary-pop-limit
1968   "s" gnus-summary-limit-to-subject
1969   "a" gnus-summary-limit-to-author
1970   "u" gnus-summary-limit-to-unread
1971   "m" gnus-summary-limit-to-marks
1972   "M" gnus-summary-limit-exclude-marks
1973   "v" gnus-summary-limit-to-score
1974   "*" gnus-summary-limit-include-cached
1975   "D" gnus-summary-limit-include-dormant
1976   "T" gnus-summary-limit-include-thread
1977   "d" gnus-summary-limit-exclude-dormant
1978   "t" gnus-summary-limit-to-age
1979   "." gnus-summary-limit-to-unseen
1980   "x" gnus-summary-limit-to-extra
1981   "p" gnus-summary-limit-to-display-predicate
1982   "E" gnus-summary-limit-include-expunged
1983   "c" gnus-summary-limit-exclude-childless-dormant
1984   "C" gnus-summary-limit-mark-excluded-as-read
1985   "o" gnus-summary-insert-old-articles
1986   "N" gnus-summary-insert-new-articles
1987   "S" gnus-summary-limit-to-singletons
1988   "r" gnus-summary-limit-to-replied
1989   "R" gnus-summary-limit-to-recipient
1990   "A" gnus-summary-limit-to-address)
1991
1992 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1993   "n" gnus-summary-next-unread-article
1994   "p" gnus-summary-prev-unread-article
1995   "N" gnus-summary-next-article
1996   "P" gnus-summary-prev-article
1997   "\C-n" gnus-summary-next-same-subject
1998   "\C-p" gnus-summary-prev-same-subject
1999   "\M-n" gnus-summary-next-unread-subject
2000   "\M-p" gnus-summary-prev-unread-subject
2001   "f" gnus-summary-first-unread-article
2002   "b" gnus-summary-best-unread-article
2003   "j" gnus-summary-goto-article
2004   "g" gnus-summary-goto-subject
2005   "l" gnus-summary-goto-last-article
2006   "o" gnus-summary-pop-article)
2007
2008 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2009   "k" gnus-summary-kill-thread
2010   "E" gnus-summary-expire-thread
2011   "l" gnus-summary-lower-thread
2012   "i" gnus-summary-raise-thread
2013   "T" gnus-summary-toggle-threads
2014   "t" gnus-summary-rethread-current
2015   "^" gnus-summary-reparent-thread
2016   "\M-^" gnus-summary-reparent-children
2017   "s" gnus-summary-show-thread
2018   "S" gnus-summary-show-all-threads
2019   "h" gnus-summary-hide-thread
2020   "H" gnus-summary-hide-all-threads
2021   "n" gnus-summary-next-thread
2022   "p" gnus-summary-prev-thread
2023   "u" gnus-summary-up-thread
2024   "o" gnus-summary-top-thread
2025   "d" gnus-summary-down-thread
2026   "#" gnus-uu-mark-thread
2027   "\M-#" gnus-uu-unmark-thread)
2028
2029 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2030   "g" gnus-summary-prepare
2031   "c" gnus-summary-insert-cached-articles
2032   "d" gnus-summary-insert-dormant-articles
2033   "t" gnus-summary-insert-ticked-articles)
2034
2035 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2036   "c" gnus-summary-catchup-and-exit
2037   "C" gnus-summary-catchup-all-and-exit
2038   "E" gnus-summary-exit-no-update
2039   "Q" gnus-summary-exit
2040   "Z" gnus-summary-exit
2041   "n" gnus-summary-catchup-and-goto-next-group
2042   "p" gnus-summary-catchup-and-goto-prev-group
2043   "R" gnus-summary-reselect-current-group
2044   "G" gnus-summary-rescan-group
2045   "N" gnus-summary-next-group
2046   "s" gnus-summary-save-newsrc
2047   "P" gnus-summary-prev-group)
2048
2049 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2050   " " gnus-summary-next-page
2051   "n" gnus-summary-next-page
2052   "\177" gnus-summary-prev-page
2053   [delete] gnus-summary-prev-page
2054   "p" gnus-summary-prev-page
2055   "\r" gnus-summary-scroll-up
2056   "\M-\r" gnus-summary-scroll-down
2057   "<" gnus-summary-beginning-of-article
2058   ">" gnus-summary-end-of-article
2059   "b" gnus-summary-beginning-of-article
2060   "e" gnus-summary-end-of-article
2061   "^" gnus-summary-refer-parent-article
2062   "r" gnus-summary-refer-parent-article
2063   "C" gnus-summary-show-complete-article
2064   "D" gnus-summary-enter-digest-group
2065   "R" gnus-summary-refer-references
2066   "T" gnus-summary-refer-thread
2067   "W" gnus-warp-to-article
2068   "g" gnus-summary-show-article
2069   "s" gnus-summary-isearch-article
2070   [tab] gnus-summary-widget-forward
2071   "P" gnus-summary-print-article
2072   "S" gnus-sticky-article
2073   "M" gnus-mailing-list-insinuate
2074   "t" gnus-article-babel)
2075
2076 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2077   "b" gnus-article-add-buttons
2078   "B" gnus-article-add-buttons-to-head
2079   "o" gnus-article-treat-overstrike
2080   "e" gnus-article-emphasize
2081   "w" gnus-article-fill-cited-article
2082   "Q" gnus-article-fill-long-lines
2083   "L" gnus-article-toggle-truncate-lines
2084   "C" gnus-article-capitalize-sentences
2085   "c" gnus-article-remove-cr
2086   "q" gnus-article-de-quoted-unreadable
2087   "6" gnus-article-de-base64-unreadable
2088   "Z" gnus-article-decode-HZ
2089   "A" gnus-article-treat-ansi-sequences
2090   "h" gnus-article-wash-html
2091   "u" gnus-article-unsplit-urls
2092   "s" gnus-summary-force-verify-and-decrypt
2093   "f" gnus-article-display-x-face
2094   "l" gnus-summary-stop-page-breaking
2095   "r" gnus-summary-caesar-message
2096   "m" gnus-summary-morse-message
2097   "t" gnus-summary-toggle-header
2098   "g" gnus-treat-smiley
2099   "v" gnus-summary-verbose-headers
2100   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2101   "p" gnus-article-verify-x-pgp-sig
2102   "d" gnus-article-treat-dumbquotes
2103   "U" gnus-article-treat-non-ascii
2104   "i" gnus-summary-idna-message)
2105
2106 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2107   ;; mnemonic: deuglif*Y*
2108   "u" gnus-article-outlook-unwrap-lines
2109   "a" gnus-article-outlook-repair-attribution
2110   "c" gnus-article-outlook-rearrange-citation
2111   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2112
2113 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2114   "a" gnus-article-hide
2115   "h" gnus-article-hide-headers
2116   "b" gnus-article-hide-boring-headers
2117   "s" gnus-article-hide-signature
2118   "c" gnus-article-hide-citation
2119   "C" gnus-article-hide-citation-in-followups
2120   "l" gnus-article-hide-list-identifiers
2121   "B" gnus-article-strip-banner
2122   "P" gnus-article-hide-pem
2123   "\C-c" gnus-article-hide-citation-maybe)
2124
2125 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2126   "a" gnus-article-highlight
2127   "h" gnus-article-highlight-headers
2128   "c" gnus-article-highlight-citation
2129   "s" gnus-article-highlight-signature)
2130
2131 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2132   "f" gnus-article-treat-fold-headers
2133   "u" gnus-article-treat-unfold-headers
2134   "n" gnus-article-treat-fold-newsgroups)
2135
2136 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2137   "x" gnus-article-display-x-face
2138   "d" gnus-article-display-face
2139   "s" gnus-treat-smiley
2140   "D" gnus-article-remove-images
2141   "W" gnus-article-show-images
2142   "f" gnus-treat-from-picon
2143   "m" gnus-treat-mail-picon
2144   "n" gnus-treat-newsgroups-picon
2145   "g" gnus-treat-from-gravatar
2146   "h" gnus-treat-mail-gravatar)
2147
2148 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2149   "w" gnus-article-decode-mime-words
2150   "c" gnus-article-decode-charset
2151   "v" gnus-mime-view-all-parts
2152   "b" gnus-article-view-part)
2153
2154 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2155   "z" gnus-article-date-ut
2156   "u" gnus-article-date-ut
2157   "l" gnus-article-date-local
2158   "p" gnus-article-date-english
2159   "e" gnus-article-date-lapsed
2160   "o" gnus-article-date-original
2161   "i" gnus-article-date-iso8601
2162   "s" gnus-article-date-user)
2163
2164 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2165   "t" gnus-article-remove-trailing-blank-lines
2166   "l" gnus-article-strip-leading-blank-lines
2167   "m" gnus-article-strip-multiple-blank-lines
2168   "a" gnus-article-strip-blank-lines
2169   "A" gnus-article-strip-all-blank-lines
2170   "s" gnus-article-strip-leading-space
2171   "e" gnus-article-strip-trailing-space
2172   "w" gnus-article-remove-leading-whitespace)
2173
2174 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2175   "v" gnus-version
2176   "d" gnus-summary-describe-group
2177   "h" gnus-summary-describe-briefly
2178   "i" gnus-info-find-node)
2179
2180 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2181   "e" gnus-summary-expire-articles
2182   "\M-\C-e" gnus-summary-expire-articles-now
2183   "\177" gnus-summary-delete-article
2184   [delete] gnus-summary-delete-article
2185   [backspace] gnus-summary-delete-article
2186   "m" gnus-summary-move-article
2187   "r" gnus-summary-respool-article
2188   "w" gnus-summary-edit-article
2189   "c" gnus-summary-copy-article
2190   "B" gnus-summary-crosspost-article
2191   "q" gnus-summary-respool-query
2192   "t" gnus-summary-respool-trace
2193   "i" gnus-summary-import-article
2194   "I" gnus-summary-create-article
2195   "p" gnus-summary-article-posted-p)
2196
2197 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2198   "o" gnus-summary-save-article
2199   "m" gnus-summary-save-article-mail
2200   "F" gnus-summary-write-article-file
2201   "r" gnus-summary-save-article-rmail
2202   "f" gnus-summary-save-article-file
2203   "b" gnus-summary-save-article-body-file
2204   "B" gnus-summary-write-article-body-file
2205   "h" gnus-summary-save-article-folder
2206   "v" gnus-summary-save-article-vm
2207   "p" gnus-summary-pipe-output
2208   "P" gnus-summary-muttprint)
2209
2210 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2211   "b" gnus-summary-display-buttonized
2212   "m" gnus-summary-repair-multipart
2213   "v" gnus-article-view-part
2214   "o" gnus-article-save-part
2215   "O" gnus-article-save-part-and-strip
2216   "r" gnus-article-replace-part
2217   "d" gnus-article-delete-part
2218   "t" gnus-article-view-part-as-type
2219   "j" gnus-article-jump-to-part
2220   "c" gnus-article-copy-part
2221   "C" gnus-article-view-part-as-charset
2222   "e" gnus-article-view-part-externally
2223   "H" gnus-article-browse-html-article
2224   "E" gnus-article-encrypt-body
2225   "i" gnus-article-inline-part
2226   "|" gnus-article-pipe-part)
2227
2228 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2229   "p" gnus-summary-mark-as-processable
2230   "u" gnus-summary-unmark-as-processable
2231   "U" gnus-summary-unmark-all-processable
2232   "v" gnus-uu-mark-over
2233   "s" gnus-uu-mark-series
2234   "r" gnus-uu-mark-region
2235   "g" gnus-uu-unmark-region
2236   "R" gnus-uu-mark-by-regexp
2237   "G" gnus-uu-unmark-by-regexp
2238   "t" gnus-uu-mark-thread
2239   "T" gnus-uu-unmark-thread
2240   "a" gnus-uu-mark-all
2241   "b" gnus-uu-mark-buffer
2242   "S" gnus-uu-mark-sparse
2243   "k" gnus-summary-kill-process-mark
2244   "y" gnus-summary-yank-process-mark
2245   "w" gnus-summary-save-process-mark
2246   "i" gnus-uu-invert-processable)
2247
2248 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2249   ;;"x" gnus-uu-extract-any
2250   "m" gnus-summary-save-parts
2251   "u" gnus-uu-decode-uu
2252   "U" gnus-uu-decode-uu-and-save
2253   "s" gnus-uu-decode-unshar
2254   "S" gnus-uu-decode-unshar-and-save
2255   "o" gnus-uu-decode-save
2256   "O" gnus-uu-decode-save
2257   "b" gnus-uu-decode-binhex
2258   "B" gnus-uu-decode-binhex
2259   "Y" gnus-uu-decode-yenc
2260   "p" gnus-uu-decode-postscript
2261   "P" gnus-uu-decode-postscript-and-save)
2262
2263 (gnus-define-keys
2264     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2265   "u" gnus-uu-decode-uu-view
2266   "U" gnus-uu-decode-uu-and-save-view
2267   "s" gnus-uu-decode-unshar-view
2268   "S" gnus-uu-decode-unshar-and-save-view
2269   "o" gnus-uu-decode-save-view
2270   "O" gnus-uu-decode-save-view
2271   "b" gnus-uu-decode-binhex-view
2272   "B" gnus-uu-decode-binhex-view
2273   "p" gnus-uu-decode-postscript-view
2274   "P" gnus-uu-decode-postscript-and-save-view)
2275
2276 (defvar gnus-article-post-menu nil)
2277
2278 (defconst gnus-summary-menu-maxlen 20)
2279
2280 (defun gnus-summary-menu-split (menu)
2281   ;; If we have lots of elements, divide them into groups of 20
2282   ;; and make a pane (or submenu) for each one.
2283   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2284       (let ((menu menu) sublists next
2285             (i 1))
2286         (while menu
2287           ;; Pull off the next gnus-summary-menu-maxlen elements
2288           ;; and make them the next element of sublist.
2289           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2290           (if next
2291               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2292                       nil))
2293           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2294                                              (aref (car (last menu)) 0)) menu)
2295                                sublists))
2296           (setq i (1+ i))
2297           (setq menu next))
2298         (nreverse sublists))
2299     ;; Few elements--put them all in one pane.
2300     menu))
2301
2302 (defun gnus-summary-make-menu-bar ()
2303   (gnus-turn-off-edit-menu 'summary)
2304
2305   (unless (boundp 'gnus-summary-misc-menu)
2306
2307     (easy-menu-define
2308       gnus-summary-kill-menu gnus-summary-mode-map ""
2309       (cons
2310        "Score"
2311        (nconc
2312         (list
2313          ["Customize" gnus-score-customize t])
2314         (gnus-make-score-map 'increase)
2315         (gnus-make-score-map 'lower)
2316         '(("Mark"
2317            ["Kill below" gnus-summary-kill-below t]
2318            ["Mark above" gnus-summary-mark-above t]
2319            ["Tick above" gnus-summary-tick-above t]
2320            ["Clear above" gnus-summary-clear-above t])
2321           ["Current score" gnus-summary-current-score t]
2322           ["Set score" gnus-summary-set-score t]
2323           ["Switch current score file..." gnus-score-change-score-file t]
2324           ["Set mark below..." gnus-score-set-mark-below t]
2325           ["Set expunge below..." gnus-score-set-expunge-below t]
2326           ["Edit current score file" gnus-score-edit-current-scores t]
2327           ["Edit score file..." gnus-score-edit-file t]
2328           ["Trace score" gnus-score-find-trace t]
2329           ["Find words" gnus-score-find-favourite-words t]
2330           ["Rescore buffer" gnus-summary-rescore t]
2331           ["Increase score..." gnus-summary-increase-score t]
2332           ["Lower score..." gnus-summary-lower-score t]))))
2333
2334     ;; Define both the Article menu in the summary buffer and the
2335     ;; equivalent Commands menu in the article buffer here for
2336     ;; consistency.
2337     (let ((innards
2338            `(("Hide"
2339               ["All" gnus-article-hide t]
2340               ["Headers" gnus-article-hide-headers t]
2341               ["Signature" gnus-article-hide-signature t]
2342               ["Citation" gnus-article-hide-citation t]
2343               ["List identifiers" gnus-article-hide-list-identifiers t]
2344               ["Banner" gnus-article-strip-banner t]
2345               ["Boring headers" gnus-article-hide-boring-headers t])
2346              ("Highlight"
2347               ["All" gnus-article-highlight t]
2348               ["Headers" gnus-article-highlight-headers t]
2349               ["Signature" gnus-article-highlight-signature t]
2350               ["Citation" gnus-article-highlight-citation t])
2351              ("MIME"
2352               ["Words" gnus-article-decode-mime-words t]
2353               ["Charset" gnus-article-decode-charset t]
2354               ["QP" gnus-article-de-quoted-unreadable t]
2355               ["Base64" gnus-article-de-base64-unreadable t]
2356               ["View MIME buttons" gnus-summary-display-buttonized t]
2357               ["View all" gnus-mime-view-all-parts t]
2358               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2359               ["Encrypt body" gnus-article-encrypt-body
2360                :active (not (gnus-group-read-only-p))
2361                ,@(if (featurep 'xemacs) nil
2362                    '(:help "Encrypt the message body on disk"))]
2363               ["Extract all parts..." gnus-summary-save-parts t]
2364               ("Multipart"
2365                ["Repair multipart" gnus-summary-repair-multipart t]
2366                ["Pipe part..." gnus-article-pipe-part t]
2367                ["Inline part" gnus-article-inline-part t]
2368                ["View part as type..." gnus-article-view-part-as-type t]
2369                ["Encrypt body" gnus-article-encrypt-body
2370                 :active (not (gnus-group-read-only-p))
2371                ,@(if (featurep 'xemacs) nil
2372                    '(:help "Encrypt the message body on disk"))]
2373                ["View part externally" gnus-article-view-part-externally t]
2374                ["View HTML parts in browser" gnus-article-browse-html-article t]
2375                ["View part with charset..." gnus-article-view-part-as-charset t]
2376                ["Copy part" gnus-article-copy-part t]
2377                ["Save part..." gnus-article-save-part t]
2378                ["View part" gnus-article-view-part t]))
2379              ("Date"
2380               ["Local" gnus-article-date-local t]
2381               ["ISO8601" gnus-article-date-iso8601 t]
2382               ["UT" gnus-article-date-ut t]
2383               ["Original" gnus-article-date-original t]
2384               ["Lapsed" gnus-article-date-lapsed t]
2385               ["User-defined" gnus-article-date-user t])
2386              ("Display"
2387               ["Remove images" gnus-article-remove-images t]
2388               ["Toggle smiley" gnus-treat-smiley t]
2389               ["Show X-Face" gnus-article-display-x-face t]
2390               ["Show picons in From" gnus-treat-from-picon t]
2391               ["Show picons in mail headers" gnus-treat-mail-picon t]
2392               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2393               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2394               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2395               ("View as different encoding"
2396                ,@(gnus-summary-menu-split
2397                   (mapcar
2398                    (lambda (cs)
2399                      ;; Since easymenu under Emacs doesn't allow
2400                      ;; lambda forms for menu commands, we should
2401                      ;; provide intern'ed function symbols.
2402                      (let ((command (intern (format "\
2403 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2404                        (fset command
2405                              `(lambda ()
2406                                 (interactive)
2407                                 (let ((gnus-summary-show-article-charset-alist
2408                                        '((1 . ,cs))))
2409                                   (gnus-summary-show-article 1))))
2410                        `[,(symbol-name cs) ,command t]))
2411                    (sort (if (fboundp 'coding-system-list)
2412                              (coding-system-list)
2413                            (mapcar 'car mm-mime-mule-charset-alist))
2414                          'string<)))))
2415              ("Washing"
2416               ("Remove Blanks"
2417                ["Leading" gnus-article-strip-leading-blank-lines t]
2418                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2419                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2420                ["All of the above" gnus-article-strip-blank-lines t]
2421                ["All" gnus-article-strip-all-blank-lines t]
2422                ["Leading space" gnus-article-strip-leading-space t]
2423                ["Trailing space" gnus-article-strip-trailing-space t]
2424                ["Leading space in headers"
2425                 gnus-article-remove-leading-whitespace t])
2426               ["Overstrike" gnus-article-treat-overstrike t]
2427               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2428               ["Non-ASCII" gnus-article-treat-non-ascii t]
2429               ["Emphasis" gnus-article-emphasize t]
2430               ["Word wrap" gnus-article-fill-cited-article t]
2431               ["Fill long lines" gnus-article-fill-long-lines t]
2432               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2433               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2434               ["Remove CR" gnus-article-remove-cr t]
2435               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2436               ["Base64" gnus-article-de-base64-unreadable t]
2437               ["Rot 13" gnus-summary-caesar-message
2438                ,@(if (featurep 'xemacs) '(t)
2439                    '(:help "\"Caesar rotate\" article by 13"))]
2440               ["De-IDNA" gnus-summary-idna-message t]
2441               ["Morse decode" gnus-summary-morse-message t]
2442               ["Unix pipe..." gnus-summary-pipe-message t]
2443               ["Add buttons" gnus-article-add-buttons t]
2444               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2445               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2446               ["Verbose header" gnus-summary-verbose-headers t]
2447               ["Toggle header" gnus-summary-toggle-header t]
2448               ["Unfold headers" gnus-article-treat-unfold-headers t]
2449               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2450               ["Html" gnus-article-wash-html t]
2451               ["Unsplit URLs" gnus-article-unsplit-urls t]
2452               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2453               ["Decode HZ" gnus-article-decode-HZ t]
2454               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2455               ("(Outlook) Deuglify"
2456                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2457                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2458                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2459                ["Full (Outlook) deuglify"
2460                 gnus-article-outlook-deuglify-article t])
2461               )
2462              ("Output"
2463               ["Save in default format..." gnus-summary-save-article
2464                ,@(if (featurep 'xemacs) '(t)
2465                    '(:help "Save article using default method"))]
2466               ["Save in file..." gnus-summary-save-article-file
2467                ,@(if (featurep 'xemacs) '(t)
2468                    '(:help "Save article in file"))]
2469               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2470               ["Save in MH folder..." gnus-summary-save-article-folder t]
2471               ["Save in VM folder..." gnus-summary-save-article-vm t]
2472               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2473               ["Save body in file..." gnus-summary-save-article-body-file t]
2474               ["Pipe through a filter..." gnus-summary-pipe-output t]
2475               ["Print with Muttprint..." gnus-summary-muttprint t]
2476               ["Print" gnus-summary-print-article
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "Generate and print a PostScript image"))])
2479              ("Copy, move,... (Backend)"
2480               ,@(if (featurep 'xemacs) nil
2481                   '(:help "Copying, moving, expiring articles..."))
2482               ["Respool article..." gnus-summary-respool-article t]
2483               ["Move article..." gnus-summary-move-article
2484                (gnus-check-backend-function
2485                 'request-move-article gnus-newsgroup-name)]
2486               ["Copy article..." gnus-summary-copy-article t]
2487               ["Crosspost article..." gnus-summary-crosspost-article
2488                (gnus-check-backend-function
2489                 'request-replace-article gnus-newsgroup-name)]
2490               ["Import file..." gnus-summary-import-article
2491                (gnus-check-backend-function
2492                 'request-accept-article gnus-newsgroup-name)]
2493               ["Create article..." gnus-summary-create-article
2494                (gnus-check-backend-function
2495                 'request-accept-article gnus-newsgroup-name)]
2496               ["Check if posted" gnus-summary-article-posted-p t]
2497               ["Edit article" gnus-summary-edit-article
2498                (not (gnus-group-read-only-p))]
2499               ["Delete article" gnus-summary-delete-article
2500                (gnus-check-backend-function
2501                 'request-expire-articles gnus-newsgroup-name)]
2502               ["Query respool" gnus-summary-respool-query t]
2503               ["Trace respool" gnus-summary-respool-trace t]
2504               ["Delete expirable articles" gnus-summary-expire-articles-now
2505                (gnus-check-backend-function
2506                 'request-expire-articles gnus-newsgroup-name)])
2507              ("Extract"
2508               ["Uudecode" gnus-uu-decode-uu
2509                ,@(if (featurep 'xemacs) '(t)
2510                    '(:help "Decode uuencoded article(s)"))]
2511               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2512               ["Unshar" gnus-uu-decode-unshar t]
2513               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2514               ["Save" gnus-uu-decode-save t]
2515               ["Binhex" gnus-uu-decode-binhex t]
2516               ["Postscript" gnus-uu-decode-postscript t]
2517               ["All MIME parts" gnus-summary-save-parts t])
2518              ("Cache"
2519               ["Enter article" gnus-cache-enter-article t]
2520               ["Remove article" gnus-cache-remove-article t])
2521              ["Translate" gnus-article-babel t]
2522              ["Select article buffer" gnus-summary-select-article-buffer t]
2523              ["Make article buffer sticky" gnus-sticky-article t]
2524              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2525              ["Isearch article..." gnus-summary-isearch-article t]
2526              ["Beginning of the article" gnus-summary-beginning-of-article t]
2527              ["End of the article" gnus-summary-end-of-article t]
2528              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2529              ["Fetch referenced articles" gnus-summary-refer-references t]
2530              ["Fetch current thread" gnus-summary-refer-thread t]
2531              ["Fetch article with id..." gnus-summary-refer-article t]
2532              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2533              ["Redisplay" gnus-summary-show-article t]
2534              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2535       (easy-menu-define
2536         gnus-summary-article-menu gnus-summary-mode-map ""
2537         (cons "Article" innards))
2538
2539       (if (not (keymapp gnus-summary-article-menu))
2540           (easy-menu-define
2541             gnus-article-commands-menu gnus-article-mode-map ""
2542             (cons "Commands" innards))
2543         ;; in Emacs, don't share menu.
2544         (setq gnus-article-commands-menu
2545               (copy-keymap gnus-summary-article-menu))
2546         (define-key gnus-article-mode-map [menu-bar commands]
2547           (cons "Commands" gnus-article-commands-menu))))
2548
2549     (easy-menu-define
2550       gnus-summary-thread-menu gnus-summary-mode-map ""
2551       '("Threads"
2552         ["Find all messages in thread" gnus-summary-refer-thread t]
2553         ["Toggle threading" gnus-summary-toggle-threads t]
2554         ["Hide threads" gnus-summary-hide-all-threads t]
2555         ["Show threads" gnus-summary-show-all-threads t]
2556         ["Hide thread" gnus-summary-hide-thread t]
2557         ["Show thread" gnus-summary-show-thread t]
2558         ["Go to next thread" gnus-summary-next-thread t]
2559         ["Go to previous thread" gnus-summary-prev-thread t]
2560         ["Go down thread" gnus-summary-down-thread t]
2561         ["Go up thread" gnus-summary-up-thread t]
2562         ["Top of thread" gnus-summary-top-thread t]
2563         ["Mark thread as read" gnus-summary-kill-thread t]
2564         ["Mark thread as expired" gnus-summary-expire-thread t]
2565         ["Lower thread score" gnus-summary-lower-thread t]
2566         ["Raise thread score" gnus-summary-raise-thread t]
2567         ["Rethread current" gnus-summary-rethread-current t]))
2568
2569     (easy-menu-define
2570       gnus-summary-post-menu gnus-summary-mode-map ""
2571       `("Post"
2572         ["Send a message (mail or news)" gnus-summary-post-news
2573          ,@(if (featurep 'xemacs) '(t)
2574              '(:help "Compose a new message (mail or news)"))]
2575         ["Followup" gnus-summary-followup
2576          ,@(if (featurep 'xemacs) '(t)
2577              '(:help "Post followup to this article"))]
2578         ["Followup and yank" gnus-summary-followup-with-original
2579          ,@(if (featurep 'xemacs) '(t)
2580              '(:help "Post followup to this article, quoting its contents"))]
2581         ["Supersede article" gnus-summary-supersede-article t]
2582         ["Cancel article" gnus-summary-cancel-article
2583          ,@(if (featurep 'xemacs) '(t)
2584              '(:help "Cancel an article you posted"))]
2585         ["Reply" gnus-summary-reply t]
2586         ["Reply and yank" gnus-summary-reply-with-original t]
2587         ["Wide reply" gnus-summary-wide-reply t]
2588         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Mail a reply, quoting this article"))]
2591         ["Very wide reply" gnus-summary-very-wide-reply t]
2592         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Mail a very wide reply, quoting this article"))]
2595         ["Mail forward" gnus-summary-mail-forward t]
2596         ["Post forward" gnus-summary-post-forward t]
2597         ["Digest and mail" gnus-uu-digest-mail-forward t]
2598         ["Digest and post" gnus-uu-digest-post-forward t]
2599         ["Resend message" gnus-summary-resend-message t]
2600         ["Resend message edit" gnus-summary-resend-message-edit t]
2601         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2602         ["Send a mail" gnus-summary-mail-other-window t]
2603         ["Create a local message" gnus-summary-news-other-window t]
2604         ["Uuencode and post" gnus-uu-post-news
2605          ,@(if (featurep 'xemacs) '(t)
2606              '(:help "Post a uuencoded article"))]
2607         ["Followup via news" gnus-summary-followup-to-mail t]
2608         ["Followup via news and yank"
2609          gnus-summary-followup-to-mail-with-original t]
2610         ["Strip signature on reply"
2611          (lambda ()
2612            (interactive)
2613            (if (not (memq message-cite-function
2614                           '(message-cite-original-without-signature
2615                             message-cite-original)))
2616                ;; Stupid workaround for XEmacs not honoring :visible.
2617                (message "Can't toggle this value of `message-cite-function'")
2618              (setq message-cite-function
2619                    (if (eq message-cite-function
2620                            'message-cite-original-without-signature)
2621                        'message-cite-original
2622                      'message-cite-original-without-signature))))
2623          ;; XEmacs barfs on :visible.
2624          ,@(if (featurep 'xemacs) nil
2625              '(:visible (memq message-cite-function
2626                               '(message-cite-original-without-signature
2627                                 message-cite-original))))
2628          :style toggle
2629          :selected (eq message-cite-function
2630                        'message-cite-original-without-signature)
2631          ,@(if (featurep 'xemacs) nil
2632              '(:help "Strip signature from cited article when replying."))]
2633         ;;("Draft"
2634         ;;["Send" gnus-summary-send-draft t]
2635         ;;["Send bounced" gnus-resend-bounced-mail t])
2636         ))
2637
2638     (cond
2639      ((not (keymapp gnus-summary-post-menu))
2640       (setq gnus-article-post-menu gnus-summary-post-menu))
2641      ((not gnus-article-post-menu)
2642       ;; Don't share post menu.
2643       (setq gnus-article-post-menu
2644             (copy-keymap gnus-summary-post-menu))))
2645     (define-key gnus-article-mode-map [menu-bar post]
2646       (cons "Post" gnus-article-post-menu))
2647
2648     (easy-menu-define
2649       gnus-summary-misc-menu gnus-summary-mode-map ""
2650       `("Gnus"
2651         ("Mark Read"
2652          ["Mark as read" gnus-summary-mark-as-read-forward t]
2653          ["Mark same subject and select"
2654           gnus-summary-kill-same-subject-and-select t]
2655          ["Mark same subject" gnus-summary-kill-same-subject t]
2656          ["Catchup" gnus-summary-catchup
2657           ,@(if (featurep 'xemacs) '(t)
2658               '(:help "Mark unread articles in this group as read"))]
2659          ["Catchup all" gnus-summary-catchup-all t]
2660          ["Catchup to here" gnus-summary-catchup-to-here t]
2661          ["Catchup from here" gnus-summary-catchup-from-here t]
2662          ["Catchup region" gnus-summary-mark-region-as-read
2663           (gnus-mark-active-p)]
2664          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2665         ("Mark Various"
2666          ["Tick" gnus-summary-tick-article-forward t]
2667          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2668          ["Remove marks" gnus-summary-clear-mark-forward t]
2669          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2670          ["Set bookmark" gnus-summary-set-bookmark t]
2671          ["Remove bookmark" gnus-summary-remove-bookmark t])
2672         ("Limit to"
2673          ["Marks..." gnus-summary-limit-to-marks t]
2674          ["Subject..." gnus-summary-limit-to-subject t]
2675          ["Author..." gnus-summary-limit-to-author t]
2676          ["Recipient..." gnus-summary-limit-to-recipient t]
2677          ["Address..." gnus-summary-limit-to-address t]
2678          ["Age..." gnus-summary-limit-to-age t]
2679          ["Extra..." gnus-summary-limit-to-extra t]
2680          ["Score..." gnus-summary-limit-to-score t]
2681          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2682          ["Unread" gnus-summary-limit-to-unread t]
2683          ["Unseen" gnus-summary-limit-to-unseen t]
2684          ["Singletons" gnus-summary-limit-to-singletons t]
2685          ["Replied" gnus-summary-limit-to-replied t]
2686          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2687          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2688          ["Pop limit" gnus-summary-pop-limit t]
2689          ["Show dormant" gnus-summary-limit-include-dormant t]
2690          ["Hide childless dormant"
2691           gnus-summary-limit-exclude-childless-dormant t]
2692          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2693          ["Hide marked" gnus-summary-limit-exclude-marks t]
2694          ["Show expunged" gnus-summary-limit-include-expunged t])
2695         ("Process Mark"
2696          ["Set mark" gnus-summary-mark-as-processable t]
2697          ["Remove mark" gnus-summary-unmark-as-processable t]
2698          ["Remove all marks" gnus-summary-unmark-all-processable t]
2699          ["Invert marks" gnus-uu-invert-processable t]
2700          ["Mark above" gnus-uu-mark-over t]
2701          ["Mark series" gnus-uu-mark-series t]
2702          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2703          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2704          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2705          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2706          ["Mark all" gnus-uu-mark-all t]
2707          ["Mark buffer" gnus-uu-mark-buffer t]
2708          ["Mark sparse" gnus-uu-mark-sparse t]
2709          ["Mark thread" gnus-uu-mark-thread t]
2710          ["Unmark thread" gnus-uu-unmark-thread t]
2711          ("Process Mark Sets"
2712           ["Kill" gnus-summary-kill-process-mark t]
2713           ["Yank" gnus-summary-yank-process-mark
2714            gnus-newsgroup-process-stack]
2715           ["Save" gnus-summary-save-process-mark t]
2716           ["Run command on marked..." gnus-summary-universal-argument t]))
2717         ("Registry Marks")
2718         ("Scroll article"
2719          ["Page forward" gnus-summary-next-page
2720           ,@(if (featurep 'xemacs) '(t)
2721               '(:help "Show next page of article"))]
2722          ["Page backward" gnus-summary-prev-page
2723           ,@(if (featurep 'xemacs) '(t)
2724               '(:help "Show previous page of article"))]
2725          ["Line forward" gnus-summary-scroll-up t])
2726         ("Move"
2727          ["Next unread article" gnus-summary-next-unread-article t]
2728          ["Previous unread article" gnus-summary-prev-unread-article t]
2729          ["Next article" gnus-summary-next-article t]
2730          ["Previous article" gnus-summary-prev-article t]
2731          ["Next unread subject" gnus-summary-next-unread-subject t]
2732          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2733          ["Next article same subject" gnus-summary-next-same-subject t]
2734          ["Previous article same subject" gnus-summary-prev-same-subject t]
2735          ["First unread article" gnus-summary-first-unread-article t]
2736          ["Best unread article" gnus-summary-best-unread-article t]
2737          ["Go to subject number..." gnus-summary-goto-subject t]
2738          ["Go to article number..." gnus-summary-goto-article t]
2739          ["Go to the last article" gnus-summary-goto-last-article t]
2740          ["Pop article off history" gnus-summary-pop-article t])
2741         ("Sort"
2742          ["Sort by number" gnus-summary-sort-by-number t]
2743          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2744          ["Sort by author" gnus-summary-sort-by-author t]
2745          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2746          ["Sort by subject" gnus-summary-sort-by-subject t]
2747          ["Sort by date" gnus-summary-sort-by-date t]
2748          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2749          ["Sort by score" gnus-summary-sort-by-score t]
2750          ["Sort by lines" gnus-summary-sort-by-lines t]
2751          ["Sort by characters" gnus-summary-sort-by-chars t]
2752          ["Randomize" gnus-summary-sort-by-random t]
2753          ["Original sort" gnus-summary-sort-by-original t])
2754         ("Help"
2755          ["Describe group" gnus-summary-describe-group t]
2756          ["Read manual" gnus-info-find-node t])
2757         ("Modes"
2758          ["Pick and read" gnus-pick-mode t]
2759          ["Binary" gnus-binary-mode t])
2760         ("Regeneration"
2761          ["Regenerate" gnus-summary-prepare t]
2762          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2763          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2764          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2765          ["Toggle threading" gnus-summary-toggle-threads t])
2766         ["See old articles" gnus-summary-insert-old-articles t]
2767         ["See new articles" gnus-summary-insert-new-articles t]
2768         ["Filter articles..." gnus-summary-execute-command t]
2769         ["Run command on articles..." gnus-summary-universal-argument t]
2770         ["Search articles forward..." gnus-summary-search-article-forward t]
2771         ["Search articles backward..." gnus-summary-search-article-backward t]
2772         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2773         ["Expand window" gnus-summary-expand-window t]
2774         ["Expire expirable articles" gnus-summary-expire-articles
2775          (gnus-check-backend-function
2776           'request-expire-articles gnus-newsgroup-name)]
2777         ["Edit local kill file" gnus-summary-edit-local-kill t]
2778         ["Edit main kill file" gnus-summary-edit-global-kill t]
2779         ["Edit group parameters" gnus-summary-edit-parameters t]
2780         ["Customize group parameters" gnus-summary-customize-parameters t]
2781         ["Send a bug report" gnus-bug t]
2782         ("Exit"
2783          ["Catchup and exit" gnus-summary-catchup-and-exit
2784           ,@(if (featurep 'xemacs) '(t)
2785               '(:help "Mark unread articles in this group as read, then exit"))]
2786          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2787          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2788          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2789          ["Exit group" gnus-summary-exit
2790           ,@(if (featurep 'xemacs) '(t)
2791               '(:help "Exit current group, return to group selection mode"))]
2792          ["Exit group without updating" gnus-summary-exit-no-update t]
2793          ["Exit and goto next group" gnus-summary-next-group t]
2794          ["Exit and goto prev group" gnus-summary-prev-group t]
2795          ["Reselect group" gnus-summary-reselect-current-group t]
2796          ["Rescan group" gnus-summary-rescan-group t]
2797          ["Update dribble" gnus-summary-save-newsrc t])))
2798
2799     (gnus-run-hooks 'gnus-summary-menu-hook)))
2800
2801 (defvar gnus-summary-tool-bar-map nil)
2802
2803 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2804 ;; affect _new_ message buffers.  We might add a function that walks thru all
2805 ;; summary-mode buffers and force the update.
2806 (defun gnus-summary-tool-bar-update (&optional symbol value)
2807   "Update summary mode toolbar.
2808 Setter function for custom variables."
2809   (setq-default gnus-summary-tool-bar-map nil)
2810   (when symbol
2811     ;; When used as ":set" function:
2812     (set-default symbol value))
2813   (when (gnus-buffer-live-p gnus-summary-buffer)
2814     (with-current-buffer gnus-summary-buffer
2815       (gnus-summary-make-tool-bar))))
2816
2817 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2818                                      'gnus-summary-tool-bar-gnome
2819                                    'gnus-summary-tool-bar-retro)
2820   "Specifies the Gnus summary tool bar.
2821
2822 It can be either a list or a symbol refering to a list.  See
2823 `gmm-tool-bar-from-list' for the format of the list.  The
2824 default key map is `gnus-summary-mode-map'.
2825
2826 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2827 `gnus-summary-tool-bar-retro'."
2828   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2829                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2830                  (repeat :tag "User defined list" gmm-tool-bar-item)
2831                  (symbol))
2832   :version "23.1" ;; No Gnus
2833   :initialize 'custom-initialize-default
2834   :set 'gnus-summary-tool-bar-update
2835   :group 'gnus-summary)
2836
2837 (defcustom gnus-summary-tool-bar-gnome
2838   '((gnus-summary-post-news "mail/compose" nil)
2839     (gnus-summary-insert-new-articles "mail/inbox" nil
2840                                       :visible (or (not gnus-agent)
2841                                                    gnus-plugged))
2842     (gnus-summary-reply-with-original "mail/reply")
2843     (gnus-summary-reply "mail/reply" nil :visible nil)
2844     (gnus-summary-followup-with-original "mail/reply-all")
2845     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2846     (gnus-summary-mail-forward "mail/forward")
2847     (gnus-summary-save-article "mail/save")
2848     (gnus-summary-search-article-forward "search" nil :visible nil)
2849     (gnus-summary-print-article "print")
2850     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2851     ;; Some new commands that may need more suitable icons:
2852     (gnus-summary-save-newsrc "save" nil :visible nil)
2853     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2854     (gnus-summary-prev-article "left-arrow")
2855     (gnus-summary-next-article "right-arrow")
2856     (gnus-summary-next-page "next-page")
2857     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2858     ;;
2859     ;; Maybe some sort-by-... could be added:
2860     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2861     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2862     (gnus-summary-mark-as-expirable
2863      "delete" nil
2864      :visible (gnus-check-backend-function 'request-expire-articles
2865                                            gnus-newsgroup-name))
2866     (gnus-summary-mark-as-spam
2867      "mail/spam" t
2868      :visible (and (fboundp 'spam-group-ham-contents-p)
2869                    (spam-group-ham-contents-p gnus-newsgroup-name))
2870      :help "Mark as spam")
2871     (gnus-summary-mark-as-read-forward
2872      "mail/not-spam" nil
2873      :visible (and (fboundp 'spam-group-spam-contents-p)
2874                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2875     ;;
2876     (gnus-summary-exit "exit")
2877     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2878     (gnus-info-find-node "help"))
2879   "List of functions for the summary tool bar (GNOME style).
2880
2881 See `gmm-tool-bar-from-list' for the format of the list."
2882   :type '(repeat gmm-tool-bar-item)
2883   :version "23.1" ;; No Gnus
2884   :initialize 'custom-initialize-default
2885   :set 'gnus-summary-tool-bar-update
2886   :group 'gnus-summary)
2887
2888 (defcustom gnus-summary-tool-bar-retro
2889   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2890     (gnus-summary-next-unread-article "gnus/next-ur")
2891     (gnus-summary-post-news "gnus/post")
2892     (gnus-summary-followup-with-original "gnus/fuwo")
2893     (gnus-summary-followup "gnus/followup")
2894     (gnus-summary-reply-with-original "gnus/reply-wo")
2895     (gnus-summary-reply "gnus/reply")
2896     (gnus-summary-caesar-message "gnus/rot13")
2897     (gnus-uu-decode-uu "gnus/uu-decode")
2898     (gnus-summary-save-article-file "gnus/save-aif")
2899     (gnus-summary-save-article "gnus/save-art")
2900     (gnus-uu-post-news "gnus/uu-post")
2901     (gnus-summary-catchup "gnus/catchup")
2902     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2903     (gnus-summary-exit "gnus/exit-summ")
2904     ;; Some new command that may need more suitable icons:
2905     (gnus-summary-print-article "gnus/print" nil :visible nil)
2906     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2907     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2908     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2909     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2910     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2911     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2912     ;;
2913     (gnus-info-find-node "gnus/help" nil :visible nil))
2914   "List of functions for the summary tool bar (retro look).
2915
2916 See `gmm-tool-bar-from-list' for the format of the list."
2917   :type '(repeat gmm-tool-bar-item)
2918   :version "23.1" ;; No Gnus
2919   :initialize 'custom-initialize-default
2920   :set 'gnus-summary-tool-bar-update
2921   :group 'gnus-summary)
2922
2923 (defcustom gnus-summary-tool-bar-zap-list t
2924   "List of icon items from the global tool bar.
2925 These items are not displayed in the Gnus summary mode tool bar.
2926
2927 See `gmm-tool-bar-from-list' for the format of the list."
2928   :type 'gmm-tool-bar-zap-list
2929   :version "23.1" ;; No Gnus
2930   :initialize 'custom-initialize-default
2931   :set 'gnus-summary-tool-bar-update
2932   :group 'gnus-summary)
2933
2934 (defvar image-load-path)
2935 (defvar tool-bar-map)
2936
2937 (defun gnus-summary-make-tool-bar (&optional force)
2938   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2939 When FORCE, rebuild the tool bar."
2940   (when (and (not (featurep 'xemacs))
2941              (boundp 'tool-bar-mode)
2942              tool-bar-mode
2943              (or (not gnus-summary-tool-bar-map) force))
2944     (let* ((load-path
2945             (gmm-image-load-path-for-library "gnus"
2946                                              "mail/save.xpm"
2947                                              nil t))
2948            (image-load-path (cons (car load-path)
2949                                   (when (boundp 'image-load-path)
2950                                     image-load-path)))
2951            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2952                                         gnus-summary-tool-bar-zap-list
2953                                         'gnus-summary-mode-map)))
2954       (when map
2955         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2956         ;; uses it's value.
2957         (setq gnus-summary-tool-bar-map map))))
2958   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2959
2960 (defun gnus-score-set-default (var value)
2961   "A version of set that updates the GNU Emacs menu-bar."
2962   (set var value)
2963   ;; It is the message that forces the active status to be updated.
2964   (message ""))
2965
2966 (defun gnus-make-score-map (type)
2967   "Make a summary score map of type TYPE."
2968   (if t
2969       nil
2970     (let ((headers '(("author" "from" string)
2971                      ("subject" "subject" string)
2972                      ("article body" "body" string)
2973                      ("article head" "head" string)
2974                      ("xref" "xref" string)
2975                      ("extra header" "extra" string)
2976                      ("lines" "lines" number)
2977                      ("followups to author" "followup" string)))
2978           (types '((number ("less than" <)
2979                            ("greater than" >)
2980                            ("equal" =))
2981                    (string ("substring" s)
2982                            ("exact string" e)
2983                            ("fuzzy string" f)
2984                            ("regexp" r))))
2985           (perms '(("temporary" (current-time-string))
2986                    ("permanent" nil)
2987                    ("immediate" now)))
2988           header)
2989       (list
2990        (apply
2991         'nconc
2992         (list
2993          (if (eq type 'lower)
2994              "Lower score"
2995            "Increase score"))
2996         (let (outh)
2997           (while headers
2998             (setq header (car headers))
2999             (setq outh
3000                   (cons
3001                    (apply
3002                     'nconc
3003                     (list (car header))
3004                     (let ((ts (cdr (assoc (nth 2 header) types)))
3005                           outt)
3006                       (while ts
3007                         (setq outt
3008                               (cons
3009                                (apply
3010                                 'nconc
3011                                 (list (caar ts))
3012                                 (let ((ps perms)
3013                                       outp)
3014                                   (while ps
3015                                     (setq outp
3016                                           (cons
3017                                            (vector
3018                                             (caar ps)
3019                                             (list
3020                                              'gnus-summary-score-entry
3021                                              (nth 1 header)
3022                                              (if (or (string= (nth 1 header)
3023                                                               "head")
3024                                                      (string= (nth 1 header)
3025                                                               "body"))
3026                                                  ""
3027                                                (list 'gnus-summary-header
3028                                                      (nth 1 header)))
3029                                              (list 'quote (nth 1 (car ts)))
3030                                              (list 'gnus-score-delta-default
3031                                                    nil)
3032                                              (nth 1 (car ps))
3033                                              t)
3034                                             t)
3035                                            outp))
3036                                     (setq ps (cdr ps)))
3037                                   (list (nreverse outp))))
3038                                outt))
3039                         (setq ts (cdr ts)))
3040                       (list (nreverse outt))))
3041                    outh))
3042             (setq headers (cdr headers)))
3043           (list (nreverse outh))))))))
3044
3045
3046 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3047 (defvar bookmark-make-record-function)
3048 \f
3049
3050 (defun gnus-summary-mode (&optional group)
3051   "Major mode for reading articles.
3052
3053 All normal editing commands are switched off.
3054 \\<gnus-summary-mode-map>
3055 Each line in this buffer represents one article.  To read an
3056 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3057 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3058 respectively.
3059
3060 You can also post articles and send mail from this buffer.  To
3061 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3062 of an article, type `\\[gnus-summary-reply]'.
3063
3064 There are approx. one gazillion commands you can execute in this
3065 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3066
3067 The following commands are available:
3068
3069 \\{gnus-summary-mode-map}"
3070   (interactive)
3071   (kill-all-local-variables)
3072   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3073     (gnus-summary-make-local-variables))
3074   (gnus-summary-make-local-variables)
3075   (setq gnus-newsgroup-name group)
3076   (when (gnus-visual-p 'summary-menu 'menu)
3077     (gnus-summary-make-menu-bar)
3078     (gnus-summary-make-tool-bar))
3079   (gnus-make-thread-indent-array)
3080   (gnus-simplify-mode-line)
3081   (setq major-mode 'gnus-summary-mode)
3082   (setq mode-name "Summary")
3083   (use-local-map gnus-summary-mode-map)
3084   (buffer-disable-undo)
3085   (setq buffer-read-only t              ;Disable modification
3086         show-trailing-whitespace nil)
3087   (setq truncate-lines t)
3088   (add-to-invisibility-spec '(gnus-sum . t))
3089   (gnus-summary-set-display-table)
3090   (gnus-set-default-directory)
3091   (make-local-variable 'gnus-summary-line-format)
3092   (make-local-variable 'gnus-summary-line-format-spec)
3093   (make-local-variable 'gnus-summary-dummy-line-format)
3094   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3095   (make-local-variable 'gnus-summary-mark-positions)
3096   (gnus-make-local-hook 'pre-command-hook)
3097   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3098   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3099   (turn-on-gnus-mailing-list-mode)
3100   (mm-enable-multibyte)
3101   (set (make-local-variable 'bookmark-make-record-function)
3102        'gnus-summary-bookmark-make-record)
3103   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3104   (gnus-update-summary-mark-positions))
3105
3106 (defun gnus-summary-make-local-variables ()
3107   "Make all the local summary buffer variables."
3108   (let (global)
3109     (dolist (local gnus-summary-local-variables)
3110       (if (consp local)
3111           (progn
3112             (if (eq (cdr local) 'global)
3113                 ;; Copy the global value of the variable.
3114                 (setq global (symbol-value (car local)))
3115               ;; Use the value from the list.
3116               (setq global (eval (cdr local))))
3117             (set (make-local-variable (car local)) global))
3118         ;; Simple nil-valued local variable.
3119         (set (make-local-variable local) nil)))))
3120
3121 ;; Summary data functions.
3122
3123 (defmacro gnus-data-number (data)
3124   `(car ,data))
3125
3126 (defmacro gnus-data-set-number (data number)
3127   `(setcar ,data ,number))
3128
3129 (defmacro gnus-data-mark (data)
3130   `(nth 1 ,data))
3131
3132 (defmacro gnus-data-set-mark (data mark)
3133   `(setcar (nthcdr 1 ,data) ,mark))
3134
3135 (defmacro gnus-data-pos (data)
3136   `(nth 2 ,data))
3137
3138 (defmacro gnus-data-set-pos (data pos)
3139   `(setcar (nthcdr 2 ,data) ,pos))
3140
3141 (defmacro gnus-data-header (data)
3142   `(nth 3 ,data))
3143
3144 (defmacro gnus-data-set-header (data header)
3145   `(setf (nth 3 ,data) ,header))
3146
3147 (defmacro gnus-data-level (data)
3148   `(nth 4 ,data))
3149
3150 (defmacro gnus-data-unread-p (data)
3151   `(= (nth 1 ,data) gnus-unread-mark))
3152
3153 (defmacro gnus-data-read-p (data)
3154   `(/= (nth 1 ,data) gnus-unread-mark))
3155
3156 (defmacro gnus-data-pseudo-p (data)
3157   `(consp (nth 3 ,data)))
3158
3159 (defmacro gnus-data-find (number)
3160   `(assq ,number gnus-newsgroup-data))
3161
3162 (defmacro gnus-data-find-list (number &optional data)
3163   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3164      (memq (assq ,number bdata)
3165            bdata)))
3166
3167 (defmacro gnus-data-make (number mark pos header level)
3168   `(list ,number ,mark ,pos ,header ,level))
3169
3170 (defun gnus-data-enter (after-article number mark pos header level offset)
3171   (let ((data (gnus-data-find-list after-article)))
3172     (unless data
3173       (error "No such article: %d" after-article))
3174     (setcdr data (cons (gnus-data-make number mark pos header level)
3175                        (cdr data)))
3176     (setq gnus-newsgroup-data-reverse nil)
3177     (gnus-data-update-list (cddr data) offset)))
3178
3179 (defun gnus-data-enter-list (after-article list &optional offset)
3180   (when list
3181     (let ((data (and after-article (gnus-data-find-list after-article)))
3182           (ilist list))
3183       (if (not (or data
3184                    after-article))
3185           (let ((odata gnus-newsgroup-data))
3186             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3187             (when offset
3188               (gnus-data-update-list odata offset)))
3189         ;; Find the last element in the list to be spliced into the main
3190         ;; list.
3191         (setq list (last list))
3192         (if (not data)
3193             (progn
3194               (setcdr list gnus-newsgroup-data)
3195               (setq gnus-newsgroup-data ilist)
3196               (when offset
3197                 (gnus-data-update-list (cdr list) offset)))
3198           (setcdr list (cdr data))
3199           (setcdr data ilist)
3200           (when offset
3201             (gnus-data-update-list (cdr list) offset))))
3202       (setq gnus-newsgroup-data-reverse nil))))
3203
3204 (defun gnus-data-remove (article &optional offset)
3205   (let ((data gnus-newsgroup-data))
3206     (if (= (gnus-data-number (car data)) article)
3207         (progn
3208           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3209                 gnus-newsgroup-data-reverse nil)
3210           (when offset
3211             (gnus-data-update-list gnus-newsgroup-data offset)))
3212       (while (cdr data)
3213         (when (= (gnus-data-number (cadr data)) article)
3214           (setcdr data (cddr data))
3215           (when offset
3216             (gnus-data-update-list (cdr data) offset))
3217           (setq data nil
3218                 gnus-newsgroup-data-reverse nil))
3219         (setq data (cdr data))))))
3220
3221 (defmacro gnus-data-list (backward)
3222   `(if ,backward
3223        (or gnus-newsgroup-data-reverse
3224            (setq gnus-newsgroup-data-reverse
3225                  (reverse gnus-newsgroup-data)))
3226      gnus-newsgroup-data))
3227
3228 (defun gnus-data-update-list (data offset)
3229   "Add OFFSET to the POS of all data entries in DATA."
3230   (setq gnus-newsgroup-data-reverse nil)
3231   (while data
3232     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3233     (setq data (cdr data))))
3234
3235 (defun gnus-summary-article-pseudo-p (article)
3236   "Say whether this article is a pseudo article or not."
3237   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3238
3239 (defmacro gnus-summary-article-sparse-p (article)
3240   "Say whether this article is a sparse article or not."
3241   `(memq ,article gnus-newsgroup-sparse))
3242
3243 (defmacro gnus-summary-article-ancient-p (article)
3244   "Say whether this article is a sparse article or not."
3245   `(memq ,article gnus-newsgroup-ancient))
3246
3247 (defun gnus-article-parent-p (number)
3248   "Say whether this article is a parent or not."
3249   (let ((data (gnus-data-find-list number)))
3250     (and (cdr data)              ; There has to be an article after...
3251          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3252             (gnus-data-level (nth 1 data))))))
3253
3254 (defun gnus-article-children (number)
3255   "Return a list of all children to NUMBER."
3256   (let* ((data (gnus-data-find-list number))
3257          (level (gnus-data-level (car data)))
3258          children)
3259     (setq data (cdr data))
3260     (while (and data
3261                 (= (gnus-data-level (car data)) (1+ level)))
3262       (push (gnus-data-number (car data)) children)
3263       (setq data (cdr data)))
3264     children))
3265
3266 (defmacro gnus-summary-skip-intangible ()
3267   "If the current article is intangible, then jump to a different article."
3268   '(let ((to (get-text-property (point) 'gnus-intangible)))
3269      (and to (gnus-summary-goto-subject to))))
3270
3271 (defmacro gnus-summary-article-intangible-p ()
3272   "Say whether this article is intangible or not."
3273   '(get-text-property (point) 'gnus-intangible))
3274
3275 (defun gnus-article-read-p (article)
3276   "Say whether ARTICLE is read or not."
3277   (not (or (memq article gnus-newsgroup-marked)
3278            (memq article gnus-newsgroup-spam-marked)
3279            (memq article gnus-newsgroup-unreads)
3280            (memq article gnus-newsgroup-unselected)
3281            (memq article gnus-newsgroup-dormant))))
3282
3283 ;; Some summary mode macros.
3284
3285 (defmacro gnus-summary-article-number ()
3286   "The article number of the article on the current line.
3287 If there isn't an article number here, then we return the current
3288 article number."
3289   '(progn
3290      (gnus-summary-skip-intangible)
3291      (or (get-text-property (point) 'gnus-number)
3292          (gnus-summary-last-subject))))
3293
3294 (defmacro gnus-summary-article-header (&optional number)
3295   "Return the header of article NUMBER."
3296   `(gnus-data-header (gnus-data-find
3297                       ,(or number '(gnus-summary-article-number)))))
3298
3299 (defmacro gnus-summary-thread-level (&optional number)
3300   "Return the level of thread that starts with article NUMBER."
3301   `(if (and (eq gnus-summary-make-false-root 'dummy)
3302             (get-text-property (point) 'gnus-intangible))
3303        0
3304      (gnus-data-level (gnus-data-find
3305                        ,(or number '(gnus-summary-article-number))))))
3306
3307 (defmacro gnus-summary-article-mark (&optional number)
3308   "Return the mark of article NUMBER."
3309   `(gnus-data-mark (gnus-data-find
3310                     ,(or number '(gnus-summary-article-number)))))
3311
3312 (defmacro gnus-summary-article-pos (&optional number)
3313   "Return the position of the line of article NUMBER."
3314   `(gnus-data-pos (gnus-data-find
3315                    ,(or number '(gnus-summary-article-number)))))
3316
3317 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3318 (defmacro gnus-summary-article-subject (&optional number)
3319   "Return current subject string or nil if nothing."
3320   `(let ((headers
3321           ,(if number
3322                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3323              '(gnus-data-header (assq (gnus-summary-article-number)
3324                                       gnus-newsgroup-data)))))
3325      (and headers
3326           (vectorp headers)
3327           (mail-header-subject headers))))
3328
3329 (defmacro gnus-summary-article-score (&optional number)
3330   "Return current article score."
3331   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3332                   gnus-newsgroup-scored))
3333        gnus-summary-default-score 0))
3334
3335 (defun gnus-summary-article-children (&optional number)
3336   "Return a list of article numbers that are children of article NUMBER."
3337   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3338          (level (gnus-data-level (car data)))
3339          l children)
3340     (while (and (setq data (cdr data))
3341                 (> (setq l (gnus-data-level (car data))) level))
3342       (and (= (1+ level) l)
3343            (push (gnus-data-number (car data))
3344                  children)))
3345     (nreverse children)))
3346
3347 (defun gnus-summary-article-parent (&optional number)
3348   "Return the article number of the parent of article NUMBER."
3349   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3350                                     (gnus-data-list t)))
3351          (level (gnus-data-level (car data))))
3352     (if (zerop level)
3353         ()                              ; This is a root.
3354       ;; We search until we find an article with a level less than
3355       ;; this one.  That function has to be the parent.
3356       (while (and (setq data (cdr data))
3357                   (not (< (gnus-data-level (car data)) level))))
3358       (and data (gnus-data-number (car data))))))
3359
3360 (defun gnus-unread-mark-p (mark)
3361   "Say whether MARK is the unread mark."
3362   (= mark gnus-unread-mark))
3363
3364 (defun gnus-read-mark-p (mark)
3365   "Say whether MARK is one of the marks that mark as read.
3366 This is all marks except unread, ticked, dormant, and expirable."
3367   (not (or (= mark gnus-unread-mark)
3368            (= mark gnus-ticked-mark)
3369            (= mark gnus-spam-mark)
3370            (= mark gnus-dormant-mark)
3371            (= mark gnus-expirable-mark))))
3372
3373 (defmacro gnus-article-mark (number)
3374   "Return the MARK of article NUMBER.
3375 This macro should only be used when computing the mark the \"first\"
3376 time; i.e., when generating the summary lines.  After that,
3377 `gnus-summary-article-mark' should be used to examine the
3378 marks of articles."
3379   `(cond
3380     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3381     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3382     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3383     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3384     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3385     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3386     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3387     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3388            gnus-ancient-mark))))
3389
3390 ;; Saving hidden threads.
3391
3392 (defmacro gnus-save-hidden-threads (&rest forms)
3393   "Save hidden threads, eval FORMS, and restore the hidden threads."
3394   (let ((config (make-symbol "config")))
3395     `(let ((,config (gnus-hidden-threads-configuration)))
3396        (unwind-protect
3397            (save-excursion
3398              ,@forms)
3399          (gnus-restore-hidden-threads-configuration ,config)))))
3400 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3401 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3402
3403 (defun gnus-data-compute-positions ()
3404   "Compute the positions of all articles."
3405   (setq gnus-newsgroup-data-reverse nil)
3406   (let ((data gnus-newsgroup-data))
3407     (save-excursion
3408       (gnus-save-hidden-threads
3409         (gnus-summary-show-all-threads)
3410         (goto-char (point-min))
3411         (while data
3412           (while (get-text-property (point) 'gnus-intangible)
3413             (forward-line 1))
3414           (gnus-data-set-pos (car data) (+ (point) 3))
3415           (setq data (cdr data))
3416           (forward-line 1))))))
3417
3418 (defun gnus-hidden-threads-configuration ()
3419   "Return the current hidden threads configuration."
3420   (save-excursion
3421     (let (config)
3422       (goto-char (point-min))
3423       (while (not (eobp))
3424         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3425           (push (save-excursion (forward-line 0) (point)) config))
3426         (forward-line 1))
3427       config)))
3428
3429 (defun gnus-restore-hidden-threads-configuration (config)
3430   "Restore hidden threads configuration from CONFIG."
3431   (save-excursion
3432     (let (point (inhibit-read-only t))
3433       (while (setq point (pop config))
3434         (goto-char point)
3435         (gnus-summary-hide-thread)))))
3436
3437 ;; Various summary mode internalish functions.
3438
3439 (defun gnus-mouse-pick-article (e)
3440   (interactive "e")
3441   (mouse-set-point e)
3442   (gnus-summary-next-page nil t))
3443
3444 (defun gnus-summary-set-display-table ()
3445   "Change the display table.
3446 Odd characters have a tendency to mess
3447 up nicely formatted displays - we make all possible glyphs
3448 display only a single character."
3449
3450   ;; We start from the standard display table, if any.
3451   (let ((table (or (copy-sequence standard-display-table)
3452                    (make-display-table)))
3453         (i 32))
3454     ;; Nix out all the control chars...
3455     (while (>= (setq i (1- i)) 0)
3456       (gnus-put-display-table i [??] table))
3457    ;; ... but not newline and cr, of course.  (cr is necessary for the
3458     ;; selective display).
3459     (gnus-put-display-table ?\n nil table)
3460     (gnus-put-display-table ?\r nil table)
3461     ;; We keep TAB as well.
3462     (gnus-put-display-table ?\t nil table)
3463     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3464     ;; Emacs 23 (unicode), that are not set already.
3465     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3466                  160
3467                256)))
3468       (while (>= (setq i (1- i)) 127)
3469         ;; Only modify if the entry is nil.
3470         (unless (gnus-get-display-table i table)
3471           (gnus-put-display-table i [??] table))))
3472     (setq buffer-display-table table)))
3473
3474 (defun gnus-summary-set-article-display-arrow (pos)
3475   "Update the overlay arrow to point to line at position POS."
3476   (when gnus-summary-display-arrow
3477     (make-local-variable 'overlay-arrow-position)
3478     (make-local-variable 'overlay-arrow-string)
3479     (save-excursion
3480       (goto-char pos)
3481       (beginning-of-line)
3482       (unless overlay-arrow-position
3483         (setq overlay-arrow-position (make-marker)))
3484       (setq overlay-arrow-string "=>"
3485             overlay-arrow-position (set-marker overlay-arrow-position
3486                                                (point)
3487                                                (current-buffer))))))
3488
3489 (defun gnus-summary-setup-buffer (group)
3490   "Initialize summary buffer."
3491   (let ((buffer (gnus-summary-buffer-name group))
3492         (dead-name (concat "*Dead Summary "
3493                            (gnus-group-decoded-name group) "*")))
3494     ;; If a dead summary buffer exists, we kill it.
3495     (when (gnus-buffer-live-p dead-name)
3496       (gnus-kill-buffer dead-name))
3497     (if (get-buffer buffer)
3498         (progn
3499           (set-buffer buffer)
3500           (setq gnus-summary-buffer (current-buffer))
3501           (not gnus-newsgroup-prepared))
3502       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3503       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3504       (gnus-summary-mode group)
3505       (when (gnus-group-quit-config group)
3506         (set (make-local-variable 'gnus-single-article-buffer) nil))
3507       (make-local-variable 'gnus-article-buffer)
3508       (make-local-variable 'gnus-article-current)
3509       (make-local-variable 'gnus-original-article-buffer)
3510       (setq gnus-newsgroup-name group)
3511       ;; Set any local variables in the group parameters.
3512       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3513       t)))
3514
3515 (defun gnus-set-global-variables ()
3516   "Set the global equivalents of the buffer-local variables.
3517 They are set to the latest values they had.  These reflect the summary
3518 buffer that was in action when the last article was fetched."
3519   (when (eq major-mode 'gnus-summary-mode)
3520     (setq gnus-summary-buffer (current-buffer))
3521     (let ((name gnus-newsgroup-name)
3522           (marked gnus-newsgroup-marked)
3523           (spam gnus-newsgroup-spam-marked)
3524           (unread gnus-newsgroup-unreads)
3525           (headers gnus-current-headers)
3526           (data gnus-newsgroup-data)
3527           (summary gnus-summary-buffer)
3528           (article-buffer gnus-article-buffer)
3529           (original gnus-original-article-buffer)
3530           (gac gnus-article-current)
3531           (reffed gnus-reffed-article-number)
3532           (score-file gnus-current-score-file)
3533           (default-charset gnus-newsgroup-charset)
3534           vlist)
3535       (let ((locals gnus-newsgroup-variables))
3536         (while locals
3537           (if (consp (car locals))
3538               (push (eval (caar locals)) vlist)
3539             (push (eval (car locals)) vlist))
3540           (setq locals (cdr locals)))
3541         (setq vlist (nreverse vlist)))
3542       (with-current-buffer gnus-group-buffer
3543         (setq gnus-newsgroup-name name
3544               gnus-newsgroup-marked marked
3545               gnus-newsgroup-spam-marked spam
3546               gnus-newsgroup-unreads unread
3547               gnus-current-headers headers
3548               gnus-newsgroup-data data
3549               gnus-article-current gac
3550               gnus-summary-buffer summary
3551               gnus-article-buffer article-buffer
3552               gnus-original-article-buffer original
3553               gnus-reffed-article-number reffed
3554               gnus-current-score-file score-file
3555               gnus-newsgroup-charset default-charset)
3556         (let ((locals gnus-newsgroup-variables))
3557           (while locals
3558             (if (consp (car locals))
3559                 (set (caar locals) (pop vlist))
3560               (set (car locals) (pop vlist)))
3561             (setq locals (cdr locals))))
3562         ;; The article buffer also has local variables.
3563         (when (gnus-buffer-live-p gnus-article-buffer)
3564           (set-buffer gnus-article-buffer)
3565           (setq gnus-summary-buffer summary))))))
3566
3567 (defun gnus-summary-article-unread-p (article)
3568   "Say whether ARTICLE is unread or not."
3569   (memq article gnus-newsgroup-unreads))
3570
3571 (defun gnus-summary-first-article-p (&optional article)
3572   "Return whether ARTICLE is the first article in the buffer."
3573   (if (not (setq article (or article (gnus-summary-article-number))))
3574       nil
3575     (eq article (caar gnus-newsgroup-data))))
3576
3577 (defun gnus-summary-last-article-p (&optional article)
3578   "Return whether ARTICLE is the last article in the buffer."
3579   (if (not (setq article (or article (gnus-summary-article-number))))
3580       ;; All non-existent numbers are the last article.  :-)
3581       t
3582     (not (cdr (gnus-data-find-list article)))))
3583
3584 (defun gnus-make-thread-indent-array (&optional n)
3585   (when (or n
3586             (progn (setq n 200) nil)
3587             (null gnus-thread-indent-array)
3588             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3589     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3590           gnus-thread-indent-array-level gnus-thread-indent-level)
3591     (while (>= n 0)
3592       (aset gnus-thread-indent-array n
3593             (make-string (* n gnus-thread-indent-level) ? ))
3594       (setq n (1- n)))))
3595
3596 (defun gnus-update-summary-mark-positions ()
3597   "Compute where the summary marks are to go."
3598   (save-excursion
3599     (when (gnus-buffer-exists-p gnus-summary-buffer)
3600       (set-buffer gnus-summary-buffer))
3601     (let ((spec gnus-summary-line-format-spec)
3602           pos)
3603       (save-excursion
3604         (gnus-set-work-buffer)
3605         (let ((gnus-tmp-unread ?Z)
3606               (gnus-replied-mark ?Z)
3607               (gnus-score-below-mark ?Z)
3608               (gnus-score-over-mark ?Z)
3609               (gnus-undownloaded-mark ?Z)
3610               (gnus-summary-line-format-spec spec)
3611               (gnus-newsgroup-downloadable '(0))
3612               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3613               case-fold-search ignores)
3614           ;; Here, all marks are bound to Z.
3615           (gnus-summary-insert-line header
3616                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3617           (goto-char (point-min))
3618           ;; Memorize the positions of the same characters as dummy marks.
3619           (while (re-search-forward "[A-D]" nil t)
3620             (push (point) ignores))
3621           (erase-buffer)
3622           ;; We use A-D as dummy marks in order to know column positions
3623           ;; where marks should be inserted.
3624           (setq gnus-tmp-unread ?A
3625                 gnus-replied-mark ?B
3626                 gnus-score-below-mark ?C
3627                 gnus-score-over-mark ?C
3628                 gnus-undownloaded-mark ?D)
3629           (gnus-summary-insert-line header
3630                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3631           ;; Ignore characters which aren't dummy marks.
3632           (dolist (p ignores)
3633             (delete-region (goto-char (1- p)) p)
3634             (insert ?Z))
3635           (goto-char (point-min))
3636           (setq pos (list (cons 'unread
3637                                 (and (search-forward "A" nil t)
3638                                      (- (point) (point-min) 1)))))
3639           (goto-char (point-min))
3640           (push (cons 'replied (and (search-forward "B" nil t)
3641                                     (- (point) (point-min) 1)))
3642                 pos)
3643           (goto-char (point-min))
3644           (push (cons 'score (and (search-forward "C" nil t)
3645                                   (- (point) (point-min) 1)))
3646                 pos)
3647           (goto-char (point-min))
3648           (push (cons 'download (and (search-forward "D" nil t)
3649                                      (- (point) (point-min) 1)))
3650                 pos)))
3651       (setq gnus-summary-mark-positions pos))))
3652
3653 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3654   "Insert a dummy root in the summary buffer."
3655   (beginning-of-line)
3656   (gnus-add-text-properties
3657    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3658    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3659
3660 (defun gnus-summary-extract-address-component (from)
3661   (or (car (funcall gnus-extract-address-components from))
3662       from))
3663
3664 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3665   (let ((mail-parse-charset gnus-newsgroup-charset)
3666         (ignored-from-addresses (gnus-ignored-from-addresses))
3667         ; Is it really necessary to do this next part for each summary line?
3668         ; Luckily, doesn't seem to slow things down much.
3669         (mail-parse-ignored-charsets
3670          (with-current-buffer gnus-summary-buffer
3671            gnus-newsgroup-ignored-charsets)))
3672     (or
3673      (and ignored-from-addresses
3674           (string-match ignored-from-addresses gnus-tmp-from)
3675           (let ((extra-headers (mail-header-extra header))
3676                 to
3677                 newsgroups)
3678             (cond
3679              ((setq to (cdr (assq 'To extra-headers)))
3680               (concat gnus-summary-to-prefix
3681                       (inline
3682                         (gnus-summary-extract-address-component
3683                          (funcall gnus-decode-encoded-address-function to)))))
3684              ((setq newsgroups
3685                     (or
3686                      (cdr (assq 'Newsgroups extra-headers))
3687                      (and
3688                       (memq 'Newsgroups gnus-extra-headers)
3689                       (eq (car (gnus-find-method-for-group
3690                                 gnus-newsgroup-name)) 'nntp)
3691                       (gnus-group-real-name gnus-newsgroup-name))))
3692               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3693      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3694
3695 (defun gnus-summary-insert-line (gnus-tmp-header
3696                                  gnus-tmp-level gnus-tmp-current
3697                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3698                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3699                                  &optional gnus-tmp-dummy gnus-tmp-score
3700                                  gnus-tmp-process)
3701   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3702       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3703                                           gnus-tmp-level)))
3704   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3705          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3706          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3707          (gnus-tmp-score-char
3708           (if (or (null gnus-summary-default-score)
3709                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3710                       gnus-summary-zcore-fuzz))
3711               ?                         ;Whitespace
3712             (if (< gnus-tmp-score gnus-summary-default-score)
3713                 gnus-score-below-mark gnus-score-over-mark)))
3714          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3715          (gnus-tmp-replied
3716           (cond (gnus-tmp-process gnus-process-mark)
3717                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3718                  gnus-cached-mark)
3719                 (gnus-tmp-replied gnus-replied-mark)
3720                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3721                  gnus-forwarded-mark)
3722                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3723                  gnus-saved-mark)
3724                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3725                  gnus-recent-mark)
3726                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3727                  gnus-unseen-mark)
3728                 (t gnus-no-mark)))
3729          (gnus-tmp-downloaded
3730           (cond (undownloaded
3731                  gnus-undownloaded-mark)
3732                 (gnus-newsgroup-agentized
3733                  gnus-downloaded-mark)
3734                 (t
3735                  gnus-no-mark)))
3736          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3737          (gnus-tmp-name
3738           (cond
3739            ((string-match "<[^>]+> *$" gnus-tmp-from)
3740             (let ((beg (match-beginning 0)))
3741               (or (and (string-match "^\".+\"" gnus-tmp-from)
3742                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3743                   (substring gnus-tmp-from 0 beg))))
3744            ((string-match "(.+)" gnus-tmp-from)
3745             (substring gnus-tmp-from
3746                        (1+ (match-beginning 0)) (1- (match-end 0))))
3747            (t gnus-tmp-from)))
3748          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3749          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3750          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3751          (inhibit-read-only t))
3752     (when (string= gnus-tmp-name "")
3753       (setq gnus-tmp-name gnus-tmp-from))
3754     (unless (numberp gnus-tmp-lines)
3755       (setq gnus-tmp-lines -1))
3756     (if (= gnus-tmp-lines -1)
3757         (setq gnus-tmp-lines "?")
3758       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3759     (condition-case ()
3760         (gnus-put-text-property
3761          (point)
3762          (progn (eval gnus-summary-line-format-spec) (point))
3763          'gnus-number gnus-tmp-number)
3764       (error (gnus-message 5 "Error updating the summary line")))
3765     (when (gnus-visual-p 'summary-highlight 'highlight)
3766       (forward-line -1)
3767       (gnus-summary-highlight-line)
3768       (gnus-run-hooks 'gnus-summary-update-hook)
3769       (forward-line 1))))
3770
3771 (defun gnus-summary-update-line (&optional dont-update)
3772   "Update summary line after change."
3773   (when (and gnus-summary-default-score
3774              (not gnus-summary-inhibit-highlight))
3775     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3776            (article (gnus-summary-article-number))
3777            (score (gnus-summary-article-score article)))
3778       (unless dont-update
3779         (if (and gnus-summary-mark-below
3780                  (< (gnus-summary-article-score)
3781                     gnus-summary-mark-below))
3782             ;; This article has a low score, so we mark it as read.
3783             (when (memq article gnus-newsgroup-unreads)
3784               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3785           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3786             ;; This article was previously marked as read on account
3787             ;; of a low score, but now it has risen, so we mark it as
3788             ;; unread.
3789             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3790         (gnus-summary-update-mark
3791          (if (or (null gnus-summary-default-score)
3792                  (<= (abs (- score gnus-summary-default-score))
3793                      gnus-summary-zcore-fuzz))
3794              ?                          ;Whitespace
3795            (if (< score gnus-summary-default-score)
3796                gnus-score-below-mark gnus-score-over-mark))
3797          'score))
3798       ;; Do visual highlighting.
3799       (when (gnus-visual-p 'summary-highlight 'highlight)
3800         (gnus-summary-highlight-line)
3801         (gnus-run-hooks 'gnus-summary-update-hook)))))
3802
3803 (defvar gnus-tmp-new-adopts nil)
3804
3805 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3806   "Return the number of articles in THREAD.
3807 This may be 0 in some cases -- if none of the articles in
3808 the thread are to be displayed."
3809   (let* ((number
3810          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3811           (cond
3812            ((not (listp thread))
3813             1)
3814            ((and (consp thread) (cdr thread))
3815             (apply
3816              '+ 1 (mapcar
3817                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3818            ((null thread)
3819             1)
3820            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3821             1)
3822            (t 0))))
3823     (when (and level (zerop level) gnus-tmp-new-adopts)
3824       (incf number
3825             (apply '+ (mapcar
3826                        'gnus-summary-number-of-articles-in-thread
3827                        gnus-tmp-new-adopts))))
3828     (if char
3829         (if (> number 1) gnus-not-empty-thread-mark
3830           gnus-empty-thread-mark)
3831       number)))
3832
3833 (defsubst gnus-summary-line-message-size (head)
3834   "Return pretty-printed version of message size.
3835 This function is intended to be used in
3836 `gnus-summary-line-format-alist'."
3837   (let ((c (or (mail-header-chars head) -1)))
3838     (cond ((< c 0) "n/a")               ; chars not available
3839           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3840           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3841           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3842           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3843
3844
3845 (defun gnus-summary-set-local-parameters (group)
3846   "Go through the local params of GROUP and set all variable specs in that list."
3847   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3848                                         ; really variables.
3849     (dolist (elem (gnus-group-find-parameter group))
3850       (and (consp elem)                 ; Has to be a cons.
3851            (consp (cdr elem))           ; The cdr has to be a list.
3852            (symbolp (car elem))         ; Has to be a symbol in there.
3853            (not (memq (car elem) vars))
3854            (ignore-errors
3855              (push (car elem) vars)
3856              ;; Variables like `gnus-show-threads' that are globally
3857              ;; bound, if used as group parameters, need to get to be
3858              ;; buffer-local, whereas just parameters like `gcc-self',
3859              ;; `timestamp', etc. should not be bound as variables.
3860              (if (boundp (car elem))
3861                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3862                (eval (nth 1 elem))))))))
3863
3864 (defun gnus-summary-read-group (group &optional show-all no-article
3865                                       kill-buffer no-display backward
3866                                       select-articles)
3867   "Start reading news in newsgroup GROUP.
3868 If SHOW-ALL is non-nil, already read articles are also listed.
3869 If NO-ARTICLE is non-nil, no article is selected initially.
3870 If NO-DISPLAY, don't generate a summary buffer."
3871   (let (result)
3872     (while (and group
3873                 (null (setq result
3874                             (let ((gnus-auto-select-next nil))
3875                               (or (gnus-summary-read-group-1
3876                                    group show-all no-article
3877                                    kill-buffer no-display
3878                                    select-articles)
3879                                   (setq show-all nil
3880                                         select-articles nil)))))
3881                 (eq gnus-auto-select-next 'quietly))
3882       (set-buffer gnus-group-buffer)
3883       ;; The entry function called above goes to the next
3884       ;; group automatically, so we go two groups back
3885       ;; if we are searching for the previous group.
3886       (when backward
3887         (gnus-group-prev-unread-group 2))
3888       (if (not (equal group (gnus-group-group-name)))
3889           (setq group (gnus-group-group-name))
3890         (setq group nil)))
3891     result))
3892
3893 (defun gnus-summary-read-group-1 (group show-all no-article
3894                                         kill-buffer no-display
3895                                         &optional select-articles)
3896   ;; Killed foreign groups can't be entered.
3897   ;;  (when (and (not (gnus-group-native-p group))
3898   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3899   ;;    (error "Dead non-native groups can't be entered"))
3900   (gnus-message 5 "Retrieving newsgroup: %s..."
3901                 (gnus-group-decoded-name group))
3902   (let* ((new-group (gnus-summary-setup-buffer group))
3903          (quit-config (gnus-group-quit-config group))
3904          (did-select (and new-group (gnus-select-newsgroup
3905                                      group show-all select-articles))))
3906     (cond
3907      ;; This summary buffer exists already, so we just select it.
3908      ((not new-group)
3909       (gnus-set-global-variables)
3910       (when kill-buffer
3911         (gnus-kill-or-deaden-summary kill-buffer))
3912       (gnus-configure-windows 'summary 'force)
3913       (gnus-set-mode-line 'summary)
3914       (gnus-summary-position-point)
3915       (message "")
3916       t)
3917      ;; We couldn't select this group.
3918      ((null did-select)
3919       (when (and (eq major-mode 'gnus-summary-mode)
3920                  (not (equal (current-buffer) kill-buffer)))
3921         (kill-buffer (current-buffer))
3922         (if (not quit-config)
3923             (progn
3924               ;; Update the info -- marks might need to be removed,
3925               ;; for instance.
3926               (gnus-summary-update-info)
3927               (set-buffer gnus-group-buffer)
3928               (gnus-group-jump-to-group group)
3929               (gnus-group-next-unread-group 1))
3930           (gnus-handle-ephemeral-exit quit-config)))
3931       (let ((grpinfo (gnus-get-info group)))
3932         (if (null (gnus-info-read grpinfo))
3933             (gnus-message 3 "Group %s contains no messages"
3934                           (gnus-group-decoded-name group))
3935           (gnus-message 3 "Can't select group")))
3936       nil)
3937      ;; The user did a `C-g' while prompting for number of articles,
3938      ;; so we exit this group.
3939      ((eq did-select 'quit)
3940       (and (eq major-mode 'gnus-summary-mode)
3941            (not (equal (current-buffer) kill-buffer))
3942            (kill-buffer (current-buffer)))
3943       (when kill-buffer
3944         (gnus-kill-or-deaden-summary kill-buffer))
3945       (if (not quit-config)
3946           (progn
3947             (set-buffer gnus-group-buffer)
3948             (gnus-group-jump-to-group group)
3949             (gnus-configure-windows 'group 'force))
3950         (gnus-handle-ephemeral-exit quit-config))
3951       ;; Finally signal the quit.
3952       (signal 'quit nil))
3953      ;; The group was successfully selected.
3954      (t
3955       (gnus-set-global-variables)
3956       ;; Save the active value in effect when the group was entered.
3957       (setq gnus-newsgroup-active
3958             (gnus-copy-sequence
3959              (gnus-active gnus-newsgroup-name)))
3960       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3961       ;; You can change the summary buffer in some way with this hook.
3962       (gnus-run-hooks 'gnus-select-group-hook)
3963       (when (memq 'summary (gnus-update-format-specifications
3964                             nil 'summary 'summary-mode 'summary-dummy))
3965         ;; The format specification for the summary line was updated,
3966         ;; so we need to update the mark positions as well.
3967         (gnus-update-summary-mark-positions))
3968       ;; Do score processing.
3969       (when gnus-use-scoring
3970         (gnus-possibly-score-headers))
3971       ;; Check whether to fill in the gaps in the threads.
3972       (when gnus-build-sparse-threads
3973         (gnus-build-sparse-threads))
3974       ;; Find the initial limit.
3975       (if show-all
3976           (let ((gnus-newsgroup-dormant nil))
3977             (gnus-summary-initial-limit show-all))
3978         (gnus-summary-initial-limit show-all))
3979       ;; Generate the summary buffer.
3980       (unless no-display
3981         (gnus-summary-prepare))
3982       (when gnus-use-trees
3983         (gnus-tree-open group)
3984         (setq gnus-summary-highlight-line-function
3985               'gnus-tree-highlight-article))
3986       ;; If the summary buffer is empty, but there are some low-scored
3987       ;; articles or some excluded dormants, we include these in the
3988       ;; buffer.
3989       (when (and (zerop (buffer-size))
3990                  (not no-display))
3991         (cond (gnus-newsgroup-dormant
3992                (gnus-summary-limit-include-dormant))
3993               ((and gnus-newsgroup-scored show-all)
3994                (gnus-summary-limit-include-expunged t))))
3995       ;; Function `gnus-apply-kill-file' must be called in this hook.
3996       (gnus-run-hooks 'gnus-apply-kill-hook)
3997       (if (and (zerop (buffer-size))
3998                (not no-display))
3999           (progn
4000             ;; This newsgroup is empty.
4001             (gnus-summary-catchup-and-exit nil t)
4002             (gnus-message 6 "No unread news")
4003             (when kill-buffer
4004               (gnus-kill-or-deaden-summary kill-buffer))
4005             ;; Return nil from this function.
4006             nil)
4007         ;; Hide conversation thread subtrees.  We cannot do this in
4008         ;; gnus-summary-prepare-hook since kill processing may not
4009         ;; work with hidden articles.
4010         (gnus-summary-maybe-hide-threads)
4011         (when kill-buffer
4012           (gnus-kill-or-deaden-summary kill-buffer))
4013         (gnus-summary-auto-select-subject)
4014         ;; Show first unread article if requested.
4015         (if (and (not no-article)
4016                  (not no-display)
4017                  gnus-newsgroup-unreads
4018                  gnus-auto-select-first)
4019             (progn
4020               (gnus-configure-windows 'summary)
4021               (let ((art (gnus-summary-article-number)))
4022                 (unless (and (not gnus-plugged)
4023                              (or (memq art gnus-newsgroup-undownloaded)
4024                                  (memq art gnus-newsgroup-downloadable)))
4025                   (gnus-summary-goto-article art))))
4026           ;; Don't select any articles.
4027           (gnus-summary-position-point)
4028           (gnus-configure-windows 'summary 'force)
4029           (gnus-set-mode-line 'summary))
4030         (when (and gnus-auto-center-group
4031                    (get-buffer-window gnus-group-buffer t))
4032           ;; Gotta use windows, because recenter does weird stuff if
4033           ;; the current buffer ain't the displayed window.
4034           (let ((owin (selected-window)))
4035             (select-window (get-buffer-window gnus-group-buffer t))
4036             (when (gnus-group-goto-group group)
4037               (recenter))
4038             (select-window owin)))
4039         ;; Mark this buffer as "prepared".
4040         (setq gnus-newsgroup-prepared t)
4041         (gnus-run-hooks 'gnus-summary-prepared-hook)
4042         (unless (gnus-ephemeral-group-p group)
4043           (gnus-group-update-group group))
4044         t)))))
4045
4046 (defun gnus-summary-auto-select-subject ()
4047   "Select the subject line on initial group entry."
4048   (goto-char (point-min))
4049   (cond
4050    ((eq gnus-auto-select-subject 'best)
4051     (gnus-summary-best-unread-subject))
4052    ((eq gnus-auto-select-subject 'unread)
4053     (gnus-summary-first-unread-subject))
4054    ((eq gnus-auto-select-subject 'unseen)
4055     (gnus-summary-first-unseen-subject))
4056    ((eq gnus-auto-select-subject 'unseen-or-unread)
4057     (gnus-summary-first-unseen-or-unread-subject))
4058    ((eq gnus-auto-select-subject 'first)
4059     ;; Do nothing.
4060     )
4061    ((functionp gnus-auto-select-subject)
4062     (funcall gnus-auto-select-subject))))
4063
4064 (defun gnus-summary-prepare ()
4065   "Generate the summary buffer."
4066   (interactive)
4067   (let ((inhibit-read-only t))
4068     (erase-buffer)
4069     (setq gnus-newsgroup-data nil
4070           gnus-newsgroup-data-reverse nil)
4071     (gnus-run-hooks 'gnus-summary-generate-hook)
4072     ;; Generate the buffer, either with threads or without.
4073     (when gnus-newsgroup-headers
4074       (gnus-summary-prepare-threads
4075        (if gnus-show-threads
4076            (gnus-sort-gathered-threads
4077             (funcall gnus-summary-thread-gathering-function
4078                      (gnus-sort-threads
4079                       (gnus-cut-threads (gnus-make-threads)))))
4080          ;; Unthreaded display.
4081          (gnus-sort-articles gnus-newsgroup-headers))))
4082     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4083     ;; Call hooks for modifying summary buffer.
4084     (goto-char (point-min))
4085     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4086
4087 (defsubst gnus-general-simplify-subject (subject)
4088   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4089   (setq subject
4090         (cond
4091          ;; Truncate the subject.
4092          (gnus-simplify-subject-functions
4093           (gnus-map-function gnus-simplify-subject-functions subject))
4094          ((numberp gnus-summary-gather-subject-limit)
4095           (setq subject (gnus-simplify-subject-re subject))
4096           (if (> (length subject) gnus-summary-gather-subject-limit)
4097               (substring subject 0 gnus-summary-gather-subject-limit)
4098             subject))
4099          ;; Fuzzily simplify it.
4100          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4101           (gnus-simplify-subject-fuzzy subject))
4102          ;; Just remove the leading "Re:".
4103          (t
4104           (gnus-simplify-subject-re subject))))
4105
4106   (if (and gnus-summary-gather-exclude-subject
4107            (string-match gnus-summary-gather-exclude-subject subject))
4108       nil                         ; This article shouldn't be gathered
4109     subject))
4110
4111 (defun gnus-summary-simplify-subject-query ()
4112   "Query where the respool algorithm would put this article."
4113   (interactive)
4114   (gnus-summary-select-article)
4115   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4116
4117 (defun gnus-gather-threads-by-subject (threads)
4118   "Gather threads by looking at Subject headers."
4119   (if (not gnus-summary-make-false-root)
4120       threads
4121     (let ((hashtb (gnus-make-hashtable 1024))
4122           (prev threads)
4123           (result threads)
4124           subject hthread whole-subject)
4125       (while threads
4126         (setq subject (gnus-general-simplify-subject
4127                        (setq whole-subject (mail-header-subject
4128                                             (caar threads)))))
4129         (when subject
4130           (if (setq hthread (gnus-gethash subject hashtb))
4131               (progn
4132                 ;; We enter a dummy root into the thread, if we
4133                 ;; haven't done that already.
4134                 (unless (stringp (caar hthread))
4135                   (setcar hthread (list whole-subject (car hthread))))
4136                 ;; We add this new gathered thread to this gathered
4137                 ;; thread.
4138                 (setcdr (car hthread)
4139                         (nconc (cdar hthread) (list (car threads))))
4140                 ;; Remove it from the list of threads.
4141                 (setcdr prev (cdr threads))
4142                 (setq threads prev))
4143             ;; Enter this thread into the hash table.
4144             (gnus-sethash subject
4145                           (if gnus-summary-make-false-root-always
4146                               (progn
4147                                 ;; If you want a dummy root above all
4148                                 ;; threads...
4149                                 (setcar threads (list whole-subject
4150                                                       (car threads)))
4151                                 threads)
4152                             threads)
4153                           hashtb)))
4154         (setq prev threads)
4155         (setq threads (cdr threads)))
4156       result)))
4157
4158 (defun gnus-gather-threads-by-references (threads)
4159   "Gather threads by looking at References headers."
4160   (let ((idhashtb (gnus-make-hashtable 1024))
4161         (thhashtb (gnus-make-hashtable 1024))
4162         (prev threads)
4163         (result threads)
4164         ids references id gthread gid entered ref)
4165     (while threads
4166       (when (setq references (mail-header-references (caar threads)))
4167         (setq id (mail-header-id (caar threads))
4168               ids (inline (gnus-split-references references))
4169               entered nil)
4170         (while (setq ref (pop ids))
4171           (setq ids (delete ref ids))
4172           (if (not (setq gid (gnus-gethash ref idhashtb)))
4173               (progn
4174                 (gnus-sethash ref id idhashtb)
4175                 (gnus-sethash id threads thhashtb))
4176             (setq gthread (gnus-gethash gid thhashtb))
4177             (unless entered
4178               ;; We enter a dummy root into the thread, if we
4179               ;; haven't done that already.
4180               (unless (stringp (caar gthread))
4181                 (setcar gthread (list (mail-header-subject (caar gthread))
4182                                       (car gthread))))
4183               ;; We add this new gathered thread to this gathered
4184               ;; thread.
4185               (setcdr (car gthread)
4186                       (nconc (cdar gthread) (list (car threads)))))
4187             ;; Add it into the thread hash table.
4188             (gnus-sethash id gthread thhashtb)
4189             (setq entered t)
4190             ;; Remove it from the list of threads.
4191             (setcdr prev (cdr threads))
4192             (setq threads prev))))
4193       (setq prev threads)
4194       (setq threads (cdr threads)))
4195     result))
4196
4197 (defun gnus-sort-gathered-threads (threads)
4198   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4199   (let ((result threads))
4200     (while threads
4201       (when (stringp (caar threads))
4202         (setcdr (car threads)
4203                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4204       (setq threads (cdr threads)))
4205     result))
4206
4207 (defun gnus-thread-loop-p (root thread)
4208   "Say whether ROOT is in THREAD."
4209   (let ((stack (list thread))
4210         (infloop 0)
4211         th)
4212     (while (setq thread (pop stack))
4213       (setq th (cdr thread))
4214       (while (and th
4215                   (not (eq (caar th) root)))
4216         (pop th))
4217       (if th
4218           ;; We have found a loop.
4219           (let (ref-dep)
4220             (setcdr thread (delq (car th) (cdr thread)))
4221             (if (boundp (setq ref-dep (intern "none"
4222                                               gnus-newsgroup-dependencies)))
4223                 (setcdr (symbol-value ref-dep)
4224                         (nconc (cdr (symbol-value ref-dep))
4225                                (list (car th))))
4226               (set ref-dep (list nil (car th))))
4227             (setq infloop 1
4228                   stack nil))
4229         ;; Push all the subthreads onto the stack.
4230         (push (cdr thread) stack)))
4231     infloop))
4232
4233 (defun gnus-make-threads ()
4234   "Go through the dependency hashtb and find the roots.  Return all threads."
4235   (let (threads)
4236     (while (catch 'infloop
4237              (mapatoms
4238               (lambda (refs)
4239                 ;; Deal with self-referencing References loops.
4240                 (when (and (car (symbol-value refs))
4241                            (not (zerop
4242                                  (apply
4243                                   '+
4244                                   (mapcar
4245                                    (lambda (thread)
4246                                      (gnus-thread-loop-p
4247                                       (car (symbol-value refs)) thread))
4248                                    (cdr (symbol-value refs)))))))
4249                   (setq threads nil)
4250                   (throw 'infloop t))
4251                 (unless (car (symbol-value refs))
4252                   ;; These threads do not refer back to any other
4253                   ;; articles, so they're roots.
4254                   (setq threads (append (cdr (symbol-value refs)) threads))))
4255               gnus-newsgroup-dependencies)))
4256     threads))
4257
4258 ;; Build the thread tree.
4259 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4260   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4261
4262 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4263 if it was already present.
4264
4265 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4266 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4267 Message-IDs will be renamed to a unique Message-ID before being
4268 entered.
4269
4270 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4271   (let* ((id (mail-header-id header))
4272          (id-dep (and id (intern id dependencies)))
4273          parent-id ref ref-dep ref-header replaced)
4274     ;; Enter this `header' in the `dependencies' table.
4275     (cond
4276      ((not id-dep)
4277       (setq header nil))
4278      ;; The first two cases do the normal part: enter a new `header'
4279      ;; in the `dependencies' table.
4280      ((not (boundp id-dep))
4281       (set id-dep (list header)))
4282      ((null (car (symbol-value id-dep)))
4283       (setcar (symbol-value id-dep) header))
4284
4285      ;; From here the `header' was already present in the
4286      ;; `dependencies' table.
4287      (force-new
4288       ;; Overrides an existing entry;
4289       ;; just set the header part of the entry.
4290       (setcar (symbol-value id-dep) header)
4291       (setq replaced t))
4292
4293      ;; Renames the existing `header' to a unique Message-ID.
4294      ((not gnus-summary-ignore-duplicates)
4295       ;; An article with this Message-ID has already been seen.
4296       ;; We rename the Message-ID.
4297       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4298            (list header))
4299       (mail-header-set-id header id))
4300
4301      ;; The last case ignores an existing entry, except it adds any
4302      ;; additional Xrefs (in case the two articles came from different
4303      ;; servers.
4304      ;; Also sets `header' to `nil' meaning that the `dependencies'
4305      ;; table was *not* modified.
4306      (t
4307       (mail-header-set-xref
4308        (car (symbol-value id-dep))
4309        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4310                    "")
4311                (or (mail-header-xref header) "")))
4312       (setq header nil)))
4313
4314     (when (and header (not replaced))
4315       ;; First check that we are not creating a References loop.
4316       (setq parent-id (gnus-parent-id (mail-header-references header)))
4317       (setq ref parent-id)
4318       (while (and ref
4319                   (setq ref-dep (intern-soft ref dependencies))
4320                   (boundp ref-dep)
4321                   (setq ref-header (car (symbol-value ref-dep))))
4322         (if (string= id ref)
4323             ;; Yuk!  This is a reference loop.  Make the article be a
4324             ;; root article.
4325             (progn
4326               (mail-header-set-references (car (symbol-value id-dep)) "none")
4327               (setq ref nil)
4328               (setq parent-id nil))
4329           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4330       (setq ref-dep (intern (or parent-id "none") dependencies))
4331       (if (boundp ref-dep)
4332           (setcdr (symbol-value ref-dep)
4333                   (nconc (cdr (symbol-value ref-dep))
4334                          (list (symbol-value id-dep))))
4335         (set ref-dep (list nil (symbol-value id-dep)))))
4336     header))
4337
4338 (defun gnus-extract-message-id-from-in-reply-to (string)
4339   (if (string-match "<[^>]+>" string)
4340       (substring string (match-beginning 0) (match-end 0))
4341     nil))
4342
4343 (defun gnus-build-sparse-threads ()
4344   (let ((headers gnus-newsgroup-headers)
4345         (mail-parse-charset gnus-newsgroup-charset)
4346         (gnus-summary-ignore-duplicates t)
4347         header references generation relations
4348         subject child end new-child date)
4349     ;; First we create an alist of generations/relations, where
4350     ;; generations is how much we trust the relation, and the relation
4351     ;; is parent/child.
4352     (gnus-message 7 "Making sparse threads...")
4353     (save-excursion
4354       (nnheader-set-temp-buffer " *gnus sparse threads*")
4355       (while (setq header (pop headers))
4356         (when (and (setq references (mail-header-references header))
4357                    (not (string= references "")))
4358           (insert references)
4359           (setq child (mail-header-id header)
4360                 subject (mail-header-subject header)
4361                 date (mail-header-date header)
4362                 generation 0)
4363           (while (search-backward ">" nil t)
4364             (setq end (1+ (point)))
4365             (when (search-backward "<" nil t)
4366               (setq new-child (buffer-substring (point) end))
4367               (push (list (incf generation)
4368                           child (setq child new-child)
4369                           subject date)
4370                     relations)))
4371           (when child
4372             (push (list (1+ generation) child nil subject) relations))
4373           (erase-buffer)))
4374       (kill-buffer (current-buffer)))
4375     ;; Sort over trustworthiness.
4376     (dolist (relation (sort relations 'car-less-than-car))
4377       (when (gnus-dependencies-add-header
4378              (make-full-mail-header
4379               gnus-reffed-article-number
4380               (nth 3 relation) "" (or (nth 4 relation) "")
4381               (nth 1 relation)
4382               (or (nth 2 relation) "") 0 0 "")
4383              gnus-newsgroup-dependencies nil)
4384         (push gnus-reffed-article-number gnus-newsgroup-limit)
4385         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4386         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4387               gnus-newsgroup-reads)
4388         (decf gnus-reffed-article-number)))
4389     (gnus-message 7 "Making sparse threads...done")))
4390
4391 (defun gnus-build-old-threads ()
4392   ;; Look at all the articles that refer back to old articles, and
4393   ;; fetch the headers for the articles that aren't there.  This will
4394   ;; build complete threads - if the roots haven't been expired by the
4395   ;; server, that is.
4396   (let ((mail-parse-charset gnus-newsgroup-charset)
4397         id heads)
4398     (mapatoms
4399      (lambda (refs)
4400        (when (not (car (symbol-value refs)))
4401          (setq heads (cdr (symbol-value refs)))
4402          (while heads
4403            (if (memq (mail-header-number (caar heads))
4404                      gnus-newsgroup-dormant)
4405                (setq heads (cdr heads))
4406              (setq id (symbol-name refs))
4407              (while (and (setq id (gnus-build-get-header id))
4408                          (not (car (gnus-id-to-thread id)))))
4409              (setq heads nil)))))
4410      gnus-newsgroup-dependencies)))
4411
4412 (defsubst gnus-remove-odd-characters (string)
4413   "Translate STRING into something that doesn't contain weird characters."
4414   (mm-subst-char-in-string
4415    ?\r ?\-
4416    (mm-subst-char-in-string ?\n ?\- string t) t))
4417
4418 ;; This function has to be called with point after the article number
4419 ;; on the beginning of the line.
4420 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4421   (let ((eol (point-at-eol))
4422         (buffer (current-buffer))
4423         header references in-reply-to)
4424
4425     ;; overview: [num subject from date id refs chars lines misc]
4426     (unwind-protect
4427         (let (x)
4428           (narrow-to-region (point) eol)
4429           (unless (eobp)
4430             (forward-char))
4431
4432           (setq header
4433                 (make-full-mail-header
4434                  number                 ; number
4435                  (condition-case ()     ; subject
4436                      (gnus-remove-odd-characters
4437                       (funcall gnus-decode-encoded-word-function
4438                                (setq x (nnheader-nov-field))))
4439                    (error x))
4440                  (condition-case ()     ; from
4441                      (gnus-remove-odd-characters
4442                       (funcall gnus-decode-encoded-address-function
4443                                (setq x (nnheader-nov-field))))
4444                    (error x))
4445                  (nnheader-nov-field)   ; date
4446                  (nnheader-nov-read-message-id number)  ; id
4447                  (setq references (nnheader-nov-field)) ; refs
4448                  (nnheader-nov-read-integer) ; chars
4449                  (nnheader-nov-read-integer) ; lines
4450                  (unless (eobp)
4451                    (if (looking-at "Xref: ")
4452                        (goto-char (match-end 0)))
4453                    (nnheader-nov-field)) ; Xref
4454                  (nnheader-nov-parse-extra)))) ; extra
4455
4456       (widen))
4457
4458     (when (and (string= references "")
4459                (setq in-reply-to (mail-header-extra header))
4460                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4461       (mail-header-set-references
4462        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4463
4464     (when gnus-alter-header-function
4465       (funcall gnus-alter-header-function header))
4466     (gnus-dependencies-add-header header dependencies force-new)))
4467
4468 (defun gnus-build-get-header (id)
4469   "Look through the buffer of NOV lines and find the header to ID.
4470 Enter this line into the dependencies hash table, and return
4471 the id of the parent article (if any)."
4472   (let ((deps gnus-newsgroup-dependencies)
4473         found header)
4474     (prog1
4475         (with-current-buffer nntp-server-buffer
4476           (let ((case-fold-search nil))
4477             (goto-char (point-min))
4478             (while (and (not found)
4479                         (search-forward id nil t))
4480               (beginning-of-line)
4481               (setq found (looking-at
4482                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4483                                    (regexp-quote id))))
4484               (or found (beginning-of-line 2)))
4485             (when found
4486               (beginning-of-line)
4487               (and
4488                (setq header (gnus-nov-parse-line
4489                              (read (current-buffer)) deps))
4490                (gnus-parent-id (mail-header-references header))))))
4491       (when header
4492         (let ((number (mail-header-number header)))
4493           (push number gnus-newsgroup-limit)
4494           (push header gnus-newsgroup-headers)
4495           (if (memq number gnus-newsgroup-unselected)
4496               (progn
4497                 (setq gnus-newsgroup-unreads
4498                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4499                                                number))
4500                 (setq gnus-newsgroup-unselected
4501                       (delq number gnus-newsgroup-unselected)))
4502             (push number gnus-newsgroup-ancient)))))))
4503
4504 (defun gnus-build-all-threads ()
4505   "Read all the headers."
4506   (let ((gnus-summary-ignore-duplicates t)
4507         (mail-parse-charset gnus-newsgroup-charset)
4508         (dependencies gnus-newsgroup-dependencies)
4509         header article)
4510     (with-current-buffer nntp-server-buffer
4511       (let ((case-fold-search nil))
4512         (goto-char (point-min))
4513         (while (not (eobp))
4514           (ignore-errors
4515             (setq article (read (current-buffer))
4516                   header (gnus-nov-parse-line article dependencies t)))
4517           (when header
4518             (with-current-buffer gnus-summary-buffer
4519               (push header gnus-newsgroup-headers)
4520               (if (memq (setq article (mail-header-number header))
4521                         gnus-newsgroup-unselected)
4522                   (progn
4523                     (setq gnus-newsgroup-unreads
4524                           (gnus-add-to-sorted-list
4525                            gnus-newsgroup-unreads article))
4526                     (setq gnus-newsgroup-unselected
4527                           (delq article gnus-newsgroup-unselected)))
4528                 (push article gnus-newsgroup-ancient)))
4529             (forward-line 1)))))))
4530
4531 (defun gnus-summary-update-article-line (article header)
4532   "Update the line for ARTICLE using HEADER."
4533   (let* ((id (mail-header-id header))
4534          (thread (gnus-id-to-thread id)))
4535     (unless thread
4536       (error "Article in no thread"))
4537     ;; Update the thread.
4538     (setcar thread header)
4539     (gnus-summary-goto-subject article)
4540     (let* ((datal (gnus-data-find-list article))
4541            (data (car datal))
4542            (inhibit-read-only t)
4543            (level (gnus-summary-thread-level)))
4544       (gnus-delete-line)
4545       (let ((inserted (- (point)
4546                          (progn
4547                            (gnus-summary-insert-line
4548                             header level nil
4549                             (memq article gnus-newsgroup-undownloaded)
4550                             (gnus-article-mark article)
4551                             (memq article gnus-newsgroup-replied)
4552                             (memq article gnus-newsgroup-expirable)
4553                             ;; Only insert the Subject string when it's different
4554                             ;; from the previous Subject string.
4555                             (if (and
4556                                  gnus-show-threads
4557                                  (gnus-subject-equal
4558                                   (condition-case ()
4559                                       (mail-header-subject
4560                                        (gnus-data-header
4561                                         (cadr
4562                                          (gnus-data-find-list
4563                                           article
4564                                           (gnus-data-list t)))))
4565                                     ;; Error on the side of excessive subjects.
4566                                     (error ""))
4567                                   (mail-header-subject header)))
4568                                 ""
4569                               (mail-header-subject header))
4570                             nil (cdr (assq article gnus-newsgroup-scored))
4571                             (memq article gnus-newsgroup-processable))
4572                            (point)))))
4573         (when (cdr datal)
4574           (gnus-data-update-list
4575            (cdr datal)
4576            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4577
4578 (defun gnus-summary-update-article (article &optional iheader)
4579   "Update ARTICLE in the summary buffer."
4580   (set-buffer gnus-summary-buffer)
4581   (let* ((header (gnus-summary-article-header article))
4582          (id (mail-header-id header))
4583          (data (gnus-data-find article))
4584          (thread (gnus-id-to-thread id))
4585          (references (mail-header-references header))
4586          (parent
4587           (gnus-id-to-thread
4588            (or (gnus-parent-id
4589                 (when (and references
4590                            (not (equal "" references)))
4591                   references))
4592                "none")))
4593          (inhibit-read-only t)
4594          (old (car thread)))
4595     (when thread
4596       (unless iheader
4597         (setcar thread nil)
4598         (when parent
4599           (delq thread parent)))
4600       (if (gnus-summary-insert-subject id header)
4601           ;; Set the (possibly) new article number in the data structure.
4602           (gnus-data-set-number data (gnus-id-to-article id))
4603         (setcar thread old)
4604         nil))))
4605
4606 (defun gnus-rebuild-thread (id &optional line)
4607   "Rebuild the thread containing ID.
4608 If LINE, insert the rebuilt thread starting on line LINE."
4609   (let ((inhibit-read-only t)
4610         old-pos current thread data)
4611     (if (not gnus-show-threads)
4612         (setq thread (list (car (gnus-id-to-thread id))))
4613       ;; Get the thread this article is part of.
4614       (setq thread (gnus-remove-thread id)))
4615     (setq old-pos (point-at-bol))
4616     (setq current (save-excursion
4617                     (and (re-search-backward "[\r\n]" nil t)
4618                          (gnus-summary-article-number))))
4619     ;; If this is a gathered thread, we have to go some re-gathering.
4620     (when (stringp (car thread))
4621       (let ((subject (car thread))
4622             roots thr)
4623         (setq thread (cdr thread))
4624         (while thread
4625           (unless (memq (setq thr (gnus-id-to-thread
4626                                    (gnus-root-id
4627                                     (mail-header-id (caar thread)))))
4628                         roots)
4629             (push thr roots))
4630           (setq thread (cdr thread)))
4631         ;; We now have all (unique) roots.
4632         (if (= (length roots) 1)
4633             ;; All the loose roots are now one solid root.
4634             (setq thread (car roots))
4635           (setq thread (cons subject (gnus-sort-threads roots))))))
4636     (let (threads)
4637       ;; We then insert this thread into the summary buffer.
4638       (when line
4639         (goto-char (point-min))
4640         (forward-line (1- line)))
4641       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4642         (if gnus-show-threads
4643             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4644           (gnus-summary-prepare-unthreaded thread))
4645         (setq data (nreverse gnus-newsgroup-data))
4646         (setq threads gnus-newsgroup-threads))
4647       ;; We splice the new data into the data structure.
4648       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4649       ;;!!! then we want to insert at the beginning of the buffer.
4650       ;;!!! That happens to be true with Gnus now, but that may
4651       ;;!!! change in the future.  Perhaps.
4652       (gnus-data-enter-list
4653        (if line nil current) data (- (point) old-pos))
4654       (setq gnus-newsgroup-threads
4655             (nconc threads gnus-newsgroup-threads))
4656       (gnus-data-compute-positions))))
4657
4658 (defun gnus-number-to-header (number)
4659   "Return the header for article NUMBER."
4660   (let ((headers gnus-newsgroup-headers))
4661     (while (and headers
4662                 (not (= number (mail-header-number (car headers)))))
4663       (pop headers))
4664     (when headers
4665       (car headers))))
4666
4667 (defun gnus-parent-headers (in-headers &optional generation)
4668   "Return the headers of the GENERATIONeth parent of HEADERS."
4669   (unless generation
4670     (setq generation 1))
4671   (let ((parent t)
4672         (headers in-headers)
4673         references)
4674     (while (and parent
4675                 (not (zerop generation))
4676                 (setq references (mail-header-references headers)))
4677       (setq headers (if (and references
4678                              (setq parent (gnus-parent-id references)))
4679                         (car (gnus-id-to-thread parent))
4680                       nil))
4681       (decf generation))
4682     (and (not (eq headers in-headers))
4683          headers)))
4684
4685 (defun gnus-id-to-thread (id)
4686   "Return the (sub-)thread where ID appears."
4687   (gnus-gethash id gnus-newsgroup-dependencies))
4688
4689 (defun gnus-id-to-article (id)
4690   "Return the article number of ID."
4691   (let ((thread (gnus-id-to-thread id)))
4692     (when (and thread
4693                (car thread))
4694       (mail-header-number (car thread)))))
4695
4696 (defun gnus-id-to-header (id)
4697   "Return the article headers of ID."
4698   (car (gnus-id-to-thread id)))
4699
4700 (defun gnus-article-displayed-root-p (article)
4701   "Say whether ARTICLE is a root(ish) article."
4702   (let ((level (gnus-summary-thread-level article))
4703         (refs (mail-header-references  (gnus-summary-article-header article)))
4704         particle)
4705     (cond
4706      ((null level) nil)
4707      ((zerop level) t)
4708      ((null refs) t)
4709      ((null (gnus-parent-id refs)) t)
4710      ((and (= 1 level)
4711            (null (setq particle (gnus-id-to-article
4712                                  (gnus-parent-id refs))))
4713            (null (gnus-summary-thread-level particle)))))))
4714
4715 (defun gnus-root-id (id)
4716   "Return the id of the root of the thread where ID appears."
4717   (let (last-id prev)
4718     (while (and id (setq prev (car (gnus-id-to-thread id))))
4719       (setq last-id id
4720             id (gnus-parent-id (mail-header-references prev))))
4721     last-id))
4722
4723 (defun gnus-articles-in-thread (thread)
4724   "Return the list of articles in THREAD."
4725   (cons (mail-header-number (car thread))
4726         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4727
4728 (defun gnus-remove-thread (id &optional dont-remove)
4729   "Remove the thread that has ID in it."
4730   (let (headers thread last-id)
4731     ;; First go up in this thread until we find the root.
4732     (setq last-id (gnus-root-id id)
4733           headers (message-flatten-list (gnus-id-to-thread last-id)))
4734     ;; We have now found the real root of this thread.  It might have
4735     ;; been gathered into some loose thread, so we have to search
4736     ;; through the threads to find the thread we wanted.
4737     (let ((threads gnus-newsgroup-threads)
4738           sub)
4739       (while threads
4740         (setq sub (car threads))
4741         (if (stringp (car sub))
4742             ;; This is a gathered thread, so we look at the roots
4743             ;; below it to find whether this article is in this
4744             ;; gathered root.
4745             (progn
4746               (setq sub (cdr sub))
4747               (while sub
4748                 (when (member (caar sub) headers)
4749                   (setq thread (car threads)
4750                         threads nil
4751                         sub nil))
4752                 (setq sub (cdr sub))))
4753           ;; It's an ordinary thread, so we check it.
4754           (when (eq (car sub) (car headers))
4755             (setq thread sub
4756                   threads nil)))
4757         (setq threads (cdr threads)))
4758       ;; If this article is in no thread, then it's a root.
4759       (if thread
4760           (unless dont-remove
4761             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4762         (setq thread (gnus-id-to-thread last-id)))
4763       (when thread
4764         (prog1
4765             thread                      ; We return this thread.
4766           (unless dont-remove
4767             (if (stringp (car thread))
4768                 (progn
4769                   ;; If we use dummy roots, then we have to remove the
4770                   ;; dummy root as well.
4771                   (when (eq gnus-summary-make-false-root 'dummy)
4772                     ;; We go to the dummy root by going to
4773                     ;; the first sub-"thread", and then one line up.
4774                     (gnus-summary-goto-article
4775                      (mail-header-number (caadr thread)))
4776                     (forward-line -1)
4777                     (gnus-delete-line)
4778                     (gnus-data-compute-positions))
4779                   (setq thread (cdr thread))
4780                   (while thread
4781                     (gnus-remove-thread-1 (car thread))
4782                     (setq thread (cdr thread))))
4783               (gnus-remove-thread-1 thread))))))))
4784
4785 (defun gnus-remove-thread-1 (thread)
4786   "Remove the thread THREAD recursively."
4787   (let ((number (mail-header-number (pop thread)))
4788         d)
4789     (setq thread (reverse thread))
4790     (while thread
4791       (gnus-remove-thread-1 (pop thread)))
4792     (when (setq d (gnus-data-find number))
4793       (goto-char (gnus-data-pos d))
4794       (gnus-summary-show-thread)
4795       (gnus-data-remove
4796        number
4797        (- (point-at-bol)
4798           (prog1
4799               (1+ (point-at-eol))
4800             (gnus-delete-line)))))))
4801
4802 (defun gnus-sort-threads-recursive (threads func)
4803   (sort (mapcar (lambda (thread)
4804                   (cons (car thread)
4805                         (and (cdr thread)
4806                              (gnus-sort-threads-recursive (cdr thread) func))))
4807                 threads) func))
4808
4809 (defun gnus-sort-threads-loop (threads func)
4810   (let* ((superthread (cons nil threads))
4811          (stack (list (cons superthread threads)))
4812          remaining-threads thread)
4813     (while stack
4814       (setq remaining-threads (cdr (car stack)))
4815       (if remaining-threads
4816           (progn (setq thread (car remaining-threads))
4817                  (setcdr (car stack) (cdr remaining-threads))
4818                  (if (cdr thread)
4819                      (push (cons thread (cdr thread)) stack)))
4820         (setq thread (caar stack))
4821         (setcdr thread (sort (cdr thread) func))
4822         (pop stack)))
4823     (cdr superthread)))
4824
4825 (defun gnus-sort-threads (threads)
4826   "Sort THREADS."
4827   (if (not gnus-thread-sort-functions)
4828       threads
4829     (gnus-message 8 "Sorting threads...")
4830     (prog1
4831         (condition-case nil
4832             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4833               (gnus-sort-threads-recursive
4834                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4835           ;; Even after binding max-lisp-eval-depth, the recursive
4836           ;; sorter might fail for very long threads.  In that case,
4837           ;; try using a (less well-tested) non-recursive sorter.
4838           (error (gnus-message 9 "Sorting threads with loop...")
4839                  (gnus-sort-threads-loop
4840                   threads (gnus-make-sort-function
4841                            gnus-thread-sort-functions))))
4842       (gnus-message 8 "Sorting threads...done"))))
4843
4844 (defun gnus-sort-articles (articles)
4845   "Sort ARTICLES."
4846   (when gnus-article-sort-functions
4847     (gnus-message 7 "Sorting articles...")
4848     (prog1
4849         (setq gnus-newsgroup-headers
4850               (sort articles (gnus-make-sort-function
4851                               gnus-article-sort-functions)))
4852       (gnus-message 7 "Sorting articles...done"))))
4853
4854 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4855 (defmacro gnus-thread-header (thread)
4856   "Return header of first article in THREAD.
4857 Note that THREAD must never, ever be anything else than a variable -
4858 using some other form will lead to serious barfage."
4859   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4860   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4861   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4862         (vector thread) 2))
4863
4864 (defsubst gnus-article-sort-by-number (h1 h2)
4865   "Sort articles by article number."
4866   (< (mail-header-number h1)
4867      (mail-header-number h2)))
4868
4869 (defun gnus-thread-sort-by-number (h1 h2)
4870   "Sort threads by root article number."
4871   (gnus-article-sort-by-number
4872    (gnus-thread-header h1) (gnus-thread-header h2)))
4873
4874 (defsubst gnus-article-sort-by-random (h1 h2)
4875   "Sort articles randomly."
4876   (zerop (random 2)))
4877
4878 (defun gnus-thread-sort-by-random (h1 h2)
4879   "Sort threads randomly."
4880   (gnus-article-sort-by-random
4881    (gnus-thread-header h1) (gnus-thread-header h2)))
4882
4883 (defsubst gnus-article-sort-by-lines (h1 h2)
4884   "Sort articles by article Lines header."
4885   (< (mail-header-lines h1)
4886      (mail-header-lines h2)))
4887
4888 (defun gnus-thread-sort-by-lines (h1 h2)
4889   "Sort threads by root article Lines header."
4890   (gnus-article-sort-by-lines
4891    (gnus-thread-header h1) (gnus-thread-header h2)))
4892
4893 (defsubst gnus-article-sort-by-chars (h1 h2)
4894   "Sort articles by octet length."
4895   (< (mail-header-chars h1)
4896      (mail-header-chars h2)))
4897
4898 (defun gnus-thread-sort-by-chars (h1 h2)
4899   "Sort threads by root article octet length."
4900   (gnus-article-sort-by-chars
4901    (gnus-thread-header h1) (gnus-thread-header h2)))
4902
4903 (defsubst gnus-article-sort-by-author (h1 h2)
4904   "Sort articles by root author."
4905   (gnus-string<
4906    (let ((extract (funcall
4907                    gnus-extract-address-components
4908                    (mail-header-from h1))))
4909      (or (car extract) (cadr extract) ""))
4910    (let ((extract (funcall
4911                    gnus-extract-address-components
4912                    (mail-header-from h2))))
4913      (or (car extract) (cadr extract) ""))))
4914
4915 (defun gnus-thread-sort-by-author (h1 h2)
4916   "Sort threads by root author."
4917   (gnus-article-sort-by-author
4918    (gnus-thread-header h1)  (gnus-thread-header h2)))
4919
4920 (defsubst gnus-article-sort-by-recipient (h1 h2)
4921   "Sort articles by recipient."
4922   (gnus-string<
4923    (let ((extract (funcall
4924                    gnus-extract-address-components
4925                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4926      (or (car extract) (cadr extract)))
4927    (let ((extract (funcall
4928                    gnus-extract-address-components
4929                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4930      (or (car extract) (cadr extract)))))
4931
4932 (defun gnus-thread-sort-by-recipient (h1 h2)
4933   "Sort threads by root recipient."
4934   (gnus-article-sort-by-recipient
4935    (gnus-thread-header h1) (gnus-thread-header h2)))
4936
4937 (defsubst gnus-article-sort-by-subject (h1 h2)
4938   "Sort articles by root subject."
4939   (gnus-string<
4940    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4941    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4942
4943 (defun gnus-thread-sort-by-subject (h1 h2)
4944   "Sort threads by root subject."
4945   (gnus-article-sort-by-subject
4946    (gnus-thread-header h1) (gnus-thread-header h2)))
4947
4948 (defsubst gnus-article-sort-by-date (h1 h2)
4949   "Sort articles by root article date."
4950   (time-less-p
4951    (gnus-date-get-time (mail-header-date h1))
4952    (gnus-date-get-time (mail-header-date h2))))
4953
4954 (defun gnus-thread-sort-by-date (h1 h2)
4955   "Sort threads by root article date."
4956   (gnus-article-sort-by-date
4957    (gnus-thread-header h1) (gnus-thread-header h2)))
4958
4959 (defsubst gnus-article-sort-by-score (h1 h2)
4960   "Sort articles by root article score.
4961 Unscored articles will be counted as having a score of zero."
4962   (> (or (cdr (assq (mail-header-number h1)
4963                     gnus-newsgroup-scored))
4964          gnus-summary-default-score 0)
4965      (or (cdr (assq (mail-header-number h2)
4966                     gnus-newsgroup-scored))
4967          gnus-summary-default-score 0)))
4968
4969 (defun gnus-thread-sort-by-score (h1 h2)
4970   "Sort threads by root article score."
4971   (gnus-article-sort-by-score
4972    (gnus-thread-header h1) (gnus-thread-header h2)))
4973
4974 (defun gnus-thread-sort-by-total-score (h1 h2)
4975   "Sort threads by the sum of all scores in the thread.
4976 Unscored articles will be counted as having a score of zero."
4977   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4978
4979 (defun gnus-thread-total-score (thread)
4980   ;; This function find the total score of THREAD.
4981   (cond
4982    ((null thread)
4983     0)
4984    ((consp thread)
4985     (if (stringp (car thread))
4986         (apply gnus-thread-score-function 0
4987                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4988       (gnus-thread-total-score-1 thread)))
4989    (t
4990     (gnus-thread-total-score-1 (list thread)))))
4991
4992 (defun gnus-article-sort-by-most-recent-number (h1 h2)
4993   "Sort articles by number."
4994   (gnus-article-sort-by-number h1 h2))
4995
4996 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4997   "Sort threads such that the thread with the most recently arrived article comes first."
4998   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4999
5000 (defun gnus-thread-highest-number (thread)
5001   "Return the highest article number in THREAD."
5002   (apply 'max (mapcar (lambda (header)
5003                         (mail-header-number header))
5004                       (message-flatten-list thread))))
5005
5006 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5007   "Sort articles by number."
5008   (gnus-article-sort-by-date h1 h2))
5009
5010 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5011   "Sort threads such that the thread with the most recently dated article comes first."
5012   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5013
5014 ; Since this is called not only to sort the top-level threads, but
5015 ; also in recursive sorts to order the articles within a thread, each
5016 ; article will be processed many times.  Thus it speeds things up
5017 ; quite a bit to use gnus-date-get-time, which caches the time value.
5018 (defun gnus-thread-latest-date (thread)
5019   "Return the highest article date in THREAD."
5020   (apply 'max
5021          (mapcar (lambda (header) (gnus-float-time
5022                                    (gnus-date-get-time
5023                                     (mail-header-date header))))
5024                  (message-flatten-list thread))))
5025
5026 (defun gnus-thread-total-score-1 (root)
5027   ;; This function find the total score of the thread below ROOT.
5028   (setq root (car root))
5029   (apply gnus-thread-score-function
5030          (or (append
5031               (mapcar 'gnus-thread-total-score
5032                       (cdr (gnus-id-to-thread (mail-header-id root))))
5033               (when (> (mail-header-number root) 0)
5034                 (list (or (cdr (assq (mail-header-number root)
5035                                      gnus-newsgroup-scored))
5036                           gnus-summary-default-score 0))))
5037              (list gnus-summary-default-score)
5038              '(0))))
5039
5040 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5041 (defvar gnus-tmp-prev-subject nil)
5042 (defvar gnus-tmp-false-parent nil)
5043 (defvar gnus-tmp-root-expunged nil)
5044 (defvar gnus-tmp-dummy-line nil)
5045
5046 (defun gnus-extra-header (type &optional header)
5047   "Return the extra header of TYPE."
5048   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5049       ""))
5050
5051 (defvar gnus-tmp-thread-tree-header-string "")
5052
5053 (defcustom gnus-sum-thread-tree-root "> "
5054   "With %B spec, used for the root of a thread.
5055 If nil, use subject instead."
5056   :version "22.1"
5057   :type '(radio (const :format "%v  " nil) string)
5058   :group 'gnus-thread)
5059
5060 (defcustom gnus-sum-thread-tree-false-root "> "
5061   "With %B spec, used for a false root of a thread.
5062 If nil, use subject instead."
5063   :version "22.1"
5064   :type '(radio (const :format "%v  " nil) string)
5065   :group 'gnus-thread)
5066
5067 (defcustom gnus-sum-thread-tree-single-indent ""
5068   "With %B spec, used for a thread with just one message.
5069 If nil, use subject instead."
5070   :version "22.1"
5071   :type '(radio (const :format "%v  " nil) string)
5072   :group 'gnus-thread)
5073
5074 (defcustom gnus-sum-thread-tree-vertical "| "
5075   "With %B spec, used for drawing a vertical line."
5076   :version "22.1"
5077   :type 'string
5078   :group 'gnus-thread)
5079
5080 (defcustom gnus-sum-thread-tree-indent "  "
5081   "With %B spec, used for indenting."
5082   :version "22.1"
5083   :type 'string
5084   :group 'gnus-thread)
5085
5086 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5087   "With %B spec, used for a leaf with brothers."
5088   :version "22.1"
5089   :type 'string
5090   :group 'gnus-thread)
5091
5092 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5093   "With %B spec, used for a leaf without brothers."
5094   :version "22.1"
5095   :type 'string
5096   :group 'gnus-thread)
5097
5098 (defcustom gnus-summary-display-while-building nil
5099   "If non-nil, show and update the summary buffer as it's being built.
5100 If the value is t, update the buffer after every line is inserted.  If
5101 the value is an integer (N), update the display every N lines."
5102   :version "22.1"
5103   :group 'gnus-thread
5104   :type '(choice (const :tag "off" nil)
5105                  number
5106                  (const :tag "frequently" t)))
5107
5108 (defun gnus-summary-prepare-threads (threads)
5109   "Prepare summary buffer from THREADS and indentation LEVEL.
5110 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5111 or a straight list of headers."
5112   (gnus-message 7 "Generating summary...")
5113
5114   (setq gnus-newsgroup-threads threads)
5115   (beginning-of-line)
5116
5117   (let ((gnus-tmp-level 0)
5118         (default-score (or gnus-summary-default-score 0))
5119         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5120         (building-line-count gnus-summary-display-while-building)
5121         (building-count (integerp gnus-summary-display-while-building))
5122         thread number subject stack state gnus-tmp-gathered beg-match
5123         new-roots gnus-tmp-new-adopts thread-end simp-subject
5124         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5125         gnus-tmp-replied gnus-tmp-subject-or-nil
5126         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5127         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5128         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5129         tree-stack)
5130
5131     (setq gnus-tmp-prev-subject nil
5132           gnus-tmp-thread-tree-header-string "")
5133
5134     (if (vectorp (car threads))
5135         ;; If this is a straight (sic) list of headers, then a
5136         ;; threaded summary display isn't required, so we just create
5137         ;; an unthreaded one.
5138         (gnus-summary-prepare-unthreaded threads)
5139
5140       ;; Do the threaded display.
5141
5142       (if gnus-summary-display-while-building
5143           (switch-to-buffer (buffer-name)))
5144       (while (or threads stack gnus-tmp-new-adopts new-roots)
5145
5146         (if (and (= gnus-tmp-level 0)
5147                  (or (not stack)
5148                      (= (caar stack) 0))
5149                  (not gnus-tmp-false-parent)
5150                  (or gnus-tmp-new-adopts new-roots))
5151             (if gnus-tmp-new-adopts
5152                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5153                       thread (list (car gnus-tmp-new-adopts))
5154                       gnus-tmp-header (caar thread)
5155                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5156               (when new-roots
5157                 (setq thread (list (car new-roots))
5158                       gnus-tmp-header (caar thread)
5159                       new-roots (cdr new-roots))))
5160
5161           (if threads
5162               ;; If there are some threads, we do them before the
5163               ;; threads on the stack.
5164               (setq thread threads
5165                     gnus-tmp-header (caar thread))
5166             ;; There were no current threads, so we pop something off
5167             ;; the stack.
5168             (setq state (car stack)
5169                   gnus-tmp-level (car state)
5170                   tree-stack (cadr state)
5171                   thread (caddr state)
5172                   stack (cdr stack)
5173                   gnus-tmp-header (caar thread))))
5174
5175         (setq gnus-tmp-false-parent nil)
5176         (setq gnus-tmp-root-expunged nil)
5177         (setq thread-end nil)
5178
5179         (if (stringp gnus-tmp-header)
5180             ;; The header is a dummy root.
5181             (cond
5182              ((eq gnus-summary-make-false-root 'adopt)
5183               ;; We let the first article adopt the rest.
5184               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5185                                                (cddar thread)))
5186               (setq gnus-tmp-gathered
5187                     (nconc (mapcar
5188                             (lambda (h) (mail-header-number (car h)))
5189                             (cddar thread))
5190                            gnus-tmp-gathered))
5191               (setq thread (cons (list (caar thread)
5192                                        (cadar thread))
5193                                  (cdr thread)))
5194               (setq gnus-tmp-level -1
5195                     gnus-tmp-false-parent t))
5196              ((eq gnus-summary-make-false-root 'empty)
5197               ;; We print adopted articles with empty subject fields.
5198               (setq gnus-tmp-gathered
5199                     (nconc (mapcar
5200                             (lambda (h) (mail-header-number (car h)))
5201                             (cddar thread))
5202                            gnus-tmp-gathered))
5203               (setq gnus-tmp-level -1))
5204              ((eq gnus-summary-make-false-root 'dummy)
5205               ;; We remember that we probably want to output a dummy
5206               ;; root.
5207               (setq gnus-tmp-dummy-line gnus-tmp-header)
5208               (setq gnus-tmp-prev-subject gnus-tmp-header))
5209              (t
5210               ;; We do not make a root for the gathered
5211               ;; sub-threads at all.
5212               (setq gnus-tmp-level -1)))
5213
5214           (setq number (mail-header-number gnus-tmp-header)
5215                 subject (mail-header-subject gnus-tmp-header)
5216                 simp-subject (gnus-simplify-subject-fully subject))
5217
5218           (cond
5219            ;; If the thread has changed subject, we might want to make
5220            ;; this subthread into a root.
5221            ((and (null gnus-thread-ignore-subject)
5222                  (not (zerop gnus-tmp-level))
5223                  gnus-tmp-prev-subject
5224                  (not (string= gnus-tmp-prev-subject simp-subject)))
5225             (setq new-roots (nconc new-roots (list (car thread)))
5226                   thread-end t
5227                   gnus-tmp-header nil))
5228            ;; If the article lies outside the current limit,
5229            ;; then we do not display it.
5230            ((not (memq number gnus-newsgroup-limit))
5231             (setq gnus-tmp-gathered
5232                   (nconc (mapcar
5233                           (lambda (h) (mail-header-number (car h)))
5234                           (cdar thread))
5235                          gnus-tmp-gathered))
5236             (setq gnus-tmp-new-adopts (if (cdar thread)
5237                                           (append gnus-tmp-new-adopts
5238                                                   (cdar thread))
5239                                         gnus-tmp-new-adopts)
5240                   thread-end t
5241                   gnus-tmp-header nil)
5242             (when (zerop gnus-tmp-level)
5243               (setq gnus-tmp-root-expunged t)))
5244            ;; Perhaps this article is to be marked as read?
5245            ((and gnus-summary-mark-below
5246                  (< (or (cdr (assq number gnus-newsgroup-scored))
5247                         default-score)
5248                     gnus-summary-mark-below)
5249                  ;; Don't touch sparse articles.
5250                  (not (gnus-summary-article-sparse-p number))
5251                  (not (gnus-summary-article-ancient-p number)))
5252             (setq gnus-newsgroup-unreads
5253                   (delq number gnus-newsgroup-unreads))
5254             (if gnus-newsgroup-auto-expire
5255                 (setq gnus-newsgroup-expirable
5256                       (gnus-add-to-sorted-list
5257                        gnus-newsgroup-expirable number))
5258               (push (cons number gnus-low-score-mark)
5259                     gnus-newsgroup-reads))))
5260
5261           (when gnus-tmp-header
5262             ;; We may have an old dummy line to output before this
5263             ;; article.
5264             (when (and gnus-tmp-dummy-line
5265                        (gnus-subject-equal
5266                         gnus-tmp-dummy-line
5267                         (mail-header-subject gnus-tmp-header)))
5268               (gnus-summary-insert-dummy-line
5269                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5270               (setq gnus-tmp-dummy-line nil))
5271
5272             ;; Compute the mark.
5273             (setq gnus-tmp-unread (gnus-article-mark number))
5274
5275             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5276                                   gnus-tmp-header gnus-tmp-level)
5277                   gnus-newsgroup-data)
5278
5279             ;; Actually insert the line.
5280             (setq
5281              gnus-tmp-subject-or-nil
5282              (cond
5283               ((and gnus-thread-ignore-subject
5284                     gnus-tmp-prev-subject
5285                     (not (string= gnus-tmp-prev-subject simp-subject)))
5286                subject)
5287               ((zerop gnus-tmp-level)
5288                (if (and (eq gnus-summary-make-false-root 'empty)
5289                         (memq number gnus-tmp-gathered)
5290                         gnus-tmp-prev-subject
5291                         (string= gnus-tmp-prev-subject simp-subject))
5292                    gnus-summary-same-subject
5293                  subject))
5294               (t gnus-summary-same-subject)))
5295             (if (and (eq gnus-summary-make-false-root 'adopt)
5296                      (= gnus-tmp-level 1)
5297                      (memq number gnus-tmp-gathered))
5298                 (setq gnus-tmp-opening-bracket ?\<
5299                       gnus-tmp-closing-bracket ?\>)
5300               (setq gnus-tmp-opening-bracket ?\[
5301                     gnus-tmp-closing-bracket ?\]))
5302             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5303                 (gnus-make-thread-indent-array
5304                  (max (* 2 (length gnus-thread-indent-array))
5305                       gnus-tmp-level)))
5306             (setq
5307              gnus-tmp-indentation
5308              (aref gnus-thread-indent-array gnus-tmp-level)
5309              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5310              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5311                                 gnus-summary-default-score 0)
5312              gnus-tmp-score-char
5313              (if (or (null gnus-summary-default-score)
5314                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5315                          gnus-summary-zcore-fuzz))
5316                  ?                      ;Whitespace
5317                (if (< gnus-tmp-score gnus-summary-default-score)
5318                    gnus-score-below-mark gnus-score-over-mark))
5319              gnus-tmp-replied
5320              (cond ((memq number gnus-newsgroup-processable)
5321                     gnus-process-mark)
5322                    ((memq number gnus-newsgroup-cached)
5323                     gnus-cached-mark)
5324                    ((memq number gnus-newsgroup-replied)
5325                     gnus-replied-mark)
5326                    ((memq number gnus-newsgroup-forwarded)
5327                     gnus-forwarded-mark)
5328                    ((memq number gnus-newsgroup-saved)
5329                     gnus-saved-mark)
5330                    ((memq number gnus-newsgroup-recent)
5331                     gnus-recent-mark)
5332                    ((memq number gnus-newsgroup-unseen)
5333                     gnus-unseen-mark)
5334                    (t gnus-no-mark))
5335              gnus-tmp-downloaded
5336              (cond ((memq number gnus-newsgroup-undownloaded)
5337                     gnus-undownloaded-mark)
5338                    (gnus-newsgroup-agentized
5339                     gnus-downloaded-mark)
5340                    (t
5341                     gnus-no-mark))
5342              gnus-tmp-from (mail-header-from gnus-tmp-header)
5343              gnus-tmp-name
5344              (cond
5345               ((string-match "<[^>]+> *$" gnus-tmp-from)
5346                (setq beg-match (match-beginning 0))
5347                (or (and (string-match "^\".+\"" gnus-tmp-from)
5348                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5349                    (substring gnus-tmp-from 0 beg-match)))
5350               ((string-match "(.+)" gnus-tmp-from)
5351                (substring gnus-tmp-from
5352                           (1+ (match-beginning 0)) (1- (match-end 0))))
5353               (t gnus-tmp-from))
5354
5355              ;; Do the %B string
5356              gnus-tmp-thread-tree-header-string
5357              (cond
5358               ((not gnus-show-threads) "")
5359               ((zerop gnus-tmp-level)
5360                (cond ((cdar thread)
5361                       (or gnus-sum-thread-tree-root subject))
5362                      (gnus-tmp-new-adopts
5363                       (or gnus-sum-thread-tree-false-root subject))
5364                      (t
5365                       (or gnus-sum-thread-tree-single-indent subject))))
5366               (t
5367                (concat (apply 'concat
5368                               (mapcar (lambda (item)
5369                                         (if (= item 1)
5370                                             gnus-sum-thread-tree-vertical
5371                                           gnus-sum-thread-tree-indent))
5372                                       (cdr (reverse tree-stack))))
5373                        (if (nth 1 thread)
5374                            gnus-sum-thread-tree-leaf-with-other
5375                          gnus-sum-thread-tree-single-leaf)))))
5376             (when (string= gnus-tmp-name "")
5377               (setq gnus-tmp-name gnus-tmp-from))
5378             (unless (numberp gnus-tmp-lines)
5379               (setq gnus-tmp-lines -1))
5380             (if (= gnus-tmp-lines -1)
5381                 (setq gnus-tmp-lines "?")
5382               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5383             (gnus-put-text-property
5384              (point)
5385              (progn (eval gnus-summary-line-format-spec) (point))
5386              'gnus-number number)
5387             (when gnus-visual-p
5388               (forward-line -1)
5389               (gnus-summary-highlight-line)
5390               (when gnus-summary-update-hook
5391                 (gnus-run-hooks 'gnus-summary-update-hook))
5392               (forward-line 1))
5393
5394             (setq gnus-tmp-prev-subject simp-subject)))
5395
5396         (when (nth 1 thread)
5397           (push (list (max 0 gnus-tmp-level)
5398                       (copy-sequence tree-stack)
5399                       (nthcdr 1 thread))
5400                 stack))
5401         (push (if (nth 1 thread) 1 0) tree-stack)
5402         (incf gnus-tmp-level)
5403         (setq threads (if thread-end nil (cdar thread)))
5404         (if gnus-summary-display-while-building
5405             (if building-count
5406                 (progn
5407                   ;; use a set frequency
5408                   (setq building-line-count (1- building-line-count))
5409                   (when (= building-line-count 0)
5410                     (sit-for 0)
5411                     (setq building-line-count
5412                           gnus-summary-display-while-building)))
5413               ;; always
5414               (sit-for 0)))
5415         (unless threads
5416           (setq gnus-tmp-level 0)))))
5417   (gnus-message 7 "Generating summary...done"))
5418
5419 (defun gnus-summary-prepare-unthreaded (headers)
5420   "Generate an unthreaded summary buffer based on HEADERS."
5421   (let (header number mark)
5422
5423     (beginning-of-line)
5424
5425     (while headers
5426       ;; We may have to root out some bad articles...
5427       (when (memq (setq number (mail-header-number
5428                                 (setq header (pop headers))))
5429                   gnus-newsgroup-limit)
5430         ;; Mark article as read when it has a low score.
5431         (when (and gnus-summary-mark-below
5432                    (< (or (cdr (assq number gnus-newsgroup-scored))
5433                           gnus-summary-default-score 0)
5434                       gnus-summary-mark-below)
5435                    (not (gnus-summary-article-ancient-p number)))
5436           (setq gnus-newsgroup-unreads
5437                 (delq number gnus-newsgroup-unreads))
5438           (if gnus-newsgroup-auto-expire
5439               (push number gnus-newsgroup-expirable)
5440             (push (cons number gnus-low-score-mark)
5441                   gnus-newsgroup-reads)))
5442
5443         (setq mark (gnus-article-mark number))
5444         (push (gnus-data-make number mark (1+ (point)) header 0)
5445               gnus-newsgroup-data)
5446         (gnus-summary-insert-line
5447          header 0 number
5448          (memq number gnus-newsgroup-undownloaded)
5449          mark (memq number gnus-newsgroup-replied)
5450          (memq number gnus-newsgroup-expirable)
5451          (mail-header-subject header) nil
5452          (cdr (assq number gnus-newsgroup-scored))
5453          (memq number gnus-newsgroup-processable))))))
5454
5455 (defun gnus-summary-remove-list-identifiers ()
5456   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5457   (let ((regexp (if (consp gnus-list-identifiers)
5458                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5459                   gnus-list-identifiers))
5460         changed subject)
5461     (when regexp
5462       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5463       (dolist (header gnus-newsgroup-headers)
5464         (setq subject (mail-header-subject header)
5465               changed nil)
5466         (while (string-match regexp subject)
5467           (setq subject
5468                 (concat (substring subject 0 (match-beginning 1))
5469                         (substring subject (match-end 0)))
5470                 changed t))
5471         (when changed
5472           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5473             (setq subject
5474                   (concat (substring subject 0 (match-beginning 1))
5475                           (substring subject (match-end 1)))))
5476           (mail-header-set-subject header subject))))))
5477
5478 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5479   "Fetch headers of ARTICLES."
5480   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5481     (gnus-message 5 "Fetching headers for %s..." name)
5482     (prog1
5483         (if (eq 'nov
5484                 (setq gnus-headers-retrieved-by
5485                       (gnus-retrieve-headers
5486                        articles gnus-newsgroup-name
5487                        (or limit
5488                            ;; We might want to fetch old headers, but
5489                            ;; not if there is only 1 article.
5490                            (and (or (and
5491                                      (not (eq gnus-fetch-old-headers 'some))
5492                                      (not (numberp gnus-fetch-old-headers)))
5493                                     (> (length articles) 1))
5494                                 gnus-fetch-old-headers)))))
5495             (gnus-get-newsgroup-headers-xover
5496              articles force-new dependencies gnus-newsgroup-name t)
5497           (gnus-get-newsgroup-headers dependencies force-new))
5498       (gnus-message 5 "Fetching headers for %s...done" name))))
5499
5500 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5501   "Select newsgroup GROUP.
5502 If READ-ALL is non-nil, all articles in the group are selected.
5503 If SELECT-ARTICLES, only select those articles from GROUP."
5504   (let* ((entry (gnus-group-entry group))
5505          ;;!!! Dirty hack; should be removed.
5506          (gnus-summary-ignore-duplicates
5507           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5508               t
5509             gnus-summary-ignore-duplicates))
5510          (info (nth 2 entry))
5511          charset articles fetched-articles cached)
5512
5513     (unless (gnus-check-server
5514              (set (make-local-variable 'gnus-current-select-method)
5515                   (gnus-find-method-for-group group)))
5516       (error "Couldn't open server"))
5517     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5518
5519     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5520         (gnus-activate-group group)     ; Or we can activate it...
5521         (progn                          ; Or we bug out.
5522           (when (equal major-mode 'gnus-summary-mode)
5523             (gnus-kill-buffer (current-buffer)))
5524           (error
5525            "Couldn't activate group %s: %s"
5526            (mm-decode-coding-string group charset)
5527            (mm-decode-coding-string (gnus-status-message group) charset))))
5528
5529     (unless (gnus-request-group group t)
5530       (when (equal major-mode 'gnus-summary-mode)
5531         (gnus-kill-buffer (current-buffer)))
5532       (error "Couldn't request group %s: %s"
5533              (mm-decode-coding-string group charset)
5534              (mm-decode-coding-string (gnus-status-message group) charset)))
5535
5536     (when gnus-agent
5537       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5538
5539       (setq gnus-summary-use-undownloaded-faces
5540             (gnus-agent-find-parameter
5541              group
5542              'agent-enable-undownloaded-faces)))
5543
5544     (setq gnus-newsgroup-name group
5545           gnus-newsgroup-unselected nil
5546           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5547
5548     (let ((display (gnus-group-find-parameter group 'display)))
5549       (setq gnus-newsgroup-display
5550             (cond
5551              ((not (zerop (or (car-safe read-all) 0)))
5552               ;; The user entered the group with C-u SPC/RET, let's show
5553               ;; all articles.
5554               'gnus-not-ignore)
5555              ((eq display 'all)
5556               'gnus-not-ignore)
5557              ((arrayp display)
5558               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5559              ((numberp display)
5560               ;; The following is probably the "correct" solution, but
5561               ;; it makes Gnus fetch all headers and then limit the
5562               ;; articles (which is slow), so instead we hack the
5563               ;; select-articles parameter instead. -- Simon Josefsson
5564               ;; <jas@kth.se>
5565               ;;
5566               ;; (gnus-byte-compile
5567               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5568               ;;                         display)))))
5569               (setq select-articles
5570                     (gnus-uncompress-range
5571                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5572                              (if (> tmp 0)
5573                                  tmp
5574                                1))
5575                            (cdr (gnus-active group)))))
5576               nil)
5577              (t
5578               nil))))
5579
5580     (gnus-summary-setup-default-charset)
5581
5582     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5583     (when (gnus-virtual-group-p group)
5584       (setq cached gnus-newsgroup-cached))
5585
5586     (setq gnus-newsgroup-unreads
5587           (gnus-sorted-ndifference
5588            (gnus-sorted-ndifference gnus-newsgroup-unreads
5589                                     gnus-newsgroup-marked)
5590            gnus-newsgroup-dormant))
5591
5592     (setq gnus-newsgroup-processable nil)
5593
5594     (gnus-update-read-articles group gnus-newsgroup-unreads)
5595
5596     ;; Adjust and set lists of article marks.
5597     (when info
5598       (gnus-adjust-marked-articles info))
5599     (if (setq articles select-articles)
5600         (setq gnus-newsgroup-unselected
5601               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5602       (setq articles (gnus-articles-to-read group read-all)))
5603
5604     (cond
5605      ((null articles)
5606       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5607       'quit)
5608      ((eq articles 0) nil)
5609      (t
5610       ;; Init the dependencies hash table.
5611       (setq gnus-newsgroup-dependencies
5612             (gnus-make-hashtable (length articles)))
5613       (gnus-set-global-variables)
5614       ;; Retrieve the headers and read them in.
5615
5616       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5617
5618       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5619       (when cached
5620         (setq gnus-newsgroup-cached cached))
5621
5622       ;; Suppress duplicates?
5623       (when gnus-suppress-duplicates
5624         (gnus-dup-suppress-articles))
5625
5626       ;; Set the initial limit.
5627       (setq gnus-newsgroup-limit (copy-sequence articles))
5628       ;; Remove canceled articles from the list of unread articles.
5629       (setq fetched-articles
5630             (mapcar (lambda (headers) (mail-header-number headers))
5631                     gnus-newsgroup-headers))
5632       (setq gnus-newsgroup-articles fetched-articles)
5633       (setq gnus-newsgroup-unreads
5634             (gnus-sorted-nintersection
5635              gnus-newsgroup-unreads fetched-articles))
5636       (gnus-compute-unseen-list)
5637
5638       ;; Removed marked articles that do not exist.
5639       (gnus-update-missing-marks
5640        (gnus-sorted-difference articles fetched-articles))
5641       ;; We might want to build some more threads first.
5642       (when (and gnus-fetch-old-headers
5643                  (eq gnus-headers-retrieved-by 'nov))
5644         (if (eq gnus-fetch-old-headers 'invisible)
5645             (gnus-build-all-threads)
5646           (gnus-build-old-threads)))
5647       ;; Let the Gnus agent mark articles as read.
5648       (when gnus-agent
5649         (gnus-agent-get-undownloaded-list))
5650       ;; Remove list identifiers from subject
5651       (when gnus-list-identifiers
5652         (gnus-summary-remove-list-identifiers))
5653       ;; Check whether auto-expire is to be done in this group.
5654       (setq gnus-newsgroup-auto-expire
5655             (gnus-group-auto-expirable-p group))
5656       ;; Set up the article buffer now, if necessary.
5657       (unless (and gnus-single-article-buffer
5658                    (equal gnus-article-buffer "*Article*"))
5659         (gnus-article-setup-buffer))
5660       ;; First and last article in this newsgroup.
5661       (when gnus-newsgroup-headers
5662         (setq gnus-newsgroup-begin
5663               (mail-header-number (car gnus-newsgroup-headers))
5664               gnus-newsgroup-end
5665               (mail-header-number
5666                (gnus-last-element gnus-newsgroup-headers))))
5667       ;; GROUP is successfully selected.
5668       (or gnus-newsgroup-headers t)))))
5669
5670 (defun gnus-compute-unseen-list ()
5671   ;; The `seen' marks are treated specially.
5672   (if (not gnus-newsgroup-seen)
5673       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5674     (setq gnus-newsgroup-unseen
5675           (gnus-inverse-list-range-intersection
5676            gnus-newsgroup-articles gnus-newsgroup-seen))))
5677
5678 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5679
5680 (defun gnus-summary-display-make-predicate (display)
5681   (require 'gnus-agent)
5682   (when (= (length display) 1)
5683     (setq display (car display)))
5684   (unless gnus-summary-display-cache
5685     (dolist (elem (append '((unread . unread)
5686                             (read . read)
5687                             (unseen . unseen))
5688                           gnus-article-mark-lists))
5689       (push (cons (cdr elem)
5690                   (gnus-byte-compile    ;Why bother?
5691                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5692             gnus-summary-display-cache)))
5693   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5694         (gnus-category-predicate-cache gnus-summary-display-cache))
5695     (gnus-get-predicate display)))
5696
5697 ;; Uses the dynamically bound `gnus-number' variable.
5698 (defvar gnus-number)
5699 (defun gnus-article-marked-p (type &optional article)
5700   (let ((article (or article gnus-number)))
5701     (cond
5702      ((eq type 'tick)
5703       (memq article gnus-newsgroup-marked))
5704      ((eq type 'spam)
5705       (memq article gnus-newsgroup-spam-marked))
5706      ((eq type 'unsend)
5707       (memq article gnus-newsgroup-unsendable))
5708      ((eq type 'undownload)
5709       (memq article gnus-newsgroup-undownloaded))
5710      ((eq type 'download)
5711       (memq article gnus-newsgroup-downloadable))
5712      ((eq type 'unread)
5713       (memq article gnus-newsgroup-unreads))
5714      ((eq type 'read)
5715       (memq article gnus-newsgroup-reads))
5716      ((eq type 'dormant)
5717       (memq article gnus-newsgroup-dormant) )
5718      ((eq type 'expire)
5719       (memq article gnus-newsgroup-expirable))
5720      ((eq type 'reply)
5721       (memq article gnus-newsgroup-replied))
5722      ((eq type 'killed)
5723       (memq article gnus-newsgroup-killed))
5724      ((eq type 'bookmark)
5725       (assq article gnus-newsgroup-bookmarks))
5726      ((eq type 'score)
5727       (assq article gnus-newsgroup-scored))
5728      ((eq type 'save)
5729       (memq article gnus-newsgroup-saved))
5730      ((eq type 'cache)
5731       (memq article gnus-newsgroup-cached))
5732      ((eq type 'forward)
5733       (memq article gnus-newsgroup-forwarded))
5734      ((eq type 'seen)
5735       (not (memq article gnus-newsgroup-unseen)))
5736      ((eq type 'recent)
5737       (memq article gnus-newsgroup-recent))
5738      (t t))))
5739
5740 (defun gnus-articles-to-read (group &optional read-all)
5741   "Find out what articles the user wants to read."
5742   (let* ((articles
5743           ;; Select all articles if `read-all' is non-nil, or if there
5744           ;; are no unread articles.
5745           (if (or read-all
5746                   (and (zerop (length gnus-newsgroup-marked))
5747                        (zerop (length gnus-newsgroup-unreads)))
5748                   ;; Fetch all if the predicate is non-nil.
5749                   gnus-newsgroup-display)
5750               ;; We want to select the headers for all the articles in
5751               ;; the group, so we select either all the active
5752               ;; articles in the group, or (if that's nil), the
5753               ;; articles in the cache.
5754               (or
5755                (if gnus-newsgroup-maximum-articles
5756                    (let ((active (gnus-active group)))
5757                      (gnus-uncompress-range
5758                       (cons (max (car active)
5759                                  (- (cdr active)
5760                                     gnus-newsgroup-maximum-articles
5761                                     -1))
5762                             (cdr active))))
5763                  (gnus-uncompress-range (gnus-active group)))
5764                (gnus-cache-articles-in-group group))
5765             ;; Select only the "normal" subset of articles.
5766             (gnus-sorted-nunion
5767              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5768              gnus-newsgroup-unreads)))
5769          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5770          (scored (length scored-list))
5771          (number (length articles))
5772          (marked (+ (length gnus-newsgroup-marked)
5773                     (length gnus-newsgroup-dormant)))
5774          (select
5775           (cond
5776            ((numberp read-all)
5777             read-all)
5778            ((numberp gnus-newsgroup-display)
5779             gnus-newsgroup-display)
5780            (t
5781             (condition-case ()
5782                 (cond
5783                  ((and (or (<= scored marked) (= scored number))
5784                        (numberp gnus-large-newsgroup)
5785                        (> number gnus-large-newsgroup))
5786                   (let* ((cursor-in-echo-area nil)
5787                          (initial (gnus-parameter-large-newsgroup-initial
5788                                    gnus-newsgroup-name))
5789                          (input
5790                           (read-string
5791                            (format
5792                             "How many articles from %s (%s %d): "
5793                             (gnus-group-decoded-name gnus-newsgroup-name)
5794                             (if initial "max" "default")
5795                             number)
5796                            (if initial
5797                                (cons (number-to-string initial)
5798                                      0)))))
5799                     (if (string-match "^[ \t]*$" input) number input)))
5800                  ((and (> scored marked) (< scored number)
5801                        (> (- scored number) 20))
5802                   (let ((input
5803                          (read-string
5804                           (format "%s %s (%d scored, %d total): "
5805                                   "How many articles from"
5806                                   (gnus-group-decoded-name group)
5807                                   scored number))))
5808                     (if (string-match "^[ \t]*$" input)
5809                         number input)))
5810                  (t number))
5811               (quit
5812                (message "Quit getting the articles to read")
5813                nil))))))
5814     (setq select (if (stringp select) (string-to-number select) select))
5815     (if (or (null select) (zerop select))
5816         select
5817       (if (and (not (zerop scored)) (<= (abs select) scored))
5818           (progn
5819             (setq articles (sort scored-list '<))
5820             (setq number (length articles)))
5821         (setq articles (copy-sequence articles)))
5822
5823       (when (< (abs select) number)
5824         (if (< select 0)
5825             ;; Select the N oldest articles.
5826             (setcdr (nthcdr (1- (abs select)) articles) nil)
5827           ;; Select the N most recent articles.
5828           (setq articles (nthcdr (- number select) articles))))
5829       (setq gnus-newsgroup-unselected
5830             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5831       (when gnus-alter-articles-to-read-function
5832         (setq articles
5833               (sort
5834                (funcall gnus-alter-articles-to-read-function
5835                         gnus-newsgroup-name articles)
5836                '<)))
5837       articles)))
5838
5839 (defun gnus-killed-articles (killed articles)
5840   (let (out)
5841     (while articles
5842       (when (inline (gnus-member-of-range (car articles) killed))
5843         (push (car articles) out))
5844       (setq articles (cdr articles)))
5845     out))
5846
5847 (defun gnus-uncompress-marks (marks)
5848   "Uncompress the mark ranges in MARKS."
5849   (let ((uncompressed '(score bookmark))
5850         out)
5851     (while marks
5852       (if (memq (caar marks) uncompressed)
5853           (push (car marks) out)
5854         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5855       (setq marks (cdr marks)))
5856     out))
5857
5858 (defun gnus-article-mark-to-type (mark)
5859   "Return the type of MARK."
5860   (or (cadr (assq mark gnus-article-special-mark-lists))
5861       'list))
5862
5863 (defun gnus-article-unpropagatable-p (mark)
5864   "Return whether MARK should be propagated to back end."
5865   (memq mark gnus-article-unpropagated-mark-lists))
5866
5867 (defun gnus-adjust-marked-articles (info)
5868   "Set all article lists and remove all marks that are no longer valid."
5869   (let* ((marked-lists (gnus-info-marks info))
5870          (active (gnus-active (gnus-info-group info)))
5871          (min (car active))
5872          (max (cdr active))
5873          (types gnus-article-mark-lists)
5874          marks var articles article mark mark-type
5875          bgn end)
5876     ;; Hack to avoid adjusting marks for imap.
5877     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5878               'nnimap)
5879       (setq min 1))
5880
5881     (dolist (marks marked-lists)
5882       (setq mark (car marks)
5883             mark-type (gnus-article-mark-to-type mark)
5884             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5885
5886       ;; We set the variable according to the type of the marks list,
5887       ;; and then adjust the marks to a subset of the active articles.
5888       (cond
5889        ;; Adjust "simple" lists - compressed yet unsorted
5890        ((eq mark-type 'list)
5891         ;; Simultaneously uncompress and clip to active range
5892         ;; See gnus-uncompress-range for a description of possible marks
5893         (let (l lh)
5894           (if (not (cadr marks))
5895               (set var nil)
5896             (setq articles (if (numberp (cddr marks))
5897                                (list (cdr marks))
5898                              (cdr marks))
5899                   lh (cons nil nil)
5900                   l lh)
5901
5902             (while (setq article (pop articles))
5903               (cond ((consp article)
5904                      (setq bgn (max (car article) min)
5905                            end (min (cdr article) max))
5906                      (while (<= bgn end)
5907                        (setq l (setcdr l (cons bgn nil))
5908                              bgn (1+ bgn))))
5909                     ((and (<= min article)
5910                           (>= max article))
5911                      (setq l (setcdr l (cons article nil))))))
5912             (set var (cdr lh)))))
5913        ;; Adjust assocs.
5914        ((eq mark-type 'tuple)
5915         (set var (setq articles (cdr marks)))
5916         (when (not (listp (cdr (symbol-value var))))
5917           (set var (list (symbol-value var))))
5918         (when (not (listp (cdr articles)))
5919           (setq articles (list articles)))
5920         (while articles
5921           (when (or (not (consp (setq article (pop articles))))
5922                     (< (car article) min)
5923                     (> (car article) max))
5924             (set var (delq article (symbol-value var))))))
5925        ;; Adjust ranges (sloppily).
5926        ((eq mark-type 'range)
5927         (cond
5928          ((eq mark 'seen)
5929           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5930           ;; It should be (seen (NUM1 . NUM2)).
5931           (when (numberp (cddr marks))
5932             (setcdr marks (list (cdr marks))))
5933           (setq articles (cdr marks))
5934           (while (and articles
5935                       (or (and (consp (car articles))
5936                                (> min (cdar articles)))
5937                           (and (numberp (car articles))
5938                                (> min (car articles)))))
5939             (pop articles))
5940           (set var articles))))))))
5941
5942 (defun gnus-update-missing-marks (missing)
5943   "Go through the list of MISSING articles and remove them from the mark lists."
5944   (when missing
5945     (let (var m)
5946       ;; Go through all types.
5947       (dolist (elem gnus-article-mark-lists)
5948         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5949           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5950           (when (symbol-value var)
5951             ;; This list has articles.  So we delete all missing
5952             ;; articles from it.
5953             (setq m missing)
5954             (while m
5955               (set var (delq (pop m) (symbol-value var))))))))))
5956
5957 (defun gnus-update-marks ()
5958   "Enter the various lists of marked articles into the newsgroup info list."
5959   (let ((types gnus-article-mark-lists)
5960         (info (gnus-get-info gnus-newsgroup-name))
5961         type list newmarked symbol delta-marks)
5962     (when info
5963       ;; Add all marks lists to the list of marks lists.
5964       (while (setq type (pop types))
5965         (setq list (symbol-value
5966                     (setq symbol
5967                           (intern (format "gnus-newsgroup-%s" (car type))))))
5968
5969         (when list
5970           ;; Get rid of the entries of the articles that have the
5971           ;; default score.
5972           (when (and (eq (cdr type) 'score)
5973                      gnus-save-score
5974                      list)
5975             (let* ((arts list)
5976                    (prev (cons nil list))
5977                    (all prev))
5978               (while arts
5979                 (if (or (not (consp (car arts)))
5980                         (= (cdar arts) gnus-summary-default-score))
5981                     (setcdr prev (cdr arts))
5982                   (setq prev arts))
5983                 (setq arts (cdr arts)))
5984               (setq list (cdr all)))))
5985
5986         (when (eq (cdr type) 'seen)
5987           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5988
5989         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5990           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5991
5992         (when (and (gnus-check-backend-function
5993                     'request-set-mark gnus-newsgroup-name)
5994                    (not (gnus-article-unpropagatable-p (cdr type))))
5995           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5996                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5997                  (add (gnus-remove-from-range
5998                        (gnus-copy-sequence list) old)))
5999             (when add
6000               (push (list add 'add (list (cdr type))) delta-marks))
6001             (when del
6002               ;; Don't delete marks from outside the active range.  This
6003               ;; shouldn't happen, but is a sanity check.
6004               (setq del (gnus-sorted-range-intersection
6005                          (gnus-active gnus-newsgroup-name) del))
6006               (push (list del 'del (list (cdr type))) delta-marks))))
6007
6008         (when list
6009           (push (cons (cdr type) list) newmarked)))
6010
6011       (when delta-marks
6012         (unless (gnus-check-group gnus-newsgroup-name)
6013           (error "Can't open server for %s" gnus-newsgroup-name))
6014         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6015
6016       ;; Enter these new marks into the info of the group.
6017       (if (nthcdr 3 info)
6018           (setcar (nthcdr 3 info) newmarked)
6019         ;; Add the marks lists to the end of the info.
6020         (when newmarked
6021           (setcdr (nthcdr 2 info) (list newmarked))))
6022
6023       ;; Cut off the end of the info if there's nothing else there.
6024       (let ((i 5))
6025         (while (and (> i 2)
6026                     (not (nth i info)))
6027           (when (nthcdr (decf i) info)
6028             (setcdr (nthcdr i info) nil)))))))
6029
6030 (defun gnus-set-mode-line (where)
6031   "Set the mode line of the article or summary buffers.
6032 If WHERE is `summary', the summary mode line format will be used."
6033   ;; Is this mode line one we keep updated?
6034   (when (and (memq where gnus-updated-mode-lines)
6035              (symbol-value
6036               (intern (format "gnus-%s-mode-line-format-spec" where))))
6037     (let (mode-string)
6038       ;; We evaluate this in the summary buffer since these
6039       ;; variables are buffer-local to that buffer.
6040       (with-current-buffer gnus-summary-buffer
6041         ;; We bind all these variables that are used in the `eval' form
6042         ;; below.
6043         (let* ((mformat (symbol-value
6044                          (intern
6045                           (format "gnus-%s-mode-line-format-spec" where))))
6046                (gnus-tmp-group-name (gnus-mode-string-quote
6047                                      (gnus-group-decoded-name
6048                                       gnus-newsgroup-name)))
6049                (gnus-tmp-article-number (or gnus-current-article 0))
6050                (gnus-tmp-unread gnus-newsgroup-unreads)
6051                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6052                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6053                (gnus-tmp-unread-and-unselected
6054                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6055                             (zerop gnus-tmp-unselected))
6056                        "")
6057                       ((zerop gnus-tmp-unselected)
6058                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6059                       (t (format "{%d(+%d) more}"
6060                                  gnus-tmp-unread-and-unticked
6061                                  gnus-tmp-unselected))))
6062                (gnus-tmp-subject
6063                 (if (and gnus-current-headers
6064                          (vectorp gnus-current-headers))
6065                     (gnus-mode-string-quote
6066                      (mail-header-subject gnus-current-headers))
6067                   ""))
6068                bufname-length max-len
6069                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6070           (setq mode-string (eval mformat))
6071           (setq bufname-length (if (string-match "%b" mode-string)
6072                                    (- (length
6073                                        (buffer-name
6074                                         (if (eq where 'summary)
6075                                             nil
6076                                           (get-buffer gnus-article-buffer))))
6077                                       2)
6078                                  0))
6079           (setq max-len (max 4 (if gnus-mode-non-string-length
6080                                    (- (window-width)
6081                                       gnus-mode-non-string-length
6082                                       bufname-length)
6083                                  (length mode-string))))
6084           ;; We might have to chop a bit of the string off...
6085           (when (> (length mode-string) max-len)
6086             (setq mode-string
6087                   (concat (truncate-string-to-width mode-string (- max-len 3))
6088                           "...")))))
6089       ;; Update the mode line.
6090       (setq mode-line-buffer-identification
6091             (gnus-mode-line-buffer-identification (list mode-string)))
6092       (set-buffer-modified-p t))))
6093
6094 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6095   "Go through the HEADERS list and add all Xrefs to a hash table.
6096 The resulting hash table is returned, or nil if no Xrefs were found."
6097   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6098          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6099          (xref-hashtb (gnus-make-hashtable))
6100          start group entry number xrefs header)
6101     (while headers
6102       (setq header (pop headers))
6103       (when (and (setq xrefs (mail-header-xref header))
6104                  (not (memq (setq number (mail-header-number header))
6105                             unreads)))
6106         (setq start 0)
6107         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6108           (setq start (match-end 0))
6109           (setq group (if prefix
6110                           (concat prefix (substring xrefs (match-beginning 1)
6111                                                     (match-end 1)))
6112                         (substring xrefs (match-beginning 1) (match-end 1))))
6113           (setq number
6114                 (string-to-number (substring xrefs (match-beginning 2)
6115                                           (match-end 2))))
6116           (if (setq entry (gnus-gethash group xref-hashtb))
6117               (setcdr entry (cons number (cdr entry)))
6118             (gnus-sethash group (cons number nil) xref-hashtb)))))
6119     (and start xref-hashtb)))
6120
6121 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6122   "Look through all the headers and mark the Xrefs as read."
6123   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6124         name info xref-hashtb idlist method nth4)
6125     (with-current-buffer gnus-group-buffer
6126       (when (setq xref-hashtb
6127                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6128         (mapatoms
6129          (lambda (group)
6130            (unless (string= from-newsgroup (setq name (symbol-name group)))
6131              (setq idlist (symbol-value group))
6132              ;; Dead groups are not updated.
6133              (and (prog1
6134                       (setq info (gnus-get-info name))
6135                     (when (stringp (setq nth4 (gnus-info-method info)))
6136                       (setq nth4 (gnus-server-to-method nth4))))
6137                   ;; Only do the xrefs if the group has the same
6138                   ;; select method as the group we have just read.
6139                   (or (gnus-methods-equal-p
6140                        nth4 (gnus-find-method-for-group from-newsgroup))
6141                       virtual
6142                       (equal nth4 (setq method (gnus-find-method-for-group
6143                                                 from-newsgroup)))
6144                       (and (equal (car nth4) (car method))
6145                            (equal (nth 1 nth4) (nth 1 method))))
6146                   gnus-use-cross-reference
6147                   (or (not (eq gnus-use-cross-reference t))
6148                       virtual
6149                       ;; Only do cross-references on subscribed
6150                       ;; groups, if that is what is wanted.
6151                       (<= (gnus-info-level info) gnus-level-subscribed))
6152                   (gnus-group-make-articles-read name idlist))))
6153          xref-hashtb)))))
6154
6155 (defun gnus-compute-read-articles (group articles)
6156   (let* ((entry (gnus-group-entry group))
6157          (info (nth 2 entry))
6158          (active (gnus-active group))
6159          ninfo)
6160     (when entry
6161       ;; First peel off all invalid article numbers.
6162       (when active
6163         (let ((ids articles)
6164               id first)
6165           (while (setq id (pop ids))
6166             (when (and first (> id (cdr active)))
6167               ;; We'll end up in this situation in one particular
6168               ;; obscure situation.  If you re-scan a group and get
6169               ;; a new article that is cross-posted to a different
6170               ;; group that has not been re-scanned, you might get
6171               ;; crossposted article that has a higher number than
6172               ;; Gnus believes possible.  So we re-activate this
6173               ;; group as well.  This might mean doing the
6174               ;; crossposting thingy will *increase* the number
6175               ;; of articles in some groups.  Tsk, tsk.
6176               (setq active (or (gnus-activate-group group) active)))
6177             (when (or (> id (cdr active))
6178                       (< id (car active)))
6179               (setq articles (delq id articles))))))
6180       ;; If the read list is nil, we init it.
6181       (if (and active
6182                (null (gnus-info-read info))
6183                (> (car active) 1))
6184           (setq ninfo (cons 1 (1- (car active))))
6185         (setq ninfo (gnus-info-read info)))
6186       ;; Then we add the read articles to the range.
6187       (gnus-add-to-range
6188        ninfo (setq articles (sort articles '<))))))
6189
6190 (defun gnus-group-make-articles-read (group articles)
6191   "Update the info of GROUP to say that ARTICLES are read."
6192   (let* ((num 0)
6193          (entry (gnus-group-entry group))
6194          (info (nth 2 entry))
6195          (active (gnus-active group))
6196          range)
6197     (if (not entry)
6198         ;; Group that Gnus doesn't know exists, but still allow the
6199         ;; backend to set marks.
6200         (gnus-request-set-mark
6201          group (list (list (gnus-compress-sequence (sort articles #'<))
6202                            'add '(read))))
6203       ;; Normal, subscribed groups.
6204       (setq range (gnus-compute-read-articles group articles))
6205       (with-current-buffer gnus-group-buffer
6206         (gnus-undo-register
6207           `(progn
6208              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6209              (gnus-info-set-read ',info ',(gnus-info-read info))
6210              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6211              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6212              (gnus-group-update-group ,group t))))
6213       ;; Add the read articles to the range.
6214       (gnus-info-set-read info range)
6215       (gnus-request-set-mark group (list (list range 'add '(read))))
6216       ;; Then we have to re-compute how many unread
6217       ;; articles there are in this group.
6218       (when active
6219         (cond
6220          ((not range)
6221           (setq num (- (1+ (cdr active)) (car active))))
6222          ((not (listp (cdr range)))
6223           (setq num (- (cdr active) (- (1+ (cdr range))
6224                                        (car range)))))
6225          (t
6226           (while range
6227             (if (numberp (car range))
6228                 (setq num (1+ num))
6229               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6230             (setq range (cdr range)))
6231           (setq num (- (cdr active) num))))
6232         ;; Update the number of unread articles.
6233         (setcar entry num)
6234         ;; Update the group buffer.
6235         (unless (gnus-ephemeral-group-p group)
6236           (gnus-group-update-group group t))))))
6237
6238 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6239   (let ((cur nntp-server-buffer)
6240         (dependencies
6241          (or dependencies
6242              (with-current-buffer gnus-summary-buffer
6243                gnus-newsgroup-dependencies)))
6244         headers id end ref number
6245         (mail-parse-charset gnus-newsgroup-charset)
6246         (mail-parse-ignored-charsets
6247          (save-current-buffer (condition-case nil
6248                                   (set-buffer gnus-summary-buffer)
6249                                 (error))
6250                               gnus-newsgroup-ignored-charsets)))
6251     (with-current-buffer nntp-server-buffer
6252       ;; Translate all TAB characters into SPACE characters.
6253       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6254       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6255       (ietf-drums-unfold-fws)
6256       (gnus-run-hooks 'gnus-parse-headers-hook)
6257       (let ((case-fold-search t)
6258             in-reply-to header p lines chars)
6259         (goto-char (point-min))
6260         ;; Search to the beginning of the next header.  Error messages
6261         ;; do not begin with 2 or 3.
6262         (while (re-search-forward "^[23][0-9]+ " nil t)
6263           (setq id nil
6264                 ref nil)
6265           ;; This implementation of this function, with nine
6266           ;; search-forwards instead of the one re-search-forward and
6267           ;; a case (which basically was the old function) is actually
6268           ;; about twice as fast, even though it looks messier.  You
6269           ;; can't have everything, I guess.  Speed and elegance
6270           ;; doesn't always go hand in hand.
6271           (setq
6272            header
6273            (vector
6274             ;; Number.
6275             (prog1
6276                 (setq number (read cur))
6277               (end-of-line)
6278               (setq p (point))
6279               (narrow-to-region (point)
6280                                 (or (and (search-forward "\n.\n" nil t)
6281                                          (- (point) 2))
6282                                     (point))))
6283             ;; Subject.
6284             (progn
6285               (goto-char p)
6286               (if (search-forward "\nsubject:" nil t)
6287                   (funcall gnus-decode-encoded-word-function
6288                            (nnheader-header-value))
6289                 "(none)"))
6290             ;; From.
6291             (progn
6292               (goto-char p)
6293               (if (search-forward "\nfrom:" nil t)
6294                   (funcall gnus-decode-encoded-address-function
6295                            (nnheader-header-value))
6296                 "(nobody)"))
6297             ;; Date.
6298             (progn
6299               (goto-char p)
6300               (if (search-forward "\ndate:" nil t)
6301                   (nnheader-header-value) ""))
6302             ;; Message-ID.
6303             (progn
6304               (goto-char p)
6305               (setq id (if (re-search-forward
6306                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6307                            ;; We do it this way to make sure the Message-ID
6308                            ;; is (somewhat) syntactically valid.
6309                            (buffer-substring (match-beginning 1)
6310                                              (match-end 1))
6311                          ;; If there was no message-id, we just fake one
6312                          ;; to make subsequent routines simpler.
6313                          (nnheader-generate-fake-message-id number))))
6314             ;; References.
6315             (progn
6316               (goto-char p)
6317               (if (search-forward "\nreferences:" nil t)
6318                   (progn
6319                     (setq end (point))
6320                     (prog1
6321                         (nnheader-header-value)
6322                       (setq ref
6323                             (buffer-substring
6324                              (progn
6325                                (end-of-line)
6326                                (search-backward ">" end t)
6327                                (1+ (point)))
6328                              (progn
6329                                (search-backward "<" end t)
6330                                (point))))))
6331                 ;; Get the references from the in-reply-to header if there
6332                 ;; were no references and the in-reply-to header looks
6333                 ;; promising.
6334                 (if (and (search-forward "\nin-reply-to:" nil t)
6335                          (setq in-reply-to (nnheader-header-value))
6336                          (string-match "<[^>]+>" in-reply-to))
6337                     (let (ref2)
6338                       (setq ref (substring in-reply-to (match-beginning 0)
6339                                            (match-end 0)))
6340                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6341                         (setq ref2 (substring in-reply-to (match-beginning 0)
6342                                               (match-end 0)))
6343                         (when (> (length ref2) (length ref))
6344                           (setq ref ref2)))
6345                       ref)
6346                   (setq ref nil))))
6347             ;; Chars.
6348             (progn
6349               (goto-char p)
6350               (if (search-forward "\nchars: " nil t)
6351                   (if (numberp (setq chars (ignore-errors (read cur))))
6352                       chars -1)
6353                 -1))
6354             ;; Lines.
6355             (progn
6356               (goto-char p)
6357               (if (search-forward "\nlines: " nil t)
6358                   (if (numberp (setq lines (ignore-errors (read cur))))
6359                       lines -1)
6360                 -1))
6361             ;; Xref.
6362             (progn
6363               (goto-char p)
6364               (and (search-forward "\nxref:" nil t)
6365                    (nnheader-header-value)))
6366             ;; Extra.
6367             (when gnus-extra-headers
6368               (let ((extra gnus-extra-headers)
6369                     out)
6370                 (while extra
6371                   (goto-char p)
6372                   (when (search-forward
6373                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6374                     (push (cons (car extra) (nnheader-header-value))
6375                           out))
6376                   (pop extra))
6377                 out))))
6378           (when (equal id ref)
6379             (setq ref nil))
6380
6381           (when gnus-alter-header-function
6382             (funcall gnus-alter-header-function header)
6383             (setq id (mail-header-id header)
6384                   ref (gnus-parent-id (mail-header-references header))))
6385
6386           (when (setq header
6387                       (gnus-dependencies-add-header
6388                        header dependencies force-new))
6389             (push header headers))
6390           (goto-char (point-max))
6391           (widen))
6392         (nreverse headers)))))
6393
6394 ;; Goes through the xover lines and returns a list of vectors
6395 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6396                                                   force-new dependencies
6397                                                   group also-fetch-heads)
6398   "Parse the news overview data in the server buffer.
6399 Return a list of headers that match SEQUENCE (see
6400 `nntp-retrieve-headers')."
6401   ;; Get the Xref when the users reads the articles since most/some
6402   ;; NNTP servers do not include Xrefs when using XOVER.
6403   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6404   (let ((mail-parse-charset gnus-newsgroup-charset)
6405         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6406         (cur nntp-server-buffer)
6407         (dependencies (or dependencies gnus-newsgroup-dependencies))
6408         (allp (cond
6409                ((eq gnus-read-all-available-headers t)
6410                 t)
6411                ((and (stringp gnus-read-all-available-headers)
6412                      group)
6413                 (string-match gnus-read-all-available-headers group))
6414                (t
6415                 nil)))
6416         number headers header)
6417     (with-current-buffer nntp-server-buffer
6418       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6419       ;; Allow the user to mangle the headers before parsing them.
6420       (gnus-run-hooks 'gnus-parse-headers-hook)
6421       (goto-char (point-min))
6422       (gnus-parse-without-error
6423         (while (and (or sequence allp)
6424                     (not (eobp)))
6425           (setq number (read cur))
6426           (when (not allp)
6427             (while (and sequence
6428                         (< (car sequence) number))
6429               (setq sequence (cdr sequence))))
6430           (when (and (or allp
6431                          (and sequence
6432                               (eq number (car sequence))))
6433                      (progn
6434                        (setq sequence (cdr sequence))
6435                        (setq header (inline
6436                                       (gnus-nov-parse-line
6437                                        number dependencies force-new)))))
6438             (push header headers))
6439           (forward-line 1)))
6440       ;; A common bug in inn is that if you have posted an article and
6441       ;; then retrieves the active file, it will answer correctly --
6442       ;; the new article is included.  However, a NOV entry for the
6443       ;; article may not have been generated yet, so this may fail.
6444       ;; We work around this problem by retrieving the last few
6445       ;; headers using HEAD.
6446       (if (or (not also-fetch-heads)
6447               (not sequence))
6448           ;; We (probably) got all the headers.
6449           (nreverse headers)
6450         (let ((gnus-nov-is-evil t))
6451           (nconc
6452            (nreverse headers)
6453            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6454              (gnus-get-newsgroup-headers))))))))
6455
6456 (defun gnus-article-get-xrefs ()
6457   "Fill in the Xref value in `gnus-current-headers', if necessary.
6458 This is meant to be called in `gnus-article-internal-prepare-hook'."
6459   (let ((headers (with-current-buffer gnus-summary-buffer
6460                    gnus-current-headers)))
6461     (or (not gnus-use-cross-reference)
6462         (not headers)
6463         (and (mail-header-xref headers)
6464              (not (string= (mail-header-xref headers) "")))
6465         (let ((case-fold-search t)
6466               xref)
6467           (save-restriction
6468             (nnheader-narrow-to-headers)
6469             (goto-char (point-min))
6470             (when (or (and (not (eobp))
6471                            (eq (downcase (char-after)) ?x)
6472                            (looking-at "Xref:"))
6473                       (search-forward "\nXref:" nil t))
6474               (goto-char (1+ (match-end 0)))
6475               (setq xref (buffer-substring (point) (point-at-eol)))
6476               (mail-header-set-xref headers xref)))))))
6477
6478 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6479   "Find article ID and insert the summary line for that article.
6480 OLD-HEADER can either be a header or a line number to insert
6481 the subject line on."
6482   (let* ((line (and (numberp old-header) old-header))
6483          (old-header (and (vectorp old-header) old-header))
6484          (header (cond ((and old-header use-old-header)
6485                         old-header)
6486                        ((and (numberp id)
6487                              (gnus-number-to-header id))
6488                         (gnus-number-to-header id))
6489                        (t
6490                         (gnus-read-header id))))
6491          (number (and (numberp id) id))
6492          d)
6493     (when header
6494       ;; Rebuild the thread that this article is part of and go to the
6495       ;; article we have fetched.
6496       (when (and (not gnus-show-threads)
6497                  old-header)
6498         (when (and number
6499                    (setq d (gnus-data-find (mail-header-number old-header))))
6500           (goto-char (gnus-data-pos d))
6501           (gnus-data-remove
6502            number
6503            (- (point-at-bol)
6504               (prog1
6505                   (1+ (point-at-eol))
6506                 (gnus-delete-line))))))
6507       ;; Remove list identifiers from subject.
6508       (when gnus-list-identifiers
6509         (let ((gnus-newsgroup-headers (list header)))
6510           (gnus-summary-remove-list-identifiers)))
6511       (when old-header
6512         (mail-header-set-number header (mail-header-number old-header)))
6513       (setq gnus-newsgroup-sparse
6514             (delq (setq number (mail-header-number header))
6515                   gnus-newsgroup-sparse))
6516       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6517       (push number gnus-newsgroup-limit)
6518       (gnus-rebuild-thread (mail-header-id header) line)
6519       (gnus-summary-goto-subject number nil t))
6520     (when (and (numberp number)
6521                (> number 0))
6522       ;; We have to update the boundaries even if we can't fetch the
6523       ;; article if ID is a number -- so that the next `P' or `N'
6524       ;; command will fetch the previous (or next) article even
6525       ;; if the one we tried to fetch this time has been canceled.
6526       (when (> number gnus-newsgroup-end)
6527         (setq gnus-newsgroup-end number))
6528       (when (< number gnus-newsgroup-begin)
6529         (setq gnus-newsgroup-begin number))
6530       (setq gnus-newsgroup-unselected
6531             (delq number gnus-newsgroup-unselected)))
6532     ;; Report back a success?
6533     (and header (mail-header-number header))))
6534
6535 ;;; Process/prefix in the summary buffer
6536
6537 (defun gnus-summary-work-articles (n)
6538   "Return a list of articles to be worked upon.
6539 The prefix argument, the list of process marked articles, and the
6540 current article will be taken into consideration."
6541   (with-current-buffer gnus-summary-buffer
6542     (cond
6543      (n
6544       ;; A numerical prefix has been given.
6545       (setq n (prefix-numeric-value n))
6546       (let ((backward (< n 0))
6547             (n (abs (prefix-numeric-value n)))
6548             articles article)
6549         (save-excursion
6550           (while
6551               (and (> n 0)
6552                    (push (setq article (gnus-summary-article-number))
6553                          articles)
6554                    (if backward
6555                        (gnus-summary-find-prev nil article)
6556                      (gnus-summary-find-next nil article)))
6557             (decf n)))
6558         (nreverse articles)))
6559      ((and (gnus-region-active-p) (mark))
6560       (message "region active")
6561       ;; Work on the region between point and mark.
6562       (let ((max (max (point) (mark)))
6563             articles article)
6564         (save-excursion
6565           (goto-char (min (point) (mark)))
6566           (while
6567               (and
6568                (push (setq article (gnus-summary-article-number)) articles)
6569                (gnus-summary-find-next nil article)
6570                (< (point) max)))
6571           (nreverse articles))))
6572      (gnus-newsgroup-processable
6573       ;; There are process-marked articles present.
6574       ;; Save current state.
6575       (gnus-summary-save-process-mark)
6576       ;; Return the list.
6577       (reverse gnus-newsgroup-processable))
6578      (t
6579       ;; Just return the current article.
6580       (list (gnus-summary-article-number))))))
6581
6582 (defmacro gnus-summary-iterate (arg &rest forms)
6583   "Iterate over the process/prefixed articles and do FORMS.
6584 ARG is the interactive prefix given to the command.  FORMS will be
6585 executed with point over the summary line of the articles."
6586   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6587     `(let ((,articles (gnus-summary-work-articles ,arg)))
6588        (while ,articles
6589          (gnus-summary-goto-subject (car ,articles))
6590          ,@forms
6591          (pop ,articles)))))
6592
6593 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6594 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6595
6596 (defun gnus-summary-save-process-mark ()
6597   "Push the current set of process marked articles on the stack."
6598   (interactive)
6599   (push (copy-sequence gnus-newsgroup-processable)
6600         gnus-newsgroup-process-stack))
6601
6602 (defun gnus-summary-kill-process-mark ()
6603   "Push the current set of process marked articles on the stack and unmark."
6604   (interactive)
6605   (gnus-summary-save-process-mark)
6606   (gnus-summary-unmark-all-processable))
6607
6608 (defun gnus-summary-yank-process-mark ()
6609   "Pop the last process mark state off the stack and restore it."
6610   (interactive)
6611   (unless gnus-newsgroup-process-stack
6612     (error "Empty mark stack"))
6613   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6614
6615 (defun gnus-summary-process-mark-set (set)
6616   "Make SET into the current process marked articles."
6617   (gnus-summary-unmark-all-processable)
6618   (mapc 'gnus-summary-set-process-mark set))
6619
6620 ;;; Searching and stuff
6621
6622 (defun gnus-summary-search-group (&optional backward use-level)
6623   "Search for next unread newsgroup.
6624 If optional argument BACKWARD is non-nil, search backward instead."
6625   (with-current-buffer gnus-group-buffer
6626     (when (gnus-group-search-forward
6627            backward nil (if use-level (gnus-group-group-level) nil))
6628       (gnus-group-group-name))))
6629
6630 (defun gnus-summary-best-group (&optional exclude-group)
6631   "Find the name of the best unread group.
6632 If EXCLUDE-GROUP, do not go to this group."
6633   (with-current-buffer gnus-group-buffer
6634     (save-excursion
6635       (gnus-group-best-unread-group exclude-group))))
6636
6637 (defun gnus-summary-find-next (&optional unread article backward)
6638   (if backward
6639       (gnus-summary-find-prev unread article)
6640     (let* ((dummy (gnus-summary-article-intangible-p))
6641            (article (or article (gnus-summary-article-number)))
6642            (data (gnus-data-find-list article))
6643            result)
6644       (when (and (not dummy)
6645                  (or (not gnus-summary-check-current)
6646                      (not unread)
6647                      (not (gnus-data-unread-p (car data)))))
6648         (setq data (cdr data)))
6649       (when (setq result
6650                   (if unread
6651                       (progn
6652                         (while data
6653                           (unless (memq (gnus-data-number (car data))
6654                                         (cond
6655                                          ((eq gnus-auto-goto-ignores
6656                                               'always-undownloaded)
6657                                           gnus-newsgroup-undownloaded)
6658                                          (gnus-plugged
6659                                           nil)
6660                                          ((eq gnus-auto-goto-ignores
6661                                               'unfetched)
6662                                           gnus-newsgroup-unfetched)
6663                                          ((eq gnus-auto-goto-ignores
6664                                               'undownloaded)
6665                                           gnus-newsgroup-undownloaded)))
6666                             (when (gnus-data-unread-p (car data))
6667                               (setq result (car data)
6668                                     data nil)))
6669                           (setq data (cdr data)))
6670                         result)
6671                     (car data)))
6672         (goto-char (gnus-data-pos result))
6673         (gnus-data-number result)))))
6674
6675 (defun gnus-summary-find-prev (&optional unread article)
6676   (let* ((eobp (eobp))
6677          (article (or article (gnus-summary-article-number)))
6678          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6679          result)
6680     (when (and (not eobp)
6681                (or (not gnus-summary-check-current)
6682                    (not unread)
6683                    (not (gnus-data-unread-p (car data)))))
6684       (setq data (cdr data)))
6685     (when (setq result
6686                 (if unread
6687                     (progn
6688                       (while data
6689                         (unless (memq (gnus-data-number (car data))
6690                                       (cond
6691                                        ((eq gnus-auto-goto-ignores
6692                                             'always-undownloaded)
6693                                         gnus-newsgroup-undownloaded)
6694                                        (gnus-plugged
6695                                         nil)
6696                                        ((eq gnus-auto-goto-ignores
6697                                             'unfetched)
6698                                         gnus-newsgroup-unfetched)
6699                                        ((eq gnus-auto-goto-ignores
6700                                             'undownloaded)
6701                                         gnus-newsgroup-undownloaded)))
6702                           (when (gnus-data-unread-p (car data))
6703                             (setq result (car data)
6704                                   data nil)))
6705                         (setq data (cdr data)))
6706                       result)
6707                   (car data)))
6708       (goto-char (gnus-data-pos result))
6709       (gnus-data-number result))))
6710
6711 (defun gnus-summary-find-subject (subject &optional unread backward article)
6712   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6713          (article (or article (gnus-summary-article-number)))
6714          (articles (gnus-data-list backward))
6715          (arts (gnus-data-find-list article articles))
6716          result)
6717     (when (or (not gnus-summary-check-current)
6718               (not unread)
6719               (not (gnus-data-unread-p (car arts))))
6720       (setq arts (cdr arts)))
6721     (while arts
6722       (and (or (not unread)
6723                (gnus-data-unread-p (car arts)))
6724            (vectorp (gnus-data-header (car arts)))
6725            (gnus-subject-equal
6726             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6727            (setq result (car arts)
6728                  arts nil))
6729       (setq arts (cdr arts)))
6730     (and result
6731          (goto-char (gnus-data-pos result))
6732          (gnus-data-number result))))
6733
6734 (defun gnus-summary-search-forward (&optional unread subject backward)
6735   "Search forward for an article.
6736 If UNREAD, look for unread articles.  If SUBJECT, look for
6737 articles with that subject.  If BACKWARD, search backward instead."
6738   (cond (subject (gnus-summary-find-subject subject unread backward))
6739         (backward (gnus-summary-find-prev unread))
6740         (t (gnus-summary-find-next unread))))
6741
6742 (defun gnus-recenter (&optional n)
6743   "Center point in window and redisplay frame.
6744 Also do horizontal recentering."
6745   (interactive "P")
6746   (when (and gnus-auto-center-summary
6747              (not (eq gnus-auto-center-summary 'vertical)))
6748     (gnus-horizontal-recenter))
6749   (if (fboundp 'recenter-top-bottom)
6750       (recenter-top-bottom n)
6751     (recenter n)))
6752
6753 (put 'gnus-recenter 'isearch-scroll t)
6754
6755 (defun gnus-forward-line-ignore-invisible (n)
6756   "Move N lines forward (backward if N is negative).
6757 Like forward-line, but skip over (and don't count) invisible lines."
6758   (let (done)
6759     (while (and (> n 0) (not done))
6760       ;; If the following character is currently invisible,
6761       ;; skip all characters with that same `invisible' property value.
6762       (while (gnus-invisible-p (point))
6763         (goto-char (gnus-next-char-property-change (point))))
6764       (forward-line 1)
6765       (if (eobp)
6766           (setq done t)
6767         (setq n (1- n))))
6768     (while (and (< n 0) (not done))
6769       (forward-line -1)
6770       (if (bobp) (setq done t)
6771         (setq n (1+ n))
6772         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6773           (goto-char (gnus-previous-char-property-change (point))))))))
6774
6775 (defun gnus-summary-recenter ()
6776   "Center point in the summary window.
6777 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6778 displayed, no centering will be performed."
6779   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6780   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6781   (interactive)
6782   ;; The user has to want it.
6783   (when gnus-auto-center-summary
6784     (let* ((top (cond ((< (window-height) 4) 0)
6785                       ((< (window-height) 7) 1)
6786                       (t (if (numberp gnus-auto-center-summary)
6787                              gnus-auto-center-summary
6788                            (/ (1- (window-height)) 2)))))
6789            (height (1- (window-height)))
6790            (bottom (save-excursion
6791                      (goto-char (point-max))
6792                      (gnus-forward-line-ignore-invisible (- height))
6793                      (point)))
6794            (window (get-buffer-window (current-buffer))))
6795       (when (get-buffer-window gnus-article-buffer)
6796         ;; Only do recentering when the article buffer is displayed,
6797         ;; Set the window start to either `bottom', which is the biggest
6798         ;; possible valid number, or the second line from the top,
6799         ;; whichever is the least.
6800         (let ((top-pos (save-excursion
6801                          (gnus-forward-line-ignore-invisible (- top))
6802                          (point))))
6803           (if (> bottom top-pos)
6804               ;; Keep the second line from the top visible
6805               (set-window-start window top-pos)
6806             ;; Try to keep the bottom line visible; if it's partially
6807             ;; obscured, either scroll one more line to make it fully
6808             ;; visible, or revert to using TOP-POS.
6809             (save-excursion
6810               (goto-char (point-max))
6811               (gnus-forward-line-ignore-invisible -1)
6812               (let ((last-line-start (point)))
6813                 (goto-char bottom)
6814                 (set-window-start window (point) t)
6815                 (when (not (pos-visible-in-window-p last-line-start window))
6816                   (gnus-forward-line-ignore-invisible 1)
6817                   (set-window-start window (min (point) top-pos) t)))))))
6818       ;; Do horizontal recentering while we're at it.
6819       (when (and (get-buffer-window (current-buffer) t)
6820                  (not (eq gnus-auto-center-summary 'vertical)))
6821         (let ((selected (selected-window)))
6822           (select-window (get-buffer-window (current-buffer) t))
6823           (gnus-summary-position-point)
6824           (gnus-horizontal-recenter)
6825           (select-window selected))))))
6826
6827 (defun gnus-summary-jump-to-group (newsgroup)
6828   "Move point to NEWSGROUP in group mode buffer."
6829   ;; Keep update point of group mode buffer if visible.
6830   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6831       (save-window-excursion
6832         ;; Take care of tree window mode.
6833         (when (get-buffer-window gnus-group-buffer)
6834           (pop-to-buffer gnus-group-buffer))
6835         (gnus-group-jump-to-group newsgroup))
6836     (save-excursion
6837       ;; Take care of tree window mode.
6838       (if (get-buffer-window gnus-group-buffer 0)
6839           (pop-to-buffer gnus-group-buffer)
6840         (set-buffer gnus-group-buffer))
6841       (gnus-group-jump-to-group newsgroup))))
6842
6843 ;; This function returns a list of article numbers based on the
6844 ;; difference between the ranges of read articles in this group and
6845 ;; the range of active articles.
6846 (defun gnus-list-of-unread-articles (group)
6847   (let* ((read (gnus-info-read (gnus-get-info group)))
6848          (active (or (gnus-active group) (gnus-activate-group group)))
6849          (last (or (cdr active)
6850                    (error "Group %s couldn't be activated " group)))
6851          (bottom (if gnus-newsgroup-maximum-articles
6852                      (max (car active)
6853                           (- last gnus-newsgroup-maximum-articles -1))
6854                    (car active)))
6855          first nlast unread)
6856     ;; If none are read, then all are unread.
6857     (if (not read)
6858         (setq first bottom)
6859       ;; If the range of read articles is a single range, then the
6860       ;; first unread article is the article after the last read
6861       ;; article.  Sounds logical, doesn't it?
6862       (if (and (not (listp (cdr read)))
6863                (or (< (car read) bottom)
6864                    (progn (setq read (list read))
6865                           nil)))
6866           (setq first (max bottom (1+ (cdr read))))
6867         ;; `read' is a list of ranges.
6868         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6869                                   (caar read)))
6870                   1)
6871           (setq first bottom))
6872         (while read
6873           (when first
6874             (while (< first nlast)
6875               (setq unread (cons first unread)
6876                     first (1+ first))))
6877           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6878           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6879           (setq read (cdr read)))))
6880     ;; And add the last unread articles.
6881     (while (<= first last)
6882       (setq unread (cons first unread)
6883             first (1+ first)))
6884     ;; Return the list of unread articles.
6885     (delq 0 (nreverse unread))))
6886
6887 (defun gnus-list-of-read-articles (group)
6888   "Return a list of unread, unticked and non-dormant articles."
6889   (let* ((info (gnus-get-info group))
6890          (marked (gnus-info-marks info))
6891          (active (gnus-active group)))
6892     (and info active
6893          (gnus-list-range-difference
6894           (gnus-list-range-difference
6895            (gnus-sorted-complement
6896             (gnus-uncompress-range
6897              (if gnus-newsgroup-maximum-articles
6898                  (cons (max (car active)
6899                             (- (cdr active)
6900                                gnus-newsgroup-maximum-articles
6901                                -1))
6902                        (cdr active))
6903                active))
6904             (gnus-list-of-unread-articles group))
6905            (cdr (assq 'dormant marked)))
6906           (cdr (assq 'tick marked))))))
6907
6908 ;; This function returns a sequence of article numbers based on the
6909 ;; difference between the ranges of read articles in this group and
6910 ;; the range of active articles.
6911 (defun gnus-sequence-of-unread-articles (group)
6912   (let* ((read (gnus-info-read (gnus-get-info group)))
6913          (active (or (gnus-active group) (gnus-activate-group group)))
6914          (last (cdr active))
6915          (bottom (if gnus-newsgroup-maximum-articles
6916                      (max (car active)
6917                           (- last gnus-newsgroup-maximum-articles -1))
6918                    (car active)))
6919          first nlast unread)
6920     ;; If none are read, then all are unread.
6921     (if (not read)
6922         (setq first bottom)
6923       ;; If the range of read articles is a single range, then the
6924       ;; first unread article is the article after the last read
6925       ;; article.  Sounds logical, doesn't it?
6926       (if (and (not (listp (cdr read)))
6927                (or (< (car read) bottom)
6928                    (progn (setq read (list read))
6929                           nil)))
6930           (setq first (max bottom (1+ (cdr read))))
6931         ;; `read' is a list of ranges.
6932         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6933                                   (caar read)))
6934                   1)
6935           (setq first bottom))
6936         (while read
6937           (when first
6938             (push (cons first nlast) unread))
6939           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6940           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6941           (setq read (cdr read)))))
6942     ;; And add the last unread articles.
6943     (cond ((not (and first last))
6944            nil)
6945           ((< first last)
6946            (push (cons first last) unread))
6947           ((= first last)
6948            (push first unread)))
6949     ;; Return the sequence of unread articles.
6950     (delq 0 (nreverse unread))))
6951
6952 ;; Various summary commands
6953
6954 (defun gnus-summary-select-article-buffer ()
6955   "Reconfigure windows to show the article buffer.
6956 If `gnus-widen-article-buffer' is set, show only the article
6957 buffer."
6958   (interactive)
6959   (if (not (gnus-buffer-live-p gnus-article-buffer))
6960       (error "There is no article buffer for this summary buffer")
6961     (unless (get-buffer-window gnus-article-buffer)
6962       (gnus-summary-show-article))
6963     (gnus-configure-windows
6964      (if gnus-widen-article-window
6965          'only-article
6966        'article)
6967      t)
6968     (select-window (get-buffer-window gnus-article-buffer))))
6969
6970 (defun gnus-summary-universal-argument (arg)
6971   "Perform any operation on all articles that are process/prefixed."
6972   (interactive "P")
6973   (let ((articles (gnus-summary-work-articles arg))
6974         func article)
6975     (if (eq
6976          (setq
6977           func
6978           (key-binding
6979            (read-key-sequence
6980             (substitute-command-keys
6981              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6982          'undefined)
6983         (gnus-error 1 "Undefined key")
6984       (save-excursion
6985         (while articles
6986           (gnus-summary-goto-subject (setq article (pop articles)))
6987           (let (gnus-newsgroup-processable)
6988             (command-execute func))
6989           (gnus-summary-remove-process-mark article)))))
6990   (gnus-summary-position-point))
6991
6992 (defun gnus-summary-toggle-truncation (&optional arg)
6993   "Toggle truncation of summary lines.
6994 With ARG, turn line truncation on if ARG is positive."
6995   (interactive "P")
6996   (setq truncate-lines
6997         (if (null arg) (not truncate-lines)
6998           (> (prefix-numeric-value arg) 0)))
6999   (redraw-display))
7000
7001 (defun gnus-summary-find-for-reselect ()
7002   "Return the number of an article to stay on across a reselect.
7003 The current article is considered, then following articles, then previous
7004 articles.  An article is sought which is not cancelled and isn't a temporary
7005 insertion from another group.  If there's no such then return a dummy 0."
7006   (let (found)
7007     (dolist (rev '(nil t))
7008       (unless found      ; don't demand the reverse list if we don't need it
7009         (let ((data (gnus-data-find-list
7010                      (gnus-summary-article-number) (gnus-data-list rev))))
7011           (while (and data (not found))
7012             (if (and (< 0 (gnus-data-number (car data)))
7013                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7014                 (setq found (gnus-data-number (car data))))
7015             (setq data (cdr data))))))
7016     (or found 0)))
7017
7018 (defun gnus-summary-reselect-current-group (&optional all rescan)
7019   "Exit and then reselect the current newsgroup.
7020 The prefix argument ALL means to select all articles."
7021   (interactive "P")
7022   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7023     (error "Ephemeral groups can't be reselected"))
7024   (let ((current-subject (gnus-summary-find-for-reselect))
7025         (group gnus-newsgroup-name))
7026     (setq gnus-newsgroup-begin nil)
7027     (gnus-summary-exit nil 'leave-hidden)
7028     ;; We have to adjust the point of group mode buffer because
7029     ;; point was moved to the next unread newsgroup by exiting.
7030     (gnus-summary-jump-to-group group)
7031     (when rescan
7032       (save-excursion
7033         (gnus-group-get-new-news-this-group 1)))
7034     (gnus-group-read-group all t)
7035     (gnus-summary-goto-subject current-subject nil t)))
7036
7037 (defun gnus-summary-rescan-group (&optional all)
7038   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7039   (interactive "P")
7040   (let ((config gnus-current-window-configuration))
7041     (gnus-summary-reselect-current-group all t)
7042     (gnus-configure-windows config)
7043     (when (eq config 'article)
7044       (gnus-summary-select-article))))
7045
7046 (defun gnus-summary-update-info (&optional non-destructive)
7047   (save-excursion
7048     (let ((group gnus-newsgroup-name))
7049       (when group
7050         (when gnus-newsgroup-kill-headers
7051           (setq gnus-newsgroup-killed
7052                 (gnus-compress-sequence
7053                  (gnus-sorted-union
7054                   (gnus-list-range-intersection
7055                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7056                   gnus-newsgroup-unreads)
7057                  t)))
7058         (unless (listp (cdr gnus-newsgroup-killed))
7059           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7060         (let ((headers gnus-newsgroup-headers))
7061           ;; Set the new ranges of read articles.
7062           (with-current-buffer gnus-group-buffer
7063             (gnus-undo-force-boundary))
7064           (gnus-update-read-articles
7065            group (gnus-sorted-union
7066                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7067           ;; Set the current article marks.
7068           (let ((gnus-newsgroup-scored
7069                  (if (and (not gnus-save-score)
7070                           (not non-destructive))
7071                      nil
7072                    gnus-newsgroup-scored)))
7073             (save-excursion
7074               (gnus-update-marks)))
7075           ;; Do the cross-ref thing.
7076           (when gnus-use-cross-reference
7077             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7078           ;; Do not switch windows but change the buffer to work.
7079           (set-buffer gnus-group-buffer)
7080           (unless (gnus-ephemeral-group-p group)
7081             (gnus-group-update-group group)))))))
7082
7083 (defun gnus-summary-save-newsrc (&optional force)
7084   "Save the current number of read/marked articles in the dribble buffer.
7085 The dribble buffer will then be saved.
7086 If FORCE (the prefix), also save the .newsrc file(s)."
7087   (interactive "P")
7088   (gnus-summary-update-info t)
7089   (if force
7090       (gnus-save-newsrc-file)
7091     (gnus-dribble-save)))
7092
7093 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7094
7095 (defun gnus-summary-exit (&optional temporary leave-hidden)
7096   "Exit reading current newsgroup, and then return to group selection mode.
7097 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7098   (interactive)
7099   (gnus-set-global-variables)
7100   (when (gnus-buffer-live-p gnus-article-buffer)
7101     (with-current-buffer gnus-article-buffer
7102       (mm-destroy-parts gnus-article-mime-handles)
7103       ;; Set it to nil for safety reason.
7104       (setq gnus-article-mime-handle-alist nil)
7105       (setq gnus-article-mime-handles nil)))
7106   (gnus-kill-save-kill-buffer)
7107   (gnus-async-halt-prefetch)
7108   (let* ((group gnus-newsgroup-name)
7109          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7110          (gnus-group-is-exiting-p t)
7111          (mode major-mode)
7112          (group-point nil)
7113          (buf (current-buffer)))
7114     (unless quit-config
7115       ;; Do adaptive scoring, and possibly save score files.
7116       (when gnus-newsgroup-adaptive
7117         (gnus-score-adaptive))
7118       (when gnus-use-scoring
7119         (gnus-score-save)))
7120     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7121     (when gnus-use-cache
7122       (gnus-cache-possibly-remove-articles)
7123       (gnus-cache-save-buffers))
7124     (gnus-async-prefetch-remove-group group)
7125     (when gnus-suppress-duplicates
7126       (gnus-dup-enter-articles))
7127     (when gnus-use-trees
7128       (gnus-tree-close group))
7129     (when gnus-use-cache
7130       (gnus-cache-write-active))
7131     ;; Remove entries for this group.
7132     (nnmail-purge-split-history (gnus-group-real-name group))
7133     ;; Make all changes in this group permanent.
7134     (unless quit-config
7135       (gnus-run-hooks 'gnus-exit-group-hook)
7136       (gnus-summary-update-info))
7137     (gnus-close-group group)
7138     ;; Make sure where we were, and go to next newsgroup.
7139     (set-buffer gnus-group-buffer)
7140     (unless quit-config
7141       (gnus-group-jump-to-group group))
7142     (gnus-run-hooks 'gnus-summary-exit-hook)
7143     (unless (or quit-config
7144                 (not gnus-summary-next-group-on-exit)
7145                 ;; If this group has disappeared from the summary
7146                 ;; buffer, don't skip forwards.
7147                 (not (string= group (gnus-group-group-name))))
7148       (gnus-group-next-unread-group 1))
7149     (setq group-point (point))
7150     (if temporary
7151         nil                             ;Nothing to do.
7152       (set-buffer buf)
7153       (if (not gnus-kill-summary-on-exit)
7154           (progn
7155             (gnus-deaden-summary)
7156             (setq mode nil))
7157         (when (get-buffer gnus-article-buffer)
7158           (bury-buffer gnus-article-buffer))
7159         ;; Return to group mode buffer.
7160         (when (eq mode 'gnus-summary-mode)
7161           (gnus-kill-buffer buf)))
7162
7163       ;; If we have several article buffers, we kill them at exit.
7164       (unless gnus-single-article-buffer
7165         (when (gnus-buffer-live-p gnus-article-buffer)
7166           (with-current-buffer gnus-article-buffer
7167             ;; Don't kill sticky article buffers
7168             (unless (eq major-mode 'gnus-sticky-article-mode)
7169               (gnus-kill-buffer gnus-article-buffer)
7170               (setq gnus-article-current nil))))
7171         (gnus-kill-buffer gnus-original-article-buffer))
7172
7173       (setq gnus-current-select-method gnus-select-method)
7174       (set-buffer gnus-group-buffer)
7175       (if quit-config
7176           (gnus-handle-ephemeral-exit quit-config)
7177         (goto-char group-point)
7178         ;; If gnus-group-buffer is already displayed, make sure we also move
7179         ;; the cursor in the window that displays it.
7180         (let ((win (get-buffer-window (current-buffer) 0)))
7181           (if win (set-window-point win (point))))
7182         (unless leave-hidden
7183           (gnus-configure-windows 'group 'force)))
7184       ;; Clear the current group name.
7185       (unless quit-config
7186         (setq gnus-newsgroup-name nil)))))
7187
7188 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7189 (defun gnus-summary-exit-no-update (&optional no-questions)
7190   "Quit reading current newsgroup without updating read article info."
7191   (interactive)
7192   (let* ((group gnus-newsgroup-name)
7193          (gnus-group-is-exiting-p t)
7194          (gnus-group-is-exiting-without-update-p t)
7195          (quit-config (gnus-group-quit-config group)))
7196     (when (or no-questions
7197               gnus-expert-user
7198               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7199       (gnus-async-halt-prefetch)
7200       (run-hooks 'gnus-summary-prepare-exit-hook)
7201       (when (gnus-buffer-live-p gnus-article-buffer)
7202         (with-current-buffer gnus-article-buffer
7203           (mm-destroy-parts gnus-article-mime-handles)
7204           ;; Set it to nil for safety reason.
7205           (setq gnus-article-mime-handle-alist nil)
7206           (setq gnus-article-mime-handles nil)))
7207       ;; If we have several article buffers, we kill them at exit.
7208       (unless gnus-single-article-buffer
7209         (gnus-kill-buffer gnus-article-buffer)
7210         (gnus-kill-buffer gnus-original-article-buffer)
7211         (setq gnus-article-current nil))
7212       (if (not gnus-kill-summary-on-exit)
7213           (gnus-deaden-summary)
7214         (gnus-close-group group)
7215         (gnus-kill-buffer gnus-summary-buffer))
7216       (unless gnus-single-article-buffer
7217         (setq gnus-article-current nil))
7218       (when gnus-use-trees
7219         (gnus-tree-close group))
7220       (gnus-async-prefetch-remove-group group)
7221       (when (get-buffer gnus-article-buffer)
7222         (bury-buffer gnus-article-buffer))
7223       ;; Return to the group buffer.
7224       (gnus-configure-windows 'group 'force)
7225       ;; Clear the current group name.
7226       (setq gnus-newsgroup-name nil)
7227       (unless (gnus-ephemeral-group-p group)
7228         (gnus-group-update-group group))
7229       (when (equal (gnus-group-group-name) group)
7230         (gnus-group-next-unread-group 1))
7231       (when quit-config
7232         (gnus-handle-ephemeral-exit quit-config)))))
7233
7234 (defun gnus-handle-ephemeral-exit (quit-config)
7235   "Handle movement when leaving an ephemeral group.
7236 The state which existed when entering the ephemeral is reset."
7237   (if (not (buffer-name (car quit-config)))
7238       (gnus-configure-windows 'group 'force)
7239     (set-buffer (car quit-config))
7240     (cond ((eq major-mode 'gnus-summary-mode)
7241            (gnus-set-global-variables))
7242           ((eq major-mode 'gnus-article-mode)
7243            (save-current-buffer
7244              ;; The `gnus-summary-buffer' variable may point
7245              ;; to the old summary buffer when using a single
7246              ;; article buffer.
7247              (unless (gnus-buffer-live-p gnus-summary-buffer)
7248                (set-buffer gnus-group-buffer))
7249              (set-buffer gnus-summary-buffer)
7250              (gnus-set-global-variables))))
7251     (if (or (eq (cdr quit-config) 'article)
7252             (eq (cdr quit-config) 'pick))
7253         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7254             (gnus-configure-windows 'pick 'force)
7255           (gnus-configure-windows (cdr quit-config) 'force))
7256       (gnus-configure-windows (cdr quit-config) 'force))
7257     (when (eq major-mode 'gnus-summary-mode)
7258       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7259                                                      next-unread-noselect))
7260           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7261                                   'next-noselect)
7262                               (gnus-summary-next-subject 1 nil t))
7263                              ((eq gnus-auto-select-on-ephemeral-exit
7264                                   'next-unread-noselect)
7265                               (gnus-summary-next-subject 1 t t))))
7266             ;; Hide the article buffer which displays the article different
7267             ;; from the one that the cursor points to in the summary buffer.
7268             (gnus-configure-windows 'summary 'force))
7269         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7270                (gnus-summary-next-subject 1))
7271               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7272                (gnus-summary-next-subject 1 t))))
7273       (gnus-summary-recenter)
7274       (gnus-summary-position-point))))
7275
7276 ;;; Dead summaries.
7277
7278 (defvar gnus-dead-summary-mode-map
7279   (let ((map (make-keymap)))
7280     (suppress-keymap map)
7281     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7282     (dolist (key '("\C-d" "\r" "\177" [delete]))
7283       (define-key map key 'gnus-summary-wake-up-the-dead))
7284     (dolist (key '("q" "Q"))
7285       (define-key map key 'bury-buffer))
7286     map))
7287
7288 (define-minor-mode gnus-dead-summary-mode
7289   "Minor mode for Gnus summary buffers."
7290   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7291   (unless (derived-mode-p 'gnus-summary-mode)
7292     (setq gnus-dead-summary-mode nil)))
7293
7294 (defun gnus-deaden-summary ()
7295   "Make the current summary buffer into a dead summary buffer."
7296   ;; Kill any previous dead summary buffer.
7297   (when (and gnus-dead-summary
7298              (buffer-name gnus-dead-summary))
7299     (with-current-buffer gnus-dead-summary
7300       (when gnus-dead-summary-mode
7301         (kill-buffer (current-buffer)))))
7302   ;; Make this the current dead summary.
7303   (setq gnus-dead-summary (current-buffer))
7304   (gnus-dead-summary-mode 1)
7305   (let ((name (buffer-name)))
7306     (when (string-match "Summary" name)
7307       (rename-buffer
7308        (concat (substring name 0 (match-beginning 0)) "Dead "
7309                (substring name (match-beginning 0)))
7310        t)
7311       (bury-buffer))))
7312
7313 (defun gnus-kill-or-deaden-summary (buffer)
7314   "Kill or deaden the summary BUFFER."
7315   (save-excursion
7316     (when (and (buffer-name buffer)
7317                (not gnus-single-article-buffer))
7318       (with-current-buffer buffer
7319         (gnus-kill-buffer gnus-article-buffer)
7320         (gnus-kill-buffer gnus-original-article-buffer)))
7321     (cond
7322      ;; Kill the buffer.
7323      (gnus-kill-summary-on-exit
7324       (when (and gnus-use-trees
7325                  (gnus-buffer-exists-p buffer))
7326         (with-current-buffer buffer
7327           (gnus-tree-close gnus-newsgroup-name)))
7328       (gnus-kill-buffer buffer))
7329      ;; Deaden the buffer.
7330      ((gnus-buffer-exists-p buffer)
7331       (with-current-buffer buffer
7332         (gnus-deaden-summary))))))
7333
7334 (defun gnus-summary-wake-up-the-dead (&rest args)
7335   "Wake up the dead summary buffer."
7336   (interactive)
7337   (gnus-dead-summary-mode -1)
7338   (let ((name (buffer-name)))
7339     (when (string-match "Dead " name)
7340       (rename-buffer
7341        (concat (substring name 0 (match-beginning 0))
7342                (substring name (match-end 0)))
7343        t)))
7344   (gnus-message 3 "This dead summary is now alive again"))
7345
7346 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7347 (defun gnus-summary-describe-group (&optional force)
7348   "Describe the current newsgroup."
7349   (interactive "P")
7350   (gnus-group-describe-group force gnus-newsgroup-name))
7351
7352 (defun gnus-summary-describe-briefly ()
7353   "Describe summary mode commands briefly."
7354   (interactive)
7355   (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")))
7356
7357 ;; Walking around group mode buffer from summary mode.
7358
7359 (defun gnus-summary-next-group (&optional no-article target-group backward)
7360   "Exit current newsgroup and then select next unread newsgroup.
7361 If prefix argument NO-ARTICLE is non-nil, no article is selected
7362 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7363 previous group instead."
7364   (interactive "P")
7365   ;; Stop pre-fetching.
7366   (gnus-async-halt-prefetch)
7367   (let ((current-group gnus-newsgroup-name)
7368         (current-buffer (current-buffer))
7369         entered)
7370     ;; First we semi-exit this group to update Xrefs and all variables.
7371     ;; We can't do a real exit, because the window conf must remain
7372     ;; the same in case the user is prompted for info, and we don't
7373     ;; want the window conf to change before that...
7374     (gnus-summary-exit t)
7375     (while (not entered)
7376       ;; Then we find what group we are supposed to enter.
7377       (set-buffer gnus-group-buffer)
7378       (gnus-group-jump-to-group current-group)
7379       (setq target-group
7380             (or target-group
7381                 (if (eq gnus-keep-same-level 'best)
7382                     (gnus-summary-best-group gnus-newsgroup-name)
7383                   (gnus-summary-search-group backward gnus-keep-same-level))))
7384       (if (not target-group)
7385           ;; There are no further groups, so we return to the group
7386           ;; buffer.
7387           (progn
7388             (gnus-message 5 "Returning to the group buffer")
7389             (setq entered t)
7390             (when (gnus-buffer-live-p current-buffer)
7391               (set-buffer current-buffer)
7392               (gnus-summary-exit))
7393             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7394         ;; We try to enter the target group.
7395         (gnus-group-jump-to-group target-group)
7396         (let ((unreads (gnus-group-group-unread)))
7397           (if (and (or (eq t unreads)
7398                        (and unreads (not (zerop unreads))))
7399                    (gnus-summary-read-group
7400                     target-group nil no-article
7401                     (and (buffer-name current-buffer) current-buffer)
7402                     nil backward))
7403               (setq entered t)
7404             (setq current-group target-group
7405                   target-group nil)))))))
7406
7407 (defun gnus-summary-prev-group (&optional no-article)
7408   "Exit current newsgroup and then select previous unread newsgroup.
7409 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7410   (interactive "P")
7411   (gnus-summary-next-group no-article nil t))
7412
7413 ;; Walking around summary lines.
7414
7415 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7416   "Go to the first subject satisfying any non-nil constraint.
7417 If UNREAD is non-nil, the article should be unread.
7418 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7419 If UNSEEN is non-nil, the article should be unseen as well as unread.
7420 Returns the article selected or nil if there are no matching articles."
7421   (interactive "P")
7422   (cond
7423    ;; Empty summary.
7424    ((null gnus-newsgroup-data)
7425     (gnus-message 3 "No articles in the group")
7426     nil)
7427    ;; Pick the first article.
7428    ((not (or unread undownloaded unseen))
7429     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7430     (gnus-data-number (car gnus-newsgroup-data)))
7431    ;; Find the first unread article.
7432    (t
7433     (let ((data gnus-newsgroup-data))
7434       (while (and data
7435                   (let ((num (gnus-data-number (car data))))
7436                     (or (memq num gnus-newsgroup-unfetched)
7437                         (not (or (and unread
7438                                       (memq num gnus-newsgroup-unreads))
7439                                  (and undownloaded
7440                                       (memq num gnus-newsgroup-undownloaded))
7441                                  (and unseen
7442                                       (memq num gnus-newsgroup-unseen)
7443                                       (memq num gnus-newsgroup-unreads)))))))
7444         (setq data (cdr data)))
7445       (prog1
7446           (if data
7447               (progn
7448                 (goto-char (gnus-data-pos (car data)))
7449                 (gnus-data-number (car data)))
7450             (gnus-message 3 "No more%s articles"
7451                           (let* ((r (when unread " unread"))
7452                                  (d (when undownloaded " undownloaded"))
7453                                  (s (when unseen " unseen"))
7454                                  (l (delq nil (list r d s))))
7455                             (cond ((= 3 (length l))
7456                                    (concat r "," d ", or" s))
7457                                   ((= 2 (length l))
7458                                    (concat (car l) ", or" (cadr l)))
7459                                   ((= 1 (length l))
7460                                    (car l))
7461                                   (t
7462                                    ""))))
7463             nil
7464             )
7465         (gnus-summary-position-point))))))
7466
7467 (defun gnus-summary-next-subject (n &optional unread dont-display)
7468   "Go to next N'th summary line.
7469 If N is negative, go to the previous N'th subject line.
7470 If UNREAD is non-nil, only unread articles are selected.
7471 The difference between N and the actual number of steps taken is
7472 returned."
7473   (interactive "p")
7474   (let ((backward (< n 0))
7475         (n (abs n)))
7476     (while (and (> n 0)
7477                 (if backward
7478                     (gnus-summary-find-prev unread)
7479                   (gnus-summary-find-next unread)))
7480       (unless (zerop (setq n (1- n)))
7481         (gnus-summary-show-thread)))
7482     (when (/= 0 n)
7483       (gnus-message 7 "No more%s articles"
7484                     (if unread " unread" "")))
7485     (unless dont-display
7486       (gnus-summary-recenter)
7487       (gnus-summary-position-point))
7488     n))
7489
7490 (defun gnus-summary-next-unread-subject (n)
7491   "Go to next N'th unread summary line."
7492   (interactive "p")
7493   (gnus-summary-next-subject n t))
7494
7495 (defun gnus-summary-prev-subject (n &optional unread)
7496   "Go to previous N'th summary line.
7497 If optional argument UNREAD is non-nil, only unread article is selected."
7498   (interactive "p")
7499   (gnus-summary-next-subject (- n) unread))
7500
7501 (defun gnus-summary-prev-unread-subject (n)
7502   "Go to previous N'th unread summary line."
7503   (interactive "p")
7504   (gnus-summary-next-subject (- n) t))
7505
7506 (defun gnus-summary-goto-subjects (articles)
7507   "Insert the subject header for ARTICLES in the current buffer."
7508   (save-excursion
7509     (dolist (article articles)
7510       (gnus-summary-goto-subject article t)))
7511   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7512   (gnus-summary-position-point))
7513
7514 (defun gnus-summary-goto-subject (article &optional force silent)
7515   "Go to the subject line of ARTICLE.
7516 If FORCE, also allow jumping to articles not currently shown."
7517   (interactive "nArticle number: ")
7518   (unless (numberp article)
7519     (error "Article %s is not a number" article))
7520   (let ((b (point))
7521         (data (gnus-data-find article)))
7522     ;; We read in the article if we have to.
7523     (and (not data)
7524          force
7525          (gnus-summary-insert-subject
7526           article
7527           (if (or (numberp force) (vectorp force)) force)
7528           t)
7529          (setq data (gnus-data-find article)))
7530     (goto-char b)
7531     (if (not data)
7532         (progn
7533           (unless silent
7534             (gnus-message 3 "Can't find article %d" article))
7535           nil)
7536       (let ((pt (gnus-data-pos data)))
7537         (goto-char pt)
7538         (gnus-summary-set-article-display-arrow pt))
7539       (gnus-summary-position-point)
7540       article)))
7541
7542 ;; Walking around summary lines with displaying articles.
7543
7544 (defun gnus-summary-expand-window (&optional arg)
7545   "Make the summary buffer take up the entire Emacs frame.
7546 Given a prefix, will force an `article' buffer configuration."
7547   (interactive "P")
7548   (if arg
7549       (gnus-configure-windows 'article 'force)
7550     (gnus-configure-windows 'summary 'force)))
7551
7552 (defun gnus-summary-display-article (article &optional all-header)
7553   "Display ARTICLE in article buffer."
7554   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7555                (with-current-buffer gnus-article-buffer
7556                  (eq major-mode 'gnus-article-mode)))
7557     (gnus-article-setup-buffer))
7558   (gnus-set-global-variables)
7559   (with-current-buffer gnus-article-buffer
7560     (setq gnus-article-charset gnus-newsgroup-charset)
7561     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7562     (mm-enable-multibyte))
7563   (if (null article)
7564       nil
7565     (prog1
7566         (if gnus-summary-display-article-function
7567             (funcall gnus-summary-display-article-function article all-header)
7568           (gnus-article-prepare article all-header))
7569       (gnus-run-hooks 'gnus-select-article-hook)
7570       (when (and gnus-current-article
7571                  (not (zerop gnus-current-article)))
7572         (gnus-summary-goto-subject gnus-current-article))
7573       (gnus-summary-recenter)
7574       (when (and gnus-use-trees gnus-show-threads)
7575         (gnus-possibly-generate-tree article)
7576         (gnus-highlight-selected-tree article))
7577       ;; Successfully display article.
7578       (gnus-article-set-window-start
7579        (cdr (assq article gnus-newsgroup-bookmarks))))))
7580
7581 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7582   "Select the current article.
7583 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7584 non-nil, the article will be re-fetched even if it already present in
7585 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7586 be displayed."
7587   ;; Make sure we are in the summary buffer to work around bbdb bug.
7588   (unless (eq major-mode 'gnus-summary-mode)
7589     (set-buffer gnus-summary-buffer))
7590   (let ((article (or article (gnus-summary-article-number)))
7591         (all-headers (not (not all-headers))) ;Must be t or nil.
7592         gnus-summary-display-article-function)
7593     (and (not pseudo)
7594          (gnus-summary-article-pseudo-p article)
7595          (error "This is a pseudo-article"))
7596     (with-current-buffer gnus-summary-buffer
7597       (if (or (and gnus-single-article-buffer
7598                    (or (null gnus-current-article)
7599                        (null gnus-article-current)
7600                        (null (get-buffer gnus-article-buffer))
7601                        (not (eq article (cdr gnus-article-current)))
7602                        (not (equal (car gnus-article-current)
7603                                    gnus-newsgroup-name))
7604                        (not (get-buffer gnus-original-article-buffer))))
7605               (and (not gnus-single-article-buffer)
7606                    (or (null gnus-current-article)
7607                        (not (get-buffer gnus-original-article-buffer))
7608                        (not (eq gnus-current-article article))))
7609               force)
7610           ;; The requested article is different from the current article.
7611           (progn
7612             (gnus-summary-display-article article all-headers)
7613             (when (gnus-buffer-live-p gnus-article-buffer)
7614               (with-current-buffer gnus-article-buffer
7615                 (if (not gnus-article-decoded-p) ;; a local variable
7616                     (mm-disable-multibyte))))
7617             (gnus-article-set-window-start
7618              (cdr (assq article gnus-newsgroup-bookmarks)))
7619             article)
7620         'old))))
7621
7622 (defun gnus-summary-force-verify-and-decrypt ()
7623   "Display buttons for signed/encrypted parts and verify/decrypt them."
7624   (interactive)
7625   (let ((mm-verify-option 'known)
7626         (mm-decrypt-option 'known)
7627         (gnus-article-emulate-mime t)
7628         (gnus-buttonized-mime-types (append (list "multipart/signed"
7629                                                   "multipart/encrypted")
7630                                             gnus-buttonized-mime-types)))
7631     (gnus-summary-select-article nil 'force)))
7632
7633 (defun gnus-summary-set-current-mark (&optional current-mark)
7634   "Obsolete function."
7635   nil)
7636
7637 (defun gnus-summary-next-article (&optional unread subject backward push)
7638   "Select the next article.
7639 If UNREAD, only unread articles are selected.
7640 If SUBJECT, only articles with SUBJECT are selected.
7641 If BACKWARD, the previous article is selected instead of the next."
7642   (interactive "P")
7643   ;; Make sure we are in the summary buffer.
7644   (unless (eq major-mode 'gnus-summary-mode)
7645     (set-buffer gnus-summary-buffer))
7646   (cond
7647    ;; Is there such an article?
7648    ((and (gnus-summary-search-forward unread subject backward)
7649          (or (gnus-summary-display-article (gnus-summary-article-number))
7650              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7651     (gnus-summary-position-point))
7652    ;; If not, we try the first unread, if that is wanted.
7653    ((and subject
7654          gnus-auto-select-same
7655          (gnus-summary-first-unread-article))
7656     (gnus-summary-position-point)
7657     (gnus-message 6 "Wrapped"))
7658    ;; Try to get next/previous article not displayed in this group.
7659    ((and gnus-auto-extend-newsgroup
7660          (not unread) (not subject))
7661     (gnus-summary-goto-article
7662      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7663      nil (count-lines (point-min) (point))))
7664    ;; Go to next/previous group.
7665    (t
7666     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7667       (gnus-summary-jump-to-group gnus-newsgroup-name))
7668     (let ((cmd (if (featurep 'xemacs)
7669                    last-command-char
7670                  last-command-event))
7671           (point
7672            (with-current-buffer gnus-group-buffer
7673              (point)))
7674           (group
7675            (if (eq gnus-keep-same-level 'best)
7676                (gnus-summary-best-group gnus-newsgroup-name)
7677              (gnus-summary-search-group backward gnus-keep-same-level))))
7678       ;; For some reason, the group window gets selected.  We change
7679       ;; it back.
7680       (select-window (get-buffer-window (current-buffer)))
7681       ;; Select next unread newsgroup automagically.
7682       (cond
7683        ((or (not gnus-auto-select-next)
7684             (not cmd))
7685         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7686        ((or (eq gnus-auto-select-next 'quietly)
7687             (and (eq gnus-auto-select-next 'slightly-quietly)
7688                  push)
7689             (and (eq gnus-auto-select-next 'almost-quietly)
7690                  (gnus-summary-last-article-p)))
7691         ;; Select quietly.
7692         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7693             (gnus-summary-exit)
7694           (gnus-message 7 "No more%s articles (%s)..."
7695                         (if unread " unread" "")
7696                         (if group (concat "selecting " group)
7697                           "exiting"))
7698           (gnus-summary-next-group nil group backward)))
7699        (t
7700         (when (gnus-key-press-event-p last-input-event)
7701           (gnus-summary-walk-group-buffer
7702            gnus-newsgroup-name cmd unread backward point))))))))
7703
7704 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7705   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7706                       (?\C-p (gnus-group-prev-unread-group 1))))
7707         (cursor-in-echo-area t)
7708         keve key group ended prompt)
7709     (with-current-buffer gnus-group-buffer
7710       (goto-char start)
7711       (setq group
7712             (if (eq gnus-keep-same-level 'best)
7713                 (gnus-summary-best-group gnus-newsgroup-name)
7714               (gnus-summary-search-group backward gnus-keep-same-level))))
7715     (while (not ended)
7716       (setq prompt
7717             (format
7718              "No more%s articles%s " (if unread " unread" "")
7719              (if (and group
7720                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7721                  (format " (Type %s for %s [%s])"
7722                          (single-key-description cmd)
7723                          (gnus-group-decoded-name group)
7724                          (gnus-group-unread group))
7725                (format " (Type %s to exit %s)"
7726                        (single-key-description cmd)
7727                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7728       ;; Confirm auto selection.
7729       (setq key (car (setq keve (gnus-read-event-char prompt)))
7730             ended t)
7731       (cond
7732        ((assq key keystrokes)
7733         (let ((obuf (current-buffer)))
7734           (switch-to-buffer gnus-group-buffer)
7735           (when group
7736             (gnus-group-jump-to-group group))
7737           (eval (cadr (assq key keystrokes)))
7738           (setq group (gnus-group-group-name))
7739           (switch-to-buffer obuf))
7740         (setq ended nil))
7741        ((equal key cmd)
7742         (if (or (not group)
7743                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7744             (gnus-summary-exit)
7745           (gnus-summary-next-group nil group backward)))
7746        (t
7747         (push (cdr keve) unread-command-events))))))
7748
7749 (defun gnus-summary-next-unread-article ()
7750   "Select unread article after current one."
7751   (interactive)
7752   (gnus-summary-next-article
7753    (or (not (eq gnus-summary-goto-unread 'never))
7754        (gnus-summary-last-article-p (gnus-summary-article-number)))
7755    (and gnus-auto-select-same
7756         (gnus-summary-article-subject))))
7757
7758 (defun gnus-summary-prev-article (&optional unread subject)
7759   "Select the article before the current one.
7760 If UNREAD is non-nil, only unread articles are selected."
7761   (interactive "P")
7762   (gnus-summary-next-article unread subject t))
7763
7764 (defun gnus-summary-prev-unread-article ()
7765   "Select unread article before current one."
7766   (interactive)
7767   (gnus-summary-prev-article
7768    (or (not (eq gnus-summary-goto-unread 'never))
7769        (gnus-summary-first-article-p (gnus-summary-article-number)))
7770    (and gnus-auto-select-same
7771         (gnus-summary-article-subject))))
7772
7773 (defun gnus-summary-next-page (&optional lines circular stop)
7774   "Show next page of the selected article.
7775 If at the end of the current article, select the next article.
7776 LINES says how many lines should be scrolled up.
7777
7778 If CIRCULAR is non-nil, go to the start of the article instead of
7779 selecting the next article when reaching the end of the current
7780 article.
7781
7782 If STOP is non-nil, just stop when reaching the end of the message.
7783
7784 Also see the variable `gnus-article-skip-boring'."
7785   (interactive "P")
7786   (setq gnus-summary-buffer (current-buffer))
7787   (gnus-set-global-variables)
7788   (let ((article (gnus-summary-article-number))
7789         (article-window (get-buffer-window gnus-article-buffer t))
7790         endp)
7791     ;; If the buffer is empty, we have no article.
7792     (unless article
7793       (error "No article to select"))
7794     (gnus-configure-windows 'article)
7795     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7796         (if (and (eq gnus-summary-goto-unread 'never)
7797                  (not (gnus-summary-last-article-p article)))
7798             (gnus-summary-next-article)
7799           (gnus-summary-next-unread-article))
7800       (if (or (null gnus-current-article)
7801               (null gnus-article-current)
7802               (/= article (cdr gnus-article-current))
7803               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7804           ;; Selected subject is different from current article's.
7805           (gnus-summary-display-article article)
7806         (when article-window
7807           (gnus-eval-in-buffer-window gnus-article-buffer
7808             (setq endp (or (gnus-article-next-page lines)
7809                            (gnus-article-only-boring-p))))
7810           (when endp
7811             (cond ((or stop gnus-summary-stop-at-end-of-message)
7812                    (gnus-message 3 "End of message"))
7813                   (circular
7814                    (gnus-summary-beginning-of-article))
7815                   (lines
7816                    (gnus-message 3 "End of message"))
7817                   ((null lines)
7818                    (if (and (eq gnus-summary-goto-unread 'never)
7819                             (not (gnus-summary-last-article-p article)))
7820                        (gnus-summary-next-article)
7821                      (gnus-summary-next-unread-article))))))))
7822     (gnus-summary-recenter)
7823     (gnus-summary-position-point)))
7824
7825 (defun gnus-summary-prev-page (&optional lines move)
7826   "Show previous page of selected article.
7827 Argument LINES specifies lines to be scrolled down.
7828 If MOVE, move to the previous unread article if point is at
7829 the beginning of the buffer."
7830   (interactive "P")
7831   (let ((article (gnus-summary-article-number))
7832         (article-window (get-buffer-window gnus-article-buffer t))
7833         endp)
7834     (gnus-configure-windows 'article)
7835     (if (or (null gnus-current-article)
7836             (null gnus-article-current)
7837             (/= article (cdr gnus-article-current))
7838             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7839         ;; Selected subject is different from current article's.
7840         (gnus-summary-display-article article)
7841       (gnus-summary-recenter)
7842       (when article-window
7843         (gnus-eval-in-buffer-window gnus-article-buffer
7844           (setq endp (gnus-article-prev-page lines)))
7845         (when (and move endp)
7846           (cond (lines
7847                  (gnus-message 3 "Beginning of message"))
7848                 ((null lines)
7849                  (if (and (eq gnus-summary-goto-unread 'never)
7850                           (not (gnus-summary-first-article-p article)))
7851                      (gnus-summary-prev-article)
7852                    (gnus-summary-prev-unread-article))))))))
7853   (gnus-summary-position-point))
7854
7855 (defun gnus-summary-prev-page-or-article (&optional lines)
7856   "Show previous page of selected article.
7857 Argument LINES specifies lines to be scrolled down.
7858 If at the beginning of the article, go to the next article."
7859   (interactive "P")
7860   (gnus-summary-prev-page lines t))
7861
7862 (defun gnus-summary-scroll-up (lines)
7863   "Scroll up (or down) one line current article.
7864 Argument LINES specifies lines to be scrolled up (or down if negative).
7865 If no article is selected, then the current article will be selected first."
7866   (interactive "p")
7867   (gnus-configure-windows 'article)
7868   (gnus-summary-show-thread)
7869   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7870     (gnus-eval-in-buffer-window gnus-article-buffer
7871       (cond ((> lines 0)
7872              (when (gnus-article-next-page lines)
7873                (gnus-message 3 "End of message")))
7874             ((< lines 0)
7875              (gnus-article-prev-page (- lines))))))
7876   (gnus-summary-recenter)
7877   (gnus-summary-position-point))
7878
7879 (defun gnus-summary-scroll-down (lines)
7880   "Scroll down (or up) one line current article.
7881 Argument LINES specifies lines to be scrolled down (or up if negative).
7882 If no article is selected, then the current article will be selected first."
7883   (interactive "p")
7884   (gnus-summary-scroll-up (- lines)))
7885
7886 (defun gnus-summary-next-same-subject ()
7887   "Select next article which has the same subject as current one."
7888   (interactive)
7889   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7890
7891 (defun gnus-summary-prev-same-subject ()
7892   "Select previous article which has the same subject as current one."
7893   (interactive)
7894   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7895
7896 (defun gnus-summary-next-unread-same-subject ()
7897   "Select next unread article which has the same subject as current one."
7898   (interactive)
7899   (gnus-summary-next-article t (gnus-summary-article-subject)))
7900
7901 (defun gnus-summary-prev-unread-same-subject ()
7902   "Select previous unread article which has the same subject as current one."
7903   (interactive)
7904   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7905
7906 (defun gnus-summary-first-unread-article ()
7907   "Select the first unread article.
7908 Return nil if there are no unread articles."
7909   (interactive)
7910   (prog1
7911       (when (gnus-summary-first-subject t)
7912         (gnus-summary-show-thread)
7913         (gnus-summary-first-subject t)
7914         (gnus-summary-display-article (gnus-summary-article-number)))
7915     (gnus-summary-position-point)))
7916
7917 (defun gnus-summary-first-unread-subject ()
7918   "Place the point on the subject line of the first unread article.
7919 Return nil if there are no unread articles."
7920   (interactive)
7921   (prog1
7922       (when (gnus-summary-first-subject t)
7923         (gnus-summary-show-thread)
7924         (gnus-summary-first-subject t))
7925     (gnus-summary-position-point)))
7926
7927 (defun gnus-summary-first-unseen-subject ()
7928   "Place the point on the subject line of the first unseen article.
7929 Return nil if there are no unseen articles."
7930   (interactive)
7931   (prog1
7932       (when (gnus-summary-first-subject nil nil t)
7933         (gnus-summary-show-thread)
7934         (gnus-summary-first-subject nil nil t))
7935     (gnus-summary-position-point)))
7936
7937 (defun gnus-summary-first-unseen-or-unread-subject ()
7938   "Place the point on the subject line of the first unseen and unread article.
7939 If all article have been seen, on the subject line of the first unread
7940 article."
7941   (interactive)
7942   (prog1
7943       (unless (when (gnus-summary-first-subject nil nil t)
7944                 (gnus-summary-show-thread)
7945                 (gnus-summary-first-subject nil nil t))
7946         (when (gnus-summary-first-subject t)
7947           (gnus-summary-show-thread)
7948           (gnus-summary-first-subject t)))
7949     (gnus-summary-position-point)))
7950
7951 (defun gnus-summary-first-article ()
7952   "Select the first article.
7953 Return nil if there are no articles."
7954   (interactive)
7955   (prog1
7956       (when (gnus-summary-first-subject)
7957         (gnus-summary-show-thread)
7958         (gnus-summary-first-subject)
7959         (gnus-summary-display-article (gnus-summary-article-number)))
7960     (gnus-summary-position-point)))
7961
7962 (defun gnus-summary-best-unread-article (&optional arg)
7963   "Select the unread article with the highest score.
7964 If given a prefix argument, select the next unread article that has a
7965 score higher than the default score."
7966   (interactive "P")
7967   (let ((article (if arg
7968                      (gnus-summary-better-unread-subject)
7969                    (gnus-summary-best-unread-subject))))
7970     (if article
7971         (gnus-summary-goto-article article)
7972       (error "No unread articles"))))
7973
7974 (defun gnus-summary-best-unread-subject ()
7975   "Select the unread subject with the highest score."
7976   (interactive)
7977   (let ((best -1000000)
7978         (data gnus-newsgroup-data)
7979         article score)
7980     (while data
7981       (and (gnus-data-unread-p (car data))
7982            (> (setq score
7983                     (gnus-summary-article-score (gnus-data-number (car data))))
7984               best)
7985            (setq best score
7986                  article (gnus-data-number (car data))))
7987       (setq data (cdr data)))
7988     (when article
7989       (gnus-summary-goto-subject article))
7990     (gnus-summary-position-point)
7991     article))
7992
7993 (defun gnus-summary-better-unread-subject ()
7994   "Select the first unread subject that has a score over the default score."
7995   (interactive)
7996   (let ((data gnus-newsgroup-data)
7997         article score)
7998     (while (and (setq article (gnus-data-number (car data)))
7999                 (or (gnus-data-read-p (car data))
8000                     (not (> (gnus-summary-article-score article)
8001                             gnus-summary-default-score))))
8002       (setq data (cdr data)))
8003     (when article
8004       (gnus-summary-goto-subject article))
8005     (gnus-summary-position-point)
8006     article))
8007
8008 (defun gnus-summary-last-subject ()
8009   "Go to the last displayed subject line in the group."
8010   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8011     (when article
8012       (gnus-summary-goto-subject article))))
8013
8014 (defun gnus-summary-goto-article (article &optional all-headers force)
8015   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8016 If ALL-HEADERS is non-nil, no header lines are hidden.
8017 If FORCE, go to the article even if it isn't displayed.  If FORCE
8018 is a number, it is the line the article is to be displayed on."
8019   (interactive
8020    (list
8021     (gnus-completing-read
8022      "Article number or Message-ID"
8023      (mapcar 'int-to-string gnus-newsgroup-limit))
8024     current-prefix-arg
8025     t))
8026   (prog1
8027       (if (and (stringp article)
8028                (string-match "@\\|%40" article))
8029           (gnus-summary-refer-article article)
8030         (when (stringp article)
8031           (setq article (string-to-number article)))
8032         (if (gnus-summary-goto-subject article force)
8033             (gnus-summary-display-article article all-headers)
8034           (gnus-message 4 "Couldn't go to article %s" article) nil))
8035     (gnus-summary-position-point)))
8036
8037 (defun gnus-summary-goto-last-article ()
8038   "Go to the previously read article."
8039   (interactive)
8040   (prog1
8041       (when gnus-last-article
8042         (gnus-summary-goto-article gnus-last-article nil t))
8043     (gnus-summary-position-point)))
8044
8045 (defun gnus-summary-pop-article (number)
8046   "Pop one article off the history and go to the previous.
8047 NUMBER articles will be popped off."
8048   (interactive "p")
8049   (let (to)
8050     (setq gnus-newsgroup-history
8051           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8052     (if to
8053         (gnus-summary-goto-article (car to) nil t)
8054       (error "Article history empty")))
8055   (gnus-summary-position-point))
8056
8057 ;; Summary commands and functions for limiting the summary buffer.
8058
8059 (defun gnus-summary-limit-to-articles (n)
8060   "Limit the summary buffer to the next N articles.
8061 If not given a prefix, use the process marked articles instead."
8062   (interactive "P")
8063   (prog1
8064       (let ((articles (gnus-summary-work-articles n)))
8065         (setq gnus-newsgroup-processable nil)
8066         (gnus-summary-limit articles))
8067     (gnus-summary-position-point)))
8068
8069 (defun gnus-summary-pop-limit (&optional total)
8070   "Restore the previous limit.
8071 If given a prefix, remove all limits."
8072   (interactive "P")
8073   (when total
8074     (setq gnus-newsgroup-limits
8075           (list (mapcar (lambda (h) (mail-header-number h))
8076                         gnus-newsgroup-headers))))
8077   (unless gnus-newsgroup-limits
8078     (error "No limit to pop"))
8079   (prog1
8080       (gnus-summary-limit nil 'pop)
8081     (gnus-summary-position-point)))
8082
8083 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8084   "Limit the summary buffer to articles that have subjects that match a regexp.
8085 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8086   (interactive
8087    (list (read-string (if current-prefix-arg
8088                           "Exclude subject (regexp): "
8089                         "Limit to subject (regexp): "))
8090          nil current-prefix-arg))
8091   (unless header
8092     (setq header "subject"))
8093   (when (not (equal "" subject))
8094     (prog1
8095         (let ((articles (gnus-summary-find-matching
8096                          (or header "subject") subject 'all nil nil
8097                          not-matching)))
8098           (unless articles
8099             (error "Found no matches for \"%s\"" subject))
8100           (gnus-summary-limit articles))
8101       (gnus-summary-position-point))))
8102
8103 (defun gnus-summary-limit-to-author (from &optional not-matching)
8104   "Limit the summary buffer to articles that have authors that match a regexp.
8105 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8106   (interactive
8107    (list (read-string (if current-prefix-arg
8108                           "Exclude author (regexp): "
8109                         "Limit to author (regexp): "))
8110          current-prefix-arg))
8111   (gnus-summary-limit-to-subject from "from" not-matching))
8112
8113 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8114   "Limit the summary buffer to articles with the given RECIPIENT.
8115
8116 If NOT-MATCHING, exclude RECIPIENT.
8117
8118 To and Cc headers are checked.  You need to include them in
8119 `nnmail-extra-headers'."
8120   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8121   (interactive
8122    (list (read-string (format "%s recipient (regexp): "
8123                               (if current-prefix-arg "Exclude" "Limit to")))
8124          current-prefix-arg))
8125   (when (not (equal "" recipient))
8126     (prog1 (let* ((to
8127                    (if (memq 'To nnmail-extra-headers)
8128                        (gnus-summary-find-matching
8129                         (cons 'extra 'To) recipient 'all nil nil
8130                         not-matching)
8131                      (gnus-message
8132                       1 "`To' isn't present in `nnmail-extra-headers'")
8133                      (sit-for 1)
8134                      nil))
8135                   (cc
8136                    (if (memq 'Cc nnmail-extra-headers)
8137                        (gnus-summary-find-matching
8138                         (cons 'extra 'Cc) recipient 'all nil nil
8139                         not-matching)
8140                      (gnus-message
8141                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8142                      (sit-for 1)
8143                      nil))
8144                   (articles
8145                    (if not-matching
8146                        ;; We need the numbers that are in both lists:
8147                        (mapcar (lambda (a)
8148                                  (and (memq a to) a))
8149                                cc)
8150                      (nconc to cc))))
8151              (unless articles
8152                (error "Found no matches for \"%s\"" recipient))
8153              (gnus-summary-limit articles))
8154       (gnus-summary-position-point))))
8155
8156 (defun gnus-summary-limit-to-address (address &optional not-matching)
8157   "Limit the summary buffer to articles with the given ADDRESS.
8158
8159 If NOT-MATCHING, exclude ADDRESS.
8160
8161 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8162 in `nnmail-extra-headers'."
8163   (interactive
8164    (list (read-string (format "%s address (regexp): "
8165                               (if current-prefix-arg "Exclude" "Limit to")))
8166          current-prefix-arg))
8167   (when (not (equal "" address))
8168     (prog1 (let* ((to
8169                    (if (memq 'To nnmail-extra-headers)
8170                        (gnus-summary-find-matching
8171                         (cons 'extra 'To) address 'all nil nil
8172                         not-matching)
8173                      (gnus-message
8174                       1 "`To' isn't present in `nnmail-extra-headers'")
8175                      (sit-for 1)
8176                      t))
8177                   (cc
8178                    (if (memq 'Cc nnmail-extra-headers)
8179                        (gnus-summary-find-matching
8180                         (cons 'extra 'Cc) address 'all nil nil
8181                         not-matching)
8182                      (gnus-message
8183                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8184                      (sit-for 1)
8185                      t))
8186                   (from
8187                    (gnus-summary-find-matching "from" address
8188                                                'all nil nil not-matching))
8189                   (articles
8190                    (if not-matching
8191                        ;; We need the numbers that are in all lists:
8192                        (if (eq cc t)
8193                            (if (eq to t)
8194                                from
8195                              (mapcar (lambda (a) (car (memq a from))) to))
8196                          (if (eq to t)
8197                              (mapcar (lambda (a) (car (memq a from))) cc)
8198                            (mapcar (lambda (a) (car (memq a from)))
8199                                    (mapcar (lambda (a) (car (memq a to)))
8200                                            cc))))
8201                      (nconc (if (eq to t) nil to)
8202                             (if (eq cc t) nil cc)
8203                             from))))
8204              (unless articles
8205                (error "Found no matches for \"%s\"" address))
8206              (gnus-summary-limit articles))
8207       (gnus-summary-position-point))))
8208
8209 (defun gnus-summary-limit-strange-charsets-predicate (header)
8210   (when (fboundp 'char-charset)
8211     (let ((string (concat (mail-header-subject header)
8212                           (mail-header-from header)))
8213           charset found)
8214       (dotimes (i (1- (length string)))
8215         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8216         (when (string-match "unicode\\|big\\|japanese" charset)
8217           (setq found t)))
8218       found)))
8219
8220 (defun gnus-summary-limit-to-predicate (predicate)
8221   "Limit to articles where PREDICATE returns non-nil.
8222 PREDICATE will be called with the header structures of the
8223 articles."
8224   (let ((articles nil)
8225         (case-fold-search t))
8226     (dolist (header gnus-newsgroup-headers)
8227       (when (funcall predicate header)
8228         (push (mail-header-number header) articles)))
8229     (gnus-summary-limit (nreverse articles))))
8230
8231 (defun gnus-summary-limit-to-age (age &optional younger-p)
8232   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8233 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8234 articles that are younger than AGE days."
8235   (interactive
8236    (let ((younger current-prefix-arg)
8237          (days-got nil)
8238          days)
8239      (while (not days-got)
8240        (setq days (if younger
8241                       (read-string "Limit to articles younger than (in days, older when negative): ")
8242                     (read-string
8243                      "Limit to articles older than (in days, younger when negative): ")))
8244        (when (> (length days) 0)
8245          (setq days (read days)))
8246        (if (numberp days)
8247            (progn
8248              (setq days-got t)
8249              (when (< days 0)
8250                (setq younger (not younger))
8251                (setq days (* days -1))))
8252          (message "Please enter a number.")
8253          (sleep-for 1)))
8254      (list days younger)))
8255   (prog1
8256       (let ((data gnus-newsgroup-data)
8257             (cutoff (days-to-time age))
8258             articles d date is-younger)
8259         (while (setq d (pop data))
8260           (when (and (vectorp (gnus-data-header d))
8261                      (setq date (mail-header-date (gnus-data-header d))))
8262             (setq is-younger (time-less-p
8263                               (time-since (gnus-date-get-time date))
8264                               cutoff))
8265             (when (if younger-p
8266                       is-younger
8267                     (not is-younger))
8268               (push (gnus-data-number d) articles))))
8269         (gnus-summary-limit (nreverse articles)))
8270     (gnus-summary-position-point)))
8271
8272 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8273   "Limit the summary buffer to articles that match an 'extra' header."
8274   (interactive
8275    (let ((header
8276           (intern
8277            (gnus-completing-read
8278             (if current-prefix-arg
8279                 "Exclude extra header"
8280               "Limit extra header")
8281             (mapcar 'symbol-name gnus-extra-headers)
8282             t nil nil
8283             (symbol-name (car gnus-extra-headers))))))
8284      (list header
8285            (read-string (format "%s header %s (regexp): "
8286                                 (if current-prefix-arg "Exclude" "Limit to")
8287                                 header))
8288            current-prefix-arg)))
8289   (when (not (equal "" regexp))
8290     (prog1
8291         (let ((articles (gnus-summary-find-matching
8292                          (cons 'extra header) regexp 'all nil nil
8293                          not-matching)))
8294           (unless articles
8295             (error "Found no matches for \"%s\"" regexp))
8296           (gnus-summary-limit articles))
8297       (gnus-summary-position-point))))
8298
8299 (defun gnus-summary-limit-to-display-predicate ()
8300   "Limit the summary buffer to the predicated in the `display' group parameter."
8301   (interactive)
8302   (unless gnus-newsgroup-display
8303     (error "There is no `display' group parameter"))
8304   (let (articles)
8305     (dolist (gnus-number gnus-newsgroup-articles)
8306       (when (funcall gnus-newsgroup-display)
8307         (push gnus-number articles)))
8308     (gnus-summary-limit articles))
8309   (gnus-summary-position-point))
8310
8311 (defun gnus-summary-limit-to-unread (&optional all)
8312   "Limit the summary buffer to articles that are not marked as read.
8313 If ALL is non-nil, limit strictly to unread articles."
8314   (interactive "P")
8315   (if all
8316       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8317     (gnus-summary-limit-to-marks
8318      ;; Concat all the marks that say that an article is read and have
8319      ;; those removed.
8320      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8321            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8322            gnus-low-score-mark gnus-expirable-mark
8323            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8324            gnus-duplicate-mark)
8325      'reverse)))
8326
8327 (defun gnus-summary-limit-to-headers (match &optional reverse)
8328   "Limit the summary buffer to articles that have headers that match MATCH.
8329 If REVERSE (the prefix), limit to articles that don't match."
8330   (interactive "sMatch headers (regexp): \nP")
8331   (gnus-summary-limit-to-bodies match reverse t))
8332
8333 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8334   "Limit the summary buffer to articles that have bodies that match MATCH.
8335 If REVERSE (the prefix), limit to articles that don't match."
8336   (interactive "sMatch body (regexp): \nP")
8337   (let ((articles nil)
8338         (gnus-select-article-hook nil)  ;Disable hook.
8339         (gnus-article-prepare-hook nil)
8340         (gnus-use-article-prefetch nil)
8341         (gnus-keep-backlog nil)
8342         (gnus-break-pages nil)
8343         (gnus-summary-display-arrow nil)
8344         (gnus-updated-mode-lines nil)
8345         (gnus-auto-center-summary nil)
8346         (gnus-display-mime-function nil))
8347     (dolist (data gnus-newsgroup-data)
8348       (let (gnus-mark-article-hook)
8349         (gnus-summary-select-article t t nil (gnus-data-number data)))
8350       (with-current-buffer gnus-article-buffer
8351         (article-goto-body)
8352         (let* ((case-fold-search t)
8353                (found (if headersp
8354                           (re-search-backward match nil t)
8355                         (re-search-forward match nil t))))
8356           (when (or (and found
8357                          (not reverse))
8358                     (and (not found)
8359                          reverse))
8360             (push (gnus-data-number data) articles)))))
8361     (if (not articles)
8362         (message "No messages matched")
8363       (gnus-summary-limit articles)))
8364   (gnus-summary-position-point))
8365
8366 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8367   "Limit the summary buffer to articles that aren't part on any thread.
8368 If THREADSP (the prefix), limit to articles that are in threads."
8369   (interactive "P")
8370   (let ((articles nil)
8371         thread-articles
8372         threads)
8373     (dolist (thread gnus-newsgroup-threads)
8374       (if (stringp (car thread))
8375           (dolist (thread (cdr thread))
8376             (push thread threads))
8377         (push thread threads)))
8378     (dolist (thread threads)
8379       (setq thread-articles (gnus-articles-in-thread thread))
8380       (when (or (and threadsp
8381                      (> (length thread-articles) 1))
8382                 (and (not threadsp)
8383                      (= (length thread-articles) 1)))
8384         (setq articles (nconc thread-articles articles))))
8385     (if (not articles)
8386         (message "No messages matched")
8387       (gnus-summary-limit articles))
8388     (gnus-summary-position-point)))
8389
8390 (defun gnus-summary-limit-to-replied (&optional unreplied)
8391   "Limit the summary buffer to replied articles.
8392 If UNREPLIED (the prefix), limit to unreplied articles."
8393   (interactive "P")
8394   (if unreplied
8395       (gnus-summary-limit
8396        (gnus-set-difference gnus-newsgroup-articles
8397         gnus-newsgroup-replied))
8398     (gnus-summary-limit gnus-newsgroup-replied))
8399   (gnus-summary-position-point))
8400
8401 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8402   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8403 If REVERSE, limit the summary buffer to articles that are marked
8404 with MARKS.  MARKS can either be a string of marks or a list of marks.
8405 Returns how many articles were removed."
8406   (interactive "sMarks: ")
8407   (gnus-summary-limit-to-marks marks t))
8408
8409 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8410   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8411 If REVERSE (the prefix), limit the summary buffer to articles that are
8412 not marked with MARKS.  MARKS can either be a string of marks or a
8413 list of marks.
8414 Returns how many articles were removed."
8415   (interactive "sMarks: \nP")
8416   (prog1
8417       (let ((data gnus-newsgroup-data)
8418             (marks (if (listp marks) marks
8419                      (append marks nil))) ; Transform to list.
8420             articles)
8421         (while data
8422           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8423                   (memq (gnus-data-mark (car data)) marks))
8424             (push (gnus-data-number (car data)) articles))
8425           (setq data (cdr data)))
8426         (gnus-summary-limit articles))
8427     (gnus-summary-position-point)))
8428
8429 (defun gnus-summary-limit-to-score (score)
8430   "Limit to articles with score at or above SCORE."
8431   (interactive "NLimit to articles with score of at least: ")
8432   (let ((data gnus-newsgroup-data)
8433         articles)
8434     (while data
8435       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8436                 score)
8437         (push (gnus-data-number (car data)) articles))
8438       (setq data (cdr data)))
8439     (prog1
8440         (gnus-summary-limit articles)
8441       (gnus-summary-position-point))))
8442
8443 (defun gnus-summary-limit-to-unseen ()
8444   "Limit to unseen articles."
8445   (interactive)
8446   (prog1
8447       (gnus-summary-limit gnus-newsgroup-unseen)
8448     (gnus-summary-position-point)))
8449
8450 (defun gnus-summary-limit-include-thread (id)
8451   "Display all the hidden articles that is in the thread with ID in it.
8452 When called interactively, ID is the Message-ID of the current
8453 article."
8454   (interactive (list (mail-header-id (gnus-summary-article-header))))
8455   (let ((articles (gnus-articles-in-thread
8456                    (gnus-id-to-thread (gnus-root-id id))))
8457         ;;we REALLY want the whole thread---this prevents cut-threads
8458         ;;from removing the thread we want to include.
8459         (gnus-fetch-old-headers nil)
8460         (gnus-build-sparse-threads nil))
8461     (prog1
8462         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8463       (gnus-summary-limit-include-matching-articles
8464        "subject"
8465        (regexp-quote (gnus-simplify-subject-re
8466                       (mail-header-subject (gnus-id-to-header id)))))
8467       (gnus-summary-position-point))))
8468
8469 (defun gnus-summary-limit-include-matching-articles (header regexp)
8470   "Display all the hidden articles that have HEADERs that match REGEXP."
8471   (interactive (list (read-string "Match on header: ")
8472                      (read-string "Regexp: ")))
8473   (let ((articles (gnus-find-matching-articles header regexp)))
8474     (prog1
8475         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8476       (gnus-summary-position-point))))
8477
8478 (defun gnus-summary-insert-dormant-articles ()
8479   "Insert all the dormant articles for this group into the current buffer."
8480   (interactive)
8481   (let ((gnus-verbose (max 6 gnus-verbose)))
8482     (if (not gnus-newsgroup-dormant)
8483         (gnus-message 3 "No dormant articles for this group")
8484       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8485
8486 (defun gnus-summary-insert-ticked-articles ()
8487   "Insert ticked articles for this group into the current buffer."
8488   (interactive)
8489   (let ((gnus-verbose (max 6 gnus-verbose)))
8490     (if (not gnus-newsgroup-marked)
8491         (gnus-message 3 "No ticked articles for this group")
8492       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8493
8494 (defun gnus-summary-limit-include-dormant ()
8495   "Display all the hidden articles that are marked as dormant.
8496 Note that this command only works on a subset of the articles currently
8497 fetched for this group."
8498   (interactive)
8499   (unless gnus-newsgroup-dormant
8500     (error "There are no dormant articles in this group"))
8501   (prog1
8502       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8503     (gnus-summary-position-point)))
8504
8505 (defun gnus-summary-include-articles (articles)
8506   "Fetch the headers for ARTICLES and then display the summary lines."
8507   (let ((gnus-inhibit-demon t)
8508         (gnus-agent nil)
8509         (gnus-read-all-available-headers t))
8510     (setq gnus-newsgroup-headers
8511           (gnus-merge
8512            'list gnus-newsgroup-headers
8513            (gnus-fetch-headers articles nil t)
8514            'gnus-article-sort-by-number))
8515     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8516
8517 (defun gnus-summary-limit-exclude-dormant ()
8518   "Hide all dormant articles."
8519   (interactive)
8520   (prog1
8521       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8522     (gnus-summary-position-point)))
8523
8524 (defun gnus-summary-limit-exclude-childless-dormant ()
8525   "Hide all dormant articles that have no children."
8526   (interactive)
8527   (let ((data (gnus-data-list t))
8528         articles d children)
8529     ;; Find all articles that are either not dormant or have
8530     ;; children.
8531     (while (setq d (pop data))
8532       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8533                 (and (setq children
8534                            (gnus-article-children (gnus-data-number d)))
8535                      (let (found)
8536                        (while children
8537                          (when (memq (car children) articles)
8538                            (setq children nil
8539                                  found t))
8540                          (pop children))
8541                        found)))
8542         (push (gnus-data-number d) articles)))
8543     ;; Do the limiting.
8544     (prog1
8545         (gnus-summary-limit articles)
8546       (gnus-summary-position-point))))
8547
8548 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8549   "Mark all unread excluded articles as read.
8550 If ALL, mark even excluded ticked and dormants as read."
8551   (interactive "P")
8552   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8553   (let ((articles (gnus-sorted-ndifference
8554                    (sort
8555                     (mapcar (lambda (h) (mail-header-number h))
8556                             gnus-newsgroup-headers)
8557                     '<)
8558                    gnus-newsgroup-limit))
8559         article)
8560     (setq gnus-newsgroup-unreads
8561           (gnus-sorted-intersection gnus-newsgroup-unreads
8562                                     gnus-newsgroup-limit))
8563     (if all
8564         (setq gnus-newsgroup-dormant nil
8565               gnus-newsgroup-marked nil
8566               gnus-newsgroup-reads
8567               (nconc
8568                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8569                gnus-newsgroup-reads))
8570       (while (setq article (pop articles))
8571         (unless (or (memq article gnus-newsgroup-dormant)
8572                     (memq article gnus-newsgroup-marked))
8573           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8574
8575 (defun gnus-summary-limit (articles &optional pop)
8576   (if pop
8577       ;; We pop the previous limit off the stack and use that.
8578       (setq articles (car gnus-newsgroup-limits)
8579             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8580     ;; We use the new limit, so we push the old limit on the stack.
8581     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8582   ;; Set the limit.
8583   (setq gnus-newsgroup-limit articles)
8584   (let ((total (length gnus-newsgroup-data))
8585         (data (gnus-data-find-list (gnus-summary-article-number)))
8586         (gnus-summary-mark-below nil)   ; Inhibit this.
8587         found)
8588     ;; This will do all the work of generating the new summary buffer
8589     ;; according to the new limit.
8590     (gnus-summary-prepare)
8591     ;; Hide any threads, possibly.
8592     (gnus-summary-maybe-hide-threads)
8593     ;; Try to return to the article you were at, or one in the
8594     ;; neighborhood.
8595     (when data
8596       ;; We try to find some article after the current one.
8597       (while data
8598         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8599           (setq data nil
8600                 found t))
8601         (setq data (cdr data))))
8602     (unless found
8603       ;; If there is no data, that means that we were after the last
8604       ;; article.  The same goes when we can't find any articles
8605       ;; after the current one.
8606       (goto-char (point-max))
8607       (gnus-summary-find-prev))
8608     (gnus-set-mode-line 'summary)
8609     ;; We return how many articles were removed from the summary
8610     ;; buffer as a result of the new limit.
8611     (- total (length gnus-newsgroup-data))))
8612
8613 (defsubst gnus-invisible-cut-children (threads)
8614   (let ((num 0))
8615     (while threads
8616       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8617         (incf num))
8618       (pop threads))
8619     (< num 2)))
8620
8621 (defsubst gnus-cut-thread (thread)
8622   "Go forwards in the thread until we find an article that we want to display."
8623   (when (or (eq gnus-fetch-old-headers 'some)
8624             (eq gnus-fetch-old-headers 'invisible)
8625             (numberp gnus-fetch-old-headers)
8626             (eq gnus-build-sparse-threads 'some)
8627             (eq gnus-build-sparse-threads 'more))
8628     ;; Deal with old-fetched headers and sparse threads.
8629     (while (and
8630             thread
8631             (or
8632              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8633              (gnus-summary-article-ancient-p
8634               (mail-header-number (car thread))))
8635             (if (or (<= (length (cdr thread)) 1)
8636                     (eq gnus-fetch-old-headers 'invisible))
8637                 (setq gnus-newsgroup-limit
8638                       (delq (mail-header-number (car thread))
8639                             gnus-newsgroup-limit)
8640                       thread (cadr thread))
8641               (when (gnus-invisible-cut-children (cdr thread))
8642                 (let ((th (cdr thread)))
8643                   (while th
8644                     (if (memq (mail-header-number (caar th))
8645                               gnus-newsgroup-limit)
8646                         (setq thread (car th)
8647                               th nil)
8648                       (setq th (cdr th))))))))))
8649   thread)
8650
8651 (defun gnus-cut-threads (threads)
8652   "Cut off all uninteresting articles from the beginning of THREADS."
8653   (when (or (eq gnus-fetch-old-headers 'some)
8654             (eq gnus-fetch-old-headers 'invisible)
8655             (numberp gnus-fetch-old-headers)
8656             (eq gnus-build-sparse-threads 'some)
8657             (eq gnus-build-sparse-threads 'more))
8658     (let ((th threads))
8659       (while th
8660         (setcar th (gnus-cut-thread (car th)))
8661         (setq th (cdr th)))))
8662   ;; Remove nixed out threads.
8663   (delq nil threads))
8664
8665 (defun gnus-summary-initial-limit (&optional show-if-empty)
8666   "Figure out what the initial limit is supposed to be on group entry.
8667 This entails weeding out unwanted dormants, low-scored articles,
8668 fetch-old-headers verbiage, and so on."
8669   ;; Most groups have nothing to remove.
8670   (unless (or gnus-inhibit-limiting
8671               (and (null gnus-newsgroup-dormant)
8672                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8673                    (not (eq gnus-fetch-old-headers 'some))
8674                    (not (numberp gnus-fetch-old-headers))
8675                    (not (eq gnus-fetch-old-headers 'invisible))
8676                    (null gnus-summary-expunge-below)
8677                    (not (eq gnus-build-sparse-threads 'some))
8678                    (not (eq gnus-build-sparse-threads 'more))
8679                    (null gnus-thread-expunge-below)))
8680     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8681     (setq gnus-newsgroup-limit nil)
8682     (mapatoms
8683      (lambda (node)
8684        (unless (car (symbol-value node))
8685          ;; These threads have no parents -- they are roots.
8686          (let ((nodes (cdr (symbol-value node)))
8687                thread)
8688            (while nodes
8689              (if (and gnus-thread-expunge-below
8690                       (< (gnus-thread-total-score (car nodes))
8691                          gnus-thread-expunge-below))
8692                  (gnus-expunge-thread (pop nodes))
8693                (setq thread (pop nodes))
8694                (gnus-summary-limit-children thread))))))
8695      gnus-newsgroup-dependencies)
8696     ;; If this limitation resulted in an empty group, we might
8697     ;; pop the previous limit and use it instead.
8698     (when (and (not gnus-newsgroup-limit)
8699                show-if-empty)
8700       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8701     gnus-newsgroup-limit))
8702
8703 (defun gnus-summary-limit-children (thread)
8704   "Return 1 if this subthread is visible and 0 if it is not."
8705   ;; First we get the number of visible children to this thread.  This
8706   ;; is done by recursing down the thread using this function, so this
8707   ;; will really go down to a leaf article first, before slowly
8708   ;; working its way up towards the root.
8709   (when thread
8710     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8711            (children
8712            (if (cdr thread)
8713                (apply '+ (mapcar 'gnus-summary-limit-children
8714                                  (cdr thread)))
8715              0))
8716            (number (mail-header-number (car thread)))
8717            score)
8718       (if (and
8719            (not (memq number gnus-newsgroup-marked))
8720            (or
8721             ;; If this article is dormant and has absolutely no visible
8722             ;; children, then this article isn't visible.
8723             (and (memq number gnus-newsgroup-dormant)
8724                  (zerop children))
8725             ;; If this is "fetch-old-headered" and there is no
8726             ;; visible children, then we don't want this article.
8727             (and (or (eq gnus-fetch-old-headers 'some)
8728                      (numberp gnus-fetch-old-headers))
8729                  (gnus-summary-article-ancient-p number)
8730                  (zerop children))
8731             ;; If this is "fetch-old-headered" and `invisible', then
8732             ;; we don't want this article.
8733             (and (eq gnus-fetch-old-headers 'invisible)
8734                  (gnus-summary-article-ancient-p number))
8735             ;; If this is a sparsely inserted article with no children,
8736             ;; we don't want it.
8737             (and (eq gnus-build-sparse-threads 'some)
8738                  (gnus-summary-article-sparse-p number)
8739                  (zerop children))
8740             ;; If we use expunging, and this article is really
8741             ;; low-scored, then we don't want this article.
8742             (when (and gnus-summary-expunge-below
8743                        (< (setq score
8744                                 (or (cdr (assq number gnus-newsgroup-scored))
8745                                     gnus-summary-default-score))
8746                           gnus-summary-expunge-below))
8747               ;; We increase the expunge-tally here, but that has
8748               ;; nothing to do with the limits, really.
8749               (incf gnus-newsgroup-expunged-tally)
8750               ;; We also mark as read here, if that's wanted.
8751               (when (and gnus-summary-mark-below
8752                          (< score gnus-summary-mark-below))
8753                 (setq gnus-newsgroup-unreads
8754                       (delq number gnus-newsgroup-unreads))
8755                 (if gnus-newsgroup-auto-expire
8756                     (push number gnus-newsgroup-expirable)
8757                   (push (cons number gnus-low-score-mark)
8758                         gnus-newsgroup-reads)))
8759               t)
8760             ;; Do the `display' group parameter.
8761             (and gnus-newsgroup-display
8762                  (let ((gnus-number number))
8763                    (not (funcall gnus-newsgroup-display))))))
8764           ;; Nope, invisible article.
8765           0
8766         ;; Ok, this article is to be visible, so we add it to the limit
8767         ;; and return 1.
8768         (push number gnus-newsgroup-limit)
8769         1))))
8770
8771 (defun gnus-expunge-thread (thread)
8772   "Mark all articles in THREAD as read."
8773   (let* ((number (mail-header-number (car thread))))
8774     (incf gnus-newsgroup-expunged-tally)
8775     ;; We also mark as read here, if that's wanted.
8776     (setq gnus-newsgroup-unreads
8777           (delq number gnus-newsgroup-unreads))
8778     (if gnus-newsgroup-auto-expire
8779         (push number gnus-newsgroup-expirable)
8780       (push (cons number gnus-low-score-mark)
8781             gnus-newsgroup-reads)))
8782   ;; Go recursively through all subthreads.
8783   (mapcar 'gnus-expunge-thread (cdr thread)))
8784
8785 ;; Summary article oriented commands
8786
8787 (defun gnus-summary-refer-parent-article (n)
8788   "Refer parent article N times.
8789 If N is negative, go to ancestor -N instead.
8790 The difference between N and the number of articles fetched is returned."
8791   (interactive "p")
8792   (let ((skip 1)
8793         error header ref)
8794     (when (not (natnump n))
8795       (setq skip (abs n)
8796             n 1))
8797     (while (and (> n 0)
8798                 (not error))
8799       (setq header (gnus-summary-article-header))
8800       (if (and (eq (mail-header-number header)
8801                    (cdr gnus-article-current))
8802                (equal gnus-newsgroup-name
8803                       (car gnus-article-current)))
8804           ;; If we try to find the parent of the currently
8805           ;; displayed article, then we take a look at the actual
8806           ;; References header, since this is slightly more
8807           ;; reliable than the References field we got from the
8808           ;; server.
8809           (with-current-buffer gnus-original-article-buffer
8810             (nnheader-narrow-to-headers)
8811             (unless (setq ref (message-fetch-field "references"))
8812               (when (setq ref (message-fetch-field "in-reply-to"))
8813                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8814             (widen))
8815         (setq ref
8816               ;; It's not the current article, so we take a bet on
8817               ;; the value we got from the server.
8818               (mail-header-references header)))
8819       (if (and ref
8820                (not (equal ref "")))
8821           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8822             (gnus-message 1 "Couldn't find parent"))
8823         (gnus-message 1 "No references in article %d"
8824                       (gnus-summary-article-number))
8825         (setq error t))
8826       (decf n))
8827     (gnus-summary-position-point)
8828     n))
8829
8830 (defun gnus-summary-refer-references ()
8831   "Fetch all articles mentioned in the References header.
8832 Return the number of articles fetched."
8833   (interactive)
8834   (let ((ref (mail-header-references (gnus-summary-article-header)))
8835         (current (gnus-summary-article-number))
8836         (n 0))
8837     (if (or (not ref)
8838             (equal ref ""))
8839         (error "No References in the current article")
8840       ;; For each Message-ID in the References header...
8841       (while (string-match "<[^>]*>" ref)
8842         (incf n)
8843         ;; ... fetch that article.
8844         (gnus-summary-refer-article
8845          (prog1 (match-string 0 ref)
8846            (setq ref (substring ref (match-end 0))))))
8847       (gnus-summary-goto-subject current)
8848       (gnus-summary-position-point)
8849       n)))
8850
8851 (defun gnus-summary-refer-thread (&optional limit)
8852   "Fetch all articles in the current thread.
8853 If no backend-specific 'request-thread function is available
8854 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8855 fetch what's specified by the `gnus-refer-thread-limit'
8856 variable."
8857   (interactive "P")
8858   (gnus-warp-to-article)
8859   (let ((id (mail-header-id (gnus-summary-article-header)))
8860         (gnus-inhibit-demon t)
8861         (gnus-agent nil)
8862         (gnus-summary-ignore-duplicates t)
8863         (gnus-read-all-available-headers t)
8864         (limit (if limit (prefix-numeric-value limit)
8865                  gnus-refer-thread-limit)))
8866     (setq gnus-newsgroup-headers
8867           (gnus-merge
8868            'list gnus-newsgroup-headers
8869            (if (gnus-check-backend-function
8870                 'request-thread gnus-newsgroup-name)
8871                (gnus-request-thread id)
8872              (let* ((last (if (numberp limit)
8873                               (min (+ (mail-header-number
8874                                        (gnus-summary-article-header))
8875                                       limit)
8876                                    gnus-newsgroup-highest)
8877                             gnus-newsgroup-highest))
8878                     (subject (gnus-simplify-subject
8879                               (mail-header-subject
8880                                (gnus-summary-article-header))))
8881                     (refs (split-string (or (mail-header-references
8882                                              (gnus-summary-article-header))
8883                                             "")))
8884                     (gnus-parse-headers-hook
8885                      (lambda () (goto-char (point-min))
8886                        (keep-lines
8887                         (regexp-opt (append refs (list id subject)))))))
8888                (gnus-fetch-headers (list last) (if (numberp limit)
8889                                                    (* 2 limit) limit) t)))
8890            'gnus-article-sort-by-number))
8891     (gnus-summary-limit-include-thread id)))
8892
8893 (defun gnus-summary-refer-article (message-id)
8894   "Fetch an article specified by MESSAGE-ID."
8895   (interactive "sMessage-ID: ")
8896   (when (and (stringp message-id)
8897              (not (zerop (length message-id))))
8898     (setq message-id (gnus-replace-in-string message-id " " ""))
8899     ;; Construct the correct Message-ID if necessary.
8900     ;; Suggested by tale@pawl.rpi.edu.
8901     (unless (string-match "^<" message-id)
8902       (setq message-id (concat "<" message-id)))
8903     (unless (string-match ">$" message-id)
8904       (setq message-id (concat message-id ">")))
8905     ;; People often post MIDs from URLs, so unhex it:
8906     (unless (string-match "@" message-id)
8907       (setq message-id (gnus-url-unhex-string message-id)))
8908     (let* ((header (gnus-id-to-header message-id))
8909            (sparse (and header
8910                         (gnus-summary-article-sparse-p
8911                          (mail-header-number header))
8912                         (memq (mail-header-number header)
8913                               gnus-newsgroup-limit)))
8914            number)
8915       (cond
8916        ;; If the article is present in the buffer we just go to it.
8917        ((and header
8918              (or (not (gnus-summary-article-sparse-p
8919                        (mail-header-number header)))
8920                  sparse))
8921         (prog1
8922             (gnus-summary-goto-article
8923              (mail-header-number header) nil t)
8924           (when sparse
8925             (gnus-summary-update-article (mail-header-number header)))))
8926        (t
8927         ;; We fetch the article.
8928         (catch 'found
8929           (dolist (gnus-override-method (gnus-refer-article-methods))
8930             (when (and (gnus-check-server gnus-override-method)
8931                        ;; Fetch the header,
8932                        (setq number (gnus-summary-insert-subject message-id)))
8933               ;; and display the article.
8934               (gnus-summary-select-article nil nil nil number)
8935               (throw 'found t)))
8936           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8937
8938 (defun gnus-refer-article-methods ()
8939   "Return a list of referable methods."
8940   (cond
8941    ;; No method, so we default to current and native.
8942    ((null gnus-refer-article-method)
8943     (list gnus-current-select-method gnus-select-method))
8944    ;; Current.
8945    ((eq 'current gnus-refer-article-method)
8946     (list gnus-current-select-method))
8947    ;; List of select methods.
8948    ((not (and (symbolp (car gnus-refer-article-method))
8949               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8950     (let (out)
8951       (dolist (method gnus-refer-article-method)
8952         (push (if (eq 'current method)
8953                   gnus-current-select-method
8954                 method)
8955               out))
8956       (nreverse out)))
8957    ;; One single select method.
8958    (t
8959     (list gnus-refer-article-method))))
8960
8961 (defun gnus-summary-edit-parameters ()
8962   "Edit the group parameters of the current group."
8963   (interactive)
8964   (gnus-group-edit-group gnus-newsgroup-name 'params))
8965
8966 (defun gnus-summary-customize-parameters ()
8967   "Customize the group parameters of the current group."
8968   (interactive)
8969   (gnus-group-customize gnus-newsgroup-name))
8970
8971 (defun gnus-summary-enter-digest-group (&optional force)
8972   "Enter an nndoc group based on the current article.
8973 If FORCE, force a digest interpretation.  If not, try
8974 to guess what the document format is."
8975   (interactive "P")
8976   (let ((conf gnus-current-window-configuration))
8977     (save-window-excursion
8978       (save-excursion
8979         (let (gnus-article-prepare-hook
8980               gnus-display-mime-function
8981               gnus-break-pages)
8982           (gnus-summary-select-article))))
8983     (setq gnus-current-window-configuration conf)
8984     (let* ((name (format "%s-%d"
8985                          (gnus-group-prefixed-name
8986                           gnus-newsgroup-name (list 'nndoc ""))
8987                          (with-current-buffer gnus-summary-buffer
8988                            gnus-current-article)))
8989            (ogroup gnus-newsgroup-name)
8990            (params (append (gnus-info-params (gnus-get-info ogroup))
8991                            (list (cons 'to-group ogroup))
8992                            (list (cons 'parent-group ogroup))
8993                            (list (cons 'save-article-group ogroup))))
8994            (case-fold-search t)
8995            (buf (current-buffer))
8996            dig to-address)
8997       (with-current-buffer gnus-original-article-buffer
8998         ;; Have the digest group inherit the main mail address of
8999         ;; the parent article.
9000         (when (setq to-address (or (gnus-fetch-field "reply-to")
9001                                    (gnus-fetch-field "from")))
9002           (setq params
9003                 (append
9004                  (list (cons 'to-address
9005                              (funcall gnus-decode-encoded-address-function
9006                                       to-address))))))
9007         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9008         (insert-buffer-substring gnus-original-article-buffer)
9009         ;; Remove lines that may lead nndoc to misinterpret the
9010         ;; document type.
9011         (narrow-to-region
9012          (goto-char (point-min))
9013          (or (search-forward "\n\n" nil t) (point)))
9014         (goto-char (point-min))
9015         (delete-matching-lines "^Path:\\|^From ")
9016         (widen))
9017       (unwind-protect
9018           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9019                     (gnus-newsgroup-ephemeral-ignored-charsets
9020                      gnus-newsgroup-ignored-charsets))
9021                 (gnus-group-read-ephemeral-group
9022                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9023                               (nndoc-article-type
9024                                ,(if force 'mbox 'guess)))
9025                  t nil nil nil
9026                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9027                                                         "ADAPT")))))
9028               ;; Make all postings to this group go to the parent group.
9029               (nconc (gnus-info-params (gnus-get-info name))
9030                      params)
9031             ;; Couldn't select this doc group.
9032             (switch-to-buffer buf)
9033             (gnus-set-global-variables)
9034             (gnus-configure-windows 'summary)
9035             (gnus-message 3 "Article couldn't be entered?"))
9036         (kill-buffer dig)))))
9037
9038 (defun gnus-summary-read-document (n)
9039   "Open a new group based on the current article(s).
9040 This will allow you to read digests and other similar
9041 documents as newsgroups.
9042 Obeys the standard process/prefix convention."
9043   (interactive "P")
9044   (let* ((ogroup gnus-newsgroup-name)
9045          (params (append (gnus-info-params (gnus-get-info ogroup))
9046                          (list (cons 'to-group ogroup))))
9047          group egroup groups vgroup)
9048     (dolist (article (gnus-summary-work-articles n))
9049       (setq group (format "%s-%d" gnus-newsgroup-name article))
9050       (gnus-summary-remove-process-mark article)
9051       (when (gnus-summary-display-article article)
9052         (save-excursion ;;What for?
9053           (with-temp-buffer
9054             (insert-buffer-substring gnus-original-article-buffer)
9055             ;; Remove some headers that may lead nndoc to make
9056             ;; the wrong guess.
9057             (message-narrow-to-head)
9058             (goto-char (point-min))
9059             (delete-matching-lines "^Path:\\|^From ")
9060             (widen)
9061             (if (setq egroup
9062                       (gnus-group-read-ephemeral-group
9063                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9064                                      (nndoc-article-type guess))
9065                        t nil t))
9066                 (progn
9067                   ;; Make all postings to this group go to the parent group.
9068                   (nconc (gnus-info-params (gnus-get-info egroup))
9069                          params)
9070                   (push egroup groups))
9071               ;; Couldn't select this doc group.
9072               (gnus-error 3 "Article couldn't be entered"))))))
9073     ;; Now we have selected all the documents.
9074     (cond
9075      ((not groups)
9076       (error "None of the articles could be interpreted as documents"))
9077      ((gnus-group-read-ephemeral-group
9078        (setq vgroup (format
9079                      "nnvirtual:%s-%s" gnus-newsgroup-name
9080                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9081        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9082        t
9083        (cons (current-buffer) 'summary)))
9084      (t
9085       (error "Couldn't select virtual nndoc group")))))
9086
9087 (defun gnus-summary-widget-forward (arg)
9088   "Move point to the next field or button in the article.
9089 With optional ARG, move across that many fields."
9090   (interactive "p")
9091   (gnus-summary-select-article)
9092   (gnus-configure-windows 'article)
9093   (select-window (gnus-get-buffer-window gnus-article-buffer))
9094   (widget-forward arg))
9095
9096 (defun gnus-summary-isearch-article (&optional regexp-p)
9097   "Do incremental search forward on the current article.
9098 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9099   (interactive "P")
9100   (gnus-summary-select-article)
9101   (gnus-configure-windows 'article)
9102   (gnus-eval-in-buffer-window gnus-article-buffer
9103     (save-restriction
9104       (widen)
9105       (isearch-forward regexp-p))))
9106
9107 (defun gnus-summary-repeat-search-article-forward ()
9108   "Repeat the previous search forwards."
9109   (interactive)
9110   (unless gnus-last-search-regexp
9111     (error "No previous search"))
9112   (gnus-summary-search-article-forward gnus-last-search-regexp))
9113
9114 (defun gnus-summary-repeat-search-article-backward ()
9115   "Repeat the previous search backwards."
9116   (interactive)
9117   (unless gnus-last-search-regexp
9118     (error "No previous search"))
9119   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9120
9121 (defun gnus-summary-search-article-forward (regexp &optional backward)
9122   "Search for an article containing REGEXP forward.
9123 If BACKWARD, search backward instead."
9124   (interactive
9125    (list (read-string
9126           (format "Search article %s (regexp%s): "
9127                   (if current-prefix-arg "backward" "forward")
9128                   (if gnus-last-search-regexp
9129                       (concat ", default " gnus-last-search-regexp)
9130                     "")))
9131          current-prefix-arg))
9132   (if (string-equal regexp "")
9133       (setq regexp (or gnus-last-search-regexp ""))
9134     (setq gnus-last-search-regexp regexp)
9135     (setq gnus-article-before-search gnus-current-article))
9136   ;; Intentionally set gnus-last-article.
9137   (setq gnus-last-article gnus-article-before-search)
9138   (let ((gnus-last-article gnus-last-article))
9139     (if (gnus-summary-search-article regexp backward)
9140         (gnus-summary-show-thread)
9141       (signal 'search-failed (list regexp)))))
9142
9143 (defun gnus-summary-search-article-backward (regexp)
9144   "Search for an article containing REGEXP backward."
9145   (interactive
9146    (list (read-string
9147           (format "Search article backward (regexp%s): "
9148                   (if gnus-last-search-regexp
9149                       (concat ", default " gnus-last-search-regexp)
9150                     "")))))
9151   (gnus-summary-search-article-forward regexp 'backward))
9152
9153 (defun gnus-summary-search-article (regexp &optional backward)
9154   "Search for an article containing REGEXP.
9155 Optional argument BACKWARD means do search for backward.
9156 `gnus-select-article-hook' is not called during the search."
9157   ;; We have to require this here to make sure that the following
9158   ;; dynamic binding isn't shadowed by autoloading.
9159   (require 'gnus-async)
9160   (require 'gnus-art)
9161   (let ((gnus-select-article-hook nil)  ;Disable hook.
9162         (gnus-article-prepare-hook nil)
9163         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9164         (gnus-use-article-prefetch nil)
9165         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9166         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9167         (gnus-visual nil)
9168         (gnus-keep-backlog nil)
9169         (gnus-break-pages nil)
9170         (gnus-summary-display-arrow nil)
9171         (gnus-updated-mode-lines nil)
9172         (gnus-auto-center-summary nil)
9173         (sum (current-buffer))
9174         (gnus-display-mime-function nil)
9175         (found nil)
9176         point)
9177     (gnus-save-hidden-threads
9178       (gnus-summary-select-article)
9179       (set-buffer gnus-article-buffer)
9180       (goto-char (window-point (get-buffer-window (current-buffer))))
9181       (when backward
9182         (forward-line -1))
9183       (while (not found)
9184         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9185         (if (if backward
9186                 (re-search-backward regexp nil t)
9187               (re-search-forward regexp nil t))
9188             ;; We found the regexp.
9189             (progn
9190               (setq found 'found)
9191               (beginning-of-line)
9192               (set-window-start
9193                (get-buffer-window (current-buffer))
9194                (point))
9195               (forward-line 1)
9196               (set-window-point
9197                (get-buffer-window (current-buffer))
9198                (point))
9199               (set-buffer sum)
9200               (setq point (point)))
9201           ;; We didn't find it, so we go to the next article.
9202           (set-buffer sum)
9203           (setq found 'not)
9204           (while (eq found 'not)
9205             (if (not (if backward (gnus-summary-find-prev)
9206                        (gnus-summary-find-next)))
9207                 ;; No more articles.
9208                 (setq found t)
9209               ;; Select the next article and adjust point.
9210               (unless (gnus-summary-article-sparse-p
9211                        (gnus-summary-article-number))
9212                 (setq found nil)
9213                 (gnus-summary-select-article)
9214                 (set-buffer gnus-article-buffer)
9215                 (widen)
9216                 (goto-char (if backward (point-max) (point-min))))))))
9217       (gnus-message 7 ""))
9218     ;; Return whether we found the regexp.
9219     (when (eq found 'found)
9220       (goto-char point)
9221       (gnus-summary-show-thread)
9222       (gnus-summary-goto-subject gnus-current-article)
9223       (gnus-summary-position-point)
9224       t)))
9225
9226 (defun gnus-find-matching-articles (header regexp)
9227   "Return a list of all articles that match REGEXP on HEADER.
9228 This search includes all articles in the current group that Gnus has
9229 fetched headers for, whether they are displayed or not."
9230   (let ((articles nil)
9231         ;; Can't eta-reduce because it's a macro.
9232         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9233         (case-fold-search t))
9234     (dolist (header gnus-newsgroup-headers)
9235       (when (string-match regexp (funcall func header))
9236         (push (mail-header-number header) articles)))
9237     (nreverse articles)))
9238
9239 (defun gnus-summary-find-matching (header regexp &optional backward unread
9240                                           not-case-fold not-matching)
9241   "Return a list of all articles that match REGEXP on HEADER.
9242 The search stars on the current article and goes forwards unless
9243 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9244 If UNREAD is non-nil, only unread articles will
9245 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9246 in the comparisons. If NOT-MATCHING, return a list of all articles that
9247 not match REGEXP on HEADER."
9248   (let ((case-fold-search (not not-case-fold))
9249         articles d func)
9250     (if (consp header)
9251         (if (eq (car header) 'extra)
9252             (setq func
9253                   `(lambda (h)
9254                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9255                          "")))
9256           (error "%s is an invalid header" header))
9257       (unless (fboundp (intern (concat "mail-header-" header)))
9258         (error "%s is not a valid header" header))
9259       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9260     (dolist (d (if (eq backward 'all)
9261                    gnus-newsgroup-data
9262                  (gnus-data-find-list
9263                   (gnus-summary-article-number)
9264                   (gnus-data-list backward))))
9265       (when (and (or (not unread)       ; We want all articles...
9266                      (gnus-data-unread-p d)) ; Or just unreads.
9267                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9268                  (if not-matching
9269                      (not (string-match
9270                            regexp
9271                            (funcall func (gnus-data-header d))))
9272                    (string-match regexp
9273                                  (funcall func (gnus-data-header d)))))
9274         (push (gnus-data-number d) articles))) ; Success!
9275     (nreverse articles)))
9276
9277 (defun gnus-summary-execute-command (header regexp command &optional backward)
9278   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9279 If HEADER is an empty string (or nil), the match is done on the entire
9280 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9281   (interactive
9282    (list (let ((completion-ignore-case t))
9283            (gnus-completing-read
9284             "Header name"
9285             (mapcar 'symbol-name
9286                     (append
9287                      '(Number Subject From Lines Date
9288                        Message-ID Xref References Body)
9289                      gnus-extra-headers))
9290             'require-match))
9291          (read-string "Regexp: ")
9292          (read-key-sequence "Command: ")
9293          current-prefix-arg))
9294   (when (equal header "Body")
9295     (setq header ""))
9296   ;; Hidden thread subtrees must be searched as well.
9297   (gnus-summary-show-all-threads)
9298   ;; We don't want to change current point nor window configuration.
9299   (save-excursion
9300     (save-window-excursion
9301       (let (gnus-visual
9302             gnus-treat-strip-trailing-blank-lines
9303             gnus-treat-strip-leading-blank-lines
9304             gnus-treat-strip-multiple-blank-lines
9305             gnus-treat-hide-boring-headers
9306             gnus-treat-fold-newsgroups
9307             gnus-article-prepare-hook)
9308         (gnus-message 6 "Executing %s..." (key-description command))
9309         ;; We'd like to execute COMMAND interactively so as to give arguments.
9310         (gnus-execute header regexp
9311                       `(call-interactively ',(key-binding command))
9312                       backward)
9313         (gnus-message 6 "Executing %s...done" (key-description command))))))
9314
9315 (defun gnus-summary-beginning-of-article ()
9316   "Scroll the article back to the beginning."
9317   (interactive)
9318   (gnus-summary-select-article)
9319   (gnus-configure-windows 'article)
9320   (gnus-eval-in-buffer-window gnus-article-buffer
9321     (widen)
9322     (goto-char (point-min))
9323     (when gnus-break-pages
9324       (gnus-narrow-to-page))))
9325
9326 (defun gnus-summary-end-of-article ()
9327   "Scroll to the end of the article."
9328   (interactive)
9329   (gnus-summary-select-article)
9330   (gnus-configure-windows 'article)
9331   (gnus-eval-in-buffer-window gnus-article-buffer
9332     (widen)
9333     (goto-char (point-max))
9334     (recenter -3)
9335     (when gnus-break-pages
9336       (gnus-narrow-to-page))))
9337
9338 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9339   "Truncate to LEN and quote all \"(\"'s in STRING."
9340   (gnus-replace-in-string (if (and len (> (length string) len))
9341                               (substring string 0 len)
9342                             string)
9343                           "[()]" "\\\\\\&"))
9344
9345 (defun gnus-summary-print-article (&optional filename n)
9346   "Generate and print a PostScript image of the process-marked (mail) articles.
9347
9348 If used interactively, print the current article if none are
9349 process-marked.  With prefix arg, prompt the user for the name of the
9350 file to save in.
9351
9352 When used from Lisp, accept two optional args FILENAME and N.  N means
9353 to print the next N articles.  If N is negative, print the N previous
9354 articles.  If N is nil and articles have been marked with the process
9355 mark, print these instead.
9356
9357 If the optional first argument FILENAME is nil, send the image to the
9358 printer.  If FILENAME is a string, save the PostScript image in a file with
9359 that name.  If FILENAME is a number, prompt the user for the name of the file
9360 to save in."
9361   (interactive (list (ps-print-preprint current-prefix-arg)))
9362   (dolist (article (gnus-summary-work-articles n))
9363     (gnus-summary-select-article nil nil 'pseudo article)
9364     (gnus-eval-in-buffer-window gnus-article-buffer
9365       (gnus-print-buffer))
9366     (gnus-summary-remove-process-mark article))
9367   (ps-despool filename))
9368
9369 (defun gnus-print-buffer ()
9370   (let ((ps-left-header
9371          (list
9372           (concat "("
9373                   (gnus-summary-print-truncate-and-quote
9374                    (mail-header-subject gnus-current-headers)
9375                    66) ")")
9376           (concat "("
9377                   (gnus-summary-print-truncate-and-quote
9378                    (mail-header-from gnus-current-headers)
9379                    45) ")")))
9380         (ps-right-header
9381          (list
9382           "/pagenumberstring load"
9383           (concat "("
9384                   (mail-header-date gnus-current-headers) ")"))))
9385     (gnus-run-hooks 'gnus-ps-print-hook)
9386     (save-excursion
9387       (if ps-print-color-p
9388           (ps-spool-buffer-with-faces)
9389         (ps-spool-buffer)))))
9390
9391 (defun gnus-summary-show-complete-article ()
9392   "Show a complete version of the current article.
9393 This is only useful if you're looking at a partial version of the
9394 article currently."
9395   (interactive)
9396   (let ((gnus-keep-backlog nil)
9397         (gnus-use-cache nil)
9398         (gnus-agent nil)
9399         (variable (intern
9400                    (format "%s-fetch-partial-articles"
9401                            (car (gnus-find-method-for-group
9402                                  gnus-newsgroup-name)))
9403                    obarray))
9404         old-val)
9405     (unwind-protect
9406         (progn
9407           (setq old-val (symbol-value variable))
9408           (set variable nil)
9409           (gnus-flush-original-article-buffer)
9410           (gnus-summary-show-article))
9411       (set variable old-val))))
9412
9413 (defun gnus-summary-show-article (&optional arg)
9414   "Force redisplaying of the current article.
9415 If ARG (the prefix) is a number, show the article with the charset
9416 defined in `gnus-summary-show-article-charset-alist', or the charset
9417 input.
9418 If ARG (the prefix) is non-nil and not a number, show the article,
9419 but without running any of the article treatment functions
9420 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9421 C-u g', show the raw article."
9422   (interactive "P")
9423   (cond
9424    ((numberp arg)
9425     (gnus-summary-show-article t)
9426     (let ((gnus-newsgroup-charset
9427            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9428                (mm-read-coding-system
9429                 "View as charset: " ;; actually it is coding system.
9430                 (with-current-buffer gnus-article-buffer
9431                   (mm-detect-coding-region (point) (point-max))))))
9432           (gnus-newsgroup-ignored-charsets 'gnus-all))
9433       (gnus-summary-select-article nil 'force)
9434       (let ((deps gnus-newsgroup-dependencies)
9435             head header lines)
9436         (with-current-buffer gnus-original-article-buffer
9437           (save-restriction
9438             (message-narrow-to-head)
9439             (setq head (buffer-string))
9440             (goto-char (point-min))
9441             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9442               (goto-char (point-max))
9443               (widen)
9444               (setq lines (1- (count-lines (point) (point-max))))))
9445           (with-temp-buffer
9446             (insert (format "211 %d Article retrieved.\n"
9447                             (cdr gnus-article-current)))
9448             (insert head)
9449             (if lines (insert (format "Lines: %d\n" lines)))
9450             (insert ".\n")
9451             (let ((nntp-server-buffer (current-buffer)))
9452               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9453         (gnus-data-set-header
9454          (gnus-data-find (cdr gnus-article-current))
9455          header)
9456         (gnus-summary-update-article-line
9457          (cdr gnus-article-current) header)
9458         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9459           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9460    ((not arg)
9461     ;; Select the article the normal way.
9462     (gnus-summary-select-article nil 'force))
9463    ((or (equal arg '(16))
9464         (eq arg t))
9465     ;; C-u C-u g
9466     ;; We have to require this here to make sure that the following
9467     ;; dynamic binding isn't shadowed by autoloading.
9468     (require 'gnus-async)
9469     (require 'gnus-art)
9470     ;; Bind the article treatment functions to nil.
9471     (let ((gnus-have-all-headers t)
9472           gnus-article-prepare-hook
9473           gnus-article-decode-hook
9474           gnus-display-mime-function
9475           gnus-break-pages)
9476       ;; Destroy any MIME parts.
9477       (when (gnus-buffer-live-p gnus-article-buffer)
9478         (with-current-buffer gnus-article-buffer
9479           (mm-destroy-parts gnus-article-mime-handles)
9480           ;; Set it to nil for safety reason.
9481           (setq gnus-article-mime-handle-alist nil)
9482           (setq gnus-article-mime-handles nil)))
9483       (gnus-summary-select-article nil 'force)))
9484    (t
9485     (let ((gnus-inhibit-article-treatments t))
9486       (gnus-summary-select-article nil 'force))))
9487   (gnus-summary-goto-subject gnus-current-article)
9488   (gnus-summary-position-point))
9489
9490 (defun gnus-summary-show-raw-article ()
9491   "Show the raw article without any article massaging functions being run."
9492   (interactive)
9493   (gnus-summary-show-article t))
9494
9495 (defun gnus-summary-verbose-headers (&optional arg)
9496   "Toggle permanent full header display.
9497 If ARG is a positive number, turn header display on.
9498 If ARG is a negative number, turn header display off."
9499   (interactive "P")
9500   (setq gnus-show-all-headers
9501         (cond ((or (not (numberp arg))
9502                    (zerop arg))
9503                (not gnus-show-all-headers))
9504               ((natnump arg)
9505                t)))
9506   (gnus-summary-show-article))
9507
9508 (defun gnus-summary-toggle-header (&optional arg)
9509   "Show the headers if they are hidden, or hide them if they are shown.
9510 If ARG is a positive number, show the entire header.
9511 If ARG is a negative number, hide the unwanted header lines."
9512   (interactive "P")
9513   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9514                      (get-buffer-window gnus-article-buffer t))))
9515     (with-current-buffer gnus-article-buffer
9516       (widen)
9517       (article-narrow-to-head)
9518       (let* ((inhibit-read-only t)
9519              (inhibit-point-motion-hooks t)
9520              (hidden (if (numberp arg)
9521                          (>= arg 0)
9522                        (or (not (looking-at "[^ \t\n]+:"))
9523                            (gnus-article-hidden-text-p 'headers))))
9524              s e)
9525         (delete-region (point-min) (point-max))
9526         (with-current-buffer gnus-original-article-buffer
9527           (goto-char (setq s (point-min)))
9528           (setq e (if (search-forward "\n\n" nil t)
9529                       (1- (point))
9530                     (point-max))))
9531         (insert-buffer-substring gnus-original-article-buffer s e)
9532         (run-hooks 'gnus-article-decode-hook)
9533         (if hidden
9534             (let ((gnus-treat-hide-headers nil)
9535                   (gnus-treat-hide-boring-headers nil))
9536               (gnus-delete-wash-type 'headers)
9537               (gnus-treat-article 'head))
9538           (gnus-treat-article 'head))
9539         (widen)
9540         (if window
9541             (set-window-start window (goto-char (point-min))))
9542         (if gnus-break-pages
9543             (gnus-narrow-to-page)
9544           (when (gnus-visual-p 'page-marker)
9545             (let ((inhibit-read-only t))
9546               (gnus-remove-text-with-property 'gnus-prev)
9547               (gnus-remove-text-with-property 'gnus-next))))
9548         (gnus-set-mode-line 'article)))))
9549
9550 (defun gnus-summary-show-all-headers ()
9551   "Make all header lines visible."
9552   (interactive)
9553   (gnus-summary-toggle-header 1))
9554
9555 (defun gnus-summary-caesar-message (&optional arg)
9556   "Caesar rotate the current article by 13.
9557 With a non-numerical prefix, also rotate headers.  A numerical
9558 prefix specifies how many places to rotate each letter forward."
9559   (interactive "P")
9560   (gnus-summary-select-article)
9561   (let ((mail-header-separator ""))
9562     (gnus-eval-in-buffer-window gnus-article-buffer
9563       (save-restriction
9564         (widen)
9565         (let ((start (window-start))
9566               (inhibit-read-only t))
9567           (if (equal arg '(4))
9568               (message-caesar-buffer-body nil t)
9569             (message-caesar-buffer-body arg))
9570           (set-window-start (get-buffer-window (current-buffer)) start)))))
9571   ;; Create buttons and stuff...
9572   (gnus-treat-article nil))
9573
9574 (declare-function idna-to-unicode "ext:idna" (str))
9575
9576 (defun gnus-summary-idna-message (&optional arg)
9577   "Decode IDNA encoded domain names in the current articles.
9578 IDNA encoded domain names looks like `xn--bar'.  If a string
9579 remain unencoded after running this function, it is likely an
9580 invalid IDNA string (`xn--bar' is invalid).
9581
9582 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9583 installed for this command to work."
9584   (interactive "P")
9585   (if (not (and (condition-case nil (require 'idna)
9586                   (file-error))
9587                 (mm-coding-system-p 'utf-8)
9588                 (executable-find (symbol-value 'idna-program))))
9589       (gnus-message
9590        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9591     (gnus-summary-select-article)
9592     (let ((mail-header-separator ""))
9593       (gnus-eval-in-buffer-window gnus-article-buffer
9594         (save-restriction
9595           (widen)
9596           (let ((start (window-start))
9597                 buffer-read-only)
9598             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9599               (replace-match (idna-to-unicode (match-string 1))))
9600             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9601
9602 (defun gnus-summary-morse-message (&optional arg)
9603   "Morse decode the current article."
9604   (interactive "P")
9605   (gnus-summary-select-article)
9606   (let ((mail-header-separator ""))
9607     (gnus-eval-in-buffer-window gnus-article-buffer
9608       (save-excursion
9609         (save-restriction
9610           (widen)
9611           (let ((pos (window-start))
9612                 (inhibit-read-only t))
9613             (goto-char (point-min))
9614             (when (message-goto-body)
9615               (gnus-narrow-to-body))
9616             (goto-char (point-min))
9617             (while (search-forward "·" (point-max) t)
9618               (replace-match "."))
9619             (unmorse-region (point-min) (point-max))
9620             (widen)
9621             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9622
9623 (defun gnus-summary-stop-page-breaking ()
9624   "Stop page breaking in the current article."
9625   (interactive)
9626   (gnus-summary-select-article)
9627   (gnus-eval-in-buffer-window gnus-article-buffer
9628     (widen)
9629     (when (gnus-visual-p 'page-marker)
9630       (let ((inhibit-read-only t))
9631         (gnus-remove-text-with-property 'gnus-prev)
9632         (gnus-remove-text-with-property 'gnus-next))
9633       (setq gnus-page-broken nil))))
9634
9635 (defun gnus-summary-move-article (&optional n to-newsgroup
9636                                             select-method action)
9637   "Move the current article to a different newsgroup.
9638 If N is a positive number, move the N next articles.
9639 If N is a negative number, move the N previous articles.
9640 If N is nil and any articles have been marked with the process mark,
9641 move those articles instead.
9642 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9643 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9644 re-spool using this method.
9645
9646 When called interactively with TO-NEWSGROUP being nil, the value of
9647 the variable `gnus-move-split-methods' is used for finding a default
9648 for the target newsgroup.
9649
9650 For this function to work, both the current newsgroup and the
9651 newsgroup that you want to move to have to support the `request-move'
9652 and `request-accept' functions.
9653
9654 ACTION can be either `move' (the default), `crosspost' or `copy'."
9655   (interactive "P")
9656   (unless action
9657     (setq action 'move))
9658   ;; Check whether the source group supports the required functions.
9659   (cond ((and (eq action 'move)
9660               (not (gnus-check-backend-function
9661                     'request-move-article gnus-newsgroup-name)))
9662          (error "The current group does not support article moving"))
9663         ((and (eq action 'crosspost)
9664               (not (gnus-check-backend-function
9665                     'request-replace-article gnus-newsgroup-name)))
9666          (error "The current group does not support article editing")))
9667   (let ((articles (gnus-summary-work-articles n))
9668         (prefix (if (gnus-check-backend-function
9669                      'request-move-article gnus-newsgroup-name)
9670                     (funcall gnus-move-group-prefix-function
9671                              gnus-newsgroup-name)
9672                   ""))
9673         (names '((move "Move" "Moving")
9674                  (copy "Copy" "Copying")
9675                  (crosspost "Crosspost" "Crossposting")))
9676         (copy-buf (save-excursion
9677                     (nnheader-set-temp-buffer " *copy article*")))
9678         art-group to-method new-xref article to-groups
9679         articles-to-update-marks encoded)
9680     (unless (assq action names)
9681       (error "Unknown action %s" action))
9682     ;; Read the newsgroup name.
9683     (when (and (not to-newsgroup)
9684                (not select-method))
9685       (if (and gnus-move-split-methods
9686                (not
9687                 (and (memq gnus-current-article articles)
9688                      (gnus-buffer-live-p gnus-original-article-buffer))))
9689           ;; When `gnus-move-split-methods' is non-nil, we have to
9690           ;; select an article to give `gnus-read-move-group-name' an
9691           ;; opportunity to suggest an appropriate default.  However,
9692           ;; we needn't render or mark the article.
9693           (let ((gnus-display-mime-function nil)
9694                 (gnus-article-prepare-hook nil)
9695                 (gnus-mark-article-hook nil))
9696             (gnus-summary-select-article nil nil nil (car articles))))
9697       (setq to-newsgroup (gnus-read-move-group-name
9698                           (cadr (assq action names))
9699                           (symbol-value
9700                            (intern (format "gnus-current-%s-group" action)))
9701                           articles prefix)
9702             encoded to-newsgroup
9703             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9704       (set (intern (format "gnus-current-%s-group" action))
9705            (mm-decode-coding-string
9706             to-newsgroup
9707             (gnus-group-name-charset to-method to-newsgroup))))
9708     (unless to-method
9709       (setq to-method (or select-method
9710                           (gnus-server-to-method
9711                            (gnus-group-method to-newsgroup)))))
9712     (setq to-newsgroup
9713           (or encoded
9714               (and to-newsgroup
9715                    (mm-encode-coding-string
9716                     to-newsgroup
9717                     (gnus-group-name-charset to-method to-newsgroup)))))
9718     ;; Check the method we are to move this article to...
9719     (unless (gnus-check-backend-function
9720              'request-accept-article (car to-method))
9721       (error "%s does not support article copying" (car to-method)))
9722     (unless (gnus-check-server to-method)
9723       (error "Can't open server %s" (car to-method)))
9724     (gnus-message 6 "%s to %s: %s..."
9725                   (caddr (assq action names))
9726                   (or (car select-method)
9727                       (gnus-group-decoded-name to-newsgroup))
9728                   articles)
9729     (while articles
9730       (setq article (pop articles))
9731       ;; Set any marks that may have changed in the summary buffer.
9732       (when gnus-preserve-marks
9733         (gnus-summary-push-marks-to-backend article))
9734       (let ((gnus-newsgroup-original-name gnus-newsgroup-name)
9735             (gnus-article-original-subject
9736              (mail-header-subject
9737               (gnus-data-header (assoc article (gnus-data-list nil))))))
9738         (setq
9739          art-group
9740          (cond
9741           ;; Move the article.
9742           ((eq action 'move)
9743            ;; Remove this article from future suppression.
9744            (gnus-dup-unsuppress-article article)
9745            (let* ((from-method (gnus-find-method-for-group
9746                                 gnus-newsgroup-name))
9747                   (to-method (or select-method
9748                                  (gnus-find-method-for-group to-newsgroup)))
9749                   (move-is-internal (gnus-server-equal from-method to-method)))
9750              (gnus-request-move-article
9751               article                   ; Article to move
9752               gnus-newsgroup-name       ; From newsgroup
9753               (nth 1 (gnus-find-method-for-group
9754                       gnus-newsgroup-name)) ; Server
9755               (list 'gnus-request-accept-article
9756                     to-newsgroup (list 'quote select-method)
9757                     (not articles) t)   ; Accept form
9758               (not articles)            ; Only save nov last time
9759               (and move-is-internal
9760                    to-newsgroup         ; Not respooling
9761                                         ; Is this move internal?
9762                    (gnus-group-real-name to-newsgroup)))))
9763           ;; Copy the article.
9764           ((eq action 'copy)
9765            (with-current-buffer copy-buf
9766              (when (gnus-request-article-this-buffer article
9767                                                      gnus-newsgroup-name)
9768                (save-restriction
9769                  (nnheader-narrow-to-headers)
9770                  (dolist (hdr gnus-copy-article-ignored-headers)
9771                    (message-remove-header hdr t)))
9772                (gnus-request-accept-article
9773                 to-newsgroup select-method (not articles) t))))
9774           ;; Crosspost the article.
9775           ((eq action 'crosspost)
9776            (let ((xref (message-tokenize-header
9777                         (mail-header-xref (gnus-summary-article-header
9778                                            article))
9779                         " ")))
9780              (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9781                                     ":" (number-to-string article)))
9782              (unless xref
9783                (setq xref (list (system-name))))
9784              (setq new-xref
9785                    (concat
9786                     (mapconcat 'identity
9787                                (delete "Xref:" (delete new-xref xref))
9788                                " ")
9789                     " " new-xref))
9790              (with-current-buffer copy-buf
9791                ;; First put the article in the destination group.
9792                (gnus-request-article-this-buffer article gnus-newsgroup-name)
9793                (when (consp (setq art-group
9794                                   (gnus-request-accept-article
9795                                    to-newsgroup select-method (not articles)
9796                                    t)))
9797                  (setq new-xref (concat new-xref " " (car art-group)
9798                                         ":"
9799                                         (number-to-string (cdr art-group))))
9800                  ;; Now we have the new Xrefs header, so we insert
9801                  ;; it and replace the new article.
9802                  (nnheader-replace-header "Xref" new-xref)
9803                  (gnus-request-replace-article
9804                   (cdr art-group) to-newsgroup (current-buffer) t)
9805                  art-group))))))
9806         (cond
9807          ((not art-group)
9808           (gnus-message 1 "Couldn't %s article %s: %s"
9809                         (cadr (assq action names)) article
9810                         (nnheader-get-report (car to-method))))
9811          ((eq art-group 'junk)
9812           (when (eq action 'move)
9813             (gnus-summary-mark-article article gnus-canceled-mark)
9814             (gnus-message 4 "Deleted article %s" article)
9815             ;; run the delete hook
9816             (run-hook-with-args 'gnus-summary-article-delete-hook
9817                                 action
9818                                 (gnus-data-header
9819                                  (assoc article (gnus-data-list nil)))
9820                                 gnus-newsgroup-original-name nil
9821                                 select-method)))
9822          (t
9823           (let* ((pto-group (gnus-group-prefixed-name
9824                              (car art-group) to-method))
9825                  (info (gnus-get-info pto-group))
9826                  (to-group (gnus-info-group info))
9827                  to-marks)
9828             ;; Update the group that has been moved to.
9829             (when (and info
9830                        (memq action '(move copy)))
9831               (unless (member to-group to-groups)
9832                 (push to-group to-groups))
9833
9834               (unless (memq article gnus-newsgroup-unreads)
9835                 (push 'read to-marks)
9836                 (gnus-info-set-read
9837                  info (gnus-add-to-range (gnus-info-read info)
9838                                          (list (cdr art-group)))))
9839
9840               ;; See whether the article is to be put in the cache.
9841               (let* ((expirable (gnus-group-auto-expirable-p to-group))
9842                      (marks (if expirable
9843                                 gnus-article-mark-lists
9844                               (delete '(expirable . expire)
9845                                       (copy-sequence
9846                                        gnus-article-mark-lists))))
9847                      (to-article (cdr art-group)))
9848
9849                 ;; Enter the article into the cache in the new group,
9850                 ;; if that is required.
9851                 (when gnus-use-cache
9852                   (gnus-cache-possibly-enter-article
9853                    to-group to-article
9854                    (memq article gnus-newsgroup-marked)
9855                    (memq article gnus-newsgroup-dormant)
9856                    (memq article gnus-newsgroup-unreads)))
9857
9858                 (when gnus-preserve-marks
9859                   ;; Copy any marks over to the new group.
9860                   (when (and (equal to-group gnus-newsgroup-name)
9861                              (not (memq article gnus-newsgroup-unreads)))
9862                     ;; Mark this article as read in this group.
9863                     (push (cons to-article gnus-read-mark)
9864                           gnus-newsgroup-reads)
9865                     ;; Increase the active status of this group.
9866                     (setcdr (gnus-active to-group) to-article)
9867                     (setcdr gnus-newsgroup-active to-article))
9868
9869                   (while marks
9870                     (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9871                       (when (memq article (symbol-value
9872                                            (intern (format "gnus-newsgroup-%s"
9873                                                            (caar marks)))))
9874                         (push (cdar marks) to-marks)
9875                         ;; If the other group is the same as this group,
9876                         ;; then we have to add the mark to the list.
9877                         (when (equal to-group gnus-newsgroup-name)
9878                           (set (intern (format "gnus-newsgroup-%s"
9879                                                (caar marks)))
9880                                (cons to-article
9881                                      (symbol-value
9882                                       (intern (format "gnus-newsgroup-%s"
9883                                                       (caar marks)))))))
9884                         ;; Copy the marks to other group.
9885                         (gnus-add-marked-articles
9886                          to-group (cdar marks) (list to-article) info)))
9887                     (setq marks (cdr marks)))
9888
9889                   (when (and expirable
9890                              gnus-mark-copied-or-moved-articles-as-expirable
9891                              (not (memq 'expire to-marks)))
9892                     ;; Mark this article as expirable.
9893                     (push 'expire to-marks)
9894                     (when (equal to-group gnus-newsgroup-name)
9895                       (push to-article gnus-newsgroup-expirable))
9896                     ;; Copy the expirable mark to other group.
9897                     (gnus-add-marked-articles
9898                      to-group 'expire (list to-article) info))
9899
9900                   (when to-marks
9901                     (gnus-request-set-mark
9902                      to-group (list (list (list to-article) 'add to-marks)))))
9903
9904                 (gnus-dribble-enter
9905                  (concat "(gnus-group-set-info '"
9906                          (gnus-prin1-to-string (gnus-get-info to-group))
9907                          ")"))))
9908
9909             ;; Update the Xref header in this article to point to
9910             ;; the new crossposted article we have just created.
9911             (when (eq action 'crosspost)
9912               (with-current-buffer copy-buf
9913                 (gnus-request-article-this-buffer article gnus-newsgroup-name)
9914                 (nnheader-replace-header "Xref" new-xref)
9915                 (gnus-request-replace-article
9916                  article gnus-newsgroup-name (current-buffer) t)))
9917
9918             ;; run the move/copy/crosspost/respool hook
9919             (let ((header (gnus-data-header
9920                            (assoc article (gnus-data-list nil)))))
9921               (mail-header-set-subject header gnus-article-original-subject)
9922               (run-hook-with-args 'gnus-summary-article-move-hook
9923                                   action
9924                                   (gnus-data-header
9925                                    (assoc article (gnus-data-list nil)))
9926                                   gnus-newsgroup-original-name
9927                                   to-newsgroup
9928                                   select-method)))
9929
9930           ;;;!!!Why is this necessary?
9931           (set-buffer gnus-summary-buffer)
9932
9933           (when (eq action 'move)
9934             (save-excursion
9935               (gnus-summary-goto-subject article)
9936               (gnus-summary-mark-article article gnus-canceled-mark)))))
9937         (push article articles-to-update-marks)))
9938
9939     (save-excursion
9940       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9941     ;; Re-activate all groups that have been moved to.
9942     (with-current-buffer gnus-group-buffer
9943       (let ((gnus-group-marked to-groups))
9944         (gnus-group-get-new-news-this-group nil t)))
9945
9946     (gnus-kill-buffer copy-buf)
9947     (gnus-summary-position-point)
9948     (gnus-set-mode-line 'summary)))
9949
9950 (defun gnus-summary-push-marks-to-backend (article)
9951   (let ((set nil)
9952         (marks gnus-article-mark-lists))
9953     (when (memq article gnus-newsgroup-unreads)
9954       (push 'read set))
9955     (while marks
9956       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9957                  (memq article (symbol-value
9958                                 (intern (format "gnus-newsgroup-%s"
9959                                                 (caar marks))))))
9960         (push (cdar marks) set))
9961       (pop marks))
9962     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
9963
9964 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9965   "Copy the current article to some other group.
9966 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9967 When called interactively, if TO-NEWSGROUP is nil, use the value of
9968 the variable `gnus-move-split-methods' for finding a default target
9969 newsgroup.
9970 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9971 re-spool using this method."
9972   (interactive "P")
9973   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9974
9975 (defun gnus-summary-crosspost-article (&optional n)
9976   "Crosspost the current article to some other group."
9977   (interactive "P")
9978   (gnus-summary-move-article n nil nil 'crosspost))
9979
9980 (defcustom gnus-summary-respool-default-method nil
9981   "Default method type for respooling an article.
9982 If nil, use to the current newsgroup method."
9983   :type 'symbol
9984   :group 'gnus-summary-mail)
9985
9986 (defun gnus-summary-respool-article (&optional n method)
9987   "Respool the current article.
9988 The article will be squeezed through the mail spooling process again,
9989 which means that it will be put in some mail newsgroup or other
9990 depending on `nnmail-split-methods'.
9991 If N is a positive number, respool the N next articles.
9992 If N is a negative number, respool the N previous articles.
9993 If N is nil and any articles have been marked with the process mark,
9994 respool those articles instead.
9995
9996 Respooling can be done both from mail groups and \"real\" newsgroups.
9997 In the former case, the articles in question will be moved from the
9998 current group into whatever groups they are destined to.  In the
9999 latter case, they will be copied into the relevant groups."
10000   (interactive
10001    (list current-prefix-arg
10002          (let* ((methods (gnus-methods-using 'respool))
10003                 (methname
10004                  (symbol-name (or gnus-summary-respool-default-method
10005                                   (car (gnus-find-method-for-group
10006                                         gnus-newsgroup-name)))))
10007                 (method
10008                  (gnus-completing-read
10009                   "Backend to use when respooling"
10010                   methods t nil 'gnus-mail-method-history methname))
10011                 ms)
10012            (cond
10013             ((zerop (length (setq ms (gnus-servers-using-backend
10014                                       (intern method)))))
10015              (list (intern method) ""))
10016             ((= 1 (length ms))
10017              (car ms))
10018             (t
10019              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10020                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10021                            ms-alist))))))))
10022   (unless method
10023     (error "No method given for respooling"))
10024   (if (assoc (symbol-name
10025               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10026              (gnus-methods-using 'respool))
10027       (gnus-summary-move-article n nil method)
10028     (gnus-summary-copy-article n nil method)))
10029
10030 (defun gnus-summary-import-article (file &optional edit)
10031   "Import an arbitrary file into a mail newsgroup."
10032   (interactive "fImport file: \nP")
10033   (let ((group gnus-newsgroup-name)
10034         (now (current-time))
10035         atts lines group-art)
10036     (unless (gnus-check-backend-function 'request-accept-article group)
10037       (error "%s does not support article importing" group))
10038     (or (file-readable-p file)
10039         (not (file-regular-p file))
10040         (error "Can't read %s" file))
10041     (with-current-buffer (gnus-get-buffer-create " *import file*")
10042       (erase-buffer)
10043       (nnheader-insert-file-contents file)
10044       (goto-char (point-min))
10045       (if (nnheader-article-p)
10046           (save-restriction
10047             (goto-char (point-min))
10048             (search-forward "\n\n" nil t)
10049             (narrow-to-region (point-min) (1- (point)))
10050             (goto-char (point-min))
10051             (unless (re-search-forward "^date:" nil t)
10052               (goto-char (point-max))
10053               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10054        ;; This doesn't look like an article, so we fudge some headers.
10055         (setq atts (file-attributes file)
10056               lines (count-lines (point-min) (point-max)))
10057         (insert "From: " (read-string "From: ") "\n"
10058                 "Subject: " (read-string "Subject: ") "\n"
10059                 "Date: " (message-make-date (nth 5 atts)) "\n"
10060                 "Message-ID: " (message-make-message-id) "\n"
10061                 "Lines: " (int-to-string lines) "\n"
10062                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10063       (setq group-art (gnus-request-accept-article group nil t))
10064       (kill-buffer (current-buffer)))
10065     (setq gnus-newsgroup-active (gnus-activate-group group))
10066     (forward-line 1)
10067     (gnus-summary-goto-article (cdr group-art) nil t)
10068     (when edit
10069       (gnus-summary-edit-article))))
10070
10071 (defun gnus-summary-create-article ()
10072   "Create an article in a mail newsgroup."
10073   (interactive)
10074   (let ((group gnus-newsgroup-name)
10075         (now (current-time))
10076         group-art)
10077     (unless (gnus-check-backend-function 'request-accept-article group)
10078       (error "%s does not support article importing" group))
10079     (with-current-buffer (gnus-get-buffer-create " *import file*")
10080       (erase-buffer)
10081       (goto-char (point-min))
10082       ;; This doesn't look like an article, so we fudge some headers.
10083       (insert "From: " (read-string "From: ") "\n"
10084               "Subject: " (read-string "Subject: ") "\n"
10085               "Date: " (message-make-date now) "\n"
10086               "Message-ID: " (message-make-message-id) "\n")
10087       (setq group-art (gnus-request-accept-article group nil t))
10088       (kill-buffer (current-buffer)))
10089     (setq gnus-newsgroup-active (gnus-activate-group group))
10090     (forward-line 1)
10091     (gnus-summary-goto-article (cdr group-art) nil t)
10092     (gnus-summary-edit-article)))
10093
10094 (defun gnus-summary-article-posted-p ()
10095   "Say whether the current (mail) article is available from news as well.
10096 This will be the case if the article has both been mailed and posted."
10097   (interactive)
10098   (let ((id (mail-header-references (gnus-summary-article-header)))
10099         (gnus-override-method (car (gnus-refer-article-methods))))
10100     (if (gnus-request-head id "")
10101         (gnus-message 2 "The current message was found on %s"
10102                       gnus-override-method)
10103       (gnus-message 2 "The current message couldn't be found on %s"
10104                     gnus-override-method)
10105       nil)))
10106
10107 (defun gnus-summary-expire-articles (&optional now)
10108   "Expire all articles that are marked as expirable in the current group."
10109   (interactive)
10110   (when (and (not gnus-group-is-exiting-without-update-p)
10111              (gnus-check-backend-function
10112               'request-expire-articles gnus-newsgroup-name))
10113     ;; This backend supports expiry.
10114     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10115            (expirable (if total
10116                           (progn
10117                             ;; We need to update the info for
10118                             ;; this group for `gnus-list-of-read-articles'
10119                             ;; to give us the right answer.
10120                             (gnus-run-hooks 'gnus-exit-group-hook)
10121                             (gnus-summary-update-info)
10122                             (gnus-list-of-read-articles gnus-newsgroup-name))
10123                         (setq gnus-newsgroup-expirable
10124                               (sort gnus-newsgroup-expirable '<))))
10125            (expiry-wait (if now 'immediate
10126                           (gnus-group-find-parameter
10127                            gnus-newsgroup-name 'expiry-wait)))
10128            (nnmail-expiry-target
10129             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10130                 nnmail-expiry-target))
10131            es)
10132       (when expirable
10133         ;; There are expirable articles in this group, so we run them
10134         ;; through the expiry process.
10135         (gnus-message 6 "Expiring articles...")
10136         (unless (gnus-check-group gnus-newsgroup-name)
10137           (error "Can't open server for %s" gnus-newsgroup-name))
10138         ;; The list of articles that weren't expired is returned.
10139         (save-excursion
10140           (if expiry-wait
10141               (let ((nnmail-expiry-wait-function nil)
10142                     (nnmail-expiry-wait expiry-wait))
10143                 (setq es (gnus-request-expire-articles
10144                           expirable gnus-newsgroup-name)))
10145             (setq es (gnus-request-expire-articles
10146                       expirable gnus-newsgroup-name)))
10147           (unless total
10148             (setq gnus-newsgroup-expirable es))
10149           ;; We go through the old list of expirable, and mark all
10150           ;; really expired articles as nonexistent.
10151           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10152             (let ((gnus-use-cache nil))
10153               (dolist (article expirable)
10154                 (when (and (not (memq article es))
10155                            (gnus-data-find article))
10156                   (gnus-summary-mark-article article gnus-canceled-mark)
10157                   (run-hook-with-args 'gnus-summary-article-expire-hook
10158                                       'delete
10159                                       (gnus-data-header
10160                                        (assoc article (gnus-data-list nil)))
10161                                       gnus-newsgroup-name
10162                                       nil
10163                                       nil))))))
10164         (gnus-message 6 "Expiring articles...done")))))
10165
10166 (defun gnus-summary-expire-articles-now ()
10167   "Expunge all expirable articles in the current group.
10168 This means that *all* articles that are marked as expirable will be
10169 deleted forever, right now."
10170   (interactive)
10171   (or gnus-expert-user
10172       (gnus-yes-or-no-p
10173        "Are you really, really sure you want to delete all expirable messages? ")
10174       (error "Phew!"))
10175   (gnus-summary-expire-articles t))
10176
10177 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10178 (defun gnus-summary-delete-article (&optional n)
10179   "Delete the N next (mail) articles.
10180 This command actually deletes articles.  This is not a marking
10181 command.  The article will disappear forever from your life, never to
10182 return.
10183
10184 If N is negative, delete backwards.
10185 If N is nil and articles have been marked with the process mark,
10186 delete these instead.
10187
10188 If `gnus-novice-user' is non-nil you will be asked for
10189 confirmation before the articles are deleted."
10190   (interactive "P")
10191   (unless (gnus-check-backend-function 'request-expire-articles
10192                                        gnus-newsgroup-name)
10193     (error "The current newsgroup does not support article deletion"))
10194   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10195     (error "Couldn't open server"))
10196   ;; Compute the list of articles to delete.
10197   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10198         (nnmail-expiry-target 'delete)
10199         not-deleted)
10200     (if (and gnus-novice-user
10201              (not (gnus-yes-or-no-p
10202                    (format "Do you really want to delete %s forever? "
10203                            (if (> (length articles) 1)
10204                                (format "these %s articles" (length articles))
10205                              "this article")))))
10206         ()
10207       ;; Delete the articles.
10208       (setq not-deleted (gnus-request-expire-articles
10209                          articles gnus-newsgroup-name 'force))
10210       (save-excursion
10211         (while articles
10212           (gnus-summary-remove-process-mark (car articles))
10213           ;; The backend might not have been able to delete the article
10214           ;; after all.
10215           (unless (memq (car articles) not-deleted)
10216             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10217             (let* ((article (car articles))
10218                    (ghead  (gnus-data-header
10219                             (assoc article (gnus-data-list nil)))))
10220               (run-hook-with-args 'gnus-summary-article-delete-hook
10221                                   'delete ghead gnus-newsgroup-name nil
10222                                   nil)))
10223           (setq articles (cdr articles))))
10224       (when not-deleted
10225         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10226     (gnus-summary-position-point)
10227     (gnus-set-mode-line 'summary)
10228     not-deleted))
10229
10230 (defun gnus-summary-edit-article (&optional arg)
10231   "Edit the current article.
10232 This will have permanent effect only in mail groups.
10233 If ARG is nil, edit the decoded articles.
10234 If ARG is 1, edit the raw articles.
10235 If ARG is 2, edit the raw articles even in read-only groups.
10236 If ARG is 3, edit the articles with the current handles.
10237 Otherwise, allow editing of articles even in read-only
10238 groups."
10239   (interactive "P")
10240   (let (force raw current-handles)
10241     (cond
10242      ((null arg))
10243      ((eq arg 1)
10244       (setq raw t))
10245      ((eq arg 2)
10246       (setq raw t
10247             force t))
10248      ((eq arg 3)
10249       (setq current-handles
10250             (and (gnus-buffer-live-p gnus-article-buffer)
10251                  (with-current-buffer gnus-article-buffer
10252                    (prog1
10253                        gnus-article-mime-handles
10254                      (setq gnus-article-mime-handles nil))))))
10255      (t
10256       (setq force t)))
10257     (when (and raw (not force)
10258                (member gnus-newsgroup-name '("nndraft:delayed"
10259                                              "nndraft:drafts"
10260                                              "nndraft:queue")))
10261       (error "Can't edit the raw article in group %s"
10262              gnus-newsgroup-name))
10263     (with-current-buffer gnus-summary-buffer
10264       (let ((mail-parse-charset gnus-newsgroup-charset)
10265             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10266         (gnus-set-global-variables)
10267         (when (and (not force)
10268                    (gnus-group-read-only-p))
10269           (error "The current newsgroup does not support article editing"))
10270         (gnus-summary-show-article t)
10271         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10272           (with-current-buffer gnus-article-buffer
10273             (mm-enable-multibyte)))
10274         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10275             (setq raw t))
10276         (gnus-article-edit-article
10277          (if raw 'ignore
10278            `(lambda ()
10279               (let ((mbl mml-buffer-list))
10280                 (setq mml-buffer-list nil)
10281                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10282                   (mime-to-mml ,'current-handles))
10283                 (let ((mbl1 mml-buffer-list))
10284                   (setq mml-buffer-list mbl)
10285                   (set (make-local-variable 'mml-buffer-list) mbl1))
10286                 (gnus-make-local-hook 'kill-buffer-hook)
10287                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10288          `(lambda (no-highlight)
10289             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10290                   (message-options message-options)
10291                   (message-options-set-recipient)
10292                   (mail-parse-ignored-charsets
10293                    ',gnus-newsgroup-ignored-charsets)
10294                   (rfc2047-header-encoding-alist
10295                    ',(let ((charset (gnus-group-name-charset
10296                                      (gnus-find-method-for-group
10297                                       gnus-newsgroup-name)
10298                                      gnus-newsgroup-name)))
10299                        (append (list (cons "Newsgroups" charset)
10300                                      (cons "Followup-To" charset)
10301                                      (cons "Xref" charset))
10302                                rfc2047-header-encoding-alist))))
10303               ,(if (not raw) '(progn
10304                                 (mml-to-mime)
10305                                 (mml-destroy-buffers)
10306                                 (remove-hook 'kill-buffer-hook
10307                                              'mml-destroy-buffers t)
10308                                 (kill-local-variable 'mml-buffer-list)))
10309               (gnus-summary-edit-article-done
10310                ,(or (mail-header-references gnus-current-headers) "")
10311                ,(gnus-group-read-only-p)
10312                ,gnus-summary-buffer no-highlight))))))))
10313
10314 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10315
10316 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10317                                                  no-highlight)
10318   "Make edits to the current article permanent."
10319   (interactive)
10320   (save-excursion
10321     ;; The buffer restriction contains the entire article if it exists.
10322     (when (article-goto-body)
10323       (let ((lines (count-lines (point) (point-max)))
10324             (length (- (point-max) (point)))
10325             (case-fold-search t)
10326             (body (copy-marker (point))))
10327         (goto-char (point-min))
10328         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10329           (delete-region (match-beginning 1) (match-end 1))
10330           (insert (number-to-string length)))
10331         (goto-char (point-min))
10332         (when (re-search-forward
10333                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10334           (delete-region (match-beginning 1) (match-end 1))
10335           (insert (number-to-string length)))
10336         (goto-char (point-min))
10337         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10338           (delete-region (match-beginning 1) (match-end 1))
10339           (insert (number-to-string lines))))))
10340   ;; Replace the article.
10341   (let ((buf (current-buffer))
10342         (article (cdr gnus-article-current))
10343         replace-result)
10344     (with-temp-buffer
10345       (insert-buffer-substring buf)
10346       (if (and (not read-only)
10347                (not (setq replace-result
10348                           (gnus-request-replace-article
10349                            article (car gnus-article-current)
10350                            (current-buffer) t))))
10351           (error "Couldn't replace article")
10352         ;; If we got a number back, then that's the new article number
10353         ;; for this article.  Otherwise, the article number didn't change.
10354         (when (numberp replace-result)
10355           (with-current-buffer gnus-summary-buffer
10356             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10357             (gnus-summary-limit gnus-newsgroup-limit)
10358             (setq article replace-result)
10359             (gnus-summary-goto-subject article t)))
10360         ;; Update the summary buffer.
10361         (if (and references
10362                  (equal (message-tokenize-header references " ")
10363                         (message-tokenize-header
10364                          (or (message-fetch-field "references") "") " ")))
10365             ;; We only have to update this line.
10366             (save-excursion
10367               (save-restriction
10368                 (message-narrow-to-head)
10369                 (let ((head (buffer-substring-no-properties
10370                              (point-min) (point-max)))
10371                       header)
10372                   (with-temp-buffer
10373                     (insert (format "211 %d Article retrieved.\n" article))
10374                     (insert head)
10375                     (insert ".\n")
10376                     (let ((nntp-server-buffer (current-buffer)))
10377                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10378                     (with-current-buffer gnus-summary-buffer
10379                       (gnus-data-set-header (gnus-data-find article) header)
10380                       (gnus-summary-update-article-line article header)
10381                       (if (gnus-summary-goto-subject article nil t)
10382                           (gnus-summary-update-secondary-mark article)))))))
10383           ;; Update threads.
10384           (set-buffer (or buffer gnus-summary-buffer))
10385           (gnus-summary-update-article article)
10386           (if (gnus-summary-goto-subject article nil t)
10387               (gnus-summary-update-secondary-mark article)))
10388         ;; Prettify the article buffer again.
10389         (unless no-highlight
10390           (with-current-buffer gnus-article-buffer
10391             ;;!!! Fix this -- article should be rehighlighted.
10392             ;;(gnus-run-hooks 'gnus-article-display-hook)
10393             (set-buffer gnus-original-article-buffer)
10394             (gnus-request-article
10395              article (car gnus-article-current) (current-buffer))))
10396         ;; Prettify the summary buffer line.
10397         (when (gnus-visual-p 'summary-highlight 'highlight)
10398           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10399
10400 (defun gnus-summary-edit-wash (key)
10401   "Perform editing command KEY in the article buffer."
10402   (interactive
10403    (list
10404     (progn
10405       (message "%s" (concat (this-command-keys) "- "))
10406       (read-char))))
10407   (message "")
10408   (gnus-summary-edit-article)
10409   (execute-kbd-macro (concat (this-command-keys) key))
10410   (gnus-article-edit-done))
10411
10412 ;;; Respooling
10413
10414 (defun gnus-summary-respool-query (&optional silent trace)
10415   "Query where the respool algorithm would put this article."
10416   (interactive)
10417   (let (gnus-mark-article-hook)
10418     (gnus-summary-select-article)
10419     (with-current-buffer gnus-original-article-buffer
10420       (let ((groups (nnmail-article-group 'identity trace)))
10421         (unless silent
10422           (if groups
10423               (message "This message would go to %s"
10424                        (mapconcat 'car groups ", "))
10425             (message "This message would go to no groups"))
10426           groups)))))
10427
10428 (defun gnus-summary-respool-trace ()
10429   "Trace where the respool algorithm would put this article.
10430 Display a buffer showing all fancy splitting patterns which matched."
10431   (interactive)
10432   (gnus-summary-respool-query nil t))
10433
10434 ;; Summary marking commands.
10435
10436 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10437   "Mark articles which has the same subject as read, and then select the next.
10438 If UNMARK is positive, remove any kind of mark.
10439 If UNMARK is negative, tick articles."
10440   (interactive "P")
10441   (when unmark
10442     (setq unmark (prefix-numeric-value unmark)))
10443   (let ((count
10444          (gnus-summary-mark-same-subject
10445           (gnus-summary-article-subject) unmark)))
10446     ;; Select next unread article.  If auto-select-same mode, should
10447     ;; select the first unread article.
10448     (gnus-summary-next-article t (and gnus-auto-select-same
10449                                       (gnus-summary-article-subject)))
10450     (gnus-message 7 "%d article%s marked as %s"
10451                   count (if (= count 1) " is" "s are")
10452                   (if unmark "unread" "read"))))
10453
10454 (defun gnus-summary-kill-same-subject (&optional unmark)
10455   "Mark articles which has the same subject as read.
10456 If UNMARK is positive, remove any kind of mark.
10457 If UNMARK is negative, tick articles."
10458   (interactive "P")
10459   (when unmark
10460     (setq unmark (prefix-numeric-value unmark)))
10461   (let ((count
10462          (gnus-summary-mark-same-subject
10463           (gnus-summary-article-subject) unmark)))
10464     ;; If marked as read, go to next unread subject.
10465     (when (null unmark)
10466       ;; Go to next unread subject.
10467       (gnus-summary-next-subject 1 t))
10468     (gnus-message 7 "%d articles are marked as %s"
10469                   count (if unmark "unread" "read"))))
10470
10471 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10472   "Mark articles with same SUBJECT as read, and return marked number.
10473 If optional argument UNMARK is positive, remove any kinds of marks.
10474 If optional argument UNMARK is negative, mark articles as unread instead."
10475   (let ((count 1))
10476     (save-excursion
10477       (cond
10478        ((null unmark)                   ; Mark as read.
10479         (while (and
10480                 (progn
10481                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10482                   (gnus-summary-show-thread) t)
10483                 (gnus-summary-find-subject subject))
10484           (setq count (1+ count))))
10485        ((> unmark 0)                    ; Tick.
10486         (while (and
10487                 (progn
10488                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10489                   (gnus-summary-show-thread) t)
10490                 (gnus-summary-find-subject subject))
10491           (setq count (1+ count))))
10492        (t                               ; Mark as unread.
10493         (while (and
10494                 (progn
10495                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10496                   (gnus-summary-show-thread) t)
10497                 (gnus-summary-find-subject subject))
10498           (setq count (1+ count)))))
10499       (gnus-set-mode-line 'summary)
10500       ;; Return the number of marked articles.
10501       count)))
10502
10503 (defun gnus-summary-mark-as-processable (n &optional unmark)
10504   "Set the process mark on the next N articles.
10505 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10506 the process mark instead.  The difference between N and the actual
10507 number of articles marked is returned."
10508   (interactive "P")
10509   (if (and (null n) (gnus-region-active-p))
10510       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10511     (setq n (prefix-numeric-value n))
10512     (let ((backward (< n 0))
10513           (n (abs n)))
10514       (while (and
10515               (> n 0)
10516               (if unmark
10517                   (gnus-summary-remove-process-mark
10518                    (gnus-summary-article-number))
10519                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10520               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10521         (setq n (1- n)))
10522       (when (/= 0 n)
10523         (gnus-message 7 "No more articles"))
10524       (gnus-summary-recenter)
10525       (gnus-summary-position-point)
10526       n)))
10527
10528 (defun gnus-summary-unmark-as-processable (n)
10529   "Remove the process mark from the next N articles.
10530 If N is negative, unmark backward instead.  The difference between N and
10531 the actual number of articles unmarked is returned."
10532   (interactive "P")
10533   (gnus-summary-mark-as-processable n t))
10534
10535 (defun gnus-summary-unmark-all-processable ()
10536   "Remove the process mark from all articles."
10537   (interactive)
10538   (save-excursion
10539     (while gnus-newsgroup-processable
10540       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10541   (gnus-summary-position-point))
10542
10543 (defun gnus-summary-add-mark (article type)
10544   "Mark ARTICLE with a mark of TYPE."
10545   (let ((vtype (car (assq type gnus-article-mark-lists)))
10546         var)
10547     (if (not vtype)
10548         (error "No such mark type: %s" type)
10549       (setq var (intern (format "gnus-newsgroup-%s" type)))
10550       (set var (cons article (symbol-value var)))
10551       (if (memq type '(processable cached replied forwarded recent saved))
10552           (gnus-summary-update-secondary-mark article)
10553         ;;; !!! This is bogus.  We should find out what primary
10554         ;;; !!! mark we want to set.
10555         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10556
10557 (defun gnus-summary-mark-as-expirable (n)
10558   "Mark N articles forward as expirable.
10559 If N is negative, mark backward instead.  The difference between N and
10560 the actual number of articles marked is returned."
10561   (interactive "p")
10562   (gnus-summary-mark-forward n gnus-expirable-mark))
10563
10564 (defun gnus-summary-mark-as-spam (n)
10565   "Mark N articles forward as spam.
10566 If N is negative, mark backward instead.  The difference between N and
10567 the actual number of articles marked is returned."
10568   (interactive "p")
10569   (gnus-summary-mark-forward n gnus-spam-mark))
10570
10571 (defun gnus-summary-mark-article-as-replied (article)
10572   "Mark ARTICLE as replied to and update the summary line.
10573 ARTICLE can also be a list of articles."
10574   (interactive (list (gnus-summary-article-number)))
10575   (let ((articles (if (listp article) article (list article))))
10576     (dolist (article articles)
10577       (unless (numberp article)
10578         (error "%s is not a number" article))
10579       (push article gnus-newsgroup-replied)
10580       (let ((inhibit-read-only t))
10581         (when (gnus-summary-goto-subject article nil t)
10582           (gnus-summary-update-secondary-mark article))))))
10583
10584 (defun gnus-summary-mark-article-as-forwarded (article)
10585   "Mark ARTICLE as forwarded and update the summary line.
10586 ARTICLE can also be a list of articles."
10587   (let ((articles (if (listp article) article (list article))))
10588     (dolist (article articles)
10589       (push article gnus-newsgroup-forwarded)
10590       (let ((inhibit-read-only t))
10591         (when (gnus-summary-goto-subject article nil t)
10592           (gnus-summary-update-secondary-mark article))))))
10593
10594 (defun gnus-summary-set-bookmark (article)
10595   "Set a bookmark in current article."
10596   (interactive (list (gnus-summary-article-number)))
10597   (when (or (not (get-buffer gnus-article-buffer))
10598             (not gnus-current-article)
10599             (not gnus-article-current)
10600             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10601     (error "No current article selected"))
10602   ;; Remove old bookmark, if one exists.
10603   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10604   ;; Set the new bookmark, which is on the form
10605   ;; (article-number . line-number-in-body).
10606   (push
10607    (cons article
10608          (with-current-buffer gnus-article-buffer
10609            (count-lines
10610             (min (point)
10611                  (save-excursion
10612                    (article-goto-body)
10613                    (point)))
10614             (point))))
10615    gnus-newsgroup-bookmarks)
10616   (gnus-message 6 "A bookmark has been added to the current article."))
10617
10618 (defun gnus-summary-remove-bookmark (article)
10619   "Remove the bookmark from the current article."
10620   (interactive (list (gnus-summary-article-number)))
10621   ;; Remove old bookmark, if one exists.
10622   (if (not (assq article gnus-newsgroup-bookmarks))
10623       (gnus-message 6 "No bookmark in current article.")
10624     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10625     (gnus-message 6 "Removed bookmark.")))
10626
10627 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10628 (defun gnus-summary-mark-as-dormant (n)
10629   "Mark N articles forward as dormant.
10630 If N is negative, mark backward instead.  The difference between N and
10631 the actual number of articles marked is returned."
10632   (interactive "p")
10633   (gnus-summary-mark-forward n gnus-dormant-mark))
10634
10635 (defun gnus-summary-set-process-mark (article)
10636   "Set the process mark on ARTICLE and update the summary line."
10637   (setq gnus-newsgroup-processable
10638         (cons article
10639               (delq article gnus-newsgroup-processable)))
10640   (when (gnus-summary-goto-subject article)
10641     (gnus-summary-show-thread)
10642     (gnus-summary-goto-subject article)
10643     (gnus-summary-update-secondary-mark article)))
10644
10645 (defun gnus-summary-remove-process-mark (&rest articles)
10646   "Remove the process mark from ARTICLES and update the summary line."
10647   (dolist (article articles)
10648     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10649     (when (gnus-summary-goto-subject article)
10650       (gnus-summary-show-thread)
10651       (gnus-summary-goto-subject article)
10652       (gnus-summary-update-secondary-mark article)))
10653   t)
10654
10655 (defun gnus-summary-set-saved-mark (article)
10656   "Set the process mark on ARTICLE and update the summary line."
10657   (push article gnus-newsgroup-saved)
10658   (when (gnus-summary-goto-subject article)
10659     (gnus-summary-update-secondary-mark article)))
10660
10661 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10662   "Mark N articles as read forwards.
10663 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10664 The difference between N and the actual number of articles marked is
10665 returned.
10666 If NO-EXPIRE, auto-expiry will be inhibited."
10667   (interactive "p")
10668   (gnus-summary-show-thread)
10669   (let ((backward (< n 0))
10670         (gnus-summary-goto-unread
10671          (and gnus-summary-goto-unread
10672               (not (eq gnus-summary-goto-unread 'never))
10673               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10674                                     gnus-ticked-mark gnus-dormant-mark)))))
10675         (n (abs n))
10676         (mark (or mark gnus-del-mark)))
10677     (while (and (> n 0)
10678                 (gnus-summary-mark-article nil mark no-expire)
10679                 (zerop (gnus-summary-next-subject
10680                         (if backward -1 1)
10681                         (and gnus-summary-goto-unread
10682                              (not (eq gnus-summary-goto-unread 'never)))
10683                         t)))
10684       (setq n (1- n)))
10685     (when (/= 0 n)
10686       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10687     (gnus-summary-recenter)
10688     (gnus-summary-position-point)
10689     (gnus-set-mode-line 'summary)
10690     n))
10691
10692 (defun gnus-summary-mark-article-as-read (mark)
10693   "Mark the current article quickly as read with MARK."
10694   (let ((article (gnus-summary-article-number)))
10695     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10696     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10697     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10698     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10699     (push (cons article mark) gnus-newsgroup-reads)
10700     ;; Possibly remove from cache, if that is used.
10701     (when gnus-use-cache
10702       (gnus-cache-enter-remove-article article))
10703     ;; Allow the backend to change the mark.
10704     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10705     ;; Check for auto-expiry.
10706     (when (and gnus-newsgroup-auto-expire
10707                (memq mark gnus-auto-expirable-marks))
10708       (setq mark gnus-expirable-mark)
10709       ;; Let the backend know about the mark change.
10710       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10711       (push article gnus-newsgroup-expirable))
10712     ;; Set the mark in the buffer.
10713     (gnus-summary-update-mark mark 'unread)
10714     t))
10715
10716 (defun gnus-summary-mark-article-as-unread (mark)
10717   "Mark the current article quickly as unread with MARK."
10718   (let* ((article (gnus-summary-article-number))
10719          (old-mark (gnus-summary-article-mark article)))
10720     ;; Allow the backend to change the mark.
10721     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10722     (if (eq mark old-mark)
10723         t
10724       (if (<= article 0)
10725           (progn
10726             (gnus-error 1 "Can't mark negative article numbers")
10727             nil)
10728         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10729         (setq gnus-newsgroup-spam-marked
10730               (delq article gnus-newsgroup-spam-marked))
10731         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10732         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10733         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10734         (cond ((= mark gnus-ticked-mark)
10735                (setq gnus-newsgroup-marked
10736                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10737                                               article)))
10738               ((= mark gnus-spam-mark)
10739                (setq gnus-newsgroup-spam-marked
10740                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10741                                               article)))
10742               ((= mark gnus-dormant-mark)
10743                (setq gnus-newsgroup-dormant
10744                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10745                                               article)))
10746               (t
10747                (setq gnus-newsgroup-unreads
10748                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10749                                               article))))
10750         (gnus-alist-pull article gnus-newsgroup-reads)
10751
10752         ;; See whether the article is to be put in the cache.
10753         (and gnus-use-cache
10754              (vectorp (gnus-summary-article-header article))
10755              (save-excursion
10756                (gnus-cache-possibly-enter-article
10757                 gnus-newsgroup-name article
10758                 (= mark gnus-ticked-mark)
10759                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10760
10761         ;; Fix the mark.
10762         (gnus-summary-update-mark mark 'unread)
10763         t))))
10764
10765 (defun gnus-summary-mark-article (&optional article mark no-expire)
10766   "Mark ARTICLE with MARK.  MARK can be any character.
10767 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10768 `??' (dormant) and `?E' (expirable).
10769 If MARK is nil, then the default character `?r' is used.
10770 If ARTICLE is nil, then the article on the current line will be
10771 marked.
10772 If NO-EXPIRE, auto-expiry will be inhibited."
10773   ;; The mark might be a string.
10774   (when (stringp mark)
10775     (setq mark (aref mark 0)))
10776   ;; If no mark is given, then we check auto-expiring.
10777   (when (null mark)
10778     (setq mark gnus-del-mark))
10779   (when (and (not no-expire)
10780              gnus-newsgroup-auto-expire
10781              (memq mark gnus-auto-expirable-marks))
10782     (setq mark gnus-expirable-mark))
10783   (let ((article (or article (gnus-summary-article-number)))
10784         (old-mark (gnus-summary-article-mark article)))
10785     ;; Allow the backend to change the mark.
10786     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10787     (if (eq mark old-mark)
10788         t
10789       (unless article
10790         (error "No article on current line"))
10791       (if (not (if (or (= mark gnus-unread-mark)
10792                        (= mark gnus-ticked-mark)
10793                        (= mark gnus-spam-mark)
10794                        (= mark gnus-dormant-mark))
10795                    (gnus-mark-article-as-unread article mark)
10796                  (gnus-mark-article-as-read article mark)))
10797           t
10798         ;; See whether the article is to be put in the cache.
10799         (and gnus-use-cache
10800              (not (= mark gnus-canceled-mark))
10801              (vectorp (gnus-summary-article-header article))
10802              (save-excursion
10803                (gnus-cache-possibly-enter-article
10804                 gnus-newsgroup-name article
10805                 (= mark gnus-ticked-mark)
10806                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10807
10808         (when (gnus-summary-goto-subject article nil t)
10809           (let ((inhibit-read-only t))
10810             (gnus-summary-show-thread)
10811             ;; Fix the mark.
10812             (gnus-summary-update-mark mark 'unread)
10813             t))))))
10814
10815 (defun gnus-summary-update-secondary-mark (article)
10816   "Update the secondary (read, process, cache) mark."
10817   (gnus-summary-update-mark
10818    (cond ((memq article gnus-newsgroup-processable)
10819           gnus-process-mark)
10820          ((memq article gnus-newsgroup-cached)
10821           gnus-cached-mark)
10822          ((memq article gnus-newsgroup-replied)
10823           gnus-replied-mark)
10824          ((memq article gnus-newsgroup-forwarded)
10825           gnus-forwarded-mark)
10826          ((memq article gnus-newsgroup-saved)
10827           gnus-saved-mark)
10828          ((memq article gnus-newsgroup-recent)
10829           gnus-recent-mark)
10830          ((memq article gnus-newsgroup-unseen)
10831           gnus-unseen-mark)
10832          (t gnus-no-mark))
10833    'replied)
10834   (when (gnus-visual-p 'summary-highlight 'highlight)
10835     (gnus-summary-highlight-line)
10836     (gnus-run-hooks 'gnus-summary-update-hook))
10837   t)
10838
10839 (defun gnus-summary-update-download-mark (article)
10840   "Update the download mark."
10841   (gnus-summary-update-mark
10842    (cond ((memq article gnus-newsgroup-undownloaded)
10843           gnus-undownloaded-mark)
10844          (gnus-newsgroup-agentized
10845           gnus-downloaded-mark)
10846          (t
10847           gnus-no-mark))
10848    'download)
10849   (gnus-summary-update-line t)
10850   t)
10851
10852 (defun gnus-summary-update-mark (mark type)
10853   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10854         (inhibit-read-only t))
10855     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10856     (when forward
10857       (when (looking-at "\r")
10858         (incf forward))
10859       (when (<= (+ forward (point)) (point-max))
10860         ;; Go to the right position on the line.
10861         (goto-char (+ forward (point)))
10862         ;; Replace the old mark with the new mark.
10863         (let ((to-insert
10864                (mm-subst-char-in-string
10865                 (char-after) mark
10866                 (buffer-substring (point) (1+ (point))))))
10867           (delete-region (point) (1+ (point)))
10868           (insert to-insert))
10869         ;; Optionally update the marks by some user rule.
10870         (when (eq type 'unread)
10871           (gnus-data-set-mark
10872            (gnus-data-find (gnus-summary-article-number)) mark)
10873           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10874
10875 (defun gnus-mark-article-as-read (article &optional mark)
10876   "Enter ARTICLE in the pertinent lists and remove it from others."
10877   ;; Make the article expirable.
10878   (let ((mark (or mark gnus-del-mark)))
10879     (setq gnus-newsgroup-expirable
10880           (if (= mark gnus-expirable-mark)
10881               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10882             (delq article gnus-newsgroup-expirable)))
10883     ;; Remove from unread and marked lists.
10884     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10885     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10886     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10887     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10888     (push (cons article mark) gnus-newsgroup-reads)
10889     ;; Possibly remove from cache, if that is used.
10890     (when gnus-use-cache
10891       (gnus-cache-enter-remove-article article))
10892     t))
10893
10894 (defun gnus-mark-article-as-unread (article &optional mark)
10895   "Enter ARTICLE in the pertinent lists and remove it from others."
10896   (let ((mark (or mark gnus-ticked-mark)))
10897     (if (<= article 0)
10898         (progn
10899           (gnus-error 1 "Can't mark negative article numbers")
10900           nil)
10901       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10902             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10903             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10904             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10905             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10906
10907       ;; Unsuppress duplicates?
10908       (when gnus-suppress-duplicates
10909         (gnus-dup-unsuppress-article article))
10910
10911       (cond ((= mark gnus-ticked-mark)
10912              (setq gnus-newsgroup-marked
10913                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10914             ((= mark gnus-spam-mark)
10915              (setq gnus-newsgroup-spam-marked
10916                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10917                                             article)))
10918             ((= mark gnus-dormant-mark)
10919              (setq gnus-newsgroup-dormant
10920                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10921             (t
10922              (setq gnus-newsgroup-unreads
10923                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10924       (gnus-alist-pull article gnus-newsgroup-reads)
10925       t)))
10926
10927 (defun gnus-summary-tick-article-forward (n)
10928   "Tick N articles forwards.
10929 If N is negative, tick backwards instead.
10930 The difference between N and the number of articles ticked is returned."
10931   (interactive "p")
10932   (gnus-summary-mark-forward n gnus-ticked-mark))
10933
10934 (defun gnus-summary-tick-article-backward (n)
10935   "Tick N articles backwards.
10936 The difference between N and the number of articles ticked is returned."
10937   (interactive "p")
10938   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10939
10940 (defun gnus-summary-tick-article (&optional article clear-mark)
10941   "Mark current article as unread.
10942 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10943 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10944   (interactive)
10945   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10946                                        gnus-ticked-mark)))
10947
10948 (defun gnus-summary-mark-as-read-forward (n)
10949   "Mark N articles as read forwards.
10950 If N is negative, mark backwards instead.
10951 The difference between N and the actual number of articles marked is
10952 returned."
10953   (interactive "p")
10954   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10955
10956 (defun gnus-summary-mark-as-read-backward (n)
10957   "Mark the N articles as read backwards.
10958 The difference between N and the actual number of articles marked is
10959 returned."
10960   (interactive "p")
10961   (gnus-summary-mark-forward
10962    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10963
10964 (defun gnus-summary-mark-as-read (&optional article mark)
10965   "Mark current article as read.
10966 ARTICLE specifies the article to be marked as read.
10967 MARK specifies a string to be inserted at the beginning of the line."
10968   (gnus-summary-mark-article article mark))
10969
10970 (defun gnus-summary-clear-mark-forward (n)
10971   "Clear marks from N articles forward.
10972 If N is negative, clear backward instead.
10973 The difference between N and the number of marks cleared is returned."
10974   (interactive "p")
10975   (gnus-summary-mark-forward n gnus-unread-mark))
10976
10977 (defun gnus-summary-clear-mark-backward (n)
10978   "Clear marks from N articles backward.
10979 The difference between N and the number of marks cleared is returned."
10980   (interactive "p")
10981   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10982
10983 (defun gnus-summary-mark-unread-as-read ()
10984   "Intended to be used by `gnus-mark-article-hook'."
10985   (when (memq gnus-current-article gnus-newsgroup-unreads)
10986     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10987
10988 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10989   "Intended to be used by `gnus-mark-article-hook'."
10990   (let ((mark (gnus-summary-article-mark)))
10991     (when (or (gnus-unread-mark-p mark)
10992               (gnus-read-mark-p mark))
10993       (gnus-summary-mark-article gnus-current-article
10994                                  (or new-mark gnus-read-mark)))))
10995
10996 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10997   "Intended to be used by `gnus-mark-article-hook'."
10998   (let ((mark (gnus-summary-article-mark)))
10999     (when (or (gnus-unread-mark-p mark)
11000               (gnus-read-mark-p mark))
11001       (gnus-summary-mark-article (gnus-summary-article-number)
11002                                  (or new-mark gnus-read-mark)))))
11003
11004 (defun gnus-summary-mark-unread-as-ticked ()
11005   "Intended to be used by `gnus-mark-article-hook'."
11006   (when (memq gnus-current-article gnus-newsgroup-unreads)
11007     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11008
11009 (defun gnus-summary-mark-region-as-read (point mark all)
11010   "Mark all unread articles between point and mark as read.
11011 If given a prefix, mark all articles between point and mark as read,
11012 even ticked and dormant ones."
11013   (interactive "r\nP")
11014   (save-excursion
11015     (let (article)
11016       (goto-char point)
11017       (beginning-of-line)
11018       (while (and
11019               (< (point) mark)
11020               (progn
11021                 (when (or all
11022                           (memq (setq article (gnus-summary-article-number))
11023                                 gnus-newsgroup-unreads))
11024                   (gnus-summary-mark-article article gnus-del-mark))
11025                 t)
11026               (gnus-summary-find-next))))))
11027
11028 (defun gnus-summary-mark-below (score mark)
11029   "Mark articles with score less than SCORE with MARK."
11030   (interactive "P\ncMark: ")
11031   (setq score (if score
11032                   (prefix-numeric-value score)
11033                 (or gnus-summary-default-score 0)))
11034   (with-current-buffer gnus-summary-buffer
11035     (goto-char (point-min))
11036     (while
11037         (progn
11038           (and (< (gnus-summary-article-score) score)
11039                (gnus-summary-mark-article nil mark))
11040           (gnus-summary-find-next)))))
11041
11042 (defun gnus-summary-kill-below (&optional score)
11043   "Mark articles with score below SCORE as read."
11044   (interactive "P")
11045   (gnus-summary-mark-below score gnus-killed-mark))
11046
11047 (defun gnus-summary-clear-above (&optional score)
11048   "Clear all marks from articles with score above SCORE."
11049   (interactive "P")
11050   (gnus-summary-mark-above score gnus-unread-mark))
11051
11052 (defun gnus-summary-tick-above (&optional score)
11053   "Tick all articles with score above SCORE."
11054   (interactive "P")
11055   (gnus-summary-mark-above score gnus-ticked-mark))
11056
11057 (defun gnus-summary-mark-above (score mark)
11058   "Mark articles with score over SCORE with MARK."
11059   (interactive "P\ncMark: ")
11060   (setq score (if score
11061                   (prefix-numeric-value score)
11062                 (or gnus-summary-default-score 0)))
11063   (with-current-buffer gnus-summary-buffer
11064     (goto-char (point-min))
11065     (while (and (progn
11066                   (when (> (gnus-summary-article-score) score)
11067                     (gnus-summary-mark-article nil mark))
11068                   t)
11069                 (gnus-summary-find-next)))))
11070
11071 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11072 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11073 (defun gnus-summary-limit-include-expunged (&optional no-error)
11074   "Display all the hidden articles that were expunged for low scores."
11075   (interactive)
11076   (let ((inhibit-read-only t))
11077     (let ((scored gnus-newsgroup-scored)
11078           headers h)
11079       (while scored
11080         (unless (gnus-summary-article-header (caar scored))
11081           (and (setq h (gnus-number-to-header (caar scored)))
11082                (< (cdar scored) gnus-summary-expunge-below)
11083                (push h headers)))
11084         (setq scored (cdr scored)))
11085       (if (not headers)
11086           (when (not no-error)
11087             (error "No expunged articles hidden"))
11088         (goto-char (point-min))
11089         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11090         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11091         (dolist (x headers)
11092           (push (mail-header-number x) gnus-newsgroup-limit))
11093         (gnus-summary-prepare-unthreaded (nreverse headers))
11094         (goto-char (point-min))
11095         (gnus-summary-position-point)
11096         t))))
11097
11098 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11099   "Mark all unread articles in this newsgroup as read.
11100 If prefix argument ALL is non-nil, ticked and dormant articles will
11101 also be marked as read.
11102 If QUIETLY is non-nil, no questions will be asked.
11103
11104 If TO-HERE is non-nil, it should be a point in the buffer.  All
11105 articles before (after, if REVERSE is set) this point will be marked
11106 as read.
11107
11108 Note that this function will only catch up the unread article
11109 in the current summary buffer limitation.
11110
11111 The number of articles marked as read is returned."
11112   (interactive "P")
11113   (prog1
11114       (save-excursion
11115         (when (or quietly
11116                   (not gnus-interactive-catchup) ;Without confirmation?
11117                   gnus-expert-user
11118                   (gnus-y-or-n-p
11119                    (if all
11120                        "Mark absolutely all articles as read? "
11121                      "Mark all unread articles as read? ")))
11122           (if (and not-mark
11123                    (not gnus-newsgroup-adaptive)
11124                    (not gnus-newsgroup-auto-expire)
11125                    (not gnus-suppress-duplicates)
11126                    (or (not gnus-use-cache)
11127                        (eq gnus-use-cache 'passive)))
11128               (progn
11129                 (when all
11130                   (setq gnus-newsgroup-marked nil
11131                         gnus-newsgroup-spam-marked nil
11132                         gnus-newsgroup-dormant nil))
11133                 (setq gnus-newsgroup-unreads
11134                       (gnus-sorted-nunion
11135                        (gnus-sorted-intersection gnus-newsgroup-unreads
11136                                                  gnus-newsgroup-downloadable)
11137                        (gnus-sorted-difference gnus-newsgroup-unfetched
11138                                                gnus-newsgroup-cached))))
11139             ;; We actually mark all articles as canceled, which we
11140             ;; have to do when using auto-expiry or adaptive scoring.
11141             (gnus-summary-show-all-threads)
11142             (if (and to-here reverse)
11143                 (progn
11144                   (goto-char to-here)
11145                   (gnus-summary-mark-current-read-and-unread-as-read
11146                    gnus-catchup-mark)
11147                   (while (gnus-summary-find-next (not all))
11148                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11149               (when (gnus-summary-first-subject (not all))
11150                 (while (and
11151                         (if to-here (< (point) to-here) t)
11152                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11153                         (gnus-summary-find-next (not all))))))
11154             (gnus-set-mode-line 'summary))
11155           t))
11156     (gnus-summary-position-point)))
11157
11158 (defun gnus-summary-catchup-to-here (&optional all)
11159   "Mark all unticked articles before the current one as read.
11160 If ALL is non-nil, also mark ticked and dormant articles as read."
11161   (interactive "P")
11162   (save-excursion
11163     (gnus-save-hidden-threads
11164       (let ((beg (point)))
11165         ;; We check that there are unread articles.
11166         (when (or all (gnus-summary-find-prev))
11167           (gnus-summary-catchup all t beg)))))
11168   (gnus-summary-position-point))
11169
11170 (defun gnus-summary-catchup-from-here (&optional all)
11171   "Mark all unticked articles after (and including) the current one as read.
11172 If ALL is non-nil, also mark ticked and dormant articles as read."
11173   (interactive "P")
11174   (save-excursion
11175     (gnus-save-hidden-threads
11176       (let ((beg (point)))
11177         ;; We check that there are unread articles.
11178         (when (or all (gnus-summary-find-next))
11179           (gnus-summary-catchup all t beg nil t)))))
11180   (gnus-summary-position-point))
11181
11182 (defun gnus-summary-catchup-all (&optional quietly)
11183   "Mark all articles in this newsgroup as read.
11184 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11185 instead, which marks only unread articles as read."
11186   (interactive "P")
11187   (gnus-summary-catchup t quietly))
11188
11189 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11190   "Mark all unread articles in this group as read, then exit.
11191 If prefix argument ALL is non-nil, all articles are marked as read.
11192 If QUIETLY is non-nil, no questions will be asked."
11193   (interactive "P")
11194   (when (gnus-summary-catchup all quietly nil 'fast)
11195     ;; Select next newsgroup or exit.
11196     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11197              (eq gnus-auto-select-next 'quietly))
11198         (gnus-summary-next-group nil)
11199       (gnus-summary-exit))))
11200
11201 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11202   "Mark all articles in this newsgroup as read, and then exit.
11203 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11204 instead, which marks only unread articles as read."
11205   (interactive "P")
11206   (gnus-summary-catchup-and-exit t quietly))
11207
11208 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11209   "Mark all articles in this group as read and select the next group.
11210 If given a prefix, mark all articles, unread as well as ticked, as
11211 read."
11212   (interactive "P")
11213   (save-excursion
11214     (gnus-summary-catchup all))
11215   (gnus-summary-next-group))
11216
11217 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11218   "Mark all articles in this group as read and select the previous group.
11219 If given a prefix, mark all articles, unread as well as ticked, as
11220 read."
11221   (interactive "P")
11222   (save-excursion
11223     (gnus-summary-catchup all))
11224   (gnus-summary-next-group nil nil t))
11225
11226 ;;;
11227 ;;; with article
11228 ;;;
11229
11230 (defmacro gnus-with-article (article &rest forms)
11231   "Select ARTICLE and perform FORMS in the original article buffer.
11232 Then replace the article with the result."
11233   `(progn
11234      ;; We don't want the article to be marked as read.
11235      (let (gnus-mark-article-hook)
11236        (gnus-summary-select-article t t nil ,article))
11237      (set-buffer gnus-original-article-buffer)
11238      ,@forms
11239      (if (not (gnus-check-backend-function
11240                'request-replace-article (car gnus-article-current)))
11241          (gnus-message 5 "Read-only group; not replacing")
11242        (unless (gnus-request-replace-article
11243                 ,article (car gnus-article-current)
11244                 (current-buffer) t)
11245          (error "Couldn't replace article")))
11246      ;; The cache and backlog have to be flushed somewhat.
11247      (when gnus-keep-backlog
11248        (gnus-backlog-remove-article
11249         (car gnus-article-current) (cdr gnus-article-current)))
11250      (when gnus-use-cache
11251        (gnus-cache-update-article
11252         (car gnus-article-current) (cdr gnus-article-current)))))
11253
11254 (put 'gnus-with-article 'lisp-indent-function 1)
11255 (put 'gnus-with-article 'edebug-form-spec '(form body))
11256
11257 ;; Thread-based commands.
11258
11259 (defun gnus-summary-articles-in-thread (&optional article)
11260   "Return a list of all articles in the current thread.
11261 If ARTICLE is non-nil, return all articles in the thread that starts
11262 with that article."
11263   (let* ((article (or article (gnus-summary-article-number)))
11264          (data (gnus-data-find-list article))
11265          (top-level (gnus-data-level (car data)))
11266          (top-subject
11267           (cond ((null gnus-thread-operation-ignore-subject)
11268                  (gnus-simplify-subject-re
11269                   (mail-header-subject (gnus-data-header (car data)))))
11270                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11271                  (gnus-simplify-subject-fuzzy
11272                   (mail-header-subject (gnus-data-header (car data)))))
11273                 (t nil)))
11274          (end-point (save-excursion
11275                       (goto-char (gnus-data-pos (car data)))
11276                       (if (gnus-summary-go-to-next-thread)
11277                           (point) (point-max))))
11278          articles)
11279     (while (and data
11280                 (< (gnus-data-pos (car data)) end-point))
11281       (when (or (not top-subject)
11282                 (string= top-subject
11283                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11284                              (gnus-simplify-subject-fuzzy
11285                               (mail-header-subject
11286                                (gnus-data-header (car data))))
11287                            (gnus-simplify-subject-re
11288                             (mail-header-subject
11289                              (gnus-data-header (car data)))))))
11290         (push (gnus-data-number (car data)) articles))
11291       (unless (and (setq data (cdr data))
11292                    (> (gnus-data-level (car data)) top-level))
11293         (setq data nil)))
11294     ;; Return the list of articles.
11295     (nreverse articles)))
11296
11297 (defun gnus-summary-rethread-current ()
11298   "Rethread the thread the current article is part of."
11299   (interactive)
11300   (let* ((gnus-show-threads t)
11301          (article (gnus-summary-article-number))
11302          (id (mail-header-id (gnus-summary-article-header)))
11303          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11304     (unless id
11305       (error "No article on the current line"))
11306     (gnus-rebuild-thread id)
11307     (gnus-summary-goto-subject article)))
11308
11309 (defun gnus-summary-reparent-thread ()
11310   "Make the current article child of the marked (or previous) article.
11311
11312 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11313 is non-nil or the Subject: of both articles are the same."
11314   (interactive)
11315   (unless (not (gnus-group-read-only-p))
11316     (error "The current newsgroup does not support article editing"))
11317   (unless (<= (length gnus-newsgroup-processable) 1)
11318     (error "No more than one article may be marked"))
11319   (let ((child (gnus-summary-article-number))
11320         ;; First grab the marked article, otherwise one line up.
11321         (parent (if (not (null gnus-newsgroup-processable))
11322                     (car gnus-newsgroup-processable)
11323                   (save-excursion
11324                     (if (eq (forward-line -1) 0)
11325                         (gnus-summary-article-number)
11326                       (error "Beginning of summary buffer"))))))
11327     (gnus-summary-reparent-children parent (list child))))
11328
11329 (defun gnus-summary-reparent-children (parent children)
11330   "Make PARENT the parent of CHILDREN.
11331 When called interactively, PARENT is the current article and CHILDREN
11332 are the process-marked articles."
11333   (interactive
11334    (list (gnus-summary-article-number)
11335          (gnus-summary-work-articles nil)))
11336   (dolist (child children)
11337     (save-window-excursion
11338       (let ((gnus-article-buffer " *reparent*"))
11339         (unless (not (eq parent child))
11340           (error "An article may not be self-referential"))
11341         (let ((message-id (mail-header-id
11342                            (gnus-summary-article-header parent))))
11343           (unless (and message-id (not (equal message-id "")))
11344             (error "No message-id in desired parent"))
11345           (gnus-with-article child
11346             (save-restriction
11347               (goto-char (point-min))
11348               (message-narrow-to-head)
11349               (if (re-search-forward "^References: " nil t)
11350                   (progn
11351                     (re-search-forward "^[^ \t]" nil t)
11352                     (forward-line -1)
11353                     (end-of-line)
11354                     (insert " " message-id))
11355                 (insert "References: " message-id "\n"))))
11356           (set-buffer gnus-summary-buffer)
11357           (gnus-summary-unmark-all-processable)
11358           (gnus-summary-update-article child)
11359           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11360             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11361           (gnus-summary-rethread-current)
11362           (gnus-message 3 "Article %d is now the child of article %d"
11363                         child parent))))))
11364
11365 (defun gnus-summary-toggle-threads (&optional arg)
11366   "Toggle showing conversation threads.
11367 If ARG is positive number, turn showing conversation threads on."
11368   (interactive "P")
11369   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11370     (setq gnus-show-threads
11371           (if (null arg) (not gnus-show-threads)
11372             (> (prefix-numeric-value arg) 0)))
11373     (gnus-summary-prepare)
11374     (gnus-summary-goto-subject current)
11375     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11376     (gnus-summary-position-point)))
11377
11378 (eval-and-compile
11379   (if (fboundp 'remove-overlays)
11380       (defalias 'gnus-remove-overlays 'remove-overlays)
11381     (defun gnus-remove-overlays (beg end name val)
11382       "Clear BEG and END of overlays whose property NAME has value VAL.
11383 For compatibility with XEmacs."
11384       (dolist (ov (gnus-overlays-in beg end))
11385         (when (eq (gnus-overlay-get ov name) val)
11386           (gnus-delete-overlay ov))))))
11387
11388 (defun gnus-summary-show-all-threads ()
11389   "Show all threads."
11390   (interactive)
11391   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11392   (gnus-summary-position-point))
11393
11394 (defsubst gnus-summary--inv (p)
11395   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11396
11397 (defun gnus-summary-show-thread ()
11398   "Show thread subtrees.
11399 Returns nil if no thread was there to be shown."
11400   (interactive)
11401   (let* ((orig (point))
11402          (end (point-at-eol))
11403          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11404          ;; Leave point at bol
11405          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11406          (eoi (when end
11407                 (if (fboundp 'next-single-char-property-change)
11408                     (or (next-single-char-property-change end 'invisible)
11409                         (point-max))
11410                   (while (progn
11411                            (end-of-line 2)
11412                            (and (not (eobp))
11413                                 (eq (get-char-property (point) 'invisible)
11414                                     'gnus-sum))))
11415                   (point)))))
11416     (when eoi
11417       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11418       (goto-char orig)
11419       (gnus-summary-position-point)
11420       eoi)))
11421
11422 (defun gnus-summary-maybe-hide-threads ()
11423   "If requested, hide the threads that should be hidden."
11424   (when (and gnus-show-threads
11425              gnus-thread-hide-subtree)
11426     (gnus-summary-hide-all-threads
11427      (if (or (consp gnus-thread-hide-subtree)
11428              (functionp gnus-thread-hide-subtree))
11429          (gnus-make-predicate gnus-thread-hide-subtree)
11430        nil))))
11431
11432 ;;; Hiding predicates.
11433
11434 (defun gnus-article-unread-p (header)
11435   (memq (mail-header-number header) gnus-newsgroup-unreads))
11436
11437 (defun gnus-article-unseen-p (header)
11438   (memq (mail-header-number header) gnus-newsgroup-unseen))
11439
11440 (defun gnus-map-articles (predicate articles)
11441   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11442   (apply 'gnus-or (mapcar predicate
11443                           (mapcar (lambda (number)
11444                                     (gnus-summary-article-header number))
11445                                   articles))))
11446
11447 (defun gnus-summary-hide-all-threads (&optional predicate)
11448   "Hide all thread subtrees.
11449 If PREDICATE is supplied, threads that satisfy this predicate
11450 will not be hidden."
11451   (interactive)
11452   (save-excursion
11453     (goto-char (point-min))
11454     (let ((end nil))
11455       (while (not end)
11456         (when (or (not predicate)
11457                   (gnus-map-articles
11458                    predicate (gnus-summary-article-children)))
11459             (gnus-summary-hide-thread))
11460         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11461   (gnus-summary-position-point))
11462
11463 (defun gnus-summary-hide-thread ()
11464   "Hide thread subtrees.
11465 If PREDICATE is supplied, threads that satisfy this predicate
11466 will not be hidden.
11467 Returns nil if no threads were there to be hidden."
11468   (interactive)
11469   (let ((start (point))
11470         (starteol (line-end-position))
11471         (article (gnus-summary-article-number)))
11472     (goto-char start)
11473     ;; Go forward until either the buffer ends or the subthread ends.
11474     (when (and (not (eobp))
11475                (or (zerop (gnus-summary-next-thread 1 t))
11476                    (goto-char (point-max))))
11477       (if (and (> (point) start)
11478                ;; FIXME: this should actually search for a non-invisible \n.
11479                (search-backward "\n" start t))
11480           (progn
11481             (when (> (point) starteol)
11482               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11483               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11484                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11485                 (gnus-overlay-put ol 'evaporate t)))
11486             (gnus-summary-goto-subject article))
11487         (goto-char start)
11488         nil))))
11489
11490 (defun gnus-summary-go-to-next-thread (&optional previous)
11491   "Go to the same level (or less) next thread.
11492 If PREVIOUS is non-nil, go to previous thread instead.
11493 Return the article number moved to, or nil if moving was impossible."
11494   (let ((level (gnus-summary-thread-level))
11495         (way (if previous -1 1))
11496         (beg (point)))
11497     (forward-line way)
11498     (while (and (not (eobp))
11499                 (< level (gnus-summary-thread-level)))
11500       (forward-line way))
11501     (if (eobp)
11502         (progn
11503           (goto-char beg)
11504           nil)
11505       (setq beg (point))
11506       (prog1
11507           (gnus-summary-article-number)
11508         (goto-char beg)))))
11509
11510 (defun gnus-summary-next-thread (n &optional silent)
11511   "Go to the same level next N'th thread.
11512 If N is negative, search backward instead.
11513 Returns the difference between N and the number of skips actually
11514 done.
11515
11516 If SILENT, don't output messages."
11517   (interactive "p")
11518   (let ((backward (< n 0))
11519         (n (abs n)))
11520     (while (and (> n 0)
11521                 (gnus-summary-go-to-next-thread backward))
11522       (decf n))
11523     (unless silent
11524       (gnus-summary-position-point))
11525     (when (and (not silent) (/= 0 n))
11526       (gnus-message 7 "No more threads"))
11527     n))
11528
11529 (defun gnus-summary-prev-thread (n)
11530   "Go to the same level previous N'th thread.
11531 Returns the difference between N and the number of skips actually
11532 done."
11533   (interactive "p")
11534   (gnus-summary-next-thread (- n)))
11535
11536 (defun gnus-summary-go-down-thread ()
11537   "Go down one level in the current thread."
11538   (let ((children (gnus-summary-article-children)))
11539     (when children
11540       (gnus-summary-goto-subject (car children)))))
11541
11542 (defun gnus-summary-go-up-thread ()
11543   "Go up one level in the current thread."
11544   (let ((parent (gnus-summary-article-parent)))
11545     (when parent
11546       (gnus-summary-goto-subject parent))))
11547
11548 (defun gnus-summary-down-thread (n)
11549   "Go down thread N steps.
11550 If N is negative, go up instead.
11551 Returns the difference between N and how many steps down that were
11552 taken."
11553   (interactive "p")
11554   (let ((up (< n 0))
11555         (n (abs n)))
11556     (while (and (> n 0)
11557                 (if up (gnus-summary-go-up-thread)
11558                   (gnus-summary-go-down-thread)))
11559       (setq n (1- n)))
11560     (gnus-summary-position-point)
11561     (when (/= 0 n)
11562       (gnus-message 7 "Can't go further"))
11563     n))
11564
11565 (defun gnus-summary-up-thread (n)
11566   "Go up thread N steps.
11567 If N is negative, go down instead.
11568 Returns the difference between N and how many steps down that were
11569 taken."
11570   (interactive "p")
11571   (gnus-summary-down-thread (- n)))
11572
11573 (defun gnus-summary-top-thread ()
11574   "Go to the top of the thread."
11575   (interactive)
11576   (while (gnus-summary-go-up-thread))
11577   (gnus-summary-article-number))
11578
11579 (defun gnus-summary-expire-thread ()
11580   "Mark articles under current thread as expired."
11581   (interactive)
11582   (gnus-summary-kill-thread 0))
11583
11584 (defun gnus-summary-kill-thread (&optional unmark)
11585   "Mark articles under current thread as read.
11586 If the prefix argument is positive, remove any kinds of marks.
11587 If the prefix argument is zero, mark thread as expired.
11588 If the prefix argument is negative, tick articles instead."
11589   (interactive "P")
11590   (when unmark
11591     (setq unmark (prefix-numeric-value unmark)))
11592   (let ((articles (gnus-summary-articles-in-thread))
11593         (hide (or (null unmark) (= unmark 0))))
11594     (save-excursion
11595       ;; Expand the thread.
11596       (gnus-summary-show-thread)
11597       ;; Mark all the articles.
11598       (while articles
11599         (gnus-summary-goto-subject (car articles))
11600         (cond ((null unmark)
11601                (gnus-summary-mark-article-as-read gnus-killed-mark))
11602               ((> unmark 0)
11603                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11604               ((= unmark 0)
11605                (gnus-summary-mark-article nil gnus-expirable-mark))
11606               (t
11607                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11608         (setq articles (cdr articles))))
11609     ;; Hide killed subtrees when hide is true.
11610     (and hide
11611          gnus-thread-hide-killed
11612          (gnus-summary-hide-thread))
11613     ;; If hide is t, go to next unread subject.
11614     (when hide
11615       ;; Go to next unread subject.
11616       (gnus-summary-next-subject 1 t)))
11617   (gnus-set-mode-line 'summary))
11618
11619 ;; Summary sorting commands
11620
11621 (defun gnus-summary-sort-by-number (&optional reverse)
11622   "Sort the summary buffer by article number.
11623 Argument REVERSE means reverse order."
11624   (interactive "P")
11625   (gnus-summary-sort 'number reverse))
11626
11627 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11628   "Sort the summary buffer by most recent article number.
11629 Argument REVERSE means reverse order."
11630   (interactive "P")
11631   (gnus-summary-sort 'most-recent-number reverse))
11632
11633 (defun gnus-summary-sort-by-random (&optional reverse)
11634   "Randomize the order in the summary buffer.
11635 Argument REVERSE means to randomize in reverse order."
11636   (interactive "P")
11637   (gnus-summary-sort 'random reverse))
11638
11639 (defun gnus-summary-sort-by-author (&optional reverse)
11640   "Sort the summary buffer by author name alphabetically.
11641 If `case-fold-search' is non-nil, case of letters is ignored.
11642 Argument REVERSE means reverse order."
11643   (interactive "P")
11644   (gnus-summary-sort 'author reverse))
11645
11646 (defun gnus-summary-sort-by-recipient (&optional reverse)
11647   "Sort the summary buffer by recipient name alphabetically.
11648 If `case-fold-search' is non-nil, case of letters is ignored.
11649 Argument REVERSE means reverse order."
11650   (interactive "P")
11651   (gnus-summary-sort 'recipient reverse))
11652
11653 (defun gnus-summary-sort-by-subject (&optional reverse)
11654   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11655 If `case-fold-search' is non-nil, case of letters is ignored.
11656 Argument REVERSE means reverse order."
11657   (interactive "P")
11658   (gnus-summary-sort 'subject reverse))
11659
11660 (defun gnus-summary-sort-by-date (&optional reverse)
11661   "Sort the summary buffer by date.
11662 Argument REVERSE means reverse order."
11663   (interactive "P")
11664   (gnus-summary-sort 'date reverse))
11665
11666 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11667   "Sort the summary buffer by most recent date.
11668 Argument REVERSE means reverse order."
11669   (interactive "P")
11670   (gnus-summary-sort 'most-recent-date reverse))
11671
11672 (defun gnus-summary-sort-by-score (&optional reverse)
11673   "Sort the summary buffer by score.
11674 Argument REVERSE means reverse order."
11675   (interactive "P")
11676   (gnus-summary-sort 'score reverse))
11677
11678 (defun gnus-summary-sort-by-lines (&optional reverse)
11679   "Sort the summary buffer by the number of lines.
11680 Argument REVERSE means reverse order."
11681   (interactive "P")
11682   (gnus-summary-sort 'lines reverse))
11683
11684 (defun gnus-summary-sort-by-chars (&optional reverse)
11685   "Sort the summary buffer by article length.
11686 Argument REVERSE means reverse order."
11687   (interactive "P")
11688   (gnus-summary-sort 'chars reverse))
11689
11690 (defun gnus-summary-sort-by-original (&optional reverse)
11691   "Sort the summary buffer using the default sorting method.
11692 Argument REVERSE means reverse order."
11693   (interactive "P")
11694   (let* ((inhibit-read-only t)
11695          (gnus-summary-prepare-hook nil))
11696     ;; We do the sorting by regenerating the threads.
11697     (gnus-summary-prepare)
11698     ;; Hide subthreads if needed.
11699     (gnus-summary-maybe-hide-threads)))
11700
11701 (defun gnus-summary-sort (predicate reverse)
11702   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11703   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11704          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11705          (gnus-thread-sort-functions
11706           (if (not reverse)
11707               thread
11708             `(lambda (t1 t2)
11709                (,thread t2 t1))))
11710          (gnus-sort-gathered-threads-function
11711           gnus-thread-sort-functions)
11712          (gnus-article-sort-functions
11713           (if (not reverse)
11714               article
11715             `(lambda (t1 t2)
11716                (,article t2 t1))))
11717          (inhibit-read-only t)
11718          (gnus-summary-prepare-hook nil))
11719     ;; We do the sorting by regenerating the threads.
11720     (gnus-summary-prepare)
11721     ;; Hide subthreads if needed.
11722     (gnus-summary-maybe-hide-threads)))
11723
11724 ;; Summary saving commands.
11725
11726 (defun gnus-summary-save-article (&optional n not-saved)
11727   "Save the current article using the default saver function.
11728 If N is a positive number, save the N next articles.
11729 If N is a negative number, save the N previous articles.
11730 If N is nil and any articles have been marked with the process mark,
11731 save those articles instead.
11732 The variable `gnus-default-article-saver' specifies the saver function.
11733
11734 If the optional second argument NOT-SAVED is non-nil, articles saved
11735 will not be marked as saved."
11736   (interactive "P")
11737   (require 'gnus-art)
11738   (let* ((articles (gnus-summary-work-articles n))
11739          (save-buffer (save-excursion
11740                         (nnheader-set-temp-buffer " *Gnus Save*")))
11741          (num (length articles))
11742          ;; Whether to save decoded articles or raw articles.
11743          (decode (when gnus-article-save-coding-system
11744                    (get gnus-default-article-saver :decode)))
11745          ;; When saving many articles in a single file, use the other
11746          ;; function to save articles other than the first one.
11747          (saver2 (get gnus-default-article-saver :function))
11748          (gnus-prompt-before-saving (if saver2
11749                                         t
11750                                       gnus-prompt-before-saving))
11751          (gnus-default-article-saver gnus-default-article-saver)
11752          header file)
11753     (dolist (article articles)
11754       (setq header (gnus-summary-article-header article))
11755       (if (not (vectorp header))
11756           ;; This is a pseudo-article.
11757           (if (assq 'name header)
11758               (gnus-copy-file (cdr (assq 'name header)))
11759             (gnus-message 1 "Article %d is unsaveable" article))
11760         ;; This is a real article.
11761         (save-window-excursion
11762           (gnus-summary-select-article decode decode nil article)
11763           (gnus-summary-goto-subject article))
11764         (with-current-buffer save-buffer
11765           (erase-buffer)
11766           (insert-buffer-substring (if decode
11767                                        gnus-article-buffer
11768                                      gnus-original-article-buffer)))
11769         (setq file (gnus-article-save save-buffer file num))
11770         (gnus-summary-remove-process-mark article)
11771         (unless not-saved
11772           (gnus-summary-set-saved-mark article)))
11773       (when saver2
11774         (setq gnus-default-article-saver saver2
11775               saver2 nil)))
11776     (gnus-kill-buffer save-buffer)
11777     (gnus-summary-position-point)
11778     (gnus-set-mode-line 'summary)
11779     n))
11780
11781 (defun gnus-summary-pipe-output (&optional n sym)
11782   "Pipe the current article to a subprocess.
11783 If N is a positive number, pipe the N next articles.
11784 If N is a negative number, pipe the N previous articles.
11785 If N is nil and any articles have been marked with the process mark,
11786 pipe those articles instead.
11787 The default command to which articles are piped is specified by the
11788 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11789 will be prompted for the command.
11790
11791 The properties `:decode' and `:headers' that are put to the function
11792 symbol `gnus-summary-save-in-pipe' control whether this function
11793 decodes articles and what headers to keep (see the doc string for the
11794 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11795 is neither omitted nor the symbol `r', force including all headers
11796 regardless of the `:headers' property.  If it is the symbol `r',
11797 articles that are not decoded and include all headers will be piped
11798 no matter what the properties `:decode' and `:headers' are."
11799   (interactive (gnus-interactive "P\ny"))
11800   (require 'gnus-art)
11801   (let* ((articles (gnus-summary-work-articles n))
11802          (result-buffer "*Shell Command Output*")
11803          (all-headers (not (memq sym '(nil r))))
11804          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11805          (raw (eq sym 'r))
11806          (headers (get 'gnus-summary-save-in-pipe :headers))
11807          command result)
11808     (unless (numberp (car articles))
11809       (error "No article to pipe"))
11810     (setq command (gnus-read-shell-command
11811                    (concat "Shell command on "
11812                            (if (cdr articles)
11813                                (format "these %d articles" (length articles))
11814                              "this article")
11815                            ": ")
11816                    gnus-summary-pipe-output-default-command))
11817     (when (string-equal command "")
11818       (error "A command is required"))
11819     (when all-headers
11820       (put 'gnus-summary-save-in-pipe :headers nil))
11821     (unwind-protect
11822         (while articles
11823           (gnus-summary-goto-subject (pop articles))
11824           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11825           (when (and (get-buffer result-buffer)
11826                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11827             (setq result (concat result (with-current-buffer result-buffer
11828                                           (buffer-string))))))
11829       (put 'gnus-summary-save-in-pipe :headers headers))
11830     (unless (zerop (length result))
11831       (if (with-current-buffer (get-buffer-create result-buffer)
11832             (erase-buffer)
11833             (insert result)
11834             (prog1
11835                 (and (= (count-lines (point-min) (point)) 1)
11836                      (progn
11837                        (end-of-line 0)
11838                        (<= (current-column)
11839                            (window-width (minibuffer-window)))))
11840               (goto-char (point-min))))
11841           (message "%s" (substring result 0 -1))
11842         (message nil)
11843         (gnus-configure-windows 'pipe)))))
11844
11845 (defun gnus-summary-save-article-mail (&optional arg)
11846   "Append the current article to a Unix mail box file.
11847 If N is a positive number, save the N next articles.
11848 If N is a negative number, save the N previous articles.
11849 If N is nil and any articles have been marked with the process mark,
11850 save those articles instead."
11851   (interactive "P")
11852   (require 'gnus-art)
11853   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11854     (gnus-summary-save-article arg)))
11855
11856 (defun gnus-summary-save-article-rmail (&optional arg)
11857   "Append the current article to an rmail file.
11858 If N is a positive number, save the N next articles.
11859 If N is a negative number, save the N previous articles.
11860 If N is nil and any articles have been marked with the process mark,
11861 save those articles instead."
11862   (interactive "P")
11863   (require 'gnus-art)
11864   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11865     (gnus-summary-save-article arg)))
11866
11867 (defun gnus-summary-save-article-file (&optional arg)
11868   "Append the current article to a file.
11869 If N is a positive number, save the N next articles.
11870 If N is a negative number, save the N previous articles.
11871 If N is nil and any articles have been marked with the process mark,
11872 save those articles instead."
11873   (interactive "P")
11874   (require 'gnus-art)
11875   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11876     (gnus-summary-save-article arg)))
11877
11878 (defun gnus-summary-write-article-file (&optional arg)
11879   "Write the current article to a file, deleting the previous file.
11880 If N is a positive number, save the N next articles.
11881 If N is a negative number, save the N previous articles.
11882 If N is nil and any articles have been marked with the process mark,
11883 save those articles instead."
11884   (interactive "P")
11885   (require 'gnus-art)
11886   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11887     (gnus-summary-save-article arg)))
11888
11889 (defun gnus-summary-save-article-body-file (&optional arg)
11890   "Append the current article body to a file.
11891 If N is a positive number, save the N next articles.
11892 If N is a negative number, save the N previous articles.
11893 If N is nil and any articles have been marked with the process mark,
11894 save those articles instead."
11895   (interactive "P")
11896   (require 'gnus-art)
11897   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11898     (gnus-summary-save-article arg)))
11899
11900 (defun gnus-summary-write-article-body-file (&optional arg)
11901   "Write the current article body to a file, deleting the previous file.
11902 If N is a positive number, save the N next articles.
11903 If N is a negative number, save the N previous articles.
11904 If N is nil and any articles have been marked with the process mark,
11905 save those articles instead."
11906   (interactive "P")
11907   (require 'gnus-art)
11908   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11909     (gnus-summary-save-article arg)))
11910
11911 (defun gnus-summary-muttprint (&optional arg)
11912   "Print the current article using Muttprint.
11913 If N is a positive number, save the N next articles.
11914 If N is a negative number, save the N previous articles.
11915 If N is nil and any articles have been marked with the process mark,
11916 save those articles instead."
11917   (interactive "P")
11918   (require 'gnus-art)
11919   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11920     (gnus-summary-save-article arg t)))
11921
11922 (defun gnus-summary-pipe-message (program)
11923   "Pipe the current article through PROGRAM."
11924   (interactive "sProgram: ")
11925   (gnus-summary-select-article)
11926   (let ((mail-header-separator ""))
11927     (gnus-eval-in-buffer-window gnus-article-buffer
11928       (save-restriction
11929         (widen)
11930         (let ((start (window-start))
11931               (inhibit-read-only t))
11932           (message-pipe-buffer-body program)
11933           (set-window-start (get-buffer-window (current-buffer)) start))))))
11934
11935 (defun gnus-get-split-value (methods)
11936   "Return a value based on the split METHODS."
11937   (let (split-name method result match)
11938     (when methods
11939       (with-current-buffer gnus-original-article-buffer
11940         (save-restriction
11941           (nnheader-narrow-to-headers)
11942           (while (and methods (not split-name))
11943             (goto-char (point-min))
11944             (setq method (pop methods))
11945             (setq match (car method))
11946             (when (cond
11947                    ((stringp match)
11948                     ;; Regular expression.
11949                     (ignore-errors
11950                       (re-search-forward match nil t)))
11951                    ((functionp match)
11952                     ;; Function.
11953                     (save-restriction
11954                       (widen)
11955                       (setq result (funcall match gnus-newsgroup-name))))
11956                    ((consp match)
11957                     ;; Form.
11958                     (save-restriction
11959                       (widen)
11960                       (setq result (eval match)))))
11961               (setq split-name (cdr method))
11962               (cond ((stringp result)
11963                      (push (expand-file-name
11964                             result gnus-article-save-directory)
11965                            split-name))
11966                     ((consp result)
11967                      (setq split-name (append result split-name)))))))))
11968     (nreverse split-name)))
11969
11970 (defun gnus-valid-move-group-p (group)
11971   (and (symbolp group)
11972        (boundp group)
11973        (symbol-name group)
11974        (symbol-value group)
11975        (gnus-get-function (gnus-find-method-for-group
11976                            (symbol-name group)) 'request-accept-article t)))
11977
11978 (defun gnus-read-move-group-name (prompt default articles prefix)
11979   "Read a group name."
11980   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11981          (minibuffer-confirm-incomplete nil) ; XEmacs
11982          (prom
11983           (format "%s %s to"
11984                   prompt
11985                   (if (> (length articles) 1)
11986                       (format "these %d articles" (length articles))
11987                     "this article")))
11988          (to-newsgroup
11989           (cond
11990            ((null split-name)
11991             (gnus-group-completing-read
11992              prom
11993              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11994              nil prefix nil default))
11995            ((= 1 (length split-name))
11996             (gnus-group-completing-read
11997              prom
11998              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11999              nil prefix 'gnus-group-history (car split-name)))
12000            (t
12001             (gnus-completing-read
12002              prom (nreverse split-name) nil nil 'gnus-group-history))))
12003          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12004          encoded)
12005     (when to-newsgroup
12006       (if (or (string= to-newsgroup "")
12007               (string= to-newsgroup prefix))
12008           (setq to-newsgroup default))
12009       (unless to-newsgroup
12010         (error "No group name entered"))
12011       (setq encoded (mm-encode-coding-string
12012                      to-newsgroup
12013                      (gnus-group-name-charset to-method to-newsgroup)))
12014       (or (gnus-active encoded)
12015           (gnus-activate-group encoded nil nil to-method)
12016           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12017                                      to-newsgroup))
12018               (or (and (gnus-request-create-group encoded to-method)
12019                        (gnus-activate-group encoded nil nil to-method)
12020                        (gnus-subscribe-group encoded))
12021                   (error "Couldn't create group %s" to-newsgroup)))
12022           (error "No such group: %s" to-newsgroup))
12023       encoded)))
12024
12025 (defvar gnus-summary-save-parts-counter)
12026 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12027
12028 (defun gnus-summary-save-parts (type dir n &optional reverse)
12029   "Save parts matching TYPE to DIR.
12030 If REVERSE, save parts that do not match TYPE."
12031   (interactive
12032    (list (read-string "Save parts of type: "
12033                       (or (car gnus-summary-save-parts-type-history)
12034                           gnus-summary-save-parts-default-mime)
12035                       'gnus-summary-save-parts-type-history)
12036          (setq gnus-summary-save-parts-last-directory
12037                (read-file-name "Save to directory: "
12038                                gnus-summary-save-parts-last-directory
12039                                nil t))
12040          current-prefix-arg))
12041   (gnus-summary-iterate n
12042     (let ((gnus-display-mime-function nil)
12043           gnus-article-prepare-hook
12044           gnus-article-decode-hook
12045           gnus-display-mime-function
12046           gnus-break-pages
12047           (gnus-inhibit-treatment t))
12048       (gnus-summary-select-article))
12049     (with-current-buffer gnus-article-buffer
12050       (let ((handles (or gnus-article-mime-handles
12051                          (mm-dissect-buffer nil gnus-article-loose-mime)
12052                          (and gnus-article-emulate-mime
12053                               (mm-uu-dissect))))
12054             (gnus-summary-save-parts-counter 1))
12055         (when handles
12056           (gnus-summary-save-parts-1 type dir handles reverse)
12057           (unless gnus-article-mime-handles ;; Don't destroy this case.
12058             (mm-destroy-parts handles)))))))
12059
12060 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12061   (if (stringp (car handle))
12062       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12063               (cdr handle))
12064     (when (if reverse
12065               (not (string-match type (mm-handle-media-type handle)))
12066             (string-match type (mm-handle-media-type handle)))
12067       (let ((file (expand-file-name
12068                    (gnus-map-function
12069                     mm-file-name-rewrite-functions
12070                     (file-name-nondirectory
12071                      (or
12072                       (mail-content-type-get
12073                        (mm-handle-disposition handle) 'filename)
12074                       (mail-content-type-get
12075                        (mm-handle-type handle) 'name)
12076                       (format "%s.%d.%d" gnus-newsgroup-name
12077                               (cdr gnus-article-current)
12078                               gnus-summary-save-parts-counter))))
12079                    dir)))
12080         (incf gnus-summary-save-parts-counter)
12081         (unless (file-exists-p file)
12082           (mm-save-part-to-file handle file))))))
12083
12084 ;; Summary extract commands
12085
12086 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12087   (let ((inhibit-read-only t)
12088         (article (gnus-summary-article-number))
12089         after-article b e)
12090     (unless (gnus-summary-goto-subject article)
12091       (error "No such article: %d" article))
12092     (gnus-summary-position-point)
12093     ;; If all commands are to be bunched up on one line, we collect
12094     ;; them here.
12095     (unless gnus-view-pseudos-separately
12096       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12097             files action)
12098         (while ps
12099           (setq action (cdr (assq 'action (car ps))))
12100           (setq files (list (cdr (assq 'name (car ps)))))
12101           (while (and ps (cdr ps)
12102                       (string= (or action "1")
12103                                (or (cdr (assq 'action (cadr ps))) "2")))
12104             (push (cdr (assq 'name (cadr ps))) files)
12105             (setcdr ps (cddr ps)))
12106           (when files
12107             (when (not (string-match "%s" action))
12108               (push " " files))
12109             (push " " files)
12110             (when (assq 'execute (car ps))
12111               (setcdr (assq 'execute (car ps))
12112                       (funcall (if (string-match "%s" action)
12113                                    'format 'concat)
12114                                action
12115                                (mapconcat
12116                                 (lambda (f)
12117                                   (if (equal f " ")
12118                                       f
12119                                     (shell-quote-argument f)))
12120                                 files " ")))))
12121           (setq ps (cdr ps)))))
12122     (if (and gnus-view-pseudos (not not-view))
12123         (while pslist
12124           (when (assq 'execute (car pslist))
12125             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12126                                   (eq gnus-view-pseudos 'not-confirm)))
12127           (setq pslist (cdr pslist)))
12128       (save-excursion
12129         (while pslist
12130           (setq after-article (or (cdr (assq 'article (car pslist)))
12131                                   (gnus-summary-article-number)))
12132           (gnus-summary-goto-subject after-article)
12133           (forward-line 1)
12134           (setq b (point))
12135           (insert "    " (file-name-nondirectory
12136                           (cdr (assq 'name (car pslist))))
12137                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12138           (setq e (point))
12139           (forward-line -1)             ; back to `b'
12140           (gnus-add-text-properties
12141            b (1- e) (list 'gnus-number gnus-reffed-article-number
12142                           gnus-mouse-face-prop gnus-mouse-face))
12143           (gnus-data-enter
12144            after-article gnus-reffed-article-number
12145            gnus-unread-mark b (car pslist) 0 (- e b))
12146           (setq gnus-newsgroup-unreads
12147                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12148                                          gnus-reffed-article-number))
12149           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12150           (setq pslist (cdr pslist)))))))
12151
12152 (defun gnus-pseudos< (p1 p2)
12153   (let ((c1 (cdr (assq 'action p1)))
12154         (c2 (cdr (assq 'action p2))))
12155     (and c1 c2 (string< c1 c2))))
12156
12157 (defun gnus-request-pseudo-article (props)
12158   (cond ((assq 'execute props)
12159          (gnus-execute-command (cdr (assq 'execute props)))))
12160   (let ((gnus-current-article (gnus-summary-article-number)))
12161     (gnus-run-hooks 'gnus-mark-article-hook)))
12162
12163 (defun gnus-execute-command (command &optional automatic)
12164   (save-excursion
12165     (gnus-article-setup-buffer)
12166     (set-buffer gnus-article-buffer)
12167     (setq buffer-read-only nil)
12168     (let ((command (if automatic command
12169                      (read-string "Command: " (cons command 0)))))
12170       (erase-buffer)
12171       (insert "$ " command "\n\n")
12172       (if gnus-view-pseudo-asynchronously
12173           (start-process "gnus-execute" (current-buffer) shell-file-name
12174                          shell-command-switch command)
12175         (call-process shell-file-name nil t nil
12176                       shell-command-switch command)))))
12177
12178 ;; Summary kill commands.
12179
12180 (defun gnus-summary-edit-global-kill (article)
12181   "Edit the \"global\" kill file."
12182   (interactive (list (gnus-summary-article-number)))
12183   (gnus-group-edit-global-kill article))
12184
12185 (defun gnus-summary-edit-local-kill ()
12186   "Edit a local kill file applied to the current newsgroup."
12187   (interactive)
12188   (setq gnus-current-headers (gnus-summary-article-header))
12189   (gnus-group-edit-local-kill
12190    (gnus-summary-article-number) gnus-newsgroup-name))
12191
12192 ;;; Header reading.
12193
12194 (defun gnus-read-header (id &optional header)
12195   "Read the headers of article ID and enter them into the Gnus system."
12196   (let ((group gnus-newsgroup-name)
12197         (gnus-override-method
12198          (or
12199           gnus-override-method
12200           (and (gnus-news-group-p gnus-newsgroup-name)
12201                (car (gnus-refer-article-methods)))))
12202         where)
12203     ;; First we check to see whether the header in question is already
12204     ;; fetched.
12205     (if (stringp id)
12206         ;; This is a Message-ID.
12207         (setq header (or header (gnus-id-to-header id)))
12208       ;; This is an article number.
12209       (setq header (or header (gnus-summary-article-header id))))
12210     (if (and header
12211              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12212         ;; We have found the header.
12213         header
12214       ;; We have to really fetch the header to this article.
12215       (with-current-buffer nntp-server-buffer
12216         (when (setq where (gnus-request-head id group))
12217           (nnheader-fold-continuation-lines)
12218           (goto-char (point-max))
12219           (insert ".\n")
12220           (goto-char (point-min))
12221           (insert "211 ")
12222           (princ (cond
12223                   ((numberp id) id)
12224                   ((cdr where) (cdr where))
12225                   (header (mail-header-number header))
12226                   (t gnus-reffed-article-number))
12227                  (current-buffer))
12228           (insert " Article retrieved.\n"))
12229         (if (or (not where)
12230                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12231             ()                          ; Malformed head.
12232           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12233             (when (and (stringp id)
12234                        (or
12235                         (not (string= (gnus-group-real-name group)
12236                                       (car where)))
12237                         (not (gnus-server-equal gnus-override-method
12238                                                 (gnus-group-method group)))))
12239               ;; If we fetched by Message-ID and the article came from
12240               ;; a different group (or server), we fudge some bogus
12241               ;; article numbers for this article.
12242               (mail-header-set-number header gnus-reffed-article-number))
12243             (with-current-buffer gnus-summary-buffer
12244               (decf gnus-reffed-article-number)
12245               (gnus-remove-header (mail-header-number header))
12246               (push header gnus-newsgroup-headers)
12247               (setq gnus-current-headers header)
12248               (push (mail-header-number header) gnus-newsgroup-limit)))
12249           header)))))
12250
12251 (defun gnus-remove-header (number)
12252   "Remove header NUMBER from `gnus-newsgroup-headers'."
12253   (if (and gnus-newsgroup-headers
12254            (= number (mail-header-number (car gnus-newsgroup-headers))))
12255       (pop gnus-newsgroup-headers)
12256     (let ((headers gnus-newsgroup-headers))
12257       (while (and (cdr headers)
12258                   (not (= number (mail-header-number (cadr headers)))))
12259         (pop headers))
12260       (when (cdr headers)
12261         (setcdr headers (cddr headers))))))
12262
12263 ;;;
12264 ;;; summary highlights
12265 ;;;
12266
12267 (defun gnus-highlight-selected-summary ()
12268   "Highlight selected article in summary buffer."
12269   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12270   (when gnus-summary-selected-face
12271     (save-excursion
12272       (let* ((beg (point-at-bol))
12273              (end (point-at-eol))
12274              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12275              (from (if (get-text-property beg gnus-mouse-face-prop)
12276                        beg
12277                      (or (next-single-property-change
12278                           beg gnus-mouse-face-prop nil end)
12279                          beg)))
12280              (to
12281               (if (= from end)
12282                   (- from 2)
12283                 (or (next-single-property-change
12284                      from gnus-mouse-face-prop nil end)
12285                     end))))
12286         ;; If no mouse-face prop on line we will have to = from = end,
12287         ;; so we highlight the entire line instead.
12288         (when (= (+ to 2) from)
12289           (setq from beg)
12290           (setq to end))
12291         (if gnus-newsgroup-selected-overlay
12292             ;; Move old overlay.
12293             (gnus-move-overlay
12294              gnus-newsgroup-selected-overlay from to (current-buffer))
12295           ;; Create new overlay.
12296           (gnus-overlay-put
12297            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12298            'face gnus-summary-selected-face))))))
12299
12300 (defvar gnus-summary-highlight-line-cached nil)
12301 (defvar gnus-summary-highlight-line-trigger nil)
12302
12303 (defun gnus-summary-highlight-line-0 ()
12304   (if (and (eq gnus-summary-highlight-line-trigger
12305                gnus-summary-highlight)
12306            gnus-summary-highlight-line-cached)
12307       gnus-summary-highlight-line-cached
12308     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12309           gnus-summary-highlight-line-cached
12310           (let* ((cond (list 'cond))
12311                  (c cond)
12312                  (list gnus-summary-highlight))
12313             (while list
12314               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12315                               nil))
12316               (setq c (cdr c)
12317                     list (cdr list)))
12318             (gnus-byte-compile (list 'lambda nil cond))))))
12319
12320 (defun gnus-summary-highlight-line ()
12321   "Highlight current line according to `gnus-summary-highlight'."
12322   (let* ((beg (point-at-bol))
12323          (article (or (gnus-summary-article-number) gnus-current-article))
12324          (score (or (cdr (assq article
12325                                gnus-newsgroup-scored))
12326                     gnus-summary-default-score 0))
12327          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12328          (inhibit-read-only t)
12329          (default gnus-summary-default-score)
12330          (default-high gnus-summary-default-high-score)
12331          (default-low gnus-summary-default-low-score)
12332          (uncached (and gnus-summary-use-undownloaded-faces
12333                         (memq article gnus-newsgroup-undownloaded)
12334                         (not (memq article gnus-newsgroup-cached)))))
12335     (let ((face (funcall (gnus-summary-highlight-line-0))))
12336       (unless (eq face (get-text-property beg 'face))
12337         (gnus-put-text-property-excluding-characters-with-faces
12338          beg (point-at-eol) 'face
12339          (setq face (if (boundp face) (symbol-value face) face)))
12340         (when gnus-summary-highlight-line-function
12341           (funcall gnus-summary-highlight-line-function article face))))))
12342
12343 (defun gnus-update-read-articles (group unread &optional compute)
12344   "Update the list of read articles in GROUP.
12345 UNREAD is a sorted list."
12346   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12347         (info (gnus-get-info group))
12348         (prev 1)
12349         read)
12350     (if (or (not info) (not active))
12351         ;; There is no info on this group if it was, in fact,
12352         ;; killed.  Gnus stores no information on killed groups, so
12353         ;; there's nothing to be done.
12354         ;; One could store the information somewhere temporarily,
12355         ;; perhaps...  Hmmm...
12356         ()
12357       ;; Remove any negative articles numbers.
12358       (while (and unread (< (car unread) 0))
12359         (setq unread (cdr unread)))
12360       ;; Remove any expired article numbers
12361       (while (and unread (< (car unread) (car active)))
12362         (setq unread (cdr unread)))
12363       ;; Compute the ranges of read articles by looking at the list of
12364       ;; unread articles.
12365       (while unread
12366         (when (/= (car unread) prev)
12367           (push (if (= prev (1- (car unread))) prev
12368                   (cons prev (1- (car unread))))
12369                 read))
12370         (setq prev (1+ (car unread)))
12371         (setq unread (cdr unread)))
12372       (when (<= prev (cdr active))
12373         (push (cons prev (cdr active)) read))
12374       (setq read (if (> (length read) 1) (nreverse read) read))
12375       (if compute
12376           read
12377         (save-excursion
12378           (let (setmarkundo)
12379             ;; Propagate the read marks to the backend.
12380             (when (and gnus-propagate-marks
12381                        (gnus-check-backend-function 'request-set-mark group))
12382               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12383                     (add (gnus-remove-from-range read (gnus-info-read info))))
12384                 (when (or add del)
12385                   (unless (gnus-check-group group)
12386                     (error "Can't open server for %s" group))
12387                   (gnus-request-set-mark
12388                    group (delq nil (list (if add (list add 'add '(read)))
12389                                          (if del (list del 'del '(read))))))
12390                   (setq setmarkundo
12391                         `(gnus-request-set-mark
12392                           ,group
12393                           ',(delq nil (list
12394                                        (if del (list del 'add '(read)))
12395                                        (if add (list add 'del '(read))))))))))
12396             (set-buffer gnus-group-buffer)
12397             (gnus-undo-register
12398               `(progn
12399                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12400                  (gnus-info-set-read ',info ',(gnus-info-read info))
12401                  (gnus-get-unread-articles-in-group ',info
12402                                                     (gnus-active ,group))
12403                  (gnus-group-update-group ,group t)
12404                  ,setmarkundo))))
12405         ;; Enter this list into the group info.
12406         (gnus-info-set-read info read)
12407         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12408         (gnus-get-unread-articles-in-group info (gnus-active group))
12409         t))))
12410
12411 (defun gnus-offer-save-summaries ()
12412   "Offer to save all active summary buffers."
12413   (let (buffers)
12414     ;; Go through all buffers and find all summaries.
12415     (dolist (buffer (buffer-list))
12416       (when (and (setq buffer (buffer-name buffer))
12417                  (string-match "Summary" buffer)
12418                  (with-current-buffer buffer
12419                    ;; We check that this is, indeed, a summary buffer.
12420                    (and (eq major-mode 'gnus-summary-mode)
12421                         ;; Also make sure this isn't bogus.
12422                         gnus-newsgroup-prepared
12423                         ;; Also make sure that this isn't a
12424                         ;; dead summary buffer.
12425                         (not gnus-dead-summary-mode))))
12426         (push buffer buffers)))
12427     ;; Go through all these summary buffers and offer to save them.
12428     (when buffers
12429       (save-excursion
12430         (map-y-or-n-p
12431          "Update summary buffer %s? "
12432          (lambda (buf)
12433            (switch-to-buffer buf)
12434            (gnus-summary-exit))
12435          buffers)))))
12436
12437 (defun gnus-summary-setup-default-charset ()
12438   "Setup newsgroup default charset."
12439   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12440       (setq gnus-newsgroup-charset nil)
12441     (let* ((ignored-charsets
12442             (or gnus-newsgroup-ephemeral-ignored-charsets
12443                 (append
12444                  (and gnus-newsgroup-name
12445                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12446                  gnus-newsgroup-ignored-charsets))))
12447       (setq gnus-newsgroup-charset
12448             (or gnus-newsgroup-ephemeral-charset
12449                 (and gnus-newsgroup-name
12450                      (gnus-parameter-charset gnus-newsgroup-name))
12451                 gnus-default-charset))
12452       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12453            ignored-charsets))))
12454
12455 ;;;
12456 ;;; Mime Commands
12457 ;;;
12458
12459 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12460   "Display the current article buffer fully MIME-buttonized.
12461 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12462 treated as multipart/mixed."
12463   (interactive "P")
12464   (require 'gnus-art)
12465   (let ((gnus-unbuttonized-mime-types nil)
12466         (gnus-mime-display-multipart-as-mixed show-all-parts))
12467     (gnus-summary-show-article)))
12468
12469 (defun gnus-summary-repair-multipart (article)
12470   "Add a Content-Type header to a multipart article without one."
12471   (interactive (list (gnus-summary-article-number)))
12472   (gnus-with-article article
12473     (message-narrow-to-head)
12474     (message-remove-header "Mime-Version")
12475     (goto-char (point-max))
12476     (insert "Mime-Version: 1.0\n")
12477     (widen)
12478     (when (search-forward "\n--" nil t)
12479       (let ((separator (buffer-substring (point) (point-at-eol))))
12480         (message-narrow-to-head)
12481         (message-remove-header "Content-Type")
12482         (goto-char (point-max))
12483         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12484                         separator))
12485         (widen))))
12486   (let (gnus-mark-article-hook)
12487     (gnus-summary-select-article t t nil article)))
12488
12489 (defun gnus-summary-toggle-display-buttonized ()
12490   "Toggle the buttonizing of the article buffer."
12491   (interactive)
12492   (require 'gnus-art)
12493   (if (setq gnus-inhibit-mime-unbuttonizing
12494             (not gnus-inhibit-mime-unbuttonizing))
12495       (let ((gnus-unbuttonized-mime-types nil))
12496         (gnus-summary-show-article))
12497     (gnus-summary-show-article)))
12498
12499 ;;;
12500 ;;; Generic summary marking commands
12501 ;;;
12502
12503 (defvar gnus-summary-marking-alist
12504   '((read gnus-del-mark "d")
12505     (unread gnus-unread-mark "u")
12506     (ticked gnus-ticked-mark "!")
12507     (dormant gnus-dormant-mark "?")
12508     (expirable gnus-expirable-mark "e"))
12509   "An alist of names/marks/keystrokes.")
12510
12511 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12512 (defvar gnus-summary-mark-map)
12513
12514 (defun gnus-summary-make-all-marking-commands ()
12515   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12516   (dolist (elem gnus-summary-marking-alist)
12517     (apply 'gnus-summary-make-marking-command elem)))
12518
12519 (defun gnus-summary-make-marking-command (name mark keystroke)
12520   (let ((map (make-sparse-keymap)))
12521     (define-key gnus-summary-generic-mark-map keystroke map)
12522     (dolist (lway `((next "next" next nil "n")
12523                     (next-unread "next unread" next t "N")
12524                     (prev "previous" prev nil "p")
12525                     (prev-unread "previous unread" prev t "P")
12526                     (nomove "" nil nil ,keystroke)))
12527       (let ((func (gnus-summary-make-marking-command-1
12528                    mark (car lway) lway name)))
12529         (setq func (eval func))
12530         (define-key map (nth 4 lway) func)))))
12531
12532 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12533   `(defun ,(intern
12534             (format "gnus-summary-put-mark-as-%s%s"
12535                     name (if (eq way 'nomove)
12536                              ""
12537                            (concat "-" (symbol-name way)))))
12538      (n)
12539      ,(format
12540        "Mark the current article as %s%s.
12541 If N, the prefix, then repeat N times.
12542 If N is negative, move in reverse order.
12543 The difference between N and the actual number of articles marked is
12544 returned."
12545        name (cadr lway))
12546      (interactive "p")
12547      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12548
12549 (defun gnus-summary-generic-mark (n mark move unread)
12550   "Mark N articles with MARK."
12551   (unless (eq major-mode 'gnus-summary-mode)
12552     (error "This command can only be used in the summary buffer"))
12553   (gnus-summary-show-thread)
12554   (let ((nummove
12555          (cond
12556           ((eq move 'next) 1)
12557           ((eq move 'prev) -1)
12558           (t 0))))
12559     (if (zerop nummove)
12560         (setq n 1)
12561       (when (< n 0)
12562         (setq n (abs n)
12563               nummove (* -1 nummove))))
12564     (while (and (> n 0)
12565                 (gnus-summary-mark-article nil mark)
12566                 (zerop (gnus-summary-next-subject nummove unread t)))
12567       (setq n (1- n)))
12568     (when (/= 0 n)
12569       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12570     (gnus-summary-recenter)
12571     (gnus-summary-position-point)
12572     (gnus-set-mode-line 'summary)
12573     n))
12574
12575 (defun gnus-summary-insert-articles (articles)
12576   (when (setq articles
12577               (gnus-sorted-difference articles
12578                                       (mapcar (lambda (h)
12579                                                 (mail-header-number h))
12580                                               gnus-newsgroup-headers)))
12581     (setq gnus-newsgroup-headers
12582           (gnus-merge 'list
12583                       gnus-newsgroup-headers
12584                       (gnus-fetch-headers articles)
12585                       'gnus-article-sort-by-number))
12586     ;; Suppress duplicates?
12587     (when gnus-suppress-duplicates
12588       (gnus-dup-suppress-articles))
12589
12590     (if (and gnus-fetch-old-headers
12591              (eq gnus-headers-retrieved-by 'nov))
12592         ;; We might want to build some more threads first.
12593         (if (eq gnus-fetch-old-headers 'invisible)
12594             (gnus-build-all-threads)
12595           (gnus-build-old-threads))
12596       ;; Mark the inserted articles that are unread as unread.
12597       (setq gnus-newsgroup-unreads
12598             (gnus-sorted-nunion
12599              gnus-newsgroup-unreads
12600              (gnus-sorted-nintersection
12601               (gnus-list-of-unread-articles gnus-newsgroup-name)
12602               articles)))
12603       ;; Mark the inserted articles as selected so that the information
12604       ;; of the marks having been changed by a user may be updated when
12605       ;; exiting this group.  See `gnus-summary-update-info'.
12606       (dolist (art articles)
12607         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12608     ;; Let the Gnus agent mark articles as read.
12609     (when gnus-agent
12610       (gnus-agent-get-undownloaded-list))
12611     ;; Remove list identifiers from subject
12612     (when gnus-list-identifiers
12613       (gnus-summary-remove-list-identifiers))
12614     ;; First and last article in this newsgroup.
12615     (when gnus-newsgroup-headers
12616       (setq gnus-newsgroup-begin
12617             (mail-header-number (car gnus-newsgroup-headers))
12618             gnus-newsgroup-end
12619             (mail-header-number
12620              (gnus-last-element gnus-newsgroup-headers))))
12621     (when gnus-use-scoring
12622       (gnus-possibly-score-headers))))
12623
12624 (defun gnus-summary-insert-old-articles (&optional all)
12625   "Insert all old articles in this group.
12626 If ALL is non-nil, already read articles become readable.
12627 If ALL is a number, fetch this number of articles."
12628   (interactive "P")
12629   (prog1
12630       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12631             older len)
12632         (setq older
12633               ;; Some nntp servers lie about their active range.  When
12634               ;; this happens, the active range can be in the millions.
12635               ;; Use a compressed range to avoid creating a huge list.
12636               (gnus-range-difference (list gnus-newsgroup-active) old))
12637         (setq len (gnus-range-length older))
12638         (cond
12639          ((null older) nil)
12640          ((numberp all)
12641           (if (< all len)
12642               (let ((older-range (nreverse older)))
12643                 (setq older nil)
12644
12645                 (while (> all 0)
12646                   (let* ((r (pop older-range))
12647                          (min (if (numberp r) r (car r)))
12648                          (max (if (numberp r) r (cdr r))))
12649                     (while (and (<= min max)
12650                                 (> all 0))
12651                       (push max older)
12652                       (setq all (1- all)
12653                             max (1- max))))))
12654             (setq older (gnus-uncompress-range older))))
12655          (all
12656           (setq older (gnus-uncompress-range older)))
12657          (t
12658           (when (and (numberp gnus-large-newsgroup)
12659                    (> len gnus-large-newsgroup))
12660               (let* ((cursor-in-echo-area nil)
12661                      (initial (gnus-parameter-large-newsgroup-initial
12662                                gnus-newsgroup-name))
12663                      (input
12664                       (read-string
12665                        (format
12666                         "How many articles from %s (%s %d): "
12667                         (gnus-group-decoded-name gnus-newsgroup-name)
12668                         (if initial "max" "default")
12669                         len)
12670                        (if initial
12671                            (cons (number-to-string initial)
12672                                  0)))))
12673                 (unless (string-match "^[ \t]*$" input)
12674                   (setq all (string-to-number input))
12675                   (if (< all len)
12676                       (let ((older-range (nreverse older)))
12677                         (setq older nil)
12678
12679                         (while (> all 0)
12680                           (let* ((r (pop older-range))
12681                                  (min (if (numberp r) r (car r)))
12682                                  (max (if (numberp r) r (cdr r))))
12683                             (while (and (<= min max)
12684                                         (> all 0))
12685                               (push max older)
12686                               (setq all (1- all)
12687                                     max (1- max))))))))))
12688           (setq older (gnus-uncompress-range older))))
12689         (if (not older)
12690             (message "No old news.")
12691           (gnus-summary-insert-articles older)
12692           (gnus-summary-limit (gnus-sorted-nunion old older))))
12693     (gnus-summary-position-point)))
12694
12695 (defun gnus-summary-insert-new-articles ()
12696   "Insert all new articles in this group."
12697   (interactive)
12698   (prog1
12699       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12700             (old-high gnus-newsgroup-highest)
12701             (nnmail-fetched-sources (list t))
12702             i new)
12703         (setq gnus-newsgroup-active
12704               (gnus-copy-sequence
12705                (gnus-activate-group gnus-newsgroup-name 'scan)))
12706         (setq i (cdr gnus-newsgroup-active)
12707               gnus-newsgroup-highest i)
12708         (while (> i old-high)
12709           (push i new)
12710           (decf i))
12711         (if (not new)
12712             (message "No gnus is bad news")
12713           (gnus-summary-insert-articles new)
12714           (setq gnus-newsgroup-unreads
12715                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12716           (gnus-summary-limit (gnus-sorted-nunion old new))))
12717     (gnus-summary-position-point)))
12718
12719 ;;; Bookmark support for Gnus.
12720 (declare-function bookmark-make-record-default
12721                   "bookmark" (&optional no-file no-context posn))
12722 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12723 (declare-function bookmark-default-handler "bookmark" (bmk))
12724 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12725 (defvar bookmark-yank-point)
12726 (defvar bookmark-current-buffer)
12727
12728 (defun gnus-summary-bookmark-make-record ()
12729   "Make a bookmark entry for a Gnus summary buffer."
12730   (let (pos buf)
12731     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12732       (save-restriction              ; FIXME is it necessary to widen?
12733         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12734       (setq buf "art") ; We are recording bookmark from article buffer.
12735       (setq bookmark-yank-point (point))
12736       (setq bookmark-current-buffer (current-buffer))
12737       (gnus-article-show-summary))      ; Go back in summary buffer.
12738     ;; We are now recording bookmark from summary buffer.
12739     (unless buf (setq buf "sum"))
12740     (let* ((subject (elt (gnus-summary-article-header) 1))
12741            (grp     (car gnus-article-current))
12742            (art     (cdr gnus-article-current))
12743            (head    (gnus-summary-article-header art))
12744            (id      (mail-header-id head)))
12745       `(,subject
12746         ,@(condition-case nil
12747               (bookmark-make-record-default 'no-file 'no-context pos)
12748             (wrong-number-of-arguments
12749              (bookmark-make-record-default 'point-only)))
12750         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12751         (group . ,grp) (article . ,art)
12752         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12753
12754 ;;;###autoload
12755 (defun gnus-summary-bookmark-jump (bookmark)
12756   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12757 BOOKMARK is a bookmark name or a bookmark record."
12758   (let ((group    (bookmark-prop-get bookmark 'group))
12759         (article  (bookmark-prop-get bookmark 'article))
12760         (id       (bookmark-prop-get bookmark 'message-id))
12761         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12762     (gnus-fetch-group group (list article))
12763     (gnus-summary-insert-cached-articles)
12764     (gnus-summary-goto-article id nil 'force)
12765     ;; FIXME we have to wait article buffer is ready (only large buffer)
12766     ;; Is there a better solution to know that?
12767     ;; If we don't wait `bookmark-default-handler' will have no chance
12768     ;; to set position. However there is no error, just wrong pos.
12769     (sit-for 1)
12770     (when (string= buf "Gnus-art")
12771       (other-window 1))
12772     (bookmark-default-handler
12773      `(""
12774        (buffer . ,(current-buffer))
12775        . ,(bookmark-get-bookmark-record bookmark)))))
12776
12777 (gnus-summary-make-all-marking-commands)
12778
12779 (gnus-ems-redefine)
12780
12781 (provide 'gnus-sum)
12782
12783 (run-hooks 'gnus-sum-load-hook)
12784
12785 ;; Local Variables:
12786 ;; coding: iso-8859-1
12787 ;; End:
12788
12789 ;;; gnus-sum.el ends here