The completion function expects a list instead of an alist.
[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 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
64 (autoload 'nnir-article-group "nnir" nil nil 'macro)
65
66 (defcustom gnus-kill-summary-on-exit t
67   "*If non-nil, kill the summary buffer when you exit from it.
68 If nil, the summary will become a \"*Dead Summary*\" buffer, and
69 it will be killed sometime later."
70   :group 'gnus-summary-exit
71   :type 'boolean)
72
73 (defcustom gnus-summary-next-group-on-exit t
74   "If non-nil, go to the next unread newsgroup on summary exit.
75 See `gnus-group-goto-unread'."
76   :link '(custom-manual "(gnus)Group Maneuvering")
77   :group 'gnus-summary-exit
78   :version "23.1" ;; No Gnus
79   :type 'boolean)
80
81 (defcustom gnus-summary-stop-at-end-of-message nil
82   "If non-nil, don't select the next message when using `SPC'."
83   :link '(custom-manual "(gnus)Group Maneuvering")
84   :group 'gnus-summary-maneuvering
85   :version "24.1"
86   :type 'boolean)
87
88 (defcustom gnus-fetch-old-headers nil
89   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
90 If an unread article in the group refers to an older, already
91 read (or just marked as read) article, the old article will not
92 normally be displayed in the Summary buffer.  If this variable is
93 t, Gnus will attempt to grab the headers to the old articles, and
94 thereby build complete threads.  If it has the value `some', all
95 old headers will be fetched but only enough headers to connect
96 otherwise loose threads will be displayed.  This variable can
97 also be a number.  In that case, no more than that number of old
98 headers will be fetched.  If it has the value `invisible', all
99 old headers will be fetched, but none will be displayed.
100
101 The server has to support NOV for any of this to work.
102
103 This feature can seriously impact performance it ignores all
104 locally cached header entries.  Setting it to t for groups for a
105 server that doesn't expire articles (such as news.gmane.org),
106 leads to very slow summary generation."
107   :group 'gnus-thread
108   :type '(choice (const :tag "off" nil)
109                  (const :tag "on" t)
110                  (const some)
111                  (const invisible)
112                  number
113                  (sexp :menu-tag "other" t)))
114
115 (defcustom gnus-refer-thread-limit 500
116   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
117 If t, fetch all the available old headers."
118   :group 'gnus-thread
119   :type '(choice number
120                  (sexp :menu-tag "other" t)))
121
122 (defcustom gnus-summary-make-false-root 'adopt
123   "*nil means that Gnus won't gather loose threads.
124 If the root of a thread has expired or been read in a previous
125 session, the information necessary to build a complete thread has been
126 lost.  Instead of having many small sub-threads from this original thread
127 scattered all over the summary buffer, Gnus can gather them.
128
129 If non-nil, Gnus will try to gather all loose sub-threads from an
130 original thread into one large thread.
131
132 If this variable is non-nil, it should be one of `none', `adopt',
133 `dummy' or `empty'.
134
135 If this variable is `none', Gnus will not make a false root, but just
136 present the sub-threads after another.
137 If this variable is `dummy', Gnus will create a dummy root that will
138 have all the sub-threads as children.
139 If this variable is `adopt', Gnus will make one of the \"children\"
140 the parent and mark all the step-children as such.
141 If this variable is `empty', the \"children\" are printed with empty
142 subject fields.  (Or rather, they will be printed with a string
143 given by the `gnus-summary-same-subject' variable.)"
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  (const none)
147                  (const dummy)
148                  (const adopt)
149                  (const empty)))
150
151 (defcustom gnus-summary-make-false-root-always nil
152   "Always make a false dummy root."
153   :version "22.1"
154   :group 'gnus-thread
155   :type 'boolean)
156
157 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
158   "*A regexp to match subjects to be excluded from loose thread gathering.
159 As loose thread gathering is done on subjects only, that means that
160 there can be many false gatherings performed.  By rooting out certain
161 common subjects, gathering might become saner."
162   :group 'gnus-thread
163   :type 'regexp)
164
165 (defcustom gnus-summary-gather-subject-limit nil
166   "*Maximum length of subject comparisons when gathering loose threads.
167 Use nil to compare full subjects.  Setting this variable to a low
168 number will help gather threads that have been corrupted by
169 newsreaders chopping off subject lines, but it might also mean that
170 unrelated articles that have subject that happen to begin with the
171 same few characters will be incorrectly gathered.
172
173 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
174 comparing subjects."
175   :group 'gnus-thread
176   :type '(choice (const :tag "off" nil)
177                  (const fuzzy)
178                  (sexp :menu-tag "on" t)))
179
180 (defcustom gnus-simplify-subject-functions nil
181   "List of functions taking a string argument that simplify subjects.
182 The functions are applied recursively.
183
184 Useful functions to put in this list include:
185 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
186 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
187   :group 'gnus-thread
188   :type '(repeat function))
189
190 (defcustom gnus-simplify-ignored-prefixes nil
191   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
192   :group 'gnus-thread
193   :type '(choice (const :tag "off" nil)
194                  regexp))
195
196 (defcustom gnus-build-sparse-threads nil
197   "*If non-nil, fill in the gaps in threads.
198 If `some', only fill in the gaps that are needed to tie loose threads
199 together.  If `more', fill in all leaf nodes that Gnus can find.  If
200 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
201   :group 'gnus-thread
202   :type '(choice (const :tag "off" nil)
203                  (const some)
204                  (const more)
205                  (sexp :menu-tag "all" t)))
206
207 (defcustom gnus-summary-thread-gathering-function
208   'gnus-gather-threads-by-subject
209   "*Function used for gathering loose threads.
210 There are two pre-defined functions: `gnus-gather-threads-by-subject',
211 which only takes Subjects into consideration; and
212 `gnus-gather-threads-by-references', which compared the References
213 headers of the articles to find matches."
214   :group 'gnus-thread
215   :type '(radio (function-item gnus-gather-threads-by-subject)
216                 (function-item gnus-gather-threads-by-references)
217                 (function :tag "other")))
218
219 (defcustom gnus-summary-same-subject ""
220   "*String indicating that the current article has the same subject as the previous.
221 This variable will only be used if the value of
222 `gnus-summary-make-false-root' is `empty'."
223   :group 'gnus-summary-format
224   :type 'string)
225
226 (defcustom gnus-summary-goto-unread nil
227   "*If t, many commands will go to the next unread article.
228 This applies to marking commands as well as other commands that
229 \"naturally\" select the next article, like, for instance, `SPC' at
230 the end of an article.
231
232 If nil, the marking commands do NOT go to the next unread article
233 \(they go to the next article instead).  If `never', commands that
234 usually go to the next unread article, will go to the next article,
235 whether it is read or not."
236   :version "24.1"
237   :group 'gnus-summary-marks
238   :link '(custom-manual "(gnus)Setting Marks")
239   :type '(choice (const :tag "off" nil)
240                  (const never)
241                  (sexp :menu-tag "on" t)))
242
243 (defcustom gnus-summary-default-score 0
244   "*Default article score level.
245 All scores generated by the score files will be added to this score.
246 If this variable is nil, scoring will be disabled."
247   :group 'gnus-score-default
248   :type '(choice (const :tag "disable")
249                  integer))
250
251 (defcustom gnus-summary-default-high-score 0
252   "*Default threshold for a high scored article.
253 An article will be highlighted as high scored if its score is greater
254 than this score."
255   :version "22.1"
256   :group 'gnus-score-default
257   :type 'integer)
258
259 (defcustom gnus-summary-default-low-score 0
260   "*Default threshold for a low scored article.
261 An article will be highlighted as low scored if its score is smaller
262 than this score."
263   :version "22.1"
264   :group 'gnus-score-default
265   :type 'integer)
266
267 (defcustom gnus-summary-zcore-fuzz 0
268   "*Fuzziness factor for the zcore in the summary buffer.
269 Articles with scores closer than this to `gnus-summary-default-score'
270 will not be marked."
271   :group 'gnus-summary-format
272   :type 'integer)
273
274 (defcustom gnus-simplify-subject-fuzzy-regexp nil
275   "*Strings to be removed when doing fuzzy matches.
276 This can either be a regular expression or list of regular expressions
277 that will be removed from subject strings if fuzzy subject
278 simplification is selected."
279   :group 'gnus-thread
280   :type '(repeat regexp))
281
282 (defcustom gnus-show-threads t
283   "*If non-nil, display threads in summary mode."
284   :group 'gnus-thread
285   :type 'boolean)
286
287 (defcustom gnus-thread-hide-subtree nil
288   "*If non-nil, hide all threads initially.
289 This can be a predicate specifier which says which threads to hide.
290 If threads are hidden, you have to run the command
291 `gnus-summary-show-thread' by hand or select an article."
292   :group 'gnus-thread
293   :type '(radio (sexp :format "Non-nil\n"
294                       :match (lambda (widget value)
295                                (not (or (consp value) (functionp value))))
296                       :value t)
297                 (const nil)
298                 (sexp :tag "Predicate specifier")))
299
300 (defcustom gnus-thread-hide-killed t
301   "*If non-nil, hide killed threads automatically."
302   :group 'gnus-thread
303   :type 'boolean)
304
305 (defcustom gnus-thread-ignore-subject t
306   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
307 If nil, articles that have different subjects from their parents will
308 start separate threads."
309   :group 'gnus-thread
310   :type 'boolean)
311
312 (defcustom gnus-thread-operation-ignore-subject t
313   "*If non-nil, subjects will be ignored when doing thread commands.
314 This affects commands like `gnus-summary-kill-thread' and
315 `gnus-summary-lower-thread'.
316
317 If this variable is nil, articles in the same thread with different
318 subjects will not be included in the operation in question.  If this
319 variable is `fuzzy', only articles that have subjects that are fuzzily
320 equal will be included."
321   :group 'gnus-thread
322   :type '(choice (const :tag "off" nil)
323                  (const fuzzy)
324                  (sexp :tag "on" t)))
325
326 (defcustom gnus-thread-indent-level 4
327   "*Number that says how much each sub-thread should be indented."
328   :group 'gnus-thread
329   :type 'integer)
330
331 (defcustom gnus-auto-extend-newsgroup t
332   "*If non-nil, extend newsgroup forward and backward when requested."
333   :group 'gnus-summary-choose
334   :type 'boolean)
335
336 (defcustom gnus-auto-select-first t
337   "If non-nil, select an article on group entry.
338 An article is selected automatically when entering a group
339 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
340 `gnus-summary-catchup-and-goto-next-group'.
341
342 Which article is selected is controlled by the variable
343 `gnus-auto-select-subject'.
344
345 If you want to prevent automatic selection of articles in some
346 newsgroups, set the variable to nil in `gnus-select-group-hook'."
347   ;; Commands include...
348   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
349   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
350   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
351   :group 'gnus-group-select
352   :type '(choice (const :tag "none" nil)
353                  (sexp :menu-tag "first" t)))
354
355 (defcustom gnus-auto-select-subject 'unseen-or-unread
356   "*Says what subject to place under point when entering a group.
357
358 This variable can either be the symbols `first' (place point on the
359 first subject), `unread' (place point on the subject line of the first
360 unread article), `best' (place point on the subject line of the
361 higest-scored article), `unseen' (place point on the subject line of
362 the first unseen article), `unseen-or-unread' (place point on the subject
363 line of the first unseen article or, if all article have been seen, on the
364 subject line of the first unread article), or a function to be called to
365 place point on some subject line."
366   :version "24.1"
367   :group 'gnus-group-select
368   :type '(choice (const best)
369                  (const unread)
370                  (const first)
371                  (const unseen)
372                  (const unseen-or-unread)))
373
374 (defcustom gnus-auto-select-next t
375   "*If non-nil, offer to go to the next group from the end of the previous.
376 If the value is t and the next newsgroup is empty, Gnus will exit
377 summary mode and go back to group mode.  If the value is neither nil
378 nor t, Gnus will select the following unread newsgroup.  In
379 particular, if the value is the symbol `quietly', the next unread
380 newsgroup will be selected without any confirmation, and if it is
381 `almost-quietly', the next group will be selected without any
382 confirmation if you are located on the last article in the group.
383 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
384 will go to the next group without confirmation."
385   :group 'gnus-summary-maneuvering
386   :type '(choice (const :tag "off" nil)
387                  (const quietly)
388                  (const almost-quietly)
389                  (const slightly-quietly)
390                  (sexp :menu-tag "on" t)))
391
392 (defcustom gnus-auto-select-same nil
393   "*If non-nil, select the next article with the same subject.
394 If there are no more articles with the same subject, go to
395 the first unread article."
396   :group 'gnus-summary-maneuvering
397   :type 'boolean)
398
399 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
400   "What article should be selected after exiting an ephemeral group.
401 Valid values include:
402
403 `next'
404   Select the next article.
405 `next-unread'
406   Select the next unread article.
407 `next-noselect'
408   Move the cursor to the next article.  This is the default.
409 `next-unread-noselect'
410   Move the cursor to the next unread article.
411
412 If it has any other value or there is no next (unread) article, the
413 article selected before entering to the ephemeral group will appear."
414   :version "23.1" ;; No Gnus
415   :group 'gnus-summary-maneuvering
416   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
417                  (const next) (const next-unread)
418                  (const next-noselect) (const next-unread-noselect)
419                  (sexp :tag "other" :value nil)))
420
421 (defcustom gnus-auto-goto-ignores 'unfetched
422   "*Says how to handle unfetched articles when maneuvering.
423
424 This variable can either be the symbols nil (maneuver to any
425 article), `undownloaded' (maneuvering while unplugged ignores articles
426 that have not been fetched), `always-undownloaded' (maneuvering always
427 ignores articles that have not been fetched), `unfetched' (maneuvering
428 ignores articles whose headers have not been fetched).
429
430 NOTE: The list of unfetched articles will always be nil when plugged
431 and, when unplugged, a subset of the undownloaded article list."
432   :version "22.1"
433   :group 'gnus-summary-maneuvering
434   :type '(choice (const :tag "None" nil)
435                  (const :tag "Undownloaded when unplugged" undownloaded)
436                  (const :tag "Undownloaded" always-undownloaded)
437                  (const :tag "Unfetched" unfetched)))
438
439 (defcustom gnus-summary-check-current nil
440   "*If non-nil, consider the current article when moving.
441 The \"unread\" movement commands will stay on the same line if the
442 current article is unread."
443   :group 'gnus-summary-maneuvering
444   :type 'boolean)
445
446 (defcustom gnus-auto-center-summary 2
447   "*If non-nil, always center the current summary buffer.
448 In particular, if `vertical' do only vertical recentering.  If non-nil
449 and non-`vertical', do both horizontal and vertical recentering."
450   :group 'gnus-summary-maneuvering
451   :type '(choice (const :tag "none" nil)
452                  (const vertical)
453                  (integer :tag "height")
454                  (sexp :menu-tag "both" t)))
455
456 (defcustom gnus-auto-center-group t
457   "If non-nil, always center the group buffer."
458   :group 'gnus-summary-maneuvering
459   :type 'boolean)
460
461 (defcustom gnus-show-all-headers nil
462   "*If non-nil, don't hide any headers."
463   :group 'gnus-article-hiding
464   :group 'gnus-article-headers
465   :type 'boolean)
466
467 (defcustom gnus-summary-ignore-duplicates nil
468   "*If non-nil, ignore articles with identical Message-ID headers."
469   :group 'gnus-summary
470   :type 'boolean)
471
472 (defcustom gnus-single-article-buffer nil
473   "*If non-nil, display all articles in the same buffer.
474 If nil, each group will get its own article buffer."
475   :version "24.1"
476   :group 'gnus-article-various
477   :type 'boolean)
478
479 (defcustom gnus-widen-article-window nil
480   "If non-nil, selecting the article buffer will display only the article buffer."
481   :version "24.1"
482   :group 'gnus-article-various
483   :type 'boolean)
484
485 (defcustom gnus-break-pages t
486   "*If non-nil, do page breaking on articles.
487 The page delimiter is specified by the `gnus-page-delimiter'
488 variable."
489   :group 'gnus-article-various
490   :type 'boolean)
491
492 (defcustom gnus-move-split-methods nil
493   "*Variable used to suggest where articles are to be moved to.
494 It uses the same syntax as the `gnus-split-methods' variable.
495 However, whereas `gnus-split-methods' specifies file names as targets,
496 this variable specifies group names."
497   :group 'gnus-summary-mail
498   :type '(repeat (choice (list :value (fun) function)
499                          (cons :value ("" "") regexp (repeat string))
500                          (sexp :value nil))))
501
502 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
503   "Function used to compute default prefix for article move/copy/etc prompts.
504 The function should take one argument, a group name, and return a
505 string with the suggested prefix."
506   :group 'gnus-summary-mail
507   :type 'function)
508
509 ;; FIXME: Although the custom type is `character' for the following variables,
510 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
511
512 (defcustom gnus-unread-mark ?           ;Whitespace
513   "*Mark used for unread articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-ticked-mark ?!
518   "*Mark used for ticked articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-dormant-mark ??
523   "*Mark used for dormant articles."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-del-mark ?r
528   "*Mark used for del'd articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-read-mark ?R
533   "*Mark used for read articles."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-expirable-mark ?E
538   "*Mark used for expirable articles."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-killed-mark ?K
543   "*Mark used for killed articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-spam-mark ?$
548   "*Mark used for spam articles."
549   :version "22.1"
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-kill-file-mark ?X
554   "*Mark used for articles killed by kill files."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-low-score-mark ?Y
559   "*Mark used for articles with a low score."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-catchup-mark ?C
564   "*Mark used for articles that are caught up."
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-replied-mark ?A
569   "*Mark used for articles that have been replied to."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-forwarded-mark ?F
574   "*Mark used for articles that have been forwarded."
575   :version "22.1"
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-recent-mark ?N
580   "*Mark used for articles that are recent."
581   :version "22.1"
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-cached-mark ?*
586   "*Mark used for articles that are in the cache."
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-saved-mark ?S
591   "*Mark used for articles that have been saved."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-unseen-mark ?.
596   "*Mark used for articles that haven't been seen."
597   :version "22.1"
598   :group 'gnus-summary-marks
599   :type 'character)
600
601 (defcustom gnus-no-mark ?               ;Whitespace
602   "*Mark used for articles that have no other secondary mark."
603   :version "22.1"
604   :group 'gnus-summary-marks
605   :type 'character)
606
607 (defcustom gnus-ancient-mark ?O
608   "*Mark used for ancient articles."
609   :group 'gnus-summary-marks
610   :type 'character)
611
612 (defcustom gnus-sparse-mark ?Q
613   "*Mark used for sparsely reffed articles."
614   :group 'gnus-summary-marks
615   :type 'character)
616
617 (defcustom gnus-canceled-mark ?G
618   "*Mark used for canceled articles."
619   :group 'gnus-summary-marks
620   :type 'character)
621
622 (defcustom gnus-duplicate-mark ?M
623   "*Mark used for duplicate articles."
624   :group 'gnus-summary-marks
625   :type 'character)
626
627 (defcustom gnus-undownloaded-mark ?-
628   "*Mark used for articles that weren't downloaded."
629   :version "22.1"
630   :group 'gnus-summary-marks
631   :type 'character)
632
633 (defcustom gnus-downloaded-mark ?+
634   "*Mark used for articles that were downloaded."
635   :group 'gnus-summary-marks
636   :type 'character)
637
638 (defcustom gnus-downloadable-mark ?%
639   "*Mark used for articles that are to be downloaded."
640   :group 'gnus-summary-marks
641   :type 'character)
642
643 (defcustom gnus-unsendable-mark ?=
644   "*Mark used for articles that won't be sent."
645   :group 'gnus-summary-marks
646   :type 'character)
647
648 (defcustom gnus-score-over-mark ?+
649   "*Score mark used for articles with high scores."
650   :group 'gnus-summary-marks
651   :type 'character)
652
653 (defcustom gnus-score-below-mark ?-
654   "*Score mark used for articles with low scores."
655   :group 'gnus-summary-marks
656   :type 'character)
657
658 (defcustom gnus-empty-thread-mark ?     ;Whitespace
659   "*There is no thread under the article."
660   :group 'gnus-summary-marks
661   :type 'character)
662
663 (defcustom gnus-not-empty-thread-mark ?=
664   "*There is a thread under the article."
665   :group 'gnus-summary-marks
666   :type 'character)
667
668 (defcustom gnus-view-pseudo-asynchronously nil
669   "*If non-nil, Gnus will view pseudo-articles asynchronously."
670   :group 'gnus-extract-view
671   :type 'boolean)
672
673 (defcustom gnus-auto-expirable-marks
674   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
675         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
676         gnus-duplicate-mark)
677   "*The list of marks converted into expiration if a group is auto-expirable."
678   :version "24.1"
679   :group 'gnus-summary
680   :type '(repeat character))
681
682 (defcustom gnus-inhibit-user-auto-expire t
683   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
684   :version "21.1"
685   :group 'gnus-summary
686   :type 'boolean)
687
688 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
689   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
690 If nil, the expirable marks will be unchanged except that the marks
691 will be removed when copying or moving articles to a group that has
692 not turned auto-expire on.  If non-nil, articles that have been read
693 will be marked as expirable when being copied or moved to a group in
694 which auto-expire is turned on."
695   :version "23.2"
696   :type 'boolean
697   :group 'gnus-summary-marks)
698
699 (defcustom gnus-view-pseudos nil
700   "*If `automatic', pseudo-articles will be viewed automatically.
701 If `not-confirm', pseudos will be viewed automatically, and the user
702 will not be asked to confirm the command."
703   :group 'gnus-extract-view
704   :type '(choice (const :tag "off" nil)
705                  (const automatic)
706                  (const not-confirm)))
707
708 (defcustom gnus-view-pseudos-separately t
709   "*If non-nil, one pseudo-article will be created for each file to be viewed.
710 If nil, all files that use the same viewing command will be given as a
711 list of parameters to that command."
712   :group 'gnus-extract-view
713   :type 'boolean)
714
715 (defcustom gnus-insert-pseudo-articles t
716   "*If non-nil, insert pseudo-articles when decoding articles."
717   :group 'gnus-extract-view
718   :type 'boolean)
719
720 (defcustom gnus-summary-dummy-line-format
721   "   %(:                             :%) %S\n"
722   "*The format specification for the dummy roots in the summary buffer.
723 It works along the same lines as a normal formatting string,
724 with some simple extensions.
725
726 %S  The subject
727
728 General format specifiers can also be used.
729 See `(gnus)Formatting Variables'."
730   :link '(custom-manual "(gnus)Formatting Variables")
731   :group 'gnus-threading
732   :type 'string)
733
734 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
735   "*The format specification for the summary mode line.
736 It works along the same lines as a normal formatting string,
737 with some simple extensions:
738
739 %G  Group name
740 %p  Unprefixed group name
741 %A  Current article number
742 %z  Current article score
743 %V  Gnus version
744 %U  Number of unread articles in the group
745 %e  Number of unselected articles in the group
746 %Z  A string with unread/unselected article counts
747 %g  Shortish group name
748 %S  Subject of the current article
749 %u  User-defined spec
750 %s  Current score file name
751 %d  Number of dormant articles
752 %r  Number of articles that have been marked as read in this session
753 %E  Number of articles expunged by the score files"
754   :group 'gnus-summary-format
755   :type 'string)
756
757 (defcustom gnus-list-identifiers nil
758   "Regexp that matches list identifiers to be removed from subject.
759 This can also be a list of regexps."
760   :version "21.1"
761   :group 'gnus-summary-format
762   :group 'gnus-article-hiding
763   :type '(choice (const :tag "none" nil)
764                  (regexp :value ".*")
765                  (repeat :value (".*") regexp)))
766
767 (defcustom gnus-summary-mark-below 0
768   "*Mark all articles with a score below this variable as read.
769 This variable is local to each summary buffer and usually set by the
770 score file."
771   :group 'gnus-score-default
772   :type 'integer)
773
774 (defun gnus-widget-reversible-match (widget value)
775   "Ignoring WIDGET, convert VALUE to internal form.
776 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
777   ;; (debug value)
778   (or (symbolp value)
779       (and (listp value)
780            (eq (length value) 2)
781            (eq (nth 0 value) 'not)
782            (symbolp (nth 1 value)))))
783
784 (defun gnus-widget-reversible-to-internal (widget value)
785   "Ignoring WIDGET, convert VALUE to internal form.
786 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
787 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
788   ;; (debug value)
789   (if (atom value)
790       (list value nil)
791     (list (nth 1 value) t)))
792
793 (defun gnus-widget-reversible-to-external (widget value)
794   "Ignoring WIDGET, convert VALUE to external form.
795 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
796 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
797   ;; (debug value)
798   (if (nth 1 value)
799       (list 'not (nth 0 value))
800     (nth 0 value)))
801
802 (define-widget 'gnus-widget-reversible 'group
803   "A `group' that convert values."
804   :match 'gnus-widget-reversible-match
805   :value-to-internal 'gnus-widget-reversible-to-internal
806   :value-to-external 'gnus-widget-reversible-to-external)
807
808 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
809   "*List of functions used for sorting articles in the summary buffer.
810
811 Each function takes two articles and returns non-nil if the first
812 article should be sorted before the other.  If you use more than one
813 function, the primary sort function should be the last.  You should
814 probably always include `gnus-article-sort-by-number' in the list of
815 sorting functions -- preferably first.  Also note that sorting by date
816 is often much slower than sorting by number, and the sorting order is
817 very similar.  (Sorting by date means sorting by the time the message
818 was sent, sorting by number means sorting by arrival time.)
819
820 Each item can also be a list `(not F)' where F is a function;
821 this reverses the sort order.
822
823 Ready-made functions include `gnus-article-sort-by-number',
824 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
825 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
826 and `gnus-article-sort-by-score'.
827
828 When threading is turned on, the variable `gnus-thread-sort-functions'
829 controls how articles are sorted."
830   :group 'gnus-summary-sort
831   :type '(repeat (gnus-widget-reversible
832                   (choice (function-item gnus-article-sort-by-number)
833                           (function-item gnus-article-sort-by-author)
834                           (function-item gnus-article-sort-by-subject)
835                           (function-item gnus-article-sort-by-date)
836                           (function-item gnus-article-sort-by-score)
837                           (function-item gnus-article-sort-by-random)
838                           (function :tag "other"))
839                   (boolean :tag "Reverse order"))))
840
841
842 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
843   "*List of functions used for sorting threads in the summary buffer.
844 By default, threads are sorted by article number.
845
846 Each function takes two threads and returns non-nil if the first
847 thread should be sorted before the other.  If you use more than one
848 function, the primary sort function should be the last.  You should
849 probably always include `gnus-thread-sort-by-number' in the list of
850 sorting functions -- preferably first.  Also note that sorting by date
851 is often much slower than sorting by number, and the sorting order is
852 very similar.  (Sorting by date means sorting by the time the message
853 was sent, sorting by number means sorting by arrival time.)
854
855 Each list item can also be a list `(not F)' where F is a
856 function; this specifies reversed sort order.
857
858 Ready-made functions include `gnus-thread-sort-by-number',
859 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
860 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
861 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
862 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
863 and `gnus-thread-sort-by-total-score' (see
864 `gnus-thread-score-function').
865
866 When threading is turned off, the variable
867 `gnus-article-sort-functions' controls how articles are sorted."
868   :group 'gnus-summary-sort
869   :type '(repeat
870           (gnus-widget-reversible
871            (choice (function-item gnus-thread-sort-by-number)
872                    (function-item gnus-thread-sort-by-author)
873                    (function-item gnus-thread-sort-by-recipient)
874                    (function-item gnus-thread-sort-by-subject)
875                    (function-item gnus-thread-sort-by-date)
876                    (function-item gnus-thread-sort-by-score)
877                    (function-item gnus-thread-sort-by-most-recent-number)
878                    (function-item gnus-thread-sort-by-most-recent-date)
879                    (function-item gnus-thread-sort-by-random)
880                    (function-item gnus-thread-sort-by-total-score)
881                    (function :tag "other"))
882            (boolean :tag "Reverse order"))))
883
884 (defcustom gnus-thread-score-function '+
885   "*Function used for calculating the total score of a thread.
886
887 The function is called with the scores of the article and each
888 subthread and should then return the score of the thread.
889
890 Some functions you can use are `+', `max', or `min'."
891   :group 'gnus-summary-sort
892   :type 'function)
893
894 (defcustom gnus-summary-expunge-below nil
895   "All articles that have a score less than this variable will be expunged.
896 This variable is local to the summary buffers."
897   :group 'gnus-score-default
898   :type '(choice (const :tag "off" nil)
899                  integer))
900
901 (defcustom gnus-thread-expunge-below nil
902   "All threads that have a total score less than this variable will be expunged.
903 See `gnus-thread-score-function' for en explanation of what a
904 \"thread score\" is.
905
906 This variable is local to the summary buffers."
907   :group 'gnus-threading
908   :group 'gnus-score-default
909   :type '(choice (const :tag "off" nil)
910                  integer))
911
912 (defcustom gnus-summary-mode-hook nil
913   "*A hook for Gnus summary mode.
914 This hook is run before any variables are set in the summary buffer."
915   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
916   :group 'gnus-summary-various
917   :type 'hook)
918
919 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
920 (when (featurep 'xemacs)
921   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
922   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
923   (add-hook 'gnus-summary-mode-hook
924             'gnus-xmas-switch-horizontal-scrollbar-off))
925
926 (defcustom gnus-summary-menu-hook nil
927   "*Hook run after the creation of the summary mode menu."
928   :group 'gnus-summary-visual
929   :type 'hook)
930
931 (defcustom gnus-summary-exit-hook nil
932   "*A hook called on exit from the summary buffer.
933 It will be called with point in the group buffer."
934   :group 'gnus-summary-exit
935   :type 'hook)
936
937 (defcustom gnus-summary-prepare-hook nil
938   "*A hook called after the summary buffer has been generated.
939 If you want to modify the summary buffer, you can use this hook."
940   :group 'gnus-summary-various
941   :type 'hook)
942
943 (defcustom gnus-summary-prepared-hook nil
944   "*A hook called as the last thing after the summary buffer has been generated."
945   :group 'gnus-summary-various
946   :type 'hook)
947
948 (defcustom gnus-summary-generate-hook nil
949   "*A hook run just before generating the summary buffer.
950 This hook is commonly used to customize threading variables and the
951 like."
952   :group 'gnus-summary-various
953   :type 'hook)
954
955 (defcustom gnus-select-group-hook nil
956   "*A hook called when a newsgroup is selected.
957
958 If you'd like to simplify subjects like the
959 `gnus-summary-next-same-subject' command does, you can use the
960 following hook:
961
962  (add-hook gnus-select-group-hook
963            (lambda ()
964              (mapcar (lambda (header)
965                        (mail-header-set-subject
966                         header
967                         (gnus-simplify-subject
968                          (mail-header-subject header) 're-only)))
969                      gnus-newsgroup-headers)))"
970   :group 'gnus-group-select
971   :type 'hook)
972
973 (defcustom gnus-select-article-hook nil
974   "*A hook called when an article is selected."
975   :group 'gnus-summary-choose
976   :options '(gnus-agent-fetch-selected-article)
977   :type 'hook)
978
979 (defcustom gnus-visual-mark-article-hook
980   (list 'gnus-highlight-selected-summary)
981   "*Hook run after selecting an article in the summary buffer.
982 It is meant to be used for highlighting the article in some way.  It
983 is not run if `gnus-visual' is nil."
984   :group 'gnus-summary-visual
985   :type 'hook)
986
987 (defcustom gnus-parse-headers-hook nil
988   "*A hook called before parsing the headers."
989   :group 'gnus-various
990   :type 'hook)
991
992 (defcustom gnus-exit-group-hook nil
993   "*A hook called when exiting summary mode.
994 This hook is not called from the non-updating exit commands like `Q'."
995   :group 'gnus-various
996   :type 'hook)
997
998 (defcustom gnus-summary-update-hook nil
999   "*A hook called when a summary line is changed.
1000 The hook will not be called if `gnus-visual' is nil.
1001
1002 The default function `gnus-summary-highlight-line' will
1003 highlight the line according to the `gnus-summary-highlight'
1004 variable."
1005   :group 'gnus-summary-visual
1006   :type 'hook)
1007
1008 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1009   "*A hook called when an article is selected for the first time.
1010 The hook is intended to mark an article as read (or unread)
1011 automatically when it is selected."
1012   :group 'gnus-summary-choose
1013   :type 'hook)
1014
1015 (defcustom gnus-group-no-more-groups-hook nil
1016   "*A hook run when returning to group mode having no more (unread) groups."
1017   :group 'gnus-group-select
1018   :type 'hook)
1019
1020 (defcustom gnus-ps-print-hook nil
1021   "*A hook run before ps-printing something from Gnus."
1022   :group 'gnus-summary
1023   :type 'hook)
1024
1025 (defcustom gnus-summary-article-move-hook nil
1026   "*A hook called after an article is moved, copied, respooled, or crossposted."
1027   :version "22.1"
1028   :group 'gnus-summary
1029   :type 'hook)
1030
1031 (defcustom gnus-summary-article-delete-hook nil
1032   "*A hook called after an article is deleted."
1033   :version "22.1"
1034   :group 'gnus-summary
1035   :type 'hook)
1036
1037 (defcustom gnus-summary-article-expire-hook nil
1038   "*A hook called after an article is expired."
1039   :version "22.1"
1040   :group 'gnus-summary
1041   :type 'hook)
1042
1043 (defcustom gnus-summary-display-arrow
1044   (and (fboundp 'display-graphic-p)
1045        (display-graphic-p))
1046   "*If non-nil, display an arrow highlighting the current article."
1047   :version "22.1"
1048   :group 'gnus-summary
1049   :type 'boolean)
1050
1051 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1052   "Face used for highlighting the current article in the summary buffer."
1053   :group 'gnus-summary-visual
1054   :type 'face)
1055
1056 (defvar gnus-tmp-downloaded nil)
1057
1058 (defcustom gnus-summary-highlight
1059   '(((eq mark gnus-canceled-mark)
1060      . gnus-summary-cancelled)
1061     ((and uncached (> score default-high))
1062      . gnus-summary-high-undownloaded)
1063     ((and uncached (< score default-low))
1064      . gnus-summary-low-undownloaded)
1065     (uncached
1066      . gnus-summary-normal-undownloaded)
1067     ((and (> score default-high)
1068           (or (eq mark gnus-dormant-mark)
1069               (eq mark gnus-ticked-mark)))
1070      . gnus-summary-high-ticked)
1071     ((and (< score default-low)
1072           (or (eq mark gnus-dormant-mark)
1073               (eq mark gnus-ticked-mark)))
1074      . gnus-summary-low-ticked)
1075     ((or (eq mark gnus-dormant-mark)
1076          (eq mark gnus-ticked-mark))
1077      . gnus-summary-normal-ticked)
1078     ((and (> score default-high) (eq mark gnus-ancient-mark))
1079      . gnus-summary-high-ancient)
1080     ((and (< score default-low) (eq mark gnus-ancient-mark))
1081      . gnus-summary-low-ancient)
1082     ((eq mark gnus-ancient-mark)
1083      . gnus-summary-normal-ancient)
1084     ((and (> score default-high) (eq mark gnus-unread-mark))
1085      . gnus-summary-high-unread)
1086     ((and (< score default-low) (eq mark gnus-unread-mark))
1087      . gnus-summary-low-unread)
1088     ((eq mark gnus-unread-mark)
1089      . gnus-summary-normal-unread)
1090     ((> score default-high)
1091      . gnus-summary-high-read)
1092     ((< score default-low)
1093      . gnus-summary-low-read)
1094     (t
1095      . gnus-summary-normal-read))
1096   "*Controls the highlighting of summary buffer lines.
1097
1098 A list of (FORM . FACE) pairs.  When deciding how a particular
1099 summary line should be displayed, each form is evaluated.  The content
1100 of the face field after the first true form is used.  You can change
1101 how those summary lines are displayed, by editing the face field.
1102
1103 You can use the following variables in the FORM field.
1104
1105 score:        The article's score.
1106 default:      The default article score.
1107 default-high: The default score for high scored articles.
1108 default-low:  The default score for low scored articles.
1109 below:        The score below which articles are automatically marked as read.
1110 mark:         The article's mark.
1111 uncached:     Non-nil if the article is uncached."
1112   :group 'gnus-summary-visual
1113   :type '(repeat (cons (sexp :tag "Form" nil)
1114                        face)))
1115 (put 'gnus-summary-highlight 'risky-local-variable t)
1116
1117 (defcustom gnus-alter-header-function nil
1118   "Function called to allow alteration of article header structures.
1119 The function is called with one parameter, the article header vector,
1120 which it may alter in any way."
1121   :type '(choice (const :tag "None" nil)
1122                  function)
1123   :group 'gnus-summary)
1124
1125 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1126   "Function used to decode a string with encoded words.")
1127
1128 (defvar gnus-decode-encoded-address-function
1129   'mail-decode-encoded-address-string
1130   "Function used to decode addresses with encoded words.")
1131
1132 (defcustom gnus-extra-headers '(To Newsgroups)
1133   "*Extra headers to parse."
1134   :version "21.1"
1135   :group 'gnus-summary
1136   :type '(repeat symbol))
1137
1138 (defcustom gnus-ignored-from-addresses
1139   (and user-mail-address
1140        (not (string= user-mail-address ""))
1141        (regexp-quote user-mail-address))
1142   "*From headers that may be suppressed in favor of To headers.
1143 This can be a regexp or a list of regexps."
1144   :version "21.1"
1145   :group 'gnus-summary
1146   :type '(choice regexp
1147                  (repeat :tag "Regexp List" regexp)))
1148
1149 (defsubst gnus-ignored-from-addresses ()
1150   (gmm-regexp-concat gnus-ignored-from-addresses))
1151
1152 (defcustom gnus-summary-to-prefix "-> "
1153   "*String prefixed to the To field in the summary line when
1154 using `gnus-ignored-from-addresses'."
1155   :version "22.1"
1156   :group 'gnus-summary
1157   :type 'string)
1158
1159 (defcustom gnus-summary-newsgroup-prefix "=> "
1160   "*String prefixed to the Newsgroup field in the summary
1161 line when using `gnus-ignored-from-addresses'."
1162   :version "22.1"
1163   :group 'gnus-summary
1164   :type 'string)
1165
1166 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1167   "List of charsets that should be ignored.
1168 When these charsets are used in the \"charset\" parameter, the
1169 default charset will be used instead."
1170   :version "21.1"
1171   :type '(repeat symbol)
1172   :group 'gnus-charset)
1173
1174 (defcustom gnus-newsgroup-maximum-articles nil
1175   "The maximum number of articles a newsgroup.
1176 If this is a number, old articles in a newsgroup exceeding this number
1177 are silently ignored.  If it is nil, no article is ignored.  Note that
1178 setting this variable to a number might prevent you from reading very
1179 old articles."
1180   :group 'gnus-group-select
1181   :version "22.2"
1182   :type '(choice (const :tag "No limit" nil)
1183                  integer))
1184
1185 (gnus-define-group-parameter
1186  ignored-charsets
1187  :type list
1188  :function-document
1189  "Return the ignored charsets of GROUP."
1190  :variable gnus-group-ignored-charsets-alist
1191  :variable-default
1192  '(("alt\\.chinese\\.text" iso-8859-1))
1193  :variable-document
1194  "Alist of regexps (to match group names) and charsets that should be ignored.
1195 When these charsets are used in the \"charset\" parameter, the
1196 default charset will be used instead."
1197  :variable-group gnus-charset
1198  :variable-type '(repeat (cons (regexp :tag "Group")
1199                                (repeat symbol)))
1200  :parameter-type '(choice :tag "Ignored charsets"
1201                           :value nil
1202                           (repeat (symbol)))
1203  :parameter-document       "\
1204 List of charsets that should be ignored.
1205
1206 When these charsets are used in the \"charset\" parameter, the
1207 default charset will be used instead.")
1208
1209 (defcustom gnus-group-highlight-words-alist nil
1210   "Alist of group regexps and highlight regexps.
1211 This variable uses the same syntax as `gnus-emphasis-alist'."
1212   :version "21.1"
1213   :type '(repeat (cons (regexp :tag "Group")
1214                        (repeat (list (regexp :tag "Highlight regexp")
1215                                      (number :tag "Group for entire word" 0)
1216                                      (number :tag "Group for displayed part" 0)
1217                                      (symbol :tag "Face"
1218                                              gnus-emphasis-highlight-words)))))
1219   :group 'gnus-summary-visual)
1220
1221 (defcustom gnus-summary-show-article-charset-alist
1222   nil
1223   "Alist of number and charset.
1224 The article will be shown with the charset corresponding to the
1225 numbered argument.
1226 For example: ((1 . cn-gb-2312) (2 . big5))."
1227   :version "21.1"
1228   :type '(repeat (cons (number :tag "Argument" 1)
1229                        (symbol :tag "Charset")))
1230   :group 'gnus-charset)
1231
1232 (defcustom gnus-preserve-marks t
1233   "Whether marks are preserved when moving, copying and respooling messages."
1234   :version "21.1"
1235   :type 'boolean
1236   :group 'gnus-summary-marks)
1237
1238 (defcustom gnus-propagate-marks t
1239   "If non-nil, do not propagate marks to the backends."
1240   :version "23.1" ;; No Gnus
1241   :type 'boolean
1242   :group 'gnus-summary-marks)
1243
1244 (defcustom gnus-alter-articles-to-read-function nil
1245   "Function to be called to alter the list of articles to be selected."
1246   :type '(choice (const nil) function)
1247   :group 'gnus-summary)
1248
1249 (defcustom gnus-orphan-score nil
1250   "*All orphans get this score added.  Set in the score file."
1251   :group 'gnus-score-default
1252   :type '(choice (const nil)
1253                  integer))
1254
1255 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1256   "*A regexp to match MIME parts when saving multiple parts of a
1257 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1258 This regexp will be used by default when prompting the user for which
1259 type of files to save."
1260   :group 'gnus-summary
1261   :type 'regexp)
1262
1263 (defcustom gnus-read-all-available-headers nil
1264   "Whether Gnus should parse all headers made available to it.
1265 This is mostly relevant for slow back ends where the user may
1266 wish to widen the summary buffer to include all headers
1267 that were fetched."
1268   :version "22.1"
1269   :group 'gnus-summary
1270   :type '(choice boolean regexp))
1271
1272 (defcustom gnus-summary-pipe-output-default-command nil
1273   "Command (and optional arguments) used to pipe article to subprocess.
1274 This will be used as the default command if it is non-nil.  The value
1275 will be updated if you modify it when executing the command
1276 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1277   :version "23.1" ;; No Gnus
1278   :group 'gnus-summary
1279   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1280
1281 (defcustom gnus-summary-muttprint-program "muttprint"
1282   "Command (and optional arguments) used to run Muttprint.
1283 The value will be updated if you modify it when executing the command
1284 `gnus-summary-muttprint'."
1285   :version "22.1"
1286   :group 'gnus-summary
1287   :type 'string)
1288
1289 (defcustom gnus-article-loose-mime t
1290   "If non-nil, don't require MIME-Version header.
1291 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1292 supply the MIME-Version header or deliberately strip it from the mail.
1293 If non-nil (the default), Gnus will treat some articles as MIME
1294 even if the MIME-Version header is missing."
1295   :version "22.1"
1296   :type 'boolean
1297   :group 'gnus-article-mime)
1298
1299 (defcustom gnus-article-emulate-mime t
1300   "If non-nil, use MIME emulation for uuencode and the like.
1301 This means that Gnus will search message bodies for text that look
1302 like uuencoded bits, yEncoded bits, and so on, and present that using
1303 the normal Gnus MIME machinery."
1304   :version "22.1"
1305   :type 'boolean
1306   :group 'gnus-article-mime)
1307
1308 ;;; Internal variables
1309
1310 (defvar gnus-summary-display-cache nil)
1311 (defvar gnus-article-mime-handles nil)
1312 (defvar gnus-article-decoded-p nil)
1313 (defvar gnus-article-charset nil)
1314 (defvar gnus-article-ignored-charsets nil)
1315 (defvar gnus-scores-exclude-files nil)
1316 (defvar gnus-page-broken nil)
1317
1318 (defvar gnus-original-article nil)
1319 (defvar gnus-article-internal-prepare-hook nil)
1320 (defvar gnus-newsgroup-process-stack nil)
1321
1322 (defvar gnus-thread-indent-array nil)
1323 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1324 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1325   "Function called to sort the articles within a thread after it has been gathered together.")
1326
1327 (defvar gnus-summary-save-parts-type-history nil)
1328 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1329
1330 ;; Avoid highlighting in kill files.
1331 (defvar gnus-summary-inhibit-highlight nil)
1332 (defvar gnus-newsgroup-selected-overlay nil)
1333 (defvar gnus-inhibit-limiting nil)
1334 (defvar gnus-newsgroup-adaptive-score-file nil)
1335 (defvar gnus-current-score-file nil)
1336 (defvar gnus-current-move-group nil)
1337 (defvar gnus-current-copy-group nil)
1338 (defvar gnus-current-crosspost-group nil)
1339 (defvar gnus-newsgroup-display 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     (?Z (or ,(gnus-macroexpand-all
1367               '(nnir-article-rsv (mail-header-number gnus-tmp-header)))
1368             0) ?d)
1369     (?G (or ,(gnus-macroexpand-all
1370               '(nnir-article-group (mail-header-number gnus-tmp-header)))
1371             "") ?s)
1372     (?g (or ,(gnus-macroexpand-all
1373               '(gnus-group-short-name
1374                 (nnir-article-group (mail-header-number gnus-tmp-header))))
1375             "") ?s)
1376     (?O gnus-tmp-downloaded ?c)
1377     (?I gnus-tmp-indentation ?s)
1378     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1379     (?R gnus-tmp-replied ?c)
1380     (?\[ gnus-tmp-opening-bracket ?c)
1381     (?\] gnus-tmp-closing-bracket ?c)
1382     (?\> (make-string gnus-tmp-level ? ) ?s)
1383     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1384     (?i gnus-tmp-score ?d)
1385     (?z gnus-tmp-score-char ?c)
1386     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1387     (?U gnus-tmp-unread ?c)
1388     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1389         ?s)
1390     (?t (gnus-summary-number-of-articles-in-thread
1391          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1392         ?d)
1393     (?e (gnus-summary-number-of-articles-in-thread
1394          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1395         ?c)
1396     (?u gnus-tmp-user-defined ?s)
1397     (?P (gnus-pick-line-number) ?d)
1398     (?B gnus-tmp-thread-tree-header-string ?s)
1399     (user-date (gnus-user-date
1400                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1401   "An alist of format specifications that can appear in summary lines.
1402 These are paired with what variables they correspond with, along with
1403 the type of the variable (string, integer, character, etc).")
1404
1405 (defvar gnus-summary-dummy-line-format-alist
1406   `((?S gnus-tmp-subject ?s)
1407     (?N gnus-tmp-number ?d)
1408     (?u gnus-tmp-user-defined ?s)))
1409
1410 (defvar gnus-summary-mode-line-format-alist
1411   `((?G gnus-tmp-group-name ?s)
1412     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1413     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1414     (?A gnus-tmp-article-number ?d)
1415     (?Z gnus-tmp-unread-and-unselected ?s)
1416     (?V gnus-version ?s)
1417     (?U gnus-tmp-unread-and-unticked ?d)
1418     (?S gnus-tmp-subject ?s)
1419     (?e gnus-tmp-unselected ?d)
1420     (?u gnus-tmp-user-defined ?s)
1421     (?d (length gnus-newsgroup-dormant) ?d)
1422     (?t (length gnus-newsgroup-marked) ?d)
1423     (?h (length gnus-newsgroup-spam-marked) ?d)
1424     (?r (length gnus-newsgroup-reads) ?d)
1425     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1426     (?E gnus-newsgroup-expunged-tally ?d)
1427     (?s (gnus-current-score-file-nondirectory) ?s)))
1428
1429 ;; This is here rather than in gnus-art for compilation reasons.
1430 (defvar gnus-article-mode-line-format-alist
1431   (nconc '((?w (gnus-article-wash-status) ?s)
1432            (?m (gnus-article-mime-part-status) ?s))
1433          gnus-summary-mode-line-format-alist))
1434
1435 (defvar gnus-last-search-regexp nil
1436   "Default regexp for article search command.")
1437
1438 (defvar gnus-last-shell-command nil
1439   "Default shell command on article.")
1440
1441 (defvar gnus-newsgroup-agentized nil
1442   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1443 (defvar gnus-newsgroup-begin nil)
1444 (defvar gnus-newsgroup-end nil)
1445 (defvar gnus-newsgroup-last-rmail nil)
1446 (defvar gnus-newsgroup-last-mail nil)
1447 (defvar gnus-newsgroup-last-folder nil)
1448 (defvar gnus-newsgroup-last-file nil)
1449 (defvar gnus-newsgroup-last-directory nil)
1450 (defvar gnus-newsgroup-auto-expire nil)
1451 (defvar gnus-newsgroup-active nil)
1452 (defvar gnus-newsgroup-highest nil)
1453
1454 (defvar gnus-newsgroup-data nil)
1455 (defvar gnus-newsgroup-data-reverse nil)
1456 (defvar gnus-newsgroup-limit nil)
1457 (defvar gnus-newsgroup-limits nil)
1458 (defvar gnus-summary-use-undownloaded-faces nil)
1459
1460 (defvar gnus-newsgroup-unreads nil
1461   "Sorted list of unread articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-unselected nil
1464   "Sorted list of unselected unread articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-reads nil
1467   "Alist of read articles and article marks in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-expunged-tally nil)
1470
1471 (defvar gnus-newsgroup-marked nil
1472   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1473
1474 (defvar gnus-newsgroup-spam-marked nil
1475   "List of ranges of articles that have been marked as spam.")
1476
1477 (defvar gnus-newsgroup-killed nil
1478   "List of ranges of articles that have been through the scoring process.")
1479
1480 (defvar gnus-newsgroup-cached nil
1481   "Sorted list of articles that come from the article cache.")
1482
1483 (defvar gnus-newsgroup-saved nil
1484   "List of articles that have been saved.")
1485
1486 (defvar gnus-newsgroup-kill-headers nil)
1487
1488 (defvar gnus-newsgroup-replied nil
1489   "List of articles that have been replied to in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-forwarded nil
1492   "List of articles that have been forwarded in the current newsgroup.")
1493
1494 (defvar gnus-newsgroup-recent nil
1495   "List of articles that have are recent in the current newsgroup.")
1496
1497 (defvar gnus-newsgroup-expirable nil
1498   "Sorted list of articles in the current newsgroup that can be expired.")
1499
1500 (defvar gnus-newsgroup-processable nil
1501   "List of articles in the current newsgroup that can be processed.")
1502
1503 (defvar gnus-newsgroup-downloadable nil
1504   "Sorted list of articles in the current newsgroup that can be processed.")
1505
1506 (defvar gnus-newsgroup-unfetched nil
1507   "Sorted list of articles in the current newsgroup whose headers have
1508 not been fetched into the agent.
1509
1510 This list will always be a subset of gnus-newsgroup-undownloaded.")
1511
1512 (defvar gnus-newsgroup-undownloaded nil
1513   "List of articles in the current newsgroup that haven't been downloaded.")
1514
1515 (defvar gnus-newsgroup-unsendable nil
1516   "List of articles in the current newsgroup that won't be sent.")
1517
1518 (defvar gnus-newsgroup-bookmarks nil
1519   "List of articles in the current newsgroup that have bookmarks.")
1520
1521 (defvar gnus-newsgroup-dormant nil
1522   "Sorted list of dormant articles in the current newsgroup.")
1523
1524 (defvar gnus-newsgroup-unseen nil
1525   "List of unseen articles in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-seen nil
1528   "Range of seen articles in the current newsgroup.")
1529
1530 (defvar gnus-newsgroup-articles nil
1531   "List of articles in the current newsgroup.")
1532
1533 (defvar gnus-newsgroup-scored nil
1534   "List of scored articles in the current newsgroup.")
1535
1536 (defvar gnus-newsgroup-headers nil
1537   "List of article headers in the current newsgroup.")
1538
1539 (defvar gnus-newsgroup-threads nil)
1540
1541 (defvar gnus-newsgroup-prepared nil
1542   "Whether the current group has been prepared properly.")
1543
1544 (defvar gnus-newsgroup-ancient nil
1545   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1546
1547 (defvar gnus-newsgroup-sparse nil)
1548
1549 (defvar gnus-current-article nil)
1550 (defvar gnus-article-current nil)
1551 (defvar gnus-current-headers nil)
1552 (defvar gnus-have-all-headers nil)
1553 (defvar gnus-last-article nil)
1554 (defvar gnus-newsgroup-history nil)
1555 (defvar gnus-newsgroup-charset nil)
1556 (defvar gnus-newsgroup-ephemeral-charset nil)
1557 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1558
1559 (defvar gnus-article-before-search nil)
1560
1561 (defvar gnus-summary-local-variables
1562   '(gnus-newsgroup-name
1563
1564     ;; Marks lists
1565     gnus-newsgroup-unreads
1566     gnus-newsgroup-unselected
1567     gnus-newsgroup-marked
1568     gnus-newsgroup-spam-marked
1569     gnus-newsgroup-reads
1570     gnus-newsgroup-saved
1571     gnus-newsgroup-replied
1572     gnus-newsgroup-forwarded
1573     gnus-newsgroup-recent
1574     gnus-newsgroup-expirable
1575     gnus-newsgroup-killed
1576     gnus-newsgroup-unseen
1577     gnus-newsgroup-seen
1578     gnus-newsgroup-cached
1579     gnus-newsgroup-downloadable
1580     gnus-newsgroup-undownloaded
1581     gnus-newsgroup-unsendable
1582
1583     gnus-newsgroup-begin gnus-newsgroup-end
1584     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1585     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1586     gnus-newsgroup-last-directory
1587     gnus-newsgroup-auto-expire
1588     gnus-newsgroup-processable
1589     gnus-newsgroup-unfetched
1590     gnus-newsgroup-articles
1591     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1592     gnus-newsgroup-headers gnus-newsgroup-threads
1593     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1594     gnus-current-article gnus-current-headers gnus-have-all-headers
1595     gnus-last-article gnus-article-internal-prepare-hook
1596     (gnus-summary-article-delete-hook . global)
1597     (gnus-summary-article-move-hook . global)
1598     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1599     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1600     gnus-thread-expunge-below
1601     gnus-score-alist gnus-current-score-file
1602     (gnus-summary-expunge-below . global)
1603     (gnus-summary-mark-below . global)
1604     (gnus-orphan-score . global)
1605     gnus-newsgroup-active gnus-scores-exclude-files
1606     gnus-newsgroup-highest
1607     gnus-newsgroup-history gnus-newsgroup-ancient
1608     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1609     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1610     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1611     (gnus-newsgroup-expunged-tally . 0)
1612     gnus-cache-removable-articles
1613     gnus-newsgroup-data gnus-newsgroup-data-reverse
1614     gnus-newsgroup-limit gnus-newsgroup-limits
1615     gnus-newsgroup-charset gnus-newsgroup-display
1616     gnus-summary-use-undownloaded-faces)
1617   "Variables that are buffer-local to the summary buffers.")
1618
1619 (defvar gnus-newsgroup-variables nil
1620   "A list of variables that have separate values in different newsgroups.
1621 A list of newsgroup (summary buffer) local variables, or cons of
1622 variables and their default expressions to be evalled (when the default
1623 values are not nil), that should be made global while the summary buffer
1624 is active.
1625
1626 Note: The default expressions will be evaluated (using function `eval')
1627 before assignment to the local variable rather than just assigned to it.
1628 If the default expression is the symbol `global', that symbol will not
1629 be evaluated but the global value of the local variable will be used
1630 instead.
1631
1632 These variables can be used to set variables in the group parameters
1633 while still allowing them to affect operations done in other buffers.
1634 For example:
1635
1636 \(setq gnus-newsgroup-variables
1637      '(message-use-followup-to
1638        (gnus-visible-headers .
1639          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1640 ")
1641
1642 (eval-when-compile
1643   ;; Bind features so that require will believe that gnus-sum has
1644   ;; already been loaded (avoids infinite recursion)
1645   (let ((features (cons 'gnus-sum features)))
1646     (require 'gnus-art)))
1647
1648 ;; MIME stuff.
1649
1650 (defvar gnus-decode-encoded-word-methods
1651   '(mail-decode-encoded-word-string)
1652   "List of methods used to decode encoded words.
1653
1654 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1655 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1656 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1657 whose names match REGEXP.
1658
1659 For example:
1660 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1661  mail-decode-encoded-word-string
1662  (\"chinese\" . rfc1843-decode-string))")
1663
1664 (defvar gnus-decode-encoded-word-methods-cache nil)
1665
1666 (defun gnus-multi-decode-encoded-word-string (string)
1667   "Apply the functions from `gnus-encoded-word-methods' that match."
1668   (unless (and gnus-decode-encoded-word-methods-cache
1669                (eq gnus-newsgroup-name
1670                    (car gnus-decode-encoded-word-methods-cache)))
1671     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1672     (dolist (method gnus-decode-encoded-word-methods)
1673       (if (symbolp method)
1674           (nconc gnus-decode-encoded-word-methods-cache (list method))
1675         (if (and gnus-newsgroup-name
1676                  (string-match (car method) gnus-newsgroup-name))
1677             (nconc gnus-decode-encoded-word-methods-cache
1678                    (list (cdr method)))))))
1679   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1680     (setq string (funcall method string))))
1681
1682 ;; Subject simplification.
1683
1684 (defun gnus-simplify-whitespace (str)
1685   "Remove excessive whitespace from STR."
1686   ;; Multiple spaces.
1687   (while (string-match "[ \t][ \t]+" str)
1688     (setq str (concat (substring str 0 (match-beginning 0))
1689                         " "
1690                         (substring str (match-end 0)))))
1691   ;; Leading spaces.
1692   (when (string-match "^[ \t]+" str)
1693     (setq str (substring str (match-end 0))))
1694   ;; Trailing spaces.
1695   (when (string-match "[ \t]+$" str)
1696     (setq str (substring str 0 (match-beginning 0))))
1697   str)
1698
1699 (defun gnus-simplify-all-whitespace (str)
1700   "Remove all whitespace from STR."
1701   (while (string-match "[ \t\n]+" str)
1702     (setq str (replace-match "" nil nil str)))
1703   str)
1704
1705 (defsubst gnus-simplify-subject-re (subject)
1706   "Remove \"Re:\" from subject lines."
1707   (if (string-match message-subject-re-regexp subject)
1708       (substring subject (match-end 0))
1709     subject))
1710
1711 (defun gnus-simplify-subject (subject &optional re-only)
1712   "Remove `Re:' and words in parentheses.
1713 If RE-ONLY is non-nil, strip leading `Re:'s only."
1714   (let ((case-fold-search t))           ;Ignore case.
1715     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1716     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1717       (setq subject (substring subject (match-end 0))))
1718     ;; Remove uninteresting prefixes.
1719     (when (and (not re-only)
1720                gnus-simplify-ignored-prefixes
1721                (string-match gnus-simplify-ignored-prefixes subject))
1722       (setq subject (substring subject (match-end 0))))
1723     ;; Remove words in parentheses from end.
1724     (unless re-only
1725       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1726         (setq subject (substring subject 0 (match-beginning 0)))))
1727     ;; Return subject string.
1728     subject))
1729
1730 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1731 ;; all whitespace.
1732 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1733   (goto-char (point-min))
1734   (while (re-search-forward regexp nil t)
1735     (replace-match (or newtext ""))))
1736
1737 (defun gnus-simplify-buffer-fuzzy ()
1738   "Simplify string in the buffer fuzzily.
1739 The string in the accessible portion of the current buffer is simplified.
1740 It is assumed to be a single-line subject.
1741 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1742 matter is removed.  Additional things can be deleted by setting
1743 `gnus-simplify-subject-fuzzy-regexp'."
1744   (let ((case-fold-search t)
1745         (modified-tick))
1746     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1747
1748     (while (not (eq modified-tick (buffer-modified-tick)))
1749       (setq modified-tick (buffer-modified-tick))
1750       (cond
1751        ((listp gnus-simplify-subject-fuzzy-regexp)
1752         (mapc 'gnus-simplify-buffer-fuzzy-step
1753               gnus-simplify-subject-fuzzy-regexp))
1754        (gnus-simplify-subject-fuzzy-regexp
1755         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1756       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1757       (gnus-simplify-buffer-fuzzy-step
1758        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1759       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1760
1761     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1762     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1763     (gnus-simplify-buffer-fuzzy-step " $")
1764     (gnus-simplify-buffer-fuzzy-step "^ +")))
1765
1766 (defun gnus-simplify-subject-fuzzy (subject)
1767   "Simplify a subject string fuzzily.
1768 See `gnus-simplify-buffer-fuzzy' for details."
1769   (save-excursion
1770     (gnus-set-work-buffer)
1771     (let ((case-fold-search t))
1772       ;; Remove uninteresting prefixes.
1773       (when (and gnus-simplify-ignored-prefixes
1774                  (string-match gnus-simplify-ignored-prefixes subject))
1775         (setq subject (substring subject (match-end 0))))
1776       (insert subject)
1777       (inline (gnus-simplify-buffer-fuzzy))
1778       (buffer-string))))
1779
1780 (defsubst gnus-simplify-subject-fully (subject)
1781   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1782   (cond
1783    (gnus-simplify-subject-functions
1784     (gnus-map-function gnus-simplify-subject-functions subject))
1785    ((null gnus-summary-gather-subject-limit)
1786     (gnus-simplify-subject-re subject))
1787    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1788     (gnus-simplify-subject-fuzzy subject))
1789    ((numberp gnus-summary-gather-subject-limit)
1790     (truncate-string-to-width (gnus-simplify-subject-re subject)
1791                               gnus-summary-gather-subject-limit))
1792    (t
1793     subject)))
1794
1795 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1796   "Check whether two subjects are equal.
1797 If optional argument SIMPLE-FIRST is t, first argument is already
1798 simplified."
1799   (cond
1800    ((null simple-first)
1801     (equal (gnus-simplify-subject-fully s1)
1802            (gnus-simplify-subject-fully s2)))
1803    (t
1804     (equal s1
1805            (gnus-simplify-subject-fully s2)))))
1806
1807 (defun gnus-summary-bubble-group ()
1808   "Increase the score of the current group.
1809 This is a handy function to add to `gnus-summary-exit-hook' to
1810 increase the score of each group you read."
1811   (gnus-group-add-score gnus-newsgroup-name))
1812
1813 \f
1814 ;;;
1815 ;;; Gnus summary mode
1816 ;;;
1817
1818 (put 'gnus-summary-mode 'mode-class 'special)
1819
1820 (defvar gnus-article-commands-menu)
1821
1822 ;; Non-orthogonal keys
1823
1824 (gnus-define-keys gnus-summary-mode-map
1825   " " gnus-summary-next-page
1826   "\177" gnus-summary-prev-page
1827   [delete] gnus-summary-prev-page
1828   [backspace] gnus-summary-prev-page
1829   "\r" gnus-summary-scroll-up
1830   "\M-\r" gnus-summary-scroll-down
1831   "n" gnus-summary-next-unread-article
1832   "p" gnus-summary-prev-unread-article
1833   "N" gnus-summary-next-article
1834   "P" gnus-summary-prev-article
1835   "\M-\C-n" gnus-summary-next-same-subject
1836   "\M-\C-p" gnus-summary-prev-same-subject
1837   "\M-n" gnus-summary-next-unread-subject
1838   "\M-p" gnus-summary-prev-unread-subject
1839   "." gnus-summary-first-unread-article
1840   "," gnus-summary-best-unread-article
1841   "\M-s" gnus-summary-search-article-forward
1842   "\M-r" gnus-summary-search-article-backward
1843   "\M-S" gnus-summary-repeat-search-article-forward
1844   "\M-R" gnus-summary-repeat-search-article-backward
1845   "<" gnus-summary-beginning-of-article
1846   ">" gnus-summary-end-of-article
1847   "j" gnus-summary-goto-article
1848   "^" gnus-summary-refer-parent-article
1849   "\M-^" gnus-summary-refer-article
1850   "u" gnus-summary-tick-article-forward
1851   "!" gnus-summary-tick-article-forward
1852   "U" gnus-summary-tick-article-backward
1853   "d" gnus-summary-mark-as-read-forward
1854   "D" gnus-summary-mark-as-read-backward
1855   "E" gnus-summary-mark-as-expirable
1856   "\M-u" gnus-summary-clear-mark-forward
1857   "\M-U" gnus-summary-clear-mark-backward
1858   "k" gnus-summary-kill-same-subject-and-select
1859   "\C-k" gnus-summary-kill-same-subject
1860   "\M-\C-k" gnus-summary-kill-thread
1861   "\M-\C-l" gnus-summary-lower-thread
1862   "e" gnus-summary-edit-article
1863   "#" gnus-summary-mark-as-processable
1864   "\M-#" gnus-summary-unmark-as-processable
1865   "\M-\C-t" gnus-summary-toggle-threads
1866   "\M-\C-s" gnus-summary-show-thread
1867   "\M-\C-h" gnus-summary-hide-thread
1868   "\M-\C-f" gnus-summary-next-thread
1869   "\M-\C-b" gnus-summary-prev-thread
1870   [(meta down)] gnus-summary-next-thread
1871   [(meta up)] gnus-summary-prev-thread
1872   "\M-\C-u" gnus-summary-up-thread
1873   "\M-\C-d" gnus-summary-down-thread
1874   "&" gnus-summary-execute-command
1875   "c" gnus-summary-catchup-and-exit
1876   "\C-w" gnus-summary-mark-region-as-read
1877   "\C-t" gnus-summary-toggle-truncation
1878   "?" gnus-summary-mark-as-dormant
1879   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1880   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1881   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1882   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1883   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1884   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1885   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1886   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1887   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1888   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1889   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1890   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1891   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1892   "=" gnus-summary-expand-window
1893   "\C-x\C-s" gnus-summary-reselect-current-group
1894   "\M-g" gnus-summary-rescan-group
1895   "\C-c\C-r" gnus-summary-caesar-message
1896   "f" gnus-summary-followup
1897   "F" gnus-summary-followup-with-original
1898   "C" gnus-summary-cancel-article
1899   "r" gnus-summary-reply
1900   "R" gnus-summary-reply-with-original
1901   "\C-c\C-f" gnus-summary-mail-forward
1902   "o" gnus-summary-save-article
1903   "\C-o" gnus-summary-save-article-mail
1904   "|" gnus-summary-pipe-output
1905   "\M-k" gnus-summary-edit-local-kill
1906   "\M-K" gnus-summary-edit-global-kill
1907   ;; "V" gnus-version
1908   "\C-c\C-d" gnus-summary-describe-group
1909   "q" gnus-summary-exit
1910   "Q" gnus-summary-exit-no-update
1911   "\C-c\C-i" gnus-info-find-node
1912   gnus-mouse-2 gnus-mouse-pick-article
1913   [follow-link] mouse-face
1914   "m" gnus-summary-mail-other-window
1915   "a" gnus-summary-post-news
1916   "x" gnus-summary-limit-to-unread
1917   "s" gnus-summary-isearch-article
1918   [tab] gnus-summary-widget-forward
1919   "t" gnus-summary-toggle-header
1920   "g" gnus-summary-show-article
1921   "l" gnus-summary-goto-last-article
1922   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1923   "\C-d" gnus-summary-enter-digest-group
1924   "\M-\C-d" gnus-summary-read-document
1925   "\M-\C-e" gnus-summary-edit-parameters
1926   "\M-\C-a" gnus-summary-customize-parameters
1927   "\C-c\C-b" gnus-bug
1928   "*" gnus-cache-enter-article
1929   "\M-*" gnus-cache-remove-article
1930   "\M-&" gnus-summary-universal-argument
1931   "\C-l" gnus-recenter
1932   "I" gnus-summary-increase-score
1933   "L" gnus-summary-lower-score
1934   "\M-i" gnus-symbolic-argument
1935   "h" gnus-summary-select-article-buffer
1936
1937   "b" gnus-article-view-part
1938   "\M-t" gnus-summary-toggle-display-buttonized
1939
1940   "V" gnus-summary-score-map
1941   "X" gnus-uu-extract-map
1942   "S" gnus-summary-send-map)
1943
1944 ;; Sort of orthogonal keymap
1945 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1946   "t" gnus-summary-tick-article-forward
1947   "!" gnus-summary-tick-article-forward
1948   "d" gnus-summary-mark-as-read-forward
1949   "r" gnus-summary-mark-as-read-forward
1950   "c" gnus-summary-clear-mark-forward
1951   " " gnus-summary-clear-mark-forward
1952   "e" gnus-summary-mark-as-expirable
1953   "x" gnus-summary-mark-as-expirable
1954   "?" gnus-summary-mark-as-dormant
1955   "b" gnus-summary-set-bookmark
1956   "B" gnus-summary-remove-bookmark
1957   "#" gnus-summary-mark-as-processable
1958   "\M-#" gnus-summary-unmark-as-processable
1959   "S" gnus-summary-limit-include-expunged
1960   "C" gnus-summary-catchup
1961   "H" gnus-summary-catchup-to-here
1962   "h" gnus-summary-catchup-from-here
1963   "\C-c" gnus-summary-catchup-all
1964   "k" gnus-summary-kill-same-subject-and-select
1965   "K" gnus-summary-kill-same-subject
1966   "P" gnus-uu-mark-map)
1967
1968 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1969   "c" gnus-summary-clear-above
1970   "u" gnus-summary-tick-above
1971   "m" gnus-summary-mark-above
1972   "k" gnus-summary-kill-below)
1973
1974 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1975   "/" gnus-summary-limit-to-subject
1976   "n" gnus-summary-limit-to-articles
1977   "b" gnus-summary-limit-to-bodies
1978   "h" gnus-summary-limit-to-headers
1979   "w" gnus-summary-pop-limit
1980   "s" gnus-summary-limit-to-subject
1981   "a" gnus-summary-limit-to-author
1982   "u" gnus-summary-limit-to-unread
1983   "m" gnus-summary-limit-to-marks
1984   "M" gnus-summary-limit-exclude-marks
1985   "v" gnus-summary-limit-to-score
1986   "*" gnus-summary-limit-include-cached
1987   "D" gnus-summary-limit-include-dormant
1988   "T" gnus-summary-limit-include-thread
1989   "d" gnus-summary-limit-exclude-dormant
1990   "t" gnus-summary-limit-to-age
1991   "." gnus-summary-limit-to-unseen
1992   "x" gnus-summary-limit-to-extra
1993   "p" gnus-summary-limit-to-display-predicate
1994   "E" gnus-summary-limit-include-expunged
1995   "c" gnus-summary-limit-exclude-childless-dormant
1996   "C" gnus-summary-limit-mark-excluded-as-read
1997   "o" gnus-summary-insert-old-articles
1998   "N" gnus-summary-insert-new-articles
1999   "S" gnus-summary-limit-to-singletons
2000   "r" gnus-summary-limit-to-replied
2001   "R" gnus-summary-limit-to-recipient
2002   "A" gnus-summary-limit-to-address)
2003
2004 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2005   "n" gnus-summary-next-unread-article
2006   "p" gnus-summary-prev-unread-article
2007   "N" gnus-summary-next-article
2008   "P" gnus-summary-prev-article
2009   "\C-n" gnus-summary-next-same-subject
2010   "\C-p" gnus-summary-prev-same-subject
2011   "\M-n" gnus-summary-next-unread-subject
2012   "\M-p" gnus-summary-prev-unread-subject
2013   "f" gnus-summary-first-unread-article
2014   "b" gnus-summary-best-unread-article
2015   "j" gnus-summary-goto-article
2016   "g" gnus-summary-goto-subject
2017   "l" gnus-summary-goto-last-article
2018   "o" gnus-summary-pop-article)
2019
2020 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2021   "k" gnus-summary-kill-thread
2022   "E" gnus-summary-expire-thread
2023   "l" gnus-summary-lower-thread
2024   "i" gnus-summary-raise-thread
2025   "T" gnus-summary-toggle-threads
2026   "t" gnus-summary-rethread-current
2027   "^" gnus-summary-reparent-thread
2028   "\M-^" gnus-summary-reparent-children
2029   "s" gnus-summary-show-thread
2030   "S" gnus-summary-show-all-threads
2031   "h" gnus-summary-hide-thread
2032   "H" gnus-summary-hide-all-threads
2033   "n" gnus-summary-next-thread
2034   "p" gnus-summary-prev-thread
2035   "u" gnus-summary-up-thread
2036   "o" gnus-summary-top-thread
2037   "d" gnus-summary-down-thread
2038   "#" gnus-uu-mark-thread
2039   "\M-#" gnus-uu-unmark-thread)
2040
2041 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2042   "g" gnus-summary-prepare
2043   "c" gnus-summary-insert-cached-articles
2044   "d" gnus-summary-insert-dormant-articles
2045   "t" gnus-summary-insert-ticked-articles)
2046
2047 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2048   "c" gnus-summary-catchup-and-exit
2049   "C" gnus-summary-catchup-all-and-exit
2050   "E" gnus-summary-exit-no-update
2051   "Q" gnus-summary-exit
2052   "Z" gnus-summary-exit
2053   "n" gnus-summary-catchup-and-goto-next-group
2054   "p" gnus-summary-catchup-and-goto-prev-group
2055   "R" gnus-summary-reselect-current-group
2056   "G" gnus-summary-rescan-group
2057   "N" gnus-summary-next-group
2058   "s" gnus-summary-save-newsrc
2059   "P" gnus-summary-prev-group)
2060
2061 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2062   " " gnus-summary-next-page
2063   "n" gnus-summary-next-page
2064   "\177" gnus-summary-prev-page
2065   [delete] gnus-summary-prev-page
2066   "p" gnus-summary-prev-page
2067   "\r" gnus-summary-scroll-up
2068   "\M-\r" gnus-summary-scroll-down
2069   "<" gnus-summary-beginning-of-article
2070   ">" gnus-summary-end-of-article
2071   "b" gnus-summary-beginning-of-article
2072   "e" gnus-summary-end-of-article
2073   "^" gnus-summary-refer-parent-article
2074   "r" gnus-summary-refer-parent-article
2075   "C" gnus-summary-show-complete-article
2076   "D" gnus-summary-enter-digest-group
2077   "R" gnus-summary-refer-references
2078   "T" gnus-summary-refer-thread
2079   "W" gnus-warp-to-article
2080   "g" gnus-summary-show-article
2081   "s" gnus-summary-isearch-article
2082   [tab] gnus-summary-widget-forward
2083   "P" gnus-summary-print-article
2084   "S" gnus-sticky-article
2085   "M" gnus-mailing-list-insinuate
2086   "t" gnus-article-babel)
2087
2088 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2089   "b" gnus-article-add-buttons
2090   "B" gnus-article-add-buttons-to-head
2091   "o" gnus-article-treat-overstrike
2092   "e" gnus-article-emphasize
2093   "w" gnus-article-fill-cited-article
2094   "Q" gnus-article-fill-long-lines
2095   "L" gnus-article-toggle-truncate-lines
2096   "C" gnus-article-capitalize-sentences
2097   "c" gnus-article-remove-cr
2098   "q" gnus-article-de-quoted-unreadable
2099   "6" gnus-article-de-base64-unreadable
2100   "Z" gnus-article-decode-HZ
2101   "A" gnus-article-treat-ansi-sequences
2102   "h" gnus-article-wash-html
2103   "u" gnus-article-unsplit-urls
2104   "s" gnus-summary-force-verify-and-decrypt
2105   "f" gnus-article-display-x-face
2106   "l" gnus-summary-stop-page-breaking
2107   "r" gnus-summary-caesar-message
2108   "m" gnus-summary-morse-message
2109   "t" gnus-summary-toggle-header
2110   "g" gnus-treat-smiley
2111   "v" gnus-summary-verbose-headers
2112   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2113   "p" gnus-article-verify-x-pgp-sig
2114   "d" gnus-article-treat-dumbquotes
2115   "U" gnus-article-treat-non-ascii
2116   "i" gnus-summary-idna-message)
2117
2118 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2119   ;; mnemonic: deuglif*Y*
2120   "u" gnus-article-outlook-unwrap-lines
2121   "a" gnus-article-outlook-repair-attribution
2122   "c" gnus-article-outlook-rearrange-citation
2123   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2124
2125 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2126   "a" gnus-article-hide
2127   "h" gnus-article-hide-headers
2128   "b" gnus-article-hide-boring-headers
2129   "s" gnus-article-hide-signature
2130   "c" gnus-article-hide-citation
2131   "C" gnus-article-hide-citation-in-followups
2132   "l" gnus-article-hide-list-identifiers
2133   "B" gnus-article-strip-banner
2134   "P" gnus-article-hide-pem
2135   "\C-c" gnus-article-hide-citation-maybe)
2136
2137 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2138   "a" gnus-article-highlight
2139   "h" gnus-article-highlight-headers
2140   "c" gnus-article-highlight-citation
2141   "s" gnus-article-highlight-signature)
2142
2143 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2144   "f" gnus-article-treat-fold-headers
2145   "u" gnus-article-treat-unfold-headers
2146   "n" gnus-article-treat-fold-newsgroups)
2147
2148 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2149   "x" gnus-article-display-x-face
2150   "d" gnus-article-display-face
2151   "s" gnus-treat-smiley
2152   "D" gnus-article-remove-images
2153   "W" gnus-article-show-images
2154   "f" gnus-treat-from-picon
2155   "m" gnus-treat-mail-picon
2156   "n" gnus-treat-newsgroups-picon
2157   "g" gnus-treat-from-gravatar
2158   "h" gnus-treat-mail-gravatar)
2159
2160 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2161   "w" gnus-article-decode-mime-words
2162   "c" gnus-article-decode-charset
2163   "v" gnus-mime-view-all-parts
2164   "b" gnus-article-view-part)
2165
2166 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2167   "z" gnus-article-date-ut
2168   "u" gnus-article-date-ut
2169   "l" gnus-article-date-local
2170   "p" gnus-article-date-english
2171   "e" gnus-article-date-lapsed
2172   "o" gnus-article-date-original
2173   "i" gnus-article-date-iso8601
2174   "s" gnus-article-date-user)
2175
2176 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2177   "t" gnus-article-remove-trailing-blank-lines
2178   "l" gnus-article-strip-leading-blank-lines
2179   "m" gnus-article-strip-multiple-blank-lines
2180   "a" gnus-article-strip-blank-lines
2181   "A" gnus-article-strip-all-blank-lines
2182   "s" gnus-article-strip-leading-space
2183   "e" gnus-article-strip-trailing-space
2184   "w" gnus-article-remove-leading-whitespace)
2185
2186 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2187   "v" gnus-version
2188   "d" gnus-summary-describe-group
2189   "h" gnus-summary-describe-briefly
2190   "i" gnus-info-find-node)
2191
2192 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2193   "e" gnus-summary-expire-articles
2194   "\M-\C-e" gnus-summary-expire-articles-now
2195   "\177" gnus-summary-delete-article
2196   [delete] gnus-summary-delete-article
2197   [backspace] gnus-summary-delete-article
2198   "m" gnus-summary-move-article
2199   "r" gnus-summary-respool-article
2200   "w" gnus-summary-edit-article
2201   "c" gnus-summary-copy-article
2202   "B" gnus-summary-crosspost-article
2203   "q" gnus-summary-respool-query
2204   "t" gnus-summary-respool-trace
2205   "i" gnus-summary-import-article
2206   "I" gnus-summary-create-article
2207   "p" gnus-summary-article-posted-p)
2208
2209 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2210   "o" gnus-summary-save-article
2211   "m" gnus-summary-save-article-mail
2212   "F" gnus-summary-write-article-file
2213   "r" gnus-summary-save-article-rmail
2214   "f" gnus-summary-save-article-file
2215   "b" gnus-summary-save-article-body-file
2216   "B" gnus-summary-write-article-body-file
2217   "h" gnus-summary-save-article-folder
2218   "v" gnus-summary-save-article-vm
2219   "p" gnus-summary-pipe-output
2220   "P" gnus-summary-muttprint)
2221
2222 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2223   "b" gnus-summary-display-buttonized
2224   "m" gnus-summary-repair-multipart
2225   "v" gnus-article-view-part
2226   "o" gnus-article-save-part
2227   "O" gnus-article-save-part-and-strip
2228   "r" gnus-article-replace-part
2229   "d" gnus-article-delete-part
2230   "t" gnus-article-view-part-as-type
2231   "j" gnus-article-jump-to-part
2232   "c" gnus-article-copy-part
2233   "C" gnus-article-view-part-as-charset
2234   "e" gnus-article-view-part-externally
2235   "H" gnus-article-browse-html-article
2236   "E" gnus-article-encrypt-body
2237   "i" gnus-article-inline-part
2238   "|" gnus-article-pipe-part)
2239
2240 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2241   "p" gnus-summary-mark-as-processable
2242   "u" gnus-summary-unmark-as-processable
2243   "U" gnus-summary-unmark-all-processable
2244   "v" gnus-uu-mark-over
2245   "s" gnus-uu-mark-series
2246   "r" gnus-uu-mark-region
2247   "g" gnus-uu-unmark-region
2248   "R" gnus-uu-mark-by-regexp
2249   "G" gnus-uu-unmark-by-regexp
2250   "t" gnus-uu-mark-thread
2251   "T" gnus-uu-unmark-thread
2252   "a" gnus-uu-mark-all
2253   "b" gnus-uu-mark-buffer
2254   "S" gnus-uu-mark-sparse
2255   "k" gnus-summary-kill-process-mark
2256   "y" gnus-summary-yank-process-mark
2257   "w" gnus-summary-save-process-mark
2258   "i" gnus-uu-invert-processable)
2259
2260 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2261   ;;"x" gnus-uu-extract-any
2262   "m" gnus-summary-save-parts
2263   "u" gnus-uu-decode-uu
2264   "U" gnus-uu-decode-uu-and-save
2265   "s" gnus-uu-decode-unshar
2266   "S" gnus-uu-decode-unshar-and-save
2267   "o" gnus-uu-decode-save
2268   "O" gnus-uu-decode-save
2269   "b" gnus-uu-decode-binhex
2270   "B" gnus-uu-decode-binhex
2271   "Y" gnus-uu-decode-yenc
2272   "p" gnus-uu-decode-postscript
2273   "P" gnus-uu-decode-postscript-and-save)
2274
2275 (gnus-define-keys
2276     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2277   "u" gnus-uu-decode-uu-view
2278   "U" gnus-uu-decode-uu-and-save-view
2279   "s" gnus-uu-decode-unshar-view
2280   "S" gnus-uu-decode-unshar-and-save-view
2281   "o" gnus-uu-decode-save-view
2282   "O" gnus-uu-decode-save-view
2283   "b" gnus-uu-decode-binhex-view
2284   "B" gnus-uu-decode-binhex-view
2285   "p" gnus-uu-decode-postscript-view
2286   "P" gnus-uu-decode-postscript-and-save-view)
2287
2288 (defvar gnus-article-post-menu nil)
2289
2290 (defconst gnus-summary-menu-maxlen 20)
2291
2292 (defun gnus-summary-menu-split (menu)
2293   ;; If we have lots of elements, divide them into groups of 20
2294   ;; and make a pane (or submenu) for each one.
2295   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2296       (let ((menu menu) sublists next
2297             (i 1))
2298         (while menu
2299           ;; Pull off the next gnus-summary-menu-maxlen elements
2300           ;; and make them the next element of sublist.
2301           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2302           (if next
2303               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2304                       nil))
2305           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2306                                              (aref (car (last menu)) 0)) menu)
2307                                sublists))
2308           (setq i (1+ i))
2309           (setq menu next))
2310         (nreverse sublists))
2311     ;; Few elements--put them all in one pane.
2312     menu))
2313
2314 (defun gnus-summary-make-menu-bar ()
2315   (gnus-turn-off-edit-menu 'summary)
2316
2317   (unless (boundp 'gnus-summary-misc-menu)
2318
2319     (easy-menu-define
2320       gnus-summary-kill-menu gnus-summary-mode-map ""
2321       (cons
2322        "Score"
2323        (nconc
2324         (list
2325          ["Customize" gnus-score-customize t])
2326         (gnus-make-score-map 'increase)
2327         (gnus-make-score-map 'lower)
2328         '(("Mark"
2329            ["Kill below" gnus-summary-kill-below t]
2330            ["Mark above" gnus-summary-mark-above t]
2331            ["Tick above" gnus-summary-tick-above t]
2332            ["Clear above" gnus-summary-clear-above t])
2333           ["Current score" gnus-summary-current-score t]
2334           ["Set score" gnus-summary-set-score t]
2335           ["Switch current score file..." gnus-score-change-score-file t]
2336           ["Set mark below..." gnus-score-set-mark-below t]
2337           ["Set expunge below..." gnus-score-set-expunge-below t]
2338           ["Edit current score file" gnus-score-edit-current-scores t]
2339           ["Edit score file..." gnus-score-edit-file t]
2340           ["Trace score" gnus-score-find-trace t]
2341           ["Find words" gnus-score-find-favourite-words t]
2342           ["Rescore buffer" gnus-summary-rescore t]
2343           ["Increase score..." gnus-summary-increase-score t]
2344           ["Lower score..." gnus-summary-lower-score t]))))
2345
2346     ;; Define both the Article menu in the summary buffer and the
2347     ;; equivalent Commands menu in the article buffer here for
2348     ;; consistency.
2349     (let ((innards
2350            `(("Hide"
2351               ["All" gnus-article-hide t]
2352               ["Headers" gnus-article-hide-headers t]
2353               ["Signature" gnus-article-hide-signature t]
2354               ["Citation" gnus-article-hide-citation t]
2355               ["List identifiers" gnus-article-hide-list-identifiers t]
2356               ["Banner" gnus-article-strip-banner t]
2357               ["Boring headers" gnus-article-hide-boring-headers t])
2358              ("Highlight"
2359               ["All" gnus-article-highlight t]
2360               ["Headers" gnus-article-highlight-headers t]
2361               ["Signature" gnus-article-highlight-signature t]
2362               ["Citation" gnus-article-highlight-citation t])
2363              ("MIME"
2364               ["Words" gnus-article-decode-mime-words t]
2365               ["Charset" gnus-article-decode-charset t]
2366               ["QP" gnus-article-de-quoted-unreadable t]
2367               ["Base64" gnus-article-de-base64-unreadable t]
2368               ["View MIME buttons" gnus-summary-display-buttonized t]
2369               ["View all" gnus-mime-view-all-parts t]
2370               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2371               ["Encrypt body" gnus-article-encrypt-body
2372                :active (not (gnus-group-read-only-p))
2373                ,@(if (featurep 'xemacs) nil
2374                    '(:help "Encrypt the message body on disk"))]
2375               ["Extract all parts..." gnus-summary-save-parts t]
2376               ("Multipart"
2377                ["Repair multipart" gnus-summary-repair-multipart t]
2378                ["Pipe part..." gnus-article-pipe-part t]
2379                ["Inline part" gnus-article-inline-part t]
2380                ["View part as type..." gnus-article-view-part-as-type t]
2381                ["Encrypt body" gnus-article-encrypt-body
2382                 :active (not (gnus-group-read-only-p))
2383                ,@(if (featurep 'xemacs) nil
2384                    '(:help "Encrypt the message body on disk"))]
2385                ["View part externally" gnus-article-view-part-externally t]
2386                ["View HTML parts in browser" gnus-article-browse-html-article t]
2387                ["View part with charset..." gnus-article-view-part-as-charset t]
2388                ["Copy part" gnus-article-copy-part t]
2389                ["Save part..." gnus-article-save-part t]
2390                ["View part" gnus-article-view-part t]))
2391              ("Date"
2392               ["Local" gnus-article-date-local t]
2393               ["ISO8601" gnus-article-date-iso8601 t]
2394               ["UT" gnus-article-date-ut t]
2395               ["Original" gnus-article-date-original t]
2396               ["Lapsed" gnus-article-date-lapsed t]
2397               ["User-defined" gnus-article-date-user t])
2398              ("Display"
2399               ["Remove images" gnus-article-remove-images t]
2400               ["Toggle smiley" gnus-treat-smiley t]
2401               ["Show X-Face" gnus-article-display-x-face t]
2402               ["Show picons in From" gnus-treat-from-picon t]
2403               ["Show picons in mail headers" gnus-treat-mail-picon t]
2404               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2405               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2406               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2407               ("View as different encoding"
2408                ,@(gnus-summary-menu-split
2409                   (mapcar
2410                    (lambda (cs)
2411                      ;; Since easymenu under Emacs doesn't allow
2412                      ;; lambda forms for menu commands, we should
2413                      ;; provide intern'ed function symbols.
2414                      (let ((command (intern (format "\
2415 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2416                        (fset command
2417                              `(lambda ()
2418                                 (interactive)
2419                                 (let ((gnus-summary-show-article-charset-alist
2420                                        '((1 . ,cs))))
2421                                   (gnus-summary-show-article 1))))
2422                        `[,(symbol-name cs) ,command t]))
2423                    (sort (if (fboundp 'coding-system-list)
2424                              (coding-system-list)
2425                            (mapcar 'car mm-mime-mule-charset-alist))
2426                          'string<)))))
2427              ("Washing"
2428               ("Remove Blanks"
2429                ["Leading" gnus-article-strip-leading-blank-lines t]
2430                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2431                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2432                ["All of the above" gnus-article-strip-blank-lines t]
2433                ["All" gnus-article-strip-all-blank-lines t]
2434                ["Leading space" gnus-article-strip-leading-space t]
2435                ["Trailing space" gnus-article-strip-trailing-space t]
2436                ["Leading space in headers"
2437                 gnus-article-remove-leading-whitespace t])
2438               ["Overstrike" gnus-article-treat-overstrike t]
2439               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2440               ["Non-ASCII" gnus-article-treat-non-ascii t]
2441               ["Emphasis" gnus-article-emphasize t]
2442               ["Word wrap" gnus-article-fill-cited-article t]
2443               ["Fill long lines" gnus-article-fill-long-lines t]
2444               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2445               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2446               ["Remove CR" gnus-article-remove-cr t]
2447               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2448               ["Base64" gnus-article-de-base64-unreadable t]
2449               ["Rot 13" gnus-summary-caesar-message
2450                ,@(if (featurep 'xemacs) '(t)
2451                    '(:help "\"Caesar rotate\" article by 13"))]
2452               ["De-IDNA" gnus-summary-idna-message t]
2453               ["Morse decode" gnus-summary-morse-message t]
2454               ["Unix pipe..." gnus-summary-pipe-message t]
2455               ["Add buttons" gnus-article-add-buttons t]
2456               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2457               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2458               ["Verbose header" gnus-summary-verbose-headers t]
2459               ["Toggle header" gnus-summary-toggle-header t]
2460               ["Unfold headers" gnus-article-treat-unfold-headers t]
2461               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2462               ["Html" gnus-article-wash-html t]
2463               ["Unsplit URLs" gnus-article-unsplit-urls t]
2464               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2465               ["Decode HZ" gnus-article-decode-HZ t]
2466               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2467               ("(Outlook) Deuglify"
2468                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2469                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2470                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2471                ["Full (Outlook) deuglify"
2472                 gnus-article-outlook-deuglify-article t])
2473               )
2474              ("Output"
2475               ["Save in default format..." gnus-summary-save-article
2476                ,@(if (featurep 'xemacs) '(t)
2477                    '(:help "Save article using default method"))]
2478               ["Save in file..." gnus-summary-save-article-file
2479                ,@(if (featurep 'xemacs) '(t)
2480                    '(:help "Save article in file"))]
2481               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2482               ["Save in MH folder..." gnus-summary-save-article-folder t]
2483               ["Save in VM folder..." gnus-summary-save-article-vm t]
2484               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2485               ["Save body in file..." gnus-summary-save-article-body-file t]
2486               ["Pipe through a filter..." gnus-summary-pipe-output t]
2487               ["Print with Muttprint..." gnus-summary-muttprint t]
2488               ["Print" gnus-summary-print-article
2489                ,@(if (featurep 'xemacs) '(t)
2490                    '(:help "Generate and print a PostScript image"))])
2491              ("Copy, move,... (Backend)"
2492               ,@(if (featurep 'xemacs) nil
2493                   '(:help "Copying, moving, expiring articles..."))
2494               ["Respool article..." gnus-summary-respool-article t]
2495               ["Move article..." gnus-summary-move-article
2496                (gnus-check-backend-function
2497                 'request-move-article gnus-newsgroup-name)]
2498               ["Copy article..." gnus-summary-copy-article t]
2499               ["Crosspost article..." gnus-summary-crosspost-article
2500                (gnus-check-backend-function
2501                 'request-replace-article gnus-newsgroup-name)]
2502               ["Import file..." gnus-summary-import-article
2503                (gnus-check-backend-function
2504                 'request-accept-article gnus-newsgroup-name)]
2505               ["Create article..." gnus-summary-create-article
2506                (gnus-check-backend-function
2507                 'request-accept-article gnus-newsgroup-name)]
2508               ["Check if posted" gnus-summary-article-posted-p t]
2509               ["Edit article" gnus-summary-edit-article
2510                (not (gnus-group-read-only-p))]
2511               ["Delete article" gnus-summary-delete-article
2512                (gnus-check-backend-function
2513                 'request-expire-articles gnus-newsgroup-name)]
2514               ["Query respool" gnus-summary-respool-query t]
2515               ["Trace respool" gnus-summary-respool-trace t]
2516               ["Delete expirable articles" gnus-summary-expire-articles-now
2517                (gnus-check-backend-function
2518                 'request-expire-articles gnus-newsgroup-name)])
2519              ("Extract"
2520               ["Uudecode" gnus-uu-decode-uu
2521                ,@(if (featurep 'xemacs) '(t)
2522                    '(:help "Decode uuencoded article(s)"))]
2523               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2524               ["Unshar" gnus-uu-decode-unshar t]
2525               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2526               ["Save" gnus-uu-decode-save t]
2527               ["Binhex" gnus-uu-decode-binhex t]
2528               ["Postscript" gnus-uu-decode-postscript t]
2529               ["All MIME parts" gnus-summary-save-parts t])
2530              ("Cache"
2531               ["Enter article" gnus-cache-enter-article t]
2532               ["Remove article" gnus-cache-remove-article t])
2533              ["Translate" gnus-article-babel t]
2534              ["Select article buffer" gnus-summary-select-article-buffer t]
2535              ["Make article buffer sticky" gnus-sticky-article t]
2536              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2537              ["Isearch article..." gnus-summary-isearch-article t]
2538              ["Beginning of the article" gnus-summary-beginning-of-article t]
2539              ["End of the article" gnus-summary-end-of-article t]
2540              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2541              ["Fetch referenced articles" gnus-summary-refer-references t]
2542              ["Fetch current thread" gnus-summary-refer-thread t]
2543              ["Fetch article with id..." gnus-summary-refer-article t]
2544              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2545              ["Redisplay" gnus-summary-show-article t]
2546              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2547       (easy-menu-define
2548         gnus-summary-article-menu gnus-summary-mode-map ""
2549         (cons "Article" innards))
2550
2551       (if (not (keymapp gnus-summary-article-menu))
2552           (easy-menu-define
2553             gnus-article-commands-menu gnus-article-mode-map ""
2554             (cons "Commands" innards))
2555         ;; in Emacs, don't share menu.
2556         (setq gnus-article-commands-menu
2557               (copy-keymap gnus-summary-article-menu))
2558         (define-key gnus-article-mode-map [menu-bar commands]
2559           (cons "Commands" gnus-article-commands-menu))))
2560
2561     (easy-menu-define
2562       gnus-summary-thread-menu gnus-summary-mode-map ""
2563       '("Threads"
2564         ["Find all messages in thread" gnus-summary-refer-thread t]
2565         ["Toggle threading" gnus-summary-toggle-threads t]
2566         ["Hide threads" gnus-summary-hide-all-threads t]
2567         ["Show threads" gnus-summary-show-all-threads t]
2568         ["Hide thread" gnus-summary-hide-thread t]
2569         ["Show thread" gnus-summary-show-thread t]
2570         ["Go to next thread" gnus-summary-next-thread t]
2571         ["Go to previous thread" gnus-summary-prev-thread t]
2572         ["Go down thread" gnus-summary-down-thread t]
2573         ["Go up thread" gnus-summary-up-thread t]
2574         ["Top of thread" gnus-summary-top-thread t]
2575         ["Mark thread as read" gnus-summary-kill-thread t]
2576         ["Mark thread as expired" gnus-summary-expire-thread t]
2577         ["Lower thread score" gnus-summary-lower-thread t]
2578         ["Raise thread score" gnus-summary-raise-thread t]
2579         ["Rethread current" gnus-summary-rethread-current t]))
2580
2581     (easy-menu-define
2582       gnus-summary-post-menu gnus-summary-mode-map ""
2583       `("Post"
2584         ["Send a message (mail or news)" gnus-summary-post-news
2585          ,@(if (featurep 'xemacs) '(t)
2586              '(:help "Compose a new message (mail or news)"))]
2587         ["Followup" gnus-summary-followup
2588          ,@(if (featurep 'xemacs) '(t)
2589              '(:help "Post followup to this article"))]
2590         ["Followup and yank" gnus-summary-followup-with-original
2591          ,@(if (featurep 'xemacs) '(t)
2592              '(:help "Post followup to this article, quoting its contents"))]
2593         ["Supersede article" gnus-summary-supersede-article t]
2594         ["Cancel article" gnus-summary-cancel-article
2595          ,@(if (featurep 'xemacs) '(t)
2596              '(:help "Cancel an article you posted"))]
2597         ["Reply" gnus-summary-reply t]
2598         ["Reply and yank" gnus-summary-reply-with-original t]
2599         ["Wide reply" gnus-summary-wide-reply t]
2600         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2601          ,@(if (featurep 'xemacs) '(t)
2602              '(:help "Mail a reply, quoting this article"))]
2603         ["Very wide reply" gnus-summary-very-wide-reply t]
2604         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2605          ,@(if (featurep 'xemacs) '(t)
2606              '(:help "Mail a very wide reply, quoting this article"))]
2607         ["Mail forward" gnus-summary-mail-forward t]
2608         ["Post forward" gnus-summary-post-forward t]
2609         ["Digest and mail" gnus-uu-digest-mail-forward t]
2610         ["Digest and post" gnus-uu-digest-post-forward t]
2611         ["Resend message" gnus-summary-resend-message t]
2612         ["Resend message edit" gnus-summary-resend-message-edit t]
2613         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2614         ["Send a mail" gnus-summary-mail-other-window t]
2615         ["Create a local message" gnus-summary-news-other-window t]
2616         ["Uuencode and post" gnus-uu-post-news
2617          ,@(if (featurep 'xemacs) '(t)
2618              '(:help "Post a uuencoded article"))]
2619         ["Followup via news" gnus-summary-followup-to-mail t]
2620         ["Followup via news and yank"
2621          gnus-summary-followup-to-mail-with-original t]
2622         ["Strip signature on reply"
2623          (lambda ()
2624            (interactive)
2625            (if (not (memq message-cite-function
2626                           '(message-cite-original-without-signature
2627                             message-cite-original)))
2628                ;; Stupid workaround for XEmacs not honoring :visible.
2629                (message "Can't toggle this value of `message-cite-function'")
2630              (setq message-cite-function
2631                    (if (eq message-cite-function
2632                            'message-cite-original-without-signature)
2633                        'message-cite-original
2634                      'message-cite-original-without-signature))))
2635          ;; XEmacs barfs on :visible.
2636          ,@(if (featurep 'xemacs) nil
2637              '(:visible (memq message-cite-function
2638                               '(message-cite-original-without-signature
2639                                 message-cite-original))))
2640          :style toggle
2641          :selected (eq message-cite-function
2642                        'message-cite-original-without-signature)
2643          ,@(if (featurep 'xemacs) nil
2644              '(:help "Strip signature from cited article when replying."))]
2645         ;;("Draft"
2646         ;;["Send" gnus-summary-send-draft t]
2647         ;;["Send bounced" gnus-resend-bounced-mail t])
2648         ))
2649
2650     (cond
2651      ((not (keymapp gnus-summary-post-menu))
2652       (setq gnus-article-post-menu gnus-summary-post-menu))
2653      ((not gnus-article-post-menu)
2654       ;; Don't share post menu.
2655       (setq gnus-article-post-menu
2656             (copy-keymap gnus-summary-post-menu))))
2657     (define-key gnus-article-mode-map [menu-bar post]
2658       (cons "Post" gnus-article-post-menu))
2659
2660     (easy-menu-define
2661       gnus-summary-misc-menu gnus-summary-mode-map ""
2662       `("Gnus"
2663         ("Mark Read"
2664          ["Mark as read" gnus-summary-mark-as-read-forward t]
2665          ["Mark same subject and select"
2666           gnus-summary-kill-same-subject-and-select t]
2667          ["Mark same subject" gnus-summary-kill-same-subject t]
2668          ["Catchup" gnus-summary-catchup
2669           ,@(if (featurep 'xemacs) '(t)
2670               '(:help "Mark unread articles in this group as read"))]
2671          ["Catchup all" gnus-summary-catchup-all t]
2672          ["Catchup to here" gnus-summary-catchup-to-here t]
2673          ["Catchup from here" gnus-summary-catchup-from-here t]
2674          ["Catchup region" gnus-summary-mark-region-as-read
2675           (gnus-mark-active-p)]
2676          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2677         ("Mark Various"
2678          ["Tick" gnus-summary-tick-article-forward t]
2679          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2680          ["Remove marks" gnus-summary-clear-mark-forward t]
2681          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2682          ["Set bookmark" gnus-summary-set-bookmark t]
2683          ["Remove bookmark" gnus-summary-remove-bookmark t])
2684         ("Limit to"
2685          ["Marks..." gnus-summary-limit-to-marks t]
2686          ["Subject..." gnus-summary-limit-to-subject t]
2687          ["Author..." gnus-summary-limit-to-author t]
2688          ["Recipient..." gnus-summary-limit-to-recipient t]
2689          ["Address..." gnus-summary-limit-to-address t]
2690          ["Age..." gnus-summary-limit-to-age t]
2691          ["Extra..." gnus-summary-limit-to-extra t]
2692          ["Score..." gnus-summary-limit-to-score t]
2693          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2694          ["Unread" gnus-summary-limit-to-unread t]
2695          ["Unseen" gnus-summary-limit-to-unseen t]
2696          ["Singletons" gnus-summary-limit-to-singletons t]
2697          ["Replied" gnus-summary-limit-to-replied t]
2698          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2699          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2700          ["Pop limit" gnus-summary-pop-limit t]
2701          ["Show dormant" gnus-summary-limit-include-dormant t]
2702          ["Hide childless dormant"
2703           gnus-summary-limit-exclude-childless-dormant t]
2704          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2705          ["Hide marked" gnus-summary-limit-exclude-marks t]
2706          ["Show expunged" gnus-summary-limit-include-expunged t])
2707         ("Process Mark"
2708          ["Set mark" gnus-summary-mark-as-processable t]
2709          ["Remove mark" gnus-summary-unmark-as-processable t]
2710          ["Remove all marks" gnus-summary-unmark-all-processable t]
2711          ["Invert marks" gnus-uu-invert-processable t]
2712          ["Mark above" gnus-uu-mark-over t]
2713          ["Mark series" gnus-uu-mark-series t]
2714          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2715          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2716          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2717          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2718          ["Mark all" gnus-uu-mark-all t]
2719          ["Mark buffer" gnus-uu-mark-buffer t]
2720          ["Mark sparse" gnus-uu-mark-sparse t]
2721          ["Mark thread" gnus-uu-mark-thread t]
2722          ["Unmark thread" gnus-uu-unmark-thread t]
2723          ("Process Mark Sets"
2724           ["Kill" gnus-summary-kill-process-mark t]
2725           ["Yank" gnus-summary-yank-process-mark
2726            gnus-newsgroup-process-stack]
2727           ["Save" gnus-summary-save-process-mark t]
2728           ["Run command on marked..." gnus-summary-universal-argument t]))
2729         ("Registry Marks")
2730         ("Scroll article"
2731          ["Page forward" gnus-summary-next-page
2732           ,@(if (featurep 'xemacs) '(t)
2733               '(:help "Show next page of article"))]
2734          ["Page backward" gnus-summary-prev-page
2735           ,@(if (featurep 'xemacs) '(t)
2736               '(:help "Show previous page of article"))]
2737          ["Line forward" gnus-summary-scroll-up t])
2738         ("Move"
2739          ["Next unread article" gnus-summary-next-unread-article t]
2740          ["Previous unread article" gnus-summary-prev-unread-article t]
2741          ["Next article" gnus-summary-next-article t]
2742          ["Previous article" gnus-summary-prev-article t]
2743          ["Next unread subject" gnus-summary-next-unread-subject t]
2744          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2745          ["Next article same subject" gnus-summary-next-same-subject t]
2746          ["Previous article same subject" gnus-summary-prev-same-subject t]
2747          ["First unread article" gnus-summary-first-unread-article t]
2748          ["Best unread article" gnus-summary-best-unread-article t]
2749          ["Go to subject number..." gnus-summary-goto-subject t]
2750          ["Go to article number..." gnus-summary-goto-article t]
2751          ["Go to the last article" gnus-summary-goto-last-article t]
2752          ["Pop article off history" gnus-summary-pop-article t])
2753         ("Sort"
2754          ["Sort by number" gnus-summary-sort-by-number t]
2755          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2756          ["Sort by author" gnus-summary-sort-by-author t]
2757          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2758          ["Sort by subject" gnus-summary-sort-by-subject t]
2759          ["Sort by date" gnus-summary-sort-by-date t]
2760          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2761          ["Sort by score" gnus-summary-sort-by-score t]
2762          ["Sort by lines" gnus-summary-sort-by-lines t]
2763          ["Sort by characters" gnus-summary-sort-by-chars t]
2764          ["Randomize" gnus-summary-sort-by-random t]
2765          ["Original sort" gnus-summary-sort-by-original t])
2766         ("Help"
2767          ["Describe group" gnus-summary-describe-group t]
2768          ["Read manual" gnus-info-find-node t])
2769         ("Modes"
2770          ["Pick and read" gnus-pick-mode t]
2771          ["Binary" gnus-binary-mode t])
2772         ("Regeneration"
2773          ["Regenerate" gnus-summary-prepare t]
2774          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2775          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2776          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2777          ["Toggle threading" gnus-summary-toggle-threads t])
2778         ["See old articles" gnus-summary-insert-old-articles t]
2779         ["See new articles" gnus-summary-insert-new-articles t]
2780         ["Filter articles..." gnus-summary-execute-command t]
2781         ["Run command on articles..." gnus-summary-universal-argument t]
2782         ["Search articles forward..." gnus-summary-search-article-forward t]
2783         ["Search articles backward..." gnus-summary-search-article-backward t]
2784         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2785         ["Expand window" gnus-summary-expand-window t]
2786         ["Expire expirable articles" gnus-summary-expire-articles
2787          (gnus-check-backend-function
2788           'request-expire-articles gnus-newsgroup-name)]
2789         ["Edit local kill file" gnus-summary-edit-local-kill t]
2790         ["Edit main kill file" gnus-summary-edit-global-kill t]
2791         ["Edit group parameters" gnus-summary-edit-parameters t]
2792         ["Customize group parameters" gnus-summary-customize-parameters t]
2793         ["Send a bug report" gnus-bug t]
2794         ("Exit"
2795          ["Catchup and exit" gnus-summary-catchup-and-exit
2796           ,@(if (featurep 'xemacs) '(t)
2797               '(:help "Mark unread articles in this group as read, then exit"))]
2798          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2799          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2800          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2801          ["Exit group" gnus-summary-exit
2802           ,@(if (featurep 'xemacs) '(t)
2803               '(:help "Exit current group, return to group selection mode"))]
2804          ["Exit group without updating" gnus-summary-exit-no-update t]
2805          ["Exit and goto next group" gnus-summary-next-group t]
2806          ["Exit and goto prev group" gnus-summary-prev-group t]
2807          ["Reselect group" gnus-summary-reselect-current-group t]
2808          ["Rescan group" gnus-summary-rescan-group t]
2809          ["Update dribble" gnus-summary-save-newsrc t])))
2810
2811     (gnus-run-hooks 'gnus-summary-menu-hook)))
2812
2813 (defvar gnus-summary-tool-bar-map nil)
2814
2815 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2816 ;; affect _new_ message buffers.  We might add a function that walks thru all
2817 ;; summary-mode buffers and force the update.
2818 (defun gnus-summary-tool-bar-update (&optional symbol value)
2819   "Update summary mode toolbar.
2820 Setter function for custom variables."
2821   (setq-default gnus-summary-tool-bar-map nil)
2822   (when symbol
2823     ;; When used as ":set" function:
2824     (set-default symbol value))
2825   (when (gnus-buffer-live-p gnus-summary-buffer)
2826     (with-current-buffer gnus-summary-buffer
2827       (gnus-summary-make-tool-bar))))
2828
2829 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2830                                      'gnus-summary-tool-bar-gnome
2831                                    'gnus-summary-tool-bar-retro)
2832   "Specifies the Gnus summary tool bar.
2833
2834 It can be either a list or a symbol refering to a list.  See
2835 `gmm-tool-bar-from-list' for the format of the list.  The
2836 default key map is `gnus-summary-mode-map'.
2837
2838 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2839 `gnus-summary-tool-bar-retro'."
2840   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2841                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2842                  (repeat :tag "User defined list" gmm-tool-bar-item)
2843                  (symbol))
2844   :version "23.1" ;; No Gnus
2845   :initialize 'custom-initialize-default
2846   :set 'gnus-summary-tool-bar-update
2847   :group 'gnus-summary)
2848
2849 (defcustom gnus-summary-tool-bar-gnome
2850   '((gnus-summary-post-news "mail/compose" nil)
2851     (gnus-summary-insert-new-articles "mail/inbox" nil
2852                                       :visible (or (not gnus-agent)
2853                                                    gnus-plugged))
2854     (gnus-summary-reply-with-original "mail/reply")
2855     (gnus-summary-reply "mail/reply" nil :visible nil)
2856     (gnus-summary-followup-with-original "mail/reply-all")
2857     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2858     (gnus-summary-mail-forward "mail/forward")
2859     (gnus-summary-save-article "mail/save")
2860     (gnus-summary-search-article-forward "search" nil :visible nil)
2861     (gnus-summary-print-article "print")
2862     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2863     ;; Some new commands that may need more suitable icons:
2864     (gnus-summary-save-newsrc "save" nil :visible nil)
2865     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2866     (gnus-summary-prev-article "left-arrow")
2867     (gnus-summary-next-article "right-arrow")
2868     (gnus-summary-next-page "next-page")
2869     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2870     ;;
2871     ;; Maybe some sort-by-... could be added:
2872     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2873     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2874     (gnus-summary-mark-as-expirable
2875      "delete" nil
2876      :visible (gnus-check-backend-function 'request-expire-articles
2877                                            gnus-newsgroup-name))
2878     (gnus-summary-mark-as-spam
2879      "mail/spam" t
2880      :visible (and (fboundp 'spam-group-ham-contents-p)
2881                    (spam-group-ham-contents-p gnus-newsgroup-name))
2882      :help "Mark as spam")
2883     (gnus-summary-mark-as-read-forward
2884      "mail/not-spam" nil
2885      :visible (and (fboundp 'spam-group-spam-contents-p)
2886                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2887     ;;
2888     (gnus-summary-exit "exit")
2889     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2890     (gnus-info-find-node "help"))
2891   "List of functions for the summary tool bar (GNOME style).
2892
2893 See `gmm-tool-bar-from-list' for the format of the list."
2894   :type '(repeat gmm-tool-bar-item)
2895   :version "23.1" ;; No Gnus
2896   :initialize 'custom-initialize-default
2897   :set 'gnus-summary-tool-bar-update
2898   :group 'gnus-summary)
2899
2900 (defcustom gnus-summary-tool-bar-retro
2901   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2902     (gnus-summary-next-unread-article "gnus/next-ur")
2903     (gnus-summary-post-news "gnus/post")
2904     (gnus-summary-followup-with-original "gnus/fuwo")
2905     (gnus-summary-followup "gnus/followup")
2906     (gnus-summary-reply-with-original "gnus/reply-wo")
2907     (gnus-summary-reply "gnus/reply")
2908     (gnus-summary-caesar-message "gnus/rot13")
2909     (gnus-uu-decode-uu "gnus/uu-decode")
2910     (gnus-summary-save-article-file "gnus/save-aif")
2911     (gnus-summary-save-article "gnus/save-art")
2912     (gnus-uu-post-news "gnus/uu-post")
2913     (gnus-summary-catchup "gnus/catchup")
2914     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2915     (gnus-summary-exit "gnus/exit-summ")
2916     ;; Some new command that may need more suitable icons:
2917     (gnus-summary-print-article "gnus/print" nil :visible nil)
2918     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2919     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2920     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2921     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2922     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2923     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2924     ;;
2925     (gnus-info-find-node "gnus/help" nil :visible nil))
2926   "List of functions for the summary tool bar (retro look).
2927
2928 See `gmm-tool-bar-from-list' for the format of the list."
2929   :type '(repeat gmm-tool-bar-item)
2930   :version "23.1" ;; No Gnus
2931   :initialize 'custom-initialize-default
2932   :set 'gnus-summary-tool-bar-update
2933   :group 'gnus-summary)
2934
2935 (defcustom gnus-summary-tool-bar-zap-list t
2936   "List of icon items from the global tool bar.
2937 These items are not displayed in the Gnus summary mode tool bar.
2938
2939 See `gmm-tool-bar-from-list' for the format of the list."
2940   :type 'gmm-tool-bar-zap-list
2941   :version "23.1" ;; No Gnus
2942   :initialize 'custom-initialize-default
2943   :set 'gnus-summary-tool-bar-update
2944   :group 'gnus-summary)
2945
2946 (defvar image-load-path)
2947 (defvar tool-bar-map)
2948
2949 (defun gnus-summary-make-tool-bar (&optional force)
2950   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2951 When FORCE, rebuild the tool bar."
2952   (when (and (not (featurep 'xemacs))
2953              (boundp 'tool-bar-mode)
2954              tool-bar-mode
2955              (or (not gnus-summary-tool-bar-map) force))
2956     (let* ((load-path
2957             (gmm-image-load-path-for-library "gnus"
2958                                              "mail/save.xpm"
2959                                              nil t))
2960            (image-load-path (cons (car load-path)
2961                                   (when (boundp 'image-load-path)
2962                                     image-load-path)))
2963            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2964                                         gnus-summary-tool-bar-zap-list
2965                                         'gnus-summary-mode-map)))
2966       (when map
2967         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2968         ;; uses it's value.
2969         (setq gnus-summary-tool-bar-map map))))
2970   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2971
2972 (defun gnus-score-set-default (var value)
2973   "A version of set that updates the GNU Emacs menu-bar."
2974   (set var value)
2975   ;; It is the message that forces the active status to be updated.
2976   (message ""))
2977
2978 (defun gnus-make-score-map (type)
2979   "Make a summary score map of type TYPE."
2980   (if t
2981       nil
2982     (let ((headers '(("author" "from" string)
2983                      ("subject" "subject" string)
2984                      ("article body" "body" string)
2985                      ("article head" "head" string)
2986                      ("xref" "xref" string)
2987                      ("extra header" "extra" string)
2988                      ("lines" "lines" number)
2989                      ("followups to author" "followup" string)))
2990           (types '((number ("less than" <)
2991                            ("greater than" >)
2992                            ("equal" =))
2993                    (string ("substring" s)
2994                            ("exact string" e)
2995                            ("fuzzy string" f)
2996                            ("regexp" r))))
2997           (perms '(("temporary" (current-time-string))
2998                    ("permanent" nil)
2999                    ("immediate" now)))
3000           header)
3001       (list
3002        (apply
3003         'nconc
3004         (list
3005          (if (eq type 'lower)
3006              "Lower score"
3007            "Increase score"))
3008         (let (outh)
3009           (while headers
3010             (setq header (car headers))
3011             (setq outh
3012                   (cons
3013                    (apply
3014                     'nconc
3015                     (list (car header))
3016                     (let ((ts (cdr (assoc (nth 2 header) types)))
3017                           outt)
3018                       (while ts
3019                         (setq outt
3020                               (cons
3021                                (apply
3022                                 'nconc
3023                                 (list (caar ts))
3024                                 (let ((ps perms)
3025                                       outp)
3026                                   (while ps
3027                                     (setq outp
3028                                           (cons
3029                                            (vector
3030                                             (caar ps)
3031                                             (list
3032                                              'gnus-summary-score-entry
3033                                              (nth 1 header)
3034                                              (if (or (string= (nth 1 header)
3035                                                               "head")
3036                                                      (string= (nth 1 header)
3037                                                               "body"))
3038                                                  ""
3039                                                (list 'gnus-summary-header
3040                                                      (nth 1 header)))
3041                                              (list 'quote (nth 1 (car ts)))
3042                                              (list 'gnus-score-delta-default
3043                                                    nil)
3044                                              (nth 1 (car ps))
3045                                              t)
3046                                             t)
3047                                            outp))
3048                                     (setq ps (cdr ps)))
3049                                   (list (nreverse outp))))
3050                                outt))
3051                         (setq ts (cdr ts)))
3052                       (list (nreverse outt))))
3053                    outh))
3054             (setq headers (cdr headers)))
3055           (list (nreverse outh))))))))
3056
3057
3058 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3059 (defvar bookmark-make-record-function)
3060 \f
3061
3062 (defun gnus-summary-mode (&optional group)
3063   "Major mode for reading articles.
3064
3065 All normal editing commands are switched off.
3066 \\<gnus-summary-mode-map>
3067 Each line in this buffer represents one article.  To read an
3068 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3069 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3070 respectively.
3071
3072 You can also post articles and send mail from this buffer.  To
3073 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3074 of an article, type `\\[gnus-summary-reply]'.
3075
3076 There are approx. one gazillion commands you can execute in this
3077 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3078
3079 The following commands are available:
3080
3081 \\{gnus-summary-mode-map}"
3082   (interactive)
3083   (kill-all-local-variables)
3084   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3085     (gnus-summary-make-local-variables))
3086   (gnus-summary-make-local-variables)
3087   (setq gnus-newsgroup-name group)
3088   (when (gnus-visual-p 'summary-menu 'menu)
3089     (gnus-summary-make-menu-bar)
3090     (gnus-summary-make-tool-bar))
3091   (gnus-make-thread-indent-array)
3092   (gnus-simplify-mode-line)
3093   (setq major-mode 'gnus-summary-mode)
3094   (setq mode-name "Summary")
3095   (use-local-map gnus-summary-mode-map)
3096   (buffer-disable-undo)
3097   (setq buffer-read-only t              ;Disable modification
3098         show-trailing-whitespace nil)
3099   (setq truncate-lines t)
3100   (add-to-invisibility-spec '(gnus-sum . t))
3101   (gnus-summary-set-display-table)
3102   (gnus-set-default-directory)
3103   (make-local-variable 'gnus-summary-line-format)
3104   (make-local-variable 'gnus-summary-line-format-spec)
3105   (make-local-variable 'gnus-summary-dummy-line-format)
3106   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3107   (make-local-variable 'gnus-summary-mark-positions)
3108   (gnus-make-local-hook 'pre-command-hook)
3109   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3110   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3111   (turn-on-gnus-mailing-list-mode)
3112   (mm-enable-multibyte)
3113   (set (make-local-variable 'bookmark-make-record-function)
3114        'gnus-summary-bookmark-make-record)
3115   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3116   (gnus-update-summary-mark-positions))
3117
3118 (defun gnus-summary-make-local-variables ()
3119   "Make all the local summary buffer variables."
3120   (let (global)
3121     (dolist (local gnus-summary-local-variables)
3122       (if (consp local)
3123           (progn
3124             (if (eq (cdr local) 'global)
3125                 ;; Copy the global value of the variable.
3126                 (setq global (symbol-value (car local)))
3127               ;; Use the value from the list.
3128               (setq global (eval (cdr local))))
3129             (set (make-local-variable (car local)) global))
3130         ;; Simple nil-valued local variable.
3131         (set (make-local-variable local) nil)))))
3132
3133 ;; Summary data functions.
3134
3135 (defmacro gnus-data-number (data)
3136   `(car ,data))
3137
3138 (defmacro gnus-data-set-number (data number)
3139   `(setcar ,data ,number))
3140
3141 (defmacro gnus-data-mark (data)
3142   `(nth 1 ,data))
3143
3144 (defmacro gnus-data-set-mark (data mark)
3145   `(setcar (nthcdr 1 ,data) ,mark))
3146
3147 (defmacro gnus-data-pos (data)
3148   `(nth 2 ,data))
3149
3150 (defmacro gnus-data-set-pos (data pos)
3151   `(setcar (nthcdr 2 ,data) ,pos))
3152
3153 (defmacro gnus-data-header (data)
3154   `(nth 3 ,data))
3155
3156 (defmacro gnus-data-set-header (data header)
3157   `(setf (nth 3 ,data) ,header))
3158
3159 (defmacro gnus-data-level (data)
3160   `(nth 4 ,data))
3161
3162 (defmacro gnus-data-unread-p (data)
3163   `(= (nth 1 ,data) gnus-unread-mark))
3164
3165 (defmacro gnus-data-read-p (data)
3166   `(/= (nth 1 ,data) gnus-unread-mark))
3167
3168 (defmacro gnus-data-pseudo-p (data)
3169   `(consp (nth 3 ,data)))
3170
3171 (defmacro gnus-data-find (number)
3172   `(assq ,number gnus-newsgroup-data))
3173
3174 (defmacro gnus-data-find-list (number &optional data)
3175   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3176      (memq (assq ,number bdata)
3177            bdata)))
3178
3179 (defmacro gnus-data-make (number mark pos header level)
3180   `(list ,number ,mark ,pos ,header ,level))
3181
3182 (defun gnus-data-enter (after-article number mark pos header level offset)
3183   (let ((data (gnus-data-find-list after-article)))
3184     (unless data
3185       (error "No such article: %d" after-article))
3186     (setcdr data (cons (gnus-data-make number mark pos header level)
3187                        (cdr data)))
3188     (setq gnus-newsgroup-data-reverse nil)
3189     (gnus-data-update-list (cddr data) offset)))
3190
3191 (defun gnus-data-enter-list (after-article list &optional offset)
3192   (when list
3193     (let ((data (and after-article (gnus-data-find-list after-article)))
3194           (ilist list))
3195       (if (not (or data
3196                    after-article))
3197           (let ((odata gnus-newsgroup-data))
3198             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3199             (when offset
3200               (gnus-data-update-list odata offset)))
3201         ;; Find the last element in the list to be spliced into the main
3202         ;; list.
3203         (setq list (last list))
3204         (if (not data)
3205             (progn
3206               (setcdr list gnus-newsgroup-data)
3207               (setq gnus-newsgroup-data ilist)
3208               (when offset
3209                 (gnus-data-update-list (cdr list) offset)))
3210           (setcdr list (cdr data))
3211           (setcdr data ilist)
3212           (when offset
3213             (gnus-data-update-list (cdr list) offset))))
3214       (setq gnus-newsgroup-data-reverse nil))))
3215
3216 (defun gnus-data-remove (article &optional offset)
3217   (let ((data gnus-newsgroup-data))
3218     (if (= (gnus-data-number (car data)) article)
3219         (progn
3220           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3221                 gnus-newsgroup-data-reverse nil)
3222           (when offset
3223             (gnus-data-update-list gnus-newsgroup-data offset)))
3224       (while (cdr data)
3225         (when (= (gnus-data-number (cadr data)) article)
3226           (setcdr data (cddr data))
3227           (when offset
3228             (gnus-data-update-list (cdr data) offset))
3229           (setq data nil
3230                 gnus-newsgroup-data-reverse nil))
3231         (setq data (cdr data))))))
3232
3233 (defmacro gnus-data-list (backward)
3234   `(if ,backward
3235        (or gnus-newsgroup-data-reverse
3236            (setq gnus-newsgroup-data-reverse
3237                  (reverse gnus-newsgroup-data)))
3238      gnus-newsgroup-data))
3239
3240 (defun gnus-data-update-list (data offset)
3241   "Add OFFSET to the POS of all data entries in DATA."
3242   (setq gnus-newsgroup-data-reverse nil)
3243   (while data
3244     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3245     (setq data (cdr data))))
3246
3247 (defun gnus-summary-article-pseudo-p (article)
3248   "Say whether this article is a pseudo article or not."
3249   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3250
3251 (defmacro gnus-summary-article-sparse-p (article)
3252   "Say whether this article is a sparse article or not."
3253   `(memq ,article gnus-newsgroup-sparse))
3254
3255 (defmacro gnus-summary-article-ancient-p (article)
3256   "Say whether this article is a sparse article or not."
3257   `(memq ,article gnus-newsgroup-ancient))
3258
3259 (defun gnus-article-parent-p (number)
3260   "Say whether this article is a parent or not."
3261   (let ((data (gnus-data-find-list number)))
3262     (and (cdr data)              ; There has to be an article after...
3263          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3264             (gnus-data-level (nth 1 data))))))
3265
3266 (defun gnus-article-children (number)
3267   "Return a list of all children to NUMBER."
3268   (let* ((data (gnus-data-find-list number))
3269          (level (gnus-data-level (car data)))
3270          children)
3271     (setq data (cdr data))
3272     (while (and data
3273                 (= (gnus-data-level (car data)) (1+ level)))
3274       (push (gnus-data-number (car data)) children)
3275       (setq data (cdr data)))
3276     children))
3277
3278 (defmacro gnus-summary-skip-intangible ()
3279   "If the current article is intangible, then jump to a different article."
3280   '(let ((to (get-text-property (point) 'gnus-intangible)))
3281      (and to (gnus-summary-goto-subject to))))
3282
3283 (defmacro gnus-summary-article-intangible-p ()
3284   "Say whether this article is intangible or not."
3285   '(get-text-property (point) 'gnus-intangible))
3286
3287 (defun gnus-article-read-p (article)
3288   "Say whether ARTICLE is read or not."
3289   (not (or (memq article gnus-newsgroup-marked)
3290            (memq article gnus-newsgroup-spam-marked)
3291            (memq article gnus-newsgroup-unreads)
3292            (memq article gnus-newsgroup-unselected)
3293            (memq article gnus-newsgroup-dormant))))
3294
3295 ;; Some summary mode macros.
3296
3297 (defmacro gnus-summary-article-number ()
3298   "The article number of the article on the current line.
3299 If there isn't an article number here, then we return the current
3300 article number."
3301   '(progn
3302      (gnus-summary-skip-intangible)
3303      (or (get-text-property (point) 'gnus-number)
3304          (gnus-summary-last-subject))))
3305
3306 (defmacro gnus-summary-article-header (&optional number)
3307   "Return the header of article NUMBER."
3308   `(gnus-data-header (gnus-data-find
3309                       ,(or number '(gnus-summary-article-number)))))
3310
3311 (defmacro gnus-summary-thread-level (&optional number)
3312   "Return the level of thread that starts with article NUMBER."
3313   `(if (and (eq gnus-summary-make-false-root 'dummy)
3314             (get-text-property (point) 'gnus-intangible))
3315        0
3316      (gnus-data-level (gnus-data-find
3317                        ,(or number '(gnus-summary-article-number))))))
3318
3319 (defmacro gnus-summary-article-mark (&optional number)
3320   "Return the mark of article NUMBER."
3321   `(gnus-data-mark (gnus-data-find
3322                     ,(or number '(gnus-summary-article-number)))))
3323
3324 (defmacro gnus-summary-article-pos (&optional number)
3325   "Return the position of the line of article NUMBER."
3326   `(gnus-data-pos (gnus-data-find
3327                    ,(or number '(gnus-summary-article-number)))))
3328
3329 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3330 (defmacro gnus-summary-article-subject (&optional number)
3331   "Return current subject string or nil if nothing."
3332   `(let ((headers
3333           ,(if number
3334                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3335              '(gnus-data-header (assq (gnus-summary-article-number)
3336                                       gnus-newsgroup-data)))))
3337      (and headers
3338           (vectorp headers)
3339           (mail-header-subject headers))))
3340
3341 (defmacro gnus-summary-article-score (&optional number)
3342   "Return current article score."
3343   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3344                   gnus-newsgroup-scored))
3345        gnus-summary-default-score 0))
3346
3347 (defun gnus-summary-article-children (&optional number)
3348   "Return a list of article numbers that are children of article NUMBER."
3349   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3350          (level (gnus-data-level (car data)))
3351          l children)
3352     (while (and (setq data (cdr data))
3353                 (> (setq l (gnus-data-level (car data))) level))
3354       (and (= (1+ level) l)
3355            (push (gnus-data-number (car data))
3356                  children)))
3357     (nreverse children)))
3358
3359 (defun gnus-summary-article-parent (&optional number)
3360   "Return the article number of the parent of article NUMBER."
3361   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3362                                     (gnus-data-list t)))
3363          (level (gnus-data-level (car data))))
3364     (if (zerop level)
3365         ()                              ; This is a root.
3366       ;; We search until we find an article with a level less than
3367       ;; this one.  That function has to be the parent.
3368       (while (and (setq data (cdr data))
3369                   (not (< (gnus-data-level (car data)) level))))
3370       (and data (gnus-data-number (car data))))))
3371
3372 (defun gnus-unread-mark-p (mark)
3373   "Say whether MARK is the unread mark."
3374   (= mark gnus-unread-mark))
3375
3376 (defun gnus-read-mark-p (mark)
3377   "Say whether MARK is one of the marks that mark as read.
3378 This is all marks except unread, ticked, dormant, and expirable."
3379   (not (or (= mark gnus-unread-mark)
3380            (= mark gnus-ticked-mark)
3381            (= mark gnus-spam-mark)
3382            (= mark gnus-dormant-mark)
3383            (= mark gnus-expirable-mark))))
3384
3385 (defmacro gnus-article-mark (number)
3386   "Return the MARK of article NUMBER.
3387 This macro should only be used when computing the mark the \"first\"
3388 time; i.e., when generating the summary lines.  After that,
3389 `gnus-summary-article-mark' should be used to examine the
3390 marks of articles."
3391   `(cond
3392     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3393     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3394     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3395     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3396     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3397     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3398     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3399     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3400            gnus-ancient-mark))))
3401
3402 ;; Saving hidden threads.
3403
3404 (defmacro gnus-save-hidden-threads (&rest forms)
3405   "Save hidden threads, eval FORMS, and restore the hidden threads."
3406   (let ((config (make-symbol "config")))
3407     `(let ((,config (gnus-hidden-threads-configuration)))
3408        (unwind-protect
3409            (save-excursion
3410              ,@forms)
3411          (gnus-restore-hidden-threads-configuration ,config)))))
3412 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3413 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3414
3415 (defun gnus-data-compute-positions ()
3416   "Compute the positions of all articles."
3417   (setq gnus-newsgroup-data-reverse nil)
3418   (let ((data gnus-newsgroup-data))
3419     (save-excursion
3420       (gnus-save-hidden-threads
3421         (gnus-summary-show-all-threads)
3422         (goto-char (point-min))
3423         (while data
3424           (while (get-text-property (point) 'gnus-intangible)
3425             (forward-line 1))
3426           (gnus-data-set-pos (car data) (+ (point) 3))
3427           (setq data (cdr data))
3428           (forward-line 1))))))
3429
3430 (defun gnus-hidden-threads-configuration ()
3431   "Return the current hidden threads configuration."
3432   (save-excursion
3433     (let (config)
3434       (goto-char (point-min))
3435       (while (not (eobp))
3436         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3437           (push (save-excursion (forward-line 0) (point)) config))
3438         (forward-line 1))
3439       config)))
3440
3441 (defun gnus-restore-hidden-threads-configuration (config)
3442   "Restore hidden threads configuration from CONFIG."
3443   (save-excursion
3444     (let (point (inhibit-read-only t))
3445       (while (setq point (pop config))
3446         (goto-char point)
3447         (gnus-summary-hide-thread)))))
3448
3449 ;; Various summary mode internalish functions.
3450
3451 (defun gnus-mouse-pick-article (e)
3452   (interactive "e")
3453   (mouse-set-point e)
3454   (gnus-summary-next-page nil t))
3455
3456 (defun gnus-summary-set-display-table ()
3457   "Change the display table.
3458 Odd characters have a tendency to mess
3459 up nicely formatted displays - we make all possible glyphs
3460 display only a single character."
3461
3462   ;; We start from the standard display table, if any.
3463   (let ((table (or (copy-sequence standard-display-table)
3464                    (make-display-table)))
3465         (i 32))
3466     ;; Nix out all the control chars...
3467     (while (>= (setq i (1- i)) 0)
3468       (gnus-put-display-table i [??] table))
3469    ;; ... but not newline and cr, of course.  (cr is necessary for the
3470     ;; selective display).
3471     (gnus-put-display-table ?\n nil table)
3472     (gnus-put-display-table ?\r nil table)
3473     ;; We keep TAB as well.
3474     (gnus-put-display-table ?\t nil table)
3475     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3476     ;; Emacs 23 (unicode), that are not set already.
3477     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3478                  160
3479                256)))
3480       (while (>= (setq i (1- i)) 127)
3481         ;; Only modify if the entry is nil.
3482         (unless (gnus-get-display-table i table)
3483           (gnus-put-display-table i [??] table))))
3484     (setq buffer-display-table table)))
3485
3486 (defun gnus-summary-set-article-display-arrow (pos)
3487   "Update the overlay arrow to point to line at position POS."
3488   (when gnus-summary-display-arrow
3489     (make-local-variable 'overlay-arrow-position)
3490     (make-local-variable 'overlay-arrow-string)
3491     (save-excursion
3492       (goto-char pos)
3493       (beginning-of-line)
3494       (unless overlay-arrow-position
3495         (setq overlay-arrow-position (make-marker)))
3496       (setq overlay-arrow-string "=>"
3497             overlay-arrow-position (set-marker overlay-arrow-position
3498                                                (point)
3499                                                (current-buffer))))))
3500
3501 (defun gnus-summary-setup-buffer (group)
3502   "Initialize summary buffer."
3503   (let ((buffer (gnus-summary-buffer-name group))
3504         (dead-name (concat "*Dead Summary "
3505                            (gnus-group-decoded-name group) "*")))
3506     ;; If a dead summary buffer exists, we kill it.
3507     (when (gnus-buffer-live-p dead-name)
3508       (gnus-kill-buffer dead-name))
3509     (if (get-buffer buffer)
3510         (progn
3511           (set-buffer buffer)
3512           (setq gnus-summary-buffer (current-buffer))
3513           (not gnus-newsgroup-prepared))
3514       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3515       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3516       (gnus-summary-mode group)
3517       (when (gnus-group-quit-config group)
3518         (set (make-local-variable 'gnus-single-article-buffer) nil))
3519       (make-local-variable 'gnus-article-buffer)
3520       (make-local-variable 'gnus-article-current)
3521       (make-local-variable 'gnus-original-article-buffer)
3522       (setq gnus-newsgroup-name group)
3523       ;; Set any local variables in the group parameters.
3524       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3525       t)))
3526
3527 (defun gnus-set-global-variables ()
3528   "Set the global equivalents of the buffer-local variables.
3529 They are set to the latest values they had.  These reflect the summary
3530 buffer that was in action when the last article was fetched."
3531   (when (eq major-mode 'gnus-summary-mode)
3532     (setq gnus-summary-buffer (current-buffer))
3533     (let ((name gnus-newsgroup-name)
3534           (marked gnus-newsgroup-marked)
3535           (spam gnus-newsgroup-spam-marked)
3536           (unread gnus-newsgroup-unreads)
3537           (headers gnus-current-headers)
3538           (data gnus-newsgroup-data)
3539           (summary gnus-summary-buffer)
3540           (article-buffer gnus-article-buffer)
3541           (original gnus-original-article-buffer)
3542           (gac gnus-article-current)
3543           (reffed gnus-reffed-article-number)
3544           (score-file gnus-current-score-file)
3545           (default-charset gnus-newsgroup-charset)
3546           vlist)
3547       (let ((locals gnus-newsgroup-variables))
3548         (while locals
3549           (if (consp (car locals))
3550               (push (eval (caar locals)) vlist)
3551             (push (eval (car locals)) vlist))
3552           (setq locals (cdr locals)))
3553         (setq vlist (nreverse vlist)))
3554       (with-current-buffer gnus-group-buffer
3555         (setq gnus-newsgroup-name name
3556               gnus-newsgroup-marked marked
3557               gnus-newsgroup-spam-marked spam
3558               gnus-newsgroup-unreads unread
3559               gnus-current-headers headers
3560               gnus-newsgroup-data data
3561               gnus-article-current gac
3562               gnus-summary-buffer summary
3563               gnus-article-buffer article-buffer
3564               gnus-original-article-buffer original
3565               gnus-reffed-article-number reffed
3566               gnus-current-score-file score-file
3567               gnus-newsgroup-charset default-charset)
3568         (let ((locals gnus-newsgroup-variables))
3569           (while locals
3570             (if (consp (car locals))
3571                 (set (caar locals) (pop vlist))
3572               (set (car locals) (pop vlist)))
3573             (setq locals (cdr locals))))
3574         ;; The article buffer also has local variables.
3575         (when (gnus-buffer-live-p gnus-article-buffer)
3576           (set-buffer gnus-article-buffer)
3577           (setq gnus-summary-buffer summary))))))
3578
3579 (defun gnus-summary-article-unread-p (article)
3580   "Say whether ARTICLE is unread or not."
3581   (memq article gnus-newsgroup-unreads))
3582
3583 (defun gnus-summary-first-article-p (&optional article)
3584   "Return whether ARTICLE is the first article in the buffer."
3585   (if (not (setq article (or article (gnus-summary-article-number))))
3586       nil
3587     (eq article (caar gnus-newsgroup-data))))
3588
3589 (defun gnus-summary-last-article-p (&optional article)
3590   "Return whether ARTICLE is the last article in the buffer."
3591   (if (not (setq article (or article (gnus-summary-article-number))))
3592       ;; All non-existent numbers are the last article.  :-)
3593       t
3594     (not (cdr (gnus-data-find-list article)))))
3595
3596 (defun gnus-make-thread-indent-array (&optional n)
3597   (when (or n
3598             (progn (setq n 200) nil)
3599             (null gnus-thread-indent-array)
3600             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3601     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3602           gnus-thread-indent-array-level gnus-thread-indent-level)
3603     (while (>= n 0)
3604       (aset gnus-thread-indent-array n
3605             (make-string (* n gnus-thread-indent-level) ? ))
3606       (setq n (1- n)))))
3607
3608 (defun gnus-update-summary-mark-positions ()
3609   "Compute where the summary marks are to go."
3610   (save-excursion
3611     (when (gnus-buffer-exists-p gnus-summary-buffer)
3612       (set-buffer gnus-summary-buffer))
3613     (let ((spec gnus-summary-line-format-spec)
3614           pos)
3615       (save-excursion
3616         (gnus-set-work-buffer)
3617         (let ((gnus-tmp-unread ?Z)
3618               (gnus-replied-mark ?Z)
3619               (gnus-score-below-mark ?Z)
3620               (gnus-score-over-mark ?Z)
3621               (gnus-undownloaded-mark ?Z)
3622               (gnus-summary-line-format-spec spec)
3623               (gnus-newsgroup-downloadable '(0))
3624               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3625               case-fold-search ignores)
3626           ;; Here, all marks are bound to Z.
3627           (gnus-summary-insert-line header
3628                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3629           (goto-char (point-min))
3630           ;; Memorize the positions of the same characters as dummy marks.
3631           (while (re-search-forward "[A-D]" nil t)
3632             (push (point) ignores))
3633           (erase-buffer)
3634           ;; We use A-D as dummy marks in order to know column positions
3635           ;; where marks should be inserted.
3636           (setq gnus-tmp-unread ?A
3637                 gnus-replied-mark ?B
3638                 gnus-score-below-mark ?C
3639                 gnus-score-over-mark ?C
3640                 gnus-undownloaded-mark ?D)
3641           (gnus-summary-insert-line header
3642                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3643           ;; Ignore characters which aren't dummy marks.
3644           (dolist (p ignores)
3645             (delete-region (goto-char (1- p)) p)
3646             (insert ?Z))
3647           (goto-char (point-min))
3648           (setq pos (list (cons 'unread
3649                                 (and (search-forward "A" nil t)
3650                                      (- (point) (point-min) 1)))))
3651           (goto-char (point-min))
3652           (push (cons 'replied (and (search-forward "B" nil t)
3653                                     (- (point) (point-min) 1)))
3654                 pos)
3655           (goto-char (point-min))
3656           (push (cons 'score (and (search-forward "C" nil t)
3657                                   (- (point) (point-min) 1)))
3658                 pos)
3659           (goto-char (point-min))
3660           (push (cons 'download (and (search-forward "D" nil t)
3661                                      (- (point) (point-min) 1)))
3662                 pos)))
3663       (setq gnus-summary-mark-positions pos))))
3664
3665 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3666   "Insert a dummy root in the summary buffer."
3667   (beginning-of-line)
3668   (gnus-add-text-properties
3669    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3670    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3671
3672 (defun gnus-summary-extract-address-component (from)
3673   (or (car (funcall gnus-extract-address-components from))
3674       from))
3675
3676 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3677   (let ((mail-parse-charset gnus-newsgroup-charset)
3678         (ignored-from-addresses (gnus-ignored-from-addresses))
3679         ; Is it really necessary to do this next part for each summary line?
3680         ; Luckily, doesn't seem to slow things down much.
3681         (mail-parse-ignored-charsets
3682          (with-current-buffer gnus-summary-buffer
3683            gnus-newsgroup-ignored-charsets)))
3684     (or
3685      (and ignored-from-addresses
3686           (string-match ignored-from-addresses gnus-tmp-from)
3687           (let ((extra-headers (mail-header-extra header))
3688                 to
3689                 newsgroups)
3690             (cond
3691              ((setq to (cdr (assq 'To extra-headers)))
3692               (concat gnus-summary-to-prefix
3693                       (inline
3694                         (gnus-summary-extract-address-component
3695                          (funcall gnus-decode-encoded-address-function to)))))
3696              ((setq newsgroups
3697                     (or
3698                      (cdr (assq 'Newsgroups extra-headers))
3699                      (and
3700                       (memq 'Newsgroups gnus-extra-headers)
3701                       (eq (car (gnus-find-method-for-group
3702                                 gnus-newsgroup-name)) 'nntp)
3703                       (gnus-group-real-name gnus-newsgroup-name))))
3704               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3705      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3706
3707 (defun gnus-summary-insert-line (gnus-tmp-header
3708                                  gnus-tmp-level gnus-tmp-current
3709                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3710                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3711                                  &optional gnus-tmp-dummy gnus-tmp-score
3712                                  gnus-tmp-process)
3713   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3714       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3715                                           gnus-tmp-level)))
3716   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3717          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3718          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3719          (gnus-tmp-score-char
3720           (if (or (null gnus-summary-default-score)
3721                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3722                       gnus-summary-zcore-fuzz))
3723               ?                         ;Whitespace
3724             (if (< gnus-tmp-score gnus-summary-default-score)
3725                 gnus-score-below-mark gnus-score-over-mark)))
3726          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3727          (gnus-tmp-replied
3728           (cond (gnus-tmp-process gnus-process-mark)
3729                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3730                  gnus-cached-mark)
3731                 (gnus-tmp-replied gnus-replied-mark)
3732                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3733                  gnus-forwarded-mark)
3734                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3735                  gnus-saved-mark)
3736                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3737                  gnus-recent-mark)
3738                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3739                  gnus-unseen-mark)
3740                 (t gnus-no-mark)))
3741          (gnus-tmp-downloaded
3742           (cond (undownloaded
3743                  gnus-undownloaded-mark)
3744                 (gnus-newsgroup-agentized
3745                  gnus-downloaded-mark)
3746                 (t
3747                  gnus-no-mark)))
3748          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3749          (gnus-tmp-name
3750           (cond
3751            ((string-match "<[^>]+> *$" gnus-tmp-from)
3752             (let ((beg (match-beginning 0)))
3753               (or (and (string-match "^\".+\"" gnus-tmp-from)
3754                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3755                   (substring gnus-tmp-from 0 beg))))
3756            ((string-match "(.+)" gnus-tmp-from)
3757             (substring gnus-tmp-from
3758                        (1+ (match-beginning 0)) (1- (match-end 0))))
3759            (t gnus-tmp-from)))
3760          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3761          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3762          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3763          (inhibit-read-only t))
3764     (when (string= gnus-tmp-name "")
3765       (setq gnus-tmp-name gnus-tmp-from))
3766     (unless (numberp gnus-tmp-lines)
3767       (setq gnus-tmp-lines -1))
3768     (if (= gnus-tmp-lines -1)
3769         (setq gnus-tmp-lines "?")
3770       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3771     (condition-case ()
3772         (gnus-put-text-property
3773          (point)
3774          (progn (eval gnus-summary-line-format-spec) (point))
3775          'gnus-number gnus-tmp-number)
3776       (error (gnus-message 5 "Error updating the summary line")))
3777     (when (gnus-visual-p 'summary-highlight 'highlight)
3778       (forward-line -1)
3779       (gnus-summary-highlight-line)
3780       (gnus-run-hooks 'gnus-summary-update-hook)
3781       (forward-line 1))))
3782
3783 (defun gnus-summary-update-line (&optional dont-update)
3784   "Update summary line after change."
3785   (when (and gnus-summary-default-score
3786              (not gnus-summary-inhibit-highlight))
3787     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3788            (article (gnus-summary-article-number))
3789            (score (gnus-summary-article-score article)))
3790       (unless dont-update
3791         (if (and gnus-summary-mark-below
3792                  (< (gnus-summary-article-score)
3793                     gnus-summary-mark-below))
3794             ;; This article has a low score, so we mark it as read.
3795             (when (memq article gnus-newsgroup-unreads)
3796               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3797           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3798             ;; This article was previously marked as read on account
3799             ;; of a low score, but now it has risen, so we mark it as
3800             ;; unread.
3801             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3802         (gnus-summary-update-mark
3803          (if (or (null gnus-summary-default-score)
3804                  (<= (abs (- score gnus-summary-default-score))
3805                      gnus-summary-zcore-fuzz))
3806              ?                          ;Whitespace
3807            (if (< score gnus-summary-default-score)
3808                gnus-score-below-mark gnus-score-over-mark))
3809          'score))
3810       ;; Do visual highlighting.
3811       (when (gnus-visual-p 'summary-highlight 'highlight)
3812         (gnus-summary-highlight-line)
3813         (gnus-run-hooks 'gnus-summary-update-hook)))))
3814
3815 (defvar gnus-tmp-new-adopts nil)
3816
3817 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3818   "Return the number of articles in THREAD.
3819 This may be 0 in some cases -- if none of the articles in
3820 the thread are to be displayed."
3821   (let* ((number
3822          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3823           (cond
3824            ((not (listp thread))
3825             1)
3826            ((and (consp thread) (cdr thread))
3827             (apply
3828              '+ 1 (mapcar
3829                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3830            ((null thread)
3831             1)
3832            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3833             1)
3834            (t 0))))
3835     (when (and level (zerop level) gnus-tmp-new-adopts)
3836       (incf number
3837             (apply '+ (mapcar
3838                        'gnus-summary-number-of-articles-in-thread
3839                        gnus-tmp-new-adopts))))
3840     (if char
3841         (if (> number 1) gnus-not-empty-thread-mark
3842           gnus-empty-thread-mark)
3843       number)))
3844
3845 (defsubst gnus-summary-line-message-size (head)
3846   "Return pretty-printed version of message size.
3847 This function is intended to be used in
3848 `gnus-summary-line-format-alist'."
3849   (let ((c (or (mail-header-chars head) -1)))
3850     (cond ((< c 0) "n/a")               ; chars not available
3851           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3852           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3853           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3854           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3855
3856
3857 (defun gnus-summary-set-local-parameters (group)
3858   "Go through the local params of GROUP and set all variable specs in that list."
3859   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3860                                         ; really variables.
3861     (dolist (elem (gnus-group-find-parameter group))
3862       (and (consp elem)                 ; Has to be a cons.
3863            (consp (cdr elem))           ; The cdr has to be a list.
3864            (symbolp (car elem))         ; Has to be a symbol in there.
3865            (not (memq (car elem) vars))
3866            (ignore-errors
3867              (push (car elem) vars)
3868              ;; Variables like `gnus-show-threads' that are globally
3869              ;; bound, if used as group parameters, need to get to be
3870              ;; buffer-local, whereas just parameters like `gcc-self',
3871              ;; `timestamp', etc. should not be bound as variables.
3872              (if (boundp (car elem))
3873                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3874                (eval (nth 1 elem))))))))
3875
3876 (defun gnus-summary-read-group (group &optional show-all no-article
3877                                       kill-buffer no-display backward
3878                                       select-articles)
3879   "Start reading news in newsgroup GROUP.
3880 If SHOW-ALL is non-nil, already read articles are also listed.
3881 If NO-ARTICLE is non-nil, no article is selected initially.
3882 If NO-DISPLAY, don't generate a summary buffer."
3883   (let (result)
3884     (while (and group
3885                 (null (setq result
3886                             (let ((gnus-auto-select-next nil))
3887                               (or (gnus-summary-read-group-1
3888                                    group show-all no-article
3889                                    kill-buffer no-display
3890                                    select-articles)
3891                                   (setq show-all nil
3892                                         select-articles nil)))))
3893                 (eq gnus-auto-select-next 'quietly))
3894       (set-buffer gnus-group-buffer)
3895       ;; The entry function called above goes to the next
3896       ;; group automatically, so we go two groups back
3897       ;; if we are searching for the previous group.
3898       (when backward
3899         (gnus-group-prev-unread-group 2))
3900       (if (not (equal group (gnus-group-group-name)))
3901           (setq group (gnus-group-group-name))
3902         (setq group nil)))
3903     result))
3904
3905 (defun gnus-summary-read-group-1 (group show-all no-article
3906                                         kill-buffer no-display
3907                                         &optional select-articles)
3908   ;; Killed foreign groups can't be entered.
3909   ;;  (when (and (not (gnus-group-native-p group))
3910   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3911   ;;    (error "Dead non-native groups can't be entered"))
3912   (gnus-message 5 "Retrieving newsgroup: %s..."
3913                 (gnus-group-decoded-name group))
3914   (let* ((new-group (gnus-summary-setup-buffer group))
3915          (quit-config (gnus-group-quit-config group))
3916          (did-select (and new-group (gnus-select-newsgroup
3917                                      group show-all select-articles))))
3918     (cond
3919      ;; This summary buffer exists already, so we just select it.
3920      ((not new-group)
3921       (gnus-set-global-variables)
3922       (when kill-buffer
3923         (gnus-kill-or-deaden-summary kill-buffer))
3924       (gnus-configure-windows 'summary 'force)
3925       (gnus-set-mode-line 'summary)
3926       (gnus-summary-position-point)
3927       (message "")
3928       t)
3929      ;; We couldn't select this group.
3930      ((null did-select)
3931       (when (and (eq major-mode 'gnus-summary-mode)
3932                  (not (equal (current-buffer) kill-buffer)))
3933         (kill-buffer (current-buffer))
3934         (if (not quit-config)
3935             (progn
3936               ;; Update the info -- marks might need to be removed,
3937               ;; for instance.
3938               (gnus-summary-update-info)
3939               (set-buffer gnus-group-buffer)
3940               (gnus-group-jump-to-group group)
3941               (gnus-group-next-unread-group 1))
3942           (gnus-handle-ephemeral-exit quit-config)))
3943       (let ((grpinfo (gnus-get-info group)))
3944         (if (null (gnus-info-read grpinfo))
3945             (gnus-message 3 "Group %s contains no messages"
3946                           (gnus-group-decoded-name group))
3947           (gnus-message 3 "Can't select group")))
3948       nil)
3949      ;; The user did a `C-g' while prompting for number of articles,
3950      ;; so we exit this group.
3951      ((eq did-select 'quit)
3952       (and (eq major-mode 'gnus-summary-mode)
3953            (not (equal (current-buffer) kill-buffer))
3954            (kill-buffer (current-buffer)))
3955       (when kill-buffer
3956         (gnus-kill-or-deaden-summary kill-buffer))
3957       (if (not quit-config)
3958           (progn
3959             (set-buffer gnus-group-buffer)
3960             (gnus-group-jump-to-group group)
3961             (gnus-configure-windows 'group 'force))
3962         (gnus-handle-ephemeral-exit quit-config))
3963       ;; Finally signal the quit.
3964       (signal 'quit nil))
3965      ;; The group was successfully selected.
3966      (t
3967       (gnus-set-global-variables)
3968       ;; Save the active value in effect when the group was entered.
3969       (setq gnus-newsgroup-active
3970             (gnus-copy-sequence
3971              (gnus-active gnus-newsgroup-name)))
3972       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3973       ;; You can change the summary buffer in some way with this hook.
3974       (gnus-run-hooks 'gnus-select-group-hook)
3975       (when (memq 'summary (gnus-update-format-specifications
3976                             nil 'summary 'summary-mode 'summary-dummy))
3977         ;; The format specification for the summary line was updated,
3978         ;; so we need to update the mark positions as well.
3979         (gnus-update-summary-mark-positions))
3980       ;; Do score processing.
3981       (when gnus-use-scoring
3982         (gnus-possibly-score-headers))
3983       ;; Check whether to fill in the gaps in the threads.
3984       (when gnus-build-sparse-threads
3985         (gnus-build-sparse-threads))
3986       ;; Find the initial limit.
3987       (if show-all
3988           (let ((gnus-newsgroup-dormant nil))
3989             (gnus-summary-initial-limit show-all))
3990         (gnus-summary-initial-limit show-all))
3991       ;; Generate the summary buffer.
3992       (unless no-display
3993         (gnus-summary-prepare))
3994       (when gnus-use-trees
3995         (gnus-tree-open group)
3996         (setq gnus-summary-highlight-line-function
3997               'gnus-tree-highlight-article))
3998       ;; If the summary buffer is empty, but there are some low-scored
3999       ;; articles or some excluded dormants, we include these in the
4000       ;; buffer.
4001       (when (and (zerop (buffer-size))
4002                  (not no-display))
4003         (cond (gnus-newsgroup-dormant
4004                (gnus-summary-limit-include-dormant))
4005               ((and gnus-newsgroup-scored show-all)
4006                (gnus-summary-limit-include-expunged t))))
4007       ;; Function `gnus-apply-kill-file' must be called in this hook.
4008       (gnus-run-hooks 'gnus-apply-kill-hook)
4009       (if (and (zerop (buffer-size))
4010                (not no-display))
4011           (progn
4012             ;; This newsgroup is empty.
4013             (gnus-summary-catchup-and-exit nil t)
4014             (gnus-message 6 "No unread news")
4015             (when kill-buffer
4016               (gnus-kill-or-deaden-summary kill-buffer))
4017             ;; Return nil from this function.
4018             nil)
4019         ;; Hide conversation thread subtrees.  We cannot do this in
4020         ;; gnus-summary-prepare-hook since kill processing may not
4021         ;; work with hidden articles.
4022         (gnus-summary-maybe-hide-threads)
4023         (when kill-buffer
4024           (gnus-kill-or-deaden-summary kill-buffer))
4025         (gnus-summary-auto-select-subject)
4026         ;; Show first unread article if requested.
4027         (if (and (not no-article)
4028                  (not no-display)
4029                  gnus-newsgroup-unreads
4030                  gnus-auto-select-first)
4031             (progn
4032               (gnus-configure-windows 'summary)
4033               (let ((art (gnus-summary-article-number)))
4034                 (unless (and (not gnus-plugged)
4035                              (or (memq art gnus-newsgroup-undownloaded)
4036                                  (memq art gnus-newsgroup-downloadable)))
4037                   (gnus-summary-goto-article art))))
4038           ;; Don't select any articles.
4039           (gnus-summary-position-point)
4040           (gnus-configure-windows 'summary 'force)
4041           (gnus-set-mode-line 'summary))
4042         (when (and gnus-auto-center-group
4043                    (get-buffer-window gnus-group-buffer t))
4044           ;; Gotta use windows, because recenter does weird stuff if
4045           ;; the current buffer ain't the displayed window.
4046           (let ((owin (selected-window)))
4047             (select-window (get-buffer-window gnus-group-buffer t))
4048             (when (gnus-group-goto-group group)
4049               (recenter))
4050             (select-window owin)))
4051         ;; Mark this buffer as "prepared".
4052         (setq gnus-newsgroup-prepared t)
4053         (gnus-run-hooks 'gnus-summary-prepared-hook)
4054         (unless (gnus-ephemeral-group-p group)
4055           (gnus-group-update-group group))
4056         t)))))
4057
4058 (defun gnus-summary-auto-select-subject ()
4059   "Select the subject line on initial group entry."
4060   (goto-char (point-min))
4061   (cond
4062    ((eq gnus-auto-select-subject 'best)
4063     (gnus-summary-best-unread-subject))
4064    ((eq gnus-auto-select-subject 'unread)
4065     (gnus-summary-first-unread-subject))
4066    ((eq gnus-auto-select-subject 'unseen)
4067     (gnus-summary-first-unseen-subject))
4068    ((eq gnus-auto-select-subject 'unseen-or-unread)
4069     (gnus-summary-first-unseen-or-unread-subject))
4070    ((eq gnus-auto-select-subject 'first)
4071     ;; Do nothing.
4072     )
4073    ((functionp gnus-auto-select-subject)
4074     (funcall gnus-auto-select-subject))))
4075
4076 (defun gnus-summary-prepare ()
4077   "Generate the summary buffer."
4078   (interactive)
4079   (let ((inhibit-read-only t))
4080     (erase-buffer)
4081     (setq gnus-newsgroup-data nil
4082           gnus-newsgroup-data-reverse nil)
4083     (gnus-run-hooks 'gnus-summary-generate-hook)
4084     ;; Generate the buffer, either with threads or without.
4085     (when gnus-newsgroup-headers
4086       (gnus-summary-prepare-threads
4087        (if gnus-show-threads
4088            (gnus-sort-gathered-threads
4089             (funcall gnus-summary-thread-gathering-function
4090                      (gnus-sort-threads
4091                       (gnus-cut-threads (gnus-make-threads)))))
4092          ;; Unthreaded display.
4093          (gnus-sort-articles gnus-newsgroup-headers))))
4094     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4095     ;; Call hooks for modifying summary buffer.
4096     (goto-char (point-min))
4097     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4098
4099 (defsubst gnus-general-simplify-subject (subject)
4100   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4101   (setq subject
4102         (cond
4103          ;; Truncate the subject.
4104          (gnus-simplify-subject-functions
4105           (gnus-map-function gnus-simplify-subject-functions subject))
4106          ((numberp gnus-summary-gather-subject-limit)
4107           (setq subject (gnus-simplify-subject-re subject))
4108           (if (> (length subject) gnus-summary-gather-subject-limit)
4109               (substring subject 0 gnus-summary-gather-subject-limit)
4110             subject))
4111          ;; Fuzzily simplify it.
4112          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4113           (gnus-simplify-subject-fuzzy subject))
4114          ;; Just remove the leading "Re:".
4115          (t
4116           (gnus-simplify-subject-re subject))))
4117
4118   (if (and gnus-summary-gather-exclude-subject
4119            (string-match gnus-summary-gather-exclude-subject subject))
4120       nil                         ; This article shouldn't be gathered
4121     subject))
4122
4123 (defun gnus-summary-simplify-subject-query ()
4124   "Query where the respool algorithm would put this article."
4125   (interactive)
4126   (gnus-summary-select-article)
4127   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4128
4129 (defun gnus-gather-threads-by-subject (threads)
4130   "Gather threads by looking at Subject headers."
4131   (if (not gnus-summary-make-false-root)
4132       threads
4133     (let ((hashtb (gnus-make-hashtable 1024))
4134           (prev threads)
4135           (result threads)
4136           subject hthread whole-subject)
4137       (while threads
4138         (setq subject (gnus-general-simplify-subject
4139                        (setq whole-subject (mail-header-subject
4140                                             (caar threads)))))
4141         (when subject
4142           (if (setq hthread (gnus-gethash subject hashtb))
4143               (progn
4144                 ;; We enter a dummy root into the thread, if we
4145                 ;; haven't done that already.
4146                 (unless (stringp (caar hthread))
4147                   (setcar hthread (list whole-subject (car hthread))))
4148                 ;; We add this new gathered thread to this gathered
4149                 ;; thread.
4150                 (setcdr (car hthread)
4151                         (nconc (cdar hthread) (list (car threads))))
4152                 ;; Remove it from the list of threads.
4153                 (setcdr prev (cdr threads))
4154                 (setq threads prev))
4155             ;; Enter this thread into the hash table.
4156             (gnus-sethash subject
4157                           (if gnus-summary-make-false-root-always
4158                               (progn
4159                                 ;; If you want a dummy root above all
4160                                 ;; threads...
4161                                 (setcar threads (list whole-subject
4162                                                       (car threads)))
4163                                 threads)
4164                             threads)
4165                           hashtb)))
4166         (setq prev threads)
4167         (setq threads (cdr threads)))
4168       result)))
4169
4170 (defun gnus-gather-threads-by-references (threads)
4171   "Gather threads by looking at References headers."
4172   (let ((idhashtb (gnus-make-hashtable 1024))
4173         (thhashtb (gnus-make-hashtable 1024))
4174         (prev threads)
4175         (result threads)
4176         ids references id gthread gid entered ref)
4177     (while threads
4178       (when (setq references (mail-header-references (caar threads)))
4179         (setq id (mail-header-id (caar threads))
4180               ids (inline (gnus-split-references references))
4181               entered nil)
4182         (while (setq ref (pop ids))
4183           (setq ids (delete ref ids))
4184           (if (not (setq gid (gnus-gethash ref idhashtb)))
4185               (progn
4186                 (gnus-sethash ref id idhashtb)
4187                 (gnus-sethash id threads thhashtb))
4188             (setq gthread (gnus-gethash gid thhashtb))
4189             (unless entered
4190               ;; We enter a dummy root into the thread, if we
4191               ;; haven't done that already.
4192               (unless (stringp (caar gthread))
4193                 (setcar gthread (list (mail-header-subject (caar gthread))
4194                                       (car gthread))))
4195               ;; We add this new gathered thread to this gathered
4196               ;; thread.
4197               (setcdr (car gthread)
4198                       (nconc (cdar gthread) (list (car threads)))))
4199             ;; Add it into the thread hash table.
4200             (gnus-sethash id gthread thhashtb)
4201             (setq entered t)
4202             ;; Remove it from the list of threads.
4203             (setcdr prev (cdr threads))
4204             (setq threads prev))))
4205       (setq prev threads)
4206       (setq threads (cdr threads)))
4207     result))
4208
4209 (defun gnus-sort-gathered-threads (threads)
4210   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4211   (let ((result threads))
4212     (while threads
4213       (when (stringp (caar threads))
4214         (setcdr (car threads)
4215                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4216       (setq threads (cdr threads)))
4217     result))
4218
4219 (defun gnus-thread-loop-p (root thread)
4220   "Say whether ROOT is in THREAD."
4221   (let ((stack (list thread))
4222         (infloop 0)
4223         th)
4224     (while (setq thread (pop stack))
4225       (setq th (cdr thread))
4226       (while (and th
4227                   (not (eq (caar th) root)))
4228         (pop th))
4229       (if th
4230           ;; We have found a loop.
4231           (let (ref-dep)
4232             (setcdr thread (delq (car th) (cdr thread)))
4233             (if (boundp (setq ref-dep (intern "none"
4234                                               gnus-newsgroup-dependencies)))
4235                 (setcdr (symbol-value ref-dep)
4236                         (nconc (cdr (symbol-value ref-dep))
4237                                (list (car th))))
4238               (set ref-dep (list nil (car th))))
4239             (setq infloop 1
4240                   stack nil))
4241         ;; Push all the subthreads onto the stack.
4242         (push (cdr thread) stack)))
4243     infloop))
4244
4245 (defun gnus-make-threads ()
4246   "Go through the dependency hashtb and find the roots.  Return all threads."
4247   (let (threads)
4248     (while (catch 'infloop
4249              (mapatoms
4250               (lambda (refs)
4251                 ;; Deal with self-referencing References loops.
4252                 (when (and (car (symbol-value refs))
4253                            (not (zerop
4254                                  (apply
4255                                   '+
4256                                   (mapcar
4257                                    (lambda (thread)
4258                                      (gnus-thread-loop-p
4259                                       (car (symbol-value refs)) thread))
4260                                    (cdr (symbol-value refs)))))))
4261                   (setq threads nil)
4262                   (throw 'infloop t))
4263                 (unless (car (symbol-value refs))
4264                   ;; These threads do not refer back to any other
4265                   ;; articles, so they're roots.
4266                   (setq threads (append (cdr (symbol-value refs)) threads))))
4267               gnus-newsgroup-dependencies)))
4268     threads))
4269
4270 ;; Build the thread tree.
4271 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4272   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4273
4274 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4275 if it was already present.
4276
4277 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4278 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4279 Message-IDs will be renamed to a unique Message-ID before being
4280 entered.
4281
4282 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4283   (let* ((id (mail-header-id header))
4284          (id-dep (and id (intern id dependencies)))
4285          parent-id ref ref-dep ref-header replaced)
4286     ;; Enter this `header' in the `dependencies' table.
4287     (cond
4288      ((not id-dep)
4289       (setq header nil))
4290      ;; The first two cases do the normal part: enter a new `header'
4291      ;; in the `dependencies' table.
4292      ((not (boundp id-dep))
4293       (set id-dep (list header)))
4294      ((null (car (symbol-value id-dep)))
4295       (setcar (symbol-value id-dep) header))
4296
4297      ;; From here the `header' was already present in the
4298      ;; `dependencies' table.
4299      (force-new
4300       ;; Overrides an existing entry;
4301       ;; just set the header part of the entry.
4302       (setcar (symbol-value id-dep) header)
4303       (setq replaced t))
4304
4305      ;; Renames the existing `header' to a unique Message-ID.
4306      ((not gnus-summary-ignore-duplicates)
4307       ;; An article with this Message-ID has already been seen.
4308       ;; We rename the Message-ID.
4309       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4310            (list header))
4311       (mail-header-set-id header id))
4312
4313      ;; The last case ignores an existing entry, except it adds any
4314      ;; additional Xrefs (in case the two articles came from different
4315      ;; servers.
4316      ;; Also sets `header' to `nil' meaning that the `dependencies'
4317      ;; table was *not* modified.
4318      (t
4319       (mail-header-set-xref
4320        (car (symbol-value id-dep))
4321        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4322                    "")
4323                (or (mail-header-xref header) "")))
4324       (setq header nil)))
4325
4326     (when (and header (not replaced))
4327       ;; First check that we are not creating a References loop.
4328       (setq parent-id (gnus-parent-id (mail-header-references header)))
4329       (setq ref parent-id)
4330       (while (and ref
4331                   (setq ref-dep (intern-soft ref dependencies))
4332                   (boundp ref-dep)
4333                   (setq ref-header (car (symbol-value ref-dep))))
4334         (if (string= id ref)
4335             ;; Yuk!  This is a reference loop.  Make the article be a
4336             ;; root article.
4337             (progn
4338               (mail-header-set-references (car (symbol-value id-dep)) "none")
4339               (setq ref nil)
4340               (setq parent-id nil))
4341           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4342       (setq ref-dep (intern (or parent-id "none") dependencies))
4343       (if (boundp ref-dep)
4344           (setcdr (symbol-value ref-dep)
4345                   (nconc (cdr (symbol-value ref-dep))
4346                          (list (symbol-value id-dep))))
4347         (set ref-dep (list nil (symbol-value id-dep)))))
4348     header))
4349
4350 (defun gnus-extract-message-id-from-in-reply-to (string)
4351   (if (string-match "<[^>]+>" string)
4352       (substring string (match-beginning 0) (match-end 0))
4353     nil))
4354
4355 (defun gnus-build-sparse-threads ()
4356   (let ((headers gnus-newsgroup-headers)
4357         (mail-parse-charset gnus-newsgroup-charset)
4358         (gnus-summary-ignore-duplicates t)
4359         header references generation relations
4360         subject child end new-child date)
4361     ;; First we create an alist of generations/relations, where
4362     ;; generations is how much we trust the relation, and the relation
4363     ;; is parent/child.
4364     (gnus-message 7 "Making sparse threads...")
4365     (save-excursion
4366       (nnheader-set-temp-buffer " *gnus sparse threads*")
4367       (while (setq header (pop headers))
4368         (when (and (setq references (mail-header-references header))
4369                    (not (string= references "")))
4370           (insert references)
4371           (setq child (mail-header-id header)
4372                 subject (mail-header-subject header)
4373                 date (mail-header-date header)
4374                 generation 0)
4375           (while (search-backward ">" nil t)
4376             (setq end (1+ (point)))
4377             (when (search-backward "<" nil t)
4378               (setq new-child (buffer-substring (point) end))
4379               (push (list (incf generation)
4380                           child (setq child new-child)
4381                           subject date)
4382                     relations)))
4383           (when child
4384             (push (list (1+ generation) child nil subject) relations))
4385           (erase-buffer)))
4386       (kill-buffer (current-buffer)))
4387     ;; Sort over trustworthiness.
4388     (dolist (relation (sort relations 'car-less-than-car))
4389       (when (gnus-dependencies-add-header
4390              (make-full-mail-header
4391               gnus-reffed-article-number
4392               (nth 3 relation) "" (or (nth 4 relation) "")
4393               (nth 1 relation)
4394               (or (nth 2 relation) "") 0 0 "")
4395              gnus-newsgroup-dependencies nil)
4396         (push gnus-reffed-article-number gnus-newsgroup-limit)
4397         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4398         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4399               gnus-newsgroup-reads)
4400         (decf gnus-reffed-article-number)))
4401     (gnus-message 7 "Making sparse threads...done")))
4402
4403 (defun gnus-build-old-threads ()
4404   ;; Look at all the articles that refer back to old articles, and
4405   ;; fetch the headers for the articles that aren't there.  This will
4406   ;; build complete threads - if the roots haven't been expired by the
4407   ;; server, that is.
4408   (let ((mail-parse-charset gnus-newsgroup-charset)
4409         id heads)
4410     (mapatoms
4411      (lambda (refs)
4412        (when (not (car (symbol-value refs)))
4413          (setq heads (cdr (symbol-value refs)))
4414          (while heads
4415            (if (memq (mail-header-number (caar heads))
4416                      gnus-newsgroup-dormant)
4417                (setq heads (cdr heads))
4418              (setq id (symbol-name refs))
4419              (while (and (setq id (gnus-build-get-header id))
4420                          (not (car (gnus-id-to-thread id)))))
4421              (setq heads nil)))))
4422      gnus-newsgroup-dependencies)))
4423
4424 (defsubst gnus-remove-odd-characters (string)
4425   "Translate STRING into something that doesn't contain weird characters."
4426   (mm-subst-char-in-string
4427    ?\r ?\-
4428    (mm-subst-char-in-string ?\n ?\- string t) t))
4429
4430 ;; This function has to be called with point after the article number
4431 ;; on the beginning of the line.
4432 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4433   (let ((eol (point-at-eol))
4434         (buffer (current-buffer))
4435         header references in-reply-to)
4436
4437     ;; overview: [num subject from date id refs chars lines misc]
4438     (unwind-protect
4439         (let (x)
4440           (narrow-to-region (point) eol)
4441           (unless (eobp)
4442             (forward-char))
4443
4444           (setq header
4445                 (make-full-mail-header
4446                  number                 ; number
4447                  (condition-case ()     ; subject
4448                      (gnus-remove-odd-characters
4449                       (funcall gnus-decode-encoded-word-function
4450                                (setq x (nnheader-nov-field))))
4451                    (error x))
4452                  (condition-case ()     ; from
4453                      (gnus-remove-odd-characters
4454                       (funcall gnus-decode-encoded-address-function
4455                                (setq x (nnheader-nov-field))))
4456                    (error x))
4457                  (nnheader-nov-field)   ; date
4458                  (nnheader-nov-read-message-id number)  ; id
4459                  (setq references (nnheader-nov-field)) ; refs
4460                  (nnheader-nov-read-integer) ; chars
4461                  (nnheader-nov-read-integer) ; lines
4462                  (unless (eobp)
4463                    (if (looking-at "Xref: ")
4464                        (goto-char (match-end 0)))
4465                    (nnheader-nov-field)) ; Xref
4466                  (nnheader-nov-parse-extra)))) ; extra
4467
4468       (widen))
4469
4470     (when (and (string= references "")
4471                (setq in-reply-to (mail-header-extra header))
4472                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4473       (mail-header-set-references
4474        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4475
4476     (when gnus-alter-header-function
4477       (funcall gnus-alter-header-function header))
4478     (gnus-dependencies-add-header header dependencies force-new)))
4479
4480 (defun gnus-build-get-header (id)
4481   "Look through the buffer of NOV lines and find the header to ID.
4482 Enter this line into the dependencies hash table, and return
4483 the id of the parent article (if any)."
4484   (let ((deps gnus-newsgroup-dependencies)
4485         found header)
4486     (prog1
4487         (with-current-buffer nntp-server-buffer
4488           (let ((case-fold-search nil))
4489             (goto-char (point-min))
4490             (while (and (not found)
4491                         (search-forward id nil t))
4492               (beginning-of-line)
4493               (setq found (looking-at
4494                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4495                                    (regexp-quote id))))
4496               (or found (beginning-of-line 2)))
4497             (when found
4498               (beginning-of-line)
4499               (and
4500                (setq header (gnus-nov-parse-line
4501                              (read (current-buffer)) deps))
4502                (gnus-parent-id (mail-header-references header))))))
4503       (when header
4504         (let ((number (mail-header-number header)))
4505           (push number gnus-newsgroup-limit)
4506           (push header gnus-newsgroup-headers)
4507           (if (memq number gnus-newsgroup-unselected)
4508               (progn
4509                 (setq gnus-newsgroup-unreads
4510                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4511                                                number))
4512                 (setq gnus-newsgroup-unselected
4513                       (delq number gnus-newsgroup-unselected)))
4514             (push number gnus-newsgroup-ancient)))))))
4515
4516 (defun gnus-build-all-threads ()
4517   "Read all the headers."
4518   (let ((gnus-summary-ignore-duplicates t)
4519         (mail-parse-charset gnus-newsgroup-charset)
4520         (dependencies gnus-newsgroup-dependencies)
4521         header article)
4522     (with-current-buffer nntp-server-buffer
4523       (let ((case-fold-search nil))
4524         (goto-char (point-min))
4525         (while (not (eobp))
4526           (ignore-errors
4527             (setq article (read (current-buffer))
4528                   header (gnus-nov-parse-line article dependencies t)))
4529           (when header
4530             (with-current-buffer gnus-summary-buffer
4531               (push header gnus-newsgroup-headers)
4532               (if (memq (setq article (mail-header-number header))
4533                         gnus-newsgroup-unselected)
4534                   (progn
4535                     (setq gnus-newsgroup-unreads
4536                           (gnus-add-to-sorted-list
4537                            gnus-newsgroup-unreads article))
4538                     (setq gnus-newsgroup-unselected
4539                           (delq article gnus-newsgroup-unselected)))
4540                 (push article gnus-newsgroup-ancient)))
4541             (forward-line 1)))))))
4542
4543 (defun gnus-summary-update-article-line (article header)
4544   "Update the line for ARTICLE using HEADER."
4545   (let* ((id (mail-header-id header))
4546          (thread (gnus-id-to-thread id)))
4547     (unless thread
4548       (error "Article in no thread"))
4549     ;; Update the thread.
4550     (setcar thread header)
4551     (gnus-summary-goto-subject article)
4552     (let* ((datal (gnus-data-find-list article))
4553            (data (car datal))
4554            (inhibit-read-only t)
4555            (level (gnus-summary-thread-level)))
4556       (gnus-delete-line)
4557       (let ((inserted (- (point)
4558                          (progn
4559                            (gnus-summary-insert-line
4560                             header level nil
4561                             (memq article gnus-newsgroup-undownloaded)
4562                             (gnus-article-mark article)
4563                             (memq article gnus-newsgroup-replied)
4564                             (memq article gnus-newsgroup-expirable)
4565                             ;; Only insert the Subject string when it's different
4566                             ;; from the previous Subject string.
4567                             (if (and
4568                                  gnus-show-threads
4569                                  (gnus-subject-equal
4570                                   (condition-case ()
4571                                       (mail-header-subject
4572                                        (gnus-data-header
4573                                         (cadr
4574                                          (gnus-data-find-list
4575                                           article
4576                                           (gnus-data-list t)))))
4577                                     ;; Error on the side of excessive subjects.
4578                                     (error ""))
4579                                   (mail-header-subject header)))
4580                                 ""
4581                               (mail-header-subject header))
4582                             nil (cdr (assq article gnus-newsgroup-scored))
4583                             (memq article gnus-newsgroup-processable))
4584                            (point)))))
4585         (when (cdr datal)
4586           (gnus-data-update-list
4587            (cdr datal)
4588            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4589
4590 (defun gnus-summary-update-article (article &optional iheader)
4591   "Update ARTICLE in the summary buffer."
4592   (set-buffer gnus-summary-buffer)
4593   (let* ((header (gnus-summary-article-header article))
4594          (id (mail-header-id header))
4595          (data (gnus-data-find article))
4596          (thread (gnus-id-to-thread id))
4597          (references (mail-header-references header))
4598          (parent
4599           (gnus-id-to-thread
4600            (or (gnus-parent-id
4601                 (when (and references
4602                            (not (equal "" references)))
4603                   references))
4604                "none")))
4605          (inhibit-read-only t)
4606          (old (car thread)))
4607     (when thread
4608       (unless iheader
4609         (setcar thread nil)
4610         (when parent
4611           (delq thread parent)))
4612       (if (gnus-summary-insert-subject id header)
4613           ;; Set the (possibly) new article number in the data structure.
4614           (gnus-data-set-number data (gnus-id-to-article id))
4615         (setcar thread old)
4616         nil))))
4617
4618 (defun gnus-rebuild-thread (id &optional line)
4619   "Rebuild the thread containing ID.
4620 If LINE, insert the rebuilt thread starting on line LINE."
4621   (let ((inhibit-read-only t)
4622         old-pos current thread data)
4623     (if (not gnus-show-threads)
4624         (setq thread (list (car (gnus-id-to-thread id))))
4625       ;; Get the thread this article is part of.
4626       (setq thread (gnus-remove-thread id)))
4627     (setq old-pos (point-at-bol))
4628     (setq current (save-excursion
4629                     (and (re-search-backward "[\r\n]" nil t)
4630                          (gnus-summary-article-number))))
4631     ;; If this is a gathered thread, we have to go some re-gathering.
4632     (when (stringp (car thread))
4633       (let ((subject (car thread))
4634             roots thr)
4635         (setq thread (cdr thread))
4636         (while thread
4637           (unless (memq (setq thr (gnus-id-to-thread
4638                                    (gnus-root-id
4639                                     (mail-header-id (caar thread)))))
4640                         roots)
4641             (push thr roots))
4642           (setq thread (cdr thread)))
4643         ;; We now have all (unique) roots.
4644         (if (= (length roots) 1)
4645             ;; All the loose roots are now one solid root.
4646             (setq thread (car roots))
4647           (setq thread (cons subject (gnus-sort-threads roots))))))
4648     (let (threads)
4649       ;; We then insert this thread into the summary buffer.
4650       (when line
4651         (goto-char (point-min))
4652         (forward-line (1- line)))
4653       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4654         (if gnus-show-threads
4655             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4656           (gnus-summary-prepare-unthreaded thread))
4657         (setq data (nreverse gnus-newsgroup-data))
4658         (setq threads gnus-newsgroup-threads))
4659       ;; We splice the new data into the data structure.
4660       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4661       ;;!!! then we want to insert at the beginning of the buffer.
4662       ;;!!! That happens to be true with Gnus now, but that may
4663       ;;!!! change in the future.  Perhaps.
4664       (gnus-data-enter-list
4665        (if line nil current) data (- (point) old-pos))
4666       (setq gnus-newsgroup-threads
4667             (nconc threads gnus-newsgroup-threads))
4668       (gnus-data-compute-positions))))
4669
4670 (defun gnus-number-to-header (number)
4671   "Return the header for article NUMBER."
4672   (let ((headers gnus-newsgroup-headers))
4673     (while (and headers
4674                 (not (= number (mail-header-number (car headers)))))
4675       (pop headers))
4676     (when headers
4677       (car headers))))
4678
4679 (defun gnus-parent-headers (in-headers &optional generation)
4680   "Return the headers of the GENERATIONeth parent of HEADERS."
4681   (unless generation
4682     (setq generation 1))
4683   (let ((parent t)
4684         (headers in-headers)
4685         references)
4686     (while (and parent
4687                 (not (zerop generation))
4688                 (setq references (mail-header-references headers)))
4689       (setq headers (if (and references
4690                              (setq parent (gnus-parent-id references)))
4691                         (car (gnus-id-to-thread parent))
4692                       nil))
4693       (decf generation))
4694     (and (not (eq headers in-headers))
4695          headers)))
4696
4697 (defun gnus-id-to-thread (id)
4698   "Return the (sub-)thread where ID appears."
4699   (gnus-gethash id gnus-newsgroup-dependencies))
4700
4701 (defun gnus-id-to-article (id)
4702   "Return the article number of ID."
4703   (let ((thread (gnus-id-to-thread id)))
4704     (when (and thread
4705                (car thread))
4706       (mail-header-number (car thread)))))
4707
4708 (defun gnus-id-to-header (id)
4709   "Return the article headers of ID."
4710   (car (gnus-id-to-thread id)))
4711
4712 (defun gnus-article-displayed-root-p (article)
4713   "Say whether ARTICLE is a root(ish) article."
4714   (let ((level (gnus-summary-thread-level article))
4715         (refs (mail-header-references  (gnus-summary-article-header article)))
4716         particle)
4717     (cond
4718      ((null level) nil)
4719      ((zerop level) t)
4720      ((null refs) t)
4721      ((null (gnus-parent-id refs)) t)
4722      ((and (= 1 level)
4723            (null (setq particle (gnus-id-to-article
4724                                  (gnus-parent-id refs))))
4725            (null (gnus-summary-thread-level particle)))))))
4726
4727 (defun gnus-root-id (id)
4728   "Return the id of the root of the thread where ID appears."
4729   (let (last-id prev)
4730     (while (and id (setq prev (car (gnus-id-to-thread id))))
4731       (setq last-id id
4732             id (gnus-parent-id (mail-header-references prev))))
4733     last-id))
4734
4735 (defun gnus-articles-in-thread (thread)
4736   "Return the list of articles in THREAD."
4737   (cons (mail-header-number (car thread))
4738         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4739
4740 (defun gnus-remove-thread (id &optional dont-remove)
4741   "Remove the thread that has ID in it."
4742   (let (headers thread last-id)
4743     ;; First go up in this thread until we find the root.
4744     (setq last-id (gnus-root-id id)
4745           headers (message-flatten-list (gnus-id-to-thread last-id)))
4746     ;; We have now found the real root of this thread.  It might have
4747     ;; been gathered into some loose thread, so we have to search
4748     ;; through the threads to find the thread we wanted.
4749     (let ((threads gnus-newsgroup-threads)
4750           sub)
4751       (while threads
4752         (setq sub (car threads))
4753         (if (stringp (car sub))
4754             ;; This is a gathered thread, so we look at the roots
4755             ;; below it to find whether this article is in this
4756             ;; gathered root.
4757             (progn
4758               (setq sub (cdr sub))
4759               (while sub
4760                 (when (member (caar sub) headers)
4761                   (setq thread (car threads)
4762                         threads nil
4763                         sub nil))
4764                 (setq sub (cdr sub))))
4765           ;; It's an ordinary thread, so we check it.
4766           (when (eq (car sub) (car headers))
4767             (setq thread sub
4768                   threads nil)))
4769         (setq threads (cdr threads)))
4770       ;; If this article is in no thread, then it's a root.
4771       (if thread
4772           (unless dont-remove
4773             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4774         (setq thread (gnus-id-to-thread last-id)))
4775       (when thread
4776         (prog1
4777             thread                      ; We return this thread.
4778           (unless dont-remove
4779             (if (stringp (car thread))
4780                 (progn
4781                   ;; If we use dummy roots, then we have to remove the
4782                   ;; dummy root as well.
4783                   (when (eq gnus-summary-make-false-root 'dummy)
4784                     ;; We go to the dummy root by going to
4785                     ;; the first sub-"thread", and then one line up.
4786                     (gnus-summary-goto-article
4787                      (mail-header-number (caadr thread)))
4788                     (forward-line -1)
4789                     (gnus-delete-line)
4790                     (gnus-data-compute-positions))
4791                   (setq thread (cdr thread))
4792                   (while thread
4793                     (gnus-remove-thread-1 (car thread))
4794                     (setq thread (cdr thread))))
4795               (gnus-remove-thread-1 thread))))))))
4796
4797 (defun gnus-remove-thread-1 (thread)
4798   "Remove the thread THREAD recursively."
4799   (let ((number (mail-header-number (pop thread)))
4800         d)
4801     (setq thread (reverse thread))
4802     (while thread
4803       (gnus-remove-thread-1 (pop thread)))
4804     (when (setq d (gnus-data-find number))
4805       (goto-char (gnus-data-pos d))
4806       (gnus-summary-show-thread)
4807       (gnus-data-remove
4808        number
4809        (- (point-at-bol)
4810           (prog1
4811               (1+ (point-at-eol))
4812             (gnus-delete-line)))))))
4813
4814 (defun gnus-sort-threads-recursive (threads func)
4815   (sort (mapcar (lambda (thread)
4816                   (cons (car thread)
4817                         (and (cdr thread)
4818                              (gnus-sort-threads-recursive (cdr thread) func))))
4819                 threads) func))
4820
4821 (defun gnus-sort-threads-loop (threads func)
4822   (let* ((superthread (cons nil threads))
4823          (stack (list (cons superthread threads)))
4824          remaining-threads thread)
4825     (while stack
4826       (setq remaining-threads (cdr (car stack)))
4827       (if remaining-threads
4828           (progn (setq thread (car remaining-threads))
4829                  (setcdr (car stack) (cdr remaining-threads))
4830                  (if (cdr thread)
4831                      (push (cons thread (cdr thread)) stack)))
4832         (setq thread (caar stack))
4833         (setcdr thread (sort (cdr thread) func))
4834         (pop stack)))
4835     (cdr superthread)))
4836
4837 (defun gnus-sort-threads (threads)
4838   "Sort THREADS."
4839   (if (not gnus-thread-sort-functions)
4840       threads
4841     (gnus-message 8 "Sorting threads...")
4842     (prog1
4843         (condition-case nil
4844             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4845               (gnus-sort-threads-recursive
4846                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4847           ;; Even after binding max-lisp-eval-depth, the recursive
4848           ;; sorter might fail for very long threads.  In that case,
4849           ;; try using a (less well-tested) non-recursive sorter.
4850           (error (gnus-message 9 "Sorting threads with loop...")
4851                  (gnus-sort-threads-loop
4852                   threads (gnus-make-sort-function
4853                            gnus-thread-sort-functions))))
4854       (gnus-message 8 "Sorting threads...done"))))
4855
4856 (defun gnus-sort-articles (articles)
4857   "Sort ARTICLES."
4858   (when gnus-article-sort-functions
4859     (gnus-message 7 "Sorting articles...")
4860     (prog1
4861         (setq gnus-newsgroup-headers
4862               (sort articles (gnus-make-sort-function
4863                               gnus-article-sort-functions)))
4864       (gnus-message 7 "Sorting articles...done"))))
4865
4866 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4867 (defmacro gnus-thread-header (thread)
4868   "Return header of first article in THREAD.
4869 Note that THREAD must never, ever be anything else than a variable -
4870 using some other form will lead to serious barfage."
4871   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4872   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4873   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4874         (vector thread) 2))
4875
4876 (defsubst gnus-article-sort-by-number (h1 h2)
4877   "Sort articles by article number."
4878   (< (mail-header-number h1)
4879      (mail-header-number h2)))
4880
4881 (defun gnus-thread-sort-by-number (h1 h2)
4882   "Sort threads by root article number."
4883   (gnus-article-sort-by-number
4884    (gnus-thread-header h1) (gnus-thread-header h2)))
4885
4886 (defsubst gnus-article-sort-by-random (h1 h2)
4887   "Sort articles randomly."
4888   (zerop (random 2)))
4889
4890 (defun gnus-thread-sort-by-random (h1 h2)
4891   "Sort threads randomly."
4892   (gnus-article-sort-by-random
4893    (gnus-thread-header h1) (gnus-thread-header h2)))
4894
4895 (defsubst gnus-article-sort-by-lines (h1 h2)
4896   "Sort articles by article Lines header."
4897   (< (mail-header-lines h1)
4898      (mail-header-lines h2)))
4899
4900 (defun gnus-thread-sort-by-lines (h1 h2)
4901   "Sort threads by root article Lines header."
4902   (gnus-article-sort-by-lines
4903    (gnus-thread-header h1) (gnus-thread-header h2)))
4904
4905 (defsubst gnus-article-sort-by-chars (h1 h2)
4906   "Sort articles by octet length."
4907   (< (mail-header-chars h1)
4908      (mail-header-chars h2)))
4909
4910 (defun gnus-thread-sort-by-chars (h1 h2)
4911   "Sort threads by root article octet length."
4912   (gnus-article-sort-by-chars
4913    (gnus-thread-header h1) (gnus-thread-header h2)))
4914
4915 (defsubst gnus-article-sort-by-author (h1 h2)
4916   "Sort articles by root author."
4917   (gnus-string<
4918    (let ((extract (funcall
4919                    gnus-extract-address-components
4920                    (mail-header-from h1))))
4921      (or (car extract) (cadr extract) ""))
4922    (let ((extract (funcall
4923                    gnus-extract-address-components
4924                    (mail-header-from h2))))
4925      (or (car extract) (cadr extract) ""))))
4926
4927 (defun gnus-thread-sort-by-author (h1 h2)
4928   "Sort threads by root author."
4929   (gnus-article-sort-by-author
4930    (gnus-thread-header h1)  (gnus-thread-header h2)))
4931
4932 (defsubst gnus-article-sort-by-recipient (h1 h2)
4933   "Sort articles by recipient."
4934   (gnus-string<
4935    (let ((extract (funcall
4936                    gnus-extract-address-components
4937                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4938      (or (car extract) (cadr extract)))
4939    (let ((extract (funcall
4940                    gnus-extract-address-components
4941                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4942      (or (car extract) (cadr extract)))))
4943
4944 (defun gnus-thread-sort-by-recipient (h1 h2)
4945   "Sort threads by root recipient."
4946   (gnus-article-sort-by-recipient
4947    (gnus-thread-header h1) (gnus-thread-header h2)))
4948
4949 (defsubst gnus-article-sort-by-subject (h1 h2)
4950   "Sort articles by root subject."
4951   (gnus-string<
4952    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4953    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4954
4955 (defun gnus-thread-sort-by-subject (h1 h2)
4956   "Sort threads by root subject."
4957   (gnus-article-sort-by-subject
4958    (gnus-thread-header h1) (gnus-thread-header h2)))
4959
4960 (defsubst gnus-article-sort-by-date (h1 h2)
4961   "Sort articles by root article date."
4962   (time-less-p
4963    (gnus-date-get-time (mail-header-date h1))
4964    (gnus-date-get-time (mail-header-date h2))))
4965
4966 (defun gnus-thread-sort-by-date (h1 h2)
4967   "Sort threads by root article date."
4968   (gnus-article-sort-by-date
4969    (gnus-thread-header h1) (gnus-thread-header h2)))
4970
4971 (defsubst gnus-article-sort-by-score (h1 h2)
4972   "Sort articles by root article score.
4973 Unscored articles will be counted as having a score of zero."
4974   (> (or (cdr (assq (mail-header-number h1)
4975                     gnus-newsgroup-scored))
4976          gnus-summary-default-score 0)
4977      (or (cdr (assq (mail-header-number h2)
4978                     gnus-newsgroup-scored))
4979          gnus-summary-default-score 0)))
4980
4981 (defun gnus-thread-sort-by-score (h1 h2)
4982   "Sort threads by root article score."
4983   (gnus-article-sort-by-score
4984    (gnus-thread-header h1) (gnus-thread-header h2)))
4985
4986 (defun gnus-thread-sort-by-total-score (h1 h2)
4987   "Sort threads by the sum of all scores in the thread.
4988 Unscored articles will be counted as having a score of zero."
4989   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4990
4991 (defun gnus-thread-total-score (thread)
4992   ;; This function find the total score of THREAD.
4993   (cond
4994    ((null thread)
4995     0)
4996    ((consp thread)
4997     (if (stringp (car thread))
4998         (apply gnus-thread-score-function 0
4999                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5000       (gnus-thread-total-score-1 thread)))
5001    (t
5002     (gnus-thread-total-score-1 (list thread)))))
5003
5004 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5005   "Sort articles by number."
5006   (gnus-article-sort-by-number h1 h2))
5007
5008 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5009   "Sort threads such that the thread with the most recently arrived article comes first."
5010   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5011
5012 (defun gnus-thread-highest-number (thread)
5013   "Return the highest article number in THREAD."
5014   (apply 'max (mapcar (lambda (header)
5015                         (mail-header-number header))
5016                       (message-flatten-list thread))))
5017
5018 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5019   "Sort articles by number."
5020   (gnus-article-sort-by-date h1 h2))
5021
5022 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5023   "Sort threads such that the thread with the most recently dated article comes first."
5024   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5025
5026 ; Since this is called not only to sort the top-level threads, but
5027 ; also in recursive sorts to order the articles within a thread, each
5028 ; article will be processed many times.  Thus it speeds things up
5029 ; quite a bit to use gnus-date-get-time, which caches the time value.
5030 (defun gnus-thread-latest-date (thread)
5031   "Return the highest article date in THREAD."
5032   (apply 'max
5033          (mapcar (lambda (header) (gnus-float-time
5034                                    (gnus-date-get-time
5035                                     (mail-header-date header))))
5036                  (message-flatten-list thread))))
5037
5038 (defun gnus-thread-total-score-1 (root)
5039   ;; This function find the total score of the thread below ROOT.
5040   (setq root (car root))
5041   (apply gnus-thread-score-function
5042          (or (append
5043               (mapcar 'gnus-thread-total-score
5044                       (cdr (gnus-id-to-thread (mail-header-id root))))
5045               (when (> (mail-header-number root) 0)
5046                 (list (or (cdr (assq (mail-header-number root)
5047                                      gnus-newsgroup-scored))
5048                           gnus-summary-default-score 0))))
5049              (list gnus-summary-default-score)
5050              '(0))))
5051
5052 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5053 (defvar gnus-tmp-prev-subject nil)
5054 (defvar gnus-tmp-false-parent nil)
5055 (defvar gnus-tmp-root-expunged nil)
5056 (defvar gnus-tmp-dummy-line nil)
5057
5058 (defun gnus-extra-header (type &optional header)
5059   "Return the extra header of TYPE."
5060   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5061       ""))
5062
5063 (defvar gnus-tmp-thread-tree-header-string "")
5064
5065 (defcustom gnus-sum-thread-tree-root "> "
5066   "With %B spec, used for the root of a thread.
5067 If nil, use subject instead."
5068   :version "22.1"
5069   :type '(radio (const :format "%v  " nil) string)
5070   :group 'gnus-thread)
5071
5072 (defcustom gnus-sum-thread-tree-false-root "> "
5073   "With %B spec, used for a false root of a thread.
5074 If nil, use subject instead."
5075   :version "22.1"
5076   :type '(radio (const :format "%v  " nil) string)
5077   :group 'gnus-thread)
5078
5079 (defcustom gnus-sum-thread-tree-single-indent ""
5080   "With %B spec, used for a thread with just one message.
5081 If nil, use subject instead."
5082   :version "22.1"
5083   :type '(radio (const :format "%v  " nil) string)
5084   :group 'gnus-thread)
5085
5086 (defcustom gnus-sum-thread-tree-vertical "| "
5087   "With %B spec, used for drawing a vertical line."
5088   :version "22.1"
5089   :type 'string
5090   :group 'gnus-thread)
5091
5092 (defcustom gnus-sum-thread-tree-indent "  "
5093   "With %B spec, used for indenting."
5094   :version "22.1"
5095   :type 'string
5096   :group 'gnus-thread)
5097
5098 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5099   "With %B spec, used for a leaf with brothers."
5100   :version "22.1"
5101   :type 'string
5102   :group 'gnus-thread)
5103
5104 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5105   "With %B spec, used for a leaf without brothers."
5106   :version "22.1"
5107   :type 'string
5108   :group 'gnus-thread)
5109
5110 (defcustom gnus-summary-display-while-building nil
5111   "If non-nil, show and update the summary buffer as it's being built.
5112 If the value is t, update the buffer after every line is inserted.  If
5113 the value is an integer (N), update the display every N lines."
5114   :version "22.1"
5115   :group 'gnus-thread
5116   :type '(choice (const :tag "off" nil)
5117                  number
5118                  (const :tag "frequently" t)))
5119
5120 (defun gnus-summary-prepare-threads (threads)
5121   "Prepare summary buffer from THREADS and indentation LEVEL.
5122 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5123 or a straight list of headers."
5124   (gnus-message 7 "Generating summary...")
5125
5126   (setq gnus-newsgroup-threads threads)
5127   (beginning-of-line)
5128
5129   (let ((gnus-tmp-level 0)
5130         (default-score (or gnus-summary-default-score 0))
5131         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5132         (building-line-count gnus-summary-display-while-building)
5133         (building-count (integerp gnus-summary-display-while-building))
5134         thread number subject stack state gnus-tmp-gathered beg-match
5135         new-roots gnus-tmp-new-adopts thread-end simp-subject
5136         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5137         gnus-tmp-replied gnus-tmp-subject-or-nil
5138         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5139         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5140         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5141         tree-stack)
5142
5143     (setq gnus-tmp-prev-subject nil
5144           gnus-tmp-thread-tree-header-string "")
5145
5146     (if (vectorp (car threads))
5147         ;; If this is a straight (sic) list of headers, then a
5148         ;; threaded summary display isn't required, so we just create
5149         ;; an unthreaded one.
5150         (gnus-summary-prepare-unthreaded threads)
5151
5152       ;; Do the threaded display.
5153
5154       (if gnus-summary-display-while-building
5155           (switch-to-buffer (buffer-name)))
5156       (while (or threads stack gnus-tmp-new-adopts new-roots)
5157
5158         (if (and (= gnus-tmp-level 0)
5159                  (or (not stack)
5160                      (= (caar stack) 0))
5161                  (not gnus-tmp-false-parent)
5162                  (or gnus-tmp-new-adopts new-roots))
5163             (if gnus-tmp-new-adopts
5164                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5165                       thread (list (car gnus-tmp-new-adopts))
5166                       gnus-tmp-header (caar thread)
5167                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5168               (when new-roots
5169                 (setq thread (list (car new-roots))
5170                       gnus-tmp-header (caar thread)
5171                       new-roots (cdr new-roots))))
5172
5173           (if threads
5174               ;; If there are some threads, we do them before the
5175               ;; threads on the stack.
5176               (setq thread threads
5177                     gnus-tmp-header (caar thread))
5178             ;; There were no current threads, so we pop something off
5179             ;; the stack.
5180             (setq state (car stack)
5181                   gnus-tmp-level (car state)
5182                   tree-stack (cadr state)
5183                   thread (caddr state)
5184                   stack (cdr stack)
5185                   gnus-tmp-header (caar thread))))
5186
5187         (setq gnus-tmp-false-parent nil)
5188         (setq gnus-tmp-root-expunged nil)
5189         (setq thread-end nil)
5190
5191         (if (stringp gnus-tmp-header)
5192             ;; The header is a dummy root.
5193             (cond
5194              ((eq gnus-summary-make-false-root 'adopt)
5195               ;; We let the first article adopt the rest.
5196               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5197                                                (cddar thread)))
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 thread (cons (list (caar thread)
5204                                        (cadar thread))
5205                                  (cdr thread)))
5206               (setq gnus-tmp-level -1
5207                     gnus-tmp-false-parent t))
5208              ((eq gnus-summary-make-false-root 'empty)
5209               ;; We print adopted articles with empty subject fields.
5210               (setq gnus-tmp-gathered
5211                     (nconc (mapcar
5212                             (lambda (h) (mail-header-number (car h)))
5213                             (cddar thread))
5214                            gnus-tmp-gathered))
5215               (setq gnus-tmp-level -1))
5216              ((eq gnus-summary-make-false-root 'dummy)
5217               ;; We remember that we probably want to output a dummy
5218               ;; root.
5219               (setq gnus-tmp-dummy-line gnus-tmp-header)
5220               (setq gnus-tmp-prev-subject gnus-tmp-header))
5221              (t
5222               ;; We do not make a root for the gathered
5223               ;; sub-threads at all.
5224               (setq gnus-tmp-level -1)))
5225
5226           (setq number (mail-header-number gnus-tmp-header)
5227                 subject (mail-header-subject gnus-tmp-header)
5228                 simp-subject (gnus-simplify-subject-fully subject))
5229
5230           (cond
5231            ;; If the thread has changed subject, we might want to make
5232            ;; this subthread into a root.
5233            ((and (null gnus-thread-ignore-subject)
5234                  (not (zerop gnus-tmp-level))
5235                  gnus-tmp-prev-subject
5236                  (not (string= gnus-tmp-prev-subject simp-subject)))
5237             (setq new-roots (nconc new-roots (list (car thread)))
5238                   thread-end t
5239                   gnus-tmp-header nil))
5240            ;; If the article lies outside the current limit,
5241            ;; then we do not display it.
5242            ((not (memq number gnus-newsgroup-limit))
5243             (setq gnus-tmp-gathered
5244                   (nconc (mapcar
5245                           (lambda (h) (mail-header-number (car h)))
5246                           (cdar thread))
5247                          gnus-tmp-gathered))
5248             (setq gnus-tmp-new-adopts (if (cdar thread)
5249                                           (append gnus-tmp-new-adopts
5250                                                   (cdar thread))
5251                                         gnus-tmp-new-adopts)
5252                   thread-end t
5253                   gnus-tmp-header nil)
5254             (when (zerop gnus-tmp-level)
5255               (setq gnus-tmp-root-expunged t)))
5256            ;; Perhaps this article is to be marked as read?
5257            ((and gnus-summary-mark-below
5258                  (< (or (cdr (assq number gnus-newsgroup-scored))
5259                         default-score)
5260                     gnus-summary-mark-below)
5261                  ;; Don't touch sparse articles.
5262                  (not (gnus-summary-article-sparse-p number))
5263                  (not (gnus-summary-article-ancient-p number)))
5264             (setq gnus-newsgroup-unreads
5265                   (delq number gnus-newsgroup-unreads))
5266             (if gnus-newsgroup-auto-expire
5267                 (setq gnus-newsgroup-expirable
5268                       (gnus-add-to-sorted-list
5269                        gnus-newsgroup-expirable number))
5270               (push (cons number gnus-low-score-mark)
5271                     gnus-newsgroup-reads))))
5272
5273           (when gnus-tmp-header
5274             ;; We may have an old dummy line to output before this
5275             ;; article.
5276             (when (and gnus-tmp-dummy-line
5277                        (gnus-subject-equal
5278                         gnus-tmp-dummy-line
5279                         (mail-header-subject gnus-tmp-header)))
5280               (gnus-summary-insert-dummy-line
5281                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5282               (setq gnus-tmp-dummy-line nil))
5283
5284             ;; Compute the mark.
5285             (setq gnus-tmp-unread (gnus-article-mark number))
5286
5287             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5288                                   gnus-tmp-header gnus-tmp-level)
5289                   gnus-newsgroup-data)
5290
5291             ;; Actually insert the line.
5292             (setq
5293              gnus-tmp-subject-or-nil
5294              (cond
5295               ((and gnus-thread-ignore-subject
5296                     gnus-tmp-prev-subject
5297                     (not (string= gnus-tmp-prev-subject simp-subject)))
5298                subject)
5299               ((zerop gnus-tmp-level)
5300                (if (and (eq gnus-summary-make-false-root 'empty)
5301                         (memq number gnus-tmp-gathered)
5302                         gnus-tmp-prev-subject
5303                         (string= gnus-tmp-prev-subject simp-subject))
5304                    gnus-summary-same-subject
5305                  subject))
5306               (t gnus-summary-same-subject)))
5307             (if (and (eq gnus-summary-make-false-root 'adopt)
5308                      (= gnus-tmp-level 1)
5309                      (memq number gnus-tmp-gathered))
5310                 (setq gnus-tmp-opening-bracket ?\<
5311                       gnus-tmp-closing-bracket ?\>)
5312               (setq gnus-tmp-opening-bracket ?\[
5313                     gnus-tmp-closing-bracket ?\]))
5314             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5315                 (gnus-make-thread-indent-array
5316                  (max (* 2 (length gnus-thread-indent-array))
5317                       gnus-tmp-level)))
5318             (setq
5319              gnus-tmp-indentation
5320              (aref gnus-thread-indent-array gnus-tmp-level)
5321              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5322              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5323                                 gnus-summary-default-score 0)
5324              gnus-tmp-score-char
5325              (if (or (null gnus-summary-default-score)
5326                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5327                          gnus-summary-zcore-fuzz))
5328                  ?                      ;Whitespace
5329                (if (< gnus-tmp-score gnus-summary-default-score)
5330                    gnus-score-below-mark gnus-score-over-mark))
5331              gnus-tmp-replied
5332              (cond ((memq number gnus-newsgroup-processable)
5333                     gnus-process-mark)
5334                    ((memq number gnus-newsgroup-cached)
5335                     gnus-cached-mark)
5336                    ((memq number gnus-newsgroup-replied)
5337                     gnus-replied-mark)
5338                    ((memq number gnus-newsgroup-forwarded)
5339                     gnus-forwarded-mark)
5340                    ((memq number gnus-newsgroup-saved)
5341                     gnus-saved-mark)
5342                    ((memq number gnus-newsgroup-recent)
5343                     gnus-recent-mark)
5344                    ((memq number gnus-newsgroup-unseen)
5345                     gnus-unseen-mark)
5346                    (t gnus-no-mark))
5347              gnus-tmp-downloaded
5348              (cond ((memq number gnus-newsgroup-undownloaded)
5349                     gnus-undownloaded-mark)
5350                    (gnus-newsgroup-agentized
5351                     gnus-downloaded-mark)
5352                    (t
5353                     gnus-no-mark))
5354              gnus-tmp-from (mail-header-from gnus-tmp-header)
5355              gnus-tmp-name
5356              (cond
5357               ((string-match "<[^>]+> *$" gnus-tmp-from)
5358                (setq beg-match (match-beginning 0))
5359                (or (and (string-match "^\".+\"" gnus-tmp-from)
5360                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5361                    (substring gnus-tmp-from 0 beg-match)))
5362               ((string-match "(.+)" gnus-tmp-from)
5363                (substring gnus-tmp-from
5364                           (1+ (match-beginning 0)) (1- (match-end 0))))
5365               (t gnus-tmp-from))
5366
5367              ;; Do the %B string
5368              gnus-tmp-thread-tree-header-string
5369              (cond
5370               ((not gnus-show-threads) "")
5371               ((zerop gnus-tmp-level)
5372                (cond ((cdar thread)
5373                       (or gnus-sum-thread-tree-root subject))
5374                      (gnus-tmp-new-adopts
5375                       (or gnus-sum-thread-tree-false-root subject))
5376                      (t
5377                       (or gnus-sum-thread-tree-single-indent subject))))
5378               (t
5379                (concat (apply 'concat
5380                               (mapcar (lambda (item)
5381                                         (if (= item 1)
5382                                             gnus-sum-thread-tree-vertical
5383                                           gnus-sum-thread-tree-indent))
5384                                       (cdr (reverse tree-stack))))
5385                        (if (nth 1 thread)
5386                            gnus-sum-thread-tree-leaf-with-other
5387                          gnus-sum-thread-tree-single-leaf)))))
5388             (when (string= gnus-tmp-name "")
5389               (setq gnus-tmp-name gnus-tmp-from))
5390             (unless (numberp gnus-tmp-lines)
5391               (setq gnus-tmp-lines -1))
5392             (if (= gnus-tmp-lines -1)
5393                 (setq gnus-tmp-lines "?")
5394               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5395             (gnus-put-text-property
5396              (point)
5397              (progn (eval gnus-summary-line-format-spec) (point))
5398              'gnus-number number)
5399             (when gnus-visual-p
5400               (forward-line -1)
5401               (gnus-summary-highlight-line)
5402               (when gnus-summary-update-hook
5403                 (gnus-run-hooks 'gnus-summary-update-hook))
5404               (forward-line 1))
5405
5406             (setq gnus-tmp-prev-subject simp-subject)))
5407
5408         (when (nth 1 thread)
5409           (push (list (max 0 gnus-tmp-level)
5410                       (copy-sequence tree-stack)
5411                       (nthcdr 1 thread))
5412                 stack))
5413         (push (if (nth 1 thread) 1 0) tree-stack)
5414         (incf gnus-tmp-level)
5415         (setq threads (if thread-end nil (cdar thread)))
5416         (if gnus-summary-display-while-building
5417             (if building-count
5418                 (progn
5419                   ;; use a set frequency
5420                   (setq building-line-count (1- building-line-count))
5421                   (when (= building-line-count 0)
5422                     (sit-for 0)
5423                     (setq building-line-count
5424                           gnus-summary-display-while-building)))
5425               ;; always
5426               (sit-for 0)))
5427         (unless threads
5428           (setq gnus-tmp-level 0)))))
5429   (gnus-message 7 "Generating summary...done"))
5430
5431 (defun gnus-summary-prepare-unthreaded (headers)
5432   "Generate an unthreaded summary buffer based on HEADERS."
5433   (let (header number mark)
5434
5435     (beginning-of-line)
5436
5437     (while headers
5438       ;; We may have to root out some bad articles...
5439       (when (memq (setq number (mail-header-number
5440                                 (setq header (pop headers))))
5441                   gnus-newsgroup-limit)
5442         ;; Mark article as read when it has a low score.
5443         (when (and gnus-summary-mark-below
5444                    (< (or (cdr (assq number gnus-newsgroup-scored))
5445                           gnus-summary-default-score 0)
5446                       gnus-summary-mark-below)
5447                    (not (gnus-summary-article-ancient-p number)))
5448           (setq gnus-newsgroup-unreads
5449                 (delq number gnus-newsgroup-unreads))
5450           (if gnus-newsgroup-auto-expire
5451               (push number gnus-newsgroup-expirable)
5452             (push (cons number gnus-low-score-mark)
5453                   gnus-newsgroup-reads)))
5454
5455         (setq mark (gnus-article-mark number))
5456         (push (gnus-data-make number mark (1+ (point)) header 0)
5457               gnus-newsgroup-data)
5458         (gnus-summary-insert-line
5459          header 0 number
5460          (memq number gnus-newsgroup-undownloaded)
5461          mark (memq number gnus-newsgroup-replied)
5462          (memq number gnus-newsgroup-expirable)
5463          (mail-header-subject header) nil
5464          (cdr (assq number gnus-newsgroup-scored))
5465          (memq number gnus-newsgroup-processable))))))
5466
5467 (defun gnus-summary-remove-list-identifiers ()
5468   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5469   (let ((regexp (if (consp gnus-list-identifiers)
5470                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5471                   gnus-list-identifiers))
5472         changed subject)
5473     (when regexp
5474       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5475       (dolist (header gnus-newsgroup-headers)
5476         (setq subject (mail-header-subject header)
5477               changed nil)
5478         (while (string-match regexp subject)
5479           (setq subject
5480                 (concat (substring subject 0 (match-beginning 1))
5481                         (substring subject (match-end 0)))
5482                 changed t))
5483         (when changed
5484           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5485             (setq subject
5486                   (concat (substring subject 0 (match-beginning 1))
5487                           (substring subject (match-end 1)))))
5488           (mail-header-set-subject header subject))))))
5489
5490 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5491   "Fetch headers of ARTICLES."
5492   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5493     (gnus-message 5 "Fetching headers for %s..." name)
5494     (prog1
5495         (if (eq 'nov
5496                 (setq gnus-headers-retrieved-by
5497                       (gnus-retrieve-headers
5498                        articles gnus-newsgroup-name
5499                        (or limit
5500                            ;; We might want to fetch old headers, but
5501                            ;; not if there is only 1 article.
5502                            (and (or (and
5503                                      (not (eq gnus-fetch-old-headers 'some))
5504                                      (not (numberp gnus-fetch-old-headers)))
5505                                     (> (length articles) 1))
5506                                 gnus-fetch-old-headers)))))
5507             (gnus-get-newsgroup-headers-xover
5508              articles force-new dependencies gnus-newsgroup-name t)
5509           (gnus-get-newsgroup-headers dependencies force-new))
5510       (gnus-message 5 "Fetching headers for %s...done" name))))
5511
5512 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5513   "Select newsgroup GROUP.
5514 If READ-ALL is non-nil, all articles in the group are selected.
5515 If SELECT-ARTICLES, only select those articles from GROUP."
5516   (let* ((entry (gnus-group-entry group))
5517          ;;!!! Dirty hack; should be removed.
5518          (gnus-summary-ignore-duplicates
5519           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5520               t
5521             gnus-summary-ignore-duplicates))
5522          (info (nth 2 entry))
5523          charset articles fetched-articles cached)
5524
5525     (unless (gnus-check-server
5526              (set (make-local-variable 'gnus-current-select-method)
5527                   (gnus-find-method-for-group group)))
5528       (error "Couldn't open server"))
5529     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5530
5531     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5532         (gnus-activate-group group)     ; Or we can activate it...
5533         (progn                          ; Or we bug out.
5534           (when (equal major-mode 'gnus-summary-mode)
5535             (gnus-kill-buffer (current-buffer)))
5536           (error
5537            "Couldn't activate group %s: %s"
5538            (mm-decode-coding-string group charset)
5539            (mm-decode-coding-string (gnus-status-message group) charset))))
5540
5541     (unless (gnus-request-group group t)
5542       (when (equal major-mode 'gnus-summary-mode)
5543         (gnus-kill-buffer (current-buffer)))
5544       (error "Couldn't request group %s: %s"
5545              (mm-decode-coding-string group charset)
5546              (mm-decode-coding-string (gnus-status-message group) charset)))
5547
5548     (when gnus-agent
5549       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5550
5551       (setq gnus-summary-use-undownloaded-faces
5552             (gnus-agent-find-parameter
5553              group
5554              'agent-enable-undownloaded-faces)))
5555
5556     (setq gnus-newsgroup-name group
5557           gnus-newsgroup-unselected nil
5558           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5559
5560     (let ((display (gnus-group-find-parameter group 'display)))
5561       (setq gnus-newsgroup-display
5562             (cond
5563              ((not (zerop (or (car-safe read-all) 0)))
5564               ;; The user entered the group with C-u SPC/RET, let's show
5565               ;; all articles.
5566               'gnus-not-ignore)
5567              ((eq display 'all)
5568               'gnus-not-ignore)
5569              ((arrayp display)
5570               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5571              ((numberp display)
5572               ;; The following is probably the "correct" solution, but
5573               ;; it makes Gnus fetch all headers and then limit the
5574               ;; articles (which is slow), so instead we hack the
5575               ;; select-articles parameter instead. -- Simon Josefsson
5576               ;; <jas@kth.se>
5577               ;;
5578               ;; (gnus-byte-compile
5579               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5580               ;;                         display)))))
5581               (setq select-articles
5582                     (gnus-uncompress-range
5583                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5584                              (if (> tmp 0)
5585                                  tmp
5586                                1))
5587                            (cdr (gnus-active group)))))
5588               nil)
5589              (t
5590               nil))))
5591
5592     (gnus-summary-setup-default-charset)
5593
5594     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5595     (when (gnus-virtual-group-p group)
5596       (setq cached gnus-newsgroup-cached))
5597
5598     (setq gnus-newsgroup-unreads
5599           (gnus-sorted-ndifference
5600            (gnus-sorted-ndifference gnus-newsgroup-unreads
5601                                     gnus-newsgroup-marked)
5602            gnus-newsgroup-dormant))
5603
5604     (setq gnus-newsgroup-processable nil)
5605
5606     (gnus-update-read-articles group gnus-newsgroup-unreads)
5607
5608     ;; Adjust and set lists of article marks.
5609     (when info
5610       (gnus-adjust-marked-articles info))
5611     (if (setq articles select-articles)
5612         (setq gnus-newsgroup-unselected
5613               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5614       (setq articles (gnus-articles-to-read group read-all)))
5615
5616     (cond
5617      ((null articles)
5618       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5619       'quit)
5620      ((eq articles 0) nil)
5621      (t
5622       ;; Init the dependencies hash table.
5623       (setq gnus-newsgroup-dependencies
5624             (gnus-make-hashtable (length articles)))
5625       (gnus-set-global-variables)
5626       ;; Retrieve the headers and read them in.
5627
5628       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5629
5630       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5631       (when cached
5632         (setq gnus-newsgroup-cached cached))
5633
5634       ;; Suppress duplicates?
5635       (when gnus-suppress-duplicates
5636         (gnus-dup-suppress-articles))
5637
5638       ;; Set the initial limit.
5639       (setq gnus-newsgroup-limit (copy-sequence articles))
5640       ;; Remove canceled articles from the list of unread articles.
5641       (setq fetched-articles
5642             (mapcar (lambda (headers) (mail-header-number headers))
5643                     gnus-newsgroup-headers))
5644       (setq gnus-newsgroup-articles fetched-articles)
5645       (setq gnus-newsgroup-unreads
5646             (gnus-sorted-nintersection
5647              gnus-newsgroup-unreads fetched-articles))
5648       (gnus-compute-unseen-list)
5649
5650       ;; Removed marked articles that do not exist.
5651       (gnus-update-missing-marks
5652        (gnus-sorted-difference articles fetched-articles))
5653       ;; We might want to build some more threads first.
5654       (when (and gnus-fetch-old-headers
5655                  (eq gnus-headers-retrieved-by 'nov))
5656         (if (eq gnus-fetch-old-headers 'invisible)
5657             (gnus-build-all-threads)
5658           (gnus-build-old-threads)))
5659       ;; Let the Gnus agent mark articles as read.
5660       (when gnus-agent
5661         (gnus-agent-get-undownloaded-list))
5662       ;; Remove list identifiers from subject
5663       (when gnus-list-identifiers
5664         (gnus-summary-remove-list-identifiers))
5665       ;; Check whether auto-expire is to be done in this group.
5666       (setq gnus-newsgroup-auto-expire
5667             (gnus-group-auto-expirable-p group))
5668       ;; Set up the article buffer now, if necessary.
5669       (unless (and gnus-single-article-buffer
5670                    (equal gnus-article-buffer "*Article*"))
5671         (gnus-article-setup-buffer))
5672       ;; First and last article in this newsgroup.
5673       (when gnus-newsgroup-headers
5674         (setq gnus-newsgroup-begin
5675               (mail-header-number (car gnus-newsgroup-headers))
5676               gnus-newsgroup-end
5677               (mail-header-number
5678                (gnus-last-element gnus-newsgroup-headers))))
5679       ;; GROUP is successfully selected.
5680       (or gnus-newsgroup-headers t)))))
5681
5682 (defun gnus-compute-unseen-list ()
5683   ;; The `seen' marks are treated specially.
5684   (if (not gnus-newsgroup-seen)
5685       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5686     (setq gnus-newsgroup-unseen
5687           (gnus-inverse-list-range-intersection
5688            gnus-newsgroup-articles gnus-newsgroup-seen))))
5689
5690 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5691
5692 (defun gnus-summary-display-make-predicate (display)
5693   (require 'gnus-agent)
5694   (when (= (length display) 1)
5695     (setq display (car display)))
5696   (unless gnus-summary-display-cache
5697     (dolist (elem (append '((unread . unread)
5698                             (read . read)
5699                             (unseen . unseen))
5700                           gnus-article-mark-lists))
5701       (push (cons (cdr elem)
5702                   (gnus-byte-compile    ;Why bother?
5703                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5704             gnus-summary-display-cache)))
5705   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5706         (gnus-category-predicate-cache gnus-summary-display-cache))
5707     (gnus-get-predicate display)))
5708
5709 ;; Uses the dynamically bound `gnus-number' variable.
5710 (defvar gnus-number)
5711 (defun gnus-article-marked-p (type &optional article)
5712   (let ((article (or article gnus-number)))
5713     (cond
5714      ((eq type 'tick)
5715       (memq article gnus-newsgroup-marked))
5716      ((eq type 'spam)
5717       (memq article gnus-newsgroup-spam-marked))
5718      ((eq type 'unsend)
5719       (memq article gnus-newsgroup-unsendable))
5720      ((eq type 'undownload)
5721       (memq article gnus-newsgroup-undownloaded))
5722      ((eq type 'download)
5723       (memq article gnus-newsgroup-downloadable))
5724      ((eq type 'unread)
5725       (memq article gnus-newsgroup-unreads))
5726      ((eq type 'read)
5727       (memq article gnus-newsgroup-reads))
5728      ((eq type 'dormant)
5729       (memq article gnus-newsgroup-dormant) )
5730      ((eq type 'expire)
5731       (memq article gnus-newsgroup-expirable))
5732      ((eq type 'reply)
5733       (memq article gnus-newsgroup-replied))
5734      ((eq type 'killed)
5735       (memq article gnus-newsgroup-killed))
5736      ((eq type 'bookmark)
5737       (assq article gnus-newsgroup-bookmarks))
5738      ((eq type 'score)
5739       (assq article gnus-newsgroup-scored))
5740      ((eq type 'save)
5741       (memq article gnus-newsgroup-saved))
5742      ((eq type 'cache)
5743       (memq article gnus-newsgroup-cached))
5744      ((eq type 'forward)
5745       (memq article gnus-newsgroup-forwarded))
5746      ((eq type 'seen)
5747       (not (memq article gnus-newsgroup-unseen)))
5748      ((eq type 'recent)
5749       (memq article gnus-newsgroup-recent))
5750      (t t))))
5751
5752 (defun gnus-articles-to-read (group &optional read-all)
5753   "Find out what articles the user wants to read."
5754   (let* ((articles
5755           ;; Select all articles if `read-all' is non-nil, or if there
5756           ;; are no unread articles.
5757           (if (or read-all
5758                   (and (zerop (length gnus-newsgroup-marked))
5759                        (zerop (length gnus-newsgroup-unreads)))
5760                   ;; Fetch all if the predicate is non-nil.
5761                   gnus-newsgroup-display)
5762               ;; We want to select the headers for all the articles in
5763               ;; the group, so we select either all the active
5764               ;; articles in the group, or (if that's nil), the
5765               ;; articles in the cache.
5766               (or
5767                (if gnus-newsgroup-maximum-articles
5768                    (let ((active (gnus-active group)))
5769                      (gnus-uncompress-range
5770                       (cons (max (car active)
5771                                  (- (cdr active)
5772                                     gnus-newsgroup-maximum-articles
5773                                     -1))
5774                             (cdr active))))
5775                  (gnus-uncompress-range (gnus-active group)))
5776                (gnus-cache-articles-in-group group))
5777             ;; Select only the "normal" subset of articles.
5778             (gnus-sorted-nunion
5779              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5780              gnus-newsgroup-unreads)))
5781          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5782          (scored (length scored-list))
5783          (number (length articles))
5784          (marked (+ (length gnus-newsgroup-marked)
5785                     (length gnus-newsgroup-dormant)))
5786          (select
5787           (cond
5788            ((numberp read-all)
5789             read-all)
5790            ((numberp gnus-newsgroup-display)
5791             gnus-newsgroup-display)
5792            (t
5793             (condition-case ()
5794                 (cond
5795                  ((and (or (<= scored marked) (= scored number))
5796                        (numberp gnus-large-newsgroup)
5797                        (> number gnus-large-newsgroup))
5798                   (let* ((cursor-in-echo-area nil)
5799                          (initial (gnus-parameter-large-newsgroup-initial
5800                                    gnus-newsgroup-name))
5801                          (input
5802                           (read-string
5803                            (format
5804                             "How many articles from %s (%s %d): "
5805                             (gnus-group-decoded-name gnus-newsgroup-name)
5806                             (if initial "max" "default")
5807                             number)
5808                            (if initial
5809                                (cons (number-to-string initial)
5810                                      0)))))
5811                     (if (string-match "^[ \t]*$" input) number input)))
5812                  ((and (> scored marked) (< scored number)
5813                        (> (- scored number) 20))
5814                   (let ((input
5815                          (read-string
5816                           (format "%s %s (%d scored, %d total): "
5817                                   "How many articles from"
5818                                   (gnus-group-decoded-name group)
5819                                   scored number))))
5820                     (if (string-match "^[ \t]*$" input)
5821                         number input)))
5822                  (t number))
5823               (quit
5824                (message "Quit getting the articles to read")
5825                nil))))))
5826     (setq select (if (stringp select) (string-to-number select) select))
5827     (if (or (null select) (zerop select))
5828         select
5829       (if (and (not (zerop scored)) (<= (abs select) scored))
5830           (progn
5831             (setq articles (sort scored-list '<))
5832             (setq number (length articles)))
5833         (setq articles (copy-sequence articles)))
5834
5835       (when (< (abs select) number)
5836         (if (< select 0)
5837             ;; Select the N oldest articles.
5838             (setcdr (nthcdr (1- (abs select)) articles) nil)
5839           ;; Select the N most recent articles.
5840           (setq articles (nthcdr (- number select) articles))))
5841       (setq gnus-newsgroup-unselected
5842             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5843       (when gnus-alter-articles-to-read-function
5844         (setq articles
5845               (sort
5846                (funcall gnus-alter-articles-to-read-function
5847                         gnus-newsgroup-name articles)
5848                '<)))
5849       articles)))
5850
5851 (defun gnus-killed-articles (killed articles)
5852   (let (out)
5853     (while articles
5854       (when (inline (gnus-member-of-range (car articles) killed))
5855         (push (car articles) out))
5856       (setq articles (cdr articles)))
5857     out))
5858
5859 (defun gnus-uncompress-marks (marks)
5860   "Uncompress the mark ranges in MARKS."
5861   (let ((uncompressed '(score bookmark))
5862         out)
5863     (while marks
5864       (if (memq (caar marks) uncompressed)
5865           (push (car marks) out)
5866         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5867       (setq marks (cdr marks)))
5868     out))
5869
5870 (defun gnus-article-mark-to-type (mark)
5871   "Return the type of MARK."
5872   (or (cadr (assq mark gnus-article-special-mark-lists))
5873       'list))
5874
5875 (defun gnus-article-unpropagatable-p (mark)
5876   "Return whether MARK should be propagated to back end."
5877   (memq mark gnus-article-unpropagated-mark-lists))
5878
5879 (defun gnus-adjust-marked-articles (info)
5880   "Set all article lists and remove all marks that are no longer valid."
5881   (let* ((marked-lists (gnus-info-marks info))
5882          (active (gnus-active (gnus-info-group info)))
5883          (min (car active))
5884          (max (cdr active))
5885          (types gnus-article-mark-lists)
5886          marks var articles article mark mark-type
5887          bgn end)
5888     ;; Hack to avoid adjusting marks for imap.
5889     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5890               'nnimap)
5891       (setq min 1))
5892
5893     (dolist (marks marked-lists)
5894       (setq mark (car marks)
5895             mark-type (gnus-article-mark-to-type mark)
5896             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5897
5898       ;; We set the variable according to the type of the marks list,
5899       ;; and then adjust the marks to a subset of the active articles.
5900       (cond
5901        ;; Adjust "simple" lists - compressed yet unsorted
5902        ((eq mark-type 'list)
5903         ;; Simultaneously uncompress and clip to active range
5904         ;; See gnus-uncompress-range for a description of possible marks
5905         (let (l lh)
5906           (if (not (cadr marks))
5907               (set var nil)
5908             (setq articles (if (numberp (cddr marks))
5909                                (list (cdr marks))
5910                              (cdr marks))
5911                   lh (cons nil nil)
5912                   l lh)
5913
5914             (while (setq article (pop articles))
5915               (cond ((consp article)
5916                      (setq bgn (max (car article) min)
5917                            end (min (cdr article) max))
5918                      (while (<= bgn end)
5919                        (setq l (setcdr l (cons bgn nil))
5920                              bgn (1+ bgn))))
5921                     ((and (<= min article)
5922                           (>= max article))
5923                      (setq l (setcdr l (cons article nil))))))
5924             (set var (cdr lh)))))
5925        ;; Adjust assocs.
5926        ((eq mark-type 'tuple)
5927         (set var (setq articles (cdr marks)))
5928         (when (not (listp (cdr (symbol-value var))))
5929           (set var (list (symbol-value var))))
5930         (when (not (listp (cdr articles)))
5931           (setq articles (list articles)))
5932         (while articles
5933           (when (or (not (consp (setq article (pop articles))))
5934                     (< (car article) min)
5935                     (> (car article) max))
5936             (set var (delq article (symbol-value var))))))
5937        ;; Adjust ranges (sloppily).
5938        ((eq mark-type 'range)
5939         (cond
5940          ((eq mark 'seen)
5941           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5942           ;; It should be (seen (NUM1 . NUM2)).
5943           (when (numberp (cddr marks))
5944             (setcdr marks (list (cdr marks))))
5945           (setq articles (cdr marks))
5946           (while (and articles
5947                       (or (and (consp (car articles))
5948                                (> min (cdar articles)))
5949                           (and (numberp (car articles))
5950                                (> min (car articles)))))
5951             (pop articles))
5952           (set var articles))))))))
5953
5954 (defun gnus-update-missing-marks (missing)
5955   "Go through the list of MISSING articles and remove them from the mark lists."
5956   (when missing
5957     (let (var m)
5958       ;; Go through all types.
5959       (dolist (elem gnus-article-mark-lists)
5960         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5961           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5962           (when (symbol-value var)
5963             ;; This list has articles.  So we delete all missing
5964             ;; articles from it.
5965             (setq m missing)
5966             (while m
5967               (set var (delq (pop m) (symbol-value var))))))))))
5968
5969 (defun gnus-update-marks ()
5970   "Enter the various lists of marked articles into the newsgroup info list."
5971   (let ((types gnus-article-mark-lists)
5972         (info (gnus-get-info gnus-newsgroup-name))
5973         type list newmarked symbol delta-marks)
5974     (when info
5975       ;; Add all marks lists to the list of marks lists.
5976       (while (setq type (pop types))
5977         (setq list (symbol-value
5978                     (setq symbol
5979                           (intern (format "gnus-newsgroup-%s" (car type))))))
5980
5981         (when list
5982           ;; Get rid of the entries of the articles that have the
5983           ;; default score.
5984           (when (and (eq (cdr type) 'score)
5985                      gnus-save-score
5986                      list)
5987             (let* ((arts list)
5988                    (prev (cons nil list))
5989                    (all prev))
5990               (while arts
5991                 (if (or (not (consp (car arts)))
5992                         (= (cdar arts) gnus-summary-default-score))
5993                     (setcdr prev (cdr arts))
5994                   (setq prev arts))
5995                 (setq arts (cdr arts)))
5996               (setq list (cdr all)))))
5997
5998         (when (eq (cdr type) 'seen)
5999           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6000
6001         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6002           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6003
6004         (when (and (gnus-check-backend-function
6005                     'request-set-mark gnus-newsgroup-name)
6006                    (not (gnus-article-unpropagatable-p (cdr type))))
6007           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6008                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
6009                  (add (gnus-remove-from-range
6010                        (gnus-copy-sequence list) old)))
6011             (when add
6012               (push (list add 'add (list (cdr type))) delta-marks))
6013             (when del
6014               ;; Don't delete marks from outside the active range.  This
6015               ;; shouldn't happen, but is a sanity check.
6016               (setq del (gnus-sorted-range-intersection
6017                          (gnus-active gnus-newsgroup-name) del))
6018               (push (list del 'del (list (cdr type))) delta-marks))))
6019
6020         (when list
6021           (push (cons (cdr type) list) newmarked)))
6022
6023       (when delta-marks
6024         (unless (gnus-check-group gnus-newsgroup-name)
6025           (error "Can't open server for %s" gnus-newsgroup-name))
6026         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6027
6028       ;; Enter these new marks into the info of the group.
6029       (if (nthcdr 3 info)
6030           (setcar (nthcdr 3 info) newmarked)
6031         ;; Add the marks lists to the end of the info.
6032         (when newmarked
6033           (setcdr (nthcdr 2 info) (list newmarked))))
6034
6035       ;; Cut off the end of the info if there's nothing else there.
6036       (let ((i 5))
6037         (while (and (> i 2)
6038                     (not (nth i info)))
6039           (when (nthcdr (decf i) info)
6040             (setcdr (nthcdr i info) nil)))))))
6041
6042 (defun gnus-set-mode-line (where)
6043   "Set the mode line of the article or summary buffers.
6044 If WHERE is `summary', the summary mode line format will be used."
6045   ;; Is this mode line one we keep updated?
6046   (when (and (memq where gnus-updated-mode-lines)
6047              (symbol-value
6048               (intern (format "gnus-%s-mode-line-format-spec" where))))
6049     (let (mode-string)
6050       ;; We evaluate this in the summary buffer since these
6051       ;; variables are buffer-local to that buffer.
6052       (with-current-buffer gnus-summary-buffer
6053         ;; We bind all these variables that are used in the `eval' form
6054         ;; below.
6055         (let* ((mformat (symbol-value
6056                          (intern
6057                           (format "gnus-%s-mode-line-format-spec" where))))
6058                (gnus-tmp-group-name (gnus-mode-string-quote
6059                                      (gnus-group-decoded-name
6060                                       gnus-newsgroup-name)))
6061                (gnus-tmp-article-number (or gnus-current-article 0))
6062                (gnus-tmp-unread gnus-newsgroup-unreads)
6063                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6064                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6065                (gnus-tmp-unread-and-unselected
6066                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6067                             (zerop gnus-tmp-unselected))
6068                        "")
6069                       ((zerop gnus-tmp-unselected)
6070                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6071                       (t (format "{%d(+%d) more}"
6072                                  gnus-tmp-unread-and-unticked
6073                                  gnus-tmp-unselected))))
6074                (gnus-tmp-subject
6075                 (if (and gnus-current-headers
6076                          (vectorp gnus-current-headers))
6077                     (gnus-mode-string-quote
6078                      (mail-header-subject gnus-current-headers))
6079                   ""))
6080                bufname-length max-len
6081                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6082           (setq mode-string (eval mformat))
6083           (setq bufname-length (if (string-match "%b" mode-string)
6084                                    (- (length
6085                                        (buffer-name
6086                                         (if (eq where 'summary)
6087                                             nil
6088                                           (get-buffer gnus-article-buffer))))
6089                                       2)
6090                                  0))
6091           (setq max-len (max 4 (if gnus-mode-non-string-length
6092                                    (- (window-width)
6093                                       gnus-mode-non-string-length
6094                                       bufname-length)
6095                                  (length mode-string))))
6096           ;; We might have to chop a bit of the string off...
6097           (when (> (length mode-string) max-len)
6098             (setq mode-string
6099                   (concat (truncate-string-to-width mode-string (- max-len 3))
6100                           "...")))))
6101       ;; Update the mode line.
6102       (setq mode-line-buffer-identification
6103             (gnus-mode-line-buffer-identification (list mode-string)))
6104       (set-buffer-modified-p t))))
6105
6106 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6107   "Go through the HEADERS list and add all Xrefs to a hash table.
6108 The resulting hash table is returned, or nil if no Xrefs were found."
6109   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6110          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6111          (xref-hashtb (gnus-make-hashtable))
6112          start group entry number xrefs header)
6113     (while headers
6114       (setq header (pop headers))
6115       (when (and (setq xrefs (mail-header-xref header))
6116                  (not (memq (setq number (mail-header-number header))
6117                             unreads)))
6118         (setq start 0)
6119         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6120           (setq start (match-end 0))
6121           (setq group (if prefix
6122                           (concat prefix (substring xrefs (match-beginning 1)
6123                                                     (match-end 1)))
6124                         (substring xrefs (match-beginning 1) (match-end 1))))
6125           (setq number
6126                 (string-to-number (substring xrefs (match-beginning 2)
6127                                           (match-end 2))))
6128           (if (setq entry (gnus-gethash group xref-hashtb))
6129               (setcdr entry (cons number (cdr entry)))
6130             (gnus-sethash group (cons number nil) xref-hashtb)))))
6131     (and start xref-hashtb)))
6132
6133 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6134   "Look through all the headers and mark the Xrefs as read."
6135   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6136         name info xref-hashtb idlist method nth4)
6137     (with-current-buffer gnus-group-buffer
6138       (when (setq xref-hashtb
6139                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6140         (mapatoms
6141          (lambda (group)
6142            (unless (string= from-newsgroup (setq name (symbol-name group)))
6143              (setq idlist (symbol-value group))
6144              ;; Dead groups are not updated.
6145              (and (prog1
6146                       (setq info (gnus-get-info name))
6147                     (when (stringp (setq nth4 (gnus-info-method info)))
6148                       (setq nth4 (gnus-server-to-method nth4))))
6149                   ;; Only do the xrefs if the group has the same
6150                   ;; select method as the group we have just read.
6151                   (or (gnus-methods-equal-p
6152                        nth4 (gnus-find-method-for-group from-newsgroup))
6153                       virtual
6154                       (equal nth4 (setq method (gnus-find-method-for-group
6155                                                 from-newsgroup)))
6156                       (and (equal (car nth4) (car method))
6157                            (equal (nth 1 nth4) (nth 1 method))))
6158                   gnus-use-cross-reference
6159                   (or (not (eq gnus-use-cross-reference t))
6160                       virtual
6161                       ;; Only do cross-references on subscribed
6162                       ;; groups, if that is what is wanted.
6163                       (<= (gnus-info-level info) gnus-level-subscribed))
6164                   (gnus-group-make-articles-read name idlist))))
6165          xref-hashtb)))))
6166
6167 (defun gnus-compute-read-articles (group articles)
6168   (let* ((entry (gnus-group-entry group))
6169          (info (nth 2 entry))
6170          (active (gnus-active group))
6171          ninfo)
6172     (when entry
6173       ;; First peel off all invalid article numbers.
6174       (when active
6175         (let ((ids articles)
6176               id first)
6177           (while (setq id (pop ids))
6178             (when (and first (> id (cdr active)))
6179               ;; We'll end up in this situation in one particular
6180               ;; obscure situation.  If you re-scan a group and get
6181               ;; a new article that is cross-posted to a different
6182               ;; group that has not been re-scanned, you might get
6183               ;; crossposted article that has a higher number than
6184               ;; Gnus believes possible.  So we re-activate this
6185               ;; group as well.  This might mean doing the
6186               ;; crossposting thingy will *increase* the number
6187               ;; of articles in some groups.  Tsk, tsk.
6188               (setq active (or (gnus-activate-group group) active)))
6189             (when (or (> id (cdr active))
6190                       (< id (car active)))
6191               (setq articles (delq id articles))))))
6192       ;; If the read list is nil, we init it.
6193       (if (and active
6194                (null (gnus-info-read info))
6195                (> (car active) 1))
6196           (setq ninfo (cons 1 (1- (car active))))
6197         (setq ninfo (gnus-info-read info)))
6198       ;; Then we add the read articles to the range.
6199       (gnus-add-to-range
6200        ninfo (setq articles (sort articles '<))))))
6201
6202 (defun gnus-group-make-articles-read (group articles)
6203   "Update the info of GROUP to say that ARTICLES are read."
6204   (let* ((num 0)
6205          (entry (gnus-group-entry group))
6206          (info (nth 2 entry))
6207          (active (gnus-active group))
6208          range)
6209     (if (not entry)
6210         ;; Group that Gnus doesn't know exists, but still allow the
6211         ;; backend to set marks.
6212         (gnus-request-set-mark
6213          group (list (list (gnus-compress-sequence (sort articles #'<))
6214                            'add '(read))))
6215       ;; Normal, subscribed groups.
6216       (setq range (gnus-compute-read-articles group articles))
6217       (with-current-buffer gnus-group-buffer
6218         (gnus-undo-register
6219           `(progn
6220              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6221              (gnus-info-set-read ',info ',(gnus-info-read info))
6222              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6223              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6224              (gnus-group-update-group ,group t))))
6225       ;; Add the read articles to the range.
6226       (gnus-info-set-read info range)
6227       (gnus-request-set-mark group (list (list range 'add '(read))))
6228       ;; Then we have to re-compute how many unread
6229       ;; articles there are in this group.
6230       (when active
6231         (cond
6232          ((not range)
6233           (setq num (- (1+ (cdr active)) (car active))))
6234          ((not (listp (cdr range)))
6235           (setq num (- (cdr active) (- (1+ (cdr range))
6236                                        (car range)))))
6237          (t
6238           (while range
6239             (if (numberp (car range))
6240                 (setq num (1+ num))
6241               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6242             (setq range (cdr range)))
6243           (setq num (- (cdr active) num))))
6244         ;; Update the number of unread articles.
6245         (setcar entry num)
6246         ;; Update the group buffer.
6247         (unless (gnus-ephemeral-group-p group)
6248           (gnus-group-update-group group t))))))
6249
6250 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6251   (let ((cur nntp-server-buffer)
6252         (dependencies
6253          (or dependencies
6254              (with-current-buffer gnus-summary-buffer
6255                gnus-newsgroup-dependencies)))
6256         headers id end ref number
6257         (mail-parse-charset gnus-newsgroup-charset)
6258         (mail-parse-ignored-charsets
6259          (save-current-buffer (condition-case nil
6260                                   (set-buffer gnus-summary-buffer)
6261                                 (error))
6262                               gnus-newsgroup-ignored-charsets)))
6263     (with-current-buffer nntp-server-buffer
6264       ;; Translate all TAB characters into SPACE characters.
6265       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6266       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6267       (ietf-drums-unfold-fws)
6268       (gnus-run-hooks 'gnus-parse-headers-hook)
6269       (let ((case-fold-search t)
6270             in-reply-to header p lines chars)
6271         (goto-char (point-min))
6272         ;; Search to the beginning of the next header.  Error messages
6273         ;; do not begin with 2 or 3.
6274         (while (re-search-forward "^[23][0-9]+ " nil t)
6275           (setq id nil
6276                 ref nil)
6277           ;; This implementation of this function, with nine
6278           ;; search-forwards instead of the one re-search-forward and
6279           ;; a case (which basically was the old function) is actually
6280           ;; about twice as fast, even though it looks messier.  You
6281           ;; can't have everything, I guess.  Speed and elegance
6282           ;; doesn't always go hand in hand.
6283           (setq
6284            header
6285            (vector
6286             ;; Number.
6287             (prog1
6288                 (setq number (read cur))
6289               (end-of-line)
6290               (setq p (point))
6291               (narrow-to-region (point)
6292                                 (or (and (search-forward "\n.\n" nil t)
6293                                          (- (point) 2))
6294                                     (point))))
6295             ;; Subject.
6296             (progn
6297               (goto-char p)
6298               (if (search-forward "\nsubject:" nil t)
6299                   (funcall gnus-decode-encoded-word-function
6300                            (nnheader-header-value))
6301                 "(none)"))
6302             ;; From.
6303             (progn
6304               (goto-char p)
6305               (if (search-forward "\nfrom:" nil t)
6306                   (funcall gnus-decode-encoded-address-function
6307                            (nnheader-header-value))
6308                 "(nobody)"))
6309             ;; Date.
6310             (progn
6311               (goto-char p)
6312               (if (search-forward "\ndate:" nil t)
6313                   (nnheader-header-value) ""))
6314             ;; Message-ID.
6315             (progn
6316               (goto-char p)
6317               (setq id (if (re-search-forward
6318                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6319                            ;; We do it this way to make sure the Message-ID
6320                            ;; is (somewhat) syntactically valid.
6321                            (buffer-substring (match-beginning 1)
6322                                              (match-end 1))
6323                          ;; If there was no message-id, we just fake one
6324                          ;; to make subsequent routines simpler.
6325                          (nnheader-generate-fake-message-id number))))
6326             ;; References.
6327             (progn
6328               (goto-char p)
6329               (if (search-forward "\nreferences:" nil t)
6330                   (progn
6331                     (setq end (point))
6332                     (prog1
6333                         (nnheader-header-value)
6334                       (setq ref
6335                             (buffer-substring
6336                              (progn
6337                                (end-of-line)
6338                                (search-backward ">" end t)
6339                                (1+ (point)))
6340                              (progn
6341                                (search-backward "<" end t)
6342                                (point))))))
6343                 ;; Get the references from the in-reply-to header if there
6344                 ;; were no references and the in-reply-to header looks
6345                 ;; promising.
6346                 (if (and (search-forward "\nin-reply-to:" nil t)
6347                          (setq in-reply-to (nnheader-header-value))
6348                          (string-match "<[^>]+>" in-reply-to))
6349                     (let (ref2)
6350                       (setq ref (substring in-reply-to (match-beginning 0)
6351                                            (match-end 0)))
6352                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6353                         (setq ref2 (substring in-reply-to (match-beginning 0)
6354                                               (match-end 0)))
6355                         (when (> (length ref2) (length ref))
6356                           (setq ref ref2)))
6357                       ref)
6358                   (setq ref nil))))
6359             ;; Chars.
6360             (progn
6361               (goto-char p)
6362               (if (search-forward "\nchars: " nil t)
6363                   (if (numberp (setq chars (ignore-errors (read cur))))
6364                       chars -1)
6365                 -1))
6366             ;; Lines.
6367             (progn
6368               (goto-char p)
6369               (if (search-forward "\nlines: " nil t)
6370                   (if (numberp (setq lines (ignore-errors (read cur))))
6371                       lines -1)
6372                 -1))
6373             ;; Xref.
6374             (progn
6375               (goto-char p)
6376               (and (search-forward "\nxref:" nil t)
6377                    (nnheader-header-value)))
6378             ;; Extra.
6379             (when gnus-extra-headers
6380               (let ((extra gnus-extra-headers)
6381                     out)
6382                 (while extra
6383                   (goto-char p)
6384                   (when (search-forward
6385                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6386                     (push (cons (car extra) (nnheader-header-value))
6387                           out))
6388                   (pop extra))
6389                 out))))
6390           (when (equal id ref)
6391             (setq ref nil))
6392
6393           (when gnus-alter-header-function
6394             (funcall gnus-alter-header-function header)
6395             (setq id (mail-header-id header)
6396                   ref (gnus-parent-id (mail-header-references header))))
6397
6398           (when (setq header
6399                       (gnus-dependencies-add-header
6400                        header dependencies force-new))
6401             (push header headers))
6402           (goto-char (point-max))
6403           (widen))
6404         (nreverse headers)))))
6405
6406 ;; Goes through the xover lines and returns a list of vectors
6407 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6408                                                   force-new dependencies
6409                                                   group also-fetch-heads)
6410   "Parse the news overview data in the server buffer.
6411 Return a list of headers that match SEQUENCE (see
6412 `nntp-retrieve-headers')."
6413   ;; Get the Xref when the users reads the articles since most/some
6414   ;; NNTP servers do not include Xrefs when using XOVER.
6415   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6416   (let ((mail-parse-charset gnus-newsgroup-charset)
6417         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6418         (cur nntp-server-buffer)
6419         (dependencies (or dependencies gnus-newsgroup-dependencies))
6420         (allp (cond
6421                ((eq gnus-read-all-available-headers t)
6422                 t)
6423                ((and (stringp gnus-read-all-available-headers)
6424                      group)
6425                 (string-match gnus-read-all-available-headers group))
6426                (t
6427                 nil)))
6428         number headers header)
6429     (with-current-buffer nntp-server-buffer
6430       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6431       ;; Allow the user to mangle the headers before parsing them.
6432       (gnus-run-hooks 'gnus-parse-headers-hook)
6433       (goto-char (point-min))
6434       (gnus-parse-without-error
6435         (while (and (or sequence allp)
6436                     (not (eobp)))
6437           (setq number (read cur))
6438           (when (not allp)
6439             (while (and sequence
6440                         (< (car sequence) number))
6441               (setq sequence (cdr sequence))))
6442           (when (and (or allp
6443                          (and sequence
6444                               (eq number (car sequence))))
6445                      (progn
6446                        (setq sequence (cdr sequence))
6447                        (setq header (inline
6448                                       (gnus-nov-parse-line
6449                                        number dependencies force-new)))))
6450             (push header headers))
6451           (forward-line 1)))
6452       ;; A common bug in inn is that if you have posted an article and
6453       ;; then retrieves the active file, it will answer correctly --
6454       ;; the new article is included.  However, a NOV entry for the
6455       ;; article may not have been generated yet, so this may fail.
6456       ;; We work around this problem by retrieving the last few
6457       ;; headers using HEAD.
6458       (if (or (not also-fetch-heads)
6459               (not sequence))
6460           ;; We (probably) got all the headers.
6461           (nreverse headers)
6462         (let ((gnus-nov-is-evil t))
6463           (nconc
6464            (nreverse headers)
6465            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6466              (gnus-get-newsgroup-headers))))))))
6467
6468 (defun gnus-article-get-xrefs ()
6469   "Fill in the Xref value in `gnus-current-headers', if necessary.
6470 This is meant to be called in `gnus-article-internal-prepare-hook'."
6471   (let ((headers (with-current-buffer gnus-summary-buffer
6472                    gnus-current-headers)))
6473     (or (not gnus-use-cross-reference)
6474         (not headers)
6475         (and (mail-header-xref headers)
6476              (not (string= (mail-header-xref headers) "")))
6477         (let ((case-fold-search t)
6478               xref)
6479           (save-restriction
6480             (nnheader-narrow-to-headers)
6481             (goto-char (point-min))
6482             (when (or (and (not (eobp))
6483                            (eq (downcase (char-after)) ?x)
6484                            (looking-at "Xref:"))
6485                       (search-forward "\nXref:" nil t))
6486               (goto-char (1+ (match-end 0)))
6487               (setq xref (buffer-substring (point) (point-at-eol)))
6488               (mail-header-set-xref headers xref)))))))
6489
6490 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6491   "Find article ID and insert the summary line for that article.
6492 OLD-HEADER can either be a header or a line number to insert
6493 the subject line on."
6494   (let* ((line (and (numberp old-header) old-header))
6495          (old-header (and (vectorp old-header) old-header))
6496          (header (cond ((and old-header use-old-header)
6497                         old-header)
6498                        ((and (numberp id)
6499                              (gnus-number-to-header id))
6500                         (gnus-number-to-header id))
6501                        (t
6502                         (gnus-read-header id))))
6503          (number (and (numberp id) id))
6504          d)
6505     (when header
6506       ;; Rebuild the thread that this article is part of and go to the
6507       ;; article we have fetched.
6508       (when (and (not gnus-show-threads)
6509                  old-header)
6510         (when (and number
6511                    (setq d (gnus-data-find (mail-header-number old-header))))
6512           (goto-char (gnus-data-pos d))
6513           (gnus-data-remove
6514            number
6515            (- (point-at-bol)
6516               (prog1
6517                   (1+ (point-at-eol))
6518                 (gnus-delete-line))))))
6519       ;; Remove list identifiers from subject.
6520       (when gnus-list-identifiers
6521         (let ((gnus-newsgroup-headers (list header)))
6522           (gnus-summary-remove-list-identifiers)))
6523       (when old-header
6524         (mail-header-set-number header (mail-header-number old-header)))
6525       (setq gnus-newsgroup-sparse
6526             (delq (setq number (mail-header-number header))
6527                   gnus-newsgroup-sparse))
6528       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6529       (push number gnus-newsgroup-limit)
6530       (gnus-rebuild-thread (mail-header-id header) line)
6531       (gnus-summary-goto-subject number nil t))
6532     (when (and (numberp number)
6533                (> number 0))
6534       ;; We have to update the boundaries even if we can't fetch the
6535       ;; article if ID is a number -- so that the next `P' or `N'
6536       ;; command will fetch the previous (or next) article even
6537       ;; if the one we tried to fetch this time has been canceled.
6538       (when (> number gnus-newsgroup-end)
6539         (setq gnus-newsgroup-end number))
6540       (when (< number gnus-newsgroup-begin)
6541         (setq gnus-newsgroup-begin number))
6542       (setq gnus-newsgroup-unselected
6543             (delq number gnus-newsgroup-unselected)))
6544     ;; Report back a success?
6545     (and header (mail-header-number header))))
6546
6547 ;;; Process/prefix in the summary buffer
6548
6549 (defun gnus-summary-work-articles (n)
6550   "Return a list of articles to be worked upon.
6551 The prefix argument, the list of process marked articles, and the
6552 current article will be taken into consideration."
6553   (with-current-buffer gnus-summary-buffer
6554     (cond
6555      (n
6556       ;; A numerical prefix has been given.
6557       (setq n (prefix-numeric-value n))
6558       (let ((backward (< n 0))
6559             (n (abs (prefix-numeric-value n)))
6560             articles article)
6561         (save-excursion
6562           (while
6563               (and (> n 0)
6564                    (push (setq article (gnus-summary-article-number))
6565                          articles)
6566                    (if backward
6567                        (gnus-summary-find-prev nil article)
6568                      (gnus-summary-find-next nil article)))
6569             (decf n)))
6570         (nreverse articles)))
6571      ((and (gnus-region-active-p) (mark))
6572       (message "region active")
6573       ;; Work on the region between point and mark.
6574       (let ((max (max (point) (mark)))
6575             articles article)
6576         (save-excursion
6577           (goto-char (min (point) (mark)))
6578           (while
6579               (and
6580                (push (setq article (gnus-summary-article-number)) articles)
6581                (gnus-summary-find-next nil article)
6582                (< (point) max)))
6583           (nreverse articles))))
6584      (gnus-newsgroup-processable
6585       ;; There are process-marked articles present.
6586       ;; Save current state.
6587       (gnus-summary-save-process-mark)
6588       ;; Return the list.
6589       (reverse gnus-newsgroup-processable))
6590      (t
6591       ;; Just return the current article.
6592       (list (gnus-summary-article-number))))))
6593
6594 (defmacro gnus-summary-iterate (arg &rest forms)
6595   "Iterate over the process/prefixed articles and do FORMS.
6596 ARG is the interactive prefix given to the command.  FORMS will be
6597 executed with point over the summary line of the articles."
6598   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6599     `(let ((,articles (gnus-summary-work-articles ,arg)))
6600        (while ,articles
6601          (gnus-summary-goto-subject (car ,articles))
6602          ,@forms
6603          (pop ,articles)))))
6604
6605 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6606 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6607
6608 (defun gnus-summary-save-process-mark ()
6609   "Push the current set of process marked articles on the stack."
6610   (interactive)
6611   (push (copy-sequence gnus-newsgroup-processable)
6612         gnus-newsgroup-process-stack))
6613
6614 (defun gnus-summary-kill-process-mark ()
6615   "Push the current set of process marked articles on the stack and unmark."
6616   (interactive)
6617   (gnus-summary-save-process-mark)
6618   (gnus-summary-unmark-all-processable))
6619
6620 (defun gnus-summary-yank-process-mark ()
6621   "Pop the last process mark state off the stack and restore it."
6622   (interactive)
6623   (unless gnus-newsgroup-process-stack
6624     (error "Empty mark stack"))
6625   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6626
6627 (defun gnus-summary-process-mark-set (set)
6628   "Make SET into the current process marked articles."
6629   (gnus-summary-unmark-all-processable)
6630   (mapc 'gnus-summary-set-process-mark set))
6631
6632 ;;; Searching and stuff
6633
6634 (defun gnus-summary-search-group (&optional backward use-level)
6635   "Search for next unread newsgroup.
6636 If optional argument BACKWARD is non-nil, search backward instead."
6637   (with-current-buffer gnus-group-buffer
6638     (when (gnus-group-search-forward
6639            backward nil (if use-level (gnus-group-group-level) nil))
6640       (gnus-group-group-name))))
6641
6642 (defun gnus-summary-best-group (&optional exclude-group)
6643   "Find the name of the best unread group.
6644 If EXCLUDE-GROUP, do not go to this group."
6645   (with-current-buffer gnus-group-buffer
6646     (save-excursion
6647       (gnus-group-best-unread-group exclude-group))))
6648
6649 (defun gnus-summary-find-next (&optional unread article backward)
6650   (if backward
6651       (gnus-summary-find-prev unread article)
6652     (let* ((dummy (gnus-summary-article-intangible-p))
6653            (article (or article (gnus-summary-article-number)))
6654            (data (gnus-data-find-list article))
6655            result)
6656       (when (and (not dummy)
6657                  (or (not gnus-summary-check-current)
6658                      (not unread)
6659                      (not (gnus-data-unread-p (car data)))))
6660         (setq data (cdr data)))
6661       (when (setq result
6662                   (if unread
6663                       (progn
6664                         (while data
6665                           (unless (memq (gnus-data-number (car data))
6666                                         (cond
6667                                          ((eq gnus-auto-goto-ignores
6668                                               'always-undownloaded)
6669                                           gnus-newsgroup-undownloaded)
6670                                          (gnus-plugged
6671                                           nil)
6672                                          ((eq gnus-auto-goto-ignores
6673                                               'unfetched)
6674                                           gnus-newsgroup-unfetched)
6675                                          ((eq gnus-auto-goto-ignores
6676                                               'undownloaded)
6677                                           gnus-newsgroup-undownloaded)))
6678                             (when (gnus-data-unread-p (car data))
6679                               (setq result (car data)
6680                                     data nil)))
6681                           (setq data (cdr data)))
6682                         result)
6683                     (car data)))
6684         (goto-char (gnus-data-pos result))
6685         (gnus-data-number result)))))
6686
6687 (defun gnus-summary-find-prev (&optional unread article)
6688   (let* ((eobp (eobp))
6689          (article (or article (gnus-summary-article-number)))
6690          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6691          result)
6692     (when (and (not eobp)
6693                (or (not gnus-summary-check-current)
6694                    (not unread)
6695                    (not (gnus-data-unread-p (car data)))))
6696       (setq data (cdr data)))
6697     (when (setq result
6698                 (if unread
6699                     (progn
6700                       (while data
6701                         (unless (memq (gnus-data-number (car data))
6702                                       (cond
6703                                        ((eq gnus-auto-goto-ignores
6704                                             'always-undownloaded)
6705                                         gnus-newsgroup-undownloaded)
6706                                        (gnus-plugged
6707                                         nil)
6708                                        ((eq gnus-auto-goto-ignores
6709                                             'unfetched)
6710                                         gnus-newsgroup-unfetched)
6711                                        ((eq gnus-auto-goto-ignores
6712                                             'undownloaded)
6713                                         gnus-newsgroup-undownloaded)))
6714                           (when (gnus-data-unread-p (car data))
6715                             (setq result (car data)
6716                                   data nil)))
6717                         (setq data (cdr data)))
6718                       result)
6719                   (car data)))
6720       (goto-char (gnus-data-pos result))
6721       (gnus-data-number result))))
6722
6723 (defun gnus-summary-find-subject (subject &optional unread backward article)
6724   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6725          (article (or article (gnus-summary-article-number)))
6726          (articles (gnus-data-list backward))
6727          (arts (gnus-data-find-list article articles))
6728          result)
6729     (when (or (not gnus-summary-check-current)
6730               (not unread)
6731               (not (gnus-data-unread-p (car arts))))
6732       (setq arts (cdr arts)))
6733     (while arts
6734       (and (or (not unread)
6735                (gnus-data-unread-p (car arts)))
6736            (vectorp (gnus-data-header (car arts)))
6737            (gnus-subject-equal
6738             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6739            (setq result (car arts)
6740                  arts nil))
6741       (setq arts (cdr arts)))
6742     (and result
6743          (goto-char (gnus-data-pos result))
6744          (gnus-data-number result))))
6745
6746 (defun gnus-summary-search-forward (&optional unread subject backward)
6747   "Search forward for an article.
6748 If UNREAD, look for unread articles.  If SUBJECT, look for
6749 articles with that subject.  If BACKWARD, search backward instead."
6750   (cond (subject (gnus-summary-find-subject subject unread backward))
6751         (backward (gnus-summary-find-prev unread))
6752         (t (gnus-summary-find-next unread))))
6753
6754 (defun gnus-recenter (&optional n)
6755   "Center point in window and redisplay frame.
6756 Also do horizontal recentering."
6757   (interactive "P")
6758   (when (and gnus-auto-center-summary
6759              (not (eq gnus-auto-center-summary 'vertical)))
6760     (gnus-horizontal-recenter))
6761   (if (fboundp 'recenter-top-bottom)
6762       (recenter-top-bottom n)
6763     (recenter n)))
6764
6765 (put 'gnus-recenter 'isearch-scroll t)
6766
6767 (defun gnus-forward-line-ignore-invisible (n)
6768   "Move N lines forward (backward if N is negative).
6769 Like forward-line, but skip over (and don't count) invisible lines."
6770   (let (done)
6771     (while (and (> n 0) (not done))
6772       ;; If the following character is currently invisible,
6773       ;; skip all characters with that same `invisible' property value.
6774       (while (gnus-invisible-p (point))
6775         (goto-char (gnus-next-char-property-change (point))))
6776       (forward-line 1)
6777       (if (eobp)
6778           (setq done t)
6779         (setq n (1- n))))
6780     (while (and (< n 0) (not done))
6781       (forward-line -1)
6782       (if (bobp) (setq done t)
6783         (setq n (1+ n))
6784         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6785           (goto-char (gnus-previous-char-property-change (point))))))))
6786
6787 (defun gnus-summary-recenter ()
6788   "Center point in the summary window.
6789 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6790 displayed, no centering will be performed."
6791   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6792   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6793   (interactive)
6794   ;; The user has to want it.
6795   (when gnus-auto-center-summary
6796     (let* ((top (cond ((< (window-height) 4) 0)
6797                       ((< (window-height) 7) 1)
6798                       (t (if (numberp gnus-auto-center-summary)
6799                              gnus-auto-center-summary
6800                            (/ (1- (window-height)) 2)))))
6801            (height (1- (window-height)))
6802            (bottom (save-excursion
6803                      (goto-char (point-max))
6804                      (gnus-forward-line-ignore-invisible (- height))
6805                      (point)))
6806            (window (get-buffer-window (current-buffer))))
6807       (when (get-buffer-window gnus-article-buffer)
6808         ;; Only do recentering when the article buffer is displayed,
6809         ;; Set the window start to either `bottom', which is the biggest
6810         ;; possible valid number, or the second line from the top,
6811         ;; whichever is the least.
6812         (let ((top-pos (save-excursion
6813                          (gnus-forward-line-ignore-invisible (- top))
6814                          (point))))
6815           (if (> bottom top-pos)
6816               ;; Keep the second line from the top visible
6817               (set-window-start window top-pos)
6818             ;; Try to keep the bottom line visible; if it's partially
6819             ;; obscured, either scroll one more line to make it fully
6820             ;; visible, or revert to using TOP-POS.
6821             (save-excursion
6822               (goto-char (point-max))
6823               (gnus-forward-line-ignore-invisible -1)
6824               (let ((last-line-start (point)))
6825                 (goto-char bottom)
6826                 (set-window-start window (point) t)
6827                 (when (not (pos-visible-in-window-p last-line-start window))
6828                   (gnus-forward-line-ignore-invisible 1)
6829                   (set-window-start window (min (point) top-pos) t)))))))
6830       ;; Do horizontal recentering while we're at it.
6831       (when (and (get-buffer-window (current-buffer) t)
6832                  (not (eq gnus-auto-center-summary 'vertical)))
6833         (let ((selected (selected-window)))
6834           (select-window (get-buffer-window (current-buffer) t))
6835           (gnus-summary-position-point)
6836           (gnus-horizontal-recenter)
6837           (select-window selected))))))
6838
6839 (defun gnus-summary-jump-to-group (newsgroup)
6840   "Move point to NEWSGROUP in group mode buffer."
6841   ;; Keep update point of group mode buffer if visible.
6842   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6843       (save-window-excursion
6844         ;; Take care of tree window mode.
6845         (when (get-buffer-window gnus-group-buffer)
6846           (pop-to-buffer gnus-group-buffer))
6847         (gnus-group-jump-to-group newsgroup))
6848     (save-excursion
6849       ;; Take care of tree window mode.
6850       (if (get-buffer-window gnus-group-buffer 0)
6851           (pop-to-buffer gnus-group-buffer)
6852         (set-buffer gnus-group-buffer))
6853       (gnus-group-jump-to-group newsgroup))))
6854
6855 ;; This function returns a list of article numbers based on the
6856 ;; difference between the ranges of read articles in this group and
6857 ;; the range of active articles.
6858 (defun gnus-list-of-unread-articles (group)
6859   (let* ((read (gnus-info-read (gnus-get-info group)))
6860          (active (or (gnus-active group) (gnus-activate-group group)))
6861          (last (or (cdr active)
6862                    (error "Group %s couldn't be activated " group)))
6863          (bottom (if gnus-newsgroup-maximum-articles
6864                      (max (car active)
6865                           (- last gnus-newsgroup-maximum-articles -1))
6866                    (car active)))
6867          first nlast unread)
6868     ;; If none are read, then all are unread.
6869     (if (not read)
6870         (setq first bottom)
6871       ;; If the range of read articles is a single range, then the
6872       ;; first unread article is the article after the last read
6873       ;; article.  Sounds logical, doesn't it?
6874       (if (and (not (listp (cdr read)))
6875                (or (< (car read) bottom)
6876                    (progn (setq read (list read))
6877                           nil)))
6878           (setq first (max bottom (1+ (cdr read))))
6879         ;; `read' is a list of ranges.
6880         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6881                                   (caar read)))
6882                   1)
6883           (setq first bottom))
6884         (while read
6885           (when first
6886             (while (< first nlast)
6887               (setq unread (cons first unread)
6888                     first (1+ first))))
6889           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6890           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6891           (setq read (cdr read)))))
6892     ;; And add the last unread articles.
6893     (while (<= first last)
6894       (setq unread (cons first unread)
6895             first (1+ first)))
6896     ;; Return the list of unread articles.
6897     (delq 0 (nreverse unread))))
6898
6899 (defun gnus-list-of-read-articles (group)
6900   "Return a list of unread, unticked and non-dormant articles."
6901   (let* ((info (gnus-get-info group))
6902          (marked (gnus-info-marks info))
6903          (active (gnus-active group)))
6904     (and info active
6905          (gnus-list-range-difference
6906           (gnus-list-range-difference
6907            (gnus-sorted-complement
6908             (gnus-uncompress-range
6909              (if gnus-newsgroup-maximum-articles
6910                  (cons (max (car active)
6911                             (- (cdr active)
6912                                gnus-newsgroup-maximum-articles
6913                                -1))
6914                        (cdr active))
6915                active))
6916             (gnus-list-of-unread-articles group))
6917            (cdr (assq 'dormant marked)))
6918           (cdr (assq 'tick marked))))))
6919
6920 ;; This function returns a sequence of article numbers based on the
6921 ;; difference between the ranges of read articles in this group and
6922 ;; the range of active articles.
6923 (defun gnus-sequence-of-unread-articles (group)
6924   (let* ((read (gnus-info-read (gnus-get-info group)))
6925          (active (or (gnus-active group) (gnus-activate-group group)))
6926          (last (cdr active))
6927          (bottom (if gnus-newsgroup-maximum-articles
6928                      (max (car active)
6929                           (- last gnus-newsgroup-maximum-articles -1))
6930                    (car active)))
6931          first nlast unread)
6932     ;; If none are read, then all are unread.
6933     (if (not read)
6934         (setq first bottom)
6935       ;; If the range of read articles is a single range, then the
6936       ;; first unread article is the article after the last read
6937       ;; article.  Sounds logical, doesn't it?
6938       (if (and (not (listp (cdr read)))
6939                (or (< (car read) bottom)
6940                    (progn (setq read (list read))
6941                           nil)))
6942           (setq first (max bottom (1+ (cdr read))))
6943         ;; `read' is a list of ranges.
6944         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6945                                   (caar read)))
6946                   1)
6947           (setq first bottom))
6948         (while read
6949           (when first
6950             (push (cons first nlast) unread))
6951           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6952           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6953           (setq read (cdr read)))))
6954     ;; And add the last unread articles.
6955     (cond ((not (and first last))
6956            nil)
6957           ((< first last)
6958            (push (cons first last) unread))
6959           ((= first last)
6960            (push first unread)))
6961     ;; Return the sequence of unread articles.
6962     (delq 0 (nreverse unread))))
6963
6964 ;; Various summary commands
6965
6966 (defun gnus-summary-select-article-buffer ()
6967   "Reconfigure windows to show the article buffer.
6968 If `gnus-widen-article-buffer' is set, show only the article
6969 buffer."
6970   (interactive)
6971   (if (not (gnus-buffer-live-p gnus-article-buffer))
6972       (error "There is no article buffer for this summary buffer")
6973     (unless (get-buffer-window gnus-article-buffer)
6974       (gnus-summary-show-article))
6975     (gnus-configure-windows
6976      (if gnus-widen-article-window
6977          'only-article
6978        'article)
6979      t)
6980     (select-window (get-buffer-window gnus-article-buffer))))
6981
6982 (defun gnus-summary-universal-argument (arg)
6983   "Perform any operation on all articles that are process/prefixed."
6984   (interactive "P")
6985   (let ((articles (gnus-summary-work-articles arg))
6986         func article)
6987     (if (eq
6988          (setq
6989           func
6990           (key-binding
6991            (read-key-sequence
6992             (substitute-command-keys
6993              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6994          'undefined)
6995         (gnus-error 1 "Undefined key")
6996       (save-excursion
6997         (while articles
6998           (gnus-summary-goto-subject (setq article (pop articles)))
6999           (let (gnus-newsgroup-processable)
7000             (command-execute func))
7001           (gnus-summary-remove-process-mark article)))))
7002   (gnus-summary-position-point))
7003
7004 (defun gnus-summary-toggle-truncation (&optional arg)
7005   "Toggle truncation of summary lines.
7006 With ARG, turn line truncation on if ARG is positive."
7007   (interactive "P")
7008   (setq truncate-lines
7009         (if (null arg) (not truncate-lines)
7010           (> (prefix-numeric-value arg) 0)))
7011   (redraw-display))
7012
7013 (defun gnus-summary-find-for-reselect ()
7014   "Return the number of an article to stay on across a reselect.
7015 The current article is considered, then following articles, then previous
7016 articles.  An article is sought which is not cancelled and isn't a temporary
7017 insertion from another group.  If there's no such then return a dummy 0."
7018   (let (found)
7019     (dolist (rev '(nil t))
7020       (unless found      ; don't demand the reverse list if we don't need it
7021         (let ((data (gnus-data-find-list
7022                      (gnus-summary-article-number) (gnus-data-list rev))))
7023           (while (and data (not found))
7024             (if (and (< 0 (gnus-data-number (car data)))
7025                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7026                 (setq found (gnus-data-number (car data))))
7027             (setq data (cdr data))))))
7028     (or found 0)))
7029
7030 (defun gnus-summary-reselect-current-group (&optional all rescan)
7031   "Exit and then reselect the current newsgroup.
7032 The prefix argument ALL means to select all articles."
7033   (interactive "P")
7034   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7035     (error "Ephemeral groups can't be reselected"))
7036   (let ((current-subject (gnus-summary-find-for-reselect))
7037         (group gnus-newsgroup-name))
7038     (setq gnus-newsgroup-begin nil)
7039     (gnus-summary-exit nil 'leave-hidden)
7040     ;; We have to adjust the point of group mode buffer because
7041     ;; point was moved to the next unread newsgroup by exiting.
7042     (gnus-summary-jump-to-group group)
7043     (when rescan
7044       (save-excursion
7045         (gnus-group-get-new-news-this-group 1)))
7046     (gnus-group-read-group all t)
7047     (gnus-summary-goto-subject current-subject nil t)))
7048
7049 (defun gnus-summary-rescan-group (&optional all)
7050   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7051   (interactive "P")
7052   (let ((config gnus-current-window-configuration))
7053     (gnus-summary-reselect-current-group all t)
7054     (gnus-configure-windows config)
7055     (when (eq config 'article)
7056       (gnus-summary-select-article))))
7057
7058 (defun gnus-summary-update-info (&optional non-destructive)
7059   (save-excursion
7060     (let ((group gnus-newsgroup-name))
7061       (when group
7062         (when gnus-newsgroup-kill-headers
7063           (setq gnus-newsgroup-killed
7064                 (gnus-compress-sequence
7065                  (gnus-sorted-union
7066                   (gnus-list-range-intersection
7067                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7068                   gnus-newsgroup-unreads)
7069                  t)))
7070         (unless (listp (cdr gnus-newsgroup-killed))
7071           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7072         (let ((headers gnus-newsgroup-headers))
7073           ;; Set the new ranges of read articles.
7074           (with-current-buffer gnus-group-buffer
7075             (gnus-undo-force-boundary))
7076           (gnus-update-read-articles
7077            group (gnus-sorted-union
7078                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7079           ;; Set the current article marks.
7080           (let ((gnus-newsgroup-scored
7081                  (if (and (not gnus-save-score)
7082                           (not non-destructive))
7083                      nil
7084                    gnus-newsgroup-scored)))
7085             (save-excursion
7086               (gnus-update-marks)))
7087           ;; Do the cross-ref thing.
7088           (when gnus-use-cross-reference
7089             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7090           ;; Do not switch windows but change the buffer to work.
7091           (set-buffer gnus-group-buffer)
7092           (unless (gnus-ephemeral-group-p group)
7093             (gnus-group-update-group group)))))))
7094
7095 (defun gnus-summary-save-newsrc (&optional force)
7096   "Save the current number of read/marked articles in the dribble buffer.
7097 The dribble buffer will then be saved.
7098 If FORCE (the prefix), also save the .newsrc file(s)."
7099   (interactive "P")
7100   (gnus-summary-update-info t)
7101   (if force
7102       (gnus-save-newsrc-file)
7103     (gnus-dribble-save)))
7104
7105 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7106
7107 (defun gnus-summary-exit (&optional temporary leave-hidden)
7108   "Exit reading current newsgroup, and then return to group selection mode.
7109 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7110   (interactive)
7111   (gnus-set-global-variables)
7112   (when (gnus-buffer-live-p gnus-article-buffer)
7113     (with-current-buffer gnus-article-buffer
7114       (mm-destroy-parts gnus-article-mime-handles)
7115       ;; Set it to nil for safety reason.
7116       (setq gnus-article-mime-handle-alist nil)
7117       (setq gnus-article-mime-handles nil)))
7118   (gnus-kill-save-kill-buffer)
7119   (gnus-async-halt-prefetch)
7120   (let* ((group gnus-newsgroup-name)
7121          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7122          (gnus-group-is-exiting-p t)
7123          (mode major-mode)
7124          (group-point nil)
7125          (buf (current-buffer)))
7126     (unless quit-config
7127       ;; Do adaptive scoring, and possibly save score files.
7128       (when gnus-newsgroup-adaptive
7129         (gnus-score-adaptive))
7130       (when gnus-use-scoring
7131         (gnus-score-save)))
7132     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7133     (when gnus-use-cache
7134       (gnus-cache-possibly-remove-articles)
7135       (gnus-cache-save-buffers))
7136     (gnus-async-prefetch-remove-group group)
7137     (when gnus-suppress-duplicates
7138       (gnus-dup-enter-articles))
7139     (when gnus-use-trees
7140       (gnus-tree-close group))
7141     (when gnus-use-cache
7142       (gnus-cache-write-active))
7143     ;; Remove entries for this group.
7144     (nnmail-purge-split-history (gnus-group-real-name group))
7145     ;; Make all changes in this group permanent.
7146     (unless quit-config
7147       (gnus-run-hooks 'gnus-exit-group-hook)
7148       (gnus-summary-update-info))
7149     (gnus-close-group group)
7150     ;; Make sure where we were, and go to next newsgroup.
7151     (set-buffer gnus-group-buffer)
7152     (unless quit-config
7153       (gnus-group-jump-to-group group))
7154     (gnus-run-hooks 'gnus-summary-exit-hook)
7155     (unless (or quit-config
7156                 (not gnus-summary-next-group-on-exit)
7157                 ;; If this group has disappeared from the summary
7158                 ;; buffer, don't skip forwards.
7159                 (not (string= group (gnus-group-group-name))))
7160       (gnus-group-next-unread-group 1))
7161     (setq group-point (point))
7162     (if temporary
7163         nil                             ;Nothing to do.
7164       (set-buffer buf)
7165       (if (not gnus-kill-summary-on-exit)
7166           (progn
7167             (gnus-deaden-summary)
7168             (setq mode nil))
7169         (when (get-buffer gnus-article-buffer)
7170           (bury-buffer gnus-article-buffer))
7171         ;; Return to group mode buffer.
7172         (when (eq mode 'gnus-summary-mode)
7173           (gnus-kill-buffer buf)))
7174
7175       ;; If we have several article buffers, we kill them at exit.
7176       (unless gnus-single-article-buffer
7177         (when (gnus-buffer-live-p gnus-article-buffer)
7178           (with-current-buffer gnus-article-buffer
7179             ;; Don't kill sticky article buffers
7180             (unless (eq major-mode 'gnus-sticky-article-mode)
7181               (gnus-kill-buffer gnus-article-buffer)
7182               (setq gnus-article-current nil))))
7183         (gnus-kill-buffer gnus-original-article-buffer))
7184
7185       (setq gnus-current-select-method gnus-select-method)
7186       (set-buffer gnus-group-buffer)
7187       (if quit-config
7188           (gnus-handle-ephemeral-exit quit-config)
7189         (goto-char group-point)
7190         ;; If gnus-group-buffer is already displayed, make sure we also move
7191         ;; the cursor in the window that displays it.
7192         (let ((win (get-buffer-window (current-buffer) 0)))
7193           (if win (set-window-point win (point))))
7194         (unless leave-hidden
7195           (gnus-configure-windows 'group 'force)))
7196       ;; Clear the current group name.
7197       (unless quit-config
7198         (setq gnus-newsgroup-name nil)))))
7199
7200 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7201 (defun gnus-summary-exit-no-update (&optional no-questions)
7202   "Quit reading current newsgroup without updating read article info."
7203   (interactive)
7204   (let* ((group gnus-newsgroup-name)
7205          (gnus-group-is-exiting-p t)
7206          (gnus-group-is-exiting-without-update-p t)
7207          (quit-config (gnus-group-quit-config group)))
7208     (when (or no-questions
7209               gnus-expert-user
7210               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7211       (gnus-async-halt-prefetch)
7212       (run-hooks 'gnus-summary-prepare-exit-hook)
7213       (when (gnus-buffer-live-p gnus-article-buffer)
7214         (with-current-buffer gnus-article-buffer
7215           (mm-destroy-parts gnus-article-mime-handles)
7216           ;; Set it to nil for safety reason.
7217           (setq gnus-article-mime-handle-alist nil)
7218           (setq gnus-article-mime-handles nil)))
7219       ;; If we have several article buffers, we kill them at exit.
7220       (unless gnus-single-article-buffer
7221         (gnus-kill-buffer gnus-article-buffer)
7222         (gnus-kill-buffer gnus-original-article-buffer)
7223         (setq gnus-article-current nil))
7224       (if (not gnus-kill-summary-on-exit)
7225           (gnus-deaden-summary)
7226         (gnus-close-group group)
7227         (gnus-kill-buffer gnus-summary-buffer))
7228       (unless gnus-single-article-buffer
7229         (setq gnus-article-current nil))
7230       (when gnus-use-trees
7231         (gnus-tree-close group))
7232       (gnus-async-prefetch-remove-group group)
7233       (when (get-buffer gnus-article-buffer)
7234         (bury-buffer gnus-article-buffer))
7235       ;; Return to the group buffer.
7236       (gnus-configure-windows 'group 'force)
7237       ;; Clear the current group name.
7238       (setq gnus-newsgroup-name nil)
7239       (unless (gnus-ephemeral-group-p group)
7240         (gnus-group-update-group group))
7241       (when (equal (gnus-group-group-name) group)
7242         (gnus-group-next-unread-group 1))
7243       (when quit-config
7244         (gnus-handle-ephemeral-exit quit-config)))))
7245
7246 (defun gnus-handle-ephemeral-exit (quit-config)
7247   "Handle movement when leaving an ephemeral group.
7248 The state which existed when entering the ephemeral is reset."
7249   (if (not (buffer-name (car quit-config)))
7250       (gnus-configure-windows 'group 'force)
7251     (set-buffer (car quit-config))
7252     (cond ((eq major-mode 'gnus-summary-mode)
7253            (gnus-set-global-variables))
7254           ((eq major-mode 'gnus-article-mode)
7255            (save-current-buffer
7256              ;; The `gnus-summary-buffer' variable may point
7257              ;; to the old summary buffer when using a single
7258              ;; article buffer.
7259              (unless (gnus-buffer-live-p gnus-summary-buffer)
7260                (set-buffer gnus-group-buffer))
7261              (set-buffer gnus-summary-buffer)
7262              (gnus-set-global-variables))))
7263     (if (or (eq (cdr quit-config) 'article)
7264             (eq (cdr quit-config) 'pick))
7265         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7266             (gnus-configure-windows 'pick 'force)
7267           (gnus-configure-windows (cdr quit-config) 'force))
7268       (gnus-configure-windows (cdr quit-config) 'force))
7269     (when (eq major-mode 'gnus-summary-mode)
7270       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7271                                                      next-unread-noselect))
7272           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7273                                   'next-noselect)
7274                               (gnus-summary-next-subject 1 nil t))
7275                              ((eq gnus-auto-select-on-ephemeral-exit
7276                                   'next-unread-noselect)
7277                               (gnus-summary-next-subject 1 t t))))
7278             ;; Hide the article buffer which displays the article different
7279             ;; from the one that the cursor points to in the summary buffer.
7280             (gnus-configure-windows 'summary 'force))
7281         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7282                (gnus-summary-next-subject 1))
7283               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7284                (gnus-summary-next-subject 1 t))))
7285       (gnus-summary-recenter)
7286       (gnus-summary-position-point))))
7287
7288 ;;; Dead summaries.
7289
7290 (defvar gnus-dead-summary-mode-map
7291   (let ((map (make-keymap)))
7292     (suppress-keymap map)
7293     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7294     (dolist (key '("\C-d" "\r" "\177" [delete]))
7295       (define-key map key 'gnus-summary-wake-up-the-dead))
7296     (dolist (key '("q" "Q"))
7297       (define-key map key 'bury-buffer))
7298     map))
7299
7300 (define-minor-mode gnus-dead-summary-mode
7301   "Minor mode for Gnus summary buffers."
7302   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7303   (unless (derived-mode-p 'gnus-summary-mode)
7304     (setq gnus-dead-summary-mode nil)))
7305
7306 (defun gnus-deaden-summary ()
7307   "Make the current summary buffer into a dead summary buffer."
7308   ;; Kill any previous dead summary buffer.
7309   (when (and gnus-dead-summary
7310              (buffer-name gnus-dead-summary))
7311     (with-current-buffer gnus-dead-summary
7312       (when gnus-dead-summary-mode
7313         (kill-buffer (current-buffer)))))
7314   ;; Make this the current dead summary.
7315   (setq gnus-dead-summary (current-buffer))
7316   (gnus-dead-summary-mode 1)
7317   (let ((name (buffer-name)))
7318     (when (string-match "Summary" name)
7319       (rename-buffer
7320        (concat (substring name 0 (match-beginning 0)) "Dead "
7321                (substring name (match-beginning 0)))
7322        t)
7323       (bury-buffer))))
7324
7325 (defun gnus-kill-or-deaden-summary (buffer)
7326   "Kill or deaden the summary BUFFER."
7327   (save-excursion
7328     (when (and (buffer-name buffer)
7329                (not gnus-single-article-buffer))
7330       (with-current-buffer buffer
7331         (gnus-kill-buffer gnus-article-buffer)
7332         (gnus-kill-buffer gnus-original-article-buffer)))
7333     (cond
7334      ;; Kill the buffer.
7335      (gnus-kill-summary-on-exit
7336       (when (and gnus-use-trees
7337                  (gnus-buffer-exists-p buffer))
7338         (with-current-buffer buffer
7339           (gnus-tree-close gnus-newsgroup-name)))
7340       (gnus-kill-buffer buffer))
7341      ;; Deaden the buffer.
7342      ((gnus-buffer-exists-p buffer)
7343       (with-current-buffer buffer
7344         (gnus-deaden-summary))))))
7345
7346 (defun gnus-summary-wake-up-the-dead (&rest args)
7347   "Wake up the dead summary buffer."
7348   (interactive)
7349   (gnus-dead-summary-mode -1)
7350   (let ((name (buffer-name)))
7351     (when (string-match "Dead " name)
7352       (rename-buffer
7353        (concat (substring name 0 (match-beginning 0))
7354                (substring name (match-end 0)))
7355        t)))
7356   (gnus-message 3 "This dead summary is now alive again"))
7357
7358 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7359 (defun gnus-summary-describe-group (&optional force)
7360   "Describe the current newsgroup."
7361   (interactive "P")
7362   (gnus-group-describe-group force gnus-newsgroup-name))
7363
7364 (defun gnus-summary-describe-briefly ()
7365   "Describe summary mode commands briefly."
7366   (interactive)
7367   (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")))
7368
7369 ;; Walking around group mode buffer from summary mode.
7370
7371 (defun gnus-summary-next-group (&optional no-article target-group backward)
7372   "Exit current newsgroup and then select next unread newsgroup.
7373 If prefix argument NO-ARTICLE is non-nil, no article is selected
7374 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7375 previous group instead."
7376   (interactive "P")
7377   ;; Stop pre-fetching.
7378   (gnus-async-halt-prefetch)
7379   (let ((current-group gnus-newsgroup-name)
7380         (current-buffer (current-buffer))
7381         entered)
7382     ;; First we semi-exit this group to update Xrefs and all variables.
7383     ;; We can't do a real exit, because the window conf must remain
7384     ;; the same in case the user is prompted for info, and we don't
7385     ;; want the window conf to change before that...
7386     (gnus-summary-exit t)
7387     (while (not entered)
7388       ;; Then we find what group we are supposed to enter.
7389       (set-buffer gnus-group-buffer)
7390       (gnus-group-jump-to-group current-group)
7391       (setq target-group
7392             (or target-group
7393                 (if (eq gnus-keep-same-level 'best)
7394                     (gnus-summary-best-group gnus-newsgroup-name)
7395                   (gnus-summary-search-group backward gnus-keep-same-level))))
7396       (if (not target-group)
7397           ;; There are no further groups, so we return to the group
7398           ;; buffer.
7399           (progn
7400             (gnus-message 5 "Returning to the group buffer")
7401             (setq entered t)
7402             (when (gnus-buffer-live-p current-buffer)
7403               (set-buffer current-buffer)
7404               (gnus-summary-exit))
7405             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7406         ;; We try to enter the target group.
7407         (gnus-group-jump-to-group target-group)
7408         (let ((unreads (gnus-group-group-unread)))
7409           (if (and (or (eq t unreads)
7410                        (and unreads (not (zerop unreads))))
7411                    (gnus-summary-read-group
7412                     target-group nil no-article
7413                     (and (buffer-name current-buffer) current-buffer)
7414                     nil backward))
7415               (setq entered t)
7416             (setq current-group target-group
7417                   target-group nil)))))))
7418
7419 (defun gnus-summary-prev-group (&optional no-article)
7420   "Exit current newsgroup and then select previous unread newsgroup.
7421 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7422   (interactive "P")
7423   (gnus-summary-next-group no-article nil t))
7424
7425 ;; Walking around summary lines.
7426
7427 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7428   "Go to the first subject satisfying any non-nil constraint.
7429 If UNREAD is non-nil, the article should be unread.
7430 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7431 If UNSEEN is non-nil, the article should be unseen as well as unread.
7432 Returns the article selected or nil if there are no matching articles."
7433   (interactive "P")
7434   (cond
7435    ;; Empty summary.
7436    ((null gnus-newsgroup-data)
7437     (gnus-message 3 "No articles in the group")
7438     nil)
7439    ;; Pick the first article.
7440    ((not (or unread undownloaded unseen))
7441     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7442     (gnus-data-number (car gnus-newsgroup-data)))
7443    ;; Find the first unread article.
7444    (t
7445     (let ((data gnus-newsgroup-data))
7446       (while (and data
7447                   (let ((num (gnus-data-number (car data))))
7448                     (or (memq num gnus-newsgroup-unfetched)
7449                         (not (or (and unread
7450                                       (memq num gnus-newsgroup-unreads))
7451                                  (and undownloaded
7452                                       (memq num gnus-newsgroup-undownloaded))
7453                                  (and unseen
7454                                       (memq num gnus-newsgroup-unseen)
7455                                       (memq num gnus-newsgroup-unreads)))))))
7456         (setq data (cdr data)))
7457       (prog1
7458           (if data
7459               (progn
7460                 (goto-char (gnus-data-pos (car data)))
7461                 (gnus-data-number (car data)))
7462             (gnus-message 3 "No more%s articles"
7463                           (let* ((r (when unread " unread"))
7464                                  (d (when undownloaded " undownloaded"))
7465                                  (s (when unseen " unseen"))
7466                                  (l (delq nil (list r d s))))
7467                             (cond ((= 3 (length l))
7468                                    (concat r "," d ", or" s))
7469                                   ((= 2 (length l))
7470                                    (concat (car l) ", or" (cadr l)))
7471                                   ((= 1 (length l))
7472                                    (car l))
7473                                   (t
7474                                    ""))))
7475             nil
7476             )
7477         (gnus-summary-position-point))))))
7478
7479 (defun gnus-summary-next-subject (n &optional unread dont-display)
7480   "Go to next N'th summary line.
7481 If N is negative, go to the previous N'th subject line.
7482 If UNREAD is non-nil, only unread articles are selected.
7483 The difference between N and the actual number of steps taken is
7484 returned."
7485   (interactive "p")
7486   (let ((backward (< n 0))
7487         (n (abs n)))
7488     (while (and (> n 0)
7489                 (if backward
7490                     (gnus-summary-find-prev unread)
7491                   (gnus-summary-find-next unread)))
7492       (unless (zerop (setq n (1- n)))
7493         (gnus-summary-show-thread)))
7494     (when (/= 0 n)
7495       (gnus-message 7 "No more%s articles"
7496                     (if unread " unread" "")))
7497     (unless dont-display
7498       (gnus-summary-recenter)
7499       (gnus-summary-position-point))
7500     n))
7501
7502 (defun gnus-summary-next-unread-subject (n)
7503   "Go to next N'th unread summary line."
7504   (interactive "p")
7505   (gnus-summary-next-subject n t))
7506
7507 (defun gnus-summary-prev-subject (n &optional unread)
7508   "Go to previous N'th summary line.
7509 If optional argument UNREAD is non-nil, only unread article is selected."
7510   (interactive "p")
7511   (gnus-summary-next-subject (- n) unread))
7512
7513 (defun gnus-summary-prev-unread-subject (n)
7514   "Go to previous N'th unread summary line."
7515   (interactive "p")
7516   (gnus-summary-next-subject (- n) t))
7517
7518 (defun gnus-summary-goto-subjects (articles)
7519   "Insert the subject header for ARTICLES in the current buffer."
7520   (save-excursion
7521     (dolist (article articles)
7522       (gnus-summary-goto-subject article t)))
7523   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7524   (gnus-summary-position-point))
7525
7526 (defun gnus-summary-goto-subject (article &optional force silent)
7527   "Go to the subject line of ARTICLE.
7528 If FORCE, also allow jumping to articles not currently shown."
7529   (interactive "nArticle number: ")
7530   (unless (numberp article)
7531     (error "Article %s is not a number" article))
7532   (let ((b (point))
7533         (data (gnus-data-find article)))
7534     ;; We read in the article if we have to.
7535     (and (not data)
7536          force
7537          (gnus-summary-insert-subject
7538           article
7539           (if (or (numberp force) (vectorp force)) force)
7540           t)
7541          (setq data (gnus-data-find article)))
7542     (goto-char b)
7543     (if (not data)
7544         (progn
7545           (unless silent
7546             (gnus-message 3 "Can't find article %d" article))
7547           nil)
7548       (let ((pt (gnus-data-pos data)))
7549         (goto-char pt)
7550         (gnus-summary-set-article-display-arrow pt))
7551       (gnus-summary-position-point)
7552       article)))
7553
7554 ;; Walking around summary lines with displaying articles.
7555
7556 (defun gnus-summary-expand-window (&optional arg)
7557   "Make the summary buffer take up the entire Emacs frame.
7558 Given a prefix, will force an `article' buffer configuration."
7559   (interactive "P")
7560   (if arg
7561       (gnus-configure-windows 'article 'force)
7562     (gnus-configure-windows 'summary 'force)))
7563
7564 (defun gnus-summary-display-article (article &optional all-header)
7565   "Display ARTICLE in article buffer."
7566   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7567                (with-current-buffer gnus-article-buffer
7568                  (eq major-mode 'gnus-article-mode)))
7569     (gnus-article-setup-buffer))
7570   (gnus-set-global-variables)
7571   (with-current-buffer gnus-article-buffer
7572     (setq gnus-article-charset gnus-newsgroup-charset)
7573     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7574     (mm-enable-multibyte))
7575   (if (null article)
7576       nil
7577     (prog1
7578         (if gnus-summary-display-article-function
7579             (funcall gnus-summary-display-article-function article all-header)
7580           (gnus-article-prepare article all-header))
7581       (gnus-run-hooks 'gnus-select-article-hook)
7582       (when (and gnus-current-article
7583                  (not (zerop gnus-current-article)))
7584         (gnus-summary-goto-subject gnus-current-article))
7585       (gnus-summary-recenter)
7586       (when (and gnus-use-trees gnus-show-threads)
7587         (gnus-possibly-generate-tree article)
7588         (gnus-highlight-selected-tree article))
7589       ;; Successfully display article.
7590       (gnus-article-set-window-start
7591        (cdr (assq article gnus-newsgroup-bookmarks))))))
7592
7593 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7594   "Select the current article.
7595 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7596 non-nil, the article will be re-fetched even if it already present in
7597 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7598 be displayed."
7599   ;; Make sure we are in the summary buffer to work around bbdb bug.
7600   (unless (eq major-mode 'gnus-summary-mode)
7601     (set-buffer gnus-summary-buffer))
7602   (let ((article (or article (gnus-summary-article-number)))
7603         (all-headers (not (not all-headers))) ;Must be t or nil.
7604         gnus-summary-display-article-function)
7605     (and (not pseudo)
7606          (gnus-summary-article-pseudo-p article)
7607          (error "This is a pseudo-article"))
7608     (with-current-buffer gnus-summary-buffer
7609       (if (or (and gnus-single-article-buffer
7610                    (or (null gnus-current-article)
7611                        (null gnus-article-current)
7612                        (null (get-buffer gnus-article-buffer))
7613                        (not (eq article (cdr gnus-article-current)))
7614                        (not (equal (car gnus-article-current)
7615                                    gnus-newsgroup-name))
7616                        (not (get-buffer gnus-original-article-buffer))))
7617               (and (not gnus-single-article-buffer)
7618                    (or (null gnus-current-article)
7619                        (not (get-buffer gnus-original-article-buffer))
7620                        (not (eq gnus-current-article article))))
7621               force)
7622           ;; The requested article is different from the current article.
7623           (progn
7624             (gnus-summary-display-article article all-headers)
7625             (when (gnus-buffer-live-p gnus-article-buffer)
7626               (with-current-buffer gnus-article-buffer
7627                 (if (not gnus-article-decoded-p) ;; a local variable
7628                     (mm-disable-multibyte))))
7629             (gnus-article-set-window-start
7630              (cdr (assq article gnus-newsgroup-bookmarks)))
7631             article)
7632         'old))))
7633
7634 (defun gnus-summary-force-verify-and-decrypt ()
7635   "Display buttons for signed/encrypted parts and verify/decrypt them."
7636   (interactive)
7637   (let ((mm-verify-option 'known)
7638         (mm-decrypt-option 'known)
7639         (gnus-article-emulate-mime t)
7640         (gnus-buttonized-mime-types (append (list "multipart/signed"
7641                                                   "multipart/encrypted")
7642                                             gnus-buttonized-mime-types)))
7643     (gnus-summary-select-article nil 'force)))
7644
7645 (defun gnus-summary-set-current-mark (&optional current-mark)
7646   "Obsolete function."
7647   nil)
7648
7649 (defun gnus-summary-next-article (&optional unread subject backward push)
7650   "Select the next article.
7651 If UNREAD, only unread articles are selected.
7652 If SUBJECT, only articles with SUBJECT are selected.
7653 If BACKWARD, the previous article is selected instead of the next."
7654   (interactive "P")
7655   ;; Make sure we are in the summary buffer.
7656   (unless (eq major-mode 'gnus-summary-mode)
7657     (set-buffer gnus-summary-buffer))
7658   (cond
7659    ;; Is there such an article?
7660    ((and (gnus-summary-search-forward unread subject backward)
7661          (or (gnus-summary-display-article (gnus-summary-article-number))
7662              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7663     (gnus-summary-position-point))
7664    ;; If not, we try the first unread, if that is wanted.
7665    ((and subject
7666          gnus-auto-select-same
7667          (gnus-summary-first-unread-article))
7668     (gnus-summary-position-point)
7669     (gnus-message 6 "Wrapped"))
7670    ;; Try to get next/previous article not displayed in this group.
7671    ((and gnus-auto-extend-newsgroup
7672          (not unread) (not subject))
7673     (gnus-summary-goto-article
7674      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7675      nil (count-lines (point-min) (point))))
7676    ;; Go to next/previous group.
7677    (t
7678     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7679       (gnus-summary-jump-to-group gnus-newsgroup-name))
7680     (let ((cmd (if (featurep 'xemacs)
7681                    last-command-char
7682                  last-command-event))
7683           (point
7684            (with-current-buffer gnus-group-buffer
7685              (point)))
7686           (group
7687            (if (eq gnus-keep-same-level 'best)
7688                (gnus-summary-best-group gnus-newsgroup-name)
7689              (gnus-summary-search-group backward gnus-keep-same-level))))
7690       ;; For some reason, the group window gets selected.  We change
7691       ;; it back.
7692       (select-window (get-buffer-window (current-buffer)))
7693       ;; Select next unread newsgroup automagically.
7694       (cond
7695        ((or (not gnus-auto-select-next)
7696             (not cmd))
7697         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7698        ((or (eq gnus-auto-select-next 'quietly)
7699             (and (eq gnus-auto-select-next 'slightly-quietly)
7700                  push)
7701             (and (eq gnus-auto-select-next 'almost-quietly)
7702                  (gnus-summary-last-article-p)))
7703         ;; Select quietly.
7704         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7705             (gnus-summary-exit)
7706           (gnus-message 7 "No more%s articles (%s)..."
7707                         (if unread " unread" "")
7708                         (if group (concat "selecting " group)
7709                           "exiting"))
7710           (gnus-summary-next-group nil group backward)))
7711        (t
7712         (when (gnus-key-press-event-p last-input-event)
7713           (gnus-summary-walk-group-buffer
7714            gnus-newsgroup-name cmd unread backward point))))))))
7715
7716 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7717   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7718                       (?\C-p (gnus-group-prev-unread-group 1))))
7719         (cursor-in-echo-area t)
7720         keve key group ended prompt)
7721     (with-current-buffer gnus-group-buffer
7722       (goto-char start)
7723       (setq group
7724             (if (eq gnus-keep-same-level 'best)
7725                 (gnus-summary-best-group gnus-newsgroup-name)
7726               (gnus-summary-search-group backward gnus-keep-same-level))))
7727     (while (not ended)
7728       (setq prompt
7729             (format
7730              "No more%s articles%s " (if unread " unread" "")
7731              (if (and group
7732                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7733                  (format " (Type %s for %s [%s])"
7734                          (single-key-description cmd)
7735                          (gnus-group-decoded-name group)
7736                          (gnus-group-unread group))
7737                (format " (Type %s to exit %s)"
7738                        (single-key-description cmd)
7739                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7740       ;; Confirm auto selection.
7741       (setq key (car (setq keve (gnus-read-event-char prompt)))
7742             ended t)
7743       (cond
7744        ((assq key keystrokes)
7745         (let ((obuf (current-buffer)))
7746           (switch-to-buffer gnus-group-buffer)
7747           (when group
7748             (gnus-group-jump-to-group group))
7749           (eval (cadr (assq key keystrokes)))
7750           (setq group (gnus-group-group-name))
7751           (switch-to-buffer obuf))
7752         (setq ended nil))
7753        ((equal key cmd)
7754         (if (or (not group)
7755                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7756             (gnus-summary-exit)
7757           (gnus-summary-next-group nil group backward)))
7758        (t
7759         (push (cdr keve) unread-command-events))))))
7760
7761 (defun gnus-summary-next-unread-article ()
7762   "Select unread article after current one."
7763   (interactive)
7764   (gnus-summary-next-article
7765    (or (not (eq gnus-summary-goto-unread 'never))
7766        (gnus-summary-last-article-p (gnus-summary-article-number)))
7767    (and gnus-auto-select-same
7768         (gnus-summary-article-subject))))
7769
7770 (defun gnus-summary-prev-article (&optional unread subject)
7771   "Select the article before the current one.
7772 If UNREAD is non-nil, only unread articles are selected."
7773   (interactive "P")
7774   (gnus-summary-next-article unread subject t))
7775
7776 (defun gnus-summary-prev-unread-article ()
7777   "Select unread article before current one."
7778   (interactive)
7779   (gnus-summary-prev-article
7780    (or (not (eq gnus-summary-goto-unread 'never))
7781        (gnus-summary-first-article-p (gnus-summary-article-number)))
7782    (and gnus-auto-select-same
7783         (gnus-summary-article-subject))))
7784
7785 (defun gnus-summary-next-page (&optional lines circular stop)
7786   "Show next page of the selected article.
7787 If at the end of the current article, select the next article.
7788 LINES says how many lines should be scrolled up.
7789
7790 If CIRCULAR is non-nil, go to the start of the article instead of
7791 selecting the next article when reaching the end of the current
7792 article.
7793
7794 If STOP is non-nil, just stop when reaching the end of the message.
7795
7796 Also see the variable `gnus-article-skip-boring'."
7797   (interactive "P")
7798   (setq gnus-summary-buffer (current-buffer))
7799   (gnus-set-global-variables)
7800   (let ((article (gnus-summary-article-number))
7801         (article-window (get-buffer-window gnus-article-buffer t))
7802         endp)
7803     ;; If the buffer is empty, we have no article.
7804     (unless article
7805       (error "No article to select"))
7806     (gnus-configure-windows 'article)
7807     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7808         (if (and (eq gnus-summary-goto-unread 'never)
7809                  (not (gnus-summary-last-article-p article)))
7810             (gnus-summary-next-article)
7811           (gnus-summary-next-unread-article))
7812       (if (or (null gnus-current-article)
7813               (null gnus-article-current)
7814               (/= article (cdr gnus-article-current))
7815               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7816           ;; Selected subject is different from current article's.
7817           (gnus-summary-display-article article)
7818         (when article-window
7819           (gnus-eval-in-buffer-window gnus-article-buffer
7820             (setq endp (or (gnus-article-next-page lines)
7821                            (gnus-article-only-boring-p))))
7822           (when endp
7823             (cond ((or stop gnus-summary-stop-at-end-of-message)
7824                    (gnus-message 3 "End of message"))
7825                   (circular
7826                    (gnus-summary-beginning-of-article))
7827                   (lines
7828                    (gnus-message 3 "End of message"))
7829                   ((null lines)
7830                    (if (and (eq gnus-summary-goto-unread 'never)
7831                             (not (gnus-summary-last-article-p article)))
7832                        (gnus-summary-next-article)
7833                      (gnus-summary-next-unread-article))))))))
7834     (gnus-summary-recenter)
7835     (gnus-summary-position-point)))
7836
7837 (defun gnus-summary-prev-page (&optional lines move)
7838   "Show previous page of selected article.
7839 Argument LINES specifies lines to be scrolled down.
7840 If MOVE, move to the previous unread article if point is at
7841 the beginning of the buffer."
7842   (interactive "P")
7843   (let ((article (gnus-summary-article-number))
7844         (article-window (get-buffer-window gnus-article-buffer t))
7845         endp)
7846     (gnus-configure-windows 'article)
7847     (if (or (null gnus-current-article)
7848             (null gnus-article-current)
7849             (/= article (cdr gnus-article-current))
7850             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7851         ;; Selected subject is different from current article's.
7852         (gnus-summary-display-article article)
7853       (gnus-summary-recenter)
7854       (when article-window
7855         (gnus-eval-in-buffer-window gnus-article-buffer
7856           (setq endp (gnus-article-prev-page lines)))
7857         (when (and move endp)
7858           (cond (lines
7859                  (gnus-message 3 "Beginning of message"))
7860                 ((null lines)
7861                  (if (and (eq gnus-summary-goto-unread 'never)
7862                           (not (gnus-summary-first-article-p article)))
7863                      (gnus-summary-prev-article)
7864                    (gnus-summary-prev-unread-article))))))))
7865   (gnus-summary-position-point))
7866
7867 (defun gnus-summary-prev-page-or-article (&optional lines)
7868   "Show previous page of selected article.
7869 Argument LINES specifies lines to be scrolled down.
7870 If at the beginning of the article, go to the next article."
7871   (interactive "P")
7872   (gnus-summary-prev-page lines t))
7873
7874 (defun gnus-summary-scroll-up (lines)
7875   "Scroll up (or down) one line current article.
7876 Argument LINES specifies lines to be scrolled up (or down if negative).
7877 If no article is selected, then the current article will be selected first."
7878   (interactive "p")
7879   (gnus-configure-windows 'article)
7880   (gnus-summary-show-thread)
7881   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7882     (gnus-eval-in-buffer-window gnus-article-buffer
7883       (cond ((> lines 0)
7884              (when (gnus-article-next-page lines)
7885                (gnus-message 3 "End of message")))
7886             ((< lines 0)
7887              (gnus-article-prev-page (- lines))))))
7888   (gnus-summary-recenter)
7889   (gnus-summary-position-point))
7890
7891 (defun gnus-summary-scroll-down (lines)
7892   "Scroll down (or up) one line current article.
7893 Argument LINES specifies lines to be scrolled down (or up if negative).
7894 If no article is selected, then the current article will be selected first."
7895   (interactive "p")
7896   (gnus-summary-scroll-up (- lines)))
7897
7898 (defun gnus-summary-next-same-subject ()
7899   "Select next article which has the same subject as current one."
7900   (interactive)
7901   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7902
7903 (defun gnus-summary-prev-same-subject ()
7904   "Select previous article which has the same subject as current one."
7905   (interactive)
7906   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7907
7908 (defun gnus-summary-next-unread-same-subject ()
7909   "Select next unread article which has the same subject as current one."
7910   (interactive)
7911   (gnus-summary-next-article t (gnus-summary-article-subject)))
7912
7913 (defun gnus-summary-prev-unread-same-subject ()
7914   "Select previous unread article which has the same subject as current one."
7915   (interactive)
7916   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7917
7918 (defun gnus-summary-first-unread-article ()
7919   "Select the first unread article.
7920 Return nil if there are no unread articles."
7921   (interactive)
7922   (prog1
7923       (when (gnus-summary-first-subject t)
7924         (gnus-summary-show-thread)
7925         (gnus-summary-first-subject t)
7926         (gnus-summary-display-article (gnus-summary-article-number)))
7927     (gnus-summary-position-point)))
7928
7929 (defun gnus-summary-first-unread-subject ()
7930   "Place the point on the subject line of the first unread article.
7931 Return nil if there are no unread articles."
7932   (interactive)
7933   (prog1
7934       (when (gnus-summary-first-subject t)
7935         (gnus-summary-show-thread)
7936         (gnus-summary-first-subject t))
7937     (gnus-summary-position-point)))
7938
7939 (defun gnus-summary-first-unseen-subject ()
7940   "Place the point on the subject line of the first unseen article.
7941 Return nil if there are no unseen articles."
7942   (interactive)
7943   (prog1
7944       (when (gnus-summary-first-subject nil nil t)
7945         (gnus-summary-show-thread)
7946         (gnus-summary-first-subject nil nil t))
7947     (gnus-summary-position-point)))
7948
7949 (defun gnus-summary-first-unseen-or-unread-subject ()
7950   "Place the point on the subject line of the first unseen and unread article.
7951 If all article have been seen, on the subject line of the first unread
7952 article."
7953   (interactive)
7954   (prog1
7955       (unless (when (gnus-summary-first-subject nil nil t)
7956                 (gnus-summary-show-thread)
7957                 (gnus-summary-first-subject nil nil t))
7958         (when (gnus-summary-first-subject t)
7959           (gnus-summary-show-thread)
7960           (gnus-summary-first-subject t)))
7961     (gnus-summary-position-point)))
7962
7963 (defun gnus-summary-first-article ()
7964   "Select the first article.
7965 Return nil if there are no articles."
7966   (interactive)
7967   (prog1
7968       (when (gnus-summary-first-subject)
7969         (gnus-summary-show-thread)
7970         (gnus-summary-first-subject)
7971         (gnus-summary-display-article (gnus-summary-article-number)))
7972     (gnus-summary-position-point)))
7973
7974 (defun gnus-summary-best-unread-article (&optional arg)
7975   "Select the unread article with the highest score.
7976 If given a prefix argument, select the next unread article that has a
7977 score higher than the default score."
7978   (interactive "P")
7979   (let ((article (if arg
7980                      (gnus-summary-better-unread-subject)
7981                    (gnus-summary-best-unread-subject))))
7982     (if article
7983         (gnus-summary-goto-article article)
7984       (error "No unread articles"))))
7985
7986 (defun gnus-summary-best-unread-subject ()
7987   "Select the unread subject with the highest score."
7988   (interactive)
7989   (let ((best -1000000)
7990         (data gnus-newsgroup-data)
7991         article score)
7992     (while data
7993       (and (gnus-data-unread-p (car data))
7994            (> (setq score
7995                     (gnus-summary-article-score (gnus-data-number (car data))))
7996               best)
7997            (setq best score
7998                  article (gnus-data-number (car data))))
7999       (setq data (cdr data)))
8000     (when article
8001       (gnus-summary-goto-subject article))
8002     (gnus-summary-position-point)
8003     article))
8004
8005 (defun gnus-summary-better-unread-subject ()
8006   "Select the first unread subject that has a score over the default score."
8007   (interactive)
8008   (let ((data gnus-newsgroup-data)
8009         article score)
8010     (while (and (setq article (gnus-data-number (car data)))
8011                 (or (gnus-data-read-p (car data))
8012                     (not (> (gnus-summary-article-score article)
8013                             gnus-summary-default-score))))
8014       (setq data (cdr data)))
8015     (when article
8016       (gnus-summary-goto-subject article))
8017     (gnus-summary-position-point)
8018     article))
8019
8020 (defun gnus-summary-last-subject ()
8021   "Go to the last displayed subject line in the group."
8022   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8023     (when article
8024       (gnus-summary-goto-subject article))))
8025
8026 (defun gnus-summary-goto-article (article &optional all-headers force)
8027   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8028 If ALL-HEADERS is non-nil, no header lines are hidden.
8029 If FORCE, go to the article even if it isn't displayed.  If FORCE
8030 is a number, it is the line the article is to be displayed on."
8031   (interactive
8032    (list
8033     (gnus-completing-read
8034      "Article number or Message-ID"
8035      (mapcar 'int-to-string gnus-newsgroup-limit))
8036     current-prefix-arg
8037     t))
8038   (prog1
8039       (if (and (stringp article)
8040                (string-match "@\\|%40" article))
8041           (gnus-summary-refer-article article)
8042         (when (stringp article)
8043           (setq article (string-to-number article)))
8044         (if (gnus-summary-goto-subject article force)
8045             (gnus-summary-display-article article all-headers)
8046           (gnus-message 4 "Couldn't go to article %s" article) nil))
8047     (gnus-summary-position-point)))
8048
8049 (defun gnus-summary-goto-last-article ()
8050   "Go to the previously read article."
8051   (interactive)
8052   (prog1
8053       (when gnus-last-article
8054         (gnus-summary-goto-article gnus-last-article nil t))
8055     (gnus-summary-position-point)))
8056
8057 (defun gnus-summary-pop-article (number)
8058   "Pop one article off the history and go to the previous.
8059 NUMBER articles will be popped off."
8060   (interactive "p")
8061   (let (to)
8062     (setq gnus-newsgroup-history
8063           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8064     (if to
8065         (gnus-summary-goto-article (car to) nil t)
8066       (error "Article history empty")))
8067   (gnus-summary-position-point))
8068
8069 ;; Summary commands and functions for limiting the summary buffer.
8070
8071 (defun gnus-summary-limit-to-articles (n)
8072   "Limit the summary buffer to the next N articles.
8073 If not given a prefix, use the process marked articles instead."
8074   (interactive "P")
8075   (prog1
8076       (let ((articles (gnus-summary-work-articles n)))
8077         (setq gnus-newsgroup-processable nil)
8078         (gnus-summary-limit articles))
8079     (gnus-summary-position-point)))
8080
8081 (defun gnus-summary-pop-limit (&optional total)
8082   "Restore the previous limit.
8083 If given a prefix, remove all limits."
8084   (interactive "P")
8085   (when total
8086     (setq gnus-newsgroup-limits
8087           (list (mapcar (lambda (h) (mail-header-number h))
8088                         gnus-newsgroup-headers))))
8089   (unless gnus-newsgroup-limits
8090     (error "No limit to pop"))
8091   (prog1
8092       (gnus-summary-limit nil 'pop)
8093     (gnus-summary-position-point)))
8094
8095 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8096   "Limit the summary buffer to articles that have subjects that match a regexp.
8097 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8098   (interactive
8099    (list (read-string (if current-prefix-arg
8100                           "Exclude subject (regexp): "
8101                         "Limit to subject (regexp): "))
8102          nil current-prefix-arg))
8103   (unless header
8104     (setq header "subject"))
8105   (when (not (equal "" subject))
8106     (prog1
8107         (let ((articles (gnus-summary-find-matching
8108                          (or header "subject") subject 'all nil nil
8109                          not-matching)))
8110           (unless articles
8111             (error "Found no matches for \"%s\"" subject))
8112           (gnus-summary-limit articles))
8113       (gnus-summary-position-point))))
8114
8115 (defun gnus-summary-limit-to-author (from &optional not-matching)
8116   "Limit the summary buffer to articles that have authors that match a regexp.
8117 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8118   (interactive
8119    (list (read-string (if current-prefix-arg
8120                           "Exclude author (regexp): "
8121                         "Limit to author (regexp): "))
8122          current-prefix-arg))
8123   (gnus-summary-limit-to-subject from "from" not-matching))
8124
8125 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8126   "Limit the summary buffer to articles with the given RECIPIENT.
8127
8128 If NOT-MATCHING, exclude RECIPIENT.
8129
8130 To and Cc headers are checked.  You need to include them in
8131 `nnmail-extra-headers'."
8132   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8133   (interactive
8134    (list (read-string (format "%s recipient (regexp): "
8135                               (if current-prefix-arg "Exclude" "Limit to")))
8136          current-prefix-arg))
8137   (when (not (equal "" recipient))
8138     (prog1 (let* ((to
8139                    (if (memq 'To nnmail-extra-headers)
8140                        (gnus-summary-find-matching
8141                         (cons 'extra 'To) recipient 'all nil nil
8142                         not-matching)
8143                      (gnus-message
8144                       1 "`To' isn't present in `nnmail-extra-headers'")
8145                      (sit-for 1)
8146                      nil))
8147                   (cc
8148                    (if (memq 'Cc nnmail-extra-headers)
8149                        (gnus-summary-find-matching
8150                         (cons 'extra 'Cc) recipient 'all nil nil
8151                         not-matching)
8152                      (gnus-message
8153                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8154                      (sit-for 1)
8155                      nil))
8156                   (articles
8157                    (if not-matching
8158                        ;; We need the numbers that are in both lists:
8159                        (mapcar (lambda (a)
8160                                  (and (memq a to) a))
8161                                cc)
8162                      (nconc to cc))))
8163              (unless articles
8164                (error "Found no matches for \"%s\"" recipient))
8165              (gnus-summary-limit articles))
8166       (gnus-summary-position-point))))
8167
8168 (defun gnus-summary-limit-to-address (address &optional not-matching)
8169   "Limit the summary buffer to articles with the given ADDRESS.
8170
8171 If NOT-MATCHING, exclude ADDRESS.
8172
8173 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8174 in `nnmail-extra-headers'."
8175   (interactive
8176    (list (read-string (format "%s address (regexp): "
8177                               (if current-prefix-arg "Exclude" "Limit to")))
8178          current-prefix-arg))
8179   (when (not (equal "" address))
8180     (prog1 (let* ((to
8181                    (if (memq 'To nnmail-extra-headers)
8182                        (gnus-summary-find-matching
8183                         (cons 'extra 'To) address 'all nil nil
8184                         not-matching)
8185                      (gnus-message
8186                       1 "`To' isn't present in `nnmail-extra-headers'")
8187                      (sit-for 1)
8188                      t))
8189                   (cc
8190                    (if (memq 'Cc nnmail-extra-headers)
8191                        (gnus-summary-find-matching
8192                         (cons 'extra 'Cc) address 'all nil nil
8193                         not-matching)
8194                      (gnus-message
8195                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8196                      (sit-for 1)
8197                      t))
8198                   (from
8199                    (gnus-summary-find-matching "from" address
8200                                                'all nil nil not-matching))
8201                   (articles
8202                    (if not-matching
8203                        ;; We need the numbers that are in all lists:
8204                        (if (eq cc t)
8205                            (if (eq to t)
8206                                from
8207                              (mapcar (lambda (a) (car (memq a from))) to))
8208                          (if (eq to t)
8209                              (mapcar (lambda (a) (car (memq a from))) cc)
8210                            (mapcar (lambda (a) (car (memq a from)))
8211                                    (mapcar (lambda (a) (car (memq a to)))
8212                                            cc))))
8213                      (nconc (if (eq to t) nil to)
8214                             (if (eq cc t) nil cc)
8215                             from))))
8216              (unless articles
8217                (error "Found no matches for \"%s\"" address))
8218              (gnus-summary-limit articles))
8219       (gnus-summary-position-point))))
8220
8221 (defun gnus-summary-limit-strange-charsets-predicate (header)
8222   (when (fboundp 'char-charset)
8223     (let ((string (concat (mail-header-subject header)
8224                           (mail-header-from header)))
8225           charset found)
8226       (dotimes (i (1- (length string)))
8227         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8228         (when (string-match "unicode\\|big\\|japanese" charset)
8229           (setq found t)))
8230       found)))
8231
8232 (defun gnus-summary-limit-to-predicate (predicate)
8233   "Limit to articles where PREDICATE returns non-nil.
8234 PREDICATE will be called with the header structures of the
8235 articles."
8236   (let ((articles nil)
8237         (case-fold-search t))
8238     (dolist (header gnus-newsgroup-headers)
8239       (when (funcall predicate header)
8240         (push (mail-header-number header) articles)))
8241     (gnus-summary-limit (nreverse articles))))
8242
8243 (defun gnus-summary-limit-to-age (age &optional younger-p)
8244   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8245 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8246 articles that are younger than AGE days."
8247   (interactive
8248    (let ((younger current-prefix-arg)
8249          (days-got nil)
8250          days)
8251      (while (not days-got)
8252        (setq days (if younger
8253                       (read-string "Limit to articles younger than (in days, older when negative): ")
8254                     (read-string
8255                      "Limit to articles older than (in days, younger when negative): ")))
8256        (when (> (length days) 0)
8257          (setq days (read days)))
8258        (if (numberp days)
8259            (progn
8260              (setq days-got t)
8261              (when (< days 0)
8262                (setq younger (not younger))
8263                (setq days (* days -1))))
8264          (message "Please enter a number.")
8265          (sleep-for 1)))
8266      (list days younger)))
8267   (prog1
8268       (let ((data gnus-newsgroup-data)
8269             (cutoff (days-to-time age))
8270             articles d date is-younger)
8271         (while (setq d (pop data))
8272           (when (and (vectorp (gnus-data-header d))
8273                      (setq date (mail-header-date (gnus-data-header d))))
8274             (setq is-younger (time-less-p
8275                               (time-since (gnus-date-get-time date))
8276                               cutoff))
8277             (when (if younger-p
8278                       is-younger
8279                     (not is-younger))
8280               (push (gnus-data-number d) articles))))
8281         (gnus-summary-limit (nreverse articles)))
8282     (gnus-summary-position-point)))
8283
8284 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8285   "Limit the summary buffer to articles that match an 'extra' header."
8286   (interactive
8287    (let ((header
8288           (intern
8289            (gnus-completing-read
8290             (if current-prefix-arg
8291                 "Exclude extra header"
8292               "Limit extra header")
8293             (mapcar 'symbol-name gnus-extra-headers)
8294             t nil nil
8295             (symbol-name (car gnus-extra-headers))))))
8296      (list header
8297            (read-string (format "%s header %s (regexp): "
8298                                 (if current-prefix-arg "Exclude" "Limit to")
8299                                 header))
8300            current-prefix-arg)))
8301   (when (not (equal "" regexp))
8302     (prog1
8303         (let ((articles (gnus-summary-find-matching
8304                          (cons 'extra header) regexp 'all nil nil
8305                          not-matching)))
8306           (unless articles
8307             (error "Found no matches for \"%s\"" regexp))
8308           (gnus-summary-limit articles))
8309       (gnus-summary-position-point))))
8310
8311 (defun gnus-summary-limit-to-display-predicate ()
8312   "Limit the summary buffer to the predicated in the `display' group parameter."
8313   (interactive)
8314   (unless gnus-newsgroup-display
8315     (error "There is no `display' group parameter"))
8316   (let (articles)
8317     (dolist (gnus-number gnus-newsgroup-articles)
8318       (when (funcall gnus-newsgroup-display)
8319         (push gnus-number articles)))
8320     (gnus-summary-limit articles))
8321   (gnus-summary-position-point))
8322
8323 (defun gnus-summary-limit-to-unread (&optional all)
8324   "Limit the summary buffer to articles that are not marked as read.
8325 If ALL is non-nil, limit strictly to unread articles."
8326   (interactive "P")
8327   (if all
8328       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8329     (gnus-summary-limit-to-marks
8330      ;; Concat all the marks that say that an article is read and have
8331      ;; those removed.
8332      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8333            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8334            gnus-low-score-mark gnus-expirable-mark
8335            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8336            gnus-duplicate-mark)
8337      'reverse)))
8338
8339 (defun gnus-summary-limit-to-headers (match &optional reverse)
8340   "Limit the summary buffer to articles that have headers that match MATCH.
8341 If REVERSE (the prefix), limit to articles that don't match."
8342   (interactive "sMatch headers (regexp): \nP")
8343   (gnus-summary-limit-to-bodies match reverse t))
8344
8345 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8346   "Limit the summary buffer to articles that have bodies that match MATCH.
8347 If REVERSE (the prefix), limit to articles that don't match."
8348   (interactive "sMatch body (regexp): \nP")
8349   (let ((articles nil)
8350         (gnus-select-article-hook nil)  ;Disable hook.
8351         (gnus-article-prepare-hook nil)
8352         (gnus-use-article-prefetch nil)
8353         (gnus-keep-backlog nil)
8354         (gnus-break-pages nil)
8355         (gnus-summary-display-arrow nil)
8356         (gnus-updated-mode-lines nil)
8357         (gnus-auto-center-summary nil)
8358         (gnus-display-mime-function nil))
8359     (dolist (data gnus-newsgroup-data)
8360       (let (gnus-mark-article-hook)
8361         (gnus-summary-select-article t t nil (gnus-data-number data)))
8362       (with-current-buffer gnus-article-buffer
8363         (article-goto-body)
8364         (let* ((case-fold-search t)
8365                (found (if headersp
8366                           (re-search-backward match nil t)
8367                         (re-search-forward match nil t))))
8368           (when (or (and found
8369                          (not reverse))
8370                     (and (not found)
8371                          reverse))
8372             (push (gnus-data-number data) articles)))))
8373     (if (not articles)
8374         (message "No messages matched")
8375       (gnus-summary-limit articles)))
8376   (gnus-summary-position-point))
8377
8378 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8379   "Limit the summary buffer to articles that aren't part on any thread.
8380 If THREADSP (the prefix), limit to articles that are in threads."
8381   (interactive "P")
8382   (let ((articles nil)
8383         thread-articles
8384         threads)
8385     (dolist (thread gnus-newsgroup-threads)
8386       (if (stringp (car thread))
8387           (dolist (thread (cdr thread))
8388             (push thread threads))
8389         (push thread threads)))
8390     (dolist (thread threads)
8391       (setq thread-articles (gnus-articles-in-thread thread))
8392       (when (or (and threadsp
8393                      (> (length thread-articles) 1))
8394                 (and (not threadsp)
8395                      (= (length thread-articles) 1)))
8396         (setq articles (nconc thread-articles articles))))
8397     (if (not articles)
8398         (message "No messages matched")
8399       (gnus-summary-limit articles))
8400     (gnus-summary-position-point)))
8401
8402 (defun gnus-summary-limit-to-replied (&optional unreplied)
8403   "Limit the summary buffer to replied articles.
8404 If UNREPLIED (the prefix), limit to unreplied articles."
8405   (interactive "P")
8406   (if unreplied
8407       (gnus-summary-limit
8408        (gnus-set-difference gnus-newsgroup-articles
8409         gnus-newsgroup-replied))
8410     (gnus-summary-limit gnus-newsgroup-replied))
8411   (gnus-summary-position-point))
8412
8413 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8414   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8415 If REVERSE, limit the summary buffer to articles that are marked
8416 with MARKS.  MARKS can either be a string of marks or a list of marks.
8417 Returns how many articles were removed."
8418   (interactive "sMarks: ")
8419   (gnus-summary-limit-to-marks marks t))
8420
8421 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8422   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8423 If REVERSE (the prefix), limit the summary buffer to articles that are
8424 not marked with MARKS.  MARKS can either be a string of marks or a
8425 list of marks.
8426 Returns how many articles were removed."
8427   (interactive "sMarks: \nP")
8428   (prog1
8429       (let ((data gnus-newsgroup-data)
8430             (marks (if (listp marks) marks
8431                      (append marks nil))) ; Transform to list.
8432             articles)
8433         (while data
8434           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8435                   (memq (gnus-data-mark (car data)) marks))
8436             (push (gnus-data-number (car data)) articles))
8437           (setq data (cdr data)))
8438         (gnus-summary-limit articles))
8439     (gnus-summary-position-point)))
8440
8441 (defun gnus-summary-limit-to-score (score)
8442   "Limit to articles with score at or above SCORE."
8443   (interactive "NLimit to articles with score of at least: ")
8444   (let ((data gnus-newsgroup-data)
8445         articles)
8446     (while data
8447       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8448                 score)
8449         (push (gnus-data-number (car data)) articles))
8450       (setq data (cdr data)))
8451     (prog1
8452         (gnus-summary-limit articles)
8453       (gnus-summary-position-point))))
8454
8455 (defun gnus-summary-limit-to-unseen ()
8456   "Limit to unseen articles."
8457   (interactive)
8458   (prog1
8459       (gnus-summary-limit gnus-newsgroup-unseen)
8460     (gnus-summary-position-point)))
8461
8462 (defun gnus-summary-limit-include-thread (id)
8463   "Display all the hidden articles that is in the thread with ID in it.
8464 When called interactively, ID is the Message-ID of the current
8465 article."
8466   (interactive (list (mail-header-id (gnus-summary-article-header))))
8467   (let ((articles (gnus-articles-in-thread
8468                    (gnus-id-to-thread (gnus-root-id id))))
8469         ;;we REALLY want the whole thread---this prevents cut-threads
8470         ;;from removing the thread we want to include.
8471         (gnus-fetch-old-headers nil)
8472         (gnus-build-sparse-threads nil))
8473     (prog1
8474         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8475       (gnus-summary-limit-include-matching-articles
8476        "subject"
8477        (regexp-quote (gnus-simplify-subject-re
8478                       (mail-header-subject (gnus-id-to-header id)))))
8479       (gnus-summary-position-point))))
8480
8481 (defun gnus-summary-limit-include-matching-articles (header regexp)
8482   "Display all the hidden articles that have HEADERs that match REGEXP."
8483   (interactive (list (read-string "Match on header: ")
8484                      (read-string "Regexp: ")))
8485   (let ((articles (gnus-find-matching-articles header regexp)))
8486     (prog1
8487         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8488       (gnus-summary-position-point))))
8489
8490 (defun gnus-summary-insert-dormant-articles ()
8491   "Insert all the dormant articles for this group into the current buffer."
8492   (interactive)
8493   (let ((gnus-verbose (max 6 gnus-verbose)))
8494     (if (not gnus-newsgroup-dormant)
8495         (gnus-message 3 "No dormant articles for this group")
8496       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8497
8498 (defun gnus-summary-insert-ticked-articles ()
8499   "Insert ticked articles for this group into the current buffer."
8500   (interactive)
8501   (let ((gnus-verbose (max 6 gnus-verbose)))
8502     (if (not gnus-newsgroup-marked)
8503         (gnus-message 3 "No ticked articles for this group")
8504       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8505
8506 (defun gnus-summary-limit-include-dormant ()
8507   "Display all the hidden articles that are marked as dormant.
8508 Note that this command only works on a subset of the articles currently
8509 fetched for this group."
8510   (interactive)
8511   (unless gnus-newsgroup-dormant
8512     (error "There are no dormant articles in this group"))
8513   (prog1
8514       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8515     (gnus-summary-position-point)))
8516
8517 (defun gnus-summary-include-articles (articles)
8518   "Fetch the headers for ARTICLES and then display the summary lines."
8519   (let ((gnus-inhibit-demon t)
8520         (gnus-agent nil)
8521         (gnus-read-all-available-headers t))
8522     (setq gnus-newsgroup-headers
8523           (gnus-merge
8524            'list gnus-newsgroup-headers
8525            (gnus-fetch-headers articles nil t)
8526            'gnus-article-sort-by-number))
8527     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8528
8529 (defun gnus-summary-limit-exclude-dormant ()
8530   "Hide all dormant articles."
8531   (interactive)
8532   (prog1
8533       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8534     (gnus-summary-position-point)))
8535
8536 (defun gnus-summary-limit-exclude-childless-dormant ()
8537   "Hide all dormant articles that have no children."
8538   (interactive)
8539   (let ((data (gnus-data-list t))
8540         articles d children)
8541     ;; Find all articles that are either not dormant or have
8542     ;; children.
8543     (while (setq d (pop data))
8544       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8545                 (and (setq children
8546                            (gnus-article-children (gnus-data-number d)))
8547                      (let (found)
8548                        (while children
8549                          (when (memq (car children) articles)
8550                            (setq children nil
8551                                  found t))
8552                          (pop children))
8553                        found)))
8554         (push (gnus-data-number d) articles)))
8555     ;; Do the limiting.
8556     (prog1
8557         (gnus-summary-limit articles)
8558       (gnus-summary-position-point))))
8559
8560 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8561   "Mark all unread excluded articles as read.
8562 If ALL, mark even excluded ticked and dormants as read."
8563   (interactive "P")
8564   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8565   (let ((articles (gnus-sorted-ndifference
8566                    (sort
8567                     (mapcar (lambda (h) (mail-header-number h))
8568                             gnus-newsgroup-headers)
8569                     '<)
8570                    gnus-newsgroup-limit))
8571         article)
8572     (setq gnus-newsgroup-unreads
8573           (gnus-sorted-intersection gnus-newsgroup-unreads
8574                                     gnus-newsgroup-limit))
8575     (if all
8576         (setq gnus-newsgroup-dormant nil
8577               gnus-newsgroup-marked nil
8578               gnus-newsgroup-reads
8579               (nconc
8580                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8581                gnus-newsgroup-reads))
8582       (while (setq article (pop articles))
8583         (unless (or (memq article gnus-newsgroup-dormant)
8584                     (memq article gnus-newsgroup-marked))
8585           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8586
8587 (defun gnus-summary-limit (articles &optional pop)
8588   (if pop
8589       ;; We pop the previous limit off the stack and use that.
8590       (setq articles (car gnus-newsgroup-limits)
8591             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8592     ;; We use the new limit, so we push the old limit on the stack.
8593     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8594   ;; Set the limit.
8595   (setq gnus-newsgroup-limit articles)
8596   (let ((total (length gnus-newsgroup-data))
8597         (data (gnus-data-find-list (gnus-summary-article-number)))
8598         (gnus-summary-mark-below nil)   ; Inhibit this.
8599         found)
8600     ;; This will do all the work of generating the new summary buffer
8601     ;; according to the new limit.
8602     (gnus-summary-prepare)
8603     ;; Hide any threads, possibly.
8604     (gnus-summary-maybe-hide-threads)
8605     ;; Try to return to the article you were at, or one in the
8606     ;; neighborhood.
8607     (when data
8608       ;; We try to find some article after the current one.
8609       (while data
8610         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8611           (setq data nil
8612                 found t))
8613         (setq data (cdr data))))
8614     (unless found
8615       ;; If there is no data, that means that we were after the last
8616       ;; article.  The same goes when we can't find any articles
8617       ;; after the current one.
8618       (goto-char (point-max))
8619       (gnus-summary-find-prev))
8620     (gnus-set-mode-line 'summary)
8621     ;; We return how many articles were removed from the summary
8622     ;; buffer as a result of the new limit.
8623     (- total (length gnus-newsgroup-data))))
8624
8625 (defsubst gnus-invisible-cut-children (threads)
8626   (let ((num 0))
8627     (while threads
8628       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8629         (incf num))
8630       (pop threads))
8631     (< num 2)))
8632
8633 (defsubst gnus-cut-thread (thread)
8634   "Go forwards in the thread until we find an article that we want to display."
8635   (when (or (eq gnus-fetch-old-headers 'some)
8636             (eq gnus-fetch-old-headers 'invisible)
8637             (numberp gnus-fetch-old-headers)
8638             (eq gnus-build-sparse-threads 'some)
8639             (eq gnus-build-sparse-threads 'more))
8640     ;; Deal with old-fetched headers and sparse threads.
8641     (while (and
8642             thread
8643             (or
8644              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8645              (gnus-summary-article-ancient-p
8646               (mail-header-number (car thread))))
8647             (if (or (<= (length (cdr thread)) 1)
8648                     (eq gnus-fetch-old-headers 'invisible))
8649                 (setq gnus-newsgroup-limit
8650                       (delq (mail-header-number (car thread))
8651                             gnus-newsgroup-limit)
8652                       thread (cadr thread))
8653               (when (gnus-invisible-cut-children (cdr thread))
8654                 (let ((th (cdr thread)))
8655                   (while th
8656                     (if (memq (mail-header-number (caar th))
8657                               gnus-newsgroup-limit)
8658                         (setq thread (car th)
8659                               th nil)
8660                       (setq th (cdr th))))))))))
8661   thread)
8662
8663 (defun gnus-cut-threads (threads)
8664   "Cut off all uninteresting articles from the beginning of THREADS."
8665   (when (or (eq gnus-fetch-old-headers 'some)
8666             (eq gnus-fetch-old-headers 'invisible)
8667             (numberp gnus-fetch-old-headers)
8668             (eq gnus-build-sparse-threads 'some)
8669             (eq gnus-build-sparse-threads 'more))
8670     (let ((th threads))
8671       (while th
8672         (setcar th (gnus-cut-thread (car th)))
8673         (setq th (cdr th)))))
8674   ;; Remove nixed out threads.
8675   (delq nil threads))
8676
8677 (defun gnus-summary-initial-limit (&optional show-if-empty)
8678   "Figure out what the initial limit is supposed to be on group entry.
8679 This entails weeding out unwanted dormants, low-scored articles,
8680 fetch-old-headers verbiage, and so on."
8681   ;; Most groups have nothing to remove.
8682   (unless (or gnus-inhibit-limiting
8683               (and (null gnus-newsgroup-dormant)
8684                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8685                    (not (eq gnus-fetch-old-headers 'some))
8686                    (not (numberp gnus-fetch-old-headers))
8687                    (not (eq gnus-fetch-old-headers 'invisible))
8688                    (null gnus-summary-expunge-below)
8689                    (not (eq gnus-build-sparse-threads 'some))
8690                    (not (eq gnus-build-sparse-threads 'more))
8691                    (null gnus-thread-expunge-below)))
8692     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8693     (setq gnus-newsgroup-limit nil)
8694     (mapatoms
8695      (lambda (node)
8696        (unless (car (symbol-value node))
8697          ;; These threads have no parents -- they are roots.
8698          (let ((nodes (cdr (symbol-value node)))
8699                thread)
8700            (while nodes
8701              (if (and gnus-thread-expunge-below
8702                       (< (gnus-thread-total-score (car nodes))
8703                          gnus-thread-expunge-below))
8704                  (gnus-expunge-thread (pop nodes))
8705                (setq thread (pop nodes))
8706                (gnus-summary-limit-children thread))))))
8707      gnus-newsgroup-dependencies)
8708     ;; If this limitation resulted in an empty group, we might
8709     ;; pop the previous limit and use it instead.
8710     (when (and (not gnus-newsgroup-limit)
8711                show-if-empty)
8712       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8713     gnus-newsgroup-limit))
8714
8715 (defun gnus-summary-limit-children (thread)
8716   "Return 1 if this subthread is visible and 0 if it is not."
8717   ;; First we get the number of visible children to this thread.  This
8718   ;; is done by recursing down the thread using this function, so this
8719   ;; will really go down to a leaf article first, before slowly
8720   ;; working its way up towards the root.
8721   (when thread
8722     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8723            (children
8724            (if (cdr thread)
8725                (apply '+ (mapcar 'gnus-summary-limit-children
8726                                  (cdr thread)))
8727              0))
8728            (number (mail-header-number (car thread)))
8729            score)
8730       (if (and
8731            (not (memq number gnus-newsgroup-marked))
8732            (or
8733             ;; If this article is dormant and has absolutely no visible
8734             ;; children, then this article isn't visible.
8735             (and (memq number gnus-newsgroup-dormant)
8736                  (zerop children))
8737             ;; If this is "fetch-old-headered" and there is no
8738             ;; visible children, then we don't want this article.
8739             (and (or (eq gnus-fetch-old-headers 'some)
8740                      (numberp gnus-fetch-old-headers))
8741                  (gnus-summary-article-ancient-p number)
8742                  (zerop children))
8743             ;; If this is "fetch-old-headered" and `invisible', then
8744             ;; we don't want this article.
8745             (and (eq gnus-fetch-old-headers 'invisible)
8746                  (gnus-summary-article-ancient-p number))
8747             ;; If this is a sparsely inserted article with no children,
8748             ;; we don't want it.
8749             (and (eq gnus-build-sparse-threads 'some)
8750                  (gnus-summary-article-sparse-p number)
8751                  (zerop children))
8752             ;; If we use expunging, and this article is really
8753             ;; low-scored, then we don't want this article.
8754             (when (and gnus-summary-expunge-below
8755                        (< (setq score
8756                                 (or (cdr (assq number gnus-newsgroup-scored))
8757                                     gnus-summary-default-score))
8758                           gnus-summary-expunge-below))
8759               ;; We increase the expunge-tally here, but that has
8760               ;; nothing to do with the limits, really.
8761               (incf gnus-newsgroup-expunged-tally)
8762               ;; We also mark as read here, if that's wanted.
8763               (when (and gnus-summary-mark-below
8764                          (< score gnus-summary-mark-below))
8765                 (setq gnus-newsgroup-unreads
8766                       (delq number gnus-newsgroup-unreads))
8767                 (if gnus-newsgroup-auto-expire
8768                     (push number gnus-newsgroup-expirable)
8769                   (push (cons number gnus-low-score-mark)
8770                         gnus-newsgroup-reads)))
8771               t)
8772             ;; Do the `display' group parameter.
8773             (and gnus-newsgroup-display
8774                  (let ((gnus-number number))
8775                    (not (funcall gnus-newsgroup-display))))))
8776           ;; Nope, invisible article.
8777           0
8778         ;; Ok, this article is to be visible, so we add it to the limit
8779         ;; and return 1.
8780         (push number gnus-newsgroup-limit)
8781         1))))
8782
8783 (defun gnus-expunge-thread (thread)
8784   "Mark all articles in THREAD as read."
8785   (let* ((number (mail-header-number (car thread))))
8786     (incf gnus-newsgroup-expunged-tally)
8787     ;; We also mark as read here, if that's wanted.
8788     (setq gnus-newsgroup-unreads
8789           (delq number gnus-newsgroup-unreads))
8790     (if gnus-newsgroup-auto-expire
8791         (push number gnus-newsgroup-expirable)
8792       (push (cons number gnus-low-score-mark)
8793             gnus-newsgroup-reads)))
8794   ;; Go recursively through all subthreads.
8795   (mapcar 'gnus-expunge-thread (cdr thread)))
8796
8797 ;; Summary article oriented commands
8798
8799 (defun gnus-summary-refer-parent-article (n)
8800   "Refer parent article N times.
8801 If N is negative, go to ancestor -N instead.
8802 The difference between N and the number of articles fetched is returned."
8803   (interactive "p")
8804   (let ((skip 1)
8805         error header ref)
8806     (when (not (natnump n))
8807       (setq skip (abs n)
8808             n 1))
8809     (while (and (> n 0)
8810                 (not error))
8811       (setq header (gnus-summary-article-header))
8812       (if (and (eq (mail-header-number header)
8813                    (cdr gnus-article-current))
8814                (equal gnus-newsgroup-name
8815                       (car gnus-article-current)))
8816           ;; If we try to find the parent of the currently
8817           ;; displayed article, then we take a look at the actual
8818           ;; References header, since this is slightly more
8819           ;; reliable than the References field we got from the
8820           ;; server.
8821           (with-current-buffer gnus-original-article-buffer
8822             (nnheader-narrow-to-headers)
8823             (unless (setq ref (message-fetch-field "references"))
8824               (when (setq ref (message-fetch-field "in-reply-to"))
8825                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8826             (widen))
8827         (setq ref
8828               ;; It's not the current article, so we take a bet on
8829               ;; the value we got from the server.
8830               (mail-header-references header)))
8831       (if (and ref
8832                (not (equal ref "")))
8833           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8834             (gnus-message 1 "Couldn't find parent"))
8835         (gnus-message 1 "No references in article %d"
8836                       (gnus-summary-article-number))
8837         (setq error t))
8838       (decf n))
8839     (gnus-summary-position-point)
8840     n))
8841
8842 (defun gnus-summary-refer-references ()
8843   "Fetch all articles mentioned in the References header.
8844 Return the number of articles fetched."
8845   (interactive)
8846   (let ((ref (mail-header-references (gnus-summary-article-header)))
8847         (current (gnus-summary-article-number))
8848         (n 0))
8849     (if (or (not ref)
8850             (equal ref ""))
8851         (error "No References in the current article")
8852       ;; For each Message-ID in the References header...
8853       (while (string-match "<[^>]*>" ref)
8854         (incf n)
8855         ;; ... fetch that article.
8856         (gnus-summary-refer-article
8857          (prog1 (match-string 0 ref)
8858            (setq ref (substring ref (match-end 0))))))
8859       (gnus-summary-goto-subject current)
8860       (gnus-summary-position-point)
8861       n)))
8862
8863 (defun gnus-summary-refer-thread (&optional limit)
8864   "Fetch all articles in the current thread.
8865 If no backend-specific 'request-thread function is available
8866 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8867 fetch what's specified by the `gnus-refer-thread-limit'
8868 variable."
8869   (interactive "P")
8870   (gnus-warp-to-article)
8871   (let ((id (mail-header-id (gnus-summary-article-header)))
8872         (gnus-inhibit-demon t)
8873         (gnus-agent nil)
8874         (gnus-summary-ignore-duplicates t)
8875         (gnus-read-all-available-headers t)
8876         (limit (if limit (prefix-numeric-value limit)
8877                  gnus-refer-thread-limit)))
8878     (setq gnus-newsgroup-headers
8879           (gnus-merge
8880            'list gnus-newsgroup-headers
8881            (if (gnus-check-backend-function
8882                 'request-thread gnus-newsgroup-name)
8883                (gnus-request-thread id)
8884              (let* ((last (if (numberp limit)
8885                               (min (+ (mail-header-number
8886                                        (gnus-summary-article-header))
8887                                       limit)
8888                                    gnus-newsgroup-highest)
8889                             gnus-newsgroup-highest))
8890                     (subject (gnus-simplify-subject
8891                               (mail-header-subject
8892                                (gnus-summary-article-header))))
8893                     (refs (split-string (or (mail-header-references
8894                                              (gnus-summary-article-header))
8895                                             "")))
8896                     (gnus-parse-headers-hook
8897                      (lambda () (goto-char (point-min))
8898                        (keep-lines
8899                         (regexp-opt (append refs (list id subject)))))))
8900                (gnus-fetch-headers (list last) (if (numberp limit)
8901                                                    (* 2 limit) limit) t)))
8902            'gnus-article-sort-by-number))
8903     (gnus-summary-limit-include-thread id)))
8904
8905 (defun gnus-summary-refer-article (message-id)
8906   "Fetch an article specified by MESSAGE-ID."
8907   (interactive "sMessage-ID: ")
8908   (when (and (stringp message-id)
8909              (not (zerop (length message-id))))
8910     (setq message-id (gnus-replace-in-string message-id " " ""))
8911     ;; Construct the correct Message-ID if necessary.
8912     ;; Suggested by tale@pawl.rpi.edu.
8913     (unless (string-match "^<" message-id)
8914       (setq message-id (concat "<" message-id)))
8915     (unless (string-match ">$" message-id)
8916       (setq message-id (concat message-id ">")))
8917     ;; People often post MIDs from URLs, so unhex it:
8918     (unless (string-match "@" message-id)
8919       (setq message-id (gnus-url-unhex-string message-id)))
8920     (let* ((header (gnus-id-to-header message-id))
8921            (sparse (and header
8922                         (gnus-summary-article-sparse-p
8923                          (mail-header-number header))
8924                         (memq (mail-header-number header)
8925                               gnus-newsgroup-limit)))
8926            number)
8927       (cond
8928        ;; If the article is present in the buffer we just go to it.
8929        ((and header
8930              (or (not (gnus-summary-article-sparse-p
8931                        (mail-header-number header)))
8932                  sparse))
8933         (prog1
8934             (gnus-summary-goto-article
8935              (mail-header-number header) nil t)
8936           (when sparse
8937             (gnus-summary-update-article (mail-header-number header)))))
8938        (t
8939         ;; We fetch the article.
8940         (catch 'found
8941           (dolist (gnus-override-method (gnus-refer-article-methods))
8942             (when (and (gnus-check-server gnus-override-method)
8943                        ;; Fetch the header,
8944                        (setq number (gnus-summary-insert-subject message-id)))
8945               ;; and display the article.
8946               (gnus-summary-select-article nil nil nil number)
8947               (throw 'found t)))
8948           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8949
8950 (defun gnus-refer-article-methods ()
8951   "Return a list of referable methods."
8952   (cond
8953    ;; No method, so we default to current and native.
8954    ((null gnus-refer-article-method)
8955     (list gnus-current-select-method gnus-select-method))
8956    ;; Current.
8957    ((eq 'current gnus-refer-article-method)
8958     (list gnus-current-select-method))
8959    ;; List of select methods.
8960    ((not (and (symbolp (car gnus-refer-article-method))
8961               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8962     (let (out)
8963       (dolist (method gnus-refer-article-method)
8964         (push (if (eq 'current method)
8965                   gnus-current-select-method
8966                 method)
8967               out))
8968       (nreverse out)))
8969    ;; One single select method.
8970    (t
8971     (list gnus-refer-article-method))))
8972
8973 (defun gnus-summary-edit-parameters ()
8974   "Edit the group parameters of the current group."
8975   (interactive)
8976   (gnus-group-edit-group gnus-newsgroup-name 'params))
8977
8978 (defun gnus-summary-customize-parameters ()
8979   "Customize the group parameters of the current group."
8980   (interactive)
8981   (gnus-group-customize gnus-newsgroup-name))
8982
8983 (defun gnus-summary-enter-digest-group (&optional force)
8984   "Enter an nndoc group based on the current article.
8985 If FORCE, force a digest interpretation.  If not, try
8986 to guess what the document format is."
8987   (interactive "P")
8988   (let ((conf gnus-current-window-configuration))
8989     (save-window-excursion
8990       (save-excursion
8991         (let (gnus-article-prepare-hook
8992               gnus-display-mime-function
8993               gnus-break-pages)
8994           (gnus-summary-select-article))))
8995     (setq gnus-current-window-configuration conf)
8996     (let* ((name (format "%s-%d"
8997                          (gnus-group-prefixed-name
8998                           gnus-newsgroup-name (list 'nndoc ""))
8999                          (with-current-buffer gnus-summary-buffer
9000                            gnus-current-article)))
9001            (ogroup gnus-newsgroup-name)
9002            (params (append (gnus-info-params (gnus-get-info ogroup))
9003                            (list (cons 'to-group ogroup))
9004                            (list (cons 'parent-group ogroup))
9005                            (list (cons 'save-article-group ogroup))))
9006            (case-fold-search t)
9007            (buf (current-buffer))
9008            dig to-address)
9009       (with-current-buffer gnus-original-article-buffer
9010         ;; Have the digest group inherit the main mail address of
9011         ;; the parent article.
9012         (when (setq to-address (or (gnus-fetch-field "reply-to")
9013                                    (gnus-fetch-field "from")))
9014           (setq params
9015                 (append
9016                  (list (cons 'to-address
9017                              (funcall gnus-decode-encoded-address-function
9018                                       to-address))))))
9019         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9020         (insert-buffer-substring gnus-original-article-buffer)
9021         ;; Remove lines that may lead nndoc to misinterpret the
9022         ;; document type.
9023         (narrow-to-region
9024          (goto-char (point-min))
9025          (or (search-forward "\n\n" nil t) (point)))
9026         (goto-char (point-min))
9027         (delete-matching-lines "^Path:\\|^From ")
9028         (widen))
9029       (unwind-protect
9030           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9031                     (gnus-newsgroup-ephemeral-ignored-charsets
9032                      gnus-newsgroup-ignored-charsets))
9033                 (gnus-group-read-ephemeral-group
9034                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9035                               (nndoc-article-type
9036                                ,(if force 'mbox 'guess)))
9037                  t nil nil nil
9038                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9039                                                         "ADAPT")))))
9040               ;; Make all postings to this group go to the parent group.
9041               (nconc (gnus-info-params (gnus-get-info name))
9042                      params)
9043             ;; Couldn't select this doc group.
9044             (switch-to-buffer buf)
9045             (gnus-set-global-variables)
9046             (gnus-configure-windows 'summary)
9047             (gnus-message 3 "Article couldn't be entered?"))
9048         (kill-buffer dig)))))
9049
9050 (defun gnus-summary-read-document (n)
9051   "Open a new group based on the current article(s).
9052 This will allow you to read digests and other similar
9053 documents as newsgroups.
9054 Obeys the standard process/prefix convention."
9055   (interactive "P")
9056   (let* ((ogroup gnus-newsgroup-name)
9057          (params (append (gnus-info-params (gnus-get-info ogroup))
9058                          (list (cons 'to-group ogroup))))
9059          group egroup groups vgroup)
9060     (dolist (article (gnus-summary-work-articles n))
9061       (setq group (format "%s-%d" gnus-newsgroup-name article))
9062       (gnus-summary-remove-process-mark article)
9063       (when (gnus-summary-display-article article)
9064         (save-excursion ;;What for?
9065           (with-temp-buffer
9066             (insert-buffer-substring gnus-original-article-buffer)
9067             ;; Remove some headers that may lead nndoc to make
9068             ;; the wrong guess.
9069             (message-narrow-to-head)
9070             (goto-char (point-min))
9071             (delete-matching-lines "^Path:\\|^From ")
9072             (widen)
9073             (if (setq egroup
9074                       (gnus-group-read-ephemeral-group
9075                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9076                                      (nndoc-article-type guess))
9077                        t nil t))
9078                 (progn
9079                   ;; Make all postings to this group go to the parent group.
9080                   (nconc (gnus-info-params (gnus-get-info egroup))
9081                          params)
9082                   (push egroup groups))
9083               ;; Couldn't select this doc group.
9084               (gnus-error 3 "Article couldn't be entered"))))))
9085     ;; Now we have selected all the documents.
9086     (cond
9087      ((not groups)
9088       (error "None of the articles could be interpreted as documents"))
9089      ((gnus-group-read-ephemeral-group
9090        (setq vgroup (format
9091                      "nnvirtual:%s-%s" gnus-newsgroup-name
9092                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9093        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9094        t
9095        (cons (current-buffer) 'summary)))
9096      (t
9097       (error "Couldn't select virtual nndoc group")))))
9098
9099 (defun gnus-summary-widget-forward (arg)
9100   "Move point to the next field or button in the article.
9101 With optional ARG, move across that many fields."
9102   (interactive "p")
9103   (gnus-summary-select-article)
9104   (gnus-configure-windows 'article)
9105   (select-window (gnus-get-buffer-window gnus-article-buffer))
9106   (widget-forward arg))
9107
9108 (defun gnus-summary-isearch-article (&optional regexp-p)
9109   "Do incremental search forward on the current article.
9110 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9111   (interactive "P")
9112   (gnus-summary-select-article)
9113   (gnus-configure-windows 'article)
9114   (gnus-eval-in-buffer-window gnus-article-buffer
9115     (save-restriction
9116       (widen)
9117       (isearch-forward regexp-p))))
9118
9119 (defun gnus-summary-repeat-search-article-forward ()
9120   "Repeat the previous search forwards."
9121   (interactive)
9122   (unless gnus-last-search-regexp
9123     (error "No previous search"))
9124   (gnus-summary-search-article-forward gnus-last-search-regexp))
9125
9126 (defun gnus-summary-repeat-search-article-backward ()
9127   "Repeat the previous search backwards."
9128   (interactive)
9129   (unless gnus-last-search-regexp
9130     (error "No previous search"))
9131   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9132
9133 (defun gnus-summary-search-article-forward (regexp &optional backward)
9134   "Search for an article containing REGEXP forward.
9135 If BACKWARD, search backward instead."
9136   (interactive
9137    (list (read-string
9138           (format "Search article %s (regexp%s): "
9139                   (if current-prefix-arg "backward" "forward")
9140                   (if gnus-last-search-regexp
9141                       (concat ", default " gnus-last-search-regexp)
9142                     "")))
9143          current-prefix-arg))
9144   (if (string-equal regexp "")
9145       (setq regexp (or gnus-last-search-regexp ""))
9146     (setq gnus-last-search-regexp regexp)
9147     (setq gnus-article-before-search gnus-current-article))
9148   ;; Intentionally set gnus-last-article.
9149   (setq gnus-last-article gnus-article-before-search)
9150   (let ((gnus-last-article gnus-last-article))
9151     (if (gnus-summary-search-article regexp backward)
9152         (gnus-summary-show-thread)
9153       (signal 'search-failed (list regexp)))))
9154
9155 (defun gnus-summary-search-article-backward (regexp)
9156   "Search for an article containing REGEXP backward."
9157   (interactive
9158    (list (read-string
9159           (format "Search article backward (regexp%s): "
9160                   (if gnus-last-search-regexp
9161                       (concat ", default " gnus-last-search-regexp)
9162                     "")))))
9163   (gnus-summary-search-article-forward regexp 'backward))
9164
9165 (defun gnus-summary-search-article (regexp &optional backward)
9166   "Search for an article containing REGEXP.
9167 Optional argument BACKWARD means do search for backward.
9168 `gnus-select-article-hook' is not called during the search."
9169   ;; We have to require this here to make sure that the following
9170   ;; dynamic binding isn't shadowed by autoloading.
9171   (require 'gnus-async)
9172   (require 'gnus-art)
9173   (let ((gnus-select-article-hook nil)  ;Disable hook.
9174         (gnus-article-prepare-hook nil)
9175         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9176         (gnus-use-article-prefetch nil)
9177         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9178         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9179         (gnus-visual nil)
9180         (gnus-keep-backlog nil)
9181         (gnus-break-pages nil)
9182         (gnus-summary-display-arrow nil)
9183         (gnus-updated-mode-lines nil)
9184         (gnus-auto-center-summary nil)
9185         (sum (current-buffer))
9186         (gnus-display-mime-function nil)
9187         (found nil)
9188         point)
9189     (gnus-save-hidden-threads
9190       (gnus-summary-select-article)
9191       (set-buffer gnus-article-buffer)
9192       (goto-char (window-point (get-buffer-window (current-buffer))))
9193       (when backward
9194         (forward-line -1))
9195       (while (not found)
9196         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9197         (if (if backward
9198                 (re-search-backward regexp nil t)
9199               (re-search-forward regexp nil t))
9200             ;; We found the regexp.
9201             (progn
9202               (setq found 'found)
9203               (beginning-of-line)
9204               (set-window-start
9205                (get-buffer-window (current-buffer))
9206                (point))
9207               (forward-line 1)
9208               (set-window-point
9209                (get-buffer-window (current-buffer))
9210                (point))
9211               (set-buffer sum)
9212               (setq point (point)))
9213           ;; We didn't find it, so we go to the next article.
9214           (set-buffer sum)
9215           (setq found 'not)
9216           (while (eq found 'not)
9217             (if (not (if backward (gnus-summary-find-prev)
9218                        (gnus-summary-find-next)))
9219                 ;; No more articles.
9220                 (setq found t)
9221               ;; Select the next article and adjust point.
9222               (unless (gnus-summary-article-sparse-p
9223                        (gnus-summary-article-number))
9224                 (setq found nil)
9225                 (gnus-summary-select-article)
9226                 (set-buffer gnus-article-buffer)
9227                 (widen)
9228                 (goto-char (if backward (point-max) (point-min))))))))
9229       (gnus-message 7 ""))
9230     ;; Return whether we found the regexp.
9231     (when (eq found 'found)
9232       (goto-char point)
9233       (gnus-summary-show-thread)
9234       (gnus-summary-goto-subject gnus-current-article)
9235       (gnus-summary-position-point)
9236       t)))
9237
9238 (defun gnus-find-matching-articles (header regexp)
9239   "Return a list of all articles that match REGEXP on HEADER.
9240 This search includes all articles in the current group that Gnus has
9241 fetched headers for, whether they are displayed or not."
9242   (let ((articles nil)
9243         ;; Can't eta-reduce because it's a macro.
9244         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9245         (case-fold-search t))
9246     (dolist (header gnus-newsgroup-headers)
9247       (when (string-match regexp (funcall func header))
9248         (push (mail-header-number header) articles)))
9249     (nreverse articles)))
9250
9251 (defun gnus-summary-find-matching (header regexp &optional backward unread
9252                                           not-case-fold not-matching)
9253   "Return a list of all articles that match REGEXP on HEADER.
9254 The search stars on the current article and goes forwards unless
9255 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9256 If UNREAD is non-nil, only unread articles will
9257 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9258 in the comparisons. If NOT-MATCHING, return a list of all articles that
9259 not match REGEXP on HEADER."
9260   (let ((case-fold-search (not not-case-fold))
9261         articles d func)
9262     (if (consp header)
9263         (if (eq (car header) 'extra)
9264             (setq func
9265                   `(lambda (h)
9266                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9267                          "")))
9268           (error "%s is an invalid header" header))
9269       (unless (fboundp (intern (concat "mail-header-" header)))
9270         (error "%s is not a valid header" header))
9271       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9272     (dolist (d (if (eq backward 'all)
9273                    gnus-newsgroup-data
9274                  (gnus-data-find-list
9275                   (gnus-summary-article-number)
9276                   (gnus-data-list backward))))
9277       (when (and (or (not unread)       ; We want all articles...
9278                      (gnus-data-unread-p d)) ; Or just unreads.
9279                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9280                  (if not-matching
9281                      (not (string-match
9282                            regexp
9283                            (funcall func (gnus-data-header d))))
9284                    (string-match regexp
9285                                  (funcall func (gnus-data-header d)))))
9286         (push (gnus-data-number d) articles))) ; Success!
9287     (nreverse articles)))
9288
9289 (defun gnus-summary-execute-command (header regexp command &optional backward)
9290   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9291 If HEADER is an empty string (or nil), the match is done on the entire
9292 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9293   (interactive
9294    (list (let ((completion-ignore-case t))
9295            (gnus-completing-read
9296             "Header name"
9297             (mapcar 'symbol-name
9298                     (append
9299                      '(Number Subject From Lines Date
9300                        Message-ID Xref References Body)
9301                      gnus-extra-headers))
9302             'require-match))
9303          (read-string "Regexp: ")
9304          (read-key-sequence "Command: ")
9305          current-prefix-arg))
9306   (when (equal header "Body")
9307     (setq header ""))
9308   ;; Hidden thread subtrees must be searched as well.
9309   (gnus-summary-show-all-threads)
9310   ;; We don't want to change current point nor window configuration.
9311   (save-excursion
9312     (save-window-excursion
9313       (let (gnus-visual
9314             gnus-treat-strip-trailing-blank-lines
9315             gnus-treat-strip-leading-blank-lines
9316             gnus-treat-strip-multiple-blank-lines
9317             gnus-treat-hide-boring-headers
9318             gnus-treat-fold-newsgroups
9319             gnus-article-prepare-hook)
9320         (gnus-message 6 "Executing %s..." (key-description command))
9321         ;; We'd like to execute COMMAND interactively so as to give arguments.
9322         (gnus-execute header regexp
9323                       `(call-interactively ',(key-binding command))
9324                       backward)
9325         (gnus-message 6 "Executing %s...done" (key-description command))))))
9326
9327 (defun gnus-summary-beginning-of-article ()
9328   "Scroll the article back to the beginning."
9329   (interactive)
9330   (gnus-summary-select-article)
9331   (gnus-configure-windows 'article)
9332   (gnus-eval-in-buffer-window gnus-article-buffer
9333     (widen)
9334     (goto-char (point-min))
9335     (when gnus-break-pages
9336       (gnus-narrow-to-page))))
9337
9338 (defun gnus-summary-end-of-article ()
9339   "Scroll to the end of the article."
9340   (interactive)
9341   (gnus-summary-select-article)
9342   (gnus-configure-windows 'article)
9343   (gnus-eval-in-buffer-window gnus-article-buffer
9344     (widen)
9345     (goto-char (point-max))
9346     (recenter -3)
9347     (when gnus-break-pages
9348       (gnus-narrow-to-page))))
9349
9350 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9351   "Truncate to LEN and quote all \"(\"'s in STRING."
9352   (gnus-replace-in-string (if (and len (> (length string) len))
9353                               (substring string 0 len)
9354                             string)
9355                           "[()]" "\\\\\\&"))
9356
9357 (defun gnus-summary-print-article (&optional filename n)
9358   "Generate and print a PostScript image of the process-marked (mail) articles.
9359
9360 If used interactively, print the current article if none are
9361 process-marked.  With prefix arg, prompt the user for the name of the
9362 file to save in.
9363
9364 When used from Lisp, accept two optional args FILENAME and N.  N means
9365 to print the next N articles.  If N is negative, print the N previous
9366 articles.  If N is nil and articles have been marked with the process
9367 mark, print these instead.
9368
9369 If the optional first argument FILENAME is nil, send the image to the
9370 printer.  If FILENAME is a string, save the PostScript image in a file with
9371 that name.  If FILENAME is a number, prompt the user for the name of the file
9372 to save in."
9373   (interactive (list (ps-print-preprint current-prefix-arg)))
9374   (dolist (article (gnus-summary-work-articles n))
9375     (gnus-summary-select-article nil nil 'pseudo article)
9376     (gnus-eval-in-buffer-window gnus-article-buffer
9377       (gnus-print-buffer))
9378     (gnus-summary-remove-process-mark article))
9379   (ps-despool filename))
9380
9381 (defun gnus-print-buffer ()
9382   (let ((ps-left-header
9383          (list
9384           (concat "("
9385                   (gnus-summary-print-truncate-and-quote
9386                    (mail-header-subject gnus-current-headers)
9387                    66) ")")
9388           (concat "("
9389                   (gnus-summary-print-truncate-and-quote
9390                    (mail-header-from gnus-current-headers)
9391                    45) ")")))
9392         (ps-right-header
9393          (list
9394           "/pagenumberstring load"
9395           (concat "("
9396                   (mail-header-date gnus-current-headers) ")"))))
9397     (gnus-run-hooks 'gnus-ps-print-hook)
9398     (save-excursion
9399       (if ps-print-color-p
9400           (ps-spool-buffer-with-faces)
9401         (ps-spool-buffer)))))
9402
9403 (defun gnus-summary-show-complete-article ()
9404   "Show a complete version of the current article.
9405 This is only useful if you're looking at a partial version of the
9406 article currently."
9407   (interactive)
9408   (let ((gnus-keep-backlog nil)
9409         (gnus-use-cache nil)
9410         (gnus-agent nil)
9411         (variable (intern
9412                    (format "%s-fetch-partial-articles"
9413                            (car (gnus-find-method-for-group
9414                                  gnus-newsgroup-name)))
9415                    obarray))
9416         old-val)
9417     (unwind-protect
9418         (progn
9419           (setq old-val (symbol-value variable))
9420           (set variable nil)
9421           (gnus-flush-original-article-buffer)
9422           (gnus-summary-show-article))
9423       (set variable old-val))))
9424
9425 (defun gnus-summary-show-article (&optional arg)
9426   "Force redisplaying of the current article.
9427 If ARG (the prefix) is a number, show the article with the charset
9428 defined in `gnus-summary-show-article-charset-alist', or the charset
9429 input.
9430 If ARG (the prefix) is non-nil and not a number, show the article,
9431 but without running any of the article treatment functions
9432 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9433 C-u g', show the raw article."
9434   (interactive "P")
9435   (cond
9436    ((numberp arg)
9437     (gnus-summary-show-article t)
9438     (let ((gnus-newsgroup-charset
9439            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9440                (mm-read-coding-system
9441                 "View as charset: " ;; actually it is coding system.
9442                 (with-current-buffer gnus-article-buffer
9443                   (mm-detect-coding-region (point) (point-max))))))
9444           (gnus-newsgroup-ignored-charsets 'gnus-all))
9445       (gnus-summary-select-article nil 'force)
9446       (let ((deps gnus-newsgroup-dependencies)
9447             head header lines)
9448         (with-current-buffer gnus-original-article-buffer
9449           (save-restriction
9450             (message-narrow-to-head)
9451             (setq head (buffer-string))
9452             (goto-char (point-min))
9453             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9454               (goto-char (point-max))
9455               (widen)
9456               (setq lines (1- (count-lines (point) (point-max))))))
9457           (with-temp-buffer
9458             (insert (format "211 %d Article retrieved.\n"
9459                             (cdr gnus-article-current)))
9460             (insert head)
9461             (if lines (insert (format "Lines: %d\n" lines)))
9462             (insert ".\n")
9463             (let ((nntp-server-buffer (current-buffer)))
9464               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9465         (gnus-data-set-header
9466          (gnus-data-find (cdr gnus-article-current))
9467          header)
9468         (gnus-summary-update-article-line
9469          (cdr gnus-article-current) header)
9470         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9471           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9472    ((not arg)
9473     ;; Select the article the normal way.
9474     (gnus-summary-select-article nil 'force))
9475    ((or (equal arg '(16))
9476         (eq arg t))
9477     ;; C-u C-u g
9478     (let ((gnus-inhibit-article-treatments t))
9479       (gnus-summary-select-article nil 'force)))
9480    (t
9481     ;; We have to require this here to make sure that the following
9482     ;; dynamic binding isn't shadowed by autoloading.
9483     (require 'gnus-async)
9484     (require 'gnus-art)
9485     ;; Bind the article treatment functions to nil.
9486     (let ((gnus-have-all-headers t)
9487           gnus-article-prepare-hook
9488           gnus-article-decode-hook
9489           gnus-display-mime-function
9490           gnus-break-pages)
9491       ;; Destroy any MIME parts.
9492       (when (gnus-buffer-live-p gnus-article-buffer)
9493         (with-current-buffer gnus-article-buffer
9494           (mm-destroy-parts gnus-article-mime-handles)
9495           ;; Set it to nil for safety reason.
9496           (setq gnus-article-mime-handle-alist nil)
9497           (setq gnus-article-mime-handles nil)))
9498       (gnus-summary-select-article nil 'force))))
9499   (gnus-summary-goto-subject gnus-current-article)
9500   (gnus-summary-position-point))
9501
9502 (defun gnus-summary-show-raw-article ()
9503   "Show the raw article without any article massaging functions being run."
9504   (interactive)
9505   (gnus-summary-show-article t))
9506
9507 (defun gnus-summary-verbose-headers (&optional arg)
9508   "Toggle permanent full header display.
9509 If ARG is a positive number, turn header display on.
9510 If ARG is a negative number, turn header display off."
9511   (interactive "P")
9512   (setq gnus-show-all-headers
9513         (cond ((or (not (numberp arg))
9514                    (zerop arg))
9515                (not gnus-show-all-headers))
9516               ((natnump arg)
9517                t)))
9518   (gnus-summary-show-article))
9519
9520 (defun gnus-summary-toggle-header (&optional arg)
9521   "Show the headers if they are hidden, or hide them if they are shown.
9522 If ARG is a positive number, show the entire header.
9523 If ARG is a negative number, hide the unwanted header lines."
9524   (interactive "P")
9525   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9526                      (get-buffer-window gnus-article-buffer t))))
9527     (with-current-buffer gnus-article-buffer
9528       (widen)
9529       (article-narrow-to-head)
9530       (let* ((inhibit-read-only t)
9531              (inhibit-point-motion-hooks t)
9532              (hidden (if (numberp arg)
9533                          (>= arg 0)
9534                        (or (not (looking-at "[^ \t\n]+:"))
9535                            (gnus-article-hidden-text-p 'headers))))
9536              s e)
9537         (delete-region (point-min) (point-max))
9538         (with-current-buffer gnus-original-article-buffer
9539           (goto-char (setq s (point-min)))
9540           (setq e (if (search-forward "\n\n" nil t)
9541                       (1- (point))
9542                     (point-max))))
9543         (insert-buffer-substring gnus-original-article-buffer s e)
9544         (run-hooks 'gnus-article-decode-hook)
9545         (if hidden
9546             (let ((gnus-treat-hide-headers nil)
9547                   (gnus-treat-hide-boring-headers nil))
9548               (gnus-delete-wash-type 'headers)
9549               (gnus-treat-article 'head))
9550           (gnus-treat-article 'head))
9551         (widen)
9552         (if window
9553             (set-window-start window (goto-char (point-min))))
9554         (if gnus-break-pages
9555             (gnus-narrow-to-page)
9556           (when (gnus-visual-p 'page-marker)
9557             (let ((inhibit-read-only t))
9558               (gnus-remove-text-with-property 'gnus-prev)
9559               (gnus-remove-text-with-property 'gnus-next))))
9560         (gnus-set-mode-line 'article)))))
9561
9562 (defun gnus-summary-show-all-headers ()
9563   "Make all header lines visible."
9564   (interactive)
9565   (gnus-summary-toggle-header 1))
9566
9567 (defun gnus-summary-caesar-message (&optional arg)
9568   "Caesar rotate the current article by 13.
9569 With a non-numerical prefix, also rotate headers.  A numerical
9570 prefix specifies how many places to rotate each letter forward."
9571   (interactive "P")
9572   (gnus-summary-select-article)
9573   (let ((mail-header-separator ""))
9574     (gnus-eval-in-buffer-window gnus-article-buffer
9575       (save-restriction
9576         (widen)
9577         (let ((start (window-start))
9578               (inhibit-read-only t))
9579           (if (equal arg '(4))
9580               (message-caesar-buffer-body nil t)
9581             (message-caesar-buffer-body arg))
9582           (set-window-start (get-buffer-window (current-buffer)) start)))))
9583   ;; Create buttons and stuff...
9584   (gnus-treat-article nil))
9585
9586 (declare-function idna-to-unicode "ext:idna" (str))
9587
9588 (defun gnus-summary-idna-message (&optional arg)
9589   "Decode IDNA encoded domain names in the current articles.
9590 IDNA encoded domain names looks like `xn--bar'.  If a string
9591 remain unencoded after running this function, it is likely an
9592 invalid IDNA string (`xn--bar' is invalid).
9593
9594 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9595 installed for this command to work."
9596   (interactive "P")
9597   (if (not (and (condition-case nil (require 'idna)
9598                   (file-error))
9599                 (mm-coding-system-p 'utf-8)
9600                 (executable-find (symbol-value 'idna-program))))
9601       (gnus-message
9602        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9603     (gnus-summary-select-article)
9604     (let ((mail-header-separator ""))
9605       (gnus-eval-in-buffer-window gnus-article-buffer
9606         (save-restriction
9607           (widen)
9608           (let ((start (window-start))
9609                 buffer-read-only)
9610             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9611               (replace-match (idna-to-unicode (match-string 1))))
9612             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9613
9614 (defun gnus-summary-morse-message (&optional arg)
9615   "Morse decode the current article."
9616   (interactive "P")
9617   (gnus-summary-select-article)
9618   (let ((mail-header-separator ""))
9619     (gnus-eval-in-buffer-window gnus-article-buffer
9620       (save-excursion
9621         (save-restriction
9622           (widen)
9623           (let ((pos (window-start))
9624                 (inhibit-read-only t))
9625             (goto-char (point-min))
9626             (when (message-goto-body)
9627               (gnus-narrow-to-body))
9628             (goto-char (point-min))
9629             (while (search-forward "·" (point-max) t)
9630               (replace-match "."))
9631             (unmorse-region (point-min) (point-max))
9632             (widen)
9633             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9634
9635 (defun gnus-summary-stop-page-breaking ()
9636   "Stop page breaking in the current article."
9637   (interactive)
9638   (gnus-summary-select-article)
9639   (gnus-eval-in-buffer-window gnus-article-buffer
9640     (widen)
9641     (when (gnus-visual-p 'page-marker)
9642       (let ((inhibit-read-only t))
9643         (gnus-remove-text-with-property 'gnus-prev)
9644         (gnus-remove-text-with-property 'gnus-next))
9645       (setq gnus-page-broken nil))))
9646
9647 (defun gnus-summary-move-article (&optional n to-newsgroup
9648                                             select-method action)
9649   "Move the current article to a different newsgroup.
9650 If N is a positive number, move the N next articles.
9651 If N is a negative number, move the N previous articles.
9652 If N is nil and any articles have been marked with the process mark,
9653 move those articles instead.
9654 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9655 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9656 re-spool using this method.
9657
9658 When called interactively with TO-NEWSGROUP being nil, the value of
9659 the variable `gnus-move-split-methods' is used for finding a default
9660 for the target newsgroup.
9661
9662 For this function to work, both the current newsgroup and the
9663 newsgroup that you want to move to have to support the `request-move'
9664 and `request-accept' functions.
9665
9666 ACTION can be either `move' (the default), `crosspost' or `copy'."
9667   (interactive "P")
9668   (unless action
9669     (setq action 'move))
9670   ;; Check whether the source group supports the required functions.
9671   (cond ((and (eq action 'move)
9672               (not (gnus-check-backend-function
9673                     'request-move-article gnus-newsgroup-name)))
9674          (error "The current group does not support article moving"))
9675         ((and (eq action 'crosspost)
9676               (not (gnus-check-backend-function
9677                     'request-replace-article gnus-newsgroup-name)))
9678          (error "The current group does not support article editing")))
9679   (let ((articles (gnus-summary-work-articles n))
9680         (prefix (if (gnus-check-backend-function
9681                      'request-move-article gnus-newsgroup-name)
9682                     (funcall gnus-move-group-prefix-function
9683                              gnus-newsgroup-name)
9684                   ""))
9685         (names '((move "Move" "Moving")
9686                  (copy "Copy" "Copying")
9687                  (crosspost "Crosspost" "Crossposting")))
9688         (copy-buf (save-excursion
9689                     (nnheader-set-temp-buffer " *copy article*")))
9690         art-group to-method new-xref article to-groups
9691         articles-to-update-marks encoded)
9692     (unless (assq action names)
9693       (error "Unknown action %s" action))
9694     ;; Read the newsgroup name.
9695     (when (and (not to-newsgroup)
9696                (not select-method))
9697       (if (and gnus-move-split-methods
9698                (not
9699                 (and (memq gnus-current-article articles)
9700                      (gnus-buffer-live-p gnus-original-article-buffer))))
9701           ;; When `gnus-move-split-methods' is non-nil, we have to
9702           ;; select an article to give `gnus-read-move-group-name' an
9703           ;; opportunity to suggest an appropriate default.  However,
9704           ;; we needn't render or mark the article.
9705           (let ((gnus-display-mime-function nil)
9706                 (gnus-article-prepare-hook nil)
9707                 (gnus-mark-article-hook nil))
9708             (gnus-summary-select-article nil nil nil (car articles))))
9709       (setq to-newsgroup (gnus-read-move-group-name
9710                           (cadr (assq action names))
9711                           (symbol-value
9712                            (intern (format "gnus-current-%s-group" action)))
9713                           articles prefix)
9714             encoded to-newsgroup
9715             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9716       (set (intern (format "gnus-current-%s-group" action))
9717            (mm-decode-coding-string
9718             to-newsgroup
9719             (gnus-group-name-charset to-method to-newsgroup))))
9720     (unless to-method
9721       (setq to-method (or select-method
9722                           (gnus-server-to-method
9723                            (gnus-group-method to-newsgroup)))))
9724     (setq to-newsgroup
9725           (or encoded
9726               (and to-newsgroup
9727                    (mm-encode-coding-string
9728                     to-newsgroup
9729                     (gnus-group-name-charset to-method to-newsgroup)))))
9730     ;; Check the method we are to move this article to...
9731     (unless (gnus-check-backend-function
9732              'request-accept-article (car to-method))
9733       (error "%s does not support article copying" (car to-method)))
9734     (unless (gnus-check-server to-method)
9735       (error "Can't open server %s" (car to-method)))
9736     (gnus-message 6 "%s to %s: %s..."
9737                   (caddr (assq action names))
9738                   (or (car select-method)
9739                       (gnus-group-decoded-name to-newsgroup))
9740                   articles)
9741     (while articles
9742       (setq article (pop articles))
9743       ;; Set any marks that may have changed in the summary buffer.
9744       (when gnus-preserve-marks
9745         (gnus-summary-push-marks-to-backend article))
9746       (setq
9747        art-group
9748        (cond
9749         ;; Move the article.
9750         ((eq action 'move)
9751          ;; Remove this article from future suppression.
9752          (gnus-dup-unsuppress-article article)
9753          (let* ((from-method (gnus-find-method-for-group
9754                               gnus-newsgroup-name))
9755                 (to-method (or select-method
9756                                (gnus-find-method-for-group to-newsgroup)))
9757                 (move-is-internal (gnus-server-equal from-method to-method)))
9758            (gnus-request-move-article
9759             article                     ; Article to move
9760             gnus-newsgroup-name         ; From newsgroup
9761             (nth 1 (gnus-find-method-for-group
9762                     gnus-newsgroup-name)) ; Server
9763             (list 'gnus-request-accept-article
9764                   to-newsgroup (list 'quote select-method)
9765                   (not articles) t)     ; Accept form
9766             (not articles)              ; Only save nov last time
9767             (and move-is-internal
9768                  to-newsgroup           ; Not respooling
9769                                         ; Is this move internal?
9770                  (gnus-group-real-name to-newsgroup)))))
9771         ;; Copy the article.
9772         ((eq action 'copy)
9773          (with-current-buffer copy-buf
9774            (when (gnus-request-article-this-buffer article
9775                                                    gnus-newsgroup-name)
9776              (save-restriction
9777                (nnheader-narrow-to-headers)
9778                (dolist (hdr gnus-copy-article-ignored-headers)
9779                  (message-remove-header hdr t)))
9780              (gnus-request-accept-article
9781               to-newsgroup select-method (not articles) t))))
9782         ;; Crosspost the article.
9783         ((eq action 'crosspost)
9784          (let ((xref (message-tokenize-header
9785                       (mail-header-xref (gnus-summary-article-header
9786                                          article))
9787                       " ")))
9788            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9789                                   ":" (number-to-string article)))
9790            (unless xref
9791              (setq xref (list (system-name))))
9792            (setq new-xref
9793                  (concat
9794                   (mapconcat 'identity
9795                              (delete "Xref:" (delete new-xref xref))
9796                              " ")
9797                   " " new-xref))
9798            (with-current-buffer copy-buf
9799              ;; First put the article in the destination group.
9800              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9801              (when (consp (setq art-group
9802                                 (gnus-request-accept-article
9803                                  to-newsgroup select-method (not articles)
9804                                  t)))
9805                (setq new-xref (concat new-xref " " (car art-group)
9806                                       ":"
9807                                       (number-to-string (cdr art-group))))
9808                ;; Now we have the new Xrefs header, so we insert
9809                ;; it and replace the new article.
9810                (nnheader-replace-header "Xref" new-xref)
9811                (gnus-request-replace-article
9812                 (cdr art-group) to-newsgroup (current-buffer) t)
9813                art-group))))))
9814       (cond
9815        ((not art-group)
9816         (gnus-message 1 "Couldn't %s article %s: %s"
9817                       (cadr (assq action names)) article
9818                       (nnheader-get-report (car to-method))))
9819        ((eq art-group 'junk)
9820         (when (eq action 'move)
9821           (gnus-summary-mark-article article gnus-canceled-mark)
9822           (gnus-message 4 "Deleted article %s" article)
9823           ;; run the delete hook
9824           (run-hook-with-args 'gnus-summary-article-delete-hook
9825                               action
9826                               (gnus-data-header
9827                                (assoc article (gnus-data-list nil)))
9828                               gnus-newsgroup-name nil
9829                               select-method)))
9830        (t
9831         (let* ((pto-group (gnus-group-prefixed-name
9832                            (car art-group) to-method))
9833                (info (gnus-get-info pto-group))
9834                (to-group (gnus-info-group info))
9835                to-marks)
9836           ;; Update the group that has been moved to.
9837           (when (and info
9838                      (memq action '(move copy)))
9839             (unless (member to-group to-groups)
9840               (push to-group to-groups))
9841
9842             (unless (memq article gnus-newsgroup-unreads)
9843               (push 'read to-marks)
9844               (gnus-info-set-read
9845                info (gnus-add-to-range (gnus-info-read info)
9846                                        (list (cdr art-group)))))
9847
9848             ;; See whether the article is to be put in the cache.
9849             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9850                    (marks (if expirable
9851                               gnus-article-mark-lists
9852                             (delete '(expirable . expire)
9853                                     (copy-sequence
9854                                      gnus-article-mark-lists))))
9855                    (to-article (cdr art-group)))
9856
9857               ;; Enter the article into the cache in the new group,
9858               ;; if that is required.
9859               (when gnus-use-cache
9860                 (gnus-cache-possibly-enter-article
9861                  to-group to-article
9862                  (memq article gnus-newsgroup-marked)
9863                  (memq article gnus-newsgroup-dormant)
9864                  (memq article gnus-newsgroup-unreads)))
9865
9866               (when gnus-preserve-marks
9867                 ;; Copy any marks over to the new group.
9868                 (when (and (equal to-group gnus-newsgroup-name)
9869                            (not (memq article gnus-newsgroup-unreads)))
9870                   ;; Mark this article as read in this group.
9871                   (push (cons to-article gnus-read-mark)
9872                         gnus-newsgroup-reads)
9873                   ;; Increase the active status of this group.
9874                   (setcdr (gnus-active to-group) to-article)
9875                   (setcdr gnus-newsgroup-active to-article))
9876
9877                 (while marks
9878                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9879                     (when (memq article (symbol-value
9880                                          (intern (format "gnus-newsgroup-%s"
9881                                                          (caar marks)))))
9882                       (push (cdar marks) to-marks)
9883                       ;; If the other group is the same as this group,
9884                       ;; then we have to add the mark to the list.
9885                       (when (equal to-group gnus-newsgroup-name)
9886                         (set (intern (format "gnus-newsgroup-%s"
9887                                              (caar marks)))
9888                              (cons to-article
9889                                    (symbol-value
9890                                     (intern (format "gnus-newsgroup-%s"
9891                                                     (caar marks)))))))
9892                       ;; Copy the marks to other group.
9893                       (gnus-add-marked-articles
9894                        to-group (cdar marks) (list to-article) info)))
9895                   (setq marks (cdr marks)))
9896
9897                 (when (and expirable
9898                            gnus-mark-copied-or-moved-articles-as-expirable
9899                            (not (memq 'expire to-marks)))
9900                   ;; Mark this article as expirable.
9901                   (push 'expire to-marks)
9902                   (when (equal to-group gnus-newsgroup-name)
9903                     (push to-article gnus-newsgroup-expirable))
9904                   ;; Copy the expirable mark to other group.
9905                   (gnus-add-marked-articles
9906                    to-group 'expire (list to-article) info))
9907
9908                 (when to-marks
9909                   (gnus-request-set-mark
9910                    to-group (list (list (list to-article) 'add to-marks)))))
9911
9912               (gnus-dribble-enter
9913                (concat "(gnus-group-set-info '"
9914                        (gnus-prin1-to-string (gnus-get-info to-group))
9915                        ")"))))
9916
9917           ;; Update the Xref header in this article to point to
9918           ;; the new crossposted article we have just created.
9919           (when (eq action 'crosspost)
9920             (with-current-buffer copy-buf
9921               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9922               (nnheader-replace-header "Xref" new-xref)
9923               (gnus-request-replace-article
9924                article gnus-newsgroup-name (current-buffer) t)))
9925
9926           ;; run the move/copy/crosspost/respool hook
9927           (run-hook-with-args 'gnus-summary-article-move-hook
9928                               action
9929                               (gnus-data-header
9930                                (assoc article (gnus-data-list nil)))
9931                               gnus-newsgroup-name
9932                               to-newsgroup
9933                               select-method))
9934
9935         ;;;!!!Why is this necessary?
9936         (set-buffer gnus-summary-buffer)
9937
9938         (when (eq action 'move)
9939           (save-excursion
9940             (gnus-summary-goto-subject article)
9941             (gnus-summary-mark-article article gnus-canceled-mark)))))
9942       (push article articles-to-update-marks))
9943
9944     (save-excursion
9945       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9946     ;; Re-activate all groups that have been moved to.
9947     (with-current-buffer gnus-group-buffer
9948       (let ((gnus-group-marked to-groups))
9949         (gnus-group-get-new-news-this-group nil t)))
9950
9951     (gnus-kill-buffer copy-buf)
9952     (gnus-summary-position-point)
9953     (gnus-set-mode-line 'summary)))
9954
9955 (defun gnus-summary-push-marks-to-backend (article)
9956   (let ((set nil)
9957         (marks gnus-article-mark-lists))
9958     (when (memq article gnus-newsgroup-unreads)
9959       (push 'read set))
9960     (while marks
9961       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9962                  (memq article (symbol-value
9963                                 (intern (format "gnus-newsgroup-%s"
9964                                                 (caar marks))))))
9965         (push (cdar marks) set))
9966       (pop marks))
9967     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
9968
9969 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9970   "Copy the current article to some other group.
9971 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9972 When called interactively, if TO-NEWSGROUP is nil, use the value of
9973 the variable `gnus-move-split-methods' for finding a default target
9974 newsgroup.
9975 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9976 re-spool using this method."
9977   (interactive "P")
9978   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9979
9980 (defun gnus-summary-crosspost-article (&optional n)
9981   "Crosspost the current article to some other group."
9982   (interactive "P")
9983   (gnus-summary-move-article n nil nil 'crosspost))
9984
9985 (defcustom gnus-summary-respool-default-method nil
9986   "Default method type for respooling an article.
9987 If nil, use to the current newsgroup method."
9988   :type 'symbol
9989   :group 'gnus-summary-mail)
9990
9991 (defun gnus-summary-respool-article (&optional n method)
9992   "Respool the current article.
9993 The article will be squeezed through the mail spooling process again,
9994 which means that it will be put in some mail newsgroup or other
9995 depending on `nnmail-split-methods'.
9996 If N is a positive number, respool the N next articles.
9997 If N is a negative number, respool the N previous articles.
9998 If N is nil and any articles have been marked with the process mark,
9999 respool those articles instead.
10000
10001 Respooling can be done both from mail groups and \"real\" newsgroups.
10002 In the former case, the articles in question will be moved from the
10003 current group into whatever groups they are destined to.  In the
10004 latter case, they will be copied into the relevant groups."
10005   (interactive
10006    (list current-prefix-arg
10007          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10008                 (methname
10009                  (symbol-name (or gnus-summary-respool-default-method
10010                                   (car (gnus-find-method-for-group
10011                                         gnus-newsgroup-name)))))
10012                 (method
10013                  (gnus-completing-read
10014                   "Backend to use when respooling"
10015                   methods t nil 'gnus-mail-method-history methname))
10016                 ms)
10017            (cond
10018             ((zerop (length (setq ms (gnus-servers-using-backend
10019                                       (intern method)))))
10020              (list (intern method) ""))
10021             ((= 1 (length ms))
10022              (car ms))
10023             (t
10024              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10025                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10026                            ms-alist))))))))
10027   (unless method
10028     (error "No method given for respooling"))
10029   (if (assoc (symbol-name
10030               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10031              (gnus-methods-using 'respool))
10032       (gnus-summary-move-article n nil method)
10033     (gnus-summary-copy-article n nil method)))
10034
10035 (defun gnus-summary-import-article (file &optional edit)
10036   "Import an arbitrary file into a mail newsgroup."
10037   (interactive "fImport file: \nP")
10038   (let ((group gnus-newsgroup-name)
10039         (now (current-time))
10040         atts lines group-art)
10041     (unless (gnus-check-backend-function 'request-accept-article group)
10042       (error "%s does not support article importing" group))
10043     (or (file-readable-p file)
10044         (not (file-regular-p file))
10045         (error "Can't read %s" file))
10046     (with-current-buffer (gnus-get-buffer-create " *import file*")
10047       (erase-buffer)
10048       (nnheader-insert-file-contents file)
10049       (goto-char (point-min))
10050       (if (nnheader-article-p)
10051           (save-restriction
10052             (goto-char (point-min))
10053             (search-forward "\n\n" nil t)
10054             (narrow-to-region (point-min) (1- (point)))
10055             (goto-char (point-min))
10056             (unless (re-search-forward "^date:" nil t)
10057               (goto-char (point-max))
10058               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10059        ;; This doesn't look like an article, so we fudge some headers.
10060         (setq atts (file-attributes file)
10061               lines (count-lines (point-min) (point-max)))
10062         (insert "From: " (read-string "From: ") "\n"
10063                 "Subject: " (read-string "Subject: ") "\n"
10064                 "Date: " (message-make-date (nth 5 atts)) "\n"
10065                 "Message-ID: " (message-make-message-id) "\n"
10066                 "Lines: " (int-to-string lines) "\n"
10067                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10068       (setq group-art (gnus-request-accept-article group nil t))
10069       (kill-buffer (current-buffer)))
10070     (setq gnus-newsgroup-active (gnus-activate-group group))
10071     (forward-line 1)
10072     (gnus-summary-goto-article (cdr group-art) nil t)
10073     (when edit
10074       (gnus-summary-edit-article))))
10075
10076 (defun gnus-summary-create-article ()
10077   "Create an article in a mail newsgroup."
10078   (interactive)
10079   (let ((group gnus-newsgroup-name)
10080         (now (current-time))
10081         group-art)
10082     (unless (gnus-check-backend-function 'request-accept-article group)
10083       (error "%s does not support article importing" group))
10084     (with-current-buffer (gnus-get-buffer-create " *import file*")
10085       (erase-buffer)
10086       (goto-char (point-min))
10087       ;; This doesn't look like an article, so we fudge some headers.
10088       (insert "From: " (read-string "From: ") "\n"
10089               "Subject: " (read-string "Subject: ") "\n"
10090               "Date: " (message-make-date now) "\n"
10091               "Message-ID: " (message-make-message-id) "\n")
10092       (setq group-art (gnus-request-accept-article group nil t))
10093       (kill-buffer (current-buffer)))
10094     (setq gnus-newsgroup-active (gnus-activate-group group))
10095     (forward-line 1)
10096     (gnus-summary-goto-article (cdr group-art) nil t)
10097     (gnus-summary-edit-article)))
10098
10099 (defun gnus-summary-article-posted-p ()
10100   "Say whether the current (mail) article is available from news as well.
10101 This will be the case if the article has both been mailed and posted."
10102   (interactive)
10103   (let ((id (mail-header-references (gnus-summary-article-header)))
10104         (gnus-override-method (car (gnus-refer-article-methods))))
10105     (if (gnus-request-head id "")
10106         (gnus-message 2 "The current message was found on %s"
10107                       gnus-override-method)
10108       (gnus-message 2 "The current message couldn't be found on %s"
10109                     gnus-override-method)
10110       nil)))
10111
10112 (defun gnus-summary-expire-articles (&optional now)
10113   "Expire all articles that are marked as expirable in the current group."
10114   (interactive)
10115   (when (and (not gnus-group-is-exiting-without-update-p)
10116              (gnus-check-backend-function
10117               'request-expire-articles gnus-newsgroup-name))
10118     ;; This backend supports expiry.
10119     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10120            (expirable (if total
10121                           (progn
10122                             ;; We need to update the info for
10123                             ;; this group for `gnus-list-of-read-articles'
10124                             ;; to give us the right answer.
10125                             (gnus-run-hooks 'gnus-exit-group-hook)
10126                             (gnus-summary-update-info)
10127                             (gnus-list-of-read-articles gnus-newsgroup-name))
10128                         (setq gnus-newsgroup-expirable
10129                               (sort gnus-newsgroup-expirable '<))))
10130            (expiry-wait (if now 'immediate
10131                           (gnus-group-find-parameter
10132                            gnus-newsgroup-name 'expiry-wait)))
10133            (nnmail-expiry-target
10134             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10135                 nnmail-expiry-target))
10136            es)
10137       (when expirable
10138         ;; There are expirable articles in this group, so we run them
10139         ;; through the expiry process.
10140         (gnus-message 6 "Expiring articles...")
10141         (unless (gnus-check-group gnus-newsgroup-name)
10142           (error "Can't open server for %s" gnus-newsgroup-name))
10143         ;; The list of articles that weren't expired is returned.
10144         (save-excursion
10145           (if expiry-wait
10146               (let ((nnmail-expiry-wait-function nil)
10147                     (nnmail-expiry-wait expiry-wait))
10148                 (setq es (gnus-request-expire-articles
10149                           expirable gnus-newsgroup-name)))
10150             (setq es (gnus-request-expire-articles
10151                       expirable gnus-newsgroup-name)))
10152           (unless total
10153             (setq gnus-newsgroup-expirable es))
10154           ;; We go through the old list of expirable, and mark all
10155           ;; really expired articles as nonexistent.
10156           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10157             (let ((gnus-use-cache nil))
10158               (dolist (article expirable)
10159                 (when (and (not (memq article es))
10160                            (gnus-data-find article))
10161                   (gnus-summary-mark-article article gnus-canceled-mark)
10162                   (run-hook-with-args 'gnus-summary-article-expire-hook
10163                                       'delete
10164                                       (gnus-data-header
10165                                        (assoc article (gnus-data-list nil)))
10166                                       gnus-newsgroup-name
10167                                       nil
10168                                       nil))))))
10169         (gnus-message 6 "Expiring articles...done")))))
10170
10171 (defun gnus-summary-expire-articles-now ()
10172   "Expunge all expirable articles in the current group.
10173 This means that *all* articles that are marked as expirable will be
10174 deleted forever, right now."
10175   (interactive)
10176   (or gnus-expert-user
10177       (gnus-yes-or-no-p
10178        "Are you really, really sure you want to delete all expirable messages? ")
10179       (error "Phew!"))
10180   (gnus-summary-expire-articles t))
10181
10182 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10183 (defun gnus-summary-delete-article (&optional n)
10184   "Delete the N next (mail) articles.
10185 This command actually deletes articles.  This is not a marking
10186 command.  The article will disappear forever from your life, never to
10187 return.
10188
10189 If N is negative, delete backwards.
10190 If N is nil and articles have been marked with the process mark,
10191 delete these instead.
10192
10193 If `gnus-novice-user' is non-nil you will be asked for
10194 confirmation before the articles are deleted."
10195   (interactive "P")
10196   (unless (gnus-check-backend-function 'request-expire-articles
10197                                        gnus-newsgroup-name)
10198     (error "The current newsgroup does not support article deletion"))
10199   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10200     (error "Couldn't open server"))
10201   ;; Compute the list of articles to delete.
10202   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10203         (nnmail-expiry-target 'delete)
10204         not-deleted)
10205     (if (and gnus-novice-user
10206              (not (gnus-yes-or-no-p
10207                    (format "Do you really want to delete %s forever? "
10208                            (if (> (length articles) 1)
10209                                (format "these %s articles" (length articles))
10210                              "this article")))))
10211         ()
10212       ;; Delete the articles.
10213       (setq not-deleted (gnus-request-expire-articles
10214                          articles gnus-newsgroup-name 'force))
10215       (save-excursion
10216         (while articles
10217           (gnus-summary-remove-process-mark (car articles))
10218           ;; The backend might not have been able to delete the article
10219           ;; after all.
10220           (unless (memq (car articles) not-deleted)
10221             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10222             (let* ((article (car articles))
10223                    (ghead  (gnus-data-header
10224                             (assoc article (gnus-data-list nil)))))
10225               (run-hook-with-args 'gnus-summary-article-delete-hook
10226                                   'delete ghead gnus-newsgroup-name nil
10227                                   nil)))
10228           (setq articles (cdr articles))))
10229       (when not-deleted
10230         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10231     (gnus-summary-position-point)
10232     (gnus-set-mode-line 'summary)
10233     not-deleted))
10234
10235 (defun gnus-summary-edit-article (&optional arg)
10236   "Edit the current article.
10237 This will have permanent effect only in mail groups.
10238 If ARG is nil, edit the decoded articles.
10239 If ARG is 1, edit the raw articles.
10240 If ARG is 2, edit the raw articles even in read-only groups.
10241 If ARG is 3, edit the articles with the current handles.
10242 Otherwise, allow editing of articles even in read-only
10243 groups."
10244   (interactive "P")
10245   (let (force raw current-handles)
10246     (cond
10247      ((null arg))
10248      ((eq arg 1)
10249       (setq raw t))
10250      ((eq arg 2)
10251       (setq raw t
10252             force t))
10253      ((eq arg 3)
10254       (setq current-handles
10255             (and (gnus-buffer-live-p gnus-article-buffer)
10256                  (with-current-buffer gnus-article-buffer
10257                    (prog1
10258                        gnus-article-mime-handles
10259                      (setq gnus-article-mime-handles nil))))))
10260      (t
10261       (setq force t)))
10262     (when (and raw (not force)
10263                (member gnus-newsgroup-name '("nndraft:delayed"
10264                                              "nndraft:drafts"
10265                                              "nndraft:queue")))
10266       (error "Can't edit the raw article in group %s"
10267              gnus-newsgroup-name))
10268     (with-current-buffer gnus-summary-buffer
10269       (let ((mail-parse-charset gnus-newsgroup-charset)
10270             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10271         (gnus-set-global-variables)
10272         (when (and (not force)
10273                    (gnus-group-read-only-p))
10274           (error "The current newsgroup does not support article editing"))
10275         (gnus-summary-show-article t)
10276         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10277           (with-current-buffer gnus-article-buffer
10278             (mm-enable-multibyte)))
10279         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10280             (setq raw t))
10281         (gnus-article-edit-article
10282          (if raw 'ignore
10283            `(lambda ()
10284               (let ((mbl mml-buffer-list))
10285                 (setq mml-buffer-list nil)
10286                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10287                   (mime-to-mml ,'current-handles))
10288                 (let ((mbl1 mml-buffer-list))
10289                   (setq mml-buffer-list mbl)
10290                   (set (make-local-variable 'mml-buffer-list) mbl1))
10291                 (gnus-make-local-hook 'kill-buffer-hook)
10292                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10293          `(lambda (no-highlight)
10294             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10295                   (message-options message-options)
10296                   (message-options-set-recipient)
10297                   (mail-parse-ignored-charsets
10298                    ',gnus-newsgroup-ignored-charsets)
10299                   (rfc2047-header-encoding-alist
10300                    ',(let ((charset (gnus-group-name-charset
10301                                      (gnus-find-method-for-group
10302                                       gnus-newsgroup-name)
10303                                      gnus-newsgroup-name)))
10304                        (append (list (cons "Newsgroups" charset)
10305                                      (cons "Followup-To" charset)
10306                                      (cons "Xref" charset))
10307                                rfc2047-header-encoding-alist))))
10308               ,(if (not raw) '(progn
10309                                 (mml-to-mime)
10310                                 (mml-destroy-buffers)
10311                                 (remove-hook 'kill-buffer-hook
10312                                              'mml-destroy-buffers t)
10313                                 (kill-local-variable 'mml-buffer-list)))
10314               (gnus-summary-edit-article-done
10315                ,(or (mail-header-references gnus-current-headers) "")
10316                ,(gnus-group-read-only-p)
10317                ,gnus-summary-buffer no-highlight))))))))
10318
10319 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10320
10321 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10322                                                  no-highlight)
10323   "Make edits to the current article permanent."
10324   (interactive)
10325   (save-excursion
10326     ;; The buffer restriction contains the entire article if it exists.
10327     (when (article-goto-body)
10328       (let ((lines (count-lines (point) (point-max)))
10329             (length (- (point-max) (point)))
10330             (case-fold-search t)
10331             (body (copy-marker (point))))
10332         (goto-char (point-min))
10333         (when (re-search-forward "^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
10338                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10339           (delete-region (match-beginning 1) (match-end 1))
10340           (insert (number-to-string length)))
10341         (goto-char (point-min))
10342         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10343           (delete-region (match-beginning 1) (match-end 1))
10344           (insert (number-to-string lines))))))
10345   ;; Replace the article.
10346   (let ((buf (current-buffer))
10347         (article (cdr gnus-article-current))
10348         replace-result)
10349     (with-temp-buffer
10350       (insert-buffer-substring buf)
10351       (if (and (not read-only)
10352                (not (setq replace-result
10353                           (gnus-request-replace-article
10354                            article (car gnus-article-current)
10355                            (current-buffer) t))))
10356           (error "Couldn't replace article")
10357         ;; If we got a number back, then that's the new article number
10358         ;; for this article.  Otherwise, the article number didn't change.
10359         (when (numberp replace-result)
10360           (with-current-buffer gnus-summary-buffer
10361             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10362             (gnus-summary-limit gnus-newsgroup-limit)
10363             (setq article replace-result)
10364             (gnus-summary-goto-subject article t)))
10365         ;; Update the summary buffer.
10366         (if (and references
10367                  (equal (message-tokenize-header references " ")
10368                         (message-tokenize-header
10369                          (or (message-fetch-field "references") "") " ")))
10370             ;; We only have to update this line.
10371             (save-excursion
10372               (save-restriction
10373                 (message-narrow-to-head)
10374                 (let ((head (buffer-substring-no-properties
10375                              (point-min) (point-max)))
10376                       header)
10377                   (with-temp-buffer
10378                     (insert (format "211 %d Article retrieved.\n" article))
10379                     (insert head)
10380                     (insert ".\n")
10381                     (let ((nntp-server-buffer (current-buffer)))
10382                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10383                     (with-current-buffer gnus-summary-buffer
10384                       (gnus-data-set-header (gnus-data-find article) header)
10385                       (gnus-summary-update-article-line article header)
10386                       (if (gnus-summary-goto-subject article nil t)
10387                           (gnus-summary-update-secondary-mark article)))))))
10388           ;; Update threads.
10389           (set-buffer (or buffer gnus-summary-buffer))
10390           (gnus-summary-update-article article)
10391           (if (gnus-summary-goto-subject article nil t)
10392               (gnus-summary-update-secondary-mark article)))
10393         ;; Prettify the article buffer again.
10394         (unless no-highlight
10395           (with-current-buffer gnus-article-buffer
10396             ;;!!! Fix this -- article should be rehighlighted.
10397             ;;(gnus-run-hooks 'gnus-article-display-hook)
10398             (set-buffer gnus-original-article-buffer)
10399             (gnus-request-article
10400              article (car gnus-article-current) (current-buffer))))
10401         ;; Prettify the summary buffer line.
10402         (when (gnus-visual-p 'summary-highlight 'highlight)
10403           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10404
10405 (defun gnus-summary-edit-wash (key)
10406   "Perform editing command KEY in the article buffer."
10407   (interactive
10408    (list
10409     (progn
10410       (message "%s" (concat (this-command-keys) "- "))
10411       (read-char))))
10412   (message "")
10413   (gnus-summary-edit-article)
10414   (execute-kbd-macro (concat (this-command-keys) key))
10415   (gnus-article-edit-done))
10416
10417 ;;; Respooling
10418
10419 (defun gnus-summary-respool-query (&optional silent trace)
10420   "Query where the respool algorithm would put this article."
10421   (interactive)
10422   (let (gnus-mark-article-hook)
10423     (gnus-summary-select-article)
10424     (with-current-buffer gnus-original-article-buffer
10425       (let ((groups (nnmail-article-group 'identity trace)))
10426         (unless silent
10427           (if groups
10428               (message "This message would go to %s"
10429                        (mapconcat 'car groups ", "))
10430             (message "This message would go to no groups"))
10431           groups)))))
10432
10433 (defun gnus-summary-respool-trace ()
10434   "Trace where the respool algorithm would put this article.
10435 Display a buffer showing all fancy splitting patterns which matched."
10436   (interactive)
10437   (gnus-summary-respool-query nil t))
10438
10439 ;; Summary marking commands.
10440
10441 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10442   "Mark articles which has the same subject as read, and then select the next.
10443 If UNMARK is positive, remove any kind of mark.
10444 If UNMARK is negative, tick articles."
10445   (interactive "P")
10446   (when unmark
10447     (setq unmark (prefix-numeric-value unmark)))
10448   (let ((count
10449          (gnus-summary-mark-same-subject
10450           (gnus-summary-article-subject) unmark)))
10451     ;; Select next unread article.  If auto-select-same mode, should
10452     ;; select the first unread article.
10453     (gnus-summary-next-article t (and gnus-auto-select-same
10454                                       (gnus-summary-article-subject)))
10455     (gnus-message 7 "%d article%s marked as %s"
10456                   count (if (= count 1) " is" "s are")
10457                   (if unmark "unread" "read"))))
10458
10459 (defun gnus-summary-kill-same-subject (&optional unmark)
10460   "Mark articles which has the same subject as read.
10461 If UNMARK is positive, remove any kind of mark.
10462 If UNMARK is negative, tick articles."
10463   (interactive "P")
10464   (when unmark
10465     (setq unmark (prefix-numeric-value unmark)))
10466   (let ((count
10467          (gnus-summary-mark-same-subject
10468           (gnus-summary-article-subject) unmark)))
10469     ;; If marked as read, go to next unread subject.
10470     (when (null unmark)
10471       ;; Go to next unread subject.
10472       (gnus-summary-next-subject 1 t))
10473     (gnus-message 7 "%d articles are marked as %s"
10474                   count (if unmark "unread" "read"))))
10475
10476 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10477   "Mark articles with same SUBJECT as read, and return marked number.
10478 If optional argument UNMARK is positive, remove any kinds of marks.
10479 If optional argument UNMARK is negative, mark articles as unread instead."
10480   (let ((count 1))
10481     (save-excursion
10482       (cond
10483        ((null unmark)                   ; Mark as read.
10484         (while (and
10485                 (progn
10486                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10487                   (gnus-summary-show-thread) t)
10488                 (gnus-summary-find-subject subject))
10489           (setq count (1+ count))))
10490        ((> unmark 0)                    ; Tick.
10491         (while (and
10492                 (progn
10493                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10494                   (gnus-summary-show-thread) t)
10495                 (gnus-summary-find-subject subject))
10496           (setq count (1+ count))))
10497        (t                               ; Mark as unread.
10498         (while (and
10499                 (progn
10500                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10501                   (gnus-summary-show-thread) t)
10502                 (gnus-summary-find-subject subject))
10503           (setq count (1+ count)))))
10504       (gnus-set-mode-line 'summary)
10505       ;; Return the number of marked articles.
10506       count)))
10507
10508 (defun gnus-summary-mark-as-processable (n &optional unmark)
10509   "Set the process mark on the next N articles.
10510 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10511 the process mark instead.  The difference between N and the actual
10512 number of articles marked is returned."
10513   (interactive "P")
10514   (if (and (null n) (gnus-region-active-p))
10515       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10516     (setq n (prefix-numeric-value n))
10517     (let ((backward (< n 0))
10518           (n (abs n)))
10519       (while (and
10520               (> n 0)
10521               (if unmark
10522                   (gnus-summary-remove-process-mark
10523                    (gnus-summary-article-number))
10524                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10525               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10526         (setq n (1- n)))
10527       (when (/= 0 n)
10528         (gnus-message 7 "No more articles"))
10529       (gnus-summary-recenter)
10530       (gnus-summary-position-point)
10531       n)))
10532
10533 (defun gnus-summary-unmark-as-processable (n)
10534   "Remove the process mark from the next N articles.
10535 If N is negative, unmark backward instead.  The difference between N and
10536 the actual number of articles unmarked is returned."
10537   (interactive "P")
10538   (gnus-summary-mark-as-processable n t))
10539
10540 (defun gnus-summary-unmark-all-processable ()
10541   "Remove the process mark from all articles."
10542   (interactive)
10543   (save-excursion
10544     (while gnus-newsgroup-processable
10545       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10546   (gnus-summary-position-point))
10547
10548 (defun gnus-summary-add-mark (article type)
10549   "Mark ARTICLE with a mark of TYPE."
10550   (let ((vtype (car (assq type gnus-article-mark-lists)))
10551         var)
10552     (if (not vtype)
10553         (error "No such mark type: %s" type)
10554       (setq var (intern (format "gnus-newsgroup-%s" type)))
10555       (set var (cons article (symbol-value var)))
10556       (if (memq type '(processable cached replied forwarded recent saved))
10557           (gnus-summary-update-secondary-mark article)
10558         ;;; !!! This is bogus.  We should find out what primary
10559         ;;; !!! mark we want to set.
10560         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10561
10562 (defun gnus-summary-mark-as-expirable (n)
10563   "Mark N articles forward as expirable.
10564 If N is negative, mark backward instead.  The difference between N and
10565 the actual number of articles marked is returned."
10566   (interactive "p")
10567   (gnus-summary-mark-forward n gnus-expirable-mark))
10568
10569 (defun gnus-summary-mark-as-spam (n)
10570   "Mark N articles forward as spam.
10571 If N is negative, mark backward instead.  The difference between N and
10572 the actual number of articles marked is returned."
10573   (interactive "p")
10574   (gnus-summary-mark-forward n gnus-spam-mark))
10575
10576 (defun gnus-summary-mark-article-as-replied (article)
10577   "Mark ARTICLE as replied to and update the summary line.
10578 ARTICLE can also be a list of articles."
10579   (interactive (list (gnus-summary-article-number)))
10580   (let ((articles (if (listp article) article (list article))))
10581     (dolist (article articles)
10582       (unless (numberp article)
10583         (error "%s is not a number" article))
10584       (push article gnus-newsgroup-replied)
10585       (let ((inhibit-read-only t))
10586         (when (gnus-summary-goto-subject article nil t)
10587           (gnus-summary-update-secondary-mark article))))))
10588
10589 (defun gnus-summary-mark-article-as-forwarded (article)
10590   "Mark ARTICLE as forwarded and update the summary line.
10591 ARTICLE can also be a list of articles."
10592   (let ((articles (if (listp article) article (list article))))
10593     (dolist (article articles)
10594       (push article gnus-newsgroup-forwarded)
10595       (let ((inhibit-read-only t))
10596         (when (gnus-summary-goto-subject article nil t)
10597           (gnus-summary-update-secondary-mark article))))))
10598
10599 (defun gnus-summary-set-bookmark (article)
10600   "Set a bookmark in current article."
10601   (interactive (list (gnus-summary-article-number)))
10602   (when (or (not (get-buffer gnus-article-buffer))
10603             (not gnus-current-article)
10604             (not gnus-article-current)
10605             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10606     (error "No current article selected"))
10607   ;; Remove old bookmark, if one exists.
10608   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10609   ;; Set the new bookmark, which is on the form
10610   ;; (article-number . line-number-in-body).
10611   (push
10612    (cons article
10613          (with-current-buffer gnus-article-buffer
10614            (count-lines
10615             (min (point)
10616                  (save-excursion
10617                    (article-goto-body)
10618                    (point)))
10619             (point))))
10620    gnus-newsgroup-bookmarks)
10621   (gnus-message 6 "A bookmark has been added to the current article."))
10622
10623 (defun gnus-summary-remove-bookmark (article)
10624   "Remove the bookmark from the current article."
10625   (interactive (list (gnus-summary-article-number)))
10626   ;; Remove old bookmark, if one exists.
10627   (if (not (assq article gnus-newsgroup-bookmarks))
10628       (gnus-message 6 "No bookmark in current article.")
10629     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10630     (gnus-message 6 "Removed bookmark.")))
10631
10632 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10633 (defun gnus-summary-mark-as-dormant (n)
10634   "Mark N articles forward as dormant.
10635 If N is negative, mark backward instead.  The difference between N and
10636 the actual number of articles marked is returned."
10637   (interactive "p")
10638   (gnus-summary-mark-forward n gnus-dormant-mark))
10639
10640 (defun gnus-summary-set-process-mark (article)
10641   "Set the process mark on ARTICLE and update the summary line."
10642   (setq gnus-newsgroup-processable
10643         (cons article
10644               (delq article gnus-newsgroup-processable)))
10645   (when (gnus-summary-goto-subject article)
10646     (gnus-summary-show-thread)
10647     (gnus-summary-goto-subject article)
10648     (gnus-summary-update-secondary-mark article)))
10649
10650 (defun gnus-summary-remove-process-mark (&rest articles)
10651   "Remove the process mark from ARTICLES and update the summary line."
10652   (dolist (article articles)
10653     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10654     (when (gnus-summary-goto-subject article)
10655       (gnus-summary-show-thread)
10656       (gnus-summary-goto-subject article)
10657       (gnus-summary-update-secondary-mark article)))
10658   t)
10659
10660 (defun gnus-summary-set-saved-mark (article)
10661   "Set the process mark on ARTICLE and update the summary line."
10662   (push article gnus-newsgroup-saved)
10663   (when (gnus-summary-goto-subject article)
10664     (gnus-summary-update-secondary-mark article)))
10665
10666 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10667   "Mark N articles as read forwards.
10668 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10669 The difference between N and the actual number of articles marked is
10670 returned.
10671 If NO-EXPIRE, auto-expiry will be inhibited."
10672   (interactive "p")
10673   (gnus-summary-show-thread)
10674   (let ((backward (< n 0))
10675         (gnus-summary-goto-unread
10676          (and gnus-summary-goto-unread
10677               (not (eq gnus-summary-goto-unread 'never))
10678               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10679                                     gnus-ticked-mark gnus-dormant-mark)))))
10680         (n (abs n))
10681         (mark (or mark gnus-del-mark)))
10682     (while (and (> n 0)
10683                 (gnus-summary-mark-article nil mark no-expire)
10684                 (zerop (gnus-summary-next-subject
10685                         (if backward -1 1)
10686                         (and gnus-summary-goto-unread
10687                              (not (eq gnus-summary-goto-unread 'never)))
10688                         t)))
10689       (setq n (1- n)))
10690     (when (/= 0 n)
10691       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10692     (gnus-summary-recenter)
10693     (gnus-summary-position-point)
10694     (gnus-set-mode-line 'summary)
10695     n))
10696
10697 (defun gnus-summary-mark-article-as-read (mark)
10698   "Mark the current article quickly as read with MARK."
10699   (let ((article (gnus-summary-article-number)))
10700     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10701     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10702     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10703     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10704     (push (cons article mark) gnus-newsgroup-reads)
10705     ;; Possibly remove from cache, if that is used.
10706     (when gnus-use-cache
10707       (gnus-cache-enter-remove-article article))
10708     ;; Allow the backend to change the mark.
10709     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10710     ;; Check for auto-expiry.
10711     (when (and gnus-newsgroup-auto-expire
10712                (memq mark gnus-auto-expirable-marks))
10713       (setq mark gnus-expirable-mark)
10714       ;; Let the backend know about the mark change.
10715       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10716       (push article gnus-newsgroup-expirable))
10717     ;; Set the mark in the buffer.
10718     (gnus-summary-update-mark mark 'unread)
10719     t))
10720
10721 (defun gnus-summary-mark-article-as-unread (mark)
10722   "Mark the current article quickly as unread with MARK."
10723   (let* ((article (gnus-summary-article-number))
10724          (old-mark (gnus-summary-article-mark article)))
10725     ;; Allow the backend to change the mark.
10726     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10727     (if (eq mark old-mark)
10728         t
10729       (if (<= article 0)
10730           (progn
10731             (gnus-error 1 "Can't mark negative article numbers")
10732             nil)
10733         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10734         (setq gnus-newsgroup-spam-marked
10735               (delq article gnus-newsgroup-spam-marked))
10736         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10737         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10738         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10739         (cond ((= mark gnus-ticked-mark)
10740                (setq gnus-newsgroup-marked
10741                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10742                                               article)))
10743               ((= mark gnus-spam-mark)
10744                (setq gnus-newsgroup-spam-marked
10745                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10746                                               article)))
10747               ((= mark gnus-dormant-mark)
10748                (setq gnus-newsgroup-dormant
10749                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10750                                               article)))
10751               (t
10752                (setq gnus-newsgroup-unreads
10753                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10754                                               article))))
10755         (gnus-alist-pull article gnus-newsgroup-reads)
10756
10757         ;; See whether the article is to be put in the cache.
10758         (and gnus-use-cache
10759              (vectorp (gnus-summary-article-header article))
10760              (save-excursion
10761                (gnus-cache-possibly-enter-article
10762                 gnus-newsgroup-name article
10763                 (= mark gnus-ticked-mark)
10764                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10765
10766         ;; Fix the mark.
10767         (gnus-summary-update-mark mark 'unread)
10768         t))))
10769
10770 (defun gnus-summary-mark-article (&optional article mark no-expire)
10771   "Mark ARTICLE with MARK.  MARK can be any character.
10772 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10773 `??' (dormant) and `?E' (expirable).
10774 If MARK is nil, then the default character `?r' is used.
10775 If ARTICLE is nil, then the article on the current line will be
10776 marked.
10777 If NO-EXPIRE, auto-expiry will be inhibited."
10778   ;; The mark might be a string.
10779   (when (stringp mark)
10780     (setq mark (aref mark 0)))
10781   ;; If no mark is given, then we check auto-expiring.
10782   (when (null mark)
10783     (setq mark gnus-del-mark))
10784   (when (and (not no-expire)
10785              gnus-newsgroup-auto-expire
10786              (memq mark gnus-auto-expirable-marks))
10787     (setq mark gnus-expirable-mark))
10788   (let ((article (or article (gnus-summary-article-number)))
10789         (old-mark (gnus-summary-article-mark article)))
10790     ;; Allow the backend to change the mark.
10791     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10792     (if (eq mark old-mark)
10793         t
10794       (unless article
10795         (error "No article on current line"))
10796       (if (not (if (or (= mark gnus-unread-mark)
10797                        (= mark gnus-ticked-mark)
10798                        (= mark gnus-spam-mark)
10799                        (= mark gnus-dormant-mark))
10800                    (gnus-mark-article-as-unread article mark)
10801                  (gnus-mark-article-as-read article mark)))
10802           t
10803         ;; See whether the article is to be put in the cache.
10804         (and gnus-use-cache
10805              (not (= mark gnus-canceled-mark))
10806              (vectorp (gnus-summary-article-header article))
10807              (save-excursion
10808                (gnus-cache-possibly-enter-article
10809                 gnus-newsgroup-name article
10810                 (= mark gnus-ticked-mark)
10811                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10812
10813         (when (gnus-summary-goto-subject article nil t)
10814           (let ((inhibit-read-only t))
10815             (gnus-summary-show-thread)
10816             ;; Fix the mark.
10817             (gnus-summary-update-mark mark 'unread)
10818             t))))))
10819
10820 (defun gnus-summary-update-secondary-mark (article)
10821   "Update the secondary (read, process, cache) mark."
10822   (gnus-summary-update-mark
10823    (cond ((memq article gnus-newsgroup-processable)
10824           gnus-process-mark)
10825          ((memq article gnus-newsgroup-cached)
10826           gnus-cached-mark)
10827          ((memq article gnus-newsgroup-replied)
10828           gnus-replied-mark)
10829          ((memq article gnus-newsgroup-forwarded)
10830           gnus-forwarded-mark)
10831          ((memq article gnus-newsgroup-saved)
10832           gnus-saved-mark)
10833          ((memq article gnus-newsgroup-recent)
10834           gnus-recent-mark)
10835          ((memq article gnus-newsgroup-unseen)
10836           gnus-unseen-mark)
10837          (t gnus-no-mark))
10838    'replied)
10839   (when (gnus-visual-p 'summary-highlight 'highlight)
10840     (gnus-summary-highlight-line)
10841     (gnus-run-hooks 'gnus-summary-update-hook))
10842   t)
10843
10844 (defun gnus-summary-update-download-mark (article)
10845   "Update the download mark."
10846   (gnus-summary-update-mark
10847    (cond ((memq article gnus-newsgroup-undownloaded)
10848           gnus-undownloaded-mark)
10849          (gnus-newsgroup-agentized
10850           gnus-downloaded-mark)
10851          (t
10852           gnus-no-mark))
10853    'download)
10854   (gnus-summary-update-line t)
10855   t)
10856
10857 (defun gnus-summary-update-mark (mark type)
10858   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10859         (inhibit-read-only t))
10860     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10861     (when forward
10862       (when (looking-at "\r")
10863         (incf forward))
10864       (when (<= (+ forward (point)) (point-max))
10865         ;; Go to the right position on the line.
10866         (goto-char (+ forward (point)))
10867         ;; Replace the old mark with the new mark.
10868         (let ((to-insert
10869                (mm-subst-char-in-string
10870                 (char-after) mark
10871                 (buffer-substring (point) (1+ (point))))))
10872           (delete-region (point) (1+ (point)))
10873           (insert to-insert))
10874         ;; Optionally update the marks by some user rule.
10875         (when (eq type 'unread)
10876           (gnus-data-set-mark
10877            (gnus-data-find (gnus-summary-article-number)) mark)
10878           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10879
10880 (defun gnus-mark-article-as-read (article &optional mark)
10881   "Enter ARTICLE in the pertinent lists and remove it from others."
10882   ;; Make the article expirable.
10883   (let ((mark (or mark gnus-del-mark)))
10884     (setq gnus-newsgroup-expirable
10885           (if (= mark gnus-expirable-mark)
10886               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10887             (delq article gnus-newsgroup-expirable)))
10888     ;; Remove from unread and marked lists.
10889     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10890     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10891     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10892     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10893     (push (cons article mark) gnus-newsgroup-reads)
10894     ;; Possibly remove from cache, if that is used.
10895     (when gnus-use-cache
10896       (gnus-cache-enter-remove-article article))
10897     t))
10898
10899 (defun gnus-mark-article-as-unread (article &optional mark)
10900   "Enter ARTICLE in the pertinent lists and remove it from others."
10901   (let ((mark (or mark gnus-ticked-mark)))
10902     (if (<= article 0)
10903         (progn
10904           (gnus-error 1 "Can't mark negative article numbers")
10905           nil)
10906       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10907             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10908             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10909             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10910             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10911
10912       ;; Unsuppress duplicates?
10913       (when gnus-suppress-duplicates
10914         (gnus-dup-unsuppress-article article))
10915
10916       (cond ((= mark gnus-ticked-mark)
10917              (setq gnus-newsgroup-marked
10918                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10919             ((= mark gnus-spam-mark)
10920              (setq gnus-newsgroup-spam-marked
10921                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10922                                             article)))
10923             ((= mark gnus-dormant-mark)
10924              (setq gnus-newsgroup-dormant
10925                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10926             (t
10927              (setq gnus-newsgroup-unreads
10928                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10929       (gnus-alist-pull article gnus-newsgroup-reads)
10930       t)))
10931
10932 (defun gnus-summary-tick-article-forward (n)
10933   "Tick N articles forwards.
10934 If N is negative, tick backwards instead.
10935 The difference between N and the number of articles ticked is returned."
10936   (interactive "p")
10937   (gnus-summary-mark-forward n gnus-ticked-mark))
10938
10939 (defun gnus-summary-tick-article-backward (n)
10940   "Tick N articles backwards.
10941 The difference between N and the number of articles ticked is returned."
10942   (interactive "p")
10943   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10944
10945 (defun gnus-summary-tick-article (&optional article clear-mark)
10946   "Mark current article as unread.
10947 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10948 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10949   (interactive)
10950   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10951                                        gnus-ticked-mark)))
10952
10953 (defun gnus-summary-mark-as-read-forward (n)
10954   "Mark N articles as read forwards.
10955 If N is negative, mark backwards instead.
10956 The difference between N and the actual number of articles marked is
10957 returned."
10958   (interactive "p")
10959   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10960
10961 (defun gnus-summary-mark-as-read-backward (n)
10962   "Mark the N articles as read backwards.
10963 The difference between N and the actual number of articles marked is
10964 returned."
10965   (interactive "p")
10966   (gnus-summary-mark-forward
10967    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10968
10969 (defun gnus-summary-mark-as-read (&optional article mark)
10970   "Mark current article as read.
10971 ARTICLE specifies the article to be marked as read.
10972 MARK specifies a string to be inserted at the beginning of the line."
10973   (gnus-summary-mark-article article mark))
10974
10975 (defun gnus-summary-clear-mark-forward (n)
10976   "Clear marks from N articles forward.
10977 If N is negative, clear backward instead.
10978 The difference between N and the number of marks cleared is returned."
10979   (interactive "p")
10980   (gnus-summary-mark-forward n gnus-unread-mark))
10981
10982 (defun gnus-summary-clear-mark-backward (n)
10983   "Clear marks from N articles backward.
10984 The difference between N and the number of marks cleared is returned."
10985   (interactive "p")
10986   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10987
10988 (defun gnus-summary-mark-unread-as-read ()
10989   "Intended to be used by `gnus-mark-article-hook'."
10990   (when (memq gnus-current-article gnus-newsgroup-unreads)
10991     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10992
10993 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10994   "Intended to be used by `gnus-mark-article-hook'."
10995   (let ((mark (gnus-summary-article-mark)))
10996     (when (or (gnus-unread-mark-p mark)
10997               (gnus-read-mark-p mark))
10998       (gnus-summary-mark-article gnus-current-article
10999                                  (or new-mark gnus-read-mark)))))
11000
11001 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11002   "Intended to be used by `gnus-mark-article-hook'."
11003   (let ((mark (gnus-summary-article-mark)))
11004     (when (or (gnus-unread-mark-p mark)
11005               (gnus-read-mark-p mark))
11006       (gnus-summary-mark-article (gnus-summary-article-number)
11007                                  (or new-mark gnus-read-mark)))))
11008
11009 (defun gnus-summary-mark-unread-as-ticked ()
11010   "Intended to be used by `gnus-mark-article-hook'."
11011   (when (memq gnus-current-article gnus-newsgroup-unreads)
11012     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11013
11014 (defun gnus-summary-mark-region-as-read (point mark all)
11015   "Mark all unread articles between point and mark as read.
11016 If given a prefix, mark all articles between point and mark as read,
11017 even ticked and dormant ones."
11018   (interactive "r\nP")
11019   (save-excursion
11020     (let (article)
11021       (goto-char point)
11022       (beginning-of-line)
11023       (while (and
11024               (< (point) mark)
11025               (progn
11026                 (when (or all
11027                           (memq (setq article (gnus-summary-article-number))
11028                                 gnus-newsgroup-unreads))
11029                   (gnus-summary-mark-article article gnus-del-mark))
11030                 t)
11031               (gnus-summary-find-next))))))
11032
11033 (defun gnus-summary-mark-below (score mark)
11034   "Mark articles with score less than SCORE with MARK."
11035   (interactive "P\ncMark: ")
11036   (setq score (if score
11037                   (prefix-numeric-value score)
11038                 (or gnus-summary-default-score 0)))
11039   (with-current-buffer gnus-summary-buffer
11040     (goto-char (point-min))
11041     (while
11042         (progn
11043           (and (< (gnus-summary-article-score) score)
11044                (gnus-summary-mark-article nil mark))
11045           (gnus-summary-find-next)))))
11046
11047 (defun gnus-summary-kill-below (&optional score)
11048   "Mark articles with score below SCORE as read."
11049   (interactive "P")
11050   (gnus-summary-mark-below score gnus-killed-mark))
11051
11052 (defun gnus-summary-clear-above (&optional score)
11053   "Clear all marks from articles with score above SCORE."
11054   (interactive "P")
11055   (gnus-summary-mark-above score gnus-unread-mark))
11056
11057 (defun gnus-summary-tick-above (&optional score)
11058   "Tick all articles with score above SCORE."
11059   (interactive "P")
11060   (gnus-summary-mark-above score gnus-ticked-mark))
11061
11062 (defun gnus-summary-mark-above (score mark)
11063   "Mark articles with score over SCORE with MARK."
11064   (interactive "P\ncMark: ")
11065   (setq score (if score
11066                   (prefix-numeric-value score)
11067                 (or gnus-summary-default-score 0)))
11068   (with-current-buffer gnus-summary-buffer
11069     (goto-char (point-min))
11070     (while (and (progn
11071                   (when (> (gnus-summary-article-score) score)
11072                     (gnus-summary-mark-article nil mark))
11073                   t)
11074                 (gnus-summary-find-next)))))
11075
11076 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11077 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11078 (defun gnus-summary-limit-include-expunged (&optional no-error)
11079   "Display all the hidden articles that were expunged for low scores."
11080   (interactive)
11081   (let ((inhibit-read-only t))
11082     (let ((scored gnus-newsgroup-scored)
11083           headers h)
11084       (while scored
11085         (unless (gnus-summary-article-header (caar scored))
11086           (and (setq h (gnus-number-to-header (caar scored)))
11087                (< (cdar scored) gnus-summary-expunge-below)
11088                (push h headers)))
11089         (setq scored (cdr scored)))
11090       (if (not headers)
11091           (when (not no-error)
11092             (error "No expunged articles hidden"))
11093         (goto-char (point-min))
11094         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11095         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11096         (dolist (x headers)
11097           (push (mail-header-number x) gnus-newsgroup-limit))
11098         (gnus-summary-prepare-unthreaded (nreverse headers))
11099         (goto-char (point-min))
11100         (gnus-summary-position-point)
11101         t))))
11102
11103 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11104   "Mark all unread articles in this newsgroup as read.
11105 If prefix argument ALL is non-nil, ticked and dormant articles will
11106 also be marked as read.
11107 If QUIETLY is non-nil, no questions will be asked.
11108
11109 If TO-HERE is non-nil, it should be a point in the buffer.  All
11110 articles before (after, if REVERSE is set) this point will be marked
11111 as read.
11112
11113 Note that this function will only catch up the unread article
11114 in the current summary buffer limitation.
11115
11116 The number of articles marked as read is returned."
11117   (interactive "P")
11118   (prog1
11119       (save-excursion
11120         (when (or quietly
11121                   (not gnus-interactive-catchup) ;Without confirmation?
11122                   gnus-expert-user
11123                   (gnus-y-or-n-p
11124                    (if all
11125                        "Mark absolutely all articles as read? "
11126                      "Mark all unread articles as read? ")))
11127           (if (and not-mark
11128                    (not gnus-newsgroup-adaptive)
11129                    (not gnus-newsgroup-auto-expire)
11130                    (not gnus-suppress-duplicates)
11131                    (or (not gnus-use-cache)
11132                        (eq gnus-use-cache 'passive)))
11133               (progn
11134                 (when all
11135                   (setq gnus-newsgroup-marked nil
11136                         gnus-newsgroup-spam-marked nil
11137                         gnus-newsgroup-dormant nil))
11138                 (setq gnus-newsgroup-unreads
11139                       (gnus-sorted-nunion
11140                        (gnus-sorted-intersection gnus-newsgroup-unreads
11141                                                  gnus-newsgroup-downloadable)
11142                        (gnus-sorted-difference gnus-newsgroup-unfetched
11143                                                gnus-newsgroup-cached))))
11144             ;; We actually mark all articles as canceled, which we
11145             ;; have to do when using auto-expiry or adaptive scoring.
11146             (gnus-summary-show-all-threads)
11147             (if (and to-here reverse)
11148                 (progn
11149                   (goto-char to-here)
11150                   (gnus-summary-mark-current-read-and-unread-as-read
11151                    gnus-catchup-mark)
11152                   (while (gnus-summary-find-next (not all))
11153                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11154               (when (gnus-summary-first-subject (not all))
11155                 (while (and
11156                         (if to-here (< (point) to-here) t)
11157                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11158                         (gnus-summary-find-next (not all))))))
11159             (gnus-set-mode-line 'summary))
11160           t))
11161     (gnus-summary-position-point)))
11162
11163 (defun gnus-summary-catchup-to-here (&optional all)
11164   "Mark all unticked articles before the current one as read.
11165 If ALL is non-nil, also mark ticked and dormant articles as read."
11166   (interactive "P")
11167   (save-excursion
11168     (gnus-save-hidden-threads
11169       (let ((beg (point)))
11170         ;; We check that there are unread articles.
11171         (when (or all (gnus-summary-find-prev))
11172           (gnus-summary-catchup all t beg)))))
11173   (gnus-summary-position-point))
11174
11175 (defun gnus-summary-catchup-from-here (&optional all)
11176   "Mark all unticked articles after (and including) the current one as read.
11177 If ALL is non-nil, also mark ticked and dormant articles as read."
11178   (interactive "P")
11179   (save-excursion
11180     (gnus-save-hidden-threads
11181       (let ((beg (point)))
11182         ;; We check that there are unread articles.
11183         (when (or all (gnus-summary-find-next))
11184           (gnus-summary-catchup all t beg nil t)))))
11185   (gnus-summary-position-point))
11186
11187 (defun gnus-summary-catchup-all (&optional quietly)
11188   "Mark all articles in this newsgroup as read.
11189 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11190 instead, which marks only unread articles as read."
11191   (interactive "P")
11192   (gnus-summary-catchup t quietly))
11193
11194 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11195   "Mark all unread articles in this group as read, then exit.
11196 If prefix argument ALL is non-nil, all articles are marked as read.
11197 If QUIETLY is non-nil, no questions will be asked."
11198   (interactive "P")
11199   (when (gnus-summary-catchup all quietly nil 'fast)
11200     ;; Select next newsgroup or exit.
11201     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11202              (eq gnus-auto-select-next 'quietly))
11203         (gnus-summary-next-group nil)
11204       (gnus-summary-exit))))
11205
11206 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11207   "Mark all articles in this newsgroup as read, and then exit.
11208 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11209 instead, which marks only unread articles as read."
11210   (interactive "P")
11211   (gnus-summary-catchup-and-exit t quietly))
11212
11213 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11214   "Mark all articles in this group as read and select the next group.
11215 If given a prefix, mark all articles, unread as well as ticked, as
11216 read."
11217   (interactive "P")
11218   (save-excursion
11219     (gnus-summary-catchup all))
11220   (gnus-summary-next-group))
11221
11222 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11223   "Mark all articles in this group as read and select the previous group.
11224 If given a prefix, mark all articles, unread as well as ticked, as
11225 read."
11226   (interactive "P")
11227   (save-excursion
11228     (gnus-summary-catchup all))
11229   (gnus-summary-next-group nil nil t))
11230
11231 ;;;
11232 ;;; with article
11233 ;;;
11234
11235 (defmacro gnus-with-article (article &rest forms)
11236   "Select ARTICLE and perform FORMS in the original article buffer.
11237 Then replace the article with the result."
11238   `(progn
11239      ;; We don't want the article to be marked as read.
11240      (let (gnus-mark-article-hook)
11241        (gnus-summary-select-article t t nil ,article))
11242      (set-buffer gnus-original-article-buffer)
11243      ,@forms
11244      (if (not (gnus-check-backend-function
11245                'request-replace-article (car gnus-article-current)))
11246          (gnus-message 5 "Read-only group; not replacing")
11247        (unless (gnus-request-replace-article
11248                 ,article (car gnus-article-current)
11249                 (current-buffer) t)
11250          (error "Couldn't replace article")))
11251      ;; The cache and backlog have to be flushed somewhat.
11252      (when gnus-keep-backlog
11253        (gnus-backlog-remove-article
11254         (car gnus-article-current) (cdr gnus-article-current)))
11255      (when gnus-use-cache
11256        (gnus-cache-update-article
11257         (car gnus-article-current) (cdr gnus-article-current)))))
11258
11259 (put 'gnus-with-article 'lisp-indent-function 1)
11260 (put 'gnus-with-article 'edebug-form-spec '(form body))
11261
11262 ;; Thread-based commands.
11263
11264 (defun gnus-summary-articles-in-thread (&optional article)
11265   "Return a list of all articles in the current thread.
11266 If ARTICLE is non-nil, return all articles in the thread that starts
11267 with that article."
11268   (let* ((article (or article (gnus-summary-article-number)))
11269          (data (gnus-data-find-list article))
11270          (top-level (gnus-data-level (car data)))
11271          (top-subject
11272           (cond ((null gnus-thread-operation-ignore-subject)
11273                  (gnus-simplify-subject-re
11274                   (mail-header-subject (gnus-data-header (car data)))))
11275                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11276                  (gnus-simplify-subject-fuzzy
11277                   (mail-header-subject (gnus-data-header (car data)))))
11278                 (t nil)))
11279          (end-point (save-excursion
11280                       (goto-char (gnus-data-pos (car data)))
11281                       (if (gnus-summary-go-to-next-thread)
11282                           (point) (point-max))))
11283          articles)
11284     (while (and data
11285                 (< (gnus-data-pos (car data)) end-point))
11286       (when (or (not top-subject)
11287                 (string= top-subject
11288                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11289                              (gnus-simplify-subject-fuzzy
11290                               (mail-header-subject
11291                                (gnus-data-header (car data))))
11292                            (gnus-simplify-subject-re
11293                             (mail-header-subject
11294                              (gnus-data-header (car data)))))))
11295         (push (gnus-data-number (car data)) articles))
11296       (unless (and (setq data (cdr data))
11297                    (> (gnus-data-level (car data)) top-level))
11298         (setq data nil)))
11299     ;; Return the list of articles.
11300     (nreverse articles)))
11301
11302 (defun gnus-summary-rethread-current ()
11303   "Rethread the thread the current article is part of."
11304   (interactive)
11305   (let* ((gnus-show-threads t)
11306          (article (gnus-summary-article-number))
11307          (id (mail-header-id (gnus-summary-article-header)))
11308          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11309     (unless id
11310       (error "No article on the current line"))
11311     (gnus-rebuild-thread id)
11312     (gnus-summary-goto-subject article)))
11313
11314 (defun gnus-summary-reparent-thread ()
11315   "Make the current article child of the marked (or previous) article.
11316
11317 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11318 is non-nil or the Subject: of both articles are the same."
11319   (interactive)
11320   (unless (not (gnus-group-read-only-p))
11321     (error "The current newsgroup does not support article editing"))
11322   (unless (<= (length gnus-newsgroup-processable) 1)
11323     (error "No more than one article may be marked"))
11324   (let ((child (gnus-summary-article-number))
11325         ;; First grab the marked article, otherwise one line up.
11326         (parent (if (not (null gnus-newsgroup-processable))
11327                     (car gnus-newsgroup-processable)
11328                   (save-excursion
11329                     (if (eq (forward-line -1) 0)
11330                         (gnus-summary-article-number)
11331                       (error "Beginning of summary buffer"))))))
11332     (gnus-summary-reparent-children parent (list child))))
11333
11334 (defun gnus-summary-reparent-children (parent children)
11335   "Make PARENT the parent of CHILDREN.
11336 When called interactively, PARENT is the current article and CHILDREN
11337 are the process-marked articles."
11338   (interactive
11339    (list (gnus-summary-article-number)
11340          (gnus-summary-work-articles nil)))
11341   (dolist (child children)
11342     (save-window-excursion
11343       (let ((gnus-article-buffer " *reparent*"))
11344         (unless (not (eq parent child))
11345           (error "An article may not be self-referential"))
11346         (let ((message-id (mail-header-id
11347                            (gnus-summary-article-header parent))))
11348           (unless (and message-id (not (equal message-id "")))
11349             (error "No message-id in desired parent"))
11350           (gnus-with-article child
11351             (save-restriction
11352               (goto-char (point-min))
11353               (message-narrow-to-head)
11354               (if (re-search-forward "^References: " nil t)
11355                   (progn
11356                     (re-search-forward "^[^ \t]" nil t)
11357                     (forward-line -1)
11358                     (end-of-line)
11359                     (insert " " message-id))
11360                 (insert "References: " message-id "\n"))))
11361           (set-buffer gnus-summary-buffer)
11362           (gnus-summary-unmark-all-processable)
11363           (gnus-summary-update-article child)
11364           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11365             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11366           (gnus-summary-rethread-current)
11367           (gnus-message 3 "Article %d is now the child of article %d"
11368                         child parent))))))
11369
11370 (defun gnus-summary-toggle-threads (&optional arg)
11371   "Toggle showing conversation threads.
11372 If ARG is positive number, turn showing conversation threads on."
11373   (interactive "P")
11374   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11375     (setq gnus-show-threads
11376           (if (null arg) (not gnus-show-threads)
11377             (> (prefix-numeric-value arg) 0)))
11378     (gnus-summary-prepare)
11379     (gnus-summary-goto-subject current)
11380     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11381     (gnus-summary-position-point)))
11382
11383 (eval-and-compile
11384   (if (fboundp 'remove-overlays)
11385       (defalias 'gnus-remove-overlays 'remove-overlays)
11386     (defun gnus-remove-overlays (beg end name val)
11387       "Clear BEG and END of overlays whose property NAME has value VAL.
11388 For compatibility with XEmacs."
11389       (dolist (ov (gnus-overlays-in beg end))
11390         (when (eq (gnus-overlay-get ov name) val)
11391           (gnus-delete-overlay ov))))))
11392
11393 (defun gnus-summary-show-all-threads ()
11394   "Show all threads."
11395   (interactive)
11396   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11397   (gnus-summary-position-point))
11398
11399 (defsubst gnus-summary--inv (p)
11400   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11401
11402 (defun gnus-summary-show-thread ()
11403   "Show thread subtrees.
11404 Returns nil if no thread was there to be shown."
11405   (interactive)
11406   (let* ((orig (point))
11407          (end (point-at-eol))
11408          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11409          ;; Leave point at bol
11410          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11411          (eoi (when end
11412                 (if (fboundp 'next-single-char-property-change)
11413                     (or (next-single-char-property-change end 'invisible)
11414                         (point-max))
11415                   (while (progn
11416                            (end-of-line 2)
11417                            (and (not (eobp))
11418                                 (eq (get-char-property (point) 'invisible)
11419                                     'gnus-sum))))
11420                   (point)))))
11421     (when eoi
11422       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11423       (goto-char orig)
11424       (gnus-summary-position-point)
11425       eoi)))
11426
11427 (defun gnus-summary-maybe-hide-threads ()
11428   "If requested, hide the threads that should be hidden."
11429   (when (and gnus-show-threads
11430              gnus-thread-hide-subtree)
11431     (gnus-summary-hide-all-threads
11432      (if (or (consp gnus-thread-hide-subtree)
11433              (functionp gnus-thread-hide-subtree))
11434          (gnus-make-predicate gnus-thread-hide-subtree)
11435        nil))))
11436
11437 ;;; Hiding predicates.
11438
11439 (defun gnus-article-unread-p (header)
11440   (memq (mail-header-number header) gnus-newsgroup-unreads))
11441
11442 (defun gnus-article-unseen-p (header)
11443   (memq (mail-header-number header) gnus-newsgroup-unseen))
11444
11445 (defun gnus-map-articles (predicate articles)
11446   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11447   (apply 'gnus-or (mapcar predicate
11448                           (mapcar (lambda (number)
11449                                     (gnus-summary-article-header number))
11450                                   articles))))
11451
11452 (defun gnus-summary-hide-all-threads (&optional predicate)
11453   "Hide all thread subtrees.
11454 If PREDICATE is supplied, threads that satisfy this predicate
11455 will not be hidden."
11456   (interactive)
11457   (save-excursion
11458     (goto-char (point-min))
11459     (let ((end nil))
11460       (while (not end)
11461         (when (or (not predicate)
11462                   (gnus-map-articles
11463                    predicate (gnus-summary-article-children)))
11464             (gnus-summary-hide-thread))
11465         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11466   (gnus-summary-position-point))
11467
11468 (defun gnus-summary-hide-thread ()
11469   "Hide thread subtrees.
11470 If PREDICATE is supplied, threads that satisfy this predicate
11471 will not be hidden.
11472 Returns nil if no threads were there to be hidden."
11473   (interactive)
11474   (let ((start (point))
11475         (starteol (line-end-position))
11476         (article (gnus-summary-article-number)))
11477     (goto-char start)
11478     ;; Go forward until either the buffer ends or the subthread ends.
11479     (when (and (not (eobp))
11480                (or (zerop (gnus-summary-next-thread 1 t))
11481                    (goto-char (point-max))))
11482       (if (and (> (point) start)
11483                ;; FIXME: this should actually search for a non-invisible \n.
11484                (search-backward "\n" start t))
11485           (progn
11486             (when (> (point) starteol)
11487               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11488               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11489                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11490                 (gnus-overlay-put ol 'evaporate t)))
11491             (gnus-summary-goto-subject article))
11492         (goto-char start)
11493         nil))))
11494
11495 (defun gnus-summary-go-to-next-thread (&optional previous)
11496   "Go to the same level (or less) next thread.
11497 If PREVIOUS is non-nil, go to previous thread instead.
11498 Return the article number moved to, or nil if moving was impossible."
11499   (let ((level (gnus-summary-thread-level))
11500         (way (if previous -1 1))
11501         (beg (point)))
11502     (forward-line way)
11503     (while (and (not (eobp))
11504                 (< level (gnus-summary-thread-level)))
11505       (forward-line way))
11506     (if (eobp)
11507         (progn
11508           (goto-char beg)
11509           nil)
11510       (setq beg (point))
11511       (prog1
11512           (gnus-summary-article-number)
11513         (goto-char beg)))))
11514
11515 (defun gnus-summary-next-thread (n &optional silent)
11516   "Go to the same level next N'th thread.
11517 If N is negative, search backward instead.
11518 Returns the difference between N and the number of skips actually
11519 done.
11520
11521 If SILENT, don't output messages."
11522   (interactive "p")
11523   (let ((backward (< n 0))
11524         (n (abs n)))
11525     (while (and (> n 0)
11526                 (gnus-summary-go-to-next-thread backward))
11527       (decf n))
11528     (unless silent
11529       (gnus-summary-position-point))
11530     (when (and (not silent) (/= 0 n))
11531       (gnus-message 7 "No more threads"))
11532     n))
11533
11534 (defun gnus-summary-prev-thread (n)
11535   "Go to the same level previous N'th thread.
11536 Returns the difference between N and the number of skips actually
11537 done."
11538   (interactive "p")
11539   (gnus-summary-next-thread (- n)))
11540
11541 (defun gnus-summary-go-down-thread ()
11542   "Go down one level in the current thread."
11543   (let ((children (gnus-summary-article-children)))
11544     (when children
11545       (gnus-summary-goto-subject (car children)))))
11546
11547 (defun gnus-summary-go-up-thread ()
11548   "Go up one level in the current thread."
11549   (let ((parent (gnus-summary-article-parent)))
11550     (when parent
11551       (gnus-summary-goto-subject parent))))
11552
11553 (defun gnus-summary-down-thread (n)
11554   "Go down thread N steps.
11555 If N is negative, go up instead.
11556 Returns the difference between N and how many steps down that were
11557 taken."
11558   (interactive "p")
11559   (let ((up (< n 0))
11560         (n (abs n)))
11561     (while (and (> n 0)
11562                 (if up (gnus-summary-go-up-thread)
11563                   (gnus-summary-go-down-thread)))
11564       (setq n (1- n)))
11565     (gnus-summary-position-point)
11566     (when (/= 0 n)
11567       (gnus-message 7 "Can't go further"))
11568     n))
11569
11570 (defun gnus-summary-up-thread (n)
11571   "Go up thread N steps.
11572 If N is negative, go down instead.
11573 Returns the difference between N and how many steps down that were
11574 taken."
11575   (interactive "p")
11576   (gnus-summary-down-thread (- n)))
11577
11578 (defun gnus-summary-top-thread ()
11579   "Go to the top of the thread."
11580   (interactive)
11581   (while (gnus-summary-go-up-thread))
11582   (gnus-summary-article-number))
11583
11584 (defun gnus-summary-expire-thread ()
11585   "Mark articles under current thread as expired."
11586   (interactive)
11587   (gnus-summary-kill-thread 0))
11588
11589 (defun gnus-summary-kill-thread (&optional unmark)
11590   "Mark articles under current thread as read.
11591 If the prefix argument is positive, remove any kinds of marks.
11592 If the prefix argument is zero, mark thread as expired.
11593 If the prefix argument is negative, tick articles instead."
11594   (interactive "P")
11595   (when unmark
11596     (setq unmark (prefix-numeric-value unmark)))
11597   (let ((articles (gnus-summary-articles-in-thread))
11598         (hide (or (null unmark) (= unmark 0))))
11599     (save-excursion
11600       ;; Expand the thread.
11601       (gnus-summary-show-thread)
11602       ;; Mark all the articles.
11603       (while articles
11604         (gnus-summary-goto-subject (car articles))
11605         (cond ((null unmark)
11606                (gnus-summary-mark-article-as-read gnus-killed-mark))
11607               ((> unmark 0)
11608                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11609               ((= unmark 0)
11610                (gnus-summary-mark-article nil gnus-expirable-mark))
11611               (t
11612                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11613         (setq articles (cdr articles))))
11614     ;; Hide killed subtrees when hide is true.
11615     (and hide
11616          gnus-thread-hide-killed
11617          (gnus-summary-hide-thread))
11618     ;; If hide is t, go to next unread subject.
11619     (when hide
11620       ;; Go to next unread subject.
11621       (gnus-summary-next-subject 1 t)))
11622   (gnus-set-mode-line 'summary))
11623
11624 ;; Summary sorting commands
11625
11626 (defun gnus-summary-sort-by-number (&optional reverse)
11627   "Sort the summary buffer by article number.
11628 Argument REVERSE means reverse order."
11629   (interactive "P")
11630   (gnus-summary-sort 'number reverse))
11631
11632 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11633   "Sort the summary buffer by most recent article number.
11634 Argument REVERSE means reverse order."
11635   (interactive "P")
11636   (gnus-summary-sort 'most-recent-number reverse))
11637
11638 (defun gnus-summary-sort-by-random (&optional reverse)
11639   "Randomize the order in the summary buffer.
11640 Argument REVERSE means to randomize in reverse order."
11641   (interactive "P")
11642   (gnus-summary-sort 'random reverse))
11643
11644 (defun gnus-summary-sort-by-author (&optional reverse)
11645   "Sort the summary buffer by author name alphabetically.
11646 If `case-fold-search' is non-nil, case of letters is ignored.
11647 Argument REVERSE means reverse order."
11648   (interactive "P")
11649   (gnus-summary-sort 'author reverse))
11650
11651 (defun gnus-summary-sort-by-recipient (&optional reverse)
11652   "Sort the summary buffer by recipient name alphabetically.
11653 If `case-fold-search' is non-nil, case of letters is ignored.
11654 Argument REVERSE means reverse order."
11655   (interactive "P")
11656   (gnus-summary-sort 'recipient reverse))
11657
11658 (defun gnus-summary-sort-by-subject (&optional reverse)
11659   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11660 If `case-fold-search' is non-nil, case of letters is ignored.
11661 Argument REVERSE means reverse order."
11662   (interactive "P")
11663   (gnus-summary-sort 'subject reverse))
11664
11665 (defun gnus-summary-sort-by-date (&optional reverse)
11666   "Sort the summary buffer by date.
11667 Argument REVERSE means reverse order."
11668   (interactive "P")
11669   (gnus-summary-sort 'date reverse))
11670
11671 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11672   "Sort the summary buffer by most recent date.
11673 Argument REVERSE means reverse order."
11674   (interactive "P")
11675   (gnus-summary-sort 'most-recent-date reverse))
11676
11677 (defun gnus-summary-sort-by-score (&optional reverse)
11678   "Sort the summary buffer by score.
11679 Argument REVERSE means reverse order."
11680   (interactive "P")
11681   (gnus-summary-sort 'score reverse))
11682
11683 (defun gnus-summary-sort-by-lines (&optional reverse)
11684   "Sort the summary buffer by the number of lines.
11685 Argument REVERSE means reverse order."
11686   (interactive "P")
11687   (gnus-summary-sort 'lines reverse))
11688
11689 (defun gnus-summary-sort-by-chars (&optional reverse)
11690   "Sort the summary buffer by article length.
11691 Argument REVERSE means reverse order."
11692   (interactive "P")
11693   (gnus-summary-sort 'chars reverse))
11694
11695 (defun gnus-summary-sort-by-original (&optional reverse)
11696   "Sort the summary buffer using the default sorting method.
11697 Argument REVERSE means reverse order."
11698   (interactive "P")
11699   (let* ((inhibit-read-only t)
11700          (gnus-summary-prepare-hook nil))
11701     ;; We do the sorting by regenerating the threads.
11702     (gnus-summary-prepare)
11703     ;; Hide subthreads if needed.
11704     (gnus-summary-maybe-hide-threads)))
11705
11706 (defun gnus-summary-sort (predicate reverse)
11707   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11708   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11709          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11710          (gnus-thread-sort-functions
11711           (if (not reverse)
11712               thread
11713             `(lambda (t1 t2)
11714                (,thread t2 t1))))
11715          (gnus-sort-gathered-threads-function
11716           gnus-thread-sort-functions)
11717          (gnus-article-sort-functions
11718           (if (not reverse)
11719               article
11720             `(lambda (t1 t2)
11721                (,article t2 t1))))
11722          (inhibit-read-only t)
11723          (gnus-summary-prepare-hook nil))
11724     ;; We do the sorting by regenerating the threads.
11725     (gnus-summary-prepare)
11726     ;; Hide subthreads if needed.
11727     (gnus-summary-maybe-hide-threads)))
11728
11729 ;; Summary saving commands.
11730
11731 (defun gnus-summary-save-article (&optional n not-saved)
11732   "Save the current article using the default saver function.
11733 If N is a positive number, save the N next articles.
11734 If N is a negative number, save the N previous articles.
11735 If N is nil and any articles have been marked with the process mark,
11736 save those articles instead.
11737 The variable `gnus-default-article-saver' specifies the saver function.
11738
11739 If the optional second argument NOT-SAVED is non-nil, articles saved
11740 will not be marked as saved."
11741   (interactive "P")
11742   (require 'gnus-art)
11743   (let* ((articles (gnus-summary-work-articles n))
11744          (save-buffer (save-excursion
11745                         (nnheader-set-temp-buffer " *Gnus Save*")))
11746          (num (length articles))
11747          ;; Whether to save decoded articles or raw articles.
11748          (decode (when gnus-article-save-coding-system
11749                    (get gnus-default-article-saver :decode)))
11750          ;; When saving many articles in a single file, use the other
11751          ;; function to save articles other than the first one.
11752          (saver2 (get gnus-default-article-saver :function))
11753          (gnus-prompt-before-saving (if saver2
11754                                         t
11755                                       gnus-prompt-before-saving))
11756          (gnus-default-article-saver gnus-default-article-saver)
11757          header file)
11758     (dolist (article articles)
11759       (setq header (gnus-summary-article-header article))
11760       (if (not (vectorp header))
11761           ;; This is a pseudo-article.
11762           (if (assq 'name header)
11763               (gnus-copy-file (cdr (assq 'name header)))
11764             (gnus-message 1 "Article %d is unsaveable" article))
11765         ;; This is a real article.
11766         (save-window-excursion
11767           (gnus-summary-select-article decode decode nil article)
11768           (gnus-summary-goto-subject article))
11769         (with-current-buffer save-buffer
11770           (erase-buffer)
11771           (insert-buffer-substring (if decode
11772                                        gnus-article-buffer
11773                                      gnus-original-article-buffer)))
11774         (setq file (gnus-article-save save-buffer file num))
11775         (gnus-summary-remove-process-mark article)
11776         (unless not-saved
11777           (gnus-summary-set-saved-mark article)))
11778       (when saver2
11779         (setq gnus-default-article-saver saver2
11780               saver2 nil)))
11781     (gnus-kill-buffer save-buffer)
11782     (gnus-summary-position-point)
11783     (gnus-set-mode-line 'summary)
11784     n))
11785
11786 (defun gnus-summary-pipe-output (&optional n sym)
11787   "Pipe the current article to a subprocess.
11788 If N is a positive number, pipe the N next articles.
11789 If N is a negative number, pipe the N previous articles.
11790 If N is nil and any articles have been marked with the process mark,
11791 pipe those articles instead.
11792 The default command to which articles are piped is specified by the
11793 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11794 will be prompted for the command.
11795
11796 The properties `:decode' and `:headers' that are put to the function
11797 symbol `gnus-summary-save-in-pipe' control whether this function
11798 decodes articles and what headers to keep (see the doc string for the
11799 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11800 is neither omitted nor the symbol `r', force including all headers
11801 regardless of the `:headers' property.  If it is the symbol `r',
11802 articles that are not decoded and include all headers will be piped
11803 no matter what the properties `:decode' and `:headers' are."
11804   (interactive (gnus-interactive "P\ny"))
11805   (require 'gnus-art)
11806   (let* ((articles (gnus-summary-work-articles n))
11807          (result-buffer "*Shell Command Output*")
11808          (all-headers (not (memq sym '(nil r))))
11809          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11810          (raw (eq sym 'r))
11811          (headers (get 'gnus-summary-save-in-pipe :headers))
11812          command result)
11813     (unless (numberp (car articles))
11814       (error "No article to pipe"))
11815     (setq command (gnus-read-shell-command
11816                    (concat "Shell command on "
11817                            (if (cdr articles)
11818                                (format "these %d articles" (length articles))
11819                              "this article")
11820                            ": ")
11821                    gnus-summary-pipe-output-default-command))
11822     (when (string-equal command "")
11823       (error "A command is required"))
11824     (when all-headers
11825       (put 'gnus-summary-save-in-pipe :headers nil))
11826     (unwind-protect
11827         (while articles
11828           (gnus-summary-goto-subject (pop articles))
11829           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11830           (when (and (get-buffer result-buffer)
11831                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11832             (setq result (concat result (with-current-buffer result-buffer
11833                                           (buffer-string))))))
11834       (put 'gnus-summary-save-in-pipe :headers headers))
11835     (unless (zerop (length result))
11836       (if (with-current-buffer (get-buffer-create result-buffer)
11837             (erase-buffer)
11838             (insert result)
11839             (prog1
11840                 (and (= (count-lines (point-min) (point)) 1)
11841                      (progn
11842                        (end-of-line 0)
11843                        (<= (current-column)
11844                            (window-width (minibuffer-window)))))
11845               (goto-char (point-min))))
11846           (message "%s" (substring result 0 -1))
11847         (message nil)
11848         (gnus-configure-windows 'pipe)))))
11849
11850 (defun gnus-summary-save-article-mail (&optional arg)
11851   "Append the current article to a Unix mail box file.
11852 If N is a positive number, save the N next articles.
11853 If N is a negative number, save the N previous articles.
11854 If N is nil and any articles have been marked with the process mark,
11855 save those articles instead."
11856   (interactive "P")
11857   (require 'gnus-art)
11858   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11859     (gnus-summary-save-article arg)))
11860
11861 (defun gnus-summary-save-article-rmail (&optional arg)
11862   "Append the current article to an rmail file.
11863 If N is a positive number, save the N next articles.
11864 If N is a negative number, save the N previous articles.
11865 If N is nil and any articles have been marked with the process mark,
11866 save those articles instead."
11867   (interactive "P")
11868   (require 'gnus-art)
11869   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11870     (gnus-summary-save-article arg)))
11871
11872 (defun gnus-summary-save-article-file (&optional arg)
11873   "Append the current article to a file.
11874 If N is a positive number, save the N next articles.
11875 If N is a negative number, save the N previous articles.
11876 If N is nil and any articles have been marked with the process mark,
11877 save those articles instead."
11878   (interactive "P")
11879   (require 'gnus-art)
11880   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11881     (gnus-summary-save-article arg)))
11882
11883 (defun gnus-summary-write-article-file (&optional arg)
11884   "Write the current article to a file, deleting the previous file.
11885 If N is a positive number, save the N next articles.
11886 If N is a negative number, save the N previous articles.
11887 If N is nil and any articles have been marked with the process mark,
11888 save those articles instead."
11889   (interactive "P")
11890   (require 'gnus-art)
11891   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11892     (gnus-summary-save-article arg)))
11893
11894 (defun gnus-summary-save-article-body-file (&optional arg)
11895   "Append the current article body to a file.
11896 If N is a positive number, save the N next articles.
11897 If N is a negative number, save the N previous articles.
11898 If N is nil and any articles have been marked with the process mark,
11899 save those articles instead."
11900   (interactive "P")
11901   (require 'gnus-art)
11902   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11903     (gnus-summary-save-article arg)))
11904
11905 (defun gnus-summary-write-article-body-file (&optional arg)
11906   "Write the current article body to a file, deleting the previous file.
11907 If N is a positive number, save the N next articles.
11908 If N is a negative number, save the N previous articles.
11909 If N is nil and any articles have been marked with the process mark,
11910 save those articles instead."
11911   (interactive "P")
11912   (require 'gnus-art)
11913   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11914     (gnus-summary-save-article arg)))
11915
11916 (defun gnus-summary-muttprint (&optional arg)
11917   "Print the current article using Muttprint.
11918 If N is a positive number, save the N next articles.
11919 If N is a negative number, save the N previous articles.
11920 If N is nil and any articles have been marked with the process mark,
11921 save those articles instead."
11922   (interactive "P")
11923   (require 'gnus-art)
11924   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11925     (gnus-summary-save-article arg t)))
11926
11927 (defun gnus-summary-pipe-message (program)
11928   "Pipe the current article through PROGRAM."
11929   (interactive "sProgram: ")
11930   (gnus-summary-select-article)
11931   (let ((mail-header-separator ""))
11932     (gnus-eval-in-buffer-window gnus-article-buffer
11933       (save-restriction
11934         (widen)
11935         (let ((start (window-start))
11936               (inhibit-read-only t))
11937           (message-pipe-buffer-body program)
11938           (set-window-start (get-buffer-window (current-buffer)) start))))))
11939
11940 (defun gnus-get-split-value (methods)
11941   "Return a value based on the split METHODS."
11942   (let (split-name method result match)
11943     (when methods
11944       (with-current-buffer gnus-original-article-buffer
11945         (save-restriction
11946           (nnheader-narrow-to-headers)
11947           (while (and methods (not split-name))
11948             (goto-char (point-min))
11949             (setq method (pop methods))
11950             (setq match (car method))
11951             (when (cond
11952                    ((stringp match)
11953                     ;; Regular expression.
11954                     (ignore-errors
11955                       (re-search-forward match nil t)))
11956                    ((functionp match)
11957                     ;; Function.
11958                     (save-restriction
11959                       (widen)
11960                       (setq result (funcall match gnus-newsgroup-name))))
11961                    ((consp match)
11962                     ;; Form.
11963                     (save-restriction
11964                       (widen)
11965                       (setq result (eval match)))))
11966               (setq split-name (cdr method))
11967               (cond ((stringp result)
11968                      (push (expand-file-name
11969                             result gnus-article-save-directory)
11970                            split-name))
11971                     ((consp result)
11972                      (setq split-name (append result split-name)))))))))
11973     (nreverse split-name)))
11974
11975 (defun gnus-valid-move-group-p (group)
11976   (and (symbolp group)
11977        (boundp group)
11978        (symbol-name group)
11979        (symbol-value group)
11980        (gnus-get-function (gnus-find-method-for-group
11981                            (symbol-name group)) 'request-accept-article t)))
11982
11983 (defun gnus-read-move-group-name (prompt default articles prefix)
11984   "Read a group name."
11985   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11986          (minibuffer-confirm-incomplete nil) ; XEmacs
11987          (prom
11988           (format "%s %s to"
11989                   prompt
11990                   (if (> (length articles) 1)
11991                       (format "these %d articles" (length articles))
11992                     "this article")))
11993          (to-newsgroup
11994           (cond
11995            ((null 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 nil default))
12000            ((= 1 (length split-name))
12001             (gnus-group-completing-read
12002              prom
12003              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12004              nil prefix 'gnus-group-history (car split-name)))
12005            (t
12006             (gnus-completing-read
12007              prom (nreverse split-name) nil nil 'gnus-group-history))))
12008          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12009          encoded)
12010     (when to-newsgroup
12011       (if (or (string= to-newsgroup "")
12012               (string= to-newsgroup prefix))
12013           (setq to-newsgroup default))
12014       (unless to-newsgroup
12015         (error "No group name entered"))
12016       (setq encoded (mm-encode-coding-string
12017                      to-newsgroup
12018                      (gnus-group-name-charset to-method to-newsgroup)))
12019       (or (gnus-active encoded)
12020           (gnus-activate-group encoded nil nil to-method)
12021           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12022                                      to-newsgroup))
12023               (or (and (gnus-request-create-group encoded to-method)
12024                        (gnus-activate-group encoded nil nil to-method)
12025                        (gnus-subscribe-group encoded))
12026                   (error "Couldn't create group %s" to-newsgroup)))
12027           (error "No such group: %s" to-newsgroup))
12028       encoded)))
12029
12030 (defvar gnus-summary-save-parts-counter)
12031 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12032
12033 (defun gnus-summary-save-parts (type dir n &optional reverse)
12034   "Save parts matching TYPE to DIR.
12035 If REVERSE, save parts that do not match TYPE."
12036   (interactive
12037    (list (read-string "Save parts of type: "
12038                       (or (car gnus-summary-save-parts-type-history)
12039                           gnus-summary-save-parts-default-mime)
12040                       'gnus-summary-save-parts-type-history)
12041          (setq gnus-summary-save-parts-last-directory
12042                (read-file-name "Save to directory: "
12043                                gnus-summary-save-parts-last-directory
12044                                nil t))
12045          current-prefix-arg))
12046   (gnus-summary-iterate n
12047     (let ((gnus-display-mime-function nil)
12048           gnus-article-prepare-hook
12049           gnus-article-decode-hook
12050           gnus-display-mime-function
12051           gnus-break-pages
12052           (gnus-inhibit-treatment t))
12053       (gnus-summary-select-article))
12054     (with-current-buffer gnus-article-buffer
12055       (let ((handles (or gnus-article-mime-handles
12056                          (mm-dissect-buffer nil gnus-article-loose-mime)
12057                          (and gnus-article-emulate-mime
12058                               (mm-uu-dissect))))
12059             (gnus-summary-save-parts-counter 1))
12060         (when handles
12061           (gnus-summary-save-parts-1 type dir handles reverse)
12062           (unless gnus-article-mime-handles ;; Don't destroy this case.
12063             (mm-destroy-parts handles)))))))
12064
12065 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12066   (if (stringp (car handle))
12067       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12068               (cdr handle))
12069     (when (if reverse
12070               (not (string-match type (mm-handle-media-type handle)))
12071             (string-match type (mm-handle-media-type handle)))
12072       (let ((file (expand-file-name
12073                    (gnus-map-function
12074                     mm-file-name-rewrite-functions
12075                     (file-name-nondirectory
12076                      (or
12077                       (mail-content-type-get
12078                        (mm-handle-disposition handle) 'filename)
12079                       (mail-content-type-get
12080                        (mm-handle-type handle) 'name)
12081                       (format "%s.%d.%d" gnus-newsgroup-name
12082                               (cdr gnus-article-current)
12083                               gnus-summary-save-parts-counter))))
12084                    dir)))
12085         (incf gnus-summary-save-parts-counter)
12086         (unless (file-exists-p file)
12087           (mm-save-part-to-file handle file))))))
12088
12089 ;; Summary extract commands
12090
12091 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12092   (let ((inhibit-read-only t)
12093         (article (gnus-summary-article-number))
12094         after-article b e)
12095     (unless (gnus-summary-goto-subject article)
12096       (error "No such article: %d" article))
12097     (gnus-summary-position-point)
12098     ;; If all commands are to be bunched up on one line, we collect
12099     ;; them here.
12100     (unless gnus-view-pseudos-separately
12101       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12102             files action)
12103         (while ps
12104           (setq action (cdr (assq 'action (car ps))))
12105           (setq files (list (cdr (assq 'name (car ps)))))
12106           (while (and ps (cdr ps)
12107                       (string= (or action "1")
12108                                (or (cdr (assq 'action (cadr ps))) "2")))
12109             (push (cdr (assq 'name (cadr ps))) files)
12110             (setcdr ps (cddr ps)))
12111           (when files
12112             (when (not (string-match "%s" action))
12113               (push " " files))
12114             (push " " files)
12115             (when (assq 'execute (car ps))
12116               (setcdr (assq 'execute (car ps))
12117                       (funcall (if (string-match "%s" action)
12118                                    'format 'concat)
12119                                action
12120                                (mapconcat
12121                                 (lambda (f)
12122                                   (if (equal f " ")
12123                                       f
12124                                     (shell-quote-argument f)))
12125                                 files " ")))))
12126           (setq ps (cdr ps)))))
12127     (if (and gnus-view-pseudos (not not-view))
12128         (while pslist
12129           (when (assq 'execute (car pslist))
12130             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12131                                   (eq gnus-view-pseudos 'not-confirm)))
12132           (setq pslist (cdr pslist)))
12133       (save-excursion
12134         (while pslist
12135           (setq after-article (or (cdr (assq 'article (car pslist)))
12136                                   (gnus-summary-article-number)))
12137           (gnus-summary-goto-subject after-article)
12138           (forward-line 1)
12139           (setq b (point))
12140           (insert "    " (file-name-nondirectory
12141                           (cdr (assq 'name (car pslist))))
12142                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12143           (setq e (point))
12144           (forward-line -1)             ; back to `b'
12145           (gnus-add-text-properties
12146            b (1- e) (list 'gnus-number gnus-reffed-article-number
12147                           gnus-mouse-face-prop gnus-mouse-face))
12148           (gnus-data-enter
12149            after-article gnus-reffed-article-number
12150            gnus-unread-mark b (car pslist) 0 (- e b))
12151           (setq gnus-newsgroup-unreads
12152                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12153                                          gnus-reffed-article-number))
12154           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12155           (setq pslist (cdr pslist)))))))
12156
12157 (defun gnus-pseudos< (p1 p2)
12158   (let ((c1 (cdr (assq 'action p1)))
12159         (c2 (cdr (assq 'action p2))))
12160     (and c1 c2 (string< c1 c2))))
12161
12162 (defun gnus-request-pseudo-article (props)
12163   (cond ((assq 'execute props)
12164          (gnus-execute-command (cdr (assq 'execute props)))))
12165   (let ((gnus-current-article (gnus-summary-article-number)))
12166     (gnus-run-hooks 'gnus-mark-article-hook)))
12167
12168 (defun gnus-execute-command (command &optional automatic)
12169   (save-excursion
12170     (gnus-article-setup-buffer)
12171     (set-buffer gnus-article-buffer)
12172     (setq buffer-read-only nil)
12173     (let ((command (if automatic command
12174                      (read-string "Command: " (cons command 0)))))
12175       (erase-buffer)
12176       (insert "$ " command "\n\n")
12177       (if gnus-view-pseudo-asynchronously
12178           (start-process "gnus-execute" (current-buffer) shell-file-name
12179                          shell-command-switch command)
12180         (call-process shell-file-name nil t nil
12181                       shell-command-switch command)))))
12182
12183 ;; Summary kill commands.
12184
12185 (defun gnus-summary-edit-global-kill (article)
12186   "Edit the \"global\" kill file."
12187   (interactive (list (gnus-summary-article-number)))
12188   (gnus-group-edit-global-kill article))
12189
12190 (defun gnus-summary-edit-local-kill ()
12191   "Edit a local kill file applied to the current newsgroup."
12192   (interactive)
12193   (setq gnus-current-headers (gnus-summary-article-header))
12194   (gnus-group-edit-local-kill
12195    (gnus-summary-article-number) gnus-newsgroup-name))
12196
12197 ;;; Header reading.
12198
12199 (defun gnus-read-header (id &optional header)
12200   "Read the headers of article ID and enter them into the Gnus system."
12201   (let ((group gnus-newsgroup-name)
12202         (gnus-override-method
12203          (or
12204           gnus-override-method
12205           (and (gnus-news-group-p gnus-newsgroup-name)
12206                (car (gnus-refer-article-methods)))))
12207         where)
12208     ;; First we check to see whether the header in question is already
12209     ;; fetched.
12210     (if (stringp id)
12211         ;; This is a Message-ID.
12212         (setq header (or header (gnus-id-to-header id)))
12213       ;; This is an article number.
12214       (setq header (or header (gnus-summary-article-header id))))
12215     (if (and header
12216              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12217         ;; We have found the header.
12218         header
12219       ;; We have to really fetch the header to this article.
12220       (with-current-buffer nntp-server-buffer
12221         (when (setq where (gnus-request-head id group))
12222           (nnheader-fold-continuation-lines)
12223           (goto-char (point-max))
12224           (insert ".\n")
12225           (goto-char (point-min))
12226           (insert "211 ")
12227           (princ (cond
12228                   ((numberp id) id)
12229                   ((cdr where) (cdr where))
12230                   (header (mail-header-number header))
12231                   (t gnus-reffed-article-number))
12232                  (current-buffer))
12233           (insert " Article retrieved.\n"))
12234         (if (or (not where)
12235                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12236             ()                          ; Malformed head.
12237           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12238             (when (and (stringp id)
12239                        (or
12240                         (not (string= (gnus-group-real-name group)
12241                                       (car where)))
12242                         (not (gnus-server-equal gnus-override-method
12243                                                 (gnus-group-method group)))))
12244               ;; If we fetched by Message-ID and the article came from
12245               ;; a different group (or server), we fudge some bogus
12246               ;; article numbers for this article.
12247               (mail-header-set-number header gnus-reffed-article-number))
12248             (with-current-buffer gnus-summary-buffer
12249               (decf gnus-reffed-article-number)
12250               (gnus-remove-header (mail-header-number header))
12251               (push header gnus-newsgroup-headers)
12252               (setq gnus-current-headers header)
12253               (push (mail-header-number header) gnus-newsgroup-limit)))
12254           header)))))
12255
12256 (defun gnus-remove-header (number)
12257   "Remove header NUMBER from `gnus-newsgroup-headers'."
12258   (if (and gnus-newsgroup-headers
12259            (= number (mail-header-number (car gnus-newsgroup-headers))))
12260       (pop gnus-newsgroup-headers)
12261     (let ((headers gnus-newsgroup-headers))
12262       (while (and (cdr headers)
12263                   (not (= number (mail-header-number (cadr headers)))))
12264         (pop headers))
12265       (when (cdr headers)
12266         (setcdr headers (cddr headers))))))
12267
12268 ;;;
12269 ;;; summary highlights
12270 ;;;
12271
12272 (defun gnus-highlight-selected-summary ()
12273   "Highlight selected article in summary buffer."
12274   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12275   (when gnus-summary-selected-face
12276     (save-excursion
12277       (let* ((beg (point-at-bol))
12278              (end (point-at-eol))
12279              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12280              (from (if (get-text-property beg gnus-mouse-face-prop)
12281                        beg
12282                      (or (next-single-property-change
12283                           beg gnus-mouse-face-prop nil end)
12284                          beg)))
12285              (to
12286               (if (= from end)
12287                   (- from 2)
12288                 (or (next-single-property-change
12289                      from gnus-mouse-face-prop nil end)
12290                     end))))
12291         ;; If no mouse-face prop on line we will have to = from = end,
12292         ;; so we highlight the entire line instead.
12293         (when (= (+ to 2) from)
12294           (setq from beg)
12295           (setq to end))
12296         (if gnus-newsgroup-selected-overlay
12297             ;; Move old overlay.
12298             (gnus-move-overlay
12299              gnus-newsgroup-selected-overlay from to (current-buffer))
12300           ;; Create new overlay.
12301           (gnus-overlay-put
12302            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12303            'face gnus-summary-selected-face))))))
12304
12305 (defvar gnus-summary-highlight-line-cached nil)
12306 (defvar gnus-summary-highlight-line-trigger nil)
12307
12308 (defun gnus-summary-highlight-line-0 ()
12309   (if (and (eq gnus-summary-highlight-line-trigger
12310                gnus-summary-highlight)
12311            gnus-summary-highlight-line-cached)
12312       gnus-summary-highlight-line-cached
12313     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12314           gnus-summary-highlight-line-cached
12315           (let* ((cond (list 'cond))
12316                  (c cond)
12317                  (list gnus-summary-highlight))
12318             (while list
12319               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12320                               nil))
12321               (setq c (cdr c)
12322                     list (cdr list)))
12323             (gnus-byte-compile (list 'lambda nil cond))))))
12324
12325 (defun gnus-summary-highlight-line ()
12326   "Highlight current line according to `gnus-summary-highlight'."
12327   (let* ((beg (point-at-bol))
12328          (article (or (gnus-summary-article-number) gnus-current-article))
12329          (score (or (cdr (assq article
12330                                gnus-newsgroup-scored))
12331                     gnus-summary-default-score 0))
12332          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12333          (inhibit-read-only t)
12334          (default gnus-summary-default-score)
12335          (default-high gnus-summary-default-high-score)
12336          (default-low gnus-summary-default-low-score)
12337          (uncached (and gnus-summary-use-undownloaded-faces
12338                         (memq article gnus-newsgroup-undownloaded)
12339                         (not (memq article gnus-newsgroup-cached)))))
12340     (let ((face (funcall (gnus-summary-highlight-line-0))))
12341       (unless (eq face (get-text-property beg 'face))
12342         (gnus-put-text-property-excluding-characters-with-faces
12343          beg (point-at-eol) 'face
12344          (setq face (if (boundp face) (symbol-value face) face)))
12345         (when gnus-summary-highlight-line-function
12346           (funcall gnus-summary-highlight-line-function article face))))))
12347
12348 (defun gnus-update-read-articles (group unread &optional compute)
12349   "Update the list of read articles in GROUP.
12350 UNREAD is a sorted list."
12351   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12352         (info (gnus-get-info group))
12353         (prev 1)
12354         read)
12355     (if (or (not info) (not active))
12356         ;; There is no info on this group if it was, in fact,
12357         ;; killed.  Gnus stores no information on killed groups, so
12358         ;; there's nothing to be done.
12359         ;; One could store the information somewhere temporarily,
12360         ;; perhaps...  Hmmm...
12361         ()
12362       ;; Remove any negative articles numbers.
12363       (while (and unread (< (car unread) 0))
12364         (setq unread (cdr unread)))
12365       ;; Remove any expired article numbers
12366       (while (and unread (< (car unread) (car active)))
12367         (setq unread (cdr unread)))
12368       ;; Compute the ranges of read articles by looking at the list of
12369       ;; unread articles.
12370       (while unread
12371         (when (/= (car unread) prev)
12372           (push (if (= prev (1- (car unread))) prev
12373                   (cons prev (1- (car unread))))
12374                 read))
12375         (setq prev (1+ (car unread)))
12376         (setq unread (cdr unread)))
12377       (when (<= prev (cdr active))
12378         (push (cons prev (cdr active)) read))
12379       (setq read (if (> (length read) 1) (nreverse read) read))
12380       (if compute
12381           read
12382         (save-excursion
12383           (let (setmarkundo)
12384             ;; Propagate the read marks to the backend.
12385             (when (and gnus-propagate-marks
12386                        (gnus-check-backend-function 'request-set-mark group))
12387               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12388                     (add (gnus-remove-from-range read (gnus-info-read info))))
12389                 (when (or add del)
12390                   (unless (gnus-check-group group)
12391                     (error "Can't open server for %s" group))
12392                   (gnus-request-set-mark
12393                    group (delq nil (list (if add (list add 'add '(read)))
12394                                          (if del (list del 'del '(read))))))
12395                   (setq setmarkundo
12396                         `(gnus-request-set-mark
12397                           ,group
12398                           ',(delq nil (list
12399                                        (if del (list del 'add '(read)))
12400                                        (if add (list add 'del '(read))))))))))
12401             (set-buffer gnus-group-buffer)
12402             (gnus-undo-register
12403               `(progn
12404                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12405                  (gnus-info-set-read ',info ',(gnus-info-read info))
12406                  (gnus-get-unread-articles-in-group ',info
12407                                                     (gnus-active ,group))
12408                  (gnus-group-update-group ,group t)
12409                  ,setmarkundo))))
12410         ;; Enter this list into the group info.
12411         (gnus-info-set-read info read)
12412         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12413         (gnus-get-unread-articles-in-group info (gnus-active group))
12414         t))))
12415
12416 (defun gnus-offer-save-summaries ()
12417   "Offer to save all active summary buffers."
12418   (let (buffers)
12419     ;; Go through all buffers and find all summaries.
12420     (dolist (buffer (buffer-list))
12421       (when (and (setq buffer (buffer-name buffer))
12422                  (string-match "Summary" buffer)
12423                  (with-current-buffer buffer
12424                    ;; We check that this is, indeed, a summary buffer.
12425                    (and (eq major-mode 'gnus-summary-mode)
12426                         ;; Also make sure this isn't bogus.
12427                         gnus-newsgroup-prepared
12428                         ;; Also make sure that this isn't a
12429                         ;; dead summary buffer.
12430                         (not gnus-dead-summary-mode))))
12431         (push buffer buffers)))
12432     ;; Go through all these summary buffers and offer to save them.
12433     (when buffers
12434       (save-excursion
12435         (map-y-or-n-p
12436          "Update summary buffer %s? "
12437          (lambda (buf)
12438            (switch-to-buffer buf)
12439            (gnus-summary-exit))
12440          buffers)))))
12441
12442 (defun gnus-summary-setup-default-charset ()
12443   "Setup newsgroup default charset."
12444   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12445       (setq gnus-newsgroup-charset nil)
12446     (let* ((ignored-charsets
12447             (or gnus-newsgroup-ephemeral-ignored-charsets
12448                 (append
12449                  (and gnus-newsgroup-name
12450                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12451                  gnus-newsgroup-ignored-charsets))))
12452       (setq gnus-newsgroup-charset
12453             (or gnus-newsgroup-ephemeral-charset
12454                 (and gnus-newsgroup-name
12455                      (gnus-parameter-charset gnus-newsgroup-name))
12456                 gnus-default-charset))
12457       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12458            ignored-charsets))))
12459
12460 ;;;
12461 ;;; Mime Commands
12462 ;;;
12463
12464 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12465   "Display the current article buffer fully MIME-buttonized.
12466 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12467 treated as multipart/mixed."
12468   (interactive "P")
12469   (require 'gnus-art)
12470   (let ((gnus-unbuttonized-mime-types nil)
12471         (gnus-mime-display-multipart-as-mixed show-all-parts))
12472     (gnus-summary-show-article)))
12473
12474 (defun gnus-summary-repair-multipart (article)
12475   "Add a Content-Type header to a multipart article without one."
12476   (interactive (list (gnus-summary-article-number)))
12477   (gnus-with-article article
12478     (message-narrow-to-head)
12479     (message-remove-header "Mime-Version")
12480     (goto-char (point-max))
12481     (insert "Mime-Version: 1.0\n")
12482     (widen)
12483     (when (search-forward "\n--" nil t)
12484       (let ((separator (buffer-substring (point) (point-at-eol))))
12485         (message-narrow-to-head)
12486         (message-remove-header "Content-Type")
12487         (goto-char (point-max))
12488         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12489                         separator))
12490         (widen))))
12491   (let (gnus-mark-article-hook)
12492     (gnus-summary-select-article t t nil article)))
12493
12494 (defun gnus-summary-toggle-display-buttonized ()
12495   "Toggle the buttonizing of the article buffer."
12496   (interactive)
12497   (require 'gnus-art)
12498   (if (setq gnus-inhibit-mime-unbuttonizing
12499             (not gnus-inhibit-mime-unbuttonizing))
12500       (let ((gnus-unbuttonized-mime-types nil))
12501         (gnus-summary-show-article))
12502     (gnus-summary-show-article)))
12503
12504 ;;;
12505 ;;; Generic summary marking commands
12506 ;;;
12507
12508 (defvar gnus-summary-marking-alist
12509   '((read gnus-del-mark "d")
12510     (unread gnus-unread-mark "u")
12511     (ticked gnus-ticked-mark "!")
12512     (dormant gnus-dormant-mark "?")
12513     (expirable gnus-expirable-mark "e"))
12514   "An alist of names/marks/keystrokes.")
12515
12516 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12517 (defvar gnus-summary-mark-map)
12518
12519 (defun gnus-summary-make-all-marking-commands ()
12520   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12521   (dolist (elem gnus-summary-marking-alist)
12522     (apply 'gnus-summary-make-marking-command elem)))
12523
12524 (defun gnus-summary-make-marking-command (name mark keystroke)
12525   (let ((map (make-sparse-keymap)))
12526     (define-key gnus-summary-generic-mark-map keystroke map)
12527     (dolist (lway `((next "next" next nil "n")
12528                     (next-unread "next unread" next t "N")
12529                     (prev "previous" prev nil "p")
12530                     (prev-unread "previous unread" prev t "P")
12531                     (nomove "" nil nil ,keystroke)))
12532       (let ((func (gnus-summary-make-marking-command-1
12533                    mark (car lway) lway name)))
12534         (setq func (eval func))
12535         (define-key map (nth 4 lway) func)))))
12536
12537 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12538   `(defun ,(intern
12539             (format "gnus-summary-put-mark-as-%s%s"
12540                     name (if (eq way 'nomove)
12541                              ""
12542                            (concat "-" (symbol-name way)))))
12543      (n)
12544      ,(format
12545        "Mark the current article as %s%s.
12546 If N, the prefix, then repeat N times.
12547 If N is negative, move in reverse order.
12548 The difference between N and the actual number of articles marked is
12549 returned."
12550        name (cadr lway))
12551      (interactive "p")
12552      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12553
12554 (defun gnus-summary-generic-mark (n mark move unread)
12555   "Mark N articles with MARK."
12556   (unless (eq major-mode 'gnus-summary-mode)
12557     (error "This command can only be used in the summary buffer"))
12558   (gnus-summary-show-thread)
12559   (let ((nummove
12560          (cond
12561           ((eq move 'next) 1)
12562           ((eq move 'prev) -1)
12563           (t 0))))
12564     (if (zerop nummove)
12565         (setq n 1)
12566       (when (< n 0)
12567         (setq n (abs n)
12568               nummove (* -1 nummove))))
12569     (while (and (> n 0)
12570                 (gnus-summary-mark-article nil mark)
12571                 (zerop (gnus-summary-next-subject nummove unread t)))
12572       (setq n (1- n)))
12573     (when (/= 0 n)
12574       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12575     (gnus-summary-recenter)
12576     (gnus-summary-position-point)
12577     (gnus-set-mode-line 'summary)
12578     n))
12579
12580 (defun gnus-summary-insert-articles (articles)
12581   (when (setq articles
12582               (gnus-sorted-difference articles
12583                                       (mapcar (lambda (h)
12584                                                 (mail-header-number h))
12585                                               gnus-newsgroup-headers)))
12586     (setq gnus-newsgroup-headers
12587           (gnus-merge 'list
12588                       gnus-newsgroup-headers
12589                       (gnus-fetch-headers articles)
12590                       'gnus-article-sort-by-number))
12591     ;; Suppress duplicates?
12592     (when gnus-suppress-duplicates
12593       (gnus-dup-suppress-articles))
12594
12595     (if (and gnus-fetch-old-headers
12596              (eq gnus-headers-retrieved-by 'nov))
12597         ;; We might want to build some more threads first.
12598         (if (eq gnus-fetch-old-headers 'invisible)
12599             (gnus-build-all-threads)
12600           (gnus-build-old-threads))
12601       ;; Mark the inserted articles that are unread as unread.
12602       (setq gnus-newsgroup-unreads
12603             (gnus-sorted-nunion
12604              gnus-newsgroup-unreads
12605              (gnus-sorted-nintersection
12606               (gnus-list-of-unread-articles gnus-newsgroup-name)
12607               articles)))
12608       ;; Mark the inserted articles as selected so that the information
12609       ;; of the marks having been changed by a user may be updated when
12610       ;; exiting this group.  See `gnus-summary-update-info'.
12611       (dolist (art articles)
12612         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12613     ;; Let the Gnus agent mark articles as read.
12614     (when gnus-agent
12615       (gnus-agent-get-undownloaded-list))
12616     ;; Remove list identifiers from subject
12617     (when gnus-list-identifiers
12618       (gnus-summary-remove-list-identifiers))
12619     ;; First and last article in this newsgroup.
12620     (when gnus-newsgroup-headers
12621       (setq gnus-newsgroup-begin
12622             (mail-header-number (car gnus-newsgroup-headers))
12623             gnus-newsgroup-end
12624             (mail-header-number
12625              (gnus-last-element gnus-newsgroup-headers))))
12626     (when gnus-use-scoring
12627       (gnus-possibly-score-headers))))
12628
12629 (defun gnus-summary-insert-old-articles (&optional all)
12630   "Insert all old articles in this group.
12631 If ALL is non-nil, already read articles become readable.
12632 If ALL is a number, fetch this number of articles."
12633   (interactive "P")
12634   (prog1
12635       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12636             older len)
12637         (setq older
12638               ;; Some nntp servers lie about their active range.  When
12639               ;; this happens, the active range can be in the millions.
12640               ;; Use a compressed range to avoid creating a huge list.
12641               (gnus-range-difference (list gnus-newsgroup-active) old))
12642         (setq len (gnus-range-length older))
12643         (cond
12644          ((null older) nil)
12645          ((numberp all)
12646           (if (< all len)
12647               (let ((older-range (nreverse older)))
12648                 (setq older nil)
12649
12650                 (while (> all 0)
12651                   (let* ((r (pop older-range))
12652                          (min (if (numberp r) r (car r)))
12653                          (max (if (numberp r) r (cdr r))))
12654                     (while (and (<= min max)
12655                                 (> all 0))
12656                       (push max older)
12657                       (setq all (1- all)
12658                             max (1- max))))))
12659             (setq older (gnus-uncompress-range older))))
12660          (all
12661           (setq older (gnus-uncompress-range older)))
12662          (t
12663           (when (and (numberp gnus-large-newsgroup)
12664                    (> len gnus-large-newsgroup))
12665               (let* ((cursor-in-echo-area nil)
12666                      (initial (gnus-parameter-large-newsgroup-initial
12667                                gnus-newsgroup-name))
12668                      (input
12669                       (read-string
12670                        (format
12671                         "How many articles from %s (%s %d): "
12672                         (gnus-group-decoded-name gnus-newsgroup-name)
12673                         (if initial "max" "default")
12674                         len)
12675                        (if initial
12676                            (cons (number-to-string initial)
12677                                  0)))))
12678                 (unless (string-match "^[ \t]*$" input)
12679                   (setq all (string-to-number input))
12680                   (if (< all len)
12681                       (let ((older-range (nreverse older)))
12682                         (setq older nil)
12683
12684                         (while (> all 0)
12685                           (let* ((r (pop older-range))
12686                                  (min (if (numberp r) r (car r)))
12687                                  (max (if (numberp r) r (cdr r))))
12688                             (while (and (<= min max)
12689                                         (> all 0))
12690                               (push max older)
12691                               (setq all (1- all)
12692                                     max (1- max))))))))))
12693           (setq older (gnus-uncompress-range older))))
12694         (if (not older)
12695             (message "No old news.")
12696           (gnus-summary-insert-articles older)
12697           (gnus-summary-limit (gnus-sorted-nunion old older))))
12698     (gnus-summary-position-point)))
12699
12700 (defun gnus-summary-insert-new-articles ()
12701   "Insert all new articles in this group."
12702   (interactive)
12703   (prog1
12704       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12705             (old-high gnus-newsgroup-highest)
12706             (nnmail-fetched-sources (list t))
12707             i new)
12708         (setq gnus-newsgroup-active
12709               (gnus-copy-sequence
12710                (gnus-activate-group gnus-newsgroup-name 'scan)))
12711         (setq i (cdr gnus-newsgroup-active)
12712               gnus-newsgroup-highest i)
12713         (while (> i old-high)
12714           (push i new)
12715           (decf i))
12716         (if (not new)
12717             (message "No gnus is bad news")
12718           (gnus-summary-insert-articles new)
12719           (setq gnus-newsgroup-unreads
12720                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12721           (gnus-summary-limit (gnus-sorted-nunion old new))))
12722     (gnus-summary-position-point)))
12723
12724 ;;; Bookmark support for Gnus.
12725 (declare-function bookmark-make-record-default
12726                   "bookmark" (&optional no-file no-context posn))
12727 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12728 (declare-function bookmark-default-handler "bookmark" (bmk))
12729 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12730 (defvar bookmark-yank-point)
12731 (defvar bookmark-current-buffer)
12732
12733 (defun gnus-summary-bookmark-make-record ()
12734   "Make a bookmark entry for a Gnus summary buffer."
12735   (let (pos buf)
12736     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12737       (save-restriction              ; FIXME is it necessary to widen?
12738         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12739       (setq buf "art") ; We are recording bookmark from article buffer.
12740       (setq bookmark-yank-point (point))
12741       (setq bookmark-current-buffer (current-buffer))
12742       (gnus-article-show-summary))      ; Go back in summary buffer.
12743     ;; We are now recording bookmark from summary buffer.
12744     (unless buf (setq buf "sum"))
12745     (let* ((subject (elt (gnus-summary-article-header) 1))
12746            (grp     (car gnus-article-current))
12747            (art     (cdr gnus-article-current))
12748            (head    (gnus-summary-article-header art))
12749            (id      (mail-header-id head)))
12750       `(,subject
12751         ,@(condition-case nil
12752               (bookmark-make-record-default 'no-file 'no-context pos)
12753             (wrong-number-of-arguments
12754              (bookmark-make-record-default 'point-only)))
12755         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12756         (group . ,grp) (article . ,art)
12757         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12758
12759 ;;;###autoload
12760 (defun gnus-summary-bookmark-jump (bookmark)
12761   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12762 BOOKMARK is a bookmark name or a bookmark record."
12763   (let ((group    (bookmark-prop-get bookmark 'group))
12764         (article  (bookmark-prop-get bookmark 'article))
12765         (id       (bookmark-prop-get bookmark 'message-id))
12766         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12767     (gnus-fetch-group group (list article))
12768     (gnus-summary-insert-cached-articles)
12769     (gnus-summary-goto-article id nil 'force)
12770     ;; FIXME we have to wait article buffer is ready (only large buffer)
12771     ;; Is there a better solution to know that?
12772     ;; If we don't wait `bookmark-default-handler' will have no chance
12773     ;; to set position. However there is no error, just wrong pos.
12774     (sit-for 1)
12775     (when (string= buf "Gnus-art")
12776       (other-window 1))
12777     (bookmark-default-handler
12778      `(""
12779        (buffer . ,(current-buffer))
12780        . ,(bookmark-get-bookmark-record bookmark)))))
12781
12782 (gnus-summary-make-all-marking-commands)
12783
12784 (gnus-ems-redefine)
12785
12786 (provide 'gnus-sum)
12787
12788 (run-hooks 'gnus-sum-load-hook)
12789
12790 ;; Local Variables:
12791 ;; coding: iso-8859-1
12792 ;; End:
12793
12794 ;;; gnus-sum.el ends here