(gnus-auto-select-on-ephemeral-exit): New variable.
[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 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 2, or (at your option)
14 ;; 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; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-mode))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'gmm-utils)
42 (require 'mm-decode)
43 (require 'nnoo)
44
45 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
46 (autoload 'gnus-cache-write-active "gnus-cache")
47 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
48 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
49 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
52   "Deuglify broken Outlook (Express) articles and redisplay."
53   t)
54 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
55 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
56 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
57
58 (defcustom gnus-kill-summary-on-exit t
59   "*If non-nil, kill the summary buffer when you exit from it.
60 If nil, the summary will become a \"*Dead Summary*\" buffer, and
61 it will be killed sometime later."
62   :group 'gnus-summary-exit
63   :type 'boolean)
64
65 (defcustom gnus-fetch-old-headers nil
66   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
67 If an unread article in the group refers to an older, already
68 read (or just marked as read) article, the old article will not
69 normally be displayed in the Summary buffer.  If this variable is
70 t, Gnus will attempt to grab the headers to the old articles, and
71 thereby build complete threads.  If it has the value `some', all
72 old headers will be fetched but only enough headers to connect
73 otherwise loose threads will be displayed.  This variable can
74 also be a number.  In that case, no more than that number of old
75 headers will be fetched.  If it has the value `invisible', all
76 old headers will be fetched, but none will be displayed.
77
78 The server has to support NOV for any of this to work.
79
80 This feature can seriously impact performance it ignores all
81 locally cached header entries."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const :tag "on" t)
85                  (const some)
86                  (const invisible)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 500
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :version "22.1"
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :version "22.1"
230   :group 'gnus-score-default
231   :type 'integer)
232
233 (defcustom gnus-summary-default-low-score 0
234   "*Default threshold for a low scored article.
235 An article will be highlighted as low scored if its score is smaller
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-zcore-fuzz 0
242   "*Fuzziness factor for the zcore in the summary buffer.
243 Articles with scores closer than this to `gnus-summary-default-score'
244 will not be marked."
245   :group 'gnus-summary-format
246   :type 'integer)
247
248 (defcustom gnus-simplify-subject-fuzzy-regexp nil
249   "*Strings to be removed when doing fuzzy matches.
250 This can either be a regular expression or list of regular expressions
251 that will be removed from subject strings if fuzzy subject
252 simplification is selected."
253   :group 'gnus-thread
254   :type '(repeat regexp))
255
256 (defcustom gnus-show-threads t
257   "*If non-nil, display threads in summary mode."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-hide-subtree nil
262   "*If non-nil, hide all threads initially.
263 This can be a predicate specifier which says which threads to hide.
264 If threads are hidden, you have to run the command
265 `gnus-summary-show-thread' by hand or select an article."
266   :group 'gnus-thread
267   :type '(radio (sexp :format "Non-nil\n"
268                       :match (lambda (widget value)
269                                (not (or (consp value) (functionp value))))
270                       :value t)
271                 (const nil)
272                 (sexp :tag "Predicate specifier")))
273
274 (defcustom gnus-thread-hide-killed t
275   "*If non-nil, hide killed threads automatically."
276   :group 'gnus-thread
277   :type 'boolean)
278
279 (defcustom gnus-thread-ignore-subject t
280   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
281 If nil, articles that have different subjects from their parents will
282 start separate threads."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-operation-ignore-subject t
287   "*If non-nil, subjects will be ignored when doing thread commands.
288 This affects commands like `gnus-summary-kill-thread' and
289 `gnus-summary-lower-thread'.
290
291 If this variable is nil, articles in the same thread with different
292 subjects will not be included in the operation in question.  If this
293 variable is `fuzzy', only articles that have subjects that are fuzzily
294 equal will be included."
295   :group 'gnus-thread
296   :type '(choice (const :tag "off" nil)
297                  (const fuzzy)
298                  (sexp :tag "on" t)))
299
300 (defcustom gnus-thread-indent-level 4
301   "*Number that says how much each sub-thread should be indented."
302   :group 'gnus-thread
303   :type 'integer)
304
305 (defcustom gnus-auto-extend-newsgroup t
306   "*If non-nil, extend newsgroup forward and backward when requested."
307   :group 'gnus-summary-choose
308   :type 'boolean)
309
310 (defcustom gnus-auto-select-first t
311   "If non-nil, select an article on group entry.
312 An article is selected automatically when entering a group
313 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
314 `gnus-summary-catchup-and-goto-next-group'.
315
316 Which article is selected is controlled by the variable
317 `gnus-auto-select-subject'.
318
319 If you want to prevent automatic selection of articles in some
320 newsgroups, set the variable to nil in `gnus-select-group-hook'."
321   ;; Commands include...
322   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
323   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
324   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
325   :group 'gnus-group-select
326   :type '(choice (const :tag "none" nil)
327                  (sexp :menu-tag "first" t)))
328
329 (defcustom gnus-auto-select-subject 'unread
330   "*Says what subject to place under point when entering a group.
331
332 This variable can either be the symbols `first' (place point on the
333 first subject), `unread' (place point on the subject line of the first
334 unread article), `best' (place point on the subject line of the
335 higest-scored article), `unseen' (place point on the subject line of
336 the first unseen article), `unseen-or-unread' (place point on the subject
337 line of the first unseen article or, if all article have been seen, on the
338 subject line of the first unread article), or a function to be called to
339 place point on some subject line."
340   :version "22.1"
341   :group 'gnus-group-select
342   :type '(choice (const best)
343                  (const unread)
344                  (const first)
345                  (const unseen)
346                  (const unseen-or-unread)))
347
348 (defcustom gnus-auto-select-next t
349   "*If non-nil, offer to go to the next group from the end of the previous.
350 If the value is t and the next newsgroup is empty, Gnus will exit
351 summary mode and go back to group mode.  If the value is neither nil
352 nor t, Gnus will select the following unread newsgroup.  In
353 particular, if the value is the symbol `quietly', the next unread
354 newsgroup will be selected without any confirmation, and if it is
355 `almost-quietly', the next group will be selected without any
356 confirmation if you are located on the last article in the group.
357 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
358 will go to the next group without confirmation."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "off" nil)
361                  (const quietly)
362                  (const almost-quietly)
363                  (const slightly-quietly)
364                  (sexp :menu-tag "on" t)))
365
366 (defcustom gnus-auto-select-same nil
367   "*If non-nil, select the next article with the same subject.
368 If there are no more articles with the same subject, go to
369 the first unread article."
370   :group 'gnus-summary-maneuvering
371   :type 'boolean)
372
373 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
374   "What article should be selected after exiting an ephemeral group.
375 Valid values include:
376
377 `next'
378   Select the next article.
379 `next-unread'
380   Select the next unread article.
381 `next-noselect'
382   Move the cursor to the next article.  This is the default.
383 `next-unread-noselect'
384   Move the cursor to the next unread article.
385
386 If it has any other value or there is no next (unread) article, the
387 article selected before entering to the ephemeral group will appear."
388   :version "23.0" ;; No Gnus
389   :group 'gnus-summary-maneuvering
390   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
391                  (const next) (const next-unread)
392                  (const next-noselect) (const next-unread-noselect)
393                  (sexp :tag "other" :value nil)))
394
395 (defcustom gnus-auto-goto-ignores 'unfetched
396   "*Says how to handle unfetched articles when maneuvering.
397
398 This variable can either be the symbols nil (maneuver to any
399 article), `undownloaded' (maneuvering while unplugged ignores articles
400 that have not been fetched), `always-undownloaded' (maneuvering always
401 ignores articles that have not been fetched), `unfetched' (maneuvering
402 ignores articles whose headers have not been fetched).
403
404 NOTE: The list of unfetched articles will always be nil when plugged
405 and, when unplugged, a subset of the undownloaded article list."
406   :version "22.1"
407   :group 'gnus-summary-maneuvering
408   :type '(choice (const :tag "None" nil)
409                  (const :tag "Undownloaded when unplugged" undownloaded)
410                  (const :tag "Undownloaded" always-undownloaded)
411                  (const :tag "Unfetched" unfetched)))
412
413 (defcustom gnus-summary-check-current nil
414   "*If non-nil, consider the current article when moving.
415 The \"unread\" movement commands will stay on the same line if the
416 current article is unread."
417   :group 'gnus-summary-maneuvering
418   :type 'boolean)
419
420 (defcustom gnus-auto-center-summary 2
421   "*If non-nil, always center the current summary buffer.
422 In particular, if `vertical' do only vertical recentering.  If non-nil
423 and non-`vertical', do both horizontal and vertical recentering."
424   :group 'gnus-summary-maneuvering
425   :type '(choice (const :tag "none" nil)
426                  (const vertical)
427                  (integer :tag "height")
428                  (sexp :menu-tag "both" t)))
429
430 (defvar gnus-auto-center-group t
431   "*If non-nil, always center the group buffer.")
432
433 (defcustom gnus-show-all-headers nil
434   "*If non-nil, don't hide any headers."
435   :group 'gnus-article-hiding
436   :group 'gnus-article-headers
437   :type 'boolean)
438
439 (defcustom gnus-summary-ignore-duplicates nil
440   "*If non-nil, ignore articles with identical Message-ID headers."
441   :group 'gnus-summary
442   :type 'boolean)
443
444 (defcustom gnus-single-article-buffer t
445   "*If non-nil, display all articles in the same buffer.
446 If nil, each group will get its own article buffer."
447   :group 'gnus-article-various
448   :type 'boolean)
449
450 (defcustom gnus-break-pages t
451   "*If non-nil, do page breaking on articles.
452 The page delimiter is specified by the `gnus-page-delimiter'
453 variable."
454   :group 'gnus-article-various
455   :type 'boolean)
456
457 (defcustom gnus-move-split-methods nil
458   "*Variable used to suggest where articles are to be moved to.
459 It uses the same syntax as the `gnus-split-methods' variable.
460 However, whereas `gnus-split-methods' specifies file names as targets,
461 this variable specifies group names."
462   :group 'gnus-summary-mail
463   :type '(repeat (choice (list :value (fun) function)
464                          (cons :value ("" "") regexp (repeat string))
465                          (sexp :value nil))))
466
467 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
468   "Function used to compute default prefix for article move/copy/etc prompts.
469 The function should take one argument, a group name, and return a
470 string with the suggested prefix."
471   :group 'gnus-summary-mail
472   :type 'function)
473
474 ;; FIXME: Although the custom type is `character' for the following variables,
475 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
476
477 (defcustom gnus-unread-mark ?           ;Whitespace
478   "*Mark used for unread articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-ticked-mark ?!
483   "*Mark used for ticked articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-dormant-mark ??
488   "*Mark used for dormant articles."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-del-mark ?r
493   "*Mark used for del'd articles."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-read-mark ?R
498   "*Mark used for read articles."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-expirable-mark ?E
503   "*Mark used for expirable articles."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-killed-mark ?K
508   "*Mark used for killed articles."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-spam-mark ?$
513   "*Mark used for spam articles."
514   :version "22.1"
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-souped-mark ?F
519   "*Mark used for souped articles."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-kill-file-mark ?X
524   "*Mark used for articles killed by kill files."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-low-score-mark ?Y
529   "*Mark used for articles with a low score."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-catchup-mark ?C
534   "*Mark used for articles that are caught up."
535   :group 'gnus-summary-marks
536   :type 'character)
537
538 (defcustom gnus-replied-mark ?A
539   "*Mark used for articles that have been replied to."
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-forwarded-mark ?F
544   "*Mark used for articles that have been forwarded."
545   :version "22.1"
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-recent-mark ?N
550   "*Mark used for articles that are recent."
551   :version "22.1"
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-cached-mark ?*
556   "*Mark used for articles that are in the cache."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-saved-mark ?S
561   "*Mark used for articles that have been saved."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-unseen-mark ?.
566   "*Mark used for articles that haven't been seen."
567   :version "22.1"
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-no-mark ?               ;Whitespace
572   "*Mark used for articles that have no other secondary mark."
573   :version "22.1"
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-ancient-mark ?O
578   "*Mark used for ancient articles."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-sparse-mark ?Q
583   "*Mark used for sparsely reffed articles."
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-canceled-mark ?G
588   "*Mark used for canceled articles."
589   :group 'gnus-summary-marks
590   :type 'character)
591
592 (defcustom gnus-duplicate-mark ?M
593   "*Mark used for duplicate articles."
594   :group 'gnus-summary-marks
595   :type 'character)
596
597 (defcustom gnus-undownloaded-mark ?-
598   "*Mark used for articles that weren't downloaded."
599   :version "22.1"
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-downloaded-mark ?+
604   "*Mark used for articles that were downloaded."
605   :group 'gnus-summary-marks
606   :type 'character)
607
608 (defcustom gnus-downloadable-mark ?%
609   "*Mark used for articles that are to be downloaded."
610   :group 'gnus-summary-marks
611   :type 'character)
612
613 (defcustom gnus-unsendable-mark ?=
614   "*Mark used for articles that won't be sent."
615   :group 'gnus-summary-marks
616   :type 'character)
617
618 (defcustom gnus-score-over-mark ?+
619   "*Score mark used for articles with high scores."
620   :group 'gnus-summary-marks
621   :type 'character)
622
623 (defcustom gnus-score-below-mark ?-
624   "*Score mark used for articles with low scores."
625   :group 'gnus-summary-marks
626   :type 'character)
627
628 (defcustom gnus-empty-thread-mark ?     ;Whitespace
629   "*There is no thread under the article."
630   :group 'gnus-summary-marks
631   :type 'character)
632
633 (defcustom gnus-not-empty-thread-mark ?=
634   "*There is a thread under the article."
635   :group 'gnus-summary-marks
636   :type 'character)
637
638 (defcustom gnus-view-pseudo-asynchronously nil
639   "*If non-nil, Gnus will view pseudo-articles asynchronously."
640   :group 'gnus-extract-view
641   :type 'boolean)
642
643 (defcustom gnus-auto-expirable-marks
644   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
645         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
646         gnus-souped-mark gnus-duplicate-mark)
647   "*The list of marks converted into expiration if a group is auto-expirable."
648   :version "21.1"
649   :group 'gnus-summary
650   :type '(repeat character))
651
652 (defcustom gnus-inhibit-user-auto-expire t
653   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
654   :version "21.1"
655   :group 'gnus-summary
656   :type 'boolean)
657
658 (defcustom gnus-view-pseudos nil
659   "*If `automatic', pseudo-articles will be viewed automatically.
660 If `not-confirm', pseudos will be viewed automatically, and the user
661 will not be asked to confirm the command."
662   :group 'gnus-extract-view
663   :type '(choice (const :tag "off" nil)
664                  (const automatic)
665                  (const not-confirm)))
666
667 (defcustom gnus-view-pseudos-separately t
668   "*If non-nil, one pseudo-article will be created for each file to be viewed.
669 If nil, all files that use the same viewing command will be given as a
670 list of parameters to that command."
671   :group 'gnus-extract-view
672   :type 'boolean)
673
674 (defcustom gnus-insert-pseudo-articles t
675   "*If non-nil, insert pseudo-articles when decoding articles."
676   :group 'gnus-extract-view
677   :type 'boolean)
678
679 (defcustom gnus-summary-dummy-line-format
680   "   %(:                             :%) %S\n"
681   "*The format specification for the dummy roots in the summary buffer.
682 It works along the same lines as a normal formatting string,
683 with some simple extensions.
684
685 %S  The subject
686
687 General format specifiers can also be used.
688 See `(gnus)Formatting Variables'."
689   :link '(custom-manual "(gnus)Formatting Variables")
690   :group 'gnus-threading
691   :type 'string)
692
693 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
694   "*The format specification for the summary mode line.
695 It works along the same lines as a normal formatting string,
696 with some simple extensions:
697
698 %G  Group name
699 %p  Unprefixed group name
700 %A  Current article number
701 %z  Current article score
702 %V  Gnus version
703 %U  Number of unread articles in the group
704 %e  Number of unselected articles in the group
705 %Z  A string with unread/unselected article counts
706 %g  Shortish group name
707 %S  Subject of the current article
708 %u  User-defined spec
709 %s  Current score file name
710 %d  Number of dormant articles
711 %r  Number of articles that have been marked as read in this session
712 %E  Number of articles expunged by the score files"
713   :group 'gnus-summary-format
714   :type 'string)
715
716 (defcustom gnus-list-identifiers nil
717   "Regexp that matches list identifiers to be removed from subject.
718 This can also be a list of regexps."
719   :version "21.1"
720   :group 'gnus-summary-format
721   :group 'gnus-article-hiding
722   :type '(choice (const :tag "none" nil)
723                  (regexp :value ".*")
724                  (repeat :value (".*") regexp)))
725
726 (defcustom gnus-summary-mark-below 0
727   "*Mark all articles with a score below this variable as read.
728 This variable is local to each summary buffer and usually set by the
729 score file."
730   :group 'gnus-score-default
731   :type 'integer)
732
733 (defun gnus-widget-reversible-match (widget value)
734   "Ignoring WIDGET, convert VALUE to internal form.
735 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
736   ;; (debug value)
737   (or (symbolp value)
738       (and (listp value)
739            (eq (length value) 2)
740            (eq (nth 0 value) 'not)
741            (symbolp (nth 1 value)))))
742
743 (defun gnus-widget-reversible-to-internal (widget value)
744   "Ignoring WIDGET, convert VALUE to internal form.
745 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
746 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
747   ;; (debug value)
748   (if (atom value)
749       (list value nil)
750     (list (nth 1 value) t)))
751
752 (defun gnus-widget-reversible-to-external (widget value)
753   "Ignoring WIDGET, convert VALUE to external form.
754 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
755 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
756   ;; (debug value)
757   (if (nth 1 value)
758       (list 'not (nth 0 value))
759     (nth 0 value)))
760
761 (define-widget 'gnus-widget-reversible 'group
762   "A `group' that convert values."
763   :match 'gnus-widget-reversible-match
764   :value-to-internal 'gnus-widget-reversible-to-internal
765   :value-to-external 'gnus-widget-reversible-to-external)
766
767 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
768   "*List of functions used for sorting articles in the summary buffer.
769
770 Each function takes two articles and returns non-nil if the first
771 article should be sorted before the other.  If you use more than one
772 function, the primary sort function should be the last.  You should
773 probably always include `gnus-article-sort-by-number' in the list of
774 sorting functions -- preferably first.  Also note that sorting by date
775 is often much slower than sorting by number, and the sorting order is
776 very similar.  (Sorting by date means sorting by the time the message
777 was sent, sorting by number means sorting by arrival time.)
778
779 Each item can also be a list `(not F)' where F is a function;
780 this reverses the sort order.
781
782 Ready-made functions include `gnus-article-sort-by-number',
783 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
784 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
785 and `gnus-article-sort-by-score'.
786
787 When threading is turned on, the variable `gnus-thread-sort-functions'
788 controls how articles are sorted."
789   :group 'gnus-summary-sort
790   :type '(repeat (gnus-widget-reversible
791                   (choice (function-item gnus-article-sort-by-number)
792                           (function-item gnus-article-sort-by-author)
793                           (function-item gnus-article-sort-by-subject)
794                           (function-item gnus-article-sort-by-date)
795                           (function-item gnus-article-sort-by-score)
796                           (function-item gnus-article-sort-by-random)
797                           (function :tag "other"))
798                   (boolean :tag "Reverse order"))))
799
800
801 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
802   "*List of functions used for sorting threads in the summary buffer.
803 By default, threads are sorted by article number.
804
805 Each function takes two threads and returns non-nil if the first
806 thread should be sorted before the other.  If you use more than one
807 function, the primary sort function should be the last.  You should
808 probably always include `gnus-thread-sort-by-number' in the list of
809 sorting functions -- preferably first.  Also note that sorting by date
810 is often much slower than sorting by number, and the sorting order is
811 very similar.  (Sorting by date means sorting by the time the message
812 was sent, sorting by number means sorting by arrival time.)
813
814 Each list item can also be a list `(not F)' where F is a
815 function; this specifies reversed sort order.
816
817 Ready-made functions include `gnus-thread-sort-by-number',
818 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
819 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
820 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
821 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
822 and `gnus-thread-sort-by-total-score' (see
823 `gnus-thread-score-function').
824
825 When threading is turned off, the variable
826 `gnus-article-sort-functions' controls how articles are sorted."
827   :group 'gnus-summary-sort
828   :type '(repeat
829           (gnus-widget-reversible
830            (choice (function-item gnus-thread-sort-by-number)
831                    (function-item gnus-thread-sort-by-author)
832                    (function-item gnus-thread-sort-by-recipient)
833                    (function-item gnus-thread-sort-by-subject)
834                    (function-item gnus-thread-sort-by-date)
835                    (function-item gnus-thread-sort-by-score)
836                    (function-item gnus-thread-sort-by-most-recent-number)
837                    (function-item gnus-thread-sort-by-most-recent-date)
838                    (function-item gnus-thread-sort-by-random)
839                    (function-item gnus-thread-sort-by-total-score)
840                    (function :tag "other"))
841            (boolean :tag "Reverse order"))))
842
843 (defcustom gnus-thread-score-function '+
844   "*Function used for calculating the total score of a thread.
845
846 The function is called with the scores of the article and each
847 subthread and should then return the score of the thread.
848
849 Some functions you can use are `+', `max', or `min'."
850   :group 'gnus-summary-sort
851   :type 'function)
852
853 (defcustom gnus-summary-expunge-below nil
854   "All articles that have a score less than this variable will be expunged.
855 This variable is local to the summary buffers."
856   :group 'gnus-score-default
857   :type '(choice (const :tag "off" nil)
858                  integer))
859
860 (defcustom gnus-thread-expunge-below nil
861   "All threads that have a total score less than this variable will be expunged.
862 See `gnus-thread-score-function' for en explanation of what a
863 \"thread score\" is.
864
865 This variable is local to the summary buffers."
866   :group 'gnus-threading
867   :group 'gnus-score-default
868   :type '(choice (const :tag "off" nil)
869                  integer))
870
871 (defcustom gnus-summary-mode-hook nil
872   "*A hook for Gnus summary mode.
873 This hook is run before any variables are set in the summary buffer."
874   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
875   :group 'gnus-summary-various
876   :type 'hook)
877
878 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
879 (when (featurep 'xemacs)
880   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
881   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
882   (add-hook 'gnus-summary-mode-hook
883             'gnus-xmas-switch-horizontal-scrollbar-off))
884
885 (defcustom gnus-summary-menu-hook nil
886   "*Hook run after the creation of the summary mode menu."
887   :group 'gnus-summary-visual
888   :type 'hook)
889
890 (defcustom gnus-summary-exit-hook nil
891   "*A hook called on exit from the summary buffer.
892 It will be called with point in the group buffer."
893   :group 'gnus-summary-exit
894   :type 'hook)
895
896 (defcustom gnus-summary-prepare-hook nil
897   "*A hook called after the summary buffer has been generated.
898 If you want to modify the summary buffer, you can use this hook."
899   :group 'gnus-summary-various
900   :type 'hook)
901
902 (defcustom gnus-summary-prepared-hook nil
903   "*A hook called as the last thing after the summary buffer has been generated."
904   :group 'gnus-summary-various
905   :type 'hook)
906
907 (defcustom gnus-summary-generate-hook nil
908   "*A hook run just before generating the summary buffer.
909 This hook is commonly used to customize threading variables and the
910 like."
911   :group 'gnus-summary-various
912   :type 'hook)
913
914 (defcustom gnus-select-group-hook nil
915   "*A hook called when a newsgroup is selected.
916
917 If you'd like to simplify subjects like the
918 `gnus-summary-next-same-subject' command does, you can use the
919 following hook:
920
921  (add-hook gnus-select-group-hook
922            (lambda ()
923              (mapcar (lambda (header)
924                        (mail-header-set-subject
925                         header
926                         (gnus-simplify-subject
927                          (mail-header-subject header) 're-only)))
928                      gnus-newsgroup-headers)))"
929   :group 'gnus-group-select
930   :type 'hook)
931
932 (defcustom gnus-select-article-hook nil
933   "*A hook called when an article is selected."
934   :group 'gnus-summary-choose
935   :options '(gnus-agent-fetch-selected-article)
936   :type 'hook)
937
938 (defcustom gnus-visual-mark-article-hook
939   (list 'gnus-highlight-selected-summary)
940   "*Hook run after selecting an article in the summary buffer.
941 It is meant to be used for highlighting the article in some way.  It
942 is not run if `gnus-visual' is nil."
943   :group 'gnus-summary-visual
944   :type 'hook)
945
946 (defcustom gnus-parse-headers-hook nil
947   "*A hook called before parsing the headers."
948   :group 'gnus-various
949   :type 'hook)
950
951 (defcustom gnus-exit-group-hook nil
952   "*A hook called when exiting summary mode.
953 This hook is not called from the non-updating exit commands like `Q'."
954   :group 'gnus-various
955   :type 'hook)
956
957 (defcustom gnus-summary-update-hook
958   (list 'gnus-summary-highlight-line)
959   "*A hook called when a summary line is changed.
960 The hook will not be called if `gnus-visual' is nil.
961
962 The default function `gnus-summary-highlight-line' will
963 highlight the line according to the `gnus-summary-highlight'
964 variable."
965   :group 'gnus-summary-visual
966   :type 'hook)
967
968 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
969   "*A hook called when an article is selected for the first time.
970 The hook is intended to mark an article as read (or unread)
971 automatically when it is selected."
972   :group 'gnus-summary-choose
973   :type 'hook)
974
975 (defcustom gnus-group-no-more-groups-hook nil
976   "*A hook run when returning to group mode having no more (unread) groups."
977   :group 'gnus-group-select
978   :type 'hook)
979
980 (defcustom gnus-ps-print-hook nil
981   "*A hook run before ps-printing something from Gnus."
982   :group 'gnus-summary
983   :type 'hook)
984
985 (defcustom gnus-summary-article-move-hook nil
986   "*A hook called after an article is moved, copied, respooled, or crossposted."
987   :version "22.1"
988   :group 'gnus-summary
989   :type 'hook)
990
991 (defcustom gnus-summary-article-delete-hook nil
992   "*A hook called after an article is deleted."
993   :version "22.1"
994   :group 'gnus-summary
995   :type 'hook)
996
997 (defcustom gnus-summary-article-expire-hook nil
998   "*A hook called after an article is expired."
999   :version "22.1"
1000   :group 'gnus-summary
1001   :type 'hook)
1002
1003 (defcustom gnus-summary-display-arrow
1004   (and (fboundp 'display-graphic-p)
1005        (display-graphic-p))
1006   "*If non-nil, display an arrow highlighting the current article."
1007   :version "22.1"
1008   :group 'gnus-summary
1009   :type 'boolean)
1010
1011 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1012   "Face used for highlighting the current article in the summary buffer."
1013   :group 'gnus-summary-visual
1014   :type 'face)
1015
1016 (defvar gnus-tmp-downloaded nil)
1017
1018 (defcustom gnus-summary-highlight
1019   '(((eq mark gnus-canceled-mark)
1020      . gnus-summary-cancelled)
1021     ((and uncached (> score default-high))
1022      . gnus-summary-high-undownloaded)
1023     ((and uncached (< score default-low))
1024      . gnus-summary-low-undownloaded)
1025     (uncached
1026      . gnus-summary-normal-undownloaded)
1027     ((and (> score default-high)
1028           (or (eq mark gnus-dormant-mark)
1029               (eq mark gnus-ticked-mark)))
1030      . gnus-summary-high-ticked)
1031     ((and (< score default-low)
1032           (or (eq mark gnus-dormant-mark)
1033               (eq mark gnus-ticked-mark)))
1034      . gnus-summary-low-ticked)
1035     ((or (eq mark gnus-dormant-mark)
1036          (eq mark gnus-ticked-mark))
1037      . gnus-summary-normal-ticked)
1038     ((and (> score default-high) (eq mark gnus-ancient-mark))
1039      . gnus-summary-high-ancient)
1040     ((and (< score default-low) (eq mark gnus-ancient-mark))
1041      . gnus-summary-low-ancient)
1042     ((eq mark gnus-ancient-mark)
1043      . gnus-summary-normal-ancient)
1044     ((and (> score default-high) (eq mark gnus-unread-mark))
1045      . gnus-summary-high-unread)
1046     ((and (< score default-low) (eq mark gnus-unread-mark))
1047      . gnus-summary-low-unread)
1048     ((eq mark gnus-unread-mark)
1049      . gnus-summary-normal-unread)
1050     ((> score default-high)
1051      . gnus-summary-high-read)
1052     ((< score default-low)
1053      . gnus-summary-low-read)
1054     (t
1055      . gnus-summary-normal-read))
1056   "*Controls the highlighting of summary buffer lines.
1057
1058 A list of (FORM . FACE) pairs.  When deciding how a a particular
1059 summary line should be displayed, each form is evaluated.  The content
1060 of the face field after the first true form is used.  You can change
1061 how those summary lines are displayed, by editing the face field.
1062
1063 You can use the following variables in the FORM field.
1064
1065 score:        The article's score
1066 default:      The default article score.
1067 default-high: The default score for high scored articles.
1068 default-low:  The default score for low scored articles.
1069 below:        The score below which articles are automatically marked as read.
1070 mark:         The article's mark.
1071 uncached:     Non-nil if the article is uncached."
1072   :group 'gnus-summary-visual
1073   :type '(repeat (cons (sexp :tag "Form" nil)
1074                        face)))
1075
1076 (defcustom gnus-alter-header-function nil
1077   "Function called to allow alteration of article header structures.
1078 The function is called with one parameter, the article header vector,
1079 which it may alter in any way."
1080   :type '(choice (const :tag "None" nil)
1081                  function)
1082   :group 'gnus-summary)
1083
1084 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1085   "Function used to decode a string with encoded words.")
1086
1087 (defvar gnus-decode-encoded-address-function
1088   'mail-decode-encoded-address-string
1089   "Function used to decode addresses with encoded words.")
1090
1091 (defcustom gnus-extra-headers '(To Newsgroups)
1092   "*Extra headers to parse."
1093   :version "21.1"
1094   :group 'gnus-summary
1095   :type '(repeat symbol))
1096
1097 (defcustom gnus-ignored-from-addresses
1098   (and user-mail-address
1099        (not (string= user-mail-address ""))
1100        (regexp-quote user-mail-address))
1101   "*From headers that may be suppressed in favor of To headers.
1102 This can be a regexp or a list of regexps."
1103   :version "21.1"
1104   :group 'gnus-summary
1105   :type '(choice regexp
1106                  (repeat :tag "Regexp List" regexp)))
1107
1108 (defsubst gnus-ignored-from-addresses ()
1109   (gmm-regexp-concat gnus-ignored-from-addresses))
1110
1111 (defcustom gnus-summary-to-prefix "-> "
1112   "*String prefixed to the To field in the summary line when
1113 using `gnus-ignored-from-addresses'."
1114   :version "22.1"
1115   :group 'gnus-summary
1116   :type 'string)
1117
1118 (defcustom gnus-summary-newsgroup-prefix "=> "
1119   "*String prefixed to the Newsgroup field in the summary
1120 line when using `gnus-ignored-from-addresses'."
1121   :version "22.1"
1122   :group 'gnus-summary
1123   :type 'string)
1124
1125 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1126   "List of charsets that should be ignored.
1127 When these charsets are used in the \"charset\" parameter, the
1128 default charset will be used instead."
1129   :version "21.1"
1130   :type '(repeat symbol)
1131   :group 'gnus-charset)
1132
1133 (gnus-define-group-parameter
1134  ignored-charsets
1135  :type list
1136  :function-document
1137  "Return the ignored charsets of GROUP."
1138  :variable gnus-group-ignored-charsets-alist
1139  :variable-default
1140  '(("alt\\.chinese\\.text" iso-8859-1))
1141  :variable-document
1142  "Alist of regexps (to match group names) and charsets that should be ignored.
1143 When these charsets are used in the \"charset\" parameter, the
1144 default charset will be used instead."
1145  :variable-group gnus-charset
1146  :variable-type '(repeat (cons (regexp :tag "Group")
1147                                (repeat symbol)))
1148  :parameter-type '(choice :tag "Ignored charsets"
1149                           :value nil
1150                           (repeat (symbol)))
1151  :parameter-document       "\
1152 List of charsets that should be ignored.
1153
1154 When these charsets are used in the \"charset\" parameter, the
1155 default charset will be used instead.")
1156
1157 (defcustom gnus-group-highlight-words-alist nil
1158   "Alist of group regexps and highlight regexps.
1159 This variable uses the same syntax as `gnus-emphasis-alist'."
1160   :version "21.1"
1161   :type '(repeat (cons (regexp :tag "Group")
1162                        (repeat (list (regexp :tag "Highlight regexp")
1163                                      (number :tag "Group for entire word" 0)
1164                                      (number :tag "Group for displayed part" 0)
1165                                      (symbol :tag "Face"
1166                                              gnus-emphasis-highlight-words)))))
1167   :group 'gnus-summary-visual)
1168
1169 (defcustom gnus-summary-show-article-charset-alist
1170   nil
1171   "Alist of number and charset.
1172 The article will be shown with the charset corresponding to the
1173 numbered argument.
1174 For example: ((1 . cn-gb-2312) (2 . big5))."
1175   :version "21.1"
1176   :type '(repeat (cons (number :tag "Argument" 1)
1177                        (symbol :tag "Charset")))
1178   :group 'gnus-charset)
1179
1180 (defcustom gnus-preserve-marks t
1181   "Whether marks are preserved when moving, copying and respooling messages."
1182   :version "21.1"
1183   :type 'boolean
1184   :group 'gnus-summary-marks)
1185
1186 (defcustom gnus-alter-articles-to-read-function nil
1187   "Function to be called to alter the list of articles to be selected."
1188   :type '(choice (const nil) function)
1189   :group 'gnus-summary)
1190
1191 (defcustom gnus-orphan-score nil
1192   "*All orphans get this score added.  Set in the score file."
1193   :group 'gnus-score-default
1194   :type '(choice (const nil)
1195                  integer))
1196
1197 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1198   "*A regexp to match MIME parts when saving multiple parts of a
1199 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1200 This regexp will be used by default when prompting the user for which
1201 type of files to save."
1202   :group 'gnus-summary
1203   :type 'regexp)
1204
1205 (defcustom gnus-read-all-available-headers nil
1206   "Whether Gnus should parse all headers made available to it.
1207 This is mostly relevant for slow back ends where the user may
1208 wish to widen the summary buffer to include all headers
1209 that were fetched.  Say, for nnultimate groups."
1210   :version "22.1"
1211   :group 'gnus-summary
1212   :type '(choice boolean regexp))
1213
1214 (defcustom gnus-summary-muttprint-program "muttprint"
1215   "Command (and optional arguments) used to run Muttprint."
1216   :version "22.1"
1217   :group 'gnus-summary
1218   :type 'string)
1219
1220 (defcustom gnus-article-loose-mime t
1221   "If non-nil, don't require MIME-Version header.
1222 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1223 supply the MIME-Version header or deliberately strip it from the mail.
1224 If non-nil (the default), Gnus will treat some articles as MIME
1225 even if the MIME-Version header is missing."
1226   :version "22.1"
1227   :type 'boolean
1228   :group 'gnus-article-mime)
1229
1230 (defcustom gnus-article-emulate-mime t
1231   "If non-nil, use MIME emulation for uuencode and the like.
1232 This means that Gnus will search message bodies for text that look
1233 like uuencoded bits, yEncoded bits, and so on, and present that using
1234 the normal Gnus MIME machinery."
1235   :version "22.1"
1236   :type 'boolean
1237   :group 'gnus-article-mime)
1238
1239 ;;; Internal variables
1240
1241 (defvar gnus-summary-display-cache nil)
1242 (defvar gnus-article-mime-handles nil)
1243 (defvar gnus-article-decoded-p nil)
1244 (defvar gnus-article-charset nil)
1245 (defvar gnus-article-ignored-charsets nil)
1246 (defvar gnus-scores-exclude-files nil)
1247 (defvar gnus-page-broken nil)
1248
1249 (defvar gnus-original-article nil)
1250 (defvar gnus-article-internal-prepare-hook nil)
1251 (defvar gnus-newsgroup-process-stack nil)
1252
1253 (defvar gnus-thread-indent-array nil)
1254 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1255 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1256   "Function called to sort the articles within a thread after it has been gathered together.")
1257
1258 (defvar gnus-summary-save-parts-type-history nil)
1259 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1260
1261 ;; Avoid highlighting in kill files.
1262 (defvar gnus-summary-inhibit-highlight nil)
1263 (defvar gnus-newsgroup-selected-overlay nil)
1264 (defvar gnus-inhibit-limiting nil)
1265 (defvar gnus-newsgroup-adaptive-score-file nil)
1266 (defvar gnus-current-score-file nil)
1267 (defvar gnus-current-move-group nil)
1268 (defvar gnus-current-copy-group nil)
1269 (defvar gnus-current-crosspost-group nil)
1270 (defvar gnus-newsgroup-display nil)
1271
1272 (defvar gnus-newsgroup-dependencies nil)
1273 (defvar gnus-newsgroup-adaptive nil)
1274 (defvar gnus-summary-display-article-function nil)
1275 (defvar gnus-summary-highlight-line-function nil
1276   "Function called after highlighting a summary line.")
1277
1278 (defvar gnus-summary-line-format-alist
1279   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1280     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1281     (?s gnus-tmp-subject-or-nil ?s)
1282     (?n gnus-tmp-name ?s)
1283     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1284         ?s)
1285     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1286             gnus-tmp-from) ?s)
1287     (?F gnus-tmp-from ?s)
1288     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1289     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1290     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1291     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1292     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1293     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1294     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1295     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1296     (?L gnus-tmp-lines ?s)
1297     (?O gnus-tmp-downloaded ?c)
1298     (?I gnus-tmp-indentation ?s)
1299     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1300     (?R gnus-tmp-replied ?c)
1301     (?\[ gnus-tmp-opening-bracket ?c)
1302     (?\] gnus-tmp-closing-bracket ?c)
1303     (?\> (make-string gnus-tmp-level ? ) ?s)
1304     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1305     (?i gnus-tmp-score ?d)
1306     (?z gnus-tmp-score-char ?c)
1307     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1308     (?U gnus-tmp-unread ?c)
1309     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1310         ?s)
1311     (?t (gnus-summary-number-of-articles-in-thread
1312          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1313         ?d)
1314     (?e (gnus-summary-number-of-articles-in-thread
1315          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1316         ?c)
1317     (?u gnus-tmp-user-defined ?s)
1318     (?P (gnus-pick-line-number) ?d)
1319     (?B gnus-tmp-thread-tree-header-string ?s)
1320     (user-date (gnus-user-date
1321                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1322   "An alist of format specifications that can appear in summary lines.
1323 These are paired with what variables they correspond with, along with
1324 the type of the variable (string, integer, character, etc).")
1325
1326 (defvar gnus-summary-dummy-line-format-alist
1327   `((?S gnus-tmp-subject ?s)
1328     (?N gnus-tmp-number ?d)
1329     (?u gnus-tmp-user-defined ?s)))
1330
1331 (defvar gnus-summary-mode-line-format-alist
1332   `((?G gnus-tmp-group-name ?s)
1333     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1334     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1335     (?A gnus-tmp-article-number ?d)
1336     (?Z gnus-tmp-unread-and-unselected ?s)
1337     (?V gnus-version ?s)
1338     (?U gnus-tmp-unread-and-unticked ?d)
1339     (?S gnus-tmp-subject ?s)
1340     (?e gnus-tmp-unselected ?d)
1341     (?u gnus-tmp-user-defined ?s)
1342     (?d (length gnus-newsgroup-dormant) ?d)
1343     (?t (length gnus-newsgroup-marked) ?d)
1344     (?h (length gnus-newsgroup-spam-marked) ?d)
1345     (?r (length gnus-newsgroup-reads) ?d)
1346     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1347     (?E gnus-newsgroup-expunged-tally ?d)
1348     (?s (gnus-current-score-file-nondirectory) ?s)))
1349
1350 (defvar gnus-last-search-regexp nil
1351   "Default regexp for article search command.")
1352
1353 (defvar gnus-last-shell-command nil
1354   "Default shell command on article.")
1355
1356 (defvar gnus-newsgroup-agentized nil
1357   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1358 (defvar gnus-newsgroup-begin nil)
1359 (defvar gnus-newsgroup-end nil)
1360 (defvar gnus-newsgroup-last-rmail nil)
1361 (defvar gnus-newsgroup-last-mail nil)
1362 (defvar gnus-newsgroup-last-folder nil)
1363 (defvar gnus-newsgroup-last-file nil)
1364 (defvar gnus-newsgroup-last-directory nil)
1365 (defvar gnus-newsgroup-auto-expire nil)
1366 (defvar gnus-newsgroup-active nil)
1367
1368 (defvar gnus-newsgroup-data nil)
1369 (defvar gnus-newsgroup-data-reverse nil)
1370 (defvar gnus-newsgroup-limit nil)
1371 (defvar gnus-newsgroup-limits nil)
1372 (defvar gnus-summary-use-undownloaded-faces nil)
1373
1374 (defvar gnus-newsgroup-unreads nil
1375   "Sorted list of unread articles in the current newsgroup.")
1376
1377 (defvar gnus-newsgroup-unselected nil
1378   "Sorted list of unselected unread articles in the current newsgroup.")
1379
1380 (defvar gnus-newsgroup-reads nil
1381   "Alist of read articles and article marks in the current newsgroup.")
1382
1383 (defvar gnus-newsgroup-expunged-tally nil)
1384
1385 (defvar gnus-newsgroup-marked nil
1386   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1387
1388 (defvar gnus-newsgroup-spam-marked nil
1389   "List of ranges of articles that have been marked as spam.")
1390
1391 (defvar gnus-newsgroup-killed nil
1392   "List of ranges of articles that have been through the scoring process.")
1393
1394 (defvar gnus-newsgroup-cached nil
1395   "Sorted list of articles that come from the article cache.")
1396
1397 (defvar gnus-newsgroup-saved nil
1398   "List of articles that have been saved.")
1399
1400 (defvar gnus-newsgroup-kill-headers nil)
1401
1402 (defvar gnus-newsgroup-replied nil
1403   "List of articles that have been replied to in the current newsgroup.")
1404
1405 (defvar gnus-newsgroup-forwarded nil
1406   "List of articles that have been forwarded in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-recent nil
1409   "List of articles that have are recent in the current newsgroup.")
1410
1411 (defvar gnus-newsgroup-expirable nil
1412   "Sorted list of articles in the current newsgroup that can be expired.")
1413
1414 (defvar gnus-newsgroup-processable nil
1415   "List of articles in the current newsgroup that can be processed.")
1416
1417 (defvar gnus-newsgroup-downloadable nil
1418   "Sorted list of articles in the current newsgroup that can be processed.")
1419
1420 (defvar gnus-newsgroup-unfetched nil
1421   "Sorted list of articles in the current newsgroup whose headers have
1422 not been fetched into the agent.
1423
1424 This list will always be a subset of gnus-newsgroup-undownloaded.")
1425
1426 (defvar gnus-newsgroup-undownloaded nil
1427   "List of articles in the current newsgroup that haven't been downloaded.")
1428
1429 (defvar gnus-newsgroup-unsendable nil
1430   "List of articles in the current newsgroup that won't be sent.")
1431
1432 (defvar gnus-newsgroup-bookmarks nil
1433   "List of articles in the current newsgroup that have bookmarks.")
1434
1435 (defvar gnus-newsgroup-dormant nil
1436   "Sorted list of dormant articles in the current newsgroup.")
1437
1438 (defvar gnus-newsgroup-unseen nil
1439   "List of unseen articles in the current newsgroup.")
1440
1441 (defvar gnus-newsgroup-seen nil
1442   "Range of seen articles in the current newsgroup.")
1443
1444 (defvar gnus-newsgroup-articles nil
1445   "List of articles in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-scored nil
1448   "List of scored articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-headers nil
1451   "List of article headers in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-threads nil)
1454
1455 (defvar gnus-newsgroup-prepared nil
1456   "Whether the current group has been prepared properly.")
1457
1458 (defvar gnus-newsgroup-ancient nil
1459   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1460
1461 (defvar gnus-newsgroup-sparse nil)
1462
1463 (defvar gnus-current-article nil)
1464 (defvar gnus-article-current nil)
1465 (defvar gnus-current-headers nil)
1466 (defvar gnus-have-all-headers nil)
1467 (defvar gnus-last-article nil)
1468 (defvar gnus-newsgroup-history nil)
1469 (defvar gnus-newsgroup-charset nil)
1470 (defvar gnus-newsgroup-ephemeral-charset nil)
1471 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1472
1473 (defvar gnus-article-before-search nil)
1474
1475 (defvar gnus-summary-local-variables
1476   '(gnus-newsgroup-name
1477     gnus-newsgroup-begin gnus-newsgroup-end
1478     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1479     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1480     gnus-newsgroup-last-directory
1481     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1482     gnus-newsgroup-unselected gnus-newsgroup-marked
1483     gnus-newsgroup-spam-marked
1484     gnus-newsgroup-reads gnus-newsgroup-saved
1485     gnus-newsgroup-replied gnus-newsgroup-forwarded
1486     gnus-newsgroup-recent
1487     gnus-newsgroup-expirable
1488     gnus-newsgroup-processable gnus-newsgroup-killed
1489     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1490     gnus-newsgroup-unfetched
1491     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1492     gnus-newsgroup-seen gnus-newsgroup-articles
1493     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1494     gnus-newsgroup-headers gnus-newsgroup-threads
1495     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1496     gnus-current-article gnus-current-headers gnus-have-all-headers
1497     gnus-last-article gnus-article-internal-prepare-hook
1498     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1499     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1500     gnus-thread-expunge-below
1501     gnus-score-alist gnus-current-score-file
1502     (gnus-summary-expunge-below . global)
1503     (gnus-summary-mark-below . global)
1504     (gnus-orphan-score . global)
1505     gnus-newsgroup-active gnus-scores-exclude-files
1506     gnus-newsgroup-history gnus-newsgroup-ancient
1507     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1508     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1509     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1510     (gnus-newsgroup-expunged-tally . 0)
1511     gnus-cache-removable-articles gnus-newsgroup-cached
1512     gnus-newsgroup-data gnus-newsgroup-data-reverse
1513     gnus-newsgroup-limit gnus-newsgroup-limits
1514     gnus-newsgroup-charset gnus-newsgroup-display
1515     gnus-summary-use-undownloaded-faces)
1516   "Variables that are buffer-local to the summary buffers.")
1517
1518 (defvar gnus-newsgroup-variables nil
1519   "A list of variables that have separate values in different newsgroups.
1520 A list of newsgroup (summary buffer) local variables, or cons of
1521 variables and their default expressions to be evalled (when the default
1522 values are not nil), that should be made global while the summary buffer
1523 is active.
1524
1525 Note: The default expressions will be evaluated (using function `eval')
1526 before assignment to the local variable rather than just assigned to it.
1527 If the default expression is the symbol `global', that symbol will not
1528 be evaluated but the global value of the local variable will be used
1529 instead.
1530
1531 These variables can be used to set variables in the group parameters
1532 while still allowing them to affect operations done in other buffers.
1533 For example:
1534
1535 \(setq gnus-newsgroup-variables
1536      '(message-use-followup-to
1537        (gnus-visible-headers .
1538          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1539 ")
1540
1541 ;; Byte-compiler warning.
1542 (eval-when-compile
1543   ;; Bind features so that require will believe that gnus-sum has
1544   ;; already been loaded (avoids infinite recursion)
1545   (let ((features (cons 'gnus-sum features)))
1546     ;; Several of the declarations in gnus-sum are needed to load the
1547     ;; following files. Right now, these definitions have been
1548     ;; compiled but not defined (evaluated).  We could either do a
1549     ;; eval-and-compile about all of the declarations or evaluate the
1550     ;; source file.
1551     (if (boundp 'gnus-newsgroup-variables)
1552         nil
1553       (load "gnus-sum.el" t t t))
1554     (require 'gnus)
1555     (require 'gnus-art)))
1556
1557 ;; MIME stuff.
1558
1559 (defvar gnus-decode-encoded-word-methods
1560   '(mail-decode-encoded-word-string)
1561   "List of methods used to decode encoded words.
1562
1563 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1564 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1565 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1566 whose names match REGEXP.
1567
1568 For example:
1569 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1570  mail-decode-encoded-word-string
1571  (\"chinese\" . rfc1843-decode-string))")
1572
1573 (defvar gnus-decode-encoded-word-methods-cache nil)
1574
1575 (defun gnus-multi-decode-encoded-word-string (string)
1576   "Apply the functions from `gnus-encoded-word-methods' that match."
1577   (unless (and gnus-decode-encoded-word-methods-cache
1578                (eq gnus-newsgroup-name
1579                    (car gnus-decode-encoded-word-methods-cache)))
1580     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1581     (mapcar (lambda (x)
1582               (if (symbolp x)
1583                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1584                 (if (and gnus-newsgroup-name
1585                          (string-match (car x) gnus-newsgroup-name))
1586                     (nconc gnus-decode-encoded-word-methods-cache
1587                            (list (cdr x))))))
1588             gnus-decode-encoded-word-methods))
1589   (let ((xlist gnus-decode-encoded-word-methods-cache))
1590     (pop xlist)
1591     (while xlist
1592       (setq string (funcall (pop xlist) string))))
1593   string)
1594
1595 ;; Subject simplification.
1596
1597 (defun gnus-simplify-whitespace (str)
1598   "Remove excessive whitespace from STR."
1599   ;; Multiple spaces.
1600   (while (string-match "[ \t][ \t]+" str)
1601     (setq str (concat (substring str 0 (match-beginning 0))
1602                         " "
1603                         (substring str (match-end 0)))))
1604   ;; Leading spaces.
1605   (when (string-match "^[ \t]+" str)
1606     (setq str (substring str (match-end 0))))
1607   ;; Trailing spaces.
1608   (when (string-match "[ \t]+$" str)
1609     (setq str (substring str 0 (match-beginning 0))))
1610   str)
1611
1612 (defun gnus-simplify-all-whitespace (str)
1613   "Remove all whitespace from STR."
1614   (while (string-match "[ \t\n]+" str)
1615     (setq str (replace-match "" nil nil str)))
1616   str)
1617
1618 (defsubst gnus-simplify-subject-re (subject)
1619   "Remove \"Re:\" from subject lines."
1620   (if (string-match message-subject-re-regexp subject)
1621       (substring subject (match-end 0))
1622     subject))
1623
1624 (defun gnus-simplify-subject (subject &optional re-only)
1625   "Remove `Re:' and words in parentheses.
1626 If RE-ONLY is non-nil, strip leading `Re:'s only."
1627   (let ((case-fold-search t))           ;Ignore case.
1628     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1629     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1630       (setq subject (substring subject (match-end 0))))
1631     ;; Remove uninteresting prefixes.
1632     (when (and (not re-only)
1633                gnus-simplify-ignored-prefixes
1634                (string-match gnus-simplify-ignored-prefixes subject))
1635       (setq subject (substring subject (match-end 0))))
1636     ;; Remove words in parentheses from end.
1637     (unless re-only
1638       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1639         (setq subject (substring subject 0 (match-beginning 0)))))
1640     ;; Return subject string.
1641     subject))
1642
1643 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1644 ;; all whitespace.
1645 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1646   (goto-char (point-min))
1647   (while (re-search-forward regexp nil t)
1648     (replace-match (or newtext ""))))
1649
1650 (defun gnus-simplify-buffer-fuzzy ()
1651   "Simplify string in the buffer fuzzily.
1652 The string in the accessible portion of the current buffer is simplified.
1653 It is assumed to be a single-line subject.
1654 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1655 matter is removed.  Additional things can be deleted by setting
1656 `gnus-simplify-subject-fuzzy-regexp'."
1657   (let ((case-fold-search t)
1658         (modified-tick))
1659     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1660
1661     (while (not (eq modified-tick (buffer-modified-tick)))
1662       (setq modified-tick (buffer-modified-tick))
1663       (cond
1664        ((listp gnus-simplify-subject-fuzzy-regexp)
1665         (mapcar 'gnus-simplify-buffer-fuzzy-step
1666                 gnus-simplify-subject-fuzzy-regexp))
1667        (gnus-simplify-subject-fuzzy-regexp
1668         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1669       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1670       (gnus-simplify-buffer-fuzzy-step
1671        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1672       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1673
1674     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1675     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1676     (gnus-simplify-buffer-fuzzy-step " $")
1677     (gnus-simplify-buffer-fuzzy-step "^ +")))
1678
1679 (defun gnus-simplify-subject-fuzzy (subject)
1680   "Simplify a subject string fuzzily.
1681 See `gnus-simplify-buffer-fuzzy' for details."
1682   (save-excursion
1683     (gnus-set-work-buffer)
1684     (let ((case-fold-search t))
1685       ;; Remove uninteresting prefixes.
1686       (when (and gnus-simplify-ignored-prefixes
1687                  (string-match gnus-simplify-ignored-prefixes subject))
1688         (setq subject (substring subject (match-end 0))))
1689       (insert subject)
1690       (inline (gnus-simplify-buffer-fuzzy))
1691       (buffer-string))))
1692
1693 (defsubst gnus-simplify-subject-fully (subject)
1694   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1695   (cond
1696    (gnus-simplify-subject-functions
1697     (gnus-map-function gnus-simplify-subject-functions subject))
1698    ((null gnus-summary-gather-subject-limit)
1699     (gnus-simplify-subject-re subject))
1700    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1701     (gnus-simplify-subject-fuzzy subject))
1702    ((numberp gnus-summary-gather-subject-limit)
1703     (gnus-limit-string (gnus-simplify-subject-re subject)
1704                        gnus-summary-gather-subject-limit))
1705    (t
1706     subject)))
1707
1708 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1709   "Check whether two subjects are equal.
1710 If optional argument SIMPLE-FIRST is t, first argument is already
1711 simplified."
1712   (cond
1713    ((null simple-first)
1714     (equal (gnus-simplify-subject-fully s1)
1715            (gnus-simplify-subject-fully s2)))
1716    (t
1717     (equal s1
1718            (gnus-simplify-subject-fully s2)))))
1719
1720 (defun gnus-summary-bubble-group ()
1721   "Increase the score of the current group.
1722 This is a handy function to add to `gnus-summary-exit-hook' to
1723 increase the score of each group you read."
1724   (gnus-group-add-score gnus-newsgroup-name))
1725
1726 \f
1727 ;;;
1728 ;;; Gnus summary mode
1729 ;;;
1730
1731 (put 'gnus-summary-mode 'mode-class 'special)
1732
1733 (defvar gnus-article-commands-menu)
1734
1735 ;; Non-orthogonal keys
1736
1737 (gnus-define-keys gnus-summary-mode-map
1738   " " gnus-summary-next-page
1739   "\177" gnus-summary-prev-page
1740   [delete] gnus-summary-prev-page
1741   [backspace] gnus-summary-prev-page
1742   "\r" gnus-summary-scroll-up
1743   "\M-\r" gnus-summary-scroll-down
1744   "n" gnus-summary-next-unread-article
1745   "p" gnus-summary-prev-unread-article
1746   "N" gnus-summary-next-article
1747   "P" gnus-summary-prev-article
1748   "\M-\C-n" gnus-summary-next-same-subject
1749   "\M-\C-p" gnus-summary-prev-same-subject
1750   "\M-n" gnus-summary-next-unread-subject
1751   "\M-p" gnus-summary-prev-unread-subject
1752   "." gnus-summary-first-unread-article
1753   "," gnus-summary-best-unread-article
1754   "\M-s" gnus-summary-search-article-forward
1755   "\M-r" gnus-summary-search-article-backward
1756   "\M-S" gnus-summary-repeat-search-article-forward
1757   "\M-R" gnus-summary-repeat-search-article-backward
1758   "<" gnus-summary-beginning-of-article
1759   ">" gnus-summary-end-of-article
1760   "j" gnus-summary-goto-article
1761   "^" gnus-summary-refer-parent-article
1762   "\M-^" gnus-summary-refer-article
1763   "u" gnus-summary-tick-article-forward
1764   "!" gnus-summary-tick-article-forward
1765   "U" gnus-summary-tick-article-backward
1766   "d" gnus-summary-mark-as-read-forward
1767   "D" gnus-summary-mark-as-read-backward
1768   "E" gnus-summary-mark-as-expirable
1769   "\M-u" gnus-summary-clear-mark-forward
1770   "\M-U" gnus-summary-clear-mark-backward
1771   "k" gnus-summary-kill-same-subject-and-select
1772   "\C-k" gnus-summary-kill-same-subject
1773   "\M-\C-k" gnus-summary-kill-thread
1774   "\M-\C-l" gnus-summary-lower-thread
1775   "e" gnus-summary-edit-article
1776   "#" gnus-summary-mark-as-processable
1777   "\M-#" gnus-summary-unmark-as-processable
1778   "\M-\C-t" gnus-summary-toggle-threads
1779   "\M-\C-s" gnus-summary-show-thread
1780   "\M-\C-h" gnus-summary-hide-thread
1781   "\M-\C-f" gnus-summary-next-thread
1782   "\M-\C-b" gnus-summary-prev-thread
1783   [(meta down)] gnus-summary-next-thread
1784   [(meta up)] gnus-summary-prev-thread
1785   "\M-\C-u" gnus-summary-up-thread
1786   "\M-\C-d" gnus-summary-down-thread
1787   "&" gnus-summary-execute-command
1788   "c" gnus-summary-catchup-and-exit
1789   "\C-w" gnus-summary-mark-region-as-read
1790   "\C-t" gnus-summary-toggle-truncation
1791   "?" gnus-summary-mark-as-dormant
1792   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1793   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1794   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1795   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1796   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1797   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1798   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1799   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1800   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1801   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1802   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1803   "=" gnus-summary-expand-window
1804   "\C-x\C-s" gnus-summary-reselect-current-group
1805   "\M-g" gnus-summary-rescan-group
1806   "w" gnus-summary-stop-page-breaking
1807   "\C-c\C-r" gnus-summary-caesar-message
1808   "f" gnus-summary-followup
1809   "F" gnus-summary-followup-with-original
1810   "C" gnus-summary-cancel-article
1811   "r" gnus-summary-reply
1812   "R" gnus-summary-reply-with-original
1813   "\C-c\C-f" gnus-summary-mail-forward
1814   "o" gnus-summary-save-article
1815   "\C-o" gnus-summary-save-article-mail
1816   "|" gnus-summary-pipe-output
1817   "\M-k" gnus-summary-edit-local-kill
1818   "\M-K" gnus-summary-edit-global-kill
1819   ;; "V" gnus-version
1820   "\C-c\C-d" gnus-summary-describe-group
1821   "q" gnus-summary-exit
1822   "Q" gnus-summary-exit-no-update
1823   "\C-c\C-i" gnus-info-find-node
1824   gnus-mouse-2 gnus-mouse-pick-article
1825   [follow-link] mouse-face
1826   "m" gnus-summary-mail-other-window
1827   "a" gnus-summary-post-news
1828   "i" gnus-summary-news-other-window
1829   "x" gnus-summary-limit-to-unread
1830   "s" gnus-summary-isearch-article
1831   "t" gnus-summary-toggle-header
1832   "g" gnus-summary-show-article
1833   "l" gnus-summary-goto-last-article
1834   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1835   "\C-d" gnus-summary-enter-digest-group
1836   "\M-\C-d" gnus-summary-read-document
1837   "\M-\C-e" gnus-summary-edit-parameters
1838   "\M-\C-a" gnus-summary-customize-parameters
1839   "\C-c\C-b" gnus-bug
1840   "*" gnus-cache-enter-article
1841   "\M-*" gnus-cache-remove-article
1842   "\M-&" gnus-summary-universal-argument
1843   "\C-l" gnus-recenter
1844   "I" gnus-summary-increase-score
1845   "L" gnus-summary-lower-score
1846   "\M-i" gnus-symbolic-argument
1847   "h" gnus-summary-select-article-buffer
1848
1849   "b" gnus-article-view-part
1850   "\M-t" gnus-summary-toggle-display-buttonized
1851
1852   "V" gnus-summary-score-map
1853   "X" gnus-uu-extract-map
1854   "S" gnus-summary-send-map)
1855
1856 ;; Sort of orthogonal keymap
1857 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1858   "t" gnus-summary-tick-article-forward
1859   "!" gnus-summary-tick-article-forward
1860   "d" gnus-summary-mark-as-read-forward
1861   "r" gnus-summary-mark-as-read-forward
1862   "c" gnus-summary-clear-mark-forward
1863   " " gnus-summary-clear-mark-forward
1864   "e" gnus-summary-mark-as-expirable
1865   "x" gnus-summary-mark-as-expirable
1866   "?" gnus-summary-mark-as-dormant
1867   "b" gnus-summary-set-bookmark
1868   "B" gnus-summary-remove-bookmark
1869   "#" gnus-summary-mark-as-processable
1870   "\M-#" gnus-summary-unmark-as-processable
1871   "S" gnus-summary-limit-include-expunged
1872   "C" gnus-summary-catchup
1873   "H" gnus-summary-catchup-to-here
1874   "h" gnus-summary-catchup-from-here
1875   "\C-c" gnus-summary-catchup-all
1876   "k" gnus-summary-kill-same-subject-and-select
1877   "K" gnus-summary-kill-same-subject
1878   "P" gnus-uu-mark-map)
1879
1880 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1881   "c" gnus-summary-clear-above
1882   "u" gnus-summary-tick-above
1883   "m" gnus-summary-mark-above
1884   "k" gnus-summary-kill-below)
1885
1886 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1887   "/" gnus-summary-limit-to-subject
1888   "n" gnus-summary-limit-to-articles
1889   "b" gnus-summary-limit-to-bodies
1890   "h" gnus-summary-limit-to-headers
1891   "w" gnus-summary-pop-limit
1892   "s" gnus-summary-limit-to-subject
1893   "a" gnus-summary-limit-to-author
1894   "u" gnus-summary-limit-to-unread
1895   "m" gnus-summary-limit-to-marks
1896   "M" gnus-summary-limit-exclude-marks
1897   "v" gnus-summary-limit-to-score
1898   "*" gnus-summary-limit-include-cached
1899   "D" gnus-summary-limit-include-dormant
1900   "T" gnus-summary-limit-include-thread
1901   "d" gnus-summary-limit-exclude-dormant
1902   "t" gnus-summary-limit-to-age
1903   "." gnus-summary-limit-to-unseen
1904   "x" gnus-summary-limit-to-extra
1905   "p" gnus-summary-limit-to-display-predicate
1906   "E" gnus-summary-limit-include-expunged
1907   "c" gnus-summary-limit-exclude-childless-dormant
1908   "C" gnus-summary-limit-mark-excluded-as-read
1909   "o" gnus-summary-insert-old-articles
1910   "N" gnus-summary-insert-new-articles
1911   "S" gnus-summary-limit-to-singletons
1912   "r" gnus-summary-limit-to-replied
1913   "R" gnus-summary-limit-to-recipient)
1914
1915 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1916   "n" gnus-summary-next-unread-article
1917   "p" gnus-summary-prev-unread-article
1918   "N" gnus-summary-next-article
1919   "P" gnus-summary-prev-article
1920   "\C-n" gnus-summary-next-same-subject
1921   "\C-p" gnus-summary-prev-same-subject
1922   "\M-n" gnus-summary-next-unread-subject
1923   "\M-p" gnus-summary-prev-unread-subject
1924   "f" gnus-summary-first-unread-article
1925   "b" gnus-summary-best-unread-article
1926   "j" gnus-summary-goto-article
1927   "g" gnus-summary-goto-subject
1928   "l" gnus-summary-goto-last-article
1929   "o" gnus-summary-pop-article)
1930
1931 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1932   "k" gnus-summary-kill-thread
1933   "l" gnus-summary-lower-thread
1934   "i" gnus-summary-raise-thread
1935   "T" gnus-summary-toggle-threads
1936   "t" gnus-summary-rethread-current
1937   "^" gnus-summary-reparent-thread
1938   "\M-^" gnus-summary-reparent-children
1939   "s" gnus-summary-show-thread
1940   "S" gnus-summary-show-all-threads
1941   "h" gnus-summary-hide-thread
1942   "H" gnus-summary-hide-all-threads
1943   "n" gnus-summary-next-thread
1944   "p" gnus-summary-prev-thread
1945   "u" gnus-summary-up-thread
1946   "o" gnus-summary-top-thread
1947   "d" gnus-summary-down-thread
1948   "#" gnus-uu-mark-thread
1949   "\M-#" gnus-uu-unmark-thread)
1950
1951 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1952   "g" gnus-summary-prepare
1953   "c" gnus-summary-insert-cached-articles
1954   "d" gnus-summary-insert-dormant-articles
1955   "t" gnus-summary-insert-ticked-articles)
1956
1957 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1958   "c" gnus-summary-catchup-and-exit
1959   "C" gnus-summary-catchup-all-and-exit
1960   "E" gnus-summary-exit-no-update
1961   "Q" gnus-summary-exit
1962   "Z" gnus-summary-exit
1963   "n" gnus-summary-catchup-and-goto-next-group
1964   "p" gnus-summary-catchup-and-goto-prev-group
1965   "R" gnus-summary-reselect-current-group
1966   "G" gnus-summary-rescan-group
1967   "N" gnus-summary-next-group
1968   "s" gnus-summary-save-newsrc
1969   "P" gnus-summary-prev-group)
1970
1971 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1972   " " gnus-summary-next-page
1973   "n" gnus-summary-next-page
1974   "\177" gnus-summary-prev-page
1975   [delete] gnus-summary-prev-page
1976   "p" gnus-summary-prev-page
1977   "\r" gnus-summary-scroll-up
1978   "\M-\r" gnus-summary-scroll-down
1979   "<" gnus-summary-beginning-of-article
1980   ">" gnus-summary-end-of-article
1981   "b" gnus-summary-beginning-of-article
1982   "e" gnus-summary-end-of-article
1983   "^" gnus-summary-refer-parent-article
1984   "r" gnus-summary-refer-parent-article
1985   "D" gnus-summary-enter-digest-group
1986   "R" gnus-summary-refer-references
1987   "T" gnus-summary-refer-thread
1988   "g" gnus-summary-show-article
1989   "s" gnus-summary-isearch-article
1990   "P" gnus-summary-print-article
1991   "M" gnus-mailing-list-insinuate
1992   "t" gnus-article-babel)
1993
1994 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1995   "b" gnus-article-add-buttons
1996   "B" gnus-article-add-buttons-to-head
1997   "o" gnus-article-treat-overstrike
1998   "e" gnus-article-emphasize
1999   "w" gnus-article-fill-cited-article
2000   "Q" gnus-article-fill-long-lines
2001   "L" gnus-article-toggle-truncate-lines
2002   "C" gnus-article-capitalize-sentences
2003   "c" gnus-article-remove-cr
2004   "q" gnus-article-de-quoted-unreadable
2005   "6" gnus-article-de-base64-unreadable
2006   "Z" gnus-article-decode-HZ
2007   "A" gnus-article-treat-ansi-sequences
2008   "h" gnus-article-wash-html
2009   "u" gnus-article-unsplit-urls
2010   "s" gnus-summary-force-verify-and-decrypt
2011   "f" gnus-article-display-x-face
2012   "l" gnus-summary-stop-page-breaking
2013   "r" gnus-summary-caesar-message
2014   "m" gnus-summary-morse-message
2015   "t" gnus-summary-toggle-header
2016   "g" gnus-treat-smiley
2017   "v" gnus-summary-verbose-headers
2018   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2019   "p" gnus-article-verify-x-pgp-sig
2020   "d" gnus-article-treat-dumbquotes
2021   "i" gnus-summary-idna-message)
2022
2023 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2024   ;; mnemonic: deuglif*Y*
2025   "u" gnus-article-outlook-unwrap-lines
2026   "a" gnus-article-outlook-repair-attribution
2027   "c" gnus-article-outlook-rearrange-citation
2028   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2029
2030 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2031   "a" gnus-article-hide
2032   "h" gnus-article-hide-headers
2033   "b" gnus-article-hide-boring-headers
2034   "s" gnus-article-hide-signature
2035   "c" gnus-article-hide-citation
2036   "C" gnus-article-hide-citation-in-followups
2037   "l" gnus-article-hide-list-identifiers
2038   "B" gnus-article-strip-banner
2039   "P" gnus-article-hide-pem
2040   "\C-c" gnus-article-hide-citation-maybe)
2041
2042 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2043   "a" gnus-article-highlight
2044   "h" gnus-article-highlight-headers
2045   "c" gnus-article-highlight-citation
2046   "s" gnus-article-highlight-signature)
2047
2048 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2049   "f" gnus-article-treat-fold-headers
2050   "u" gnus-article-treat-unfold-headers
2051   "n" gnus-article-treat-fold-newsgroups)
2052
2053 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2054   "x" gnus-article-display-x-face
2055   "d" gnus-article-display-face
2056   "s" gnus-treat-smiley
2057   "D" gnus-article-remove-images
2058   "f" gnus-treat-from-picon
2059   "m" gnus-treat-mail-picon
2060   "n" gnus-treat-newsgroups-picon)
2061
2062 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2063   "w" gnus-article-decode-mime-words
2064   "c" gnus-article-decode-charset
2065   "v" gnus-mime-view-all-parts
2066   "b" gnus-article-view-part)
2067
2068 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2069   "z" gnus-article-date-ut
2070   "u" gnus-article-date-ut
2071   "l" gnus-article-date-local
2072   "p" gnus-article-date-english
2073   "e" gnus-article-date-lapsed
2074   "o" gnus-article-date-original
2075   "i" gnus-article-date-iso8601
2076   "s" gnus-article-date-user)
2077
2078 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2079   "t" gnus-article-remove-trailing-blank-lines
2080   "l" gnus-article-strip-leading-blank-lines
2081   "m" gnus-article-strip-multiple-blank-lines
2082   "a" gnus-article-strip-blank-lines
2083   "A" gnus-article-strip-all-blank-lines
2084   "s" gnus-article-strip-leading-space
2085   "e" gnus-article-strip-trailing-space
2086   "w" gnus-article-remove-leading-whitespace)
2087
2088 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2089   "v" gnus-version
2090   "f" gnus-summary-fetch-faq
2091   "d" gnus-summary-describe-group
2092   "h" gnus-summary-describe-briefly
2093   "i" gnus-info-find-node
2094   "c" gnus-group-fetch-charter
2095   "C" gnus-group-fetch-control)
2096
2097 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2098   "e" gnus-summary-expire-articles
2099   "\M-\C-e" gnus-summary-expire-articles-now
2100   "\177" gnus-summary-delete-article
2101   [delete] gnus-summary-delete-article
2102   [backspace] gnus-summary-delete-article
2103   "m" gnus-summary-move-article
2104   "r" gnus-summary-respool-article
2105   "w" gnus-summary-edit-article
2106   "c" gnus-summary-copy-article
2107   "B" gnus-summary-crosspost-article
2108   "q" gnus-summary-respool-query
2109   "t" gnus-summary-respool-trace
2110   "i" gnus-summary-import-article
2111   "I" gnus-summary-create-article
2112   "p" gnus-summary-article-posted-p)
2113
2114 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2115   "o" gnus-summary-save-article
2116   "m" gnus-summary-save-article-mail
2117   "F" gnus-summary-write-article-file
2118   "r" gnus-summary-save-article-rmail
2119   "f" gnus-summary-save-article-file
2120   "b" gnus-summary-save-article-body-file
2121   "B" gnus-summary-write-article-body-file
2122   "h" gnus-summary-save-article-folder
2123   "v" gnus-summary-save-article-vm
2124   "p" gnus-summary-pipe-output
2125   "P" gnus-summary-muttprint
2126   "s" gnus-soup-add-article)
2127
2128 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2129   "b" gnus-summary-display-buttonized
2130   "m" gnus-summary-repair-multipart
2131   "v" gnus-article-view-part
2132   "o" gnus-article-save-part
2133   "O" gnus-article-save-part-and-strip
2134   "r" gnus-article-replace-part
2135   "d" gnus-article-delete-part
2136   "t" gnus-article-view-part-as-type
2137   "j" gnus-article-jump-to-part
2138   "c" gnus-article-copy-part
2139   "C" gnus-article-view-part-as-charset
2140   "e" gnus-article-view-part-externally
2141   "H" gnus-article-browse-html-article
2142   "E" gnus-article-encrypt-body
2143   "i" gnus-article-inline-part
2144   "|" gnus-article-pipe-part)
2145
2146 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2147   "p" gnus-summary-mark-as-processable
2148   "u" gnus-summary-unmark-as-processable
2149   "U" gnus-summary-unmark-all-processable
2150   "v" gnus-uu-mark-over
2151   "s" gnus-uu-mark-series
2152   "r" gnus-uu-mark-region
2153   "g" gnus-uu-unmark-region
2154   "R" gnus-uu-mark-by-regexp
2155   "G" gnus-uu-unmark-by-regexp
2156   "t" gnus-uu-mark-thread
2157   "T" gnus-uu-unmark-thread
2158   "a" gnus-uu-mark-all
2159   "b" gnus-uu-mark-buffer
2160   "S" gnus-uu-mark-sparse
2161   "k" gnus-summary-kill-process-mark
2162   "y" gnus-summary-yank-process-mark
2163   "w" gnus-summary-save-process-mark
2164   "i" gnus-uu-invert-processable)
2165
2166 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2167   ;;"x" gnus-uu-extract-any
2168   "m" gnus-summary-save-parts
2169   "u" gnus-uu-decode-uu
2170   "U" gnus-uu-decode-uu-and-save
2171   "s" gnus-uu-decode-unshar
2172   "S" gnus-uu-decode-unshar-and-save
2173   "o" gnus-uu-decode-save
2174   "O" gnus-uu-decode-save
2175   "b" gnus-uu-decode-binhex
2176   "B" gnus-uu-decode-binhex
2177   "p" gnus-uu-decode-postscript
2178   "P" gnus-uu-decode-postscript-and-save)
2179
2180 (gnus-define-keys
2181     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2182   "u" gnus-uu-decode-uu-view
2183   "U" gnus-uu-decode-uu-and-save-view
2184   "s" gnus-uu-decode-unshar-view
2185   "S" gnus-uu-decode-unshar-and-save-view
2186   "o" gnus-uu-decode-save-view
2187   "O" gnus-uu-decode-save-view
2188   "b" gnus-uu-decode-binhex-view
2189   "B" gnus-uu-decode-binhex-view
2190   "p" gnus-uu-decode-postscript-view
2191   "P" gnus-uu-decode-postscript-and-save-view)
2192
2193 (defvar gnus-article-post-menu nil)
2194
2195 (defconst gnus-summary-menu-maxlen 20)
2196
2197 (defun gnus-summary-menu-split (menu)
2198   ;; If we have lots of elements, divide them into groups of 20
2199   ;; and make a pane (or submenu) for each one.
2200   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2201       (let ((menu menu) sublists next
2202             (i 1))
2203         (while menu
2204           ;; Pull off the next gnus-summary-menu-maxlen elements
2205           ;; and make them the next element of sublist.
2206           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2207           (if next
2208               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2209                       nil))
2210           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2211                                              (aref (car (last menu)) 0)) menu)
2212                                sublists))
2213           (setq i (1+ i))
2214           (setq menu next))
2215         (nreverse sublists))
2216     ;; Few elements--put them all in one pane.
2217     menu))
2218
2219 (defun gnus-summary-make-menu-bar ()
2220   (gnus-turn-off-edit-menu 'summary)
2221
2222   (unless (boundp 'gnus-summary-misc-menu)
2223
2224     (easy-menu-define
2225       gnus-summary-kill-menu gnus-summary-mode-map ""
2226       (cons
2227        "Score"
2228        (nconc
2229         (list
2230          ["Customize" gnus-score-customize t])
2231         (gnus-make-score-map 'increase)
2232         (gnus-make-score-map 'lower)
2233         '(("Mark"
2234            ["Kill below" gnus-summary-kill-below t]
2235            ["Mark above" gnus-summary-mark-above t]
2236            ["Tick above" gnus-summary-tick-above t]
2237            ["Clear above" gnus-summary-clear-above t])
2238           ["Current score" gnus-summary-current-score t]
2239           ["Set score" gnus-summary-set-score t]
2240           ["Switch current score file..." gnus-score-change-score-file t]
2241           ["Set mark below..." gnus-score-set-mark-below t]
2242           ["Set expunge below..." gnus-score-set-expunge-below t]
2243           ["Edit current score file" gnus-score-edit-current-scores t]
2244           ["Edit score file" gnus-score-edit-file t]
2245           ["Trace score" gnus-score-find-trace t]
2246           ["Find words" gnus-score-find-favourite-words t]
2247           ["Rescore buffer" gnus-summary-rescore t]
2248           ["Increase score..." gnus-summary-increase-score t]
2249           ["Lower score..." gnus-summary-lower-score t]))))
2250
2251     ;; Define both the Article menu in the summary buffer and the
2252     ;; equivalent Commands menu in the article buffer here for
2253     ;; consistency.
2254     (let ((innards
2255            `(("Hide"
2256               ["All" gnus-article-hide t]
2257               ["Headers" gnus-article-hide-headers t]
2258               ["Signature" gnus-article-hide-signature t]
2259               ["Citation" gnus-article-hide-citation t]
2260               ["List identifiers" gnus-article-hide-list-identifiers t]
2261               ["Banner" gnus-article-strip-banner t]
2262               ["Boring headers" gnus-article-hide-boring-headers t])
2263              ("Highlight"
2264               ["All" gnus-article-highlight t]
2265               ["Headers" gnus-article-highlight-headers t]
2266               ["Signature" gnus-article-highlight-signature t]
2267               ["Citation" gnus-article-highlight-citation t])
2268              ("MIME"
2269               ["Words" gnus-article-decode-mime-words t]
2270               ["Charset" gnus-article-decode-charset t]
2271               ["QP" gnus-article-de-quoted-unreadable t]
2272               ["Base64" gnus-article-de-base64-unreadable t]
2273               ["View MIME buttons" gnus-summary-display-buttonized t]
2274               ["View all" gnus-mime-view-all-parts t]
2275               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2276               ["Encrypt body" gnus-article-encrypt-body
2277                :active (not (gnus-group-read-only-p))
2278                ,@(if (featurep 'xemacs) nil
2279                    '(:help "Encrypt the message body on disk"))]
2280               ["Extract all parts..." gnus-summary-save-parts t]
2281               ("Multipart"
2282                ["Repair multipart" gnus-summary-repair-multipart t]
2283                ["Pipe part..." gnus-article-pipe-part t]
2284                ["Inline part" gnus-article-inline-part t]
2285                ["View part as type..." gnus-article-view-part-as-type t]
2286                ["Encrypt body" gnus-article-encrypt-body
2287                 :active (not (gnus-group-read-only-p))
2288                ,@(if (featurep 'xemacs) nil
2289                    '(:help "Encrypt the message body on disk"))]
2290                ["View part externally" gnus-article-view-part-externally t]
2291                ["View HTML parts in browser" gnus-article-browse-html-article t]
2292                ["View part with charset..." gnus-article-view-part-as-charset t]
2293                ["Copy part" gnus-article-copy-part t]
2294                ["Save part..." gnus-article-save-part t]
2295                ["View part" gnus-article-view-part t]))
2296              ("Date"
2297               ["Local" gnus-article-date-local t]
2298               ["ISO8601" gnus-article-date-iso8601 t]
2299               ["UT" gnus-article-date-ut t]
2300               ["Original" gnus-article-date-original t]
2301               ["Lapsed" gnus-article-date-lapsed t]
2302               ["User-defined" gnus-article-date-user t])
2303              ("Display"
2304               ["Remove images" gnus-article-remove-images t]
2305               ["Toggle smiley" gnus-treat-smiley t]
2306               ["Show X-Face" gnus-article-display-x-face t]
2307               ["Show picons in From" gnus-treat-from-picon t]
2308               ["Show picons in mail headers" gnus-treat-mail-picon t]
2309               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2310               ("View as different encoding"
2311                ,@(gnus-summary-menu-split
2312                   (mapcar
2313                    (lambda (cs)
2314                      ;; Since easymenu under Emacs doesn't allow
2315                      ;; lambda forms for menu commands, we should
2316                      ;; provide intern'ed function symbols.
2317                      (let ((command (intern (format "\
2318 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2319                        (fset command
2320                              `(lambda ()
2321                                 (interactive)
2322                                 (let ((gnus-summary-show-article-charset-alist
2323                                        '((1 . ,cs))))
2324                                   (gnus-summary-show-article 1))))
2325                        `[,(symbol-name cs) ,command t]))
2326                    (sort (if (fboundp 'coding-system-list)
2327                              (coding-system-list)
2328                            (mapcar 'car mm-mime-mule-charset-alist))
2329                          'string<)))))
2330              ("Washing"
2331               ("Remove Blanks"
2332                ["Leading" gnus-article-strip-leading-blank-lines t]
2333                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2334                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2335                ["All of the above" gnus-article-strip-blank-lines t]
2336                ["All" gnus-article-strip-all-blank-lines t]
2337                ["Leading space" gnus-article-strip-leading-space t]
2338                ["Trailing space" gnus-article-strip-trailing-space t]
2339                ["Leading space in headers"
2340                 gnus-article-remove-leading-whitespace t])
2341               ["Overstrike" gnus-article-treat-overstrike t]
2342               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2343               ["Emphasis" gnus-article-emphasize t]
2344               ["Word wrap" gnus-article-fill-cited-article t]
2345               ["Fill long lines" gnus-article-fill-long-lines t]
2346               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2347               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2348               ["Remove CR" gnus-article-remove-cr t]
2349               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2350               ["Base64" gnus-article-de-base64-unreadable t]
2351               ["Rot 13" gnus-summary-caesar-message
2352                ,@(if (featurep 'xemacs) '(t)
2353                    '(:help "\"Caesar rotate\" article by 13"))]
2354               ["De-IDNA" gnus-summary-idna-message t]
2355               ["Morse decode" gnus-summary-morse-message t]
2356               ["Unix pipe..." gnus-summary-pipe-message t]
2357               ["Add buttons" gnus-article-add-buttons t]
2358               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2359               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2360               ["Verbose header" gnus-summary-verbose-headers t]
2361               ["Toggle header" gnus-summary-toggle-header t]
2362               ["Unfold headers" gnus-article-treat-unfold-headers t]
2363               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2364               ["Html" gnus-article-wash-html t]
2365               ["Unsplit URLs" gnus-article-unsplit-urls t]
2366               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2367               ["Decode HZ" gnus-article-decode-HZ t]
2368               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2369               ("(Outlook) Deuglify"
2370                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2371                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2372                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2373                ["Full (Outlook) deuglify"
2374                 gnus-article-outlook-deuglify-article t])
2375               )
2376              ("Output"
2377               ["Save in default format..." gnus-summary-save-article
2378                ,@(if (featurep 'xemacs) '(t)
2379                    '(:help "Save article using default method"))]
2380               ["Save in file..." gnus-summary-save-article-file
2381                ,@(if (featurep 'xemacs) '(t)
2382                    '(:help "Save article in file"))]
2383               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2384               ["Save in MH folder..." gnus-summary-save-article-folder t]
2385               ["Save in VM folder..." gnus-summary-save-article-vm t]
2386               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2387               ["Save body in file..." gnus-summary-save-article-body-file t]
2388               ["Pipe through a filter..." gnus-summary-pipe-output t]
2389               ["Add to SOUP packet" gnus-soup-add-article t]
2390               ["Print with Muttprint..." gnus-summary-muttprint t]
2391               ["Print" gnus-summary-print-article
2392                ,@(if (featurep 'xemacs) '(t)
2393                    '(:help "Generate and print a PostScript image"))])
2394              ("Copy, move,... (Backend)"
2395               ,@(if (featurep 'xemacs) nil
2396                   '(:help "Copying, moving, expiring articles..."))
2397               ["Respool article..." gnus-summary-respool-article t]
2398               ["Move article..." gnus-summary-move-article
2399                (gnus-check-backend-function
2400                 'request-move-article gnus-newsgroup-name)]
2401               ["Copy article..." gnus-summary-copy-article t]
2402               ["Crosspost article..." gnus-summary-crosspost-article
2403                (gnus-check-backend-function
2404                 'request-replace-article gnus-newsgroup-name)]
2405               ["Import file..." gnus-summary-import-article
2406                (gnus-check-backend-function
2407                 'request-accept-article gnus-newsgroup-name)]
2408               ["Create article..." gnus-summary-create-article
2409                (gnus-check-backend-function
2410                 'request-accept-article gnus-newsgroup-name)]
2411               ["Check if posted" gnus-summary-article-posted-p t]
2412               ["Edit article" gnus-summary-edit-article
2413                (not (gnus-group-read-only-p))]
2414               ["Delete article" gnus-summary-delete-article
2415                (gnus-check-backend-function
2416                 'request-expire-articles gnus-newsgroup-name)]
2417               ["Query respool" gnus-summary-respool-query t]
2418               ["Trace respool" gnus-summary-respool-trace t]
2419               ["Delete expirable articles" gnus-summary-expire-articles-now
2420                (gnus-check-backend-function
2421                 'request-expire-articles gnus-newsgroup-name)])
2422              ("Extract"
2423               ["Uudecode" gnus-uu-decode-uu
2424                ,@(if (featurep 'xemacs) '(t)
2425                    '(:help "Decode uuencoded article(s)"))]
2426               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2427               ["Unshar" gnus-uu-decode-unshar t]
2428               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2429               ["Save" gnus-uu-decode-save t]
2430               ["Binhex" gnus-uu-decode-binhex t]
2431               ["Postscript" gnus-uu-decode-postscript t]
2432               ["All MIME parts" gnus-summary-save-parts t])
2433              ("Cache"
2434               ["Enter article" gnus-cache-enter-article t]
2435               ["Remove article" gnus-cache-remove-article t])
2436              ["Translate" gnus-article-babel t]
2437              ["Select article buffer" gnus-summary-select-article-buffer t]
2438              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2439              ["Isearch article..." gnus-summary-isearch-article t]
2440              ["Beginning of the article" gnus-summary-beginning-of-article t]
2441              ["End of the article" gnus-summary-end-of-article t]
2442              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2443              ["Fetch referenced articles" gnus-summary-refer-references t]
2444              ["Fetch current thread" gnus-summary-refer-thread t]
2445              ["Fetch article with id..." gnus-summary-refer-article t]
2446              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2447              ["Redisplay" gnus-summary-show-article t]
2448              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2449       (easy-menu-define
2450         gnus-summary-article-menu gnus-summary-mode-map ""
2451         (cons "Article" innards))
2452
2453       (if (not (keymapp gnus-summary-article-menu))
2454           (easy-menu-define
2455             gnus-article-commands-menu gnus-article-mode-map ""
2456             (cons "Commands" innards))
2457         ;; in Emacs, don't share menu.
2458         (setq gnus-article-commands-menu
2459               (copy-keymap gnus-summary-article-menu))
2460         (define-key gnus-article-mode-map [menu-bar commands]
2461           (cons "Commands" gnus-article-commands-menu))))
2462
2463     (easy-menu-define
2464       gnus-summary-thread-menu gnus-summary-mode-map ""
2465       '("Threads"
2466         ["Find all messages in thread" gnus-summary-refer-thread t]
2467         ["Toggle threading" gnus-summary-toggle-threads t]
2468         ["Hide threads" gnus-summary-hide-all-threads t]
2469         ["Show threads" gnus-summary-show-all-threads t]
2470         ["Hide thread" gnus-summary-hide-thread t]
2471         ["Show thread" gnus-summary-show-thread t]
2472         ["Go to next thread" gnus-summary-next-thread t]
2473         ["Go to previous thread" gnus-summary-prev-thread t]
2474         ["Go down thread" gnus-summary-down-thread t]
2475         ["Go up thread" gnus-summary-up-thread t]
2476         ["Top of thread" gnus-summary-top-thread t]
2477         ["Mark thread as read" gnus-summary-kill-thread t]
2478         ["Lower thread score" gnus-summary-lower-thread t]
2479         ["Raise thread score" gnus-summary-raise-thread t]
2480         ["Rethread current" gnus-summary-rethread-current t]))
2481
2482     (easy-menu-define
2483       gnus-summary-post-menu gnus-summary-mode-map ""
2484       `("Post"
2485         ["Send a message (mail or news)" gnus-summary-post-news
2486          ,@(if (featurep 'xemacs) '(t)
2487              '(:help "Compose a new message (mail or news)"))]
2488         ["Followup" gnus-summary-followup
2489          ,@(if (featurep 'xemacs) '(t)
2490              '(:help "Post followup to this article"))]
2491         ["Followup and yank" gnus-summary-followup-with-original
2492          ,@(if (featurep 'xemacs) '(t)
2493              '(:help "Post followup to this article, quoting its contents"))]
2494         ["Supersede article" gnus-summary-supersede-article t]
2495         ["Cancel article" gnus-summary-cancel-article
2496          ,@(if (featurep 'xemacs) '(t)
2497              '(:help "Cancel an article you posted"))]
2498         ["Reply" gnus-summary-reply t]
2499         ["Reply and yank" gnus-summary-reply-with-original t]
2500         ["Wide reply" gnus-summary-wide-reply t]
2501         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2502          ,@(if (featurep 'xemacs) '(t)
2503              '(:help "Mail a reply, quoting this article"))]
2504         ["Very wide reply" gnus-summary-very-wide-reply t]
2505         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2506          ,@(if (featurep 'xemacs) '(t)
2507              '(:help "Mail a very wide reply, quoting this article"))]
2508         ["Mail forward" gnus-summary-mail-forward t]
2509         ["Post forward" gnus-summary-post-forward t]
2510         ["Digest and mail" gnus-uu-digest-mail-forward t]
2511         ["Digest and post" gnus-uu-digest-post-forward t]
2512         ["Resend message" gnus-summary-resend-message t]
2513         ["Resend message edit" gnus-summary-resend-message-edit t]
2514         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2515         ["Send a mail" gnus-summary-mail-other-window t]
2516         ["Create a local message" gnus-summary-news-other-window t]
2517         ["Uuencode and post" gnus-uu-post-news
2518          ,@(if (featurep 'xemacs) '(t)
2519              '(:help "Post a uuencoded article"))]
2520         ["Followup via news" gnus-summary-followup-to-mail t]
2521         ["Followup via news and yank"
2522          gnus-summary-followup-to-mail-with-original t]
2523         ;;("Draft"
2524         ;;["Send" gnus-summary-send-draft t]
2525         ;;["Send bounced" gnus-resend-bounced-mail t])
2526         ))
2527
2528     (cond
2529      ((not (keymapp gnus-summary-post-menu))
2530       (setq gnus-article-post-menu gnus-summary-post-menu))
2531      ((not gnus-article-post-menu)
2532       ;; Don't share post menu.
2533       (setq gnus-article-post-menu
2534             (copy-keymap gnus-summary-post-menu))))
2535     (define-key gnus-article-mode-map [menu-bar post]
2536       (cons "Post" gnus-article-post-menu))
2537
2538     (easy-menu-define
2539       gnus-summary-misc-menu gnus-summary-mode-map ""
2540       `("Gnus"
2541         ("Mark Read"
2542          ["Mark as read" gnus-summary-mark-as-read-forward t]
2543          ["Mark same subject and select"
2544           gnus-summary-kill-same-subject-and-select t]
2545          ["Mark same subject" gnus-summary-kill-same-subject t]
2546          ["Catchup" gnus-summary-catchup
2547           ,@(if (featurep 'xemacs) '(t)
2548               '(:help "Mark unread articles in this group as read"))]
2549          ["Catchup all" gnus-summary-catchup-all t]
2550          ["Catchup to here" gnus-summary-catchup-to-here t]
2551          ["Catchup from here" gnus-summary-catchup-from-here t]
2552          ["Catchup region" gnus-summary-mark-region-as-read
2553           (gnus-mark-active-p)]
2554          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2555         ("Mark Various"
2556          ["Tick" gnus-summary-tick-article-forward t]
2557          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2558          ["Remove marks" gnus-summary-clear-mark-forward t]
2559          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2560          ["Set bookmark" gnus-summary-set-bookmark t]
2561          ["Remove bookmark" gnus-summary-remove-bookmark t])
2562         ("Limit to"
2563          ["Marks..." gnus-summary-limit-to-marks t]
2564          ["Subject..." gnus-summary-limit-to-subject t]
2565          ["Author..." gnus-summary-limit-to-author t]
2566          ["Recipient..." gnus-summary-limit-to-recipient t]
2567          ["Age..." gnus-summary-limit-to-age t]
2568          ["Extra..." gnus-summary-limit-to-extra t]
2569          ["Score..." gnus-summary-limit-to-score t]
2570          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2571          ["Unread" gnus-summary-limit-to-unread t]
2572          ["Unseen" gnus-summary-limit-to-unseen t]
2573          ["Singletons" gnus-summary-limit-to-singletons t]
2574          ["Replied" gnus-summary-limit-to-replied t]
2575          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2576          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2577          ["Pop limit" gnus-summary-pop-limit t]
2578          ["Show dormant" gnus-summary-limit-include-dormant t]
2579          ["Hide childless dormant"
2580           gnus-summary-limit-exclude-childless-dormant t]
2581          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2582          ["Hide marked" gnus-summary-limit-exclude-marks t]
2583          ["Show expunged" gnus-summary-limit-include-expunged t])
2584         ("Process Mark"
2585          ["Set mark" gnus-summary-mark-as-processable t]
2586          ["Remove mark" gnus-summary-unmark-as-processable t]
2587          ["Remove all marks" gnus-summary-unmark-all-processable t]
2588          ["Invert marks" gnus-uu-invert-processable t]
2589          ["Mark above" gnus-uu-mark-over t]
2590          ["Mark series" gnus-uu-mark-series t]
2591          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2592          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2593          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2594          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2595          ["Mark all" gnus-uu-mark-all t]
2596          ["Mark buffer" gnus-uu-mark-buffer t]
2597          ["Mark sparse" gnus-uu-mark-sparse t]
2598          ["Mark thread" gnus-uu-mark-thread t]
2599          ["Unmark thread" gnus-uu-unmark-thread t]
2600          ("Process Mark Sets"
2601           ["Kill" gnus-summary-kill-process-mark t]
2602           ["Yank" gnus-summary-yank-process-mark
2603            gnus-newsgroup-process-stack]
2604           ["Save" gnus-summary-save-process-mark t]
2605           ["Run command on marked..." gnus-summary-universal-argument t]))
2606         ("Scroll article"
2607          ["Page forward" gnus-summary-next-page
2608           ,@(if (featurep 'xemacs) '(t)
2609               '(:help "Show next page of article"))]
2610          ["Page backward" gnus-summary-prev-page
2611           ,@(if (featurep 'xemacs) '(t)
2612               '(:help "Show previous page of article"))]
2613          ["Line forward" gnus-summary-scroll-up t])
2614         ("Move"
2615          ["Next unread article" gnus-summary-next-unread-article t]
2616          ["Previous unread article" gnus-summary-prev-unread-article t]
2617          ["Next article" gnus-summary-next-article t]
2618          ["Previous article" gnus-summary-prev-article t]
2619          ["Next unread subject" gnus-summary-next-unread-subject t]
2620          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2621          ["Next article same subject" gnus-summary-next-same-subject t]
2622          ["Previous article same subject" gnus-summary-prev-same-subject t]
2623          ["First unread article" gnus-summary-first-unread-article t]
2624          ["Best unread article" gnus-summary-best-unread-article t]
2625          ["Go to subject number..." gnus-summary-goto-subject t]
2626          ["Go to article number..." gnus-summary-goto-article t]
2627          ["Go to the last article" gnus-summary-goto-last-article t]
2628          ["Pop article off history" gnus-summary-pop-article t])
2629         ("Sort"
2630          ["Sort by number" gnus-summary-sort-by-number t]
2631          ["Sort by author" gnus-summary-sort-by-author t]
2632          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2633          ["Sort by subject" gnus-summary-sort-by-subject t]
2634          ["Sort by date" gnus-summary-sort-by-date t]
2635          ["Sort by score" gnus-summary-sort-by-score t]
2636          ["Sort by lines" gnus-summary-sort-by-lines t]
2637          ["Sort by characters" gnus-summary-sort-by-chars t]
2638          ["Randomize" gnus-summary-sort-by-random t]
2639          ["Original sort" gnus-summary-sort-by-original t])
2640         ("Help"
2641          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2642          ["Describe group" gnus-summary-describe-group t]
2643          ["Fetch charter" gnus-group-fetch-charter
2644           ,@(if (featurep 'xemacs) nil
2645               '(:help "Display the charter of the current group"))]
2646          ["Fetch control message" gnus-group-fetch-control
2647           ,@(if (featurep 'xemacs) nil
2648               '(:help "Display the archived control message for the current group"))]
2649          ["Read manual" gnus-info-find-node t])
2650         ("Modes"
2651          ["Pick and read" gnus-pick-mode t]
2652          ["Binary" gnus-binary-mode t])
2653         ("Regeneration"
2654          ["Regenerate" gnus-summary-prepare t]
2655          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2656          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2657          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2658          ["Toggle threading" gnus-summary-toggle-threads t])
2659         ["See old articles" gnus-summary-insert-old-articles t]
2660         ["See new articles" gnus-summary-insert-new-articles t]
2661         ["Filter articles..." gnus-summary-execute-command t]
2662         ["Run command on articles..." gnus-summary-universal-argument t]
2663         ["Search articles forward..." gnus-summary-search-article-forward t]
2664         ["Search articles backward..." gnus-summary-search-article-backward t]
2665         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2666         ["Expand window" gnus-summary-expand-window t]
2667         ["Expire expirable articles" gnus-summary-expire-articles
2668          (gnus-check-backend-function
2669           'request-expire-articles gnus-newsgroup-name)]
2670         ["Edit local kill file" gnus-summary-edit-local-kill t]
2671         ["Edit main kill file" gnus-summary-edit-global-kill t]
2672         ["Edit group parameters" gnus-summary-edit-parameters t]
2673         ["Customize group parameters" gnus-summary-customize-parameters t]
2674         ["Send a bug report" gnus-bug t]
2675         ("Exit"
2676          ["Catchup and exit" gnus-summary-catchup-and-exit
2677           ,@(if (featurep 'xemacs) '(t)
2678               '(:help "Mark unread articles in this group as read, then exit"))]
2679          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2680          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2681          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2682          ["Exit group" gnus-summary-exit
2683           ,@(if (featurep 'xemacs) '(t)
2684               '(:help "Exit current group, return to group selection mode"))]
2685          ["Exit group without updating" gnus-summary-exit-no-update t]
2686          ["Exit and goto next group" gnus-summary-next-group t]
2687          ["Exit and goto prev group" gnus-summary-prev-group t]
2688          ["Reselect group" gnus-summary-reselect-current-group t]
2689          ["Rescan group" gnus-summary-rescan-group t]
2690          ["Update dribble" gnus-summary-save-newsrc t])))
2691
2692     (gnus-run-hooks 'gnus-summary-menu-hook)))
2693
2694 (defvar gnus-summary-tool-bar-map nil)
2695
2696 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2697 ;; affect _new_ message buffers.  We might add a function that walks thru all
2698 ;; summary-mode buffers and force the update.
2699 (defun gnus-summary-tool-bar-update (&optional symbol value)
2700   "Update summary mode toolbar.
2701 Setter function for custom variables."
2702   (setq-default gnus-summary-tool-bar-map nil)
2703   (when symbol
2704     ;; When used as ":set" function:
2705     (set-default symbol value))
2706   (when (gnus-buffer-live-p gnus-summary-buffer)
2707     (with-current-buffer gnus-summary-buffer
2708       (gnus-summary-make-tool-bar))))
2709
2710 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2711                                      'gnus-summary-tool-bar-gnome
2712                                    'gnus-summary-tool-bar-retro)
2713   "Specifies the Gnus summary tool bar.
2714
2715 It can be either a list or a symbol refering to a list.  See
2716 `gmm-tool-bar-from-list' for the format of the list.  The
2717 default key map is `gnus-summary-mode-map'.
2718
2719 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2720 `gnus-summary-tool-bar-retro'."
2721   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2722                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2723                  (repeat :tag "User defined list" gmm-tool-bar-item)
2724                  (symbol))
2725   :version "23.0" ;; No Gnus
2726   :initialize 'custom-initialize-default
2727   :set 'gnus-summary-tool-bar-update
2728   :group 'gnus-summary)
2729
2730 (defcustom gnus-summary-tool-bar-gnome
2731   '((gnus-summary-post-news "mail/compose" nil)
2732     (gnus-summary-insert-new-articles "mail/inbox" nil
2733                                       :visible (or (not gnus-agent)
2734                                                    gnus-plugged))
2735     (gnus-summary-reply-with-original "mail/reply")
2736     (gnus-summary-reply "mail/reply" nil :visible nil)
2737     (gnus-summary-followup-with-original "mail/reply-all")
2738     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2739     (gnus-summary-mail-forward "mail/forward")
2740     (gnus-summary-save-article "mail/save")
2741     (gnus-summary-search-article-forward "search" nil :visible nil)
2742     (gnus-summary-print-article "print")
2743     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2744     ;; Some new commands that may need more suitable icons:
2745     (gnus-summary-save-newsrc "save" nil :visible nil)
2746     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2747     (gnus-summary-prev-article "left-arrow")
2748     (gnus-summary-next-article "right-arrow")
2749     (gnus-summary-next-page "next-page")
2750     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2751     ;;
2752     ;; Maybe some sort-by-... could be added:
2753     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2754     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2755     (gnus-summary-mark-as-expirable
2756      "delete" nil
2757      :visible (gnus-check-backend-function 'request-expire-articles
2758                                            gnus-newsgroup-name))
2759     (gnus-summary-mark-as-spam
2760      "mail/spam" t
2761      :visible (and (fboundp 'spam-group-ham-contents-p)
2762                    (spam-group-ham-contents-p gnus-newsgroup-name))
2763      :help "Mark as spam")
2764     (gnus-summary-mark-as-read-forward
2765      "mail/not-spam" nil
2766      :visible (and (fboundp 'spam-group-spam-contents-p)
2767                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2768     ;;
2769     (gnus-summary-exit "exit")
2770     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2771     (gnus-info-find-node "help"))
2772   "List of functions for the summary tool bar (GNOME style).
2773
2774 See `gmm-tool-bar-from-list' for the format of the list."
2775   :type '(repeat gmm-tool-bar-item)
2776   :version "23.0" ;; No Gnus
2777   :initialize 'custom-initialize-default
2778   :set 'gnus-summary-tool-bar-update
2779   :group 'gnus-summary)
2780
2781 (defcustom gnus-summary-tool-bar-retro
2782   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2783     (gnus-summary-next-unread-article "gnus/next-ur")
2784     (gnus-summary-post-news "gnus/post")
2785     (gnus-summary-followup-with-original "gnus/fuwo")
2786     (gnus-summary-followup "gnus/followup")
2787     (gnus-summary-reply-with-original "gnus/reply-wo")
2788     (gnus-summary-reply "gnus/reply")
2789     (gnus-summary-caesar-message "gnus/rot13")
2790     (gnus-uu-decode-uu "gnus/uu-decode")
2791     (gnus-summary-save-article-file "gnus/save-aif")
2792     (gnus-summary-save-article "gnus/save-art")
2793     (gnus-uu-post-news "gnus/uu-post")
2794     (gnus-summary-catchup "gnus/catchup")
2795     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2796     (gnus-summary-exit "gnus/exit-summ")
2797     ;; Some new command that may need more suitable icons:
2798     (gnus-summary-print-article "gnus/print" nil :visible nil)
2799     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2800     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2801     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2802     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2803     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2804     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2805     ;;
2806     (gnus-info-find-node "gnus/help" nil :visible nil))
2807   "List of functions for the summary tool bar (retro look).
2808
2809 See `gmm-tool-bar-from-list' for the format of the list."
2810   :type '(repeat gmm-tool-bar-item)
2811   :version "23.0" ;; No Gnus
2812   :initialize 'custom-initialize-default
2813   :set 'gnus-summary-tool-bar-update
2814   :group 'gnus-summary)
2815
2816 (defcustom gnus-summary-tool-bar-zap-list t
2817   "List of icon items from the global tool bar.
2818 These items are not displayed in the Gnus summary mode tool bar.
2819
2820 See `gmm-tool-bar-from-list' for the format of the list."
2821   :type 'gmm-tool-bar-zap-list
2822   :version "23.0" ;; No Gnus
2823   :initialize 'custom-initialize-default
2824   :set 'gnus-summary-tool-bar-update
2825   :group 'gnus-summary)
2826
2827 (defvar image-load-path)
2828
2829 (defun gnus-summary-make-tool-bar (&optional force)
2830   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2831 When FORCE, rebuild the tool bar."
2832   (when (and (not (featurep 'xemacs))
2833              (boundp 'tool-bar-mode)
2834              tool-bar-mode
2835              (or (not gnus-summary-tool-bar-map) force))
2836     (let* ((load-path
2837             (gmm-image-load-path-for-library "gnus"
2838                                              "mail/save.xpm"
2839                                              nil t))
2840            (image-load-path (cons (car load-path)
2841                                   (when (boundp 'image-load-path)
2842                                     image-load-path)))
2843            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2844                                         gnus-summary-tool-bar-zap-list
2845                                         'gnus-summary-mode-map)))
2846       (when map
2847         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2848         ;; uses it's value.
2849         (setq gnus-summary-tool-bar-map map))))
2850   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2851
2852 (defun gnus-score-set-default (var value)
2853   "A version of set that updates the GNU Emacs menu-bar."
2854   (set var value)
2855   ;; It is the message that forces the active status to be updated.
2856   (message ""))
2857
2858 (defun gnus-make-score-map (type)
2859   "Make a summary score map of type TYPE."
2860   (if t
2861       nil
2862     (let ((headers '(("author" "from" string)
2863                      ("subject" "subject" string)
2864                      ("article body" "body" string)
2865                      ("article head" "head" string)
2866                      ("xref" "xref" string)
2867                      ("extra header" "extra" string)
2868                      ("lines" "lines" number)
2869                      ("followups to author" "followup" string)))
2870           (types '((number ("less than" <)
2871                            ("greater than" >)
2872                            ("equal" =))
2873                    (string ("substring" s)
2874                            ("exact string" e)
2875                            ("fuzzy string" f)
2876                            ("regexp" r))))
2877           (perms '(("temporary" (current-time-string))
2878                    ("permanent" nil)
2879                    ("immediate" now)))
2880           header)
2881       (list
2882        (apply
2883         'nconc
2884         (list
2885          (if (eq type 'lower)
2886              "Lower score"
2887            "Increase score"))
2888         (let (outh)
2889           (while headers
2890             (setq header (car headers))
2891             (setq outh
2892                   (cons
2893                    (apply
2894                     'nconc
2895                     (list (car header))
2896                     (let ((ts (cdr (assoc (nth 2 header) types)))
2897                           outt)
2898                       (while ts
2899                         (setq outt
2900                               (cons
2901                                (apply
2902                                 'nconc
2903                                 (list (caar ts))
2904                                 (let ((ps perms)
2905                                       outp)
2906                                   (while ps
2907                                     (setq outp
2908                                           (cons
2909                                            (vector
2910                                             (caar ps)
2911                                             (list
2912                                              'gnus-summary-score-entry
2913                                              (nth 1 header)
2914                                              (if (or (string= (nth 1 header)
2915                                                               "head")
2916                                                      (string= (nth 1 header)
2917                                                               "body"))
2918                                                  ""
2919                                                (list 'gnus-summary-header
2920                                                      (nth 1 header)))
2921                                              (list 'quote (nth 1 (car ts)))
2922                                              (list 'gnus-score-delta-default
2923                                                    nil)
2924                                              (nth 1 (car ps))
2925                                              t)
2926                                             t)
2927                                            outp))
2928                                     (setq ps (cdr ps)))
2929                                   (list (nreverse outp))))
2930                                outt))
2931                         (setq ts (cdr ts)))
2932                       (list (nreverse outt))))
2933                    outh))
2934             (setq headers (cdr headers)))
2935           (list (nreverse outh))))))))
2936
2937 \f
2938
2939 (defun gnus-summary-mode (&optional group)
2940   "Major mode for reading articles.
2941
2942 All normal editing commands are switched off.
2943 \\<gnus-summary-mode-map>
2944 Each line in this buffer represents one article.  To read an
2945 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2946 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2947 respectively.
2948
2949 You can also post articles and send mail from this buffer.  To
2950 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2951 of an article, type `\\[gnus-summary-reply]'.
2952
2953 There are approx. one gazillion commands you can execute in this
2954 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2955
2956 The following commands are available:
2957
2958 \\{gnus-summary-mode-map}"
2959   (interactive)
2960   (kill-all-local-variables)
2961   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2962     (gnus-summary-make-local-variables))
2963   (gnus-summary-make-local-variables)
2964   (setq gnus-newsgroup-name group)
2965   (when (gnus-visual-p 'summary-menu 'menu)
2966     (gnus-summary-make-menu-bar)
2967     (gnus-summary-make-tool-bar))
2968   (gnus-make-thread-indent-array)
2969   (gnus-simplify-mode-line)
2970   (setq major-mode 'gnus-summary-mode)
2971   (setq mode-name "Summary")
2972   (make-local-variable 'minor-mode-alist)
2973   (use-local-map gnus-summary-mode-map)
2974   (buffer-disable-undo)
2975   (setq buffer-read-only t              ;Disable modification
2976         show-trailing-whitespace nil)
2977   (setq truncate-lines t)
2978   (setq selective-display t)
2979   (setq selective-display-ellipses t)   ;Display `...'
2980   (gnus-summary-set-display-table)
2981   (gnus-set-default-directory)
2982   (make-local-variable 'gnus-summary-line-format)
2983   (make-local-variable 'gnus-summary-line-format-spec)
2984   (make-local-variable 'gnus-summary-dummy-line-format)
2985   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2986   (make-local-variable 'gnus-summary-mark-positions)
2987   (gnus-make-local-hook 'pre-command-hook)
2988   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2989   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2990   (turn-on-gnus-mailing-list-mode)
2991   (mm-enable-multibyte)
2992   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2993   (gnus-update-summary-mark-positions))
2994
2995 (defun gnus-summary-make-local-variables ()
2996   "Make all the local summary buffer variables."
2997   (let (global)
2998     (dolist (local gnus-summary-local-variables)
2999       (if (consp local)
3000           (progn
3001             (if (eq (cdr local) 'global)
3002                 ;; Copy the global value of the variable.
3003                 (setq global (symbol-value (car local)))
3004               ;; Use the value from the list.
3005               (setq global (eval (cdr local))))
3006             (set (make-local-variable (car local)) global))
3007         ;; Simple nil-valued local variable.
3008         (set (make-local-variable local) nil)))))
3009
3010 (defun gnus-summary-clear-local-variables ()
3011   (let ((locals gnus-summary-local-variables))
3012     (while locals
3013       (if (consp (car locals))
3014           (and (symbolp (caar locals))
3015                (set (caar locals) nil))
3016         (and (symbolp (car locals))
3017              (set (car locals) nil)))
3018       (setq locals (cdr locals)))))
3019
3020 ;; Summary data functions.
3021
3022 (defmacro gnus-data-number (data)
3023   `(car ,data))
3024
3025 (defmacro gnus-data-set-number (data number)
3026   `(setcar ,data ,number))
3027
3028 (defmacro gnus-data-mark (data)
3029   `(nth 1 ,data))
3030
3031 (defmacro gnus-data-set-mark (data mark)
3032   `(setcar (nthcdr 1 ,data) ,mark))
3033
3034 (defmacro gnus-data-pos (data)
3035   `(nth 2 ,data))
3036
3037 (defmacro gnus-data-set-pos (data pos)
3038   `(setcar (nthcdr 2 ,data) ,pos))
3039
3040 (defmacro gnus-data-header (data)
3041   `(nth 3 ,data))
3042
3043 (defmacro gnus-data-set-header (data header)
3044   `(setf (nth 3 ,data) ,header))
3045
3046 (defmacro gnus-data-level (data)
3047   `(nth 4 ,data))
3048
3049 (defmacro gnus-data-unread-p (data)
3050   `(= (nth 1 ,data) gnus-unread-mark))
3051
3052 (defmacro gnus-data-read-p (data)
3053   `(/= (nth 1 ,data) gnus-unread-mark))
3054
3055 (defmacro gnus-data-pseudo-p (data)
3056   `(consp (nth 3 ,data)))
3057
3058 (defmacro gnus-data-find (number)
3059   `(assq ,number gnus-newsgroup-data))
3060
3061 (defmacro gnus-data-find-list (number &optional data)
3062   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3063      (memq (assq ,number bdata)
3064            bdata)))
3065
3066 (defmacro gnus-data-make (number mark pos header level)
3067   `(list ,number ,mark ,pos ,header ,level))
3068
3069 (defun gnus-data-enter (after-article number mark pos header level offset)
3070   (let ((data (gnus-data-find-list after-article)))
3071     (unless data
3072       (error "No such article: %d" after-article))
3073     (setcdr data (cons (gnus-data-make number mark pos header level)
3074                        (cdr data)))
3075     (setq gnus-newsgroup-data-reverse nil)
3076     (gnus-data-update-list (cddr data) offset)))
3077
3078 (defun gnus-data-enter-list (after-article list &optional offset)
3079   (when list
3080     (let ((data (and after-article (gnus-data-find-list after-article)))
3081           (ilist list))
3082       (if (not (or data
3083                    after-article))
3084           (let ((odata gnus-newsgroup-data))
3085             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3086             (when offset
3087               (gnus-data-update-list odata offset)))
3088         ;; Find the last element in the list to be spliced into the main
3089         ;; list.
3090         (setq list (last list))
3091         (if (not data)
3092             (progn
3093               (setcdr list gnus-newsgroup-data)
3094               (setq gnus-newsgroup-data ilist)
3095               (when offset
3096                 (gnus-data-update-list (cdr list) offset)))
3097           (setcdr list (cdr data))
3098           (setcdr data ilist)
3099           (when offset
3100             (gnus-data-update-list (cdr list) offset))))
3101       (setq gnus-newsgroup-data-reverse nil))))
3102
3103 (defun gnus-data-remove (article &optional offset)
3104   (let ((data gnus-newsgroup-data))
3105     (if (= (gnus-data-number (car data)) article)
3106         (progn
3107           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3108                 gnus-newsgroup-data-reverse nil)
3109           (when offset
3110             (gnus-data-update-list gnus-newsgroup-data offset)))
3111       (while (cdr data)
3112         (when (= (gnus-data-number (cadr data)) article)
3113           (setcdr data (cddr data))
3114           (when offset
3115             (gnus-data-update-list (cdr data) offset))
3116           (setq data nil
3117                 gnus-newsgroup-data-reverse nil))
3118         (setq data (cdr data))))))
3119
3120 (defmacro gnus-data-list (backward)
3121   `(if ,backward
3122        (or gnus-newsgroup-data-reverse
3123            (setq gnus-newsgroup-data-reverse
3124                  (reverse gnus-newsgroup-data)))
3125      gnus-newsgroup-data))
3126
3127 (defun gnus-data-update-list (data offset)
3128   "Add OFFSET to the POS of all data entries in DATA."
3129   (setq gnus-newsgroup-data-reverse nil)
3130   (while data
3131     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3132     (setq data (cdr data))))
3133
3134 (defun gnus-summary-article-pseudo-p (article)
3135   "Say whether this article is a pseudo article or not."
3136   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3137
3138 (defmacro gnus-summary-article-sparse-p (article)
3139   "Say whether this article is a sparse article or not."
3140   `(memq ,article gnus-newsgroup-sparse))
3141
3142 (defmacro gnus-summary-article-ancient-p (article)
3143   "Say whether this article is a sparse article or not."
3144   `(memq ,article gnus-newsgroup-ancient))
3145
3146 (defun gnus-article-parent-p (number)
3147   "Say whether this article is a parent or not."
3148   (let ((data (gnus-data-find-list number)))
3149     (and (cdr data)              ; There has to be an article after...
3150          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3151             (gnus-data-level (nth 1 data))))))
3152
3153 (defun gnus-article-children (number)
3154   "Return a list of all children to NUMBER."
3155   (let* ((data (gnus-data-find-list number))
3156          (level (gnus-data-level (car data)))
3157          children)
3158     (setq data (cdr data))
3159     (while (and data
3160                 (= (gnus-data-level (car data)) (1+ level)))
3161       (push (gnus-data-number (car data)) children)
3162       (setq data (cdr data)))
3163     children))
3164
3165 (defmacro gnus-summary-skip-intangible ()
3166   "If the current article is intangible, then jump to a different article."
3167   '(let ((to (get-text-property (point) 'gnus-intangible)))
3168      (and to (gnus-summary-goto-subject to))))
3169
3170 (defmacro gnus-summary-article-intangible-p ()
3171   "Say whether this article is intangible or not."
3172   '(get-text-property (point) 'gnus-intangible))
3173
3174 (defun gnus-article-read-p (article)
3175   "Say whether ARTICLE is read or not."
3176   (not (or (memq article gnus-newsgroup-marked)
3177            (memq article gnus-newsgroup-spam-marked)
3178            (memq article gnus-newsgroup-unreads)
3179            (memq article gnus-newsgroup-unselected)
3180            (memq article gnus-newsgroup-dormant))))
3181
3182 ;; Some summary mode macros.
3183
3184 (defmacro gnus-summary-article-number ()
3185   "The article number of the article on the current line.
3186 If there isn't an article number here, then we return the current
3187 article number."
3188   '(progn
3189      (gnus-summary-skip-intangible)
3190      (or (get-text-property (point) 'gnus-number)
3191          (gnus-summary-last-subject))))
3192
3193 (defmacro gnus-summary-article-header (&optional number)
3194   "Return the header of article NUMBER."
3195   `(gnus-data-header (gnus-data-find
3196                       ,(or number '(gnus-summary-article-number)))))
3197
3198 (defmacro gnus-summary-thread-level (&optional number)
3199   "Return the level of thread that starts with article NUMBER."
3200   `(if (and (eq gnus-summary-make-false-root 'dummy)
3201             (get-text-property (point) 'gnus-intangible))
3202        0
3203      (gnus-data-level (gnus-data-find
3204                        ,(or number '(gnus-summary-article-number))))))
3205
3206 (defmacro gnus-summary-article-mark (&optional number)
3207   "Return the mark of article NUMBER."
3208   `(gnus-data-mark (gnus-data-find
3209                     ,(or number '(gnus-summary-article-number)))))
3210
3211 (defmacro gnus-summary-article-pos (&optional number)
3212   "Return the position of the line of article NUMBER."
3213   `(gnus-data-pos (gnus-data-find
3214                    ,(or number '(gnus-summary-article-number)))))
3215
3216 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3217 (defmacro gnus-summary-article-subject (&optional number)
3218   "Return current subject string or nil if nothing."
3219   `(let ((headers
3220           ,(if number
3221                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3222              '(gnus-data-header (assq (gnus-summary-article-number)
3223                                       gnus-newsgroup-data)))))
3224      (and headers
3225           (vectorp headers)
3226           (mail-header-subject headers))))
3227
3228 (defmacro gnus-summary-article-score (&optional number)
3229   "Return current article score."
3230   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3231                   gnus-newsgroup-scored))
3232        gnus-summary-default-score 0))
3233
3234 (defun gnus-summary-article-children (&optional number)
3235   "Return a list of article numbers that are children of article NUMBER."
3236   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3237          (level (gnus-data-level (car data)))
3238          l children)
3239     (while (and (setq data (cdr data))
3240                 (> (setq l (gnus-data-level (car data))) level))
3241       (and (= (1+ level) l)
3242            (push (gnus-data-number (car data))
3243                  children)))
3244     (nreverse children)))
3245
3246 (defun gnus-summary-article-parent (&optional number)
3247   "Return the article number of the parent of article NUMBER."
3248   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3249                                     (gnus-data-list t)))
3250          (level (gnus-data-level (car data))))
3251     (if (zerop level)
3252         ()                              ; This is a root.
3253       ;; We search until we find an article with a level less than
3254       ;; this one.  That function has to be the parent.
3255       (while (and (setq data (cdr data))
3256                   (not (< (gnus-data-level (car data)) level))))
3257       (and data (gnus-data-number (car data))))))
3258
3259 (defun gnus-unread-mark-p (mark)
3260   "Say whether MARK is the unread mark."
3261   (= mark gnus-unread-mark))
3262
3263 (defun gnus-read-mark-p (mark)
3264   "Say whether MARK is one of the marks that mark as read.
3265 This is all marks except unread, ticked, dormant, and expirable."
3266   (not (or (= mark gnus-unread-mark)
3267            (= mark gnus-ticked-mark)
3268            (= mark gnus-spam-mark)
3269            (= mark gnus-dormant-mark)
3270            (= mark gnus-expirable-mark))))
3271
3272 (defmacro gnus-article-mark (number)
3273   "Return the MARK of article NUMBER.
3274 This macro should only be used when computing the mark the \"first\"
3275 time; i.e., when generating the summary lines.  After that,
3276 `gnus-summary-article-mark' should be used to examine the
3277 marks of articles."
3278   `(cond
3279     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3280     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3281     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3282     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3283     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3284     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3285     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3286     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3287            gnus-ancient-mark))))
3288
3289 ;; Saving hidden threads.
3290
3291 (defmacro gnus-save-hidden-threads (&rest forms)
3292   "Save hidden threads, eval FORMS, and restore the hidden threads."
3293   (let ((config (make-symbol "config")))
3294     `(let ((,config (gnus-hidden-threads-configuration)))
3295        (unwind-protect
3296            (save-excursion
3297              ,@forms)
3298          (gnus-restore-hidden-threads-configuration ,config)))))
3299 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3300 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3301
3302 (defun gnus-data-compute-positions ()
3303   "Compute the positions of all articles."
3304   (setq gnus-newsgroup-data-reverse nil)
3305   (let ((data gnus-newsgroup-data))
3306     (save-excursion
3307       (gnus-save-hidden-threads
3308         (gnus-summary-show-all-threads)
3309         (goto-char (point-min))
3310         (while data
3311           (while (get-text-property (point) 'gnus-intangible)
3312             (forward-line 1))
3313           (gnus-data-set-pos (car data) (+ (point) 3))
3314           (setq data (cdr data))
3315           (forward-line 1))))))
3316
3317 (defun gnus-hidden-threads-configuration ()
3318   "Return the current hidden threads configuration."
3319   (save-excursion
3320     (let (config)
3321       (goto-char (point-min))
3322       (while (search-forward "\r" nil t)
3323         (push (1- (point)) config))
3324       config)))
3325
3326 (defun gnus-restore-hidden-threads-configuration (config)
3327   "Restore hidden threads configuration from CONFIG."
3328   (save-excursion
3329     (let (point buffer-read-only)
3330       (while (setq point (pop config))
3331         (when (and (< point (point-max))
3332                    (goto-char point)
3333                    (eq (char-after) ?\n))
3334           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3335
3336 ;; Various summary mode internalish functions.
3337
3338 (defun gnus-mouse-pick-article (e)
3339   (interactive "e")
3340   (mouse-set-point e)
3341   (gnus-summary-next-page nil t))
3342
3343 (defun gnus-summary-set-display-table ()
3344   "Change the display table.
3345 Odd characters have a tendency to mess
3346 up nicely formatted displays - we make all possible glyphs
3347 display only a single character."
3348
3349   ;; We start from the standard display table, if any.
3350   (let ((table (or (copy-sequence standard-display-table)
3351                    (make-display-table)))
3352         (i 32))
3353     ;; Nix out all the control chars...
3354     (while (>= (setq i (1- i)) 0)
3355       (aset table i [??]))
3356    ;; ... but not newline and cr, of course.  (cr is necessary for the
3357     ;; selective display).
3358     (aset table ?\n nil)
3359     (aset table ?\r nil)
3360     ;; We keep TAB as well.
3361     (aset table ?\t nil)
3362     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3363     ;; Emacs 23 (unicode), that are not set already.
3364     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3365                  160
3366                256)))
3367       (while (>= (setq i (1- i)) 127)
3368         ;; Only modify if the entry is nil.
3369         (unless (aref table i)
3370           (aset table i [??]))))
3371     (setq buffer-display-table table)))
3372
3373 (defun gnus-summary-set-article-display-arrow (pos)
3374   "Update the overlay arrow to point to line at position POS."
3375   (when (and gnus-summary-display-arrow
3376              (boundp 'overlay-arrow-position)
3377              (boundp 'overlay-arrow-string))
3378     (save-excursion
3379       (goto-char pos)
3380       (beginning-of-line)
3381       (unless overlay-arrow-position
3382         (setq overlay-arrow-position (make-marker)))
3383       (setq overlay-arrow-string "=>"
3384             overlay-arrow-position (set-marker overlay-arrow-position
3385                                                (point)
3386                                                (current-buffer))))))
3387
3388 (defun gnus-summary-setup-buffer (group)
3389   "Initialize summary buffer."
3390   (let ((buffer (gnus-summary-buffer-name group))
3391         (dead-name (concat "*Dead Summary "
3392                            (gnus-group-decoded-name group) "*")))
3393     ;; If a dead summary buffer exists, we kill it.
3394     (when (gnus-buffer-live-p dead-name)
3395       (gnus-kill-buffer dead-name))
3396     (if (get-buffer buffer)
3397         (progn
3398           (set-buffer buffer)
3399           (setq gnus-summary-buffer (current-buffer))
3400           (not gnus-newsgroup-prepared))
3401       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3402       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3403       (gnus-summary-mode group)
3404       (when gnus-carpal
3405         (gnus-carpal-setup-buffer 'summary))
3406       (when (gnus-group-quit-config group)
3407         (set (make-local-variable 'gnus-single-article-buffer) nil))
3408       (make-local-variable 'gnus-article-buffer)
3409       (make-local-variable 'gnus-article-current)
3410       (make-local-variable 'gnus-original-article-buffer)
3411       (setq gnus-newsgroup-name group)
3412       ;; Set any local variables in the group parameters.
3413       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3414       t)))
3415
3416 (defun gnus-set-global-variables ()
3417   "Set the global equivalents of the buffer-local variables.
3418 They are set to the latest values they had.  These reflect the summary
3419 buffer that was in action when the last article was fetched."
3420   (when (eq major-mode 'gnus-summary-mode)
3421     (setq gnus-summary-buffer (current-buffer))
3422     (let ((name gnus-newsgroup-name)
3423           (marked gnus-newsgroup-marked)
3424           (spam gnus-newsgroup-spam-marked)
3425           (unread gnus-newsgroup-unreads)
3426           (headers gnus-current-headers)
3427           (data gnus-newsgroup-data)
3428           (summary gnus-summary-buffer)
3429           (article-buffer gnus-article-buffer)
3430           (original gnus-original-article-buffer)
3431           (gac gnus-article-current)
3432           (reffed gnus-reffed-article-number)
3433           (score-file gnus-current-score-file)
3434           (default-charset gnus-newsgroup-charset)
3435           vlist)
3436       (let ((locals gnus-newsgroup-variables))
3437         (while locals
3438           (if (consp (car locals))
3439               (push (eval (caar locals)) vlist)
3440             (push (eval (car locals)) vlist))
3441           (setq locals (cdr locals)))
3442         (setq vlist (nreverse vlist)))
3443       (with-current-buffer gnus-group-buffer
3444         (setq gnus-newsgroup-name name
3445               gnus-newsgroup-marked marked
3446               gnus-newsgroup-spam-marked spam
3447               gnus-newsgroup-unreads unread
3448               gnus-current-headers headers
3449               gnus-newsgroup-data data
3450               gnus-article-current gac
3451               gnus-summary-buffer summary
3452               gnus-article-buffer article-buffer
3453               gnus-original-article-buffer original
3454               gnus-reffed-article-number reffed
3455               gnus-current-score-file score-file
3456               gnus-newsgroup-charset default-charset)
3457         (let ((locals gnus-newsgroup-variables))
3458           (while locals
3459             (if (consp (car locals))
3460                 (set (caar locals) (pop vlist))
3461               (set (car locals) (pop vlist)))
3462             (setq locals (cdr locals))))
3463         ;; The article buffer also has local variables.
3464         (when (gnus-buffer-live-p gnus-article-buffer)
3465           (set-buffer gnus-article-buffer)
3466           (setq gnus-summary-buffer summary))))))
3467
3468 (defun gnus-summary-article-unread-p (article)
3469   "Say whether ARTICLE is unread or not."
3470   (memq article gnus-newsgroup-unreads))
3471
3472 (defun gnus-summary-first-article-p (&optional article)
3473   "Return whether ARTICLE is the first article in the buffer."
3474   (if (not (setq article (or article (gnus-summary-article-number))))
3475       nil
3476     (eq article (caar gnus-newsgroup-data))))
3477
3478 (defun gnus-summary-last-article-p (&optional article)
3479   "Return whether ARTICLE is the last article in the buffer."
3480   (if (not (setq article (or article (gnus-summary-article-number))))
3481       ;; All non-existent numbers are the last article.  :-)
3482       t
3483     (not (cdr (gnus-data-find-list article)))))
3484
3485 (defun gnus-make-thread-indent-array (&optional n)
3486   (when (or n
3487             (progn (setq n 200) nil)
3488             (null gnus-thread-indent-array)
3489             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3490     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3491           gnus-thread-indent-array-level gnus-thread-indent-level)
3492     (while (>= n 0)
3493       (aset gnus-thread-indent-array n
3494             (make-string (* n gnus-thread-indent-level) ? ))
3495       (setq n (1- n)))))
3496
3497 (defun gnus-update-summary-mark-positions ()
3498   "Compute where the summary marks are to go."
3499   (save-excursion
3500     (when (gnus-buffer-exists-p gnus-summary-buffer)
3501       (set-buffer gnus-summary-buffer))
3502     (let ((spec gnus-summary-line-format-spec)
3503           pos)
3504       (save-excursion
3505         (gnus-set-work-buffer)
3506         (let ((gnus-tmp-unread ?Z)
3507               (gnus-replied-mark ?Z)
3508               (gnus-score-below-mark ?Z)
3509               (gnus-score-over-mark ?Z)
3510               (gnus-undownloaded-mark ?Z)
3511               (gnus-summary-line-format-spec spec)
3512               (gnus-newsgroup-downloadable '(0))
3513               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3514               case-fold-search ignores)
3515           ;; Here, all marks are bound to Z.
3516           (gnus-summary-insert-line header
3517                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3518           (goto-char (point-min))
3519           ;; Memorize the positions of the same characters as dummy marks.
3520           (while (re-search-forward "[A-D]" nil t)
3521             (push (point) ignores))
3522           (erase-buffer)
3523           ;; We use A-D as dummy marks in order to know column positions
3524           ;; where marks should be inserted.
3525           (setq gnus-tmp-unread ?A
3526                 gnus-replied-mark ?B
3527                 gnus-score-below-mark ?C
3528                 gnus-score-over-mark ?C
3529                 gnus-undownloaded-mark ?D)
3530           (gnus-summary-insert-line header
3531                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3532           ;; Ignore characters which aren't dummy marks.
3533           (dolist (p ignores)
3534             (delete-region (goto-char (1- p)) p)
3535             (insert ?Z))
3536           (goto-char (point-min))
3537           (setq pos (list (cons 'unread
3538                                 (and (search-forward "A" nil t)
3539                                      (- (point) (point-min) 1)))))
3540           (goto-char (point-min))
3541           (push (cons 'replied (and (search-forward "B" nil t)
3542                                     (- (point) (point-min) 1)))
3543                 pos)
3544           (goto-char (point-min))
3545           (push (cons 'score (and (search-forward "C" nil t)
3546                                   (- (point) (point-min) 1)))
3547                 pos)
3548           (goto-char (point-min))
3549           (push (cons 'download (and (search-forward "D" nil t)
3550                                      (- (point) (point-min) 1)))
3551                 pos)))
3552       (setq gnus-summary-mark-positions pos))))
3553
3554 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3555   "Insert a dummy root in the summary buffer."
3556   (beginning-of-line)
3557   (gnus-add-text-properties
3558    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3559    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3560
3561 (defun gnus-summary-extract-address-component (from)
3562   (or (car (funcall gnus-extract-address-components from))
3563       from))
3564
3565 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3566   (let ((mail-parse-charset gnus-newsgroup-charset)
3567         (ignored-from-addresses (gnus-ignored-from-addresses))
3568         ; Is it really necessary to do this next part for each summary line?
3569         ; Luckily, doesn't seem to slow things down much.
3570         (mail-parse-ignored-charsets
3571          (with-current-buffer gnus-summary-buffer
3572            gnus-newsgroup-ignored-charsets)))
3573     (or
3574      (and ignored-from-addresses
3575           (string-match ignored-from-addresses gnus-tmp-from)
3576           (let ((extra-headers (mail-header-extra header))
3577                 to
3578                 newsgroups)
3579             (cond
3580              ((setq to (cdr (assq 'To extra-headers)))
3581               (concat gnus-summary-to-prefix
3582                       (inline
3583                         (gnus-summary-extract-address-component
3584                          (funcall gnus-decode-encoded-address-function to)))))
3585              ((setq newsgroups
3586                     (or
3587                      (cdr (assq 'Newsgroups extra-headers))
3588                      (and
3589                       (memq 'Newsgroups gnus-extra-headers)
3590                       (eq (car (gnus-find-method-for-group
3591                                 gnus-newsgroup-name)) 'nntp)
3592                       (gnus-group-real-name gnus-newsgroup-name))))
3593               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3594      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3595
3596 (defun gnus-summary-insert-line (gnus-tmp-header
3597                                  gnus-tmp-level gnus-tmp-current
3598                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3599                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3600                                  &optional gnus-tmp-dummy gnus-tmp-score
3601                                  gnus-tmp-process)
3602   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3603       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3604                                           gnus-tmp-level)))
3605   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3606          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3607          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3608          (gnus-tmp-score-char
3609           (if (or (null gnus-summary-default-score)
3610                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3611                       gnus-summary-zcore-fuzz))
3612               ?                         ;Whitespace
3613             (if (< gnus-tmp-score gnus-summary-default-score)
3614                 gnus-score-below-mark gnus-score-over-mark)))
3615          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3616          (gnus-tmp-replied
3617           (cond (gnus-tmp-process gnus-process-mark)
3618                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3619                  gnus-cached-mark)
3620                 (gnus-tmp-replied gnus-replied-mark)
3621                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3622                  gnus-forwarded-mark)
3623                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3624                  gnus-saved-mark)
3625                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3626                  gnus-recent-mark)
3627                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3628                  gnus-unseen-mark)
3629                 (t gnus-no-mark)))
3630          (gnus-tmp-downloaded
3631           (cond (undownloaded
3632                  gnus-undownloaded-mark)
3633                 (gnus-newsgroup-agentized
3634                  gnus-downloaded-mark)
3635                 (t
3636                  gnus-no-mark)))
3637          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3638          (gnus-tmp-name
3639           (cond
3640            ((string-match "<[^>]+> *$" gnus-tmp-from)
3641             (let ((beg (match-beginning 0)))
3642               (or (and (string-match "^\".+\"" gnus-tmp-from)
3643                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3644                   (substring gnus-tmp-from 0 beg))))
3645            ((string-match "(.+)" gnus-tmp-from)
3646             (substring gnus-tmp-from
3647                        (1+ (match-beginning 0)) (1- (match-end 0))))
3648            (t gnus-tmp-from)))
3649          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3650          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3651          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3652          (buffer-read-only nil))
3653     (when (string= gnus-tmp-name "")
3654       (setq gnus-tmp-name gnus-tmp-from))
3655     (unless (numberp gnus-tmp-lines)
3656       (setq gnus-tmp-lines -1))
3657     (if (= gnus-tmp-lines -1)
3658         (setq gnus-tmp-lines "?")
3659       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3660       (gnus-put-text-property
3661      (point)
3662      (progn (eval gnus-summary-line-format-spec) (point))
3663        'gnus-number gnus-tmp-number)
3664     (when (gnus-visual-p 'summary-highlight 'highlight)
3665       (forward-line -1)
3666       (gnus-run-hooks 'gnus-summary-update-hook)
3667       (forward-line 1))))
3668
3669 (defun gnus-summary-update-line (&optional dont-update)
3670   "Update summary line after change."
3671   (when (and gnus-summary-default-score
3672              (not gnus-summary-inhibit-highlight))
3673     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3674            (article (gnus-summary-article-number))
3675            (score (gnus-summary-article-score article)))
3676       (unless dont-update
3677         (if (and gnus-summary-mark-below
3678                  (< (gnus-summary-article-score)
3679                     gnus-summary-mark-below))
3680             ;; This article has a low score, so we mark it as read.
3681             (when (memq article gnus-newsgroup-unreads)
3682               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3683           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3684             ;; This article was previously marked as read on account
3685             ;; of a low score, but now it has risen, so we mark it as
3686             ;; unread.
3687             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3688         (gnus-summary-update-mark
3689          (if (or (null gnus-summary-default-score)
3690                  (<= (abs (- score gnus-summary-default-score))
3691                      gnus-summary-zcore-fuzz))
3692              ?                          ;Whitespace
3693            (if (< score gnus-summary-default-score)
3694                gnus-score-below-mark gnus-score-over-mark))
3695          'score))
3696       ;; Do visual highlighting.
3697       (when (gnus-visual-p 'summary-highlight 'highlight)
3698         (gnus-run-hooks 'gnus-summary-update-hook)))))
3699
3700 (defvar gnus-tmp-new-adopts nil)
3701
3702 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3703   "Return the number of articles in THREAD.
3704 This may be 0 in some cases -- if none of the articles in
3705 the thread are to be displayed."
3706   (let* ((number
3707          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3708           (cond
3709            ((not (listp thread))
3710             1)
3711            ((and (consp thread) (cdr thread))
3712             (apply
3713              '+ 1 (mapcar
3714                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3715            ((null thread)
3716             1)
3717            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3718             1)
3719            (t 0))))
3720     (when (and level (zerop level) gnus-tmp-new-adopts)
3721       (incf number
3722             (apply '+ (mapcar
3723                        'gnus-summary-number-of-articles-in-thread
3724                        gnus-tmp-new-adopts))))
3725     (if char
3726         (if (> number 1) gnus-not-empty-thread-mark
3727           gnus-empty-thread-mark)
3728       number)))
3729
3730 (defsubst gnus-summary-line-message-size (head)
3731   "Return pretty-printed version of message size.
3732 This function is intended to be used in
3733 `gnus-summary-line-format-alist'."
3734   (let ((c (or (mail-header-chars head) -1)))
3735     (cond ((< c 0) "n/a")               ; chars not available
3736           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3737           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3738           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3739           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3740
3741
3742 (defun gnus-summary-set-local-parameters (group)
3743   "Go through the local params of GROUP and set all variable specs in that list."
3744   (let ((vars '(quit-config)))          ; Ignore quit-config.
3745     (dolist (elem (gnus-group-find-parameter group))
3746       (and (consp elem)                 ; Has to be a cons.
3747            (consp (cdr elem))           ; The cdr has to be a list.
3748            (symbolp (car elem))         ; Has to be a symbol in there.
3749            (not (memq (car elem) vars))
3750            (ignore-errors               ; So we set it.
3751              (push (car elem) vars)
3752              (make-local-variable (car elem))
3753              (set (car elem) (eval (nth 1 elem))))))))
3754
3755 (defun gnus-summary-read-group (group &optional show-all no-article
3756                                       kill-buffer no-display backward
3757                                       select-articles)
3758   "Start reading news in newsgroup GROUP.
3759 If SHOW-ALL is non-nil, already read articles are also listed.
3760 If NO-ARTICLE is non-nil, no article is selected initially.
3761 If NO-DISPLAY, don't generate a summary buffer."
3762   (let (result)
3763     (while (and group
3764                 (null (setq result
3765                             (let ((gnus-auto-select-next nil))
3766                               (or (gnus-summary-read-group-1
3767                                    group show-all no-article
3768                                    kill-buffer no-display
3769                                    select-articles)
3770                                   (setq show-all nil
3771                                         select-articles nil)))))
3772                 (eq gnus-auto-select-next 'quietly))
3773       (set-buffer gnus-group-buffer)
3774       ;; The entry function called above goes to the next
3775       ;; group automatically, so we go two groups back
3776       ;; if we are searching for the previous group.
3777       (when backward
3778         (gnus-group-prev-unread-group 2))
3779       (if (not (equal group (gnus-group-group-name)))
3780           (setq group (gnus-group-group-name))
3781         (setq group nil)))
3782     result))
3783
3784 (defun gnus-summary-read-group-1 (group show-all no-article
3785                                         kill-buffer no-display
3786                                         &optional select-articles)
3787   ;; Killed foreign groups can't be entered.
3788   ;;  (when (and (not (gnus-group-native-p group))
3789   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3790   ;;    (error "Dead non-native groups can't be entered"))
3791   (gnus-message 5 "Retrieving newsgroup: %s..."
3792                 (gnus-group-decoded-name group))
3793   (let* ((new-group (gnus-summary-setup-buffer group))
3794          (quit-config (gnus-group-quit-config group))
3795          (did-select (and new-group (gnus-select-newsgroup
3796                                      group show-all select-articles))))
3797     (cond
3798      ;; This summary buffer exists already, so we just select it.
3799      ((not new-group)
3800       (gnus-set-global-variables)
3801       (when kill-buffer
3802         (gnus-kill-or-deaden-summary kill-buffer))
3803       (gnus-configure-windows 'summary 'force)
3804       (gnus-set-mode-line 'summary)
3805       (gnus-summary-position-point)
3806       (message "")
3807       t)
3808      ;; We couldn't select this group.
3809      ((null did-select)
3810       (when (and (eq major-mode 'gnus-summary-mode)
3811                  (not (equal (current-buffer) kill-buffer)))
3812         (kill-buffer (current-buffer))
3813         (if (not quit-config)
3814             (progn
3815               ;; Update the info -- marks might need to be removed,
3816               ;; for instance.
3817               (gnus-summary-update-info)
3818               (set-buffer gnus-group-buffer)
3819               (gnus-group-jump-to-group group)
3820               (gnus-group-next-unread-group 1))
3821           (gnus-handle-ephemeral-exit quit-config)))
3822       (let ((grpinfo (gnus-get-info group)))
3823         (if (null (gnus-info-read grpinfo))
3824             (gnus-message 3 "Group %s contains no messages"
3825                           (gnus-group-decoded-name group))
3826           (gnus-message 3 "Can't select group")))
3827       nil)
3828      ;; The user did a `C-g' while prompting for number of articles,
3829      ;; so we exit this group.
3830      ((eq did-select 'quit)
3831       (and (eq major-mode 'gnus-summary-mode)
3832            (not (equal (current-buffer) kill-buffer))
3833            (kill-buffer (current-buffer)))
3834       (when kill-buffer
3835         (gnus-kill-or-deaden-summary kill-buffer))
3836       (if (not quit-config)
3837           (progn
3838             (set-buffer gnus-group-buffer)
3839             (gnus-group-jump-to-group group)
3840             (gnus-group-next-unread-group 1)
3841             (gnus-configure-windows 'group 'force))
3842         (gnus-handle-ephemeral-exit quit-config))
3843       ;; Finally signal the quit.
3844       (signal 'quit nil))
3845      ;; The group was successfully selected.
3846      (t
3847       (gnus-set-global-variables)
3848       ;; Save the active value in effect when the group was entered.
3849       (setq gnus-newsgroup-active
3850             (gnus-copy-sequence
3851              (gnus-active gnus-newsgroup-name)))
3852       ;; You can change the summary buffer in some way with this hook.
3853       (gnus-run-hooks 'gnus-select-group-hook)
3854       (when (memq 'summary (gnus-update-format-specifications
3855                             nil 'summary 'summary-mode 'summary-dummy))
3856         ;; The format specification for the summary line was updated,
3857         ;; so we need to update the mark positions as well.
3858         (gnus-update-summary-mark-positions))
3859       ;; Do score processing.
3860       (when gnus-use-scoring
3861         (gnus-possibly-score-headers))
3862       ;; Check whether to fill in the gaps in the threads.
3863       (when gnus-build-sparse-threads
3864         (gnus-build-sparse-threads))
3865       ;; Find the initial limit.
3866       (if show-all
3867           (let ((gnus-newsgroup-dormant nil))
3868             (gnus-summary-initial-limit show-all))
3869         (gnus-summary-initial-limit show-all))
3870       ;; Generate the summary buffer.
3871       (unless no-display
3872         (gnus-summary-prepare))
3873       (when gnus-use-trees
3874         (gnus-tree-open group)
3875         (setq gnus-summary-highlight-line-function
3876               'gnus-tree-highlight-article))
3877       ;; If the summary buffer is empty, but there are some low-scored
3878       ;; articles or some excluded dormants, we include these in the
3879       ;; buffer.
3880       (when (and (zerop (buffer-size))
3881                  (not no-display))
3882         (cond (gnus-newsgroup-dormant
3883                (gnus-summary-limit-include-dormant))
3884               ((and gnus-newsgroup-scored show-all)
3885                (gnus-summary-limit-include-expunged t))))
3886       ;; Function `gnus-apply-kill-file' must be called in this hook.
3887       (gnus-run-hooks 'gnus-apply-kill-hook)
3888       (if (and (zerop (buffer-size))
3889                (not no-display))
3890           (progn
3891             ;; This newsgroup is empty.
3892             (gnus-summary-catchup-and-exit nil t)
3893             (gnus-message 6 "No unread news")
3894             (when kill-buffer
3895               (gnus-kill-or-deaden-summary kill-buffer))
3896             ;; Return nil from this function.
3897             nil)
3898         ;; Hide conversation thread subtrees.  We cannot do this in
3899         ;; gnus-summary-prepare-hook since kill processing may not
3900         ;; work with hidden articles.
3901         (gnus-summary-maybe-hide-threads)
3902         (when kill-buffer
3903           (gnus-kill-or-deaden-summary kill-buffer))
3904         (gnus-summary-auto-select-subject)
3905         ;; Show first unread article if requested.
3906         (if (and (not no-article)
3907                  (not no-display)
3908                  gnus-newsgroup-unreads
3909                  gnus-auto-select-first)
3910             (progn
3911               (gnus-configure-windows 'summary)
3912               (let ((art (gnus-summary-article-number)))
3913                 (unless (and (not gnus-plugged)
3914                              (or (memq art gnus-newsgroup-undownloaded)
3915                                  (memq art gnus-newsgroup-downloadable)))
3916                   (gnus-summary-goto-article art))))
3917           ;; Don't select any articles.
3918           (gnus-summary-position-point)
3919           (gnus-configure-windows 'summary 'force)
3920           (gnus-set-mode-line 'summary))
3921         (when (and gnus-auto-center-group
3922                    (get-buffer-window gnus-group-buffer t))
3923           ;; Gotta use windows, because recenter does weird stuff if
3924           ;; the current buffer ain't the displayed window.
3925           (let ((owin (selected-window)))
3926             (select-window (get-buffer-window gnus-group-buffer t))
3927             (when (gnus-group-goto-group group)
3928               (recenter))
3929             (select-window owin)))
3930         ;; Mark this buffer as "prepared".
3931         (setq gnus-newsgroup-prepared t)
3932         (gnus-run-hooks 'gnus-summary-prepared-hook)
3933         (unless (gnus-ephemeral-group-p group)
3934           (gnus-group-update-group group))
3935         t)))))
3936
3937 (defun gnus-summary-auto-select-subject ()
3938   "Select the subject line on initial group entry."
3939   (goto-char (point-min))
3940   (cond
3941    ((eq gnus-auto-select-subject 'best)
3942     (gnus-summary-best-unread-subject))
3943    ((eq gnus-auto-select-subject 'unread)
3944     (gnus-summary-first-unread-subject))
3945    ((eq gnus-auto-select-subject 'unseen)
3946     (gnus-summary-first-unseen-subject))
3947    ((eq gnus-auto-select-subject 'unseen-or-unread)
3948     (gnus-summary-first-unseen-or-unread-subject))
3949    ((eq gnus-auto-select-subject 'first)
3950     ;; Do nothing.
3951     )
3952    ((functionp gnus-auto-select-subject)
3953     (funcall gnus-auto-select-subject))))
3954
3955 (defun gnus-summary-prepare ()
3956   "Generate the summary buffer."
3957   (interactive)
3958   (let ((buffer-read-only nil))
3959     (erase-buffer)
3960     (setq gnus-newsgroup-data nil
3961           gnus-newsgroup-data-reverse nil)
3962     (gnus-run-hooks 'gnus-summary-generate-hook)
3963     ;; Generate the buffer, either with threads or without.
3964     (when gnus-newsgroup-headers
3965       (gnus-summary-prepare-threads
3966        (if gnus-show-threads
3967            (gnus-sort-gathered-threads
3968             (funcall gnus-summary-thread-gathering-function
3969                      (gnus-sort-threads
3970                       (gnus-cut-threads (gnus-make-threads)))))
3971          ;; Unthreaded display.
3972          (gnus-sort-articles gnus-newsgroup-headers))))
3973     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3974     ;; Call hooks for modifying summary buffer.
3975     (goto-char (point-min))
3976     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3977
3978 (defsubst gnus-general-simplify-subject (subject)
3979   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3980   (setq subject
3981         (cond
3982          ;; Truncate the subject.
3983          (gnus-simplify-subject-functions
3984           (gnus-map-function gnus-simplify-subject-functions subject))
3985          ((numberp gnus-summary-gather-subject-limit)
3986           (setq subject (gnus-simplify-subject-re subject))
3987           (if (> (length subject) gnus-summary-gather-subject-limit)
3988               (substring subject 0 gnus-summary-gather-subject-limit)
3989             subject))
3990          ;; Fuzzily simplify it.
3991          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3992           (gnus-simplify-subject-fuzzy subject))
3993          ;; Just remove the leading "Re:".
3994          (t
3995           (gnus-simplify-subject-re subject))))
3996
3997   (if (and gnus-summary-gather-exclude-subject
3998            (string-match gnus-summary-gather-exclude-subject subject))
3999       nil                         ; This article shouldn't be gathered
4000     subject))
4001
4002 (defun gnus-summary-simplify-subject-query ()
4003   "Query where the respool algorithm would put this article."
4004   (interactive)
4005   (gnus-summary-select-article)
4006   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
4007
4008 (defun gnus-gather-threads-by-subject (threads)
4009   "Gather threads by looking at Subject headers."
4010   (if (not gnus-summary-make-false-root)
4011       threads
4012     (let ((hashtb (gnus-make-hashtable 1024))
4013           (prev threads)
4014           (result threads)
4015           subject hthread whole-subject)
4016       (while threads
4017         (setq subject (gnus-general-simplify-subject
4018                        (setq whole-subject (mail-header-subject
4019                                             (caar threads)))))
4020         (when subject
4021           (if (setq hthread (gnus-gethash subject hashtb))
4022               (progn
4023                 ;; We enter a dummy root into the thread, if we
4024                 ;; haven't done that already.
4025                 (unless (stringp (caar hthread))
4026                   (setcar hthread (list whole-subject (car hthread))))
4027                 ;; We add this new gathered thread to this gathered
4028                 ;; thread.
4029                 (setcdr (car hthread)
4030                         (nconc (cdar hthread) (list (car threads))))
4031                 ;; Remove it from the list of threads.
4032                 (setcdr prev (cdr threads))
4033                 (setq threads prev))
4034             ;; Enter this thread into the hash table.
4035             (gnus-sethash subject
4036                           (if gnus-summary-make-false-root-always
4037                               (progn
4038                                 ;; If you want a dummy root above all
4039                                 ;; threads...
4040                                 (setcar threads (list whole-subject
4041                                                       (car threads)))
4042                                 threads)
4043                             threads)
4044                           hashtb)))
4045         (setq prev threads)
4046         (setq threads (cdr threads)))
4047       result)))
4048
4049 (defun gnus-gather-threads-by-references (threads)
4050   "Gather threads by looking at References headers."
4051   (let ((idhashtb (gnus-make-hashtable 1024))
4052         (thhashtb (gnus-make-hashtable 1024))
4053         (prev threads)
4054         (result threads)
4055         ids references id gthread gid entered ref)
4056     (while threads
4057       (when (setq references (mail-header-references (caar threads)))
4058         (setq id (mail-header-id (caar threads))
4059               ids (inline (gnus-split-references references))
4060               entered nil)
4061         (while (setq ref (pop ids))
4062           (setq ids (delete ref ids))
4063           (if (not (setq gid (gnus-gethash ref idhashtb)))
4064               (progn
4065                 (gnus-sethash ref id idhashtb)
4066                 (gnus-sethash id threads thhashtb))
4067             (setq gthread (gnus-gethash gid thhashtb))
4068             (unless entered
4069               ;; We enter a dummy root into the thread, if we
4070               ;; haven't done that already.
4071               (unless (stringp (caar gthread))
4072                 (setcar gthread (list (mail-header-subject (caar gthread))
4073                                       (car gthread))))
4074               ;; We add this new gathered thread to this gathered
4075               ;; thread.
4076               (setcdr (car gthread)
4077                       (nconc (cdar gthread) (list (car threads)))))
4078             ;; Add it into the thread hash table.
4079             (gnus-sethash id gthread thhashtb)
4080             (setq entered t)
4081             ;; Remove it from the list of threads.
4082             (setcdr prev (cdr threads))
4083             (setq threads prev))))
4084       (setq prev threads)
4085       (setq threads (cdr threads)))
4086     result))
4087
4088 (defun gnus-sort-gathered-threads (threads)
4089   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4090   (let ((result threads))
4091     (while threads
4092       (when (stringp (caar threads))
4093         (setcdr (car threads)
4094                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4095       (setq threads (cdr threads)))
4096     result))
4097
4098 (defun gnus-thread-loop-p (root thread)
4099   "Say whether ROOT is in THREAD."
4100   (let ((stack (list thread))
4101         (infloop 0)
4102         th)
4103     (while (setq thread (pop stack))
4104       (setq th (cdr thread))
4105       (while (and th
4106                   (not (eq (caar th) root)))
4107         (pop th))
4108       (if th
4109           ;; We have found a loop.
4110           (let (ref-dep)
4111             (setcdr thread (delq (car th) (cdr thread)))
4112             (if (boundp (setq ref-dep (intern "none"
4113                                               gnus-newsgroup-dependencies)))
4114                 (setcdr (symbol-value ref-dep)
4115                         (nconc (cdr (symbol-value ref-dep))
4116                                (list (car th))))
4117               (set ref-dep (list nil (car th))))
4118             (setq infloop 1
4119                   stack nil))
4120         ;; Push all the subthreads onto the stack.
4121         (push (cdr thread) stack)))
4122     infloop))
4123
4124 (defun gnus-make-threads ()
4125   "Go through the dependency hashtb and find the roots.  Return all threads."
4126   (let (threads)
4127     (while (catch 'infloop
4128              (mapatoms
4129               (lambda (refs)
4130                 ;; Deal with self-referencing References loops.
4131                 (when (and (car (symbol-value refs))
4132                            (not (zerop
4133                                  (apply
4134                                   '+
4135                                   (mapcar
4136                                    (lambda (thread)
4137                                      (gnus-thread-loop-p
4138                                       (car (symbol-value refs)) thread))
4139                                    (cdr (symbol-value refs)))))))
4140                   (setq threads nil)
4141                   (throw 'infloop t))
4142                 (unless (car (symbol-value refs))
4143                   ;; These threads do not refer back to any other
4144                   ;; articles, so they're roots.
4145                   (setq threads (append (cdr (symbol-value refs)) threads))))
4146               gnus-newsgroup-dependencies)))
4147     threads))
4148
4149 ;; Build the thread tree.
4150 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4151   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4152
4153 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4154 if it was already present.
4155
4156 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4157 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4158 Message-IDs will be renamed to a unique Message-ID before being
4159 entered.
4160
4161 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4162   (let* ((id (mail-header-id header))
4163          (id-dep (and id (intern id dependencies)))
4164          parent-id ref ref-dep ref-header replaced)
4165     ;; Enter this `header' in the `dependencies' table.
4166     (cond
4167      ((not id-dep)
4168       (setq header nil))
4169      ;; The first two cases do the normal part: enter a new `header'
4170      ;; in the `dependencies' table.
4171      ((not (boundp id-dep))
4172       (set id-dep (list header)))
4173      ((null (car (symbol-value id-dep)))
4174       (setcar (symbol-value id-dep) header))
4175
4176      ;; From here the `header' was already present in the
4177      ;; `dependencies' table.
4178      (force-new
4179       ;; Overrides an existing entry;
4180       ;; just set the header part of the entry.
4181       (setcar (symbol-value id-dep) header)
4182       (setq replaced t))
4183
4184      ;; Renames the existing `header' to a unique Message-ID.
4185      ((not gnus-summary-ignore-duplicates)
4186       ;; An article with this Message-ID has already been seen.
4187       ;; We rename the Message-ID.
4188       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4189            (list header))
4190       (mail-header-set-id header id))
4191
4192      ;; The last case ignores an existing entry, except it adds any
4193      ;; additional Xrefs (in case the two articles came from different
4194      ;; servers.
4195      ;; Also sets `header' to `nil' meaning that the `dependencies'
4196      ;; table was *not* modified.
4197      (t
4198       (mail-header-set-xref
4199        (car (symbol-value id-dep))
4200        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4201                    "")
4202                (or (mail-header-xref header) "")))
4203       (setq header nil)))
4204
4205     (when (and header (not replaced))
4206       ;; First check that we are not creating a References loop.
4207       (setq parent-id (gnus-parent-id (mail-header-references header)))
4208       (setq ref parent-id)
4209       (while (and ref
4210                   (setq ref-dep (intern-soft ref dependencies))
4211                   (boundp ref-dep)
4212                   (setq ref-header (car (symbol-value ref-dep))))
4213         (if (string= id ref)
4214             ;; Yuk!  This is a reference loop.  Make the article be a
4215             ;; root article.
4216             (progn
4217               (mail-header-set-references (car (symbol-value id-dep)) "none")
4218               (setq ref nil)
4219               (setq parent-id nil))
4220           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4221       (setq ref-dep (intern (or parent-id "none") dependencies))
4222       (if (boundp ref-dep)
4223           (setcdr (symbol-value ref-dep)
4224                   (nconc (cdr (symbol-value ref-dep))
4225                          (list (symbol-value id-dep))))
4226         (set ref-dep (list nil (symbol-value id-dep)))))
4227     header))
4228
4229 (defun gnus-extract-message-id-from-in-reply-to (string)
4230   (if (string-match "<[^>]+>" string)
4231       (substring string (match-beginning 0) (match-end 0))
4232     nil))
4233
4234 (defun gnus-build-sparse-threads ()
4235   (let ((headers gnus-newsgroup-headers)
4236         (mail-parse-charset gnus-newsgroup-charset)
4237         (gnus-summary-ignore-duplicates t)
4238         header references generation relations
4239         subject child end new-child date)
4240     ;; First we create an alist of generations/relations, where
4241     ;; generations is how much we trust the relation, and the relation
4242     ;; is parent/child.
4243     (gnus-message 7 "Making sparse threads...")
4244     (save-excursion
4245       (nnheader-set-temp-buffer " *gnus sparse threads*")
4246       (while (setq header (pop headers))
4247         (when (and (setq references (mail-header-references header))
4248                    (not (string= references "")))
4249           (insert references)
4250           (setq child (mail-header-id header)
4251                 subject (mail-header-subject header)
4252                 date (mail-header-date header)
4253                 generation 0)
4254           (while (search-backward ">" nil t)
4255             (setq end (1+ (point)))
4256             (when (search-backward "<" nil t)
4257               (setq new-child (buffer-substring (point) end))
4258               (push (list (incf generation)
4259                           child (setq child new-child)
4260                           subject date)
4261                     relations)))
4262           (when child
4263             (push (list (1+ generation) child nil subject) relations))
4264           (erase-buffer)))
4265       (kill-buffer (current-buffer)))
4266     ;; Sort over trustworthiness.
4267     (mapcar
4268      (lambda (relation)
4269        (when (gnus-dependencies-add-header
4270               (make-full-mail-header
4271                gnus-reffed-article-number
4272                (nth 3 relation) "" (or (nth 4 relation) "")
4273                (nth 1 relation)
4274                (or (nth 2 relation) "") 0 0 "")
4275               gnus-newsgroup-dependencies nil)
4276          (push gnus-reffed-article-number gnus-newsgroup-limit)
4277          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4278          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4279                gnus-newsgroup-reads)
4280          (decf gnus-reffed-article-number)))
4281      (sort relations 'car-less-than-car))
4282     (gnus-message 7 "Making sparse threads...done")))
4283
4284 (defun gnus-build-old-threads ()
4285   ;; Look at all the articles that refer back to old articles, and
4286   ;; fetch the headers for the articles that aren't there.  This will
4287   ;; build complete threads - if the roots haven't been expired by the
4288   ;; server, that is.
4289   (let ((mail-parse-charset gnus-newsgroup-charset)
4290         id heads)
4291     (mapatoms
4292      (lambda (refs)
4293        (when (not (car (symbol-value refs)))
4294          (setq heads (cdr (symbol-value refs)))
4295          (while heads
4296            (if (memq (mail-header-number (caar heads))
4297                      gnus-newsgroup-dormant)
4298                (setq heads (cdr heads))
4299              (setq id (symbol-name refs))
4300              (while (and (setq id (gnus-build-get-header id))
4301                          (not (car (gnus-id-to-thread id)))))
4302              (setq heads nil)))))
4303      gnus-newsgroup-dependencies)))
4304
4305 (defsubst gnus-remove-odd-characters (string)
4306   "Translate STRING into something that doesn't contain weird characters."
4307   (mm-subst-char-in-string
4308    ?\r ?\-
4309    (mm-subst-char-in-string ?\n ?\- string t) t))
4310
4311 ;; This function has to be called with point after the article number
4312 ;; on the beginning of the line.
4313 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4314   (let ((eol (point-at-eol))
4315         (buffer (current-buffer))
4316         header references in-reply-to)
4317
4318     ;; overview: [num subject from date id refs chars lines misc]
4319     (unwind-protect
4320         (let (x)
4321           (narrow-to-region (point) eol)
4322           (unless (eobp)
4323             (forward-char))
4324
4325           (setq header
4326                 (make-full-mail-header
4327                  number                 ; number
4328                  (condition-case ()     ; subject
4329                      (gnus-remove-odd-characters
4330                       (funcall gnus-decode-encoded-word-function
4331                                (setq x (nnheader-nov-field))))
4332                    (error x))
4333                  (condition-case ()     ; from
4334                      (gnus-remove-odd-characters
4335                       (funcall gnus-decode-encoded-address-function
4336                                (setq x (nnheader-nov-field))))
4337                    (error x))
4338                  (nnheader-nov-field)   ; date
4339                  (nnheader-nov-read-message-id number)  ; id
4340                  (setq references (nnheader-nov-field)) ; refs
4341                  (nnheader-nov-read-integer) ; chars
4342                  (nnheader-nov-read-integer) ; lines
4343                  (unless (eobp)
4344                    (if (looking-at "Xref: ")
4345                        (goto-char (match-end 0)))
4346                    (nnheader-nov-field)) ; Xref
4347                  (nnheader-nov-parse-extra)))) ; extra
4348
4349       (widen))
4350
4351     (when (and (string= references "")
4352                (setq in-reply-to (mail-header-extra header))
4353                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4354       (mail-header-set-references
4355        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4356
4357     (when gnus-alter-header-function
4358       (funcall gnus-alter-header-function header))
4359     (gnus-dependencies-add-header header dependencies force-new)))
4360
4361 (defun gnus-build-get-header (id)
4362   "Look through the buffer of NOV lines and find the header to ID.
4363 Enter this line into the dependencies hash table, and return
4364 the id of the parent article (if any)."
4365   (let ((deps gnus-newsgroup-dependencies)
4366         found header)
4367     (prog1
4368         (save-excursion
4369           (set-buffer nntp-server-buffer)
4370           (let ((case-fold-search nil))
4371             (goto-char (point-min))
4372             (while (and (not found)
4373                         (search-forward id nil t))
4374               (beginning-of-line)
4375               (setq found (looking-at
4376                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4377                                    (regexp-quote id))))
4378               (or found (beginning-of-line 2)))
4379             (when found
4380               (beginning-of-line)
4381               (and
4382                (setq header (gnus-nov-parse-line
4383                              (read (current-buffer)) deps))
4384                (gnus-parent-id (mail-header-references header))))))
4385       (when header
4386         (let ((number (mail-header-number header)))
4387           (push number gnus-newsgroup-limit)
4388           (push header gnus-newsgroup-headers)
4389           (if (memq number gnus-newsgroup-unselected)
4390               (progn
4391                 (setq gnus-newsgroup-unreads
4392                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4393                                                number))
4394                 (setq gnus-newsgroup-unselected
4395                       (delq number gnus-newsgroup-unselected)))
4396             (push number gnus-newsgroup-ancient)))))))
4397
4398 (defun gnus-build-all-threads ()
4399   "Read all the headers."
4400   (let ((gnus-summary-ignore-duplicates t)
4401         (mail-parse-charset gnus-newsgroup-charset)
4402         (dependencies gnus-newsgroup-dependencies)
4403         header article)
4404     (save-excursion
4405       (set-buffer nntp-server-buffer)
4406       (let ((case-fold-search nil))
4407         (goto-char (point-min))
4408         (while (not (eobp))
4409           (ignore-errors
4410             (setq article (read (current-buffer))
4411                   header (gnus-nov-parse-line article dependencies)))
4412           (when header
4413             (with-current-buffer gnus-summary-buffer
4414               (push header gnus-newsgroup-headers)
4415               (if (memq (setq article (mail-header-number header))
4416                         gnus-newsgroup-unselected)
4417                   (progn
4418                     (setq gnus-newsgroup-unreads
4419                           (gnus-add-to-sorted-list
4420                            gnus-newsgroup-unreads article))
4421                     (setq gnus-newsgroup-unselected
4422                           (delq article gnus-newsgroup-unselected)))
4423                 (push article gnus-newsgroup-ancient)))
4424             (forward-line 1)))))))
4425
4426 (defun gnus-summary-update-article-line (article header)
4427   "Update the line for ARTICLE using HEADER."
4428   (let* ((id (mail-header-id header))
4429          (thread (gnus-id-to-thread id)))
4430     (unless thread
4431       (error "Article in no thread"))
4432     ;; Update the thread.
4433     (setcar thread header)
4434     (gnus-summary-goto-subject article)
4435     (let* ((datal (gnus-data-find-list article))
4436            (data (car datal))
4437            (buffer-read-only nil)
4438            (level (gnus-summary-thread-level)))
4439       (gnus-delete-line)
4440       (let ((inserted (- (point)
4441                          (progn
4442                            (gnus-summary-insert-line
4443                             header level nil
4444                             (memq article gnus-newsgroup-undownloaded)
4445                             (gnus-article-mark article)
4446                             (memq article gnus-newsgroup-replied)
4447                             (memq article gnus-newsgroup-expirable)
4448                             ;; Only insert the Subject string when it's different
4449                             ;; from the previous Subject string.
4450                             (if (and
4451                                  gnus-show-threads
4452                                  (gnus-subject-equal
4453                                   (condition-case ()
4454                                       (mail-header-subject
4455                                        (gnus-data-header
4456                                         (cadr
4457                                          (gnus-data-find-list
4458                                           article
4459                                           (gnus-data-list t)))))
4460                                     ;; Error on the side of excessive subjects.
4461                                     (error ""))
4462                                   (mail-header-subject header)))
4463                                 ""
4464                               (mail-header-subject header))
4465                             nil (cdr (assq article gnus-newsgroup-scored))
4466                             (memq article gnus-newsgroup-processable))
4467                            (point)))))
4468         (when (cdr datal)
4469           (gnus-data-update-list
4470            (cdr datal)
4471            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4472
4473 (defun gnus-summary-update-article (article &optional iheader)
4474   "Update ARTICLE in the summary buffer."
4475   (set-buffer gnus-summary-buffer)
4476   (let* ((header (gnus-summary-article-header article))
4477          (id (mail-header-id header))
4478          (data (gnus-data-find article))
4479          (thread (gnus-id-to-thread id))
4480          (references (mail-header-references header))
4481          (parent
4482           (gnus-id-to-thread
4483            (or (gnus-parent-id
4484                 (when (and references
4485                            (not (equal "" references)))
4486                   references))
4487                "none")))
4488          (buffer-read-only nil)
4489          (old (car thread)))
4490     (when thread
4491       (unless iheader
4492         (setcar thread nil)
4493         (when parent
4494           (delq thread parent)))
4495       (if (gnus-summary-insert-subject id header)
4496           ;; Set the (possibly) new article number in the data structure.
4497           (gnus-data-set-number data (gnus-id-to-article id))
4498         (setcar thread old)
4499         nil))))
4500
4501 (defun gnus-rebuild-thread (id &optional line)
4502   "Rebuild the thread containing ID.
4503 If LINE, insert the rebuilt thread starting on line LINE."
4504   (let ((buffer-read-only nil)
4505         old-pos current thread data)
4506     (if (not gnus-show-threads)
4507         (setq thread (list (car (gnus-id-to-thread id))))
4508       ;; Get the thread this article is part of.
4509       (setq thread (gnus-remove-thread id)))
4510     (setq old-pos (point-at-bol))
4511     (setq current (save-excursion
4512                     (and (re-search-backward "[\r\n]" nil t)
4513                          (gnus-summary-article-number))))
4514     ;; If this is a gathered thread, we have to go some re-gathering.
4515     (when (stringp (car thread))
4516       (let ((subject (car thread))
4517             roots thr)
4518         (setq thread (cdr thread))
4519         (while thread
4520           (unless (memq (setq thr (gnus-id-to-thread
4521                                    (gnus-root-id
4522                                     (mail-header-id (caar thread)))))
4523                         roots)
4524             (push thr roots))
4525           (setq thread (cdr thread)))
4526         ;; We now have all (unique) roots.
4527         (if (= (length roots) 1)
4528             ;; All the loose roots are now one solid root.
4529             (setq thread (car roots))
4530           (setq thread (cons subject (gnus-sort-threads roots))))))
4531     (let (threads)
4532       ;; We then insert this thread into the summary buffer.
4533       (when line
4534         (goto-char (point-min))
4535         (forward-line (1- line)))
4536       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4537         (if gnus-show-threads
4538             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4539           (gnus-summary-prepare-unthreaded thread))
4540         (setq data (nreverse gnus-newsgroup-data))
4541         (setq threads gnus-newsgroup-threads))
4542       ;; We splice the new data into the data structure.
4543       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4544       ;;!!! then we want to insert at the beginning of the buffer.
4545       ;;!!! That happens to be true with Gnus now, but that may
4546       ;;!!! change in the future.  Perhaps.
4547       (gnus-data-enter-list
4548        (if line nil current) data (- (point) old-pos))
4549       (setq gnus-newsgroup-threads
4550             (nconc threads gnus-newsgroup-threads))
4551       (gnus-data-compute-positions))))
4552
4553 (defun gnus-number-to-header (number)
4554   "Return the header for article NUMBER."
4555   (let ((headers gnus-newsgroup-headers))
4556     (while (and headers
4557                 (not (= number (mail-header-number (car headers)))))
4558       (pop headers))
4559     (when headers
4560       (car headers))))
4561
4562 (defun gnus-parent-headers (in-headers &optional generation)
4563   "Return the headers of the GENERATIONeth parent of HEADERS."
4564   (unless generation
4565     (setq generation 1))
4566   (let ((parent t)
4567         (headers in-headers)
4568         references)
4569     (while (and parent
4570                 (not (zerop generation))
4571                 (setq references (mail-header-references headers)))
4572       (setq headers (if (and references
4573                              (setq parent (gnus-parent-id references)))
4574                         (car (gnus-id-to-thread parent))
4575                       nil))
4576       (decf generation))
4577     (and (not (eq headers in-headers))
4578          headers)))
4579
4580 (defun gnus-id-to-thread (id)
4581   "Return the (sub-)thread where ID appears."
4582   (gnus-gethash id gnus-newsgroup-dependencies))
4583
4584 (defun gnus-id-to-article (id)
4585   "Return the article number of ID."
4586   (let ((thread (gnus-id-to-thread id)))
4587     (when (and thread
4588                (car thread))
4589       (mail-header-number (car thread)))))
4590
4591 (defun gnus-id-to-header (id)
4592   "Return the article headers of ID."
4593   (car (gnus-id-to-thread id)))
4594
4595 (defun gnus-article-displayed-root-p (article)
4596   "Say whether ARTICLE is a root(ish) article."
4597   (let ((level (gnus-summary-thread-level article))
4598         (refs (mail-header-references  (gnus-summary-article-header article)))
4599         particle)
4600     (cond
4601      ((null level) nil)
4602      ((zerop level) t)
4603      ((null refs) t)
4604      ((null (gnus-parent-id refs)) t)
4605      ((and (= 1 level)
4606            (null (setq particle (gnus-id-to-article
4607                                  (gnus-parent-id refs))))
4608            (null (gnus-summary-thread-level particle)))))))
4609
4610 (defun gnus-root-id (id)
4611   "Return the id of the root of the thread where ID appears."
4612   (let (last-id prev)
4613     (while (and id (setq prev (car (gnus-id-to-thread id))))
4614       (setq last-id id
4615             id (gnus-parent-id (mail-header-references prev))))
4616     last-id))
4617
4618 (defun gnus-articles-in-thread (thread)
4619   "Return the list of articles in THREAD."
4620   (cons (mail-header-number (car thread))
4621         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4622
4623 (defun gnus-remove-thread (id &optional dont-remove)
4624   "Remove the thread that has ID in it."
4625   (let (headers thread last-id)
4626     ;; First go up in this thread until we find the root.
4627     (setq last-id (gnus-root-id id)
4628           headers (message-flatten-list (gnus-id-to-thread last-id)))
4629     ;; We have now found the real root of this thread.  It might have
4630     ;; been gathered into some loose thread, so we have to search
4631     ;; through the threads to find the thread we wanted.
4632     (let ((threads gnus-newsgroup-threads)
4633           sub)
4634       (while threads
4635         (setq sub (car threads))
4636         (if (stringp (car sub))
4637             ;; This is a gathered thread, so we look at the roots
4638             ;; below it to find whether this article is in this
4639             ;; gathered root.
4640             (progn
4641               (setq sub (cdr sub))
4642               (while sub
4643                 (when (member (caar sub) headers)
4644                   (setq thread (car threads)
4645                         threads nil
4646                         sub nil))
4647                 (setq sub (cdr sub))))
4648           ;; It's an ordinary thread, so we check it.
4649           (when (eq (car sub) (car headers))
4650             (setq thread sub
4651                   threads nil)))
4652         (setq threads (cdr threads)))
4653       ;; If this article is in no thread, then it's a root.
4654       (if thread
4655           (unless dont-remove
4656             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4657         (setq thread (gnus-id-to-thread last-id)))
4658       (when thread
4659         (prog1
4660             thread                      ; We return this thread.
4661           (unless dont-remove
4662             (if (stringp (car thread))
4663                 (progn
4664                   ;; If we use dummy roots, then we have to remove the
4665                   ;; dummy root as well.
4666                   (when (eq gnus-summary-make-false-root 'dummy)
4667                     ;; We go to the dummy root by going to
4668                     ;; the first sub-"thread", and then one line up.
4669                     (gnus-summary-goto-article
4670                      (mail-header-number (caadr thread)))
4671                     (forward-line -1)
4672                     (gnus-delete-line)
4673                     (gnus-data-compute-positions))
4674                   (setq thread (cdr thread))
4675                   (while thread
4676                     (gnus-remove-thread-1 (car thread))
4677                     (setq thread (cdr thread))))
4678               (gnus-remove-thread-1 thread))))))))
4679
4680 (defun gnus-remove-thread-1 (thread)
4681   "Remove the thread THREAD recursively."
4682   (let ((number (mail-header-number (pop thread)))
4683         d)
4684     (setq thread (reverse thread))
4685     (while thread
4686       (gnus-remove-thread-1 (pop thread)))
4687     (when (setq d (gnus-data-find number))
4688       (goto-char (gnus-data-pos d))
4689       (gnus-summary-show-thread)
4690       (gnus-data-remove
4691        number
4692        (- (point-at-bol)
4693           (prog1
4694               (1+ (point-at-eol))
4695             (gnus-delete-line)))))))
4696
4697 (defun gnus-sort-threads-recursive (threads func)
4698   (sort (mapcar (lambda (thread)
4699                   (cons (car thread)
4700                         (and (cdr thread)
4701                              (gnus-sort-threads-recursive (cdr thread) func))))
4702                 threads) func))
4703
4704 (defun gnus-sort-threads-loop (threads func)
4705   (let* ((superthread (cons nil threads))
4706          (stack (list (cons superthread threads)))
4707          remaining-threads thread)
4708     (while stack
4709       (setq remaining-threads (cdr (car stack)))
4710       (if remaining-threads
4711           (progn (setq thread (car remaining-threads))
4712                  (setcdr (car stack) (cdr remaining-threads))
4713                  (if (cdr thread)
4714                      (push (cons thread (cdr thread)) stack)))
4715         (setq thread (caar stack))
4716         (setcdr thread (sort (cdr thread) func))
4717         (pop stack)))
4718     (cdr superthread)))
4719
4720 (defun gnus-sort-threads (threads)
4721   "Sort THREADS."
4722   (if (not gnus-thread-sort-functions)
4723       threads
4724     (gnus-message 8 "Sorting threads...")
4725     (prog1
4726         (condition-case nil
4727             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4728               (gnus-sort-threads-recursive
4729                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4730           ;; Even after binding max-lisp-eval-depth, the recursive
4731           ;; sorter might fail for very long threads.  In that case,
4732           ;; try using a (less well-tested) non-recursive sorter.
4733           (error (gnus-sort-threads-loop
4734                   threads (gnus-make-sort-function
4735                            gnus-thread-sort-functions))))
4736       (gnus-message 8 "Sorting threads...done"))))
4737
4738 (defun gnus-sort-articles (articles)
4739   "Sort ARTICLES."
4740   (when gnus-article-sort-functions
4741     (gnus-message 7 "Sorting articles...")
4742     (prog1
4743         (setq gnus-newsgroup-headers
4744               (sort articles (gnus-make-sort-function
4745                               gnus-article-sort-functions)))
4746       (gnus-message 7 "Sorting articles...done"))))
4747
4748 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4749 (defmacro gnus-thread-header (thread)
4750   "Return header of first article in THREAD.
4751 Note that THREAD must never, ever be anything else than a variable -
4752 using some other form will lead to serious barfage."
4753   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4754   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4755   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4756         (vector thread) 2))
4757
4758 (defsubst gnus-article-sort-by-number (h1 h2)
4759   "Sort articles by article number."
4760   (< (mail-header-number h1)
4761      (mail-header-number h2)))
4762
4763 (defun gnus-thread-sort-by-number (h1 h2)
4764   "Sort threads by root article number."
4765   (gnus-article-sort-by-number
4766    (gnus-thread-header h1) (gnus-thread-header h2)))
4767
4768 (defsubst gnus-article-sort-by-random (h1 h2)
4769   "Sort articles by article number."
4770   (zerop (random 2)))
4771
4772 (defun gnus-thread-sort-by-random (h1 h2)
4773   "Sort threads by root article number."
4774   (gnus-article-sort-by-random
4775    (gnus-thread-header h1) (gnus-thread-header h2)))
4776
4777 (defsubst gnus-article-sort-by-lines (h1 h2)
4778   "Sort articles by article Lines header."
4779   (< (mail-header-lines h1)
4780      (mail-header-lines h2)))
4781
4782 (defun gnus-thread-sort-by-lines (h1 h2)
4783   "Sort threads by root article Lines header."
4784   (gnus-article-sort-by-lines
4785    (gnus-thread-header h1) (gnus-thread-header h2)))
4786
4787 (defsubst gnus-article-sort-by-chars (h1 h2)
4788   "Sort articles by octet length."
4789   (< (mail-header-chars h1)
4790      (mail-header-chars h2)))
4791
4792 (defun gnus-thread-sort-by-chars (h1 h2)
4793   "Sort threads by root article octet length."
4794   (gnus-article-sort-by-chars
4795    (gnus-thread-header h1) (gnus-thread-header h2)))
4796
4797 (defsubst gnus-article-sort-by-author (h1 h2)
4798   "Sort articles by root author."
4799   (string-lessp
4800    (let ((extract (funcall
4801                    gnus-extract-address-components
4802                    (mail-header-from h1))))
4803      (or (car extract) (cadr extract) ""))
4804    (let ((extract (funcall
4805                    gnus-extract-address-components
4806                    (mail-header-from h2))))
4807      (or (car extract) (cadr extract) ""))))
4808
4809 (defun gnus-thread-sort-by-author (h1 h2)
4810   "Sort threads by root author."
4811   (gnus-article-sort-by-author
4812    (gnus-thread-header h1)  (gnus-thread-header h2)))
4813
4814 (defsubst gnus-article-sort-by-recipient (h1 h2)
4815   "Sort articles by recipient."
4816   (string-lessp
4817    (let ((extract (funcall
4818                    gnus-extract-address-components
4819                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4820      (or (car extract) (cadr extract)))
4821    (let ((extract (funcall
4822                    gnus-extract-address-components
4823                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4824      (or (car extract) (cadr extract)))))
4825
4826 (defun gnus-thread-sort-by-recipient (h1 h2)
4827   "Sort threads by root recipient."
4828   (gnus-article-sort-by-recipient
4829    (gnus-thread-header h1) (gnus-thread-header h2)))
4830
4831 (defsubst gnus-article-sort-by-subject (h1 h2)
4832   "Sort articles by root subject."
4833   (string-lessp
4834    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4835    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4836
4837 (defun gnus-thread-sort-by-subject (h1 h2)
4838   "Sort threads by root subject."
4839   (gnus-article-sort-by-subject
4840    (gnus-thread-header h1) (gnus-thread-header h2)))
4841
4842 (defsubst gnus-article-sort-by-date (h1 h2)
4843   "Sort articles by root article date."
4844   (time-less-p
4845    (gnus-date-get-time (mail-header-date h1))
4846    (gnus-date-get-time (mail-header-date h2))))
4847
4848 (defun gnus-thread-sort-by-date (h1 h2)
4849   "Sort threads by root article date."
4850   (gnus-article-sort-by-date
4851    (gnus-thread-header h1) (gnus-thread-header h2)))
4852
4853 (defsubst gnus-article-sort-by-score (h1 h2)
4854   "Sort articles by root article score.
4855 Unscored articles will be counted as having a score of zero."
4856   (> (or (cdr (assq (mail-header-number h1)
4857                     gnus-newsgroup-scored))
4858          gnus-summary-default-score 0)
4859      (or (cdr (assq (mail-header-number h2)
4860                     gnus-newsgroup-scored))
4861          gnus-summary-default-score 0)))
4862
4863 (defun gnus-thread-sort-by-score (h1 h2)
4864   "Sort threads by root article score."
4865   (gnus-article-sort-by-score
4866    (gnus-thread-header h1) (gnus-thread-header h2)))
4867
4868 (defun gnus-thread-sort-by-total-score (h1 h2)
4869   "Sort threads by the sum of all scores in the thread.
4870 Unscored articles will be counted as having a score of zero."
4871   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4872
4873 (defun gnus-thread-total-score (thread)
4874   ;; This function find the total score of THREAD.
4875   (cond
4876    ((null thread)
4877     0)
4878    ((consp thread)
4879     (if (stringp (car thread))
4880         (apply gnus-thread-score-function 0
4881                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4882       (gnus-thread-total-score-1 thread)))
4883    (t
4884     (gnus-thread-total-score-1 (list thread)))))
4885
4886 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4887   "Sort threads such that the thread with the most recently arrived article comes first."
4888   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4889
4890 (defun gnus-thread-highest-number (thread)
4891   "Return the highest article number in THREAD."
4892   (apply 'max (mapcar (lambda (header)
4893                         (mail-header-number header))
4894                       (message-flatten-list thread))))
4895
4896 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4897   "Sort threads such that the thread with the most recently dated article comes first."
4898   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4899
4900 (defun gnus-thread-latest-date (thread)
4901   "Return the highest article date in THREAD."
4902   (let ((previous-time 0))
4903     (apply 'max
4904            (mapcar
4905             (lambda (header)
4906               (setq previous-time
4907                     (condition-case ()
4908                         (time-to-seconds (mail-header-parse-date
4909                                           (mail-header-date header)))
4910                       (error previous-time))))
4911             (sort
4912              (message-flatten-list thread)
4913              (lambda (h1 h2)
4914                (< (mail-header-number h1)
4915                   (mail-header-number h2))))))))
4916
4917 (defun gnus-thread-total-score-1 (root)
4918   ;; This function find the total score of the thread below ROOT.
4919   (setq root (car root))
4920   (apply gnus-thread-score-function
4921          (or (append
4922               (mapcar 'gnus-thread-total-score
4923                       (cdr (gnus-id-to-thread (mail-header-id root))))
4924               (when (> (mail-header-number root) 0)
4925                 (list (or (cdr (assq (mail-header-number root)
4926                                      gnus-newsgroup-scored))
4927                           gnus-summary-default-score 0))))
4928              (list gnus-summary-default-score)
4929              '(0))))
4930
4931 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4932 (defvar gnus-tmp-prev-subject nil)
4933 (defvar gnus-tmp-false-parent nil)
4934 (defvar gnus-tmp-root-expunged nil)
4935 (defvar gnus-tmp-dummy-line nil)
4936
4937 (eval-when-compile (defvar gnus-tmp-header))
4938 (defun gnus-extra-header (type &optional header)
4939   "Return the extra header of TYPE."
4940   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4941       ""))
4942
4943 (defvar gnus-tmp-thread-tree-header-string "")
4944
4945 (defcustom gnus-sum-thread-tree-root "> "
4946   "With %B spec, used for the root of a thread.
4947 If nil, use subject instead."
4948   :version "22.1"
4949   :type '(radio (const :format "%v  " nil) string)
4950   :group 'gnus-thread)
4951
4952 (defcustom gnus-sum-thread-tree-false-root "> "
4953   "With %B spec, used for a false root of a thread.
4954 If nil, use subject instead."
4955   :version "22.1"
4956   :type '(radio (const :format "%v  " nil) string)
4957   :group 'gnus-thread)
4958
4959 (defcustom gnus-sum-thread-tree-single-indent ""
4960   "With %B spec, used for a thread with just one message.
4961 If nil, use subject instead."
4962   :version "22.1"
4963   :type '(radio (const :format "%v  " nil) string)
4964   :group 'gnus-thread)
4965
4966 (defcustom gnus-sum-thread-tree-vertical "| "
4967   "With %B spec, used for drawing a vertical line."
4968   :version "22.1"
4969   :type 'string
4970   :group 'gnus-thread)
4971
4972 (defcustom gnus-sum-thread-tree-indent "  "
4973   "With %B spec, used for indenting."
4974   :version "22.1"
4975   :type 'string
4976   :group 'gnus-thread)
4977
4978 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4979   "With %B spec, used for a leaf with brothers."
4980   :version "22.1"
4981   :type 'string
4982   :group 'gnus-thread)
4983
4984 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4985   "With %B spec, used for a leaf without brothers."
4986   :version "22.1"
4987   :type 'string
4988   :group 'gnus-thread)
4989
4990 (defun gnus-summary-prepare-threads (threads)
4991   "Prepare summary buffer from THREADS and indentation LEVEL.
4992 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4993 or a straight list of headers."
4994   (gnus-message 7 "Generating summary...")
4995
4996   (setq gnus-newsgroup-threads threads)
4997   (beginning-of-line)
4998
4999   (let ((gnus-tmp-level 0)
5000         (default-score (or gnus-summary-default-score 0))
5001         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5002         (building-line-count gnus-summary-display-while-building)
5003         (building-count (integerp gnus-summary-display-while-building))
5004         thread number subject stack state gnus-tmp-gathered beg-match
5005         new-roots gnus-tmp-new-adopts thread-end simp-subject
5006         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5007         gnus-tmp-replied gnus-tmp-subject-or-nil
5008         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5009         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5010         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5011         tree-stack)
5012
5013     (setq gnus-tmp-prev-subject nil
5014           gnus-tmp-thread-tree-header-string "")
5015
5016     (if (vectorp (car threads))
5017         ;; If this is a straight (sic) list of headers, then a
5018         ;; threaded summary display isn't required, so we just create
5019         ;; an unthreaded one.
5020         (gnus-summary-prepare-unthreaded threads)
5021
5022       ;; Do the threaded display.
5023
5024       (if gnus-summary-display-while-building
5025           (switch-to-buffer (buffer-name)))
5026       (while (or threads stack gnus-tmp-new-adopts new-roots)
5027
5028         (if (and (= gnus-tmp-level 0)
5029                  (or (not stack)
5030                      (= (caar stack) 0))
5031                  (not gnus-tmp-false-parent)
5032                  (or gnus-tmp-new-adopts new-roots))
5033             (if gnus-tmp-new-adopts
5034                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5035                       thread (list (car gnus-tmp-new-adopts))
5036                       gnus-tmp-header (caar thread)
5037                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5038               (when new-roots
5039                 (setq thread (list (car new-roots))
5040                       gnus-tmp-header (caar thread)
5041                       new-roots (cdr new-roots))))
5042
5043           (if threads
5044               ;; If there are some threads, we do them before the
5045               ;; threads on the stack.
5046               (setq thread threads
5047                     gnus-tmp-header (caar thread))
5048             ;; There were no current threads, so we pop something off
5049             ;; the stack.
5050             (setq state (car stack)
5051                   gnus-tmp-level (car state)
5052                   tree-stack (cadr state)
5053                   thread (caddr state)
5054                   stack (cdr stack)
5055                   gnus-tmp-header (caar thread))))
5056
5057         (setq gnus-tmp-false-parent nil)
5058         (setq gnus-tmp-root-expunged nil)
5059         (setq thread-end nil)
5060
5061         (if (stringp gnus-tmp-header)
5062             ;; The header is a dummy root.
5063             (cond
5064              ((eq gnus-summary-make-false-root 'adopt)
5065               ;; We let the first article adopt the rest.
5066               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5067                                                (cddar thread)))
5068               (setq gnus-tmp-gathered
5069                     (nconc (mapcar
5070                             (lambda (h) (mail-header-number (car h)))
5071                             (cddar thread))
5072                            gnus-tmp-gathered))
5073               (setq thread (cons (list (caar thread)
5074                                        (cadar thread))
5075                                  (cdr thread)))
5076               (setq gnus-tmp-level -1
5077                     gnus-tmp-false-parent t))
5078              ((eq gnus-summary-make-false-root 'empty)
5079               ;; We print adopted articles with empty subject fields.
5080               (setq gnus-tmp-gathered
5081                     (nconc (mapcar
5082                             (lambda (h) (mail-header-number (car h)))
5083                             (cddar thread))
5084                            gnus-tmp-gathered))
5085               (setq gnus-tmp-level -1))
5086              ((eq gnus-summary-make-false-root 'dummy)
5087               ;; We remember that we probably want to output a dummy
5088               ;; root.
5089               (setq gnus-tmp-dummy-line gnus-tmp-header)
5090               (setq gnus-tmp-prev-subject gnus-tmp-header))
5091              (t
5092               ;; We do not make a root for the gathered
5093               ;; sub-threads at all.
5094               (setq gnus-tmp-level -1)))
5095
5096           (setq number (mail-header-number gnus-tmp-header)
5097                 subject (mail-header-subject gnus-tmp-header)
5098                 simp-subject (gnus-simplify-subject-fully subject))
5099
5100           (cond
5101            ;; If the thread has changed subject, we might want to make
5102            ;; this subthread into a root.
5103            ((and (null gnus-thread-ignore-subject)
5104                  (not (zerop gnus-tmp-level))
5105                  gnus-tmp-prev-subject
5106                  (not (string= gnus-tmp-prev-subject simp-subject)))
5107             (setq new-roots (nconc new-roots (list (car thread)))
5108                   thread-end t
5109                   gnus-tmp-header nil))
5110            ;; If the article lies outside the current limit,
5111            ;; then we do not display it.
5112            ((not (memq number gnus-newsgroup-limit))
5113             (setq gnus-tmp-gathered
5114                   (nconc (mapcar
5115                           (lambda (h) (mail-header-number (car h)))
5116                           (cdar thread))
5117                          gnus-tmp-gathered))
5118             (setq gnus-tmp-new-adopts (if (cdar thread)
5119                                           (append gnus-tmp-new-adopts
5120                                                   (cdar thread))
5121                                         gnus-tmp-new-adopts)
5122                   thread-end t
5123                   gnus-tmp-header nil)
5124             (when (zerop gnus-tmp-level)
5125               (setq gnus-tmp-root-expunged t)))
5126            ;; Perhaps this article is to be marked as read?
5127            ((and gnus-summary-mark-below
5128                  (< (or (cdr (assq number gnus-newsgroup-scored))
5129                         default-score)
5130                     gnus-summary-mark-below)
5131                  ;; Don't touch sparse articles.
5132                  (not (gnus-summary-article-sparse-p number))
5133                  (not (gnus-summary-article-ancient-p number)))
5134             (setq gnus-newsgroup-unreads
5135                   (delq number gnus-newsgroup-unreads))
5136             (if gnus-newsgroup-auto-expire
5137                 (setq gnus-newsgroup-expirable
5138                       (gnus-add-to-sorted-list
5139                        gnus-newsgroup-expirable number))
5140               (push (cons number gnus-low-score-mark)
5141                     gnus-newsgroup-reads))))
5142
5143           (when gnus-tmp-header
5144             ;; We may have an old dummy line to output before this
5145             ;; article.
5146             (when (and gnus-tmp-dummy-line
5147                        (gnus-subject-equal
5148                         gnus-tmp-dummy-line
5149                         (mail-header-subject gnus-tmp-header)))
5150               (gnus-summary-insert-dummy-line
5151                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5152               (setq gnus-tmp-dummy-line nil))
5153
5154             ;; Compute the mark.
5155             (setq gnus-tmp-unread (gnus-article-mark number))
5156
5157             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5158                                   gnus-tmp-header gnus-tmp-level)
5159                   gnus-newsgroup-data)
5160
5161             ;; Actually insert the line.
5162             (setq
5163              gnus-tmp-subject-or-nil
5164              (cond
5165               ((and gnus-thread-ignore-subject
5166                     gnus-tmp-prev-subject
5167                     (not (string= gnus-tmp-prev-subject simp-subject)))
5168                subject)
5169               ((zerop gnus-tmp-level)
5170                (if (and (eq gnus-summary-make-false-root 'empty)
5171                         (memq number gnus-tmp-gathered)
5172                         gnus-tmp-prev-subject
5173                         (string= gnus-tmp-prev-subject simp-subject))
5174                    gnus-summary-same-subject
5175                  subject))
5176               (t gnus-summary-same-subject)))
5177             (if (and (eq gnus-summary-make-false-root 'adopt)
5178                      (= gnus-tmp-level 1)
5179                      (memq number gnus-tmp-gathered))
5180                 (setq gnus-tmp-opening-bracket ?\<
5181                       gnus-tmp-closing-bracket ?\>)
5182               (setq gnus-tmp-opening-bracket ?\[
5183                     gnus-tmp-closing-bracket ?\]))
5184             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5185                 (gnus-make-thread-indent-array
5186                  (max (* 2 (length gnus-thread-indent-array))
5187                       gnus-tmp-level)))
5188             (setq
5189              gnus-tmp-indentation
5190              (aref gnus-thread-indent-array gnus-tmp-level)
5191              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5192              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5193                                 gnus-summary-default-score 0)
5194              gnus-tmp-score-char
5195              (if (or (null gnus-summary-default-score)
5196                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5197                          gnus-summary-zcore-fuzz))
5198                  ?                      ;Whitespace
5199                (if (< gnus-tmp-score gnus-summary-default-score)
5200                    gnus-score-below-mark gnus-score-over-mark))
5201              gnus-tmp-replied
5202              (cond ((memq number gnus-newsgroup-processable)
5203                     gnus-process-mark)
5204                    ((memq number gnus-newsgroup-cached)
5205                     gnus-cached-mark)
5206                    ((memq number gnus-newsgroup-replied)
5207                     gnus-replied-mark)
5208                    ((memq number gnus-newsgroup-forwarded)
5209                     gnus-forwarded-mark)
5210                    ((memq number gnus-newsgroup-saved)
5211                     gnus-saved-mark)
5212                    ((memq number gnus-newsgroup-recent)
5213                     gnus-recent-mark)
5214                    ((memq number gnus-newsgroup-unseen)
5215                     gnus-unseen-mark)
5216                    (t gnus-no-mark))
5217              gnus-tmp-downloaded
5218              (cond ((memq number gnus-newsgroup-undownloaded)
5219                     gnus-undownloaded-mark)
5220                    (gnus-newsgroup-agentized
5221                     gnus-downloaded-mark)
5222                    (t
5223                     gnus-no-mark))
5224              gnus-tmp-from (mail-header-from gnus-tmp-header)
5225              gnus-tmp-name
5226              (cond
5227               ((string-match "<[^>]+> *$" gnus-tmp-from)
5228                (setq beg-match (match-beginning 0))
5229                (or (and (string-match "^\".+\"" gnus-tmp-from)
5230                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5231                    (substring gnus-tmp-from 0 beg-match)))
5232               ((string-match "(.+)" gnus-tmp-from)
5233                (substring gnus-tmp-from
5234                           (1+ (match-beginning 0)) (1- (match-end 0))))
5235               (t gnus-tmp-from))
5236
5237              ;; Do the %B string
5238              gnus-tmp-thread-tree-header-string
5239              (cond
5240               ((not gnus-show-threads) "")
5241               ((zerop gnus-tmp-level)
5242                (cond ((cdar thread)
5243                       (or gnus-sum-thread-tree-root subject))
5244                      (gnus-tmp-new-adopts
5245                       (or gnus-sum-thread-tree-false-root subject))
5246                      (t
5247                       (or gnus-sum-thread-tree-single-indent subject))))
5248               (t
5249                (concat (apply 'concat
5250                               (mapcar (lambda (item)
5251                                         (if (= item 1)
5252                                             gnus-sum-thread-tree-vertical
5253                                           gnus-sum-thread-tree-indent))
5254                                       (cdr (reverse tree-stack))))
5255                        (if (nth 1 thread)
5256                            gnus-sum-thread-tree-leaf-with-other
5257                          gnus-sum-thread-tree-single-leaf)))))
5258             (when (string= gnus-tmp-name "")
5259               (setq gnus-tmp-name gnus-tmp-from))
5260             (unless (numberp gnus-tmp-lines)
5261               (setq gnus-tmp-lines -1))
5262             (if (= gnus-tmp-lines -1)
5263                 (setq gnus-tmp-lines "?")
5264               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5265               (gnus-put-text-property
5266              (point)
5267              (progn (eval gnus-summary-line-format-spec) (point))
5268                'gnus-number number)
5269             (when gnus-visual-p
5270               (forward-line -1)
5271               (gnus-run-hooks 'gnus-summary-update-hook)
5272               (forward-line 1))
5273
5274             (setq gnus-tmp-prev-subject simp-subject)))
5275
5276         (when (nth 1 thread)
5277           (push (list (max 0 gnus-tmp-level)
5278                       (copy-sequence tree-stack)
5279                       (nthcdr 1 thread))
5280                 stack))
5281         (push (if (nth 1 thread) 1 0) tree-stack)
5282         (incf gnus-tmp-level)
5283         (setq threads (if thread-end nil (cdar thread)))
5284         (if gnus-summary-display-while-building
5285             (if building-count
5286                 (progn
5287                   ;; use a set frequency
5288                   (setq building-line-count (1- building-line-count))
5289                   (when (= building-line-count 0)
5290                     (sit-for 0)
5291                     (setq building-line-count
5292                           gnus-summary-display-while-building)))
5293               ;; always
5294               (sit-for 0)))
5295         (unless threads
5296           (setq gnus-tmp-level 0)))))
5297   (gnus-message 7 "Generating summary...done"))
5298
5299 (defun gnus-summary-prepare-unthreaded (headers)
5300   "Generate an unthreaded summary buffer based on HEADERS."
5301   (let (header number mark)
5302
5303     (beginning-of-line)
5304
5305     (while headers
5306       ;; We may have to root out some bad articles...
5307       (when (memq (setq number (mail-header-number
5308                                 (setq header (pop headers))))
5309                   gnus-newsgroup-limit)
5310         ;; Mark article as read when it has a low score.
5311         (when (and gnus-summary-mark-below
5312                    (< (or (cdr (assq number gnus-newsgroup-scored))
5313                           gnus-summary-default-score 0)
5314                       gnus-summary-mark-below)
5315                    (not (gnus-summary-article-ancient-p number)))
5316           (setq gnus-newsgroup-unreads
5317                 (delq number gnus-newsgroup-unreads))
5318           (if gnus-newsgroup-auto-expire
5319               (push number gnus-newsgroup-expirable)
5320             (push (cons number gnus-low-score-mark)
5321                   gnus-newsgroup-reads)))
5322
5323         (setq mark (gnus-article-mark number))
5324         (push (gnus-data-make number mark (1+ (point)) header 0)
5325               gnus-newsgroup-data)
5326         (gnus-summary-insert-line
5327          header 0 number
5328          (memq number gnus-newsgroup-undownloaded)
5329          mark (memq number gnus-newsgroup-replied)
5330          (memq number gnus-newsgroup-expirable)
5331          (mail-header-subject header) nil
5332          (cdr (assq number gnus-newsgroup-scored))
5333          (memq number gnus-newsgroup-processable))))))
5334
5335 (defun gnus-summary-remove-list-identifiers ()
5336   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5337   (let ((regexp (if (consp gnus-list-identifiers)
5338                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5339                   gnus-list-identifiers))
5340         changed subject)
5341     (when regexp
5342       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5343       (dolist (header gnus-newsgroup-headers)
5344         (setq subject (mail-header-subject header)
5345               changed nil)
5346         (while (string-match regexp subject)
5347           (setq subject
5348                 (concat (substring subject 0 (match-beginning 1))
5349                         (substring subject (match-end 0)))
5350                 changed t))
5351         (when changed
5352           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5353             (setq subject
5354                   (concat (substring subject 0 (match-beginning 1))
5355                           (substring subject (match-end 1)))))
5356           (mail-header-set-subject header subject))))))
5357
5358 (defun gnus-fetch-headers (articles)
5359   "Fetch headers of ARTICLES."
5360   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5361     (gnus-message 5 "Fetching headers for %s..." name)
5362     (prog1
5363         (if (eq 'nov
5364                 (setq gnus-headers-retrieved-by
5365                       (gnus-retrieve-headers
5366                        articles gnus-newsgroup-name
5367                        ;; We might want to fetch old headers, but
5368                        ;; not if there is only 1 article.
5369                        (and (or (and
5370                                  (not (eq gnus-fetch-old-headers 'some))
5371                                  (not (numberp gnus-fetch-old-headers)))
5372                                 (> (length articles) 1))
5373                             gnus-fetch-old-headers))))
5374             (gnus-get-newsgroup-headers-xover
5375              articles nil nil gnus-newsgroup-name t)
5376           (gnus-get-newsgroup-headers))
5377       (gnus-message 5 "Fetching headers for %s...done" name))))
5378
5379 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5380   "Select newsgroup GROUP.
5381 If READ-ALL is non-nil, all articles in the group are selected.
5382 If SELECT-ARTICLES, only select those articles from GROUP."
5383   (let* ((entry (gnus-group-entry group))
5384          ;;!!! Dirty hack; should be removed.
5385          (gnus-summary-ignore-duplicates
5386           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5387               t
5388             gnus-summary-ignore-duplicates))
5389          (info (nth 2 entry))
5390          articles fetched-articles cached)
5391
5392     (unless (gnus-check-server
5393              (set (make-local-variable 'gnus-current-select-method)
5394                   (gnus-find-method-for-group group)))
5395       (error "Couldn't open server"))
5396
5397     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5398         (gnus-activate-group group)     ; Or we can activate it...
5399         (progn                          ; Or we bug out.
5400           (when (equal major-mode 'gnus-summary-mode)
5401             (gnus-kill-buffer (current-buffer)))
5402           (error "Couldn't activate group %s: %s"
5403                  (gnus-group-decoded-name group) (gnus-status-message group))))
5404
5405     (unless (gnus-request-group group t)
5406       (when (equal major-mode 'gnus-summary-mode)
5407         (gnus-kill-buffer (current-buffer)))
5408       (error "Couldn't request group %s: %s"
5409              (gnus-group-decoded-name group) (gnus-status-message group)))
5410
5411     (when gnus-agent
5412       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5413
5414       (setq gnus-summary-use-undownloaded-faces
5415             (gnus-agent-find-parameter
5416              group
5417              'agent-enable-undownloaded-faces)))
5418
5419     (setq gnus-newsgroup-name group
5420           gnus-newsgroup-unselected nil
5421           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5422
5423     (let ((display (gnus-group-find-parameter group 'display)))
5424       (setq gnus-newsgroup-display
5425             (cond
5426              ((not (zerop (or (car-safe read-all) 0)))
5427               ;; The user entered the group with C-u SPC/RET, let's show
5428               ;; all articles.
5429               'gnus-not-ignore)
5430              ((eq display 'all)
5431               'gnus-not-ignore)
5432              ((arrayp display)
5433               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5434              ((numberp display)
5435               ;; The following is probably the "correct" solution, but
5436               ;; it makes Gnus fetch all headers and then limit the
5437               ;; articles (which is slow), so instead we hack the
5438               ;; select-articles parameter instead. -- Simon Josefsson
5439               ;; <jas@kth.se>
5440               ;;
5441               ;; (gnus-byte-compile
5442               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5443               ;;                         display)))))
5444               (setq select-articles
5445                     (gnus-uncompress-range
5446                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5447                              (if (> tmp 0)
5448                                  tmp
5449                                1))
5450                            (cdr (gnus-active group)))))
5451               nil)
5452              (t
5453               nil))))
5454
5455     (gnus-summary-setup-default-charset)
5456
5457     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5458     (when (gnus-virtual-group-p group)
5459       (setq cached gnus-newsgroup-cached))
5460
5461     (setq gnus-newsgroup-unreads
5462           (gnus-sorted-ndifference
5463            (gnus-sorted-ndifference gnus-newsgroup-unreads
5464                                     gnus-newsgroup-marked)
5465            gnus-newsgroup-dormant))
5466
5467     (setq gnus-newsgroup-processable nil)
5468
5469     (gnus-update-read-articles group gnus-newsgroup-unreads)
5470
5471     ;; Adjust and set lists of article marks.
5472     (when info
5473       (gnus-adjust-marked-articles info))
5474     (if (setq articles select-articles)
5475         (setq gnus-newsgroup-unselected
5476               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5477       (setq articles (gnus-articles-to-read group read-all)))
5478
5479     (cond
5480      ((null articles)
5481       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5482       'quit)
5483      ((eq articles 0) nil)
5484      (t
5485       ;; Init the dependencies hash table.
5486       (setq gnus-newsgroup-dependencies
5487             (gnus-make-hashtable (length articles)))
5488       (gnus-set-global-variables)
5489       ;; Retrieve the headers and read them in.
5490
5491       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5492
5493       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5494       (when cached
5495         (setq gnus-newsgroup-cached cached))
5496
5497       ;; Suppress duplicates?
5498       (when gnus-suppress-duplicates
5499         (gnus-dup-suppress-articles))
5500
5501       ;; Set the initial limit.
5502       (setq gnus-newsgroup-limit (copy-sequence articles))
5503       ;; Remove canceled articles from the list of unread articles.
5504       (setq fetched-articles
5505             (mapcar (lambda (headers) (mail-header-number headers))
5506                     gnus-newsgroup-headers))
5507       (setq gnus-newsgroup-articles fetched-articles)
5508       (setq gnus-newsgroup-unreads
5509             (gnus-sorted-nintersection
5510              gnus-newsgroup-unreads fetched-articles))
5511       (gnus-compute-unseen-list)
5512
5513       ;; Removed marked articles that do not exist.
5514       (gnus-update-missing-marks
5515        (gnus-sorted-difference articles fetched-articles))
5516       ;; We might want to build some more threads first.
5517       (when (and gnus-fetch-old-headers
5518                  (eq gnus-headers-retrieved-by 'nov))
5519         (if (eq gnus-fetch-old-headers 'invisible)
5520             (gnus-build-all-threads)
5521           (gnus-build-old-threads)))
5522       ;; Let the Gnus agent mark articles as read.
5523       (when gnus-agent
5524         (gnus-agent-get-undownloaded-list))
5525       ;; Remove list identifiers from subject
5526       (when gnus-list-identifiers
5527         (gnus-summary-remove-list-identifiers))
5528       ;; Check whether auto-expire is to be done in this group.
5529       (setq gnus-newsgroup-auto-expire
5530             (gnus-group-auto-expirable-p group))
5531       ;; Set up the article buffer now, if necessary.
5532       (unless (and gnus-single-article-buffer
5533                    (equal gnus-article-buffer "*Article*"))
5534         (gnus-article-setup-buffer))
5535       ;; First and last article in this newsgroup.
5536       (when gnus-newsgroup-headers
5537         (setq gnus-newsgroup-begin
5538               (mail-header-number (car gnus-newsgroup-headers))
5539               gnus-newsgroup-end
5540               (mail-header-number
5541                (gnus-last-element gnus-newsgroup-headers))))
5542       ;; GROUP is successfully selected.
5543       (or gnus-newsgroup-headers t)))))
5544
5545 (defun gnus-compute-unseen-list ()
5546   ;; The `seen' marks are treated specially.
5547   (if (not gnus-newsgroup-seen)
5548       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5549     (setq gnus-newsgroup-unseen
5550           (gnus-inverse-list-range-intersection
5551            gnus-newsgroup-articles gnus-newsgroup-seen))))
5552
5553 (defun gnus-summary-display-make-predicate (display)
5554   (require 'gnus-agent)
5555   (when (= (length display) 1)
5556     (setq display (car display)))
5557   (unless gnus-summary-display-cache
5558     (dolist (elem (append '((unread . unread)
5559                             (read . read)
5560                             (unseen . unseen))
5561                           gnus-article-mark-lists))
5562       (push (cons (cdr elem)
5563                   (gnus-byte-compile
5564                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5565             gnus-summary-display-cache)))
5566   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5567         (gnus-category-predicate-cache gnus-summary-display-cache))
5568     (gnus-get-predicate display)))
5569
5570 ;; Uses the dynamically bound `number' variable.
5571 (eval-when-compile
5572   (defvar number))
5573 (defun gnus-article-marked-p (type &optional article)
5574   (let ((article (or article number)))
5575     (cond
5576      ((eq type 'tick)
5577       (memq article gnus-newsgroup-marked))
5578      ((eq type 'spam)
5579       (memq article gnus-newsgroup-spam-marked))
5580      ((eq type 'unsend)
5581       (memq article gnus-newsgroup-unsendable))
5582      ((eq type 'undownload)
5583       (memq article gnus-newsgroup-undownloaded))
5584      ((eq type 'download)
5585       (memq article gnus-newsgroup-downloadable))
5586      ((eq type 'unread)
5587       (memq article gnus-newsgroup-unreads))
5588      ((eq type 'read)
5589       (memq article gnus-newsgroup-reads))
5590      ((eq type 'dormant)
5591       (memq article gnus-newsgroup-dormant) )
5592      ((eq type 'expire)
5593       (memq article gnus-newsgroup-expirable))
5594      ((eq type 'reply)
5595       (memq article gnus-newsgroup-replied))
5596      ((eq type 'killed)
5597       (memq article gnus-newsgroup-killed))
5598      ((eq type 'bookmark)
5599       (assq article gnus-newsgroup-bookmarks))
5600      ((eq type 'score)
5601       (assq article gnus-newsgroup-scored))
5602      ((eq type 'save)
5603       (memq article gnus-newsgroup-saved))
5604      ((eq type 'cache)
5605       (memq article gnus-newsgroup-cached))
5606      ((eq type 'forward)
5607       (memq article gnus-newsgroup-forwarded))
5608      ((eq type 'seen)
5609       (not (memq article gnus-newsgroup-unseen)))
5610      ((eq type 'recent)
5611       (memq article gnus-newsgroup-recent))
5612      (t t))))
5613
5614 (defun gnus-articles-to-read (group &optional read-all)
5615   "Find out what articles the user wants to read."
5616   (let* ((articles
5617           ;; Select all articles if `read-all' is non-nil, or if there
5618           ;; are no unread articles.
5619           (if (or read-all
5620                   (and (zerop (length gnus-newsgroup-marked))
5621                        (zerop (length gnus-newsgroup-unreads)))
5622                   ;; Fetch all if the predicate is non-nil.
5623                   gnus-newsgroup-display)
5624               ;; We want to select the headers for all the articles in
5625               ;; the group, so we select either all the active
5626               ;; articles in the group, or (if that's nil), the
5627               ;; articles in the cache.
5628               (or
5629                (gnus-uncompress-range (gnus-active group))
5630                (gnus-cache-articles-in-group group))
5631             ;; Select only the "normal" subset of articles.
5632             (gnus-sorted-nunion
5633              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5634              gnus-newsgroup-unreads)))
5635          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5636          (scored (length scored-list))
5637          (number (length articles))
5638          (marked (+ (length gnus-newsgroup-marked)
5639                     (length gnus-newsgroup-dormant)))
5640          (select
5641           (cond
5642            ((numberp read-all)
5643             read-all)
5644            ((numberp gnus-newsgroup-display)
5645             gnus-newsgroup-display)
5646            (t
5647             (condition-case ()
5648                 (cond
5649                  ((and (or (<= scored marked) (= scored number))
5650                        (numberp gnus-large-newsgroup)
5651                        (> number gnus-large-newsgroup))
5652                   (let* ((cursor-in-echo-area nil)
5653                          (initial (gnus-parameter-large-newsgroup-initial
5654                                    gnus-newsgroup-name))
5655                          (input
5656                           (read-string
5657                            (format
5658                             "How many articles from %s (%s %d): "
5659                             (gnus-limit-string
5660                              (gnus-group-decoded-name gnus-newsgroup-name)
5661                              35)
5662                             (if initial "max" "default")
5663                             number)
5664                            (if initial
5665                                (cons (number-to-string initial)
5666                                      0)))))
5667                     (if (string-match "^[ \t]*$" input) number input)))
5668                  ((and (> scored marked) (< scored number)
5669                        (> (- scored number) 20))
5670                   (let ((input
5671                          (read-string
5672                           (format "%s %s (%d scored, %d total): "
5673                                   "How many articles from"
5674                                   (gnus-group-decoded-name group)
5675                                   scored number))))
5676                     (if (string-match "^[ \t]*$" input)
5677                         number input)))
5678                  (t number))
5679               (quit
5680                (message "Quit getting the articles to read")
5681                nil))))))
5682     (setq select (if (stringp select) (string-to-number select) select))
5683     (if (or (null select) (zerop select))
5684         select
5685       (if (and (not (zerop scored)) (<= (abs select) scored))
5686           (progn
5687             (setq articles (sort scored-list '<))
5688             (setq number (length articles)))
5689         (setq articles (copy-sequence articles)))
5690
5691       (when (< (abs select) number)
5692         (if (< select 0)
5693             ;; Select the N oldest articles.
5694             (setcdr (nthcdr (1- (abs select)) articles) nil)
5695           ;; Select the N most recent articles.
5696           (setq articles (nthcdr (- number select) articles))))
5697       (setq gnus-newsgroup-unselected
5698             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5699       (when gnus-alter-articles-to-read-function
5700         (setq articles
5701               (sort
5702                (funcall gnus-alter-articles-to-read-function
5703                         gnus-newsgroup-name articles)
5704                '<)))
5705       articles)))
5706
5707 (defun gnus-killed-articles (killed articles)
5708   (let (out)
5709     (while articles
5710       (when (inline (gnus-member-of-range (car articles) killed))
5711         (push (car articles) out))
5712       (setq articles (cdr articles)))
5713     out))
5714
5715 (defun gnus-uncompress-marks (marks)
5716   "Uncompress the mark ranges in MARKS."
5717   (let ((uncompressed '(score bookmark))
5718         out)
5719     (while marks
5720       (if (memq (caar marks) uncompressed)
5721           (push (car marks) out)
5722         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5723       (setq marks (cdr marks)))
5724     out))
5725
5726 (defun gnus-article-mark-to-type (mark)
5727   "Return the type of MARK."
5728   (or (cadr (assq mark gnus-article-special-mark-lists))
5729       'list))
5730
5731 (defun gnus-article-unpropagatable-p (mark)
5732   "Return whether MARK should be propagated to back end."
5733   (memq mark gnus-article-unpropagated-mark-lists))
5734
5735 (defun gnus-adjust-marked-articles (info)
5736   "Set all article lists and remove all marks that are no longer valid."
5737   (let* ((marked-lists (gnus-info-marks info))
5738          (active (gnus-active (gnus-info-group info)))
5739          (min (car active))
5740          (max (cdr active))
5741          (types gnus-article-mark-lists)
5742          marks var articles article mark mark-type
5743          bgn end)
5744
5745     (dolist (marks marked-lists)
5746       (setq mark (car marks)
5747             mark-type (gnus-article-mark-to-type mark)
5748             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5749
5750       ;; We set the variable according to the type of the marks list,
5751       ;; and then adjust the marks to a subset of the active articles.
5752       (cond
5753        ;; Adjust "simple" lists - compressed yet unsorted
5754        ((eq mark-type 'list)
5755         ;; Simultaneously uncompress and clip to active range
5756         ;; See gnus-uncompress-range for a description of possible marks
5757         (let (l lh)
5758           (if (not (cadr marks))
5759               (set var nil)
5760             (setq articles (if (numberp (cddr marks))
5761                                (list (cdr marks))
5762                              (cdr marks))
5763                   lh (cons nil nil)
5764                   l lh)
5765
5766             (while (setq article (pop articles))
5767               (cond ((consp article)
5768                      (setq bgn (max (car article) min)
5769                            end (min (cdr article) max))
5770                      (while (<= bgn end)
5771                        (setq l (setcdr l (cons bgn nil))
5772                              bgn (1+ bgn))))
5773                     ((and (<= min article)
5774                           (>= max article))
5775                      (setq l (setcdr l (cons article nil))))))
5776             (set var (cdr lh)))))
5777        ;; Adjust assocs.
5778        ((eq mark-type 'tuple)
5779         (set var (setq articles (cdr marks)))
5780         (when (not (listp (cdr (symbol-value var))))
5781           (set var (list (symbol-value var))))
5782         (when (not (listp (cdr articles)))
5783           (setq articles (list articles)))
5784         (while articles
5785           (when (or (not (consp (setq article (pop articles))))
5786                     (< (car article) min)
5787                     (> (car article) max))
5788             (set var (delq article (symbol-value var))))))
5789        ;; Adjust ranges (sloppily).
5790        ((eq mark-type 'range)
5791         (cond
5792          ((eq mark 'seen)
5793           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5794           ;; It should be (seen (NUM1 . NUM2)).
5795           (when (numberp (cddr marks))
5796             (setcdr marks (list (cdr marks))))
5797           (setq articles (cdr marks))
5798           (while (and articles
5799                       (or (and (consp (car articles))
5800                                (> min (cdar articles)))
5801                           (and (numberp (car articles))
5802                                (> min (car articles)))))
5803             (pop articles))
5804           (set var articles))))))))
5805
5806 (defun gnus-update-missing-marks (missing)
5807   "Go through the list of MISSING articles and remove them from the mark lists."
5808   (when missing
5809     (let (var m)
5810       ;; Go through all types.
5811       (dolist (elem gnus-article-mark-lists)
5812         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5813           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5814           (when (symbol-value var)
5815             ;; This list has articles.  So we delete all missing
5816             ;; articles from it.
5817             (setq m missing)
5818             (while m
5819               (set var (delq (pop m) (symbol-value var))))))))))
5820
5821 (defun gnus-update-marks ()
5822   "Enter the various lists of marked articles into the newsgroup info list."
5823   (let ((types gnus-article-mark-lists)
5824         (info (gnus-get-info gnus-newsgroup-name))
5825         type list newmarked symbol delta-marks)
5826     (when info
5827       ;; Add all marks lists to the list of marks lists.
5828       (while (setq type (pop types))
5829         (setq list (symbol-value
5830                     (setq symbol
5831                           (intern (format "gnus-newsgroup-%s" (car type))))))
5832
5833         (when list
5834           ;; Get rid of the entries of the articles that have the
5835           ;; default score.
5836           (when (and (eq (cdr type) 'score)
5837                      gnus-save-score
5838                      list)
5839             (let* ((arts list)
5840                    (prev (cons nil list))
5841                    (all prev))
5842               (while arts
5843                 (if (or (not (consp (car arts)))
5844                         (= (cdar arts) gnus-summary-default-score))
5845                     (setcdr prev (cdr arts))
5846                   (setq prev arts))
5847                 (setq arts (cdr arts)))
5848               (setq list (cdr all)))))
5849
5850         (when (eq (cdr type) 'seen)
5851           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5852
5853         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5854           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5855
5856         (when (and (gnus-check-backend-function
5857                     'request-set-mark gnus-newsgroup-name)
5858                    (not (gnus-article-unpropagatable-p (cdr type))))
5859           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5860                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5861                  (add (gnus-remove-from-range
5862                        (gnus-copy-sequence list) old)))
5863             (when add
5864               (push (list add 'add (list (cdr type))) delta-marks))
5865             (when del
5866               (push (list del 'del (list (cdr type))) delta-marks))))
5867
5868         (when list
5869           (push (cons (cdr type) list) newmarked)))
5870
5871       (when delta-marks
5872         (unless (gnus-check-group gnus-newsgroup-name)
5873           (error "Can't open server for %s" gnus-newsgroup-name))
5874         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5875
5876       ;; Enter these new marks into the info of the group.
5877       (if (nthcdr 3 info)
5878           (setcar (nthcdr 3 info) newmarked)
5879         ;; Add the marks lists to the end of the info.
5880         (when newmarked
5881           (setcdr (nthcdr 2 info) (list newmarked))))
5882
5883       ;; Cut off the end of the info if there's nothing else there.
5884       (let ((i 5))
5885         (while (and (> i 2)
5886                     (not (nth i info)))
5887           (when (nthcdr (decf i) info)
5888             (setcdr (nthcdr i info) nil)))))))
5889
5890 (defun gnus-set-mode-line (where)
5891   "Set the mode line of the article or summary buffers.
5892 If WHERE is `summary', the summary mode line format will be used."
5893   ;; Is this mode line one we keep updated?
5894   (when (and (memq where gnus-updated-mode-lines)
5895              (symbol-value
5896               (intern (format "gnus-%s-mode-line-format-spec" where))))
5897     (let (mode-string)
5898       (save-excursion
5899         ;; We evaluate this in the summary buffer since these
5900         ;; variables are buffer-local to that buffer.
5901         (set-buffer gnus-summary-buffer)
5902        ;; We bind all these variables that are used in the `eval' form
5903         ;; below.
5904         (let* ((mformat (symbol-value
5905                          (intern
5906                           (format "gnus-%s-mode-line-format-spec" where))))
5907                (gnus-tmp-group-name (gnus-mode-string-quote
5908                                      (gnus-group-decoded-name
5909                                       gnus-newsgroup-name)))
5910                (gnus-tmp-article-number (or gnus-current-article 0))
5911                (gnus-tmp-unread gnus-newsgroup-unreads)
5912                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5913                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5914                (gnus-tmp-unread-and-unselected
5915                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5916                             (zerop gnus-tmp-unselected))
5917                        "")
5918                       ((zerop gnus-tmp-unselected)
5919                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5920                       (t (format "{%d(+%d) more}"
5921                                  gnus-tmp-unread-and-unticked
5922                                  gnus-tmp-unselected))))
5923                (gnus-tmp-subject
5924                 (if (and gnus-current-headers
5925                          (vectorp gnus-current-headers))
5926                     (gnus-mode-string-quote
5927                      (mail-header-subject gnus-current-headers))
5928                   ""))
5929                bufname-length max-len
5930                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5931           (setq mode-string (eval mformat))
5932           (setq bufname-length (if (string-match "%b" mode-string)
5933                                    (- (length
5934                                        (buffer-name
5935                                         (if (eq where 'summary)
5936                                             nil
5937                                           (get-buffer gnus-article-buffer))))
5938                                       2)
5939                                  0))
5940           (setq max-len (max 4 (if gnus-mode-non-string-length
5941                                    (- (window-width)
5942                                       gnus-mode-non-string-length
5943                                       bufname-length)
5944                                  (length mode-string))))
5945           ;; We might have to chop a bit of the string off...
5946           (when (> (length mode-string) max-len)
5947             (setq mode-string
5948                   (concat (truncate-string-to-width mode-string (- max-len 3))
5949                           "...")))
5950           ;; Pad the mode string a bit.
5951           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5952       ;; Update the mode line.
5953       (setq mode-line-buffer-identification
5954             (gnus-mode-line-buffer-identification (list mode-string)))
5955       (set-buffer-modified-p t))))
5956
5957 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5958   "Go through the HEADERS list and add all Xrefs to a hash table.
5959 The resulting hash table is returned, or nil if no Xrefs were found."
5960   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5961          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5962          (xref-hashtb (gnus-make-hashtable))
5963          start group entry number xrefs header)
5964     (while headers
5965       (setq header (pop headers))
5966       (when (and (setq xrefs (mail-header-xref header))
5967                  (not (memq (setq number (mail-header-number header))
5968                             unreads)))
5969         (setq start 0)
5970         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5971           (setq start (match-end 0))
5972           (setq group (if prefix
5973                           (concat prefix (substring xrefs (match-beginning 1)
5974                                                     (match-end 1)))
5975                         (substring xrefs (match-beginning 1) (match-end 1))))
5976           (setq number
5977                 (string-to-number (substring xrefs (match-beginning 2)
5978                                           (match-end 2))))
5979           (if (setq entry (gnus-gethash group xref-hashtb))
5980               (setcdr entry (cons number (cdr entry)))
5981             (gnus-sethash group (cons number nil) xref-hashtb)))))
5982     (and start xref-hashtb)))
5983
5984 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5985   "Look through all the headers and mark the Xrefs as read."
5986   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5987         name info xref-hashtb idlist method nth4)
5988     (save-excursion
5989       (set-buffer gnus-group-buffer)
5990       (when (setq xref-hashtb
5991                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5992         (mapatoms
5993          (lambda (group)
5994            (unless (string= from-newsgroup (setq name (symbol-name group)))
5995              (setq idlist (symbol-value group))
5996              ;; Dead groups are not updated.
5997              (and (prog1
5998                       (setq info (gnus-get-info name))
5999                     (when (stringp (setq nth4 (gnus-info-method info)))
6000                       (setq nth4 (gnus-server-to-method nth4))))
6001                   ;; Only do the xrefs if the group has the same
6002                   ;; select method as the group we have just read.
6003                   (or (gnus-methods-equal-p
6004                        nth4 (gnus-find-method-for-group from-newsgroup))
6005                       virtual
6006                       (equal nth4 (setq method (gnus-find-method-for-group
6007                                                 from-newsgroup)))
6008                       (and (equal (car nth4) (car method))
6009                            (equal (nth 1 nth4) (nth 1 method))))
6010                   gnus-use-cross-reference
6011                   (or (not (eq gnus-use-cross-reference t))
6012                       virtual
6013                       ;; Only do cross-references on subscribed
6014                       ;; groups, if that is what is wanted.
6015                       (<= (gnus-info-level info) gnus-level-subscribed))
6016                   (gnus-group-make-articles-read name idlist))))
6017          xref-hashtb)))))
6018
6019 (defun gnus-compute-read-articles (group articles)
6020   (let* ((entry (gnus-group-entry group))
6021          (info (nth 2 entry))
6022          (active (gnus-active group))
6023          ninfo)
6024     (when entry
6025       ;; First peel off all invalid article numbers.
6026       (when active
6027         (let ((ids articles)
6028               id first)
6029           (while (setq id (pop ids))
6030             (when (and first (> id (cdr active)))
6031               ;; We'll end up in this situation in one particular
6032               ;; obscure situation.  If you re-scan a group and get
6033               ;; a new article that is cross-posted to a different
6034               ;; group that has not been re-scanned, you might get
6035               ;; crossposted article that has a higher number than
6036               ;; Gnus believes possible.  So we re-activate this
6037               ;; group as well.  This might mean doing the
6038               ;; crossposting thingy will *increase* the number
6039               ;; of articles in some groups.  Tsk, tsk.
6040               (setq active (or (gnus-activate-group group) active)))
6041             (when (or (> id (cdr active))
6042                       (< id (car active)))
6043               (setq articles (delq id articles))))))
6044       ;; If the read list is nil, we init it.
6045       (if (and active
6046                (null (gnus-info-read info))
6047                (> (car active) 1))
6048           (setq ninfo (cons 1 (1- (car active))))
6049         (setq ninfo (gnus-info-read info)))
6050       ;; Then we add the read articles to the range.
6051       (gnus-add-to-range
6052        ninfo (setq articles (sort articles '<))))))
6053
6054 (defun gnus-group-make-articles-read (group articles)
6055   "Update the info of GROUP to say that ARTICLES are read."
6056   (let* ((num 0)
6057          (entry (gnus-group-entry group))
6058          (info (nth 2 entry))
6059          (active (gnus-active group))
6060          range)
6061     (when entry
6062       (setq range (gnus-compute-read-articles group articles))
6063       (with-current-buffer gnus-group-buffer
6064         (gnus-undo-register
6065           `(progn
6066              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6067              (gnus-info-set-read ',info ',(gnus-info-read info))
6068              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6069              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6070              (gnus-group-update-group ,group t))))
6071       ;; Add the read articles to the range.
6072       (gnus-info-set-read info range)
6073       (gnus-request-set-mark group (list (list range 'add '(read))))
6074       ;; Then we have to re-compute how many unread
6075       ;; articles there are in this group.
6076       (when active
6077         (cond
6078          ((not range)
6079           (setq num (- (1+ (cdr active)) (car active))))
6080          ((not (listp (cdr range)))
6081           (setq num (- (cdr active) (- (1+ (cdr range))
6082                                        (car range)))))
6083          (t
6084           (while range
6085             (if (numberp (car range))
6086                 (setq num (1+ num))
6087               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6088             (setq range (cdr range)))
6089           (setq num (- (cdr active) num))))
6090         ;; Update the number of unread articles.
6091         (setcar entry num)
6092         ;; Update the group buffer.
6093         (unless (gnus-ephemeral-group-p group)
6094           (gnus-group-update-group group t))))))
6095
6096 (defvar gnus-newsgroup-none-id 0)
6097
6098 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6099   (let ((cur nntp-server-buffer)
6100         (dependencies
6101          (or dependencies
6102              (with-current-buffer gnus-summary-buffer
6103                gnus-newsgroup-dependencies)))
6104         headers id end ref number
6105         (mail-parse-charset gnus-newsgroup-charset)
6106         (mail-parse-ignored-charsets
6107          (save-excursion (condition-case nil
6108                              (set-buffer gnus-summary-buffer)
6109                            (error))
6110                          gnus-newsgroup-ignored-charsets)))
6111     (save-excursion
6112       (set-buffer nntp-server-buffer)
6113       ;; Translate all TAB characters into SPACE characters.
6114       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6115       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6116       (ietf-drums-unfold-fws)
6117       (gnus-run-hooks 'gnus-parse-headers-hook)
6118       (let ((case-fold-search t)
6119             in-reply-to header p lines chars)
6120         (goto-char (point-min))
6121         ;; Search to the beginning of the next header.  Error messages
6122         ;; do not begin with 2 or 3.
6123         (while (re-search-forward "^[23][0-9]+ " nil t)
6124           (setq id nil
6125                 ref nil)
6126           ;; This implementation of this function, with nine
6127           ;; search-forwards instead of the one re-search-forward and
6128           ;; a case (which basically was the old function) is actually
6129           ;; about twice as fast, even though it looks messier.  You
6130           ;; can't have everything, I guess.  Speed and elegance
6131           ;; doesn't always go hand in hand.
6132           (setq
6133            header
6134            (vector
6135             ;; Number.
6136             (prog1
6137                 (setq number (read cur))
6138               (end-of-line)
6139               (setq p (point))
6140               (narrow-to-region (point)
6141                                 (or (and (search-forward "\n.\n" nil t)
6142                                          (- (point) 2))
6143                                     (point))))
6144             ;; Subject.
6145             (progn
6146               (goto-char p)
6147               (if (search-forward "\nsubject:" nil t)
6148                   (funcall gnus-decode-encoded-word-function
6149                            (nnheader-header-value))
6150                 "(none)"))
6151             ;; From.
6152             (progn
6153               (goto-char p)
6154               (if (search-forward "\nfrom:" nil t)
6155                   (funcall gnus-decode-encoded-address-function
6156                            (nnheader-header-value))
6157                 "(nobody)"))
6158             ;; Date.
6159             (progn
6160               (goto-char p)
6161               (if (search-forward "\ndate:" nil t)
6162                   (nnheader-header-value) ""))
6163             ;; Message-ID.
6164             (progn
6165               (goto-char p)
6166               (setq id (if (re-search-forward
6167                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6168                            ;; We do it this way to make sure the Message-ID
6169                            ;; is (somewhat) syntactically valid.
6170                            (buffer-substring (match-beginning 1)
6171                                              (match-end 1))
6172                          ;; If there was no message-id, we just fake one
6173                          ;; to make subsequent routines simpler.
6174                          (nnheader-generate-fake-message-id number))))
6175             ;; References.
6176             (progn
6177               (goto-char p)
6178               (if (search-forward "\nreferences:" nil t)
6179                   (progn
6180                     (setq end (point))
6181                     (prog1
6182                         (nnheader-header-value)
6183                       (setq ref
6184                             (buffer-substring
6185                              (progn
6186                                (end-of-line)
6187                                (search-backward ">" end t)
6188                                (1+ (point)))
6189                              (progn
6190                                (search-backward "<" end t)
6191                                (point))))))
6192                 ;; Get the references from the in-reply-to header if there
6193                 ;; were no references and the in-reply-to header looks
6194                 ;; promising.
6195                 (if (and (search-forward "\nin-reply-to:" nil t)
6196                          (setq in-reply-to (nnheader-header-value))
6197                          (string-match "<[^>]+>" in-reply-to))
6198                     (let (ref2)
6199                       (setq ref (substring in-reply-to (match-beginning 0)
6200                                            (match-end 0)))
6201                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6202                         (setq ref2 (substring in-reply-to (match-beginning 0)
6203                                               (match-end 0)))
6204                         (when (> (length ref2) (length ref))
6205                           (setq ref ref2)))
6206                       ref)
6207                   (setq ref nil))))
6208             ;; Chars.
6209             (progn
6210               (goto-char p)
6211               (if (search-forward "\nchars: " nil t)
6212                   (if (numberp (setq chars (ignore-errors (read cur))))
6213                       chars -1)
6214                 -1))
6215             ;; Lines.
6216             (progn
6217               (goto-char p)
6218               (if (search-forward "\nlines: " nil t)
6219                   (if (numberp (setq lines (ignore-errors (read cur))))
6220                       lines -1)
6221                 -1))
6222             ;; Xref.
6223             (progn
6224               (goto-char p)
6225               (and (search-forward "\nxref:" nil t)
6226                    (nnheader-header-value)))
6227             ;; Extra.
6228             (when gnus-extra-headers
6229               (let ((extra gnus-extra-headers)
6230                     out)
6231                 (while extra
6232                   (goto-char p)
6233                   (when (search-forward
6234                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6235                     (push (cons (car extra) (nnheader-header-value))
6236                           out))
6237                   (pop extra))
6238                 out))))
6239           (when (equal id ref)
6240             (setq ref nil))
6241
6242           (when gnus-alter-header-function
6243             (funcall gnus-alter-header-function header)
6244             (setq id (mail-header-id header)
6245                   ref (gnus-parent-id (mail-header-references header))))
6246
6247           (when (setq header
6248                       (gnus-dependencies-add-header
6249                        header dependencies force-new))
6250             (push header headers))
6251           (goto-char (point-max))
6252           (widen))
6253         (nreverse headers)))))
6254
6255 ;; Goes through the xover lines and returns a list of vectors
6256 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6257                                                   force-new dependencies
6258                                                   group also-fetch-heads)
6259   "Parse the news overview data in the server buffer.
6260 Return a list of headers that match SEQUENCE (see
6261 `nntp-retrieve-headers')."
6262   ;; Get the Xref when the users reads the articles since most/some
6263   ;; NNTP servers do not include Xrefs when using XOVER.
6264   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6265   (let ((mail-parse-charset gnus-newsgroup-charset)
6266         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6267         (cur nntp-server-buffer)
6268         (dependencies (or dependencies gnus-newsgroup-dependencies))
6269         (allp (cond
6270                ((eq gnus-read-all-available-headers t)
6271                 t)
6272                ((and (stringp gnus-read-all-available-headers)
6273                      group)
6274                 (string-match gnus-read-all-available-headers group))
6275                (t
6276                 nil)))
6277         number headers header)
6278     (save-excursion
6279       (set-buffer nntp-server-buffer)
6280       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6281       ;; Allow the user to mangle the headers before parsing them.
6282       (gnus-run-hooks 'gnus-parse-headers-hook)
6283       (goto-char (point-min))
6284       (gnus-parse-without-error
6285         (while (and (or sequence allp)
6286                     (not (eobp)))
6287           (setq number (read cur))
6288           (when (not allp)
6289             (while (and sequence
6290                         (< (car sequence) number))
6291               (setq sequence (cdr sequence))))
6292           (when (and (or allp
6293                          (and sequence
6294                               (eq number (car sequence))))
6295                      (progn
6296                        (setq sequence (cdr sequence))
6297                        (setq header (inline
6298                                       (gnus-nov-parse-line
6299                                        number dependencies force-new)))))
6300             (push header headers))
6301           (forward-line 1)))
6302       ;; A common bug in inn is that if you have posted an article and
6303       ;; then retrieves the active file, it will answer correctly --
6304       ;; the new article is included.  However, a NOV entry for the
6305       ;; article may not have been generated yet, so this may fail.
6306       ;; We work around this problem by retrieving the last few
6307       ;; headers using HEAD.
6308       (if (or (not also-fetch-heads)
6309               (not sequence))
6310           ;; We (probably) got all the headers.
6311           (nreverse headers)
6312         (let ((gnus-nov-is-evil t))
6313           (nconc
6314            (nreverse headers)
6315            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6316              (gnus-get-newsgroup-headers))))))))
6317
6318 (defun gnus-article-get-xrefs ()
6319   "Fill in the Xref value in `gnus-current-headers', if necessary.
6320 This is meant to be called in `gnus-article-internal-prepare-hook'."
6321   (let ((headers (with-current-buffer gnus-summary-buffer
6322                    gnus-current-headers)))
6323     (or (not gnus-use-cross-reference)
6324         (not headers)
6325         (and (mail-header-xref headers)
6326              (not (string= (mail-header-xref headers) "")))
6327         (let ((case-fold-search t)
6328               xref)
6329           (save-restriction
6330             (nnheader-narrow-to-headers)
6331             (goto-char (point-min))
6332             (when (or (and (not (eobp))
6333                            (eq (downcase (char-after)) ?x)
6334                            (looking-at "Xref:"))
6335                       (search-forward "\nXref:" nil t))
6336               (goto-char (1+ (match-end 0)))
6337               (setq xref (buffer-substring (point) (point-at-eol)))
6338               (mail-header-set-xref headers xref)))))))
6339
6340 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6341   "Find article ID and insert the summary line for that article.
6342 OLD-HEADER can either be a header or a line number to insert
6343 the subject line on."
6344   (let* ((line (and (numberp old-header) old-header))
6345          (old-header (and (vectorp old-header) old-header))
6346          (header (cond ((and old-header use-old-header)
6347                         old-header)
6348                        ((and (numberp id)
6349                              (gnus-number-to-header id))
6350                         (gnus-number-to-header id))
6351                        (t
6352                         (gnus-read-header id))))
6353          (number (and (numberp id) id))
6354          d)
6355     (when header
6356       ;; Rebuild the thread that this article is part of and go to the
6357       ;; article we have fetched.
6358       (when (and (not gnus-show-threads)
6359                  old-header)
6360         (when (and number
6361                    (setq d (gnus-data-find (mail-header-number old-header))))
6362           (goto-char (gnus-data-pos d))
6363           (gnus-data-remove
6364            number
6365            (- (point-at-bol)
6366               (prog1
6367                   (1+ (point-at-eol))
6368                 (gnus-delete-line))))))
6369       ;; Remove list identifiers from subject.
6370       (when gnus-list-identifiers
6371         (let ((gnus-newsgroup-headers (list header)))
6372           (gnus-summary-remove-list-identifiers)))
6373       (when old-header
6374         (mail-header-set-number header (mail-header-number old-header)))
6375       (setq gnus-newsgroup-sparse
6376             (delq (setq number (mail-header-number header))
6377                   gnus-newsgroup-sparse))
6378       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6379       (push number gnus-newsgroup-limit)
6380       (gnus-rebuild-thread (mail-header-id header) line)
6381       (gnus-summary-goto-subject number nil t))
6382     (when (and (numberp number)
6383                (> number 0))
6384       ;; We have to update the boundaries even if we can't fetch the
6385       ;; article if ID is a number -- so that the next `P' or `N'
6386       ;; command will fetch the previous (or next) article even
6387       ;; if the one we tried to fetch this time has been canceled.
6388       (when (> number gnus-newsgroup-end)
6389         (setq gnus-newsgroup-end number))
6390       (when (< number gnus-newsgroup-begin)
6391         (setq gnus-newsgroup-begin number))
6392       (setq gnus-newsgroup-unselected
6393             (delq number gnus-newsgroup-unselected)))
6394     ;; Report back a success?
6395     (and header (mail-header-number header))))
6396
6397 ;;; Process/prefix in the summary buffer
6398
6399 (defun gnus-summary-work-articles (n)
6400   "Return a list of articles to be worked upon.
6401 The prefix argument, the list of process marked articles, and the
6402 current article will be taken into consideration."
6403   (save-excursion
6404     (set-buffer gnus-summary-buffer)
6405     (cond
6406      (n
6407       ;; A numerical prefix has been given.
6408       (setq n (prefix-numeric-value n))
6409       (let ((backward (< n 0))
6410             (n (abs (prefix-numeric-value n)))
6411             articles article)
6412         (save-excursion
6413           (while
6414               (and (> n 0)
6415                    (push (setq article (gnus-summary-article-number))
6416                          articles)
6417                    (if backward
6418                        (gnus-summary-find-prev nil article)
6419                      (gnus-summary-find-next nil article)))
6420             (decf n)))
6421         (nreverse articles)))
6422      ((and (gnus-region-active-p) (mark))
6423       (message "region active")
6424       ;; Work on the region between point and mark.
6425       (let ((max (max (point) (mark)))
6426             articles article)
6427         (save-excursion
6428           (goto-char (min (point) (mark)))
6429           (while
6430               (and
6431                (push (setq article (gnus-summary-article-number)) articles)
6432                (gnus-summary-find-next nil article)
6433                (< (point) max)))
6434           (nreverse articles))))
6435      (gnus-newsgroup-processable
6436       ;; There are process-marked articles present.
6437       ;; Save current state.
6438       (gnus-summary-save-process-mark)
6439       ;; Return the list.
6440       (reverse gnus-newsgroup-processable))
6441      (t
6442       ;; Just return the current article.
6443       (list (gnus-summary-article-number))))))
6444
6445 (defmacro gnus-summary-iterate (arg &rest forms)
6446   "Iterate over the process/prefixed articles and do FORMS.
6447 ARG is the interactive prefix given to the command.  FORMS will be
6448 executed with point over the summary line of the articles."
6449   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6450     `(let ((,articles (gnus-summary-work-articles ,arg)))
6451        (while ,articles
6452          (gnus-summary-goto-subject (car ,articles))
6453          ,@forms
6454          (pop ,articles)))))
6455
6456 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6457 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6458
6459 (defun gnus-summary-save-process-mark ()
6460   "Push the current set of process marked articles on the stack."
6461   (interactive)
6462   (push (copy-sequence gnus-newsgroup-processable)
6463         gnus-newsgroup-process-stack))
6464
6465 (defun gnus-summary-kill-process-mark ()
6466   "Push the current set of process marked articles on the stack and unmark."
6467   (interactive)
6468   (gnus-summary-save-process-mark)
6469   (gnus-summary-unmark-all-processable))
6470
6471 (defun gnus-summary-yank-process-mark ()
6472   "Pop the last process mark state off the stack and restore it."
6473   (interactive)
6474   (unless gnus-newsgroup-process-stack
6475     (error "Empty mark stack"))
6476   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6477
6478 (defun gnus-summary-process-mark-set (set)
6479   "Make SET into the current process marked articles."
6480   (gnus-summary-unmark-all-processable)
6481   (mapc 'gnus-summary-set-process-mark set))
6482
6483 ;;; Searching and stuff
6484
6485 (defun gnus-summary-search-group (&optional backward use-level)
6486   "Search for next unread newsgroup.
6487 If optional argument BACKWARD is non-nil, search backward instead."
6488   (save-excursion
6489     (set-buffer gnus-group-buffer)
6490     (when (gnus-group-search-forward
6491            backward nil (if use-level (gnus-group-group-level) nil))
6492       (gnus-group-group-name))))
6493
6494 (defun gnus-summary-best-group (&optional exclude-group)
6495   "Find the name of the best unread group.
6496 If EXCLUDE-GROUP, do not go to this group."
6497   (with-current-buffer gnus-group-buffer
6498     (save-excursion
6499       (gnus-group-best-unread-group exclude-group))))
6500
6501 (defun gnus-summary-find-next (&optional unread article backward)
6502   (if backward
6503       (gnus-summary-find-prev unread article)
6504     (let* ((dummy (gnus-summary-article-intangible-p))
6505            (article (or article (gnus-summary-article-number)))
6506            (data (gnus-data-find-list article))
6507            result)
6508       (when (and (not dummy)
6509                  (or (not gnus-summary-check-current)
6510                      (not unread)
6511                      (not (gnus-data-unread-p (car data)))))
6512         (setq data (cdr data)))
6513       (when (setq result
6514                   (if unread
6515                       (progn
6516                         (while data
6517                           (unless (memq (gnus-data-number (car data))
6518                                         (cond
6519                                          ((eq gnus-auto-goto-ignores
6520                                               'always-undownloaded)
6521                                           gnus-newsgroup-undownloaded)
6522                                          (gnus-plugged
6523                                           nil)
6524                                          ((eq gnus-auto-goto-ignores
6525                                               'unfetched)
6526                                           gnus-newsgroup-unfetched)
6527                                          ((eq gnus-auto-goto-ignores
6528                                               'undownloaded)
6529                                           gnus-newsgroup-undownloaded)))
6530                             (when (gnus-data-unread-p (car data))
6531                               (setq result (car data)
6532                                     data nil)))
6533                           (setq data (cdr data)))
6534                         result)
6535                     (car data)))
6536         (goto-char (gnus-data-pos result))
6537         (gnus-data-number result)))))
6538
6539 (defun gnus-summary-find-prev (&optional unread article)
6540   (let* ((eobp (eobp))
6541          (article (or article (gnus-summary-article-number)))
6542          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6543          result)
6544     (when (and (not eobp)
6545                (or (not gnus-summary-check-current)
6546                    (not unread)
6547                    (not (gnus-data-unread-p (car data)))))
6548       (setq data (cdr data)))
6549     (when (setq result
6550                 (if unread
6551                     (progn
6552                       (while data
6553                         (unless (memq (gnus-data-number (car data))
6554                                       (cond
6555                                        ((eq gnus-auto-goto-ignores
6556                                             'always-undownloaded)
6557                                         gnus-newsgroup-undownloaded)
6558                                        (gnus-plugged
6559                                         nil)
6560                                        ((eq gnus-auto-goto-ignores
6561                                             'unfetched)
6562                                         gnus-newsgroup-unfetched)
6563                                        ((eq gnus-auto-goto-ignores
6564                                             'undownloaded)
6565                                         gnus-newsgroup-undownloaded)))
6566                           (when (gnus-data-unread-p (car data))
6567                             (setq result (car data)
6568                                   data nil)))
6569                         (setq data (cdr data)))
6570                       result)
6571                   (car data)))
6572       (goto-char (gnus-data-pos result))
6573       (gnus-data-number result))))
6574
6575 (defun gnus-summary-find-subject (subject &optional unread backward article)
6576   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6577          (article (or article (gnus-summary-article-number)))
6578          (articles (gnus-data-list backward))
6579          (arts (gnus-data-find-list article articles))
6580          result)
6581     (when (or (not gnus-summary-check-current)
6582               (not unread)
6583               (not (gnus-data-unread-p (car arts))))
6584       (setq arts (cdr arts)))
6585     (while arts
6586       (and (or (not unread)
6587                (gnus-data-unread-p (car arts)))
6588            (vectorp (gnus-data-header (car arts)))
6589            (gnus-subject-equal
6590             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6591            (setq result (car arts)
6592                  arts nil))
6593       (setq arts (cdr arts)))
6594     (and result
6595          (goto-char (gnus-data-pos result))
6596          (gnus-data-number result))))
6597
6598 (defun gnus-summary-search-forward (&optional unread subject backward)
6599   "Search forward for an article.
6600 If UNREAD, look for unread articles.  If SUBJECT, look for
6601 articles with that subject.  If BACKWARD, search backward instead."
6602   (cond (subject (gnus-summary-find-subject subject unread backward))
6603         (backward (gnus-summary-find-prev unread))
6604         (t (gnus-summary-find-next unread))))
6605
6606 (defun gnus-recenter (&optional n)
6607   "Center point in window and redisplay frame.
6608 Also do horizontal recentering."
6609   (interactive "P")
6610   (when (and gnus-auto-center-summary
6611              (not (eq gnus-auto-center-summary 'vertical)))
6612     (gnus-horizontal-recenter))
6613   (recenter n))
6614
6615 (defun gnus-summary-recenter ()
6616   "Center point in the summary window.
6617 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6618 displayed, no centering will be performed."
6619   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6620   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6621   (interactive)
6622   ;; The user has to want it.
6623   (when gnus-auto-center-summary
6624     (let* ((top (cond ((< (window-height) 4) 0)
6625                       ((< (window-height) 7) 1)
6626                       (t (if (numberp gnus-auto-center-summary)
6627                              gnus-auto-center-summary
6628                            (/ (1- (window-height)) 2)))))
6629            (height (1- (window-height)))
6630            (bottom (save-excursion (goto-char (point-max))
6631                                    (forward-line (- height))
6632                                    (point)))
6633            (window (get-buffer-window (current-buffer))))
6634       (when (get-buffer-window gnus-article-buffer)
6635         ;; Only do recentering when the article buffer is displayed,
6636         ;; Set the window start to either `bottom', which is the biggest
6637         ;; possible valid number, or the second line from the top,
6638         ;; whichever is the least.
6639         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6640           (if (> bottom top-pos)
6641               ;; Keep the second line from the top visible
6642               (set-window-start window top-pos)
6643             ;; Try to keep the bottom line visible; if it's partially
6644             ;; obscured, either scroll one more line to make it fully
6645             ;; visible, or revert to using TOP-POS.
6646             (save-excursion
6647               (goto-char (point-max))
6648               (forward-line -1)
6649               (let ((last-line-start (point)))
6650                 (goto-char bottom)
6651                 (set-window-start window (point) t)
6652                 (when (not (pos-visible-in-window-p last-line-start window))
6653                   (forward-line 1)
6654                   (set-window-start window (min (point) top-pos) t)))))))
6655       ;; Do horizontal recentering while we're at it.
6656       (when (and (get-buffer-window (current-buffer) t)
6657                  (not (eq gnus-auto-center-summary 'vertical)))
6658         (let ((selected (selected-window)))
6659           (select-window (get-buffer-window (current-buffer) t))
6660           (gnus-summary-position-point)
6661           (gnus-horizontal-recenter)
6662           (select-window selected))))))
6663
6664 (defun gnus-summary-jump-to-group (newsgroup)
6665   "Move point to NEWSGROUP in group mode buffer."
6666   ;; Keep update point of group mode buffer if visible.
6667   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6668       (save-window-excursion
6669         ;; Take care of tree window mode.
6670         (when (get-buffer-window gnus-group-buffer)
6671           (pop-to-buffer gnus-group-buffer))
6672         (gnus-group-jump-to-group newsgroup))
6673     (save-excursion
6674       ;; Take care of tree window mode.
6675       (if (get-buffer-window gnus-group-buffer)
6676           (pop-to-buffer gnus-group-buffer)
6677         (set-buffer gnus-group-buffer))
6678       (gnus-group-jump-to-group newsgroup))))
6679
6680 ;; This function returns a list of article numbers based on the
6681 ;; difference between the ranges of read articles in this group and
6682 ;; the range of active articles.
6683 (defun gnus-list-of-unread-articles (group)
6684   (let* ((read (gnus-info-read (gnus-get-info group)))
6685          (active (or (gnus-active group) (gnus-activate-group group)))
6686          (last (or (cdr active)
6687                    (error "Group %s couldn't be activated " group)))
6688          first nlast unread)
6689     ;; If none are read, then all are unread.
6690     (if (not read)
6691         (setq first (car active))
6692       ;; If the range of read articles is a single range, then the
6693       ;; first unread article is the article after the last read
6694       ;; article.  Sounds logical, doesn't it?
6695       (if (and (not (listp (cdr read)))
6696                (or (< (car read) (car active))
6697                    (progn (setq read (list read))
6698                           nil)))
6699           (setq first (max (car active) (1+ (cdr read))))
6700         ;; `read' is a list of ranges.
6701         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6702                                   (caar read)))
6703                   1)
6704           (setq first (car active)))
6705         (while read
6706           (when first
6707             (while (< first nlast)
6708               (setq unread (cons first unread)
6709                     first (1+ first))))
6710           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6711           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6712           (setq read (cdr read)))))
6713     ;; And add the last unread articles.
6714     (while (<= first last)
6715       (setq unread (cons first unread)
6716             first (1+ first)))
6717     ;; Return the list of unread articles.
6718     (delq 0 (nreverse unread))))
6719
6720 (defun gnus-list-of-read-articles (group)
6721   "Return a list of unread, unticked and non-dormant articles."
6722   (let* ((info (gnus-get-info group))
6723          (marked (gnus-info-marks info))
6724          (active (gnus-active group)))
6725     (and info active
6726          (gnus-list-range-difference
6727           (gnus-list-range-difference
6728            (gnus-sorted-complement
6729             (gnus-uncompress-range active)
6730             (gnus-list-of-unread-articles group))
6731            (cdr (assq 'dormant marked)))
6732           (cdr (assq 'tick marked))))))
6733
6734 ;; This function returns a sequence of article numbers based on the
6735 ;; difference between the ranges of read articles in this group and
6736 ;; the range of active articles.
6737 (defun gnus-sequence-of-unread-articles (group)
6738   (let* ((read (gnus-info-read (gnus-get-info group)))
6739          (active (or (gnus-active group) (gnus-activate-group group)))
6740          (last (cdr active))
6741          first nlast unread)
6742     ;; If none are read, then all are unread.
6743     (if (not read)
6744         (setq first (car active))
6745       ;; If the range of read articles is a single range, then the
6746       ;; first unread article is the article after the last read
6747       ;; article.  Sounds logical, doesn't it?
6748       (if (and (not (listp (cdr read)))
6749                (or (< (car read) (car active))
6750                    (progn (setq read (list read))
6751                           nil)))
6752           (setq first (max (car active) (1+ (cdr read))))
6753         ;; `read' is a list of ranges.
6754         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6755                                   (caar read)))
6756                   1)
6757           (setq first (car active)))
6758         (while read
6759           (when first
6760             (push (cons first nlast) unread))
6761           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6762           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6763           (setq read (cdr read)))))
6764     ;; And add the last unread articles.
6765     (cond ((not (and first last))
6766            nil)
6767           ((< first last)
6768            (push (cons first last) unread))
6769           ((= first last)
6770            (push first unread)))
6771     ;; Return the sequence of unread articles.
6772     (delq 0 (nreverse unread))))
6773
6774 ;; Various summary commands
6775
6776 (defun gnus-summary-select-article-buffer ()
6777   "Reconfigure windows to show article buffer."
6778   (interactive)
6779   (if (not (gnus-buffer-live-p gnus-article-buffer))
6780       (error "There is no article buffer for this summary buffer")
6781     (gnus-configure-windows 'article)
6782     (select-window (get-buffer-window gnus-article-buffer))))
6783
6784 (defun gnus-summary-universal-argument (arg)
6785   "Perform any operation on all articles that are process/prefixed."
6786   (interactive "P")
6787   (let ((articles (gnus-summary-work-articles arg))
6788         func article)
6789     (if (eq
6790          (setq
6791           func
6792           (key-binding
6793            (read-key-sequence
6794             (substitute-command-keys
6795              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6796          'undefined)
6797         (gnus-error 1 "Undefined key")
6798       (save-excursion
6799         (while articles
6800           (gnus-summary-goto-subject (setq article (pop articles)))
6801           (let (gnus-newsgroup-processable)
6802             (command-execute func))
6803           (gnus-summary-remove-process-mark article)))))
6804   (gnus-summary-position-point))
6805
6806 (defun gnus-summary-toggle-truncation (&optional arg)
6807   "Toggle truncation of summary lines.
6808 With ARG, turn line truncation on if ARG is positive."
6809   (interactive "P")
6810   (setq truncate-lines
6811         (if (null arg) (not truncate-lines)
6812           (> (prefix-numeric-value arg) 0)))
6813   (redraw-display))
6814
6815 (defun gnus-summary-find-for-reselect ()
6816   "Return the number of an article to stay on across a reselect.
6817 The current article is considered, then following articles, then previous
6818 articles.  An article is sought which is not cancelled and isn't a temporary
6819 insertion from another group.  If there's no such then return a dummy 0."
6820   (let (found)
6821     (dolist (rev '(nil t))
6822       (unless found      ; don't demand the reverse list if we don't need it
6823         (let ((data (gnus-data-find-list
6824                      (gnus-summary-article-number) (gnus-data-list rev))))
6825           (while (and data (not found))
6826             (if (and (< 0 (gnus-data-number (car data)))
6827                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6828                 (setq found (gnus-data-number (car data))))
6829             (setq data (cdr data))))))
6830     (or found 0)))
6831
6832 (defun gnus-summary-reselect-current-group (&optional all rescan)
6833   "Exit and then reselect the current newsgroup.
6834 The prefix argument ALL means to select all articles."
6835   (interactive "P")
6836   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6837     (error "Ephemeral groups can't be reselected"))
6838   (let ((current-subject (gnus-summary-find-for-reselect))
6839         (group gnus-newsgroup-name))
6840     (setq gnus-newsgroup-begin nil)
6841     (gnus-summary-exit nil 'leave-hidden)
6842     ;; We have to adjust the point of group mode buffer because
6843     ;; point was moved to the next unread newsgroup by exiting.
6844     (gnus-summary-jump-to-group group)
6845     (when rescan
6846       (save-excursion
6847         (gnus-group-get-new-news-this-group 1)))
6848     (gnus-group-read-group all t)
6849     (gnus-summary-goto-subject current-subject nil t)))
6850
6851 (defun gnus-summary-rescan-group (&optional all)
6852   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6853   (interactive "P")
6854   (gnus-summary-reselect-current-group all t))
6855
6856 (defun gnus-summary-update-info (&optional non-destructive)
6857   (save-excursion
6858     (let ((group gnus-newsgroup-name))
6859       (when group
6860         (when gnus-newsgroup-kill-headers
6861           (setq gnus-newsgroup-killed
6862                 (gnus-compress-sequence
6863                  (gnus-sorted-union
6864                   (gnus-list-range-intersection
6865                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6866                   gnus-newsgroup-unreads)
6867                  t)))
6868         (unless (listp (cdr gnus-newsgroup-killed))
6869           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6870         (let ((headers gnus-newsgroup-headers))
6871           ;; Set the new ranges of read articles.
6872           (with-current-buffer gnus-group-buffer
6873             (gnus-undo-force-boundary))
6874           (gnus-update-read-articles
6875            group (gnus-sorted-union
6876                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6877           ;; Set the current article marks.
6878           (let ((gnus-newsgroup-scored
6879                  (if (and (not gnus-save-score)
6880                           (not non-destructive))
6881                      nil
6882                    gnus-newsgroup-scored)))
6883             (save-excursion
6884               (gnus-update-marks)))
6885           ;; Do the cross-ref thing.
6886           (when gnus-use-cross-reference
6887             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6888           ;; Do not switch windows but change the buffer to work.
6889           (set-buffer gnus-group-buffer)
6890           (unless (gnus-ephemeral-group-p group)
6891             (gnus-group-update-group group)))))))
6892
6893 (defun gnus-summary-save-newsrc (&optional force)
6894   "Save the current number of read/marked articles in the dribble buffer.
6895 The dribble buffer will then be saved.
6896 If FORCE (the prefix), also save the .newsrc file(s)."
6897   (interactive "P")
6898   (gnus-summary-update-info t)
6899   (if force
6900       (gnus-save-newsrc-file)
6901     (gnus-dribble-save)))
6902
6903 (defun gnus-summary-exit (&optional temporary leave-hidden)
6904   "Exit reading current newsgroup, and then return to group selection mode.
6905 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6906   (interactive)
6907   (gnus-set-global-variables)
6908   (when (gnus-buffer-live-p gnus-article-buffer)
6909     (save-excursion
6910       (set-buffer gnus-article-buffer)
6911       (mm-destroy-parts gnus-article-mime-handles)
6912       ;; Set it to nil for safety reason.
6913       (setq gnus-article-mime-handle-alist nil)
6914       (setq gnus-article-mime-handles nil)))
6915   (gnus-kill-save-kill-buffer)
6916   (gnus-async-halt-prefetch)
6917   (let* ((group gnus-newsgroup-name)
6918          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6919          (gnus-group-is-exiting-p t)
6920          (mode major-mode)
6921          (group-point nil)
6922          (buf (current-buffer)))
6923     (unless quit-config
6924       ;; Do adaptive scoring, and possibly save score files.
6925       (when gnus-newsgroup-adaptive
6926         (gnus-score-adaptive))
6927       (when gnus-use-scoring
6928         (gnus-score-save)))
6929     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6930     ;; If we have several article buffers, we kill them at exit.
6931     (unless gnus-single-article-buffer
6932       (gnus-kill-buffer gnus-article-buffer)
6933       (gnus-kill-buffer gnus-original-article-buffer)
6934       (setq gnus-article-current nil))
6935     (when gnus-use-cache
6936       (gnus-cache-possibly-remove-articles)
6937       (gnus-cache-save-buffers))
6938     (gnus-async-prefetch-remove-group group)
6939     (when gnus-suppress-duplicates
6940       (gnus-dup-enter-articles))
6941     (when gnus-use-trees
6942       (gnus-tree-close group))
6943     (when gnus-use-cache
6944       (gnus-cache-write-active))
6945     ;; Remove entries for this group.
6946     (nnmail-purge-split-history (gnus-group-real-name group))
6947     ;; Make all changes in this group permanent.
6948     (unless quit-config
6949       (gnus-run-hooks 'gnus-exit-group-hook)
6950       (gnus-summary-update-info))
6951     (gnus-close-group group)
6952     ;; Make sure where we were, and go to next newsgroup.
6953     (set-buffer gnus-group-buffer)
6954     (unless quit-config
6955       (gnus-group-jump-to-group group))
6956     (gnus-run-hooks 'gnus-summary-exit-hook)
6957     (unless (or quit-config
6958                 ;; If this group has disappeared from the summary
6959                 ;; buffer, don't skip forwards.
6960                 (not (string= group (gnus-group-group-name))))
6961       (gnus-group-next-unread-group 1))
6962     (setq group-point (point))
6963     (if temporary
6964         nil                             ;Nothing to do.
6965       ;; If we have several article buffers, we kill them at exit.
6966       (unless gnus-single-article-buffer
6967         (gnus-kill-buffer gnus-article-buffer)
6968         (gnus-kill-buffer gnus-original-article-buffer)
6969         (setq gnus-article-current nil))
6970       (set-buffer buf)
6971       (if (not gnus-kill-summary-on-exit)
6972           (progn
6973             (gnus-deaden-summary)
6974             (setq mode nil))
6975         ;; We set all buffer-local variables to nil.  It is unclear why
6976         ;; this is needed, but if we don't, buffer-local variables are
6977         ;; not garbage-collected, it seems.  This would the lead to en
6978         ;; ever-growing Emacs.
6979         (gnus-summary-clear-local-variables)
6980         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6981           (gnus-summary-clear-local-variables))
6982         (when (get-buffer gnus-article-buffer)
6983           (bury-buffer gnus-article-buffer))
6984         ;; Return to group mode buffer.
6985         (when (eq mode 'gnus-summary-mode)
6986           (gnus-kill-buffer buf)))
6987       (setq gnus-current-select-method gnus-select-method)
6988       (set-buffer gnus-group-buffer)
6989       (if quit-config
6990           (gnus-handle-ephemeral-exit quit-config)
6991         (goto-char group-point)
6992         ;; If gnus-group-buffer is already displayed, make sure we also move
6993         ;; the cursor in the window that displays it.
6994         (let ((win (get-buffer-window (current-buffer) 0)))
6995           (if win (set-window-point win (point))))
6996         (unless leave-hidden
6997           (gnus-configure-windows 'group 'force)))
6998       ;; Clear the current group name.
6999       (unless quit-config
7000         (setq gnus-newsgroup-name nil)))))
7001
7002 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7003 (defun gnus-summary-exit-no-update (&optional no-questions)
7004   "Quit reading current newsgroup without updating read article info."
7005   (interactive)
7006   (let* ((group gnus-newsgroup-name)
7007          (gnus-group-is-exiting-p t)
7008          (gnus-group-is-exiting-without-update-p t)
7009          (quit-config (gnus-group-quit-config group)))
7010     (when (or no-questions
7011               gnus-expert-user
7012               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7013       (gnus-async-halt-prefetch)
7014       (run-hooks 'gnus-summary-prepare-exit-hook)
7015       (when (gnus-buffer-live-p gnus-article-buffer)
7016         (save-excursion
7017           (set-buffer gnus-article-buffer)
7018           (mm-destroy-parts gnus-article-mime-handles)
7019           ;; Set it to nil for safety reason.
7020           (setq gnus-article-mime-handle-alist nil)
7021           (setq gnus-article-mime-handles nil)))
7022       ;; If we have several article buffers, we kill them at exit.
7023       (unless gnus-single-article-buffer
7024         (gnus-kill-buffer gnus-article-buffer)
7025         (gnus-kill-buffer gnus-original-article-buffer)
7026         (setq gnus-article-current nil))
7027       (if (not gnus-kill-summary-on-exit)
7028           (gnus-deaden-summary)
7029         (gnus-close-group group)
7030         (gnus-summary-clear-local-variables)
7031         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7032           (gnus-summary-clear-local-variables))
7033         (gnus-kill-buffer gnus-summary-buffer))
7034       (unless gnus-single-article-buffer
7035         (setq gnus-article-current nil))
7036       (when gnus-use-trees
7037         (gnus-tree-close group))
7038       (gnus-async-prefetch-remove-group group)
7039       (when (get-buffer gnus-article-buffer)
7040         (bury-buffer gnus-article-buffer))
7041       ;; Return to the group buffer.
7042       (gnus-configure-windows 'group 'force)
7043       ;; Clear the current group name.
7044       (setq gnus-newsgroup-name nil)
7045       (unless (gnus-ephemeral-group-p group)
7046         (gnus-group-update-group group))
7047       (when (equal (gnus-group-group-name) group)
7048         (gnus-group-next-unread-group 1))
7049       (when quit-config
7050         (gnus-handle-ephemeral-exit quit-config)))))
7051
7052 (defun gnus-handle-ephemeral-exit (quit-config)
7053   "Handle movement when leaving an ephemeral group.
7054 The state which existed when entering the ephemeral is reset."
7055   (if (not (buffer-name (car quit-config)))
7056       (gnus-configure-windows 'group 'force)
7057     (set-buffer (car quit-config))
7058     (cond ((eq major-mode 'gnus-summary-mode)
7059            (gnus-set-global-variables))
7060           ((eq major-mode 'gnus-article-mode)
7061            (save-excursion
7062              ;; The `gnus-summary-buffer' variable may point
7063              ;; to the old summary buffer when using a single
7064              ;; article buffer.
7065              (unless (gnus-buffer-live-p gnus-summary-buffer)
7066                (set-buffer gnus-group-buffer))
7067              (set-buffer gnus-summary-buffer)
7068              (gnus-set-global-variables))))
7069     (if (or (eq (cdr quit-config) 'article)
7070             (eq (cdr quit-config) 'pick))
7071         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7072             (gnus-configure-windows 'pick 'force)
7073           (gnus-configure-windows (cdr quit-config) 'force))
7074       (gnus-configure-windows (cdr quit-config) 'force))
7075     (when (eq major-mode 'gnus-summary-mode)
7076       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7077                                                      next-unread-noselect))
7078           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7079                                   'next-noselect)
7080                               (gnus-summary-next-subject 1 nil t))
7081                              ((eq gnus-auto-select-on-ephemeral-exit
7082                                   'next-unread-noselect)
7083                               (gnus-summary-next-subject 1 t t))))
7084             ;; Hide the article buffer which displays the article different
7085             ;; from the one that the cursor points to in the summary buffer.
7086             (gnus-configure-windows 'summary 'force))
7087         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7088                (gnus-summary-next-subject 1))
7089               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7090                (gnus-summary-next-subject 1 t))))
7091       (gnus-summary-recenter)
7092       (gnus-summary-position-point))))
7093
7094 ;;; Dead summaries.
7095
7096 (defvar gnus-dead-summary-mode-map nil)
7097
7098 (unless gnus-dead-summary-mode-map
7099   (setq gnus-dead-summary-mode-map (make-keymap))
7100   (suppress-keymap gnus-dead-summary-mode-map)
7101   (substitute-key-definition
7102    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7103   (dolist (key '("\C-d" "\r" "\177" [delete]))
7104     (define-key gnus-dead-summary-mode-map
7105       key 'gnus-summary-wake-up-the-dead))
7106   (dolist (key '("q" "Q"))
7107     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7108
7109 (defvar gnus-dead-summary-mode nil
7110   "Minor mode for Gnus summary buffers.")
7111
7112 (defun gnus-dead-summary-mode (&optional arg)
7113   "Minor mode for Gnus summary buffers."
7114   (interactive "P")
7115   (when (eq major-mode 'gnus-summary-mode)
7116     (make-local-variable 'gnus-dead-summary-mode)
7117     (setq gnus-dead-summary-mode
7118           (if (null arg) (not gnus-dead-summary-mode)
7119             (> (prefix-numeric-value arg) 0)))
7120     (when gnus-dead-summary-mode
7121       (add-minor-mode
7122        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7123
7124 (defun gnus-deaden-summary ()
7125   "Make the current summary buffer into a dead summary buffer."
7126   ;; Kill any previous dead summary buffer.
7127   (when (and gnus-dead-summary
7128              (buffer-name gnus-dead-summary))
7129     (with-current-buffer gnus-dead-summary
7130       (when gnus-dead-summary-mode
7131         (kill-buffer (current-buffer)))))
7132   ;; Make this the current dead summary.
7133   (setq gnus-dead-summary (current-buffer))
7134   (gnus-dead-summary-mode 1)
7135   (let ((name (buffer-name)))
7136     (when (string-match "Summary" name)
7137       (rename-buffer
7138        (concat (substring name 0 (match-beginning 0)) "Dead "
7139                (substring name (match-beginning 0)))
7140        t)
7141       (bury-buffer))))
7142
7143 (defun gnus-kill-or-deaden-summary (buffer)
7144   "Kill or deaden the summary BUFFER."
7145   (save-excursion
7146     (when (and (buffer-name buffer)
7147                (not gnus-single-article-buffer))
7148       (with-current-buffer buffer
7149         (gnus-kill-buffer gnus-article-buffer)
7150         (gnus-kill-buffer gnus-original-article-buffer)))
7151     (cond
7152      ;; Kill the buffer.
7153      (gnus-kill-summary-on-exit
7154       (when (and gnus-use-trees
7155                  (gnus-buffer-exists-p buffer))
7156         (save-excursion
7157           (set-buffer buffer)
7158           (gnus-tree-close gnus-newsgroup-name)))
7159       (gnus-kill-buffer buffer))
7160      ;; Deaden the buffer.
7161      ((gnus-buffer-exists-p buffer)
7162       (save-excursion
7163         (set-buffer buffer)
7164         (gnus-deaden-summary))))))
7165
7166 (defun gnus-summary-wake-up-the-dead (&rest args)
7167   "Wake up the dead summary buffer."
7168   (interactive)
7169   (gnus-dead-summary-mode -1)
7170   (let ((name (buffer-name)))
7171     (when (string-match "Dead " name)
7172       (rename-buffer
7173        (concat (substring name 0 (match-beginning 0))
7174                (substring name (match-end 0)))
7175        t)))
7176   (gnus-message 3 "This dead summary is now alive again"))
7177
7178 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7179 (defun gnus-summary-fetch-faq (&optional faq-dir)
7180   "Fetch the FAQ for the current group.
7181 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7182 in."
7183   (interactive
7184    (list
7185     (when current-prefix-arg
7186       (completing-read
7187        "FAQ dir: " (and (listp gnus-group-faq-directory)
7188                         (mapcar 'list
7189                                 gnus-group-faq-directory))))))
7190   (let (gnus-faq-buffer)
7191     (when (setq gnus-faq-buffer
7192                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7193       (gnus-configure-windows 'summary-faq))))
7194
7195 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7196 (defun gnus-summary-describe-group (&optional force)
7197   "Describe the current newsgroup."
7198   (interactive "P")
7199   (gnus-group-describe-group force gnus-newsgroup-name))
7200
7201 (defun gnus-summary-describe-briefly ()
7202   "Describe summary mode commands briefly."
7203   (interactive)
7204   (gnus-message 6 (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")))
7205
7206 ;; Walking around group mode buffer from summary mode.
7207
7208 (defun gnus-summary-next-group (&optional no-article target-group backward)
7209   "Exit current newsgroup and then select next unread newsgroup.
7210 If prefix argument NO-ARTICLE is non-nil, no article is selected
7211 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7212 previous group instead."
7213   (interactive "P")
7214   ;; Stop pre-fetching.
7215   (gnus-async-halt-prefetch)
7216   (let ((current-group gnus-newsgroup-name)
7217         (current-buffer (current-buffer))
7218         entered)
7219     ;; First we semi-exit this group to update Xrefs and all variables.
7220     ;; We can't do a real exit, because the window conf must remain
7221     ;; the same in case the user is prompted for info, and we don't
7222     ;; want the window conf to change before that...
7223     (gnus-summary-exit t)
7224     (while (not entered)
7225       ;; Then we find what group we are supposed to enter.
7226       (set-buffer gnus-group-buffer)
7227       (gnus-group-jump-to-group current-group)
7228       (setq target-group
7229             (or target-group
7230                 (if (eq gnus-keep-same-level 'best)
7231                     (gnus-summary-best-group gnus-newsgroup-name)
7232                   (gnus-summary-search-group backward gnus-keep-same-level))))
7233       (if (not target-group)
7234           ;; There are no further groups, so we return to the group
7235           ;; buffer.
7236           (progn
7237             (gnus-message 5 "Returning to the group buffer")
7238             (setq entered t)
7239             (when (gnus-buffer-live-p current-buffer)
7240               (set-buffer current-buffer)
7241               (gnus-summary-exit))
7242             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7243         ;; We try to enter the target group.
7244         (gnus-group-jump-to-group target-group)
7245         (let ((unreads (gnus-group-group-unread)))
7246           (if (and (or (eq t unreads)
7247                        (and unreads (not (zerop unreads))))
7248                    (gnus-summary-read-group
7249                     target-group nil no-article
7250                     (and (buffer-name current-buffer) current-buffer)
7251                     nil backward))
7252               (setq entered t)
7253             (setq current-group target-group
7254                   target-group nil)))))))
7255
7256 (defun gnus-summary-prev-group (&optional no-article)
7257   "Exit current newsgroup and then select previous unread newsgroup.
7258 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7259   (interactive "P")
7260   (gnus-summary-next-group no-article nil t))
7261
7262 ;; Walking around summary lines.
7263
7264 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7265   "Go to the first subject satisfying any non-nil constraint.
7266 If UNREAD is non-nil, the article should be unread.
7267 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7268 If UNSEEN is non-nil, the article should be unseen.
7269 Returns the article selected or nil if there are no matching articles."
7270   (interactive "P")
7271   (cond
7272    ;; Empty summary.
7273    ((null gnus-newsgroup-data)
7274     (gnus-message 3 "No articles in the group")
7275     nil)
7276    ;; Pick the first article.
7277    ((not (or unread undownloaded unseen))
7278     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7279     (gnus-data-number (car gnus-newsgroup-data)))
7280    ;; Find the first unread article.
7281    (t
7282     (let ((data gnus-newsgroup-data))
7283       (while (and data
7284                   (let ((num (gnus-data-number (car data))))
7285                     (or (memq num gnus-newsgroup-unfetched)
7286                         (not (or (and unread
7287                                       (memq num gnus-newsgroup-unreads))
7288                                  (and undownloaded
7289                                       (memq num gnus-newsgroup-undownloaded))
7290                                  (and unseen
7291                                       (memq num gnus-newsgroup-unseen)))))))
7292         (setq data (cdr data)))
7293       (prog1
7294           (if data
7295               (progn
7296                 (goto-char (gnus-data-pos (car data)))
7297                 (gnus-data-number (car data)))
7298             (gnus-message 3 "No more%s articles"
7299                           (let* ((r (when unread " unread"))
7300                                  (d (when undownloaded " undownloaded"))
7301                                  (s (when unseen " unseen"))
7302                                  (l (delq nil (list r d s))))
7303                             (cond ((= 3 (length l))
7304                                    (concat r "," d ", or" s))
7305                                   ((= 2 (length l))
7306                                    (concat (car l) ", or" (cadr l)))
7307                                   ((= 1 (length l))
7308                                    (car l))
7309                                   (t
7310                                    ""))))
7311             nil
7312             )
7313         (gnus-summary-position-point))))))
7314
7315 (defun gnus-summary-next-subject (n &optional unread dont-display)
7316   "Go to next N'th summary line.
7317 If N is negative, go to the previous N'th subject line.
7318 If UNREAD is non-nil, only unread articles are selected.
7319 The difference between N and the actual number of steps taken is
7320 returned."
7321   (interactive "p")
7322   (let ((backward (< n 0))
7323         (n (abs n)))
7324     (while (and (> n 0)
7325                 (if backward
7326                     (gnus-summary-find-prev unread)
7327                   (gnus-summary-find-next unread)))
7328       (unless (zerop (setq n (1- n)))
7329         (gnus-summary-show-thread)))
7330     (when (/= 0 n)
7331       (gnus-message 7 "No more%s articles"
7332                     (if unread " unread" "")))
7333     (unless dont-display
7334       (gnus-summary-recenter)
7335       (gnus-summary-position-point))
7336     n))
7337
7338 (defun gnus-summary-next-unread-subject (n)
7339   "Go to next N'th unread summary line."
7340   (interactive "p")
7341   (gnus-summary-next-subject n t))
7342
7343 (defun gnus-summary-prev-subject (n &optional unread)
7344   "Go to previous N'th summary line.
7345 If optional argument UNREAD is non-nil, only unread article is selected."
7346   (interactive "p")
7347   (gnus-summary-next-subject (- n) unread))
7348
7349 (defun gnus-summary-prev-unread-subject (n)
7350   "Go to previous N'th unread summary line."
7351   (interactive "p")
7352   (gnus-summary-next-subject (- n) t))
7353
7354 (defun gnus-summary-goto-subjects (articles)
7355   "Insert the subject header for ARTICLES in the current buffer."
7356   (save-excursion
7357     (dolist (article articles)
7358       (gnus-summary-goto-subject article t)))
7359   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7360   (gnus-summary-position-point))
7361
7362 (defun gnus-summary-goto-subject (article &optional force silent)
7363   "Go the subject line of ARTICLE.
7364 If FORCE, also allow jumping to articles not currently shown."
7365   (interactive "nArticle number: ")
7366   (unless (numberp article)
7367     (error "Article %s is not a number" article))
7368   (let ((b (point))
7369         (data (gnus-data-find article)))
7370     ;; We read in the article if we have to.
7371     (and (not data)
7372          force
7373          (gnus-summary-insert-subject
7374           article
7375           (if (or (numberp force) (vectorp force)) force)
7376           t)
7377          (setq data (gnus-data-find article)))
7378     (goto-char b)
7379     (if (not data)
7380         (progn
7381           (unless silent
7382             (gnus-message 3 "Can't find article %d" article))
7383           nil)
7384       (let ((pt (gnus-data-pos data)))
7385         (goto-char pt)
7386         (gnus-summary-set-article-display-arrow pt))
7387       (gnus-summary-position-point)
7388       article)))
7389
7390 ;; Walking around summary lines with displaying articles.
7391
7392 (defun gnus-summary-expand-window (&optional arg)
7393   "Make the summary buffer take up the entire Emacs frame.
7394 Given a prefix, will force an `article' buffer configuration."
7395   (interactive "P")
7396   (if arg
7397       (gnus-configure-windows 'article 'force)
7398     (gnus-configure-windows 'summary 'force)))
7399
7400 (defun gnus-summary-display-article (article &optional all-header)
7401   "Display ARTICLE in article buffer."
7402   (when (gnus-buffer-live-p gnus-article-buffer)
7403     (with-current-buffer gnus-article-buffer
7404       (mm-enable-multibyte)))
7405   (gnus-set-global-variables)
7406   (when (gnus-buffer-live-p gnus-article-buffer)
7407     (with-current-buffer gnus-article-buffer
7408       (setq gnus-article-charset gnus-newsgroup-charset)
7409       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7410       (mm-enable-multibyte)))
7411   (if (null article)
7412       nil
7413     (prog1
7414         (if gnus-summary-display-article-function
7415             (funcall gnus-summary-display-article-function article all-header)
7416           (gnus-article-prepare article all-header))
7417       (gnus-run-hooks 'gnus-select-article-hook)
7418       (when (and gnus-current-article
7419                  (not (zerop gnus-current-article)))
7420         (gnus-summary-goto-subject gnus-current-article))
7421       (gnus-summary-recenter)
7422       (when (and gnus-use-trees gnus-show-threads)
7423         (gnus-possibly-generate-tree article)
7424         (gnus-highlight-selected-tree article))
7425       ;; Successfully display article.
7426       (gnus-article-set-window-start
7427        (cdr (assq article gnus-newsgroup-bookmarks))))))
7428
7429 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7430   "Select the current article.
7431 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7432 non-nil, the article will be re-fetched even if it already present in
7433 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7434 be displayed."
7435   ;; Make sure we are in the summary buffer to work around bbdb bug.
7436   (unless (eq major-mode 'gnus-summary-mode)
7437     (set-buffer gnus-summary-buffer))
7438   (let ((article (or article (gnus-summary-article-number)))
7439         (all-headers (not (not all-headers))) ;Must be t or nil.
7440         gnus-summary-display-article-function)
7441     (and (not pseudo)
7442          (gnus-summary-article-pseudo-p article)
7443          (error "This is a pseudo-article"))
7444     (save-excursion
7445       (set-buffer gnus-summary-buffer)
7446       (if (or (and gnus-single-article-buffer
7447                    (or (null gnus-current-article)
7448                        (null gnus-article-current)
7449                        (null (get-buffer gnus-article-buffer))
7450                        (not (eq article (cdr gnus-article-current)))
7451                        (not (equal (car gnus-article-current)
7452                                    gnus-newsgroup-name))))
7453               (and (not gnus-single-article-buffer)
7454                    (or (null gnus-current-article)
7455                        (not (eq gnus-current-article article))))
7456               force)
7457           ;; The requested article is different from the current article.
7458           (progn
7459             (gnus-summary-display-article article all-headers)
7460             (when (gnus-buffer-live-p gnus-article-buffer)
7461               (with-current-buffer gnus-article-buffer
7462                 (if (not gnus-article-decoded-p) ;; a local variable
7463                     (mm-disable-multibyte))))
7464             (gnus-article-set-window-start
7465              (cdr (assq article gnus-newsgroup-bookmarks)))
7466             article)
7467         'old))))
7468
7469 (defun gnus-summary-force-verify-and-decrypt ()
7470   "Display buttons for signed/encrypted parts and verify/decrypt them."
7471   (interactive)
7472   (let ((mm-verify-option 'known)
7473         (mm-decrypt-option 'known)
7474         (gnus-article-emulate-mime t)
7475         (gnus-buttonized-mime-types (append (list "multipart/signed"
7476                                                   "multipart/encrypted")
7477                                             gnus-buttonized-mime-types)))
7478     (gnus-summary-select-article nil 'force)))
7479
7480 (defun gnus-summary-set-current-mark (&optional current-mark)
7481   "Obsolete function."
7482   nil)
7483
7484 (defun gnus-summary-next-article (&optional unread subject backward push)
7485   "Select the next article.
7486 If UNREAD, only unread articles are selected.
7487 If SUBJECT, only articles with SUBJECT are selected.
7488 If BACKWARD, the previous article is selected instead of the next."
7489   (interactive "P")
7490   ;; Make sure we are in the summary buffer.
7491   (unless (eq major-mode 'gnus-summary-mode)
7492     (set-buffer gnus-summary-buffer))
7493   (cond
7494    ;; Is there such an article?
7495    ((and (gnus-summary-search-forward unread subject backward)
7496          (or (gnus-summary-display-article (gnus-summary-article-number))
7497              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7498     (gnus-summary-position-point))
7499    ;; If not, we try the first unread, if that is wanted.
7500    ((and subject
7501          gnus-auto-select-same
7502          (gnus-summary-first-unread-article))
7503     (gnus-summary-position-point)
7504     (gnus-message 6 "Wrapped"))
7505    ;; Try to get next/previous article not displayed in this group.
7506    ((and gnus-auto-extend-newsgroup
7507          (not unread) (not subject))
7508     (gnus-summary-goto-article
7509      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7510      nil (count-lines (point-min) (point))))
7511    ;; Go to next/previous group.
7512    (t
7513     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7514       (gnus-summary-jump-to-group gnus-newsgroup-name))
7515     (let ((cmd last-command-char)
7516           (point
7517            (with-current-buffer gnus-group-buffer
7518              (point)))
7519           (group
7520            (if (eq gnus-keep-same-level 'best)
7521                (gnus-summary-best-group gnus-newsgroup-name)
7522              (gnus-summary-search-group backward gnus-keep-same-level))))
7523       ;; For some reason, the group window gets selected.  We change
7524       ;; it back.
7525       (select-window (get-buffer-window (current-buffer)))
7526       ;; Select next unread newsgroup automagically.
7527       (cond
7528        ((or (not gnus-auto-select-next)
7529             (not cmd))
7530         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7531        ((or (eq gnus-auto-select-next 'quietly)
7532             (and (eq gnus-auto-select-next 'slightly-quietly)
7533                  push)
7534             (and (eq gnus-auto-select-next 'almost-quietly)
7535                  (gnus-summary-last-article-p)))
7536         ;; Select quietly.
7537         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7538             (gnus-summary-exit)
7539           (gnus-message 7 "No more%s articles (%s)..."
7540                         (if unread " unread" "")
7541                         (if group (concat "selecting " group)
7542                           "exiting"))
7543           (gnus-summary-next-group nil group backward)))
7544        (t
7545         (when (gnus-key-press-event-p last-input-event)
7546           (gnus-summary-walk-group-buffer
7547            gnus-newsgroup-name cmd unread backward point))))))))
7548
7549 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7550   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7551                       (?\C-p (gnus-group-prev-unread-group 1))))
7552         (cursor-in-echo-area t)
7553         keve key group ended prompt)
7554     (save-excursion
7555       (set-buffer gnus-group-buffer)
7556       (goto-char start)
7557       (setq group
7558             (if (eq gnus-keep-same-level 'best)
7559                 (gnus-summary-best-group gnus-newsgroup-name)
7560               (gnus-summary-search-group backward gnus-keep-same-level))))
7561     (while (not ended)
7562       (setq prompt
7563             (format
7564              "No more%s articles%s " (if unread " unread" "")
7565              (if (and group
7566                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7567                  (format " (Type %s for %s [%s])"
7568                          (single-key-description cmd)
7569                          (gnus-group-decoded-name group)
7570                          (gnus-group-unread group))
7571                (format " (Type %s to exit %s)"
7572                        (single-key-description cmd)
7573                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7574       ;; Confirm auto selection.
7575       (setq key (car (setq keve (gnus-read-event-char prompt)))
7576             ended t)
7577       (cond
7578        ((assq key keystrokes)
7579         (let ((obuf (current-buffer)))
7580           (switch-to-buffer gnus-group-buffer)
7581           (when group
7582             (gnus-group-jump-to-group group))
7583           (eval (cadr (assq key keystrokes)))
7584           (setq group (gnus-group-group-name))
7585           (switch-to-buffer obuf))
7586         (setq ended nil))
7587        ((equal key cmd)
7588         (if (or (not group)
7589                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7590             (gnus-summary-exit)
7591           (gnus-summary-next-group nil group backward)))
7592        (t
7593         (push (cdr keve) unread-command-events))))))
7594
7595 (defun gnus-summary-next-unread-article ()
7596   "Select unread article after current one."
7597   (interactive)
7598   (gnus-summary-next-article
7599    (or (not (eq gnus-summary-goto-unread 'never))
7600        (gnus-summary-last-article-p (gnus-summary-article-number)))
7601    (and gnus-auto-select-same
7602         (gnus-summary-article-subject))))
7603
7604 (defun gnus-summary-prev-article (&optional unread subject)
7605   "Select the article after the current one.
7606 If UNREAD is non-nil, only unread articles are selected."
7607   (interactive "P")
7608   (gnus-summary-next-article unread subject t))
7609
7610 (defun gnus-summary-prev-unread-article ()
7611   "Select unread article before current one."
7612   (interactive)
7613   (gnus-summary-prev-article
7614    (or (not (eq gnus-summary-goto-unread 'never))
7615        (gnus-summary-first-article-p (gnus-summary-article-number)))
7616    (and gnus-auto-select-same
7617         (gnus-summary-article-subject))))
7618
7619 (defun gnus-summary-next-page (&optional lines circular stop)
7620   "Show next page of the selected article.
7621 If at the end of the current article, select the next article.
7622 LINES says how many lines should be scrolled up.
7623
7624 If CIRCULAR is non-nil, go to the start of the article instead of
7625 selecting the next article when reaching the end of the current
7626 article.
7627
7628 If STOP is non-nil, just stop when reaching the end of the message.
7629
7630 Also see the variable `gnus-article-skip-boring'."
7631   (interactive "P")
7632   (setq gnus-summary-buffer (current-buffer))
7633   (gnus-set-global-variables)
7634   (let ((article (gnus-summary-article-number))
7635         (article-window (get-buffer-window gnus-article-buffer t))
7636         endp)
7637     ;; If the buffer is empty, we have no article.
7638     (unless article
7639       (error "No article to select"))
7640     (gnus-configure-windows 'article)
7641     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7642         (if (and (eq gnus-summary-goto-unread 'never)
7643                  (not (gnus-summary-last-article-p article)))
7644             (gnus-summary-next-article)
7645           (gnus-summary-next-unread-article))
7646       (if (or (null gnus-current-article)
7647               (null gnus-article-current)
7648               (/= article (cdr gnus-article-current))
7649               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7650           ;; Selected subject is different from current article's.
7651           (gnus-summary-display-article article)
7652         (when article-window
7653           (gnus-eval-in-buffer-window gnus-article-buffer
7654             (setq endp (or (gnus-article-next-page lines)
7655                            (gnus-article-only-boring-p))))
7656           (when endp
7657             (cond (stop
7658                    (gnus-message 3 "End of message"))
7659                   (circular
7660                    (gnus-summary-beginning-of-article))
7661                   (lines
7662                    (gnus-message 3 "End of message"))
7663                   ((null lines)
7664                    (if (and (eq gnus-summary-goto-unread 'never)
7665                             (not (gnus-summary-last-article-p article)))
7666                        (gnus-summary-next-article)
7667                      (gnus-summary-next-unread-article))))))))
7668     (gnus-summary-recenter)
7669     (gnus-summary-position-point)))
7670
7671 (defun gnus-summary-prev-page (&optional lines move)
7672   "Show previous page of selected article.
7673 Argument LINES specifies lines to be scrolled down.
7674 If MOVE, move to the previous unread article if point is at
7675 the beginning of the buffer."
7676   (interactive "P")
7677   (let ((article (gnus-summary-article-number))
7678         (article-window (get-buffer-window gnus-article-buffer t))
7679         endp)
7680     (gnus-configure-windows 'article)
7681     (if (or (null gnus-current-article)
7682             (null gnus-article-current)
7683             (/= article (cdr gnus-article-current))
7684             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7685         ;; Selected subject is different from current article's.
7686         (gnus-summary-display-article article)
7687       (gnus-summary-recenter)
7688       (when article-window
7689         (gnus-eval-in-buffer-window gnus-article-buffer
7690           (setq endp (gnus-article-prev-page lines)))
7691         (when (and move endp)
7692           (cond (lines
7693                  (gnus-message 3 "Beginning of message"))
7694                 ((null lines)
7695                  (if (and (eq gnus-summary-goto-unread 'never)
7696                           (not (gnus-summary-first-article-p article)))
7697                      (gnus-summary-prev-article)
7698                    (gnus-summary-prev-unread-article))))))))
7699   (gnus-summary-position-point))
7700
7701 (defun gnus-summary-prev-page-or-article (&optional lines)
7702   "Show previous page of selected article.
7703 Argument LINES specifies lines to be scrolled down.
7704 If at the beginning of the article, go to the next article."
7705   (interactive "P")
7706   (gnus-summary-prev-page lines t))
7707
7708 (defun gnus-summary-scroll-up (lines)
7709   "Scroll up (or down) one line current article.
7710 Argument LINES specifies lines to be scrolled up (or down if negative)."
7711   (interactive "p")
7712   (gnus-configure-windows 'article)
7713   (gnus-summary-show-thread)
7714   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7715     (gnus-eval-in-buffer-window gnus-article-buffer
7716       (cond ((> lines 0)
7717              (when (gnus-article-next-page lines)
7718                (gnus-message 3 "End of message")))
7719             ((< lines 0)
7720              (gnus-article-prev-page (- lines))))))
7721   (gnus-summary-recenter)
7722   (gnus-summary-position-point))
7723
7724 (defun gnus-summary-scroll-down (lines)
7725   "Scroll down (or up) one line current article.
7726 Argument LINES specifies lines to be scrolled down (or up if negative)."
7727   (interactive "p")
7728   (gnus-summary-scroll-up (- lines)))
7729
7730 (defun gnus-summary-next-same-subject ()
7731   "Select next article which has the same subject as current one."
7732   (interactive)
7733   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7734
7735 (defun gnus-summary-prev-same-subject ()
7736   "Select previous article which has the same subject as current one."
7737   (interactive)
7738   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7739
7740 (defun gnus-summary-next-unread-same-subject ()
7741   "Select next unread article which has the same subject as current one."
7742   (interactive)
7743   (gnus-summary-next-article t (gnus-summary-article-subject)))
7744
7745 (defun gnus-summary-prev-unread-same-subject ()
7746   "Select previous unread article which has the same subject as current one."
7747   (interactive)
7748   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7749
7750 (defun gnus-summary-first-unread-article ()
7751   "Select the first unread article.
7752 Return nil if there are no unread articles."
7753   (interactive)
7754   (prog1
7755       (when (gnus-summary-first-subject t)
7756         (gnus-summary-show-thread)
7757         (gnus-summary-first-subject t)
7758         (gnus-summary-display-article (gnus-summary-article-number)))
7759     (gnus-summary-position-point)))
7760
7761 (defun gnus-summary-first-unread-subject ()
7762   "Place the point on the subject line of the first unread article.
7763 Return nil if there are no unread articles."
7764   (interactive)
7765   (prog1
7766       (when (gnus-summary-first-subject t)
7767         (gnus-summary-show-thread)
7768         (gnus-summary-first-subject t))
7769     (gnus-summary-position-point)))
7770
7771 (defun gnus-summary-first-unseen-subject ()
7772   "Place the point on the subject line of the first unseen article.
7773 Return nil if there are no unseen articles."
7774   (interactive)
7775   (prog1
7776       (when (gnus-summary-first-subject nil nil t)
7777         (gnus-summary-show-thread)
7778         (gnus-summary-first-subject nil nil t))
7779     (gnus-summary-position-point)))
7780
7781 (defun gnus-summary-first-unseen-or-unread-subject ()
7782   "Place the point on the subject line of the first unseen article or,
7783 if all article have been seen, on the subject line of the first unread
7784 article."
7785   (interactive)
7786   (prog1
7787       (unless (when (gnus-summary-first-subject nil nil t)
7788                 (gnus-summary-show-thread)
7789                 (gnus-summary-first-subject nil nil t))
7790         (when (gnus-summary-first-subject t)
7791           (gnus-summary-show-thread)
7792           (gnus-summary-first-subject t)))
7793     (gnus-summary-position-point)))
7794
7795 (defun gnus-summary-first-article ()
7796   "Select the first article.
7797 Return nil if there are no articles."
7798   (interactive)
7799   (prog1
7800       (when (gnus-summary-first-subject)
7801         (gnus-summary-show-thread)
7802         (gnus-summary-first-subject)
7803         (gnus-summary-display-article (gnus-summary-article-number)))
7804     (gnus-summary-position-point)))
7805
7806 (defun gnus-summary-best-unread-article (&optional arg)
7807   "Select the unread article with the highest score.
7808 If given a prefix argument, select the next unread article that has a
7809 score higher than the default score."
7810   (interactive "P")
7811   (let ((article (if arg
7812                      (gnus-summary-better-unread-subject)
7813                    (gnus-summary-best-unread-subject))))
7814     (if article
7815         (gnus-summary-goto-article article)
7816       (error "No unread articles"))))
7817
7818 (defun gnus-summary-best-unread-subject ()
7819   "Select the unread subject with the highest score."
7820   (interactive)
7821   (let ((best -1000000)
7822         (data gnus-newsgroup-data)
7823         article score)
7824     (while data
7825       (and (gnus-data-unread-p (car data))
7826            (> (setq score
7827                     (gnus-summary-article-score (gnus-data-number (car data))))
7828               best)
7829            (setq best score
7830                  article (gnus-data-number (car data))))
7831       (setq data (cdr data)))
7832     (when article
7833       (gnus-summary-goto-subject article))
7834     (gnus-summary-position-point)
7835     article))
7836
7837 (defun gnus-summary-better-unread-subject ()
7838   "Select the first unread subject that has a score over the default score."
7839   (interactive)
7840   (let ((data gnus-newsgroup-data)
7841         article score)
7842     (while (and (setq article (gnus-data-number (car data)))
7843                 (or (gnus-data-read-p (car data))
7844                     (not (> (gnus-summary-article-score article)
7845                             gnus-summary-default-score))))
7846       (setq data (cdr data)))
7847     (when article
7848       (gnus-summary-goto-subject article))
7849     (gnus-summary-position-point)
7850     article))
7851
7852 (defun gnus-summary-last-subject ()
7853   "Go to the last displayed subject line in the group."
7854   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7855     (when article
7856       (gnus-summary-goto-subject article))))
7857
7858 (defun gnus-summary-goto-article (article &optional all-headers force)
7859   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7860 If ALL-HEADERS is non-nil, no header lines are hidden.
7861 If FORCE, go to the article even if it isn't displayed.  If FORCE
7862 is a number, it is the line the article is to be displayed on."
7863   (interactive
7864    (list
7865     (completing-read
7866      "Article number or Message-ID: "
7867      (mapcar (lambda (number) (list (int-to-string number)))
7868              gnus-newsgroup-limit))
7869     current-prefix-arg
7870     t))
7871   (prog1
7872       (if (and (stringp article)
7873                (string-match "@\\|%40" article))
7874           (gnus-summary-refer-article article)
7875         (when (stringp article)
7876           (setq article (string-to-number article)))
7877         (if (gnus-summary-goto-subject article force)
7878             (gnus-summary-display-article article all-headers)
7879           (gnus-message 4 "Couldn't go to article %s" article) nil))
7880     (gnus-summary-position-point)))
7881
7882 (defun gnus-summary-goto-last-article ()
7883   "Go to the previously read article."
7884   (interactive)
7885   (prog1
7886       (when gnus-last-article
7887         (gnus-summary-goto-article gnus-last-article nil t))
7888     (gnus-summary-position-point)))
7889
7890 (defun gnus-summary-pop-article (number)
7891   "Pop one article off the history and go to the previous.
7892 NUMBER articles will be popped off."
7893   (interactive "p")
7894   (let (to)
7895     (setq gnus-newsgroup-history
7896           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7897     (if to
7898         (gnus-summary-goto-article (car to) nil t)
7899       (error "Article history empty")))
7900   (gnus-summary-position-point))
7901
7902 ;; Summary commands and functions for limiting the summary buffer.
7903
7904 (defun gnus-summary-limit-to-articles (n)
7905   "Limit the summary buffer to the next N articles.
7906 If not given a prefix, use the process marked articles instead."
7907   (interactive "P")
7908   (prog1
7909       (let ((articles (gnus-summary-work-articles n)))
7910         (setq gnus-newsgroup-processable nil)
7911         (gnus-summary-limit articles))
7912     (gnus-summary-position-point)))
7913
7914 (defun gnus-summary-pop-limit (&optional total)
7915   "Restore the previous limit.
7916 If given a prefix, remove all limits."
7917   (interactive "P")
7918   (when total
7919     (setq gnus-newsgroup-limits
7920           (list (mapcar (lambda (h) (mail-header-number h))
7921                         gnus-newsgroup-headers))))
7922   (unless gnus-newsgroup-limits
7923     (error "No limit to pop"))
7924   (prog1
7925       (gnus-summary-limit nil 'pop)
7926     (gnus-summary-position-point)))
7927
7928 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7929   "Limit the summary buffer to articles that have subjects that match a regexp.
7930 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7931   (interactive
7932    (list (read-string (if current-prefix-arg
7933                           "Exclude subject (regexp): "
7934                         "Limit to subject (regexp): "))
7935          nil current-prefix-arg))
7936   (unless header
7937     (setq header "subject"))
7938   (when (not (equal "" subject))
7939     (prog1
7940         (let ((articles (gnus-summary-find-matching
7941                          (or header "subject") subject 'all nil nil
7942                          not-matching)))
7943           (unless articles
7944             (error "Found no matches for \"%s\"" subject))
7945           (gnus-summary-limit articles))
7946       (gnus-summary-position-point))))
7947
7948 (defun gnus-summary-limit-to-author (from &optional not-matching)
7949   "Limit the summary buffer to articles that have authors that match a regexp.
7950 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7951   (interactive
7952    (list (read-string (if current-prefix-arg
7953                           "Exclude author (regexp): "
7954                         "Limit to author (regexp): "))
7955          current-prefix-arg))
7956   (gnus-summary-limit-to-subject from "from" not-matching))
7957
7958 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7959   "Limit the summary buffer to articles with the given RECIPIENT.
7960
7961 If NOT-MATCHING, exclude RECIPIENT.
7962
7963 To and Cc headers are checked.  You need to include them in
7964 `nnmail-extra-headers'."
7965   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7966   (interactive
7967    (list (read-string (format "%s recipient (regexp): "
7968                               (if current-prefix-arg "Exclude" "Limit to")))
7969          current-prefix-arg))
7970   (when (not (equal "" recipient))
7971     (prog1 (let* ((to
7972                    (if (memq 'To nnmail-extra-headers)
7973                        (gnus-summary-find-matching
7974                         (cons 'extra 'To) recipient 'all nil nil
7975                         not-matching)
7976                      (gnus-message
7977                       1 "`To' isn't present in `nnmail-extra-headers'")
7978                      (sit-for 1)
7979                      nil))
7980                   (cc
7981                    (if (memq 'Cc nnmail-extra-headers)
7982                        (gnus-summary-find-matching
7983                         (cons 'extra 'Cc) recipient 'all nil nil
7984                         not-matching)
7985                      (gnus-message
7986                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7987                      (sit-for 1)
7988                      nil))
7989                   (articles
7990                    (if not-matching
7991                        ;; We need the numbers that are in both lists:
7992                        (mapcar (lambda (a)
7993                                  (and (memq a to) a))
7994                                cc)
7995                      (nconc to cc))))
7996              (unless articles
7997                (error "Found no matches for \"%s\"" recipient))
7998              (gnus-summary-limit articles))
7999       (gnus-summary-position-point))))
8000
8001 (defun gnus-summary-limit-strange-charsets-predicate (header)
8002   (let ((string (concat (mail-header-subject header)
8003                         (mail-header-from header)))
8004         charset found)
8005     (dotimes (i (1- (length string)))
8006       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8007       (when (string-match "unicode\\|big\\|japanese" charset)
8008         (setq found t)))
8009     found))
8010
8011 (defun gnus-summary-limit-to-predicate (predicate)
8012   "Limit to articles where PREDICATE returns non-nil.
8013 PREDICATE will be called with the header structures of the
8014 articles."
8015   (let ((articles nil)
8016         (case-fold-search t))
8017     (dolist (header gnus-newsgroup-headers)
8018       (when (funcall predicate header)
8019         (push (mail-header-number header) articles)))
8020     (gnus-summary-limit (nreverse articles))))
8021
8022 (defun gnus-summary-limit-to-age (age &optional younger-p)
8023   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8024 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8025 articles that are younger than AGE days."
8026   (interactive
8027    (let ((younger current-prefix-arg)
8028          (days-got nil)
8029          days)
8030      (while (not days-got)
8031        (setq days (if younger
8032                       (read-string "Limit to articles younger than (in days, older when negative): ")
8033                     (read-string
8034                      "Limit to articles older than (in days, younger when negative): ")))
8035        (when (> (length days) 0)
8036          (setq days (read days)))
8037        (if (numberp days)
8038            (progn
8039              (setq days-got t)
8040              (when (< days 0)
8041                (setq younger (not younger))
8042                (setq days (* days -1))))
8043          (message "Please enter a number.")
8044          (sleep-for 1)))
8045      (list days younger)))
8046   (prog1
8047       (let ((data gnus-newsgroup-data)
8048             (cutoff (days-to-time age))
8049             articles d date is-younger)
8050         (while (setq d (pop data))
8051           (when (and (vectorp (gnus-data-header d))
8052                      (setq date (mail-header-date (gnus-data-header d))))
8053             (setq is-younger (time-less-p
8054                               (time-since (condition-case ()
8055                                               (date-to-time date)
8056                                             (error '(0 0))))
8057                               cutoff))
8058             (when (if younger-p
8059                       is-younger
8060                     (not is-younger))
8061               (push (gnus-data-number d) articles))))
8062         (gnus-summary-limit (nreverse articles)))
8063     (gnus-summary-position-point)))
8064
8065 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8066   "Limit the summary buffer to articles that match an 'extra' header."
8067   (interactive
8068    (let ((header
8069           (intern
8070            (gnus-completing-read-with-default
8071             (symbol-name (car gnus-extra-headers))
8072             (if current-prefix-arg
8073                 "Exclude extra header"
8074               "Limit extra header")
8075             (mapcar (lambda (x)
8076                       (cons (symbol-name x) x))
8077                     gnus-extra-headers)
8078             nil
8079             t))))
8080      (list header
8081            (read-string (format "%s header %s (regexp): "
8082                                 (if current-prefix-arg "Exclude" "Limit to")
8083                                 header))
8084            current-prefix-arg)))
8085   (when (not (equal "" regexp))
8086     (prog1
8087         (let ((articles (gnus-summary-find-matching
8088                          (cons 'extra header) regexp 'all nil nil
8089                          not-matching)))
8090           (unless articles
8091             (error "Found no matches for \"%s\"" regexp))
8092           (gnus-summary-limit articles))
8093       (gnus-summary-position-point))))
8094
8095 (defun gnus-summary-limit-to-display-predicate ()
8096   "Limit the summary buffer to the predicated in the `display' group parameter."
8097   (interactive)
8098   (unless gnus-newsgroup-display
8099     (error "There is no `display' group parameter"))
8100   (let (articles)
8101     (dolist (number gnus-newsgroup-articles)
8102       (when (funcall gnus-newsgroup-display)
8103         (push number articles)))
8104     (gnus-summary-limit articles))
8105   (gnus-summary-position-point))
8106
8107 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8108 (make-obsolete
8109  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8110
8111 (defun gnus-summary-limit-to-unread (&optional all)
8112   "Limit the summary buffer to articles that are not marked as read.
8113 If ALL is non-nil, limit strictly to unread articles."
8114   (interactive "P")
8115   (if all
8116       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8117     (gnus-summary-limit-to-marks
8118      ;; Concat all the marks that say that an article is read and have
8119      ;; those removed.
8120      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8121            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8122            gnus-low-score-mark gnus-expirable-mark
8123            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8124            gnus-duplicate-mark gnus-souped-mark)
8125      'reverse)))
8126
8127 (defun gnus-summary-limit-to-headers (match &optional reverse)
8128   "Limit the summary buffer to articles that have headers that match MATCH.
8129 If REVERSE (the prefix), limit to articles that don't match."
8130   (interactive "sMatch headers (regexp): \nP")
8131   (gnus-summary-limit-to-bodies match reverse t))
8132
8133 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8134   "Limit the summary buffer to articles that have bodies that match MATCH.
8135 If REVERSE (the prefix), limit to articles that don't match."
8136   (interactive "sMatch body (regexp): \nP")
8137   (let ((articles nil)
8138         (gnus-select-article-hook nil)  ;Disable hook.
8139         (gnus-article-prepare-hook nil)
8140         (gnus-use-article-prefetch nil)
8141         (gnus-keep-backlog nil)
8142         (gnus-break-pages nil)
8143         (gnus-summary-display-arrow nil)
8144         (gnus-updated-mode-lines nil)
8145         (gnus-auto-center-summary nil)
8146         (gnus-display-mime-function nil))
8147     (dolist (data gnus-newsgroup-data)
8148       (let (gnus-mark-article-hook)
8149         (gnus-summary-select-article t t nil (gnus-data-number data)))
8150       (save-excursion
8151         (set-buffer gnus-article-buffer)
8152         (article-goto-body)
8153         (let* ((case-fold-search t)
8154                (found (if headersp
8155                           (re-search-backward match nil t)
8156                         (re-search-forward match nil t))))
8157           (when (or (and found
8158                          (not reverse))
8159                     (and (not found)
8160                          reverse))
8161             (push (gnus-data-number data) articles)))))
8162     (if (not articles)
8163         (message "No messages matched")
8164       (gnus-summary-limit articles)))
8165   (gnus-summary-position-point))
8166
8167 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8168   "Limit the summary buffer to articles that aren't part on any thread.
8169 If THREADSP (the prefix), limit to articles that are in threads."
8170   (interactive "P")
8171   (let ((articles nil)
8172         thread-articles
8173         threads)
8174     (dolist (thread gnus-newsgroup-threads)
8175       (if (stringp (car thread))
8176           (dolist (thread (cdr thread))
8177             (push thread threads))
8178         (push thread threads)))
8179     (dolist (thread threads)
8180       (setq thread-articles (gnus-articles-in-thread thread))
8181       (when (or (and threadsp
8182                      (> (length thread-articles) 1))
8183                 (and (not threadsp)
8184                      (= (length thread-articles) 1)))
8185         (setq articles (nconc thread-articles articles))))
8186     (if (not articles)
8187         (message "No messages matched")
8188       (gnus-summary-limit articles))
8189     (gnus-summary-position-point)))
8190
8191 (defun gnus-summary-limit-to-replied (&optional unreplied)
8192   "Limit the summary buffer to replied articles.
8193 If UNREPLIED (the prefix), limit to unreplied articles."
8194   (interactive "P")
8195   (if unreplied
8196       (gnus-summary-limit
8197        (gnus-set-difference gnus-newsgroup-articles
8198         gnus-newsgroup-replied))
8199     (gnus-summary-limit gnus-newsgroup-replied))
8200   (gnus-summary-position-point))
8201
8202 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8203 (make-obsolete 'gnus-summary-delete-marked-with
8204                'gnus-summary-limit-exclude-marks)
8205
8206 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8207   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8208 If REVERSE, limit the summary buffer to articles that are marked
8209 with MARKS.  MARKS can either be a string of marks or a list of marks.
8210 Returns how many articles were removed."
8211   (interactive "sMarks: ")
8212   (gnus-summary-limit-to-marks marks t))
8213
8214 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8215   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8216 If REVERSE (the prefix), limit the summary buffer to articles that are
8217 not marked with MARKS.  MARKS can either be a string of marks or a
8218 list of marks.
8219 Returns how many articles were removed."
8220   (interactive "sMarks: \nP")
8221   (prog1
8222       (let ((data gnus-newsgroup-data)
8223             (marks (if (listp marks) marks
8224                      (append marks nil))) ; Transform to list.
8225             articles)
8226         (while data
8227           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8228                   (memq (gnus-data-mark (car data)) marks))
8229             (push (gnus-data-number (car data)) articles))
8230           (setq data (cdr data)))
8231         (gnus-summary-limit articles))
8232     (gnus-summary-position-point)))
8233
8234 (defun gnus-summary-limit-to-score (score)
8235   "Limit to articles with score at or above SCORE."
8236   (interactive "NLimit to articles with score of at least: ")
8237   (let ((data gnus-newsgroup-data)
8238         articles)
8239     (while data
8240       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8241                 score)
8242         (push (gnus-data-number (car data)) articles))
8243       (setq data (cdr data)))
8244     (prog1
8245         (gnus-summary-limit articles)
8246       (gnus-summary-position-point))))
8247
8248 (defun gnus-summary-limit-to-unseen ()
8249   "Limit to unseen articles."
8250   (interactive)
8251   (prog1
8252       (gnus-summary-limit gnus-newsgroup-unseen)
8253     (gnus-summary-position-point)))
8254
8255 (defun gnus-summary-limit-include-thread (id)
8256   "Display all the hidden articles that is in the thread with ID in it.
8257 When called interactively, ID is the Message-ID of the current
8258 article."
8259   (interactive (list (mail-header-id (gnus-summary-article-header))))
8260   (let ((articles (gnus-articles-in-thread
8261                    (gnus-id-to-thread (gnus-root-id id)))))
8262     (prog1
8263         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8264       (gnus-summary-limit-include-matching-articles
8265        "subject"
8266        (regexp-quote (gnus-simplify-subject-re
8267                       (mail-header-subject (gnus-id-to-header id)))))
8268       (gnus-summary-position-point))))
8269
8270 (defun gnus-summary-limit-include-matching-articles (header regexp)
8271   "Display all the hidden articles that have HEADERs that match REGEXP."
8272   (interactive (list (read-string "Match on header: ")
8273                      (read-string "Regexp: ")))
8274   (let ((articles (gnus-find-matching-articles header regexp)))
8275     (prog1
8276         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8277       (gnus-summary-position-point))))
8278
8279 (defun gnus-summary-insert-dormant-articles ()
8280   "Insert all the dormant articles for this group into the current buffer."
8281   (interactive)
8282   (let ((gnus-verbose (max 6 gnus-verbose)))
8283     (if (not gnus-newsgroup-dormant)
8284         (gnus-message 3 "No dormant articles for this group")
8285       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8286
8287 (defun gnus-summary-insert-ticked-articles ()
8288   "Insert ticked articles for this group into the current buffer."
8289   (interactive)
8290   (let ((gnus-verbose (max 6 gnus-verbose)))
8291     (if (not gnus-newsgroup-marked)
8292         (gnus-message 3 "No ticked articles for this group")
8293       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8294
8295 (defun gnus-summary-limit-include-dormant ()
8296   "Display all the hidden articles that are marked as dormant.
8297 Note that this command only works on a subset of the articles currently
8298 fetched for this group."
8299   (interactive)
8300   (unless gnus-newsgroup-dormant
8301     (error "There are no dormant articles in this group"))
8302   (prog1
8303       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8304     (gnus-summary-position-point)))
8305
8306 (defun gnus-summary-limit-exclude-dormant ()
8307   "Hide all dormant articles."
8308   (interactive)
8309   (prog1
8310       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8311     (gnus-summary-position-point)))
8312
8313 (defun gnus-summary-limit-exclude-childless-dormant ()
8314   "Hide all dormant articles that have no children."
8315   (interactive)
8316   (let ((data (gnus-data-list t))
8317         articles d children)
8318     ;; Find all articles that are either not dormant or have
8319     ;; children.
8320     (while (setq d (pop data))
8321       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8322                 (and (setq children
8323                            (gnus-article-children (gnus-data-number d)))
8324                      (let (found)
8325                        (while children
8326                          (when (memq (car children) articles)
8327                            (setq children nil
8328                                  found t))
8329                          (pop children))
8330                        found)))
8331         (push (gnus-data-number d) articles)))
8332     ;; Do the limiting.
8333     (prog1
8334         (gnus-summary-limit articles)
8335       (gnus-summary-position-point))))
8336
8337 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8338   "Mark all unread excluded articles as read.
8339 If ALL, mark even excluded ticked and dormants as read."
8340   (interactive "P")
8341   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8342   (let ((articles (gnus-sorted-ndifference
8343                    (sort
8344                     (mapcar (lambda (h) (mail-header-number h))
8345                             gnus-newsgroup-headers)
8346                     '<)
8347                    gnus-newsgroup-limit))
8348         article)
8349     (setq gnus-newsgroup-unreads
8350           (gnus-sorted-intersection gnus-newsgroup-unreads
8351                                     gnus-newsgroup-limit))
8352     (if all
8353         (setq gnus-newsgroup-dormant nil
8354               gnus-newsgroup-marked nil
8355               gnus-newsgroup-reads
8356               (nconc
8357                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8358                gnus-newsgroup-reads))
8359       (while (setq article (pop articles))
8360         (unless (or (memq article gnus-newsgroup-dormant)
8361                     (memq article gnus-newsgroup-marked))
8362           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8363
8364 (defun gnus-summary-limit (articles &optional pop)
8365   (if pop
8366       ;; We pop the previous limit off the stack and use that.
8367       (setq articles (car gnus-newsgroup-limits)
8368             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8369     ;; We use the new limit, so we push the old limit on the stack.
8370     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8371   ;; Set the limit.
8372   (setq gnus-newsgroup-limit articles)
8373   (let ((total (length gnus-newsgroup-data))
8374         (data (gnus-data-find-list (gnus-summary-article-number)))
8375         (gnus-summary-mark-below nil)   ; Inhibit this.
8376         found)
8377     ;; This will do all the work of generating the new summary buffer
8378     ;; according to the new limit.
8379     (gnus-summary-prepare)
8380     ;; Hide any threads, possibly.
8381     (gnus-summary-maybe-hide-threads)
8382     ;; Try to return to the article you were at, or one in the
8383     ;; neighborhood.
8384     (when data
8385       ;; We try to find some article after the current one.
8386       (while data
8387         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8388           (setq data nil
8389                 found t))
8390         (setq data (cdr data))))
8391     (unless found
8392       ;; If there is no data, that means that we were after the last
8393       ;; article.  The same goes when we can't find any articles
8394       ;; after the current one.
8395       (goto-char (point-max))
8396       (gnus-summary-find-prev))
8397     (gnus-set-mode-line 'summary)
8398     ;; We return how many articles were removed from the summary
8399     ;; buffer as a result of the new limit.
8400     (- total (length gnus-newsgroup-data))))
8401
8402 (defsubst gnus-invisible-cut-children (threads)
8403   (let ((num 0))
8404     (while threads
8405       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8406         (incf num))
8407       (pop threads))
8408     (< num 2)))
8409
8410 (defsubst gnus-cut-thread (thread)
8411   "Go forwards in the thread until we find an article that we want to display."
8412   (when (or (eq gnus-fetch-old-headers 'some)
8413             (eq gnus-fetch-old-headers 'invisible)
8414             (numberp gnus-fetch-old-headers)
8415             (eq gnus-build-sparse-threads 'some)
8416             (eq gnus-build-sparse-threads 'more))
8417     ;; Deal with old-fetched headers and sparse threads.
8418     (while (and
8419             thread
8420             (or
8421              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8422              (gnus-summary-article-ancient-p
8423               (mail-header-number (car thread))))
8424             (if (or (<= (length (cdr thread)) 1)
8425                     (eq gnus-fetch-old-headers 'invisible))
8426                 (setq gnus-newsgroup-limit
8427                       (delq (mail-header-number (car thread))
8428                             gnus-newsgroup-limit)
8429                       thread (cadr thread))
8430               (when (gnus-invisible-cut-children (cdr thread))
8431                 (let ((th (cdr thread)))
8432                   (while th
8433                     (if (memq (mail-header-number (caar th))
8434                               gnus-newsgroup-limit)
8435                         (setq thread (car th)
8436                               th nil)
8437                       (setq th (cdr th))))))))))
8438   thread)
8439
8440 (defun gnus-cut-threads (threads)
8441   "Cut off all uninteresting articles from the beginning of THREADS."
8442   (when (or (eq gnus-fetch-old-headers 'some)
8443             (eq gnus-fetch-old-headers 'invisible)
8444             (numberp gnus-fetch-old-headers)
8445             (eq gnus-build-sparse-threads 'some)
8446             (eq gnus-build-sparse-threads 'more))
8447     (let ((th threads))
8448       (while th
8449         (setcar th (gnus-cut-thread (car th)))
8450         (setq th (cdr th)))))
8451   ;; Remove nixed out threads.
8452   (delq nil threads))
8453
8454 (defun gnus-summary-initial-limit (&optional show-if-empty)
8455   "Figure out what the initial limit is supposed to be on group entry.
8456 This entails weeding out unwanted dormants, low-scored articles,
8457 fetch-old-headers verbiage, and so on."
8458   ;; Most groups have nothing to remove.
8459   (if (or gnus-inhibit-limiting
8460           (and (null gnus-newsgroup-dormant)
8461                (eq gnus-newsgroup-display 'gnus-not-ignore)
8462                (not (eq gnus-fetch-old-headers 'some))
8463                (not (numberp gnus-fetch-old-headers))
8464                (not (eq gnus-fetch-old-headers 'invisible))
8465                (null gnus-summary-expunge-below)
8466                (not (eq gnus-build-sparse-threads 'some))
8467                (not (eq gnus-build-sparse-threads 'more))
8468                (null gnus-thread-expunge-below)
8469                (not gnus-use-nocem)))
8470       ()                                ; Do nothing.
8471     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8472     (setq gnus-newsgroup-limit nil)
8473     (mapatoms
8474      (lambda (node)
8475        (unless (car (symbol-value node))
8476          ;; These threads have no parents -- they are roots.
8477          (let ((nodes (cdr (symbol-value node)))
8478                thread)
8479            (while nodes
8480              (if (and gnus-thread-expunge-below
8481                       (< (gnus-thread-total-score (car nodes))
8482                          gnus-thread-expunge-below))
8483                  (gnus-expunge-thread (pop nodes))
8484                (setq thread (pop nodes))
8485                (gnus-summary-limit-children thread))))))
8486      gnus-newsgroup-dependencies)
8487     ;; If this limitation resulted in an empty group, we might
8488     ;; pop the previous limit and use it instead.
8489     (when (and (not gnus-newsgroup-limit)
8490                show-if-empty)
8491       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8492     gnus-newsgroup-limit))
8493
8494 (defun gnus-summary-limit-children (thread)
8495   "Return 1 if this subthread is visible and 0 if it is not."
8496   ;; First we get the number of visible children to this thread.  This
8497   ;; is done by recursing down the thread using this function, so this
8498   ;; will really go down to a leaf article first, before slowly
8499   ;; working its way up towards the root.
8500   (when thread
8501     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8502            (children
8503            (if (cdr thread)
8504                (apply '+ (mapcar 'gnus-summary-limit-children
8505                                  (cdr thread)))
8506              0))
8507           (number (mail-header-number (car thread)))
8508           score)
8509       (if (and
8510            (not (memq number gnus-newsgroup-marked))
8511            (or
8512             ;; If this article is dormant and has absolutely no visible
8513             ;; children, then this article isn't visible.
8514             (and (memq number gnus-newsgroup-dormant)
8515                  (zerop children))
8516             ;; If this is "fetch-old-headered" and there is no
8517             ;; visible children, then we don't want this article.
8518             (and (or (eq gnus-fetch-old-headers 'some)
8519                      (numberp gnus-fetch-old-headers))
8520                  (gnus-summary-article-ancient-p number)
8521                  (zerop children))
8522             ;; If this is "fetch-old-headered" and `invisible', then
8523             ;; we don't want this article.
8524             (and (eq gnus-fetch-old-headers 'invisible)
8525                  (gnus-summary-article-ancient-p number))
8526             ;; If this is a sparsely inserted article with no children,
8527             ;; we don't want it.
8528             (and (eq gnus-build-sparse-threads 'some)
8529                  (gnus-summary-article-sparse-p number)
8530                  (zerop children))
8531             ;; If we use expunging, and this article is really
8532             ;; low-scored, then we don't want this article.
8533             (when (and gnus-summary-expunge-below
8534                        (< (setq score
8535                                 (or (cdr (assq number gnus-newsgroup-scored))
8536                                     gnus-summary-default-score))
8537                           gnus-summary-expunge-below))
8538               ;; We increase the expunge-tally here, but that has
8539               ;; nothing to do with the limits, really.
8540               (incf gnus-newsgroup-expunged-tally)
8541               ;; We also mark as read here, if that's wanted.
8542               (when (and gnus-summary-mark-below
8543                          (< score gnus-summary-mark-below))
8544                 (setq gnus-newsgroup-unreads
8545                       (delq number gnus-newsgroup-unreads))
8546                 (if gnus-newsgroup-auto-expire
8547                     (push number gnus-newsgroup-expirable)
8548                   (push (cons number gnus-low-score-mark)
8549                         gnus-newsgroup-reads)))
8550               t)
8551             ;; Do the `display' group parameter.
8552             (and gnus-newsgroup-display
8553                  (not (funcall gnus-newsgroup-display)))
8554             ;; Check NoCeM things.
8555             (when (and gnus-use-nocem
8556                        (gnus-nocem-unwanted-article-p
8557                         (mail-header-id (car thread))))
8558               (setq gnus-newsgroup-unreads
8559                     (delq number gnus-newsgroup-unreads))
8560               t)))
8561           ;; Nope, invisible article.
8562           0
8563         ;; Ok, this article is to be visible, so we add it to the limit
8564         ;; and return 1.
8565         (push number gnus-newsgroup-limit)
8566         1))))
8567
8568 (defun gnus-expunge-thread (thread)
8569   "Mark all articles in THREAD as read."
8570   (let* ((number (mail-header-number (car thread))))
8571     (incf gnus-newsgroup-expunged-tally)
8572     ;; We also mark as read here, if that's wanted.
8573     (setq gnus-newsgroup-unreads
8574           (delq number gnus-newsgroup-unreads))
8575     (if gnus-newsgroup-auto-expire
8576         (push number gnus-newsgroup-expirable)
8577       (push (cons number gnus-low-score-mark)
8578             gnus-newsgroup-reads)))
8579   ;; Go recursively through all subthreads.
8580   (mapcar 'gnus-expunge-thread (cdr thread)))
8581
8582 ;; Summary article oriented commands
8583
8584 (defun gnus-summary-refer-parent-article (n)
8585   "Refer parent article N times.
8586 If N is negative, go to ancestor -N instead.
8587 The difference between N and the number of articles fetched is returned."
8588   (interactive "p")
8589   (let ((skip 1)
8590         error header ref)
8591     (when (not (natnump n))
8592       (setq skip (abs n)
8593             n 1))
8594     (while (and (> n 0)
8595                 (not error))
8596       (setq header (gnus-summary-article-header))
8597       (if (and (eq (mail-header-number header)
8598                    (cdr gnus-article-current))
8599                (equal gnus-newsgroup-name
8600                       (car gnus-article-current)))
8601           ;; If we try to find the parent of the currently
8602           ;; displayed article, then we take a look at the actual
8603           ;; References header, since this is slightly more
8604           ;; reliable than the References field we got from the
8605           ;; server.
8606           (save-excursion
8607             (set-buffer gnus-original-article-buffer)
8608             (nnheader-narrow-to-headers)
8609             (unless (setq ref (message-fetch-field "references"))
8610               (when (setq ref (message-fetch-field "in-reply-to"))
8611                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8612             (widen))
8613         (setq ref
8614               ;; It's not the current article, so we take a bet on
8615               ;; the value we got from the server.
8616               (mail-header-references header)))
8617       (if (and ref
8618                (not (equal ref "")))
8619           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8620             (gnus-message 1 "Couldn't find parent"))
8621         (gnus-message 1 "No references in article %d"
8622                       (gnus-summary-article-number))
8623         (setq error t))
8624       (decf n))
8625     (gnus-summary-position-point)
8626     n))
8627
8628 (defun gnus-summary-refer-references ()
8629   "Fetch all articles mentioned in the References header.
8630 Return the number of articles fetched."
8631   (interactive)
8632   (let ((ref (mail-header-references (gnus-summary-article-header)))
8633         (current (gnus-summary-article-number))
8634         (n 0))
8635     (if (or (not ref)
8636             (equal ref ""))
8637         (error "No References in the current article")
8638       ;; For each Message-ID in the References header...
8639       (while (string-match "<[^>]*>" ref)
8640         (incf n)
8641         ;; ... fetch that article.
8642         (gnus-summary-refer-article
8643          (prog1 (match-string 0 ref)
8644            (setq ref (substring ref (match-end 0))))))
8645       (gnus-summary-goto-subject current)
8646       (gnus-summary-position-point)
8647       n)))
8648
8649 (defun gnus-summary-refer-thread (&optional limit)
8650   "Fetch all articles in the current thread.
8651 If LIMIT (the numerical prefix), fetch that many old headers instead
8652 of what's specified by the `gnus-refer-thread-limit' variable."
8653   (interactive "P")
8654   (let ((id (mail-header-id (gnus-summary-article-header)))
8655         (limit (if limit (prefix-numeric-value limit)
8656                  gnus-refer-thread-limit)))
8657     (unless (eq gnus-fetch-old-headers 'invisible)
8658       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8659       ;; Retrieve the headers and read them in.
8660       (if (eq (if (numberp limit)
8661                   (gnus-retrieve-headers
8662                    (list (min
8663                           (+ (mail-header-number
8664                               (gnus-summary-article-header))
8665                              limit)
8666                           gnus-newsgroup-end))
8667                    gnus-newsgroup-name (* limit 2))
8668                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8669                 ;; headers.
8670                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8671                                        gnus-newsgroup-name limit))
8672               'nov)
8673           (gnus-build-all-threads)
8674         (error "Can't fetch thread from back ends that don't support NOV"))
8675       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8676     (gnus-summary-limit-include-thread id)))
8677
8678 (defun gnus-summary-refer-article (message-id)
8679   "Fetch an article specified by MESSAGE-ID."
8680   (interactive "sMessage-ID: ")
8681   (when (and (stringp message-id)
8682              (not (zerop (length message-id))))
8683     (setq message-id (gnus-replace-in-string message-id " " ""))
8684     ;; Construct the correct Message-ID if necessary.
8685     ;; Suggested by tale@pawl.rpi.edu.
8686     (unless (string-match "^<" message-id)
8687       (setq message-id (concat "<" message-id)))
8688     (unless (string-match ">$" message-id)
8689       (setq message-id (concat message-id ">")))
8690     ;; People often post MIDs from URLs, so unhex it:
8691     (unless (string-match "@" message-id)
8692       (setq message-id (gnus-url-unhex-string message-id)))
8693     (let* ((header (gnus-id-to-header message-id))
8694            (sparse (and header
8695                         (gnus-summary-article-sparse-p
8696                          (mail-header-number header))
8697                         (memq (mail-header-number header)
8698                               gnus-newsgroup-limit)))
8699            number)
8700       (cond
8701        ;; If the article is present in the buffer we just go to it.
8702        ((and header
8703              (or (not (gnus-summary-article-sparse-p
8704                        (mail-header-number header)))
8705                  sparse))
8706         (prog1
8707             (gnus-summary-goto-article
8708              (mail-header-number header) nil t)
8709           (when sparse
8710             (gnus-summary-update-article (mail-header-number header)))))
8711        (t
8712         ;; We fetch the article.
8713         (catch 'found
8714           (dolist (gnus-override-method (gnus-refer-article-methods))
8715             (when (and (gnus-check-server gnus-override-method)
8716                        ;; Fetch the header,
8717                        (setq number (gnus-summary-insert-subject message-id)))
8718               ;; and display the article.
8719               (gnus-summary-select-article nil nil nil number)
8720               (throw 'found t)))
8721           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8722
8723 (defun gnus-refer-article-methods ()
8724   "Return a list of referable methods."
8725   (cond
8726    ;; No method, so we default to current and native.
8727    ((null gnus-refer-article-method)
8728     (list gnus-current-select-method gnus-select-method))
8729    ;; Current.
8730    ((eq 'current gnus-refer-article-method)
8731     (list gnus-current-select-method))
8732    ;; List of select methods.
8733    ((not (and (symbolp (car gnus-refer-article-method))
8734               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8735     (let (out)
8736       (dolist (method gnus-refer-article-method)
8737         (push (if (eq 'current method)
8738                   gnus-current-select-method
8739                 method)
8740               out))
8741       (nreverse out)))
8742    ;; One single select method.
8743    (t
8744     (list gnus-refer-article-method))))
8745
8746 (defun gnus-summary-edit-parameters ()
8747   "Edit the group parameters of the current group."
8748   (interactive)
8749   (gnus-group-edit-group gnus-newsgroup-name 'params))
8750
8751 (defun gnus-summary-customize-parameters ()
8752   "Customize the group parameters of the current group."
8753   (interactive)
8754   (gnus-group-customize gnus-newsgroup-name))
8755
8756 (defun gnus-summary-enter-digest-group (&optional force)
8757   "Enter an nndoc group based on the current article.
8758 If FORCE, force a digest interpretation.  If not, try
8759 to guess what the document format is."
8760   (interactive "P")
8761   (let ((conf gnus-current-window-configuration))
8762     (save-window-excursion
8763       (save-excursion
8764         (let (gnus-article-prepare-hook
8765               gnus-display-mime-function
8766               gnus-break-pages)
8767           (gnus-summary-select-article))))
8768     (setq gnus-current-window-configuration conf)
8769     (let* ((name (format "%s-%d"
8770                          (gnus-group-prefixed-name
8771                           gnus-newsgroup-name (list 'nndoc ""))
8772                          (with-current-buffer gnus-summary-buffer
8773                            gnus-current-article)))
8774            (ogroup gnus-newsgroup-name)
8775            (params (append (gnus-info-params (gnus-get-info ogroup))
8776                            (list (cons 'to-group ogroup))
8777                            (list (cons 'parent-group ogroup))
8778                            (list (cons 'save-article-group ogroup))))
8779            (case-fold-search t)
8780            (buf (current-buffer))
8781            dig to-address)
8782       (save-excursion
8783         (set-buffer gnus-original-article-buffer)
8784         ;; Have the digest group inherit the main mail address of
8785         ;; the parent article.
8786         (when (setq to-address (or (gnus-fetch-field "reply-to")
8787                                    (gnus-fetch-field "from")))
8788           (setq params
8789                 (append
8790                  (list (cons 'to-address
8791                              (funcall gnus-decode-encoded-address-function
8792                                       to-address))))))
8793         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8794         (insert-buffer-substring gnus-original-article-buffer)
8795         ;; Remove lines that may lead nndoc to misinterpret the
8796         ;; document type.
8797         (narrow-to-region
8798          (goto-char (point-min))
8799          (or (search-forward "\n\n" nil t) (point)))
8800         (goto-char (point-min))
8801         (delete-matching-lines "^Path:\\|^From ")
8802         (widen))
8803       (unwind-protect
8804           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8805                     (gnus-newsgroup-ephemeral-ignored-charsets
8806                      gnus-newsgroup-ignored-charsets))
8807                 (gnus-group-read-ephemeral-group
8808                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8809                               (nndoc-article-type
8810                                ,(if force 'mbox 'guess)))
8811                  t nil nil nil
8812                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8813                                                         "ADAPT")))))
8814               ;; Make all postings to this group go to the parent group.
8815               (nconc (gnus-info-params (gnus-get-info name))
8816                      params)
8817             ;; Couldn't select this doc group.
8818             (switch-to-buffer buf)
8819             (gnus-set-global-variables)
8820             (gnus-configure-windows 'summary)
8821             (gnus-message 3 "Article couldn't be entered?"))
8822         (kill-buffer dig)))))
8823
8824 (defun gnus-summary-read-document (n)
8825   "Open a new group based on the current article(s).
8826 This will allow you to read digests and other similar
8827 documents as newsgroups.
8828 Obeys the standard process/prefix convention."
8829   (interactive "P")
8830   (let* ((ogroup gnus-newsgroup-name)
8831          (params (append (gnus-info-params (gnus-get-info ogroup))
8832                          (list (cons 'to-group ogroup))))
8833          group egroup groups vgroup)
8834     (dolist (article (gnus-summary-work-articles n))
8835       (setq group (format "%s-%d" gnus-newsgroup-name article))
8836       (gnus-summary-remove-process-mark article)
8837       (when (gnus-summary-display-article article)
8838         (save-excursion
8839           (with-temp-buffer
8840             (insert-buffer-substring gnus-original-article-buffer)
8841             ;; Remove some headers that may lead nndoc to make
8842             ;; the wrong guess.
8843             (message-narrow-to-head)
8844             (goto-char (point-min))
8845             (delete-matching-lines "^Path:\\|^From ")
8846             (widen)
8847             (if (setq egroup
8848                       (gnus-group-read-ephemeral-group
8849                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8850                                      (nndoc-article-type guess))
8851                        t nil t))
8852                 (progn
8853             ;; Make all postings to this group go to the parent group.
8854                   (nconc (gnus-info-params (gnus-get-info egroup))
8855                          params)
8856                   (push egroup groups))
8857               ;; Couldn't select this doc group.
8858               (gnus-error 3 "Article couldn't be entered"))))))
8859     ;; Now we have selected all the documents.
8860     (cond
8861      ((not groups)
8862       (error "None of the articles could be interpreted as documents"))
8863      ((gnus-group-read-ephemeral-group
8864        (setq vgroup (format
8865                      "nnvirtual:%s-%s" gnus-newsgroup-name
8866                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8867        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8868        t
8869        (cons (current-buffer) 'summary)))
8870      (t
8871       (error "Couldn't select virtual nndoc group")))))
8872
8873 (defun gnus-summary-isearch-article (&optional regexp-p)
8874   "Do incremental search forward on the current article.
8875 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8876   (interactive "P")
8877   (gnus-summary-select-article)
8878   (gnus-configure-windows 'article)
8879   (gnus-eval-in-buffer-window gnus-article-buffer
8880     (save-restriction
8881       (widen)
8882       (isearch-forward regexp-p))))
8883
8884 (defun gnus-summary-repeat-search-article-forward ()
8885   "Repeat the previous search forwards."
8886   (interactive)
8887   (unless gnus-last-search-regexp
8888     (error "No previous search"))
8889   (gnus-summary-search-article-forward gnus-last-search-regexp))
8890
8891 (defun gnus-summary-repeat-search-article-backward ()
8892   "Repeat the previous search backwards."
8893   (interactive)
8894   (unless gnus-last-search-regexp
8895     (error "No previous search"))
8896   (gnus-summary-search-article-forward gnus-last-search-regexp t))
8897
8898 (defun gnus-summary-search-article-forward (regexp &optional backward)
8899   "Search for an article containing REGEXP forward.
8900 If BACKWARD, search backward instead."
8901   (interactive
8902    (list (read-string
8903           (format "Search article %s (regexp%s): "
8904                   (if current-prefix-arg "backward" "forward")
8905                   (if gnus-last-search-regexp
8906                       (concat ", default " gnus-last-search-regexp)
8907                     "")))
8908          current-prefix-arg))
8909   (if (string-equal regexp "")
8910       (setq regexp (or gnus-last-search-regexp ""))
8911     (setq gnus-last-search-regexp regexp)
8912     (setq gnus-article-before-search gnus-current-article))
8913   ;; Intentionally set gnus-last-article.
8914   (setq gnus-last-article gnus-article-before-search)
8915   (let ((gnus-last-article gnus-last-article))
8916     (if (gnus-summary-search-article regexp backward)
8917         (gnus-summary-show-thread)
8918       (signal 'search-failed (list regexp)))))
8919
8920 (defun gnus-summary-search-article-backward (regexp)
8921   "Search for an article containing REGEXP backward."
8922   (interactive
8923    (list (read-string
8924           (format "Search article backward (regexp%s): "
8925                   (if gnus-last-search-regexp
8926                       (concat ", default " gnus-last-search-regexp)
8927                     "")))))
8928   (gnus-summary-search-article-forward regexp 'backward))
8929
8930 (defun gnus-summary-search-article (regexp &optional backward)
8931   "Search for an article containing REGEXP.
8932 Optional argument BACKWARD means do search for backward.
8933 `gnus-select-article-hook' is not called during the search."
8934   ;; We have to require this here to make sure that the following
8935   ;; dynamic binding isn't shadowed by autoloading.
8936   (require 'gnus-async)
8937   (require 'gnus-art)
8938   (let ((gnus-select-article-hook nil)  ;Disable hook.
8939         (gnus-article-prepare-hook nil)
8940         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8941         (gnus-use-article-prefetch nil)
8942         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8943         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8944         (gnus-visual nil)
8945         (gnus-keep-backlog nil)
8946         (gnus-break-pages nil)
8947         (gnus-summary-display-arrow nil)
8948         (gnus-updated-mode-lines nil)
8949         (gnus-auto-center-summary nil)
8950         (sum (current-buffer))
8951         (gnus-display-mime-function nil)
8952         (found nil)
8953         point)
8954     (gnus-save-hidden-threads
8955       (gnus-summary-select-article)
8956       (set-buffer gnus-article-buffer)
8957       (goto-char (window-point (get-buffer-window (current-buffer))))
8958       (when backward
8959         (forward-line -1))
8960       (while (not found)
8961         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8962         (if (if backward
8963                 (re-search-backward regexp nil t)
8964               (re-search-forward regexp nil t))
8965             ;; We found the regexp.
8966             (progn
8967               (setq found 'found)
8968               (beginning-of-line)
8969               (set-window-start
8970                (get-buffer-window (current-buffer))
8971                (point))
8972               (forward-line 1)
8973               (set-window-point
8974                (get-buffer-window (current-buffer))
8975                (point))
8976               (set-buffer sum)
8977               (setq point (point)))
8978           ;; We didn't find it, so we go to the next article.
8979           (set-buffer sum)
8980           (setq found 'not)
8981           (while (eq found 'not)
8982             (if (not (if backward (gnus-summary-find-prev)
8983                        (gnus-summary-find-next)))
8984                 ;; No more articles.
8985                 (setq found t)
8986               ;; Select the next article and adjust point.
8987               (unless (gnus-summary-article-sparse-p
8988                        (gnus-summary-article-number))
8989                 (setq found nil)
8990                 (gnus-summary-select-article)
8991                 (set-buffer gnus-article-buffer)
8992                 (widen)
8993                 (goto-char (if backward (point-max) (point-min))))))))
8994       (gnus-message 7 ""))
8995     ;; Return whether we found the regexp.
8996     (when (eq found 'found)
8997       (goto-char point)
8998       (gnus-summary-show-thread)
8999       (gnus-summary-goto-subject gnus-current-article)
9000       (gnus-summary-position-point)
9001       t)))
9002
9003 (defun gnus-find-matching-articles (header regexp)
9004   "Return a list of all articles that match REGEXP on HEADER.
9005 This search includes all articles in the current group that Gnus has
9006 fetched headers for, whether they are displayed or not."
9007   (let ((articles nil)
9008         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9009         (case-fold-search t))
9010     (dolist (header gnus-newsgroup-headers)
9011       (when (string-match regexp (funcall func header))
9012         (push (mail-header-number header) articles)))
9013     (nreverse articles)))
9014
9015 (defun gnus-summary-find-matching (header regexp &optional backward unread
9016                                           not-case-fold not-matching)
9017   "Return a list of all articles that match REGEXP on HEADER.
9018 The search stars on the current article and goes forwards unless
9019 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9020 If UNREAD is non-nil, only unread articles will
9021 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9022 in the comparisons. If NOT-MATCHING, return a list of all articles that
9023 not match REGEXP on HEADER."
9024   (let ((case-fold-search (not not-case-fold))
9025         articles d func)
9026     (if (consp header)
9027         (if (eq (car header) 'extra)
9028             (setq func
9029                   `(lambda (h)
9030                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9031                          "")))
9032           (error "%s is an invalid header" header))
9033       (unless (fboundp (intern (concat "mail-header-" header)))
9034         (error "%s is not a valid header" header))
9035       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9036     (dolist (d (if (eq backward 'all)
9037                    gnus-newsgroup-data
9038                  (gnus-data-find-list
9039                   (gnus-summary-article-number)
9040                   (gnus-data-list backward))))
9041       (when (and (or (not unread)       ; We want all articles...
9042                      (gnus-data-unread-p d)) ; Or just unreads.
9043                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9044                  (if not-matching
9045                      (not (string-match
9046                            regexp
9047                            (funcall func (gnus-data-header d))))
9048                    (string-match regexp
9049                                  (funcall func (gnus-data-header d)))))
9050         (push (gnus-data-number d) articles))) ; Success!
9051     (nreverse articles)))
9052
9053 (defun gnus-summary-execute-command (header regexp command &optional backward)
9054   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9055 If HEADER is an empty string (or nil), the match is done on the entire
9056 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9057   (interactive
9058    (list (let ((completion-ignore-case t))
9059            (completing-read
9060             "Header name: "
9061             (mapcar (lambda (header) (list (format "%s" header)))
9062                     (append
9063                      '("Number" "Subject" "From" "Lines" "Date"
9064                        "Message-ID" "Xref" "References" "Body")
9065                      gnus-extra-headers))
9066             nil 'require-match))
9067          (read-string "Regexp: ")
9068          (read-key-sequence "Command: ")
9069          current-prefix-arg))
9070   (when (equal header "Body")
9071     (setq header ""))
9072   ;; Hidden thread subtrees must be searched as well.
9073   (gnus-summary-show-all-threads)
9074   ;; We don't want to change current point nor window configuration.
9075   (save-excursion
9076     (save-window-excursion
9077       (let (gnus-visual
9078             gnus-treat-strip-trailing-blank-lines
9079             gnus-treat-strip-leading-blank-lines
9080             gnus-treat-strip-multiple-blank-lines
9081             gnus-treat-hide-boring-headers
9082             gnus-treat-fold-newsgroups
9083             gnus-article-prepare-hook)
9084         (gnus-message 6 "Executing %s..." (key-description command))
9085         ;; We'd like to execute COMMAND interactively so as to give arguments.
9086         (gnus-execute header regexp
9087                       `(call-interactively ',(key-binding command))
9088                       backward)
9089         (gnus-message 6 "Executing %s...done" (key-description command))))))
9090
9091 (defun gnus-summary-beginning-of-article ()
9092   "Scroll the article back to the beginning."
9093   (interactive)
9094   (gnus-summary-select-article)
9095   (gnus-configure-windows 'article)
9096   (gnus-eval-in-buffer-window gnus-article-buffer
9097     (widen)
9098     (goto-char (point-min))
9099     (when gnus-break-pages
9100       (gnus-narrow-to-page))))
9101
9102 (defun gnus-summary-end-of-article ()
9103   "Scroll to the end of the article."
9104   (interactive)
9105   (gnus-summary-select-article)
9106   (gnus-configure-windows 'article)
9107   (gnus-eval-in-buffer-window gnus-article-buffer
9108     (widen)
9109     (goto-char (point-max))
9110     (recenter -3)
9111     (when gnus-break-pages
9112       (when (re-search-backward page-delimiter nil t)
9113         (narrow-to-region (match-end 0) (point-max)))
9114       (gnus-narrow-to-page))))
9115
9116 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9117   "Truncate to LEN and quote all \"(\"'s in STRING."
9118   (gnus-replace-in-string (if (and len (> (length string) len))
9119                               (substring string 0 len)
9120                             string)
9121                           "[()]" "\\\\\\&"))
9122
9123 (defun gnus-summary-print-article (&optional filename n)
9124   "Generate and print a PostScript image of the process-marked (mail) articles.
9125
9126 If used interactively, print the current article if none are
9127 process-marked.  With prefix arg, prompt the user for the name of the
9128 file to save in.
9129
9130 When used from Lisp, accept two optional args FILENAME and N.  N means
9131 to print the next N articles.  If N is negative, print the N previous
9132 articles.  If N is nil and articles have been marked with the process
9133 mark, print these instead.
9134
9135 If the optional first argument FILENAME is nil, send the image to the
9136 printer.  If FILENAME is a string, save the PostScript image in a file with
9137 that name.  If FILENAME is a number, prompt the user for the name of the file
9138 to save in."
9139   (interactive (list (ps-print-preprint current-prefix-arg)))
9140   (dolist (article (gnus-summary-work-articles n))
9141     (gnus-summary-select-article nil nil 'pseudo article)
9142     (gnus-eval-in-buffer-window gnus-article-buffer
9143       (gnus-print-buffer))
9144     (gnus-summary-remove-process-mark article))
9145   (ps-despool filename))
9146
9147 (defun gnus-print-buffer ()
9148   (let ((buffer (generate-new-buffer " *print*")))
9149     (unwind-protect
9150         (progn
9151           (copy-to-buffer buffer (point-min) (point-max))
9152           (set-buffer buffer)
9153           (gnus-remove-text-with-property 'gnus-decoration)
9154           (when (gnus-visual-p 'article-highlight 'highlight)
9155             ;; Copy-to-buffer doesn't copy overlay.  So redo
9156             ;; highlight.
9157             (let ((gnus-article-buffer buffer))
9158               (gnus-article-highlight-citation t)
9159               (gnus-article-highlight-signature)
9160               (gnus-article-emphasize)
9161               (gnus-article-delete-invisible-text)))
9162           (let ((ps-left-header
9163                  (list
9164                   (concat "("
9165                           (gnus-summary-print-truncate-and-quote
9166                            (mail-header-subject gnus-current-headers)
9167                            66) ")")
9168                   (concat "("
9169                           (gnus-summary-print-truncate-and-quote
9170                            (mail-header-from gnus-current-headers)
9171                            45) ")")))
9172                 (ps-right-header
9173                  (list
9174                   "/pagenumberstring load"
9175                   (concat "("
9176                           (mail-header-date gnus-current-headers) ")"))))
9177             (gnus-run-hooks 'gnus-ps-print-hook)
9178             (save-excursion
9179               (if window-system
9180                   (ps-spool-buffer-with-faces)
9181                 (ps-spool-buffer)))))
9182       (kill-buffer buffer))))
9183
9184 (defun gnus-summary-show-article (&optional arg)
9185   "Force redisplaying of the current article.
9186 If ARG (the prefix) is a number, show the article with the charset
9187 defined in `gnus-summary-show-article-charset-alist', or the charset
9188 input.
9189 If ARG (the prefix) is non-nil and not a number, show the raw article
9190 without any article massaging functions being run.  Normally, the key
9191 strokes are `C-u g'."
9192   (interactive "P")
9193   (cond
9194    ((numberp arg)
9195     (gnus-summary-show-article t)
9196     (let ((gnus-newsgroup-charset
9197            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9198                (mm-read-coding-system
9199                 "View as charset: " ;; actually it is coding system.
9200                 (with-current-buffer gnus-article-buffer
9201                   (mm-detect-coding-region (point) (point-max))))))
9202           (gnus-newsgroup-ignored-charsets 'gnus-all))
9203       (gnus-summary-select-article nil 'force)
9204       (let ((deps gnus-newsgroup-dependencies)
9205             head header lines)
9206         (save-excursion
9207           (set-buffer gnus-original-article-buffer)
9208           (save-restriction
9209             (message-narrow-to-head)
9210             (setq head (buffer-string))
9211             (goto-char (point-min))
9212             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9213               (goto-char (point-max))
9214               (widen)
9215               (setq lines (1- (count-lines (point) (point-max))))))
9216           (with-temp-buffer
9217             (insert (format "211 %d Article retrieved.\n"
9218                             (cdr gnus-article-current)))
9219             (insert head)
9220             (if lines (insert (format "Lines: %d\n" lines)))
9221             (insert ".\n")
9222             (let ((nntp-server-buffer (current-buffer)))
9223               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9224         (gnus-data-set-header
9225          (gnus-data-find (cdr gnus-article-current))
9226          header)
9227         (gnus-summary-update-article-line
9228          (cdr gnus-article-current) header)
9229         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9230           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9231    ((not arg)
9232     ;; Select the article the normal way.
9233     (gnus-summary-select-article nil 'force))
9234    (t
9235     ;; We have to require this here to make sure that the following
9236     ;; dynamic binding isn't shadowed by autoloading.
9237     (require 'gnus-async)
9238     (require 'gnus-art)
9239     ;; Bind the article treatment functions to nil.
9240     (let ((gnus-have-all-headers t)
9241           gnus-article-prepare-hook
9242           gnus-article-decode-hook
9243           gnus-display-mime-function
9244           gnus-break-pages)
9245       ;; Destroy any MIME parts.
9246       (when (gnus-buffer-live-p gnus-article-buffer)
9247         (save-excursion
9248           (set-buffer gnus-article-buffer)
9249           (mm-destroy-parts gnus-article-mime-handles)
9250           ;; Set it to nil for safety reason.
9251           (setq gnus-article-mime-handle-alist nil)
9252           (setq gnus-article-mime-handles nil)))
9253       (gnus-summary-select-article nil 'force))))
9254   (gnus-summary-goto-subject gnus-current-article)
9255   (gnus-summary-position-point))
9256
9257 (defun gnus-summary-show-raw-article ()
9258   "Show the raw article without any article massaging functions being run."
9259   (interactive)
9260   (gnus-summary-show-article t))
9261
9262 (defun gnus-summary-verbose-headers (&optional arg)
9263   "Toggle permanent full header display.
9264 If ARG is a positive number, turn header display on.
9265 If ARG is a negative number, turn header display off."
9266   (interactive "P")
9267   (setq gnus-show-all-headers
9268         (cond ((or (not (numberp arg))
9269                    (zerop arg))
9270                (not gnus-show-all-headers))
9271               ((natnump arg)
9272                t)))
9273   (gnus-summary-show-article))
9274
9275 (defun gnus-summary-toggle-header (&optional arg)
9276   "Show the headers if they are hidden, or hide them if they are shown.
9277 If ARG is a positive number, show the entire header.
9278 If ARG is a negative number, hide the unwanted header lines."
9279   (interactive "P")
9280   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9281                      (get-buffer-window gnus-article-buffer t))))
9282     (with-current-buffer gnus-article-buffer
9283       (widen)
9284       (article-narrow-to-head)
9285       (let* ((buffer-read-only nil)
9286              (inhibit-point-motion-hooks t)
9287              (hidden (if (numberp arg)
9288                          (>= arg 0)
9289                        (or (not (looking-at "[^ \t\n]+:"))
9290                            (gnus-article-hidden-text-p 'headers))))
9291              s e)
9292         (delete-region (point-min) (point-max))
9293         (with-current-buffer gnus-original-article-buffer
9294           (goto-char (setq s (point-min)))
9295           (setq e (if (search-forward "\n\n" nil t)
9296                       (1- (point))
9297                     (point-max))))
9298         (insert-buffer-substring gnus-original-article-buffer s e)
9299         (run-hooks 'gnus-article-decode-hook)
9300         (if hidden
9301             (let ((gnus-treat-hide-headers nil)
9302                   (gnus-treat-hide-boring-headers nil))
9303               (gnus-delete-wash-type 'headers)
9304               (gnus-treat-article 'head))
9305           (gnus-treat-article 'head))
9306         (widen)
9307         (if window
9308             (set-window-start window (goto-char (point-min))))
9309         (if gnus-break-pages
9310             (gnus-narrow-to-page)
9311           (when (gnus-visual-p 'page-marker)
9312             (let ((buffer-read-only nil))
9313               (gnus-remove-text-with-property 'gnus-prev)
9314               (gnus-remove-text-with-property 'gnus-next))))
9315         (gnus-set-mode-line 'article)))))
9316
9317 (defun gnus-summary-show-all-headers ()
9318   "Make all header lines visible."
9319   (interactive)
9320   (gnus-summary-toggle-header 1))
9321
9322 (defun gnus-summary-caesar-message (&optional arg)
9323   "Caesar rotate the current article by 13.
9324 With a non-numerical prefix, also rotate headers.  A numerical
9325 prefix specifies how many places to rotate each letter forward."
9326   (interactive "P")
9327   (gnus-summary-select-article)
9328   (let ((mail-header-separator ""))
9329     (gnus-eval-in-buffer-window gnus-article-buffer
9330       (save-restriction
9331         (widen)
9332         (let ((start (window-start))
9333               buffer-read-only)
9334           (if (equal arg '(4))
9335               (message-caesar-buffer-body nil t)
9336             (message-caesar-buffer-body arg))
9337           (set-window-start (get-buffer-window (current-buffer)) start)))))
9338   ;; Create buttons and stuff...
9339   (gnus-treat-article nil))
9340
9341 (defun gnus-summary-idna-message (&optional arg)
9342   "Decode IDNA encoded domain names in the current articles.
9343 IDNA encoded domain names looks like `xn--bar'.  If a string
9344 remain unencoded after running this function, it is likely an
9345 invalid IDNA string (`xn--bar' is invalid).
9346
9347 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9348 installed for this command to work."
9349   (interactive "P")
9350   (if (not (and (condition-case nil (require 'idna)
9351                   (file-error))
9352                 (mm-coding-system-p 'utf-8)
9353                 (executable-find (symbol-value 'idna-program))))
9354       (gnus-message
9355        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9356     (gnus-summary-select-article)
9357     (let ((mail-header-separator ""))
9358       (gnus-eval-in-buffer-window gnus-article-buffer
9359         (save-restriction
9360           (widen)
9361           (let ((start (window-start))
9362                 buffer-read-only)
9363             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9364               (replace-match (idna-to-unicode (match-string 1))))
9365             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9366
9367 (defun gnus-summary-morse-message (&optional arg)
9368   "Morse decode the current article."
9369   (interactive "P")
9370   (gnus-summary-select-article)
9371   (let ((mail-header-separator ""))
9372     (gnus-eval-in-buffer-window gnus-article-buffer
9373       (save-excursion
9374         (save-restriction
9375           (widen)
9376           (let ((pos (window-start))
9377                 buffer-read-only)
9378             (goto-char (point-min))
9379             (when (message-goto-body)
9380               (gnus-narrow-to-body))
9381             (goto-char (point-min))
9382             (while (search-forward "·" (point-max) t)
9383               (replace-match "."))
9384             (unmorse-region (point-min) (point-max))
9385             (widen)
9386             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9387
9388 (defun gnus-summary-stop-page-breaking ()
9389   "Stop page breaking in the current article."
9390   (interactive)
9391   (gnus-summary-select-article)
9392   (gnus-eval-in-buffer-window gnus-article-buffer
9393     (widen)
9394     (when (gnus-visual-p 'page-marker)
9395       (let ((buffer-read-only nil))
9396         (gnus-remove-text-with-property 'gnus-prev)
9397         (gnus-remove-text-with-property 'gnus-next))
9398       (setq gnus-page-broken nil))))
9399
9400 (defun gnus-summary-move-article (&optional n to-newsgroup
9401                                             select-method action)
9402   "Move the current article to a different newsgroup.
9403 If N is a positive number, move the N next articles.
9404 If N is a negative number, move the N previous articles.
9405 If N is nil and any articles have been marked with the process mark,
9406 move those articles instead.
9407 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9408 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9409 re-spool using this method.
9410
9411 When called interactively with TO-NEWSGROUP being nil, the value of
9412 the variable `gnus-move-split-methods' is used for finding a default
9413 for the target newsgroup.
9414
9415 For this function to work, both the current newsgroup and the
9416 newsgroup that you want to move to have to support the `request-move'
9417 and `request-accept' functions.
9418
9419 ACTION can be either `move' (the default), `crosspost' or `copy'."
9420   (interactive "P")
9421   (unless action
9422     (setq action 'move))
9423   ;; Check whether the source group supports the required functions.
9424   (cond ((and (eq action 'move)
9425               (not (gnus-check-backend-function
9426                     'request-move-article gnus-newsgroup-name)))
9427          (error "The current group does not support article moving"))
9428         ((and (eq action 'crosspost)
9429               (not (gnus-check-backend-function
9430                     'request-replace-article gnus-newsgroup-name)))
9431          (error "The current group does not support article editing")))
9432   (let ((articles (gnus-summary-work-articles n))
9433         (prefix (if (gnus-check-backend-function
9434                      'request-move-article gnus-newsgroup-name)
9435                     (funcall gnus-move-group-prefix-function
9436                              gnus-newsgroup-name)
9437                   ""))
9438         (names '((move "Move" "Moving")
9439                  (copy "Copy" "Copying")
9440                  (crosspost "Crosspost" "Crossposting")))
9441         (copy-buf (save-excursion
9442                     (nnheader-set-temp-buffer " *copy article*")))
9443         art-group to-method new-xref article to-groups articles-to-update-marks)
9444     (unless (assq action names)
9445       (error "Unknown action %s" action))
9446     ;; Read the newsgroup name.
9447     (when (and (not to-newsgroup)
9448                (not select-method))
9449       (if (and gnus-move-split-methods
9450                (not
9451                 (and (memq gnus-current-article articles)
9452                      (gnus-buffer-live-p gnus-original-article-buffer))))
9453           ;; When `gnus-move-split-methods' is non-nil, we have to
9454           ;; select an article to give `gnus-read-move-group-name' an
9455           ;; opportunity to suggest an appropriate default.  However,
9456           ;; we needn't render or mark the article.
9457           (let ((gnus-display-mime-function nil)
9458                 (gnus-article-prepare-hook nil)
9459                 (gnus-mark-article-hook nil))
9460             (gnus-summary-select-article nil nil nil (car articles))))
9461       (setq to-newsgroup
9462             (gnus-read-move-group-name
9463              (cadr (assq action names))
9464              (symbol-value (intern (format "gnus-current-%s-group" action)))
9465              articles prefix))
9466       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9467     (setq to-method (or select-method
9468                         (gnus-server-to-method
9469                          (gnus-group-method to-newsgroup))))
9470     ;; Check the method we are to move this article to...
9471     (unless (gnus-check-backend-function
9472              'request-accept-article (car to-method))
9473       (error "%s does not support article copying" (car to-method)))
9474     (unless (gnus-check-server to-method)
9475       (error "Can't open server %s" (car to-method)))
9476     (gnus-message 6 "%s to %s: %s..."
9477                   (caddr (assq action names))
9478                   (or (car select-method) to-newsgroup) articles)
9479     (while articles
9480       (setq article (pop articles))
9481       (setq
9482        art-group
9483        (cond
9484         ;; Move the article.
9485         ((eq action 'move)
9486          ;; Remove this article from future suppression.
9487          (gnus-dup-unsuppress-article article)
9488          (let* ((from-method (gnus-find-method-for-group
9489                               gnus-newsgroup-name))
9490                 (to-method (gnus-find-method-for-group
9491                             to-newsgroup))
9492                 (move-is-internal (gnus-method-equal from-method to-method)))
9493            (gnus-request-move-article
9494             article                     ; Article to move
9495             gnus-newsgroup-name         ; From newsgroup
9496             (nth 1 (gnus-find-method-for-group
9497                     gnus-newsgroup-name)) ; Server
9498             (list 'gnus-request-accept-article
9499                   to-newsgroup (list 'quote select-method)
9500                   (not articles) t)     ; Accept form
9501             (not articles)              ; Only save nov last time
9502             move-is-internal)))         ; is this move internal?
9503         ;; Copy the article.
9504         ((eq action 'copy)
9505          (save-excursion
9506            (set-buffer copy-buf)
9507            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9508              (save-restriction
9509                (nnheader-narrow-to-headers)
9510                (dolist (hdr gnus-copy-article-ignored-headers)
9511                  (message-remove-header hdr t)))
9512              (gnus-request-accept-article
9513               to-newsgroup select-method (not articles) t))))
9514         ;; Crosspost the article.
9515         ((eq action 'crosspost)
9516          (let ((xref (message-tokenize-header
9517                       (mail-header-xref (gnus-summary-article-header article))
9518                       " ")))
9519            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9520                                   ":" (number-to-string article)))
9521            (unless xref
9522              (setq xref (list (system-name))))
9523            (setq new-xref
9524                  (concat
9525                   (mapconcat 'identity
9526                              (delete "Xref:" (delete new-xref xref))
9527                              " ")
9528                   " " new-xref))
9529            (save-excursion
9530              (set-buffer copy-buf)
9531              ;; First put the article in the destination group.
9532              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9533              (when (consp (setq art-group
9534                                 (gnus-request-accept-article
9535                                  to-newsgroup select-method (not articles) t)))
9536                (setq new-xref (concat new-xref " " (car art-group)
9537                                       ":"
9538                                       (number-to-string (cdr art-group))))
9539                ;; Now we have the new Xrefs header, so we insert
9540                ;; it and replace the new article.
9541                (nnheader-replace-header "Xref" new-xref)
9542                (gnus-request-replace-article
9543                 (cdr art-group) to-newsgroup (current-buffer) t)
9544                art-group))))))
9545       (cond
9546        ((not art-group)
9547         (gnus-message 1 "Couldn't %s article %s: %s"
9548                       (cadr (assq action names)) article
9549                       (nnheader-get-report (car to-method))))
9550        ((eq art-group 'junk)
9551         (when (eq action 'move)
9552           (gnus-summary-mark-article article gnus-canceled-mark)
9553           (gnus-message 4 "Deleted article %s" article)
9554           ;; run the delete hook
9555           (run-hook-with-args 'gnus-summary-article-delete-hook
9556                               action
9557                               (gnus-data-header
9558                                (assoc article (gnus-data-list nil)))
9559                               gnus-newsgroup-name nil
9560                               select-method)))
9561        (t
9562         (let* ((pto-group (gnus-group-prefixed-name
9563                            (car art-group) to-method))
9564                (info (gnus-get-info pto-group))
9565                (to-group (gnus-info-group info))
9566                to-marks)
9567           ;; Update the group that has been moved to.
9568           (when (and info
9569                      (memq action '(move copy)))
9570             (unless (member to-group to-groups)
9571               (push to-group to-groups))
9572
9573             (unless (memq article gnus-newsgroup-unreads)
9574               (push 'read to-marks)
9575               (gnus-info-set-read
9576                info (gnus-add-to-range (gnus-info-read info)
9577                                        (list (cdr art-group)))))
9578
9579             ;; See whether the article is to be put in the cache.
9580             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9581                              gnus-article-mark-lists
9582                            (delete '(expirable . expire)
9583                                    (copy-sequence gnus-article-mark-lists))))
9584                   (to-article (cdr art-group)))
9585
9586               ;; Enter the article into the cache in the new group,
9587               ;; if that is required.
9588               (when gnus-use-cache
9589                 (gnus-cache-possibly-enter-article
9590                  to-group to-article
9591                  (memq article gnus-newsgroup-marked)
9592                  (memq article gnus-newsgroup-dormant)
9593                  (memq article gnus-newsgroup-unreads)))
9594
9595               (when gnus-preserve-marks
9596                 ;; Copy any marks over to the new group.
9597                 (when (and (equal to-group gnus-newsgroup-name)
9598                            (not (memq article gnus-newsgroup-unreads)))
9599                   ;; Mark this article as read in this group.
9600                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9601                   (setcdr (gnus-active to-group) to-article)
9602                   (setcdr gnus-newsgroup-active to-article))
9603
9604                 (while marks
9605                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9606                     (when (memq article (symbol-value
9607                                          (intern (format "gnus-newsgroup-%s"
9608                                                          (caar marks)))))
9609                       (push (cdar marks) to-marks)
9610                       ;; If the other group is the same as this group,
9611                       ;; then we have to add the mark to the list.
9612                       (when (equal to-group gnus-newsgroup-name)
9613                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9614                              (cons to-article
9615                                    (symbol-value
9616                                     (intern (format "gnus-newsgroup-%s"
9617                                                     (caar marks)))))))
9618                       ;; Copy the marks to other group.
9619                       (gnus-add-marked-articles
9620                        to-group (cdar marks) (list to-article) info)))
9621                   (setq marks (cdr marks)))
9622
9623                 (gnus-request-set-mark
9624                  to-group (list (list (list to-article) 'add to-marks))))
9625
9626               (gnus-dribble-enter
9627                (concat "(gnus-group-set-info '"
9628                        (gnus-prin1-to-string (gnus-get-info to-group))
9629                        ")"))))
9630
9631           ;; Update the Xref header in this article to point to
9632           ;; the new crossposted article we have just created.
9633           (when (eq action 'crosspost)
9634             (save-excursion
9635               (set-buffer copy-buf)
9636               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9637               (nnheader-replace-header "Xref" new-xref)
9638               (gnus-request-replace-article
9639                article gnus-newsgroup-name (current-buffer) t)))
9640
9641           ;; run the move/copy/crosspost/respool hook
9642           (run-hook-with-args 'gnus-summary-article-move-hook
9643                               action
9644                               (gnus-data-header
9645                                (assoc article (gnus-data-list nil)))
9646                               gnus-newsgroup-name
9647                               to-newsgroup
9648                               select-method))
9649
9650         ;;;!!!Why is this necessary?
9651         (set-buffer gnus-summary-buffer)
9652
9653         (gnus-summary-goto-subject article)
9654         (when (eq action 'move)
9655           (gnus-summary-mark-article article gnus-canceled-mark))))
9656       (push article articles-to-update-marks))
9657
9658     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9659     ;; Re-activate all groups that have been moved to.
9660     (save-excursion
9661       (set-buffer gnus-group-buffer)
9662       (let ((gnus-group-marked to-groups))
9663         (gnus-group-get-new-news-this-group nil t)))
9664
9665     (gnus-kill-buffer copy-buf)
9666     (gnus-summary-position-point)
9667     (gnus-set-mode-line 'summary)))
9668
9669 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9670   "Copy the current article to some other group.
9671 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9672 When called interactively, if TO-NEWSGROUP is nil, use the value of
9673 the variable `gnus-move-split-methods' for finding a default target
9674 newsgroup.
9675 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9676 re-spool using this method."
9677   (interactive "P")
9678   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9679
9680 (defun gnus-summary-crosspost-article (&optional n)
9681   "Crosspost the current article to some other group."
9682   (interactive "P")
9683   (gnus-summary-move-article n nil nil 'crosspost))
9684
9685 (defcustom gnus-summary-respool-default-method nil
9686   "Default method type for respooling an article.
9687 If nil, use to the current newsgroup method."
9688   :type 'symbol
9689   :group 'gnus-summary-mail)
9690
9691 (defcustom gnus-summary-display-while-building nil
9692   "If non-nil, show and update the summary buffer as it's being built.
9693 If the value is t, update the buffer after every line is inserted.  If
9694 the value is an integer (N), update the display every N lines."
9695   :version "22.1"
9696   :group 'gnus-thread
9697   :type '(choice (const :tag "off" nil)
9698                  number
9699                  (const :tag "frequently" t)))
9700
9701 (defun gnus-summary-respool-article (&optional n method)
9702   "Respool the current article.
9703 The article will be squeezed through the mail spooling process again,
9704 which means that it will be put in some mail newsgroup or other
9705 depending on `nnmail-split-methods'.
9706 If N is a positive number, respool the N next articles.
9707 If N is a negative number, respool the N previous articles.
9708 If N is nil and any articles have been marked with the process mark,
9709 respool those articles instead.
9710
9711 Respooling can be done both from mail groups and \"real\" newsgroups.
9712 In the former case, the articles in question will be moved from the
9713 current group into whatever groups they are destined to.  In the
9714 latter case, they will be copied into the relevant groups."
9715   (interactive
9716    (list current-prefix-arg
9717          (let* ((methods (gnus-methods-using 'respool))
9718                 (methname
9719                  (symbol-name (or gnus-summary-respool-default-method
9720                                   (car (gnus-find-method-for-group
9721                                         gnus-newsgroup-name)))))
9722                 (method
9723                  (gnus-completing-read-with-default
9724                   methname "Backend to use when respooling"
9725                   methods nil t nil 'gnus-mail-method-history))
9726                 ms)
9727            (cond
9728             ((zerop (length (setq ms (gnus-servers-using-backend
9729                                       (intern method)))))
9730              (list (intern method) ""))
9731             ((= 1 (length ms))
9732              (car ms))
9733             (t
9734              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9735                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9736                            ms-alist))))))))
9737   (unless method
9738     (error "No method given for respooling"))
9739   (if (assoc (symbol-name
9740               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9741              (gnus-methods-using 'respool))
9742       (gnus-summary-move-article n nil method)
9743     (gnus-summary-copy-article n nil method)))
9744
9745 (defun gnus-summary-import-article (file &optional edit)
9746   "Import an arbitrary file into a mail newsgroup."
9747   (interactive "fImport file: \nP")
9748   (let ((group gnus-newsgroup-name)
9749         (now (current-time))
9750         atts lines group-art)
9751     (unless (gnus-check-backend-function 'request-accept-article group)
9752       (error "%s does not support article importing" group))
9753     (or (file-readable-p file)
9754         (not (file-regular-p file))
9755         (error "Can't read %s" file))
9756     (save-excursion
9757       (set-buffer (gnus-get-buffer-create " *import file*"))
9758       (erase-buffer)
9759       (nnheader-insert-file-contents file)
9760       (goto-char (point-min))
9761       (if (nnheader-article-p)
9762           (save-restriction
9763             (goto-char (point-min))
9764             (search-forward "\n\n" nil t)
9765             (narrow-to-region (point-min) (1- (point)))
9766             (goto-char (point-min))
9767             (unless (re-search-forward "^date:" nil t)
9768               (goto-char (point-max))
9769               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9770        ;; This doesn't look like an article, so we fudge some headers.
9771         (setq atts (file-attributes file)
9772               lines (count-lines (point-min) (point-max)))
9773         (insert "From: " (read-string "From: ") "\n"
9774                 "Subject: " (read-string "Subject: ") "\n"
9775                 "Date: " (message-make-date (nth 5 atts)) "\n"
9776                 "Message-ID: " (message-make-message-id) "\n"
9777                 "Lines: " (int-to-string lines) "\n"
9778                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9779       (setq group-art (gnus-request-accept-article group nil t))
9780       (kill-buffer (current-buffer)))
9781     (setq gnus-newsgroup-active (gnus-activate-group group))
9782     (forward-line 1)
9783     (gnus-summary-goto-article (cdr group-art) nil t)
9784     (when edit
9785       (gnus-summary-edit-article))))
9786
9787 (defun gnus-summary-create-article ()
9788   "Create an article in a mail newsgroup."
9789   (interactive)
9790   (let ((group gnus-newsgroup-name)
9791         (now (current-time))
9792         group-art)
9793     (unless (gnus-check-backend-function 'request-accept-article group)
9794       (error "%s does not support article importing" group))
9795     (save-excursion
9796       (set-buffer (gnus-get-buffer-create " *import file*"))
9797       (erase-buffer)
9798       (goto-char (point-min))
9799       ;; This doesn't look like an article, so we fudge some headers.
9800       (insert "From: " (read-string "From: ") "\n"
9801               "Subject: " (read-string "Subject: ") "\n"
9802               "Date: " (message-make-date now) "\n"
9803               "Message-ID: " (message-make-message-id) "\n")
9804       (setq group-art (gnus-request-accept-article group nil t))
9805       (kill-buffer (current-buffer)))
9806     (setq gnus-newsgroup-active (gnus-activate-group group))
9807     (forward-line 1)
9808     (gnus-summary-goto-article (cdr group-art) nil t)
9809     (gnus-summary-edit-article)))
9810
9811 (defun gnus-summary-article-posted-p ()
9812   "Say whether the current (mail) article is available from news as well.
9813 This will be the case if the article has both been mailed and posted."
9814   (interactive)
9815   (let ((id (mail-header-references (gnus-summary-article-header)))
9816         (gnus-override-method (car (gnus-refer-article-methods))))
9817     (if (gnus-request-head id "")
9818         (gnus-message 2 "The current message was found on %s"
9819                       gnus-override-method)
9820       (gnus-message 2 "The current message couldn't be found on %s"
9821                     gnus-override-method)
9822       nil)))
9823
9824 (defun gnus-summary-expire-articles (&optional now)
9825   "Expire all articles that are marked as expirable in the current group."
9826   (interactive)
9827   (when (and (not gnus-group-is-exiting-without-update-p)
9828              (gnus-check-backend-function
9829               'request-expire-articles gnus-newsgroup-name))
9830     ;; This backend supports expiry.
9831     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9832            (expirable (if total
9833                           (progn
9834                             ;; We need to update the info for
9835                             ;; this group for `gnus-list-of-read-articles'
9836                             ;; to give us the right answer.
9837                             (gnus-run-hooks 'gnus-exit-group-hook)
9838                             (gnus-summary-update-info)
9839                             (gnus-list-of-read-articles gnus-newsgroup-name))
9840                         (setq gnus-newsgroup-expirable
9841                               (sort gnus-newsgroup-expirable '<))))
9842            (expiry-wait (if now 'immediate
9843                           (gnus-group-find-parameter
9844                            gnus-newsgroup-name 'expiry-wait)))
9845            (nnmail-expiry-target
9846             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9847                 nnmail-expiry-target))
9848            es)
9849       (when expirable
9850         ;; There are expirable articles in this group, so we run them
9851         ;; through the expiry process.
9852         (gnus-message 6 "Expiring articles...")
9853         (unless (gnus-check-group gnus-newsgroup-name)
9854           (error "Can't open server for %s" gnus-newsgroup-name))
9855         ;; The list of articles that weren't expired is returned.
9856         (save-excursion
9857           (if expiry-wait
9858               (let ((nnmail-expiry-wait-function nil)
9859                     (nnmail-expiry-wait expiry-wait))
9860                 (setq es (gnus-request-expire-articles
9861                           expirable gnus-newsgroup-name)))
9862             (setq es (gnus-request-expire-articles
9863                       expirable gnus-newsgroup-name)))
9864           (unless total
9865             (setq gnus-newsgroup-expirable es))
9866           ;; We go through the old list of expirable, and mark all
9867           ;; really expired articles as nonexistent.
9868           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9869             (let ((gnus-use-cache nil))
9870               (dolist (article expirable)
9871                 (when (and (not (memq article es))
9872                            (gnus-data-find article))
9873                   (gnus-summary-mark-article article gnus-canceled-mark)
9874                   (run-hook-with-args 'gnus-summary-article-expire-hook
9875                                       'delete
9876                                       (gnus-data-header
9877                                        (assoc article (gnus-data-list nil)))
9878                                       gnus-newsgroup-name
9879                                       nil
9880                                       nil))))))
9881         (gnus-message 6 "Expiring articles...done")))))
9882
9883 (defun gnus-summary-expire-articles-now ()
9884   "Expunge all expirable articles in the current group.
9885 This means that *all* articles that are marked as expirable will be
9886 deleted forever, right now."
9887   (interactive)
9888   (or gnus-expert-user
9889       (gnus-yes-or-no-p
9890        "Are you really, really sure you want to delete all expirable messages? ")
9891       (error "Phew!"))
9892   (gnus-summary-expire-articles t))
9893
9894 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9895 (defun gnus-summary-delete-article (&optional n)
9896   "Delete the N next (mail) articles.
9897 This command actually deletes articles.  This is not a marking
9898 command.  The article will disappear forever from your life, never to
9899 return.
9900
9901 If N is negative, delete backwards.
9902 If N is nil and articles have been marked with the process mark,
9903 delete these instead.
9904
9905 If `gnus-novice-user' is non-nil you will be asked for
9906 confirmation before the articles are deleted."
9907   (interactive "P")
9908   (unless (gnus-check-backend-function 'request-expire-articles
9909                                        gnus-newsgroup-name)
9910     (error "The current newsgroup does not support article deletion"))
9911   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9912     (error "Couldn't open server"))
9913   ;; Compute the list of articles to delete.
9914   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9915         (nnmail-expiry-target 'delete)
9916         not-deleted)
9917     (if (and gnus-novice-user
9918              (not (gnus-yes-or-no-p
9919                    (format "Do you really want to delete %s forever? "
9920                            (if (> (length articles) 1)
9921                                (format "these %s articles" (length articles))
9922                              "this article")))))
9923         ()
9924       ;; Delete the articles.
9925       (setq not-deleted (gnus-request-expire-articles
9926                          articles gnus-newsgroup-name 'force))
9927       (while articles
9928         (gnus-summary-remove-process-mark (car articles))
9929         ;; The backend might not have been able to delete the article
9930         ;; after all.
9931         (unless (memq (car articles) not-deleted)
9932           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9933         (let* ((article (car articles))
9934                (ghead  (gnus-data-header
9935                                     (assoc article (gnus-data-list nil)))))
9936           (run-hook-with-args 'gnus-summary-article-delete-hook
9937                               'delete ghead gnus-newsgroup-name nil
9938                               nil))
9939         (setq articles (cdr articles)))
9940       (when not-deleted
9941         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9942     (gnus-summary-position-point)
9943     (gnus-set-mode-line 'summary)
9944     not-deleted))
9945
9946 (defun gnus-summary-edit-article (&optional arg)
9947   "Edit the current article.
9948 This will have permanent effect only in mail groups.
9949 If ARG is nil, edit the decoded articles.
9950 If ARG is 1, edit the raw articles.
9951 If ARG is 2, edit the raw articles even in read-only groups.
9952 If ARG is 3, edit the articles with the current handles.
9953 Otherwise, allow editing of articles even in read-only
9954 groups."
9955   (interactive "P")
9956   (let (force raw current-handles)
9957     (cond
9958      ((null arg))
9959      ((eq arg 1)
9960       (setq raw t))
9961      ((eq arg 2)
9962       (setq raw t
9963             force t))
9964      ((eq arg 3)
9965       (setq current-handles
9966             (and (gnus-buffer-live-p gnus-article-buffer)
9967                  (with-current-buffer gnus-article-buffer
9968                    (prog1
9969                        gnus-article-mime-handles
9970                      (setq gnus-article-mime-handles nil))))))
9971      (t
9972       (setq force t)))
9973     (when (and raw (not force)
9974                (member gnus-newsgroup-name '("nndraft:delayed"
9975                                              "nndraft:drafts"
9976                                              "nndraft:queue")))
9977       (error "Can't edit the raw article in group %s"
9978              gnus-newsgroup-name))
9979     (save-excursion
9980       (set-buffer gnus-summary-buffer)
9981       (let ((mail-parse-charset gnus-newsgroup-charset)
9982             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9983         (gnus-set-global-variables)
9984         (when (and (not force)
9985                    (gnus-group-read-only-p))
9986           (error "The current newsgroup does not support article editing"))
9987         (gnus-summary-show-article t)
9988         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9989           (with-current-buffer gnus-article-buffer
9990             (mm-enable-multibyte)))
9991         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9992             (setq raw t))
9993         (gnus-article-edit-article
9994          (if raw 'ignore
9995            `(lambda ()
9996               (let ((mbl mml-buffer-list))
9997                 (setq mml-buffer-list nil)
9998                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9999                   (mime-to-mml ,'current-handles))
10000                 (let ((mbl1 mml-buffer-list))
10001                   (setq mml-buffer-list mbl)
10002                   (set (make-local-variable 'mml-buffer-list) mbl1))
10003                 (gnus-make-local-hook 'kill-buffer-hook)
10004                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10005          `(lambda (no-highlight)
10006             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10007                   (message-options message-options)
10008                   (message-options-set-recipient)
10009                   (mail-parse-ignored-charsets
10010                    ',gnus-newsgroup-ignored-charsets))
10011               ,(if (not raw) '(progn
10012                                 (mml-to-mime)
10013                                 (mml-destroy-buffers)
10014                                 (remove-hook 'kill-buffer-hook
10015                                              'mml-destroy-buffers t)
10016                                 (kill-local-variable 'mml-buffer-list)))
10017               (gnus-summary-edit-article-done
10018                ,(or (mail-header-references gnus-current-headers) "")
10019                ,(gnus-group-read-only-p)
10020                ,gnus-summary-buffer no-highlight))))))))
10021
10022 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10023
10024 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10025                                                  no-highlight)
10026   "Make edits to the current article permanent."
10027   (interactive)
10028   (save-excursion
10029    ;; The buffer restriction contains the entire article if it exists.
10030     (when (article-goto-body)
10031       (let ((lines (count-lines (point) (point-max)))
10032             (length (- (point-max) (point)))
10033             (case-fold-search t)
10034             (body (copy-marker (point))))
10035         (goto-char (point-min))
10036         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10037           (delete-region (match-beginning 1) (match-end 1))
10038           (insert (number-to-string length)))
10039         (goto-char (point-min))
10040         (when (re-search-forward
10041                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10042           (delete-region (match-beginning 1) (match-end 1))
10043           (insert (number-to-string length)))
10044         (goto-char (point-min))
10045         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10046           (delete-region (match-beginning 1) (match-end 1))
10047           (insert (number-to-string lines))))))
10048   ;; Replace the article.
10049   (let ((buf (current-buffer)))
10050     (with-temp-buffer
10051       (insert-buffer-substring buf)
10052
10053       (if (and (not read-only)
10054                (not (gnus-request-replace-article
10055                      (cdr gnus-article-current) (car gnus-article-current)
10056                      (current-buffer) t)))
10057           (error "Couldn't replace article")
10058         ;; Update the summary buffer.
10059         (if (and references
10060                  (equal (message-tokenize-header references " ")
10061                         (message-tokenize-header
10062                          (or (message-fetch-field "references") "") " ")))
10063             ;; We only have to update this line.
10064             (save-excursion
10065               (save-restriction
10066                 (message-narrow-to-head)
10067                 (let ((head (buffer-substring-no-properties
10068                              (point-min) (point-max)))
10069                       header)
10070                   (with-temp-buffer
10071                     (insert (format "211 %d Article retrieved.\n"
10072                                     (cdr gnus-article-current)))
10073                     (insert head)
10074                     (insert ".\n")
10075                     (let ((nntp-server-buffer (current-buffer)))
10076                       (setq header (car (gnus-get-newsgroup-headers
10077                                          nil t))))
10078                     (save-excursion
10079                       (set-buffer gnus-summary-buffer)
10080                       (gnus-data-set-header
10081                        (gnus-data-find (cdr gnus-article-current))
10082                        header)
10083                       (gnus-summary-update-article-line
10084                        (cdr gnus-article-current) header)
10085                       (if (gnus-summary-goto-subject
10086                            (cdr gnus-article-current) nil t)
10087                           (gnus-summary-update-secondary-mark
10088                            (cdr gnus-article-current))))))))
10089           ;; Update threads.
10090           (set-buffer (or buffer gnus-summary-buffer))
10091           (gnus-summary-update-article (cdr gnus-article-current))
10092           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10093               (gnus-summary-update-secondary-mark
10094                (cdr gnus-article-current))))
10095         ;; Prettify the article buffer again.
10096         (unless no-highlight
10097           (save-excursion
10098             (set-buffer gnus-article-buffer)
10099             ;;;!!! Fix this -- article should be rehighlighted.
10100             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10101             (set-buffer gnus-original-article-buffer)
10102             (gnus-request-article
10103              (cdr gnus-article-current)
10104              (car gnus-article-current) (current-buffer))))
10105         ;; Prettify the summary buffer line.
10106         (when (gnus-visual-p 'summary-highlight 'highlight)
10107           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10108
10109 (defun gnus-summary-edit-wash (key)
10110   "Perform editing command KEY in the article buffer."
10111   (interactive
10112    (list
10113     (progn
10114       (message "%s" (concat (this-command-keys) "- "))
10115       (read-char))))
10116   (message "")
10117   (gnus-summary-edit-article)
10118   (execute-kbd-macro (concat (this-command-keys) key))
10119   (gnus-article-edit-done))
10120
10121 ;;; Respooling
10122
10123 (defun gnus-summary-respool-query (&optional silent trace)
10124   "Query where the respool algorithm would put this article."
10125   (interactive)
10126   (let (gnus-mark-article-hook)
10127     (gnus-summary-select-article)
10128     (save-excursion
10129       (set-buffer gnus-original-article-buffer)
10130       (let ((groups (nnmail-article-group 'identity trace)))
10131         (unless silent
10132           (if groups
10133               (message "This message would go to %s"
10134                        (mapconcat 'car groups ", "))
10135             (message "This message would go to no groups"))
10136           groups)))))
10137
10138 (defun gnus-summary-respool-trace ()
10139   "Trace where the respool algorithm would put this article.
10140 Display a buffer showing all fancy splitting patterns which matched."
10141   (interactive)
10142   (gnus-summary-respool-query nil t))
10143
10144 ;; Summary marking commands.
10145
10146 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10147   "Mark articles which has the same subject as read, and then select the next.
10148 If UNMARK is positive, remove any kind of mark.
10149 If UNMARK is negative, tick articles."
10150   (interactive "P")
10151   (when unmark
10152     (setq unmark (prefix-numeric-value unmark)))
10153   (let ((count
10154          (gnus-summary-mark-same-subject
10155           (gnus-summary-article-subject) unmark)))
10156     ;; Select next unread article.  If auto-select-same mode, should
10157     ;; select the first unread article.
10158     (gnus-summary-next-article t (and gnus-auto-select-same
10159                                       (gnus-summary-article-subject)))
10160     (gnus-message 7 "%d article%s marked as %s"
10161                   count (if (= count 1) " is" "s are")
10162                   (if unmark "unread" "read"))))
10163
10164 (defun gnus-summary-kill-same-subject (&optional unmark)
10165   "Mark articles which has the same subject as read.
10166 If UNMARK is positive, remove any kind of mark.
10167 If UNMARK is negative, tick articles."
10168   (interactive "P")
10169   (when unmark
10170     (setq unmark (prefix-numeric-value unmark)))
10171   (let ((count
10172          (gnus-summary-mark-same-subject
10173           (gnus-summary-article-subject) unmark)))
10174     ;; If marked as read, go to next unread subject.
10175     (when (null unmark)
10176       ;; Go to next unread subject.
10177       (gnus-summary-next-subject 1 t))
10178     (gnus-message 7 "%d articles are marked as %s"
10179                   count (if unmark "unread" "read"))))
10180
10181 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10182   "Mark articles with same SUBJECT as read, and return marked number.
10183 If optional argument UNMARK is positive, remove any kinds of marks.
10184 If optional argument UNMARK is negative, mark articles as unread instead."
10185   (let ((count 1))
10186     (save-excursion
10187       (cond
10188        ((null unmark)                   ; Mark as read.
10189         (while (and
10190                 (progn
10191                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10192                   (gnus-summary-show-thread) t)
10193                 (gnus-summary-find-subject subject))
10194           (setq count (1+ count))))
10195        ((> unmark 0)                    ; Tick.
10196         (while (and
10197                 (progn
10198                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10199                   (gnus-summary-show-thread) t)
10200                 (gnus-summary-find-subject subject))
10201           (setq count (1+ count))))
10202        (t                               ; Mark as unread.
10203         (while (and
10204                 (progn
10205                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10206                   (gnus-summary-show-thread) t)
10207                 (gnus-summary-find-subject subject))
10208           (setq count (1+ count)))))
10209       (gnus-set-mode-line 'summary)
10210       ;; Return the number of marked articles.
10211       count)))
10212
10213 (defun gnus-summary-mark-as-processable (n &optional unmark)
10214   "Set the process mark on the next N articles.
10215 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10216 the process mark instead.  The difference between N and the actual
10217 number of articles marked is returned."
10218   (interactive "P")
10219   (if (and (null n) (gnus-region-active-p))
10220       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10221     (setq n (prefix-numeric-value n))
10222     (let ((backward (< n 0))
10223           (n (abs n)))
10224       (while (and
10225               (> n 0)
10226               (if unmark
10227                   (gnus-summary-remove-process-mark
10228                    (gnus-summary-article-number))
10229                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10230               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10231         (setq n (1- n)))
10232       (when (/= 0 n)
10233         (gnus-message 7 "No more articles"))
10234       (gnus-summary-recenter)
10235       (gnus-summary-position-point)
10236       n)))
10237
10238 (defun gnus-summary-unmark-as-processable (n)
10239   "Remove the process mark from the next N articles.
10240 If N is negative, unmark backward instead.  The difference between N and
10241 the actual number of articles unmarked is returned."
10242   (interactive "P")
10243   (gnus-summary-mark-as-processable n t))
10244
10245 (defun gnus-summary-unmark-all-processable ()
10246   "Remove the process mark from all articles."
10247   (interactive)
10248   (save-excursion
10249     (while gnus-newsgroup-processable
10250       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10251   (gnus-summary-position-point))
10252
10253 (defun gnus-summary-add-mark (article type)
10254   "Mark ARTICLE with a mark of TYPE."
10255   (let ((vtype (car (assq type gnus-article-mark-lists)))
10256         var)
10257     (if (not vtype)
10258         (error "No such mark type: %s" type)
10259       (setq var (intern (format "gnus-newsgroup-%s" type)))
10260       (set var (cons article (symbol-value var)))
10261       (if (memq type '(processable cached replied forwarded recent saved))
10262           (gnus-summary-update-secondary-mark article)
10263         ;;; !!! This is bogus.  We should find out what primary
10264         ;;; !!! mark we want to set.
10265         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10266
10267 (defun gnus-summary-mark-as-expirable (n)
10268   "Mark N articles forward as expirable.
10269 If N is negative, mark backward instead.  The difference between N and
10270 the actual number of articles marked is returned."
10271   (interactive "p")
10272   (gnus-summary-mark-forward n gnus-expirable-mark))
10273
10274 (defun gnus-summary-mark-as-spam (n)
10275   "Mark N articles forward as spam.
10276 If N is negative, mark backward instead.  The difference between N and
10277 the actual number of articles marked is returned."
10278   (interactive "p")
10279   (gnus-summary-mark-forward n gnus-spam-mark))
10280
10281 (defun gnus-summary-mark-article-as-replied (article)
10282   "Mark ARTICLE as replied to and update the summary line.
10283 ARTICLE can also be a list of articles."
10284   (interactive (list (gnus-summary-article-number)))
10285   (let ((articles (if (listp article) article (list article))))
10286     (dolist (article articles)
10287       (unless (numberp article)
10288         (error "%s is not a number" article))
10289       (push article gnus-newsgroup-replied)
10290       (let ((buffer-read-only nil))
10291         (when (gnus-summary-goto-subject article nil t)
10292           (gnus-summary-update-secondary-mark article))))))
10293
10294 (defun gnus-summary-mark-article-as-forwarded (article)
10295   "Mark ARTICLE as forwarded and update the summary line.
10296 ARTICLE can also be a list of articles."
10297   (let ((articles (if (listp article) article (list article))))
10298     (dolist (article articles)
10299       (push article gnus-newsgroup-forwarded)
10300       (let ((buffer-read-only nil))
10301         (when (gnus-summary-goto-subject article nil t)
10302           (gnus-summary-update-secondary-mark article))))))
10303
10304 (defun gnus-summary-set-bookmark (article)
10305   "Set a bookmark in current article."
10306   (interactive (list (gnus-summary-article-number)))
10307   (when (or (not (get-buffer gnus-article-buffer))
10308             (not gnus-current-article)
10309             (not gnus-article-current)
10310             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10311     (error "No current article selected"))
10312   ;; Remove old bookmark, if one exists.
10313   (gnus-pull article gnus-newsgroup-bookmarks)
10314   ;; Set the new bookmark, which is on the form
10315   ;; (article-number . line-number-in-body).
10316   (push
10317    (cons article
10318          (with-current-buffer gnus-article-buffer
10319            (count-lines
10320             (min (point)
10321                  (save-excursion
10322                    (article-goto-body)
10323                    (point)))
10324             (point))))
10325    gnus-newsgroup-bookmarks)
10326   (gnus-message 6 "A bookmark has been added to the current article."))
10327
10328 (defun gnus-summary-remove-bookmark (article)
10329   "Remove the bookmark from the current article."
10330   (interactive (list (gnus-summary-article-number)))
10331   ;; Remove old bookmark, if one exists.
10332   (if (not (assq article gnus-newsgroup-bookmarks))
10333       (gnus-message 6 "No bookmark in current article.")
10334     (gnus-pull article gnus-newsgroup-bookmarks)
10335     (gnus-message 6 "Removed bookmark.")))
10336
10337 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10338 (defun gnus-summary-mark-as-dormant (n)
10339   "Mark N articles forward as dormant.
10340 If N is negative, mark backward instead.  The difference between N and
10341 the actual number of articles marked is returned."
10342   (interactive "p")
10343   (gnus-summary-mark-forward n gnus-dormant-mark))
10344
10345 (defun gnus-summary-set-process-mark (article)
10346   "Set the process mark on ARTICLE and update the summary line."
10347   (setq gnus-newsgroup-processable
10348         (cons article
10349               (delq article gnus-newsgroup-processable)))
10350   (when (gnus-summary-goto-subject article)
10351     (gnus-summary-show-thread)
10352     (gnus-summary-goto-subject article)
10353     (gnus-summary-update-secondary-mark article)))
10354
10355 (defun gnus-summary-remove-process-mark (&rest articles)
10356   "Remove the process mark from ARTICLES and update the summary line."
10357   (dolist (article articles)
10358     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10359     (when (gnus-summary-goto-subject article)
10360       (gnus-summary-show-thread)
10361       (gnus-summary-goto-subject article)
10362       (gnus-summary-update-secondary-mark article)))
10363   t)
10364
10365 (defun gnus-summary-set-saved-mark (article)
10366   "Set the process mark on ARTICLE and update the summary line."
10367   (push article gnus-newsgroup-saved)
10368   (when (gnus-summary-goto-subject article)
10369     (gnus-summary-update-secondary-mark article)))
10370
10371 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10372   "Mark N articles as read forwards.
10373 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10374 The difference between N and the actual number of articles marked is
10375 returned.
10376 If NO-EXPIRE, auto-expiry will be inhibited."
10377   (interactive "p")
10378   (gnus-summary-show-thread)
10379   (let ((backward (< n 0))
10380         (gnus-summary-goto-unread
10381          (and gnus-summary-goto-unread
10382               (not (eq gnus-summary-goto-unread 'never))
10383               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10384                                     gnus-ticked-mark gnus-dormant-mark)))))
10385         (n (abs n))
10386         (mark (or mark gnus-del-mark)))
10387     (while (and (> n 0)
10388                 (gnus-summary-mark-article nil mark no-expire)
10389                 (zerop (gnus-summary-next-subject
10390                         (if backward -1 1)
10391                         (and gnus-summary-goto-unread
10392                              (not (eq gnus-summary-goto-unread 'never)))
10393                         t)))
10394       (setq n (1- n)))
10395     (when (/= 0 n)
10396       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10397     (gnus-summary-recenter)
10398     (gnus-summary-position-point)
10399     (gnus-set-mode-line 'summary)
10400     n))
10401
10402 (defun gnus-summary-mark-article-as-read (mark)
10403   "Mark the current article quickly as read with MARK."
10404   (let ((article (gnus-summary-article-number)))
10405     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10406     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10407     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10408     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10409     (push (cons article mark) gnus-newsgroup-reads)
10410     ;; Possibly remove from cache, if that is used.
10411     (when gnus-use-cache
10412       (gnus-cache-enter-remove-article article))
10413     ;; Allow the backend to change the mark.
10414     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10415     ;; Check for auto-expiry.
10416     (when (and gnus-newsgroup-auto-expire
10417                (memq mark gnus-auto-expirable-marks))
10418       (setq mark gnus-expirable-mark)
10419       ;; Let the backend know about the mark change.
10420       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10421       (push article gnus-newsgroup-expirable))
10422     ;; Set the mark in the buffer.
10423     (gnus-summary-update-mark mark 'unread)
10424     t))
10425
10426 (defun gnus-summary-mark-article-as-unread (mark)
10427   "Mark the current article quickly as unread with MARK."
10428   (let* ((article (gnus-summary-article-number))
10429          (old-mark (gnus-summary-article-mark article)))
10430     ;; Allow the backend to change the mark.
10431     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10432     (if (eq mark old-mark)
10433         t
10434       (if (<= article 0)
10435           (progn
10436             (gnus-error 1 "Can't mark negative article numbers")
10437             nil)
10438         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10439         (setq gnus-newsgroup-spam-marked
10440               (delq article gnus-newsgroup-spam-marked))
10441         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10442         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10443         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10444         (cond ((= mark gnus-ticked-mark)
10445                (setq gnus-newsgroup-marked
10446                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10447                                               article)))
10448               ((= mark gnus-spam-mark)
10449                (setq gnus-newsgroup-spam-marked
10450                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10451                                               article)))
10452               ((= mark gnus-dormant-mark)
10453                (setq gnus-newsgroup-dormant
10454                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10455                                               article)))
10456               (t
10457                (setq gnus-newsgroup-unreads
10458                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10459                                               article))))
10460         (gnus-pull article gnus-newsgroup-reads)
10461
10462         ;; See whether the article is to be put in the cache.
10463         (and gnus-use-cache
10464              (vectorp (gnus-summary-article-header article))
10465              (save-excursion
10466                (gnus-cache-possibly-enter-article
10467                 gnus-newsgroup-name article
10468                 (= mark gnus-ticked-mark)
10469                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10470
10471         ;; Fix the mark.
10472         (gnus-summary-update-mark mark 'unread)
10473         t))))
10474
10475 (defun gnus-summary-mark-article (&optional article mark no-expire)
10476   "Mark ARTICLE with MARK.  MARK can be any character.
10477 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10478 `??' (dormant) and `?E' (expirable).
10479 If MARK is nil, then the default character `?r' is used.
10480 If ARTICLE is nil, then the article on the current line will be
10481 marked.
10482 If NO-EXPIRE, auto-expiry will be inhibited."
10483   ;; The mark might be a string.
10484   (when (stringp mark)
10485     (setq mark (aref mark 0)))
10486   ;; If no mark is given, then we check auto-expiring.
10487   (when (null mark)
10488     (setq mark gnus-del-mark))
10489   (when (and (not no-expire)
10490              gnus-newsgroup-auto-expire
10491              (memq mark gnus-auto-expirable-marks))
10492     (setq mark gnus-expirable-mark))
10493   (let ((article (or article (gnus-summary-article-number)))
10494         (old-mark (gnus-summary-article-mark article)))
10495     ;; Allow the backend to change the mark.
10496     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10497     (if (eq mark old-mark)
10498         t
10499       (unless article
10500         (error "No article on current line"))
10501       (if (not (if (or (= mark gnus-unread-mark)
10502                        (= mark gnus-ticked-mark)
10503                        (= mark gnus-spam-mark)
10504                        (= mark gnus-dormant-mark))
10505                    (gnus-mark-article-as-unread article mark)
10506                  (gnus-mark-article-as-read article mark)))
10507           t
10508         ;; See whether the article is to be put in the cache.
10509         (and gnus-use-cache
10510              (not (= mark gnus-canceled-mark))
10511              (vectorp (gnus-summary-article-header article))
10512              (save-excursion
10513                (gnus-cache-possibly-enter-article
10514                 gnus-newsgroup-name article
10515                 (= mark gnus-ticked-mark)
10516                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10517
10518         (when (gnus-summary-goto-subject article nil t)
10519           (let ((buffer-read-only nil))
10520             (gnus-summary-show-thread)
10521             ;; Fix the mark.
10522             (gnus-summary-update-mark mark 'unread)
10523             t))))))
10524
10525 (defun gnus-summary-update-secondary-mark (article)
10526   "Update the secondary (read, process, cache) mark."
10527   (gnus-summary-update-mark
10528    (cond ((memq article gnus-newsgroup-processable)
10529           gnus-process-mark)
10530          ((memq article gnus-newsgroup-cached)
10531           gnus-cached-mark)
10532          ((memq article gnus-newsgroup-replied)
10533           gnus-replied-mark)
10534          ((memq article gnus-newsgroup-forwarded)
10535           gnus-forwarded-mark)
10536          ((memq article gnus-newsgroup-saved)
10537           gnus-saved-mark)
10538          ((memq article gnus-newsgroup-recent)
10539           gnus-recent-mark)
10540          ((memq article gnus-newsgroup-unseen)
10541           gnus-unseen-mark)
10542          (t gnus-no-mark))
10543    'replied)
10544   (when (gnus-visual-p 'summary-highlight 'highlight)
10545     (gnus-run-hooks 'gnus-summary-update-hook))
10546   t)
10547
10548 (defun gnus-summary-update-download-mark (article)
10549   "Update the download mark."
10550   (gnus-summary-update-mark
10551    (cond ((memq article gnus-newsgroup-undownloaded)
10552           gnus-undownloaded-mark)
10553          (gnus-newsgroup-agentized
10554           gnus-downloaded-mark)
10555          (t
10556           gnus-no-mark))
10557    'download)
10558   (gnus-summary-update-line t)
10559   t)
10560
10561 (defun gnus-summary-update-mark (mark type)
10562   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10563         (buffer-read-only nil))
10564     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10565     (when forward
10566       (when (looking-at "\r")
10567         (incf forward))
10568       (when (<= (+ forward (point)) (point-max))
10569         ;; Go to the right position on the line.
10570         (goto-char (+ forward (point)))
10571         ;; Replace the old mark with the new mark.
10572         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10573         ;; Optionally update the marks by some user rule.
10574         (when (eq type 'unread)
10575           (gnus-data-set-mark
10576            (gnus-data-find (gnus-summary-article-number)) mark)
10577           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10578
10579 (defun gnus-mark-article-as-read (article &optional mark)
10580   "Enter ARTICLE in the pertinent lists and remove it from others."
10581   ;; Make the article expirable.
10582   (let ((mark (or mark gnus-del-mark)))
10583     (setq gnus-newsgroup-expirable
10584           (if (= mark gnus-expirable-mark)
10585               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10586             (delq article gnus-newsgroup-expirable)))
10587     ;; Remove from unread and marked lists.
10588     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10589     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10590     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10591     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10592     (push (cons article mark) gnus-newsgroup-reads)
10593     ;; Possibly remove from cache, if that is used.
10594     (when gnus-use-cache
10595       (gnus-cache-enter-remove-article article))
10596     t))
10597
10598 (defun gnus-mark-article-as-unread (article &optional mark)
10599   "Enter ARTICLE in the pertinent lists and remove it from others."
10600   (let ((mark (or mark gnus-ticked-mark)))
10601     (if (<= article 0)
10602         (progn
10603           (gnus-error 1 "Can't mark negative article numbers")
10604           nil)
10605       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10606             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10607             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10608             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10609             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10610
10611       ;; Unsuppress duplicates?
10612       (when gnus-suppress-duplicates
10613         (gnus-dup-unsuppress-article article))
10614
10615       (cond ((= mark gnus-ticked-mark)
10616              (setq gnus-newsgroup-marked
10617                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10618             ((= mark gnus-spam-mark)
10619              (setq gnus-newsgroup-spam-marked
10620                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10621                                             article)))
10622             ((= mark gnus-dormant-mark)
10623              (setq gnus-newsgroup-dormant
10624                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10625             (t
10626              (setq gnus-newsgroup-unreads
10627                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10628       (gnus-pull article gnus-newsgroup-reads)
10629       t)))
10630
10631 (defalias 'gnus-summary-mark-as-unread-forward
10632   'gnus-summary-tick-article-forward)
10633 (make-obsolete 'gnus-summary-mark-as-unread-forward
10634                'gnus-summary-tick-article-forward)
10635 (defun gnus-summary-tick-article-forward (n)
10636   "Tick N articles forwards.
10637 If N is negative, tick backwards instead.
10638 The difference between N and the number of articles ticked is returned."
10639   (interactive "p")
10640   (gnus-summary-mark-forward n gnus-ticked-mark))
10641
10642 (defalias 'gnus-summary-mark-as-unread-backward
10643   'gnus-summary-tick-article-backward)
10644 (make-obsolete 'gnus-summary-mark-as-unread-backward
10645                'gnus-summary-tick-article-backward)
10646 (defun gnus-summary-tick-article-backward (n)
10647   "Tick N articles backwards.
10648 The difference between N and the number of articles ticked is returned."
10649   (interactive "p")
10650   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10651
10652 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10653 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10654 (defun gnus-summary-tick-article (&optional article clear-mark)
10655   "Mark current article as unread.
10656 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10657 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10658   (interactive)
10659   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10660                                        gnus-ticked-mark)))
10661
10662 (defun gnus-summary-mark-as-read-forward (n)
10663   "Mark N articles as read forwards.
10664 If N is negative, mark backwards instead.
10665 The difference between N and the actual number of articles marked is
10666 returned."
10667   (interactive "p")
10668   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10669
10670 (defun gnus-summary-mark-as-read-backward (n)
10671   "Mark the N articles as read backwards.
10672 The difference between N and the actual number of articles marked is
10673 returned."
10674   (interactive "p")
10675   (gnus-summary-mark-forward
10676    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10677
10678 (defun gnus-summary-mark-as-read (&optional article mark)
10679   "Mark current article as read.
10680 ARTICLE specifies the article to be marked as read.
10681 MARK specifies a string to be inserted at the beginning of the line."
10682   (gnus-summary-mark-article article mark))
10683
10684 (defun gnus-summary-clear-mark-forward (n)
10685   "Clear marks from N articles forward.
10686 If N is negative, clear backward instead.
10687 The difference between N and the number of marks cleared is returned."
10688   (interactive "p")
10689   (gnus-summary-mark-forward n gnus-unread-mark))
10690
10691 (defun gnus-summary-clear-mark-backward (n)
10692   "Clear marks from N articles backward.
10693 The difference between N and the number of marks cleared is returned."
10694   (interactive "p")
10695   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10696
10697 (defun gnus-summary-mark-unread-as-read ()
10698   "Intended to be used by `gnus-summary-mark-article-hook'."
10699   (when (memq gnus-current-article gnus-newsgroup-unreads)
10700     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10701
10702 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10703   "Intended to be used by `gnus-summary-mark-article-hook'."
10704   (let ((mark (gnus-summary-article-mark)))
10705     (when (or (gnus-unread-mark-p mark)
10706               (gnus-read-mark-p mark))
10707       (gnus-summary-mark-article gnus-current-article
10708                                  (or new-mark gnus-read-mark)))))
10709
10710 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10711   "Intended to be used by `gnus-summary-mark-article-hook'."
10712   (let ((mark (gnus-summary-article-mark)))
10713     (when (or (gnus-unread-mark-p mark)
10714               (gnus-read-mark-p mark))
10715       (gnus-summary-mark-article (gnus-summary-article-number)
10716                                  (or new-mark gnus-read-mark)))))
10717
10718 (defun gnus-summary-mark-unread-as-ticked ()
10719   "Intended to be used by `gnus-summary-mark-article-hook'."
10720   (when (memq gnus-current-article gnus-newsgroup-unreads)
10721     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10722
10723 (defun gnus-summary-mark-region-as-read (point mark all)
10724   "Mark all unread articles between point and mark as read.
10725 If given a prefix, mark all articles between point and mark as read,
10726 even ticked and dormant ones."
10727   (interactive "r\nP")
10728   (save-excursion
10729     (let (article)
10730       (goto-char point)
10731       (beginning-of-line)
10732       (while (and
10733               (< (point) mark)
10734               (progn
10735                 (when (or all
10736                           (memq (setq article (gnus-summary-article-number))
10737                                 gnus-newsgroup-unreads))
10738                   (gnus-summary-mark-article article gnus-del-mark))
10739                 t)
10740               (gnus-summary-find-next))))))
10741
10742 (defun gnus-summary-mark-below (score mark)
10743   "Mark articles with score less than SCORE with MARK."
10744   (interactive "P\ncMark: ")
10745   (setq score (if score
10746                   (prefix-numeric-value score)
10747                 (or gnus-summary-default-score 0)))
10748   (save-excursion
10749     (set-buffer gnus-summary-buffer)
10750     (goto-char (point-min))
10751     (while
10752         (progn
10753           (and (< (gnus-summary-article-score) score)
10754                (gnus-summary-mark-article nil mark))
10755           (gnus-summary-find-next)))))
10756
10757 (defun gnus-summary-kill-below (&optional score)
10758   "Mark articles with score below SCORE as read."
10759   (interactive "P")
10760   (gnus-summary-mark-below score gnus-killed-mark))
10761
10762 (defun gnus-summary-clear-above (&optional score)
10763   "Clear all marks from articles with score above SCORE."
10764   (interactive "P")
10765   (gnus-summary-mark-above score gnus-unread-mark))
10766
10767 (defun gnus-summary-tick-above (&optional score)
10768   "Tick all articles with score above SCORE."
10769   (interactive "P")
10770   (gnus-summary-mark-above score gnus-ticked-mark))
10771
10772 (defun gnus-summary-mark-above (score mark)
10773   "Mark articles with score over SCORE with MARK."
10774   (interactive "P\ncMark: ")
10775   (setq score (if score
10776                   (prefix-numeric-value score)
10777                 (or gnus-summary-default-score 0)))
10778   (save-excursion
10779     (set-buffer gnus-summary-buffer)
10780     (goto-char (point-min))
10781     (while (and (progn
10782                   (when (> (gnus-summary-article-score) score)
10783                     (gnus-summary-mark-article nil mark))
10784                   t)
10785                 (gnus-summary-find-next)))))
10786
10787 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10788 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10789 (defun gnus-summary-limit-include-expunged (&optional no-error)
10790   "Display all the hidden articles that were expunged for low scores."
10791   (interactive)
10792   (let ((buffer-read-only nil))
10793     (let ((scored gnus-newsgroup-scored)
10794           headers h)
10795       (while scored
10796         (unless (gnus-summary-article-header (caar scored))
10797           (and (setq h (gnus-number-to-header (caar scored)))
10798                (< (cdar scored) gnus-summary-expunge-below)
10799                (push h headers)))
10800         (setq scored (cdr scored)))
10801       (if (not headers)
10802           (when (not no-error)
10803             (error "No expunged articles hidden"))
10804         (goto-char (point-min))
10805         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10806         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10807         (mapcar (lambda (x) (push (mail-header-number x)
10808                                   gnus-newsgroup-limit))
10809                 headers)
10810         (gnus-summary-prepare-unthreaded (nreverse headers))
10811         (goto-char (point-min))
10812         (gnus-summary-position-point)
10813         t))))
10814
10815 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10816   "Mark all unread articles in this newsgroup as read.
10817 If prefix argument ALL is non-nil, ticked and dormant articles will
10818 also be marked as read.
10819 If QUIETLY is non-nil, no questions will be asked.
10820
10821 If TO-HERE is non-nil, it should be a point in the buffer.  All
10822 articles before (after, if REVERSE is set) this point will be marked
10823 as read.
10824
10825 Note that this function will only catch up the unread article
10826 in the current summary buffer limitation.
10827
10828 The number of articles marked as read is returned."
10829   (interactive "P")
10830   (prog1
10831       (save-excursion
10832         (when (or quietly
10833                   (not gnus-interactive-catchup) ;Without confirmation?
10834                   gnus-expert-user
10835                   (gnus-y-or-n-p
10836                    (if all
10837                        "Mark absolutely all articles as read? "
10838                      "Mark all unread articles as read? ")))
10839           (if (and not-mark
10840                    (not gnus-newsgroup-adaptive)
10841                    (not gnus-newsgroup-auto-expire)
10842                    (not gnus-suppress-duplicates)
10843                    (or (not gnus-use-cache)
10844                        (eq gnus-use-cache 'passive)))
10845               (progn
10846                 (when all
10847                   (setq gnus-newsgroup-marked nil
10848                         gnus-newsgroup-spam-marked nil
10849                         gnus-newsgroup-dormant nil))
10850                 (setq gnus-newsgroup-unreads
10851                       (gnus-sorted-nunion
10852                        (gnus-sorted-intersection gnus-newsgroup-unreads
10853                                                  gnus-newsgroup-downloadable)
10854                        gnus-newsgroup-unfetched)))
10855             ;; We actually mark all articles as canceled, which we
10856             ;; have to do when using auto-expiry or adaptive scoring.
10857             (gnus-summary-show-all-threads)
10858             (if (and to-here reverse)
10859                 (progn
10860                   (goto-char to-here)
10861                   (gnus-summary-mark-current-read-and-unread-as-read
10862                    gnus-catchup-mark)
10863                   (while (gnus-summary-find-next (not all))
10864                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10865               (when (gnus-summary-first-subject (not all))
10866                 (while (and
10867                         (if to-here (< (point) to-here) t)
10868                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10869                         (gnus-summary-find-next (not all))))))
10870             (gnus-set-mode-line 'summary))
10871           t))
10872     (gnus-summary-position-point)))
10873
10874 (defun gnus-summary-catchup-to-here (&optional all)
10875   "Mark all unticked articles before the current one as read.
10876 If ALL is non-nil, also mark ticked and dormant articles as read."
10877   (interactive "P")
10878   (save-excursion
10879     (gnus-save-hidden-threads
10880       (let ((beg (point)))
10881         ;; We check that there are unread articles.
10882         (when (or all (gnus-summary-find-prev))
10883           (gnus-summary-catchup all t beg)))))
10884   (gnus-summary-position-point))
10885
10886 (defun gnus-summary-catchup-from-here (&optional all)
10887   "Mark all unticked articles after (and including) the current one as read.
10888 If ALL is non-nil, also mark ticked and dormant articles as read."
10889   (interactive "P")
10890   (save-excursion
10891     (gnus-save-hidden-threads
10892       (let ((beg (point)))
10893         ;; We check that there are unread articles.
10894         (when (or all (gnus-summary-find-next))
10895           (gnus-summary-catchup all t beg nil t)))))
10896   (gnus-summary-position-point))
10897
10898 (defun gnus-summary-catchup-all (&optional quietly)
10899   "Mark all articles in this newsgroup as read.
10900 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10901 instead, which marks only unread articles as read."
10902   (interactive "P")
10903   (gnus-summary-catchup t quietly))
10904
10905 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10906   "Mark all unread articles in this group as read, then exit.
10907 If prefix argument ALL is non-nil, all articles are marked as read.
10908 If QUIETLY is non-nil, no questions will be asked."
10909   (interactive "P")
10910   (when (gnus-summary-catchup all quietly nil 'fast)
10911     ;; Select next newsgroup or exit.
10912     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10913              (eq gnus-auto-select-next 'quietly))
10914         (gnus-summary-next-group nil)
10915       (gnus-summary-exit))))
10916
10917 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10918   "Mark all articles in this newsgroup as read, and then exit.
10919 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10920 instead, which marks only unread articles as read."
10921   (interactive "P")
10922   (gnus-summary-catchup-and-exit t quietly))
10923
10924 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10925   "Mark all articles in this group as read and select the next group.
10926 If given a prefix, mark all articles, unread as well as ticked, as
10927 read."
10928   (interactive "P")
10929   (save-excursion
10930     (gnus-summary-catchup all))
10931   (gnus-summary-next-group))
10932
10933 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10934   "Mark all articles in this group as read and select the previous group.
10935 If given a prefix, mark all articles, unread as well as ticked, as
10936 read."
10937   (interactive "P")
10938   (save-excursion
10939     (gnus-summary-catchup all))
10940   (gnus-summary-next-group nil nil t))
10941
10942 ;;;
10943 ;;; with article
10944 ;;;
10945
10946 (defmacro gnus-with-article (article &rest forms)
10947   "Select ARTICLE and perform FORMS in the original article buffer.
10948 Then replace the article with the result."
10949   `(progn
10950      ;; We don't want the article to be marked as read.
10951      (let (gnus-mark-article-hook)
10952        (gnus-summary-select-article t t nil ,article))
10953      (set-buffer gnus-original-article-buffer)
10954      ,@forms
10955      (if (not (gnus-check-backend-function
10956                'request-replace-article (car gnus-article-current)))
10957          (gnus-message 5 "Read-only group; not replacing")
10958        (unless (gnus-request-replace-article
10959                 ,article (car gnus-article-current)
10960                 (current-buffer) t)
10961          (error "Couldn't replace article")))
10962      ;; The cache and backlog have to be flushed somewhat.
10963      (when gnus-keep-backlog
10964        (gnus-backlog-remove-article
10965         (car gnus-article-current) (cdr gnus-article-current)))
10966      (when gnus-use-cache
10967        (gnus-cache-update-article
10968         (car gnus-article-current) (cdr gnus-article-current)))))
10969
10970 (put 'gnus-with-article 'lisp-indent-function 1)
10971 (put 'gnus-with-article 'edebug-form-spec '(form body))
10972
10973 ;; Thread-based commands.
10974
10975 (defun gnus-summary-articles-in-thread (&optional article)
10976   "Return a list of all articles in the current thread.
10977 If ARTICLE is non-nil, return all articles in the thread that starts
10978 with that article."
10979   (let* ((article (or article (gnus-summary-article-number)))
10980          (data (gnus-data-find-list article))
10981          (top-level (gnus-data-level (car data)))
10982          (top-subject
10983           (cond ((null gnus-thread-operation-ignore-subject)
10984                  (gnus-simplify-subject-re
10985                   (mail-header-subject (gnus-data-header (car data)))))
10986                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10987                  (gnus-simplify-subject-fuzzy
10988                   (mail-header-subject (gnus-data-header (car data)))))
10989                 (t nil)))
10990          (end-point (save-excursion
10991                       (if (gnus-summary-go-to-next-thread)
10992                           (point) (point-max))))
10993          articles)
10994     (while (and data
10995                 (< (gnus-data-pos (car data)) end-point))
10996       (when (or (not top-subject)
10997                 (string= top-subject
10998                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10999                              (gnus-simplify-subject-fuzzy
11000                               (mail-header-subject
11001                                (gnus-data-header (car data))))
11002                            (gnus-simplify-subject-re
11003                             (mail-header-subject
11004                              (gnus-data-header (car data)))))))
11005         (push (gnus-data-number (car data)) articles))
11006       (unless (and (setq data (cdr data))
11007                    (> (gnus-data-level (car data)) top-level))
11008         (setq data nil)))
11009     ;; Return the list of articles.
11010     (nreverse articles)))
11011
11012 (defun gnus-summary-rethread-current ()
11013   "Rethread the thread the current article is part of."
11014   (interactive)
11015   (let* ((gnus-show-threads t)
11016          (article (gnus-summary-article-number))
11017          (id (mail-header-id (gnus-summary-article-header)))
11018          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11019     (unless id
11020       (error "No article on the current line"))
11021     (gnus-rebuild-thread id)
11022     (gnus-summary-goto-subject article)))
11023
11024 (defun gnus-summary-reparent-thread ()
11025   "Make the current article child of the marked (or previous) article.
11026
11027 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11028 is non-nil or the Subject: of both articles are the same."
11029   (interactive)
11030   (unless (not (gnus-group-read-only-p))
11031     (error "The current newsgroup does not support article editing"))
11032   (unless (<= (length gnus-newsgroup-processable) 1)
11033     (error "No more than one article may be marked"))
11034   (let ((child (gnus-summary-article-number))
11035         ;; First grab the marked article, otherwise one line up.
11036         (parent (if (not (null gnus-newsgroup-processable))
11037                     (car gnus-newsgroup-processable)
11038                   (save-excursion
11039                     (if (eq (forward-line -1) 0)
11040                         (gnus-summary-article-number)
11041                       (error "Beginning of summary buffer"))))))
11042     (gnus-summary-reparent-children parent (list child))))
11043
11044 (defun gnus-summary-reparent-children (parent children)
11045   "Make PARENT the parent of CHILDREN.
11046 When called interactively, PARENT is the current article and CHILDREN
11047 are the process-marked articles."
11048   (interactive
11049    (list (gnus-summary-article-number)
11050          (gnus-summary-work-articles nil)))
11051   (dolist (child children)
11052     (save-window-excursion
11053       (let ((gnus-article-buffer " *reparent*"))
11054         (unless (not (eq parent child))
11055           (error "An article may not be self-referential"))
11056         (let ((message-id (mail-header-id
11057                            (gnus-summary-article-header parent))))
11058           (unless (and message-id (not (equal message-id "")))
11059             (error "No message-id in desired parent"))
11060           (gnus-with-article child
11061             (save-restriction
11062               (goto-char (point-min))
11063               (message-narrow-to-head)
11064               (if (re-search-forward "^References: " nil t)
11065                   (progn
11066                     (re-search-forward "^[^ \t]" nil t)
11067                     (forward-line -1)
11068                     (end-of-line)
11069                     (insert " " message-id))
11070                 (insert "References: " message-id "\n"))))
11071           (set-buffer gnus-summary-buffer)
11072           (gnus-summary-unmark-all-processable)
11073           (gnus-summary-update-article child)
11074           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11075             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11076           (gnus-summary-rethread-current)
11077           (gnus-message 3 "Article %d is now the child of article %d"
11078                         child parent))))))
11079
11080 (defun gnus-summary-toggle-threads (&optional arg)
11081   "Toggle showing conversation threads.
11082 If ARG is positive number, turn showing conversation threads on."
11083   (interactive "P")
11084   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11085     (setq gnus-show-threads
11086           (if (null arg) (not gnus-show-threads)
11087             (> (prefix-numeric-value arg) 0)))
11088     (gnus-summary-prepare)
11089     (gnus-summary-goto-subject current)
11090     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11091     (gnus-summary-position-point)))
11092
11093 (defun gnus-summary-show-all-threads ()
11094   "Show all threads."
11095   (interactive)
11096   (save-excursion
11097     (let ((buffer-read-only nil))
11098       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11099   (gnus-summary-position-point))
11100
11101 (defun gnus-summary-show-thread ()
11102   "Show thread subtrees.
11103 Returns nil if no thread was there to be shown."
11104   (interactive)
11105   (let ((buffer-read-only nil)
11106         (orig (point))
11107         (end (point-at-eol))
11108         ;; Leave point at bol
11109         (beg (progn (beginning-of-line) (point))))
11110     (prog1
11111         ;; Any hidden lines here?
11112         (search-forward "\r" end t)
11113       (subst-char-in-region beg end ?\^M ?\n t)
11114       (goto-char orig)
11115       (gnus-summary-position-point))))
11116
11117 (defun gnus-summary-maybe-hide-threads ()
11118   "If requested, hide the threads that should be hidden."
11119   (when (and gnus-show-threads
11120              gnus-thread-hide-subtree)
11121     (gnus-summary-hide-all-threads
11122      (if (or (consp gnus-thread-hide-subtree)
11123              (functionp gnus-thread-hide-subtree))
11124          (gnus-make-predicate gnus-thread-hide-subtree)
11125        nil))))
11126
11127 ;;; Hiding predicates.
11128
11129 (defun gnus-article-unread-p (header)
11130   (memq (mail-header-number header) gnus-newsgroup-unreads))
11131
11132 (defun gnus-article-unseen-p (header)
11133   (memq (mail-header-number header) gnus-newsgroup-unseen))
11134
11135 (defun gnus-map-articles (predicate articles)
11136   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11137   (apply 'gnus-or (mapcar predicate
11138                           (mapcar (lambda (number)
11139                                     (gnus-summary-article-header number))
11140                                   articles))))
11141
11142 (defun gnus-summary-hide-all-threads (&optional predicate)
11143   "Hide all thread subtrees.
11144 If PREDICATE is supplied, threads that satisfy this predicate
11145 will not be hidden."
11146   (interactive)
11147   (save-excursion
11148     (goto-char (point-min))
11149     (let ((end nil))
11150       (while (not end)
11151         (when (or (not predicate)
11152                   (gnus-map-articles
11153                    predicate (gnus-summary-article-children)))
11154             (gnus-summary-hide-thread))
11155         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11156   (gnus-summary-position-point))
11157
11158 (defun gnus-summary-hide-thread ()
11159   "Hide thread subtrees.
11160 If PREDICATE is supplied, threads that satisfy this predicate
11161 will not be hidden.
11162 Returns nil if no threads were there to be hidden."
11163   (interactive)
11164   (let ((buffer-read-only nil)
11165         (start (point))
11166         (article (gnus-summary-article-number)))
11167     (goto-char start)
11168     ;; Go forward until either the buffer ends or the subthread
11169     ;; ends.
11170     (when (and (not (eobp))
11171                (or (zerop (gnus-summary-next-thread 1 t))
11172                    (goto-char (point-max))))
11173       (prog1
11174           (if (and (> (point) start)
11175                    (search-backward "\n" start t))
11176               (progn
11177                 (subst-char-in-region start (point) ?\n ?\^M)
11178                 (gnus-summary-goto-subject article))
11179             (goto-char start)
11180             nil)))))
11181
11182 (defun gnus-summary-go-to-next-thread (&optional previous)
11183   "Go to the same level (or less) next thread.
11184 If PREVIOUS is non-nil, go to previous thread instead.
11185 Return the article number moved to, or nil if moving was impossible."
11186   (let ((level (gnus-summary-thread-level))
11187         (way (if previous -1 1))
11188         (beg (point)))
11189     (forward-line way)
11190     (while (and (not (eobp))
11191                 (< level (gnus-summary-thread-level)))
11192       (forward-line way))
11193     (if (eobp)
11194         (progn
11195           (goto-char beg)
11196           nil)
11197       (setq beg (point))
11198       (prog1
11199           (gnus-summary-article-number)
11200         (goto-char beg)))))
11201
11202 (defun gnus-summary-next-thread (n &optional silent)
11203   "Go to the same level next N'th thread.
11204 If N is negative, search backward instead.
11205 Returns the difference between N and the number of skips actually
11206 done.
11207
11208 If SILENT, don't output messages."
11209   (interactive "p")
11210   (let ((backward (< n 0))
11211         (n (abs n)))
11212     (while (and (> n 0)
11213                 (gnus-summary-go-to-next-thread backward))
11214       (decf n))
11215     (unless silent
11216       (gnus-summary-position-point))
11217     (when (and (not silent) (/= 0 n))
11218       (gnus-message 7 "No more threads"))
11219     n))
11220
11221 (defun gnus-summary-prev-thread (n)
11222   "Go to the same level previous N'th thread.
11223 Returns the difference between N and the number of skips actually
11224 done."
11225   (interactive "p")
11226   (gnus-summary-next-thread (- n)))
11227
11228 (defun gnus-summary-go-down-thread ()
11229   "Go down one level in the current thread."
11230   (let ((children (gnus-summary-article-children)))
11231     (when children
11232       (gnus-summary-goto-subject (car children)))))
11233
11234 (defun gnus-summary-go-up-thread ()
11235   "Go up one level in the current thread."
11236   (let ((parent (gnus-summary-article-parent)))
11237     (when parent
11238       (gnus-summary-goto-subject parent))))
11239
11240 (defun gnus-summary-down-thread (n)
11241   "Go down thread N steps.
11242 If N is negative, go up instead.
11243 Returns the difference between N and how many steps down that were
11244 taken."
11245   (interactive "p")
11246   (let ((up (< n 0))
11247         (n (abs n)))
11248     (while (and (> n 0)
11249                 (if up (gnus-summary-go-up-thread)
11250                   (gnus-summary-go-down-thread)))
11251       (setq n (1- n)))
11252     (gnus-summary-position-point)
11253     (when (/= 0 n)
11254       (gnus-message 7 "Can't go further"))
11255     n))
11256
11257 (defun gnus-summary-up-thread (n)
11258   "Go up thread N steps.
11259 If N is negative, go down instead.
11260 Returns the difference between N and how many steps down that were
11261 taken."
11262   (interactive "p")
11263   (gnus-summary-down-thread (- n)))
11264
11265 (defun gnus-summary-top-thread ()
11266   "Go to the top of the thread."
11267   (interactive)
11268   (while (gnus-summary-go-up-thread))
11269   (gnus-summary-article-number))
11270
11271 (defun gnus-summary-kill-thread (&optional unmark)
11272   "Mark articles under current thread as read.
11273 If the prefix argument is positive, remove any kinds of marks.
11274 If the prefix argument is negative, tick articles instead."
11275   (interactive "P")
11276   (when unmark
11277     (setq unmark (prefix-numeric-value unmark)))
11278   (let ((articles (gnus-summary-articles-in-thread)))
11279     (save-excursion
11280       ;; Expand the thread.
11281       (gnus-summary-show-thread)
11282       ;; Mark all the articles.
11283       (while articles
11284         (gnus-summary-goto-subject (car articles))
11285         (cond ((null unmark)
11286                (gnus-summary-mark-article-as-read gnus-killed-mark))
11287               ((> unmark 0)
11288                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11289               (t
11290                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11291         (setq articles (cdr articles))))
11292     ;; Hide killed subtrees.
11293     (and (null unmark)
11294          gnus-thread-hide-killed
11295          (gnus-summary-hide-thread))
11296     ;; If marked as read, go to next unread subject.
11297     (when (null unmark)
11298       ;; Go to next unread subject.
11299       (gnus-summary-next-subject 1 t)))
11300   (gnus-set-mode-line 'summary))
11301
11302 ;; Summary sorting commands
11303
11304 (defun gnus-summary-sort-by-number (&optional reverse)
11305   "Sort the summary buffer by article number.
11306 Argument REVERSE means reverse order."
11307   (interactive "P")
11308   (gnus-summary-sort 'number reverse))
11309
11310 (defun gnus-summary-sort-by-random (&optional reverse)
11311   "Randomize the order in the summary buffer.
11312 Argument REVERSE means to randomize in reverse order."
11313   (interactive "P")
11314   (gnus-summary-sort 'random reverse))
11315
11316 (defun gnus-summary-sort-by-author (&optional reverse)
11317   "Sort the summary buffer by author name alphabetically.
11318 If `case-fold-search' is non-nil, case of letters is ignored.
11319 Argument REVERSE means reverse order."
11320   (interactive "P")
11321   (gnus-summary-sort 'author reverse))
11322
11323 (defun gnus-summary-sort-by-recipient (&optional reverse)
11324   "Sort the summary buffer by recipient name alphabetically.
11325 If `case-fold-search' is non-nil, case of letters is ignored.
11326 Argument REVERSE means reverse order."
11327   (interactive "P")
11328   (gnus-summary-sort 'recipient reverse))
11329
11330 (defun gnus-summary-sort-by-subject (&optional reverse)
11331   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11332 If `case-fold-search' is non-nil, case of letters is ignored.
11333 Argument REVERSE means reverse order."
11334   (interactive "P")
11335   (gnus-summary-sort 'subject reverse))
11336
11337 (defun gnus-summary-sort-by-date (&optional reverse)
11338   "Sort the summary buffer by date.
11339 Argument REVERSE means reverse order."
11340   (interactive "P")
11341   (gnus-summary-sort 'date reverse))
11342
11343 (defun gnus-summary-sort-by-score (&optional reverse)
11344   "Sort the summary buffer by score.
11345 Argument REVERSE means reverse order."
11346   (interactive "P")
11347   (gnus-summary-sort 'score reverse))
11348
11349 (defun gnus-summary-sort-by-lines (&optional reverse)
11350   "Sort the summary buffer by the number of lines.
11351 Argument REVERSE means reverse order."
11352   (interactive "P")
11353   (gnus-summary-sort 'lines reverse))
11354
11355 (defun gnus-summary-sort-by-chars (&optional reverse)
11356   "Sort the summary buffer by article length.
11357 Argument REVERSE means reverse order."
11358   (interactive "P")
11359   (gnus-summary-sort 'chars reverse))
11360
11361 (defun gnus-summary-sort-by-original (&optional reverse)
11362   "Sort the summary buffer using the default sorting method.
11363 Argument REVERSE means reverse order."
11364   (interactive "P")
11365   (let* ((buffer-read-only)
11366          (gnus-summary-prepare-hook nil))
11367     ;; We do the sorting by regenerating the threads.
11368     (gnus-summary-prepare)
11369     ;; Hide subthreads if needed.
11370     (gnus-summary-maybe-hide-threads)))
11371
11372 (defun gnus-summary-sort (predicate reverse)
11373   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11374   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11375          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11376          (gnus-thread-sort-functions
11377           (if (not reverse)
11378               thread
11379             `(lambda (t1 t2)
11380                (,thread t2 t1))))
11381          (gnus-sort-gathered-threads-function
11382           gnus-thread-sort-functions)
11383          (gnus-article-sort-functions
11384           (if (not reverse)
11385               article
11386             `(lambda (t1 t2)
11387                (,article t2 t1))))
11388          (buffer-read-only)
11389          (gnus-summary-prepare-hook nil))
11390     ;; We do the sorting by regenerating the threads.
11391     (gnus-summary-prepare)
11392     ;; Hide subthreads if needed.
11393     (gnus-summary-maybe-hide-threads)))
11394
11395 ;; Summary saving commands.
11396
11397 (defun gnus-summary-save-article (&optional n not-saved)
11398   "Save the current article using the default saver function.
11399 If N is a positive number, save the N next articles.
11400 If N is a negative number, save the N previous articles.
11401 If N is nil and any articles have been marked with the process mark,
11402 save those articles instead.
11403 The variable `gnus-default-article-saver' specifies the saver function.
11404
11405 If the optional second argument NOT-SAVED is non-nil, articles saved
11406 will not be marked as saved."
11407   (interactive "P")
11408   (require 'gnus-art)
11409   (let* ((articles (gnus-summary-work-articles n))
11410          (save-buffer (save-excursion
11411                         (nnheader-set-temp-buffer " *Gnus Save*")))
11412          (num (length articles))
11413          ;; Whether to save decoded articles or raw articles.
11414          (decode (when gnus-article-save-coding-system
11415                    (get gnus-default-article-saver :decode)))
11416          ;; When saving many articles in a single file, use the other
11417          ;; function to save articles other than the first one.
11418          (saver2 (get gnus-default-article-saver :function))
11419          (gnus-prompt-before-saving (if saver2
11420                                         t
11421                                       gnus-prompt-before-saving))
11422          (gnus-default-article-saver gnus-default-article-saver)
11423          header file)
11424     (dolist (article articles)
11425       (setq header (gnus-summary-article-header article))
11426       (if (not (vectorp header))
11427           ;; This is a pseudo-article.
11428           (if (assq 'name header)
11429               (gnus-copy-file (cdr (assq 'name header)))
11430             (gnus-message 1 "Article %d is unsaveable" article))
11431         ;; This is a real article.
11432         (save-window-excursion
11433           (let ((gnus-display-mime-function (when decode
11434                                               gnus-display-mime-function))
11435                 (gnus-article-prepare-hook (when decode
11436                                              gnus-article-prepare-hook)))
11437             (gnus-summary-select-article t nil nil article)
11438             (gnus-summary-goto-subject article)))
11439         (save-excursion
11440           (set-buffer save-buffer)
11441           (erase-buffer)
11442           (insert-buffer-substring (if decode
11443                                        gnus-article-buffer
11444                                      gnus-original-article-buffer)))
11445         (setq file (gnus-article-save save-buffer file num))
11446         (gnus-summary-remove-process-mark article)
11447         (unless not-saved
11448           (gnus-summary-set-saved-mark article)))
11449       (when saver2
11450         (setq gnus-default-article-saver saver2
11451               saver2 nil)))
11452     (gnus-kill-buffer save-buffer)
11453     (gnus-summary-position-point)
11454     (gnus-set-mode-line 'summary)
11455     n))
11456
11457 (defun gnus-summary-pipe-output (&optional arg headers)
11458   "Pipe the current article to a subprocess.
11459 If N is a positive number, pipe the N next articles.
11460 If N is a negative number, pipe the N previous articles.
11461 If N is nil and any articles have been marked with the process mark,
11462 pipe those articles instead.
11463 If HEADERS (the symbolic prefix), include the headers, too."
11464   (interactive (gnus-interactive "P\ny"))
11465   (require 'gnus-art)
11466   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11467         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11468     (gnus-summary-save-article arg t))
11469   (let ((buffer (get-buffer "*Shell Command Output*")))
11470     (when (and buffer
11471                (not (zerop (buffer-size buffer))))
11472       (gnus-configure-windows 'pipe))))
11473
11474 (defun gnus-summary-save-article-mail (&optional arg)
11475   "Append the current article to a Unix mail box file.
11476 If N is a positive number, save the N next articles.
11477 If N is a negative number, save the N previous articles.
11478 If N is nil and any articles have been marked with the process mark,
11479 save those articles instead."
11480   (interactive "P")
11481   (require 'gnus-art)
11482   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11483     (gnus-summary-save-article arg)))
11484
11485 (defun gnus-summary-save-article-rmail (&optional arg)
11486   "Append the current article to an rmail file.
11487 If N is a positive number, save the N next articles.
11488 If N is a negative number, save the N previous articles.
11489 If N is nil and any articles have been marked with the process mark,
11490 save those articles instead."
11491   (interactive "P")
11492   (require 'gnus-art)
11493   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11494     (gnus-summary-save-article arg)))
11495
11496 (defun gnus-summary-save-article-file (&optional arg)
11497   "Append the current article to a file.
11498 If N is a positive number, save the N next articles.
11499 If N is a negative number, save the N previous articles.
11500 If N is nil and any articles have been marked with the process mark,
11501 save those articles instead."
11502   (interactive "P")
11503   (require 'gnus-art)
11504   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11505     (gnus-summary-save-article arg)))
11506
11507 (defun gnus-summary-write-article-file (&optional arg)
11508   "Write the current article to a file, deleting the previous file.
11509 If N is a positive number, save the N next articles.
11510 If N is a negative number, save the N previous articles.
11511 If N is nil and any articles have been marked with the process mark,
11512 save those articles instead."
11513   (interactive "P")
11514   (require 'gnus-art)
11515   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11516     (gnus-summary-save-article arg)))
11517
11518 (defun gnus-summary-save-article-body-file (&optional arg)
11519   "Append the current article body to a file.
11520 If N is a positive number, save the N next articles.
11521 If N is a negative number, save the N previous articles.
11522 If N is nil and any articles have been marked with the process mark,
11523 save those articles instead."
11524   (interactive "P")
11525   (require 'gnus-art)
11526   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11527     (gnus-summary-save-article arg)))
11528
11529 (defun gnus-summary-write-article-body-file (&optional arg)
11530   "Write the current article body to a file, deleting the previous file.
11531 If N is a positive number, save the N next articles.
11532 If N is a negative number, save the N previous articles.
11533 If N is nil and any articles have been marked with the process mark,
11534 save those articles instead."
11535   (interactive "P")
11536   (require 'gnus-art)
11537   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11538     (gnus-summary-save-article arg)))
11539
11540 (defun gnus-summary-muttprint (&optional arg)
11541   "Print the current article using Muttprint.
11542 If N is a positive number, save the N next articles.
11543 If N is a negative number, save the N previous articles.
11544 If N is nil and any articles have been marked with the process mark,
11545 save those articles instead."
11546   (interactive "P")
11547   (require 'gnus-art)
11548   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11549     (gnus-summary-save-article arg t)))
11550
11551 (defun gnus-summary-pipe-message (program)
11552   "Pipe the current article through PROGRAM."
11553   (interactive "sProgram: ")
11554   (gnus-summary-select-article)
11555   (let ((mail-header-separator ""))
11556     (gnus-eval-in-buffer-window gnus-article-buffer
11557       (save-restriction
11558         (widen)
11559         (let ((start (window-start))
11560               buffer-read-only)
11561           (message-pipe-buffer-body program)
11562           (set-window-start (get-buffer-window (current-buffer)) start))))))
11563
11564 (defun gnus-get-split-value (methods)
11565   "Return a value based on the split METHODS."
11566   (let (split-name method result match)
11567     (when methods
11568       (save-excursion
11569         (set-buffer gnus-original-article-buffer)
11570         (save-restriction
11571           (nnheader-narrow-to-headers)
11572           (while (and methods (not split-name))
11573             (goto-char (point-min))
11574             (setq method (pop methods))
11575             (setq match (car method))
11576             (when (cond
11577                    ((stringp match)
11578                     ;; Regular expression.
11579                     (ignore-errors
11580                       (re-search-forward match nil t)))
11581                    ((functionp match)
11582                     ;; Function.
11583                     (save-restriction
11584                       (widen)
11585                       (setq result (funcall match gnus-newsgroup-name))))
11586                    ((consp match)
11587                     ;; Form.
11588                     (save-restriction
11589                       (widen)
11590                       (setq result (eval match)))))
11591               (setq split-name (cdr method))
11592               (cond ((stringp result)
11593                      (push (expand-file-name
11594                             result gnus-article-save-directory)
11595                            split-name))
11596                     ((consp result)
11597                      (setq split-name (append result split-name)))))))))
11598     (nreverse split-name)))
11599
11600 (defun gnus-valid-move-group-p (group)
11601   (and (boundp group)
11602        (symbol-name group)
11603        (symbol-value group)
11604        (gnus-get-function (gnus-find-method-for-group
11605                            (symbol-name group)) 'request-accept-article t)))
11606
11607 (defun gnus-read-move-group-name (prompt default articles prefix)
11608   "Read a group name."
11609   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11610          (minibuffer-confirm-incomplete nil) ; XEmacs
11611          (prom
11612           (format "%s %s to"
11613                   prompt
11614                   (if (> (length articles) 1)
11615                       (format "these %d articles" (length articles))
11616                     "this article")))
11617          (to-newsgroup
11618           (cond
11619            ((null split-name)
11620             (gnus-completing-read-with-default
11621              default prom
11622              gnus-active-hashtb
11623              'gnus-valid-move-group-p
11624              nil prefix
11625              'gnus-group-history))
11626            ((= 1 (length split-name))
11627             (gnus-completing-read-with-default
11628              (car split-name) prom
11629              gnus-active-hashtb
11630              'gnus-valid-move-group-p
11631              nil nil
11632              'gnus-group-history))
11633            (t
11634             (gnus-completing-read-with-default
11635              nil prom
11636              (mapcar 'list (nreverse split-name))
11637              nil nil nil
11638              'gnus-group-history))))
11639          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11640     (when to-newsgroup
11641       (if (or (string= to-newsgroup "")
11642               (string= to-newsgroup prefix))
11643           (setq to-newsgroup default))
11644       (unless to-newsgroup
11645         (error "No group name entered"))
11646       (or (gnus-active to-newsgroup)
11647           (gnus-activate-group to-newsgroup nil nil to-method)
11648           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11649                                      to-newsgroup))
11650               (or (and (gnus-request-create-group to-newsgroup to-method)
11651                        (gnus-activate-group
11652                         to-newsgroup nil nil to-method)
11653                        (gnus-subscribe-group to-newsgroup))
11654                   (error "Couldn't create group %s" to-newsgroup)))
11655           (error "No such group: %s" to-newsgroup)))
11656     to-newsgroup))
11657
11658 (defvar gnus-summary-save-parts-counter)
11659
11660 (defun gnus-summary-save-parts (type dir n &optional reverse)
11661   "Save parts matching TYPE to DIR.
11662 If REVERSE, save parts that do not match TYPE."
11663   (interactive
11664    (list (read-string "Save parts of type: "
11665                       (or (car gnus-summary-save-parts-type-history)
11666                           gnus-summary-save-parts-default-mime)
11667                       'gnus-summary-save-parts-type-history)
11668          (setq gnus-summary-save-parts-last-directory
11669                (read-file-name "Save to directory: "
11670                                gnus-summary-save-parts-last-directory
11671                                nil t))
11672          current-prefix-arg))
11673   (gnus-summary-iterate n
11674     (let ((gnus-display-mime-function nil)
11675           (gnus-inhibit-treatment t))
11676       (gnus-summary-select-article))
11677     (save-excursion
11678       (set-buffer gnus-article-buffer)
11679       (let ((handles (or gnus-article-mime-handles
11680                          (mm-dissect-buffer nil gnus-article-loose-mime)
11681                          (and gnus-article-emulate-mime
11682                               (mm-uu-dissect))))
11683             (gnus-summary-save-parts-counter 1))
11684         (when handles
11685           (gnus-summary-save-parts-1 type dir handles reverse)
11686           (unless gnus-article-mime-handles ;; Don't destroy this case.
11687             (mm-destroy-parts handles)))))))
11688
11689 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11690   (if (stringp (car handle))
11691       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11692               (cdr handle))
11693     (when (if reverse
11694               (not (string-match type (mm-handle-media-type handle)))
11695             (string-match type (mm-handle-media-type handle)))
11696       (let ((file (expand-file-name
11697                    (gnus-map-function
11698                     mm-file-name-rewrite-functions
11699                     (file-name-nondirectory
11700                      (or
11701                       (mail-content-type-get
11702                        (mm-handle-disposition handle) 'filename)
11703                       (mail-content-type-get
11704                        (mm-handle-type handle) 'name)
11705                       (format "%s.%d.%d" gnus-newsgroup-name
11706                               (cdr gnus-article-current)
11707                               gnus-summary-save-parts-counter))))
11708                    dir)))
11709         (incf gnus-summary-save-parts-counter)
11710         (unless (file-exists-p file)
11711           (mm-save-part-to-file handle file))))))
11712
11713 ;; Summary extract commands
11714
11715 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11716   (let ((buffer-read-only nil)
11717         (article (gnus-summary-article-number))
11718         after-article b e)
11719     (unless (gnus-summary-goto-subject article)
11720       (error "No such article: %d" article))
11721     (gnus-summary-position-point)
11722     ;; If all commands are to be bunched up on one line, we collect
11723     ;; them here.
11724     (unless gnus-view-pseudos-separately
11725       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11726             files action)
11727         (while ps
11728           (setq action (cdr (assq 'action (car ps))))
11729           (setq files (list (cdr (assq 'name (car ps)))))
11730           (while (and ps (cdr ps)
11731                       (string= (or action "1")
11732                                (or (cdr (assq 'action (cadr ps))) "2")))
11733             (push (cdr (assq 'name (cadr ps))) files)
11734             (setcdr ps (cddr ps)))
11735           (when files
11736             (when (not (string-match "%s" action))
11737               (push " " files))
11738             (push " " files)
11739             (when (assq 'execute (car ps))
11740               (setcdr (assq 'execute (car ps))
11741                       (funcall (if (string-match "%s" action)
11742                                    'format 'concat)
11743                                action
11744                                (mapconcat
11745                                 (lambda (f)
11746                                   (if (equal f " ")
11747                                       f
11748                                     (shell-quote-argument f)))
11749                                 files " ")))))
11750           (setq ps (cdr ps)))))
11751     (if (and gnus-view-pseudos (not not-view))
11752         (while pslist
11753           (when (assq 'execute (car pslist))
11754             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11755                                   (eq gnus-view-pseudos 'not-confirm)))
11756           (setq pslist (cdr pslist)))
11757       (save-excursion
11758         (while pslist
11759           (setq after-article (or (cdr (assq 'article (car pslist)))
11760                                   (gnus-summary-article-number)))
11761           (gnus-summary-goto-subject after-article)
11762           (forward-line 1)
11763           (setq b (point))
11764           (insert "    " (file-name-nondirectory
11765                           (cdr (assq 'name (car pslist))))
11766                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11767           (setq e (point))
11768           (forward-line -1)             ; back to `b'
11769           (gnus-add-text-properties
11770            b (1- e) (list 'gnus-number gnus-reffed-article-number
11771                           gnus-mouse-face-prop gnus-mouse-face))
11772           (gnus-data-enter
11773            after-article gnus-reffed-article-number
11774            gnus-unread-mark b (car pslist) 0 (- e b))
11775           (setq gnus-newsgroup-unreads
11776                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11777                                          gnus-reffed-article-number))
11778           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11779           (setq pslist (cdr pslist)))))))
11780
11781 (defun gnus-pseudos< (p1 p2)
11782   (let ((c1 (cdr (assq 'action p1)))
11783         (c2 (cdr (assq 'action p2))))
11784     (and c1 c2 (string< c1 c2))))
11785
11786 (defun gnus-request-pseudo-article (props)
11787   (cond ((assq 'execute props)
11788          (gnus-execute-command (cdr (assq 'execute props)))))
11789   (let ((gnus-current-article (gnus-summary-article-number)))
11790     (gnus-run-hooks 'gnus-mark-article-hook)))
11791
11792 (defun gnus-execute-command (command &optional automatic)
11793   (save-excursion
11794     (gnus-article-setup-buffer)
11795     (set-buffer gnus-article-buffer)
11796     (setq buffer-read-only nil)
11797     (let ((command (if automatic command
11798                      (read-string "Command: " (cons command 0)))))
11799       (erase-buffer)
11800       (insert "$ " command "\n\n")
11801       (if gnus-view-pseudo-asynchronously
11802           (start-process "gnus-execute" (current-buffer) shell-file-name
11803                          shell-command-switch command)
11804         (call-process shell-file-name nil t nil
11805                       shell-command-switch command)))))
11806
11807 ;; Summary kill commands.
11808
11809 (defun gnus-summary-edit-global-kill (article)
11810   "Edit the \"global\" kill file."
11811   (interactive (list (gnus-summary-article-number)))
11812   (gnus-group-edit-global-kill article))
11813
11814 (defun gnus-summary-edit-local-kill ()
11815   "Edit a local kill file applied to the current newsgroup."
11816   (interactive)
11817   (setq gnus-current-headers (gnus-summary-article-header))
11818   (gnus-group-edit-local-kill
11819    (gnus-summary-article-number) gnus-newsgroup-name))
11820
11821 ;;; Header reading.
11822
11823 (defun gnus-read-header (id &optional header)
11824   "Read the headers of article ID and enter them into the Gnus system."
11825   (let ((group gnus-newsgroup-name)
11826         (gnus-override-method
11827          (or
11828           gnus-override-method
11829           (and (gnus-news-group-p gnus-newsgroup-name)
11830                (car (gnus-refer-article-methods)))))
11831         where)
11832     ;; First we check to see whether the header in question is already
11833     ;; fetched.
11834     (if (stringp id)
11835         ;; This is a Message-ID.
11836         (setq header (or header (gnus-id-to-header id)))
11837       ;; This is an article number.
11838       (setq header (or header (gnus-summary-article-header id))))
11839     (if (and header
11840              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11841         ;; We have found the header.
11842         header
11843       ;; We have to really fetch the header to this article.
11844       (save-excursion
11845         (set-buffer nntp-server-buffer)
11846         (when (setq where (gnus-request-head id group))
11847           (nnheader-fold-continuation-lines)
11848           (goto-char (point-max))
11849           (insert ".\n")
11850           (goto-char (point-min))
11851           (insert "211 ")
11852           (princ (cond
11853                   ((numberp id) id)
11854                   ((cdr where) (cdr where))
11855                   (header (mail-header-number header))
11856                   (t gnus-reffed-article-number))
11857                  (current-buffer))
11858           (insert " Article retrieved.\n"))
11859         (if (or (not where)
11860                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11861             ()                          ; Malformed head.
11862           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11863             (when (and (stringp id)
11864                        (or
11865                         (not (string= (gnus-group-real-name group)
11866                                       (car where)))
11867                         (not (gnus-server-equal gnus-override-method
11868                                                 (gnus-group-method group)))))
11869               ;; If we fetched by Message-ID and the article came from
11870               ;; a different group (or server), we fudge some bogus
11871               ;; article numbers for this article.
11872               (mail-header-set-number header gnus-reffed-article-number))
11873             (save-excursion
11874               (set-buffer gnus-summary-buffer)
11875               (decf gnus-reffed-article-number)
11876               (gnus-remove-header (mail-header-number header))
11877               (push header gnus-newsgroup-headers)
11878               (setq gnus-current-headers header)
11879               (push (mail-header-number header) gnus-newsgroup-limit)))
11880           header)))))
11881
11882 (defun gnus-remove-header (number)
11883   "Remove header NUMBER from `gnus-newsgroup-headers'."
11884   (if (and gnus-newsgroup-headers
11885            (= number (mail-header-number (car gnus-newsgroup-headers))))
11886       (pop gnus-newsgroup-headers)
11887     (let ((headers gnus-newsgroup-headers))
11888       (while (and (cdr headers)
11889                   (not (= number (mail-header-number (cadr headers)))))
11890         (pop headers))
11891       (when (cdr headers)
11892         (setcdr headers (cddr headers))))))
11893
11894 ;;;
11895 ;;; summary highlights
11896 ;;;
11897
11898 (defun gnus-highlight-selected-summary ()
11899   "Highlight selected article in summary buffer."
11900   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11901   (when gnus-summary-selected-face
11902     (save-excursion
11903       (let* ((beg (point-at-bol))
11904              (end (point-at-eol))
11905              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11906              (from (if (get-text-property beg gnus-mouse-face-prop)
11907                        beg
11908                      (or (next-single-property-change
11909                           beg gnus-mouse-face-prop nil end)
11910                          beg)))
11911              (to
11912               (if (= from end)
11913                   (- from 2)
11914                 (or (next-single-property-change
11915                      from gnus-mouse-face-prop nil end)
11916                     end))))
11917         ;; If no mouse-face prop on line we will have to = from = end,
11918         ;; so we highlight the entire line instead.
11919         (when (= (+ to 2) from)
11920           (setq from beg)
11921           (setq to end))
11922         (if gnus-newsgroup-selected-overlay
11923             ;; Move old overlay.
11924             (gnus-move-overlay
11925              gnus-newsgroup-selected-overlay from to (current-buffer))
11926           ;; Create new overlay.
11927           (gnus-overlay-put
11928            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11929            'face gnus-summary-selected-face))))))
11930
11931 (defvar gnus-summary-highlight-line-cached nil)
11932 (defvar gnus-summary-highlight-line-trigger nil)
11933
11934 (defun gnus-summary-highlight-line-0 ()
11935   (if (and (eq gnus-summary-highlight-line-trigger
11936                gnus-summary-highlight)
11937            gnus-summary-highlight-line-cached)
11938       gnus-summary-highlight-line-cached
11939     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11940           gnus-summary-highlight-line-cached
11941           (let* ((cond (list 'cond))
11942                  (c cond)
11943                  (list gnus-summary-highlight))
11944             (while list
11945               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11946                               nil))
11947               (setq c (cdr c)
11948                     list (cdr list)))
11949             (gnus-byte-compile (list 'lambda nil cond))))))
11950
11951 (defun gnus-summary-highlight-line ()
11952   "Highlight current line according to `gnus-summary-highlight'."
11953   (let* ((beg (point-at-bol))
11954          (article (or (gnus-summary-article-number) gnus-current-article))
11955          (score (or (cdr (assq article
11956                                gnus-newsgroup-scored))
11957                     gnus-summary-default-score 0))
11958          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11959          (inhibit-read-only t)
11960          (default gnus-summary-default-score)
11961          (default-high gnus-summary-default-high-score)
11962          (default-low gnus-summary-default-low-score)
11963          (uncached (and gnus-summary-use-undownloaded-faces
11964                         (memq article gnus-newsgroup-undownloaded)
11965                         (not (memq article gnus-newsgroup-cached)))))
11966     (let ((face (funcall (gnus-summary-highlight-line-0))))
11967       (unless (eq face (get-text-property beg 'face))
11968         (gnus-put-text-property-excluding-characters-with-faces
11969          beg (point-at-eol) 'face
11970          (setq face (if (boundp face) (symbol-value face) face)))
11971         (when gnus-summary-highlight-line-function
11972           (funcall gnus-summary-highlight-line-function article face))))))
11973
11974 (defun gnus-update-read-articles (group unread &optional compute)
11975   "Update the list of read articles in GROUP.
11976 UNREAD is a sorted list."
11977   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11978         (info (gnus-get-info group))
11979         (prev 1)
11980         read)
11981     (if (or (not info) (not active))
11982         ;; There is no info on this group if it was, in fact,
11983         ;; killed.  Gnus stores no information on killed groups, so
11984         ;; there's nothing to be done.
11985         ;; One could store the information somewhere temporarily,
11986         ;; perhaps...  Hmmm...
11987         ()
11988       ;; Remove any negative articles numbers.
11989       (while (and unread (< (car unread) 0))
11990         (setq unread (cdr unread)))
11991       ;; Remove any expired article numbers
11992       (while (and unread (< (car unread) (car active)))
11993         (setq unread (cdr unread)))
11994       ;; Compute the ranges of read articles by looking at the list of
11995       ;; unread articles.
11996       (while unread
11997         (when (/= (car unread) prev)
11998           (push (if (= prev (1- (car unread))) prev
11999                   (cons prev (1- (car unread))))
12000                 read))
12001         (setq prev (1+ (car unread)))
12002         (setq unread (cdr unread)))
12003       (when (<= prev (cdr active))
12004         (push (cons prev (cdr active)) read))
12005       (setq read (if (> (length read) 1) (nreverse read) read))
12006       (if compute
12007           read
12008         (save-excursion
12009           (let (setmarkundo)
12010             ;; Propagate the read marks to the backend.
12011             (when (gnus-check-backend-function 'request-set-mark group)
12012               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12013                     (add (gnus-remove-from-range read (gnus-info-read info))))
12014                 (when (or add del)
12015                   (unless (gnus-check-group group)
12016                     (error "Can't open server for %s" group))
12017                   (gnus-request-set-mark
12018                    group (delq nil (list (if add (list add 'add '(read)))
12019                                          (if del (list del 'del '(read))))))
12020                   (setq setmarkundo
12021                         `(gnus-request-set-mark
12022                           ,group
12023                           ',(delq nil (list
12024                                        (if del (list del 'add '(read)))
12025                                        (if add (list add 'del '(read))))))))))
12026             (set-buffer gnus-group-buffer)
12027             (gnus-undo-register
12028               `(progn
12029                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12030                  (gnus-info-set-read ',info ',(gnus-info-read info))
12031                  (gnus-get-unread-articles-in-group ',info
12032                                                     (gnus-active ,group))
12033                  (gnus-group-update-group ,group t)
12034                  ,setmarkundo))))
12035         ;; Enter this list into the group info.
12036         (gnus-info-set-read info read)
12037         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12038         (gnus-get-unread-articles-in-group info (gnus-active group))
12039         t))))
12040
12041 (defun gnus-offer-save-summaries ()
12042   "Offer to save all active summary buffers."
12043   (let (buffers)
12044     ;; Go through all buffers and find all summaries.
12045     (dolist (buffer (buffer-list))
12046       (when (and (setq buffer (buffer-name buffer))
12047                  (string-match "Summary" buffer)
12048                  (with-current-buffer buffer
12049                    ;; We check that this is, indeed, a summary buffer.
12050                    (and (eq major-mode 'gnus-summary-mode)
12051                         ;; Also make sure this isn't bogus.
12052                         gnus-newsgroup-prepared
12053                         ;; Also make sure that this isn't a
12054                         ;; dead summary buffer.
12055                         (not gnus-dead-summary-mode))))
12056         (push buffer buffers)))
12057     ;; Go through all these summary buffers and offer to save them.
12058     (when buffers
12059       (save-excursion
12060         (map-y-or-n-p
12061          "Update summary buffer %s? "
12062          (lambda (buf)
12063            (switch-to-buffer buf)
12064            (gnus-summary-exit))
12065          buffers)))))
12066
12067 (defun gnus-summary-setup-default-charset ()
12068   "Setup newsgroup default charset."
12069   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12070       (setq gnus-newsgroup-charset nil)
12071     (let* ((ignored-charsets
12072             (or gnus-newsgroup-ephemeral-ignored-charsets
12073                 (append
12074                  (and gnus-newsgroup-name
12075                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12076                  gnus-newsgroup-ignored-charsets))))
12077       (setq gnus-newsgroup-charset
12078             (or gnus-newsgroup-ephemeral-charset
12079                 (and gnus-newsgroup-name
12080                      (gnus-parameter-charset gnus-newsgroup-name))
12081                 gnus-default-charset))
12082       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12083            ignored-charsets))))
12084
12085 ;;;
12086 ;;; Mime Commands
12087 ;;;
12088
12089 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12090   "Display the current article buffer fully MIME-buttonized.
12091 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12092 treated as multipart/mixed."
12093   (interactive "P")
12094   (require 'gnus-art)
12095   (let ((gnus-unbuttonized-mime-types nil)
12096         (gnus-mime-display-multipart-as-mixed show-all-parts))
12097     (gnus-summary-show-article)))
12098
12099 (defun gnus-summary-repair-multipart (article)
12100   "Add a Content-Type header to a multipart article without one."
12101   (interactive (list (gnus-summary-article-number)))
12102   (gnus-with-article article
12103     (message-narrow-to-head)
12104     (message-remove-header "Mime-Version")
12105     (goto-char (point-max))
12106     (insert "Mime-Version: 1.0\n")
12107     (widen)
12108     (when (search-forward "\n--" nil t)
12109       (let ((separator (buffer-substring (point) (point-at-eol))))
12110         (message-narrow-to-head)
12111         (message-remove-header "Content-Type")
12112         (goto-char (point-max))
12113         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12114                         separator))
12115         (widen))))
12116   (let (gnus-mark-article-hook)
12117     (gnus-summary-select-article t t nil article)))
12118
12119 (defun gnus-summary-toggle-display-buttonized ()
12120   "Toggle the buttonizing of the article buffer."
12121   (interactive)
12122   (require 'gnus-art)
12123   (if (setq gnus-inhibit-mime-unbuttonizing
12124             (not gnus-inhibit-mime-unbuttonizing))
12125       (let ((gnus-unbuttonized-mime-types nil))
12126         (gnus-summary-show-article))
12127     (gnus-summary-show-article)))
12128
12129 ;;;
12130 ;;; Generic summary marking commands
12131 ;;;
12132
12133 (defvar gnus-summary-marking-alist
12134   '((read gnus-del-mark "d")
12135     (unread gnus-unread-mark "u")
12136     (ticked gnus-ticked-mark "!")
12137     (dormant gnus-dormant-mark "?")
12138     (expirable gnus-expirable-mark "e"))
12139   "An alist of names/marks/keystrokes.")
12140
12141 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12142 (defvar gnus-summary-mark-map)
12143
12144 (defun gnus-summary-make-all-marking-commands ()
12145   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12146   (dolist (elem gnus-summary-marking-alist)
12147     (apply 'gnus-summary-make-marking-command elem)))
12148
12149 (defun gnus-summary-make-marking-command (name mark keystroke)
12150   (let ((map (make-sparse-keymap)))
12151     (define-key gnus-summary-generic-mark-map keystroke map)
12152     (dolist (lway `((next "next" next nil "n")
12153                     (next-unread "next unread" next t "N")
12154                     (prev "previous" prev nil "p")
12155                     (prev-unread "previous unread" prev t "P")
12156                     (nomove "" nil nil ,keystroke)))
12157       (let ((func (gnus-summary-make-marking-command-1
12158                    mark (car lway) lway name)))
12159         (setq func (eval func))
12160         (define-key map (nth 4 lway) func)))))
12161
12162 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12163   `(defun ,(intern
12164             (format "gnus-summary-put-mark-as-%s%s"
12165                     name (if (eq way 'nomove)
12166                              ""
12167                            (concat "-" (symbol-name way)))))
12168      (n)
12169      ,(format
12170        "Mark the current article as %s%s.
12171 If N, the prefix, then repeat N times.
12172 If N is negative, move in reverse order.
12173 The difference between N and the actual number of articles marked is
12174 returned."
12175        name (cadr lway))
12176      (interactive "p")
12177      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12178
12179 (defun gnus-summary-generic-mark (n mark move unread)
12180   "Mark N articles with MARK."
12181   (unless (eq major-mode 'gnus-summary-mode)
12182     (error "This command can only be used in the summary buffer"))
12183   (gnus-summary-show-thread)
12184   (let ((nummove
12185          (cond
12186           ((eq move 'next) 1)
12187           ((eq move 'prev) -1)
12188           (t 0))))
12189     (if (zerop nummove)
12190         (setq n 1)
12191       (when (< n 0)
12192         (setq n (abs n)
12193               nummove (* -1 nummove))))
12194     (while (and (> n 0)
12195                 (gnus-summary-mark-article nil mark)
12196                 (zerop (gnus-summary-next-subject nummove unread t)))
12197       (setq n (1- n)))
12198     (when (/= 0 n)
12199       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12200     (gnus-summary-recenter)
12201     (gnus-summary-position-point)
12202     (gnus-set-mode-line 'summary)
12203     n))
12204
12205 (defun gnus-summary-insert-articles (articles)
12206   (when (setq articles
12207               (gnus-sorted-difference articles
12208                                       (mapcar (lambda (h)
12209                                                 (mail-header-number h))
12210                                               gnus-newsgroup-headers)))
12211     (setq gnus-newsgroup-headers
12212           (gnus-merge 'list
12213                       gnus-newsgroup-headers
12214                       (gnus-fetch-headers articles)
12215                       'gnus-article-sort-by-number))
12216     ;; Suppress duplicates?
12217     (when gnus-suppress-duplicates
12218       (gnus-dup-suppress-articles))
12219
12220     ;; We might want to build some more threads first.
12221     (when (and gnus-fetch-old-headers
12222                (eq gnus-headers-retrieved-by 'nov))
12223       (if (eq gnus-fetch-old-headers 'invisible)
12224           (gnus-build-all-threads)
12225         (gnus-build-old-threads)))
12226     ;; Let the Gnus agent mark articles as read.
12227     (when gnus-agent
12228       (gnus-agent-get-undownloaded-list))
12229     ;; Remove list identifiers from subject
12230     (when gnus-list-identifiers
12231       (gnus-summary-remove-list-identifiers))
12232     ;; First and last article in this newsgroup.
12233     (when gnus-newsgroup-headers
12234       (setq gnus-newsgroup-begin
12235             (mail-header-number (car gnus-newsgroup-headers))
12236             gnus-newsgroup-end
12237             (mail-header-number
12238              (gnus-last-element gnus-newsgroup-headers))))
12239     (when gnus-use-scoring
12240       (gnus-possibly-score-headers))))
12241
12242 (defun gnus-summary-insert-old-articles (&optional all)
12243   "Insert all old articles in this group.
12244 If ALL is non-nil, already read articles become readable.
12245 If ALL is a number, fetch this number of articles."
12246   (interactive "P")
12247   (prog1
12248       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12249             older len)
12250         (setq older
12251               ;; Some nntp servers lie about their active range.  When
12252               ;; this happens, the active range can be in the millions.
12253               ;; Use a compressed range to avoid creating a huge list.
12254               (gnus-range-difference (list gnus-newsgroup-active) old))
12255         (setq len (gnus-range-length older))
12256         (cond
12257          ((null older) nil)
12258          ((numberp all)
12259           (if (< all len)
12260               (let ((older-range (nreverse older)))
12261                 (setq older nil)
12262
12263                 (while (> all 0)
12264                   (let* ((r (pop older-range))
12265                          (min (if (numberp r) r (car r)))
12266                          (max (if (numberp r) r (cdr r))))
12267                     (while (and (<= min max)
12268                                 (> all 0))
12269                       (push max older)
12270                       (setq all (1- all)
12271                             max (1- max))))))
12272             (setq older (gnus-uncompress-range older))))
12273          (all
12274           (setq older (gnus-uncompress-range older)))
12275          (t
12276           (when (and (numberp gnus-large-newsgroup)
12277                    (> len gnus-large-newsgroup))
12278               (let* ((cursor-in-echo-area nil)
12279                      (initial (gnus-parameter-large-newsgroup-initial
12280                                gnus-newsgroup-name))
12281                      (input
12282                       (read-string
12283                        (format
12284                         "How many articles from %s (%s %d): "
12285                         (gnus-limit-string
12286                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12287                         (if initial "max" "default")
12288                         len)
12289                        (if initial
12290                            (cons (number-to-string initial)
12291                                  0)))))
12292                 (unless (string-match "^[ \t]*$" input)
12293                   (setq all (string-to-number input))
12294                   (if (< all len)
12295                       (let ((older-range (nreverse older)))
12296                         (setq older nil)
12297
12298                         (while (> all 0)
12299                           (let* ((r (pop older-range))
12300                                  (min (if (numberp r) r (car r)))
12301                                  (max (if (numberp r) r (cdr r))))
12302                             (while (and (<= min max)
12303                                         (> all 0))
12304                               (push max older)
12305                               (setq all (1- all)
12306                                     max (1- max))))))))))
12307           (setq older (gnus-uncompress-range older))))
12308         (if (not older)
12309             (message "No old news.")
12310           (gnus-summary-insert-articles older)
12311           (gnus-summary-limit (gnus-sorted-nunion old older))))
12312     (gnus-summary-position-point)))
12313
12314 (defun gnus-summary-insert-new-articles ()
12315   "Insert all new articles in this group."
12316   (interactive)
12317   (prog1
12318       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12319             (old-active gnus-newsgroup-active)
12320             (nnmail-fetched-sources (list t))
12321             i new)
12322         (setq gnus-newsgroup-active
12323               (gnus-activate-group gnus-newsgroup-name 'scan))
12324         (setq i (cdr gnus-newsgroup-active))
12325         (while (> i (cdr old-active))
12326           (push i new)
12327           (decf i))
12328         (if (not new)
12329             (message "No gnus is bad news")
12330           (gnus-summary-insert-articles new)
12331           (setq gnus-newsgroup-unreads
12332                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12333           (gnus-summary-limit (gnus-sorted-nunion old new))))
12334     (gnus-summary-position-point)))
12335
12336 (gnus-summary-make-all-marking-commands)
12337
12338 (gnus-ems-redefine)
12339
12340 (provide 'gnus-sum)
12341
12342 (run-hooks 'gnus-sum-load-hook)
12343
12344 ;; Local Variables:
12345 ;; coding: iso-8859-1
12346 ;; End:
12347
12348 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12349 ;;; gnus-sum.el ends here